blob: 32bfb21d7baf85df78c9e3b427a3540c5ab69b36 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#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>
andrew hendrya9288522010-02-14 02:00:45 +000058#include <linux/ctype.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080061#include <net/compat.h>
Sachin Sharma20d658e2014-02-18 00:16:23 -080062#ifdef KW_TAINT_ANALYSIS
63 extern void * get_tainted_stuff();
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
66int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
67int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
68int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
69int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080070int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72HLIST_HEAD(x25_list);
73DEFINE_RWLOCK(x25_list_lock);
74
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080075static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static struct x25_address null_x25_address = {" "};
78
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080079#ifdef CONFIG_COMPAT
80struct compat_x25_subscrip_struct {
81 char device[200-sizeof(compat_ulong_t)];
82 compat_ulong_t global_facil_mask;
83 compat_uint_t extended;
84};
85#endif
86
John Hughesf5eb9172010-04-07 21:29:25 -070087
88int x25_parse_address_block(struct sk_buff *skb,
89 struct x25_address *called_addr,
90 struct x25_address *calling_addr)
91{
92 unsigned char len;
93 int needed;
94 int rc;
95
Matthew Daleycb101ed2011-10-14 18:45:04 +000096 if (!pskb_may_pull(skb, 1)) {
John Hughesf5eb9172010-04-07 21:29:25 -070097 /* packet has no address block */
98 rc = 0;
99 goto empty;
100 }
101
102 len = *skb->data;
103 needed = 1 + (len >> 4) + (len & 0x0f);
104
Matthew Daleycb101ed2011-10-14 18:45:04 +0000105 if (!pskb_may_pull(skb, needed)) {
John Hughesf5eb9172010-04-07 21:29:25 -0700106 /* packet is too short to hold the addresses it claims
107 to hold */
108 rc = -1;
109 goto empty;
110 }
111
112 return x25_addr_ntoa(skb->data, called_addr, calling_addr);
113
114empty:
115 *called_addr->x25_addr = 0;
116 *calling_addr->x25_addr = 0;
117
118 return rc;
119}
120
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
123 struct x25_address *calling_addr)
124{
Eric Dumazet6bf15742008-01-13 22:27:52 -0800125 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -0800127 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 called_len = (*p >> 0) & 0x0F;
130 calling_len = (*p >> 4) & 0x0F;
131
132 called = called_addr->x25_addr;
133 calling = calling_addr->x25_addr;
134 p++;
135
136 for (i = 0; i < (called_len + calling_len); i++) {
137 if (i < called_len) {
138 if (i % 2 != 0) {
139 *called++ = ((*p >> 0) & 0x0F) + '0';
140 p++;
141 } else {
142 *called++ = ((*p >> 4) & 0x0F) + '0';
143 }
144 } else {
145 if (i % 2 != 0) {
146 *calling++ = ((*p >> 0) & 0x0F) + '0';
147 p++;
148 } else {
149 *calling++ = ((*p >> 4) & 0x0F) + '0';
150 }
151 }
152 }
153
154 *called = *calling = '\0';
155
156 return 1 + (called_len + calling_len + 1) / 2;
157}
158
159int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
160 struct x25_address *calling_addr)
161{
162 unsigned int called_len, calling_len;
163 char *called, *calling;
164 int i;
165
166 called = called_addr->x25_addr;
167 calling = calling_addr->x25_addr;
168
169 called_len = strlen(called);
170 calling_len = strlen(calling);
171
172 *p++ = (calling_len << 4) | (called_len << 0);
173
174 for (i = 0; i < (called_len + calling_len); i++) {
175 if (i < called_len) {
176 if (i % 2 != 0) {
177 *p |= (*called++ - '0') << 0;
178 p++;
179 } else {
180 *p = 0x00;
181 *p |= (*called++ - '0') << 4;
182 }
183 } else {
184 if (i % 2 != 0) {
185 *p |= (*calling++ - '0') << 0;
186 p++;
187 } else {
188 *p = 0x00;
189 *p |= (*calling++ - '0') << 4;
190 }
191 }
192 }
193
194 return 1 + (called_len + calling_len + 1) / 2;
195}
196
197/*
198 * Socket removal during an interrupt is now safe.
199 */
200static void x25_remove_socket(struct sock *sk)
201{
202 write_lock_bh(&x25_list_lock);
203 sk_del_node_init(sk);
204 write_unlock_bh(&x25_list_lock);
205}
206
207/*
208 * Kill all bound sockets on a dropped device.
209 */
210static void x25_kill_by_device(struct net_device *dev)
211{
212 struct sock *s;
213 struct hlist_node *node;
214
215 write_lock_bh(&x25_list_lock);
216
217 sk_for_each(s, node, &x25_list)
218 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
219 x25_disconnect(s, ENETUNREACH, 0, 0);
220
221 write_unlock_bh(&x25_list_lock);
222}
223
224/*
225 * Handle device status changes.
226 */
227static int x25_device_event(struct notifier_block *this, unsigned long event,
228 void *ptr)
229{
230 struct net_device *dev = ptr;
231 struct x25_neigh *nb;
232
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700233 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200234 return NOTIFY_DONE;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (dev->type == ARPHRD_X25
Igor Maravić29c36262011-12-12 02:58:23 +0000237#if IS_ENABLED(CONFIG_LLC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 || dev->type == ARPHRD_ETHER
239#endif
240 ) {
241 switch (event) {
Joe Perchesfddc5f32011-07-01 09:43:13 +0000242 case NETDEV_UP:
243 x25_link_device_up(dev);
244 break;
245 case NETDEV_GOING_DOWN:
246 nb = x25_get_neigh(dev);
247 if (nb) {
248 x25_terminate_link(nb);
249 x25_neigh_put(nb);
250 }
251 break;
252 case NETDEV_DOWN:
253 x25_kill_by_device(dev);
254 x25_route_device_down(dev);
255 x25_link_device_down(dev);
256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258 }
259
260 return NOTIFY_DONE;
261}
262
263/*
264 * Add a socket to the bound sockets list.
265 */
266static void x25_insert_socket(struct sock *sk)
267{
268 write_lock_bh(&x25_list_lock);
269 sk_add_node(sk, &x25_list);
270 write_unlock_bh(&x25_list_lock);
271}
272
273/*
274 * Find a socket that wants to accept the Call Request we just
275 * received. Check the full list for an address/cud match.
276 * If no cuds match return the next_best thing, an address match.
277 * Note: if a listening socket has cud set it must only get calls
278 * with matching cud.
279 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700280static struct sock *x25_find_listener(struct x25_address *addr,
281 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct sock *s;
284 struct sock *next_best;
285 struct hlist_node *node;
286
287 read_lock_bh(&x25_list_lock);
288 next_best = NULL;
289
290 sk_for_each(s, node, &x25_list)
291 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700292 x25_sk(s)->source_addr.x25_addr) ||
293 !strcmp(addr->x25_addr,
294 null_x25_address.x25_addr)) &&
295 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /*
297 * Found a listening socket, now check the incoming
298 * call user data vs this sockets call user data
299 */
Matthew Daley7f81e252011-10-14 18:45:05 +0000300 if (x25_sk(s)->cudmatchlength > 0 &&
301 skb->len >= x25_sk(s)->cudmatchlength) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900302 if((memcmp(x25_sk(s)->calluserdata.cuddata,
303 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700304 x25_sk(s)->cudmatchlength)) == 0) {
305 sock_hold(s);
306 goto found;
307 }
308 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 }
311 if (next_best) {
312 s = next_best;
313 sock_hold(s);
314 goto found;
315 }
316 s = NULL;
317found:
318 read_unlock_bh(&x25_list_lock);
319 return s;
320}
321
322/*
323 * Find a connected X.25 socket given my LCI and neighbour.
324 */
325static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
326{
327 struct sock *s;
328 struct hlist_node *node;
329
330 sk_for_each(s, node, &x25_list)
331 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
332 sock_hold(s);
333 goto found;
334 }
335 s = NULL;
336found:
337 return s;
338}
339
340struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
341{
342 struct sock *s;
343
344 read_lock_bh(&x25_list_lock);
345 s = __x25_find_socket(lci, nb);
346 read_unlock_bh(&x25_list_lock);
347 return s;
348}
349
350/*
351 * Find a unique LCI for a given device.
352 */
353static unsigned int x25_new_lci(struct x25_neigh *nb)
354{
355 unsigned int lci = 1;
356 struct sock *sk;
357
358 read_lock_bh(&x25_list_lock);
359
360 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
361 sock_put(sk);
362 if (++lci == 4096) {
363 lci = 0;
364 break;
365 }
366 }
367
368 read_unlock_bh(&x25_list_lock);
369 return lci;
370}
371
372/*
373 * Deferred destroy.
374 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700375static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377/*
378 * handler for deferred kills.
379 */
380static void x25_destroy_timer(unsigned long data)
381{
David S. Miller14ebaf82009-06-16 05:40:30 -0700382 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385/*
386 * This is called from user mode and the timers. Thus it protects itself
387 * against interrupt users but doesn't worry about being called during
388 * work. Once it is removed from the queue no interrupt or bottom half
389 * will touch it and we are (fairly 8-) ) safe.
390 * Not static as it's used by the timer
391 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700392static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 struct sk_buff *skb;
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 x25_stop_heartbeat(sk);
397 x25_stop_timer(sk);
398
399 x25_remove_socket(sk);
400 x25_clear_queues(sk); /* Flush the queues */
401
402 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
403 if (skb->sk != sk) { /* A pending connection */
404 /*
405 * Queue the unaccepted socket for death
406 */
andrew hendry2cec6b02010-04-17 14:17:32 +0000407 skb->sk->sk_state = TCP_LISTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sock_set_flag(skb->sk, SOCK_DEAD);
409 x25_start_heartbeat(skb->sk);
410 x25_sk(skb->sk)->state = X25_STATE_0;
411 }
412
413 kfree_skb(skb);
414 }
415
Eric Dumazetc5640392009-06-16 10:12:03 +0000416 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /* Defer: outstanding buffers */
418 sk->sk_timer.expires = jiffies + 10 * HZ;
419 sk->sk_timer.function = x25_destroy_timer;
420 sk->sk_timer.data = (unsigned long)sk;
421 add_timer(&sk->sk_timer);
422 } else {
423 /* drop last reference so sock_put will free */
424 __sock_put(sk);
425 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700426}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
David S. Miller14ebaf82009-06-16 05:40:30 -0700428void x25_destroy_socket_from_timer(struct sock *sk)
429{
430 sock_hold(sk);
431 bh_lock_sock(sk);
432 __x25_destroy_socket(sk);
433 bh_unlock_sock(sk);
434 sock_put(sk);
435}
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/*
438 * Handling for system calls applied via the various interfaces to a
439 * X.25 socket object.
440 */
441
442static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700443 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 int opt;
446 struct sock *sk = sock->sk;
447 int rc = -ENOPROTOOPT;
448
449 if (level != SOL_X25 || optname != X25_QBITINCL)
450 goto out;
451
452 rc = -EINVAL;
453 if (optlen < sizeof(int))
454 goto out;
455
456 rc = -EFAULT;
457 if (get_user(opt, (int __user *)optval))
458 goto out;
459
andrew hendrycb863ff2010-05-16 22:59:41 +0000460 if (opt)
461 set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
462 else
463 clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 rc = 0;
465out:
466 return rc;
467}
468
469static int x25_getsockopt(struct socket *sock, int level, int optname,
470 char __user *optval, int __user *optlen)
471{
472 struct sock *sk = sock->sk;
473 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (level != SOL_X25 || optname != X25_QBITINCL)
476 goto out;
477
478 rc = -EFAULT;
479 if (get_user(len, optlen))
480 goto out;
481
482 len = min_t(unsigned int, len, sizeof(int));
483
484 rc = -EINVAL;
485 if (len < 0)
486 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rc = -EFAULT;
489 if (put_user(len, optlen))
490 goto out;
491
andrew hendrycb863ff2010-05-16 22:59:41 +0000492 val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
494out:
495 return rc;
496}
497
498static int x25_listen(struct socket *sock, int backlog)
499{
500 struct sock *sk = sock->sk;
501 int rc = -EOPNOTSUPP;
502
andrew hendry25aa4ef2010-09-14 13:31:16 +0000503 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (sk->sk_state != TCP_LISTEN) {
505 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
506 sk->sk_max_ack_backlog = backlog;
507 sk->sk_state = TCP_LISTEN;
508 rc = 0;
509 }
andrew hendry25aa4ef2010-09-14 13:31:16 +0000510 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 return rc;
513}
514
515static struct proto x25_proto = {
516 .name = "X25",
517 .owner = THIS_MODULE,
518 .obj_size = sizeof(struct x25_sock),
519};
520
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700521static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700524 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 if (!sk)
527 goto out;
528
529 sock_init_data(NULL, sk);
530
531 x25 = x25_sk(sk);
532 skb_queue_head_init(&x25->ack_queue);
533 skb_queue_head_init(&x25->fragment_queue);
534 skb_queue_head_init(&x25->interrupt_in_queue);
535 skb_queue_head_init(&x25->interrupt_out_queue);
536out:
537 return sk;
538}
539
Eric Paris3f378b62009-11-05 22:18:14 -0800540static int x25_create(struct net *net, struct socket *sock, int protocol,
541 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct sock *sk;
544 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000545 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800547 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto out;
549
andrew hendryb18e7a02010-02-14 02:00:11 +0000550 rc = -ESOCKTNOSUPPORT;
551 if (sock->type != SOCK_SEQPACKET)
552 goto out;
553
554 rc = -EINVAL;
555 if (protocol)
556 goto out;
557
558 rc = -ENOBUFS;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700559 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto out;
561
562 x25 = x25_sk(sk);
563
564 sock_init_data(sock, sk);
565
566 x25_init_timers(sk);
567
568 sock->ops = &x25_proto_ops;
569 sk->sk_protocol = protocol;
570 sk->sk_backlog_rcv = x25_backlog_rcv;
571
572 x25->t21 = sysctl_x25_call_request_timeout;
573 x25->t22 = sysctl_x25_reset_request_timeout;
574 x25->t23 = sysctl_x25_clear_request_timeout;
575 x25->t2 = sysctl_x25_ack_holdback_timeout;
576 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700577 x25->cudmatchlength = 0;
andrew hendry37cda782010-05-16 23:00:27 +0000578 set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); /* normally no cud */
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700579 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
582 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
583 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
584 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
John Hughesddd04512010-04-04 06:48:10 +0000585 x25->facilities.throughput = 0; /* by default don't negotiate
586 throughput */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900588 x25->dte_facilities.calling_len = 0;
589 x25->dte_facilities.called_len = 0;
590 memset(x25->dte_facilities.called_ae, '\0',
591 sizeof(x25->dte_facilities.called_ae));
592 memset(x25->dte_facilities.calling_ae, '\0',
593 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 rc = 0;
596out:
597 return rc;
598}
599
600static struct sock *x25_make_new(struct sock *osk)
601{
602 struct sock *sk = NULL;
603 struct x25_sock *x25, *ox25;
604
605 if (osk->sk_type != SOCK_SEQPACKET)
606 goto out;
607
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900608 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 goto out;
610
611 x25 = x25_sk(sk);
612
613 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sk->sk_priority = osk->sk_priority;
615 sk->sk_protocol = osk->sk_protocol;
616 sk->sk_rcvbuf = osk->sk_rcvbuf;
617 sk->sk_sndbuf = osk->sk_sndbuf;
618 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800620 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 ox25 = x25_sk(osk);
623 x25->t21 = ox25->t21;
624 x25->t22 = ox25->t22;
625 x25->t23 = ox25->t23;
626 x25->t2 = ox25->t2;
andrew hendrycb863ff2010-05-16 22:59:41 +0000627 x25->flags = ox25->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 x25->facilities = ox25->facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800629 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700630 x25->cudmatchlength = ox25->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
andrew hendryb7792e32010-05-16 23:00:02 +0000632 clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 x25_init_timers(sk);
634out:
635 return sk;
636}
637
638static int x25_release(struct socket *sock)
639{
640 struct sock *sk = sock->sk;
641 struct x25_sock *x25;
642
643 if (!sk)
Arnd Bergmann77b22832011-01-22 23:44:59 +0100644 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 x25 = x25_sk(sk);
647
Arnd Bergmann77b22832011-01-22 23:44:59 +0100648 sock_hold(sk);
649 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 switch (x25->state) {
651
652 case X25_STATE_0:
653 case X25_STATE_2:
654 x25_disconnect(sk, 0, 0, 0);
Arnd Bergmann77b22832011-01-22 23:44:59 +0100655 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 goto out;
657
658 case X25_STATE_1:
659 case X25_STATE_3:
660 case X25_STATE_4:
661 x25_clear_queues(sk);
662 x25_write_internal(sk, X25_CLEAR_REQUEST);
663 x25_start_t23timer(sk);
664 x25->state = X25_STATE_2;
665 sk->sk_state = TCP_CLOSE;
666 sk->sk_shutdown |= SEND_SHUTDOWN;
667 sk->sk_state_change(sk);
668 sock_set_flag(sk, SOCK_DEAD);
669 sock_set_flag(sk, SOCK_DESTROY);
670 break;
671 }
672
David S. Millerc751e4f2008-06-17 03:05:13 -0700673 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674out:
Arnd Bergmann77b22832011-01-22 23:44:59 +0100675 release_sock(sk);
676 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678}
679
680static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
681{
682 struct sock *sk = sock->sk;
683 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
andrew hendrya9288522010-02-14 02:00:45 +0000684 int len, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 if (!sock_flag(sk, SOCK_ZAPPED) ||
687 addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000688 addr->sx25_family != AF_X25) {
689 rc = -EINVAL;
690 goto out;
691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
andrew hendrya9288522010-02-14 02:00:45 +0000693 len = strlen(addr->sx25_addr.x25_addr);
694 for (i = 0; i < len; i++) {
695 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
696 rc = -EINVAL;
697 goto out;
698 }
699 }
700
andrew hendry90c27292010-09-14 13:31:38 +0000701 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 x25_sk(sk)->source_addr = addr->sx25_addr;
703 x25_insert_socket(sk);
704 sock_reset_flag(sk, SOCK_ZAPPED);
andrew hendry90c27292010-09-14 13:31:38 +0000705 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000707out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000708 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
711static int x25_wait_for_connection_establishment(struct sock *sk)
712{
713 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900714 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Eric Dumazetaa395142010-04-20 13:03:51 +0000716 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 for (;;) {
718 __set_current_state(TASK_INTERRUPTIBLE);
719 rc = -ERESTARTSYS;
720 if (signal_pending(current))
721 break;
722 rc = sock_error(sk);
723 if (rc) {
724 sk->sk_socket->state = SS_UNCONNECTED;
725 break;
726 }
727 rc = 0;
728 if (sk->sk_state != TCP_ESTABLISHED) {
729 release_sock(sk);
730 schedule();
731 lock_sock(sk);
732 } else
733 break;
734 }
735 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000736 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return rc;
738}
739
740static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
741 int addr_len, int flags)
742{
743 struct sock *sk = sock->sk;
744 struct x25_sock *x25 = x25_sk(sk);
745 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
746 struct x25_route *rt;
747 int rc = 0;
748
749 lock_sock(sk);
750 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
751 sock->state = SS_CONNECTED;
752 goto out; /* Connect completed during a ERESTARTSYS event */
753 }
754
755 rc = -ECONNREFUSED;
756 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
757 sock->state = SS_UNCONNECTED;
758 goto out;
759 }
760
761 rc = -EISCONN; /* No reconnect on a seqpacket socket */
762 if (sk->sk_state == TCP_ESTABLISHED)
763 goto out;
764
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900765 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 sock->state = SS_UNCONNECTED;
767
768 rc = -EINVAL;
769 if (addr_len != sizeof(struct sockaddr_x25) ||
770 addr->sx25_family != AF_X25)
771 goto out;
772
773 rc = -ENETUNREACH;
774 rt = x25_get_route(&addr->sx25_addr);
775 if (!rt)
776 goto out;
777
778 x25->neighbour = x25_get_neigh(rt->dev);
779 if (!x25->neighbour)
780 goto out_put_route;
781
782 x25_limit_facilities(&x25->facilities, x25->neighbour);
783
784 x25->lci = x25_new_lci(x25->neighbour);
785 if (!x25->lci)
786 goto out_put_neigh;
787
788 rc = -EINVAL;
789 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
790 goto out_put_neigh;
791
792 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
793 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
794
795 x25->dest_addr = addr->sx25_addr;
796
797 /* Move to connecting socket, start sending Connect Requests */
798 sock->state = SS_CONNECTING;
799 sk->sk_state = TCP_SYN_SENT;
800
801 x25->state = X25_STATE_1;
802
803 x25_write_internal(sk, X25_CALL_REQUEST);
804
805 x25_start_heartbeat(sk);
806 x25_start_t21timer(sk);
807
808 /* Now the loop */
809 rc = -EINPROGRESS;
810 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
811 goto out_put_neigh;
812
813 rc = x25_wait_for_connection_establishment(sk);
814 if (rc)
815 goto out_put_neigh;
816
817 sock->state = SS_CONNECTED;
818 rc = 0;
819out_put_neigh:
820 if (rc)
821 x25_neigh_put(x25->neighbour);
822out_put_route:
823 x25_route_put(rt);
824out:
825 release_sock(sk);
826 return rc;
827}
828
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800829static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 DECLARE_WAITQUEUE(wait, current);
832 int rc = 0;
833
Eric Dumazetaa395142010-04-20 13:03:51 +0000834 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 for (;;) {
836 __set_current_state(TASK_INTERRUPTIBLE);
837 if (sk->sk_shutdown & RCV_SHUTDOWN)
838 break;
839 rc = -ERESTARTSYS;
840 if (signal_pending(current))
841 break;
842 rc = -EAGAIN;
843 if (!timeout)
844 break;
845 rc = 0;
846 if (skb_queue_empty(&sk->sk_receive_queue)) {
847 release_sock(sk);
848 timeout = schedule_timeout(timeout);
849 lock_sock(sk);
850 } else
851 break;
852 }
853 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000854 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return rc;
856}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
859{
860 struct sock *sk = sock->sk;
861 struct sock *newsk;
862 struct sk_buff *skb;
863 int rc = -EINVAL;
864
Andrew Hendry141646c2010-09-14 20:38:54 -0700865 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 goto out;
867
868 rc = -EOPNOTSUPP;
869 if (sk->sk_type != SOCK_SEQPACKET)
870 goto out;
871
872 lock_sock(sk);
Andrew Hendry141646c2010-09-14 20:38:54 -0700873 rc = -EINVAL;
874 if (sk->sk_state != TCP_LISTEN)
875 goto out2;
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
878 if (rc)
879 goto out2;
880 skb = skb_dequeue(&sk->sk_receive_queue);
881 rc = -EINVAL;
882 if (!skb->sk)
883 goto out2;
884 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700885 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* Now attach up the new socket */
888 skb->sk = NULL;
889 kfree_skb(skb);
890 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 newsock->state = SS_CONNECTED;
892 rc = 0;
893out2:
894 release_sock(sk);
895out:
896 return rc;
897}
898
899static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
900 int *uaddr_len, int peer)
901{
902 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
903 struct sock *sk = sock->sk;
904 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000905 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000908 if (sk->sk_state != TCP_ESTABLISHED) {
909 rc = -ENOTCONN;
910 goto out;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 sx25->sx25_addr = x25->dest_addr;
913 } else
914 sx25->sx25_addr = x25->source_addr;
915
916 sx25->sx25_family = AF_X25;
917 *uaddr_len = sizeof(*sx25);
918
Arnd Bergmann91774902009-11-05 04:37:29 +0000919out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000920 return rc;
921}
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
924 unsigned int lci)
925{
926 struct sock *sk;
927 struct sock *make;
928 struct x25_sock *makex25;
929 struct x25_address source_addr, dest_addr;
930 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800931 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800932 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /*
935 * Remove the LCI and frame type.
936 */
937 skb_pull(skb, X25_STD_MIN_LEN);
938
939 /*
940 * Extract the X.25 addresses and convert them to ASCII strings,
941 * and remove them.
John Hughesf5eb9172010-04-07 21:29:25 -0700942 *
943 * Address block is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
John Hughesf5eb9172010-04-07 21:29:25 -0700945 addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
946 if (addr_len <= 0)
947 goto out_clear_request;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800948 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * Get the length of the facilities, skip past them for the moment
952 * get the call user data because this is needed to determine
953 * the correct listener
John Hughesf5eb9172010-04-07 21:29:25 -0700954 *
955 * Facilities length is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 */
Matthew Daleycb101ed2011-10-14 18:45:04 +0000957 if (!pskb_may_pull(skb, 1))
John Hughesf5eb9172010-04-07 21:29:25 -0700958 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 len = skb->data[0] + 1;
Matthew Daleycb101ed2011-10-14 18:45:04 +0000960 if (!pskb_may_pull(skb, len))
John Hughesf5eb9172010-04-07 21:29:25 -0700961 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 skb_pull(skb,len);
963
964 /*
Matthew Daleyc7fd0d42011-10-14 18:45:03 +0000965 * Ensure that the amount of call user data is valid.
966 */
967 if (skb->len > X25_MAX_CUD_LEN)
968 goto out_clear_request;
969
970 /*
Matthew Daleycb101ed2011-10-14 18:45:04 +0000971 * Get all the call user data so it can be used in
972 * x25_find_listener and skb_copy_from_linear_data up ahead.
973 */
974 if (!pskb_may_pull(skb, skb->len))
975 goto out_clear_request;
976
977 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 * Find a listener for the particular address/cud pair.
979 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700980 sk = x25_find_listener(&source_addr,skb);
981 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800983 if (sk != NULL && sk_acceptq_is_full(sk)) {
984 goto out_sock_put;
985 }
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800988 * We dont have any listeners for this incoming call.
989 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800991 if (sk == NULL) {
992 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800993 if (sysctl_x25_forward &&
994 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800995 {
996 /* Call was forwarded, dont process it any more */
997 kfree_skb(skb);
998 rc = 1;
999 goto out;
1000 } else {
1001 /* No listeners, can't forward, clear the call */
1002 goto out_clear_request;
1003 }
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 /*
1007 * Try to reach a compromise on the requested facilities.
1008 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001009 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1010 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto out_sock_put;
1012
1013 /*
1014 * current neighbour/link might impose additional limits
1015 * on certain facilties
1016 */
1017
1018 x25_limit_facilities(&facilities, nb);
1019
1020 /*
1021 * Try to create a new socket.
1022 */
1023 make = x25_make_new(sk);
1024 if (!make)
1025 goto out_sock_put;
1026
1027 /*
1028 * Remove the facilities
1029 */
1030 skb_pull(skb, len);
1031
1032 skb->sk = make;
1033 make->sk_state = TCP_ESTABLISHED;
1034
1035 makex25 = x25_sk(make);
1036 makex25->lci = lci;
1037 makex25->dest_addr = dest_addr;
1038 makex25->source_addr = source_addr;
1039 makex25->neighbour = nb;
1040 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001041 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001043 /* ensure no reverse facil on accept */
1044 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001045 /* ensure no calling address extension on accept */
1046 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001047 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
andrew hendry37cda782010-05-16 23:00:27 +00001049 /* Normally all calls are accepted immediately */
1050 if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001051 x25_write_internal(make, X25_CALL_ACCEPTED);
1052 makex25->state = X25_STATE_3;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Shaun Pereiracb65d502005-06-22 22:15:01 -07001055 /*
1056 * Incoming Call User Data.
1057 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001058 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1059 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 sk->sk_ack_backlog++;
1062
1063 x25_insert_socket(make);
1064
1065 skb_queue_head(&sk->sk_receive_queue, skb);
1066
1067 x25_start_heartbeat(make);
1068
1069 if (!sock_flag(sk, SOCK_DEAD))
1070 sk->sk_data_ready(sk, skb->len);
1071 rc = 1;
1072 sock_put(sk);
1073out:
1074 return rc;
1075out_sock_put:
1076 sock_put(sk);
1077out_clear_request:
1078 rc = 0;
1079 x25_transmit_clear_request(nb, lci, 0x01);
1080 goto out;
1081}
1082
1083static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1084 struct msghdr *msg, size_t len)
1085{
1086 struct sock *sk = sock->sk;
1087 struct x25_sock *x25 = x25_sk(sk);
1088 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1089 struct sockaddr_x25 sx25;
1090 struct sk_buff *skb;
1091 unsigned char *asmptr;
1092 int noblock = msg->msg_flags & MSG_DONTWAIT;
1093 size_t size;
1094 int qbit = 0, rc = -EINVAL;
1095
Arnd Bergmann77b22832011-01-22 23:44:59 +01001096 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1098 goto out;
1099
1100 /* we currently don't support segmented records at the user interface */
1101 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1102 goto out;
1103
1104 rc = -EADDRNOTAVAIL;
1105 if (sock_flag(sk, SOCK_ZAPPED))
1106 goto out;
1107
1108 rc = -EPIPE;
1109 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1110 send_sig(SIGPIPE, current, 0);
1111 goto out;
1112 }
1113
1114 rc = -ENETUNREACH;
1115 if (!x25->neighbour)
1116 goto out;
1117
1118 if (usx25) {
1119 rc = -EINVAL;
1120 if (msg->msg_namelen < sizeof(sx25))
1121 goto out;
1122 memcpy(&sx25, usx25, sizeof(sx25));
1123 rc = -EISCONN;
1124 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1125 goto out;
1126 rc = -EINVAL;
1127 if (sx25.sx25_family != AF_X25)
1128 goto out;
1129 } else {
1130 /*
1131 * FIXME 1003.1g - if the socket is like this because
1132 * it has become closed (not started closed) we ought
1133 * to SIGPIPE, EPIPE;
1134 */
1135 rc = -ENOTCONN;
1136 if (sk->sk_state != TCP_ESTABLISHED)
1137 goto out;
1138
1139 sx25.sx25_family = AF_X25;
1140 sx25.sx25_addr = x25->dest_addr;
1141 }
1142
Alan Cox83e0bbc2009-03-27 00:28:21 -07001143 /* Sanity check the packet size */
1144 if (len > 65535) {
1145 rc = -EMSGSIZE;
1146 goto out;
1147 }
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1150
1151 /* Build a packet */
1152 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1153
1154 if ((msg->msg_flags & MSG_OOB) && len > 32)
1155 len = 32;
1156
1157 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1158
Arnd Bergmann77b22832011-01-22 23:44:59 +01001159 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
Arnd Bergmann77b22832011-01-22 23:44:59 +01001161 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (!skb)
1163 goto out;
1164 X25_SKB_CB(skb)->flags = msg->msg_flags;
1165
1166 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1167
1168 /*
1169 * Put the data on the end
1170 */
1171 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1172
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001173 skb_reset_transport_header(skb);
1174 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001176 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (rc)
1178 goto out_kfree_skb;
1179
1180 /*
1181 * If the Q BIT Include socket option is in force, the first
1182 * byte of the user data is the logical value of the Q Bit.
1183 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001184 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Matthew Daleycb101ed2011-10-14 18:45:04 +00001185 if (!pskb_may_pull(skb, 1))
1186 goto out_kfree_skb;
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 qbit = skb->data[0];
1189 skb_pull(skb, 1);
1190 }
1191
1192 /*
1193 * Push down the X.25 header
1194 */
1195 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1196
1197 if (msg->msg_flags & MSG_OOB) {
1198 if (x25->neighbour->extended) {
1199 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1200 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1201 *asmptr++ = (x25->lci >> 0) & 0xFF;
1202 *asmptr++ = X25_INTERRUPT;
1203 } else {
1204 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1205 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1206 *asmptr++ = (x25->lci >> 0) & 0xFF;
1207 *asmptr++ = X25_INTERRUPT;
1208 }
1209 } else {
1210 if (x25->neighbour->extended) {
1211 /* Build an Extended X.25 header */
1212 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1213 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1214 *asmptr++ = (x25->lci >> 0) & 0xFF;
1215 *asmptr++ = X25_DATA;
1216 *asmptr++ = X25_DATA;
1217 } else {
1218 /* Build an Standard X.25 header */
1219 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1220 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1221 *asmptr++ = (x25->lci >> 0) & 0xFF;
1222 *asmptr++ = X25_DATA;
1223 }
1224
1225 if (qbit)
1226 skb->data[0] |= X25_Q_BIT;
1227 }
1228
1229 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1230 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1231
1232 rc = -ENOTCONN;
1233 if (sk->sk_state != TCP_ESTABLISHED)
1234 goto out_kfree_skb;
1235
1236 if (msg->msg_flags & MSG_OOB)
1237 skb_queue_tail(&x25->interrupt_out_queue, skb);
1238 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001239 rc = x25_output(sk, skb);
1240 len = rc;
1241 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 kfree_skb(skb);
andrew hendrycb863ff2010-05-16 22:59:41 +00001243 else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 len++;
1245 }
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 x25_kick(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 rc = len;
1249out:
Arnd Bergmann77b22832011-01-22 23:44:59 +01001250 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return rc;
1252out_kfree_skb:
1253 kfree_skb(skb);
1254 goto out;
1255}
1256
1257
1258static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1259 struct msghdr *msg, size_t size,
1260 int flags)
1261{
1262 struct sock *sk = sock->sk;
1263 struct x25_sock *x25 = x25_sk(sk);
1264 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1265 size_t copied;
Dave Jones501e89d2011-11-01 16:26:44 +00001266 int qbit, header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 struct sk_buff *skb;
1268 unsigned char *asmptr;
1269 int rc = -ENOTCONN;
1270
Arnd Bergmann77b22832011-01-22 23:44:59 +01001271 lock_sock(sk);
Dave Jones501e89d2011-11-01 16:26:44 +00001272
1273 if (x25->neighbour == NULL)
1274 goto out;
1275
1276 header_len = x25->neighbour->extended ?
1277 X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * This works for seqpacket too. The receiver has ordered the queue for
1281 * us! We do one quick check first though
1282 */
1283 if (sk->sk_state != TCP_ESTABLISHED)
1284 goto out;
1285
1286 if (flags & MSG_OOB) {
1287 rc = -EINVAL;
1288 if (sock_flag(sk, SOCK_URGINLINE) ||
1289 !skb_peek(&x25->interrupt_in_queue))
1290 goto out;
1291
1292 skb = skb_dequeue(&x25->interrupt_in_queue);
1293
Matthew Daleycb101ed2011-10-14 18:45:04 +00001294 if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1295 goto out_free_dgram;
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 skb_pull(skb, X25_STD_MIN_LEN);
1298
1299 /*
1300 * No Q bit information on Interrupt data.
1301 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001302 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 asmptr = skb_push(skb, 1);
1304 *asmptr = 0x00;
1305 }
1306
1307 msg->msg_flags |= MSG_OOB;
1308 } else {
1309 /* Now we can treat all alike */
Arnd Bergmann77b22832011-01-22 23:44:59 +01001310 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1312 flags & MSG_DONTWAIT, &rc);
Arnd Bergmann77b22832011-01-22 23:44:59 +01001313 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (!skb)
1315 goto out;
1316
Matthew Daleycb101ed2011-10-14 18:45:04 +00001317 if (!pskb_may_pull(skb, header_len))
1318 goto out_free_dgram;
1319
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1321
Matthew Daleycb101ed2011-10-14 18:45:04 +00001322 skb_pull(skb, header_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
andrew hendrycb863ff2010-05-16 22:59:41 +00001324 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 asmptr = skb_push(skb, 1);
1326 *asmptr = qbit;
1327 }
1328 }
1329
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001330 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 copied = skb->len;
1332
1333 if (copied > size) {
1334 copied = size;
1335 msg->msg_flags |= MSG_TRUNC;
1336 }
1337
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001338 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 msg->msg_flags |= MSG_EOR;
1340
1341 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1342 if (rc)
1343 goto out_free_dgram;
1344
1345 if (sx25) {
1346 sx25->sx25_family = AF_X25;
1347 sx25->sx25_addr = x25->dest_addr;
Hannes Frederic Sowaa7ef3862013-11-21 03:14:22 +01001348 msg->msg_namelen = sizeof(*sx25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 x25_check_rbuf(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 rc = copied;
1353out_free_dgram:
1354 skb_free_datagram(sk, skb);
1355out:
Arnd Bergmann77b22832011-01-22 23:44:59 +01001356 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return rc;
1358}
1359
1360
1361static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1362{
1363 struct sock *sk = sock->sk;
1364 struct x25_sock *x25 = x25_sk(sk);
1365 void __user *argp = (void __user *)arg;
1366 int rc;
1367
1368 switch (cmd) {
Joe Perchesfddc5f32011-07-01 09:43:13 +00001369 case TIOCOUTQ: {
1370 int amount;
Eric Dumazet31e6d362009-06-17 19:05:41 -07001371
Joe Perchesfddc5f32011-07-01 09:43:13 +00001372 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1373 if (amount < 0)
1374 amount = 0;
1375 rc = put_user(amount, (unsigned int __user *)argp);
1376 break;
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Joe Perchesfddc5f32011-07-01 09:43:13 +00001379 case TIOCINQ: {
1380 struct sk_buff *skb;
1381 int amount = 0;
1382 /*
1383 * These two are safe on a single CPU system as
1384 * only user tasks fiddle here
1385 */
1386 lock_sock(sk);
1387 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1388 amount = skb->len;
1389 release_sock(sk);
1390 rc = put_user(amount, (unsigned int __user *)argp);
1391 break;
1392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Joe Perchesfddc5f32011-07-01 09:43:13 +00001394 case SIOCGSTAMP:
1395 rc = -EINVAL;
1396 if (sk)
1397 rc = sock_get_timestamp(sk,
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001398 (struct timeval __user *)argp);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001399 break;
1400 case SIOCGSTAMPNS:
1401 rc = -EINVAL;
1402 if (sk)
1403 rc = sock_get_timestampns(sk,
1404 (struct timespec __user *)argp);
1405 break;
1406 case SIOCGIFADDR:
1407 case SIOCSIFADDR:
1408 case SIOCGIFDSTADDR:
1409 case SIOCSIFDSTADDR:
1410 case SIOCGIFBRDADDR:
1411 case SIOCSIFBRDADDR:
1412 case SIOCGIFNETMASK:
1413 case SIOCSIFNETMASK:
1414 case SIOCGIFMETRIC:
1415 case SIOCSIFMETRIC:
1416 rc = -EINVAL;
1417 break;
1418 case SIOCADDRT:
1419 case SIOCDELRT:
1420 rc = -EPERM;
1421 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001423 rc = x25_route_ioctl(cmd, argp);
1424 break;
1425 case SIOCX25GSUBSCRIP:
1426 rc = x25_subscr_ioctl(cmd, argp);
1427 break;
1428 case SIOCX25SSUBSCRIP:
1429 rc = -EPERM;
1430 if (!capable(CAP_NET_ADMIN))
Eric Dumazetae40eb12007-03-18 17:33:16 -07001431 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001432 rc = x25_subscr_ioctl(cmd, argp);
1433 break;
1434 case SIOCX25GFACILITIES: {
1435 lock_sock(sk);
1436 rc = copy_to_user(argp, &x25->facilities,
1437 sizeof(x25->facilities))
1438 ? -EFAULT : 0;
1439 release_sock(sk);
1440 break;
1441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Joe Perchesfddc5f32011-07-01 09:43:13 +00001443 case SIOCX25SFACILITIES: {
1444 struct x25_facilities facilities;
1445 rc = -EFAULT;
1446 if (copy_from_user(&facilities, argp, sizeof(facilities)))
1447 break;
1448 rc = -EINVAL;
1449 lock_sock(sk);
1450 if (sk->sk_state != TCP_LISTEN &&
1451 sk->sk_state != TCP_CLOSE)
1452 goto out_fac_release;
1453 if (facilities.pacsize_in < X25_PS16 ||
1454 facilities.pacsize_in > X25_PS4096)
1455 goto out_fac_release;
1456 if (facilities.pacsize_out < X25_PS16 ||
1457 facilities.pacsize_out > X25_PS4096)
1458 goto out_fac_release;
1459 if (facilities.winsize_in < 1 ||
1460 facilities.winsize_in > 127)
1461 goto out_fac_release;
1462 if (facilities.throughput) {
1463 int out = facilities.throughput & 0xf0;
1464 int in = facilities.throughput & 0x0f;
1465 if (!out)
1466 facilities.throughput |=
1467 X25_DEFAULT_THROUGHPUT << 4;
1468 else if (out < 0x30 || out > 0xD0)
andrew hendryf90de662010-11-25 02:18:35 +00001469 goto out_fac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001470 if (!in)
1471 facilities.throughput |=
1472 X25_DEFAULT_THROUGHPUT;
1473 else if (in < 0x03 || in > 0x0D)
andrew hendryf90de662010-11-25 02:18:35 +00001474 goto out_fac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001475 }
1476 if (facilities.reverse &&
1477 (facilities.reverse & 0x81) != 0x81)
1478 goto out_fac_release;
1479 x25->facilities = facilities;
1480 rc = 0;
andrew hendryf90de662010-11-25 02:18:35 +00001481out_fac_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001482 release_sock(sk);
1483 break;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Joe Perchesfddc5f32011-07-01 09:43:13 +00001486 case SIOCX25GDTEFACILITIES: {
1487 lock_sock(sk);
1488 rc = copy_to_user(argp, &x25->dte_facilities,
1489 sizeof(x25->dte_facilities));
1490 release_sock(sk);
1491 if (rc)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001492 rc = -EFAULT;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001493 break;
1494 }
1495
1496 case SIOCX25SDTEFACILITIES: {
1497 struct x25_dte_facilities dtefacs;
1498 rc = -EFAULT;
1499 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1500 break;
1501 rc = -EINVAL;
1502 lock_sock(sk);
1503 if (sk->sk_state != TCP_LISTEN &&
1504 sk->sk_state != TCP_CLOSE)
1505 goto out_dtefac_release;
1506 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1507 goto out_dtefac_release;
1508 if (dtefacs.calling_ae == NULL)
1509 goto out_dtefac_release;
1510 if (dtefacs.called_len > X25_MAX_AE_LEN)
1511 goto out_dtefac_release;
1512 if (dtefacs.called_ae == NULL)
1513 goto out_dtefac_release;
1514 x25->dte_facilities = dtefacs;
1515 rc = 0;
andrew hendryf90de662010-11-25 02:18:35 +00001516out_dtefac_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001517 release_sock(sk);
1518 break;
1519 }
1520
1521 case SIOCX25GCALLUSERDATA: {
1522 lock_sock(sk);
1523 rc = copy_to_user(argp, &x25->calluserdata,
1524 sizeof(x25->calluserdata))
1525 ? -EFAULT : 0;
1526 release_sock(sk);
1527 break;
1528 }
1529
1530 case SIOCX25SCALLUSERDATA: {
1531 struct x25_calluserdata calluserdata;
1532
1533 rc = -EFAULT;
1534 if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001535 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001536 rc = -EINVAL;
1537 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001539 lock_sock(sk);
1540 x25->calluserdata = calluserdata;
1541 release_sock(sk);
1542 rc = 0;
1543 break;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Joe Perchesfddc5f32011-07-01 09:43:13 +00001546 case SIOCX25GCAUSEDIAG: {
1547 lock_sock(sk);
1548 rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
1549 ? -EFAULT : 0;
1550 release_sock(sk);
1551 break;
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Joe Perchesfddc5f32011-07-01 09:43:13 +00001554 case SIOCX25SCAUSEDIAG: {
1555 struct x25_causediag causediag;
1556 rc = -EFAULT;
1557 if (copy_from_user(&causediag, argp, sizeof(causediag)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001559 lock_sock(sk);
1560 x25->causediag = causediag;
1561 release_sock(sk);
1562 rc = 0;
1563 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
Joe Perchesfddc5f32011-07-01 09:43:13 +00001565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Joe Perchesfddc5f32011-07-01 09:43:13 +00001567 case SIOCX25SCUDMATCHLEN: {
1568 struct x25_subaddr sub_addr;
1569 rc = -EINVAL;
1570 lock_sock(sk);
1571 if(sk->sk_state != TCP_CLOSE)
1572 goto out_cud_release;
1573 rc = -EFAULT;
1574 if (copy_from_user(&sub_addr, argp,
1575 sizeof(sub_addr)))
1576 goto out_cud_release;
1577 rc = -EINVAL;
1578 if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1579 goto out_cud_release;
1580 x25->cudmatchlength = sub_addr.cudmatchlength;
1581 rc = 0;
andrew hendry3f0a0692010-11-25 02:18:45 +00001582out_cud_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001583 release_sock(sk);
1584 break;
1585 }
Shaun Pereiracb65d502005-06-22 22:15:01 -07001586
Joe Perchesfddc5f32011-07-01 09:43:13 +00001587 case SIOCX25CALLACCPTAPPRV: {
1588 rc = -EINVAL;
1589 lock_sock(sk);
Dave Jones35ddeb72013-06-28 12:13:52 -04001590 if (sk->sk_state == TCP_CLOSE) {
1591 clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1592 rc = 0;
1593 }
Joe Perchesfddc5f32011-07-01 09:43:13 +00001594 release_sock(sk);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001595 break;
1596 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001597
Joe Perchesfddc5f32011-07-01 09:43:13 +00001598 case SIOCX25SENDCALLACCPT: {
1599 rc = -EINVAL;
1600 lock_sock(sk);
1601 if (sk->sk_state != TCP_ESTABLISHED)
Dave Jones35ddeb72013-06-28 12:13:52 -04001602 goto out_sendcallaccpt_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001603 /* must call accptapprv above */
1604 if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
Dave Jones35ddeb72013-06-28 12:13:52 -04001605 goto out_sendcallaccpt_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001606 x25_write_internal(sk, X25_CALL_ACCEPTED);
1607 x25->state = X25_STATE_3;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001608 rc = 0;
Dave Jones35ddeb72013-06-28 12:13:52 -04001609out_sendcallaccpt_release:
1610 release_sock(sk);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001611 break;
1612 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001613
Joe Perchesfddc5f32011-07-01 09:43:13 +00001614 default:
1615 rc = -ENOIOCTLCMD;
1616 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
1618
1619 return rc;
1620}
1621
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001622static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 .family = AF_X25,
1624 .create = x25_create,
1625 .owner = THIS_MODULE,
1626};
1627
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001628#ifdef CONFIG_COMPAT
1629static int compat_x25_subscr_ioctl(unsigned int cmd,
Sachin Sharma20d658e2014-02-18 00:16:23 -08001630 struct compat_x25_subscrip_struct __user *x25_subscr32_actual)
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001631{
1632 struct compat_x25_subscrip_struct x25_subscr;
1633 struct x25_neigh *nb;
1634 struct net_device *dev;
1635 int rc = -EINVAL;
1636
1637 rc = -EFAULT;
Sachin Sharma20d658e2014-02-18 00:16:23 -08001638 #ifdef KW_TAINT_ANALYSIS
1639 struct compat_x25_subscrip_struct __user *x25_subscr32 = (struct compat_x25_subscrip_struct __user *)get_tainted_stuff();
1640 #else
1641 struct compat_x25_subscrip_struct __user *x25_subscr32 = x25_subscr32_actual;
1642 #endif
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001643 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1644 goto out;
1645
1646 rc = -EINVAL;
1647 dev = x25_dev_get(x25_subscr.device);
1648 if (dev == NULL)
1649 goto out;
1650
1651 nb = x25_get_neigh(dev);
1652 if (nb == NULL)
1653 goto out_dev_put;
1654
1655 dev_put(dev);
1656
1657 if (cmd == SIOCX25GSUBSCRIP) {
andrew hendry5595a1a2010-11-25 02:18:15 +00001658 read_lock_bh(&x25_neigh_list_lock);
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001659 x25_subscr.extended = nb->extended;
1660 x25_subscr.global_facil_mask = nb->global_facil_mask;
andrew hendry5595a1a2010-11-25 02:18:15 +00001661 read_unlock_bh(&x25_neigh_list_lock);
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001662 rc = copy_to_user(x25_subscr32, &x25_subscr,
1663 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1664 } else {
1665 rc = -EINVAL;
1666 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1667 rc = 0;
andrew hendry5595a1a2010-11-25 02:18:15 +00001668 write_lock_bh(&x25_neigh_list_lock);
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001669 nb->extended = x25_subscr.extended;
1670 nb->global_facil_mask = x25_subscr.global_facil_mask;
andrew hendry5595a1a2010-11-25 02:18:15 +00001671 write_unlock_bh(&x25_neigh_list_lock);
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001672 }
1673 }
1674 x25_neigh_put(nb);
1675out:
1676 return rc;
1677out_dev_put:
1678 dev_put(dev);
1679 goto out;
1680}
1681
1682static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1683 unsigned long arg)
1684{
Sachin Sharma20d658e2014-02-18 00:16:23 -08001685 #ifdef KW_TAINT_ANALYSIS
1686 void __user *argp = (void __user *)get_tainted_stuff();
1687 #else
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001688 void __user *argp = compat_ptr(arg);
Sachin Sharma20d658e2014-02-18 00:16:23 -08001689 #endif
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001690 struct sock *sk = sock->sk;
1691
1692 int rc = -ENOIOCTLCMD;
1693
1694 switch(cmd) {
1695 case TIOCOUTQ:
1696 case TIOCINQ:
1697 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1698 break;
1699 case SIOCGSTAMP:
1700 rc = -EINVAL;
1701 if (sk)
1702 rc = compat_sock_get_timestamp(sk,
1703 (struct timeval __user*)argp);
1704 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001705 case SIOCGSTAMPNS:
1706 rc = -EINVAL;
1707 if (sk)
1708 rc = compat_sock_get_timestampns(sk,
1709 (struct timespec __user*)argp);
1710 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001711 case SIOCGIFADDR:
1712 case SIOCSIFADDR:
1713 case SIOCGIFDSTADDR:
1714 case SIOCSIFDSTADDR:
1715 case SIOCGIFBRDADDR:
1716 case SIOCSIFBRDADDR:
1717 case SIOCGIFNETMASK:
1718 case SIOCSIFNETMASK:
1719 case SIOCGIFMETRIC:
1720 case SIOCSIFMETRIC:
1721 rc = -EINVAL;
1722 break;
1723 case SIOCADDRT:
1724 case SIOCDELRT:
1725 rc = -EPERM;
1726 if (!capable(CAP_NET_ADMIN))
1727 break;
1728 rc = x25_route_ioctl(cmd, argp);
1729 break;
1730 case SIOCX25GSUBSCRIP:
1731 rc = compat_x25_subscr_ioctl(cmd, argp);
1732 break;
1733 case SIOCX25SSUBSCRIP:
1734 rc = -EPERM;
1735 if (!capable(CAP_NET_ADMIN))
1736 break;
1737 rc = compat_x25_subscr_ioctl(cmd, argp);
1738 break;
1739 case SIOCX25GFACILITIES:
1740 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001741 case SIOCX25GDTEFACILITIES:
1742 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001743 case SIOCX25GCALLUSERDATA:
1744 case SIOCX25SCALLUSERDATA:
1745 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001746 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001747 case SIOCX25SCUDMATCHLEN:
1748 case SIOCX25CALLACCPTAPPRV:
1749 case SIOCX25SENDCALLACCPT:
1750 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1751 break;
1752 default:
1753 rc = -ENOIOCTLCMD;
1754 break;
1755 }
1756 return rc;
1757}
1758#endif
1759
Arnd Bergmann91774902009-11-05 04:37:29 +00001760static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 .family = AF_X25,
1762 .owner = THIS_MODULE,
1763 .release = x25_release,
1764 .bind = x25_bind,
1765 .connect = x25_connect,
1766 .socketpair = sock_no_socketpair,
1767 .accept = x25_accept,
1768 .getname = x25_getname,
andrew hendry768190f2010-09-21 15:24:45 +00001769 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001771#ifdef CONFIG_COMPAT
1772 .compat_ioctl = compat_x25_ioctl,
1773#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 .listen = x25_listen,
1775 .shutdown = sock_no_shutdown,
1776 .setsockopt = x25_setsockopt,
1777 .getsockopt = x25_getsockopt,
1778 .sendmsg = x25_sendmsg,
1779 .recvmsg = x25_recvmsg,
1780 .mmap = sock_no_mmap,
1781 .sendpage = sock_no_sendpage,
1782};
1783
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001784static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001785 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 .func = x25_lapb_receive_frame,
1787};
1788
1789static struct notifier_block x25_dev_notifier = {
1790 .notifier_call = x25_device_event,
1791};
1792
1793void x25_kill_by_neigh(struct x25_neigh *nb)
1794{
1795 struct sock *s;
1796 struct hlist_node *node;
1797
1798 write_lock_bh(&x25_list_lock);
1799
1800 sk_for_each(s, node, &x25_list)
1801 if (x25_sk(s)->neighbour == nb)
1802 x25_disconnect(s, ENETUNREACH, 0, 0);
1803
1804 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001805
1806 /* Remove any related forwards */
1807 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808}
1809
1810static int __init x25_init(void)
1811{
1812 int rc = proto_register(&x25_proto, 0);
1813
1814 if (rc != 0)
1815 goto out;
1816
andrew hendry1fd975a2009-11-24 15:15:42 +00001817 rc = sock_register(&x25_family_ops);
1818 if (rc != 0)
1819 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
1821 dev_add_pack(&x25_packet_type);
1822
andrew hendry1fd975a2009-11-24 15:15:42 +00001823 rc = register_netdevice_notifier(&x25_dev_notifier);
1824 if (rc != 0)
1825 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
maximilian attemsa44562e2008-01-28 20:43:16 -08001827 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 x25_register_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001830 rc = x25_proc_init();
1831 if (rc != 0)
1832 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833out:
1834 return rc;
andrew hendry1fd975a2009-11-24 15:15:42 +00001835out_dev:
1836 unregister_netdevice_notifier(&x25_dev_notifier);
1837out_sock:
1838 sock_unregister(AF_X25);
1839out_proto:
1840 proto_unregister(&x25_proto);
1841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843module_init(x25_init);
1844
1845static void __exit x25_exit(void)
1846{
1847 x25_proc_exit();
1848 x25_link_free();
1849 x25_route_free();
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 unregister_netdevice_notifier(&x25_dev_notifier);
1854
1855 dev_remove_pack(&x25_packet_type);
1856
1857 sock_unregister(AF_X25);
1858 proto_unregister(&x25_proto);
1859}
1860module_exit(x25_exit);
1861
1862MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1863MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1864MODULE_LICENSE("GPL");
1865MODULE_ALIAS_NETPROTO(PF_X25);