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