blob: 8ed51c926c5036989f107a2c6d0789b62f6f1499 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * X.25 Packet Layer release 002
3 *
4 * This is ALPHA test software. This code may break your machine,
5 * randomly fail to work with new releases, misbehave and/or generally
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09006 * screw up. It might even work.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This code REQUIRES 2.1.15 or higher
9 *
10 * This module:
11 * This module is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
15 *
16 * History
17 * X.25 001 Jonathan Naylor Started coding.
18 * X.25 002 Jonathan Naylor Centralised disconnect handling.
19 * New timer architecture.
20 * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090021 * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
22 * facilities negotiation and increased
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * the throughput upper limit.
24 * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +090025 * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * Fixed x25_output() related skb leakage.
27 * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
28 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
29 * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN
30 * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
31 * x25_proc.c, using seq_file
Shaun Pereiracb65d502005-06-22 22:15:01 -070032 * 2005-04-02 Shaun Pereira Selective sub address matching
33 * with call user data
Shaun Pereiraebc3f642005-06-22 22:16:17 -070034 * 2005-04-15 Shaun Pereira Fast select with no restriction on
35 * response
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/module.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080039#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/errno.h>
41#include <linux/kernel.h>
42#include <linux/sched.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040043#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/timer.h>
45#include <linux/string.h>
46#include <linux/net.h>
47#include <linux/netdevice.h>
48#include <linux/if_arp.h>
49#include <linux/skbuff.h>
50#include <net/sock.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070051#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/uaccess.h>
53#include <linux/fcntl.h>
54#include <linux/termios.h> /* For TIOCINQ/OUTQ */
55#include <linux/notifier.h>
56#include <linux/init.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080057#include <linux/compat.h>
andrew hendrya9288522010-02-14 02:00:45 +000058#include <linux/ctype.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080061#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
64int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
65int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
66int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
67int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080068int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70HLIST_HEAD(x25_list);
71DEFINE_RWLOCK(x25_list_lock);
72
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080073static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct x25_address null_x25_address = {" "};
76
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080077#ifdef CONFIG_COMPAT
78struct compat_x25_subscrip_struct {
79 char device[200-sizeof(compat_ulong_t)];
80 compat_ulong_t global_facil_mask;
81 compat_uint_t extended;
82};
83#endif
84
John Hughesf5eb9172010-04-07 21:29:25 -070085
86int x25_parse_address_block(struct sk_buff *skb,
87 struct x25_address *called_addr,
88 struct x25_address *calling_addr)
89{
90 unsigned char len;
91 int needed;
92 int rc;
93
94 if (skb->len < 1) {
95 /* packet has no address block */
96 rc = 0;
97 goto empty;
98 }
99
100 len = *skb->data;
101 needed = 1 + (len >> 4) + (len & 0x0f);
102
103 if (skb->len < needed) {
104 /* packet is too short to hold the addresses it claims
105 to hold */
106 rc = -1;
107 goto empty;
108 }
109
110 return x25_addr_ntoa(skb->data, called_addr, calling_addr);
111
112empty:
113 *called_addr->x25_addr = 0;
114 *calling_addr->x25_addr = 0;
115
116 return rc;
117}
118
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
121 struct x25_address *calling_addr)
122{
Eric Dumazet6bf15742008-01-13 22:27:52 -0800123 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -0800125 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 called_len = (*p >> 0) & 0x0F;
128 calling_len = (*p >> 4) & 0x0F;
129
130 called = called_addr->x25_addr;
131 calling = calling_addr->x25_addr;
132 p++;
133
134 for (i = 0; i < (called_len + calling_len); i++) {
135 if (i < called_len) {
136 if (i % 2 != 0) {
137 *called++ = ((*p >> 0) & 0x0F) + '0';
138 p++;
139 } else {
140 *called++ = ((*p >> 4) & 0x0F) + '0';
141 }
142 } else {
143 if (i % 2 != 0) {
144 *calling++ = ((*p >> 0) & 0x0F) + '0';
145 p++;
146 } else {
147 *calling++ = ((*p >> 4) & 0x0F) + '0';
148 }
149 }
150 }
151
152 *called = *calling = '\0';
153
154 return 1 + (called_len + calling_len + 1) / 2;
155}
156
157int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
158 struct x25_address *calling_addr)
159{
160 unsigned int called_len, calling_len;
161 char *called, *calling;
162 int i;
163
164 called = called_addr->x25_addr;
165 calling = calling_addr->x25_addr;
166
167 called_len = strlen(called);
168 calling_len = strlen(calling);
169
170 *p++ = (calling_len << 4) | (called_len << 0);
171
172 for (i = 0; i < (called_len + calling_len); i++) {
173 if (i < called_len) {
174 if (i % 2 != 0) {
175 *p |= (*called++ - '0') << 0;
176 p++;
177 } else {
178 *p = 0x00;
179 *p |= (*called++ - '0') << 4;
180 }
181 } else {
182 if (i % 2 != 0) {
183 *p |= (*calling++ - '0') << 0;
184 p++;
185 } else {
186 *p = 0x00;
187 *p |= (*calling++ - '0') << 4;
188 }
189 }
190 }
191
192 return 1 + (called_len + calling_len + 1) / 2;
193}
194
195/*
196 * Socket removal during an interrupt is now safe.
197 */
198static void x25_remove_socket(struct sock *sk)
199{
200 write_lock_bh(&x25_list_lock);
201 sk_del_node_init(sk);
202 write_unlock_bh(&x25_list_lock);
203}
204
205/*
206 * Kill all bound sockets on a dropped device.
207 */
208static void x25_kill_by_device(struct net_device *dev)
209{
210 struct sock *s;
211 struct hlist_node *node;
212
213 write_lock_bh(&x25_list_lock);
214
215 sk_for_each(s, node, &x25_list)
216 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
217 x25_disconnect(s, ENETUNREACH, 0, 0);
218
219 write_unlock_bh(&x25_list_lock);
220}
221
222/*
223 * Handle device status changes.
224 */
225static int x25_device_event(struct notifier_block *this, unsigned long event,
226 void *ptr)
227{
228 struct net_device *dev = ptr;
229 struct x25_neigh *nb;
230
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700231 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200232 return NOTIFY_DONE;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (dev->type == ARPHRD_X25
235#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
236 || dev->type == ARPHRD_ETHER
237#endif
238 ) {
239 switch (event) {
240 case NETDEV_UP:
241 x25_link_device_up(dev);
242 break;
243 case NETDEV_GOING_DOWN:
244 nb = x25_get_neigh(dev);
245 if (nb) {
246 x25_terminate_link(nb);
247 x25_neigh_put(nb);
248 }
249 break;
250 case NETDEV_DOWN:
251 x25_kill_by_device(dev);
252 x25_route_device_down(dev);
253 x25_link_device_down(dev);
254 break;
255 }
256 }
257
258 return NOTIFY_DONE;
259}
260
261/*
262 * Add a socket to the bound sockets list.
263 */
264static void x25_insert_socket(struct sock *sk)
265{
266 write_lock_bh(&x25_list_lock);
267 sk_add_node(sk, &x25_list);
268 write_unlock_bh(&x25_list_lock);
269}
270
271/*
272 * Find a socket that wants to accept the Call Request we just
273 * received. Check the full list for an address/cud match.
274 * If no cuds match return the next_best thing, an address match.
275 * Note: if a listening socket has cud set it must only get calls
276 * with matching cud.
277 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700278static struct sock *x25_find_listener(struct x25_address *addr,
279 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct sock *s;
282 struct sock *next_best;
283 struct hlist_node *node;
284
285 read_lock_bh(&x25_list_lock);
286 next_best = NULL;
287
288 sk_for_each(s, node, &x25_list)
289 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700290 x25_sk(s)->source_addr.x25_addr) ||
291 !strcmp(addr->x25_addr,
292 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 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700298 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900299 if((memcmp(x25_sk(s)->calluserdata.cuddata,
300 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700301 x25_sk(s)->cudmatchlength)) == 0) {
302 sock_hold(s);
303 goto found;
304 }
305 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 if (next_best) {
309 s = next_best;
310 sock_hold(s);
311 goto found;
312 }
313 s = NULL;
314found:
315 read_unlock_bh(&x25_list_lock);
316 return s;
317}
318
319/*
320 * Find a connected X.25 socket given my LCI and neighbour.
321 */
322static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
323{
324 struct sock *s;
325 struct hlist_node *node;
326
327 sk_for_each(s, node, &x25_list)
328 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
355 read_lock_bh(&x25_list_lock);
356
357 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
358 sock_put(sk);
359 if (++lci == 4096) {
360 lci = 0;
361 break;
362 }
363 }
364
365 read_unlock_bh(&x25_list_lock);
366 return lci;
367}
368
369/*
370 * Deferred destroy.
371 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700372static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/*
375 * handler for deferred kills.
376 */
377static void x25_destroy_timer(unsigned long data)
378{
David S. Miller14ebaf82009-06-16 05:40:30 -0700379 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382/*
383 * This is called from user mode and the timers. Thus it protects itself
384 * against interrupt users but doesn't worry about being called during
385 * work. Once it is removed from the queue no interrupt or bottom half
386 * will touch it and we are (fairly 8-) ) safe.
387 * Not static as it's used by the timer
388 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700389static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct sk_buff *skb;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 x25_stop_heartbeat(sk);
394 x25_stop_timer(sk);
395
396 x25_remove_socket(sk);
397 x25_clear_queues(sk); /* Flush the queues */
398
399 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
400 if (skb->sk != sk) { /* A pending connection */
401 /*
402 * Queue the unaccepted socket for death
403 */
404 sock_set_flag(skb->sk, SOCK_DEAD);
405 x25_start_heartbeat(skb->sk);
406 x25_sk(skb->sk)->state = X25_STATE_0;
407 }
408
409 kfree_skb(skb);
410 }
411
Eric Dumazetc5640392009-06-16 10:12:03 +0000412 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 /* Defer: outstanding buffers */
414 sk->sk_timer.expires = jiffies + 10 * HZ;
415 sk->sk_timer.function = x25_destroy_timer;
416 sk->sk_timer.data = (unsigned long)sk;
417 add_timer(&sk->sk_timer);
418 } else {
419 /* drop last reference so sock_put will free */
420 __sock_put(sk);
421 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700422}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David S. Miller14ebaf82009-06-16 05:40:30 -0700424void x25_destroy_socket_from_timer(struct sock *sk)
425{
426 sock_hold(sk);
427 bh_lock_sock(sk);
428 __x25_destroy_socket(sk);
429 bh_unlock_sock(sk);
430 sock_put(sk);
431}
432
433static void x25_destroy_socket(struct sock *sk)
434{
435 sock_hold(sk);
436 lock_sock(sk);
437 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 release_sock(sk);
439 sock_put(sk);
440}
441
442/*
443 * Handling for system calls applied via the various interfaces to a
444 * X.25 socket object.
445 */
446
447static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700448 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 int opt;
451 struct sock *sk = sock->sk;
452 int rc = -ENOPROTOOPT;
453
Arnd Bergmann91774902009-11-05 04:37:29 +0000454 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (level != SOL_X25 || optname != X25_QBITINCL)
456 goto out;
457
458 rc = -EINVAL;
459 if (optlen < sizeof(int))
460 goto out;
461
462 rc = -EFAULT;
463 if (get_user(opt, (int __user *)optval))
464 goto out;
465
466 x25_sk(sk)->qbitincl = !!opt;
467 rc = 0;
468out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000469 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return rc;
471}
472
473static int x25_getsockopt(struct socket *sock, int level, int optname,
474 char __user *optval, int __user *optlen)
475{
476 struct sock *sk = sock->sk;
477 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900478
Arnd Bergmann91774902009-11-05 04:37:29 +0000479 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (level != SOL_X25 || optname != X25_QBITINCL)
481 goto out;
482
483 rc = -EFAULT;
484 if (get_user(len, optlen))
485 goto out;
486
487 len = min_t(unsigned int, len, sizeof(int));
488
489 rc = -EINVAL;
490 if (len < 0)
491 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 rc = -EFAULT;
494 if (put_user(len, optlen))
495 goto out;
496
497 val = x25_sk(sk)->qbitincl;
498 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
499out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000500 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return rc;
502}
503
504static int x25_listen(struct socket *sock, int backlog)
505{
506 struct sock *sk = sock->sk;
507 int rc = -EOPNOTSUPP;
508
Arnd Bergmann91774902009-11-05 04:37:29 +0000509 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (sk->sk_state != TCP_LISTEN) {
511 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
512 sk->sk_max_ack_backlog = backlog;
513 sk->sk_state = TCP_LISTEN;
514 rc = 0;
515 }
Arnd Bergmann91774902009-11-05 04:37:29 +0000516 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 return rc;
519}
520
521static struct proto x25_proto = {
522 .name = "X25",
523 .owner = THIS_MODULE,
524 .obj_size = sizeof(struct x25_sock),
525};
526
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700527static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
529 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700530 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (!sk)
533 goto out;
534
535 sock_init_data(NULL, sk);
536
537 x25 = x25_sk(sk);
538 skb_queue_head_init(&x25->ack_queue);
539 skb_queue_head_init(&x25->fragment_queue);
540 skb_queue_head_init(&x25->interrupt_in_queue);
541 skb_queue_head_init(&x25->interrupt_out_queue);
542out:
543 return sk;
544}
545
Eric Paris3f378b62009-11-05 22:18:14 -0800546static int x25_create(struct net *net, struct socket *sock, int protocol,
547 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 struct sock *sk;
550 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000551 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800553 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 goto out;
555
andrew hendryb18e7a02010-02-14 02:00:11 +0000556 rc = -ESOCKTNOSUPPORT;
557 if (sock->type != SOCK_SEQPACKET)
558 goto out;
559
560 rc = -EINVAL;
561 if (protocol)
562 goto out;
563
564 rc = -ENOBUFS;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700565 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto out;
567
568 x25 = x25_sk(sk);
569
570 sock_init_data(sock, sk);
571
572 x25_init_timers(sk);
573
574 sock->ops = &x25_proto_ops;
575 sk->sk_protocol = protocol;
576 sk->sk_backlog_rcv = x25_backlog_rcv;
577
578 x25->t21 = sysctl_x25_call_request_timeout;
579 x25->t22 = sysctl_x25_reset_request_timeout;
580 x25->t23 = sysctl_x25_clear_request_timeout;
581 x25->t2 = sysctl_x25_ack_holdback_timeout;
582 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700583 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700584 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
585 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
588 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
589 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
590 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
John Hughesddd04512010-04-04 06:48:10 +0000591 x25->facilities.throughput = 0; /* by default don't negotiate
592 throughput */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900594 x25->dte_facilities.calling_len = 0;
595 x25->dte_facilities.called_len = 0;
596 memset(x25->dte_facilities.called_ae, '\0',
597 sizeof(x25->dte_facilities.called_ae));
598 memset(x25->dte_facilities.calling_ae, '\0',
599 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 rc = 0;
602out:
603 return rc;
604}
605
606static struct sock *x25_make_new(struct sock *osk)
607{
608 struct sock *sk = NULL;
609 struct x25_sock *x25, *ox25;
610
611 if (osk->sk_type != SOCK_SEQPACKET)
612 goto out;
613
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900614 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 goto out;
616
617 x25 = x25_sk(sk);
618
619 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 sk->sk_priority = osk->sk_priority;
621 sk->sk_protocol = osk->sk_protocol;
622 sk->sk_rcvbuf = osk->sk_rcvbuf;
623 sk->sk_sndbuf = osk->sk_sndbuf;
624 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800626 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 ox25 = x25_sk(osk);
629 x25->t21 = ox25->t21;
630 x25->t22 = ox25->t22;
631 x25->t23 = ox25->t23;
632 x25->t2 = ox25->t2;
633 x25->facilities = ox25->facilities;
634 x25->qbitincl = ox25->qbitincl;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800635 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700636 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700637 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 x25_init_timers(sk);
640out:
641 return sk;
642}
643
644static int x25_release(struct socket *sock)
645{
646 struct sock *sk = sock->sk;
647 struct x25_sock *x25;
648
Arnd Bergmann91774902009-11-05 04:37:29 +0000649 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 if (!sk)
651 goto out;
652
653 x25 = x25_sk(sk);
654
655 switch (x25->state) {
656
657 case X25_STATE_0:
658 case X25_STATE_2:
659 x25_disconnect(sk, 0, 0, 0);
660 x25_destroy_socket(sk);
661 goto out;
662
663 case X25_STATE_1:
664 case X25_STATE_3:
665 case X25_STATE_4:
666 x25_clear_queues(sk);
667 x25_write_internal(sk, X25_CLEAR_REQUEST);
668 x25_start_t23timer(sk);
669 x25->state = X25_STATE_2;
670 sk->sk_state = TCP_CLOSE;
671 sk->sk_shutdown |= SEND_SHUTDOWN;
672 sk->sk_state_change(sk);
673 sock_set_flag(sk, SOCK_DEAD);
674 sock_set_flag(sk, SOCK_DESTROY);
675 break;
676 }
677
David S. Millerc751e4f2008-06-17 03:05:13 -0700678 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000680 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return 0;
682}
683
684static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
685{
686 struct sock *sk = sock->sk;
687 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
andrew hendrya9288522010-02-14 02:00:45 +0000688 int len, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Arnd Bergmann91774902009-11-05 04:37:29 +0000690 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (!sock_flag(sk, SOCK_ZAPPED) ||
692 addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000693 addr->sx25_family != AF_X25) {
694 rc = -EINVAL;
695 goto out;
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
andrew hendrya9288522010-02-14 02:00:45 +0000698 len = strlen(addr->sx25_addr.x25_addr);
699 for (i = 0; i < len; i++) {
700 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
701 rc = -EINVAL;
702 goto out;
703 }
704 }
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 x25_sk(sk)->source_addr = addr->sx25_addr;
707 x25_insert_socket(sk);
708 sock_reset_flag(sk, SOCK_ZAPPED);
709 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000710out:
711 unlock_kernel();
712 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static int x25_wait_for_connection_establishment(struct sock *sk)
716{
717 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900718 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 add_wait_queue_exclusive(sk->sk_sleep, &wait);
721 for (;;) {
722 __set_current_state(TASK_INTERRUPTIBLE);
723 rc = -ERESTARTSYS;
724 if (signal_pending(current))
725 break;
726 rc = sock_error(sk);
727 if (rc) {
728 sk->sk_socket->state = SS_UNCONNECTED;
729 break;
730 }
731 rc = 0;
732 if (sk->sk_state != TCP_ESTABLISHED) {
733 release_sock(sk);
734 schedule();
735 lock_sock(sk);
736 } else
737 break;
738 }
739 __set_current_state(TASK_RUNNING);
740 remove_wait_queue(sk->sk_sleep, &wait);
741 return rc;
742}
743
744static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
745 int addr_len, int flags)
746{
747 struct sock *sk = sock->sk;
748 struct x25_sock *x25 = x25_sk(sk);
749 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
750 struct x25_route *rt;
751 int rc = 0;
752
Arnd Bergmann91774902009-11-05 04:37:29 +0000753 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 lock_sock(sk);
755 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
756 sock->state = SS_CONNECTED;
757 goto out; /* Connect completed during a ERESTARTSYS event */
758 }
759
760 rc = -ECONNREFUSED;
761 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
762 sock->state = SS_UNCONNECTED;
763 goto out;
764 }
765
766 rc = -EISCONN; /* No reconnect on a seqpacket socket */
767 if (sk->sk_state == TCP_ESTABLISHED)
768 goto out;
769
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900770 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 sock->state = SS_UNCONNECTED;
772
773 rc = -EINVAL;
774 if (addr_len != sizeof(struct sockaddr_x25) ||
775 addr->sx25_family != AF_X25)
776 goto out;
777
778 rc = -ENETUNREACH;
779 rt = x25_get_route(&addr->sx25_addr);
780 if (!rt)
781 goto out;
782
783 x25->neighbour = x25_get_neigh(rt->dev);
784 if (!x25->neighbour)
785 goto out_put_route;
786
787 x25_limit_facilities(&x25->facilities, x25->neighbour);
788
789 x25->lci = x25_new_lci(x25->neighbour);
790 if (!x25->lci)
791 goto out_put_neigh;
792
793 rc = -EINVAL;
794 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
795 goto out_put_neigh;
796
797 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
798 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
799
800 x25->dest_addr = addr->sx25_addr;
801
802 /* Move to connecting socket, start sending Connect Requests */
803 sock->state = SS_CONNECTING;
804 sk->sk_state = TCP_SYN_SENT;
805
806 x25->state = X25_STATE_1;
807
808 x25_write_internal(sk, X25_CALL_REQUEST);
809
810 x25_start_heartbeat(sk);
811 x25_start_t21timer(sk);
812
813 /* Now the loop */
814 rc = -EINPROGRESS;
815 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
816 goto out_put_neigh;
817
818 rc = x25_wait_for_connection_establishment(sk);
819 if (rc)
820 goto out_put_neigh;
821
822 sock->state = SS_CONNECTED;
823 rc = 0;
824out_put_neigh:
825 if (rc)
826 x25_neigh_put(x25->neighbour);
827out_put_route:
828 x25_route_put(rt);
829out:
830 release_sock(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000831 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return rc;
833}
834
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800835static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 DECLARE_WAITQUEUE(wait, current);
838 int rc = 0;
839
840 add_wait_queue_exclusive(sk->sk_sleep, &wait);
841 for (;;) {
842 __set_current_state(TASK_INTERRUPTIBLE);
843 if (sk->sk_shutdown & RCV_SHUTDOWN)
844 break;
845 rc = -ERESTARTSYS;
846 if (signal_pending(current))
847 break;
848 rc = -EAGAIN;
849 if (!timeout)
850 break;
851 rc = 0;
852 if (skb_queue_empty(&sk->sk_receive_queue)) {
853 release_sock(sk);
854 timeout = schedule_timeout(timeout);
855 lock_sock(sk);
856 } else
857 break;
858 }
859 __set_current_state(TASK_RUNNING);
860 remove_wait_queue(sk->sk_sleep, &wait);
861 return rc;
862}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
865{
866 struct sock *sk = sock->sk;
867 struct sock *newsk;
868 struct sk_buff *skb;
869 int rc = -EINVAL;
870
Arnd Bergmann91774902009-11-05 04:37:29 +0000871 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (!sk || sk->sk_state != TCP_LISTEN)
873 goto out;
874
875 rc = -EOPNOTSUPP;
876 if (sk->sk_type != SOCK_SEQPACKET)
877 goto out;
878
879 lock_sock(sk);
880 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
881 if (rc)
882 goto out2;
883 skb = skb_dequeue(&sk->sk_receive_queue);
884 rc = -EINVAL;
885 if (!skb->sk)
886 goto out2;
887 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700888 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 /* Now attach up the new socket */
891 skb->sk = NULL;
892 kfree_skb(skb);
893 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 newsock->state = SS_CONNECTED;
895 rc = 0;
896out2:
897 release_sock(sk);
898out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000899 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 return rc;
901}
902
903static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
904 int *uaddr_len, int peer)
905{
906 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
907 struct sock *sk = sock->sk;
908 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000909 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Arnd Bergmann91774902009-11-05 04:37:29 +0000911 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000913 if (sk->sk_state != TCP_ESTABLISHED) {
914 rc = -ENOTCONN;
915 goto out;
916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sx25->sx25_addr = x25->dest_addr;
918 } else
919 sx25->sx25_addr = x25->source_addr;
920
921 sx25->sx25_family = AF_X25;
922 *uaddr_len = sizeof(*sx25);
923
Arnd Bergmann91774902009-11-05 04:37:29 +0000924out:
925 unlock_kernel();
926 return rc;
927}
928
929static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
930 poll_table *wait)
931{
932 int rc;
933
934 lock_kernel();
935 rc = datagram_poll(file, sock, wait);
936 unlock_kernel();
937
938 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
942 unsigned int lci)
943{
944 struct sock *sk;
945 struct sock *make;
946 struct x25_sock *makex25;
947 struct x25_address source_addr, dest_addr;
948 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800949 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800950 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 /*
953 * Remove the LCI and frame type.
954 */
955 skb_pull(skb, X25_STD_MIN_LEN);
956
957 /*
958 * Extract the X.25 addresses and convert them to ASCII strings,
959 * and remove them.
John Hughesf5eb9172010-04-07 21:29:25 -0700960 *
961 * Address block is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
John Hughesf5eb9172010-04-07 21:29:25 -0700963 addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
964 if (addr_len <= 0)
965 goto out_clear_request;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800966 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 /*
969 * Get the length of the facilities, skip past them for the moment
970 * get the call user data because this is needed to determine
971 * the correct listener
John Hughesf5eb9172010-04-07 21:29:25 -0700972 *
973 * Facilities length is mandatory in call request packets
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
John Hughesf5eb9172010-04-07 21:29:25 -0700975 if (skb->len < 1)
976 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 len = skb->data[0] + 1;
John Hughesf5eb9172010-04-07 21:29:25 -0700978 if (skb->len < len)
979 goto out_clear_request;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 skb_pull(skb,len);
981
982 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 * Find a listener for the particular address/cud pair.
984 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700985 sk = x25_find_listener(&source_addr,skb);
986 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800988 if (sk != NULL && sk_acceptq_is_full(sk)) {
989 goto out_sock_put;
990 }
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800993 * We dont have any listeners for this incoming call.
994 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800996 if (sk == NULL) {
997 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800998 if (sysctl_x25_forward &&
999 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001000 {
1001 /* Call was forwarded, dont process it any more */
1002 kfree_skb(skb);
1003 rc = 1;
1004 goto out;
1005 } else {
1006 /* No listeners, can't forward, clear the call */
1007 goto out_clear_request;
1008 }
1009 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /*
1012 * Try to reach a compromise on the requested facilities.
1013 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001014 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1015 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto out_sock_put;
1017
1018 /*
1019 * current neighbour/link might impose additional limits
1020 * on certain facilties
1021 */
1022
1023 x25_limit_facilities(&facilities, nb);
1024
1025 /*
1026 * Try to create a new socket.
1027 */
1028 make = x25_make_new(sk);
1029 if (!make)
1030 goto out_sock_put;
1031
1032 /*
1033 * Remove the facilities
1034 */
1035 skb_pull(skb, len);
1036
1037 skb->sk = make;
1038 make->sk_state = TCP_ESTABLISHED;
1039
1040 makex25 = x25_sk(make);
1041 makex25->lci = lci;
1042 makex25->dest_addr = dest_addr;
1043 makex25->source_addr = source_addr;
1044 makex25->neighbour = nb;
1045 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001046 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001048 /* ensure no reverse facil on accept */
1049 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001050 /* ensure no calling address extension on accept */
1051 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001052 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001054 /* Normally all calls are accepted immediatly */
1055 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1056 x25_write_internal(make, X25_CALL_ACCEPTED);
1057 makex25->state = X25_STATE_3;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Shaun Pereiracb65d502005-06-22 22:15:01 -07001060 /*
1061 * Incoming Call User Data.
1062 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001063 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1064 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 sk->sk_ack_backlog++;
1067
1068 x25_insert_socket(make);
1069
1070 skb_queue_head(&sk->sk_receive_queue, skb);
1071
1072 x25_start_heartbeat(make);
1073
1074 if (!sock_flag(sk, SOCK_DEAD))
1075 sk->sk_data_ready(sk, skb->len);
1076 rc = 1;
1077 sock_put(sk);
1078out:
1079 return rc;
1080out_sock_put:
1081 sock_put(sk);
1082out_clear_request:
1083 rc = 0;
1084 x25_transmit_clear_request(nb, lci, 0x01);
1085 goto out;
1086}
1087
1088static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1089 struct msghdr *msg, size_t len)
1090{
1091 struct sock *sk = sock->sk;
1092 struct x25_sock *x25 = x25_sk(sk);
1093 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1094 struct sockaddr_x25 sx25;
1095 struct sk_buff *skb;
1096 unsigned char *asmptr;
1097 int noblock = msg->msg_flags & MSG_DONTWAIT;
1098 size_t size;
1099 int qbit = 0, rc = -EINVAL;
1100
Arnd Bergmann91774902009-11-05 04:37:29 +00001101 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1103 goto out;
1104
1105 /* we currently don't support segmented records at the user interface */
1106 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1107 goto out;
1108
1109 rc = -EADDRNOTAVAIL;
1110 if (sock_flag(sk, SOCK_ZAPPED))
1111 goto out;
1112
1113 rc = -EPIPE;
1114 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1115 send_sig(SIGPIPE, current, 0);
1116 goto out;
1117 }
1118
1119 rc = -ENETUNREACH;
1120 if (!x25->neighbour)
1121 goto out;
1122
1123 if (usx25) {
1124 rc = -EINVAL;
1125 if (msg->msg_namelen < sizeof(sx25))
1126 goto out;
1127 memcpy(&sx25, usx25, sizeof(sx25));
1128 rc = -EISCONN;
1129 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1130 goto out;
1131 rc = -EINVAL;
1132 if (sx25.sx25_family != AF_X25)
1133 goto out;
1134 } else {
1135 /*
1136 * FIXME 1003.1g - if the socket is like this because
1137 * it has become closed (not started closed) we ought
1138 * to SIGPIPE, EPIPE;
1139 */
1140 rc = -ENOTCONN;
1141 if (sk->sk_state != TCP_ESTABLISHED)
1142 goto out;
1143
1144 sx25.sx25_family = AF_X25;
1145 sx25.sx25_addr = x25->dest_addr;
1146 }
1147
Alan Cox83e0bbc2009-03-27 00:28:21 -07001148 /* Sanity check the packet size */
1149 if (len > 65535) {
1150 rc = -EMSGSIZE;
1151 goto out;
1152 }
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1155
1156 /* Build a packet */
1157 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1158
1159 if ((msg->msg_flags & MSG_OOB) && len > 32)
1160 len = 32;
1161
1162 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1163
1164 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1165 if (!skb)
1166 goto out;
1167 X25_SKB_CB(skb)->flags = msg->msg_flags;
1168
1169 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1170
1171 /*
1172 * Put the data on the end
1173 */
1174 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1175
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001176 skb_reset_transport_header(skb);
1177 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001179 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 if (rc)
1181 goto out_kfree_skb;
1182
1183 /*
1184 * If the Q BIT Include socket option is in force, the first
1185 * byte of the user data is the logical value of the Q Bit.
1186 */
1187 if (x25->qbitincl) {
1188 qbit = skb->data[0];
1189 skb_pull(skb, 1);
1190 }
1191
1192 /*
1193 * Push down the X.25 header
1194 */
1195 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1196
1197 if (msg->msg_flags & MSG_OOB) {
1198 if (x25->neighbour->extended) {
1199 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1200 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1201 *asmptr++ = (x25->lci >> 0) & 0xFF;
1202 *asmptr++ = X25_INTERRUPT;
1203 } else {
1204 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1205 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1206 *asmptr++ = (x25->lci >> 0) & 0xFF;
1207 *asmptr++ = X25_INTERRUPT;
1208 }
1209 } else {
1210 if (x25->neighbour->extended) {
1211 /* Build an Extended X.25 header */
1212 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1213 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1214 *asmptr++ = (x25->lci >> 0) & 0xFF;
1215 *asmptr++ = X25_DATA;
1216 *asmptr++ = X25_DATA;
1217 } else {
1218 /* Build an Standard X.25 header */
1219 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1220 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1221 *asmptr++ = (x25->lci >> 0) & 0xFF;
1222 *asmptr++ = X25_DATA;
1223 }
1224
1225 if (qbit)
1226 skb->data[0] |= X25_Q_BIT;
1227 }
1228
1229 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1230 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1231
1232 rc = -ENOTCONN;
1233 if (sk->sk_state != TCP_ESTABLISHED)
1234 goto out_kfree_skb;
1235
1236 if (msg->msg_flags & MSG_OOB)
1237 skb_queue_tail(&x25->interrupt_out_queue, skb);
1238 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001239 rc = x25_output(sk, skb);
1240 len = rc;
1241 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 kfree_skb(skb);
1243 else if (x25->qbitincl)
1244 len++;
1245 }
1246
1247 /*
1248 * lock_sock() is currently only used to serialize this x25_kick()
1249 * against input-driven x25_kick() calls. It currently only blocks
1250 * incoming packets for this socket and does not protect against
1251 * any other socket state changes and is not called from anywhere
1252 * else. As x25_kick() cannot block and as long as all socket
1253 * operations are BKL-wrapped, we don't need take to care about
1254 * purging the backlog queue in x25_release().
1255 *
1256 * Using lock_sock() to protect all socket operations entirely
1257 * (and making the whole x25 stack SMP aware) unfortunately would
1258 * require major changes to {send,recv}msg and skb allocation methods.
1259 * -> 2.5 ;)
1260 */
1261 lock_sock(sk);
1262 x25_kick(sk);
1263 release_sock(sk);
1264 rc = len;
1265out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001266 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return rc;
1268out_kfree_skb:
1269 kfree_skb(skb);
1270 goto out;
1271}
1272
1273
1274static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1275 struct msghdr *msg, size_t size,
1276 int flags)
1277{
1278 struct sock *sk = sock->sk;
1279 struct x25_sock *x25 = x25_sk(sk);
1280 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1281 size_t copied;
1282 int qbit;
1283 struct sk_buff *skb;
1284 unsigned char *asmptr;
1285 int rc = -ENOTCONN;
1286
Arnd Bergmann91774902009-11-05 04:37:29 +00001287 lock_kernel();
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
1303 skb_pull(skb, X25_STD_MIN_LEN);
1304
1305 /*
1306 * No Q bit information on Interrupt data.
1307 */
1308 if (x25->qbitincl) {
1309 asmptr = skb_push(skb, 1);
1310 *asmptr = 0x00;
1311 }
1312
1313 msg->msg_flags |= MSG_OOB;
1314 } else {
1315 /* Now we can treat all alike */
1316 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1317 flags & MSG_DONTWAIT, &rc);
1318 if (!skb)
1319 goto out;
1320
1321 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1322
1323 skb_pull(skb, x25->neighbour->extended ?
1324 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1325
1326 if (x25->qbitincl) {
1327 asmptr = skb_push(skb, 1);
1328 *asmptr = qbit;
1329 }
1330 }
1331
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001332 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 copied = skb->len;
1334
1335 if (copied > size) {
1336 copied = size;
1337 msg->msg_flags |= MSG_TRUNC;
1338 }
1339
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001340 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 msg->msg_flags |= MSG_EOR;
1342
1343 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1344 if (rc)
1345 goto out_free_dgram;
1346
1347 if (sx25) {
1348 sx25->sx25_family = AF_X25;
1349 sx25->sx25_addr = x25->dest_addr;
1350 }
1351
1352 msg->msg_namelen = sizeof(struct sockaddr_x25);
1353
1354 lock_sock(sk);
1355 x25_check_rbuf(sk);
1356 release_sock(sk);
1357 rc = copied;
1358out_free_dgram:
1359 skb_free_datagram(sk, skb);
1360out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001361 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return rc;
1363}
1364
1365
1366static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1367{
1368 struct sock *sk = sock->sk;
1369 struct x25_sock *x25 = x25_sk(sk);
1370 void __user *argp = (void __user *)arg;
1371 int rc;
1372
Arnd Bergmann91774902009-11-05 04:37:29 +00001373 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 switch (cmd) {
1375 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001376 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1377
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 if (amount < 0)
1379 amount = 0;
1380 rc = put_user(amount, (unsigned int __user *)argp);
1381 break;
1382 }
1383
1384 case TIOCINQ: {
1385 struct sk_buff *skb;
1386 int amount = 0;
1387 /*
1388 * These two are safe on a single CPU system as
1389 * only user tasks fiddle here
1390 */
1391 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1392 amount = skb->len;
1393 rc = put_user(amount, (unsigned int __user *)argp);
1394 break;
1395 }
1396
1397 case SIOCGSTAMP:
1398 rc = -EINVAL;
1399 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001400 rc = sock_get_timestamp(sk,
1401 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001403 case SIOCGSTAMPNS:
1404 rc = -EINVAL;
1405 if (sk)
1406 rc = sock_get_timestampns(sk,
1407 (struct timespec __user *)argp);
1408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 case SIOCGIFADDR:
1410 case SIOCSIFADDR:
1411 case SIOCGIFDSTADDR:
1412 case SIOCSIFDSTADDR:
1413 case SIOCGIFBRDADDR:
1414 case SIOCSIFBRDADDR:
1415 case SIOCGIFNETMASK:
1416 case SIOCSIFNETMASK:
1417 case SIOCGIFMETRIC:
1418 case SIOCSIFMETRIC:
1419 rc = -EINVAL;
1420 break;
1421 case SIOCADDRT:
1422 case SIOCDELRT:
1423 rc = -EPERM;
1424 if (!capable(CAP_NET_ADMIN))
1425 break;
1426 rc = x25_route_ioctl(cmd, argp);
1427 break;
1428 case SIOCX25GSUBSCRIP:
1429 rc = x25_subscr_ioctl(cmd, argp);
1430 break;
1431 case SIOCX25SSUBSCRIP:
1432 rc = -EPERM;
1433 if (!capable(CAP_NET_ADMIN))
1434 break;
1435 rc = x25_subscr_ioctl(cmd, argp);
1436 break;
1437 case SIOCX25GFACILITIES: {
1438 struct x25_facilities fac = x25->facilities;
1439 rc = copy_to_user(argp, &fac,
1440 sizeof(fac)) ? -EFAULT : 0;
1441 break;
1442 }
1443
1444 case SIOCX25SFACILITIES: {
1445 struct x25_facilities facilities;
1446 rc = -EFAULT;
1447 if (copy_from_user(&facilities, argp,
1448 sizeof(facilities)))
1449 break;
1450 rc = -EINVAL;
1451 if (sk->sk_state != TCP_LISTEN &&
1452 sk->sk_state != TCP_CLOSE)
1453 break;
1454 if (facilities.pacsize_in < X25_PS16 ||
1455 facilities.pacsize_in > X25_PS4096)
1456 break;
1457 if (facilities.pacsize_out < X25_PS16 ||
1458 facilities.pacsize_out > X25_PS4096)
1459 break;
1460 if (facilities.winsize_in < 1 ||
1461 facilities.winsize_in > 127)
1462 break;
John Hughesddd04512010-04-04 06:48:10 +00001463 if (facilities.throughput) {
1464 int out = facilities.throughput & 0xf0;
1465 int in = facilities.throughput & 0x0f;
1466 if (!out)
1467 facilities.throughput |=
1468 X25_DEFAULT_THROUGHPUT << 4;
1469 else if (out < 0x30 || out > 0xD0)
1470 break;
1471 if (!in)
1472 facilities.throughput |=
1473 X25_DEFAULT_THROUGHPUT;
1474 else if (in < 0x03 || in > 0x0D)
1475 break;
1476 }
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001477 if (facilities.reverse &&
roel kluin06a96b332009-10-07 00:59:42 +00001478 (facilities.reverse & 0x81) != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break;
1480 x25->facilities = facilities;
1481 rc = 0;
1482 break;
1483 }
1484
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001485 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001486 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001487 sizeof(x25->dte_facilities));
1488 if (rc)
1489 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001490 break;
1491 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001492
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001493 case SIOCX25SDTEFACILITIES: {
1494 struct x25_dte_facilities dtefacs;
1495 rc = -EFAULT;
1496 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001497 break;
1498 rc = -EINVAL;
1499 if (sk->sk_state != TCP_LISTEN &&
1500 sk->sk_state != TCP_CLOSE)
1501 break;
1502 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1503 break;
1504 if (dtefacs.calling_ae == NULL)
1505 break;
1506 if (dtefacs.called_len > X25_MAX_AE_LEN)
1507 break;
1508 if (dtefacs.called_ae == NULL)
1509 break;
1510 x25->dte_facilities = dtefacs;
1511 rc = 0;
1512 break;
1513 }
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 case SIOCX25GCALLUSERDATA: {
1516 struct x25_calluserdata cud = x25->calluserdata;
1517 rc = copy_to_user(argp, &cud,
1518 sizeof(cud)) ? -EFAULT : 0;
1519 break;
1520 }
1521
1522 case SIOCX25SCALLUSERDATA: {
1523 struct x25_calluserdata calluserdata;
1524
1525 rc = -EFAULT;
1526 if (copy_from_user(&calluserdata, argp,
1527 sizeof(calluserdata)))
1528 break;
1529 rc = -EINVAL;
1530 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1531 break;
1532 x25->calluserdata = calluserdata;
1533 rc = 0;
1534 break;
1535 }
1536
1537 case SIOCX25GCAUSEDIAG: {
1538 struct x25_causediag causediag;
1539 causediag = x25->causediag;
1540 rc = copy_to_user(argp, &causediag,
1541 sizeof(causediag)) ? -EFAULT : 0;
1542 break;
1543 }
1544
Andrew Hendry386e50c2009-11-18 23:30:41 -08001545 case SIOCX25SCAUSEDIAG: {
1546 struct x25_causediag causediag;
1547 rc = -EFAULT;
1548 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1549 break;
1550 x25->causediag = causediag;
1551 rc = 0;
1552 break;
1553
1554 }
1555
Shaun Pereiracb65d502005-06-22 22:15:01 -07001556 case SIOCX25SCUDMATCHLEN: {
1557 struct x25_subaddr sub_addr;
1558 rc = -EINVAL;
1559 if(sk->sk_state != TCP_CLOSE)
1560 break;
1561 rc = -EFAULT;
1562 if (copy_from_user(&sub_addr, argp,
1563 sizeof(sub_addr)))
1564 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001565 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001566 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1567 break;
1568 x25->cudmatchlength = sub_addr.cudmatchlength;
1569 rc = 0;
1570 break;
1571 }
1572
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001573 case SIOCX25CALLACCPTAPPRV: {
1574 rc = -EINVAL;
1575 if (sk->sk_state != TCP_CLOSE)
1576 break;
1577 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1578 rc = 0;
1579 break;
1580 }
1581
1582 case SIOCX25SENDCALLACCPT: {
1583 rc = -EINVAL;
1584 if (sk->sk_state != TCP_ESTABLISHED)
1585 break;
1586 if (x25->accptapprv) /* must call accptapprv above */
1587 break;
1588 x25_write_internal(sk, X25_CALL_ACCEPTED);
1589 x25->state = X25_STATE_3;
1590 rc = 0;
1591 break;
1592 }
1593
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001594 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001595 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 break;
1597 }
Arnd Bergmann91774902009-11-05 04:37:29 +00001598 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 return rc;
1601}
1602
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001603static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 .family = AF_X25,
1605 .create = x25_create,
1606 .owner = THIS_MODULE,
1607};
1608
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001609#ifdef CONFIG_COMPAT
1610static int compat_x25_subscr_ioctl(unsigned int cmd,
1611 struct compat_x25_subscrip_struct __user *x25_subscr32)
1612{
1613 struct compat_x25_subscrip_struct x25_subscr;
1614 struct x25_neigh *nb;
1615 struct net_device *dev;
1616 int rc = -EINVAL;
1617
1618 rc = -EFAULT;
1619 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1620 goto out;
1621
1622 rc = -EINVAL;
1623 dev = x25_dev_get(x25_subscr.device);
1624 if (dev == NULL)
1625 goto out;
1626
1627 nb = x25_get_neigh(dev);
1628 if (nb == NULL)
1629 goto out_dev_put;
1630
1631 dev_put(dev);
1632
1633 if (cmd == SIOCX25GSUBSCRIP) {
1634 x25_subscr.extended = nb->extended;
1635 x25_subscr.global_facil_mask = nb->global_facil_mask;
1636 rc = copy_to_user(x25_subscr32, &x25_subscr,
1637 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1638 } else {
1639 rc = -EINVAL;
1640 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1641 rc = 0;
1642 nb->extended = x25_subscr.extended;
1643 nb->global_facil_mask = x25_subscr.global_facil_mask;
1644 }
1645 }
1646 x25_neigh_put(nb);
1647out:
1648 return rc;
1649out_dev_put:
1650 dev_put(dev);
1651 goto out;
1652}
1653
1654static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1655 unsigned long arg)
1656{
1657 void __user *argp = compat_ptr(arg);
1658 struct sock *sk = sock->sk;
1659
1660 int rc = -ENOIOCTLCMD;
1661
1662 switch(cmd) {
1663 case TIOCOUTQ:
1664 case TIOCINQ:
1665 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1666 break;
1667 case SIOCGSTAMP:
1668 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001669 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001670 if (sk)
1671 rc = compat_sock_get_timestamp(sk,
1672 (struct timeval __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001673 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001674 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001675 case SIOCGSTAMPNS:
1676 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001677 lock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001678 if (sk)
1679 rc = compat_sock_get_timestampns(sk,
1680 (struct timespec __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001681 unlock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001682 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001683 case SIOCGIFADDR:
1684 case SIOCSIFADDR:
1685 case SIOCGIFDSTADDR:
1686 case SIOCSIFDSTADDR:
1687 case SIOCGIFBRDADDR:
1688 case SIOCSIFBRDADDR:
1689 case SIOCGIFNETMASK:
1690 case SIOCSIFNETMASK:
1691 case SIOCGIFMETRIC:
1692 case SIOCSIFMETRIC:
1693 rc = -EINVAL;
1694 break;
1695 case SIOCADDRT:
1696 case SIOCDELRT:
1697 rc = -EPERM;
1698 if (!capable(CAP_NET_ADMIN))
1699 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001700 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001701 rc = x25_route_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001702 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001703 break;
1704 case SIOCX25GSUBSCRIP:
Arnd Bergmann91774902009-11-05 04:37:29 +00001705 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001706 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001707 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001708 break;
1709 case SIOCX25SSUBSCRIP:
1710 rc = -EPERM;
1711 if (!capable(CAP_NET_ADMIN))
1712 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001713 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001714 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001715 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001716 break;
1717 case SIOCX25GFACILITIES:
1718 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001719 case SIOCX25GDTEFACILITIES:
1720 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001721 case SIOCX25GCALLUSERDATA:
1722 case SIOCX25SCALLUSERDATA:
1723 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001724 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001725 case SIOCX25SCUDMATCHLEN:
1726 case SIOCX25CALLACCPTAPPRV:
1727 case SIOCX25SENDCALLACCPT:
1728 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1729 break;
1730 default:
1731 rc = -ENOIOCTLCMD;
1732 break;
1733 }
1734 return rc;
1735}
1736#endif
1737
Arnd Bergmann91774902009-11-05 04:37:29 +00001738static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .family = AF_X25,
1740 .owner = THIS_MODULE,
1741 .release = x25_release,
1742 .bind = x25_bind,
1743 .connect = x25_connect,
1744 .socketpair = sock_no_socketpair,
1745 .accept = x25_accept,
1746 .getname = x25_getname,
Arnd Bergmann91774902009-11-05 04:37:29 +00001747 .poll = x25_datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001749#ifdef CONFIG_COMPAT
1750 .compat_ioctl = compat_x25_ioctl,
1751#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .listen = x25_listen,
1753 .shutdown = sock_no_shutdown,
1754 .setsockopt = x25_setsockopt,
1755 .getsockopt = x25_getsockopt,
1756 .sendmsg = x25_sendmsg,
1757 .recvmsg = x25_recvmsg,
1758 .mmap = sock_no_mmap,
1759 .sendpage = sock_no_sendpage,
1760};
1761
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001762static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001763 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 .func = x25_lapb_receive_frame,
1765};
1766
1767static struct notifier_block x25_dev_notifier = {
1768 .notifier_call = x25_device_event,
1769};
1770
1771void x25_kill_by_neigh(struct x25_neigh *nb)
1772{
1773 struct sock *s;
1774 struct hlist_node *node;
1775
1776 write_lock_bh(&x25_list_lock);
1777
1778 sk_for_each(s, node, &x25_list)
1779 if (x25_sk(s)->neighbour == nb)
1780 x25_disconnect(s, ENETUNREACH, 0, 0);
1781
1782 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001783
1784 /* Remove any related forwards */
1785 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
1787
1788static int __init x25_init(void)
1789{
1790 int rc = proto_register(&x25_proto, 0);
1791
1792 if (rc != 0)
1793 goto out;
1794
andrew hendry1fd975a2009-11-24 15:15:42 +00001795 rc = sock_register(&x25_family_ops);
1796 if (rc != 0)
1797 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 dev_add_pack(&x25_packet_type);
1800
andrew hendry1fd975a2009-11-24 15:15:42 +00001801 rc = register_netdevice_notifier(&x25_dev_notifier);
1802 if (rc != 0)
1803 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
maximilian attemsa44562e2008-01-28 20:43:16 -08001805 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 x25_register_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001808 rc = x25_proc_init();
1809 if (rc != 0)
1810 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811out:
1812 return rc;
andrew hendry1fd975a2009-11-24 15:15:42 +00001813out_dev:
1814 unregister_netdevice_notifier(&x25_dev_notifier);
1815out_sock:
1816 sock_unregister(AF_X25);
1817out_proto:
1818 proto_unregister(&x25_proto);
1819 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821module_init(x25_init);
1822
1823static void __exit x25_exit(void)
1824{
1825 x25_proc_exit();
1826 x25_link_free();
1827 x25_route_free();
1828
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 unregister_netdevice_notifier(&x25_dev_notifier);
1832
1833 dev_remove_pack(&x25_packet_type);
1834
1835 sock_unregister(AF_X25);
1836 proto_unregister(&x25_proto);
1837}
1838module_exit(x25_exit);
1839
1840MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1841MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1842MODULE_LICENSE("GPL");
1843MODULE_ALIAS_NETPROTO(PF_X25);