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