[X25]: Selective sub-address matching with call user data.
[linux-2.6.git] / net / x25 / af_x25.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 Centralised disconnect handling.
19  *                                      New timer architecture.
20  *      2000-03-11      Henner Eisen    MSG_EOR handling more POSIX compliant.
21  *      2000-03-22      Daniela Squassoni Allowed disabling/enabling of 
22  *                                        facilities negotiation and increased 
23  *                                        the throughput upper limit.
24  *      2000-08-27      Arnaldo C. Melo s/suser/capable/ + micro cleanups
25  *      2000-09-04      Henner Eisen    Set sock->state in x25_accept(). 
26  *                                      Fixed x25_output() related skb leakage.
27  *      2000-10-02      Henner Eisen    Made x25_kick() single threaded per socket.
28  *      2000-10-27      Henner Eisen    MSG_DONTWAIT for fragment allocation.
29  *      2000-11-14      Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
30  *      2002-10-06      Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
31  *                                      x25_proc.c, using seq_file
32  *      2005-04-02      Shaun Pereira   Selective sub address matching
33  *                                      with call user data
34  */
35
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <linux/string.h>
43 #include <linux/net.h>
44 #include <linux/netdevice.h>
45 #include <linux/if_arp.h>
46 #include <linux/skbuff.h>
47 #include <net/sock.h>
48 #include <net/tcp.h>
49 #include <asm/uaccess.h>
50 #include <linux/fcntl.h>
51 #include <linux/termios.h>      /* For TIOCINQ/OUTQ */
52 #include <linux/notifier.h>
53 #include <linux/init.h>
54 #include <net/x25.h>
55
56 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
57 int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
58 int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
59 int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
60 int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
61
62 HLIST_HEAD(x25_list);
63 DEFINE_RWLOCK(x25_list_lock);
64
65 static struct proto_ops x25_proto_ops;
66
67 static struct x25_address null_x25_address = {"               "};
68
69 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
70                   struct x25_address *calling_addr)
71 {
72         int called_len, calling_len;
73         char *called, *calling;
74         int i;
75
76         called_len  = (*p >> 0) & 0x0F;
77         calling_len = (*p >> 4) & 0x0F;
78
79         called  = called_addr->x25_addr;
80         calling = calling_addr->x25_addr;
81         p++;
82
83         for (i = 0; i < (called_len + calling_len); i++) {
84                 if (i < called_len) {
85                         if (i % 2 != 0) {
86                                 *called++ = ((*p >> 0) & 0x0F) + '0';
87                                 p++;
88                         } else {
89                                 *called++ = ((*p >> 4) & 0x0F) + '0';
90                         }
91                 } else {
92                         if (i % 2 != 0) {
93                                 *calling++ = ((*p >> 0) & 0x0F) + '0';
94                                 p++;
95                         } else {
96                                 *calling++ = ((*p >> 4) & 0x0F) + '0';
97                         }
98                 }
99         }
100
101         *called = *calling = '\0';
102
103         return 1 + (called_len + calling_len + 1) / 2;
104 }
105
106 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
107                   struct x25_address *calling_addr)
108 {
109         unsigned int called_len, calling_len;
110         char *called, *calling;
111         int i;
112
113         called  = called_addr->x25_addr;
114         calling = calling_addr->x25_addr;
115
116         called_len  = strlen(called);
117         calling_len = strlen(calling);
118
119         *p++ = (calling_len << 4) | (called_len << 0);
120
121         for (i = 0; i < (called_len + calling_len); i++) {
122                 if (i < called_len) {
123                         if (i % 2 != 0) {
124                                 *p |= (*called++ - '0') << 0;
125                                 p++;
126                         } else {
127                                 *p = 0x00;
128                                 *p |= (*called++ - '0') << 4;
129                         }
130                 } else {
131                         if (i % 2 != 0) {
132                                 *p |= (*calling++ - '0') << 0;
133                                 p++;
134                         } else {
135                                 *p = 0x00;
136                                 *p |= (*calling++ - '0') << 4;
137                         }
138                 }
139         }
140
141         return 1 + (called_len + calling_len + 1) / 2;
142 }
143
144 /*
145  *      Socket removal during an interrupt is now safe.
146  */
147 static void x25_remove_socket(struct sock *sk)
148 {
149         write_lock_bh(&x25_list_lock);
150         sk_del_node_init(sk);
151         write_unlock_bh(&x25_list_lock);
152 }
153
154 /*
155  *      Kill all bound sockets on a dropped device.
156  */
157 static void x25_kill_by_device(struct net_device *dev)
158 {
159         struct sock *s;
160         struct hlist_node *node;
161
162         write_lock_bh(&x25_list_lock);
163
164         sk_for_each(s, node, &x25_list)
165                 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
166                         x25_disconnect(s, ENETUNREACH, 0, 0);
167
168         write_unlock_bh(&x25_list_lock);
169 }
170
171 /*
172  *      Handle device status changes.
173  */
174 static int x25_device_event(struct notifier_block *this, unsigned long event,
175                             void *ptr)
176 {
177         struct net_device *dev = ptr;
178         struct x25_neigh *nb;
179
180         if (dev->type == ARPHRD_X25
181 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
182          || dev->type == ARPHRD_ETHER
183 #endif
184          ) {
185                 switch (event) {
186                         case NETDEV_UP:
187                                 x25_link_device_up(dev);
188                                 break;
189                         case NETDEV_GOING_DOWN:
190                                 nb = x25_get_neigh(dev);
191                                 if (nb) {
192                                         x25_terminate_link(nb);
193                                         x25_neigh_put(nb);
194                                 }
195                                 break;
196                         case NETDEV_DOWN:
197                                 x25_kill_by_device(dev);
198                                 x25_route_device_down(dev);
199                                 x25_link_device_down(dev);
200                                 break;
201                 }
202         }
203
204         return NOTIFY_DONE;
205 }
206
207 /*
208  *      Add a socket to the bound sockets list.
209  */
210 static void x25_insert_socket(struct sock *sk)
211 {
212         write_lock_bh(&x25_list_lock);
213         sk_add_node(sk, &x25_list);
214         write_unlock_bh(&x25_list_lock);
215 }
216
217 /*
218  *      Find a socket that wants to accept the Call Request we just
219  *      received. Check the full list for an address/cud match.
220  *      If no cuds match return the next_best thing, an address match.
221  *      Note: if a listening socket has cud set it must only get calls
222  *      with matching cud.
223  */
224 static struct sock *x25_find_listener(struct x25_address *addr,
225                                         struct sk_buff *skb)
226 {
227         struct sock *s;
228         struct sock *next_best;
229         struct hlist_node *node;
230
231         read_lock_bh(&x25_list_lock);
232         next_best = NULL;
233
234         sk_for_each(s, node, &x25_list)
235                 if ((!strcmp(addr->x25_addr,
236                         x25_sk(s)->source_addr.x25_addr) ||
237                                 !strcmp(addr->x25_addr,
238                                         null_x25_address.x25_addr)) &&
239                                         s->sk_state == TCP_LISTEN) {
240                         /*
241                          * Found a listening socket, now check the incoming
242                          * call user data vs this sockets call user data
243                          */
244                         if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
245                                 if((memcmp(x25_sk(s)->calluserdata.cuddata,
246                                         skb->data,
247                                         x25_sk(s)->cudmatchlength)) == 0) {
248                                         sock_hold(s);
249                                         goto found;
250                                  }
251                         } else
252                                 next_best = s;
253                 }
254         if (next_best) {
255                 s = next_best;
256                 sock_hold(s);
257                 goto found;
258         }
259         s = NULL;
260 found:
261         read_unlock_bh(&x25_list_lock);
262         return s;
263 }
264
265 /*
266  *      Find a connected X.25 socket given my LCI and neighbour.
267  */
268 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
269 {
270         struct sock *s;
271         struct hlist_node *node;
272
273         sk_for_each(s, node, &x25_list)
274                 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
275                         sock_hold(s);
276                         goto found;
277                 }
278         s = NULL;
279 found:
280         return s;
281 }
282
283 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
284 {
285         struct sock *s;
286
287         read_lock_bh(&x25_list_lock);
288         s = __x25_find_socket(lci, nb);
289         read_unlock_bh(&x25_list_lock);
290         return s;
291 }
292
293 /*
294  *      Find a unique LCI for a given device.
295  */
296 static unsigned int x25_new_lci(struct x25_neigh *nb)
297 {
298         unsigned int lci = 1;
299         struct sock *sk;
300
301         read_lock_bh(&x25_list_lock);
302
303         while ((sk = __x25_find_socket(lci, nb)) != NULL) {
304                 sock_put(sk);
305                 if (++lci == 4096) {
306                         lci = 0;
307                         break;
308                 }
309         }
310
311         read_unlock_bh(&x25_list_lock);
312         return lci;
313 }
314
315 /*
316  *      Deferred destroy.
317  */
318 void x25_destroy_socket(struct sock *);
319
320 /*
321  *      handler for deferred kills.
322  */
323 static void x25_destroy_timer(unsigned long data)
324 {
325         x25_destroy_socket((struct sock *)data);
326 }
327
328 /*
329  *      This is called from user mode and the timers. Thus it protects itself
330  *      against interrupt users but doesn't worry about being called during
331  *      work. Once it is removed from the queue no interrupt or bottom half
332  *      will touch it and we are (fairly 8-) ) safe.
333  *      Not static as it's used by the timer
334  */
335 void x25_destroy_socket(struct sock *sk)
336 {
337         struct sk_buff *skb;
338
339         sock_hold(sk);
340         lock_sock(sk);
341         x25_stop_heartbeat(sk);
342         x25_stop_timer(sk);
343
344         x25_remove_socket(sk);
345         x25_clear_queues(sk);           /* Flush the queues */
346
347         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
348                 if (skb->sk != sk) {            /* A pending connection */
349                         /*
350                          * Queue the unaccepted socket for death
351                          */
352                         sock_set_flag(skb->sk, SOCK_DEAD);
353                         x25_start_heartbeat(skb->sk);
354                         x25_sk(skb->sk)->state = X25_STATE_0;
355                 }
356
357                 kfree_skb(skb);
358         }
359
360         if (atomic_read(&sk->sk_wmem_alloc) ||
361             atomic_read(&sk->sk_rmem_alloc)) {
362                 /* Defer: outstanding buffers */
363                 sk->sk_timer.expires  = jiffies + 10 * HZ;
364                 sk->sk_timer.function = x25_destroy_timer;
365                 sk->sk_timer.data = (unsigned long)sk;
366                 add_timer(&sk->sk_timer);
367         } else {
368                 /* drop last reference so sock_put will free */
369                 __sock_put(sk);
370         }
371
372         release_sock(sk);
373         sock_put(sk);
374 }
375
376 /*
377  *      Handling for system calls applied via the various interfaces to a
378  *      X.25 socket object.
379  */
380
381 static int x25_setsockopt(struct socket *sock, int level, int optname,
382                           char __user *optval, int optlen)
383 {
384         int opt;
385         struct sock *sk = sock->sk;
386         int rc = -ENOPROTOOPT;
387
388         if (level != SOL_X25 || optname != X25_QBITINCL)
389                 goto out;
390
391         rc = -EINVAL;
392         if (optlen < sizeof(int))
393                 goto out;
394
395         rc = -EFAULT;
396         if (get_user(opt, (int __user *)optval))
397                 goto out;
398
399         x25_sk(sk)->qbitincl = !!opt;
400         rc = 0;
401 out:
402         return rc;
403 }
404
405 static int x25_getsockopt(struct socket *sock, int level, int optname,
406                           char __user *optval, int __user *optlen)
407 {
408         struct sock *sk = sock->sk;
409         int val, len, rc = -ENOPROTOOPT;
410         
411         if (level != SOL_X25 || optname != X25_QBITINCL)
412                 goto out;
413
414         rc = -EFAULT;
415         if (get_user(len, optlen))
416                 goto out;
417
418         len = min_t(unsigned int, len, sizeof(int));
419
420         rc = -EINVAL;
421         if (len < 0)
422                 goto out;
423                 
424         rc = -EFAULT;
425         if (put_user(len, optlen))
426                 goto out;
427
428         val = x25_sk(sk)->qbitincl;
429         rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
430 out:
431         return rc;
432 }
433
434 static int x25_listen(struct socket *sock, int backlog)
435 {
436         struct sock *sk = sock->sk;
437         int rc = -EOPNOTSUPP;
438
439         if (sk->sk_state != TCP_LISTEN) {
440                 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
441                 sk->sk_max_ack_backlog = backlog;
442                 sk->sk_state           = TCP_LISTEN;
443                 rc = 0;
444         }
445
446         return rc;
447 }
448
449 static struct proto x25_proto = {
450         .name     = "X25",
451         .owner    = THIS_MODULE,
452         .obj_size = sizeof(struct x25_sock),
453 };
454
455 static struct sock *x25_alloc_socket(void)
456 {
457         struct x25_sock *x25;
458         struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1);
459
460         if (!sk)
461                 goto out;
462
463         sock_init_data(NULL, sk);
464
465         x25 = x25_sk(sk);
466         skb_queue_head_init(&x25->ack_queue);
467         skb_queue_head_init(&x25->fragment_queue);
468         skb_queue_head_init(&x25->interrupt_in_queue);
469         skb_queue_head_init(&x25->interrupt_out_queue);
470 out:
471         return sk;
472 }
473
474 void x25_init_timers(struct sock *sk);
475
476 static int x25_create(struct socket *sock, int protocol)
477 {
478         struct sock *sk;
479         struct x25_sock *x25;
480         int rc = -ESOCKTNOSUPPORT;
481
482         if (sock->type != SOCK_SEQPACKET || protocol)
483                 goto out;
484
485         rc = -ENOMEM;
486         if ((sk = x25_alloc_socket()) == NULL)
487                 goto out;
488
489         x25 = x25_sk(sk);
490
491         sock_init_data(sock, sk);
492
493         x25_init_timers(sk);
494
495         sock->ops    = &x25_proto_ops;
496         sk->sk_protocol = protocol;
497         sk->sk_backlog_rcv = x25_backlog_rcv;
498
499         x25->t21   = sysctl_x25_call_request_timeout;
500         x25->t22   = sysctl_x25_reset_request_timeout;
501         x25->t23   = sysctl_x25_clear_request_timeout;
502         x25->t2    = sysctl_x25_ack_holdback_timeout;
503         x25->state = X25_STATE_0;
504         x25->cudmatchlength = 0;
505
506         x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
507         x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
508         x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
509         x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
510         x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;
511         x25->facilities.reverse     = X25_DEFAULT_REVERSE;
512         rc = 0;
513 out:
514         return rc;
515 }
516
517 static struct sock *x25_make_new(struct sock *osk)
518 {
519         struct sock *sk = NULL;
520         struct x25_sock *x25, *ox25;
521
522         if (osk->sk_type != SOCK_SEQPACKET)
523                 goto out;
524
525         if ((sk = x25_alloc_socket()) == NULL)
526                 goto out;
527
528         x25 = x25_sk(sk);
529
530         sk->sk_type        = osk->sk_type;
531         sk->sk_socket      = osk->sk_socket;
532         sk->sk_priority    = osk->sk_priority;
533         sk->sk_protocol    = osk->sk_protocol;
534         sk->sk_rcvbuf      = osk->sk_rcvbuf;
535         sk->sk_sndbuf      = osk->sk_sndbuf;
536         sk->sk_state       = TCP_ESTABLISHED;
537         sk->sk_sleep       = osk->sk_sleep;
538         sk->sk_backlog_rcv = osk->sk_backlog_rcv;
539
540         if (sock_flag(osk, SOCK_ZAPPED))
541                 sock_set_flag(sk, SOCK_ZAPPED);
542         
543         if (sock_flag(osk, SOCK_DBG))
544                 sock_set_flag(sk, SOCK_DBG);
545
546         ox25 = x25_sk(osk);
547         x25->t21        = ox25->t21;
548         x25->t22        = ox25->t22;
549         x25->t23        = ox25->t23;
550         x25->t2         = ox25->t2;
551         x25->facilities = ox25->facilities;
552         x25->qbitincl   = ox25->qbitincl;
553         x25->cudmatchlength = ox25->cudmatchlength;
554
555         x25_init_timers(sk);
556 out:
557         return sk;
558 }
559
560 static int x25_release(struct socket *sock)
561 {
562         struct sock *sk = sock->sk;
563         struct x25_sock *x25;
564
565         if (!sk)
566                 goto out;
567
568         x25 = x25_sk(sk);
569
570         switch (x25->state) {
571
572                 case X25_STATE_0:
573                 case X25_STATE_2:
574                         x25_disconnect(sk, 0, 0, 0);
575                         x25_destroy_socket(sk);
576                         goto out;
577
578                 case X25_STATE_1:
579                 case X25_STATE_3:
580                 case X25_STATE_4:
581                         x25_clear_queues(sk);
582                         x25_write_internal(sk, X25_CLEAR_REQUEST);
583                         x25_start_t23timer(sk);
584                         x25->state = X25_STATE_2;
585                         sk->sk_state    = TCP_CLOSE;
586                         sk->sk_shutdown |= SEND_SHUTDOWN;
587                         sk->sk_state_change(sk);
588                         sock_set_flag(sk, SOCK_DEAD);
589                         sock_set_flag(sk, SOCK_DESTROY);
590                         break;
591         }
592
593         sock->sk        = NULL; 
594         sk->sk_socket   = NULL; /* Not used, but we should do this */
595 out:
596         return 0;
597 }
598
599 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
600 {
601         struct sock *sk = sock->sk;
602         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
603
604         if (!sock_flag(sk, SOCK_ZAPPED) ||
605             addr_len != sizeof(struct sockaddr_x25) ||
606             addr->sx25_family != AF_X25)
607                 return -EINVAL;
608
609         x25_sk(sk)->source_addr = addr->sx25_addr;
610         x25_insert_socket(sk);
611         sock_reset_flag(sk, SOCK_ZAPPED);
612         SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
613
614         return 0;
615 }
616
617 static int x25_wait_for_connection_establishment(struct sock *sk)
618 {
619         DECLARE_WAITQUEUE(wait, current);
620         int rc;
621
622         add_wait_queue_exclusive(sk->sk_sleep, &wait);
623         for (;;) {
624                 __set_current_state(TASK_INTERRUPTIBLE);
625                 rc = -ERESTARTSYS;
626                 if (signal_pending(current))
627                         break;
628                 rc = sock_error(sk);
629                 if (rc) {
630                         sk->sk_socket->state = SS_UNCONNECTED;
631                         break;
632                 }
633                 rc = 0;
634                 if (sk->sk_state != TCP_ESTABLISHED) {
635                         release_sock(sk);
636                         schedule();
637                         lock_sock(sk);
638                 } else
639                         break;
640         }
641         __set_current_state(TASK_RUNNING);
642         remove_wait_queue(sk->sk_sleep, &wait);
643         return rc;
644 }
645
646 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
647                        int addr_len, int flags)
648 {
649         struct sock *sk = sock->sk;
650         struct x25_sock *x25 = x25_sk(sk);
651         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
652         struct x25_route *rt;
653         int rc = 0;
654
655         lock_sock(sk);
656         if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
657                 sock->state = SS_CONNECTED;
658                 goto out; /* Connect completed during a ERESTARTSYS event */
659         }
660
661         rc = -ECONNREFUSED;
662         if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
663                 sock->state = SS_UNCONNECTED;
664                 goto out;
665         }
666
667         rc = -EISCONN;  /* No reconnect on a seqpacket socket */
668         if (sk->sk_state == TCP_ESTABLISHED)
669                 goto out;
670
671         sk->sk_state   = TCP_CLOSE;     
672         sock->state = SS_UNCONNECTED;
673
674         rc = -EINVAL;
675         if (addr_len != sizeof(struct sockaddr_x25) ||
676             addr->sx25_family != AF_X25)
677                 goto out;
678
679         rc = -ENETUNREACH;
680         rt = x25_get_route(&addr->sx25_addr);
681         if (!rt)
682                 goto out;
683
684         x25->neighbour = x25_get_neigh(rt->dev);
685         if (!x25->neighbour)
686                 goto out_put_route;
687
688         x25_limit_facilities(&x25->facilities, x25->neighbour);
689
690         x25->lci = x25_new_lci(x25->neighbour);
691         if (!x25->lci)
692                 goto out_put_neigh;
693
694         rc = -EINVAL;
695         if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
696                 goto out_put_neigh;
697
698         if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
699                 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
700
701         x25->dest_addr = addr->sx25_addr;
702
703         /* Move to connecting socket, start sending Connect Requests */
704         sock->state   = SS_CONNECTING;
705         sk->sk_state  = TCP_SYN_SENT;
706
707         x25->state = X25_STATE_1;
708
709         x25_write_internal(sk, X25_CALL_REQUEST);
710
711         x25_start_heartbeat(sk);
712         x25_start_t21timer(sk);
713
714         /* Now the loop */
715         rc = -EINPROGRESS;
716         if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
717                 goto out_put_neigh;
718
719         rc = x25_wait_for_connection_establishment(sk);
720         if (rc)
721                 goto out_put_neigh;
722
723         sock->state = SS_CONNECTED;
724         rc = 0;
725 out_put_neigh:
726         if (rc)
727                 x25_neigh_put(x25->neighbour);
728 out_put_route:
729         x25_route_put(rt);
730 out:
731         release_sock(sk);
732         return rc;
733 }
734
735 static int x25_wait_for_data(struct sock *sk, int timeout)
736 {
737         DECLARE_WAITQUEUE(wait, current);
738         int rc = 0;
739
740         add_wait_queue_exclusive(sk->sk_sleep, &wait);
741         for (;;) {
742                 __set_current_state(TASK_INTERRUPTIBLE);
743                 if (sk->sk_shutdown & RCV_SHUTDOWN)
744                         break;
745                 rc = -ERESTARTSYS;
746                 if (signal_pending(current))
747                         break;
748                 rc = -EAGAIN;
749                 if (!timeout)
750                         break;
751                 rc = 0;
752                 if (skb_queue_empty(&sk->sk_receive_queue)) {
753                         release_sock(sk);
754                         timeout = schedule_timeout(timeout);
755                         lock_sock(sk);
756                 } else
757                         break;
758         }
759         __set_current_state(TASK_RUNNING);
760         remove_wait_queue(sk->sk_sleep, &wait);
761         return rc;
762 }
763         
764 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
765 {
766         struct sock *sk = sock->sk;
767         struct sock *newsk;
768         struct sk_buff *skb;
769         int rc = -EINVAL;
770
771         if (!sk || sk->sk_state != TCP_LISTEN)
772                 goto out;
773
774         rc = -EOPNOTSUPP;
775         if (sk->sk_type != SOCK_SEQPACKET)
776                 goto out;
777
778         lock_sock(sk);
779         rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
780         if (rc)
781                 goto out2;
782         skb = skb_dequeue(&sk->sk_receive_queue);
783         rc = -EINVAL;
784         if (!skb->sk)
785                 goto out2;
786         newsk            = skb->sk;
787         newsk->sk_socket = newsock;
788         newsk->sk_sleep  = &newsock->wait;
789
790         /* Now attach up the new socket */
791         skb->sk = NULL;
792         kfree_skb(skb);
793         sk->sk_ack_backlog--;
794         newsock->sk    = newsk;
795         newsock->state = SS_CONNECTED;
796         rc = 0;
797 out2:
798         release_sock(sk);
799 out:
800         return rc;
801 }
802
803 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
804                        int *uaddr_len, int peer)
805 {
806         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
807         struct sock *sk = sock->sk;
808         struct x25_sock *x25 = x25_sk(sk);
809
810         if (peer) {
811                 if (sk->sk_state != TCP_ESTABLISHED)
812                         return -ENOTCONN;
813                 sx25->sx25_addr = x25->dest_addr;
814         } else
815                 sx25->sx25_addr = x25->source_addr;
816
817         sx25->sx25_family = AF_X25;
818         *uaddr_len = sizeof(*sx25);
819
820         return 0;
821 }
822  
823 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
824                         unsigned int lci)
825 {
826         struct sock *sk;
827         struct sock *make;
828         struct x25_sock *makex25;
829         struct x25_address source_addr, dest_addr;
830         struct x25_facilities facilities;
831         int len, rc;
832
833         /*
834          *      Remove the LCI and frame type.
835          */
836         skb_pull(skb, X25_STD_MIN_LEN);
837
838         /*
839          *      Extract the X.25 addresses and convert them to ASCII strings,
840          *      and remove them.
841          */
842         skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
843
844         /*
845          *      Get the length of the facilities, skip past them for the moment
846          *      get the call user data because this is needed to determine
847          *      the correct listener
848          */
849         len = skb->data[0] + 1;
850         skb_pull(skb,len);
851
852         /*
853          *      Find a listener for the particular address/cud pair.
854          */
855         sk = x25_find_listener(&source_addr,skb);
856         skb_push(skb,len);
857
858         /*
859          *      We can't accept the Call Request.
860          */
861         if (sk == NULL || sk_acceptq_is_full(sk))
862                 goto out_clear_request;
863
864         /*
865          *      Try to reach a compromise on the requested facilities.
866          */
867         if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
868                 goto out_sock_put;
869
870         /*
871          * current neighbour/link might impose additional limits
872          * on certain facilties
873          */
874
875         x25_limit_facilities(&facilities, nb);
876
877         /*
878          *      Try to create a new socket.
879          */
880         make = x25_make_new(sk);
881         if (!make)
882                 goto out_sock_put;
883
884         /*
885          *      Remove the facilities
886          */
887         skb_pull(skb, len);
888
889         skb->sk     = make;
890         make->sk_state = TCP_ESTABLISHED;
891
892         makex25 = x25_sk(make);
893         makex25->lci           = lci;
894         makex25->dest_addr     = dest_addr;
895         makex25->source_addr   = source_addr;
896         makex25->neighbour     = nb;
897         makex25->facilities    = facilities;
898         makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
899         /* ensure no reverse facil on accept */
900         makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
901         makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
902
903         x25_write_internal(make, X25_CALL_ACCEPTED);
904
905         makex25->state = X25_STATE_3;
906
907         /*
908          *      Incoming Call User Data.
909          */
910         if (skb->len >= 0) {
911                 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
912                 makex25->calluserdata.cudlength = skb->len;
913         }
914
915         sk->sk_ack_backlog++;
916
917         x25_insert_socket(make);
918
919         skb_queue_head(&sk->sk_receive_queue, skb);
920
921         x25_start_heartbeat(make);
922
923         if (!sock_flag(sk, SOCK_DEAD))
924                 sk->sk_data_ready(sk, skb->len);
925         rc = 1;
926         sock_put(sk);
927 out:
928         return rc;
929 out_sock_put:
930         sock_put(sk);
931 out_clear_request:
932         rc = 0;
933         x25_transmit_clear_request(nb, lci, 0x01);
934         goto out;
935 }
936
937 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
938                        struct msghdr *msg, size_t len)
939 {
940         struct sock *sk = sock->sk;
941         struct x25_sock *x25 = x25_sk(sk);
942         struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
943         struct sockaddr_x25 sx25;
944         struct sk_buff *skb;
945         unsigned char *asmptr;
946         int noblock = msg->msg_flags & MSG_DONTWAIT;
947         size_t size;
948         int qbit = 0, rc = -EINVAL;
949
950         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
951                 goto out;
952
953         /* we currently don't support segmented records at the user interface */
954         if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
955                 goto out;
956
957         rc = -EADDRNOTAVAIL;
958         if (sock_flag(sk, SOCK_ZAPPED))
959                 goto out;
960
961         rc = -EPIPE;
962         if (sk->sk_shutdown & SEND_SHUTDOWN) {
963                 send_sig(SIGPIPE, current, 0);
964                 goto out;
965         }
966
967         rc = -ENETUNREACH;
968         if (!x25->neighbour)
969                 goto out;
970
971         if (usx25) {
972                 rc = -EINVAL;
973                 if (msg->msg_namelen < sizeof(sx25))
974                         goto out;
975                 memcpy(&sx25, usx25, sizeof(sx25));
976                 rc = -EISCONN;
977                 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
978                         goto out;
979                 rc = -EINVAL;
980                 if (sx25.sx25_family != AF_X25)
981                         goto out;
982         } else {
983                 /*
984                  *      FIXME 1003.1g - if the socket is like this because
985                  *      it has become closed (not started closed) we ought
986                  *      to SIGPIPE, EPIPE;
987                  */
988                 rc = -ENOTCONN;
989                 if (sk->sk_state != TCP_ESTABLISHED)
990                         goto out;
991
992                 sx25.sx25_family = AF_X25;
993                 sx25.sx25_addr   = x25->dest_addr;
994         }
995
996         SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
997
998         /* Build a packet */
999         SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1000
1001         if ((msg->msg_flags & MSG_OOB) && len > 32)
1002                 len = 32;
1003
1004         size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1005
1006         skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1007         if (!skb)
1008                 goto out;
1009         X25_SKB_CB(skb)->flags = msg->msg_flags;
1010
1011         skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1012
1013         /*
1014          *      Put the data on the end
1015          */
1016         SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1017
1018         asmptr = skb->h.raw = skb_put(skb, len);
1019
1020         rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1021         if (rc)
1022                 goto out_kfree_skb;
1023
1024         /*
1025          *      If the Q BIT Include socket option is in force, the first
1026          *      byte of the user data is the logical value of the Q Bit.
1027          */
1028         if (x25->qbitincl) {
1029                 qbit = skb->data[0];
1030                 skb_pull(skb, 1);
1031         }
1032
1033         /*
1034          *      Push down the X.25 header
1035          */
1036         SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1037
1038         if (msg->msg_flags & MSG_OOB) {
1039                 if (x25->neighbour->extended) {
1040                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1041                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1042                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1043                         *asmptr++ = X25_INTERRUPT;
1044                 } else {
1045                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1046                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1047                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1048                         *asmptr++ = X25_INTERRUPT;
1049                 }
1050         } else {
1051                 if (x25->neighbour->extended) {
1052                         /* Build an Extended X.25 header */
1053                         asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
1054                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1055                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1056                         *asmptr++ = X25_DATA;
1057                         *asmptr++ = X25_DATA;
1058                 } else {
1059                         /* Build an Standard X.25 header */
1060                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1061                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1062                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1063                         *asmptr++ = X25_DATA;
1064                 }
1065
1066                 if (qbit)
1067                         skb->data[0] |= X25_Q_BIT;
1068         }
1069
1070         SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1071         SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1072
1073         rc = -ENOTCONN;
1074         if (sk->sk_state != TCP_ESTABLISHED)
1075                 goto out_kfree_skb;
1076
1077         if (msg->msg_flags & MSG_OOB)
1078                 skb_queue_tail(&x25->interrupt_out_queue, skb);
1079         else {
1080                 len = x25_output(sk, skb);
1081                 if (len < 0)
1082                         kfree_skb(skb);
1083                 else if (x25->qbitincl)
1084                         len++;
1085         }
1086
1087         /*
1088          * lock_sock() is currently only used to serialize this x25_kick()
1089          * against input-driven x25_kick() calls. It currently only blocks
1090          * incoming packets for this socket and does not protect against
1091          * any other socket state changes and is not called from anywhere
1092          * else. As x25_kick() cannot block and as long as all socket
1093          * operations are BKL-wrapped, we don't need take to care about
1094          * purging the backlog queue in x25_release().
1095          *
1096          * Using lock_sock() to protect all socket operations entirely
1097          * (and making the whole x25 stack SMP aware) unfortunately would
1098          * require major changes to {send,recv}msg and skb allocation methods.
1099          * -> 2.5 ;)
1100          */
1101         lock_sock(sk);
1102         x25_kick(sk);
1103         release_sock(sk);
1104         rc = len;
1105 out:
1106         return rc;
1107 out_kfree_skb:
1108         kfree_skb(skb);
1109         goto out;
1110 }
1111
1112
1113 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1114                        struct msghdr *msg, size_t size,
1115                        int flags)
1116 {
1117         struct sock *sk = sock->sk;
1118         struct x25_sock *x25 = x25_sk(sk);
1119         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1120         size_t copied;
1121         int qbit;
1122         struct sk_buff *skb;
1123         unsigned char *asmptr;
1124         int rc = -ENOTCONN;
1125
1126         /*
1127          * This works for seqpacket too. The receiver has ordered the queue for
1128          * us! We do one quick check first though
1129          */
1130         if (sk->sk_state != TCP_ESTABLISHED)
1131                 goto out;
1132
1133         if (flags & MSG_OOB) {
1134                 rc = -EINVAL;
1135                 if (sock_flag(sk, SOCK_URGINLINE) ||
1136                     !skb_peek(&x25->interrupt_in_queue))
1137                         goto out;
1138
1139                 skb = skb_dequeue(&x25->interrupt_in_queue);
1140
1141                 skb_pull(skb, X25_STD_MIN_LEN);
1142
1143                 /*
1144                  *      No Q bit information on Interrupt data.
1145                  */
1146                 if (x25->qbitincl) {
1147                         asmptr  = skb_push(skb, 1);
1148                         *asmptr = 0x00;
1149                 }
1150
1151                 msg->msg_flags |= MSG_OOB;
1152         } else {
1153                 /* Now we can treat all alike */
1154                 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1155                                         flags & MSG_DONTWAIT, &rc);
1156                 if (!skb)
1157                         goto out;
1158
1159                 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1160
1161                 skb_pull(skb, x25->neighbour->extended ?
1162                                 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1163
1164                 if (x25->qbitincl) {
1165                         asmptr  = skb_push(skb, 1);
1166                         *asmptr = qbit;
1167                 }
1168         }
1169
1170         skb->h.raw = skb->data;
1171
1172         copied = skb->len;
1173
1174         if (copied > size) {
1175                 copied = size;
1176                 msg->msg_flags |= MSG_TRUNC;
1177         }
1178
1179         /* Currently, each datagram always contains a complete record */ 
1180         msg->msg_flags |= MSG_EOR;
1181
1182         rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1183         if (rc)
1184                 goto out_free_dgram;
1185
1186         if (sx25) {
1187                 sx25->sx25_family = AF_X25;
1188                 sx25->sx25_addr   = x25->dest_addr;
1189         }
1190
1191         msg->msg_namelen = sizeof(struct sockaddr_x25);
1192
1193         lock_sock(sk);
1194         x25_check_rbuf(sk);
1195         release_sock(sk);
1196         rc = copied;
1197 out_free_dgram:
1198         skb_free_datagram(sk, skb);
1199 out:
1200         return rc;
1201 }
1202
1203
1204 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1205 {
1206         struct sock *sk = sock->sk;
1207         struct x25_sock *x25 = x25_sk(sk);
1208         void __user *argp = (void __user *)arg;
1209         int rc;
1210
1211         switch (cmd) {
1212                 case TIOCOUTQ: {
1213                         int amount = sk->sk_sndbuf -
1214                                      atomic_read(&sk->sk_wmem_alloc);
1215                         if (amount < 0)
1216                                 amount = 0;
1217                         rc = put_user(amount, (unsigned int __user *)argp);
1218                         break;
1219                 }
1220
1221                 case TIOCINQ: {
1222                         struct sk_buff *skb;
1223                         int amount = 0;
1224                         /*
1225                          * These two are safe on a single CPU system as
1226                          * only user tasks fiddle here
1227                          */
1228                         if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1229                                 amount = skb->len;
1230                         rc = put_user(amount, (unsigned int __user *)argp);
1231                         break;
1232                 }
1233
1234                 case SIOCGSTAMP:
1235                         rc = -EINVAL;
1236                         if (sk)
1237                                 rc = sock_get_timestamp(sk, 
1238                                                 (struct timeval __user *)argp); 
1239                         break;
1240                 case SIOCGIFADDR:
1241                 case SIOCSIFADDR:
1242                 case SIOCGIFDSTADDR:
1243                 case SIOCSIFDSTADDR:
1244                 case SIOCGIFBRDADDR:
1245                 case SIOCSIFBRDADDR:
1246                 case SIOCGIFNETMASK:
1247                 case SIOCSIFNETMASK:
1248                 case SIOCGIFMETRIC:
1249                 case SIOCSIFMETRIC:
1250                         rc = -EINVAL;
1251                         break;
1252                 case SIOCADDRT:
1253                 case SIOCDELRT:
1254                         rc = -EPERM;
1255                         if (!capable(CAP_NET_ADMIN))
1256                                 break;
1257                         rc = x25_route_ioctl(cmd, argp);
1258                         break;
1259                 case SIOCX25GSUBSCRIP:
1260                         rc = x25_subscr_ioctl(cmd, argp);
1261                         break;
1262                 case SIOCX25SSUBSCRIP:
1263                         rc = -EPERM;
1264                         if (!capable(CAP_NET_ADMIN))
1265                                 break;
1266                         rc = x25_subscr_ioctl(cmd, argp);
1267                         break;
1268                 case SIOCX25GFACILITIES: {
1269                         struct x25_facilities fac = x25->facilities;
1270                         rc = copy_to_user(argp, &fac,
1271                                           sizeof(fac)) ? -EFAULT : 0;
1272                         break;
1273                 }
1274
1275                 case SIOCX25SFACILITIES: {
1276                         struct x25_facilities facilities;
1277                         rc = -EFAULT;
1278                         if (copy_from_user(&facilities, argp,
1279                                            sizeof(facilities)))
1280                                 break;
1281                         rc = -EINVAL;
1282                         if (sk->sk_state != TCP_LISTEN &&
1283                             sk->sk_state != TCP_CLOSE)
1284                                 break;
1285                         if (facilities.pacsize_in < X25_PS16 ||
1286                             facilities.pacsize_in > X25_PS4096)
1287                                 break;
1288                         if (facilities.pacsize_out < X25_PS16 ||
1289                             facilities.pacsize_out > X25_PS4096)
1290                                 break;
1291                         if (facilities.winsize_in < 1 ||
1292                             facilities.winsize_in > 127)
1293                                 break;
1294                         if (facilities.throughput < 0x03 ||
1295                             facilities.throughput > 0xDD)
1296                                 break;
1297                         if (facilities.reverse && facilities.reverse != 1)
1298                                 break;
1299                         x25->facilities = facilities;
1300                         rc = 0;
1301                         break;
1302                 }
1303
1304                 case SIOCX25GCALLUSERDATA: {
1305                         struct x25_calluserdata cud = x25->calluserdata;
1306                         rc = copy_to_user(argp, &cud,
1307                                           sizeof(cud)) ? -EFAULT : 0;
1308                         break;
1309                 }
1310
1311                 case SIOCX25SCALLUSERDATA: {
1312                         struct x25_calluserdata calluserdata;
1313
1314                         rc = -EFAULT;
1315                         if (copy_from_user(&calluserdata, argp,
1316                                            sizeof(calluserdata)))
1317                                 break;
1318                         rc = -EINVAL;
1319                         if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1320                                 break;
1321                         x25->calluserdata = calluserdata;
1322                         rc = 0;
1323                         break;
1324                 }
1325
1326                 case SIOCX25GCAUSEDIAG: {
1327                         struct x25_causediag causediag;
1328                         causediag = x25->causediag;
1329                         rc = copy_to_user(argp, &causediag,
1330                                           sizeof(causediag)) ? -EFAULT : 0;
1331                         break;
1332                 }
1333
1334                 case SIOCX25SCUDMATCHLEN: {
1335                         struct x25_subaddr sub_addr;
1336                         rc = -EINVAL;
1337                         if(sk->sk_state != TCP_CLOSE)
1338                                 break;
1339                         rc = -EFAULT;
1340                         if (copy_from_user(&sub_addr, argp,
1341                                         sizeof(sub_addr)))
1342                                 break;
1343                         rc = -EINVAL;
1344                         if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1345                                 break;
1346                         x25->cudmatchlength = sub_addr.cudmatchlength;
1347                         rc = 0;
1348                         break;
1349                 }
1350
1351                 default:
1352                         rc = dev_ioctl(cmd, argp);
1353                         break;
1354         }
1355
1356         return rc;
1357 }
1358
1359 static struct net_proto_family x25_family_ops = {
1360         .family =       AF_X25,
1361         .create =       x25_create,
1362         .owner  =       THIS_MODULE,
1363 };
1364
1365 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1366         .family =       AF_X25,
1367         .owner =        THIS_MODULE,
1368         .release =      x25_release,
1369         .bind =         x25_bind,
1370         .connect =      x25_connect,
1371         .socketpair =   sock_no_socketpair,
1372         .accept =       x25_accept,
1373         .getname =      x25_getname,
1374         .poll =         datagram_poll,
1375         .ioctl =        x25_ioctl,
1376         .listen =       x25_listen,
1377         .shutdown =     sock_no_shutdown,
1378         .setsockopt =   x25_setsockopt,
1379         .getsockopt =   x25_getsockopt,
1380         .sendmsg =      x25_sendmsg,
1381         .recvmsg =      x25_recvmsg,
1382         .mmap =         sock_no_mmap,
1383         .sendpage =     sock_no_sendpage,
1384 };
1385
1386 #include <linux/smp_lock.h>
1387 SOCKOPS_WRAP(x25_proto, AF_X25);
1388
1389 static struct packet_type x25_packet_type = {
1390         .type = __constant_htons(ETH_P_X25),
1391         .func = x25_lapb_receive_frame,
1392 };
1393
1394 static struct notifier_block x25_dev_notifier = {
1395         .notifier_call = x25_device_event,
1396 };
1397
1398 void x25_kill_by_neigh(struct x25_neigh *nb)
1399 {
1400         struct sock *s;
1401         struct hlist_node *node;
1402
1403         write_lock_bh(&x25_list_lock);
1404
1405         sk_for_each(s, node, &x25_list)
1406                 if (x25_sk(s)->neighbour == nb)
1407                         x25_disconnect(s, ENETUNREACH, 0, 0);
1408
1409         write_unlock_bh(&x25_list_lock);
1410 }
1411
1412 static int __init x25_init(void)
1413 {
1414         int rc = proto_register(&x25_proto, 0);
1415
1416         if (rc != 0)
1417                 goto out;
1418
1419         sock_register(&x25_family_ops);
1420
1421         dev_add_pack(&x25_packet_type);
1422
1423         register_netdevice_notifier(&x25_dev_notifier);
1424
1425         printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1426
1427 #ifdef CONFIG_SYSCTL
1428         x25_register_sysctl();
1429 #endif
1430         x25_proc_init();
1431 out:
1432         return rc;
1433 }
1434 module_init(x25_init);
1435
1436 static void __exit x25_exit(void)
1437 {
1438         x25_proc_exit();
1439         x25_link_free();
1440         x25_route_free();
1441
1442 #ifdef CONFIG_SYSCTL
1443         x25_unregister_sysctl();
1444 #endif
1445
1446         unregister_netdevice_notifier(&x25_dev_notifier);
1447
1448         dev_remove_pack(&x25_packet_type);
1449
1450         sock_unregister(AF_X25);
1451         proto_unregister(&x25_proto);
1452 }
1453 module_exit(x25_exit);
1454
1455 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1456 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1457 MODULE_LICENSE("GPL");
1458 MODULE_ALIAS_NETPROTO(PF_X25);