ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / isdn / i4l / isdn_x25iface.c
1 /* $Id: isdn_x25iface.c,v 1.1.2.2 2004/01/12 22:37:19 keil Exp $
2  *
3  * Linux ISDN subsystem, X.25 related functions
4  *
5  * This software may be used and distributed according to the terms
6  * of the GNU General Public License, incorporated herein by reference.
7  *
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.
14  *
15  * Only protocol specific stuff goes here. Device specific stuff
16  * goes to another -- device related -- concap_proto support source file.
17  *
18  */
19
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"
25
26 /* for debugging messages not to cause an oops when device pointer is NULL*/
27 #define MY_DEVNAME(dev)  ( (dev) ? (dev)->name : "DEVICE UNSPECIFIED" )
28
29
30 typedef struct isdn_x25iface_proto_data {
31         int magic;
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;
37 } ix25_pdata_t;
38
39
40
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 *,
45                                  struct net_device *,
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 * );
51
52
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,
58         &isdn_x25iface_xmit,
59         &isdn_x25iface_receive,
60         &isdn_x25iface_connect_ind,
61         &isdn_x25iface_disconn_ind
62 };
63
64 /* error message helper function */
65 static void illegal_state_warn( unsigned state, unsigned char firstbyte) 
66 {
67         printk( KERN_WARNING "isdn_x25iface: firstbyte %x illegal in"
68                 "current state %d\n",firstbyte, state );
69 }
70
71 /* check protocol data field for consistency */
72 static int pdata_is_bad( ix25_pdata_t * pda ){
73
74         if( pda  &&  pda -> magic == ISDN_X25IFACE_MAGIC ) return 0;
75         printk( KERN_WARNING
76                 "isdn_x25iface_xxx: illegal pointer to proto data\n" );
77         return 1;
78 }
79
80 /* create a new x25 interface protocol instance
81  */
82 struct concap_proto * isdn_x25iface_proto_new()
83 {
84         ix25_pdata_t * tmp = kmalloc(sizeof(ix25_pdata_t),GFP_KERNEL);
85         IX25DEBUG("isdn_x25iface_proto_new\n");
86         if( tmp ){
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) );
98         }
99         return NULL;
100 };
101
102 /* close the x25iface encapsulation protocol 
103  */
104 int isdn_x25iface_proto_close(struct concap_proto *cprot){
105
106         ix25_pdata_t *tmp;
107         int ret = 0;
108         ulong flags;
109
110         if( ! cprot ){
111                 printk( KERN_ERR "isdn_x25iface_proto_close: "
112                         "invalid concap_proto pointer\n" );
113                 return -1;
114         }
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 ) ){
121                 ret = -1;
122         } else {
123                 tmp -> state = WAN_UNCONFIGURED;
124         }
125         spin_unlock_irqrestore(&cprot->lock, flags);
126         return ret;
127 }
128
129 /* Delete the x25iface encapsulation protocol instance
130  */
131 void isdn_x25iface_proto_del(struct concap_proto *cprot){
132
133         ix25_pdata_t * tmp;
134  
135         IX25DEBUG( "isdn_x25iface_proto_del \n" );
136         if( ! cprot ){
137                 printk( KERN_ERR "isdn_x25iface_proto_del: "
138                         "concap_proto pointer is NULL\n" );
139                 return;
140         }
141         tmp = cprot -> proto_data;
142         if( tmp == NULL ){ 
143                 printk( KERN_ERR "isdn_x25iface_proto_del: inconsistent "
144                         "proto_data pointer (maybe already deleted?)\n"); 
145                 return;
146         }
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 */
151         tmp -> magic = 0;
152         cprot -> proto_data = NULL;
153
154         kfree( tmp );
155         return;
156 }
157
158 /* (re-)initialize the data structures for x25iface encapsulation
159  */
160 int isdn_x25iface_proto_restart(struct concap_proto *cprot,
161                                 struct net_device *ndev, 
162                                 struct concap_device_ops *dops)
163 {
164         ix25_pdata_t * pda = cprot -> proto_data ;
165         ulong flags;
166
167         IX25DEBUG( "isdn_x25iface_proto_restart %s \n", MY_DEVNAME(ndev) );
168
169         if ( pdata_is_bad( pda ) ) return -1;
170
171         if( !( dops  && dops -> data_req && dops -> connect_req 
172                && dops -> disconn_req )  ){
173                 printk( KERN_WARNING "isdn_x25iface_restart: required dops"
174                         " missing\n" );
175                 isdn_x25iface_proto_close(cprot);
176                 return -1;
177         }
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);
184         return 0;
185 }
186
187 /* deliver a dl_data frame received from i4l HL driver to the network layer 
188  */
189 int isdn_x25iface_receive(struct concap_proto *cprot, struct sk_buff *skb)
190 {
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)){
198                         skb -> data[0]=0x00;
199                         skb -> mac.raw = skb -> data;
200                         netif_rx(skb);
201                         return 0;
202                 }
203         }
204         printk(KERN_WARNING "isdn_x25iface_receive %s: not connected, skb dropped\n", MY_DEVNAME(cprot->net_dev) );
205         dev_kfree_skb(skb);
206         return -1;
207 }
208
209 /* a connection set up is indicated by lower layer 
210  */
211 int isdn_x25iface_connect_ind(struct concap_proto *cprot)
212 {
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 ){ 
219                 printk(KERN_WARNING 
220                        "isdn_x25iface_connect_ind while unconfigured %s\n"
221                        , MY_DEVNAME(cprot->net_dev) );
222                 return -1;
223         }
224         *state_p = WAN_CONNECTED;
225         if( skb ){
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;
231                 netif_rx(skb);
232                 return 0;
233         } else {
234                 printk(KERN_WARNING "isdn_x25iface_connect_ind: "
235                        " out of memory -- disconnecting\n");
236                 cprot -> dops -> disconn_req(cprot);
237                 return -1;
238         }
239 }
240         
241 /* a disconnect is indicated by lower layer 
242  */
243 int isdn_x25iface_disconn_ind(struct concap_proto *cprot)
244 {
245         struct sk_buff *skb;
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 ){ 
250                 printk(KERN_WARNING 
251                        "isdn_x25iface_disconn_ind while unconfigured\n");
252                 return -1;
253         }
254         if(! cprot -> net_dev) return -1;
255         *state_p = WAN_DISCONNECTED;
256         skb = dev_alloc_skb(1);
257         if( skb ){
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;
263                 netif_rx(skb);
264                 return 0;
265         } else {
266                 printk(KERN_WARNING "isdn_x25iface_disconn_ind:"
267                        " out of memory\n");
268                 return -1;
269         }
270 }
271
272 /* process a frame handed over to us from linux network layer. First byte
273    semantics as defined in ../../Documentation/networking/x25-iface.txt 
274    */
275 int isdn_x25iface_xmit(struct concap_proto *cprot, struct sk_buff *skb)
276 {
277         unsigned char firstbyte = skb->data[0];
278         unsigned *state = 
279                 &( ( (ix25_pdata_t*) (cprot -> proto_data) ) -> state  );
280         int ret = 0;
281         IX25DEBUG( "isdn_x25iface_xmit: %s first=%x state=%d \n", MY_DEVNAME(cprot -> net_dev), firstbyte, *state );
282         switch ( firstbyte ){
283         case 0x00: /* dl_data request */
284                 if( *state == WAN_CONNECTED ){
285                         skb_pull(skb, 1);
286                         cprot -> net_dev -> trans_start = jiffies;
287                         ret = ( cprot -> dops -> data_req(cprot, skb) );
288                         /* prepare for future retransmissions */
289                         if( ret ) skb_push(skb,1);
290                         return ret;
291                 }
292                 illegal_state_warn( *state, firstbyte ); 
293                 break;
294         case 0x01: /* dl_connect request */
295                 if( *state == WAN_DISCONNECTED ){
296                         *state = WAN_CONNECTING;
297                         ret = cprot -> dops -> connect_req(cprot);
298                         if(ret){
299                                 /* reset state and notify upper layer about
300                                  * immidiatly failed attempts */
301                                 isdn_x25iface_disconn_ind(cprot);
302                         }
303                 } else {
304                         illegal_state_warn( *state, firstbyte );
305                 }
306                 break;
307         case 0x02: /* dl_disconnect request */
308                 switch ( *state ){
309                 case WAN_DISCONNECTED: 
310                         /* Should not happen. However, give upper layer a
311                            chance to recover from inconstistency  but don't
312                            trust the lower layer sending the disconn_confirm
313                            when already disconnected */
314                         printk(KERN_WARNING "isdn_x25iface_xmit: disconnect "
315                                " requested while disconnected\n" );
316                         isdn_x25iface_disconn_ind(cprot);
317                         break; /* prevent infinite loops */
318                 case WAN_CONNECTING:
319                 case WAN_CONNECTED:
320                         *state = WAN_DISCONNECTED;
321                         cprot -> dops -> disconn_req(cprot);
322                         break;
323                 default:
324                         illegal_state_warn( *state, firstbyte );
325                 }
326                 break;
327         case 0x03: /* changing lapb parameters requested */
328                 printk(KERN_WARNING "isdn_x25iface_xmit: setting of lapb"
329                        " options not yet supported\n");
330                 break;
331         default:
332                 printk(KERN_WARNING "isdn_x25iface_xmit: frame with illegal"
333                        " first byte %x ignored:\n", firstbyte);
334         }
335         dev_kfree_skb(skb);
336         return 0;
337 }