blob: 9796f3ed1edbc5dce941f449aa235276628a3d86 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070085int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
86 struct x25_address *calling_addr)
87{
Eric Dumazet6bf15742008-01-13 22:27:52 -080088 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -080090 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 called_len = (*p >> 0) & 0x0F;
93 calling_len = (*p >> 4) & 0x0F;
94
95 called = called_addr->x25_addr;
96 calling = calling_addr->x25_addr;
97 p++;
98
99 for (i = 0; i < (called_len + calling_len); i++) {
100 if (i < called_len) {
101 if (i % 2 != 0) {
102 *called++ = ((*p >> 0) & 0x0F) + '0';
103 p++;
104 } else {
105 *called++ = ((*p >> 4) & 0x0F) + '0';
106 }
107 } else {
108 if (i % 2 != 0) {
109 *calling++ = ((*p >> 0) & 0x0F) + '0';
110 p++;
111 } else {
112 *calling++ = ((*p >> 4) & 0x0F) + '0';
113 }
114 }
115 }
116
117 *called = *calling = '\0';
118
119 return 1 + (called_len + calling_len + 1) / 2;
120}
121
122int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
123 struct x25_address *calling_addr)
124{
125 unsigned int called_len, calling_len;
126 char *called, *calling;
127 int i;
128
129 called = called_addr->x25_addr;
130 calling = calling_addr->x25_addr;
131
132 called_len = strlen(called);
133 calling_len = strlen(calling);
134
135 *p++ = (calling_len << 4) | (called_len << 0);
136
137 for (i = 0; i < (called_len + calling_len); i++) {
138 if (i < called_len) {
139 if (i % 2 != 0) {
140 *p |= (*called++ - '0') << 0;
141 p++;
142 } else {
143 *p = 0x00;
144 *p |= (*called++ - '0') << 4;
145 }
146 } else {
147 if (i % 2 != 0) {
148 *p |= (*calling++ - '0') << 0;
149 p++;
150 } else {
151 *p = 0x00;
152 *p |= (*calling++ - '0') << 4;
153 }
154 }
155 }
156
157 return 1 + (called_len + calling_len + 1) / 2;
158}
159
160/*
161 * Socket removal during an interrupt is now safe.
162 */
163static void x25_remove_socket(struct sock *sk)
164{
165 write_lock_bh(&x25_list_lock);
166 sk_del_node_init(sk);
167 write_unlock_bh(&x25_list_lock);
168}
169
170/*
171 * Kill all bound sockets on a dropped device.
172 */
173static void x25_kill_by_device(struct net_device *dev)
174{
175 struct sock *s;
176 struct hlist_node *node;
177
178 write_lock_bh(&x25_list_lock);
179
180 sk_for_each(s, node, &x25_list)
181 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
182 x25_disconnect(s, ENETUNREACH, 0, 0);
183
184 write_unlock_bh(&x25_list_lock);
185}
186
187/*
188 * Handle device status changes.
189 */
190static int x25_device_event(struct notifier_block *this, unsigned long event,
191 void *ptr)
192{
193 struct net_device *dev = ptr;
194 struct x25_neigh *nb;
195
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700196 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200197 return NOTIFY_DONE;
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (dev->type == ARPHRD_X25
200#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
201 || dev->type == ARPHRD_ETHER
202#endif
203 ) {
204 switch (event) {
205 case NETDEV_UP:
206 x25_link_device_up(dev);
207 break;
208 case NETDEV_GOING_DOWN:
209 nb = x25_get_neigh(dev);
210 if (nb) {
211 x25_terminate_link(nb);
212 x25_neigh_put(nb);
213 }
214 break;
215 case NETDEV_DOWN:
216 x25_kill_by_device(dev);
217 x25_route_device_down(dev);
218 x25_link_device_down(dev);
219 break;
220 }
221 }
222
223 return NOTIFY_DONE;
224}
225
226/*
227 * Add a socket to the bound sockets list.
228 */
229static void x25_insert_socket(struct sock *sk)
230{
231 write_lock_bh(&x25_list_lock);
232 sk_add_node(sk, &x25_list);
233 write_unlock_bh(&x25_list_lock);
234}
235
236/*
237 * Find a socket that wants to accept the Call Request we just
238 * received. Check the full list for an address/cud match.
239 * If no cuds match return the next_best thing, an address match.
240 * Note: if a listening socket has cud set it must only get calls
241 * with matching cud.
242 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700243static struct sock *x25_find_listener(struct x25_address *addr,
244 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct sock *s;
247 struct sock *next_best;
248 struct hlist_node *node;
249
250 read_lock_bh(&x25_list_lock);
251 next_best = NULL;
252
253 sk_for_each(s, node, &x25_list)
254 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700255 x25_sk(s)->source_addr.x25_addr) ||
256 !strcmp(addr->x25_addr,
257 null_x25_address.x25_addr)) &&
258 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /*
260 * Found a listening socket, now check the incoming
261 * call user data vs this sockets call user data
262 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700263 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900264 if((memcmp(x25_sk(s)->calluserdata.cuddata,
265 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700266 x25_sk(s)->cudmatchlength)) == 0) {
267 sock_hold(s);
268 goto found;
269 }
270 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273 if (next_best) {
274 s = next_best;
275 sock_hold(s);
276 goto found;
277 }
278 s = NULL;
279found:
280 read_unlock_bh(&x25_list_lock);
281 return s;
282}
283
284/*
285 * Find a connected X.25 socket given my LCI and neighbour.
286 */
287static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
288{
289 struct sock *s;
290 struct hlist_node *node;
291
292 sk_for_each(s, node, &x25_list)
293 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
294 sock_hold(s);
295 goto found;
296 }
297 s = NULL;
298found:
299 return s;
300}
301
302struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
303{
304 struct sock *s;
305
306 read_lock_bh(&x25_list_lock);
307 s = __x25_find_socket(lci, nb);
308 read_unlock_bh(&x25_list_lock);
309 return s;
310}
311
312/*
313 * Find a unique LCI for a given device.
314 */
315static unsigned int x25_new_lci(struct x25_neigh *nb)
316{
317 unsigned int lci = 1;
318 struct sock *sk;
319
320 read_lock_bh(&x25_list_lock);
321
322 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
323 sock_put(sk);
324 if (++lci == 4096) {
325 lci = 0;
326 break;
327 }
328 }
329
330 read_unlock_bh(&x25_list_lock);
331 return lci;
332}
333
334/*
335 * Deferred destroy.
336 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700337static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339/*
340 * handler for deferred kills.
341 */
342static void x25_destroy_timer(unsigned long data)
343{
David S. Miller14ebaf82009-06-16 05:40:30 -0700344 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/*
348 * This is called from user mode and the timers. Thus it protects itself
349 * against interrupt users but doesn't worry about being called during
350 * work. Once it is removed from the queue no interrupt or bottom half
351 * will touch it and we are (fairly 8-) ) safe.
352 * Not static as it's used by the timer
353 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700354static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
356 struct sk_buff *skb;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 x25_stop_heartbeat(sk);
359 x25_stop_timer(sk);
360
361 x25_remove_socket(sk);
362 x25_clear_queues(sk); /* Flush the queues */
363
364 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
365 if (skb->sk != sk) { /* A pending connection */
366 /*
367 * Queue the unaccepted socket for death
368 */
369 sock_set_flag(skb->sk, SOCK_DEAD);
370 x25_start_heartbeat(skb->sk);
371 x25_sk(skb->sk)->state = X25_STATE_0;
372 }
373
374 kfree_skb(skb);
375 }
376
Eric Dumazetc5640392009-06-16 10:12:03 +0000377 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Defer: outstanding buffers */
379 sk->sk_timer.expires = jiffies + 10 * HZ;
380 sk->sk_timer.function = x25_destroy_timer;
381 sk->sk_timer.data = (unsigned long)sk;
382 add_timer(&sk->sk_timer);
383 } else {
384 /* drop last reference so sock_put will free */
385 __sock_put(sk);
386 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
David S. Miller14ebaf82009-06-16 05:40:30 -0700389void x25_destroy_socket_from_timer(struct sock *sk)
390{
391 sock_hold(sk);
392 bh_lock_sock(sk);
393 __x25_destroy_socket(sk);
394 bh_unlock_sock(sk);
395 sock_put(sk);
396}
397
398static void x25_destroy_socket(struct sock *sk)
399{
400 sock_hold(sk);
401 lock_sock(sk);
402 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 release_sock(sk);
404 sock_put(sk);
405}
406
407/*
408 * Handling for system calls applied via the various interfaces to a
409 * X.25 socket object.
410 */
411
412static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700413 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 int opt;
416 struct sock *sk = sock->sk;
417 int rc = -ENOPROTOOPT;
418
Arnd Bergmann91774902009-11-05 04:37:29 +0000419 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (level != SOL_X25 || optname != X25_QBITINCL)
421 goto out;
422
423 rc = -EINVAL;
424 if (optlen < sizeof(int))
425 goto out;
426
427 rc = -EFAULT;
428 if (get_user(opt, (int __user *)optval))
429 goto out;
430
431 x25_sk(sk)->qbitincl = !!opt;
432 rc = 0;
433out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000434 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return rc;
436}
437
438static int x25_getsockopt(struct socket *sock, int level, int optname,
439 char __user *optval, int __user *optlen)
440{
441 struct sock *sk = sock->sk;
442 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900443
Arnd Bergmann91774902009-11-05 04:37:29 +0000444 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (level != SOL_X25 || optname != X25_QBITINCL)
446 goto out;
447
448 rc = -EFAULT;
449 if (get_user(len, optlen))
450 goto out;
451
452 len = min_t(unsigned int, len, sizeof(int));
453
454 rc = -EINVAL;
455 if (len < 0)
456 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 rc = -EFAULT;
459 if (put_user(len, optlen))
460 goto out;
461
462 val = x25_sk(sk)->qbitincl;
463 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
464out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000465 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 return rc;
467}
468
469static int x25_listen(struct socket *sock, int backlog)
470{
471 struct sock *sk = sock->sk;
472 int rc = -EOPNOTSUPP;
473
Arnd Bergmann91774902009-11-05 04:37:29 +0000474 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (sk->sk_state != TCP_LISTEN) {
476 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
477 sk->sk_max_ack_backlog = backlog;
478 sk->sk_state = TCP_LISTEN;
479 rc = 0;
480 }
Arnd Bergmann91774902009-11-05 04:37:29 +0000481 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return rc;
484}
485
486static struct proto x25_proto = {
487 .name = "X25",
488 .owner = THIS_MODULE,
489 .obj_size = sizeof(struct x25_sock),
490};
491
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700492static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700495 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 if (!sk)
498 goto out;
499
500 sock_init_data(NULL, sk);
501
502 x25 = x25_sk(sk);
503 skb_queue_head_init(&x25->ack_queue);
504 skb_queue_head_init(&x25->fragment_queue);
505 skb_queue_head_init(&x25->interrupt_in_queue);
506 skb_queue_head_init(&x25->interrupt_out_queue);
507out:
508 return sk;
509}
510
Eric Paris3f378b62009-11-05 22:18:14 -0800511static int x25_create(struct net *net, struct socket *sock, int protocol,
512 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
514 struct sock *sk;
515 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000516 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800518 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 goto out;
520
andrew hendryb18e7a02010-02-14 02:00:11 +0000521 rc = -ESOCKTNOSUPPORT;
522 if (sock->type != SOCK_SEQPACKET)
523 goto out;
524
525 rc = -EINVAL;
526 if (protocol)
527 goto out;
528
529 rc = -ENOBUFS;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700530 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
532
533 x25 = x25_sk(sk);
534
535 sock_init_data(sock, sk);
536
537 x25_init_timers(sk);
538
539 sock->ops = &x25_proto_ops;
540 sk->sk_protocol = protocol;
541 sk->sk_backlog_rcv = x25_backlog_rcv;
542
543 x25->t21 = sysctl_x25_call_request_timeout;
544 x25->t22 = sysctl_x25_reset_request_timeout;
545 x25->t23 = sysctl_x25_clear_request_timeout;
546 x25->t2 = sysctl_x25_ack_holdback_timeout;
547 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700548 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700549 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
550 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
553 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
554 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
555 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
556 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
557 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900558 x25->dte_facilities.calling_len = 0;
559 x25->dte_facilities.called_len = 0;
560 memset(x25->dte_facilities.called_ae, '\0',
561 sizeof(x25->dte_facilities.called_ae));
562 memset(x25->dte_facilities.calling_ae, '\0',
563 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rc = 0;
566out:
567 return rc;
568}
569
570static struct sock *x25_make_new(struct sock *osk)
571{
572 struct sock *sk = NULL;
573 struct x25_sock *x25, *ox25;
574
575 if (osk->sk_type != SOCK_SEQPACKET)
576 goto out;
577
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900578 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 goto out;
580
581 x25 = x25_sk(sk);
582
583 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 sk->sk_priority = osk->sk_priority;
585 sk->sk_protocol = osk->sk_protocol;
586 sk->sk_rcvbuf = osk->sk_rcvbuf;
587 sk->sk_sndbuf = osk->sk_sndbuf;
588 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800590 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 ox25 = x25_sk(osk);
593 x25->t21 = ox25->t21;
594 x25->t22 = ox25->t22;
595 x25->t23 = ox25->t23;
596 x25->t2 = ox25->t2;
597 x25->facilities = ox25->facilities;
598 x25->qbitincl = ox25->qbitincl;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800599 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700600 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700601 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 x25_init_timers(sk);
604out:
605 return sk;
606}
607
608static int x25_release(struct socket *sock)
609{
610 struct sock *sk = sock->sk;
611 struct x25_sock *x25;
612
Arnd Bergmann91774902009-11-05 04:37:29 +0000613 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!sk)
615 goto out;
616
617 x25 = x25_sk(sk);
618
619 switch (x25->state) {
620
621 case X25_STATE_0:
622 case X25_STATE_2:
623 x25_disconnect(sk, 0, 0, 0);
624 x25_destroy_socket(sk);
625 goto out;
626
627 case X25_STATE_1:
628 case X25_STATE_3:
629 case X25_STATE_4:
630 x25_clear_queues(sk);
631 x25_write_internal(sk, X25_CLEAR_REQUEST);
632 x25_start_t23timer(sk);
633 x25->state = X25_STATE_2;
634 sk->sk_state = TCP_CLOSE;
635 sk->sk_shutdown |= SEND_SHUTDOWN;
636 sk->sk_state_change(sk);
637 sock_set_flag(sk, SOCK_DEAD);
638 sock_set_flag(sk, SOCK_DESTROY);
639 break;
640 }
641
David S. Millerc751e4f2008-06-17 03:05:13 -0700642 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000644 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return 0;
646}
647
648static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
649{
650 struct sock *sk = sock->sk;
651 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
andrew hendrya9288522010-02-14 02:00:45 +0000652 int len, i, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Arnd Bergmann91774902009-11-05 04:37:29 +0000654 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!sock_flag(sk, SOCK_ZAPPED) ||
656 addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000657 addr->sx25_family != AF_X25) {
658 rc = -EINVAL;
659 goto out;
660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
andrew hendrya9288522010-02-14 02:00:45 +0000662 len = strlen(addr->sx25_addr.x25_addr);
663 for (i = 0; i < len; i++) {
664 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
665 rc = -EINVAL;
666 goto out;
667 }
668 }
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 x25_sk(sk)->source_addr = addr->sx25_addr;
671 x25_insert_socket(sk);
672 sock_reset_flag(sk, SOCK_ZAPPED);
673 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000674out:
675 unlock_kernel();
676 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static int x25_wait_for_connection_establishment(struct sock *sk)
680{
681 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900682 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684 add_wait_queue_exclusive(sk->sk_sleep, &wait);
685 for (;;) {
686 __set_current_state(TASK_INTERRUPTIBLE);
687 rc = -ERESTARTSYS;
688 if (signal_pending(current))
689 break;
690 rc = sock_error(sk);
691 if (rc) {
692 sk->sk_socket->state = SS_UNCONNECTED;
693 break;
694 }
695 rc = 0;
696 if (sk->sk_state != TCP_ESTABLISHED) {
697 release_sock(sk);
698 schedule();
699 lock_sock(sk);
700 } else
701 break;
702 }
703 __set_current_state(TASK_RUNNING);
704 remove_wait_queue(sk->sk_sleep, &wait);
705 return rc;
706}
707
708static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
709 int addr_len, int flags)
710{
711 struct sock *sk = sock->sk;
712 struct x25_sock *x25 = x25_sk(sk);
713 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
714 struct x25_route *rt;
715 int rc = 0;
716
Arnd Bergmann91774902009-11-05 04:37:29 +0000717 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 lock_sock(sk);
719 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
720 sock->state = SS_CONNECTED;
721 goto out; /* Connect completed during a ERESTARTSYS event */
722 }
723
724 rc = -ECONNREFUSED;
725 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
726 sock->state = SS_UNCONNECTED;
727 goto out;
728 }
729
730 rc = -EISCONN; /* No reconnect on a seqpacket socket */
731 if (sk->sk_state == TCP_ESTABLISHED)
732 goto out;
733
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900734 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sock->state = SS_UNCONNECTED;
736
737 rc = -EINVAL;
738 if (addr_len != sizeof(struct sockaddr_x25) ||
739 addr->sx25_family != AF_X25)
740 goto out;
741
742 rc = -ENETUNREACH;
743 rt = x25_get_route(&addr->sx25_addr);
744 if (!rt)
745 goto out;
746
747 x25->neighbour = x25_get_neigh(rt->dev);
748 if (!x25->neighbour)
749 goto out_put_route;
750
751 x25_limit_facilities(&x25->facilities, x25->neighbour);
752
753 x25->lci = x25_new_lci(x25->neighbour);
754 if (!x25->lci)
755 goto out_put_neigh;
756
757 rc = -EINVAL;
758 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
759 goto out_put_neigh;
760
761 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
762 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
763
764 x25->dest_addr = addr->sx25_addr;
765
766 /* Move to connecting socket, start sending Connect Requests */
767 sock->state = SS_CONNECTING;
768 sk->sk_state = TCP_SYN_SENT;
769
770 x25->state = X25_STATE_1;
771
772 x25_write_internal(sk, X25_CALL_REQUEST);
773
774 x25_start_heartbeat(sk);
775 x25_start_t21timer(sk);
776
777 /* Now the loop */
778 rc = -EINPROGRESS;
779 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
780 goto out_put_neigh;
781
782 rc = x25_wait_for_connection_establishment(sk);
783 if (rc)
784 goto out_put_neigh;
785
786 sock->state = SS_CONNECTED;
787 rc = 0;
788out_put_neigh:
789 if (rc)
790 x25_neigh_put(x25->neighbour);
791out_put_route:
792 x25_route_put(rt);
793out:
794 release_sock(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000795 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return rc;
797}
798
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800799static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 DECLARE_WAITQUEUE(wait, current);
802 int rc = 0;
803
804 add_wait_queue_exclusive(sk->sk_sleep, &wait);
805 for (;;) {
806 __set_current_state(TASK_INTERRUPTIBLE);
807 if (sk->sk_shutdown & RCV_SHUTDOWN)
808 break;
809 rc = -ERESTARTSYS;
810 if (signal_pending(current))
811 break;
812 rc = -EAGAIN;
813 if (!timeout)
814 break;
815 rc = 0;
816 if (skb_queue_empty(&sk->sk_receive_queue)) {
817 release_sock(sk);
818 timeout = schedule_timeout(timeout);
819 lock_sock(sk);
820 } else
821 break;
822 }
823 __set_current_state(TASK_RUNNING);
824 remove_wait_queue(sk->sk_sleep, &wait);
825 return rc;
826}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
829{
830 struct sock *sk = sock->sk;
831 struct sock *newsk;
832 struct sk_buff *skb;
833 int rc = -EINVAL;
834
Arnd Bergmann91774902009-11-05 04:37:29 +0000835 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!sk || sk->sk_state != TCP_LISTEN)
837 goto out;
838
839 rc = -EOPNOTSUPP;
840 if (sk->sk_type != SOCK_SEQPACKET)
841 goto out;
842
843 lock_sock(sk);
844 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
845 if (rc)
846 goto out2;
847 skb = skb_dequeue(&sk->sk_receive_queue);
848 rc = -EINVAL;
849 if (!skb->sk)
850 goto out2;
851 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700852 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 /* Now attach up the new socket */
855 skb->sk = NULL;
856 kfree_skb(skb);
857 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 newsock->state = SS_CONNECTED;
859 rc = 0;
860out2:
861 release_sock(sk);
862out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000863 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 return rc;
865}
866
867static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
868 int *uaddr_len, int peer)
869{
870 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
871 struct sock *sk = sock->sk;
872 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000873 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Arnd Bergmann91774902009-11-05 04:37:29 +0000875 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000877 if (sk->sk_state != TCP_ESTABLISHED) {
878 rc = -ENOTCONN;
879 goto out;
880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 sx25->sx25_addr = x25->dest_addr;
882 } else
883 sx25->sx25_addr = x25->source_addr;
884
885 sx25->sx25_family = AF_X25;
886 *uaddr_len = sizeof(*sx25);
887
Arnd Bergmann91774902009-11-05 04:37:29 +0000888out:
889 unlock_kernel();
890 return rc;
891}
892
893static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
894 poll_table *wait)
895{
896 int rc;
897
898 lock_kernel();
899 rc = datagram_poll(file, sock, wait);
900 unlock_kernel();
901
902 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
906 unsigned int lci)
907{
908 struct sock *sk;
909 struct sock *make;
910 struct x25_sock *makex25;
911 struct x25_address source_addr, dest_addr;
912 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800913 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800914 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 /*
917 * Remove the LCI and frame type.
918 */
919 skb_pull(skb, X25_STD_MIN_LEN);
920
921 /*
922 * Extract the X.25 addresses and convert them to ASCII strings,
923 * and remove them.
924 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800925 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
926 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 /*
929 * Get the length of the facilities, skip past them for the moment
930 * get the call user data because this is needed to determine
931 * the correct listener
932 */
933 len = skb->data[0] + 1;
934 skb_pull(skb,len);
935
936 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 * Find a listener for the particular address/cud pair.
938 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700939 sk = x25_find_listener(&source_addr,skb);
940 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800942 if (sk != NULL && sk_acceptq_is_full(sk)) {
943 goto out_sock_put;
944 }
945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800947 * We dont have any listeners for this incoming call.
948 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800950 if (sk == NULL) {
951 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800952 if (sysctl_x25_forward &&
953 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800954 {
955 /* Call was forwarded, dont process it any more */
956 kfree_skb(skb);
957 rc = 1;
958 goto out;
959 } else {
960 /* No listeners, can't forward, clear the call */
961 goto out_clear_request;
962 }
963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * Try to reach a compromise on the requested facilities.
967 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800968 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
969 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out_sock_put;
971
972 /*
973 * current neighbour/link might impose additional limits
974 * on certain facilties
975 */
976
977 x25_limit_facilities(&facilities, nb);
978
979 /*
980 * Try to create a new socket.
981 */
982 make = x25_make_new(sk);
983 if (!make)
984 goto out_sock_put;
985
986 /*
987 * Remove the facilities
988 */
989 skb_pull(skb, len);
990
991 skb->sk = make;
992 make->sk_state = TCP_ESTABLISHED;
993
994 makex25 = x25_sk(make);
995 makex25->lci = lci;
996 makex25->dest_addr = dest_addr;
997 makex25->source_addr = source_addr;
998 makex25->neighbour = nb;
999 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001000 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001002 /* ensure no reverse facil on accept */
1003 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001004 /* ensure no calling address extension on accept */
1005 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001006 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001008 /* Normally all calls are accepted immediatly */
1009 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1010 x25_write_internal(make, X25_CALL_ACCEPTED);
1011 makex25->state = X25_STATE_3;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Shaun Pereiracb65d502005-06-22 22:15:01 -07001014 /*
1015 * Incoming Call User Data.
1016 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001017 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1018 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 sk->sk_ack_backlog++;
1021
1022 x25_insert_socket(make);
1023
1024 skb_queue_head(&sk->sk_receive_queue, skb);
1025
1026 x25_start_heartbeat(make);
1027
1028 if (!sock_flag(sk, SOCK_DEAD))
1029 sk->sk_data_ready(sk, skb->len);
1030 rc = 1;
1031 sock_put(sk);
1032out:
1033 return rc;
1034out_sock_put:
1035 sock_put(sk);
1036out_clear_request:
1037 rc = 0;
1038 x25_transmit_clear_request(nb, lci, 0x01);
1039 goto out;
1040}
1041
1042static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1043 struct msghdr *msg, size_t len)
1044{
1045 struct sock *sk = sock->sk;
1046 struct x25_sock *x25 = x25_sk(sk);
1047 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1048 struct sockaddr_x25 sx25;
1049 struct sk_buff *skb;
1050 unsigned char *asmptr;
1051 int noblock = msg->msg_flags & MSG_DONTWAIT;
1052 size_t size;
1053 int qbit = 0, rc = -EINVAL;
1054
Arnd Bergmann91774902009-11-05 04:37:29 +00001055 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1057 goto out;
1058
1059 /* we currently don't support segmented records at the user interface */
1060 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1061 goto out;
1062
1063 rc = -EADDRNOTAVAIL;
1064 if (sock_flag(sk, SOCK_ZAPPED))
1065 goto out;
1066
1067 rc = -EPIPE;
1068 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1069 send_sig(SIGPIPE, current, 0);
1070 goto out;
1071 }
1072
1073 rc = -ENETUNREACH;
1074 if (!x25->neighbour)
1075 goto out;
1076
1077 if (usx25) {
1078 rc = -EINVAL;
1079 if (msg->msg_namelen < sizeof(sx25))
1080 goto out;
1081 memcpy(&sx25, usx25, sizeof(sx25));
1082 rc = -EISCONN;
1083 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1084 goto out;
1085 rc = -EINVAL;
1086 if (sx25.sx25_family != AF_X25)
1087 goto out;
1088 } else {
1089 /*
1090 * FIXME 1003.1g - if the socket is like this because
1091 * it has become closed (not started closed) we ought
1092 * to SIGPIPE, EPIPE;
1093 */
1094 rc = -ENOTCONN;
1095 if (sk->sk_state != TCP_ESTABLISHED)
1096 goto out;
1097
1098 sx25.sx25_family = AF_X25;
1099 sx25.sx25_addr = x25->dest_addr;
1100 }
1101
Alan Cox83e0bbc2009-03-27 00:28:21 -07001102 /* Sanity check the packet size */
1103 if (len > 65535) {
1104 rc = -EMSGSIZE;
1105 goto out;
1106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1109
1110 /* Build a packet */
1111 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1112
1113 if ((msg->msg_flags & MSG_OOB) && len > 32)
1114 len = 32;
1115
1116 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1117
1118 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1119 if (!skb)
1120 goto out;
1121 X25_SKB_CB(skb)->flags = msg->msg_flags;
1122
1123 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1124
1125 /*
1126 * Put the data on the end
1127 */
1128 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1129
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001130 skb_reset_transport_header(skb);
1131 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001133 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (rc)
1135 goto out_kfree_skb;
1136
1137 /*
1138 * If the Q BIT Include socket option is in force, the first
1139 * byte of the user data is the logical value of the Q Bit.
1140 */
1141 if (x25->qbitincl) {
1142 qbit = skb->data[0];
1143 skb_pull(skb, 1);
1144 }
1145
1146 /*
1147 * Push down the X.25 header
1148 */
1149 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1150
1151 if (msg->msg_flags & MSG_OOB) {
1152 if (x25->neighbour->extended) {
1153 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1154 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1155 *asmptr++ = (x25->lci >> 0) & 0xFF;
1156 *asmptr++ = X25_INTERRUPT;
1157 } else {
1158 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1159 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1160 *asmptr++ = (x25->lci >> 0) & 0xFF;
1161 *asmptr++ = X25_INTERRUPT;
1162 }
1163 } else {
1164 if (x25->neighbour->extended) {
1165 /* Build an Extended X.25 header */
1166 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1167 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1168 *asmptr++ = (x25->lci >> 0) & 0xFF;
1169 *asmptr++ = X25_DATA;
1170 *asmptr++ = X25_DATA;
1171 } else {
1172 /* Build an Standard X.25 header */
1173 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1174 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1175 *asmptr++ = (x25->lci >> 0) & 0xFF;
1176 *asmptr++ = X25_DATA;
1177 }
1178
1179 if (qbit)
1180 skb->data[0] |= X25_Q_BIT;
1181 }
1182
1183 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1184 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1185
1186 rc = -ENOTCONN;
1187 if (sk->sk_state != TCP_ESTABLISHED)
1188 goto out_kfree_skb;
1189
1190 if (msg->msg_flags & MSG_OOB)
1191 skb_queue_tail(&x25->interrupt_out_queue, skb);
1192 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001193 rc = x25_output(sk, skb);
1194 len = rc;
1195 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 kfree_skb(skb);
1197 else if (x25->qbitincl)
1198 len++;
1199 }
1200
1201 /*
1202 * lock_sock() is currently only used to serialize this x25_kick()
1203 * against input-driven x25_kick() calls. It currently only blocks
1204 * incoming packets for this socket and does not protect against
1205 * any other socket state changes and is not called from anywhere
1206 * else. As x25_kick() cannot block and as long as all socket
1207 * operations are BKL-wrapped, we don't need take to care about
1208 * purging the backlog queue in x25_release().
1209 *
1210 * Using lock_sock() to protect all socket operations entirely
1211 * (and making the whole x25 stack SMP aware) unfortunately would
1212 * require major changes to {send,recv}msg and skb allocation methods.
1213 * -> 2.5 ;)
1214 */
1215 lock_sock(sk);
1216 x25_kick(sk);
1217 release_sock(sk);
1218 rc = len;
1219out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001220 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return rc;
1222out_kfree_skb:
1223 kfree_skb(skb);
1224 goto out;
1225}
1226
1227
1228static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1229 struct msghdr *msg, size_t size,
1230 int flags)
1231{
1232 struct sock *sk = sock->sk;
1233 struct x25_sock *x25 = x25_sk(sk);
1234 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1235 size_t copied;
1236 int qbit;
1237 struct sk_buff *skb;
1238 unsigned char *asmptr;
1239 int rc = -ENOTCONN;
1240
Arnd Bergmann91774902009-11-05 04:37:29 +00001241 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 /*
1243 * This works for seqpacket too. The receiver has ordered the queue for
1244 * us! We do one quick check first though
1245 */
1246 if (sk->sk_state != TCP_ESTABLISHED)
1247 goto out;
1248
1249 if (flags & MSG_OOB) {
1250 rc = -EINVAL;
1251 if (sock_flag(sk, SOCK_URGINLINE) ||
1252 !skb_peek(&x25->interrupt_in_queue))
1253 goto out;
1254
1255 skb = skb_dequeue(&x25->interrupt_in_queue);
1256
1257 skb_pull(skb, X25_STD_MIN_LEN);
1258
1259 /*
1260 * No Q bit information on Interrupt data.
1261 */
1262 if (x25->qbitincl) {
1263 asmptr = skb_push(skb, 1);
1264 *asmptr = 0x00;
1265 }
1266
1267 msg->msg_flags |= MSG_OOB;
1268 } else {
1269 /* Now we can treat all alike */
1270 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1271 flags & MSG_DONTWAIT, &rc);
1272 if (!skb)
1273 goto out;
1274
1275 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1276
1277 skb_pull(skb, x25->neighbour->extended ?
1278 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1279
1280 if (x25->qbitincl) {
1281 asmptr = skb_push(skb, 1);
1282 *asmptr = qbit;
1283 }
1284 }
1285
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001286 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 copied = skb->len;
1288
1289 if (copied > size) {
1290 copied = size;
1291 msg->msg_flags |= MSG_TRUNC;
1292 }
1293
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001294 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 msg->msg_flags |= MSG_EOR;
1296
1297 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1298 if (rc)
1299 goto out_free_dgram;
1300
1301 if (sx25) {
1302 sx25->sx25_family = AF_X25;
1303 sx25->sx25_addr = x25->dest_addr;
1304 }
1305
1306 msg->msg_namelen = sizeof(struct sockaddr_x25);
1307
1308 lock_sock(sk);
1309 x25_check_rbuf(sk);
1310 release_sock(sk);
1311 rc = copied;
1312out_free_dgram:
1313 skb_free_datagram(sk, skb);
1314out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001315 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 return rc;
1317}
1318
1319
1320static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1321{
1322 struct sock *sk = sock->sk;
1323 struct x25_sock *x25 = x25_sk(sk);
1324 void __user *argp = (void __user *)arg;
1325 int rc;
1326
Arnd Bergmann91774902009-11-05 04:37:29 +00001327 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 switch (cmd) {
1329 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001330 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1331
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (amount < 0)
1333 amount = 0;
1334 rc = put_user(amount, (unsigned int __user *)argp);
1335 break;
1336 }
1337
1338 case TIOCINQ: {
1339 struct sk_buff *skb;
1340 int amount = 0;
1341 /*
1342 * These two are safe on a single CPU system as
1343 * only user tasks fiddle here
1344 */
1345 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1346 amount = skb->len;
1347 rc = put_user(amount, (unsigned int __user *)argp);
1348 break;
1349 }
1350
1351 case SIOCGSTAMP:
1352 rc = -EINVAL;
1353 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001354 rc = sock_get_timestamp(sk,
1355 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001357 case SIOCGSTAMPNS:
1358 rc = -EINVAL;
1359 if (sk)
1360 rc = sock_get_timestampns(sk,
1361 (struct timespec __user *)argp);
1362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case SIOCGIFADDR:
1364 case SIOCSIFADDR:
1365 case SIOCGIFDSTADDR:
1366 case SIOCSIFDSTADDR:
1367 case SIOCGIFBRDADDR:
1368 case SIOCSIFBRDADDR:
1369 case SIOCGIFNETMASK:
1370 case SIOCSIFNETMASK:
1371 case SIOCGIFMETRIC:
1372 case SIOCSIFMETRIC:
1373 rc = -EINVAL;
1374 break;
1375 case SIOCADDRT:
1376 case SIOCDELRT:
1377 rc = -EPERM;
1378 if (!capable(CAP_NET_ADMIN))
1379 break;
1380 rc = x25_route_ioctl(cmd, argp);
1381 break;
1382 case SIOCX25GSUBSCRIP:
1383 rc = x25_subscr_ioctl(cmd, argp);
1384 break;
1385 case SIOCX25SSUBSCRIP:
1386 rc = -EPERM;
1387 if (!capable(CAP_NET_ADMIN))
1388 break;
1389 rc = x25_subscr_ioctl(cmd, argp);
1390 break;
1391 case SIOCX25GFACILITIES: {
1392 struct x25_facilities fac = x25->facilities;
1393 rc = copy_to_user(argp, &fac,
1394 sizeof(fac)) ? -EFAULT : 0;
1395 break;
1396 }
1397
1398 case SIOCX25SFACILITIES: {
1399 struct x25_facilities facilities;
1400 rc = -EFAULT;
1401 if (copy_from_user(&facilities, argp,
1402 sizeof(facilities)))
1403 break;
1404 rc = -EINVAL;
1405 if (sk->sk_state != TCP_LISTEN &&
1406 sk->sk_state != TCP_CLOSE)
1407 break;
1408 if (facilities.pacsize_in < X25_PS16 ||
1409 facilities.pacsize_in > X25_PS4096)
1410 break;
1411 if (facilities.pacsize_out < X25_PS16 ||
1412 facilities.pacsize_out > X25_PS4096)
1413 break;
1414 if (facilities.winsize_in < 1 ||
1415 facilities.winsize_in > 127)
1416 break;
1417 if (facilities.throughput < 0x03 ||
1418 facilities.throughput > 0xDD)
1419 break;
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001420 if (facilities.reverse &&
roel kluin06a96b32009-10-07 00:59:42 +00001421 (facilities.reverse & 0x81) != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 break;
1423 x25->facilities = facilities;
1424 rc = 0;
1425 break;
1426 }
1427
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001428 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001429 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001430 sizeof(x25->dte_facilities));
1431 if (rc)
1432 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001433 break;
1434 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001435
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001436 case SIOCX25SDTEFACILITIES: {
1437 struct x25_dte_facilities dtefacs;
1438 rc = -EFAULT;
1439 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001440 break;
1441 rc = -EINVAL;
1442 if (sk->sk_state != TCP_LISTEN &&
1443 sk->sk_state != TCP_CLOSE)
1444 break;
1445 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1446 break;
1447 if (dtefacs.calling_ae == NULL)
1448 break;
1449 if (dtefacs.called_len > X25_MAX_AE_LEN)
1450 break;
1451 if (dtefacs.called_ae == NULL)
1452 break;
1453 x25->dte_facilities = dtefacs;
1454 rc = 0;
1455 break;
1456 }
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 case SIOCX25GCALLUSERDATA: {
1459 struct x25_calluserdata cud = x25->calluserdata;
1460 rc = copy_to_user(argp, &cud,
1461 sizeof(cud)) ? -EFAULT : 0;
1462 break;
1463 }
1464
1465 case SIOCX25SCALLUSERDATA: {
1466 struct x25_calluserdata calluserdata;
1467
1468 rc = -EFAULT;
1469 if (copy_from_user(&calluserdata, argp,
1470 sizeof(calluserdata)))
1471 break;
1472 rc = -EINVAL;
1473 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1474 break;
1475 x25->calluserdata = calluserdata;
1476 rc = 0;
1477 break;
1478 }
1479
1480 case SIOCX25GCAUSEDIAG: {
1481 struct x25_causediag causediag;
1482 causediag = x25->causediag;
1483 rc = copy_to_user(argp, &causediag,
1484 sizeof(causediag)) ? -EFAULT : 0;
1485 break;
1486 }
1487
Andrew Hendry386e50c2009-11-18 23:30:41 -08001488 case SIOCX25SCAUSEDIAG: {
1489 struct x25_causediag causediag;
1490 rc = -EFAULT;
1491 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1492 break;
1493 x25->causediag = causediag;
1494 rc = 0;
1495 break;
1496
1497 }
1498
Shaun Pereiracb65d502005-06-22 22:15:01 -07001499 case SIOCX25SCUDMATCHLEN: {
1500 struct x25_subaddr sub_addr;
1501 rc = -EINVAL;
1502 if(sk->sk_state != TCP_CLOSE)
1503 break;
1504 rc = -EFAULT;
1505 if (copy_from_user(&sub_addr, argp,
1506 sizeof(sub_addr)))
1507 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001508 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001509 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1510 break;
1511 x25->cudmatchlength = sub_addr.cudmatchlength;
1512 rc = 0;
1513 break;
1514 }
1515
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001516 case SIOCX25CALLACCPTAPPRV: {
1517 rc = -EINVAL;
1518 if (sk->sk_state != TCP_CLOSE)
1519 break;
1520 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1521 rc = 0;
1522 break;
1523 }
1524
1525 case SIOCX25SENDCALLACCPT: {
1526 rc = -EINVAL;
1527 if (sk->sk_state != TCP_ESTABLISHED)
1528 break;
1529 if (x25->accptapprv) /* must call accptapprv above */
1530 break;
1531 x25_write_internal(sk, X25_CALL_ACCEPTED);
1532 x25->state = X25_STATE_3;
1533 rc = 0;
1534 break;
1535 }
1536
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001537 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001538 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540 }
Arnd Bergmann91774902009-11-05 04:37:29 +00001541 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543 return rc;
1544}
1545
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001546static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 .family = AF_X25,
1548 .create = x25_create,
1549 .owner = THIS_MODULE,
1550};
1551
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001552#ifdef CONFIG_COMPAT
1553static int compat_x25_subscr_ioctl(unsigned int cmd,
1554 struct compat_x25_subscrip_struct __user *x25_subscr32)
1555{
1556 struct compat_x25_subscrip_struct x25_subscr;
1557 struct x25_neigh *nb;
1558 struct net_device *dev;
1559 int rc = -EINVAL;
1560
1561 rc = -EFAULT;
1562 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1563 goto out;
1564
1565 rc = -EINVAL;
1566 dev = x25_dev_get(x25_subscr.device);
1567 if (dev == NULL)
1568 goto out;
1569
1570 nb = x25_get_neigh(dev);
1571 if (nb == NULL)
1572 goto out_dev_put;
1573
1574 dev_put(dev);
1575
1576 if (cmd == SIOCX25GSUBSCRIP) {
1577 x25_subscr.extended = nb->extended;
1578 x25_subscr.global_facil_mask = nb->global_facil_mask;
1579 rc = copy_to_user(x25_subscr32, &x25_subscr,
1580 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1581 } else {
1582 rc = -EINVAL;
1583 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1584 rc = 0;
1585 nb->extended = x25_subscr.extended;
1586 nb->global_facil_mask = x25_subscr.global_facil_mask;
1587 }
1588 }
1589 x25_neigh_put(nb);
1590out:
1591 return rc;
1592out_dev_put:
1593 dev_put(dev);
1594 goto out;
1595}
1596
1597static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1598 unsigned long arg)
1599{
1600 void __user *argp = compat_ptr(arg);
1601 struct sock *sk = sock->sk;
1602
1603 int rc = -ENOIOCTLCMD;
1604
1605 switch(cmd) {
1606 case TIOCOUTQ:
1607 case TIOCINQ:
1608 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1609 break;
1610 case SIOCGSTAMP:
1611 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001612 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001613 if (sk)
1614 rc = compat_sock_get_timestamp(sk,
1615 (struct timeval __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001616 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001617 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001618 case SIOCGSTAMPNS:
1619 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001620 lock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001621 if (sk)
1622 rc = compat_sock_get_timestampns(sk,
1623 (struct timespec __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001624 unlock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001625 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001626 case SIOCGIFADDR:
1627 case SIOCSIFADDR:
1628 case SIOCGIFDSTADDR:
1629 case SIOCSIFDSTADDR:
1630 case SIOCGIFBRDADDR:
1631 case SIOCSIFBRDADDR:
1632 case SIOCGIFNETMASK:
1633 case SIOCSIFNETMASK:
1634 case SIOCGIFMETRIC:
1635 case SIOCSIFMETRIC:
1636 rc = -EINVAL;
1637 break;
1638 case SIOCADDRT:
1639 case SIOCDELRT:
1640 rc = -EPERM;
1641 if (!capable(CAP_NET_ADMIN))
1642 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001643 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001644 rc = x25_route_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001645 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001646 break;
1647 case SIOCX25GSUBSCRIP:
Arnd Bergmann91774902009-11-05 04:37:29 +00001648 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001649 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001650 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001651 break;
1652 case SIOCX25SSUBSCRIP:
1653 rc = -EPERM;
1654 if (!capable(CAP_NET_ADMIN))
1655 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001656 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001657 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001658 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001659 break;
1660 case SIOCX25GFACILITIES:
1661 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001662 case SIOCX25GDTEFACILITIES:
1663 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001664 case SIOCX25GCALLUSERDATA:
1665 case SIOCX25SCALLUSERDATA:
1666 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001667 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001668 case SIOCX25SCUDMATCHLEN:
1669 case SIOCX25CALLACCPTAPPRV:
1670 case SIOCX25SENDCALLACCPT:
1671 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1672 break;
1673 default:
1674 rc = -ENOIOCTLCMD;
1675 break;
1676 }
1677 return rc;
1678}
1679#endif
1680
Arnd Bergmann91774902009-11-05 04:37:29 +00001681static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .family = AF_X25,
1683 .owner = THIS_MODULE,
1684 .release = x25_release,
1685 .bind = x25_bind,
1686 .connect = x25_connect,
1687 .socketpair = sock_no_socketpair,
1688 .accept = x25_accept,
1689 .getname = x25_getname,
Arnd Bergmann91774902009-11-05 04:37:29 +00001690 .poll = x25_datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001692#ifdef CONFIG_COMPAT
1693 .compat_ioctl = compat_x25_ioctl,
1694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 .listen = x25_listen,
1696 .shutdown = sock_no_shutdown,
1697 .setsockopt = x25_setsockopt,
1698 .getsockopt = x25_getsockopt,
1699 .sendmsg = x25_sendmsg,
1700 .recvmsg = x25_recvmsg,
1701 .mmap = sock_no_mmap,
1702 .sendpage = sock_no_sendpage,
1703};
1704
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001705static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001706 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 .func = x25_lapb_receive_frame,
1708};
1709
1710static struct notifier_block x25_dev_notifier = {
1711 .notifier_call = x25_device_event,
1712};
1713
1714void x25_kill_by_neigh(struct x25_neigh *nb)
1715{
1716 struct sock *s;
1717 struct hlist_node *node;
1718
1719 write_lock_bh(&x25_list_lock);
1720
1721 sk_for_each(s, node, &x25_list)
1722 if (x25_sk(s)->neighbour == nb)
1723 x25_disconnect(s, ENETUNREACH, 0, 0);
1724
1725 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001726
1727 /* Remove any related forwards */
1728 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
1731static int __init x25_init(void)
1732{
1733 int rc = proto_register(&x25_proto, 0);
1734
1735 if (rc != 0)
1736 goto out;
1737
andrew hendry1fd975a2009-11-24 15:15:42 +00001738 rc = sock_register(&x25_family_ops);
1739 if (rc != 0)
1740 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
1742 dev_add_pack(&x25_packet_type);
1743
andrew hendry1fd975a2009-11-24 15:15:42 +00001744 rc = register_netdevice_notifier(&x25_dev_notifier);
1745 if (rc != 0)
1746 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
maximilian attemsa44562e2008-01-28 20:43:16 -08001748 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 x25_register_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001751 rc = x25_proc_init();
1752 if (rc != 0)
1753 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754out:
1755 return rc;
andrew hendry1fd975a2009-11-24 15:15:42 +00001756out_dev:
1757 unregister_netdevice_notifier(&x25_dev_notifier);
1758out_sock:
1759 sock_unregister(AF_X25);
1760out_proto:
1761 proto_unregister(&x25_proto);
1762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764module_init(x25_init);
1765
1766static void __exit x25_exit(void)
1767{
1768 x25_proc_exit();
1769 x25_link_free();
1770 x25_route_free();
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 unregister_netdevice_notifier(&x25_dev_notifier);
1775
1776 dev_remove_pack(&x25_packet_type);
1777
1778 sock_unregister(AF_X25);
1779 proto_unregister(&x25_proto);
1780}
1781module_exit(x25_exit);
1782
1783MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1784MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1785MODULE_LICENSE("GPL");
1786MODULE_ALIAS_NETPROTO(PF_X25);