Import 2.2.0pre6
[davej-history.git] / drivers / isdn / isdn_x25iface.c
blob2ae21fcf38ef26470195a392d1cd13ee9d27f461
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
17 * correct comments
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 {
35 int magic;
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;
41 } ix25_pdata_t;
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 *,
49 struct device *,
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,
62 &isdn_x25iface_xmit,
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;
79 printk( KERN_WARNING
80 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
81 return 1;
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");
90 if( tmp ){
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) );
102 return NULL;
105 /* close the x25iface encapsulation protocol
107 int isdn_x25iface_proto_close(struct concap_proto *cprot){
109 ix25_pdata_t *tmp;
110 int ret = 0;
111 ulong flags;
113 if( ! cprot ){
114 printk( KERN_ERR "isdn_x25iface_proto_close: "
115 "invalid concap_proto pointer\n" );
116 return -1;
118 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot -> net_dev) );
119 save_flags(flags);
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 ) ){
126 ret = -1;
127 } else {
128 tmp -> state = WAN_UNCONFIGURED;
130 restore_flags(flags);
132 return ret;
135 /* Delete the x25iface encapsulation protocol instance
137 void isdn_x25iface_proto_del(struct concap_proto *cprot){
139 ix25_pdata_t * tmp;
141 IX25DEBUG( "isdn_x25iface_proto_del \n" );
142 if( ! cprot ){
143 printk( KERN_ERR "isdn_x25iface_proto_del: "
144 "concap_proto pointer is NULL\n" );
145 return;
147 tmp = cprot -> proto_data;
148 if( tmp == NULL ){
149 printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
150 "proto_data pointer (maybe already deleted?)\n");
151 return;
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 */
157 tmp -> magic = 0;
158 cprot -> proto_data = NULL;
160 kfree( tmp );
161 return;
164 /* (re-)initialize the data structures for x25iface encapsulation
166 int isdn_x25iface_proto_restart(struct concap_proto *cprot,
167 struct device *ndev,
168 struct concap_device_ops *dops)
170 ix25_pdata_t * pda = cprot -> proto_data ;
171 ulong flags;
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"
180 " missing\n" );
181 isdn_x25iface_proto_close(cprot);
182 return -1;
184 save_flags(flags);
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);
192 return 0;
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)){
206 skb -> data[0]=0x00;
207 skb -> mac.raw = skb -> data;
208 netif_rx(skb);
209 return 0;
212 printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
213 dev_kfree_skb(skb);
214 return -1;
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 ){
227 printk(KERN_WARNING
228 "isdn_x25iface_connect_ind while unconfigured %s\n"
229 , MY_DEVNAME(cprot->net_dev) );
230 return -1;
232 *state_p = WAN_CONNECTED;
233 if( skb ){
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;
239 netif_rx(skb);
240 return 0;
241 } else {
242 printk(KERN_WARNING "isdn_x25iface_connect_ind: "
243 " out of memory -- disconnecting\n");
244 cprot -> dops -> disconn_req(cprot);
245 return -1;
249 /* a disconnect is indicated by lower layer
251 int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
253 struct sk_buff *skb;
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 ){
258 printk(KERN_WARNING
259 "isdn_x25iface_disconn_ind while unconfigured\n");
260 return -1;
262 if(! cprot -> net_dev) return -1;
263 *state_p = WAN_DISCONNECTED;
264 skb = dev_alloc_skb(1);
265 if( skb ){
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;
271 netif_rx(skb);
272 return 0;
273 } else {
274 printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
275 " out of memory\n");
276 return -1;
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];
286 unsigned *state =
287 &( ( (ix25_pdata_t*) (cprot -> proto_data) ) -> state );
288 int ret = 0;
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 ){
293 skb_pull(skb, 1);
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);
298 return ret;
300 illegal_state_warn( *state, firstbyte );
301 break;
302 case 0x01: /* dl_connect request */
303 if( *state == WAN_DISCONNECTED ){
304 *state = WAN_CONNECTING;
305 cprot -> dops -> connect_req(cprot);
306 } else {
307 illegal_state_warn( *state, firstbyte );
309 break;
310 case 0x02: /* dl_disconnect request */
311 switch ( *state ){
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 */
321 case WAN_CONNECTING:
322 case WAN_CONNECTED:
323 *state = WAN_DISCONNECTED;
324 cprot -> dops -> disconn_req(cprot);
325 break;
326 default:
327 illegal_state_warn( *state, firstbyte );
329 break;
330 case 0x03: /* changing lapb parameters requested */
331 printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
332 " options not yet supported\n");
333 break;
334 default:
335 printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
336 " first byte %x ignored:\n", firstbyte);
338 dev_kfree_skb(skb);
339 return 0;