blob: 720534ce1fd97fe25aa9830c7417b68faa5b5c92 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * X.25 Packet Layer release 002
3 *
4 * This is ALPHA test software. This code may break your machine,
5 * randomly fail to work with new releases, misbehave and/or generally
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09006 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code REQUIRES 2.1.15 or higher
9 *
10 * This module:
11 * This module is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * History
17 * X.25 001 Jonathan Naylor Started coding.
18 * X.25 002 Jonathan Naylor Centralised disconnect handling.
19 * New timer architecture.
20 * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090021 * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
22 * facilities negotiation and increased
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * the throughput upper limit.
24 * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090025 * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Fixed x25_output() related skb leakage.
27 * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
28 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
29 * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN
30 * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
31 * x25_proc.c, using seq_file
Shaun Pereiracb65d502005-06-22 22:15:01 -070032 * 2005-04-02 Shaun Pereira Selective sub address matching
33 * with call user data
Shaun Pereiraebc3f642005-06-22 22:16:17 -070034 * 2005-04-15 Shaun Pereira Fast select with no restriction on
35 * response
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080039#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/kernel.h>
42#include <linux/sched.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040043#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/timer.h>
45#include <linux/string.h>
46#include <linux/net.h>
47#include <linux/netdevice.h>
48#include <linux/if_arp.h>
49#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070052#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/uaccess.h>
54#include <linux/fcntl.h>
55#include <linux/termios.h> /* For TIOCINQ/OUTQ */
56#include <linux/notifier.h>
57#include <linux/init.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080058#include <linux/compat.h>
andrew hendrya9288522010-02-14 02:00:45 +000059#include <linux/ctype.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080062#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
65int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
66int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
67int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
68int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080069int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71HLIST_HEAD(x25_list);
72DEFINE_RWLOCK(x25_list_lock);
73
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080074static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76static struct x25_address null_x25_address = {" "};
77
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080078#ifdef CONFIG_COMPAT
79struct compat_x25_subscrip_struct {
80 char device[200-sizeof(compat_ulong_t)];
81 compat_ulong_t global_facil_mask;
82 compat_uint_t extended;
83};
84#endif
85
John Hughesf5eb9172010-04-07 21:29:25 -070086
87int x25_parse_address_block(struct sk_buff *skb,
88 struct x25_address *called_addr,
89 struct x25_address *calling_addr)
90{
91 unsigned char len;
92 int needed;
93 int rc;
94
95 if (skb->len < 1) {
96 /* packet has no address block */
97 rc = 0;
98 goto empty;
99 }
100
101 len = *skb->data;
102 needed = 1 + (len >> 4) + (len & 0x0f);
103
104 if (skb->len < needed) {
105 /* packet is too short to hold the addresses it claims
106 to hold */
107 rc = -1;
108 goto empty;
109 }
110
111 return x25_addr_ntoa(skb->data, called_addr, calling_addr);
112
113empty:
114 *called_addr->x25_addr = 0;
115 *calling_addr->x25_addr = 0;
116
117 return rc;
118}
119
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
122 struct x25_address *calling_addr)
123{
Eric Dumazet6bf15742008-01-13 22:27:52 -0800124 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -0800126 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 called_len = (*p >> 0) & 0x0F;
129 calling_len = (*p >> 4) & 0x0F;
130
131 called = called_addr->x25_addr;
132 calling = calling_addr->x25_addr;
133 p++;
134
135 for (i = 0; i < (called_len + calling_len); i++) {
136 if (i < called_len) {
137 if (i % 2 != 0) {
138 *called++ = ((*p >> 0) & 0x0F) + '0';
139 p++;
140 } else {
141 *called++ = ((*p >> 4) & 0x0F) + '0';
142 }
143 } else {
144 if (i % 2 != 0) {
145 *calling++ = ((*p >> 0) & 0x0F) + '0';
146 p++;
147 } else {
148 *calling++ = ((*p >> 4) & 0x0F) + '0';
149 }
150 }
151 }
152
153 *called = *calling = '\0';
154
155 return 1 + (called_len + calling_len + 1) / 2;
156}
157
158int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
159 struct x25_address *calling_addr)
160{
161 unsigned int called_len, calling_len;
162 char *called, *calling;
163 int i;
164
165 called = called_addr->x25_addr;
166 calling = calling_addr->x25_addr;
167
168 called_len = strlen(called);
169 calling_len = strlen(calling);
170
171 *p++ = (calling_len << 4) | (called_len << 0);
172
173 for (i = 0; i < (called_len + calling_len); i++) {
174 if (i < called_len) {
175 if (i % 2 != 0) {
176 *p |= (*called++ - '0') << 0;
177 p++;
178 } else {
179 *p = 0x00;
180 *p |= (*called++ - '0') << 4;
181 }
182 } else {
183 if (i % 2 != 0) {
184 *p |= (*calling++ - '0') << 0;
185 p++;
186 } else {
187 *p = 0x00;
188 *p |= (*calling++ - '0') << 4;
189 }
190 }
191 }
192
193 return 1 + (called_len + calling_len + 1) / 2;
194}
195
196/*
197 * Socket removal during an interrupt is now safe.
198 */
199static void x25_remove_socket(struct sock *sk)
200{
201 write_lock_bh(&x25_list_lock);
202 sk_del_node_init(sk);
203 write_unlock_bh(&x25_list_lock);
204}
205
206/*
207 * Kill all bound sockets on a dropped device.
208 */
209static void x25_kill_by_device(struct net_device *dev)
210{
211 struct sock *s;
212 struct hlist_node *node;
213
214 write_lock_bh(&x25_list_lock);
215
216 sk_for_each(s, node, &x25_list)
217 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{
229 struct net_device *dev = ptr;
230 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
236#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
237 || dev->type == ARPHRD_ETHER
238#endif
239 ) {
240 switch (event) {
241 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;
256 }
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;
284 struct hlist_node *node;
285
286 read_lock_bh(&x25_list_lock);
287 next_best = NULL;
288
289 sk_for_each(s, node, &x25_list)
290 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700291 x25_sk(s)->source_addr.x25_addr) ||
292 !strcmp(addr->x25_addr,
293 null_x25_address.x25_addr)) &&
294 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /*
296 * Found a listening socket, now check the incoming
297 * call user data vs this sockets call user data
298 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700299 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
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;
326 struct hlist_node *node;
327
328 sk_for_each(s, node, &x25_list)
329 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
330 sock_hold(s);
331 goto found;
332 }
333 s = NULL;
334found:
335 return s;
336}
337
338struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
339{
340 struct sock *s;
341
342 read_lock_bh(&x25_list_lock);
343 s = __x25_find_socket(lci, nb);
344 read_unlock_bh(&x25_list_lock);
345 return s;
346}
347
348/*
349 * Find a unique LCI for a given device.
350 */
351static unsigned int x25_new_lci(struct x25_neigh *nb)
352{
353 unsigned int lci = 1;
354 struct sock *sk;
355
356 read_lock_bh(&x25_list_lock);
357
358 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
359 sock_put(sk);
360 if (++lci == 4096) {
361 lci = 0;
362 break;
363 }
364 }
365
366 read_unlock_bh(&x25_list_lock);
367 return lci;
368}
369
370/*
371 * Deferred destroy.
372 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700373static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375/*
376 * handler for deferred kills.
377 */
378static void x25_destroy_timer(unsigned long data)
379{
David S. Miller14ebaf82009-06-16 05:40:30 -0700380 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/*
384 * This is called from user mode and the timers. Thus it protects itself
385 * against interrupt users but doesn't worry about being called during
386 * work. Once it is removed from the queue no interrupt or bottom half
387 * will touch it and we are (fairly 8-) ) safe.
388 * Not static as it's used by the timer
389 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700390static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct sk_buff *skb;
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 x25_stop_heartbeat(sk);
395 x25_stop_timer(sk);
396
397 x25_remove_socket(sk);
398 x25_clear_queues(sk); /* Flush the queues */
399
400 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
401 if (skb->sk != sk) { /* A pending connection */
402 /*
403 * Queue the unaccepted socket for death
404 */
andrew hendry2cec6b02010-04-17 14:17:32 +0000405 skb->sk->sk_state = TCP_LISTEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 sock_set_flag(skb->sk, SOCK_DEAD);
407 x25_start_heartbeat(skb->sk);
408 x25_sk(skb->sk)->state = X25_STATE_0;
409 }
410
411 kfree_skb(skb);
412 }
413
Eric Dumazetc5640392009-06-16 10:12:03 +0000414 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Defer: outstanding buffers */
416 sk->sk_timer.expires = jiffies + 10 * HZ;
417 sk->sk_timer.function = x25_destroy_timer;
418 sk->sk_timer.data = (unsigned long)sk;
419 add_timer(&sk->sk_timer);
420 } else {
421 /* drop last reference so sock_put will free */
422 __sock_put(sk);
423 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700424}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David S. Miller14ebaf82009-06-16 05:40:30 -0700426void x25_destroy_socket_from_timer(struct sock *sk)
427{
428 sock_hold(sk);
429 bh_lock_sock(sk);
430 __x25_destroy_socket(sk);
431 bh_unlock_sock(sk);
432 sock_put(sk);
433}
434
435static void x25_destroy_socket(struct sock *sk)
436{
437 sock_hold(sk);
438 lock_sock(sk);
439 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 release_sock(sk);
441 sock_put(sk);
442}
443
444/*
445 * Handling for system calls applied via the various interfaces to a
446 * X.25 socket object.
447 */
448
449static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700450 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int opt;
453 struct sock *sk = sock->sk;
454 int rc = -ENOPROTOOPT;
455
Arnd Bergmann91774902009-11-05 04:37:29 +0000456 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (level != SOL_X25 || optname != X25_QBITINCL)
458 goto out;
459
460 rc = -EINVAL;
461 if (optlen < sizeof(int))
462 goto out;
463
464 rc = -EFAULT;
465 if (get_user(opt, (int __user *)optval))
466 goto out;
467
andrew hendrycb863ff2010-05-16 22:59:41 +0000468 if (opt)
469 set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
470 else
471 clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 rc = 0;
473out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000474 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return rc;
476}
477
478static int x25_getsockopt(struct socket *sock, int level, int optname,
479 char __user *optval, int __user *optlen)
480{
481 struct sock *sk = sock->sk;
482 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900483
Arnd Bergmann91774902009-11-05 04:37:29 +0000484 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (level != SOL_X25 || optname != X25_QBITINCL)
486 goto out;
487
488 rc = -EFAULT;
489 if (get_user(len, optlen))
490 goto out;
491
492 len = min_t(unsigned int, len, sizeof(int));
493
494 rc = -EINVAL;
495 if (len < 0)
496 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 rc = -EFAULT;
499 if (put_user(len, optlen))
500 goto out;
501
andrew hendrycb863ff2010-05-16 22:59:41 +0000502 val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
504out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000505 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return rc;
507}
508
509static int x25_listen(struct socket *sock, int backlog)
510{
511 struct sock *sk = sock->sk;
512 int rc = -EOPNOTSUPP;
513
Arnd Bergmann91774902009-11-05 04:37:29 +0000514 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (sk->sk_state != TCP_LISTEN) {
516 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
517 sk->sk_max_ack_backlog = backlog;
518 sk->sk_state = TCP_LISTEN;
519 rc = 0;
520 }
Arnd Bergmann91774902009-11-05 04:37:29 +0000521 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 return rc;
524}
525
526static struct proto x25_proto = {
527 .name = "X25",
528 .owner = THIS_MODULE,
529 .obj_size = sizeof(struct x25_sock),
530};
531
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700532static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700535 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 if (!sk)
538 goto out;
539
540 sock_init_data(NULL, sk);
541
542 x25 = x25_sk(sk);
543 skb_queue_head_init(&x25->ack_queue);
544 skb_queue_head_init(&x25->fragment_queue);
545 skb_queue_head_init(&x25->interrupt_in_queue);
546 skb_queue_head_init(&x25->interrupt_out_queue);
547out:
548 return sk;
549}
550
Eric Paris3f378b62009-11-05 22:18:14 -0800551static int x25_create(struct net *net, struct socket *sock, int protocol,
552 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
554 struct sock *sk;
555 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000556 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800558 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 goto out;
560
andrew hendryb18e7a02010-02-14 02:00:11 +0000561 rc = -ESOCKTNOSUPPORT;
562 if (sock->type != SOCK_SEQPACKET)
563 goto out;
564
565 rc = -EINVAL;
566 if (protocol)
567 goto out;
568
569 rc = -ENOBUFS;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700570 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 goto out;
572
573 x25 = x25_sk(sk);
574
575 sock_init_data(sock, sk);
576
577 x25_init_timers(sk);
578
579 sock->ops = &x25_proto_ops;
580 sk->sk_protocol = protocol;
581 sk->sk_backlog_rcv = x25_backlog_rcv;
582
583 x25->t21 = sysctl_x25_call_request_timeout;
584 x25->t22 = sysctl_x25_reset_request_timeout;
585 x25->t23 = sysctl_x25_clear_request_timeout;
586 x25->t2 = sysctl_x25_ack_holdback_timeout;
587 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700588 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700589 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
590 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
593 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
594 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
595 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
John Hughesddd04512010-04-04 06:48:10 +0000596 x25->facilities.throughput = 0; /* by default don't negotiate
597 throughput */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900599 x25->dte_facilities.calling_len = 0;
600 x25->dte_facilities.called_len = 0;
601 memset(x25->dte_facilities.called_ae, '\0',
602 sizeof(x25->dte_facilities.called_ae));
603 memset(x25->dte_facilities.calling_ae, '\0',
604 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 rc = 0;
607out:
608 return rc;
609}
610
611static struct sock *x25_make_new(struct sock *osk)
612{
613 struct sock *sk = NULL;
614 struct x25_sock *x25, *ox25;
615
616 if (osk->sk_type != SOCK_SEQPACKET)
617 goto out;
618
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900619 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 goto out;
621
622 x25 = x25_sk(sk);
623
624 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sk->sk_priority = osk->sk_priority;
626 sk->sk_protocol = osk->sk_protocol;
627 sk->sk_rcvbuf = osk->sk_rcvbuf;
628 sk->sk_sndbuf = osk->sk_sndbuf;
629 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800631 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 ox25 = x25_sk(osk);
634 x25->t21 = ox25->t21;
635 x25->t22 = ox25->t22;
636 x25->t23 = ox25->t23;
637 x25->t2 = ox25->t2;
andrew hendrycb863ff2010-05-16 22:59:41 +0000638 x25->flags = ox25->flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 x25->facilities = ox25->facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800640 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700641 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700642 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 x25_init_timers(sk);
645out:
646 return sk;
647}
648
649static int x25_release(struct socket *sock)
650{
651 struct sock *sk = sock->sk;
652 struct x25_sock *x25;
653
Arnd Bergmann91774902009-11-05 04:37:29 +0000654 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!sk)
656 goto out;
657
658 x25 = x25_sk(sk);
659
660 switch (x25->state) {
661
662 case X25_STATE_0:
663 case X25_STATE_2:
664 x25_disconnect(sk, 0, 0, 0);
665 x25_destroy_socket(sk);
666 goto out;
667
668 case X25_STATE_1:
669 case X25_STATE_3:
670 case X25_STATE_4:
671 x25_clear_queues(sk);
672 x25_write_internal(sk, X25_CLEAR_REQUEST);
673 x25_start_t23timer(sk);
674 x25->state = X25_STATE_2;
675 sk->sk_state = TCP_CLOSE;
676 sk->sk_shutdown |= SEND_SHUTDOWN;
677 sk->sk_state_change(sk);
678 sock_set_flag(sk, SOCK_DEAD);
679 sock_set_flag(sk, SOCK_DESTROY);
680 break;
681 }
682
David S. Millerc751e4f2008-06-17 03:05:13 -0700683 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000685 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return 0;
687}
688
689static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
690{
691 struct sock *sk = sock->sk;
692 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
andrew hendrya9288522010-02-14 02:00:45 +0000693 int len, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Arnd Bergmann91774902009-11-05 04:37:29 +0000695 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (!sock_flag(sk, SOCK_ZAPPED) ||
697 addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000698 addr->sx25_family != AF_X25) {
699 rc = -EINVAL;
700 goto out;
701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
andrew hendrya9288522010-02-14 02:00:45 +0000703 len = strlen(addr->sx25_addr.x25_addr);
704 for (i = 0; i < len; i++) {
705 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
706 rc = -EINVAL;
707 goto out;
708 }
709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 x25_sk(sk)->source_addr = addr->sx25_addr;
712 x25_insert_socket(sk);
713 sock_reset_flag(sk, SOCK_ZAPPED);
714 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000715out:
716 unlock_kernel();
717 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720static int x25_wait_for_connection_establishment(struct sock *sk)
721{
722 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900723 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Eric Dumazetaa395142010-04-20 13:03:51 +0000725 add_wait_queue_exclusive(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 for (;;) {
727 __set_current_state(TASK_INTERRUPTIBLE);
728 rc = -ERESTARTSYS;
729 if (signal_pending(current))
730 break;
731 rc = sock_error(sk);
732 if (rc) {
733 sk->sk_socket->state = SS_UNCONNECTED;
734 break;
735 }
736 rc = 0;
737 if (sk->sk_state != TCP_ESTABLISHED) {
738 release_sock(sk);
739 schedule();
740 lock_sock(sk);
741 } else
742 break;
743 }
744 __set_current_state(TASK_RUNNING);
Eric Dumazetaa395142010-04-20 13:03:51 +0000745 remove_wait_queue(sk_sleep(sk), &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return rc;
747}
748
749static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
750 int addr_len, int flags)
751{
752 struct sock *sk = sock->sk;
753 struct x25_sock *x25 = x25_sk(sk);
754 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
755 struct x25_route *rt;
756 int rc = 0;
757
Arnd Bergmann91774902009-11-05 04:37:29 +0000758 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 lock_sock(sk);
760 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
761 sock->state = SS_CONNECTED;
762 goto out; /* Connect completed during a ERESTARTSYS event */
763 }
764
765 rc = -ECONNREFUSED;
766 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
767 sock->state = SS_UNCONNECTED;
768 goto out;
769 }
770
771 rc = -EISCONN; /* No reconnect on a seqpacket socket */
772 if (sk->sk_state == TCP_ESTABLISHED)
773 goto out;
774
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900775 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 sock->state = SS_UNCONNECTED;
777
778 rc = -EINVAL;
779 if (addr_len != sizeof(struct sockaddr_x25) ||
780 addr->sx25_family != AF_X25)
781 goto out;
782
783 rc = -ENETUNREACH;
784 rt = x25_get_route(&addr->sx25_addr);
785 if (!rt)
786 goto out;
787
788 x25->neighbour = x25_get_neigh(rt->dev);
789 if (!x25->neighbour)
790 goto out_put_route;
791
792 x25_limit_facilities(&x25->facilities, x25->neighbour);
793
794 x25->lci = x25_new_lci(x25->neighbour);
795 if (!x25->lci)
796 goto out_put_neigh;
797
798 rc = -EINVAL;
799 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
800 goto out_put_neigh;
801
802 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
803 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
804
805 x25->dest_addr = addr->sx25_addr;
806
807 /* Move to connecting socket, start sending Connect Requests */
808 sock->state = SS_CONNECTING;
809 sk->sk_state = TCP_SYN_SENT;
810
811 x25->state = X25_STATE_1;
812
813 x25_write_internal(sk, X25_CALL_REQUEST);
814
815 x25_start_heartbeat(sk);
816 x25_start_t21timer(sk);
817
818 /* Now the loop */
819 rc = -EINPROGRESS;
820 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
821 goto out_put_neigh;
822
823 rc = x25_wait_for_connection_establishment(sk);
824 if (rc)
825 goto out_put_neigh;
826
827 sock->state = SS_CONNECTED;
828 rc = 0;
829out_put_neigh:
830 if (rc)
831 x25_neigh_put(x25->neighbour);
832out_put_route:
833 x25_route_put(rt);
834out:
835 release_sock(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000836 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 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
Arnd Bergmann91774902009-11-05 04:37:29 +0000876 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!sk || sk->sk_state != TCP_LISTEN)
878 goto out;
879
880 rc = -EOPNOTSUPP;
881 if (sk->sk_type != SOCK_SEQPACKET)
882 goto out;
883
884 lock_sock(sk);
885 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
886 if (rc)
887 goto out2;
888 skb = skb_dequeue(&sk->sk_receive_queue);
889 rc = -EINVAL;
890 if (!skb->sk)
891 goto out2;
892 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700893 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 /* Now attach up the new socket */
896 skb->sk = NULL;
897 kfree_skb(skb);
898 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 newsock->state = SS_CONNECTED;
900 rc = 0;
901out2:
902 release_sock(sk);
903out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000904 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return rc;
906}
907
908static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
909 int *uaddr_len, int peer)
910{
911 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
912 struct sock *sk = sock->sk;
913 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000914 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Arnd Bergmann91774902009-11-05 04:37:29 +0000916 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000918 if (sk->sk_state != TCP_ESTABLISHED) {
919 rc = -ENOTCONN;
920 goto out;
921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 sx25->sx25_addr = x25->dest_addr;
923 } else
924 sx25->sx25_addr = x25->source_addr;
925
926 sx25->sx25_family = AF_X25;
927 *uaddr_len = sizeof(*sx25);
928
Arnd Bergmann91774902009-11-05 04:37:29 +0000929out:
930 unlock_kernel();
931 return rc;
932}
933
934static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
935 poll_table *wait)
936{
937 int rc;
938
939 lock_kernel();
940 rc = datagram_poll(file, sock, wait);
941 unlock_kernel();
942
943 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
947 unsigned int lci)
948{
949 struct sock *sk;
950 struct sock *make;
951 struct x25_sock *makex25;
952 struct x25_address source_addr, dest_addr;
953 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800954 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800955 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 /*
958 * Remove the LCI and frame type.
959 */
960 skb_pull(skb, X25_STD_MIN_LEN);
961
962 /*
963 * Extract the X.25 addresses and convert them to ASCII strings,
964 * and remove them.
John Hughesf5eb9172010-04-07 21:29:25 -0700965 *
966 * Address block is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 */
John Hughesf5eb9172010-04-07 21:29:25 -0700968 addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
969 if (addr_len <= 0)
970 goto out_clear_request;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800971 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 /*
974 * Get the length of the facilities, skip past them for the moment
975 * get the call user data because this is needed to determine
976 * the correct listener
John Hughesf5eb9172010-04-07 21:29:25 -0700977 *
978 * Facilities length is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
John Hughesf5eb9172010-04-07 21:29:25 -0700980 if (skb->len < 1)
981 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 len = skb->data[0] + 1;
John Hughesf5eb9172010-04-07 21:29:25 -0700983 if (skb->len < len)
984 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 skb_pull(skb,len);
986
987 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 * Find a listener for the particular address/cud pair.
989 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700990 sk = x25_find_listener(&source_addr,skb);
991 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800993 if (sk != NULL && sk_acceptq_is_full(sk)) {
994 goto out_sock_put;
995 }
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800998 * We dont have any listeners for this incoming call.
999 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001001 if (sk == NULL) {
1002 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -08001003 if (sysctl_x25_forward &&
1004 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001005 {
1006 /* Call was forwarded, dont process it any more */
1007 kfree_skb(skb);
1008 rc = 1;
1009 goto out;
1010 } else {
1011 /* No listeners, can't forward, clear the call */
1012 goto out_clear_request;
1013 }
1014 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /*
1017 * Try to reach a compromise on the requested facilities.
1018 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001019 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1020 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 goto out_sock_put;
1022
1023 /*
1024 * current neighbour/link might impose additional limits
1025 * on certain facilties
1026 */
1027
1028 x25_limit_facilities(&facilities, nb);
1029
1030 /*
1031 * Try to create a new socket.
1032 */
1033 make = x25_make_new(sk);
1034 if (!make)
1035 goto out_sock_put;
1036
1037 /*
1038 * Remove the facilities
1039 */
1040 skb_pull(skb, len);
1041
1042 skb->sk = make;
1043 make->sk_state = TCP_ESTABLISHED;
1044
1045 makex25 = x25_sk(make);
1046 makex25->lci = lci;
1047 makex25->dest_addr = dest_addr;
1048 makex25->source_addr = source_addr;
1049 makex25->neighbour = nb;
1050 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001051 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001053 /* ensure no reverse facil on accept */
1054 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001055 /* ensure no calling address extension on accept */
1056 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001057 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001059 /* Normally all calls are accepted immediatly */
1060 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1061 x25_write_internal(make, X25_CALL_ACCEPTED);
1062 makex25->state = X25_STATE_3;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Shaun Pereiracb65d502005-06-22 22:15:01 -07001065 /*
1066 * Incoming Call User Data.
1067 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001068 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1069 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 sk->sk_ack_backlog++;
1072
1073 x25_insert_socket(make);
1074
1075 skb_queue_head(&sk->sk_receive_queue, skb);
1076
1077 x25_start_heartbeat(make);
1078
1079 if (!sock_flag(sk, SOCK_DEAD))
1080 sk->sk_data_ready(sk, skb->len);
1081 rc = 1;
1082 sock_put(sk);
1083out:
1084 return rc;
1085out_sock_put:
1086 sock_put(sk);
1087out_clear_request:
1088 rc = 0;
1089 x25_transmit_clear_request(nb, lci, 0x01);
1090 goto out;
1091}
1092
1093static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1094 struct msghdr *msg, size_t len)
1095{
1096 struct sock *sk = sock->sk;
1097 struct x25_sock *x25 = x25_sk(sk);
1098 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1099 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 Bergmann91774902009-11-05 04:37:29 +00001106 lock_kernel();
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
1169 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1170 if (!skb)
1171 goto out;
1172 X25_SKB_CB(skb)->flags = msg->msg_flags;
1173
1174 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1175
1176 /*
1177 * Put the data on the end
1178 */
1179 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1180
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001181 skb_reset_transport_header(skb);
1182 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001184 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (rc)
1186 goto out_kfree_skb;
1187
1188 /*
1189 * If the Q BIT Include socket option is in force, the first
1190 * byte of the user data is the logical value of the Q Bit.
1191 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001192 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 qbit = skb->data[0];
1194 skb_pull(skb, 1);
1195 }
1196
1197 /*
1198 * Push down the X.25 header
1199 */
1200 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1201
1202 if (msg->msg_flags & MSG_OOB) {
1203 if (x25->neighbour->extended) {
1204 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1205 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1206 *asmptr++ = (x25->lci >> 0) & 0xFF;
1207 *asmptr++ = X25_INTERRUPT;
1208 } else {
1209 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1210 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1211 *asmptr++ = (x25->lci >> 0) & 0xFF;
1212 *asmptr++ = X25_INTERRUPT;
1213 }
1214 } else {
1215 if (x25->neighbour->extended) {
1216 /* Build an Extended X.25 header */
1217 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1218 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1219 *asmptr++ = (x25->lci >> 0) & 0xFF;
1220 *asmptr++ = X25_DATA;
1221 *asmptr++ = X25_DATA;
1222 } else {
1223 /* Build an Standard X.25 header */
1224 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1225 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1226 *asmptr++ = (x25->lci >> 0) & 0xFF;
1227 *asmptr++ = X25_DATA;
1228 }
1229
1230 if (qbit)
1231 skb->data[0] |= X25_Q_BIT;
1232 }
1233
1234 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1235 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1236
1237 rc = -ENOTCONN;
1238 if (sk->sk_state != TCP_ESTABLISHED)
1239 goto out_kfree_skb;
1240
1241 if (msg->msg_flags & MSG_OOB)
1242 skb_queue_tail(&x25->interrupt_out_queue, skb);
1243 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001244 rc = x25_output(sk, skb);
1245 len = rc;
1246 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 kfree_skb(skb);
andrew hendrycb863ff2010-05-16 22:59:41 +00001248 else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 len++;
1250 }
1251
1252 /*
1253 * lock_sock() is currently only used to serialize this x25_kick()
1254 * against input-driven x25_kick() calls. It currently only blocks
1255 * incoming packets for this socket and does not protect against
1256 * any other socket state changes and is not called from anywhere
1257 * else. As x25_kick() cannot block and as long as all socket
1258 * operations are BKL-wrapped, we don't need take to care about
1259 * purging the backlog queue in x25_release().
1260 *
1261 * Using lock_sock() to protect all socket operations entirely
1262 * (and making the whole x25 stack SMP aware) unfortunately would
1263 * require major changes to {send,recv}msg and skb allocation methods.
1264 * -> 2.5 ;)
1265 */
1266 lock_sock(sk);
1267 x25_kick(sk);
1268 release_sock(sk);
1269 rc = len;
1270out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001271 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return rc;
1273out_kfree_skb:
1274 kfree_skb(skb);
1275 goto out;
1276}
1277
1278
1279static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1280 struct msghdr *msg, size_t size,
1281 int flags)
1282{
1283 struct sock *sk = sock->sk;
1284 struct x25_sock *x25 = x25_sk(sk);
1285 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1286 size_t copied;
1287 int qbit;
1288 struct sk_buff *skb;
1289 unsigned char *asmptr;
1290 int rc = -ENOTCONN;
1291
Arnd Bergmann91774902009-11-05 04:37:29 +00001292 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 /*
1294 * This works for seqpacket too. The receiver has ordered the queue for
1295 * us! We do one quick check first though
1296 */
1297 if (sk->sk_state != TCP_ESTABLISHED)
1298 goto out;
1299
1300 if (flags & MSG_OOB) {
1301 rc = -EINVAL;
1302 if (sock_flag(sk, SOCK_URGINLINE) ||
1303 !skb_peek(&x25->interrupt_in_queue))
1304 goto out;
1305
1306 skb = skb_dequeue(&x25->interrupt_in_queue);
1307
1308 skb_pull(skb, X25_STD_MIN_LEN);
1309
1310 /*
1311 * No Q bit information on Interrupt data.
1312 */
andrew hendrycb863ff2010-05-16 22:59:41 +00001313 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 asmptr = skb_push(skb, 1);
1315 *asmptr = 0x00;
1316 }
1317
1318 msg->msg_flags |= MSG_OOB;
1319 } else {
1320 /* Now we can treat all alike */
1321 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1322 flags & MSG_DONTWAIT, &rc);
1323 if (!skb)
1324 goto out;
1325
1326 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1327
1328 skb_pull(skb, x25->neighbour->extended ?
1329 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1330
andrew hendrycb863ff2010-05-16 22:59:41 +00001331 if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 asmptr = skb_push(skb, 1);
1333 *asmptr = qbit;
1334 }
1335 }
1336
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001337 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 copied = skb->len;
1339
1340 if (copied > size) {
1341 copied = size;
1342 msg->msg_flags |= MSG_TRUNC;
1343 }
1344
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001345 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 msg->msg_flags |= MSG_EOR;
1347
1348 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1349 if (rc)
1350 goto out_free_dgram;
1351
1352 if (sx25) {
1353 sx25->sx25_family = AF_X25;
1354 sx25->sx25_addr = x25->dest_addr;
1355 }
1356
1357 msg->msg_namelen = sizeof(struct sockaddr_x25);
1358
1359 lock_sock(sk);
1360 x25_check_rbuf(sk);
1361 release_sock(sk);
1362 rc = copied;
1363out_free_dgram:
1364 skb_free_datagram(sk, skb);
1365out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001366 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return rc;
1368}
1369
1370
1371static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1372{
1373 struct sock *sk = sock->sk;
1374 struct x25_sock *x25 = x25_sk(sk);
1375 void __user *argp = (void __user *)arg;
1376 int rc;
1377
Arnd Bergmann91774902009-11-05 04:37:29 +00001378 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 switch (cmd) {
1380 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001381 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (amount < 0)
1384 amount = 0;
1385 rc = put_user(amount, (unsigned int __user *)argp);
1386 break;
1387 }
1388
1389 case TIOCINQ: {
1390 struct sk_buff *skb;
1391 int amount = 0;
1392 /*
1393 * These two are safe on a single CPU system as
1394 * only user tasks fiddle here
1395 */
1396 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1397 amount = skb->len;
1398 rc = put_user(amount, (unsigned int __user *)argp);
1399 break;
1400 }
1401
1402 case SIOCGSTAMP:
1403 rc = -EINVAL;
1404 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001405 rc = sock_get_timestamp(sk,
1406 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001408 case SIOCGSTAMPNS:
1409 rc = -EINVAL;
1410 if (sk)
1411 rc = sock_get_timestampns(sk,
1412 (struct timespec __user *)argp);
1413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 case SIOCGIFADDR:
1415 case SIOCSIFADDR:
1416 case SIOCGIFDSTADDR:
1417 case SIOCSIFDSTADDR:
1418 case SIOCGIFBRDADDR:
1419 case SIOCSIFBRDADDR:
1420 case SIOCGIFNETMASK:
1421 case SIOCSIFNETMASK:
1422 case SIOCGIFMETRIC:
1423 case SIOCSIFMETRIC:
1424 rc = -EINVAL;
1425 break;
1426 case SIOCADDRT:
1427 case SIOCDELRT:
1428 rc = -EPERM;
1429 if (!capable(CAP_NET_ADMIN))
1430 break;
1431 rc = x25_route_ioctl(cmd, argp);
1432 break;
1433 case SIOCX25GSUBSCRIP:
1434 rc = x25_subscr_ioctl(cmd, argp);
1435 break;
1436 case SIOCX25SSUBSCRIP:
1437 rc = -EPERM;
1438 if (!capable(CAP_NET_ADMIN))
1439 break;
1440 rc = x25_subscr_ioctl(cmd, argp);
1441 break;
1442 case SIOCX25GFACILITIES: {
1443 struct x25_facilities fac = x25->facilities;
1444 rc = copy_to_user(argp, &fac,
1445 sizeof(fac)) ? -EFAULT : 0;
1446 break;
1447 }
1448
1449 case SIOCX25SFACILITIES: {
1450 struct x25_facilities facilities;
1451 rc = -EFAULT;
1452 if (copy_from_user(&facilities, argp,
1453 sizeof(facilities)))
1454 break;
1455 rc = -EINVAL;
1456 if (sk->sk_state != TCP_LISTEN &&
1457 sk->sk_state != TCP_CLOSE)
1458 break;
1459 if (facilities.pacsize_in < X25_PS16 ||
1460 facilities.pacsize_in > X25_PS4096)
1461 break;
1462 if (facilities.pacsize_out < X25_PS16 ||
1463 facilities.pacsize_out > X25_PS4096)
1464 break;
1465 if (facilities.winsize_in < 1 ||
1466 facilities.winsize_in > 127)
1467 break;
John Hughesddd04512010-04-04 06:48:10 +00001468 if (facilities.throughput) {
1469 int out = facilities.throughput & 0xf0;
1470 int in = facilities.throughput & 0x0f;
1471 if (!out)
1472 facilities.throughput |=
1473 X25_DEFAULT_THROUGHPUT << 4;
1474 else if (out < 0x30 || out > 0xD0)
1475 break;
1476 if (!in)
1477 facilities.throughput |=
1478 X25_DEFAULT_THROUGHPUT;
1479 else if (in < 0x03 || in > 0x0D)
1480 break;
1481 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001482 if (facilities.reverse &&
roel kluin06a96b32009-10-07 00:59:42 +00001483 (facilities.reverse & 0x81) != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 break;
1485 x25->facilities = facilities;
1486 rc = 0;
1487 break;
1488 }
1489
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001490 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001491 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001492 sizeof(x25->dte_facilities));
1493 if (rc)
1494 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001495 break;
1496 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001497
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001498 case SIOCX25SDTEFACILITIES: {
1499 struct x25_dte_facilities dtefacs;
1500 rc = -EFAULT;
1501 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001502 break;
1503 rc = -EINVAL;
1504 if (sk->sk_state != TCP_LISTEN &&
1505 sk->sk_state != TCP_CLOSE)
1506 break;
1507 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1508 break;
1509 if (dtefacs.calling_ae == NULL)
1510 break;
1511 if (dtefacs.called_len > X25_MAX_AE_LEN)
1512 break;
1513 if (dtefacs.called_ae == NULL)
1514 break;
1515 x25->dte_facilities = dtefacs;
1516 rc = 0;
1517 break;
1518 }
1519
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 case SIOCX25GCALLUSERDATA: {
1521 struct x25_calluserdata cud = x25->calluserdata;
1522 rc = copy_to_user(argp, &cud,
1523 sizeof(cud)) ? -EFAULT : 0;
1524 break;
1525 }
1526
1527 case SIOCX25SCALLUSERDATA: {
1528 struct x25_calluserdata calluserdata;
1529
1530 rc = -EFAULT;
1531 if (copy_from_user(&calluserdata, argp,
1532 sizeof(calluserdata)))
1533 break;
1534 rc = -EINVAL;
1535 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1536 break;
1537 x25->calluserdata = calluserdata;
1538 rc = 0;
1539 break;
1540 }
1541
1542 case SIOCX25GCAUSEDIAG: {
1543 struct x25_causediag causediag;
1544 causediag = x25->causediag;
1545 rc = copy_to_user(argp, &causediag,
1546 sizeof(causediag)) ? -EFAULT : 0;
1547 break;
1548 }
1549
Andrew Hendry386e50c2009-11-18 23:30:41 -08001550 case SIOCX25SCAUSEDIAG: {
1551 struct x25_causediag causediag;
1552 rc = -EFAULT;
1553 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1554 break;
1555 x25->causediag = causediag;
1556 rc = 0;
1557 break;
1558
1559 }
1560
Shaun Pereiracb65d502005-06-22 22:15:01 -07001561 case SIOCX25SCUDMATCHLEN: {
1562 struct x25_subaddr sub_addr;
1563 rc = -EINVAL;
1564 if(sk->sk_state != TCP_CLOSE)
1565 break;
1566 rc = -EFAULT;
1567 if (copy_from_user(&sub_addr, argp,
1568 sizeof(sub_addr)))
1569 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001570 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001571 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1572 break;
1573 x25->cudmatchlength = sub_addr.cudmatchlength;
1574 rc = 0;
1575 break;
1576 }
1577
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001578 case SIOCX25CALLACCPTAPPRV: {
1579 rc = -EINVAL;
1580 if (sk->sk_state != TCP_CLOSE)
1581 break;
1582 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1583 rc = 0;
1584 break;
1585 }
1586
1587 case SIOCX25SENDCALLACCPT: {
1588 rc = -EINVAL;
1589 if (sk->sk_state != TCP_ESTABLISHED)
1590 break;
1591 if (x25->accptapprv) /* must call accptapprv above */
1592 break;
1593 x25_write_internal(sk, X25_CALL_ACCEPTED);
1594 x25->state = X25_STATE_3;
1595 rc = 0;
1596 break;
1597 }
1598
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001599 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001600 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 break;
1602 }
Arnd Bergmann91774902009-11-05 04:37:29 +00001603 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
1605 return rc;
1606}
1607
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001608static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 .family = AF_X25,
1610 .create = x25_create,
1611 .owner = THIS_MODULE,
1612};
1613
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001614#ifdef CONFIG_COMPAT
1615static int compat_x25_subscr_ioctl(unsigned int cmd,
1616 struct compat_x25_subscrip_struct __user *x25_subscr32)
1617{
1618 struct compat_x25_subscrip_struct x25_subscr;
1619 struct x25_neigh *nb;
1620 struct net_device *dev;
1621 int rc = -EINVAL;
1622
1623 rc = -EFAULT;
1624 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1625 goto out;
1626
1627 rc = -EINVAL;
1628 dev = x25_dev_get(x25_subscr.device);
1629 if (dev == NULL)
1630 goto out;
1631
1632 nb = x25_get_neigh(dev);
1633 if (nb == NULL)
1634 goto out_dev_put;
1635
1636 dev_put(dev);
1637
1638 if (cmd == SIOCX25GSUBSCRIP) {
1639 x25_subscr.extended = nb->extended;
1640 x25_subscr.global_facil_mask = nb->global_facil_mask;
1641 rc = copy_to_user(x25_subscr32, &x25_subscr,
1642 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1643 } else {
1644 rc = -EINVAL;
1645 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1646 rc = 0;
1647 nb->extended = x25_subscr.extended;
1648 nb->global_facil_mask = x25_subscr.global_facil_mask;
1649 }
1650 }
1651 x25_neigh_put(nb);
1652out:
1653 return rc;
1654out_dev_put:
1655 dev_put(dev);
1656 goto out;
1657}
1658
1659static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1660 unsigned long arg)
1661{
1662 void __user *argp = compat_ptr(arg);
1663 struct sock *sk = sock->sk;
1664
1665 int rc = -ENOIOCTLCMD;
1666
1667 switch(cmd) {
1668 case TIOCOUTQ:
1669 case TIOCINQ:
1670 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1671 break;
1672 case SIOCGSTAMP:
1673 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001674 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001675 if (sk)
1676 rc = compat_sock_get_timestamp(sk,
1677 (struct timeval __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001678 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001679 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001680 case SIOCGSTAMPNS:
1681 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001682 lock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001683 if (sk)
1684 rc = compat_sock_get_timestampns(sk,
1685 (struct timespec __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001686 unlock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001687 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001688 case SIOCGIFADDR:
1689 case SIOCSIFADDR:
1690 case SIOCGIFDSTADDR:
1691 case SIOCSIFDSTADDR:
1692 case SIOCGIFBRDADDR:
1693 case SIOCSIFBRDADDR:
1694 case SIOCGIFNETMASK:
1695 case SIOCSIFNETMASK:
1696 case SIOCGIFMETRIC:
1697 case SIOCSIFMETRIC:
1698 rc = -EINVAL;
1699 break;
1700 case SIOCADDRT:
1701 case SIOCDELRT:
1702 rc = -EPERM;
1703 if (!capable(CAP_NET_ADMIN))
1704 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001705 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001706 rc = x25_route_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001707 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001708 break;
1709 case SIOCX25GSUBSCRIP:
Arnd Bergmann91774902009-11-05 04:37:29 +00001710 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001711 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001712 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001713 break;
1714 case SIOCX25SSUBSCRIP:
1715 rc = -EPERM;
1716 if (!capable(CAP_NET_ADMIN))
1717 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001718 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001719 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001720 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001721 break;
1722 case SIOCX25GFACILITIES:
1723 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001724 case SIOCX25GDTEFACILITIES:
1725 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001726 case SIOCX25GCALLUSERDATA:
1727 case SIOCX25SCALLUSERDATA:
1728 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001729 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001730 case SIOCX25SCUDMATCHLEN:
1731 case SIOCX25CALLACCPTAPPRV:
1732 case SIOCX25SENDCALLACCPT:
1733 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1734 break;
1735 default:
1736 rc = -ENOIOCTLCMD;
1737 break;
1738 }
1739 return rc;
1740}
1741#endif
1742
Arnd Bergmann91774902009-11-05 04:37:29 +00001743static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 .family = AF_X25,
1745 .owner = THIS_MODULE,
1746 .release = x25_release,
1747 .bind = x25_bind,
1748 .connect = x25_connect,
1749 .socketpair = sock_no_socketpair,
1750 .accept = x25_accept,
1751 .getname = x25_getname,
Arnd Bergmann91774902009-11-05 04:37:29 +00001752 .poll = x25_datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001754#ifdef CONFIG_COMPAT
1755 .compat_ioctl = compat_x25_ioctl,
1756#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .listen = x25_listen,
1758 .shutdown = sock_no_shutdown,
1759 .setsockopt = x25_setsockopt,
1760 .getsockopt = x25_getsockopt,
1761 .sendmsg = x25_sendmsg,
1762 .recvmsg = x25_recvmsg,
1763 .mmap = sock_no_mmap,
1764 .sendpage = sock_no_sendpage,
1765};
1766
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001767static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001768 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 .func = x25_lapb_receive_frame,
1770};
1771
1772static struct notifier_block x25_dev_notifier = {
1773 .notifier_call = x25_device_event,
1774};
1775
1776void x25_kill_by_neigh(struct x25_neigh *nb)
1777{
1778 struct sock *s;
1779 struct hlist_node *node;
1780
1781 write_lock_bh(&x25_list_lock);
1782
1783 sk_for_each(s, node, &x25_list)
1784 if (x25_sk(s)->neighbour == nb)
1785 x25_disconnect(s, ENETUNREACH, 0, 0);
1786
1787 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001788
1789 /* Remove any related forwards */
1790 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
1793static int __init x25_init(void)
1794{
1795 int rc = proto_register(&x25_proto, 0);
1796
1797 if (rc != 0)
1798 goto out;
1799
andrew hendry1fd975a2009-11-24 15:15:42 +00001800 rc = sock_register(&x25_family_ops);
1801 if (rc != 0)
1802 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 dev_add_pack(&x25_packet_type);
1805
andrew hendry1fd975a2009-11-24 15:15:42 +00001806 rc = register_netdevice_notifier(&x25_dev_notifier);
1807 if (rc != 0)
1808 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
maximilian attemsa44562e2008-01-28 20:43:16 -08001810 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 x25_register_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001813 rc = x25_proc_init();
1814 if (rc != 0)
1815 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816out:
1817 return rc;
andrew hendry1fd975a2009-11-24 15:15:42 +00001818out_dev:
1819 unregister_netdevice_notifier(&x25_dev_notifier);
1820out_sock:
1821 sock_unregister(AF_X25);
1822out_proto:
1823 proto_unregister(&x25_proto);
1824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826module_init(x25_init);
1827
1828static void __exit x25_exit(void)
1829{
1830 x25_proc_exit();
1831 x25_link_free();
1832 x25_route_free();
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 unregister_netdevice_notifier(&x25_dev_notifier);
1837
1838 dev_remove_pack(&x25_packet_type);
1839
1840 sock_unregister(AF_X25);
1841 proto_unregister(&x25_proto);
1842}
1843module_exit(x25_exit);
1844
1845MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1846MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1847MODULE_LICENSE("GPL");
1848MODULE_ALIAS_NETPROTO(PF_X25);