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