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