1 /* $Id: isdn_x25iface.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
3 * Linux ISDN subsystem, X.25 related functions
5 * This software may be used and distributed according to the terms
6 * of the GNU General Public License, incorporated herein by reference.
8 * stuff needed to support the Linux X.25 PLP code on top of devices that
9 * can provide a lab_b service using the concap_proto mechanism.
10 * This module supports a network interface which provides lapb_sematics
11 * -- as defined in Documentation/networking/x25-iface.txt -- to
12 * the upper layer and assumes that the lower layer provides a reliable
13 * data link service by means of the concap_device_ops callbacks.
15 * Only protocol specific stuff goes here. Device specific stuff
16 * goes to another -- device related -- concap_proto support source file.
20 /* #include <linux/isdn.h> */
21 #include <linux/netdevice.h>
22 #include <linux/concap.h>
23 #include <linux/slab.h>
24 #include <linux/wanrouter.h>
25 #include <net/x25device.h>
26 #include "isdn_x25iface.h"
28 /* for debugging messages not to cause an oops when device pointer is NULL*/
29 #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
32 typedef struct isdn_x25iface_proto_data
{
34 enum wan_states state
;
35 /* Private stuff, not to be accessed via proto_data. We provide the
36 other storage for the concap_proto instance here as well,
37 enabling us to allocate both with just one kmalloc(): */
38 struct concap_proto priv
;
43 /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
44 static void isdn_x25iface_proto_del( struct concap_proto
* );
45 static int isdn_x25iface_proto_close( struct concap_proto
* );
46 static int isdn_x25iface_proto_restart( struct concap_proto
*,
48 struct concap_device_ops
*);
49 static int isdn_x25iface_xmit( struct concap_proto
*, struct sk_buff
* );
50 static int isdn_x25iface_receive( struct concap_proto
*, struct sk_buff
* );
51 static int isdn_x25iface_connect_ind( struct concap_proto
* );
52 static int isdn_x25iface_disconn_ind( struct concap_proto
* );
55 static struct concap_proto_ops ix25_pops
= {
56 &isdn_x25iface_proto_new
,
57 &isdn_x25iface_proto_del
,
58 &isdn_x25iface_proto_restart
,
59 &isdn_x25iface_proto_close
,
61 &isdn_x25iface_receive
,
62 &isdn_x25iface_connect_ind
,
63 &isdn_x25iface_disconn_ind
66 /* error message helper function */
67 static void illegal_state_warn( unsigned state
, unsigned char firstbyte
)
69 printk( KERN_WARNING
"isdn_x25iface: firstbyte %x illegal in"
70 "current state %d\n",firstbyte
, state
);
73 /* check protocol data field for consistency */
74 static int pdata_is_bad( ix25_pdata_t
* pda
){
76 if( pda
&& pda
-> magic
== ISDN_X25IFACE_MAGIC
) return 0;
78 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
82 /* create a new x25 interface protocol instance
84 struct concap_proto
* isdn_x25iface_proto_new(void)
86 ix25_pdata_t
* tmp
= kmalloc(sizeof(ix25_pdata_t
),GFP_KERNEL
);
87 IX25DEBUG("isdn_x25iface_proto_new\n");
89 tmp
-> magic
= ISDN_X25IFACE_MAGIC
;
90 tmp
-> state
= WAN_UNCONFIGURED
;
91 /* private data space used to hold the concap_proto data.
92 Only to be accessed via the returned pointer */
93 spin_lock_init(&tmp
->priv
.lock
);
94 tmp
-> priv
.dops
= NULL
;
95 tmp
-> priv
.net_dev
= NULL
;
96 tmp
-> priv
.pops
= &ix25_pops
;
97 tmp
-> priv
.flags
= 0;
98 tmp
-> priv
.proto_data
= tmp
;
99 return( &(tmp
-> priv
) );
104 /* close the x25iface encapsulation protocol
106 static int isdn_x25iface_proto_close(struct concap_proto
*cprot
){
113 printk( KERN_ERR
"isdn_x25iface_proto_close: "
114 "invalid concap_proto pointer\n" );
117 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot
-> net_dev
) );
118 spin_lock_irqsave(&cprot
->lock
, flags
);
119 cprot
-> dops
= NULL
;
120 cprot
-> net_dev
= NULL
;
121 tmp
= cprot
-> proto_data
;
122 if( pdata_is_bad( tmp
) ){
125 tmp
-> state
= WAN_UNCONFIGURED
;
127 spin_unlock_irqrestore(&cprot
->lock
, flags
);
131 /* Delete the x25iface encapsulation protocol instance
133 static void isdn_x25iface_proto_del(struct concap_proto
*cprot
){
137 IX25DEBUG( "isdn_x25iface_proto_del \n" );
139 printk( KERN_ERR
"isdn_x25iface_proto_del: "
140 "concap_proto pointer is NULL\n" );
143 tmp
= cprot
-> proto_data
;
145 printk( KERN_ERR
"isdn_x25iface_proto_del: inconsistent "
146 "proto_data pointer (maybe already deleted?)\n");
149 /* close if the protocol is still open */
150 if( cprot
-> dops
) isdn_x25iface_proto_close(cprot
);
151 /* freeing the storage should be sufficient now. But some additional
152 settings might help to catch wild pointer bugs */
154 cprot
-> proto_data
= NULL
;
160 /* (re-)initialize the data structures for x25iface encapsulation
162 static int isdn_x25iface_proto_restart(struct concap_proto
*cprot
,
163 struct net_device
*ndev
,
164 struct concap_device_ops
*dops
)
166 ix25_pdata_t
* pda
= cprot
-> proto_data
;
169 IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev
) );
171 if ( pdata_is_bad( pda
) ) return -1;
173 if( !( dops
&& dops
-> data_req
&& dops
-> connect_req
174 && dops
-> disconn_req
) ){
175 printk( KERN_WARNING
"isdn_x25iface_restart: required dops"
177 isdn_x25iface_proto_close(cprot
);
180 spin_lock_irqsave(&cprot
->lock
, flags
);
181 cprot
-> net_dev
= ndev
;
182 cprot
-> pops
= &ix25_pops
;
183 cprot
-> dops
= dops
;
184 pda
-> state
= WAN_DISCONNECTED
;
185 spin_unlock_irqrestore(&cprot
->lock
, flags
);
189 /* deliver a dl_data frame received from i4l HL driver to the network layer
191 static int isdn_x25iface_receive(struct concap_proto
*cprot
, struct sk_buff
*skb
)
193 IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot
->net_dev
) );
194 if ( ( (ix25_pdata_t
*) (cprot
->proto_data
) )
195 -> state
== WAN_CONNECTED
){
196 if( skb_push(skb
, 1)){
198 skb
->protocol
= x25_type_trans(skb
, cprot
->net_dev
);
203 printk(KERN_WARNING
"isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot
->net_dev
) );
208 /* a connection set up is indicated by lower layer
210 static int isdn_x25iface_connect_ind(struct concap_proto
*cprot
)
212 struct sk_buff
* skb
;
213 enum wan_states
*state_p
214 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
215 IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
216 , MY_DEVNAME(cprot
->net_dev
) );
217 if( *state_p
== WAN_UNCONFIGURED
){
219 "isdn_x25iface_connect_ind while unconfigured %s\n"
220 , MY_DEVNAME(cprot
->net_dev
) );
223 *state_p
= WAN_CONNECTED
;
225 skb
= dev_alloc_skb(1);
227 *( skb_put(skb
, 1) ) = 0x01;
228 skb
->protocol
= x25_type_trans(skb
, cprot
->net_dev
);
232 printk(KERN_WARNING
"isdn_x25iface_connect_ind: "
233 " out of memory -- disconnecting\n");
234 cprot
-> dops
-> disconn_req(cprot
);
239 /* a disconnect is indicated by lower layer
241 static int isdn_x25iface_disconn_ind(struct concap_proto
*cprot
)
244 enum wan_states
*state_p
245 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
246 IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot
-> net_dev
) );
247 if( *state_p
== WAN_UNCONFIGURED
){
249 "isdn_x25iface_disconn_ind while unconfigured\n");
252 if(! cprot
-> net_dev
) return -1;
253 *state_p
= WAN_DISCONNECTED
;
254 skb
= dev_alloc_skb(1);
256 *( skb_put(skb
, 1) ) = 0x02;
257 skb
->protocol
= x25_type_trans(skb
, cprot
->net_dev
);
261 printk(KERN_WARNING
"isdn_x25iface_disconn_ind:"
267 /* process a frame handed over to us from linux network layer. First byte
268 semantics as defined in Documentation/networking/x25-iface.txt
270 static int isdn_x25iface_xmit(struct concap_proto
*cprot
, struct sk_buff
*skb
)
272 unsigned char firstbyte
= skb
->data
[0];
273 enum wan_states
*state
= &((ix25_pdata_t
*)cprot
->proto_data
)->state
;
275 IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot
-> net_dev
), firstbyte
, *state
);
276 switch ( firstbyte
){
277 case 0x00: /* dl_data request */
278 if( *state
== WAN_CONNECTED
){
280 cprot
-> net_dev
-> trans_start
= jiffies
;
281 ret
= ( cprot
-> dops
-> data_req(cprot
, skb
) );
282 /* prepare for future retransmissions */
283 if( ret
) skb_push(skb
,1);
286 illegal_state_warn( *state
, firstbyte
);
288 case 0x01: /* dl_connect request */
289 if( *state
== WAN_DISCONNECTED
){
290 *state
= WAN_CONNECTING
;
291 ret
= cprot
-> dops
-> connect_req(cprot
);
293 /* reset state and notify upper layer about
294 * immidiatly failed attempts */
295 isdn_x25iface_disconn_ind(cprot
);
298 illegal_state_warn( *state
, firstbyte
);
301 case 0x02: /* dl_disconnect request */
303 case WAN_DISCONNECTED
:
304 /* Should not happen. However, give upper layer a
305 chance to recover from inconstistency but don't
306 trust the lower layer sending the disconn_confirm
307 when already disconnected */
308 printk(KERN_WARNING
"isdn_x25iface_xmit: disconnect "
309 " requested while disconnected\n" );
310 isdn_x25iface_disconn_ind(cprot
);
311 break; /* prevent infinite loops */
314 *state
= WAN_DISCONNECTED
;
315 cprot
-> dops
-> disconn_req(cprot
);
318 illegal_state_warn( *state
, firstbyte
);
321 case 0x03: /* changing lapb parameters requested */
322 printk(KERN_WARNING
"isdn_x25iface_xmit: setting of lapb"
323 " options not yet supported\n");
326 printk(KERN_WARNING
"isdn_x25iface_xmit: frame with illegal"
327 " first byte %x ignored:\n", firstbyte
);