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