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 wich 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/wanrouter.h>
24 #include "isdn_x25iface.h"
26 /* for debugging messages not to cause an oops when device pointer is NULL*/
27 #define MY_DEVNAME(dev) ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
30 typedef struct isdn_x25iface_proto_data {
32 enum wan_states state;
33 /* Private stuff, not to be accessed via proto_data. We provide the
34 other storage for the concap_proto instance here as well,
35 enabling us to allocate both with just one kmalloc(): */
36 struct concap_proto priv;
41 /* is now in header file (extern): struct concap_proto * isdn_x25iface_proto_new(void); */
42 void isdn_x25iface_proto_del( struct concap_proto * );
43 int isdn_x25iface_proto_close( struct concap_proto * );
44 int isdn_x25iface_proto_restart( struct concap_proto *,
46 struct concap_device_ops *);
47 int isdn_x25iface_xmit( struct concap_proto *, struct sk_buff * );
48 int isdn_x25iface_receive( struct concap_proto *, struct sk_buff * );
49 int isdn_x25iface_connect_ind( struct concap_proto * );
50 int isdn_x25iface_disconn_ind( struct concap_proto * );
53 static struct concap_proto_ops ix25_pops = {
54 &isdn_x25iface_proto_new,
55 &isdn_x25iface_proto_del,
56 &isdn_x25iface_proto_restart,
57 &isdn_x25iface_proto_close,
59 &isdn_x25iface_receive,
60 &isdn_x25iface_connect_ind,
61 &isdn_x25iface_disconn_ind
64 /* error message helper function */
65 static void illegal_state_warn( unsigned state, unsigned char firstbyte)
67 printk( KERN_WARNING "isdn_x25iface: firstbyte %x illegal in"
68 "current state %d\n",firstbyte, state );
71 /* check protocol data field for consistency */
72 static int pdata_is_bad( ix25_pdata_t * pda ){
74 if( pda && pda -> magic == ISDN_X25IFACE_MAGIC ) return 0;
76 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
80 /* create a new x25 interface protocol instance
82 struct concap_proto * isdn_x25iface_proto_new(void)
84 ix25_pdata_t * tmp = kmalloc(sizeof(ix25_pdata_t),GFP_KERNEL);
85 IX25DEBUG("isdn_x25iface_proto_new\n");
87 tmp -> magic = ISDN_X25IFACE_MAGIC;
88 tmp -> state = WAN_UNCONFIGURED;
89 /* private data space used to hold the concap_proto data.
90 Only to be accessed via the returned pointer */
91 spin_lock_init(&tmp->priv.lock);
92 tmp -> priv.dops = NULL;
93 tmp -> priv.net_dev = NULL;
94 tmp -> priv.pops = &ix25_pops;
95 tmp -> priv.flags = 0;
96 tmp -> priv.proto_data = tmp;
97 return( &(tmp -> priv) );
102 /* close the x25iface encapsulation protocol
104 int isdn_x25iface_proto_close(struct concap_proto *cprot){
111 printk( KERN_ERR "isdn_x25iface_proto_close: "
112 "invalid concap_proto pointer\n" );
115 IX25DEBUG( "isdn_x25iface_proto_close %s \n", MY_DEVNAME(cprot -> net_dev) );
116 spin_lock_irqsave(&cprot->lock, flags);
117 cprot -> dops = NULL;
118 cprot -> net_dev = NULL;
119 tmp = cprot -> proto_data;
120 if( pdata_is_bad( tmp ) ){
123 tmp -> state = WAN_UNCONFIGURED;
125 spin_unlock_irqrestore(&cprot->lock, flags);
129 /* Delete the x25iface encapsulation protocol instance
131 void isdn_x25iface_proto_del(struct concap_proto *cprot){
135 IX25DEBUG( "isdn_x25iface_proto_del \n" );
137 printk( KERN_ERR "isdn_x25iface_proto_del: "
138 "concap_proto pointer is NULL\n" );
141 tmp = cprot -> proto_data;
143 printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
144 "proto_data pointer (maybe already deleted?)\n");
147 /* close if the protocol is still open */
148 if( cprot -> dops ) isdn_x25iface_proto_close(cprot);
149 /* freeing the storage should be sufficient now. But some additional
150 settings might help to catch wild pointer bugs */
152 cprot -> proto_data = NULL;
158 /* (re-)initialize the data structures for x25iface encapsulation
160 int isdn_x25iface_proto_restart(struct concap_proto *cprot,
161 struct net_device *ndev,
162 struct concap_device_ops *dops)
164 ix25_pdata_t * pda = cprot -> proto_data ;
167 IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev) );
169 if ( pdata_is_bad( pda ) ) return -1;
171 if( !( dops && dops -> data_req && dops -> connect_req
172 && dops -> disconn_req ) ){
173 printk( KERN_WARNING "isdn_x25iface_restart: required dops"
175 isdn_x25iface_proto_close(cprot);
178 spin_lock_irqsave(&cprot->lock, flags);
179 cprot -> net_dev = ndev;
180 cprot -> pops = &ix25_pops;
181 cprot -> dops = dops;
182 pda -> state = WAN_DISCONNECTED;
183 spin_unlock_irqrestore(&cprot->lock, flags);
187 /* deliver a dl_data frame received from i4l HL driver to the network layer
189 int isdn_x25iface_receive(struct concap_proto *cprot, struct sk_buff *skb)
191 IX25DEBUG( "isdn_x25iface_receive %s \n", MY_DEVNAME(cprot->net_dev) );
192 if ( ( (ix25_pdata_t*) (cprot->proto_data) )
193 -> state == WAN_CONNECTED ){
194 skb -> dev = cprot -> net_dev;
195 skb -> protocol = htons(ETH_P_X25);
196 skb -> pkt_type = PACKET_HOST;
197 if( skb_push(skb, 1)){
199 skb -> mac.raw = skb -> data;
204 printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
209 /* a connection set up is indicated by lower layer
211 int isdn_x25iface_connect_ind(struct concap_proto *cprot)
213 struct sk_buff * skb = dev_alloc_skb(1);
214 enum wan_states *state_p
215 = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
216 IX25DEBUG( "isdn_x25iface_connect_ind %s \n"
217 , MY_DEVNAME(cprot->net_dev) );
218 if( *state_p == WAN_UNCONFIGURED ){
220 "isdn_x25iface_connect_ind while unconfigured %s\n"
221 , MY_DEVNAME(cprot->net_dev) );
224 *state_p = WAN_CONNECTED;
226 *( skb_put(skb, 1) ) = 0x01;
227 skb -> mac.raw = skb -> data;
228 skb -> dev = cprot -> net_dev;
229 skb -> protocol = htons(ETH_P_X25);
230 skb -> pkt_type = PACKET_HOST;
234 printk(KERN_WARNING "isdn_x25iface_connect_ind: "
235 " out of memory -- disconnecting\n");
236 cprot -> dops -> disconn_req(cprot);
241 /* a disconnect is indicated by lower layer
243 int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
246 enum wan_states *state_p
247 = &( ( (ix25_pdata_t*) (cprot->proto_data) ) -> state);
248 IX25DEBUG( "isdn_x25iface_disconn_ind %s \n", MY_DEVNAME(cprot -> net_dev) );
249 if( *state_p == WAN_UNCONFIGURED ){
251 "isdn_x25iface_disconn_ind while unconfigured\n");
254 if(! cprot -> net_dev) return -1;
255 *state_p = WAN_DISCONNECTED;
256 skb = dev_alloc_skb(1);
258 *( skb_put(skb, 1) ) = 0x02;
259 skb -> mac.raw = skb -> data;
260 skb -> dev = cprot -> net_dev;
261 skb -> protocol = htons(ETH_P_X25);
262 skb -> pkt_type = PACKET_HOST;
266 printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
272 /* process a frame handed over to us from linux network layer. First byte
273 semantics as defined in Documentation/networking/x25-iface.txt
275 int isdn_x25iface_xmit(struct concap_proto *cprot, struct sk_buff *skb)
277 unsigned char firstbyte = skb->data[0];
278 enum wan_states *state = &((ix25_pdata_t*)cprot->proto_data)->state;
280 IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot -> net_dev), firstbyte, *state );
281 switch ( firstbyte ){
282 case 0x00: /* dl_data request */
283 if( *state == WAN_CONNECTED ){
285 cprot -> net_dev -> trans_start = jiffies;
286 ret = ( cprot -> dops -> data_req(cprot, skb) );
287 /* prepare for future retransmissions */
288 if( ret ) skb_push(skb,1);
291 illegal_state_warn( *state, firstbyte );
293 case 0x01: /* dl_connect request */
294 if( *state == WAN_DISCONNECTED ){
295 *state = WAN_CONNECTING;
296 ret = cprot -> dops -> connect_req(cprot);
298 /* reset state and notify upper layer about
299 * immidiatly failed attempts */
300 isdn_x25iface_disconn_ind(cprot);
303 illegal_state_warn( *state, firstbyte );
306 case 0x02: /* dl_disconnect request */
308 case WAN_DISCONNECTED:
309 /* Should not happen. However, give upper layer a
310 chance to recover from inconstistency but don't
311 trust the lower layer sending the disconn_confirm
312 when already disconnected */
313 printk(KERN_WARNING "isdn_x25iface_xmit: disconnect "
314 " requested while disconnected\n" );
315 isdn_x25iface_disconn_ind(cprot);
316 break; /* prevent infinite loops */
319 *state = WAN_DISCONNECTED;
320 cprot -> dops -> disconn_req(cprot);
323 illegal_state_warn( *state, firstbyte );
326 case 0x03: /* changing lapb parameters requested */
327 printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
328 " options not yet supported\n");
331 printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
332 " first byte %x ignored:\n", firstbyte);