blob: 6c7104edec606a6928fb6b6e208f1a6a7f2986cf [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>
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/x25.h>
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080060#include <net/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
63int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
64int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
65int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
66int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
Andrew Hendry39e21c02007-02-08 13:34:36 -080067int sysctl_x25_forward = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69HLIST_HEAD(x25_list);
70DEFINE_RWLOCK(x25_list_lock);
71
Eric Dumazet90ddc4f2005-12-22 12:49:22 -080072static const struct proto_ops x25_proto_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static struct x25_address null_x25_address = {" "};
75
Shaun Pereira1b06e6b2006-03-22 00:00:12 -080076#ifdef CONFIG_COMPAT
77struct compat_x25_subscrip_struct {
78 char device[200-sizeof(compat_ulong_t)];
79 compat_ulong_t global_facil_mask;
80 compat_uint_t extended;
81};
82#endif
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
85 struct x25_address *calling_addr)
86{
Eric Dumazet6bf15742008-01-13 22:27:52 -080087 unsigned int called_len, calling_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 char *called, *calling;
Eric Dumazet6bf15742008-01-13 22:27:52 -080089 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 called_len = (*p >> 0) & 0x0F;
92 calling_len = (*p >> 4) & 0x0F;
93
94 called = called_addr->x25_addr;
95 calling = calling_addr->x25_addr;
96 p++;
97
98 for (i = 0; i < (called_len + calling_len); i++) {
99 if (i < called_len) {
100 if (i % 2 != 0) {
101 *called++ = ((*p >> 0) & 0x0F) + '0';
102 p++;
103 } else {
104 *called++ = ((*p >> 4) & 0x0F) + '0';
105 }
106 } else {
107 if (i % 2 != 0) {
108 *calling++ = ((*p >> 0) & 0x0F) + '0';
109 p++;
110 } else {
111 *calling++ = ((*p >> 4) & 0x0F) + '0';
112 }
113 }
114 }
115
116 *called = *calling = '\0';
117
118 return 1 + (called_len + calling_len + 1) / 2;
119}
120
121int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
122 struct x25_address *calling_addr)
123{
124 unsigned int called_len, calling_len;
125 char *called, *calling;
126 int i;
127
128 called = called_addr->x25_addr;
129 calling = calling_addr->x25_addr;
130
131 called_len = strlen(called);
132 calling_len = strlen(calling);
133
134 *p++ = (calling_len << 4) | (called_len << 0);
135
136 for (i = 0; i < (called_len + calling_len); i++) {
137 if (i < called_len) {
138 if (i % 2 != 0) {
139 *p |= (*called++ - '0') << 0;
140 p++;
141 } else {
142 *p = 0x00;
143 *p |= (*called++ - '0') << 4;
144 }
145 } else {
146 if (i % 2 != 0) {
147 *p |= (*calling++ - '0') << 0;
148 p++;
149 } else {
150 *p = 0x00;
151 *p |= (*calling++ - '0') << 4;
152 }
153 }
154 }
155
156 return 1 + (called_len + calling_len + 1) / 2;
157}
158
159/*
160 * Socket removal during an interrupt is now safe.
161 */
162static void x25_remove_socket(struct sock *sk)
163{
164 write_lock_bh(&x25_list_lock);
165 sk_del_node_init(sk);
166 write_unlock_bh(&x25_list_lock);
167}
168
169/*
170 * Kill all bound sockets on a dropped device.
171 */
172static void x25_kill_by_device(struct net_device *dev)
173{
174 struct sock *s;
175 struct hlist_node *node;
176
177 write_lock_bh(&x25_list_lock);
178
179 sk_for_each(s, node, &x25_list)
180 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
181 x25_disconnect(s, ENETUNREACH, 0, 0);
182
183 write_unlock_bh(&x25_list_lock);
184}
185
186/*
187 * Handle device status changes.
188 */
189static int x25_device_event(struct notifier_block *this, unsigned long event,
190 void *ptr)
191{
192 struct net_device *dev = ptr;
193 struct x25_neigh *nb;
194
YOSHIFUJI Hideaki721499e2008-07-19 22:34:43 -0700195 if (!net_eq(dev_net(dev), &init_net))
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200196 return NOTIFY_DONE;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (dev->type == ARPHRD_X25
199#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
200 || dev->type == ARPHRD_ETHER
201#endif
202 ) {
203 switch (event) {
204 case NETDEV_UP:
205 x25_link_device_up(dev);
206 break;
207 case NETDEV_GOING_DOWN:
208 nb = x25_get_neigh(dev);
209 if (nb) {
210 x25_terminate_link(nb);
211 x25_neigh_put(nb);
212 }
213 break;
214 case NETDEV_DOWN:
215 x25_kill_by_device(dev);
216 x25_route_device_down(dev);
217 x25_link_device_down(dev);
218 break;
219 }
220 }
221
222 return NOTIFY_DONE;
223}
224
225/*
226 * Add a socket to the bound sockets list.
227 */
228static void x25_insert_socket(struct sock *sk)
229{
230 write_lock_bh(&x25_list_lock);
231 sk_add_node(sk, &x25_list);
232 write_unlock_bh(&x25_list_lock);
233}
234
235/*
236 * Find a socket that wants to accept the Call Request we just
237 * received. Check the full list for an address/cud match.
238 * If no cuds match return the next_best thing, an address match.
239 * Note: if a listening socket has cud set it must only get calls
240 * with matching cud.
241 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700242static struct sock *x25_find_listener(struct x25_address *addr,
243 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct sock *s;
246 struct sock *next_best;
247 struct hlist_node *node;
248
249 read_lock_bh(&x25_list_lock);
250 next_best = NULL;
251
252 sk_for_each(s, node, &x25_list)
253 if ((!strcmp(addr->x25_addr,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700254 x25_sk(s)->source_addr.x25_addr) ||
255 !strcmp(addr->x25_addr,
256 null_x25_address.x25_addr)) &&
257 s->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 /*
259 * Found a listening socket, now check the incoming
260 * call user data vs this sockets call user data
261 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700262 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900263 if((memcmp(x25_sk(s)->calluserdata.cuddata,
264 skb->data,
Shaun Pereiracb65d502005-06-22 22:15:01 -0700265 x25_sk(s)->cudmatchlength)) == 0) {
266 sock_hold(s);
267 goto found;
268 }
269 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 next_best = s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272 if (next_best) {
273 s = next_best;
274 sock_hold(s);
275 goto found;
276 }
277 s = NULL;
278found:
279 read_unlock_bh(&x25_list_lock);
280 return s;
281}
282
283/*
284 * Find a connected X.25 socket given my LCI and neighbour.
285 */
286static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
287{
288 struct sock *s;
289 struct hlist_node *node;
290
291 sk_for_each(s, node, &x25_list)
292 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
293 sock_hold(s);
294 goto found;
295 }
296 s = NULL;
297found:
298 return s;
299}
300
301struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
302{
303 struct sock *s;
304
305 read_lock_bh(&x25_list_lock);
306 s = __x25_find_socket(lci, nb);
307 read_unlock_bh(&x25_list_lock);
308 return s;
309}
310
311/*
312 * Find a unique LCI for a given device.
313 */
314static unsigned int x25_new_lci(struct x25_neigh *nb)
315{
316 unsigned int lci = 1;
317 struct sock *sk;
318
319 read_lock_bh(&x25_list_lock);
320
321 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
322 sock_put(sk);
323 if (++lci == 4096) {
324 lci = 0;
325 break;
326 }
327 }
328
329 read_unlock_bh(&x25_list_lock);
330 return lci;
331}
332
333/*
334 * Deferred destroy.
335 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700336static void __x25_destroy_socket(struct sock *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/*
339 * handler for deferred kills.
340 */
341static void x25_destroy_timer(unsigned long data)
342{
David S. Miller14ebaf82009-06-16 05:40:30 -0700343 x25_destroy_socket_from_timer((struct sock *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346/*
347 * This is called from user mode and the timers. Thus it protects itself
348 * against interrupt users but doesn't worry about being called during
349 * work. Once it is removed from the queue no interrupt or bottom half
350 * will touch it and we are (fairly 8-) ) safe.
351 * Not static as it's used by the timer
352 */
David S. Miller14ebaf82009-06-16 05:40:30 -0700353static void __x25_destroy_socket(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct sk_buff *skb;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 x25_stop_heartbeat(sk);
358 x25_stop_timer(sk);
359
360 x25_remove_socket(sk);
361 x25_clear_queues(sk); /* Flush the queues */
362
363 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
364 if (skb->sk != sk) { /* A pending connection */
365 /*
366 * Queue the unaccepted socket for death
367 */
368 sock_set_flag(skb->sk, SOCK_DEAD);
369 x25_start_heartbeat(skb->sk);
370 x25_sk(skb->sk)->state = X25_STATE_0;
371 }
372
373 kfree_skb(skb);
374 }
375
Eric Dumazetc5640392009-06-16 10:12:03 +0000376 if (sk_has_allocations(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 /* Defer: outstanding buffers */
378 sk->sk_timer.expires = jiffies + 10 * HZ;
379 sk->sk_timer.function = x25_destroy_timer;
380 sk->sk_timer.data = (unsigned long)sk;
381 add_timer(&sk->sk_timer);
382 } else {
383 /* drop last reference so sock_put will free */
384 __sock_put(sk);
385 }
David S. Miller14ebaf82009-06-16 05:40:30 -0700386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
David S. Miller14ebaf82009-06-16 05:40:30 -0700388void x25_destroy_socket_from_timer(struct sock *sk)
389{
390 sock_hold(sk);
391 bh_lock_sock(sk);
392 __x25_destroy_socket(sk);
393 bh_unlock_sock(sk);
394 sock_put(sk);
395}
396
397static void x25_destroy_socket(struct sock *sk)
398{
399 sock_hold(sk);
400 lock_sock(sk);
401 __x25_destroy_socket(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 release_sock(sk);
403 sock_put(sk);
404}
405
406/*
407 * Handling for system calls applied via the various interfaces to a
408 * X.25 socket object.
409 */
410
411static int x25_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700412 char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int opt;
415 struct sock *sk = sock->sk;
416 int rc = -ENOPROTOOPT;
417
Arnd Bergmann91774902009-11-05 04:37:29 +0000418 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (level != SOL_X25 || optname != X25_QBITINCL)
420 goto out;
421
422 rc = -EINVAL;
423 if (optlen < sizeof(int))
424 goto out;
425
426 rc = -EFAULT;
427 if (get_user(opt, (int __user *)optval))
428 goto out;
429
430 x25_sk(sk)->qbitincl = !!opt;
431 rc = 0;
432out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000433 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return rc;
435}
436
437static int x25_getsockopt(struct socket *sock, int level, int optname,
438 char __user *optval, int __user *optlen)
439{
440 struct sock *sk = sock->sk;
441 int val, len, rc = -ENOPROTOOPT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900442
Arnd Bergmann91774902009-11-05 04:37:29 +0000443 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (level != SOL_X25 || optname != X25_QBITINCL)
445 goto out;
446
447 rc = -EFAULT;
448 if (get_user(len, optlen))
449 goto out;
450
451 len = min_t(unsigned int, len, sizeof(int));
452
453 rc = -EINVAL;
454 if (len < 0)
455 goto out;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 rc = -EFAULT;
458 if (put_user(len, optlen))
459 goto out;
460
461 val = x25_sk(sk)->qbitincl;
462 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
463out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000464 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 return rc;
466}
467
468static int x25_listen(struct socket *sock, int backlog)
469{
470 struct sock *sk = sock->sk;
471 int rc = -EOPNOTSUPP;
472
Arnd Bergmann91774902009-11-05 04:37:29 +0000473 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (sk->sk_state != TCP_LISTEN) {
475 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
476 sk->sk_max_ack_backlog = backlog;
477 sk->sk_state = TCP_LISTEN;
478 rc = 0;
479 }
Arnd Bergmann91774902009-11-05 04:37:29 +0000480 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 return rc;
483}
484
485static struct proto x25_proto = {
486 .name = "X25",
487 .owner = THIS_MODULE,
488 .obj_size = sizeof(struct x25_sock),
489};
490
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700491static struct sock *x25_alloc_socket(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 struct x25_sock *x25;
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700494 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 if (!sk)
497 goto out;
498
499 sock_init_data(NULL, sk);
500
501 x25 = x25_sk(sk);
502 skb_queue_head_init(&x25->ack_queue);
503 skb_queue_head_init(&x25->fragment_queue);
504 skb_queue_head_init(&x25->interrupt_in_queue);
505 skb_queue_head_init(&x25->interrupt_out_queue);
506out:
507 return sk;
508}
509
Eric Paris3f378b62009-11-05 22:18:14 -0800510static int x25_create(struct net *net, struct socket *sock, int protocol,
511 int kern)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct sock *sk;
514 struct x25_sock *x25;
andrew hendryb18e7a02010-02-14 02:00:11 +0000515 int rc = -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800517 if (!net_eq(net, &init_net))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 goto out;
519
andrew hendryb18e7a02010-02-14 02:00:11 +0000520 rc = -ESOCKTNOSUPPORT;
521 if (sock->type != SOCK_SEQPACKET)
522 goto out;
523
524 rc = -EINVAL;
525 if (protocol)
526 goto out;
527
528 rc = -ENOBUFS;
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700529 if ((sk = x25_alloc_socket(net)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 goto out;
531
532 x25 = x25_sk(sk);
533
534 sock_init_data(sock, sk);
535
536 x25_init_timers(sk);
537
538 sock->ops = &x25_proto_ops;
539 sk->sk_protocol = protocol;
540 sk->sk_backlog_rcv = x25_backlog_rcv;
541
542 x25->t21 = sysctl_x25_call_request_timeout;
543 x25->t22 = sysctl_x25_reset_request_timeout;
544 x25->t23 = sysctl_x25_clear_request_timeout;
545 x25->t2 = sysctl_x25_ack_holdback_timeout;
546 x25->state = X25_STATE_0;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700547 x25->cudmatchlength = 0;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700548 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
549 /* on call accept */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
552 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
553 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
554 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
555 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
556 x25->facilities.reverse = X25_DEFAULT_REVERSE;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900557 x25->dte_facilities.calling_len = 0;
558 x25->dte_facilities.called_len = 0;
559 memset(x25->dte_facilities.called_ae, '\0',
560 sizeof(x25->dte_facilities.called_ae));
561 memset(x25->dte_facilities.calling_ae, '\0',
562 sizeof(x25->dte_facilities.calling_ae));
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 rc = 0;
565out:
566 return rc;
567}
568
569static struct sock *x25_make_new(struct sock *osk)
570{
571 struct sock *sk = NULL;
572 struct x25_sock *x25, *ox25;
573
574 if (osk->sk_type != SOCK_SEQPACKET)
575 goto out;
576
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900577 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 goto out;
579
580 x25 = x25_sk(sk);
581
582 sk->sk_type = osk->sk_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 sk->sk_priority = osk->sk_priority;
584 sk->sk_protocol = osk->sk_protocol;
585 sk->sk_rcvbuf = osk->sk_rcvbuf;
586 sk->sk_sndbuf = osk->sk_sndbuf;
587 sk->sk_state = TCP_ESTABLISHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
Shaun Pereiraa20a8552006-01-06 13:11:35 -0800589 sock_copy_flags(sk, osk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 ox25 = x25_sk(osk);
592 x25->t21 = ox25->t21;
593 x25->t22 = ox25->t22;
594 x25->t23 = ox25->t23;
595 x25->t2 = ox25->t2;
596 x25->facilities = ox25->facilities;
597 x25->qbitincl = ox25->qbitincl;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800598 x25->dte_facilities = ox25->dte_facilities;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700599 x25->cudmatchlength = ox25->cudmatchlength;
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700600 x25->accptapprv = ox25->accptapprv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 x25_init_timers(sk);
603out:
604 return sk;
605}
606
607static int x25_release(struct socket *sock)
608{
609 struct sock *sk = sock->sk;
610 struct x25_sock *x25;
611
Arnd Bergmann91774902009-11-05 04:37:29 +0000612 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (!sk)
614 goto out;
615
616 x25 = x25_sk(sk);
617
618 switch (x25->state) {
619
620 case X25_STATE_0:
621 case X25_STATE_2:
622 x25_disconnect(sk, 0, 0, 0);
623 x25_destroy_socket(sk);
624 goto out;
625
626 case X25_STATE_1:
627 case X25_STATE_3:
628 case X25_STATE_4:
629 x25_clear_queues(sk);
630 x25_write_internal(sk, X25_CLEAR_REQUEST);
631 x25_start_t23timer(sk);
632 x25->state = X25_STATE_2;
633 sk->sk_state = TCP_CLOSE;
634 sk->sk_shutdown |= SEND_SHUTDOWN;
635 sk->sk_state_change(sk);
636 sock_set_flag(sk, SOCK_DEAD);
637 sock_set_flag(sk, SOCK_DESTROY);
638 break;
639 }
640
David S. Millerc751e4f2008-06-17 03:05:13 -0700641 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000643 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
645}
646
647static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
648{
649 struct sock *sk = sock->sk;
650 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
Arnd Bergmann91774902009-11-05 04:37:29 +0000651 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Arnd Bergmann91774902009-11-05 04:37:29 +0000653 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 if (!sock_flag(sk, SOCK_ZAPPED) ||
655 addr_len != sizeof(struct sockaddr_x25) ||
Arnd Bergmann91774902009-11-05 04:37:29 +0000656 addr->sx25_family != AF_X25) {
657 rc = -EINVAL;
658 goto out;
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 x25_sk(sk)->source_addr = addr->sx25_addr;
662 x25_insert_socket(sk);
663 sock_reset_flag(sk, SOCK_ZAPPED);
664 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
Arnd Bergmann91774902009-11-05 04:37:29 +0000665out:
666 unlock_kernel();
667 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static int x25_wait_for_connection_establishment(struct sock *sk)
671{
672 DECLARE_WAITQUEUE(wait, current);
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900673 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 add_wait_queue_exclusive(sk->sk_sleep, &wait);
676 for (;;) {
677 __set_current_state(TASK_INTERRUPTIBLE);
678 rc = -ERESTARTSYS;
679 if (signal_pending(current))
680 break;
681 rc = sock_error(sk);
682 if (rc) {
683 sk->sk_socket->state = SS_UNCONNECTED;
684 break;
685 }
686 rc = 0;
687 if (sk->sk_state != TCP_ESTABLISHED) {
688 release_sock(sk);
689 schedule();
690 lock_sock(sk);
691 } else
692 break;
693 }
694 __set_current_state(TASK_RUNNING);
695 remove_wait_queue(sk->sk_sleep, &wait);
696 return rc;
697}
698
699static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
700 int addr_len, int flags)
701{
702 struct sock *sk = sock->sk;
703 struct x25_sock *x25 = x25_sk(sk);
704 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
705 struct x25_route *rt;
706 int rc = 0;
707
Arnd Bergmann91774902009-11-05 04:37:29 +0000708 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 lock_sock(sk);
710 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
711 sock->state = SS_CONNECTED;
712 goto out; /* Connect completed during a ERESTARTSYS event */
713 }
714
715 rc = -ECONNREFUSED;
716 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
717 sock->state = SS_UNCONNECTED;
718 goto out;
719 }
720
721 rc = -EISCONN; /* No reconnect on a seqpacket socket */
722 if (sk->sk_state == TCP_ESTABLISHED)
723 goto out;
724
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900725 sk->sk_state = TCP_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 sock->state = SS_UNCONNECTED;
727
728 rc = -EINVAL;
729 if (addr_len != sizeof(struct sockaddr_x25) ||
730 addr->sx25_family != AF_X25)
731 goto out;
732
733 rc = -ENETUNREACH;
734 rt = x25_get_route(&addr->sx25_addr);
735 if (!rt)
736 goto out;
737
738 x25->neighbour = x25_get_neigh(rt->dev);
739 if (!x25->neighbour)
740 goto out_put_route;
741
742 x25_limit_facilities(&x25->facilities, x25->neighbour);
743
744 x25->lci = x25_new_lci(x25->neighbour);
745 if (!x25->lci)
746 goto out_put_neigh;
747
748 rc = -EINVAL;
749 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
750 goto out_put_neigh;
751
752 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
753 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
754
755 x25->dest_addr = addr->sx25_addr;
756
757 /* Move to connecting socket, start sending Connect Requests */
758 sock->state = SS_CONNECTING;
759 sk->sk_state = TCP_SYN_SENT;
760
761 x25->state = X25_STATE_1;
762
763 x25_write_internal(sk, X25_CALL_REQUEST);
764
765 x25_start_heartbeat(sk);
766 x25_start_t21timer(sk);
767
768 /* Now the loop */
769 rc = -EINPROGRESS;
770 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
771 goto out_put_neigh;
772
773 rc = x25_wait_for_connection_establishment(sk);
774 if (rc)
775 goto out_put_neigh;
776
777 sock->state = SS_CONNECTED;
778 rc = 0;
779out_put_neigh:
780 if (rc)
781 x25_neigh_put(x25->neighbour);
782out_put_route:
783 x25_route_put(rt);
784out:
785 release_sock(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000786 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return rc;
788}
789
Shaun Pereirabac37ec2006-03-22 00:00:40 -0800790static int x25_wait_for_data(struct sock *sk, long timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 DECLARE_WAITQUEUE(wait, current);
793 int rc = 0;
794
795 add_wait_queue_exclusive(sk->sk_sleep, &wait);
796 for (;;) {
797 __set_current_state(TASK_INTERRUPTIBLE);
798 if (sk->sk_shutdown & RCV_SHUTDOWN)
799 break;
800 rc = -ERESTARTSYS;
801 if (signal_pending(current))
802 break;
803 rc = -EAGAIN;
804 if (!timeout)
805 break;
806 rc = 0;
807 if (skb_queue_empty(&sk->sk_receive_queue)) {
808 release_sock(sk);
809 timeout = schedule_timeout(timeout);
810 lock_sock(sk);
811 } else
812 break;
813 }
814 __set_current_state(TASK_RUNNING);
815 remove_wait_queue(sk->sk_sleep, &wait);
816 return rc;
817}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
820{
821 struct sock *sk = sock->sk;
822 struct sock *newsk;
823 struct sk_buff *skb;
824 int rc = -EINVAL;
825
Arnd Bergmann91774902009-11-05 04:37:29 +0000826 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (!sk || sk->sk_state != TCP_LISTEN)
828 goto out;
829
830 rc = -EOPNOTSUPP;
831 if (sk->sk_type != SOCK_SEQPACKET)
832 goto out;
833
834 lock_sock(sk);
835 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
836 if (rc)
837 goto out2;
838 skb = skb_dequeue(&sk->sk_receive_queue);
839 rc = -EINVAL;
840 if (!skb->sk)
841 goto out2;
842 newsk = skb->sk;
David S. Millerb61d38e2008-06-17 02:44:35 -0700843 sock_graft(newsk, newsock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Now attach up the new socket */
846 skb->sk = NULL;
847 kfree_skb(skb);
848 sk->sk_ack_backlog--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 newsock->state = SS_CONNECTED;
850 rc = 0;
851out2:
852 release_sock(sk);
853out:
Arnd Bergmann91774902009-11-05 04:37:29 +0000854 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 return rc;
856}
857
858static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
859 int *uaddr_len, int peer)
860{
861 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
862 struct sock *sk = sock->sk;
863 struct x25_sock *x25 = x25_sk(sk);
Arnd Bergmann91774902009-11-05 04:37:29 +0000864 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Arnd Bergmann91774902009-11-05 04:37:29 +0000866 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (peer) {
Arnd Bergmann91774902009-11-05 04:37:29 +0000868 if (sk->sk_state != TCP_ESTABLISHED) {
869 rc = -ENOTCONN;
870 goto out;
871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 sx25->sx25_addr = x25->dest_addr;
873 } else
874 sx25->sx25_addr = x25->source_addr;
875
876 sx25->sx25_family = AF_X25;
877 *uaddr_len = sizeof(*sx25);
878
Arnd Bergmann91774902009-11-05 04:37:29 +0000879out:
880 unlock_kernel();
881 return rc;
882}
883
884static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
885 poll_table *wait)
886{
887 int rc;
888
889 lock_kernel();
890 rc = datagram_poll(file, sock, wait);
891 unlock_kernel();
892
893 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +0900895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
897 unsigned int lci)
898{
899 struct sock *sk;
900 struct sock *make;
901 struct x25_sock *makex25;
902 struct x25_address source_addr, dest_addr;
903 struct x25_facilities facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800904 struct x25_dte_facilities dte_facilities;
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800905 int len, addr_len, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /*
908 * Remove the LCI and frame type.
909 */
910 skb_pull(skb, X25_STD_MIN_LEN);
911
912 /*
913 * Extract the X.25 addresses and convert them to ASCII strings,
914 * and remove them.
915 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800916 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
917 skb_pull(skb, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 /*
920 * Get the length of the facilities, skip past them for the moment
921 * get the call user data because this is needed to determine
922 * the correct listener
923 */
924 len = skb->data[0] + 1;
925 skb_pull(skb,len);
926
927 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 * Find a listener for the particular address/cud pair.
929 */
Shaun Pereiracb65d502005-06-22 22:15:01 -0700930 sk = x25_find_listener(&source_addr,skb);
931 skb_push(skb,len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800933 if (sk != NULL && sk_acceptq_is_full(sk)) {
934 goto out_sock_put;
935 }
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 /*
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800938 * We dont have any listeners for this incoming call.
939 * Try forwarding it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800941 if (sk == NULL) {
942 skb_push(skb, addr_len + X25_STD_MIN_LEN);
Andrew Hendry39e21c02007-02-08 13:34:36 -0800943 if (sysctl_x25_forward &&
944 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
Andrew Hendry95a9dc42007-02-08 13:34:02 -0800945 {
946 /* Call was forwarded, dont process it any more */
947 kfree_skb(skb);
948 rc = 1;
949 goto out;
950 } else {
951 /* No listeners, can't forward, clear the call */
952 goto out_clear_request;
953 }
954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /*
957 * Try to reach a compromise on the requested facilities.
958 */
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800959 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
960 if (len == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 goto out_sock_put;
962
963 /*
964 * current neighbour/link might impose additional limits
965 * on certain facilties
966 */
967
968 x25_limit_facilities(&facilities, nb);
969
970 /*
971 * Try to create a new socket.
972 */
973 make = x25_make_new(sk);
974 if (!make)
975 goto out_sock_put;
976
977 /*
978 * Remove the facilities
979 */
980 skb_pull(skb, len);
981
982 skb->sk = make;
983 make->sk_state = TCP_ESTABLISHED;
984
985 makex25 = x25_sk(make);
986 makex25->lci = lci;
987 makex25->dest_addr = dest_addr;
988 makex25->source_addr = source_addr;
989 makex25->neighbour = nb;
990 makex25->facilities = facilities;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800991 makex25->dte_facilities= dte_facilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700993 /* ensure no reverse facil on accept */
994 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
Shaun Pereiraa64b7b92006-03-22 00:01:31 -0800995 /* ensure no calling address extension on accept */
996 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
Shaun Pereiracb65d502005-06-22 22:15:01 -0700997 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Shaun Pereiraebc3f642005-06-22 22:16:17 -0700999 /* Normally all calls are accepted immediatly */
1000 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1001 x25_write_internal(make, X25_CALL_ACCEPTED);
1002 makex25->state = X25_STATE_3;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Shaun Pereiracb65d502005-06-22 22:15:01 -07001005 /*
1006 * Incoming Call User Data.
1007 */
Roel Kluin8db09f22009-03-13 16:04:12 -07001008 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1009 makex25->calluserdata.cudlength = skb->len;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 sk->sk_ack_backlog++;
1012
1013 x25_insert_socket(make);
1014
1015 skb_queue_head(&sk->sk_receive_queue, skb);
1016
1017 x25_start_heartbeat(make);
1018
1019 if (!sock_flag(sk, SOCK_DEAD))
1020 sk->sk_data_ready(sk, skb->len);
1021 rc = 1;
1022 sock_put(sk);
1023out:
1024 return rc;
1025out_sock_put:
1026 sock_put(sk);
1027out_clear_request:
1028 rc = 0;
1029 x25_transmit_clear_request(nb, lci, 0x01);
1030 goto out;
1031}
1032
1033static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1034 struct msghdr *msg, size_t len)
1035{
1036 struct sock *sk = sock->sk;
1037 struct x25_sock *x25 = x25_sk(sk);
1038 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1039 struct sockaddr_x25 sx25;
1040 struct sk_buff *skb;
1041 unsigned char *asmptr;
1042 int noblock = msg->msg_flags & MSG_DONTWAIT;
1043 size_t size;
1044 int qbit = 0, rc = -EINVAL;
1045
Arnd Bergmann91774902009-11-05 04:37:29 +00001046 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1048 goto out;
1049
1050 /* we currently don't support segmented records at the user interface */
1051 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1052 goto out;
1053
1054 rc = -EADDRNOTAVAIL;
1055 if (sock_flag(sk, SOCK_ZAPPED))
1056 goto out;
1057
1058 rc = -EPIPE;
1059 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1060 send_sig(SIGPIPE, current, 0);
1061 goto out;
1062 }
1063
1064 rc = -ENETUNREACH;
1065 if (!x25->neighbour)
1066 goto out;
1067
1068 if (usx25) {
1069 rc = -EINVAL;
1070 if (msg->msg_namelen < sizeof(sx25))
1071 goto out;
1072 memcpy(&sx25, usx25, sizeof(sx25));
1073 rc = -EISCONN;
1074 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1075 goto out;
1076 rc = -EINVAL;
1077 if (sx25.sx25_family != AF_X25)
1078 goto out;
1079 } else {
1080 /*
1081 * FIXME 1003.1g - if the socket is like this because
1082 * it has become closed (not started closed) we ought
1083 * to SIGPIPE, EPIPE;
1084 */
1085 rc = -ENOTCONN;
1086 if (sk->sk_state != TCP_ESTABLISHED)
1087 goto out;
1088
1089 sx25.sx25_family = AF_X25;
1090 sx25.sx25_addr = x25->dest_addr;
1091 }
1092
Alan Cox83e0bbc2009-03-27 00:28:21 -07001093 /* Sanity check the packet size */
1094 if (len > 65535) {
1095 rc = -EMSGSIZE;
1096 goto out;
1097 }
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1100
1101 /* Build a packet */
1102 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1103
1104 if ((msg->msg_flags & MSG_OOB) && len > 32)
1105 len = 32;
1106
1107 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1108
1109 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1110 if (!skb)
1111 goto out;
1112 X25_SKB_CB(skb)->flags = msg->msg_flags;
1113
1114 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1115
1116 /*
1117 * Put the data on the end
1118 */
1119 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1120
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001121 skb_reset_transport_header(skb);
1122 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Arnaldo Carvalho de Meloeeeb0372007-03-14 21:04:34 -03001124 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (rc)
1126 goto out_kfree_skb;
1127
1128 /*
1129 * If the Q BIT Include socket option is in force, the first
1130 * byte of the user data is the logical value of the Q Bit.
1131 */
1132 if (x25->qbitincl) {
1133 qbit = skb->data[0];
1134 skb_pull(skb, 1);
1135 }
1136
1137 /*
1138 * Push down the X.25 header
1139 */
1140 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1141
1142 if (msg->msg_flags & MSG_OOB) {
1143 if (x25->neighbour->extended) {
1144 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1145 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1146 *asmptr++ = (x25->lci >> 0) & 0xFF;
1147 *asmptr++ = X25_INTERRUPT;
1148 } else {
1149 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1150 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1151 *asmptr++ = (x25->lci >> 0) & 0xFF;
1152 *asmptr++ = X25_INTERRUPT;
1153 }
1154 } else {
1155 if (x25->neighbour->extended) {
1156 /* Build an Extended X.25 header */
1157 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1158 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1159 *asmptr++ = (x25->lci >> 0) & 0xFF;
1160 *asmptr++ = X25_DATA;
1161 *asmptr++ = X25_DATA;
1162 } else {
1163 /* Build an Standard X.25 header */
1164 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1165 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1166 *asmptr++ = (x25->lci >> 0) & 0xFF;
1167 *asmptr++ = X25_DATA;
1168 }
1169
1170 if (qbit)
1171 skb->data[0] |= X25_Q_BIT;
1172 }
1173
1174 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1175 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1176
1177 rc = -ENOTCONN;
1178 if (sk->sk_state != TCP_ESTABLISHED)
1179 goto out_kfree_skb;
1180
1181 if (msg->msg_flags & MSG_OOB)
1182 skb_queue_tail(&x25->interrupt_out_queue, skb);
1183 else {
Roel Kluin8db09f22009-03-13 16:04:12 -07001184 rc = x25_output(sk, skb);
1185 len = rc;
1186 if (rc < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 kfree_skb(skb);
1188 else if (x25->qbitincl)
1189 len++;
1190 }
1191
1192 /*
1193 * lock_sock() is currently only used to serialize this x25_kick()
1194 * against input-driven x25_kick() calls. It currently only blocks
1195 * incoming packets for this socket and does not protect against
1196 * any other socket state changes and is not called from anywhere
1197 * else. As x25_kick() cannot block and as long as all socket
1198 * operations are BKL-wrapped, we don't need take to care about
1199 * purging the backlog queue in x25_release().
1200 *
1201 * Using lock_sock() to protect all socket operations entirely
1202 * (and making the whole x25 stack SMP aware) unfortunately would
1203 * require major changes to {send,recv}msg and skb allocation methods.
1204 * -> 2.5 ;)
1205 */
1206 lock_sock(sk);
1207 x25_kick(sk);
1208 release_sock(sk);
1209 rc = len;
1210out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001211 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return rc;
1213out_kfree_skb:
1214 kfree_skb(skb);
1215 goto out;
1216}
1217
1218
1219static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1220 struct msghdr *msg, size_t size,
1221 int flags)
1222{
1223 struct sock *sk = sock->sk;
1224 struct x25_sock *x25 = x25_sk(sk);
1225 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1226 size_t copied;
1227 int qbit;
1228 struct sk_buff *skb;
1229 unsigned char *asmptr;
1230 int rc = -ENOTCONN;
1231
Arnd Bergmann91774902009-11-05 04:37:29 +00001232 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 /*
1234 * This works for seqpacket too. The receiver has ordered the queue for
1235 * us! We do one quick check first though
1236 */
1237 if (sk->sk_state != TCP_ESTABLISHED)
1238 goto out;
1239
1240 if (flags & MSG_OOB) {
1241 rc = -EINVAL;
1242 if (sock_flag(sk, SOCK_URGINLINE) ||
1243 !skb_peek(&x25->interrupt_in_queue))
1244 goto out;
1245
1246 skb = skb_dequeue(&x25->interrupt_in_queue);
1247
1248 skb_pull(skb, X25_STD_MIN_LEN);
1249
1250 /*
1251 * No Q bit information on Interrupt data.
1252 */
1253 if (x25->qbitincl) {
1254 asmptr = skb_push(skb, 1);
1255 *asmptr = 0x00;
1256 }
1257
1258 msg->msg_flags |= MSG_OOB;
1259 } else {
1260 /* Now we can treat all alike */
1261 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1262 flags & MSG_DONTWAIT, &rc);
1263 if (!skb)
1264 goto out;
1265
1266 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1267
1268 skb_pull(skb, x25->neighbour->extended ?
1269 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1270
1271 if (x25->qbitincl) {
1272 asmptr = skb_push(skb, 1);
1273 *asmptr = qbit;
1274 }
1275 }
1276
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001277 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 copied = skb->len;
1279
1280 if (copied > size) {
1281 copied = size;
1282 msg->msg_flags |= MSG_TRUNC;
1283 }
1284
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001285 /* Currently, each datagram always contains a complete record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 msg->msg_flags |= MSG_EOR;
1287
1288 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1289 if (rc)
1290 goto out_free_dgram;
1291
1292 if (sx25) {
1293 sx25->sx25_family = AF_X25;
1294 sx25->sx25_addr = x25->dest_addr;
1295 }
1296
1297 msg->msg_namelen = sizeof(struct sockaddr_x25);
1298
1299 lock_sock(sk);
1300 x25_check_rbuf(sk);
1301 release_sock(sk);
1302 rc = copied;
1303out_free_dgram:
1304 skb_free_datagram(sk, skb);
1305out:
Arnd Bergmann91774902009-11-05 04:37:29 +00001306 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return rc;
1308}
1309
1310
1311static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1312{
1313 struct sock *sk = sock->sk;
1314 struct x25_sock *x25 = x25_sk(sk);
1315 void __user *argp = (void __user *)arg;
1316 int rc;
1317
Arnd Bergmann91774902009-11-05 04:37:29 +00001318 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 switch (cmd) {
1320 case TIOCOUTQ: {
Eric Dumazet31e6d362009-06-17 19:05:41 -07001321 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1322
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 if (amount < 0)
1324 amount = 0;
1325 rc = put_user(amount, (unsigned int __user *)argp);
1326 break;
1327 }
1328
1329 case TIOCINQ: {
1330 struct sk_buff *skb;
1331 int amount = 0;
1332 /*
1333 * These two are safe on a single CPU system as
1334 * only user tasks fiddle here
1335 */
1336 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1337 amount = skb->len;
1338 rc = put_user(amount, (unsigned int __user *)argp);
1339 break;
1340 }
1341
1342 case SIOCGSTAMP:
1343 rc = -EINVAL;
1344 if (sk)
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001345 rc = sock_get_timestamp(sk,
1346 (struct timeval __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001348 case SIOCGSTAMPNS:
1349 rc = -EINVAL;
1350 if (sk)
1351 rc = sock_get_timestampns(sk,
1352 (struct timespec __user *)argp);
1353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 case SIOCGIFADDR:
1355 case SIOCSIFADDR:
1356 case SIOCGIFDSTADDR:
1357 case SIOCSIFDSTADDR:
1358 case SIOCGIFBRDADDR:
1359 case SIOCSIFBRDADDR:
1360 case SIOCGIFNETMASK:
1361 case SIOCSIFNETMASK:
1362 case SIOCGIFMETRIC:
1363 case SIOCSIFMETRIC:
1364 rc = -EINVAL;
1365 break;
1366 case SIOCADDRT:
1367 case SIOCDELRT:
1368 rc = -EPERM;
1369 if (!capable(CAP_NET_ADMIN))
1370 break;
1371 rc = x25_route_ioctl(cmd, argp);
1372 break;
1373 case SIOCX25GSUBSCRIP:
1374 rc = x25_subscr_ioctl(cmd, argp);
1375 break;
1376 case SIOCX25SSUBSCRIP:
1377 rc = -EPERM;
1378 if (!capable(CAP_NET_ADMIN))
1379 break;
1380 rc = x25_subscr_ioctl(cmd, argp);
1381 break;
1382 case SIOCX25GFACILITIES: {
1383 struct x25_facilities fac = x25->facilities;
1384 rc = copy_to_user(argp, &fac,
1385 sizeof(fac)) ? -EFAULT : 0;
1386 break;
1387 }
1388
1389 case SIOCX25SFACILITIES: {
1390 struct x25_facilities facilities;
1391 rc = -EFAULT;
1392 if (copy_from_user(&facilities, argp,
1393 sizeof(facilities)))
1394 break;
1395 rc = -EINVAL;
1396 if (sk->sk_state != TCP_LISTEN &&
1397 sk->sk_state != TCP_CLOSE)
1398 break;
1399 if (facilities.pacsize_in < X25_PS16 ||
1400 facilities.pacsize_in > X25_PS4096)
1401 break;
1402 if (facilities.pacsize_out < X25_PS16 ||
1403 facilities.pacsize_out > X25_PS4096)
1404 break;
1405 if (facilities.winsize_in < 1 ||
1406 facilities.winsize_in > 127)
1407 break;
1408 if (facilities.throughput < 0x03 ||
1409 facilities.throughput > 0xDD)
1410 break;
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001411 if (facilities.reverse &&
roel kluin06a96b32009-10-07 00:59:42 +00001412 (facilities.reverse & 0x81) != 0x81)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 x25->facilities = facilities;
1415 rc = 0;
1416 break;
1417 }
1418
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001419 case SIOCX25GDTEFACILITIES: {
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001420 rc = copy_to_user(argp, &x25->dte_facilities,
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001421 sizeof(x25->dte_facilities));
1422 if (rc)
1423 rc = -EFAULT;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001424 break;
1425 }
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001426
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001427 case SIOCX25SDTEFACILITIES: {
1428 struct x25_dte_facilities dtefacs;
1429 rc = -EFAULT;
1430 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
Shaun Pereiraa64b7b92006-03-22 00:01:31 -08001431 break;
1432 rc = -EINVAL;
1433 if (sk->sk_state != TCP_LISTEN &&
1434 sk->sk_state != TCP_CLOSE)
1435 break;
1436 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1437 break;
1438 if (dtefacs.calling_ae == NULL)
1439 break;
1440 if (dtefacs.called_len > X25_MAX_AE_LEN)
1441 break;
1442 if (dtefacs.called_ae == NULL)
1443 break;
1444 x25->dte_facilities = dtefacs;
1445 rc = 0;
1446 break;
1447 }
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 case SIOCX25GCALLUSERDATA: {
1450 struct x25_calluserdata cud = x25->calluserdata;
1451 rc = copy_to_user(argp, &cud,
1452 sizeof(cud)) ? -EFAULT : 0;
1453 break;
1454 }
1455
1456 case SIOCX25SCALLUSERDATA: {
1457 struct x25_calluserdata calluserdata;
1458
1459 rc = -EFAULT;
1460 if (copy_from_user(&calluserdata, argp,
1461 sizeof(calluserdata)))
1462 break;
1463 rc = -EINVAL;
1464 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1465 break;
1466 x25->calluserdata = calluserdata;
1467 rc = 0;
1468 break;
1469 }
1470
1471 case SIOCX25GCAUSEDIAG: {
1472 struct x25_causediag causediag;
1473 causediag = x25->causediag;
1474 rc = copy_to_user(argp, &causediag,
1475 sizeof(causediag)) ? -EFAULT : 0;
1476 break;
1477 }
1478
Andrew Hendry386e50c2009-11-18 23:30:41 -08001479 case SIOCX25SCAUSEDIAG: {
1480 struct x25_causediag causediag;
1481 rc = -EFAULT;
1482 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1483 break;
1484 x25->causediag = causediag;
1485 rc = 0;
1486 break;
1487
1488 }
1489
Shaun Pereiracb65d502005-06-22 22:15:01 -07001490 case SIOCX25SCUDMATCHLEN: {
1491 struct x25_subaddr sub_addr;
1492 rc = -EINVAL;
1493 if(sk->sk_state != TCP_CLOSE)
1494 break;
1495 rc = -EFAULT;
1496 if (copy_from_user(&sub_addr, argp,
1497 sizeof(sub_addr)))
1498 break;
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001499 rc = -EINVAL;
Shaun Pereiracb65d502005-06-22 22:15:01 -07001500 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1501 break;
1502 x25->cudmatchlength = sub_addr.cudmatchlength;
1503 rc = 0;
1504 break;
1505 }
1506
Shaun Pereiraebc3f642005-06-22 22:16:17 -07001507 case SIOCX25CALLACCPTAPPRV: {
1508 rc = -EINVAL;
1509 if (sk->sk_state != TCP_CLOSE)
1510 break;
1511 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1512 rc = 0;
1513 break;
1514 }
1515
1516 case SIOCX25SENDCALLACCPT: {
1517 rc = -EINVAL;
1518 if (sk->sk_state != TCP_ESTABLISHED)
1519 break;
1520 if (x25->accptapprv) /* must call accptapprv above */
1521 break;
1522 x25_write_internal(sk, X25_CALL_ACCEPTED);
1523 x25->state = X25_STATE_3;
1524 rc = 0;
1525 break;
1526 }
1527
YOSHIFUJI Hideakif8e1d2012007-02-09 23:25:27 +09001528 default:
Christoph Hellwigb5e5fa52006-01-03 14:18:33 -08001529 rc = -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 break;
1531 }
Arnd Bergmann91774902009-11-05 04:37:29 +00001532 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 return rc;
1535}
1536
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00001537static const struct net_proto_family x25_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 .family = AF_X25,
1539 .create = x25_create,
1540 .owner = THIS_MODULE,
1541};
1542
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001543#ifdef CONFIG_COMPAT
1544static int compat_x25_subscr_ioctl(unsigned int cmd,
1545 struct compat_x25_subscrip_struct __user *x25_subscr32)
1546{
1547 struct compat_x25_subscrip_struct x25_subscr;
1548 struct x25_neigh *nb;
1549 struct net_device *dev;
1550 int rc = -EINVAL;
1551
1552 rc = -EFAULT;
1553 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1554 goto out;
1555
1556 rc = -EINVAL;
1557 dev = x25_dev_get(x25_subscr.device);
1558 if (dev == NULL)
1559 goto out;
1560
1561 nb = x25_get_neigh(dev);
1562 if (nb == NULL)
1563 goto out_dev_put;
1564
1565 dev_put(dev);
1566
1567 if (cmd == SIOCX25GSUBSCRIP) {
1568 x25_subscr.extended = nb->extended;
1569 x25_subscr.global_facil_mask = nb->global_facil_mask;
1570 rc = copy_to_user(x25_subscr32, &x25_subscr,
1571 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1572 } else {
1573 rc = -EINVAL;
1574 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1575 rc = 0;
1576 nb->extended = x25_subscr.extended;
1577 nb->global_facil_mask = x25_subscr.global_facil_mask;
1578 }
1579 }
1580 x25_neigh_put(nb);
1581out:
1582 return rc;
1583out_dev_put:
1584 dev_put(dev);
1585 goto out;
1586}
1587
1588static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1589 unsigned long arg)
1590{
1591 void __user *argp = compat_ptr(arg);
1592 struct sock *sk = sock->sk;
1593
1594 int rc = -ENOIOCTLCMD;
1595
1596 switch(cmd) {
1597 case TIOCOUTQ:
1598 case TIOCINQ:
1599 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1600 break;
1601 case SIOCGSTAMP:
1602 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001603 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001604 if (sk)
1605 rc = compat_sock_get_timestamp(sk,
1606 (struct timeval __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001607 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001608 break;
Eric Dumazetae40eb12007-03-18 17:33:16 -07001609 case SIOCGSTAMPNS:
1610 rc = -EINVAL;
Arnd Bergmann91774902009-11-05 04:37:29 +00001611 lock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001612 if (sk)
1613 rc = compat_sock_get_timestampns(sk,
1614 (struct timespec __user*)argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001615 unlock_kernel();
Eric Dumazetae40eb12007-03-18 17:33:16 -07001616 break;
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001617 case SIOCGIFADDR:
1618 case SIOCSIFADDR:
1619 case SIOCGIFDSTADDR:
1620 case SIOCSIFDSTADDR:
1621 case SIOCGIFBRDADDR:
1622 case SIOCSIFBRDADDR:
1623 case SIOCGIFNETMASK:
1624 case SIOCSIFNETMASK:
1625 case SIOCGIFMETRIC:
1626 case SIOCSIFMETRIC:
1627 rc = -EINVAL;
1628 break;
1629 case SIOCADDRT:
1630 case SIOCDELRT:
1631 rc = -EPERM;
1632 if (!capable(CAP_NET_ADMIN))
1633 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001634 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001635 rc = x25_route_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001636 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001637 break;
1638 case SIOCX25GSUBSCRIP:
Arnd Bergmann91774902009-11-05 04:37:29 +00001639 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001640 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001641 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001642 break;
1643 case SIOCX25SSUBSCRIP:
1644 rc = -EPERM;
1645 if (!capable(CAP_NET_ADMIN))
1646 break;
Arnd Bergmann91774902009-11-05 04:37:29 +00001647 lock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001648 rc = compat_x25_subscr_ioctl(cmd, argp);
Arnd Bergmann91774902009-11-05 04:37:29 +00001649 unlock_kernel();
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001650 break;
1651 case SIOCX25GFACILITIES:
1652 case SIOCX25SFACILITIES:
Shaun Pereira9a6b9f22006-03-22 00:02:00 -08001653 case SIOCX25GDTEFACILITIES:
1654 case SIOCX25SDTEFACILITIES:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001655 case SIOCX25GCALLUSERDATA:
1656 case SIOCX25SCALLUSERDATA:
1657 case SIOCX25GCAUSEDIAG:
Andrew Hendry386e50c2009-11-18 23:30:41 -08001658 case SIOCX25SCAUSEDIAG:
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001659 case SIOCX25SCUDMATCHLEN:
1660 case SIOCX25CALLACCPTAPPRV:
1661 case SIOCX25SENDCALLACCPT:
1662 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1663 break;
1664 default:
1665 rc = -ENOIOCTLCMD;
1666 break;
1667 }
1668 return rc;
1669}
1670#endif
1671
Arnd Bergmann91774902009-11-05 04:37:29 +00001672static const struct proto_ops x25_proto_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 .family = AF_X25,
1674 .owner = THIS_MODULE,
1675 .release = x25_release,
1676 .bind = x25_bind,
1677 .connect = x25_connect,
1678 .socketpair = sock_no_socketpair,
1679 .accept = x25_accept,
1680 .getname = x25_getname,
Arnd Bergmann91774902009-11-05 04:37:29 +00001681 .poll = x25_datagram_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 .ioctl = x25_ioctl,
Shaun Pereira1b06e6b2006-03-22 00:00:12 -08001683#ifdef CONFIG_COMPAT
1684 .compat_ioctl = compat_x25_ioctl,
1685#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 .listen = x25_listen,
1687 .shutdown = sock_no_shutdown,
1688 .setsockopt = x25_setsockopt,
1689 .getsockopt = x25_getsockopt,
1690 .sendmsg = x25_sendmsg,
1691 .recvmsg = x25_recvmsg,
1692 .mmap = sock_no_mmap,
1693 .sendpage = sock_no_sendpage,
1694};
1695
Stephen Hemminger7546dd92009-03-09 08:18:29 +00001696static struct packet_type x25_packet_type __read_mostly = {
Harvey Harrison09640e62009-02-01 00:45:17 -08001697 .type = cpu_to_be16(ETH_P_X25),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 .func = x25_lapb_receive_frame,
1699};
1700
1701static struct notifier_block x25_dev_notifier = {
1702 .notifier_call = x25_device_event,
1703};
1704
1705void x25_kill_by_neigh(struct x25_neigh *nb)
1706{
1707 struct sock *s;
1708 struct hlist_node *node;
1709
1710 write_lock_bh(&x25_list_lock);
1711
1712 sk_for_each(s, node, &x25_list)
1713 if (x25_sk(s)->neighbour == nb)
1714 x25_disconnect(s, ENETUNREACH, 0, 0);
1715
1716 write_unlock_bh(&x25_list_lock);
Andrew Hendry95a9dc42007-02-08 13:34:02 -08001717
1718 /* Remove any related forwards */
1719 x25_clear_forward_by_dev(nb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720}
1721
1722static int __init x25_init(void)
1723{
1724 int rc = proto_register(&x25_proto, 0);
1725
1726 if (rc != 0)
1727 goto out;
1728
andrew hendry1fd975a2009-11-24 15:15:42 +00001729 rc = sock_register(&x25_family_ops);
1730 if (rc != 0)
1731 goto out_proto;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 dev_add_pack(&x25_packet_type);
1734
andrew hendry1fd975a2009-11-24 15:15:42 +00001735 rc = register_netdevice_notifier(&x25_dev_notifier);
1736 if (rc != 0)
1737 goto out_sock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
maximilian attemsa44562e2008-01-28 20:43:16 -08001739 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 x25_register_sysctl();
andrew hendry1fd975a2009-11-24 15:15:42 +00001742 rc = x25_proc_init();
1743 if (rc != 0)
1744 goto out_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745out:
1746 return rc;
andrew hendry1fd975a2009-11-24 15:15:42 +00001747out_dev:
1748 unregister_netdevice_notifier(&x25_dev_notifier);
1749out_sock:
1750 sock_unregister(AF_X25);
1751out_proto:
1752 proto_unregister(&x25_proto);
1753 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755module_init(x25_init);
1756
1757static void __exit x25_exit(void)
1758{
1759 x25_proc_exit();
1760 x25_link_free();
1761 x25_route_free();
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 x25_unregister_sysctl();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
1765 unregister_netdevice_notifier(&x25_dev_notifier);
1766
1767 dev_remove_pack(&x25_packet_type);
1768
1769 sock_unregister(AF_X25);
1770 proto_unregister(&x25_proto);
1771}
1772module_exit(x25_exit);
1773
1774MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1775MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1776MODULE_LICENSE("GPL");
1777MODULE_ALIAS_NETPROTO(PF_X25);