1 /* $Id: isdn_x25iface.c,v 1.3 1998/02/20 17:25:20 fritz 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.3 1998/02/20 17:25:20 fritz
14 * Changes for recent kernels.
16 * Revision 1.2 1998/01/31 22:49:22 keil
19 * Revision 1.1 1998/01/31 22:27:58 keil
20 * New files from Henner Eisen for X.25 support
24 /* #include <linux/isdn.h> */
25 #include <linux/netdevice.h>
26 #include <linux/concap.h>
27 #include <linux/wanrouter.h>
28 #include "isdn_x25iface.h"
30 /* for debugging messages not to cause an oops when device pointer is NULL*/
31 #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
34 typedef struct isdn_x25iface_proto_data
{
36 enum wan_states state
;
37 /* Private stuff, not to be accessed via proto_data. We provide the
38 other storage for the concap_proto instance here as well,
39 enabling us to allocate both with just one kmalloc(): */
40 struct concap_proto priv
;
45 /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
46 void isdn_x25iface_proto_del( struct concap_proto
* );
47 int isdn_x25iface_proto_close( struct concap_proto
* );
48 int isdn_x25iface_proto_restart( struct concap_proto
*,
50 struct concap_device_ops
*);
51 int isdn_x25iface_xmit( struct concap_proto
*, struct sk_buff
* );
52 int isdn_x25iface_receive( struct concap_proto
*, struct sk_buff
* );
53 int isdn_x25iface_connect_ind( struct concap_proto
* );
54 int isdn_x25iface_disconn_ind( struct concap_proto
* );
57 static struct concap_proto_ops ix25_pops
= {
58 &isdn_x25iface_proto_new
,
59 &isdn_x25iface_proto_del
,
60 &isdn_x25iface_proto_restart
,
61 &isdn_x25iface_proto_close
,
63 &isdn_x25iface_receive
,
64 &isdn_x25iface_connect_ind
,
65 &isdn_x25iface_disconn_ind
68 /* error message helper function */
69 static void illegal_state_warn( unsigned state
, unsigned char firstbyte
)
71 printk( KERN_WARNING
"isdn_x25iface: firstbyte %x illegal in"
72 "current state %d\n",firstbyte
, state
);
75 /* check protocol data field for consistency */
76 static int pdata_is_bad( ix25_pdata_t
* pda
){
78 if( pda
&& pda
-> magic
== ISDN_X25IFACE_MAGIC
) return 0;
80 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
84 /* create a new x25 interface protocol instance
86 struct concap_proto
* isdn_x25iface_proto_new()
88 ix25_pdata_t
* tmp
= kmalloc(sizeof(ix25_pdata_t
),GFP_KERNEL
);
89 IX25DEBUG("isdn_x25iface_proto_new\n");
91 tmp
-> magic
= ISDN_X25IFACE_MAGIC
;
92 tmp
-> state
= WAN_UNCONFIGURED
;
93 /* private data space used to hold the concap_proto data.
94 Only to be accessed via the returned pointer */
95 tmp
-> priv
.dops
= NULL
;
96 tmp
-> priv
.net_dev
= NULL
;
97 tmp
-> priv
.pops
= &ix25_pops
;
98 tmp
-> priv
.flags
= 0;
99 tmp
-> priv
.proto_data
= tmp
;
100 return( &(tmp
-> priv
) );
105 /* close the x25iface encapsulation protocol
107 int isdn_x25iface_proto_close(struct concap_proto
*cprot
){
114 printk( KERN_ERR
"isdn_x25iface_proto_close: "
115 "invalid concap_proto pointer\n" );
118 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot
-> net_dev
) );
120 cli(); /* avoid races with incoming events calling pops methods while
121 cprot members are inconsistent */
122 cprot
-> dops
= NULL
;
123 cprot
-> net_dev
= NULL
;
124 tmp
= cprot
-> proto_data
;
125 if( pdata_is_bad( tmp
) ){
128 tmp
-> state
= WAN_UNCONFIGURED
;
130 restore_flags(flags
);
135 /* Delete the x25iface encapsulation protocol instance
137 void isdn_x25iface_proto_del(struct concap_proto
*cprot
){
141 IX25DEBUG( "isdn_x25iface_proto_del \n" );
143 printk( KERN_ERR
"isdn_x25iface_proto_del: "
144 "concap_proto pointer is NULL\n" );
147 tmp
= cprot
-> proto_data
;
149 printk( KERN_ERR
"isdn_x25iface_proto_del: inconsistent "
150 "proto_data pointer (maybe already deleted?)\n");
153 /* close if the protocol is still open */
154 if( cprot
-> dops
) isdn_x25iface_proto_close(cprot
);
155 /* freeing the storage should be sufficient now. But some additional
156 settings might help to catch wild pointer bugs */
158 cprot
-> proto_data
= NULL
;
164 /* (re-)initialize the data structures for x25iface encapsulation
166 int isdn_x25iface_proto_restart(struct concap_proto
*cprot
,
168 struct concap_device_ops
*dops
)
170 ix25_pdata_t
* pda
= cprot
-> proto_data
;
173 IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev
) );
175 if ( pdata_is_bad( pda
) ) return -1;
177 if( !( dops
&& dops
-> data_req
&& dops
-> connect_req
178 && dops
-> disconn_req
) ){
179 printk( KERN_WARNING
"isdn_x25iface_restart: required dops"
181 isdn_x25iface_proto_close(cprot
);
185 cli(); /* avoid races with incoming events calling pops methods while
186 cprot members are inconsistent */
187 cprot
-> net_dev
= ndev
;
188 cprot
-> pops
= &ix25_pops
;
189 cprot
-> dops
= dops
;
190 pda
-> state
= WAN_DISCONNECTED
;
191 restore_flags(flags
);
195 /* deliver a dl_data frame received from i4l HL driver to the network layer
197 int isdn_x25iface_receive(struct concap_proto
*cprot
, struct sk_buff
*skb
)
199 IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot
->net_dev
) );
200 if ( ( (ix25_pdata_t
*) (cprot
->proto_data
) )
201 -> state
== WAN_CONNECTED
){
202 skb
-> dev
= cprot
-> net_dev
;
203 skb
-> protocol
= htons(ETH_P_X25
);
204 skb
-> pkt_type
= PACKET_HOST
;
205 if( skb_push(skb
, 1)){
207 skb
-> mac
.raw
= skb
-> data
;
212 printk(KERN_WARNING
"isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot
->net_dev
) );
217 /* a connection set up is indicated by lower layer
219 int isdn_x25iface_connect_ind(struct concap_proto
*cprot
)
221 struct sk_buff
* skb
= dev_alloc_skb(1);
222 enum wan_states
*state_p
223 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
224 IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
225 , MY_DEVNAME(cprot
->net_dev
) );
226 if( *state_p
== WAN_UNCONFIGURED
){
228 "isdn_x25iface_connect_ind while unconfigured %s\n"
229 , MY_DEVNAME(cprot
->net_dev
) );
232 *state_p
= WAN_CONNECTED
;
234 *( skb_put(skb
, 1) ) = 0x01;
235 skb
-> mac
.raw
= skb
-> data
;
236 skb
-> dev
= cprot
-> net_dev
;
237 skb
-> protocol
= htons(ETH_P_X25
);
238 skb
-> pkt_type
= PACKET_HOST
;
242 printk(KERN_WARNING
"isdn_x25iface_connect_ind: "
243 " out of memory -- disconnecting\n");
244 cprot
-> dops
-> disconn_req(cprot
);
249 /* a disconnect is indicated by lower layer
251 int isdn_x25iface_disconn_ind(struct concap_proto
*cprot
)
254 enum wan_states
*state_p
255 = &( ( (ix25_pdata_t
*) (cprot
->proto_data
) ) -> state
);
256 IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot
-> net_dev
) );
257 if( *state_p
== WAN_UNCONFIGURED
){
259 "isdn_x25iface_disconn_ind while unconfigured\n");
262 if(! cprot
-> net_dev
) return -1;
263 *state_p
= WAN_DISCONNECTED
;
264 skb
= dev_alloc_skb(1);
266 *( skb_put(skb
, 1) ) = 0x02;
267 skb
-> mac
.raw
= skb
-> data
;
268 skb
-> dev
= cprot
-> net_dev
;
269 skb
-> protocol
= htons(ETH_P_X25
);
270 skb
-> pkt_type
= PACKET_HOST
;
274 printk(KERN_WARNING
"isdn_x25iface_disconn_ind:"
280 /* process a frame handed over to us from linux network layer. First byte
281 semantics as defined in ../../Documentation/networking/x25-iface.txt
283 int isdn_x25iface_xmit(struct concap_proto
*cprot
, struct sk_buff
*skb
)
285 unsigned char firstbyte
= skb
->data
[0];
287 &( ( (ix25_pdata_t
*) (cprot
-> proto_data
) ) -> state
);
289 IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot
-> net_dev
), firstbyte
, *state
);
290 switch ( firstbyte
){
291 case 0x00: /* dl_data request */
292 if( *state
== WAN_CONNECTED
){
294 cprot
-> net_dev
-> trans_start
= jiffies
;
295 ret
= ( cprot
-> dops
-> data_req(cprot
, skb
) );
296 /* prepare for future retransmissions */
297 if( ret
) skb_push(skb
,1);
300 illegal_state_warn( *state
, firstbyte
);
302 case 0x01: /* dl_connect request */
303 if( *state
== WAN_DISCONNECTED
){
304 *state
= WAN_CONNECTING
;
305 cprot
-> dops
-> connect_req(cprot
);
307 illegal_state_warn( *state
, firstbyte
);
310 case 0x02: /* dl_disconnect request */
312 case WAN_DISCONNECTED
:
313 /* Should not happen. However, give upper layer a
314 chance to recover from inconstistency but don't
315 trust the lower layer sending the disconn_confirm
316 when already disconnected */
317 printk(KERN_WARNING
"isdn_x25iface_xmit: disconnect "
318 " requested while disconnected\n" );
319 isdn_x25iface_disconn_ind(cprot
);
320 break; /* prevent infinite loops */
323 *state
= WAN_DISCONNECTED
;
324 cprot
-> dops
-> disconn_req(cprot
);
327 illegal_state_warn( *state
, firstbyte
);
330 case 0x03: /* changing lapb parameters requested */
331 printk(KERN_WARNING
"isdn_x25iface_xmit: setting of lapb"
332 " options not yet supported\n");
335 printk(KERN_WARNING
"isdn_x25iface_xmit: frame with illegal"
336 " first byte %x ignored:\n", firstbyte
);