blob: a8ca79810dcc6c44e42a96eb9b5b1e987838b83e [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
wangweidongb73e9e32013-12-06 19:24:33 +080038#define pr_fmt(fmt) "X25: " fmt
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080041#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/errno.h>
43#include <linux/kernel.h>
44#include <linux/sched.h>
45#include <linux/timer.h>
46#include <linux/string.h>
47#include <linux/net.h>
48#include <linux/netdevice.h>
49#include <linux/if_arp.h>
50#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070053#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <asm/uaccess.h>
55#include <linux/fcntl.h>
56#include <linux/termios.h> /* For TIOCINQ/OUTQ */
57#include <linux/notifier.h>
58#include <linux/init.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080059#include <linux/compat.h>
andrew hendrya9288522010-02-14 02:00:45 +000060#include <linux/ctype.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080063#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65int 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;
Martin Schillerad4c7562018-11-27 09:50:27 +0100103 needed = 1 + ((len >> 4) + (len & 0x0f) + 1) / 2;
John Hughesf5eb9172010-04-07 21:29:25 -0700104
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 write_lock_bh(&x25_list_lock);
215
Sasha Levinb67bfe02013-02-27 17:06:00 -0800216 sk_for_each(s, &x25_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
218 x25_disconnect(s, ENETUNREACH, 0, 0);
219
220 write_unlock_bh(&x25_list_lock);
221}
222
223/*
224 * Handle device status changes.
225 */
226static int x25_device_event(struct notifier_block *this, unsigned long event,
227 void *ptr)
228{
Jiri Pirko351638e2013-05-28 01:30:21 +0000229 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct x25_neigh *nb;
231
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700232 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200233 return NOTIFY_DONE;
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 if (dev->type == ARPHRD_X25
Igor Maravić29c36262011-12-12 02:58:23 +0000236#if IS_ENABLED(CONFIG_LLC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 || dev->type == ARPHRD_ETHER
238#endif
239 ) {
240 switch (event) {
Joe Perchesfddc5f32011-07-01 09:43:13 +0000241 case NETDEV_UP:
242 x25_link_device_up(dev);
243 break;
244 case NETDEV_GOING_DOWN:
245 nb = x25_get_neigh(dev);
246 if (nb) {
247 x25_terminate_link(nb);
248 x25_neigh_put(nb);
249 }
250 break;
251 case NETDEV_DOWN:
252 x25_kill_by_device(dev);
253 x25_route_device_down(dev);
254 x25_link_device_down(dev);
255 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257 }
258
259 return NOTIFY_DONE;
260}
261
262/*
263 * Add a socket to the bound sockets list.
264 */
265static void x25_insert_socket(struct sock *sk)
266{
267 write_lock_bh(&x25_list_lock);
268 sk_add_node(sk, &x25_list);
269 write_unlock_bh(&x25_list_lock);
270}
271
272/*
273 * Find a socket that wants to accept the Call Request we just
274 * received. Check the full list for an address/cud match.
275 * If no cuds match return the next_best thing, an address match.
276 * Note: if a listening socket has cud set it must only get calls
277 * with matching cud.
278 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700279static struct sock *x25_find_listener(struct x25_address *addr,
280 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 struct sock *s;
283 struct sock *next_best;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 read_lock_bh(&x25_list_lock);
286 next_best = NULL;
287
Sasha Levinb67bfe02013-02-27 17:06:00 -0800288 sk_for_each(s, &x25_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700290 x25_sk(s)->source_addr.x25_addr) ||
Martin Schiller3f55d0e2018-11-27 09:50:28 +0100291 !strcmp(x25_sk(s)->source_addr.x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700292 null_x25_address.x25_addr)) &&
293 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 /*
295 * Found a listening socket, now check the incoming
296 * call user data vs this sockets call user data
297 */
Matthew Daley7f81e252011-10-14 18:45:05 +0000298 if (x25_sk(s)->cudmatchlength > 0 &&
299 skb->len >= x25_sk(s)->cudmatchlength) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900300 if((memcmp(x25_sk(s)->calluserdata.cuddata,
301 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700302 x25_sk(s)->cudmatchlength)) == 0) {
303 sock_hold(s);
304 goto found;
305 }
306 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 if (next_best) {
310 s = next_best;
311 sock_hold(s);
312 goto found;
313 }
314 s = NULL;
315found:
316 read_unlock_bh(&x25_list_lock);
317 return s;
318}
319
320/*
321 * Find a connected X.25 socket given my LCI and neighbour.
322 */
323static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
324{
325 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Sasha Levinb67bfe02013-02-27 17:06:00 -0800327 sk_for_each(s, &x25_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
329 sock_hold(s);
330 goto found;
331 }
332 s = NULL;
333found:
334 return s;
335}
336
337struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
338{
339 struct sock *s;
340
341 read_lock_bh(&x25_list_lock);
342 s = __x25_find_socket(lci, nb);
343 read_unlock_bh(&x25_list_lock);
344 return s;
345}
346
347/*
348 * Find a unique LCI for a given device.
349 */
350static unsigned int x25_new_lci(struct x25_neigh *nb)
351{
352 unsigned int lci = 1;
353 struct sock *sk;
354
Eric Dumazet4833df32019-02-08 12:41:05 -0800355 while ((sk = x25_find_socket(lci, nb)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 sock_put(sk);
357 if (++lci == 4096) {
358 lci = 0;
359 break;
360 }
Eric Dumazet4833df32019-02-08 12:41:05 -0800361 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return lci;
365}
366
367/*
368 * Deferred destroy.
369 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700370static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/*
373 * handler for deferred kills.
374 */
375static void x25_destroy_timer(unsigned long data)
376{
David S. Miller14ebaf82009-06-16 05:40:30 -0700377 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
381 * This is called from user mode and the timers. Thus it protects itself
382 * against interrupt users but doesn't worry about being called during
383 * work. Once it is removed from the queue no interrupt or bottom half
384 * will touch it and we are (fairly 8-) ) safe.
385 * Not static as it's used by the timer
386 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700387static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 struct sk_buff *skb;
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 x25_stop_heartbeat(sk);
392 x25_stop_timer(sk);
393
394 x25_remove_socket(sk);
395 x25_clear_queues(sk); /* Flush the queues */
396
397 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
398 if (skb->sk != sk) { /* A pending connection */
399 /*
400 * Queue the unaccepted socket for death
401 */
andrew hendry2cec6b02010-04-17 14:17:32 +0000402 skb->sk->sk_state = TCP_LISTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 sock_set_flag(skb->sk, SOCK_DEAD);
404 x25_start_heartbeat(skb->sk);
405 x25_sk(skb->sk)->state = X25_STATE_0;
406 }
407
408 kfree_skb(skb);
409 }
410
Eric Dumazetc5640392009-06-16 10:12:03 +0000411 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* Defer: outstanding buffers */
413 sk->sk_timer.expires = jiffies + 10 * HZ;
414 sk->sk_timer.function = x25_destroy_timer;
415 sk->sk_timer.data = (unsigned long)sk;
416 add_timer(&sk->sk_timer);
417 } else {
418 /* drop last reference so sock_put will free */
419 __sock_put(sk);
420 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
David S. Miller14ebaf82009-06-16 05:40:30 -0700423void x25_destroy_socket_from_timer(struct sock *sk)
424{
425 sock_hold(sk);
426 bh_lock_sock(sk);
427 __x25_destroy_socket(sk);
428 bh_unlock_sock(sk);
429 sock_put(sk);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
433 * Handling for system calls applied via the various interfaces to a
434 * X.25 socket object.
435 */
436
437static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700438 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 int opt;
441 struct sock *sk = sock->sk;
442 int rc = -ENOPROTOOPT;
443
444 if (level != SOL_X25 || optname != X25_QBITINCL)
445 goto out;
446
447 rc = -EINVAL;
448 if (optlen < sizeof(int))
449 goto out;
450
451 rc = -EFAULT;
452 if (get_user(opt, (int __user *)optval))
453 goto out;
454
andrew hendrycb863ff2010-05-16 22:59:41 +0000455 if (opt)
456 set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
457 else
458 clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 rc = 0;
460out:
461 return rc;
462}
463
464static int x25_getsockopt(struct socket *sock, int level, int optname,
465 char __user *optval, int __user *optlen)
466{
467 struct sock *sk = sock->sk;
468 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (level != SOL_X25 || optname != X25_QBITINCL)
471 goto out;
472
473 rc = -EFAULT;
474 if (get_user(len, optlen))
475 goto out;
476
477 len = min_t(unsigned int, len, sizeof(int));
478
479 rc = -EINVAL;
480 if (len < 0)
481 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 rc = -EFAULT;
484 if (put_user(len, optlen))
485 goto out;
486
andrew hendrycb863ff2010-05-16 22:59:41 +0000487 val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
489out:
490 return rc;
491}
492
493static int x25_listen(struct socket *sock, int backlog)
494{
495 struct sock *sk = sock->sk;
496 int rc = -EOPNOTSUPP;
497
andrew hendry25aa4ef2010-09-14 13:31:16 +0000498 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 if (sk->sk_state != TCP_LISTEN) {
500 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
501 sk->sk_max_ack_backlog = backlog;
502 sk->sk_state = TCP_LISTEN;
503 rc = 0;
504 }
andrew hendry25aa4ef2010-09-14 13:31:16 +0000505 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 return rc;
508}
509
510static struct proto x25_proto = {
511 .name = "X25",
512 .owner = THIS_MODULE,
513 .obj_size = sizeof(struct x25_sock),
514};
515
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500516static struct sock *x25_alloc_socket(struct net *net, int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
518 struct x25_sock *x25;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500519 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 if (!sk)
522 goto out;
523
524 sock_init_data(NULL, sk);
525
526 x25 = x25_sk(sk);
527 skb_queue_head_init(&x25->ack_queue);
528 skb_queue_head_init(&x25->fragment_queue);
529 skb_queue_head_init(&x25->interrupt_in_queue);
530 skb_queue_head_init(&x25->interrupt_out_queue);
531out:
532 return sk;
533}
534
Eric Paris3f378b62009-11-05 22:18:14 -0800535static int x25_create(struct net *net, struct socket *sock, int protocol,
536 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
538 struct sock *sk;
539 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000540 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800542 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto out;
544
andrew hendryb18e7a02010-02-14 02:00:11 +0000545 rc = -ESOCKTNOSUPPORT;
546 if (sock->type != SOCK_SEQPACKET)
547 goto out;
548
549 rc = -EINVAL;
550 if (protocol)
551 goto out;
552
553 rc = -ENOBUFS;
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500554 if ((sk = x25_alloc_socket(net, kern)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out;
556
557 x25 = x25_sk(sk);
558
559 sock_init_data(sock, sk);
560
561 x25_init_timers(sk);
562
563 sock->ops = &x25_proto_ops;
564 sk->sk_protocol = protocol;
565 sk->sk_backlog_rcv = x25_backlog_rcv;
566
567 x25->t21 = sysctl_x25_call_request_timeout;
568 x25->t22 = sysctl_x25_reset_request_timeout;
569 x25->t23 = sysctl_x25_clear_request_timeout;
570 x25->t2 = sysctl_x25_ack_holdback_timeout;
571 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700572 x25->cudmatchlength = 0;
andrew hendry37cda782010-05-16 23:00:27 +0000573 set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); /* normally no cud */
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700574 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
577 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
578 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
579 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
John Hughesddd04512010-04-04 06:48:10 +0000580 x25->facilities.throughput = 0; /* by default don't negotiate
581 throughput */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900583 x25->dte_facilities.calling_len = 0;
584 x25->dte_facilities.called_len = 0;
585 memset(x25->dte_facilities.called_ae, '\0',
586 sizeof(x25->dte_facilities.called_ae));
587 memset(x25->dte_facilities.calling_ae, '\0',
588 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 rc = 0;
591out:
592 return rc;
593}
594
595static struct sock *x25_make_new(struct sock *osk)
596{
597 struct sock *sk = NULL;
598 struct x25_sock *x25, *ox25;
599
600 if (osk->sk_type != SOCK_SEQPACKET)
601 goto out;
602
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500603 if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto out;
605
606 x25 = x25_sk(sk);
607
608 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 sk->sk_priority = osk->sk_priority;
610 sk->sk_protocol = osk->sk_protocol;
611 sk->sk_rcvbuf = osk->sk_rcvbuf;
612 sk->sk_sndbuf = osk->sk_sndbuf;
613 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800615 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 ox25 = x25_sk(osk);
618 x25->t21 = ox25->t21;
619 x25->t22 = ox25->t22;
620 x25->t23 = ox25->t23;
621 x25->t2 = ox25->t2;
andrew hendrycb863ff2010-05-16 22:59:41 +0000622 x25->flags = ox25->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 x25->facilities = ox25->facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800624 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700625 x25->cudmatchlength = ox25->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
andrew hendryb7792e32010-05-16 23:00:02 +0000627 clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 x25_init_timers(sk);
629out:
630 return sk;
631}
632
633static int x25_release(struct socket *sock)
634{
635 struct sock *sk = sock->sk;
636 struct x25_sock *x25;
637
638 if (!sk)
Arnd Bergmann77b22832011-01-22 23:44:59 +0100639 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 x25 = x25_sk(sk);
642
Arnd Bergmann77b22832011-01-22 23:44:59 +0100643 sock_hold(sk);
644 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 switch (x25->state) {
646
647 case X25_STATE_0:
648 case X25_STATE_2:
649 x25_disconnect(sk, 0, 0, 0);
Arnd Bergmann77b22832011-01-22 23:44:59 +0100650 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 goto out;
652
653 case X25_STATE_1:
654 case X25_STATE_3:
655 case X25_STATE_4:
656 x25_clear_queues(sk);
657 x25_write_internal(sk, X25_CLEAR_REQUEST);
658 x25_start_t23timer(sk);
659 x25->state = X25_STATE_2;
660 sk->sk_state = TCP_CLOSE;
661 sk->sk_shutdown |= SEND_SHUTDOWN;
662 sk->sk_state_change(sk);
663 sock_set_flag(sk, SOCK_DEAD);
664 sock_set_flag(sk, SOCK_DESTROY);
665 break;
666 }
667
David S. Millerc751e4f2008-06-17 03:05:13 -0700668 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669out:
Arnd Bergmann77b22832011-01-22 23:44:59 +0100670 release_sock(sk);
671 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 return 0;
673}
674
675static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
676{
677 struct sock *sk = sock->sk;
678 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
andrew hendrya9288522010-02-14 02:00:45 +0000679 int len, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Eric Dumazet92382cd2019-02-23 13:24:59 -0800681 if (addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000682 addr->sx25_family != AF_X25) {
683 rc = -EINVAL;
684 goto out;
685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Martin Schiller3f55d0e2018-11-27 09:50:28 +0100687 /* check for the null_x25_address */
688 if (strcmp(addr->sx25_addr.x25_addr, null_x25_address.x25_addr)) {
689
690 len = strlen(addr->sx25_addr.x25_addr);
691 for (i = 0; i < len; i++) {
692 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
693 rc = -EINVAL;
694 goto out;
695 }
andrew hendrya9288522010-02-14 02:00:45 +0000696 }
697 }
698
andrew hendry90c27292010-09-14 13:31:38 +0000699 lock_sock(sk);
Eric Dumazet92382cd2019-02-23 13:24:59 -0800700 if (sock_flag(sk, SOCK_ZAPPED)) {
701 x25_sk(sk)->source_addr = addr->sx25_addr;
702 x25_insert_socket(sk);
703 sock_reset_flag(sk, SOCK_ZAPPED);
704 } else {
705 rc = -EINVAL;
706 }
andrew hendry90c27292010-09-14 13:31:38 +0000707 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000709out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000710 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711}
712
713static int x25_wait_for_connection_establishment(struct sock *sk)
714{
715 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900716 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Eric Dumazetaa395142010-04-20 13:03:51 +0000718 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 for (;;) {
720 __set_current_state(TASK_INTERRUPTIBLE);
721 rc = -ERESTARTSYS;
722 if (signal_pending(current))
723 break;
724 rc = sock_error(sk);
725 if (rc) {
726 sk->sk_socket->state = SS_UNCONNECTED;
727 break;
728 }
729 rc = 0;
730 if (sk->sk_state != TCP_ESTABLISHED) {
731 release_sock(sk);
732 schedule();
733 lock_sock(sk);
734 } else
735 break;
736 }
737 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000738 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return rc;
740}
741
742static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
743 int addr_len, int flags)
744{
745 struct sock *sk = sock->sk;
746 struct x25_sock *x25 = x25_sk(sk);
747 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
748 struct x25_route *rt;
749 int rc = 0;
750
751 lock_sock(sk);
752 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
753 sock->state = SS_CONNECTED;
754 goto out; /* Connect completed during a ERESTARTSYS event */
755 }
756
757 rc = -ECONNREFUSED;
758 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
759 sock->state = SS_UNCONNECTED;
760 goto out;
761 }
762
763 rc = -EISCONN; /* No reconnect on a seqpacket socket */
764 if (sk->sk_state == TCP_ESTABLISHED)
765 goto out;
766
Martin Schillerc4efc612020-01-09 07:31:14 +0100767 rc = -EALREADY; /* Do nothing if call is already in progress */
768 if (sk->sk_state == TCP_SYN_SENT)
769 goto out;
770
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900771 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 sock->state = SS_UNCONNECTED;
773
774 rc = -EINVAL;
775 if (addr_len != sizeof(struct sockaddr_x25) ||
776 addr->sx25_family != AF_X25)
777 goto out;
778
779 rc = -ENETUNREACH;
780 rt = x25_get_route(&addr->sx25_addr);
781 if (!rt)
782 goto out;
783
784 x25->neighbour = x25_get_neigh(rt->dev);
785 if (!x25->neighbour)
786 goto out_put_route;
787
788 x25_limit_facilities(&x25->facilities, x25->neighbour);
789
790 x25->lci = x25_new_lci(x25->neighbour);
791 if (!x25->lci)
792 goto out_put_neigh;
793
794 rc = -EINVAL;
795 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
796 goto out_put_neigh;
797
798 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
799 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
800
801 x25->dest_addr = addr->sx25_addr;
802
803 /* Move to connecting socket, start sending Connect Requests */
804 sock->state = SS_CONNECTING;
805 sk->sk_state = TCP_SYN_SENT;
806
807 x25->state = X25_STATE_1;
808
809 x25_write_internal(sk, X25_CALL_REQUEST);
810
811 x25_start_heartbeat(sk);
812 x25_start_t21timer(sk);
813
814 /* Now the loop */
815 rc = -EINPROGRESS;
816 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
Martin Schillerc4efc612020-01-09 07:31:14 +0100817 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 rc = x25_wait_for_connection_establishment(sk);
820 if (rc)
821 goto out_put_neigh;
822
823 sock->state = SS_CONNECTED;
824 rc = 0;
825out_put_neigh:
Eric Dumazet31235452019-03-10 09:07:14 -0700826 if (rc) {
827 read_lock_bh(&x25_list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 x25_neigh_put(x25->neighbour);
Eric Dumazet31235452019-03-10 09:07:14 -0700829 x25->neighbour = NULL;
830 read_unlock_bh(&x25_list_lock);
Eric Dumazet06518892019-03-11 13:48:44 -0700831 x25->state = X25_STATE_0;
Eric Dumazet31235452019-03-10 09:07:14 -0700832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833out_put_route:
834 x25_route_put(rt);
835out:
836 release_sock(sk);
837 return rc;
838}
839
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800840static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
842 DECLARE_WAITQUEUE(wait, current);
843 int rc = 0;
844
Eric Dumazetaa395142010-04-20 13:03:51 +0000845 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 for (;;) {
847 __set_current_state(TASK_INTERRUPTIBLE);
848 if (sk->sk_shutdown & RCV_SHUTDOWN)
849 break;
850 rc = -ERESTARTSYS;
851 if (signal_pending(current))
852 break;
853 rc = -EAGAIN;
854 if (!timeout)
855 break;
856 rc = 0;
857 if (skb_queue_empty(&sk->sk_receive_queue)) {
858 release_sock(sk);
859 timeout = schedule_timeout(timeout);
860 lock_sock(sk);
861 } else
862 break;
863 }
864 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000865 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return rc;
867}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
870{
871 struct sock *sk = sock->sk;
872 struct sock *newsk;
873 struct sk_buff *skb;
874 int rc = -EINVAL;
875
Andrew Hendry141646c2010-09-14 20:38:54 -0700876 if (!sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto out;
878
879 rc = -EOPNOTSUPP;
880 if (sk->sk_type != SOCK_SEQPACKET)
881 goto out;
882
883 lock_sock(sk);
Andrew Hendry141646c2010-09-14 20:38:54 -0700884 rc = -EINVAL;
885 if (sk->sk_state != TCP_LISTEN)
886 goto out2;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
889 if (rc)
890 goto out2;
891 skb = skb_dequeue(&sk->sk_receive_queue);
892 rc = -EINVAL;
893 if (!skb->sk)
894 goto out2;
895 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700896 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 /* Now attach up the new socket */
899 skb->sk = NULL;
900 kfree_skb(skb);
901 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 newsock->state = SS_CONNECTED;
903 rc = 0;
904out2:
905 release_sock(sk);
906out:
907 return rc;
908}
909
910static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
911 int *uaddr_len, int peer)
912{
913 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
914 struct sock *sk = sock->sk;
915 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000916 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000919 if (sk->sk_state != TCP_ESTABLISHED) {
920 rc = -ENOTCONN;
921 goto out;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 sx25->sx25_addr = x25->dest_addr;
924 } else
925 sx25->sx25_addr = x25->source_addr;
926
927 sx25->sx25_family = AF_X25;
928 *uaddr_len = sizeof(*sx25);
929
Arnd Bergmann91774902009-11-05 04:37:29 +0000930out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000931 return rc;
932}
933
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
935 unsigned int lci)
936{
937 struct sock *sk;
938 struct sock *make;
939 struct x25_sock *makex25;
940 struct x25_address source_addr, dest_addr;
941 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800942 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800943 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /*
946 * Remove the LCI and frame type.
947 */
948 skb_pull(skb, X25_STD_MIN_LEN);
949
950 /*
951 * Extract the X.25 addresses and convert them to ASCII strings,
952 * and remove them.
John Hughesf5eb9172010-04-07 21:29:25 -0700953 *
954 * Address block is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 */
John Hughesf5eb9172010-04-07 21:29:25 -0700956 addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
957 if (addr_len <= 0)
958 goto out_clear_request;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800959 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * Get the length of the facilities, skip past them for the moment
963 * get the call user data because this is needed to determine
964 * the correct listener
John Hughesf5eb9172010-04-07 21:29:25 -0700965 *
966 * Facilities length is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
Matthew Daleycb101ed2011-10-14 18:45:04 +0000968 if (!pskb_may_pull(skb, 1))
John Hughesf5eb9172010-04-07 21:29:25 -0700969 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 len = skb->data[0] + 1;
Matthew Daleycb101ed2011-10-14 18:45:04 +0000971 if (!pskb_may_pull(skb, len))
John Hughesf5eb9172010-04-07 21:29:25 -0700972 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 skb_pull(skb,len);
974
975 /*
Matthew Daleyc7fd0d42011-10-14 18:45:03 +0000976 * Ensure that the amount of call user data is valid.
977 */
978 if (skb->len > X25_MAX_CUD_LEN)
979 goto out_clear_request;
980
981 /*
Matthew Daleycb101ed2011-10-14 18:45:04 +0000982 * Get all the call user data so it can be used in
983 * x25_find_listener and skb_copy_from_linear_data up ahead.
984 */
985 if (!pskb_may_pull(skb, skb->len))
986 goto out_clear_request;
987
988 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * Find a listener for the particular address/cud pair.
990 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700991 sk = x25_find_listener(&source_addr,skb);
992 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800994 if (sk != NULL && sk_acceptq_is_full(sk)) {
995 goto out_sock_put;
996 }
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800999 * We dont have any listeners for this incoming call.
1000 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001002 if (sk == NULL) {
1003 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -08001004 if (sysctl_x25_forward &&
1005 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001006 {
1007 /* Call was forwarded, dont process it any more */
1008 kfree_skb(skb);
1009 rc = 1;
1010 goto out;
1011 } else {
1012 /* No listeners, can't forward, clear the call */
1013 goto out_clear_request;
1014 }
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 /*
1018 * Try to reach a compromise on the requested facilities.
1019 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001020 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1021 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto out_sock_put;
1023
1024 /*
1025 * current neighbour/link might impose additional limits
1026 * on certain facilties
1027 */
1028
1029 x25_limit_facilities(&facilities, nb);
1030
1031 /*
1032 * Try to create a new socket.
1033 */
1034 make = x25_make_new(sk);
1035 if (!make)
1036 goto out_sock_put;
1037
1038 /*
1039 * Remove the facilities
1040 */
1041 skb_pull(skb, len);
1042
1043 skb->sk = make;
1044 make->sk_state = TCP_ESTABLISHED;
1045
1046 makex25 = x25_sk(make);
1047 makex25->lci = lci;
1048 makex25->dest_addr = dest_addr;
1049 makex25->source_addr = source_addr;
1050 makex25->neighbour = nb;
1051 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001052 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001054 /* ensure no reverse facil on accept */
1055 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001056 /* ensure no calling address extension on accept */
1057 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001058 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
andrew hendry37cda782010-05-16 23:00:27 +00001060 /* Normally all calls are accepted immediately */
1061 if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001062 x25_write_internal(make, X25_CALL_ACCEPTED);
1063 makex25->state = X25_STATE_3;
1064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Shaun Pereiracb65d502005-06-22 22:15:01 -07001066 /*
1067 * Incoming Call User Data.
1068 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001069 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1070 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 sk->sk_ack_backlog++;
1073
1074 x25_insert_socket(make);
1075
1076 skb_queue_head(&sk->sk_receive_queue, skb);
1077
1078 x25_start_heartbeat(make);
1079
1080 if (!sock_flag(sk, SOCK_DEAD))
David S. Miller676d2362014-04-11 16:15:36 -04001081 sk->sk_data_ready(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 rc = 1;
1083 sock_put(sk);
1084out:
1085 return rc;
1086out_sock_put:
1087 sock_put(sk);
1088out_clear_request:
1089 rc = 0;
1090 x25_transmit_clear_request(nb, lci, 0x01);
1091 goto out;
1092}
1093
Ying Xue1b784142015-03-02 15:37:48 +08001094static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 struct sock *sk = sock->sk;
1097 struct x25_sock *x25 = x25_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001098 DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 struct sockaddr_x25 sx25;
1100 struct sk_buff *skb;
1101 unsigned char *asmptr;
1102 int noblock = msg->msg_flags & MSG_DONTWAIT;
1103 size_t size;
1104 int qbit = 0, rc = -EINVAL;
1105
Arnd Bergmann77b22832011-01-22 23:44:59 +01001106 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1108 goto out;
1109
1110 /* we currently don't support segmented records at the user interface */
1111 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1112 goto out;
1113
1114 rc = -EADDRNOTAVAIL;
1115 if (sock_flag(sk, SOCK_ZAPPED))
1116 goto out;
1117
1118 rc = -EPIPE;
1119 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1120 send_sig(SIGPIPE, current, 0);
1121 goto out;
1122 }
1123
1124 rc = -ENETUNREACH;
1125 if (!x25->neighbour)
1126 goto out;
1127
1128 if (usx25) {
1129 rc = -EINVAL;
1130 if (msg->msg_namelen < sizeof(sx25))
1131 goto out;
1132 memcpy(&sx25, usx25, sizeof(sx25));
1133 rc = -EISCONN;
1134 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1135 goto out;
1136 rc = -EINVAL;
1137 if (sx25.sx25_family != AF_X25)
1138 goto out;
1139 } else {
1140 /*
1141 * FIXME 1003.1g - if the socket is like this because
1142 * it has become closed (not started closed) we ought
1143 * to SIGPIPE, EPIPE;
1144 */
1145 rc = -ENOTCONN;
1146 if (sk->sk_state != TCP_ESTABLISHED)
1147 goto out;
1148
1149 sx25.sx25_family = AF_X25;
1150 sx25.sx25_addr = x25->dest_addr;
1151 }
1152
Alan Cox83e0bbc2009-03-27 00:28:21 -07001153 /* Sanity check the packet size */
1154 if (len > 65535) {
1155 rc = -EMSGSIZE;
1156 goto out;
1157 }
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1160
1161 /* Build a packet */
1162 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1163
1164 if ((msg->msg_flags & MSG_OOB) && len > 32)
1165 len = 32;
1166
1167 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1168
Arnd Bergmann77b22832011-01-22 23:44:59 +01001169 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
Arnd Bergmann77b22832011-01-22 23:44:59 +01001171 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!skb)
1173 goto out;
1174 X25_SKB_CB(skb)->flags = msg->msg_flags;
1175
1176 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1177
1178 /*
1179 * Put the data on the end
1180 */
1181 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1182
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001183 skb_reset_transport_header(skb);
1184 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Al Viro6ce8e9c2014-04-06 21:25:44 -04001186 rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (rc)
1188 goto out_kfree_skb;
1189
1190 /*
1191 * If the Q BIT Include socket option is in force, the first
1192 * byte of the user data is the logical value of the Q Bit.
1193 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001194 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Matthew Daleycb101ed2011-10-14 18:45:04 +00001195 if (!pskb_may_pull(skb, 1))
1196 goto out_kfree_skb;
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 qbit = skb->data[0];
1199 skb_pull(skb, 1);
1200 }
1201
1202 /*
1203 * Push down the X.25 header
1204 */
1205 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1206
1207 if (msg->msg_flags & MSG_OOB) {
1208 if (x25->neighbour->extended) {
1209 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1210 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1211 *asmptr++ = (x25->lci >> 0) & 0xFF;
1212 *asmptr++ = X25_INTERRUPT;
1213 } else {
1214 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1215 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1216 *asmptr++ = (x25->lci >> 0) & 0xFF;
1217 *asmptr++ = X25_INTERRUPT;
1218 }
1219 } else {
1220 if (x25->neighbour->extended) {
1221 /* Build an Extended X.25 header */
1222 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1223 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1224 *asmptr++ = (x25->lci >> 0) & 0xFF;
1225 *asmptr++ = X25_DATA;
1226 *asmptr++ = X25_DATA;
1227 } else {
1228 /* Build an Standard X.25 header */
1229 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1230 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1231 *asmptr++ = (x25->lci >> 0) & 0xFF;
1232 *asmptr++ = X25_DATA;
1233 }
1234
1235 if (qbit)
1236 skb->data[0] |= X25_Q_BIT;
1237 }
1238
1239 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1240 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1241
1242 rc = -ENOTCONN;
1243 if (sk->sk_state != TCP_ESTABLISHED)
1244 goto out_kfree_skb;
1245
1246 if (msg->msg_flags & MSG_OOB)
1247 skb_queue_tail(&x25->interrupt_out_queue, skb);
1248 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001249 rc = x25_output(sk, skb);
1250 len = rc;
1251 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 kfree_skb(skb);
andrew hendrycb863ff2010-05-16 22:59:41 +00001253 else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 len++;
1255 }
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 x25_kick(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 rc = len;
1259out:
Arnd Bergmann77b22832011-01-22 23:44:59 +01001260 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 return rc;
1262out_kfree_skb:
1263 kfree_skb(skb);
1264 goto out;
1265}
1266
1267
Ying Xue1b784142015-03-02 15:37:48 +08001268static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int flags)
1270{
1271 struct sock *sk = sock->sk;
1272 struct x25_sock *x25 = x25_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001273 DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 size_t copied;
Dave Jones501e89d2011-11-01 16:26:44 +00001275 int qbit, header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 struct sk_buff *skb;
1277 unsigned char *asmptr;
1278 int rc = -ENOTCONN;
1279
Arnd Bergmann77b22832011-01-22 23:44:59 +01001280 lock_sock(sk);
Dave Jones501e89d2011-11-01 16:26:44 +00001281
1282 if (x25->neighbour == NULL)
1283 goto out;
1284
1285 header_len = x25->neighbour->extended ?
1286 X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 /*
1289 * This works for seqpacket too. The receiver has ordered the queue for
1290 * us! We do one quick check first though
1291 */
1292 if (sk->sk_state != TCP_ESTABLISHED)
1293 goto out;
1294
1295 if (flags & MSG_OOB) {
1296 rc = -EINVAL;
1297 if (sock_flag(sk, SOCK_URGINLINE) ||
1298 !skb_peek(&x25->interrupt_in_queue))
1299 goto out;
1300
1301 skb = skb_dequeue(&x25->interrupt_in_queue);
1302
Matthew Daleycb101ed2011-10-14 18:45:04 +00001303 if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1304 goto out_free_dgram;
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 skb_pull(skb, X25_STD_MIN_LEN);
1307
1308 /*
1309 * No Q bit information on Interrupt data.
1310 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001311 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 asmptr = skb_push(skb, 1);
1313 *asmptr = 0x00;
1314 }
1315
1316 msg->msg_flags |= MSG_OOB;
1317 } else {
1318 /* Now we can treat all alike */
Arnd Bergmann77b22832011-01-22 23:44:59 +01001319 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1321 flags & MSG_DONTWAIT, &rc);
Arnd Bergmann77b22832011-01-22 23:44:59 +01001322 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (!skb)
1324 goto out;
1325
Matthew Daleycb101ed2011-10-14 18:45:04 +00001326 if (!pskb_may_pull(skb, header_len))
1327 goto out_free_dgram;
1328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1330
Matthew Daleycb101ed2011-10-14 18:45:04 +00001331 skb_pull(skb, header_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
andrew hendrycb863ff2010-05-16 22:59:41 +00001333 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 asmptr = skb_push(skb, 1);
1335 *asmptr = qbit;
1336 }
1337 }
1338
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001339 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 copied = skb->len;
1341
1342 if (copied > size) {
1343 copied = size;
1344 msg->msg_flags |= MSG_TRUNC;
1345 }
1346
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001347 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 msg->msg_flags |= MSG_EOR;
1349
David S. Miller51f3d022014-11-05 16:46:40 -05001350 rc = skb_copy_datagram_msg(skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (rc)
1352 goto out_free_dgram;
1353
1354 if (sx25) {
1355 sx25->sx25_family = AF_X25;
1356 sx25->sx25_addr = x25->dest_addr;
Hannes Frederic Sowaf3d33422013-11-21 03:14:22 +01001357 msg->msg_namelen = sizeof(*sx25);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 x25_check_rbuf(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 rc = copied;
1362out_free_dgram:
1363 skb_free_datagram(sk, skb);
1364out:
Arnd Bergmann77b22832011-01-22 23:44:59 +01001365 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return rc;
1367}
1368
1369
1370static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1371{
1372 struct sock *sk = sock->sk;
1373 struct x25_sock *x25 = x25_sk(sk);
1374 void __user *argp = (void __user *)arg;
1375 int rc;
1376
1377 switch (cmd) {
Joe Perchesfddc5f32011-07-01 09:43:13 +00001378 case TIOCOUTQ: {
1379 int amount;
Eric Dumazet31e6d362009-06-17 19:05:41 -07001380
Joe Perchesfddc5f32011-07-01 09:43:13 +00001381 amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1382 if (amount < 0)
1383 amount = 0;
1384 rc = put_user(amount, (unsigned int __user *)argp);
1385 break;
1386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Joe Perchesfddc5f32011-07-01 09:43:13 +00001388 case TIOCINQ: {
1389 struct sk_buff *skb;
1390 int amount = 0;
1391 /*
1392 * These two are safe on a single CPU system as
1393 * only user tasks fiddle here
1394 */
1395 lock_sock(sk);
1396 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1397 amount = skb->len;
1398 release_sock(sk);
1399 rc = put_user(amount, (unsigned int __user *)argp);
1400 break;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Joe Perchesfddc5f32011-07-01 09:43:13 +00001403 case SIOCGSTAMP:
1404 rc = -EINVAL;
1405 if (sk)
1406 rc = sock_get_timestamp(sk,
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001407 (struct timeval __user *)argp);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001408 break;
1409 case SIOCGSTAMPNS:
1410 rc = -EINVAL;
1411 if (sk)
1412 rc = sock_get_timestampns(sk,
1413 (struct timespec __user *)argp);
1414 break;
1415 case SIOCGIFADDR:
1416 case SIOCSIFADDR:
1417 case SIOCGIFDSTADDR:
1418 case SIOCSIFDSTADDR:
1419 case SIOCGIFBRDADDR:
1420 case SIOCSIFBRDADDR:
1421 case SIOCGIFNETMASK:
1422 case SIOCSIFNETMASK:
1423 case SIOCGIFMETRIC:
1424 case SIOCSIFMETRIC:
1425 rc = -EINVAL;
1426 break;
1427 case SIOCADDRT:
1428 case SIOCDELRT:
1429 rc = -EPERM;
1430 if (!capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001432 rc = x25_route_ioctl(cmd, argp);
1433 break;
1434 case SIOCX25GSUBSCRIP:
1435 rc = x25_subscr_ioctl(cmd, argp);
1436 break;
1437 case SIOCX25SSUBSCRIP:
1438 rc = -EPERM;
1439 if (!capable(CAP_NET_ADMIN))
Eric Dumazetae40eb12007-03-18 17:33:16 -07001440 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001441 rc = x25_subscr_ioctl(cmd, argp);
1442 break;
1443 case SIOCX25GFACILITIES: {
1444 lock_sock(sk);
1445 rc = copy_to_user(argp, &x25->facilities,
1446 sizeof(x25->facilities))
1447 ? -EFAULT : 0;
1448 release_sock(sk);
1449 break;
1450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Joe Perchesfddc5f32011-07-01 09:43:13 +00001452 case SIOCX25SFACILITIES: {
1453 struct x25_facilities facilities;
1454 rc = -EFAULT;
1455 if (copy_from_user(&facilities, argp, sizeof(facilities)))
1456 break;
1457 rc = -EINVAL;
1458 lock_sock(sk);
1459 if (sk->sk_state != TCP_LISTEN &&
1460 sk->sk_state != TCP_CLOSE)
1461 goto out_fac_release;
1462 if (facilities.pacsize_in < X25_PS16 ||
1463 facilities.pacsize_in > X25_PS4096)
1464 goto out_fac_release;
1465 if (facilities.pacsize_out < X25_PS16 ||
1466 facilities.pacsize_out > X25_PS4096)
1467 goto out_fac_release;
1468 if (facilities.winsize_in < 1 ||
1469 facilities.winsize_in > 127)
1470 goto out_fac_release;
1471 if (facilities.throughput) {
1472 int out = facilities.throughput & 0xf0;
1473 int in = facilities.throughput & 0x0f;
1474 if (!out)
1475 facilities.throughput |=
1476 X25_DEFAULT_THROUGHPUT << 4;
1477 else if (out < 0x30 || out > 0xD0)
andrew hendryf90de662010-11-25 02:18:35 +00001478 goto out_fac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001479 if (!in)
1480 facilities.throughput |=
1481 X25_DEFAULT_THROUGHPUT;
1482 else if (in < 0x03 || in > 0x0D)
andrew hendryf90de662010-11-25 02:18:35 +00001483 goto out_fac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001484 }
1485 if (facilities.reverse &&
1486 (facilities.reverse & 0x81) != 0x81)
1487 goto out_fac_release;
1488 x25->facilities = facilities;
1489 rc = 0;
andrew hendryf90de662010-11-25 02:18:35 +00001490out_fac_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001491 release_sock(sk);
1492 break;
1493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Joe Perchesfddc5f32011-07-01 09:43:13 +00001495 case SIOCX25GDTEFACILITIES: {
1496 lock_sock(sk);
1497 rc = copy_to_user(argp, &x25->dte_facilities,
1498 sizeof(x25->dte_facilities));
1499 release_sock(sk);
1500 if (rc)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001501 rc = -EFAULT;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001502 break;
1503 }
1504
1505 case SIOCX25SDTEFACILITIES: {
1506 struct x25_dte_facilities dtefacs;
1507 rc = -EFAULT;
1508 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1509 break;
1510 rc = -EINVAL;
1511 lock_sock(sk);
1512 if (sk->sk_state != TCP_LISTEN &&
1513 sk->sk_state != TCP_CLOSE)
1514 goto out_dtefac_release;
1515 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1516 goto out_dtefac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001517 if (dtefacs.called_len > X25_MAX_AE_LEN)
1518 goto out_dtefac_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001519 x25->dte_facilities = dtefacs;
1520 rc = 0;
andrew hendryf90de662010-11-25 02:18:35 +00001521out_dtefac_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001522 release_sock(sk);
1523 break;
1524 }
1525
1526 case SIOCX25GCALLUSERDATA: {
1527 lock_sock(sk);
1528 rc = copy_to_user(argp, &x25->calluserdata,
1529 sizeof(x25->calluserdata))
1530 ? -EFAULT : 0;
1531 release_sock(sk);
1532 break;
1533 }
1534
1535 case SIOCX25SCALLUSERDATA: {
1536 struct x25_calluserdata calluserdata;
1537
1538 rc = -EFAULT;
1539 if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001540 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001541 rc = -EINVAL;
1542 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001544 lock_sock(sk);
1545 x25->calluserdata = calluserdata;
1546 release_sock(sk);
1547 rc = 0;
1548 break;
1549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Joe Perchesfddc5f32011-07-01 09:43:13 +00001551 case SIOCX25GCAUSEDIAG: {
1552 lock_sock(sk);
1553 rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
1554 ? -EFAULT : 0;
1555 release_sock(sk);
1556 break;
1557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Joe Perchesfddc5f32011-07-01 09:43:13 +00001559 case SIOCX25SCAUSEDIAG: {
1560 struct x25_causediag causediag;
1561 rc = -EFAULT;
1562 if (copy_from_user(&causediag, argp, sizeof(causediag)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 break;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001564 lock_sock(sk);
1565 x25->causediag = causediag;
1566 release_sock(sk);
1567 rc = 0;
1568 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Joe Perchesfddc5f32011-07-01 09:43:13 +00001570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Joe Perchesfddc5f32011-07-01 09:43:13 +00001572 case SIOCX25SCUDMATCHLEN: {
1573 struct x25_subaddr sub_addr;
1574 rc = -EINVAL;
1575 lock_sock(sk);
1576 if(sk->sk_state != TCP_CLOSE)
1577 goto out_cud_release;
1578 rc = -EFAULT;
1579 if (copy_from_user(&sub_addr, argp,
1580 sizeof(sub_addr)))
1581 goto out_cud_release;
1582 rc = -EINVAL;
1583 if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1584 goto out_cud_release;
1585 x25->cudmatchlength = sub_addr.cudmatchlength;
1586 rc = 0;
andrew hendry3f0a0692010-11-25 02:18:45 +00001587out_cud_release:
Joe Perchesfddc5f32011-07-01 09:43:13 +00001588 release_sock(sk);
1589 break;
1590 }
Shaun Pereiracb65d502005-06-22 22:15:01 -07001591
Joe Perchesfddc5f32011-07-01 09:43:13 +00001592 case SIOCX25CALLACCPTAPPRV: {
1593 rc = -EINVAL;
1594 lock_sock(sk);
Dave Jones4ccb93c2013-06-28 12:13:52 -04001595 if (sk->sk_state == TCP_CLOSE) {
1596 clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1597 rc = 0;
1598 }
Joe Perchesfddc5f32011-07-01 09:43:13 +00001599 release_sock(sk);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001600 break;
1601 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001602
Joe Perchesfddc5f32011-07-01 09:43:13 +00001603 case SIOCX25SENDCALLACCPT: {
1604 rc = -EINVAL;
1605 lock_sock(sk);
1606 if (sk->sk_state != TCP_ESTABLISHED)
Dave Jones4ccb93c2013-06-28 12:13:52 -04001607 goto out_sendcallaccpt_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001608 /* must call accptapprv above */
1609 if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
Dave Jones4ccb93c2013-06-28 12:13:52 -04001610 goto out_sendcallaccpt_release;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001611 x25_write_internal(sk, X25_CALL_ACCEPTED);
1612 x25->state = X25_STATE_3;
Joe Perchesfddc5f32011-07-01 09:43:13 +00001613 rc = 0;
Dave Jones4ccb93c2013-06-28 12:13:52 -04001614out_sendcallaccpt_release:
1615 release_sock(sk);
Joe Perchesfddc5f32011-07-01 09:43:13 +00001616 break;
1617 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001618
Joe Perchesfddc5f32011-07-01 09:43:13 +00001619 default:
1620 rc = -ENOIOCTLCMD;
1621 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 return rc;
1625}
1626
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001627static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 .family = AF_X25,
1629 .create = x25_create,
1630 .owner = THIS_MODULE,
1631};
1632
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001633#ifdef CONFIG_COMPAT
1634static int compat_x25_subscr_ioctl(unsigned int cmd,
1635 struct compat_x25_subscrip_struct __user *x25_subscr32)
1636{
1637 struct compat_x25_subscrip_struct x25_subscr;
1638 struct x25_neigh *nb;
1639 struct net_device *dev;
1640 int rc = -EINVAL;
1641
1642 rc = -EFAULT;
1643 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{
1685 void __user *argp = compat_ptr(arg);
1686 struct sock *sk = sock->sk;
1687
1688 int rc = -ENOIOCTLCMD;
1689
1690 switch(cmd) {
1691 case TIOCOUTQ:
1692 case TIOCINQ:
1693 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1694 break;
1695 case SIOCGSTAMP:
1696 rc = -EINVAL;
1697 if (sk)
1698 rc = compat_sock_get_timestamp(sk,
1699 (struct timeval __user*)argp);
1700 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001701 case SIOCGSTAMPNS:
1702 rc = -EINVAL;
1703 if (sk)
1704 rc = compat_sock_get_timestampns(sk,
1705 (struct timespec __user*)argp);
1706 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001707 case SIOCGIFADDR:
1708 case SIOCSIFADDR:
1709 case SIOCGIFDSTADDR:
1710 case SIOCSIFDSTADDR:
1711 case SIOCGIFBRDADDR:
1712 case SIOCSIFBRDADDR:
1713 case SIOCGIFNETMASK:
1714 case SIOCSIFNETMASK:
1715 case SIOCGIFMETRIC:
1716 case SIOCSIFMETRIC:
1717 rc = -EINVAL;
1718 break;
1719 case SIOCADDRT:
1720 case SIOCDELRT:
1721 rc = -EPERM;
1722 if (!capable(CAP_NET_ADMIN))
1723 break;
1724 rc = x25_route_ioctl(cmd, argp);
1725 break;
1726 case SIOCX25GSUBSCRIP:
1727 rc = compat_x25_subscr_ioctl(cmd, argp);
1728 break;
1729 case SIOCX25SSUBSCRIP:
1730 rc = -EPERM;
1731 if (!capable(CAP_NET_ADMIN))
1732 break;
1733 rc = compat_x25_subscr_ioctl(cmd, argp);
1734 break;
1735 case SIOCX25GFACILITIES:
1736 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001737 case SIOCX25GDTEFACILITIES:
1738 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001739 case SIOCX25GCALLUSERDATA:
1740 case SIOCX25SCALLUSERDATA:
1741 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001742 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001743 case SIOCX25SCUDMATCHLEN:
1744 case SIOCX25CALLACCPTAPPRV:
1745 case SIOCX25SENDCALLACCPT:
1746 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1747 break;
1748 default:
1749 rc = -ENOIOCTLCMD;
1750 break;
1751 }
1752 return rc;
1753}
1754#endif
1755
Arnd Bergmann91774902009-11-05 04:37:29 +00001756static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .family = AF_X25,
1758 .owner = THIS_MODULE,
1759 .release = x25_release,
1760 .bind = x25_bind,
1761 .connect = x25_connect,
1762 .socketpair = sock_no_socketpair,
1763 .accept = x25_accept,
1764 .getname = x25_getname,
andrew hendry768190f2010-09-21 15:24:45 +00001765 .poll = datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001767#ifdef CONFIG_COMPAT
1768 .compat_ioctl = compat_x25_ioctl,
1769#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .listen = x25_listen,
1771 .shutdown = sock_no_shutdown,
1772 .setsockopt = x25_setsockopt,
1773 .getsockopt = x25_getsockopt,
1774 .sendmsg = x25_sendmsg,
1775 .recvmsg = x25_recvmsg,
1776 .mmap = sock_no_mmap,
1777 .sendpage = sock_no_sendpage,
1778};
1779
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001780static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001781 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 .func = x25_lapb_receive_frame,
1783};
1784
1785static struct notifier_block x25_dev_notifier = {
1786 .notifier_call = x25_device_event,
1787};
1788
1789void x25_kill_by_neigh(struct x25_neigh *nb)
1790{
1791 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
1793 write_lock_bh(&x25_list_lock);
1794
Sasha Levinb67bfe02013-02-27 17:06:00 -08001795 sk_for_each(s, &x25_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (x25_sk(s)->neighbour == nb)
1797 x25_disconnect(s, ENETUNREACH, 0, 0);
1798
1799 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001800
1801 /* Remove any related forwards */
1802 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805static int __init x25_init(void)
1806{
linzhang53678832017-05-17 12:05:07 +08001807 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
linzhang53678832017-05-17 12:05:07 +08001809 rc = proto_register(&x25_proto, 0);
1810 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto out;
1812
andrew hendry1fd975a2009-11-24 15:15:42 +00001813 rc = sock_register(&x25_family_ops);
linzhang53678832017-05-17 12:05:07 +08001814 if (rc)
andrew hendry1fd975a2009-11-24 15:15:42 +00001815 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 dev_add_pack(&x25_packet_type);
1818
andrew hendry1fd975a2009-11-24 15:15:42 +00001819 rc = register_netdevice_notifier(&x25_dev_notifier);
linzhang53678832017-05-17 12:05:07 +08001820 if (rc)
andrew hendry1fd975a2009-11-24 15:15:42 +00001821 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
linzhang53678832017-05-17 12:05:07 +08001823 rc = x25_register_sysctl();
1824 if (rc)
1825 goto out_dev;
1826
1827 rc = x25_proc_init();
1828 if (rc)
1829 goto out_sysctl;
1830
wangweidongb73e9e32013-12-06 19:24:33 +08001831 pr_info("Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833out:
1834 return rc;
linzhang53678832017-05-17 12:05:07 +08001835out_sysctl:
1836 x25_unregister_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001837out_dev:
1838 unregister_netdevice_notifier(&x25_dev_notifier);
1839out_sock:
linzhang53678832017-05-17 12:05:07 +08001840 dev_remove_pack(&x25_packet_type);
andrew hendry1fd975a2009-11-24 15:15:42 +00001841 sock_unregister(AF_X25);
1842out_proto:
1843 proto_unregister(&x25_proto);
1844 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846module_init(x25_init);
1847
1848static void __exit x25_exit(void)
1849{
1850 x25_proc_exit();
1851 x25_link_free();
1852 x25_route_free();
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 unregister_netdevice_notifier(&x25_dev_notifier);
1857
1858 dev_remove_pack(&x25_packet_type);
1859
1860 sock_unregister(AF_X25);
1861 proto_unregister(&x25_proto);
1862}
1863module_exit(x25_exit);
1864
1865MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1866MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1867MODULE_LICENSE("GPL");
1868MODULE_ALIAS_NETPROTO(PF_X25);