blob: bcb091f713ec0899a07bde24c722d32d22c3baa2 [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{
Eric Dumazet6bf15742008-01-13 22:27:52 -080086 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -080088 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
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
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900194 if (dev_net(dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200195 return NOTIFY_DONE;
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 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 */
227static 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 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700241static struct sock *x25_find_listener(struct x25_address *addr,
242 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
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,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700253 x25_sk(s)->source_addr.x25_addr) ||
254 !strcmp(addr->x25_addr,
255 null_x25_address.x25_addr)) &&
256 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 /*
258 * Found a listening socket, now check the incoming
259 * call user data vs this sockets call user data
260 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700261 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900262 if((memcmp(x25_sk(s)->calluserdata.cuddata,
263 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700264 x25_sk(s)->cudmatchlength)) == 0) {
265 sock_hold(s);
266 goto found;
267 }
268 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
271 if (next_best) {
272 s = next_best;
273 sock_hold(s);
274 goto found;
275 }
276 s = NULL;
277found:
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 */
285static 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;
296found:
297 return s;
298}
299
300struct 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 */
313static 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 */
335void x25_destroy_socket(struct sock *);
336
337/*
338 * handler for deferred kills.
339 */
340static 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 */
352void 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
398static 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;
418out:
419 return rc;
420}
421
422static 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;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 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;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 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;
447out:
448 return rc;
449}
450
451static 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
466static struct proto x25_proto = {
467 .name = "X25",
468 .owner = THIS_MODULE,
469 .obj_size = sizeof(struct x25_sock),
470};
471
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700472static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700475 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
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);
487out:
488 return sk;
489}
490
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700491static int x25_create(struct net *net, struct socket *sock, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct sock *sk;
494 struct x25_sock *x25;
495 int rc = -ESOCKTNOSUPPORT;
496
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700497 if (net != &init_net)
498 return -EAFNOSUPPORT;
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (sock->type != SOCK_SEQPACKET || protocol)
501 goto out;
502
503 rc = -ENOMEM;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700504 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 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;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700522 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700523 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
524 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
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;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900532 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));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 rc = 0;
540out:
541 return rc;
542}
543
544static 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
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900552 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto out;
554
555 x25 = x25_sk(sk);
556
557 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800564 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
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;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800573 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700574 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700575 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 x25_init_timers(sk);
578out:
579 return sk;
580}
581
582static 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
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900615 sock->sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 sk->sk_socket = NULL; /* Not used, but we should do this */
617out:
618 return 0;
619}
620
621static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
622{
623 struct sock *sk = sock->sk;
624 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
625
626 if (!sock_flag(sk, SOCK_ZAPPED) ||
627 addr_len != sizeof(struct sockaddr_x25) ||
628 addr->sx25_family != AF_X25)
629 return -EINVAL;
630
631 x25_sk(sk)->source_addr = addr->sx25_addr;
632 x25_insert_socket(sk);
633 sock_reset_flag(sk, SOCK_ZAPPED);
634 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
635
636 return 0;
637}
638
639static int x25_wait_for_connection_establishment(struct sock *sk)
640{
641 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900642 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 add_wait_queue_exclusive(sk->sk_sleep, &wait);
645 for (;;) {
646 __set_current_state(TASK_INTERRUPTIBLE);
647 rc = -ERESTARTSYS;
648 if (signal_pending(current))
649 break;
650 rc = sock_error(sk);
651 if (rc) {
652 sk->sk_socket->state = SS_UNCONNECTED;
653 break;
654 }
655 rc = 0;
656 if (sk->sk_state != TCP_ESTABLISHED) {
657 release_sock(sk);
658 schedule();
659 lock_sock(sk);
660 } else
661 break;
662 }
663 __set_current_state(TASK_RUNNING);
664 remove_wait_queue(sk->sk_sleep, &wait);
665 return rc;
666}
667
668static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
669 int addr_len, int flags)
670{
671 struct sock *sk = sock->sk;
672 struct x25_sock *x25 = x25_sk(sk);
673 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
674 struct x25_route *rt;
675 int rc = 0;
676
677 lock_sock(sk);
678 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
679 sock->state = SS_CONNECTED;
680 goto out; /* Connect completed during a ERESTARTSYS event */
681 }
682
683 rc = -ECONNREFUSED;
684 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
685 sock->state = SS_UNCONNECTED;
686 goto out;
687 }
688
689 rc = -EISCONN; /* No reconnect on a seqpacket socket */
690 if (sk->sk_state == TCP_ESTABLISHED)
691 goto out;
692
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900693 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 sock->state = SS_UNCONNECTED;
695
696 rc = -EINVAL;
697 if (addr_len != sizeof(struct sockaddr_x25) ||
698 addr->sx25_family != AF_X25)
699 goto out;
700
701 rc = -ENETUNREACH;
702 rt = x25_get_route(&addr->sx25_addr);
703 if (!rt)
704 goto out;
705
706 x25->neighbour = x25_get_neigh(rt->dev);
707 if (!x25->neighbour)
708 goto out_put_route;
709
710 x25_limit_facilities(&x25->facilities, x25->neighbour);
711
712 x25->lci = x25_new_lci(x25->neighbour);
713 if (!x25->lci)
714 goto out_put_neigh;
715
716 rc = -EINVAL;
717 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
718 goto out_put_neigh;
719
720 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
721 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
722
723 x25->dest_addr = addr->sx25_addr;
724
725 /* Move to connecting socket, start sending Connect Requests */
726 sock->state = SS_CONNECTING;
727 sk->sk_state = TCP_SYN_SENT;
728
729 x25->state = X25_STATE_1;
730
731 x25_write_internal(sk, X25_CALL_REQUEST);
732
733 x25_start_heartbeat(sk);
734 x25_start_t21timer(sk);
735
736 /* Now the loop */
737 rc = -EINPROGRESS;
738 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
739 goto out_put_neigh;
740
741 rc = x25_wait_for_connection_establishment(sk);
742 if (rc)
743 goto out_put_neigh;
744
745 sock->state = SS_CONNECTED;
746 rc = 0;
747out_put_neigh:
748 if (rc)
749 x25_neigh_put(x25->neighbour);
750out_put_route:
751 x25_route_put(rt);
752out:
753 release_sock(sk);
754 return rc;
755}
756
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800757static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
759 DECLARE_WAITQUEUE(wait, current);
760 int rc = 0;
761
762 add_wait_queue_exclusive(sk->sk_sleep, &wait);
763 for (;;) {
764 __set_current_state(TASK_INTERRUPTIBLE);
765 if (sk->sk_shutdown & RCV_SHUTDOWN)
766 break;
767 rc = -ERESTARTSYS;
768 if (signal_pending(current))
769 break;
770 rc = -EAGAIN;
771 if (!timeout)
772 break;
773 rc = 0;
774 if (skb_queue_empty(&sk->sk_receive_queue)) {
775 release_sock(sk);
776 timeout = schedule_timeout(timeout);
777 lock_sock(sk);
778 } else
779 break;
780 }
781 __set_current_state(TASK_RUNNING);
782 remove_wait_queue(sk->sk_sleep, &wait);
783 return rc;
784}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
787{
788 struct sock *sk = sock->sk;
789 struct sock *newsk;
790 struct sk_buff *skb;
791 int rc = -EINVAL;
792
793 if (!sk || sk->sk_state != TCP_LISTEN)
794 goto out;
795
796 rc = -EOPNOTSUPP;
797 if (sk->sk_type != SOCK_SEQPACKET)
798 goto out;
799
800 lock_sock(sk);
801 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
802 if (rc)
803 goto out2;
804 skb = skb_dequeue(&sk->sk_receive_queue);
805 rc = -EINVAL;
806 if (!skb->sk)
807 goto out2;
808 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700809 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 /* Now attach up the new socket */
812 skb->sk = NULL;
813 kfree_skb(skb);
814 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 newsock->state = SS_CONNECTED;
816 rc = 0;
817out2:
818 release_sock(sk);
819out:
820 return rc;
821}
822
823static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
824 int *uaddr_len, int peer)
825{
826 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
827 struct sock *sk = sock->sk;
828 struct x25_sock *x25 = x25_sk(sk);
829
830 if (peer) {
831 if (sk->sk_state != TCP_ESTABLISHED)
832 return -ENOTCONN;
833 sx25->sx25_addr = x25->dest_addr;
834 } else
835 sx25->sx25_addr = x25->source_addr;
836
837 sx25->sx25_family = AF_X25;
838 *uaddr_len = sizeof(*sx25);
839
840 return 0;
841}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900842
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
844 unsigned int lci)
845{
846 struct sock *sk;
847 struct sock *make;
848 struct x25_sock *makex25;
849 struct x25_address source_addr, dest_addr;
850 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800851 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800852 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /*
855 * Remove the LCI and frame type.
856 */
857 skb_pull(skb, X25_STD_MIN_LEN);
858
859 /*
860 * Extract the X.25 addresses and convert them to ASCII strings,
861 * and remove them.
862 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800863 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
864 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866 /*
867 * Get the length of the facilities, skip past them for the moment
868 * get the call user data because this is needed to determine
869 * the correct listener
870 */
871 len = skb->data[0] + 1;
872 skb_pull(skb,len);
873
874 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 * Find a listener for the particular address/cud pair.
876 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700877 sk = x25_find_listener(&source_addr,skb);
878 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800880 if (sk != NULL && sk_acceptq_is_full(sk)) {
881 goto out_sock_put;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800885 * We dont have any listeners for this incoming call.
886 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800888 if (sk == NULL) {
889 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800890 if (sysctl_x25_forward &&
891 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800892 {
893 /* Call was forwarded, dont process it any more */
894 kfree_skb(skb);
895 rc = 1;
896 goto out;
897 } else {
898 /* No listeners, can't forward, clear the call */
899 goto out_clear_request;
900 }
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /*
904 * Try to reach a compromise on the requested facilities.
905 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800906 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
907 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto out_sock_put;
909
910 /*
911 * current neighbour/link might impose additional limits
912 * on certain facilties
913 */
914
915 x25_limit_facilities(&facilities, nb);
916
917 /*
918 * Try to create a new socket.
919 */
920 make = x25_make_new(sk);
921 if (!make)
922 goto out_sock_put;
923
924 /*
925 * Remove the facilities
926 */
927 skb_pull(skb, len);
928
929 skb->sk = make;
930 make->sk_state = TCP_ESTABLISHED;
931
932 makex25 = x25_sk(make);
933 makex25->lci = lci;
934 makex25->dest_addr = dest_addr;
935 makex25->source_addr = source_addr;
936 makex25->neighbour = nb;
937 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800938 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700940 /* ensure no reverse facil on accept */
941 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800942 /* ensure no calling address extension on accept */
943 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700944 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700946 /* Normally all calls are accepted immediatly */
947 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
948 x25_write_internal(make, X25_CALL_ACCEPTED);
949 makex25->state = X25_STATE_3;
950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Shaun Pereiracb65d502005-06-22 22:15:01 -0700952 /*
953 * Incoming Call User Data.
954 */
955 if (skb->len >= 0) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300956 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
Shaun Pereiracb65d502005-06-22 22:15:01 -0700957 makex25->calluserdata.cudlength = skb->len;
958 }
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sk->sk_ack_backlog++;
961
962 x25_insert_socket(make);
963
964 skb_queue_head(&sk->sk_receive_queue, skb);
965
966 x25_start_heartbeat(make);
967
968 if (!sock_flag(sk, SOCK_DEAD))
969 sk->sk_data_ready(sk, skb->len);
970 rc = 1;
971 sock_put(sk);
972out:
973 return rc;
974out_sock_put:
975 sock_put(sk);
976out_clear_request:
977 rc = 0;
978 x25_transmit_clear_request(nb, lci, 0x01);
979 goto out;
980}
981
982static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
983 struct msghdr *msg, size_t len)
984{
985 struct sock *sk = sock->sk;
986 struct x25_sock *x25 = x25_sk(sk);
987 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
988 struct sockaddr_x25 sx25;
989 struct sk_buff *skb;
990 unsigned char *asmptr;
991 int noblock = msg->msg_flags & MSG_DONTWAIT;
992 size_t size;
993 int qbit = 0, rc = -EINVAL;
994
995 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
996 goto out;
997
998 /* we currently don't support segmented records at the user interface */
999 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1000 goto out;
1001
1002 rc = -EADDRNOTAVAIL;
1003 if (sock_flag(sk, SOCK_ZAPPED))
1004 goto out;
1005
1006 rc = -EPIPE;
1007 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1008 send_sig(SIGPIPE, current, 0);
1009 goto out;
1010 }
1011
1012 rc = -ENETUNREACH;
1013 if (!x25->neighbour)
1014 goto out;
1015
1016 if (usx25) {
1017 rc = -EINVAL;
1018 if (msg->msg_namelen < sizeof(sx25))
1019 goto out;
1020 memcpy(&sx25, usx25, sizeof(sx25));
1021 rc = -EISCONN;
1022 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1023 goto out;
1024 rc = -EINVAL;
1025 if (sx25.sx25_family != AF_X25)
1026 goto out;
1027 } else {
1028 /*
1029 * FIXME 1003.1g - if the socket is like this because
1030 * it has become closed (not started closed) we ought
1031 * to SIGPIPE, EPIPE;
1032 */
1033 rc = -ENOTCONN;
1034 if (sk->sk_state != TCP_ESTABLISHED)
1035 goto out;
1036
1037 sx25.sx25_family = AF_X25;
1038 sx25.sx25_addr = x25->dest_addr;
1039 }
1040
1041 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1042
1043 /* Build a packet */
1044 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1045
1046 if ((msg->msg_flags & MSG_OOB) && len > 32)
1047 len = 32;
1048
1049 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1050
1051 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1052 if (!skb)
1053 goto out;
1054 X25_SKB_CB(skb)->flags = msg->msg_flags;
1055
1056 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1057
1058 /*
1059 * Put the data on the end
1060 */
1061 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1062
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001063 skb_reset_transport_header(skb);
1064 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001066 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 if (rc)
1068 goto out_kfree_skb;
1069
1070 /*
1071 * If the Q BIT Include socket option is in force, the first
1072 * byte of the user data is the logical value of the Q Bit.
1073 */
1074 if (x25->qbitincl) {
1075 qbit = skb->data[0];
1076 skb_pull(skb, 1);
1077 }
1078
1079 /*
1080 * Push down the X.25 header
1081 */
1082 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1083
1084 if (msg->msg_flags & MSG_OOB) {
1085 if (x25->neighbour->extended) {
1086 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1087 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1088 *asmptr++ = (x25->lci >> 0) & 0xFF;
1089 *asmptr++ = X25_INTERRUPT;
1090 } else {
1091 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1092 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1093 *asmptr++ = (x25->lci >> 0) & 0xFF;
1094 *asmptr++ = X25_INTERRUPT;
1095 }
1096 } else {
1097 if (x25->neighbour->extended) {
1098 /* Build an Extended X.25 header */
1099 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1100 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1101 *asmptr++ = (x25->lci >> 0) & 0xFF;
1102 *asmptr++ = X25_DATA;
1103 *asmptr++ = X25_DATA;
1104 } else {
1105 /* Build an Standard X.25 header */
1106 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1107 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1108 *asmptr++ = (x25->lci >> 0) & 0xFF;
1109 *asmptr++ = X25_DATA;
1110 }
1111
1112 if (qbit)
1113 skb->data[0] |= X25_Q_BIT;
1114 }
1115
1116 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1117 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1118
1119 rc = -ENOTCONN;
1120 if (sk->sk_state != TCP_ESTABLISHED)
1121 goto out_kfree_skb;
1122
1123 if (msg->msg_flags & MSG_OOB)
1124 skb_queue_tail(&x25->interrupt_out_queue, skb);
1125 else {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001126 len = x25_output(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (len < 0)
1128 kfree_skb(skb);
1129 else if (x25->qbitincl)
1130 len++;
1131 }
1132
1133 /*
1134 * lock_sock() is currently only used to serialize this x25_kick()
1135 * against input-driven x25_kick() calls. It currently only blocks
1136 * incoming packets for this socket and does not protect against
1137 * any other socket state changes and is not called from anywhere
1138 * else. As x25_kick() cannot block and as long as all socket
1139 * operations are BKL-wrapped, we don't need take to care about
1140 * purging the backlog queue in x25_release().
1141 *
1142 * Using lock_sock() to protect all socket operations entirely
1143 * (and making the whole x25 stack SMP aware) unfortunately would
1144 * require major changes to {send,recv}msg and skb allocation methods.
1145 * -> 2.5 ;)
1146 */
1147 lock_sock(sk);
1148 x25_kick(sk);
1149 release_sock(sk);
1150 rc = len;
1151out:
1152 return rc;
1153out_kfree_skb:
1154 kfree_skb(skb);
1155 goto out;
1156}
1157
1158
1159static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1160 struct msghdr *msg, size_t size,
1161 int flags)
1162{
1163 struct sock *sk = sock->sk;
1164 struct x25_sock *x25 = x25_sk(sk);
1165 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1166 size_t copied;
1167 int qbit;
1168 struct sk_buff *skb;
1169 unsigned char *asmptr;
1170 int rc = -ENOTCONN;
1171
1172 /*
1173 * This works for seqpacket too. The receiver has ordered the queue for
1174 * us! We do one quick check first though
1175 */
1176 if (sk->sk_state != TCP_ESTABLISHED)
1177 goto out;
1178
1179 if (flags & MSG_OOB) {
1180 rc = -EINVAL;
1181 if (sock_flag(sk, SOCK_URGINLINE) ||
1182 !skb_peek(&x25->interrupt_in_queue))
1183 goto out;
1184
1185 skb = skb_dequeue(&x25->interrupt_in_queue);
1186
1187 skb_pull(skb, X25_STD_MIN_LEN);
1188
1189 /*
1190 * No Q bit information on Interrupt data.
1191 */
1192 if (x25->qbitincl) {
1193 asmptr = skb_push(skb, 1);
1194 *asmptr = 0x00;
1195 }
1196
1197 msg->msg_flags |= MSG_OOB;
1198 } else {
1199 /* Now we can treat all alike */
1200 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1201 flags & MSG_DONTWAIT, &rc);
1202 if (!skb)
1203 goto out;
1204
1205 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1206
1207 skb_pull(skb, x25->neighbour->extended ?
1208 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1209
1210 if (x25->qbitincl) {
1211 asmptr = skb_push(skb, 1);
1212 *asmptr = qbit;
1213 }
1214 }
1215
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001216 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 copied = skb->len;
1218
1219 if (copied > size) {
1220 copied = size;
1221 msg->msg_flags |= MSG_TRUNC;
1222 }
1223
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001224 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 msg->msg_flags |= MSG_EOR;
1226
1227 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1228 if (rc)
1229 goto out_free_dgram;
1230
1231 if (sx25) {
1232 sx25->sx25_family = AF_X25;
1233 sx25->sx25_addr = x25->dest_addr;
1234 }
1235
1236 msg->msg_namelen = sizeof(struct sockaddr_x25);
1237
1238 lock_sock(sk);
1239 x25_check_rbuf(sk);
1240 release_sock(sk);
1241 rc = copied;
1242out_free_dgram:
1243 skb_free_datagram(sk, skb);
1244out:
1245 return rc;
1246}
1247
1248
1249static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1250{
1251 struct sock *sk = sock->sk;
1252 struct x25_sock *x25 = x25_sk(sk);
1253 void __user *argp = (void __user *)arg;
1254 int rc;
1255
1256 switch (cmd) {
1257 case TIOCOUTQ: {
1258 int amount = sk->sk_sndbuf -
1259 atomic_read(&sk->sk_wmem_alloc);
1260 if (amount < 0)
1261 amount = 0;
1262 rc = put_user(amount, (unsigned int __user *)argp);
1263 break;
1264 }
1265
1266 case TIOCINQ: {
1267 struct sk_buff *skb;
1268 int amount = 0;
1269 /*
1270 * These two are safe on a single CPU system as
1271 * only user tasks fiddle here
1272 */
1273 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1274 amount = skb->len;
1275 rc = put_user(amount, (unsigned int __user *)argp);
1276 break;
1277 }
1278
1279 case SIOCGSTAMP:
1280 rc = -EINVAL;
1281 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001282 rc = sock_get_timestamp(sk,
1283 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001285 case SIOCGSTAMPNS:
1286 rc = -EINVAL;
1287 if (sk)
1288 rc = sock_get_timestampns(sk,
1289 (struct timespec __user *)argp);
1290 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 case SIOCGIFADDR:
1292 case SIOCSIFADDR:
1293 case SIOCGIFDSTADDR:
1294 case SIOCSIFDSTADDR:
1295 case SIOCGIFBRDADDR:
1296 case SIOCSIFBRDADDR:
1297 case SIOCGIFNETMASK:
1298 case SIOCSIFNETMASK:
1299 case SIOCGIFMETRIC:
1300 case SIOCSIFMETRIC:
1301 rc = -EINVAL;
1302 break;
1303 case SIOCADDRT:
1304 case SIOCDELRT:
1305 rc = -EPERM;
1306 if (!capable(CAP_NET_ADMIN))
1307 break;
1308 rc = x25_route_ioctl(cmd, argp);
1309 break;
1310 case SIOCX25GSUBSCRIP:
1311 rc = x25_subscr_ioctl(cmd, argp);
1312 break;
1313 case SIOCX25SSUBSCRIP:
1314 rc = -EPERM;
1315 if (!capable(CAP_NET_ADMIN))
1316 break;
1317 rc = x25_subscr_ioctl(cmd, argp);
1318 break;
1319 case SIOCX25GFACILITIES: {
1320 struct x25_facilities fac = x25->facilities;
1321 rc = copy_to_user(argp, &fac,
1322 sizeof(fac)) ? -EFAULT : 0;
1323 break;
1324 }
1325
1326 case SIOCX25SFACILITIES: {
1327 struct x25_facilities facilities;
1328 rc = -EFAULT;
1329 if (copy_from_user(&facilities, argp,
1330 sizeof(facilities)))
1331 break;
1332 rc = -EINVAL;
1333 if (sk->sk_state != TCP_LISTEN &&
1334 sk->sk_state != TCP_CLOSE)
1335 break;
1336 if (facilities.pacsize_in < X25_PS16 ||
1337 facilities.pacsize_in > X25_PS4096)
1338 break;
1339 if (facilities.pacsize_out < X25_PS16 ||
1340 facilities.pacsize_out > X25_PS4096)
1341 break;
1342 if (facilities.winsize_in < 1 ||
1343 facilities.winsize_in > 127)
1344 break;
1345 if (facilities.throughput < 0x03 ||
1346 facilities.throughput > 0xDD)
1347 break;
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001348 if (facilities.reverse &&
1349 (facilities.reverse | 0x81)!= 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 break;
1351 x25->facilities = facilities;
1352 rc = 0;
1353 break;
1354 }
1355
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001356 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001357 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001358 sizeof(x25->dte_facilities));
1359 if (rc)
1360 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001361 break;
1362 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001363
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001364 case SIOCX25SDTEFACILITIES: {
1365 struct x25_dte_facilities dtefacs;
1366 rc = -EFAULT;
1367 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001368 break;
1369 rc = -EINVAL;
1370 if (sk->sk_state != TCP_LISTEN &&
1371 sk->sk_state != TCP_CLOSE)
1372 break;
1373 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1374 break;
1375 if (dtefacs.calling_ae == NULL)
1376 break;
1377 if (dtefacs.called_len > X25_MAX_AE_LEN)
1378 break;
1379 if (dtefacs.called_ae == NULL)
1380 break;
1381 x25->dte_facilities = dtefacs;
1382 rc = 0;
1383 break;
1384 }
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 case SIOCX25GCALLUSERDATA: {
1387 struct x25_calluserdata cud = x25->calluserdata;
1388 rc = copy_to_user(argp, &cud,
1389 sizeof(cud)) ? -EFAULT : 0;
1390 break;
1391 }
1392
1393 case SIOCX25SCALLUSERDATA: {
1394 struct x25_calluserdata calluserdata;
1395
1396 rc = -EFAULT;
1397 if (copy_from_user(&calluserdata, argp,
1398 sizeof(calluserdata)))
1399 break;
1400 rc = -EINVAL;
1401 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1402 break;
1403 x25->calluserdata = calluserdata;
1404 rc = 0;
1405 break;
1406 }
1407
1408 case SIOCX25GCAUSEDIAG: {
1409 struct x25_causediag causediag;
1410 causediag = x25->causediag;
1411 rc = copy_to_user(argp, &causediag,
1412 sizeof(causediag)) ? -EFAULT : 0;
1413 break;
1414 }
1415
Shaun Pereiracb65d502005-06-22 22:15:01 -07001416 case SIOCX25SCUDMATCHLEN: {
1417 struct x25_subaddr sub_addr;
1418 rc = -EINVAL;
1419 if(sk->sk_state != TCP_CLOSE)
1420 break;
1421 rc = -EFAULT;
1422 if (copy_from_user(&sub_addr, argp,
1423 sizeof(sub_addr)))
1424 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001425 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001426 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1427 break;
1428 x25->cudmatchlength = sub_addr.cudmatchlength;
1429 rc = 0;
1430 break;
1431 }
1432
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001433 case SIOCX25CALLACCPTAPPRV: {
1434 rc = -EINVAL;
1435 if (sk->sk_state != TCP_CLOSE)
1436 break;
1437 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1438 rc = 0;
1439 break;
1440 }
1441
1442 case SIOCX25SENDCALLACCPT: {
1443 rc = -EINVAL;
1444 if (sk->sk_state != TCP_ESTABLISHED)
1445 break;
1446 if (x25->accptapprv) /* must call accptapprv above */
1447 break;
1448 x25_write_internal(sk, X25_CALL_ACCEPTED);
1449 x25->state = X25_STATE_3;
1450 rc = 0;
1451 break;
1452 }
1453
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001454 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001455 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 break;
1457 }
1458
1459 return rc;
1460}
1461
1462static struct net_proto_family x25_family_ops = {
1463 .family = AF_X25,
1464 .create = x25_create,
1465 .owner = THIS_MODULE,
1466};
1467
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001468#ifdef CONFIG_COMPAT
1469static int compat_x25_subscr_ioctl(unsigned int cmd,
1470 struct compat_x25_subscrip_struct __user *x25_subscr32)
1471{
1472 struct compat_x25_subscrip_struct x25_subscr;
1473 struct x25_neigh *nb;
1474 struct net_device *dev;
1475 int rc = -EINVAL;
1476
1477 rc = -EFAULT;
1478 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1479 goto out;
1480
1481 rc = -EINVAL;
1482 dev = x25_dev_get(x25_subscr.device);
1483 if (dev == NULL)
1484 goto out;
1485
1486 nb = x25_get_neigh(dev);
1487 if (nb == NULL)
1488 goto out_dev_put;
1489
1490 dev_put(dev);
1491
1492 if (cmd == SIOCX25GSUBSCRIP) {
1493 x25_subscr.extended = nb->extended;
1494 x25_subscr.global_facil_mask = nb->global_facil_mask;
1495 rc = copy_to_user(x25_subscr32, &x25_subscr,
1496 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1497 } else {
1498 rc = -EINVAL;
1499 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1500 rc = 0;
1501 nb->extended = x25_subscr.extended;
1502 nb->global_facil_mask = x25_subscr.global_facil_mask;
1503 }
1504 }
1505 x25_neigh_put(nb);
1506out:
1507 return rc;
1508out_dev_put:
1509 dev_put(dev);
1510 goto out;
1511}
1512
1513static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1514 unsigned long arg)
1515{
1516 void __user *argp = compat_ptr(arg);
1517 struct sock *sk = sock->sk;
1518
1519 int rc = -ENOIOCTLCMD;
1520
1521 switch(cmd) {
1522 case TIOCOUTQ:
1523 case TIOCINQ:
1524 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1525 break;
1526 case SIOCGSTAMP:
1527 rc = -EINVAL;
1528 if (sk)
1529 rc = compat_sock_get_timestamp(sk,
1530 (struct timeval __user*)argp);
1531 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001532 case SIOCGSTAMPNS:
1533 rc = -EINVAL;
1534 if (sk)
1535 rc = compat_sock_get_timestampns(sk,
1536 (struct timespec __user*)argp);
1537 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001538 case SIOCGIFADDR:
1539 case SIOCSIFADDR:
1540 case SIOCGIFDSTADDR:
1541 case SIOCSIFDSTADDR:
1542 case SIOCGIFBRDADDR:
1543 case SIOCSIFBRDADDR:
1544 case SIOCGIFNETMASK:
1545 case SIOCSIFNETMASK:
1546 case SIOCGIFMETRIC:
1547 case SIOCSIFMETRIC:
1548 rc = -EINVAL;
1549 break;
1550 case SIOCADDRT:
1551 case SIOCDELRT:
1552 rc = -EPERM;
1553 if (!capable(CAP_NET_ADMIN))
1554 break;
1555 rc = x25_route_ioctl(cmd, argp);
1556 break;
1557 case SIOCX25GSUBSCRIP:
1558 rc = compat_x25_subscr_ioctl(cmd, argp);
1559 break;
1560 case SIOCX25SSUBSCRIP:
1561 rc = -EPERM;
1562 if (!capable(CAP_NET_ADMIN))
1563 break;
1564 rc = compat_x25_subscr_ioctl(cmd, argp);
1565 break;
1566 case SIOCX25GFACILITIES:
1567 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001568 case SIOCX25GDTEFACILITIES:
1569 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001570 case SIOCX25GCALLUSERDATA:
1571 case SIOCX25SCALLUSERDATA:
1572 case SIOCX25GCAUSEDIAG:
1573 case SIOCX25SCUDMATCHLEN:
1574 case SIOCX25CALLACCPTAPPRV:
1575 case SIOCX25SENDCALLACCPT:
1576 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1577 break;
1578 default:
1579 rc = -ENOIOCTLCMD;
1580 break;
1581 }
1582 return rc;
1583}
1584#endif
1585
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001586static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 .family = AF_X25,
1588 .owner = THIS_MODULE,
1589 .release = x25_release,
1590 .bind = x25_bind,
1591 .connect = x25_connect,
1592 .socketpair = sock_no_socketpair,
1593 .accept = x25_accept,
1594 .getname = x25_getname,
1595 .poll = datagram_poll,
1596 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001597#ifdef CONFIG_COMPAT
1598 .compat_ioctl = compat_x25_ioctl,
1599#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 .listen = x25_listen,
1601 .shutdown = sock_no_shutdown,
1602 .setsockopt = x25_setsockopt,
1603 .getsockopt = x25_getsockopt,
1604 .sendmsg = x25_sendmsg,
1605 .recvmsg = x25_recvmsg,
1606 .mmap = sock_no_mmap,
1607 .sendpage = sock_no_sendpage,
1608};
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610SOCKOPS_WRAP(x25_proto, AF_X25);
1611
1612static struct packet_type x25_packet_type = {
1613 .type = __constant_htons(ETH_P_X25),
1614 .func = x25_lapb_receive_frame,
1615};
1616
1617static struct notifier_block x25_dev_notifier = {
1618 .notifier_call = x25_device_event,
1619};
1620
1621void x25_kill_by_neigh(struct x25_neigh *nb)
1622{
1623 struct sock *s;
1624 struct hlist_node *node;
1625
1626 write_lock_bh(&x25_list_lock);
1627
1628 sk_for_each(s, node, &x25_list)
1629 if (x25_sk(s)->neighbour == nb)
1630 x25_disconnect(s, ENETUNREACH, 0, 0);
1631
1632 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001633
1634 /* Remove any related forwards */
1635 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
1638static int __init x25_init(void)
1639{
1640 int rc = proto_register(&x25_proto, 0);
1641
1642 if (rc != 0)
1643 goto out;
1644
1645 sock_register(&x25_family_ops);
1646
1647 dev_add_pack(&x25_packet_type);
1648
1649 register_netdevice_notifier(&x25_dev_notifier);
1650
maximilian attemsa44562e2008-01-28 20:43:16 -08001651 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653#ifdef CONFIG_SYSCTL
1654 x25_register_sysctl();
1655#endif
1656 x25_proc_init();
1657out:
1658 return rc;
1659}
1660module_init(x25_init);
1661
1662static void __exit x25_exit(void)
1663{
1664 x25_proc_exit();
1665 x25_link_free();
1666 x25_route_free();
1667
1668#ifdef CONFIG_SYSCTL
1669 x25_unregister_sysctl();
1670#endif
1671
1672 unregister_netdevice_notifier(&x25_dev_notifier);
1673
1674 dev_remove_pack(&x25_packet_type);
1675
1676 sock_unregister(AF_X25);
1677 proto_unregister(&x25_proto);
1678}
1679module_exit(x25_exit);
1680
1681MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1682MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1683MODULE_LICENSE("GPL");
1684MODULE_ALIAS_NETPROTO(PF_X25);