1 /* $Id: isdn_x25iface.c,v 1.7 1999/08/22 20:26:13 calle Exp $
2 * stuff needed to support the Linux X.25 PLP code on top of devices that
3 * can provide a lab_b service using the concap_proto mechanism.
4 * This module supports a network interface wich provides lapb_sematics
5 * -- as defined in ../../Documentation/networking/x25-iface.txt -- to
6 * the upper layer and assumes that the lower layer provides a reliable
7 * data link service by means of the concap_device_ops callbacks.
9 * Only protocol specific stuff goes here. Device specific stuff
10 * goes to another -- device related -- concap_proto support source file.
12 * $Log: isdn_x25iface.c,v $
13 * Revision 1.7 1999/08/22 20:26:13 calle
14 * backported changes from kernel 2.3.14:
15 * - several #include "config.h" gone, others come.
16 * - "struct device" changed to "struct net_device" in 2.3.14, added a
17 * define in isdn_compat.h for older kernel versions.
19 * Revision 1.6 1999/01/27 22:53:19 he
20 * minor updates (spellings, jiffies wrap around in isdn_tty)
22 * Revision 1.5 1998/10/30 17:55:39 he
23 * dialmode for x25iface and multulink ppp
25 * Revision 1.4 1998/06/17 19:51:00 he
26 * merged with 2.1.10[34] (cosmetics and udelay() -> mdelay())
27 * brute force fix to avoid Ugh's in isdn_tty_write()
28 * cleaned up some dead code
30 * Revision 1.3 1998/02/20 17:25:20 fritz
31 * Changes for recent kernels.
33 * Revision 1.2 1998/01/31 22:49:22 keil
36 * Revision 1.1 1998/01/31 22:27:58 keil
37 * New files from Henner Eisen for X.25 support
41 /* #include <linux/isdn.h> */
42 #include <linux/netdevice.h>
43 #include <linux/concap.h>
44 #include <linux/wanrouter.h>
45 #include "isdn_x25iface.h"
47 /* for debugging messages not to cause an oops when device pointer is NULL*/
48 #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
51 typedef struct isdn_x25iface_proto_data
{
53 enum wan_states state
;
54 /* Private stuff, not to be accessed via proto_data. We provide the
55 other storage for the concap_proto instance here as well,
56 enabling us to allocate both with just one kmalloc(): */
57 struct concap_proto priv
;
62 /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
63 void isdn_x25iface_proto_del( struct concap_proto
* );
64 int isdn_x25iface_proto_close( struct concap_proto
* );
65 int isdn_x25iface_proto_restart( struct concap_proto
*,
67 struct concap_device_ops
*);
68 int isdn_x25iface_xmit( struct concap_proto
*, struct sk_buff
* );
69 int isdn_x25iface_receive( struct concap_proto
*, struct sk_buff
* );
70 int isdn_x25iface_connect_ind( struct concap_proto
* );
71 int isdn_x25iface_disconn_ind( struct concap_proto
* );
74 static struct concap_proto_ops ix25_pops
= {
75 &isdn_x25iface_proto_new
,
76 &isdn_x25iface_proto_del
,
77 &isdn_x25iface_proto_restart
,
78 &isdn_x25iface_proto_close
,
80 &isdn_x25iface_receive
,
81 &isdn_x25iface_connect_ind
,
82 &isdn_x25iface_disconn_ind
85 /* error message helper function */
86 static void illegal_state_warn( unsigned state
, unsigned char firstbyte
)
88 printk( KERN_WARNING
"isdn_x25iface: firstbyte %x illegal in"
89 "current state %d\n",firstbyte
, state
);
92 /* check protocol data field for consistency */
93 static int pdata_is_bad( ix25_pdata_t
* pda
){
95 if( pda
&& pda
-> magic
== ISDN_X25IFACE_MAGIC
) return 0;
97 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
101 /* create a new x25 interface protocol instance
103 struct concap_proto
* isdn_x25iface_proto_new()
105 ix25_pdata_t
* tmp
= kmalloc(sizeof(ix25_pdata_t
),GFP_KERNEL
);
106 IX25DEBUG("isdn_x25iface_proto_new\n");
108 tmp
-> magic
= ISDN_X25IFACE_MAGIC
;
109 tmp
-> state
= WAN_UNCONFIGURED
;
110 /* private data space used to hold the concap_proto data.
111 Only to be accessed via the returned pointer */
112 tmp
-> priv
.dops
= NULL
;
113 tmp
-> priv
.net_dev
= NULL
;
114 tmp
-> priv
.pops
= &ix25_pops
;
115 tmp
-> priv
.flags
= 0;
116 tmp
-> priv
.proto_data
= tmp
;
117 return( &(tmp
-> priv
) );
122 /* close the x25iface encapsulation protocol
124 int isdn_x25iface_proto_close(struct concap_proto
*cprot
){
131 printk( KERN_ERR
"isdn_x25iface_proto_close: "
132 "invalid concap_proto pointer\n" );
135 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot
-> net_dev
) );
137 cli(); /* avoid races with incoming events calling pops methods while
138 cprot members are inconsistent */
139 cprot
-> dops
= NULL
;
140 cprot
-> net_dev
= NULL
;
141 tmp
= cprot
-> proto_data
;
142 if( pdata_is_bad( tmp
) ){
145 tmp
-> state
= WAN_UNCONFIGURED
;
147 restore_flags(flags
);
152 /* Delete the x25iface encapsulation protocol instance
154 void isdn_x25iface_proto_del(struct concap_proto
*cprot
){
158 IX25DEBUG( "isdn_x25iface_proto_del \n" );
160 printk( KERN_ERR
"isdn_x25iface_proto_del: "
161 "concap_proto pointer is NULL\n" );
164 tmp
= cprot
-> proto_data
;
166 printk( KERN_ERR
"isdn_x25iface_proto_del: inconsistent "
167 "proto_data pointer (maybe already deleted?)\n");
170 /* close if the protocol is still open */
171 if( cprot
-> dops
) isdn_x25iface_proto_close(cprot
);
172 /* freeing the storage should be sufficient now. But some additional
173 settings might help to catch wild pointer bugs */
175 cprot
-> proto_data
= NULL
;
181 /* (re-)initialize the data structures for x25iface encapsulation
183 int isdn_x25iface_proto_restart(struct concap_proto
*cprot
,
184 struct net_device
*ndev
,
185 struct concap_device_ops
*dops
)
187 ix25_pdata_t
* pda
= cprot
-> proto_data
;
190 IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev
) );
192 if ( pdata_is_bad( pda
) ) return -1;
194 if( !( dops
&& dops
-> data_req
&& dops
-> connect_req
195 && dops
-> disconn_req
) ){
196 printk( KERN_WARNING
"isdn_x25iface_restart: required dops"
198 isdn_x25iface_proto_close(cprot
);
202 cli(); /* avoid races with incoming events calling pops methods while
203 cprot members are inconsistent */
204 cprot
-> net_dev
= ndev
;
205 cprot
-> pops
= &ix25_pops
;
206 cprot
-> dops
= dops
;
207 pda
-> state
= WAN_DISCONNECTED
;
208 restore_flags(flags
);
212 /* deliver a dl_data frame received from i4l HL driver to the network layer
214 int isdn_x25iface_receive(struct concap_proto
*cprot
, struct sk_buff
*skb
)
216 IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot
->net_dev
) );
217 if ( ( (ix25_pdata_t
*) (cprot
->proto_data
) )
218 -> state
== WAN_CONNECTED
){
219 skb
-> dev
= cprot
-> net_dev
;
220 skb
-> protocol
= htons(ETH_P_X25
);
221 skb
-> pkt_type
= PACKET_HOST
;
222 if( skb_push(skb
, 1)){
224 skb
-> mac
.raw
= skb
-> data
;
229 printk(KERN_WARNING
"isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot
->net_dev
) );
234 /* a connection set up is indicated by lower layer
236 int isdn_x25iface_connect_ind(struct concap_proto
*cprot
)
238 struct sk_buff
* skb
= dev_alloc_skb(1);
239 enum wan_states
*state_p
240 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
241 IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
242 , MY_DEVNAME(cprot
->net_dev
) );
243 if( *state_p
== WAN_UNCONFIGURED
){
245 "isdn_x25iface_connect_ind while unconfigured %s\n"
246 , MY_DEVNAME(cprot
->net_dev
) );
249 *state_p
= WAN_CONNECTED
;
251 *( skb_put(skb
, 1) ) = 0x01;
252 skb
-> mac
.raw
= skb
-> data
;
253 skb
-> dev
= cprot
-> net_dev
;
254 skb
-> protocol
= htons(ETH_P_X25
);
255 skb
-> pkt_type
= PACKET_HOST
;
259 printk(KERN_WARNING
"isdn_x25iface_connect_ind: "
260 " out of memory -- disconnecting\n");
261 cprot
-> dops
-> disconn_req(cprot
);
266 /* a disconnect is indicated by lower layer
268 int isdn_x25iface_disconn_ind(struct concap_proto
*cprot
)
271 enum wan_states
*state_p
272 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
273 IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot
-> net_dev
) );
274 if( *state_p
== WAN_UNCONFIGURED
){
276 "isdn_x25iface_disconn_ind while unconfigured\n");
279 if(! cprot
-> net_dev
) return -1;
280 *state_p
= WAN_DISCONNECTED
;
281 skb
= dev_alloc_skb(1);
283 *( skb_put(skb
, 1) ) = 0x02;
284 skb
-> mac
.raw
= skb
-> data
;
285 skb
-> dev
= cprot
-> net_dev
;
286 skb
-> protocol
= htons(ETH_P_X25
);
287 skb
-> pkt_type
= PACKET_HOST
;
291 printk(KERN_WARNING
"isdn_x25iface_disconn_ind:"
297 /* process a frame handed over to us from linux network layer. First byte
298 semantics as defined in ../../Documentation/networking/x25-iface.txt
300 int isdn_x25iface_xmit(struct concap_proto
*cprot
, struct sk_buff
*skb
)
302 unsigned char firstbyte
= skb
->data
[0];
304 &( ( (ix25_pdata_t
*) (cprot
-> proto_data
) ) -> state
);
306 IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot
-> net_dev
), firstbyte
, *state
);
307 switch ( firstbyte
){
308 case 0x00: /* dl_data request */
309 if( *state
== WAN_CONNECTED
){
311 cprot
-> net_dev
-> trans_start
= jiffies
;
312 ret
= ( cprot
-> dops
-> data_req(cprot
, skb
) );
313 /* prepare for future retransmissions */
314 if( ret
) skb_push(skb
,1);
317 illegal_state_warn( *state
, firstbyte
);
319 case 0x01: /* dl_connect request */
320 if( *state
== WAN_DISCONNECTED
){
321 *state
= WAN_CONNECTING
;
322 ret
= cprot
-> dops
-> connect_req(cprot
);
324 /* reset state and notify upper layer about
325 * immidiatly failed attempts */
326 isdn_x25iface_disconn_ind(cprot
);
329 illegal_state_warn( *state
, firstbyte
);
332 case 0x02: /* dl_disconnect request */
334 case WAN_DISCONNECTED
:
335 /* Should not happen. However, give upper layer a
336 chance to recover from inconstistency but don't
337 trust the lower layer sending the disconn_confirm
338 when already disconnected */
339 printk(KERN_WARNING
"isdn_x25iface_xmit: disconnect "
340 " requested while disconnected\n" );
341 isdn_x25iface_disconn_ind(cprot
);
342 break; /* prevent infinite loops */
345 *state
= WAN_DISCONNECTED
;
346 cprot
-> dops
-> disconn_req(cprot
);
349 illegal_state_warn( *state
, firstbyte
);
352 case 0x03: /* changing lapb parameters requested */
353 printk(KERN_WARNING
"isdn_x25iface_xmit: setting of lapb"
354 " options not yet supported\n");
357 printk(KERN_WARNING
"isdn_x25iface_xmit: frame with illegal"
358 " first byte %x ignored:\n", firstbyte
);