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