blob: 479927cb45cacd3c8f3f797a916bb51edf0cbbfc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09006 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090021 * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
22 * facilities negotiation and increased
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * the throughput upper limit.
24 * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090025 * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * 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
Shaun Pereiracb65d502005-06-22 22:15:01 -070032 * 2005-04-02 Shaun Pereira Selective sub address matching
33 * with call user data
Shaun Pereiraebc3f642005-06-22 22:16:17 -070034 * 2005-04-15 Shaun Pereira Fast select with no restriction on
35 * response
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080039#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#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>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070050#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#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>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080056#include <linux/compat.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080059#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
62int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
63int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
64int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
65int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080066int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68HLIST_HEAD(x25_list);
69DEFINE_RWLOCK(x25_list_lock);
70
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080071static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static struct x25_address null_x25_address = {" "};
74
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080075#ifdef CONFIG_COMPAT
76struct 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070083int 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
120int 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 */
161static 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 */
171static 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 */
188static 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 */
224static 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 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700238static struct sock *x25_find_listener(struct x25_address *addr,
239 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
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,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700250 x25_sk(s)->source_addr.x25_addr) ||
251 !strcmp(addr->x25_addr,
252 null_x25_address.x25_addr)) &&
253 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 /*
255 * Found a listening socket, now check the incoming
256 * call user data vs this sockets call user data
257 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700258 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900259 if((memcmp(x25_sk(s)->calluserdata.cuddata,
260 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700261 x25_sk(s)->cudmatchlength)) == 0) {
262 sock_hold(s);
263 goto found;
264 }
265 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268 if (next_best) {
269 s = next_best;
270 sock_hold(s);
271 goto found;
272 }
273 s = NULL;
274found:
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 */
282static 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;
293found:
294 return s;
295}
296
297struct 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 */
310static 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 */
332void x25_destroy_socket(struct sock *);
333
334/*
335 * handler for deferred kills.
336 */
337static 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 */
349void 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
395static 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;
415out:
416 return rc;
417}
418
419static 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;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 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;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 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;
444out:
445 return rc;
446}
447
448static 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
463static struct proto x25_proto = {
464 .name = "X25",
465 .owner = THIS_MODULE,
466 .obj_size = sizeof(struct x25_sock),
467};
468
469static 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);
484out:
485 return sk;
486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488static int x25_create(struct socket *sock, int protocol)
489{
490 struct sock *sk;
491 struct x25_sock *x25;
492 int rc = -ESOCKTNOSUPPORT;
493
494 if (sock->type != SOCK_SEQPACKET || protocol)
495 goto out;
496
497 rc = -ENOMEM;
498 if ((sk = x25_alloc_socket()) == NULL)
499 goto out;
500
501 x25 = x25_sk(sk);
502
503 sock_init_data(sock, sk);
504
505 x25_init_timers(sk);
506
507 sock->ops = &x25_proto_ops;
508 sk->sk_protocol = protocol;
509 sk->sk_backlog_rcv = x25_backlog_rcv;
510
511 x25->t21 = sysctl_x25_call_request_timeout;
512 x25->t22 = sysctl_x25_reset_request_timeout;
513 x25->t23 = sysctl_x25_clear_request_timeout;
514 x25->t2 = sysctl_x25_ack_holdback_timeout;
515 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700516 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700517 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
518 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
521 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
522 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
523 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
524 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
525 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900526 x25->dte_facilities.calling_len = 0;
527 x25->dte_facilities.called_len = 0;
528 memset(x25->dte_facilities.called_ae, '\0',
529 sizeof(x25->dte_facilities.called_ae));
530 memset(x25->dte_facilities.calling_ae, '\0',
531 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 rc = 0;
534out:
535 return rc;
536}
537
538static struct sock *x25_make_new(struct sock *osk)
539{
540 struct sock *sk = NULL;
541 struct x25_sock *x25, *ox25;
542
543 if (osk->sk_type != SOCK_SEQPACKET)
544 goto out;
545
546 if ((sk = x25_alloc_socket()) == NULL)
547 goto out;
548
549 x25 = x25_sk(sk);
550
551 sk->sk_type = osk->sk_type;
552 sk->sk_socket = osk->sk_socket;
553 sk->sk_priority = osk->sk_priority;
554 sk->sk_protocol = osk->sk_protocol;
555 sk->sk_rcvbuf = osk->sk_rcvbuf;
556 sk->sk_sndbuf = osk->sk_sndbuf;
557 sk->sk_state = TCP_ESTABLISHED;
558 sk->sk_sleep = osk->sk_sleep;
559 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800560 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 ox25 = x25_sk(osk);
563 x25->t21 = ox25->t21;
564 x25->t22 = ox25->t22;
565 x25->t23 = ox25->t23;
566 x25->t2 = ox25->t2;
567 x25->facilities = ox25->facilities;
568 x25->qbitincl = ox25->qbitincl;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800569 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700570 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700571 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 x25_init_timers(sk);
574out:
575 return sk;
576}
577
578static int x25_release(struct socket *sock)
579{
580 struct sock *sk = sock->sk;
581 struct x25_sock *x25;
582
583 if (!sk)
584 goto out;
585
586 x25 = x25_sk(sk);
587
588 switch (x25->state) {
589
590 case X25_STATE_0:
591 case X25_STATE_2:
592 x25_disconnect(sk, 0, 0, 0);
593 x25_destroy_socket(sk);
594 goto out;
595
596 case X25_STATE_1:
597 case X25_STATE_3:
598 case X25_STATE_4:
599 x25_clear_queues(sk);
600 x25_write_internal(sk, X25_CLEAR_REQUEST);
601 x25_start_t23timer(sk);
602 x25->state = X25_STATE_2;
603 sk->sk_state = TCP_CLOSE;
604 sk->sk_shutdown |= SEND_SHUTDOWN;
605 sk->sk_state_change(sk);
606 sock_set_flag(sk, SOCK_DEAD);
607 sock_set_flag(sk, SOCK_DESTROY);
608 break;
609 }
610
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900611 sock->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 sk->sk_socket = NULL; /* Not used, but we should do this */
613out:
614 return 0;
615}
616
617static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
618{
619 struct sock *sk = sock->sk;
620 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
621
622 if (!sock_flag(sk, SOCK_ZAPPED) ||
623 addr_len != sizeof(struct sockaddr_x25) ||
624 addr->sx25_family != AF_X25)
625 return -EINVAL;
626
627 x25_sk(sk)->source_addr = addr->sx25_addr;
628 x25_insert_socket(sk);
629 sock_reset_flag(sk, SOCK_ZAPPED);
630 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
631
632 return 0;
633}
634
635static int x25_wait_for_connection_establishment(struct sock *sk)
636{
637 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900638 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 add_wait_queue_exclusive(sk->sk_sleep, &wait);
641 for (;;) {
642 __set_current_state(TASK_INTERRUPTIBLE);
643 rc = -ERESTARTSYS;
644 if (signal_pending(current))
645 break;
646 rc = sock_error(sk);
647 if (rc) {
648 sk->sk_socket->state = SS_UNCONNECTED;
649 break;
650 }
651 rc = 0;
652 if (sk->sk_state != TCP_ESTABLISHED) {
653 release_sock(sk);
654 schedule();
655 lock_sock(sk);
656 } else
657 break;
658 }
659 __set_current_state(TASK_RUNNING);
660 remove_wait_queue(sk->sk_sleep, &wait);
661 return rc;
662}
663
664static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
665 int addr_len, int flags)
666{
667 struct sock *sk = sock->sk;
668 struct x25_sock *x25 = x25_sk(sk);
669 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
670 struct x25_route *rt;
671 int rc = 0;
672
673 lock_sock(sk);
674 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
675 sock->state = SS_CONNECTED;
676 goto out; /* Connect completed during a ERESTARTSYS event */
677 }
678
679 rc = -ECONNREFUSED;
680 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
681 sock->state = SS_UNCONNECTED;
682 goto out;
683 }
684
685 rc = -EISCONN; /* No reconnect on a seqpacket socket */
686 if (sk->sk_state == TCP_ESTABLISHED)
687 goto out;
688
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900689 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 sock->state = SS_UNCONNECTED;
691
692 rc = -EINVAL;
693 if (addr_len != sizeof(struct sockaddr_x25) ||
694 addr->sx25_family != AF_X25)
695 goto out;
696
697 rc = -ENETUNREACH;
698 rt = x25_get_route(&addr->sx25_addr);
699 if (!rt)
700 goto out;
701
702 x25->neighbour = x25_get_neigh(rt->dev);
703 if (!x25->neighbour)
704 goto out_put_route;
705
706 x25_limit_facilities(&x25->facilities, x25->neighbour);
707
708 x25->lci = x25_new_lci(x25->neighbour);
709 if (!x25->lci)
710 goto out_put_neigh;
711
712 rc = -EINVAL;
713 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
714 goto out_put_neigh;
715
716 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
717 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
718
719 x25->dest_addr = addr->sx25_addr;
720
721 /* Move to connecting socket, start sending Connect Requests */
722 sock->state = SS_CONNECTING;
723 sk->sk_state = TCP_SYN_SENT;
724
725 x25->state = X25_STATE_1;
726
727 x25_write_internal(sk, X25_CALL_REQUEST);
728
729 x25_start_heartbeat(sk);
730 x25_start_t21timer(sk);
731
732 /* Now the loop */
733 rc = -EINPROGRESS;
734 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
735 goto out_put_neigh;
736
737 rc = x25_wait_for_connection_establishment(sk);
738 if (rc)
739 goto out_put_neigh;
740
741 sock->state = SS_CONNECTED;
742 rc = 0;
743out_put_neigh:
744 if (rc)
745 x25_neigh_put(x25->neighbour);
746out_put_route:
747 x25_route_put(rt);
748out:
749 release_sock(sk);
750 return rc;
751}
752
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800753static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
755 DECLARE_WAITQUEUE(wait, current);
756 int rc = 0;
757
758 add_wait_queue_exclusive(sk->sk_sleep, &wait);
759 for (;;) {
760 __set_current_state(TASK_INTERRUPTIBLE);
761 if (sk->sk_shutdown & RCV_SHUTDOWN)
762 break;
763 rc = -ERESTARTSYS;
764 if (signal_pending(current))
765 break;
766 rc = -EAGAIN;
767 if (!timeout)
768 break;
769 rc = 0;
770 if (skb_queue_empty(&sk->sk_receive_queue)) {
771 release_sock(sk);
772 timeout = schedule_timeout(timeout);
773 lock_sock(sk);
774 } else
775 break;
776 }
777 __set_current_state(TASK_RUNNING);
778 remove_wait_queue(sk->sk_sleep, &wait);
779 return rc;
780}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
783{
784 struct sock *sk = sock->sk;
785 struct sock *newsk;
786 struct sk_buff *skb;
787 int rc = -EINVAL;
788
789 if (!sk || sk->sk_state != TCP_LISTEN)
790 goto out;
791
792 rc = -EOPNOTSUPP;
793 if (sk->sk_type != SOCK_SEQPACKET)
794 goto out;
795
796 lock_sock(sk);
797 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
798 if (rc)
799 goto out2;
800 skb = skb_dequeue(&sk->sk_receive_queue);
801 rc = -EINVAL;
802 if (!skb->sk)
803 goto out2;
804 newsk = skb->sk;
805 newsk->sk_socket = newsock;
806 newsk->sk_sleep = &newsock->wait;
807
808 /* Now attach up the new socket */
809 skb->sk = NULL;
810 kfree_skb(skb);
811 sk->sk_ack_backlog--;
812 newsock->sk = newsk;
813 newsock->state = SS_CONNECTED;
814 rc = 0;
815out2:
816 release_sock(sk);
817out:
818 return rc;
819}
820
821static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
822 int *uaddr_len, int peer)
823{
824 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
825 struct sock *sk = sock->sk;
826 struct x25_sock *x25 = x25_sk(sk);
827
828 if (peer) {
829 if (sk->sk_state != TCP_ESTABLISHED)
830 return -ENOTCONN;
831 sx25->sx25_addr = x25->dest_addr;
832 } else
833 sx25->sx25_addr = x25->source_addr;
834
835 sx25->sx25_family = AF_X25;
836 *uaddr_len = sizeof(*sx25);
837
838 return 0;
839}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
842 unsigned int lci)
843{
844 struct sock *sk;
845 struct sock *make;
846 struct x25_sock *makex25;
847 struct x25_address source_addr, dest_addr;
848 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800849 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800850 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Remove the LCI and frame type.
854 */
855 skb_pull(skb, X25_STD_MIN_LEN);
856
857 /*
858 * Extract the X.25 addresses and convert them to ASCII strings,
859 * and remove them.
860 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800861 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
862 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /*
865 * Get the length of the facilities, skip past them for the moment
866 * get the call user data because this is needed to determine
867 * the correct listener
868 */
869 len = skb->data[0] + 1;
870 skb_pull(skb,len);
871
872 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * Find a listener for the particular address/cud pair.
874 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700875 sk = x25_find_listener(&source_addr,skb);
876 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800878 if (sk != NULL && sk_acceptq_is_full(sk)) {
879 goto out_sock_put;
880 }
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800883 * We dont have any listeners for this incoming call.
884 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800886 if (sk == NULL) {
887 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800888 if (sysctl_x25_forward &&
889 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800890 {
891 /* Call was forwarded, dont process it any more */
892 kfree_skb(skb);
893 rc = 1;
894 goto out;
895 } else {
896 /* No listeners, can't forward, clear the call */
897 goto out_clear_request;
898 }
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /*
902 * Try to reach a compromise on the requested facilities.
903 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800904 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
905 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto out_sock_put;
907
908 /*
909 * current neighbour/link might impose additional limits
910 * on certain facilties
911 */
912
913 x25_limit_facilities(&facilities, nb);
914
915 /*
916 * Try to create a new socket.
917 */
918 make = x25_make_new(sk);
919 if (!make)
920 goto out_sock_put;
921
922 /*
923 * Remove the facilities
924 */
925 skb_pull(skb, len);
926
927 skb->sk = make;
928 make->sk_state = TCP_ESTABLISHED;
929
930 makex25 = x25_sk(make);
931 makex25->lci = lci;
932 makex25->dest_addr = dest_addr;
933 makex25->source_addr = source_addr;
934 makex25->neighbour = nb;
935 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800936 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700938 /* ensure no reverse facil on accept */
939 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800940 /* ensure no calling address extension on accept */
941 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700942 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700944 /* Normally all calls are accepted immediatly */
945 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
946 x25_write_internal(make, X25_CALL_ACCEPTED);
947 makex25->state = X25_STATE_3;
948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Shaun Pereiracb65d502005-06-22 22:15:01 -0700950 /*
951 * Incoming Call User Data.
952 */
953 if (skb->len >= 0) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300954 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
Shaun Pereiracb65d502005-06-22 22:15:01 -0700955 makex25->calluserdata.cudlength = skb->len;
956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 sk->sk_ack_backlog++;
959
960 x25_insert_socket(make);
961
962 skb_queue_head(&sk->sk_receive_queue, skb);
963
964 x25_start_heartbeat(make);
965
966 if (!sock_flag(sk, SOCK_DEAD))
967 sk->sk_data_ready(sk, skb->len);
968 rc = 1;
969 sock_put(sk);
970out:
971 return rc;
972out_sock_put:
973 sock_put(sk);
974out_clear_request:
975 rc = 0;
976 x25_transmit_clear_request(nb, lci, 0x01);
977 goto out;
978}
979
980static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
981 struct msghdr *msg, size_t len)
982{
983 struct sock *sk = sock->sk;
984 struct x25_sock *x25 = x25_sk(sk);
985 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
986 struct sockaddr_x25 sx25;
987 struct sk_buff *skb;
988 unsigned char *asmptr;
989 int noblock = msg->msg_flags & MSG_DONTWAIT;
990 size_t size;
991 int qbit = 0, rc = -EINVAL;
992
993 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
994 goto out;
995
996 /* we currently don't support segmented records at the user interface */
997 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
998 goto out;
999
1000 rc = -EADDRNOTAVAIL;
1001 if (sock_flag(sk, SOCK_ZAPPED))
1002 goto out;
1003
1004 rc = -EPIPE;
1005 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1006 send_sig(SIGPIPE, current, 0);
1007 goto out;
1008 }
1009
1010 rc = -ENETUNREACH;
1011 if (!x25->neighbour)
1012 goto out;
1013
1014 if (usx25) {
1015 rc = -EINVAL;
1016 if (msg->msg_namelen < sizeof(sx25))
1017 goto out;
1018 memcpy(&sx25, usx25, sizeof(sx25));
1019 rc = -EISCONN;
1020 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1021 goto out;
1022 rc = -EINVAL;
1023 if (sx25.sx25_family != AF_X25)
1024 goto out;
1025 } else {
1026 /*
1027 * FIXME 1003.1g - if the socket is like this because
1028 * it has become closed (not started closed) we ought
1029 * to SIGPIPE, EPIPE;
1030 */
1031 rc = -ENOTCONN;
1032 if (sk->sk_state != TCP_ESTABLISHED)
1033 goto out;
1034
1035 sx25.sx25_family = AF_X25;
1036 sx25.sx25_addr = x25->dest_addr;
1037 }
1038
1039 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1040
1041 /* Build a packet */
1042 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1043
1044 if ((msg->msg_flags & MSG_OOB) && len > 32)
1045 len = 32;
1046
1047 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1048
1049 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1050 if (!skb)
1051 goto out;
1052 X25_SKB_CB(skb)->flags = msg->msg_flags;
1053
1054 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1055
1056 /*
1057 * Put the data on the end
1058 */
1059 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1060
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001061 skb_reset_transport_header(skb);
1062 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001064 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (rc)
1066 goto out_kfree_skb;
1067
1068 /*
1069 * If the Q BIT Include socket option is in force, the first
1070 * byte of the user data is the logical value of the Q Bit.
1071 */
1072 if (x25->qbitincl) {
1073 qbit = skb->data[0];
1074 skb_pull(skb, 1);
1075 }
1076
1077 /*
1078 * Push down the X.25 header
1079 */
1080 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1081
1082 if (msg->msg_flags & MSG_OOB) {
1083 if (x25->neighbour->extended) {
1084 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1085 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1086 *asmptr++ = (x25->lci >> 0) & 0xFF;
1087 *asmptr++ = X25_INTERRUPT;
1088 } else {
1089 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1090 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1091 *asmptr++ = (x25->lci >> 0) & 0xFF;
1092 *asmptr++ = X25_INTERRUPT;
1093 }
1094 } else {
1095 if (x25->neighbour->extended) {
1096 /* Build an Extended X.25 header */
1097 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1098 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1099 *asmptr++ = (x25->lci >> 0) & 0xFF;
1100 *asmptr++ = X25_DATA;
1101 *asmptr++ = X25_DATA;
1102 } else {
1103 /* Build an Standard X.25 header */
1104 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1105 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1106 *asmptr++ = (x25->lci >> 0) & 0xFF;
1107 *asmptr++ = X25_DATA;
1108 }
1109
1110 if (qbit)
1111 skb->data[0] |= X25_Q_BIT;
1112 }
1113
1114 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1115 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1116
1117 rc = -ENOTCONN;
1118 if (sk->sk_state != TCP_ESTABLISHED)
1119 goto out_kfree_skb;
1120
1121 if (msg->msg_flags & MSG_OOB)
1122 skb_queue_tail(&x25->interrupt_out_queue, skb);
1123 else {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001124 len = x25_output(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (len < 0)
1126 kfree_skb(skb);
1127 else if (x25->qbitincl)
1128 len++;
1129 }
1130
1131 /*
1132 * lock_sock() is currently only used to serialize this x25_kick()
1133 * against input-driven x25_kick() calls. It currently only blocks
1134 * incoming packets for this socket and does not protect against
1135 * any other socket state changes and is not called from anywhere
1136 * else. As x25_kick() cannot block and as long as all socket
1137 * operations are BKL-wrapped, we don't need take to care about
1138 * purging the backlog queue in x25_release().
1139 *
1140 * Using lock_sock() to protect all socket operations entirely
1141 * (and making the whole x25 stack SMP aware) unfortunately would
1142 * require major changes to {send,recv}msg and skb allocation methods.
1143 * -> 2.5 ;)
1144 */
1145 lock_sock(sk);
1146 x25_kick(sk);
1147 release_sock(sk);
1148 rc = len;
1149out:
1150 return rc;
1151out_kfree_skb:
1152 kfree_skb(skb);
1153 goto out;
1154}
1155
1156
1157static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1158 struct msghdr *msg, size_t size,
1159 int flags)
1160{
1161 struct sock *sk = sock->sk;
1162 struct x25_sock *x25 = x25_sk(sk);
1163 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1164 size_t copied;
1165 int qbit;
1166 struct sk_buff *skb;
1167 unsigned char *asmptr;
1168 int rc = -ENOTCONN;
1169
1170 /*
1171 * This works for seqpacket too. The receiver has ordered the queue for
1172 * us! We do one quick check first though
1173 */
1174 if (sk->sk_state != TCP_ESTABLISHED)
1175 goto out;
1176
1177 if (flags & MSG_OOB) {
1178 rc = -EINVAL;
1179 if (sock_flag(sk, SOCK_URGINLINE) ||
1180 !skb_peek(&x25->interrupt_in_queue))
1181 goto out;
1182
1183 skb = skb_dequeue(&x25->interrupt_in_queue);
1184
1185 skb_pull(skb, X25_STD_MIN_LEN);
1186
1187 /*
1188 * No Q bit information on Interrupt data.
1189 */
1190 if (x25->qbitincl) {
1191 asmptr = skb_push(skb, 1);
1192 *asmptr = 0x00;
1193 }
1194
1195 msg->msg_flags |= MSG_OOB;
1196 } else {
1197 /* Now we can treat all alike */
1198 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1199 flags & MSG_DONTWAIT, &rc);
1200 if (!skb)
1201 goto out;
1202
1203 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1204
1205 skb_pull(skb, x25->neighbour->extended ?
1206 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1207
1208 if (x25->qbitincl) {
1209 asmptr = skb_push(skb, 1);
1210 *asmptr = qbit;
1211 }
1212 }
1213
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001214 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 copied = skb->len;
1216
1217 if (copied > size) {
1218 copied = size;
1219 msg->msg_flags |= MSG_TRUNC;
1220 }
1221
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001222 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 msg->msg_flags |= MSG_EOR;
1224
1225 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1226 if (rc)
1227 goto out_free_dgram;
1228
1229 if (sx25) {
1230 sx25->sx25_family = AF_X25;
1231 sx25->sx25_addr = x25->dest_addr;
1232 }
1233
1234 msg->msg_namelen = sizeof(struct sockaddr_x25);
1235
1236 lock_sock(sk);
1237 x25_check_rbuf(sk);
1238 release_sock(sk);
1239 rc = copied;
1240out_free_dgram:
1241 skb_free_datagram(sk, skb);
1242out:
1243 return rc;
1244}
1245
1246
1247static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1248{
1249 struct sock *sk = sock->sk;
1250 struct x25_sock *x25 = x25_sk(sk);
1251 void __user *argp = (void __user *)arg;
1252 int rc;
1253
1254 switch (cmd) {
1255 case TIOCOUTQ: {
1256 int amount = sk->sk_sndbuf -
1257 atomic_read(&sk->sk_wmem_alloc);
1258 if (amount < 0)
1259 amount = 0;
1260 rc = put_user(amount, (unsigned int __user *)argp);
1261 break;
1262 }
1263
1264 case TIOCINQ: {
1265 struct sk_buff *skb;
1266 int amount = 0;
1267 /*
1268 * These two are safe on a single CPU system as
1269 * only user tasks fiddle here
1270 */
1271 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1272 amount = skb->len;
1273 rc = put_user(amount, (unsigned int __user *)argp);
1274 break;
1275 }
1276
1277 case SIOCGSTAMP:
1278 rc = -EINVAL;
1279 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001280 rc = sock_get_timestamp(sk,
1281 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001283 case SIOCGSTAMPNS:
1284 rc = -EINVAL;
1285 if (sk)
1286 rc = sock_get_timestampns(sk,
1287 (struct timespec __user *)argp);
1288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 case SIOCGIFADDR:
1290 case SIOCSIFADDR:
1291 case SIOCGIFDSTADDR:
1292 case SIOCSIFDSTADDR:
1293 case SIOCGIFBRDADDR:
1294 case SIOCSIFBRDADDR:
1295 case SIOCGIFNETMASK:
1296 case SIOCSIFNETMASK:
1297 case SIOCGIFMETRIC:
1298 case SIOCSIFMETRIC:
1299 rc = -EINVAL;
1300 break;
1301 case SIOCADDRT:
1302 case SIOCDELRT:
1303 rc = -EPERM;
1304 if (!capable(CAP_NET_ADMIN))
1305 break;
1306 rc = x25_route_ioctl(cmd, argp);
1307 break;
1308 case SIOCX25GSUBSCRIP:
1309 rc = x25_subscr_ioctl(cmd, argp);
1310 break;
1311 case SIOCX25SSUBSCRIP:
1312 rc = -EPERM;
1313 if (!capable(CAP_NET_ADMIN))
1314 break;
1315 rc = x25_subscr_ioctl(cmd, argp);
1316 break;
1317 case SIOCX25GFACILITIES: {
1318 struct x25_facilities fac = x25->facilities;
1319 rc = copy_to_user(argp, &fac,
1320 sizeof(fac)) ? -EFAULT : 0;
1321 break;
1322 }
1323
1324 case SIOCX25SFACILITIES: {
1325 struct x25_facilities facilities;
1326 rc = -EFAULT;
1327 if (copy_from_user(&facilities, argp,
1328 sizeof(facilities)))
1329 break;
1330 rc = -EINVAL;
1331 if (sk->sk_state != TCP_LISTEN &&
1332 sk->sk_state != TCP_CLOSE)
1333 break;
1334 if (facilities.pacsize_in < X25_PS16 ||
1335 facilities.pacsize_in > X25_PS4096)
1336 break;
1337 if (facilities.pacsize_out < X25_PS16 ||
1338 facilities.pacsize_out > X25_PS4096)
1339 break;
1340 if (facilities.winsize_in < 1 ||
1341 facilities.winsize_in > 127)
1342 break;
1343 if (facilities.throughput < 0x03 ||
1344 facilities.throughput > 0xDD)
1345 break;
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001346 if (facilities.reverse &&
1347 (facilities.reverse | 0x81)!= 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 break;
1349 x25->facilities = facilities;
1350 rc = 0;
1351 break;
1352 }
1353
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001354 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001355 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001356 sizeof(x25->dte_facilities));
1357 if (rc)
1358 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001359 break;
1360 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001361
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001362 case SIOCX25SDTEFACILITIES: {
1363 struct x25_dte_facilities dtefacs;
1364 rc = -EFAULT;
1365 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001366 break;
1367 rc = -EINVAL;
1368 if (sk->sk_state != TCP_LISTEN &&
1369 sk->sk_state != TCP_CLOSE)
1370 break;
1371 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1372 break;
1373 if (dtefacs.calling_ae == NULL)
1374 break;
1375 if (dtefacs.called_len > X25_MAX_AE_LEN)
1376 break;
1377 if (dtefacs.called_ae == NULL)
1378 break;
1379 x25->dte_facilities = dtefacs;
1380 rc = 0;
1381 break;
1382 }
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 case SIOCX25GCALLUSERDATA: {
1385 struct x25_calluserdata cud = x25->calluserdata;
1386 rc = copy_to_user(argp, &cud,
1387 sizeof(cud)) ? -EFAULT : 0;
1388 break;
1389 }
1390
1391 case SIOCX25SCALLUSERDATA: {
1392 struct x25_calluserdata calluserdata;
1393
1394 rc = -EFAULT;
1395 if (copy_from_user(&calluserdata, argp,
1396 sizeof(calluserdata)))
1397 break;
1398 rc = -EINVAL;
1399 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1400 break;
1401 x25->calluserdata = calluserdata;
1402 rc = 0;
1403 break;
1404 }
1405
1406 case SIOCX25GCAUSEDIAG: {
1407 struct x25_causediag causediag;
1408 causediag = x25->causediag;
1409 rc = copy_to_user(argp, &causediag,
1410 sizeof(causediag)) ? -EFAULT : 0;
1411 break;
1412 }
1413
Shaun Pereiracb65d502005-06-22 22:15:01 -07001414 case SIOCX25SCUDMATCHLEN: {
1415 struct x25_subaddr sub_addr;
1416 rc = -EINVAL;
1417 if(sk->sk_state != TCP_CLOSE)
1418 break;
1419 rc = -EFAULT;
1420 if (copy_from_user(&sub_addr, argp,
1421 sizeof(sub_addr)))
1422 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001423 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001424 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1425 break;
1426 x25->cudmatchlength = sub_addr.cudmatchlength;
1427 rc = 0;
1428 break;
1429 }
1430
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001431 case SIOCX25CALLACCPTAPPRV: {
1432 rc = -EINVAL;
1433 if (sk->sk_state != TCP_CLOSE)
1434 break;
1435 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1436 rc = 0;
1437 break;
1438 }
1439
1440 case SIOCX25SENDCALLACCPT: {
1441 rc = -EINVAL;
1442 if (sk->sk_state != TCP_ESTABLISHED)
1443 break;
1444 if (x25->accptapprv) /* must call accptapprv above */
1445 break;
1446 x25_write_internal(sk, X25_CALL_ACCEPTED);
1447 x25->state = X25_STATE_3;
1448 rc = 0;
1449 break;
1450 }
1451
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001452 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001453 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 }
1456
1457 return rc;
1458}
1459
1460static struct net_proto_family x25_family_ops = {
1461 .family = AF_X25,
1462 .create = x25_create,
1463 .owner = THIS_MODULE,
1464};
1465
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001466#ifdef CONFIG_COMPAT
1467static int compat_x25_subscr_ioctl(unsigned int cmd,
1468 struct compat_x25_subscrip_struct __user *x25_subscr32)
1469{
1470 struct compat_x25_subscrip_struct x25_subscr;
1471 struct x25_neigh *nb;
1472 struct net_device *dev;
1473 int rc = -EINVAL;
1474
1475 rc = -EFAULT;
1476 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1477 goto out;
1478
1479 rc = -EINVAL;
1480 dev = x25_dev_get(x25_subscr.device);
1481 if (dev == NULL)
1482 goto out;
1483
1484 nb = x25_get_neigh(dev);
1485 if (nb == NULL)
1486 goto out_dev_put;
1487
1488 dev_put(dev);
1489
1490 if (cmd == SIOCX25GSUBSCRIP) {
1491 x25_subscr.extended = nb->extended;
1492 x25_subscr.global_facil_mask = nb->global_facil_mask;
1493 rc = copy_to_user(x25_subscr32, &x25_subscr,
1494 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1495 } else {
1496 rc = -EINVAL;
1497 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1498 rc = 0;
1499 nb->extended = x25_subscr.extended;
1500 nb->global_facil_mask = x25_subscr.global_facil_mask;
1501 }
1502 }
1503 x25_neigh_put(nb);
1504out:
1505 return rc;
1506out_dev_put:
1507 dev_put(dev);
1508 goto out;
1509}
1510
1511static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1512 unsigned long arg)
1513{
1514 void __user *argp = compat_ptr(arg);
1515 struct sock *sk = sock->sk;
1516
1517 int rc = -ENOIOCTLCMD;
1518
1519 switch(cmd) {
1520 case TIOCOUTQ:
1521 case TIOCINQ:
1522 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1523 break;
1524 case SIOCGSTAMP:
1525 rc = -EINVAL;
1526 if (sk)
1527 rc = compat_sock_get_timestamp(sk,
1528 (struct timeval __user*)argp);
1529 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001530 case SIOCGSTAMPNS:
1531 rc = -EINVAL;
1532 if (sk)
1533 rc = compat_sock_get_timestampns(sk,
1534 (struct timespec __user*)argp);
1535 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001536 case SIOCGIFADDR:
1537 case SIOCSIFADDR:
1538 case SIOCGIFDSTADDR:
1539 case SIOCSIFDSTADDR:
1540 case SIOCGIFBRDADDR:
1541 case SIOCSIFBRDADDR:
1542 case SIOCGIFNETMASK:
1543 case SIOCSIFNETMASK:
1544 case SIOCGIFMETRIC:
1545 case SIOCSIFMETRIC:
1546 rc = -EINVAL;
1547 break;
1548 case SIOCADDRT:
1549 case SIOCDELRT:
1550 rc = -EPERM;
1551 if (!capable(CAP_NET_ADMIN))
1552 break;
1553 rc = x25_route_ioctl(cmd, argp);
1554 break;
1555 case SIOCX25GSUBSCRIP:
1556 rc = compat_x25_subscr_ioctl(cmd, argp);
1557 break;
1558 case SIOCX25SSUBSCRIP:
1559 rc = -EPERM;
1560 if (!capable(CAP_NET_ADMIN))
1561 break;
1562 rc = compat_x25_subscr_ioctl(cmd, argp);
1563 break;
1564 case SIOCX25GFACILITIES:
1565 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001566 case SIOCX25GDTEFACILITIES:
1567 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001568 case SIOCX25GCALLUSERDATA:
1569 case SIOCX25SCALLUSERDATA:
1570 case SIOCX25GCAUSEDIAG:
1571 case SIOCX25SCUDMATCHLEN:
1572 case SIOCX25CALLACCPTAPPRV:
1573 case SIOCX25SENDCALLACCPT:
1574 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1575 break;
1576 default:
1577 rc = -ENOIOCTLCMD;
1578 break;
1579 }
1580 return rc;
1581}
1582#endif
1583
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001584static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 .family = AF_X25,
1586 .owner = THIS_MODULE,
1587 .release = x25_release,
1588 .bind = x25_bind,
1589 .connect = x25_connect,
1590 .socketpair = sock_no_socketpair,
1591 .accept = x25_accept,
1592 .getname = x25_getname,
1593 .poll = datagram_poll,
1594 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001595#ifdef CONFIG_COMPAT
1596 .compat_ioctl = compat_x25_ioctl,
1597#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 .listen = x25_listen,
1599 .shutdown = sock_no_shutdown,
1600 .setsockopt = x25_setsockopt,
1601 .getsockopt = x25_getsockopt,
1602 .sendmsg = x25_sendmsg,
1603 .recvmsg = x25_recvmsg,
1604 .mmap = sock_no_mmap,
1605 .sendpage = sock_no_sendpage,
1606};
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608SOCKOPS_WRAP(x25_proto, AF_X25);
1609
1610static struct packet_type x25_packet_type = {
1611 .type = __constant_htons(ETH_P_X25),
1612 .func = x25_lapb_receive_frame,
1613};
1614
1615static struct notifier_block x25_dev_notifier = {
1616 .notifier_call = x25_device_event,
1617};
1618
1619void x25_kill_by_neigh(struct x25_neigh *nb)
1620{
1621 struct sock *s;
1622 struct hlist_node *node;
1623
1624 write_lock_bh(&x25_list_lock);
1625
1626 sk_for_each(s, node, &x25_list)
1627 if (x25_sk(s)->neighbour == nb)
1628 x25_disconnect(s, ENETUNREACH, 0, 0);
1629
1630 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001631
1632 /* Remove any related forwards */
1633 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636static int __init x25_init(void)
1637{
1638 int rc = proto_register(&x25_proto, 0);
1639
1640 if (rc != 0)
1641 goto out;
1642
1643 sock_register(&x25_family_ops);
1644
1645 dev_add_pack(&x25_packet_type);
1646
1647 register_netdevice_notifier(&x25_dev_notifier);
1648
1649 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1650
1651#ifdef CONFIG_SYSCTL
1652 x25_register_sysctl();
1653#endif
1654 x25_proc_init();
1655out:
1656 return rc;
1657}
1658module_init(x25_init);
1659
1660static void __exit x25_exit(void)
1661{
1662 x25_proc_exit();
1663 x25_link_free();
1664 x25_route_free();
1665
1666#ifdef CONFIG_SYSCTL
1667 x25_unregister_sysctl();
1668#endif
1669
1670 unregister_netdevice_notifier(&x25_dev_notifier);
1671
1672 dev_remove_pack(&x25_packet_type);
1673
1674 sock_unregister(AF_X25);
1675 proto_unregister(&x25_proto);
1676}
1677module_exit(x25_exit);
1678
1679MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1680MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1681MODULE_LICENSE("GPL");
1682MODULE_ALIAS_NETPROTO(PF_X25);