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