patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / net / x25 / x25_link.c
1 /*
2  *      X.25 Packet Layer release 002
3  *
4  *      This is ALPHA test software. This code may break your machine,
5  *      randomly fail to work with new releases, misbehave and/or generally
6  *      screw up. It might even work. 
7  *
8  *      This code REQUIRES 2.1.15 or higher
9  *
10  *      This module:
11  *              This module is free software; you can redistribute it and/or
12  *              modify it under the terms of the GNU General Public License
13  *              as published by the Free Software Foundation; either version
14  *              2 of the License, or (at your option) any later version.
15  *
16  *      History
17  *      X.25 001        Jonathan Naylor   Started coding.
18  *      X.25 002        Jonathan Naylor   New timer architecture.
19  *      mar/20/00       Daniela Squassoni Disabling/enabling of facilities 
20  *                                        negotiation.
21  *      2000-09-04      Henner Eisen      dev_hold() / dev_put() for x25_neigh.
22  */
23
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/in.h>
28 #include <linux/kernel.h>
29 #include <linux/jiffies.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/inet.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <net/sock.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
40 #include <linux/fcntl.h>
41 #include <linux/mm.h>
42 #include <linux/interrupt.h>
43 #include <linux/init.h>
44 #include <net/x25.h>
45
46 static struct list_head x25_neigh_list = LIST_HEAD_INIT(x25_neigh_list);
47 static rwlock_t x25_neigh_list_lock = RW_LOCK_UNLOCKED;
48
49 static void x25_t20timer_expiry(unsigned long);
50
51 /*
52  *      Linux set/reset timer routines
53  */
54 static inline void x25_start_t20timer(struct x25_neigh *nb)
55 {
56         mod_timer(&nb->t20timer, jiffies + nb->t20);
57 }
58
59 static void x25_t20timer_expiry(unsigned long param)
60 {
61         struct x25_neigh *nb = (struct x25_neigh *)param;
62
63         x25_transmit_restart_request(nb);
64
65         x25_start_t20timer(nb);
66 }
67
68 static inline void x25_stop_t20timer(struct x25_neigh *nb)
69 {
70         del_timer(&nb->t20timer);
71 }
72
73 static inline int x25_t20timer_pending(struct x25_neigh *nb)
74 {
75         return timer_pending(&nb->t20timer);
76 }
77
78 /*
79  *      This handles all restart and diagnostic frames.
80  */
81 void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
82                       unsigned short frametype)
83 {
84         struct sk_buff *skbn;
85         int confirm;
86
87         switch (frametype) {
88                 case X25_RESTART_REQUEST:
89                         confirm = !x25_t20timer_pending(nb);
90                         x25_stop_t20timer(nb);
91                         nb->state = X25_LINK_STATE_3;
92                         if (confirm)
93                                 x25_transmit_restart_confirmation(nb);
94                         break;
95
96                 case X25_RESTART_CONFIRMATION:
97                         x25_stop_t20timer(nb);
98                         nb->state = X25_LINK_STATE_3;
99                         break;
100
101                 case X25_DIAGNOSTIC:
102                         printk(KERN_WARNING "x25: diagnostic #%d - "
103                                "%02X %02X %02X\n",
104                                skb->data[3], skb->data[4],
105                                skb->data[5], skb->data[6]);
106                         break;
107                         
108                 default:
109                         printk(KERN_WARNING "x25: received unknown %02X "
110                                "with LCI 000\n", frametype);
111                         break;
112         }
113
114         if (nb->state == X25_LINK_STATE_3)
115                 while ((skbn = skb_dequeue(&nb->queue)) != NULL)
116                         x25_send_frame(skbn, nb);
117 }
118
119 /*
120  *      This routine is called when a Restart Request is needed
121  */
122 void x25_transmit_restart_request(struct x25_neigh *nb)
123 {
124         unsigned char *dptr;
125         int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
126         struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
127
128         if (!skb)
129                 return;
130
131         skb_reserve(skb, X25_MAX_L2_LEN);
132
133         dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
134
135         *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
136         *dptr++ = 0x00;
137         *dptr++ = X25_RESTART_REQUEST;
138         *dptr++ = 0x00;
139         *dptr++ = 0;
140
141         skb->sk = NULL;
142
143         x25_send_frame(skb, nb);
144 }
145
146 /*
147  * This routine is called when a Restart Confirmation is needed
148  */
149 void x25_transmit_restart_confirmation(struct x25_neigh *nb)
150 {
151         unsigned char *dptr;
152         int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN;
153         struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
154
155         if (!skb)
156                 return;
157
158         skb_reserve(skb, X25_MAX_L2_LEN);
159
160         dptr = skb_put(skb, X25_STD_MIN_LEN);
161
162         *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
163         *dptr++ = 0x00;
164         *dptr++ = X25_RESTART_CONFIRMATION;
165
166         skb->sk = NULL;
167
168         x25_send_frame(skb, nb);
169 }
170
171 /*
172  * This routine is called when a Diagnostic is required.
173  */
174 void x25_transmit_diagnostic(struct x25_neigh *nb, unsigned char diag)
175 {
176         unsigned char *dptr;
177         int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 1;
178         struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
179
180         if (!skb)
181                 return;
182
183         skb_reserve(skb, X25_MAX_L2_LEN);
184
185         dptr = skb_put(skb, X25_STD_MIN_LEN + 1);
186
187         *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
188         *dptr++ = 0x00;
189         *dptr++ = X25_DIAGNOSTIC;
190         *dptr++ = diag;
191
192         skb->sk = NULL;
193
194         x25_send_frame(skb, nb);
195 }
196
197 /*
198  *      This routine is called when a Clear Request is needed outside of the context
199  *      of a connected socket.
200  */
201 void x25_transmit_clear_request(struct x25_neigh *nb, unsigned int lci,
202                                 unsigned char cause)
203 {
204         unsigned char *dptr;
205         int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
206         struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
207
208         if (!skb)
209                 return;
210
211         skb_reserve(skb, X25_MAX_L2_LEN);
212
213         dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
214
215         *dptr++ = ((lci >> 8) & 0x0F) | (nb->extended ?
216                                          X25_GFI_EXTSEQ :
217                                          X25_GFI_STDSEQ);
218         *dptr++ = (lci >> 0) & 0xFF;
219         *dptr++ = X25_CLEAR_REQUEST;
220         *dptr++ = cause;
221         *dptr++ = 0x00;
222
223         skb->sk = NULL;
224
225         x25_send_frame(skb, nb);
226 }
227
228 void x25_transmit_link(struct sk_buff *skb, struct x25_neigh *nb)
229 {
230         switch (nb->state) {
231                 case X25_LINK_STATE_0:
232                         skb_queue_tail(&nb->queue, skb);
233                         nb->state = X25_LINK_STATE_1;
234                         x25_establish_link(nb);
235                         break;
236                 case X25_LINK_STATE_1:
237                 case X25_LINK_STATE_2:
238                         skb_queue_tail(&nb->queue, skb);
239                         break;
240                 case X25_LINK_STATE_3:
241                         x25_send_frame(skb, nb);
242                         break;
243         }
244 }
245
246 /*
247  *      Called when the link layer has become established.
248  */
249 void x25_link_established(struct x25_neigh *nb)
250 {
251         switch (nb->state) {
252                 case X25_LINK_STATE_0:
253                         nb->state = X25_LINK_STATE_2;
254                         break;
255                 case X25_LINK_STATE_1:
256                         x25_transmit_restart_request(nb);
257                         nb->state = X25_LINK_STATE_2;
258                         x25_start_t20timer(nb);
259                         break;
260         }
261 }
262
263 /*
264  *      Called when the link layer has terminated, or an establishment
265  *      request has failed.
266  */
267
268 void x25_link_terminated(struct x25_neigh *nb)
269 {
270         nb->state = X25_LINK_STATE_0;
271         /* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
272         x25_kill_by_neigh(nb);
273 }
274
275 /*
276  *      Add a new device.
277  */
278 void x25_link_device_up(struct net_device *dev)
279 {
280         struct x25_neigh *nb = kmalloc(sizeof(*nb), GFP_ATOMIC);
281
282         if (!nb)
283                 return;
284
285         skb_queue_head_init(&nb->queue);
286
287         init_timer(&nb->t20timer);
288         nb->t20timer.data     = (unsigned long)nb;
289         nb->t20timer.function = &x25_t20timer_expiry;
290
291         dev_hold(dev);
292         nb->dev      = dev;
293         nb->state    = X25_LINK_STATE_0;
294         nb->extended = 0;
295         /*
296          * Enables negotiation
297          */
298         nb->global_facil_mask = X25_MASK_REVERSE |
299                                        X25_MASK_THROUGHPUT |
300                                        X25_MASK_PACKET_SIZE |
301                                        X25_MASK_WINDOW_SIZE;
302         nb->t20      = sysctl_x25_restart_request_timeout;
303         atomic_set(&nb->refcnt, 1);
304
305         write_lock_bh(&x25_neigh_list_lock);
306         list_add(&nb->node, &x25_neigh_list);
307         write_unlock_bh(&x25_neigh_list_lock);
308 }
309
310 /**
311  *      __x25_remove_neigh - remove neighbour from x25_neigh_list
312  *      @nb - neigh to remove
313  *
314  *      Remove neighbour from x25_neigh_list. If it was there.
315  *      Caller must hold x25_neigh_list_lock.
316  */
317 static void __x25_remove_neigh(struct x25_neigh *nb)
318 {
319         skb_queue_purge(&nb->queue);
320         x25_stop_t20timer(nb);
321
322         if (nb->node.next) {
323                 list_del(&nb->node);
324                 x25_neigh_put(nb);
325         }
326 }
327
328 /*
329  *      A device has been removed, remove its links.
330  */
331 void x25_link_device_down(struct net_device *dev)
332 {
333         struct x25_neigh *nb;
334         struct list_head *entry, *tmp;
335
336         write_lock_bh(&x25_neigh_list_lock);
337
338         list_for_each_safe(entry, tmp, &x25_neigh_list) {
339                 nb = list_entry(entry, struct x25_neigh, node);
340
341                 if (nb->dev == dev) {
342                         __x25_remove_neigh(nb);
343                         dev_put(dev);
344                 }
345         }
346
347         write_unlock_bh(&x25_neigh_list_lock);
348 }
349
350 /*
351  *      Given a device, return the neighbour address.
352  */
353 struct x25_neigh *x25_get_neigh(struct net_device *dev)
354 {
355         struct x25_neigh *nb, *use = NULL;
356         struct list_head *entry;
357
358         read_lock_bh(&x25_neigh_list_lock);
359         list_for_each(entry, &x25_neigh_list) {
360                 nb = list_entry(entry, struct x25_neigh, node);
361
362                 if (nb->dev == dev) {
363                         use = nb;
364                         break;
365                 }
366         }
367
368         if (use)
369                 x25_neigh_hold(use);
370         read_unlock_bh(&x25_neigh_list_lock);
371         return use;
372 }
373
374 /*
375  *      Handle the ioctls that control the subscription functions.
376  */
377 int x25_subscr_ioctl(unsigned int cmd, void __user *arg)
378 {
379         struct x25_subscrip_struct x25_subscr;
380         struct x25_neigh *nb;
381         struct net_device *dev;
382         int rc = -EINVAL;
383
384         if (cmd != SIOCX25GSUBSCRIP && cmd != SIOCX25SSUBSCRIP)
385                 goto out;
386
387         rc = -EFAULT;
388         if (copy_from_user(&x25_subscr, arg, sizeof(x25_subscr)))
389                 goto out;
390
391         rc = -EINVAL;
392         if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
393                 goto out;
394
395         if ((nb = x25_get_neigh(dev)) == NULL)
396                 goto out_dev_put;
397
398         dev_put(dev);
399
400         if (cmd == SIOCX25GSUBSCRIP) {
401                 x25_subscr.extended          = nb->extended;
402                 x25_subscr.global_facil_mask = nb->global_facil_mask;
403                 rc = copy_to_user(arg, &x25_subscr,
404                                   sizeof(x25_subscr)) ? -EFAULT : 0;
405         } else {
406                 rc = -EINVAL;
407                 if (!(x25_subscr.extended && x25_subscr.extended != 1)) {
408                         rc = 0;
409                         nb->extended         = x25_subscr.extended;
410                         nb->global_facil_mask = x25_subscr.global_facil_mask;
411                 }
412         }
413         x25_neigh_put(nb);
414 out:
415         return rc;
416 out_dev_put:
417         dev_put(dev);
418         goto out;
419 }
420
421
422 /*
423  *      Release all memory associated with X.25 neighbour structures.
424  */
425 void __exit x25_link_free(void)
426 {
427         struct x25_neigh *nb;
428         struct list_head *entry, *tmp;
429
430         write_lock_bh(&x25_neigh_list_lock);
431
432         list_for_each_safe(entry, tmp, &x25_neigh_list) {
433                 nb = list_entry(entry, struct x25_neigh, node);
434                 __x25_remove_neigh(nb);
435         }
436         write_unlock_bh(&x25_neigh_list_lock);
437 }