blob: 38e235f61e27028c1db80c36608fef1aae069c0e [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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040043#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/timer.h>
45#include <linux/string.h>
46#include <linux/net.h>
47#include <linux/netdevice.h>
48#include <linux/if_arp.h>
49#include <linux/skbuff.h>
50#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070051#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
53#include <linux/fcntl.h>
54#include <linux/termios.h> /* For TIOCINQ/OUTQ */
55#include <linux/notifier.h>
56#include <linux/init.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080057#include <linux/compat.h>
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080060#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
63int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
64int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
65int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
66int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080067int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69HLIST_HEAD(x25_list);
70DEFINE_RWLOCK(x25_list_lock);
71
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080072static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static struct x25_address null_x25_address = {" "};
75
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080076#ifdef CONFIG_COMPAT
77struct compat_x25_subscrip_struct {
78 char device[200-sizeof(compat_ulong_t)];
79 compat_ulong_t global_facil_mask;
80 compat_uint_t extended;
81};
82#endif
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
85 struct x25_address *calling_addr)
86{
Eric Dumazet6bf15742008-01-13 22:27:52 -080087 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -080089 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 called_len = (*p >> 0) & 0x0F;
92 calling_len = (*p >> 4) & 0x0F;
93
94 called = called_addr->x25_addr;
95 calling = calling_addr->x25_addr;
96 p++;
97
98 for (i = 0; i < (called_len + calling_len); i++) {
99 if (i < called_len) {
100 if (i % 2 != 0) {
101 *called++ = ((*p >> 0) & 0x0F) + '0';
102 p++;
103 } else {
104 *called++ = ((*p >> 4) & 0x0F) + '0';
105 }
106 } else {
107 if (i % 2 != 0) {
108 *calling++ = ((*p >> 0) & 0x0F) + '0';
109 p++;
110 } else {
111 *calling++ = ((*p >> 4) & 0x0F) + '0';
112 }
113 }
114 }
115
116 *called = *calling = '\0';
117
118 return 1 + (called_len + calling_len + 1) / 2;
119}
120
121int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
122 struct x25_address *calling_addr)
123{
124 unsigned int called_len, calling_len;
125 char *called, *calling;
126 int i;
127
128 called = called_addr->x25_addr;
129 calling = calling_addr->x25_addr;
130
131 called_len = strlen(called);
132 calling_len = strlen(calling);
133
134 *p++ = (calling_len << 4) | (called_len << 0);
135
136 for (i = 0; i < (called_len + calling_len); i++) {
137 if (i < called_len) {
138 if (i % 2 != 0) {
139 *p |= (*called++ - '0') << 0;
140 p++;
141 } else {
142 *p = 0x00;
143 *p |= (*called++ - '0') << 4;
144 }
145 } else {
146 if (i % 2 != 0) {
147 *p |= (*calling++ - '0') << 0;
148 p++;
149 } else {
150 *p = 0x00;
151 *p |= (*calling++ - '0') << 4;
152 }
153 }
154 }
155
156 return 1 + (called_len + calling_len + 1) / 2;
157}
158
159/*
160 * Socket removal during an interrupt is now safe.
161 */
162static void x25_remove_socket(struct sock *sk)
163{
164 write_lock_bh(&x25_list_lock);
165 sk_del_node_init(sk);
166 write_unlock_bh(&x25_list_lock);
167}
168
169/*
170 * Kill all bound sockets on a dropped device.
171 */
172static void x25_kill_by_device(struct net_device *dev)
173{
174 struct sock *s;
175 struct hlist_node *node;
176
177 write_lock_bh(&x25_list_lock);
178
179 sk_for_each(s, node, &x25_list)
180 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
181 x25_disconnect(s, ENETUNREACH, 0, 0);
182
183 write_unlock_bh(&x25_list_lock);
184}
185
186/*
187 * Handle device status changes.
188 */
189static int x25_device_event(struct notifier_block *this, unsigned long event,
190 void *ptr)
191{
192 struct net_device *dev = ptr;
193 struct x25_neigh *nb;
194
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700195 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200196 return NOTIFY_DONE;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (dev->type == ARPHRD_X25
199#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
200 || dev->type == ARPHRD_ETHER
201#endif
202 ) {
203 switch (event) {
204 case NETDEV_UP:
205 x25_link_device_up(dev);
206 break;
207 case NETDEV_GOING_DOWN:
208 nb = x25_get_neigh(dev);
209 if (nb) {
210 x25_terminate_link(nb);
211 x25_neigh_put(nb);
212 }
213 break;
214 case NETDEV_DOWN:
215 x25_kill_by_device(dev);
216 x25_route_device_down(dev);
217 x25_link_device_down(dev);
218 break;
219 }
220 }
221
222 return NOTIFY_DONE;
223}
224
225/*
226 * Add a socket to the bound sockets list.
227 */
228static void x25_insert_socket(struct sock *sk)
229{
230 write_lock_bh(&x25_list_lock);
231 sk_add_node(sk, &x25_list);
232 write_unlock_bh(&x25_list_lock);
233}
234
235/*
236 * Find a socket that wants to accept the Call Request we just
237 * received. Check the full list for an address/cud match.
238 * If no cuds match return the next_best thing, an address match.
239 * Note: if a listening socket has cud set it must only get calls
240 * with matching cud.
241 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700242static struct sock *x25_find_listener(struct x25_address *addr,
243 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct sock *s;
246 struct sock *next_best;
247 struct hlist_node *node;
248
249 read_lock_bh(&x25_list_lock);
250 next_best = NULL;
251
252 sk_for_each(s, node, &x25_list)
253 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700254 x25_sk(s)->source_addr.x25_addr) ||
255 !strcmp(addr->x25_addr,
256 null_x25_address.x25_addr)) &&
257 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
259 * Found a listening socket, now check the incoming
260 * call user data vs this sockets call user data
261 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700262 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900263 if((memcmp(x25_sk(s)->calluserdata.cuddata,
264 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700265 x25_sk(s)->cudmatchlength)) == 0) {
266 sock_hold(s);
267 goto found;
268 }
269 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 if (next_best) {
273 s = next_best;
274 sock_hold(s);
275 goto found;
276 }
277 s = NULL;
278found:
279 read_unlock_bh(&x25_list_lock);
280 return s;
281}
282
283/*
284 * Find a connected X.25 socket given my LCI and neighbour.
285 */
286static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
287{
288 struct sock *s;
289 struct hlist_node *node;
290
291 sk_for_each(s, node, &x25_list)
292 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
293 sock_hold(s);
294 goto found;
295 }
296 s = NULL;
297found:
298 return s;
299}
300
301struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
302{
303 struct sock *s;
304
305 read_lock_bh(&x25_list_lock);
306 s = __x25_find_socket(lci, nb);
307 read_unlock_bh(&x25_list_lock);
308 return s;
309}
310
311/*
312 * Find a unique LCI for a given device.
313 */
314static unsigned int x25_new_lci(struct x25_neigh *nb)
315{
316 unsigned int lci = 1;
317 struct sock *sk;
318
319 read_lock_bh(&x25_list_lock);
320
321 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
322 sock_put(sk);
323 if (++lci == 4096) {
324 lci = 0;
325 break;
326 }
327 }
328
329 read_unlock_bh(&x25_list_lock);
330 return lci;
331}
332
333/*
334 * Deferred destroy.
335 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700336static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/*
339 * handler for deferred kills.
340 */
341static void x25_destroy_timer(unsigned long data)
342{
David S. Miller14ebaf82009-06-16 05:40:30 -0700343 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/*
347 * This is called from user mode and the timers. Thus it protects itself
348 * against interrupt users but doesn't worry about being called during
349 * work. Once it is removed from the queue no interrupt or bottom half
350 * will touch it and we are (fairly 8-) ) safe.
351 * Not static as it's used by the timer
352 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700353static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sk_buff *skb;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 x25_stop_heartbeat(sk);
358 x25_stop_timer(sk);
359
360 x25_remove_socket(sk);
361 x25_clear_queues(sk); /* Flush the queues */
362
363 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
364 if (skb->sk != sk) { /* A pending connection */
365 /*
366 * Queue the unaccepted socket for death
367 */
368 sock_set_flag(skb->sk, SOCK_DEAD);
369 x25_start_heartbeat(skb->sk);
370 x25_sk(skb->sk)->state = X25_STATE_0;
371 }
372
373 kfree_skb(skb);
374 }
375
Eric Dumazetc5640392009-06-16 10:12:03 +0000376 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Defer: outstanding buffers */
378 sk->sk_timer.expires = jiffies + 10 * HZ;
379 sk->sk_timer.function = x25_destroy_timer;
380 sk->sk_timer.data = (unsigned long)sk;
381 add_timer(&sk->sk_timer);
382 } else {
383 /* drop last reference so sock_put will free */
384 __sock_put(sk);
385 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David S. Miller14ebaf82009-06-16 05:40:30 -0700388void x25_destroy_socket_from_timer(struct sock *sk)
389{
390 sock_hold(sk);
391 bh_lock_sock(sk);
392 __x25_destroy_socket(sk);
393 bh_unlock_sock(sk);
394 sock_put(sk);
395}
396
397static void x25_destroy_socket(struct sock *sk)
398{
399 sock_hold(sk);
400 lock_sock(sk);
401 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 release_sock(sk);
403 sock_put(sk);
404}
405
406/*
407 * Handling for system calls applied via the various interfaces to a
408 * X.25 socket object.
409 */
410
411static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700412 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int opt;
415 struct sock *sk = sock->sk;
416 int rc = -ENOPROTOOPT;
417
418 if (level != SOL_X25 || optname != X25_QBITINCL)
419 goto out;
420
421 rc = -EINVAL;
422 if (optlen < sizeof(int))
423 goto out;
424
425 rc = -EFAULT;
426 if (get_user(opt, (int __user *)optval))
427 goto out;
428
429 x25_sk(sk)->qbitincl = !!opt;
430 rc = 0;
431out:
432 return rc;
433}
434
435static int x25_getsockopt(struct socket *sock, int level, int optname,
436 char __user *optval, int __user *optlen)
437{
438 struct sock *sk = sock->sk;
439 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (level != SOL_X25 || optname != X25_QBITINCL)
442 goto out;
443
444 rc = -EFAULT;
445 if (get_user(len, optlen))
446 goto out;
447
448 len = min_t(unsigned int, len, sizeof(int));
449
450 rc = -EINVAL;
451 if (len < 0)
452 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 rc = -EFAULT;
455 if (put_user(len, optlen))
456 goto out;
457
458 val = x25_sk(sk)->qbitincl;
459 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
460out:
461 return rc;
462}
463
464static int x25_listen(struct socket *sock, int backlog)
465{
466 struct sock *sk = sock->sk;
467 int rc = -EOPNOTSUPP;
468
469 if (sk->sk_state != TCP_LISTEN) {
470 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
471 sk->sk_max_ack_backlog = backlog;
472 sk->sk_state = TCP_LISTEN;
473 rc = 0;
474 }
475
476 return rc;
477}
478
479static struct proto x25_proto = {
480 .name = "X25",
481 .owner = THIS_MODULE,
482 .obj_size = sizeof(struct x25_sock),
483};
484
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700485static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700488 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 if (!sk)
491 goto out;
492
493 sock_init_data(NULL, sk);
494
495 x25 = x25_sk(sk);
496 skb_queue_head_init(&x25->ack_queue);
497 skb_queue_head_init(&x25->fragment_queue);
498 skb_queue_head_init(&x25->interrupt_in_queue);
499 skb_queue_head_init(&x25->interrupt_out_queue);
500out:
501 return sk;
502}
503
Eric Paris3f378b62009-11-05 22:18:14 -0800504static int x25_create(struct net *net, struct socket *sock, int protocol,
505 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct sock *sk;
508 struct x25_sock *x25;
509 int rc = -ESOCKTNOSUPPORT;
510
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700511 if (net != &init_net)
512 return -EAFNOSUPPORT;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (sock->type != SOCK_SEQPACKET || protocol)
515 goto out;
516
517 rc = -ENOMEM;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700518 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520
521 x25 = x25_sk(sk);
522
523 sock_init_data(sock, sk);
524
525 x25_init_timers(sk);
526
527 sock->ops = &x25_proto_ops;
528 sk->sk_protocol = protocol;
529 sk->sk_backlog_rcv = x25_backlog_rcv;
530
531 x25->t21 = sysctl_x25_call_request_timeout;
532 x25->t22 = sysctl_x25_reset_request_timeout;
533 x25->t23 = sysctl_x25_clear_request_timeout;
534 x25->t2 = sysctl_x25_ack_holdback_timeout;
535 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700536 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700537 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
538 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
541 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
542 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
543 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
544 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
545 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900546 x25->dte_facilities.calling_len = 0;
547 x25->dte_facilities.called_len = 0;
548 memset(x25->dte_facilities.called_ae, '\0',
549 sizeof(x25->dte_facilities.called_ae));
550 memset(x25->dte_facilities.calling_ae, '\0',
551 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 rc = 0;
554out:
555 return rc;
556}
557
558static struct sock *x25_make_new(struct sock *osk)
559{
560 struct sock *sk = NULL;
561 struct x25_sock *x25, *ox25;
562
563 if (osk->sk_type != SOCK_SEQPACKET)
564 goto out;
565
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900566 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto out;
568
569 x25 = x25_sk(sk);
570
571 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 sk->sk_priority = osk->sk_priority;
573 sk->sk_protocol = osk->sk_protocol;
574 sk->sk_rcvbuf = osk->sk_rcvbuf;
575 sk->sk_sndbuf = osk->sk_sndbuf;
576 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800578 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 ox25 = x25_sk(osk);
581 x25->t21 = ox25->t21;
582 x25->t22 = ox25->t22;
583 x25->t23 = ox25->t23;
584 x25->t2 = ox25->t2;
585 x25->facilities = ox25->facilities;
586 x25->qbitincl = ox25->qbitincl;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800587 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700588 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700589 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 x25_init_timers(sk);
592out:
593 return sk;
594}
595
596static int x25_release(struct socket *sock)
597{
598 struct sock *sk = sock->sk;
599 struct x25_sock *x25;
600
601 if (!sk)
602 goto out;
603
604 x25 = x25_sk(sk);
605
606 switch (x25->state) {
607
608 case X25_STATE_0:
609 case X25_STATE_2:
610 x25_disconnect(sk, 0, 0, 0);
611 x25_destroy_socket(sk);
612 goto out;
613
614 case X25_STATE_1:
615 case X25_STATE_3:
616 case X25_STATE_4:
617 x25_clear_queues(sk);
618 x25_write_internal(sk, X25_CLEAR_REQUEST);
619 x25_start_t23timer(sk);
620 x25->state = X25_STATE_2;
621 sk->sk_state = TCP_CLOSE;
622 sk->sk_shutdown |= SEND_SHUTDOWN;
623 sk->sk_state_change(sk);
624 sock_set_flag(sk, SOCK_DEAD);
625 sock_set_flag(sk, SOCK_DESTROY);
626 break;
627 }
628
David S. Millerc751e4f2008-06-17 03:05:13 -0700629 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630out:
631 return 0;
632}
633
634static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
635{
636 struct sock *sk = sock->sk;
637 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
638
639 if (!sock_flag(sk, SOCK_ZAPPED) ||
640 addr_len != sizeof(struct sockaddr_x25) ||
641 addr->sx25_family != AF_X25)
642 return -EINVAL;
643
644 x25_sk(sk)->source_addr = addr->sx25_addr;
645 x25_insert_socket(sk);
646 sock_reset_flag(sk, SOCK_ZAPPED);
647 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
648
649 return 0;
650}
651
652static int x25_wait_for_connection_establishment(struct sock *sk)
653{
654 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900655 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657 add_wait_queue_exclusive(sk->sk_sleep, &wait);
658 for (;;) {
659 __set_current_state(TASK_INTERRUPTIBLE);
660 rc = -ERESTARTSYS;
661 if (signal_pending(current))
662 break;
663 rc = sock_error(sk);
664 if (rc) {
665 sk->sk_socket->state = SS_UNCONNECTED;
666 break;
667 }
668 rc = 0;
669 if (sk->sk_state != TCP_ESTABLISHED) {
670 release_sock(sk);
671 schedule();
672 lock_sock(sk);
673 } else
674 break;
675 }
676 __set_current_state(TASK_RUNNING);
677 remove_wait_queue(sk->sk_sleep, &wait);
678 return rc;
679}
680
681static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
682 int addr_len, int flags)
683{
684 struct sock *sk = sock->sk;
685 struct x25_sock *x25 = x25_sk(sk);
686 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
687 struct x25_route *rt;
688 int rc = 0;
689
690 lock_sock(sk);
691 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
692 sock->state = SS_CONNECTED;
693 goto out; /* Connect completed during a ERESTARTSYS event */
694 }
695
696 rc = -ECONNREFUSED;
697 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
698 sock->state = SS_UNCONNECTED;
699 goto out;
700 }
701
702 rc = -EISCONN; /* No reconnect on a seqpacket socket */
703 if (sk->sk_state == TCP_ESTABLISHED)
704 goto out;
705
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900706 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 sock->state = SS_UNCONNECTED;
708
709 rc = -EINVAL;
710 if (addr_len != sizeof(struct sockaddr_x25) ||
711 addr->sx25_family != AF_X25)
712 goto out;
713
714 rc = -ENETUNREACH;
715 rt = x25_get_route(&addr->sx25_addr);
716 if (!rt)
717 goto out;
718
719 x25->neighbour = x25_get_neigh(rt->dev);
720 if (!x25->neighbour)
721 goto out_put_route;
722
723 x25_limit_facilities(&x25->facilities, x25->neighbour);
724
725 x25->lci = x25_new_lci(x25->neighbour);
726 if (!x25->lci)
727 goto out_put_neigh;
728
729 rc = -EINVAL;
730 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
731 goto out_put_neigh;
732
733 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
734 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
735
736 x25->dest_addr = addr->sx25_addr;
737
738 /* Move to connecting socket, start sending Connect Requests */
739 sock->state = SS_CONNECTING;
740 sk->sk_state = TCP_SYN_SENT;
741
742 x25->state = X25_STATE_1;
743
744 x25_write_internal(sk, X25_CALL_REQUEST);
745
746 x25_start_heartbeat(sk);
747 x25_start_t21timer(sk);
748
749 /* Now the loop */
750 rc = -EINPROGRESS;
751 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
752 goto out_put_neigh;
753
754 rc = x25_wait_for_connection_establishment(sk);
755 if (rc)
756 goto out_put_neigh;
757
758 sock->state = SS_CONNECTED;
759 rc = 0;
760out_put_neigh:
761 if (rc)
762 x25_neigh_put(x25->neighbour);
763out_put_route:
764 x25_route_put(rt);
765out:
766 release_sock(sk);
767 return rc;
768}
769
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800770static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 DECLARE_WAITQUEUE(wait, current);
773 int rc = 0;
774
775 add_wait_queue_exclusive(sk->sk_sleep, &wait);
776 for (;;) {
777 __set_current_state(TASK_INTERRUPTIBLE);
778 if (sk->sk_shutdown & RCV_SHUTDOWN)
779 break;
780 rc = -ERESTARTSYS;
781 if (signal_pending(current))
782 break;
783 rc = -EAGAIN;
784 if (!timeout)
785 break;
786 rc = 0;
787 if (skb_queue_empty(&sk->sk_receive_queue)) {
788 release_sock(sk);
789 timeout = schedule_timeout(timeout);
790 lock_sock(sk);
791 } else
792 break;
793 }
794 __set_current_state(TASK_RUNNING);
795 remove_wait_queue(sk->sk_sleep, &wait);
796 return rc;
797}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
800{
801 struct sock *sk = sock->sk;
802 struct sock *newsk;
803 struct sk_buff *skb;
804 int rc = -EINVAL;
805
806 if (!sk || sk->sk_state != TCP_LISTEN)
807 goto out;
808
809 rc = -EOPNOTSUPP;
810 if (sk->sk_type != SOCK_SEQPACKET)
811 goto out;
812
813 lock_sock(sk);
814 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
815 if (rc)
816 goto out2;
817 skb = skb_dequeue(&sk->sk_receive_queue);
818 rc = -EINVAL;
819 if (!skb->sk)
820 goto out2;
821 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700822 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 /* Now attach up the new socket */
825 skb->sk = NULL;
826 kfree_skb(skb);
827 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 newsock->state = SS_CONNECTED;
829 rc = 0;
830out2:
831 release_sock(sk);
832out:
833 return rc;
834}
835
836static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
837 int *uaddr_len, int peer)
838{
839 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
840 struct sock *sk = sock->sk;
841 struct x25_sock *x25 = x25_sk(sk);
842
843 if (peer) {
844 if (sk->sk_state != TCP_ESTABLISHED)
845 return -ENOTCONN;
846 sx25->sx25_addr = x25->dest_addr;
847 } else
848 sx25->sx25_addr = x25->source_addr;
849
850 sx25->sx25_family = AF_X25;
851 *uaddr_len = sizeof(*sx25);
852
853 return 0;
854}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
857 unsigned int lci)
858{
859 struct sock *sk;
860 struct sock *make;
861 struct x25_sock *makex25;
862 struct x25_address source_addr, dest_addr;
863 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800864 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800865 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * Remove the LCI and frame type.
869 */
870 skb_pull(skb, X25_STD_MIN_LEN);
871
872 /*
873 * Extract the X.25 addresses and convert them to ASCII strings,
874 * and remove them.
875 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800876 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
877 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /*
880 * Get the length of the facilities, skip past them for the moment
881 * get the call user data because this is needed to determine
882 * the correct listener
883 */
884 len = skb->data[0] + 1;
885 skb_pull(skb,len);
886
887 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * Find a listener for the particular address/cud pair.
889 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700890 sk = x25_find_listener(&source_addr,skb);
891 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800893 if (sk != NULL && sk_acceptq_is_full(sk)) {
894 goto out_sock_put;
895 }
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800898 * We dont have any listeners for this incoming call.
899 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800901 if (sk == NULL) {
902 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800903 if (sysctl_x25_forward &&
904 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800905 {
906 /* Call was forwarded, dont process it any more */
907 kfree_skb(skb);
908 rc = 1;
909 goto out;
910 } else {
911 /* No listeners, can't forward, clear the call */
912 goto out_clear_request;
913 }
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * Try to reach a compromise on the requested facilities.
918 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800919 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
920 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 goto out_sock_put;
922
923 /*
924 * current neighbour/link might impose additional limits
925 * on certain facilties
926 */
927
928 x25_limit_facilities(&facilities, nb);
929
930 /*
931 * Try to create a new socket.
932 */
933 make = x25_make_new(sk);
934 if (!make)
935 goto out_sock_put;
936
937 /*
938 * Remove the facilities
939 */
940 skb_pull(skb, len);
941
942 skb->sk = make;
943 make->sk_state = TCP_ESTABLISHED;
944
945 makex25 = x25_sk(make);
946 makex25->lci = lci;
947 makex25->dest_addr = dest_addr;
948 makex25->source_addr = source_addr;
949 makex25->neighbour = nb;
950 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800951 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700953 /* ensure no reverse facil on accept */
954 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800955 /* ensure no calling address extension on accept */
956 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700957 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700959 /* Normally all calls are accepted immediatly */
960 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
961 x25_write_internal(make, X25_CALL_ACCEPTED);
962 makex25->state = X25_STATE_3;
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Shaun Pereiracb65d502005-06-22 22:15:01 -0700965 /*
966 * Incoming Call User Data.
967 */
Roel Kluin8db09f22009-03-13 16:04:12 -0700968 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
969 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 sk->sk_ack_backlog++;
972
973 x25_insert_socket(make);
974
975 skb_queue_head(&sk->sk_receive_queue, skb);
976
977 x25_start_heartbeat(make);
978
979 if (!sock_flag(sk, SOCK_DEAD))
980 sk->sk_data_ready(sk, skb->len);
981 rc = 1;
982 sock_put(sk);
983out:
984 return rc;
985out_sock_put:
986 sock_put(sk);
987out_clear_request:
988 rc = 0;
989 x25_transmit_clear_request(nb, lci, 0x01);
990 goto out;
991}
992
993static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
994 struct msghdr *msg, size_t len)
995{
996 struct sock *sk = sock->sk;
997 struct x25_sock *x25 = x25_sk(sk);
998 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
999 struct sockaddr_x25 sx25;
1000 struct sk_buff *skb;
1001 unsigned char *asmptr;
1002 int noblock = msg->msg_flags & MSG_DONTWAIT;
1003 size_t size;
1004 int qbit = 0, rc = -EINVAL;
1005
1006 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1007 goto out;
1008
1009 /* we currently don't support segmented records at the user interface */
1010 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1011 goto out;
1012
1013 rc = -EADDRNOTAVAIL;
1014 if (sock_flag(sk, SOCK_ZAPPED))
1015 goto out;
1016
1017 rc = -EPIPE;
1018 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1019 send_sig(SIGPIPE, current, 0);
1020 goto out;
1021 }
1022
1023 rc = -ENETUNREACH;
1024 if (!x25->neighbour)
1025 goto out;
1026
1027 if (usx25) {
1028 rc = -EINVAL;
1029 if (msg->msg_namelen < sizeof(sx25))
1030 goto out;
1031 memcpy(&sx25, usx25, sizeof(sx25));
1032 rc = -EISCONN;
1033 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1034 goto out;
1035 rc = -EINVAL;
1036 if (sx25.sx25_family != AF_X25)
1037 goto out;
1038 } else {
1039 /*
1040 * FIXME 1003.1g - if the socket is like this because
1041 * it has become closed (not started closed) we ought
1042 * to SIGPIPE, EPIPE;
1043 */
1044 rc = -ENOTCONN;
1045 if (sk->sk_state != TCP_ESTABLISHED)
1046 goto out;
1047
1048 sx25.sx25_family = AF_X25;
1049 sx25.sx25_addr = x25->dest_addr;
1050 }
1051
Alan Cox83e0bbc2009-03-27 00:28:21 -07001052 /* Sanity check the packet size */
1053 if (len > 65535) {
1054 rc = -EMSGSIZE;
1055 goto out;
1056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1059
1060 /* Build a packet */
1061 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1062
1063 if ((msg->msg_flags & MSG_OOB) && len > 32)
1064 len = 32;
1065
1066 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1067
1068 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1069 if (!skb)
1070 goto out;
1071 X25_SKB_CB(skb)->flags = msg->msg_flags;
1072
1073 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1074
1075 /*
1076 * Put the data on the end
1077 */
1078 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1079
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001080 skb_reset_transport_header(skb);
1081 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001083 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (rc)
1085 goto out_kfree_skb;
1086
1087 /*
1088 * If the Q BIT Include socket option is in force, the first
1089 * byte of the user data is the logical value of the Q Bit.
1090 */
1091 if (x25->qbitincl) {
1092 qbit = skb->data[0];
1093 skb_pull(skb, 1);
1094 }
1095
1096 /*
1097 * Push down the X.25 header
1098 */
1099 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1100
1101 if (msg->msg_flags & MSG_OOB) {
1102 if (x25->neighbour->extended) {
1103 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1104 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1105 *asmptr++ = (x25->lci >> 0) & 0xFF;
1106 *asmptr++ = X25_INTERRUPT;
1107 } else {
1108 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1109 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1110 *asmptr++ = (x25->lci >> 0) & 0xFF;
1111 *asmptr++ = X25_INTERRUPT;
1112 }
1113 } else {
1114 if (x25->neighbour->extended) {
1115 /* Build an Extended X.25 header */
1116 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1117 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1118 *asmptr++ = (x25->lci >> 0) & 0xFF;
1119 *asmptr++ = X25_DATA;
1120 *asmptr++ = X25_DATA;
1121 } else {
1122 /* Build an Standard X.25 header */
1123 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1124 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1125 *asmptr++ = (x25->lci >> 0) & 0xFF;
1126 *asmptr++ = X25_DATA;
1127 }
1128
1129 if (qbit)
1130 skb->data[0] |= X25_Q_BIT;
1131 }
1132
1133 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1134 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1135
1136 rc = -ENOTCONN;
1137 if (sk->sk_state != TCP_ESTABLISHED)
1138 goto out_kfree_skb;
1139
1140 if (msg->msg_flags & MSG_OOB)
1141 skb_queue_tail(&x25->interrupt_out_queue, skb);
1142 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001143 rc = x25_output(sk, skb);
1144 len = rc;
1145 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 kfree_skb(skb);
1147 else if (x25->qbitincl)
1148 len++;
1149 }
1150
1151 /*
1152 * lock_sock() is currently only used to serialize this x25_kick()
1153 * against input-driven x25_kick() calls. It currently only blocks
1154 * incoming packets for this socket and does not protect against
1155 * any other socket state changes and is not called from anywhere
1156 * else. As x25_kick() cannot block and as long as all socket
1157 * operations are BKL-wrapped, we don't need take to care about
1158 * purging the backlog queue in x25_release().
1159 *
1160 * Using lock_sock() to protect all socket operations entirely
1161 * (and making the whole x25 stack SMP aware) unfortunately would
1162 * require major changes to {send,recv}msg and skb allocation methods.
1163 * -> 2.5 ;)
1164 */
1165 lock_sock(sk);
1166 x25_kick(sk);
1167 release_sock(sk);
1168 rc = len;
1169out:
1170 return rc;
1171out_kfree_skb:
1172 kfree_skb(skb);
1173 goto out;
1174}
1175
1176
1177static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1178 struct msghdr *msg, size_t size,
1179 int flags)
1180{
1181 struct sock *sk = sock->sk;
1182 struct x25_sock *x25 = x25_sk(sk);
1183 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1184 size_t copied;
1185 int qbit;
1186 struct sk_buff *skb;
1187 unsigned char *asmptr;
1188 int rc = -ENOTCONN;
1189
1190 /*
1191 * This works for seqpacket too. The receiver has ordered the queue for
1192 * us! We do one quick check first though
1193 */
1194 if (sk->sk_state != TCP_ESTABLISHED)
1195 goto out;
1196
1197 if (flags & MSG_OOB) {
1198 rc = -EINVAL;
1199 if (sock_flag(sk, SOCK_URGINLINE) ||
1200 !skb_peek(&x25->interrupt_in_queue))
1201 goto out;
1202
1203 skb = skb_dequeue(&x25->interrupt_in_queue);
1204
1205 skb_pull(skb, X25_STD_MIN_LEN);
1206
1207 /*
1208 * No Q bit information on Interrupt data.
1209 */
1210 if (x25->qbitincl) {
1211 asmptr = skb_push(skb, 1);
1212 *asmptr = 0x00;
1213 }
1214
1215 msg->msg_flags |= MSG_OOB;
1216 } else {
1217 /* Now we can treat all alike */
1218 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1219 flags & MSG_DONTWAIT, &rc);
1220 if (!skb)
1221 goto out;
1222
1223 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1224
1225 skb_pull(skb, x25->neighbour->extended ?
1226 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1227
1228 if (x25->qbitincl) {
1229 asmptr = skb_push(skb, 1);
1230 *asmptr = qbit;
1231 }
1232 }
1233
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001234 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 copied = skb->len;
1236
1237 if (copied > size) {
1238 copied = size;
1239 msg->msg_flags |= MSG_TRUNC;
1240 }
1241
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001242 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 msg->msg_flags |= MSG_EOR;
1244
1245 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1246 if (rc)
1247 goto out_free_dgram;
1248
1249 if (sx25) {
1250 sx25->sx25_family = AF_X25;
1251 sx25->sx25_addr = x25->dest_addr;
1252 }
1253
1254 msg->msg_namelen = sizeof(struct sockaddr_x25);
1255
1256 lock_sock(sk);
1257 x25_check_rbuf(sk);
1258 release_sock(sk);
1259 rc = copied;
1260out_free_dgram:
1261 skb_free_datagram(sk, skb);
1262out:
1263 return rc;
1264}
1265
1266
1267static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1268{
1269 struct sock *sk = sock->sk;
1270 struct x25_sock *x25 = x25_sk(sk);
1271 void __user *argp = (void __user *)arg;
1272 int rc;
1273
1274 switch (cmd) {
1275 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001276 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (amount < 0)
1279 amount = 0;
1280 rc = put_user(amount, (unsigned int __user *)argp);
1281 break;
1282 }
1283
1284 case TIOCINQ: {
1285 struct sk_buff *skb;
1286 int amount = 0;
1287 /*
1288 * These two are safe on a single CPU system as
1289 * only user tasks fiddle here
1290 */
1291 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1292 amount = skb->len;
1293 rc = put_user(amount, (unsigned int __user *)argp);
1294 break;
1295 }
1296
1297 case SIOCGSTAMP:
1298 rc = -EINVAL;
1299 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001300 rc = sock_get_timestamp(sk,
1301 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001303 case SIOCGSTAMPNS:
1304 rc = -EINVAL;
1305 if (sk)
1306 rc = sock_get_timestampns(sk,
1307 (struct timespec __user *)argp);
1308 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 case SIOCGIFADDR:
1310 case SIOCSIFADDR:
1311 case SIOCGIFDSTADDR:
1312 case SIOCSIFDSTADDR:
1313 case SIOCGIFBRDADDR:
1314 case SIOCSIFBRDADDR:
1315 case SIOCGIFNETMASK:
1316 case SIOCSIFNETMASK:
1317 case SIOCGIFMETRIC:
1318 case SIOCSIFMETRIC:
1319 rc = -EINVAL;
1320 break;
1321 case SIOCADDRT:
1322 case SIOCDELRT:
1323 rc = -EPERM;
1324 if (!capable(CAP_NET_ADMIN))
1325 break;
1326 rc = x25_route_ioctl(cmd, argp);
1327 break;
1328 case SIOCX25GSUBSCRIP:
1329 rc = x25_subscr_ioctl(cmd, argp);
1330 break;
1331 case SIOCX25SSUBSCRIP:
1332 rc = -EPERM;
1333 if (!capable(CAP_NET_ADMIN))
1334 break;
1335 rc = x25_subscr_ioctl(cmd, argp);
1336 break;
1337 case SIOCX25GFACILITIES: {
1338 struct x25_facilities fac = x25->facilities;
1339 rc = copy_to_user(argp, &fac,
1340 sizeof(fac)) ? -EFAULT : 0;
1341 break;
1342 }
1343
1344 case SIOCX25SFACILITIES: {
1345 struct x25_facilities facilities;
1346 rc = -EFAULT;
1347 if (copy_from_user(&facilities, argp,
1348 sizeof(facilities)))
1349 break;
1350 rc = -EINVAL;
1351 if (sk->sk_state != TCP_LISTEN &&
1352 sk->sk_state != TCP_CLOSE)
1353 break;
1354 if (facilities.pacsize_in < X25_PS16 ||
1355 facilities.pacsize_in > X25_PS4096)
1356 break;
1357 if (facilities.pacsize_out < X25_PS16 ||
1358 facilities.pacsize_out > X25_PS4096)
1359 break;
1360 if (facilities.winsize_in < 1 ||
1361 facilities.winsize_in > 127)
1362 break;
1363 if (facilities.throughput < 0x03 ||
1364 facilities.throughput > 0xDD)
1365 break;
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001366 if (facilities.reverse &&
roel kluin06a96b32009-10-07 00:59:42 +00001367 (facilities.reverse & 0x81) != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
1369 x25->facilities = facilities;
1370 rc = 0;
1371 break;
1372 }
1373
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001374 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001375 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001376 sizeof(x25->dte_facilities));
1377 if (rc)
1378 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001379 break;
1380 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001381
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001382 case SIOCX25SDTEFACILITIES: {
1383 struct x25_dte_facilities dtefacs;
1384 rc = -EFAULT;
1385 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001386 break;
1387 rc = -EINVAL;
1388 if (sk->sk_state != TCP_LISTEN &&
1389 sk->sk_state != TCP_CLOSE)
1390 break;
1391 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1392 break;
1393 if (dtefacs.calling_ae == NULL)
1394 break;
1395 if (dtefacs.called_len > X25_MAX_AE_LEN)
1396 break;
1397 if (dtefacs.called_ae == NULL)
1398 break;
1399 x25->dte_facilities = dtefacs;
1400 rc = 0;
1401 break;
1402 }
1403
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 case SIOCX25GCALLUSERDATA: {
1405 struct x25_calluserdata cud = x25->calluserdata;
1406 rc = copy_to_user(argp, &cud,
1407 sizeof(cud)) ? -EFAULT : 0;
1408 break;
1409 }
1410
1411 case SIOCX25SCALLUSERDATA: {
1412 struct x25_calluserdata calluserdata;
1413
1414 rc = -EFAULT;
1415 if (copy_from_user(&calluserdata, argp,
1416 sizeof(calluserdata)))
1417 break;
1418 rc = -EINVAL;
1419 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1420 break;
1421 x25->calluserdata = calluserdata;
1422 rc = 0;
1423 break;
1424 }
1425
1426 case SIOCX25GCAUSEDIAG: {
1427 struct x25_causediag causediag;
1428 causediag = x25->causediag;
1429 rc = copy_to_user(argp, &causediag,
1430 sizeof(causediag)) ? -EFAULT : 0;
1431 break;
1432 }
1433
Shaun Pereiracb65d502005-06-22 22:15:01 -07001434 case SIOCX25SCUDMATCHLEN: {
1435 struct x25_subaddr sub_addr;
1436 rc = -EINVAL;
1437 if(sk->sk_state != TCP_CLOSE)
1438 break;
1439 rc = -EFAULT;
1440 if (copy_from_user(&sub_addr, argp,
1441 sizeof(sub_addr)))
1442 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001443 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001444 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1445 break;
1446 x25->cudmatchlength = sub_addr.cudmatchlength;
1447 rc = 0;
1448 break;
1449 }
1450
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001451 case SIOCX25CALLACCPTAPPRV: {
1452 rc = -EINVAL;
1453 if (sk->sk_state != TCP_CLOSE)
1454 break;
1455 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1456 rc = 0;
1457 break;
1458 }
1459
1460 case SIOCX25SENDCALLACCPT: {
1461 rc = -EINVAL;
1462 if (sk->sk_state != TCP_ESTABLISHED)
1463 break;
1464 if (x25->accptapprv) /* must call accptapprv above */
1465 break;
1466 x25_write_internal(sk, X25_CALL_ACCEPTED);
1467 x25->state = X25_STATE_3;
1468 rc = 0;
1469 break;
1470 }
1471
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001472 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001473 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break;
1475 }
1476
1477 return rc;
1478}
1479
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001480static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 .family = AF_X25,
1482 .create = x25_create,
1483 .owner = THIS_MODULE,
1484};
1485
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001486#ifdef CONFIG_COMPAT
1487static int compat_x25_subscr_ioctl(unsigned int cmd,
1488 struct compat_x25_subscrip_struct __user *x25_subscr32)
1489{
1490 struct compat_x25_subscrip_struct x25_subscr;
1491 struct x25_neigh *nb;
1492 struct net_device *dev;
1493 int rc = -EINVAL;
1494
1495 rc = -EFAULT;
1496 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1497 goto out;
1498
1499 rc = -EINVAL;
1500 dev = x25_dev_get(x25_subscr.device);
1501 if (dev == NULL)
1502 goto out;
1503
1504 nb = x25_get_neigh(dev);
1505 if (nb == NULL)
1506 goto out_dev_put;
1507
1508 dev_put(dev);
1509
1510 if (cmd == SIOCX25GSUBSCRIP) {
1511 x25_subscr.extended = nb->extended;
1512 x25_subscr.global_facil_mask = nb->global_facil_mask;
1513 rc = copy_to_user(x25_subscr32, &x25_subscr,
1514 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1515 } else {
1516 rc = -EINVAL;
1517 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1518 rc = 0;
1519 nb->extended = x25_subscr.extended;
1520 nb->global_facil_mask = x25_subscr.global_facil_mask;
1521 }
1522 }
1523 x25_neigh_put(nb);
1524out:
1525 return rc;
1526out_dev_put:
1527 dev_put(dev);
1528 goto out;
1529}
1530
1531static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1532 unsigned long arg)
1533{
1534 void __user *argp = compat_ptr(arg);
1535 struct sock *sk = sock->sk;
1536
1537 int rc = -ENOIOCTLCMD;
1538
1539 switch(cmd) {
1540 case TIOCOUTQ:
1541 case TIOCINQ:
1542 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1543 break;
1544 case SIOCGSTAMP:
1545 rc = -EINVAL;
1546 if (sk)
1547 rc = compat_sock_get_timestamp(sk,
1548 (struct timeval __user*)argp);
1549 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001550 case SIOCGSTAMPNS:
1551 rc = -EINVAL;
1552 if (sk)
1553 rc = compat_sock_get_timestampns(sk,
1554 (struct timespec __user*)argp);
1555 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001556 case SIOCGIFADDR:
1557 case SIOCSIFADDR:
1558 case SIOCGIFDSTADDR:
1559 case SIOCSIFDSTADDR:
1560 case SIOCGIFBRDADDR:
1561 case SIOCSIFBRDADDR:
1562 case SIOCGIFNETMASK:
1563 case SIOCSIFNETMASK:
1564 case SIOCGIFMETRIC:
1565 case SIOCSIFMETRIC:
1566 rc = -EINVAL;
1567 break;
1568 case SIOCADDRT:
1569 case SIOCDELRT:
1570 rc = -EPERM;
1571 if (!capable(CAP_NET_ADMIN))
1572 break;
1573 rc = x25_route_ioctl(cmd, argp);
1574 break;
1575 case SIOCX25GSUBSCRIP:
1576 rc = compat_x25_subscr_ioctl(cmd, argp);
1577 break;
1578 case SIOCX25SSUBSCRIP:
1579 rc = -EPERM;
1580 if (!capable(CAP_NET_ADMIN))
1581 break;
1582 rc = compat_x25_subscr_ioctl(cmd, argp);
1583 break;
1584 case SIOCX25GFACILITIES:
1585 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001586 case SIOCX25GDTEFACILITIES:
1587 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001588 case SIOCX25GCALLUSERDATA:
1589 case SIOCX25SCALLUSERDATA:
1590 case SIOCX25GCAUSEDIAG:
1591 case SIOCX25SCUDMATCHLEN:
1592 case SIOCX25CALLACCPTAPPRV:
1593 case SIOCX25SENDCALLACCPT:
1594 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1595 break;
1596 default:
1597 rc = -ENOIOCTLCMD;
1598 break;
1599 }
1600 return rc;
1601}
1602#endif
1603
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001604static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .family = AF_X25,
1606 .owner = THIS_MODULE,
1607 .release = x25_release,
1608 .bind = x25_bind,
1609 .connect = x25_connect,
1610 .socketpair = sock_no_socketpair,
1611 .accept = x25_accept,
1612 .getname = x25_getname,
1613 .poll = datagram_poll,
1614 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001615#ifdef CONFIG_COMPAT
1616 .compat_ioctl = compat_x25_ioctl,
1617#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 .listen = x25_listen,
1619 .shutdown = sock_no_shutdown,
1620 .setsockopt = x25_setsockopt,
1621 .getsockopt = x25_getsockopt,
1622 .sendmsg = x25_sendmsg,
1623 .recvmsg = x25_recvmsg,
1624 .mmap = sock_no_mmap,
1625 .sendpage = sock_no_sendpage,
1626};
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628SOCKOPS_WRAP(x25_proto, AF_X25);
1629
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001630static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001631 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 .func = x25_lapb_receive_frame,
1633};
1634
1635static struct notifier_block x25_dev_notifier = {
1636 .notifier_call = x25_device_event,
1637};
1638
1639void x25_kill_by_neigh(struct x25_neigh *nb)
1640{
1641 struct sock *s;
1642 struct hlist_node *node;
1643
1644 write_lock_bh(&x25_list_lock);
1645
1646 sk_for_each(s, node, &x25_list)
1647 if (x25_sk(s)->neighbour == nb)
1648 x25_disconnect(s, ENETUNREACH, 0, 0);
1649
1650 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001651
1652 /* Remove any related forwards */
1653 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656static int __init x25_init(void)
1657{
1658 int rc = proto_register(&x25_proto, 0);
1659
1660 if (rc != 0)
1661 goto out;
1662
1663 sock_register(&x25_family_ops);
1664
1665 dev_add_pack(&x25_packet_type);
1666
1667 register_netdevice_notifier(&x25_dev_notifier);
1668
maximilian attemsa44562e2008-01-28 20:43:16 -08001669 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671#ifdef CONFIG_SYSCTL
1672 x25_register_sysctl();
1673#endif
1674 x25_proc_init();
1675out:
1676 return rc;
1677}
1678module_init(x25_init);
1679
1680static void __exit x25_exit(void)
1681{
1682 x25_proc_exit();
1683 x25_link_free();
1684 x25_route_free();
1685
1686#ifdef CONFIG_SYSCTL
1687 x25_unregister_sysctl();
1688#endif
1689
1690 unregister_netdevice_notifier(&x25_dev_notifier);
1691
1692 dev_remove_pack(&x25_packet_type);
1693
1694 sock_unregister(AF_X25);
1695 proto_unregister(&x25_proto);
1696}
1697module_exit(x25_exit);
1698
1699MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1700MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1701MODULE_LICENSE("GPL");
1702MODULE_ALIAS_NETPROTO(PF_X25);