[X25]: proper prototype for x25_init_timers()
[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 static int x25_create(struct socket *sock, int protocol)
488 {
489         struct sock *sk;
490         struct x25_sock *x25;
491         int rc = -ESOCKTNOSUPPORT;
492
493         if (sock->type != SOCK_SEQPACKET || protocol)
494                 goto out;
495
496         rc = -ENOMEM;
497         if ((sk = x25_alloc_socket()) == NULL)
498                 goto out;
499
500         x25 = x25_sk(sk);
501
502         sock_init_data(sock, sk);
503
504         x25_init_timers(sk);
505
506         sock->ops    = &x25_proto_ops;
507         sk->sk_protocol = protocol;
508         sk->sk_backlog_rcv = x25_backlog_rcv;
509
510         x25->t21   = sysctl_x25_call_request_timeout;
511         x25->t22   = sysctl_x25_reset_request_timeout;
512         x25->t23   = sysctl_x25_clear_request_timeout;
513         x25->t2    = sysctl_x25_ack_holdback_timeout;
514         x25->state = X25_STATE_0;
515         x25->cudmatchlength = 0;
516         x25->accptapprv = X25_DENY_ACCPT_APPRV;         /* normally no cud  */
517                                                         /* on call accept   */
518
519         x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
520         x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
521         x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
522         x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
523         x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;
524         x25->facilities.reverse     = X25_DEFAULT_REVERSE;
525         x25->dte_facilities.calling_len = 0;
526         x25->dte_facilities.called_len = 0;
527         memset(x25->dte_facilities.called_ae, '\0',
528                         sizeof(x25->dte_facilities.called_ae));
529         memset(x25->dte_facilities.calling_ae, '\0',
530                         sizeof(x25->dte_facilities.calling_ae));
531
532         rc = 0;
533 out:
534         return rc;
535 }
536
537 static struct sock *x25_make_new(struct sock *osk)
538 {
539         struct sock *sk = NULL;
540         struct x25_sock *x25, *ox25;
541
542         if (osk->sk_type != SOCK_SEQPACKET)
543                 goto out;
544
545         if ((sk = x25_alloc_socket()) == NULL)
546                 goto out;
547
548         x25 = x25_sk(sk);
549
550         sk->sk_type        = osk->sk_type;
551         sk->sk_socket      = osk->sk_socket;
552         sk->sk_priority    = osk->sk_priority;
553         sk->sk_protocol    = osk->sk_protocol;
554         sk->sk_rcvbuf      = osk->sk_rcvbuf;
555         sk->sk_sndbuf      = osk->sk_sndbuf;
556         sk->sk_state       = TCP_ESTABLISHED;
557         sk->sk_sleep       = osk->sk_sleep;
558         sk->sk_backlog_rcv = osk->sk_backlog_rcv;
559         sock_copy_flags(sk, osk);
560
561         ox25 = x25_sk(osk);
562         x25->t21        = ox25->t21;
563         x25->t22        = ox25->t22;
564         x25->t23        = ox25->t23;
565         x25->t2         = ox25->t2;
566         x25->facilities = ox25->facilities;
567         x25->qbitincl   = ox25->qbitincl;
568         x25->dte_facilities = ox25->dte_facilities;
569         x25->cudmatchlength = ox25->cudmatchlength;
570         x25->accptapprv = ox25->accptapprv;
571
572         x25_init_timers(sk);
573 out:
574         return sk;
575 }
576
577 static int x25_release(struct socket *sock)
578 {
579         struct sock *sk = sock->sk;
580         struct x25_sock *x25;
581
582         if (!sk)
583                 goto out;
584
585         x25 = x25_sk(sk);
586
587         switch (x25->state) {
588
589                 case X25_STATE_0:
590                 case X25_STATE_2:
591                         x25_disconnect(sk, 0, 0, 0);
592                         x25_destroy_socket(sk);
593                         goto out;
594
595                 case X25_STATE_1:
596                 case X25_STATE_3:
597                 case X25_STATE_4:
598                         x25_clear_queues(sk);
599                         x25_write_internal(sk, X25_CLEAR_REQUEST);
600                         x25_start_t23timer(sk);
601                         x25->state = X25_STATE_2;
602                         sk->sk_state    = TCP_CLOSE;
603                         sk->sk_shutdown |= SEND_SHUTDOWN;
604                         sk->sk_state_change(sk);
605                         sock_set_flag(sk, SOCK_DEAD);
606                         sock_set_flag(sk, SOCK_DESTROY);
607                         break;
608         }
609
610         sock->sk        = NULL; 
611         sk->sk_socket   = NULL; /* Not used, but we should do this */
612 out:
613         return 0;
614 }
615
616 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
617 {
618         struct sock *sk = sock->sk;
619         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
620
621         if (!sock_flag(sk, SOCK_ZAPPED) ||
622             addr_len != sizeof(struct sockaddr_x25) ||
623             addr->sx25_family != AF_X25)
624                 return -EINVAL;
625
626         x25_sk(sk)->source_addr = addr->sx25_addr;
627         x25_insert_socket(sk);
628         sock_reset_flag(sk, SOCK_ZAPPED);
629         SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
630
631         return 0;
632 }
633
634 static int x25_wait_for_connection_establishment(struct sock *sk)
635 {
636         DECLARE_WAITQUEUE(wait, current);
637         int rc;
638
639         add_wait_queue_exclusive(sk->sk_sleep, &wait);
640         for (;;) {
641                 __set_current_state(TASK_INTERRUPTIBLE);
642                 rc = -ERESTARTSYS;
643                 if (signal_pending(current))
644                         break;
645                 rc = sock_error(sk);
646                 if (rc) {
647                         sk->sk_socket->state = SS_UNCONNECTED;
648                         break;
649                 }
650                 rc = 0;
651                 if (sk->sk_state != TCP_ESTABLISHED) {
652                         release_sock(sk);
653                         schedule();
654                         lock_sock(sk);
655                 } else
656                         break;
657         }
658         __set_current_state(TASK_RUNNING);
659         remove_wait_queue(sk->sk_sleep, &wait);
660         return rc;
661 }
662
663 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
664                        int addr_len, int flags)
665 {
666         struct sock *sk = sock->sk;
667         struct x25_sock *x25 = x25_sk(sk);
668         struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
669         struct x25_route *rt;
670         int rc = 0;
671
672         lock_sock(sk);
673         if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
674                 sock->state = SS_CONNECTED;
675                 goto out; /* Connect completed during a ERESTARTSYS event */
676         }
677
678         rc = -ECONNREFUSED;
679         if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
680                 sock->state = SS_UNCONNECTED;
681                 goto out;
682         }
683
684         rc = -EISCONN;  /* No reconnect on a seqpacket socket */
685         if (sk->sk_state == TCP_ESTABLISHED)
686                 goto out;
687
688         sk->sk_state   = TCP_CLOSE;     
689         sock->state = SS_UNCONNECTED;
690
691         rc = -EINVAL;
692         if (addr_len != sizeof(struct sockaddr_x25) ||
693             addr->sx25_family != AF_X25)
694                 goto out;
695
696         rc = -ENETUNREACH;
697         rt = x25_get_route(&addr->sx25_addr);
698         if (!rt)
699                 goto out;
700
701         x25->neighbour = x25_get_neigh(rt->dev);
702         if (!x25->neighbour)
703                 goto out_put_route;
704
705         x25_limit_facilities(&x25->facilities, x25->neighbour);
706
707         x25->lci = x25_new_lci(x25->neighbour);
708         if (!x25->lci)
709                 goto out_put_neigh;
710
711         rc = -EINVAL;
712         if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
713                 goto out_put_neigh;
714
715         if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
716                 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
717
718         x25->dest_addr = addr->sx25_addr;
719
720         /* Move to connecting socket, start sending Connect Requests */
721         sock->state   = SS_CONNECTING;
722         sk->sk_state  = TCP_SYN_SENT;
723
724         x25->state = X25_STATE_1;
725
726         x25_write_internal(sk, X25_CALL_REQUEST);
727
728         x25_start_heartbeat(sk);
729         x25_start_t21timer(sk);
730
731         /* Now the loop */
732         rc = -EINPROGRESS;
733         if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
734                 goto out_put_neigh;
735
736         rc = x25_wait_for_connection_establishment(sk);
737         if (rc)
738                 goto out_put_neigh;
739
740         sock->state = SS_CONNECTED;
741         rc = 0;
742 out_put_neigh:
743         if (rc)
744                 x25_neigh_put(x25->neighbour);
745 out_put_route:
746         x25_route_put(rt);
747 out:
748         release_sock(sk);
749         return rc;
750 }
751
752 static int x25_wait_for_data(struct sock *sk, long timeout)
753 {
754         DECLARE_WAITQUEUE(wait, current);
755         int rc = 0;
756
757         add_wait_queue_exclusive(sk->sk_sleep, &wait);
758         for (;;) {
759                 __set_current_state(TASK_INTERRUPTIBLE);
760                 if (sk->sk_shutdown & RCV_SHUTDOWN)
761                         break;
762                 rc = -ERESTARTSYS;
763                 if (signal_pending(current))
764                         break;
765                 rc = -EAGAIN;
766                 if (!timeout)
767                         break;
768                 rc = 0;
769                 if (skb_queue_empty(&sk->sk_receive_queue)) {
770                         release_sock(sk);
771                         timeout = schedule_timeout(timeout);
772                         lock_sock(sk);
773                 } else
774                         break;
775         }
776         __set_current_state(TASK_RUNNING);
777         remove_wait_queue(sk->sk_sleep, &wait);
778         return rc;
779 }
780         
781 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
782 {
783         struct sock *sk = sock->sk;
784         struct sock *newsk;
785         struct sk_buff *skb;
786         int rc = -EINVAL;
787
788         if (!sk || sk->sk_state != TCP_LISTEN)
789                 goto out;
790
791         rc = -EOPNOTSUPP;
792         if (sk->sk_type != SOCK_SEQPACKET)
793                 goto out;
794
795         lock_sock(sk);
796         rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
797         if (rc)
798                 goto out2;
799         skb = skb_dequeue(&sk->sk_receive_queue);
800         rc = -EINVAL;
801         if (!skb->sk)
802                 goto out2;
803         newsk            = skb->sk;
804         newsk->sk_socket = newsock;
805         newsk->sk_sleep  = &newsock->wait;
806
807         /* Now attach up the new socket */
808         skb->sk = NULL;
809         kfree_skb(skb);
810         sk->sk_ack_backlog--;
811         newsock->sk    = newsk;
812         newsock->state = SS_CONNECTED;
813         rc = 0;
814 out2:
815         release_sock(sk);
816 out:
817         return rc;
818 }
819
820 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
821                        int *uaddr_len, int peer)
822 {
823         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
824         struct sock *sk = sock->sk;
825         struct x25_sock *x25 = x25_sk(sk);
826
827         if (peer) {
828                 if (sk->sk_state != TCP_ESTABLISHED)
829                         return -ENOTCONN;
830                 sx25->sx25_addr = x25->dest_addr;
831         } else
832                 sx25->sx25_addr = x25->source_addr;
833
834         sx25->sx25_family = AF_X25;
835         *uaddr_len = sizeof(*sx25);
836
837         return 0;
838 }
839  
840 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
841                         unsigned int lci)
842 {
843         struct sock *sk;
844         struct sock *make;
845         struct x25_sock *makex25;
846         struct x25_address source_addr, dest_addr;
847         struct x25_facilities facilities;
848         struct x25_dte_facilities dte_facilities;
849         int len, rc;
850
851         /*
852          *      Remove the LCI and frame type.
853          */
854         skb_pull(skb, X25_STD_MIN_LEN);
855
856         /*
857          *      Extract the X.25 addresses and convert them to ASCII strings,
858          *      and remove them.
859          */
860         skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
861
862         /*
863          *      Get the length of the facilities, skip past them for the moment
864          *      get the call user data because this is needed to determine
865          *      the correct listener
866          */
867         len = skb->data[0] + 1;
868         skb_pull(skb,len);
869
870         /*
871          *      Find a listener for the particular address/cud pair.
872          */
873         sk = x25_find_listener(&source_addr,skb);
874         skb_push(skb,len);
875
876         /*
877          *      We can't accept the Call Request.
878          */
879         if (sk == NULL || sk_acceptq_is_full(sk))
880                 goto out_clear_request;
881
882         /*
883          *      Try to reach a compromise on the requested facilities.
884          */
885         len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
886         if (len == -1)
887                 goto out_sock_put;
888
889         /*
890          * current neighbour/link might impose additional limits
891          * on certain facilties
892          */
893
894         x25_limit_facilities(&facilities, nb);
895
896         /*
897          *      Try to create a new socket.
898          */
899         make = x25_make_new(sk);
900         if (!make)
901                 goto out_sock_put;
902
903         /*
904          *      Remove the facilities
905          */
906         skb_pull(skb, len);
907
908         skb->sk     = make;
909         make->sk_state = TCP_ESTABLISHED;
910
911         makex25 = x25_sk(make);
912         makex25->lci           = lci;
913         makex25->dest_addr     = dest_addr;
914         makex25->source_addr   = source_addr;
915         makex25->neighbour     = nb;
916         makex25->facilities    = facilities;
917         makex25->dte_facilities= dte_facilities;
918         makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
919         /* ensure no reverse facil on accept */
920         makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
921         /* ensure no calling address extension on accept */
922         makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
923         makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
924
925         /* Normally all calls are accepted immediatly */
926         if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
927                 x25_write_internal(make, X25_CALL_ACCEPTED);
928                 makex25->state = X25_STATE_3;
929         }
930
931         /*
932          *      Incoming Call User Data.
933          */
934         if (skb->len >= 0) {
935                 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
936                 makex25->calluserdata.cudlength = skb->len;
937         }
938
939         sk->sk_ack_backlog++;
940
941         x25_insert_socket(make);
942
943         skb_queue_head(&sk->sk_receive_queue, skb);
944
945         x25_start_heartbeat(make);
946
947         if (!sock_flag(sk, SOCK_DEAD))
948                 sk->sk_data_ready(sk, skb->len);
949         rc = 1;
950         sock_put(sk);
951 out:
952         return rc;
953 out_sock_put:
954         sock_put(sk);
955 out_clear_request:
956         rc = 0;
957         x25_transmit_clear_request(nb, lci, 0x01);
958         goto out;
959 }
960
961 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
962                        struct msghdr *msg, size_t len)
963 {
964         struct sock *sk = sock->sk;
965         struct x25_sock *x25 = x25_sk(sk);
966         struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
967         struct sockaddr_x25 sx25;
968         struct sk_buff *skb;
969         unsigned char *asmptr;
970         int noblock = msg->msg_flags & MSG_DONTWAIT;
971         size_t size;
972         int qbit = 0, rc = -EINVAL;
973
974         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
975                 goto out;
976
977         /* we currently don't support segmented records at the user interface */
978         if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
979                 goto out;
980
981         rc = -EADDRNOTAVAIL;
982         if (sock_flag(sk, SOCK_ZAPPED))
983                 goto out;
984
985         rc = -EPIPE;
986         if (sk->sk_shutdown & SEND_SHUTDOWN) {
987                 send_sig(SIGPIPE, current, 0);
988                 goto out;
989         }
990
991         rc = -ENETUNREACH;
992         if (!x25->neighbour)
993                 goto out;
994
995         if (usx25) {
996                 rc = -EINVAL;
997                 if (msg->msg_namelen < sizeof(sx25))
998                         goto out;
999                 memcpy(&sx25, usx25, sizeof(sx25));
1000                 rc = -EISCONN;
1001                 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1002                         goto out;
1003                 rc = -EINVAL;
1004                 if (sx25.sx25_family != AF_X25)
1005                         goto out;
1006         } else {
1007                 /*
1008                  *      FIXME 1003.1g - if the socket is like this because
1009                  *      it has become closed (not started closed) we ought
1010                  *      to SIGPIPE, EPIPE;
1011                  */
1012                 rc = -ENOTCONN;
1013                 if (sk->sk_state != TCP_ESTABLISHED)
1014                         goto out;
1015
1016                 sx25.sx25_family = AF_X25;
1017                 sx25.sx25_addr   = x25->dest_addr;
1018         }
1019
1020         SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1021
1022         /* Build a packet */
1023         SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1024
1025         if ((msg->msg_flags & MSG_OOB) && len > 32)
1026                 len = 32;
1027
1028         size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1029
1030         skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1031         if (!skb)
1032                 goto out;
1033         X25_SKB_CB(skb)->flags = msg->msg_flags;
1034
1035         skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1036
1037         /*
1038          *      Put the data on the end
1039          */
1040         SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1041
1042         asmptr = skb->h.raw = skb_put(skb, len);
1043
1044         rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1045         if (rc)
1046                 goto out_kfree_skb;
1047
1048         /*
1049          *      If the Q BIT Include socket option is in force, the first
1050          *      byte of the user data is the logical value of the Q Bit.
1051          */
1052         if (x25->qbitincl) {
1053                 qbit = skb->data[0];
1054                 skb_pull(skb, 1);
1055         }
1056
1057         /*
1058          *      Push down the X.25 header
1059          */
1060         SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1061
1062         if (msg->msg_flags & MSG_OOB) {
1063                 if (x25->neighbour->extended) {
1064                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1065                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1066                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1067                         *asmptr++ = X25_INTERRUPT;
1068                 } else {
1069                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1070                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1071                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1072                         *asmptr++ = X25_INTERRUPT;
1073                 }
1074         } else {
1075                 if (x25->neighbour->extended) {
1076                         /* Build an Extended X.25 header */
1077                         asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
1078                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1079                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1080                         *asmptr++ = X25_DATA;
1081                         *asmptr++ = X25_DATA;
1082                 } else {
1083                         /* Build an Standard X.25 header */
1084                         asmptr    = skb_push(skb, X25_STD_MIN_LEN);
1085                         *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1086                         *asmptr++ = (x25->lci >> 0) & 0xFF;
1087                         *asmptr++ = X25_DATA;
1088                 }
1089
1090                 if (qbit)
1091                         skb->data[0] |= X25_Q_BIT;
1092         }
1093
1094         SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1095         SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1096
1097         rc = -ENOTCONN;
1098         if (sk->sk_state != TCP_ESTABLISHED)
1099                 goto out_kfree_skb;
1100
1101         if (msg->msg_flags & MSG_OOB)
1102                 skb_queue_tail(&x25->interrupt_out_queue, skb);
1103         else {
1104                 len = x25_output(sk, skb);
1105                 if (len < 0)
1106                         kfree_skb(skb);
1107                 else if (x25->qbitincl)
1108                         len++;
1109         }
1110
1111         /*
1112          * lock_sock() is currently only used to serialize this x25_kick()
1113          * against input-driven x25_kick() calls. It currently only blocks
1114          * incoming packets for this socket and does not protect against
1115          * any other socket state changes and is not called from anywhere
1116          * else. As x25_kick() cannot block and as long as all socket
1117          * operations are BKL-wrapped, we don't need take to care about
1118          * purging the backlog queue in x25_release().
1119          *
1120          * Using lock_sock() to protect all socket operations entirely
1121          * (and making the whole x25 stack SMP aware) unfortunately would
1122          * require major changes to {send,recv}msg and skb allocation methods.
1123          * -> 2.5 ;)
1124          */
1125         lock_sock(sk);
1126         x25_kick(sk);
1127         release_sock(sk);
1128         rc = len;
1129 out:
1130         return rc;
1131 out_kfree_skb:
1132         kfree_skb(skb);
1133         goto out;
1134 }
1135
1136
1137 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1138                        struct msghdr *msg, size_t size,
1139                        int flags)
1140 {
1141         struct sock *sk = sock->sk;
1142         struct x25_sock *x25 = x25_sk(sk);
1143         struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1144         size_t copied;
1145         int qbit;
1146         struct sk_buff *skb;
1147         unsigned char *asmptr;
1148         int rc = -ENOTCONN;
1149
1150         /*
1151          * This works for seqpacket too. The receiver has ordered the queue for
1152          * us! We do one quick check first though
1153          */
1154         if (sk->sk_state != TCP_ESTABLISHED)
1155                 goto out;
1156
1157         if (flags & MSG_OOB) {
1158                 rc = -EINVAL;
1159                 if (sock_flag(sk, SOCK_URGINLINE) ||
1160                     !skb_peek(&x25->interrupt_in_queue))
1161                         goto out;
1162
1163                 skb = skb_dequeue(&x25->interrupt_in_queue);
1164
1165                 skb_pull(skb, X25_STD_MIN_LEN);
1166
1167                 /*
1168                  *      No Q bit information on Interrupt data.
1169                  */
1170                 if (x25->qbitincl) {
1171                         asmptr  = skb_push(skb, 1);
1172                         *asmptr = 0x00;
1173                 }
1174
1175                 msg->msg_flags |= MSG_OOB;
1176         } else {
1177                 /* Now we can treat all alike */
1178                 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1179                                         flags & MSG_DONTWAIT, &rc);
1180                 if (!skb)
1181                         goto out;
1182
1183                 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1184
1185                 skb_pull(skb, x25->neighbour->extended ?
1186                                 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1187
1188                 if (x25->qbitincl) {
1189                         asmptr  = skb_push(skb, 1);
1190                         *asmptr = qbit;
1191                 }
1192         }
1193
1194         skb->h.raw = skb->data;
1195
1196         copied = skb->len;
1197
1198         if (copied > size) {
1199                 copied = size;
1200                 msg->msg_flags |= MSG_TRUNC;
1201         }
1202
1203         /* Currently, each datagram always contains a complete record */ 
1204         msg->msg_flags |= MSG_EOR;
1205
1206         rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1207         if (rc)
1208                 goto out_free_dgram;
1209
1210         if (sx25) {
1211                 sx25->sx25_family = AF_X25;
1212                 sx25->sx25_addr   = x25->dest_addr;
1213         }
1214
1215         msg->msg_namelen = sizeof(struct sockaddr_x25);
1216
1217         lock_sock(sk);
1218         x25_check_rbuf(sk);
1219         release_sock(sk);
1220         rc = copied;
1221 out_free_dgram:
1222         skb_free_datagram(sk, skb);
1223 out:
1224         return rc;
1225 }
1226
1227
1228 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1229 {
1230         struct sock *sk = sock->sk;
1231         struct x25_sock *x25 = x25_sk(sk);
1232         void __user *argp = (void __user *)arg;
1233         int rc;
1234
1235         switch (cmd) {
1236                 case TIOCOUTQ: {
1237                         int amount = sk->sk_sndbuf -
1238                                      atomic_read(&sk->sk_wmem_alloc);
1239                         if (amount < 0)
1240                                 amount = 0;
1241                         rc = put_user(amount, (unsigned int __user *)argp);
1242                         break;
1243                 }
1244
1245                 case TIOCINQ: {
1246                         struct sk_buff *skb;
1247                         int amount = 0;
1248                         /*
1249                          * These two are safe on a single CPU system as
1250                          * only user tasks fiddle here
1251                          */
1252                         if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1253                                 amount = skb->len;
1254                         rc = put_user(amount, (unsigned int __user *)argp);
1255                         break;
1256                 }
1257
1258                 case SIOCGSTAMP:
1259                         rc = -EINVAL;
1260                         if (sk)
1261                                 rc = sock_get_timestamp(sk, 
1262                                                 (struct timeval __user *)argp); 
1263                         break;
1264                 case SIOCGIFADDR:
1265                 case SIOCSIFADDR:
1266                 case SIOCGIFDSTADDR:
1267                 case SIOCSIFDSTADDR:
1268                 case SIOCGIFBRDADDR:
1269                 case SIOCSIFBRDADDR:
1270                 case SIOCGIFNETMASK:
1271                 case SIOCSIFNETMASK:
1272                 case SIOCGIFMETRIC:
1273                 case SIOCSIFMETRIC:
1274                         rc = -EINVAL;
1275                         break;
1276                 case SIOCADDRT:
1277                 case SIOCDELRT:
1278                         rc = -EPERM;
1279                         if (!capable(CAP_NET_ADMIN))
1280                                 break;
1281                         rc = x25_route_ioctl(cmd, argp);
1282                         break;
1283                 case SIOCX25GSUBSCRIP:
1284                         rc = x25_subscr_ioctl(cmd, argp);
1285                         break;
1286                 case SIOCX25SSUBSCRIP:
1287                         rc = -EPERM;
1288                         if (!capable(CAP_NET_ADMIN))
1289                                 break;
1290                         rc = x25_subscr_ioctl(cmd, argp);
1291                         break;
1292                 case SIOCX25GFACILITIES: {
1293                         struct x25_facilities fac = x25->facilities;
1294                         rc = copy_to_user(argp, &fac,
1295                                           sizeof(fac)) ? -EFAULT : 0;
1296                         break;
1297                 }
1298
1299                 case SIOCX25SFACILITIES: {
1300                         struct x25_facilities facilities;
1301                         rc = -EFAULT;
1302                         if (copy_from_user(&facilities, argp,
1303                                            sizeof(facilities)))
1304                                 break;
1305                         rc = -EINVAL;
1306                         if (sk->sk_state != TCP_LISTEN &&
1307                             sk->sk_state != TCP_CLOSE)
1308                                 break;
1309                         if (facilities.pacsize_in < X25_PS16 ||
1310                             facilities.pacsize_in > X25_PS4096)
1311                                 break;
1312                         if (facilities.pacsize_out < X25_PS16 ||
1313                             facilities.pacsize_out > X25_PS4096)
1314                                 break;
1315                         if (facilities.winsize_in < 1 ||
1316                             facilities.winsize_in > 127)
1317                                 break;
1318                         if (facilities.throughput < 0x03 ||
1319                             facilities.throughput > 0xDD)
1320                                 break;
1321                         if (facilities.reverse &&
1322                                 (facilities.reverse | 0x81)!= 0x81)
1323                                 break;
1324                         x25->facilities = facilities;
1325                         rc = 0;
1326                         break;
1327                 }
1328
1329                 case SIOCX25GDTEFACILITIES: {
1330                         rc = copy_to_user(argp, &x25->dte_facilities,
1331                                                 sizeof(x25->dte_facilities));
1332                         if (rc)
1333                                 rc = -EFAULT;
1334                         break;
1335                 }
1336
1337                 case SIOCX25SDTEFACILITIES: {
1338                         struct x25_dte_facilities dtefacs;
1339                         rc = -EFAULT;
1340                         if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1341                                 break;
1342                         rc = -EINVAL;
1343                         if (sk->sk_state != TCP_LISTEN &&
1344                                         sk->sk_state != TCP_CLOSE)
1345                                 break;
1346                         if (dtefacs.calling_len > X25_MAX_AE_LEN)
1347                                 break;
1348                         if (dtefacs.calling_ae == NULL)
1349                                 break;
1350                         if (dtefacs.called_len > X25_MAX_AE_LEN)
1351                                 break;
1352                         if (dtefacs.called_ae == NULL)
1353                                 break;
1354                         x25->dte_facilities = dtefacs;
1355                         rc = 0;
1356                         break;
1357                 }
1358
1359                 case SIOCX25GCALLUSERDATA: {
1360                         struct x25_calluserdata cud = x25->calluserdata;
1361                         rc = copy_to_user(argp, &cud,
1362                                           sizeof(cud)) ? -EFAULT : 0;
1363                         break;
1364                 }
1365
1366                 case SIOCX25SCALLUSERDATA: {
1367                         struct x25_calluserdata calluserdata;
1368
1369                         rc = -EFAULT;
1370                         if (copy_from_user(&calluserdata, argp,
1371                                            sizeof(calluserdata)))
1372                                 break;
1373                         rc = -EINVAL;
1374                         if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1375                                 break;
1376                         x25->calluserdata = calluserdata;
1377                         rc = 0;
1378                         break;
1379                 }
1380
1381                 case SIOCX25GCAUSEDIAG: {
1382                         struct x25_causediag causediag;
1383                         causediag = x25->causediag;
1384                         rc = copy_to_user(argp, &causediag,
1385                                           sizeof(causediag)) ? -EFAULT : 0;
1386                         break;
1387                 }
1388
1389                 case SIOCX25SCUDMATCHLEN: {
1390                         struct x25_subaddr sub_addr;
1391                         rc = -EINVAL;
1392                         if(sk->sk_state != TCP_CLOSE)
1393                                 break;
1394                         rc = -EFAULT;
1395                         if (copy_from_user(&sub_addr, argp,
1396                                         sizeof(sub_addr)))
1397                                 break;
1398                         rc = -EINVAL;
1399                         if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1400                                 break;
1401                         x25->cudmatchlength = sub_addr.cudmatchlength;
1402                         rc = 0;
1403                         break;
1404                 }
1405
1406                 case SIOCX25CALLACCPTAPPRV: {
1407                         rc = -EINVAL;
1408                         if (sk->sk_state != TCP_CLOSE)
1409                                 break;
1410                         x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1411                         rc = 0;
1412                         break;
1413                 }
1414
1415                 case SIOCX25SENDCALLACCPT:  {
1416                         rc = -EINVAL;
1417                         if (sk->sk_state != TCP_ESTABLISHED)
1418                                 break;
1419                         if (x25->accptapprv)    /* must call accptapprv above */
1420                                 break;
1421                         x25_write_internal(sk, X25_CALL_ACCEPTED);
1422                         x25->state = X25_STATE_3;
1423                         rc = 0;
1424                         break;
1425                 }
1426
1427                 default:
1428                         rc = -ENOIOCTLCMD;
1429                         break;
1430         }
1431
1432         return rc;
1433 }
1434
1435 static struct net_proto_family x25_family_ops = {
1436         .family =       AF_X25,
1437         .create =       x25_create,
1438         .owner  =       THIS_MODULE,
1439 };
1440
1441 #ifdef CONFIG_COMPAT
1442 static int compat_x25_subscr_ioctl(unsigned int cmd,
1443                 struct compat_x25_subscrip_struct __user *x25_subscr32)
1444 {
1445         struct compat_x25_subscrip_struct x25_subscr;
1446         struct x25_neigh *nb;
1447         struct net_device *dev;
1448         int rc = -EINVAL;
1449
1450         rc = -EFAULT;
1451         if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1452                 goto out;
1453
1454         rc = -EINVAL;
1455         dev = x25_dev_get(x25_subscr.device);
1456         if (dev == NULL)
1457                 goto out;
1458
1459         nb = x25_get_neigh(dev);
1460         if (nb == NULL)
1461                 goto out_dev_put;
1462
1463         dev_put(dev);
1464
1465         if (cmd == SIOCX25GSUBSCRIP) {
1466                 x25_subscr.extended = nb->extended;
1467                 x25_subscr.global_facil_mask = nb->global_facil_mask;
1468                 rc = copy_to_user(x25_subscr32, &x25_subscr,
1469                                 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1470         } else {
1471                 rc = -EINVAL;
1472                 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1473                         rc = 0;
1474                         nb->extended = x25_subscr.extended;
1475                         nb->global_facil_mask = x25_subscr.global_facil_mask;
1476                 }
1477         }
1478         x25_neigh_put(nb);
1479 out:
1480         return rc;
1481 out_dev_put:
1482         dev_put(dev);
1483         goto out;
1484 }
1485
1486 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1487                                 unsigned long arg)
1488 {
1489         void __user *argp = compat_ptr(arg);
1490         struct sock *sk = sock->sk;
1491
1492         int rc = -ENOIOCTLCMD;
1493
1494         switch(cmd) {
1495         case TIOCOUTQ:
1496         case TIOCINQ:
1497                 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1498                 break;
1499         case SIOCGSTAMP:
1500                 rc = -EINVAL;
1501                 if (sk)
1502                         rc = compat_sock_get_timestamp(sk,
1503                                         (struct timeval __user*)argp);
1504                 break;
1505         case SIOCGIFADDR:
1506         case SIOCSIFADDR:
1507         case SIOCGIFDSTADDR:
1508         case SIOCSIFDSTADDR:
1509         case SIOCGIFBRDADDR:
1510         case SIOCSIFBRDADDR:
1511         case SIOCGIFNETMASK:
1512         case SIOCSIFNETMASK:
1513         case SIOCGIFMETRIC:
1514         case SIOCSIFMETRIC:
1515                 rc = -EINVAL;
1516                 break;
1517         case SIOCADDRT:
1518         case SIOCDELRT:
1519                 rc = -EPERM;
1520                 if (!capable(CAP_NET_ADMIN))
1521                         break;
1522                 rc = x25_route_ioctl(cmd, argp);
1523                 break;
1524         case SIOCX25GSUBSCRIP:
1525                 rc = compat_x25_subscr_ioctl(cmd, argp);
1526                 break;
1527         case SIOCX25SSUBSCRIP:
1528                 rc = -EPERM;
1529                 if (!capable(CAP_NET_ADMIN))
1530                         break;
1531                 rc = compat_x25_subscr_ioctl(cmd, argp);
1532                 break;
1533         case SIOCX25GFACILITIES:
1534         case SIOCX25SFACILITIES:
1535         case SIOCX25GDTEFACILITIES:
1536         case SIOCX25SDTEFACILITIES:
1537         case SIOCX25GCALLUSERDATA:
1538         case SIOCX25SCALLUSERDATA:
1539         case SIOCX25GCAUSEDIAG:
1540         case SIOCX25SCUDMATCHLEN:
1541         case SIOCX25CALLACCPTAPPRV:
1542         case SIOCX25SENDCALLACCPT:
1543                 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1544                 break;
1545         default:
1546                 rc = -ENOIOCTLCMD;
1547                 break;
1548         }
1549         return rc;
1550 }
1551 #endif
1552
1553 static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1554         .family =       AF_X25,
1555         .owner =        THIS_MODULE,
1556         .release =      x25_release,
1557         .bind =         x25_bind,
1558         .connect =      x25_connect,
1559         .socketpair =   sock_no_socketpair,
1560         .accept =       x25_accept,
1561         .getname =      x25_getname,
1562         .poll =         datagram_poll,
1563         .ioctl =        x25_ioctl,
1564 #ifdef CONFIG_COMPAT
1565         .compat_ioctl = compat_x25_ioctl,
1566 #endif
1567         .listen =       x25_listen,
1568         .shutdown =     sock_no_shutdown,
1569         .setsockopt =   x25_setsockopt,
1570         .getsockopt =   x25_getsockopt,
1571         .sendmsg =      x25_sendmsg,
1572         .recvmsg =      x25_recvmsg,
1573         .mmap =         sock_no_mmap,
1574         .sendpage =     sock_no_sendpage,
1575 };
1576
1577 #include <linux/smp_lock.h>
1578 SOCKOPS_WRAP(x25_proto, AF_X25);
1579
1580 static struct packet_type x25_packet_type = {
1581         .type = __constant_htons(ETH_P_X25),
1582         .func = x25_lapb_receive_frame,
1583 };
1584
1585 static struct notifier_block x25_dev_notifier = {
1586         .notifier_call = x25_device_event,
1587 };
1588
1589 void x25_kill_by_neigh(struct x25_neigh *nb)
1590 {
1591         struct sock *s;
1592         struct hlist_node *node;
1593
1594         write_lock_bh(&x25_list_lock);
1595
1596         sk_for_each(s, node, &x25_list)
1597                 if (x25_sk(s)->neighbour == nb)
1598                         x25_disconnect(s, ENETUNREACH, 0, 0);
1599
1600         write_unlock_bh(&x25_list_lock);
1601 }
1602
1603 static int __init x25_init(void)
1604 {
1605         int rc = proto_register(&x25_proto, 0);
1606
1607         if (rc != 0)
1608                 goto out;
1609
1610         sock_register(&x25_family_ops);
1611
1612         dev_add_pack(&x25_packet_type);
1613
1614         register_netdevice_notifier(&x25_dev_notifier);
1615
1616         printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1617
1618 #ifdef CONFIG_SYSCTL
1619         x25_register_sysctl();
1620 #endif
1621         x25_proc_init();
1622 out:
1623         return rc;
1624 }
1625 module_init(x25_init);
1626
1627 static void __exit x25_exit(void)
1628 {
1629         x25_proc_exit();
1630         x25_link_free();
1631         x25_route_free();
1632
1633 #ifdef CONFIG_SYSCTL
1634         x25_unregister_sysctl();
1635 #endif
1636
1637         unregister_netdevice_notifier(&x25_dev_notifier);
1638
1639         dev_remove_pack(&x25_packet_type);
1640
1641         sock_unregister(AF_X25);
1642         proto_unregister(&x25_proto);
1643 }
1644 module_exit(x25_exit);
1645
1646 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1647 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1648 MODULE_LICENSE("GPL");
1649 MODULE_ALIAS_NETPROTO(PF_X25);