Merge branch 'merge'
[pandora-kernel.git] / net / lapb / lapb_iface.c
1 /*
2  *      LAPB release 002
3  *
4  *      This code REQUIRES 2.1.15 or higher/ NET3.038
5  *
6  *      This module:
7  *              This module is free software; you can redistribute it and/or
8  *              modify it under the terms of the GNU General Public License
9  *              as published by the Free Software Foundation; either version
10  *              2 of the License, or (at your option) any later version.
11  *
12  *      History
13  *      LAPB 001        Jonathan Naylor Started Coding
14  *      LAPB 002        Jonathan Naylor New timer architecture.
15  *      2000-10-29      Henner Eisen    lapb_data_indication() return status.
16  */
17  
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/types.h>
21 #include <linux/socket.h>
22 #include <linux/in.h>
23 #include <linux/kernel.h>
24 #include <linux/jiffies.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/inet.h>
30 #include <linux/if_arp.h>
31 #include <linux/skbuff.h>
32 #include <net/sock.h>
33 #include <asm/uaccess.h>
34 #include <asm/system.h>
35 #include <linux/fcntl.h>
36 #include <linux/mm.h>
37 #include <linux/interrupt.h>
38 #include <linux/stat.h>
39 #include <linux/init.h>
40 #include <net/lapb.h>
41
42 static struct list_head lapb_list = LIST_HEAD_INIT(lapb_list);
43 static DEFINE_RWLOCK(lapb_list_lock);
44
45 /*
46  *      Free an allocated lapb control block. 
47  */
48 static void lapb_free_cb(struct lapb_cb *lapb)
49 {
50         kfree(lapb);
51 }
52
53 static __inline__ void lapb_hold(struct lapb_cb *lapb)
54 {
55         atomic_inc(&lapb->refcnt);
56 }
57
58 static __inline__ void lapb_put(struct lapb_cb *lapb)
59 {
60         if (atomic_dec_and_test(&lapb->refcnt))
61                 lapb_free_cb(lapb);
62 }
63
64 /*
65  *      Socket removal during an interrupt is now safe.
66  */
67 static void __lapb_remove_cb(struct lapb_cb *lapb)
68 {
69         if (lapb->node.next) {
70                 list_del(&lapb->node);
71                 lapb_put(lapb);
72         }
73 }
74
75 /*
76  *      Add a socket to the bound sockets list.
77  */
78 static void __lapb_insert_cb(struct lapb_cb *lapb)
79 {
80         list_add(&lapb->node, &lapb_list);
81         lapb_hold(lapb);
82 }
83
84 static struct lapb_cb *__lapb_devtostruct(struct net_device *dev)
85 {
86         struct list_head *entry;
87         struct lapb_cb *lapb, *use = NULL;
88
89         list_for_each(entry, &lapb_list) {
90                 lapb = list_entry(entry, struct lapb_cb, node);
91                 if (lapb->dev == dev) {
92                         use = lapb;
93                         break;
94                 }
95         }
96
97         if (use)
98                 lapb_hold(use);
99
100         return use;
101 }
102
103 static struct lapb_cb *lapb_devtostruct(struct net_device *dev)
104 {
105         struct lapb_cb *rc;
106
107         read_lock_bh(&lapb_list_lock);
108         rc = __lapb_devtostruct(dev);
109         read_unlock_bh(&lapb_list_lock);
110
111         return rc;
112 }
113 /*
114  *      Create an empty LAPB control block.
115  */
116 static struct lapb_cb *lapb_create_cb(void)
117 {
118         struct lapb_cb *lapb = kzalloc(sizeof(*lapb), GFP_ATOMIC);
119
120
121         if (!lapb)
122                 goto out;
123
124         skb_queue_head_init(&lapb->write_queue);
125         skb_queue_head_init(&lapb->ack_queue);
126
127         init_timer(&lapb->t1timer);
128         init_timer(&lapb->t2timer);
129
130         lapb->t1      = LAPB_DEFAULT_T1;
131         lapb->t2      = LAPB_DEFAULT_T2;
132         lapb->n2      = LAPB_DEFAULT_N2;
133         lapb->mode    = LAPB_DEFAULT_MODE;
134         lapb->window  = LAPB_DEFAULT_WINDOW;
135         lapb->state   = LAPB_STATE_0;
136         atomic_set(&lapb->refcnt, 1);
137 out:
138         return lapb;
139 }
140
141 int lapb_register(struct net_device *dev, struct lapb_register_struct *callbacks)
142 {
143         struct lapb_cb *lapb;
144         int rc = LAPB_BADTOKEN;
145
146         write_lock_bh(&lapb_list_lock);
147
148         lapb = __lapb_devtostruct(dev);
149         if (lapb) {
150                 lapb_put(lapb);
151                 goto out;
152         }
153
154         lapb = lapb_create_cb();
155         rc = LAPB_NOMEM;
156         if (!lapb)
157                 goto out;
158
159         lapb->dev       = dev;
160         lapb->callbacks = *callbacks;
161
162         __lapb_insert_cb(lapb);
163
164         lapb_start_t1timer(lapb);
165
166         rc = LAPB_OK;
167 out:
168         write_unlock_bh(&lapb_list_lock);
169         return rc;
170 }
171
172 int lapb_unregister(struct net_device *dev)
173 {
174         struct lapb_cb *lapb;
175         int rc = LAPB_BADTOKEN;
176
177         write_lock_bh(&lapb_list_lock);
178         lapb = __lapb_devtostruct(dev);
179         if (!lapb)
180                 goto out;
181
182         lapb_stop_t1timer(lapb);
183         lapb_stop_t2timer(lapb);
184
185         lapb_clear_queues(lapb);
186
187         __lapb_remove_cb(lapb);
188
189         lapb_put(lapb);
190         rc = LAPB_OK;
191 out:
192         write_unlock_bh(&lapb_list_lock);
193         return rc;
194 }
195
196 int lapb_getparms(struct net_device *dev, struct lapb_parms_struct *parms)
197 {
198         int rc = LAPB_BADTOKEN;
199         struct lapb_cb *lapb = lapb_devtostruct(dev);
200
201         if (!lapb)
202                 goto out;
203
204         parms->t1      = lapb->t1 / HZ;
205         parms->t2      = lapb->t2 / HZ;
206         parms->n2      = lapb->n2;
207         parms->n2count = lapb->n2count;
208         parms->state   = lapb->state;
209         parms->window  = lapb->window;
210         parms->mode    = lapb->mode;
211
212         if (!timer_pending(&lapb->t1timer))
213                 parms->t1timer = 0;
214         else
215                 parms->t1timer = (lapb->t1timer.expires - jiffies) / HZ;
216
217         if (!timer_pending(&lapb->t2timer))
218                 parms->t2timer = 0;
219         else
220                 parms->t2timer = (lapb->t2timer.expires - jiffies) / HZ;
221
222         lapb_put(lapb);
223         rc = LAPB_OK;
224 out:
225         return rc;
226 }
227
228 int lapb_setparms(struct net_device *dev, struct lapb_parms_struct *parms)
229 {
230         int rc = LAPB_BADTOKEN;
231         struct lapb_cb *lapb = lapb_devtostruct(dev);
232
233         if (!lapb)
234                 goto out;
235
236         rc = LAPB_INVALUE;
237         if (parms->t1 < 1 || parms->t2 < 1 || parms->n2 < 1)
238                 goto out_put;
239
240         if (lapb->state == LAPB_STATE_0) {
241                 if (((parms->mode & LAPB_EXTENDED) &&
242                      (parms->window < 1 || parms->window > 127)) ||
243                     (parms->window < 1 || parms->window > 7))
244                         goto out_put;
245
246                 lapb->mode    = parms->mode;
247                 lapb->window  = parms->window;
248         }
249
250         lapb->t1    = parms->t1 * HZ;
251         lapb->t2    = parms->t2 * HZ;
252         lapb->n2    = parms->n2;
253
254         rc = LAPB_OK;
255 out_put:
256         lapb_put(lapb);
257 out:
258         return rc;
259 }
260
261 int lapb_connect_request(struct net_device *dev)
262 {
263         struct lapb_cb *lapb = lapb_devtostruct(dev);
264         int rc = LAPB_BADTOKEN;
265
266         if (!lapb)
267                 goto out;
268
269         rc = LAPB_OK;
270         if (lapb->state == LAPB_STATE_1)
271                 goto out_put;
272
273         rc = LAPB_CONNECTED;
274         if (lapb->state == LAPB_STATE_3 || lapb->state == LAPB_STATE_4)
275                 goto out_put;
276
277         lapb_establish_data_link(lapb);
278
279 #if LAPB_DEBUG > 0
280         printk(KERN_DEBUG "lapb: (%p) S0 -> S1\n", lapb->dev);
281 #endif
282         lapb->state = LAPB_STATE_1;
283
284         rc = LAPB_OK;
285 out_put:
286         lapb_put(lapb);
287 out:
288         return rc;
289 }
290
291 int lapb_disconnect_request(struct net_device *dev)
292 {
293         struct lapb_cb *lapb = lapb_devtostruct(dev);
294         int rc = LAPB_BADTOKEN;
295
296         if (!lapb)
297                 goto out;
298
299         switch (lapb->state) {
300                 case LAPB_STATE_0:
301                         rc = LAPB_NOTCONNECTED;
302                         goto out_put;
303
304                 case LAPB_STATE_1:
305 #if LAPB_DEBUG > 1
306                         printk(KERN_DEBUG "lapb: (%p) S1 TX DISC(1)\n", lapb->dev);
307 #endif
308 #if LAPB_DEBUG > 0
309                         printk(KERN_DEBUG "lapb: (%p) S1 -> S0\n", lapb->dev);
310 #endif
311                         lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
312                         lapb->state = LAPB_STATE_0;
313                         lapb_start_t1timer(lapb);
314                         rc = LAPB_NOTCONNECTED;
315                         goto out_put;
316
317                 case LAPB_STATE_2:
318                         rc = LAPB_OK;
319                         goto out_put;
320         }
321
322         lapb_clear_queues(lapb);
323         lapb->n2count = 0;
324         lapb_send_control(lapb, LAPB_DISC, LAPB_POLLON, LAPB_COMMAND);
325         lapb_start_t1timer(lapb);
326         lapb_stop_t2timer(lapb);
327         lapb->state = LAPB_STATE_2;
328
329 #if LAPB_DEBUG > 1
330         printk(KERN_DEBUG "lapb: (%p) S3 DISC(1)\n", lapb->dev);
331 #endif
332 #if LAPB_DEBUG > 0
333         printk(KERN_DEBUG "lapb: (%p) S3 -> S2\n", lapb->dev);
334 #endif
335
336         rc = LAPB_OK;
337 out_put:
338         lapb_put(lapb);
339 out:
340         return rc;
341 }
342
343 int lapb_data_request(struct net_device *dev, struct sk_buff *skb)
344 {
345         struct lapb_cb *lapb = lapb_devtostruct(dev);
346         int rc = LAPB_BADTOKEN;
347
348         if (!lapb)
349                 goto out;
350
351         rc = LAPB_NOTCONNECTED;
352         if (lapb->state != LAPB_STATE_3 && lapb->state != LAPB_STATE_4)
353                 goto out_put;
354
355         skb_queue_tail(&lapb->write_queue, skb);
356         lapb_kick(lapb);
357         rc = LAPB_OK;
358 out_put:
359         lapb_put(lapb);
360 out:
361         return rc;
362 }
363
364 int lapb_data_received(struct net_device *dev, struct sk_buff *skb)
365 {
366         struct lapb_cb *lapb = lapb_devtostruct(dev);
367         int rc = LAPB_BADTOKEN;
368
369         if (lapb) {
370                 lapb_data_input(lapb, skb);
371                 lapb_put(lapb);
372                 rc = LAPB_OK;
373         }
374
375         return rc;
376 }
377
378 void lapb_connect_confirmation(struct lapb_cb *lapb, int reason)
379 {
380         if (lapb->callbacks.connect_confirmation)
381                 lapb->callbacks.connect_confirmation(lapb->dev, reason);
382 }
383
384 void lapb_connect_indication(struct lapb_cb *lapb, int reason)
385 {
386         if (lapb->callbacks.connect_indication)
387                 lapb->callbacks.connect_indication(lapb->dev, reason);
388 }
389
390 void lapb_disconnect_confirmation(struct lapb_cb *lapb, int reason)
391 {
392         if (lapb->callbacks.disconnect_confirmation)
393                 lapb->callbacks.disconnect_confirmation(lapb->dev, reason);
394 }
395
396 void lapb_disconnect_indication(struct lapb_cb *lapb, int reason)
397 {
398         if (lapb->callbacks.disconnect_indication)
399                 lapb->callbacks.disconnect_indication(lapb->dev, reason);
400 }
401
402 int lapb_data_indication(struct lapb_cb *lapb, struct sk_buff *skb)
403 {
404         if (lapb->callbacks.data_indication)
405                 return lapb->callbacks.data_indication(lapb->dev, skb);
406
407         kfree_skb(skb);
408         return NET_RX_CN_HIGH; /* For now; must be != NET_RX_DROP */ 
409 }
410
411 int lapb_data_transmit(struct lapb_cb *lapb, struct sk_buff *skb)
412 {
413         int used = 0;
414
415         if (lapb->callbacks.data_transmit) {
416                 lapb->callbacks.data_transmit(lapb->dev, skb);
417                 used = 1;
418         }
419
420         return used;
421 }
422
423 EXPORT_SYMBOL(lapb_register);
424 EXPORT_SYMBOL(lapb_unregister);
425 EXPORT_SYMBOL(lapb_getparms);
426 EXPORT_SYMBOL(lapb_setparms);
427 EXPORT_SYMBOL(lapb_connect_request);
428 EXPORT_SYMBOL(lapb_disconnect_request);
429 EXPORT_SYMBOL(lapb_data_request);
430 EXPORT_SYMBOL(lapb_data_received);
431
432 static int __init lapb_init(void)
433 {
434         return 0;
435 }
436
437 static void __exit lapb_exit(void)
438 {
439         WARN_ON(!list_empty(&lapb_list));
440 }
441
442 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
443 MODULE_DESCRIPTION("The X.25 Link Access Procedure B link layer protocol");
444 MODULE_LICENSE("GPL");
445
446 module_init(lapb_init);
447 module_exit(lapb_exit);