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