aboutsummaryrefslogtreecommitdiffstats
path: root/net/x25
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/x25
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'net/x25')
-rw-r--r--net/x25/Makefile10
-rw-r--r--net/x25/af_x25.c1435
-rw-r--r--net/x25/sysctl_net_x25.c107
-rw-r--r--net/x25/x25_dev.c207
-rw-r--r--net/x25/x25_facilities.c231
-rw-r--r--net/x25/x25_in.c361
-rw-r--r--net/x25/x25_link.c401
-rw-r--r--net/x25/x25_out.c226
-rw-r--r--net/x25/x25_proc.c256
-rw-r--r--net/x25/x25_route.c221
-rw-r--r--net/x25/x25_subr.c374
-rw-r--r--net/x25/x25_timer.c176
12 files changed, 4005 insertions, 0 deletions
diff --git a/net/x25/Makefile b/net/x25/Makefile
new file mode 100644
index 000000000000..587a71aa411d
--- /dev/null
+++ b/net/x25/Makefile
@@ -0,0 +1,10 @@
1#
2# Makefile for the Linux X.25 Packet layer.
3#
4
5obj-$(CONFIG_X25) += x25.o
6
7x25-y := af_x25.o x25_dev.o x25_facilities.o x25_in.o \
8 x25_link.o x25_out.o x25_route.o x25_subr.o \
9 x25_timer.o x25_proc.o
10x25-$(CONFIG_SYSCTL) += sysctl_net_x25.o
diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c
new file mode 100644
index 000000000000..2a24b243b841
--- /dev/null
+++ b/net/x25/af_x25.c
@@ -0,0 +1,1435 @@
1/*
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
6 * screw up. It might even work.
7 *
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.
21 * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
22 * facilities negotiation and increased
23 * the throughput upper limit.
24 * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
25 * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
26 * 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
32 */
33
34#include <linux/config.h>
35#include <linux/module.h>
36#include <linux/errno.h>
37#include <linux/kernel.h>
38#include <linux/sched.h>
39#include <linux/timer.h>
40#include <linux/string.h>
41#include <linux/net.h>
42#include <linux/netdevice.h>
43#include <linux/if_arp.h>
44#include <linux/skbuff.h>
45#include <net/sock.h>
46#include <net/tcp.h>
47#include <asm/uaccess.h>
48#include <linux/fcntl.h>
49#include <linux/termios.h> /* For TIOCINQ/OUTQ */
50#include <linux/notifier.h>
51#include <linux/init.h>
52#include <net/x25.h>
53
54int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
55int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
56int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
57int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
58int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
59
60HLIST_HEAD(x25_list);
61DEFINE_RWLOCK(x25_list_lock);
62
63static struct proto_ops x25_proto_ops;
64
65static struct x25_address null_x25_address = {" "};
66
67int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
68 struct x25_address *calling_addr)
69{
70 int called_len, calling_len;
71 char *called, *calling;
72 int i;
73
74 called_len = (*p >> 0) & 0x0F;
75 calling_len = (*p >> 4) & 0x0F;
76
77 called = called_addr->x25_addr;
78 calling = calling_addr->x25_addr;
79 p++;
80
81 for (i = 0; i < (called_len + calling_len); i++) {
82 if (i < called_len) {
83 if (i % 2 != 0) {
84 *called++ = ((*p >> 0) & 0x0F) + '0';
85 p++;
86 } else {
87 *called++ = ((*p >> 4) & 0x0F) + '0';
88 }
89 } else {
90 if (i % 2 != 0) {
91 *calling++ = ((*p >> 0) & 0x0F) + '0';
92 p++;
93 } else {
94 *calling++ = ((*p >> 4) & 0x0F) + '0';
95 }
96 }
97 }
98
99 *called = *calling = '\0';
100
101 return 1 + (called_len + calling_len + 1) / 2;
102}
103
104int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
105 struct x25_address *calling_addr)
106{
107 unsigned int called_len, calling_len;
108 char *called, *calling;
109 int i;
110
111 called = called_addr->x25_addr;
112 calling = calling_addr->x25_addr;
113
114 called_len = strlen(called);
115 calling_len = strlen(calling);
116
117 *p++ = (calling_len << 4) | (called_len << 0);
118
119 for (i = 0; i < (called_len + calling_len); i++) {
120 if (i < called_len) {
121 if (i % 2 != 0) {
122 *p |= (*called++ - '0') << 0;
123 p++;
124 } else {
125 *p = 0x00;
126 *p |= (*called++ - '0') << 4;
127 }
128 } else {
129 if (i % 2 != 0) {
130 *p |= (*calling++ - '0') << 0;
131 p++;
132 } else {
133 *p = 0x00;
134 *p |= (*calling++ - '0') << 4;
135 }
136 }
137 }
138
139 return 1 + (called_len + calling_len + 1) / 2;
140}
141
142/*
143 * Socket removal during an interrupt is now safe.
144 */
145static void x25_remove_socket(struct sock *sk)
146{
147 write_lock_bh(&x25_list_lock);
148 sk_del_node_init(sk);
149 write_unlock_bh(&x25_list_lock);
150}
151
152/*
153 * Kill all bound sockets on a dropped device.
154 */
155static void x25_kill_by_device(struct net_device *dev)
156{
157 struct sock *s;
158 struct hlist_node *node;
159
160 write_lock_bh(&x25_list_lock);
161
162 sk_for_each(s, node, &x25_list)
163 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
164 x25_disconnect(s, ENETUNREACH, 0, 0);
165
166 write_unlock_bh(&x25_list_lock);
167}
168
169/*
170 * Handle device status changes.
171 */
172static int x25_device_event(struct notifier_block *this, unsigned long event,
173 void *ptr)
174{
175 struct net_device *dev = ptr;
176 struct x25_neigh *nb;
177
178 if (dev->type == ARPHRD_X25
179#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
180 || dev->type == ARPHRD_ETHER
181#endif
182 ) {
183 switch (event) {
184 case NETDEV_UP:
185 x25_link_device_up(dev);
186 break;
187 case NETDEV_GOING_DOWN:
188 nb = x25_get_neigh(dev);
189 if (nb) {
190 x25_terminate_link(nb);
191 x25_neigh_put(nb);
192 }
193 break;
194 case NETDEV_DOWN:
195 x25_kill_by_device(dev);
196 x25_route_device_down(dev);
197 x25_link_device_down(dev);
198 break;
199 }
200 }
201
202 return NOTIFY_DONE;
203}
204
205/*
206 * Add a socket to the bound sockets list.
207 */
208static void x25_insert_socket(struct sock *sk)
209{
210 write_lock_bh(&x25_list_lock);
211 sk_add_node(sk, &x25_list);
212 write_unlock_bh(&x25_list_lock);
213}
214
215/*
216 * Find a socket that wants to accept the Call Request we just
217 * received. Check the full list for an address/cud match.
218 * If no cuds match return the next_best thing, an address match.
219 * Note: if a listening socket has cud set it must only get calls
220 * with matching cud.
221 */
222static struct sock *x25_find_listener(struct x25_address *addr, struct x25_calluserdata *calluserdata)
223{
224 struct sock *s;
225 struct sock *next_best;
226 struct hlist_node *node;
227
228 read_lock_bh(&x25_list_lock);
229 next_best = NULL;
230
231 sk_for_each(s, node, &x25_list)
232 if ((!strcmp(addr->x25_addr,
233 x25_sk(s)->source_addr.x25_addr) ||
234 !strcmp(addr->x25_addr,
235 null_x25_address.x25_addr)) &&
236 s->sk_state == TCP_LISTEN) {
237
238 /*
239 * Found a listening socket, now check the incoming
240 * call user data vs this sockets call user data
241 */
242 if (x25_check_calluserdata(&x25_sk(s)->calluserdata, calluserdata)) {
243 sock_hold(s);
244 goto found;
245 }
246 if (x25_sk(s)->calluserdata.cudlength == 0) {
247 next_best = s;
248 }
249 }
250 if (next_best) {
251 s = next_best;
252 sock_hold(s);
253 goto found;
254 }
255 s = NULL;
256found:
257 read_unlock_bh(&x25_list_lock);
258 return s;
259}
260
261/*
262 * Find a connected X.25 socket given my LCI and neighbour.
263 */
264static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
265{
266 struct sock *s;
267 struct hlist_node *node;
268
269 sk_for_each(s, node, &x25_list)
270 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
271 sock_hold(s);
272 goto found;
273 }
274 s = NULL;
275found:
276 return s;
277}
278
279struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
280{
281 struct sock *s;
282
283 read_lock_bh(&x25_list_lock);
284 s = __x25_find_socket(lci, nb);
285 read_unlock_bh(&x25_list_lock);
286 return s;
287}
288
289/*
290 * Find a unique LCI for a given device.
291 */
292static unsigned int x25_new_lci(struct x25_neigh *nb)
293{
294 unsigned int lci = 1;
295 struct sock *sk;
296
297 read_lock_bh(&x25_list_lock);
298
299 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
300 sock_put(sk);
301 if (++lci == 4096) {
302 lci = 0;
303 break;
304 }
305 }
306
307 read_unlock_bh(&x25_list_lock);
308 return lci;
309}
310
311/*
312 * Deferred destroy.
313 */
314void x25_destroy_socket(struct sock *);
315
316/*
317 * handler for deferred kills.
318 */
319static void x25_destroy_timer(unsigned long data)
320{
321 x25_destroy_socket((struct sock *)data);
322}
323
324/*
325 * This is called from user mode and the timers. Thus it protects itself
326 * against interrupt users but doesn't worry about being called during
327 * work. Once it is removed from the queue no interrupt or bottom half
328 * will touch it and we are (fairly 8-) ) safe.
329 * Not static as it's used by the timer
330 */
331void x25_destroy_socket(struct sock *sk)
332{
333 struct sk_buff *skb;
334
335 sock_hold(sk);
336 lock_sock(sk);
337 x25_stop_heartbeat(sk);
338 x25_stop_timer(sk);
339
340 x25_remove_socket(sk);
341 x25_clear_queues(sk); /* Flush the queues */
342
343 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
344 if (skb->sk != sk) { /* A pending connection */
345 /*
346 * Queue the unaccepted socket for death
347 */
348 sock_set_flag(skb->sk, SOCK_DEAD);
349 x25_start_heartbeat(skb->sk);
350 x25_sk(skb->sk)->state = X25_STATE_0;
351 }
352
353 kfree_skb(skb);
354 }
355
356 if (atomic_read(&sk->sk_wmem_alloc) ||
357 atomic_read(&sk->sk_rmem_alloc)) {
358 /* Defer: outstanding buffers */
359 sk->sk_timer.expires = jiffies + 10 * HZ;
360 sk->sk_timer.function = x25_destroy_timer;
361 sk->sk_timer.data = (unsigned long)sk;
362 add_timer(&sk->sk_timer);
363 } else {
364 /* drop last reference so sock_put will free */
365 __sock_put(sk);
366 }
367
368 release_sock(sk);
369 sock_put(sk);
370}
371
372/*
373 * Handling for system calls applied via the various interfaces to a
374 * X.25 socket object.
375 */
376
377static int x25_setsockopt(struct socket *sock, int level, int optname,
378 char __user *optval, int optlen)
379{
380 int opt;
381 struct sock *sk = sock->sk;
382 int rc = -ENOPROTOOPT;
383
384 if (level != SOL_X25 || optname != X25_QBITINCL)
385 goto out;
386
387 rc = -EINVAL;
388 if (optlen < sizeof(int))
389 goto out;
390
391 rc = -EFAULT;
392 if (get_user(opt, (int __user *)optval))
393 goto out;
394
395 x25_sk(sk)->qbitincl = !!opt;
396 rc = 0;
397out:
398 return rc;
399}
400
401static int x25_getsockopt(struct socket *sock, int level, int optname,
402 char __user *optval, int __user *optlen)
403{
404 struct sock *sk = sock->sk;
405 int val, len, rc = -ENOPROTOOPT;
406
407 if (level != SOL_X25 || optname != X25_QBITINCL)
408 goto out;
409
410 rc = -EFAULT;
411 if (get_user(len, optlen))
412 goto out;
413
414 len = min_t(unsigned int, len, sizeof(int));
415
416 rc = -EINVAL;
417 if (len < 0)
418 goto out;
419
420 rc = -EFAULT;
421 if (put_user(len, optlen))
422 goto out;
423
424 val = x25_sk(sk)->qbitincl;
425 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
426out:
427 return rc;
428}
429
430static int x25_listen(struct socket *sock, int backlog)
431{
432 struct sock *sk = sock->sk;
433 int rc = -EOPNOTSUPP;
434
435 if (sk->sk_state != TCP_LISTEN) {
436 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
437 sk->sk_max_ack_backlog = backlog;
438 sk->sk_state = TCP_LISTEN;
439 rc = 0;
440 }
441
442 return rc;
443}
444
445static struct proto x25_proto = {
446 .name = "X25",
447 .owner = THIS_MODULE,
448 .obj_size = sizeof(struct x25_sock),
449};
450
451static struct sock *x25_alloc_socket(void)
452{
453 struct x25_sock *x25;
454 struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1);
455
456 if (!sk)
457 goto out;
458
459 sock_init_data(NULL, sk);
460
461 x25 = x25_sk(sk);
462 skb_queue_head_init(&x25->ack_queue);
463 skb_queue_head_init(&x25->fragment_queue);
464 skb_queue_head_init(&x25->interrupt_in_queue);
465 skb_queue_head_init(&x25->interrupt_out_queue);
466out:
467 return sk;
468}
469
470void x25_init_timers(struct sock *sk);
471
472static int x25_create(struct socket *sock, int protocol)
473{
474 struct sock *sk;
475 struct x25_sock *x25;
476 int rc = -ESOCKTNOSUPPORT;
477
478 if (sock->type != SOCK_SEQPACKET || protocol)
479 goto out;
480
481 rc = -ENOMEM;
482 if ((sk = x25_alloc_socket()) == NULL)
483 goto out;
484
485 x25 = x25_sk(sk);
486
487 sock_init_data(sock, sk);
488
489 x25_init_timers(sk);
490
491 sock->ops = &x25_proto_ops;
492 sk->sk_protocol = protocol;
493 sk->sk_backlog_rcv = x25_backlog_rcv;
494
495 x25->t21 = sysctl_x25_call_request_timeout;
496 x25->t22 = sysctl_x25_reset_request_timeout;
497 x25->t23 = sysctl_x25_clear_request_timeout;
498 x25->t2 = sysctl_x25_ack_holdback_timeout;
499 x25->state = X25_STATE_0;
500
501 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
502 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
503 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
504 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
505 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
506 x25->facilities.reverse = X25_DEFAULT_REVERSE;
507 rc = 0;
508out:
509 return rc;
510}
511
512static struct sock *x25_make_new(struct sock *osk)
513{
514 struct sock *sk = NULL;
515 struct x25_sock *x25, *ox25;
516
517 if (osk->sk_type != SOCK_SEQPACKET)
518 goto out;
519
520 if ((sk = x25_alloc_socket()) == NULL)
521 goto out;
522
523 x25 = x25_sk(sk);
524
525 sk->sk_type = osk->sk_type;
526 sk->sk_socket = osk->sk_socket;
527 sk->sk_priority = osk->sk_priority;
528 sk->sk_protocol = osk->sk_protocol;
529 sk->sk_rcvbuf = osk->sk_rcvbuf;
530 sk->sk_sndbuf = osk->sk_sndbuf;
531 sk->sk_state = TCP_ESTABLISHED;
532 sk->sk_sleep = osk->sk_sleep;
533 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
534
535 if (sock_flag(osk, SOCK_ZAPPED))
536 sock_set_flag(sk, SOCK_ZAPPED);
537
538 if (sock_flag(osk, SOCK_DBG))
539 sock_set_flag(sk, SOCK_DBG);
540
541 ox25 = x25_sk(osk);
542 x25->t21 = ox25->t21;
543 x25->t22 = ox25->t22;
544 x25->t23 = ox25->t23;
545 x25->t2 = ox25->t2;
546 x25->facilities = ox25->facilities;
547 x25->qbitincl = ox25->qbitincl;
548
549 x25_init_timers(sk);
550out:
551 return sk;
552}
553
554static int x25_release(struct socket *sock)
555{
556 struct sock *sk = sock->sk;
557 struct x25_sock *x25;
558
559 if (!sk)
560 goto out;
561
562 x25 = x25_sk(sk);
563
564 switch (x25->state) {
565
566 case X25_STATE_0:
567 case X25_STATE_2:
568 x25_disconnect(sk, 0, 0, 0);
569 x25_destroy_socket(sk);
570 goto out;
571
572 case X25_STATE_1:
573 case X25_STATE_3:
574 case X25_STATE_4:
575 x25_clear_queues(sk);
576 x25_write_internal(sk, X25_CLEAR_REQUEST);
577 x25_start_t23timer(sk);
578 x25->state = X25_STATE_2;
579 sk->sk_state = TCP_CLOSE;
580 sk->sk_shutdown |= SEND_SHUTDOWN;
581 sk->sk_state_change(sk);
582 sock_set_flag(sk, SOCK_DEAD);
583 sock_set_flag(sk, SOCK_DESTROY);
584 break;
585 }
586
587 sock->sk = NULL;
588 sk->sk_socket = NULL; /* Not used, but we should do this */
589out:
590 return 0;
591}
592
593static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
594{
595 struct sock *sk = sock->sk;
596 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
597
598 if (!sock_flag(sk, SOCK_ZAPPED) ||
599 addr_len != sizeof(struct sockaddr_x25) ||
600 addr->sx25_family != AF_X25)
601 return -EINVAL;
602
603 x25_sk(sk)->source_addr = addr->sx25_addr;
604 x25_insert_socket(sk);
605 sock_reset_flag(sk, SOCK_ZAPPED);
606 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
607
608 return 0;
609}
610
611static int x25_wait_for_connection_establishment(struct sock *sk)
612{
613 DECLARE_WAITQUEUE(wait, current);
614 int rc;
615
616 add_wait_queue_exclusive(sk->sk_sleep, &wait);
617 for (;;) {
618 __set_current_state(TASK_INTERRUPTIBLE);
619 rc = -ERESTARTSYS;
620 if (signal_pending(current))
621 break;
622 rc = sock_error(sk);
623 if (rc) {
624 sk->sk_socket->state = SS_UNCONNECTED;
625 break;
626 }
627 rc = 0;
628 if (sk->sk_state != TCP_ESTABLISHED) {
629 release_sock(sk);
630 schedule();
631 lock_sock(sk);
632 } else
633 break;
634 }
635 __set_current_state(TASK_RUNNING);
636 remove_wait_queue(sk->sk_sleep, &wait);
637 return rc;
638}
639
640static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
641 int addr_len, int flags)
642{
643 struct sock *sk = sock->sk;
644 struct x25_sock *x25 = x25_sk(sk);
645 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
646 struct x25_route *rt;
647 int rc = 0;
648
649 lock_sock(sk);
650 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
651 sock->state = SS_CONNECTED;
652 goto out; /* Connect completed during a ERESTARTSYS event */
653 }
654
655 rc = -ECONNREFUSED;
656 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
657 sock->state = SS_UNCONNECTED;
658 goto out;
659 }
660
661 rc = -EISCONN; /* No reconnect on a seqpacket socket */
662 if (sk->sk_state == TCP_ESTABLISHED)
663 goto out;
664
665 sk->sk_state = TCP_CLOSE;
666 sock->state = SS_UNCONNECTED;
667
668 rc = -EINVAL;
669 if (addr_len != sizeof(struct sockaddr_x25) ||
670 addr->sx25_family != AF_X25)
671 goto out;
672
673 rc = -ENETUNREACH;
674 rt = x25_get_route(&addr->sx25_addr);
675 if (!rt)
676 goto out;
677
678 x25->neighbour = x25_get_neigh(rt->dev);
679 if (!x25->neighbour)
680 goto out_put_route;
681
682 x25_limit_facilities(&x25->facilities, x25->neighbour);
683
684 x25->lci = x25_new_lci(x25->neighbour);
685 if (!x25->lci)
686 goto out_put_neigh;
687
688 rc = -EINVAL;
689 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
690 goto out_put_neigh;
691
692 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
693 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
694
695 x25->dest_addr = addr->sx25_addr;
696
697 /* Move to connecting socket, start sending Connect Requests */
698 sock->state = SS_CONNECTING;
699 sk->sk_state = TCP_SYN_SENT;
700
701 x25->state = X25_STATE_1;
702
703 x25_write_internal(sk, X25_CALL_REQUEST);
704
705 x25_start_heartbeat(sk);
706 x25_start_t21timer(sk);
707
708 /* Now the loop */
709 rc = -EINPROGRESS;
710 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
711 goto out_put_neigh;
712
713 rc = x25_wait_for_connection_establishment(sk);
714 if (rc)
715 goto out_put_neigh;
716
717 sock->state = SS_CONNECTED;
718 rc = 0;
719out_put_neigh:
720 if (rc)
721 x25_neigh_put(x25->neighbour);
722out_put_route:
723 x25_route_put(rt);
724out:
725 release_sock(sk);
726 return rc;
727}
728
729static int x25_wait_for_data(struct sock *sk, int timeout)
730{
731 DECLARE_WAITQUEUE(wait, current);
732 int rc = 0;
733
734 add_wait_queue_exclusive(sk->sk_sleep, &wait);
735 for (;;) {
736 __set_current_state(TASK_INTERRUPTIBLE);
737 if (sk->sk_shutdown & RCV_SHUTDOWN)
738 break;
739 rc = -ERESTARTSYS;
740 if (signal_pending(current))
741 break;
742 rc = -EAGAIN;
743 if (!timeout)
744 break;
745 rc = 0;
746 if (skb_queue_empty(&sk->sk_receive_queue)) {
747 release_sock(sk);
748 timeout = schedule_timeout(timeout);
749 lock_sock(sk);
750 } else
751 break;
752 }
753 __set_current_state(TASK_RUNNING);
754 remove_wait_queue(sk->sk_sleep, &wait);
755 return rc;
756}
757
758static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
759{
760 struct sock *sk = sock->sk;
761 struct sock *newsk;
762 struct sk_buff *skb;
763 int rc = -EINVAL;
764
765 if (!sk || sk->sk_state != TCP_LISTEN)
766 goto out;
767
768 rc = -EOPNOTSUPP;
769 if (sk->sk_type != SOCK_SEQPACKET)
770 goto out;
771
772 lock_sock(sk);
773 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
774 if (rc)
775 goto out2;
776 skb = skb_dequeue(&sk->sk_receive_queue);
777 rc = -EINVAL;
778 if (!skb->sk)
779 goto out2;
780 newsk = skb->sk;
781 newsk->sk_socket = newsock;
782 newsk->sk_sleep = &newsock->wait;
783
784 /* Now attach up the new socket */
785 skb->sk = NULL;
786 kfree_skb(skb);
787 sk->sk_ack_backlog--;
788 newsock->sk = newsk;
789 newsock->state = SS_CONNECTED;
790 rc = 0;
791out2:
792 release_sock(sk);
793out:
794 return rc;
795}
796
797static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
798 int *uaddr_len, int peer)
799{
800 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
801 struct sock *sk = sock->sk;
802 struct x25_sock *x25 = x25_sk(sk);
803
804 if (peer) {
805 if (sk->sk_state != TCP_ESTABLISHED)
806 return -ENOTCONN;
807 sx25->sx25_addr = x25->dest_addr;
808 } else
809 sx25->sx25_addr = x25->source_addr;
810
811 sx25->sx25_family = AF_X25;
812 *uaddr_len = sizeof(*sx25);
813
814 return 0;
815}
816
817int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
818 unsigned int lci)
819{
820 struct sock *sk;
821 struct sock *make;
822 struct x25_sock *makex25;
823 struct x25_address source_addr, dest_addr;
824 struct x25_facilities facilities;
825 struct x25_calluserdata calluserdata;
826 int len, rc;
827
828 /*
829 * Remove the LCI and frame type.
830 */
831 skb_pull(skb, X25_STD_MIN_LEN);
832
833 /*
834 * Extract the X.25 addresses and convert them to ASCII strings,
835 * and remove them.
836 */
837 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
838
839 /*
840 * Get the length of the facilities, skip past them for the moment
841 * get the call user data because this is needed to determine
842 * the correct listener
843 */
844 len = skb->data[0] + 1;
845 skb_pull(skb,len);
846
847 /*
848 * Incoming Call User Data.
849 */
850 if (skb->len >= 0) {
851 memcpy(calluserdata.cuddata, skb->data, skb->len);
852 calluserdata.cudlength = skb->len;
853 }
854
855 skb_push(skb,len);
856
857 /*
858 * Find a listener for the particular address/cud pair.
859 */
860 sk = x25_find_listener(&source_addr,&calluserdata);
861
862 /*
863 * We can't accept the Call Request.
864 */
865 if (sk == NULL || sk_acceptq_is_full(sk))
866 goto out_clear_request;
867
868 /*
869 * Try to reach a compromise on the requested facilities.
870 */
871 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
872 goto out_sock_put;
873
874 /*
875 * current neighbour/link might impose additional limits
876 * on certain facilties
877 */
878
879 x25_limit_facilities(&facilities, nb);
880
881 /*
882 * Try to create a new socket.
883 */
884 make = x25_make_new(sk);
885 if (!make)
886 goto out_sock_put;
887
888 /*
889 * Remove the facilities
890 */
891 skb_pull(skb, len);
892
893 skb->sk = make;
894 make->sk_state = TCP_ESTABLISHED;
895
896 makex25 = x25_sk(make);
897 makex25->lci = lci;
898 makex25->dest_addr = dest_addr;
899 makex25->source_addr = source_addr;
900 makex25->neighbour = nb;
901 makex25->facilities = facilities;
902 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
903 makex25->calluserdata = calluserdata;
904
905 x25_write_internal(make, X25_CALL_ACCEPTED);
906
907 makex25->state = X25_STATE_3;
908
909 sk->sk_ack_backlog++;
910
911 x25_insert_socket(make);
912
913 skb_queue_head(&sk->sk_receive_queue, skb);
914
915 x25_start_heartbeat(make);
916
917 if (!sock_flag(sk, SOCK_DEAD))
918 sk->sk_data_ready(sk, skb->len);
919 rc = 1;
920 sock_put(sk);
921out:
922 return rc;
923out_sock_put:
924 sock_put(sk);
925out_clear_request:
926 rc = 0;
927 x25_transmit_clear_request(nb, lci, 0x01);
928 goto out;
929}
930
931static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
932 struct msghdr *msg, size_t len)
933{
934 struct sock *sk = sock->sk;
935 struct x25_sock *x25 = x25_sk(sk);
936 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
937 struct sockaddr_x25 sx25;
938 struct sk_buff *skb;
939 unsigned char *asmptr;
940 int noblock = msg->msg_flags & MSG_DONTWAIT;
941 size_t size;
942 int qbit = 0, rc = -EINVAL;
943
944 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
945 goto out;
946
947 /* we currently don't support segmented records at the user interface */
948 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
949 goto out;
950
951 rc = -EADDRNOTAVAIL;
952 if (sock_flag(sk, SOCK_ZAPPED))
953 goto out;
954
955 rc = -EPIPE;
956 if (sk->sk_shutdown & SEND_SHUTDOWN) {
957 send_sig(SIGPIPE, current, 0);
958 goto out;
959 }
960
961 rc = -ENETUNREACH;
962 if (!x25->neighbour)
963 goto out;
964
965 if (usx25) {
966 rc = -EINVAL;
967 if (msg->msg_namelen < sizeof(sx25))
968 goto out;
969 memcpy(&sx25, usx25, sizeof(sx25));
970 rc = -EISCONN;
971 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
972 goto out;
973 rc = -EINVAL;
974 if (sx25.sx25_family != AF_X25)
975 goto out;
976 } else {
977 /*
978 * FIXME 1003.1g - if the socket is like this because
979 * it has become closed (not started closed) we ought
980 * to SIGPIPE, EPIPE;
981 */
982 rc = -ENOTCONN;
983 if (sk->sk_state != TCP_ESTABLISHED)
984 goto out;
985
986 sx25.sx25_family = AF_X25;
987 sx25.sx25_addr = x25->dest_addr;
988 }
989
990 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
991
992 /* Build a packet */
993 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
994
995 if ((msg->msg_flags & MSG_OOB) && len > 32)
996 len = 32;
997
998 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
999
1000 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1001 if (!skb)
1002 goto out;
1003 X25_SKB_CB(skb)->flags = msg->msg_flags;
1004
1005 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1006
1007 /*
1008 * Put the data on the end
1009 */
1010 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1011
1012 asmptr = skb->h.raw = skb_put(skb, len);
1013
1014 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1015 if (rc)
1016 goto out_kfree_skb;
1017
1018 /*
1019 * If the Q BIT Include socket option is in force, the first
1020 * byte of the user data is the logical value of the Q Bit.
1021 */
1022 if (x25->qbitincl) {
1023 qbit = skb->data[0];
1024 skb_pull(skb, 1);
1025 }
1026
1027 /*
1028 * Push down the X.25 header
1029 */
1030 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1031
1032 if (msg->msg_flags & MSG_OOB) {
1033 if (x25->neighbour->extended) {
1034 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1035 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1036 *asmptr++ = (x25->lci >> 0) & 0xFF;
1037 *asmptr++ = X25_INTERRUPT;
1038 } else {
1039 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1040 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1041 *asmptr++ = (x25->lci >> 0) & 0xFF;
1042 *asmptr++ = X25_INTERRUPT;
1043 }
1044 } else {
1045 if (x25->neighbour->extended) {
1046 /* Build an Extended X.25 header */
1047 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1048 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1049 *asmptr++ = (x25->lci >> 0) & 0xFF;
1050 *asmptr++ = X25_DATA;
1051 *asmptr++ = X25_DATA;
1052 } else {
1053 /* Build an Standard X.25 header */
1054 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1055 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1056 *asmptr++ = (x25->lci >> 0) & 0xFF;
1057 *asmptr++ = X25_DATA;
1058 }
1059
1060 if (qbit)
1061 skb->data[0] |= X25_Q_BIT;
1062 }
1063
1064 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1065 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1066
1067 rc = -ENOTCONN;
1068 if (sk->sk_state != TCP_ESTABLISHED)
1069 goto out_kfree_skb;
1070
1071 if (msg->msg_flags & MSG_OOB)
1072 skb_queue_tail(&x25->interrupt_out_queue, skb);
1073 else {
1074 len = x25_output(sk, skb);
1075 if (len < 0)
1076 kfree_skb(skb);
1077 else if (x25->qbitincl)
1078 len++;
1079 }
1080
1081 /*
1082 * lock_sock() is currently only used to serialize this x25_kick()
1083 * against input-driven x25_kick() calls. It currently only blocks
1084 * incoming packets for this socket and does not protect against
1085 * any other socket state changes and is not called from anywhere
1086 * else. As x25_kick() cannot block and as long as all socket
1087 * operations are BKL-wrapped, we don't need take to care about
1088 * purging the backlog queue in x25_release().
1089 *
1090 * Using lock_sock() to protect all socket operations entirely
1091 * (and making the whole x25 stack SMP aware) unfortunately would
1092 * require major changes to {send,recv}msg and skb allocation methods.
1093 * -> 2.5 ;)
1094 */
1095 lock_sock(sk);
1096 x25_kick(sk);
1097 release_sock(sk);
1098 rc = len;
1099out:
1100 return rc;
1101out_kfree_skb:
1102 kfree_skb(skb);
1103 goto out;
1104}
1105
1106
1107static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1108 struct msghdr *msg, size_t size,
1109 int flags)
1110{
1111 struct sock *sk = sock->sk;
1112 struct x25_sock *x25 = x25_sk(sk);
1113 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1114 size_t copied;
1115 int qbit;
1116 struct sk_buff *skb;
1117 unsigned char *asmptr;
1118 int rc = -ENOTCONN;
1119
1120 /*
1121 * This works for seqpacket too. The receiver has ordered the queue for
1122 * us! We do one quick check first though
1123 */
1124 if (sk->sk_state != TCP_ESTABLISHED)
1125 goto out;
1126
1127 if (flags & MSG_OOB) {
1128 rc = -EINVAL;
1129 if (sock_flag(sk, SOCK_URGINLINE) ||
1130 !skb_peek(&x25->interrupt_in_queue))
1131 goto out;
1132
1133 skb = skb_dequeue(&x25->interrupt_in_queue);
1134
1135 skb_pull(skb, X25_STD_MIN_LEN);
1136
1137 /*
1138 * No Q bit information on Interrupt data.
1139 */
1140 if (x25->qbitincl) {
1141 asmptr = skb_push(skb, 1);
1142 *asmptr = 0x00;
1143 }
1144
1145 msg->msg_flags |= MSG_OOB;
1146 } else {
1147 /* Now we can treat all alike */
1148 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1149 flags & MSG_DONTWAIT, &rc);
1150 if (!skb)
1151 goto out;
1152
1153 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1154
1155 skb_pull(skb, x25->neighbour->extended ?
1156 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1157
1158 if (x25->qbitincl) {
1159 asmptr = skb_push(skb, 1);
1160 *asmptr = qbit;
1161 }
1162 }
1163
1164 skb->h.raw = skb->data;
1165
1166 copied = skb->len;
1167
1168 if (copied > size) {
1169 copied = size;
1170 msg->msg_flags |= MSG_TRUNC;
1171 }
1172
1173 /* Currently, each datagram always contains a complete record */
1174 msg->msg_flags |= MSG_EOR;
1175
1176 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1177 if (rc)
1178 goto out_free_dgram;
1179
1180 if (sx25) {
1181 sx25->sx25_family = AF_X25;
1182 sx25->sx25_addr = x25->dest_addr;
1183 }
1184
1185 msg->msg_namelen = sizeof(struct sockaddr_x25);
1186
1187 lock_sock(sk);
1188 x25_check_rbuf(sk);
1189 release_sock(sk);
1190 rc = copied;
1191out_free_dgram:
1192 skb_free_datagram(sk, skb);
1193out:
1194 return rc;
1195}
1196
1197
1198static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1199{
1200 struct sock *sk = sock->sk;
1201 struct x25_sock *x25 = x25_sk(sk);
1202 void __user *argp = (void __user *)arg;
1203 int rc;
1204
1205 switch (cmd) {
1206 case TIOCOUTQ: {
1207 int amount = sk->sk_sndbuf -
1208 atomic_read(&sk->sk_wmem_alloc);
1209 if (amount < 0)
1210 amount = 0;
1211 rc = put_user(amount, (unsigned int __user *)argp);
1212 break;
1213 }
1214
1215 case TIOCINQ: {
1216 struct sk_buff *skb;
1217 int amount = 0;
1218 /*
1219 * These two are safe on a single CPU system as
1220 * only user tasks fiddle here
1221 */
1222 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1223 amount = skb->len;
1224 rc = put_user(amount, (unsigned int __user *)argp);
1225 break;
1226 }
1227
1228 case SIOCGSTAMP:
1229 rc = -EINVAL;
1230 if (sk)
1231 rc = sock_get_timestamp(sk,
1232 (struct timeval __user *)argp);
1233 break;
1234 case SIOCGIFADDR:
1235 case SIOCSIFADDR:
1236 case SIOCGIFDSTADDR:
1237 case SIOCSIFDSTADDR:
1238 case SIOCGIFBRDADDR:
1239 case SIOCSIFBRDADDR:
1240 case SIOCGIFNETMASK:
1241 case SIOCSIFNETMASK:
1242 case SIOCGIFMETRIC:
1243 case SIOCSIFMETRIC:
1244 rc = -EINVAL;
1245 break;
1246 case SIOCADDRT:
1247 case SIOCDELRT:
1248 rc = -EPERM;
1249 if (!capable(CAP_NET_ADMIN))
1250 break;
1251 rc = x25_route_ioctl(cmd, argp);
1252 break;
1253 case SIOCX25GSUBSCRIP:
1254 rc = x25_subscr_ioctl(cmd, argp);
1255 break;
1256 case SIOCX25SSUBSCRIP:
1257 rc = -EPERM;
1258 if (!capable(CAP_NET_ADMIN))
1259 break;
1260 rc = x25_subscr_ioctl(cmd, argp);
1261 break;
1262 case SIOCX25GFACILITIES: {
1263 struct x25_facilities fac = x25->facilities;
1264 rc = copy_to_user(argp, &fac,
1265 sizeof(fac)) ? -EFAULT : 0;
1266 break;
1267 }
1268
1269 case SIOCX25SFACILITIES: {
1270 struct x25_facilities facilities;
1271 rc = -EFAULT;
1272 if (copy_from_user(&facilities, argp,
1273 sizeof(facilities)))
1274 break;
1275 rc = -EINVAL;
1276 if (sk->sk_state != TCP_LISTEN &&
1277 sk->sk_state != TCP_CLOSE)
1278 break;
1279 if (facilities.pacsize_in < X25_PS16 ||
1280 facilities.pacsize_in > X25_PS4096)
1281 break;
1282 if (facilities.pacsize_out < X25_PS16 ||
1283 facilities.pacsize_out > X25_PS4096)
1284 break;
1285 if (facilities.winsize_in < 1 ||
1286 facilities.winsize_in > 127)
1287 break;
1288 if (facilities.throughput < 0x03 ||
1289 facilities.throughput > 0xDD)
1290 break;
1291 if (facilities.reverse && facilities.reverse != 1)
1292 break;
1293 x25->facilities = facilities;
1294 rc = 0;
1295 break;
1296 }
1297
1298 case SIOCX25GCALLUSERDATA: {
1299 struct x25_calluserdata cud = x25->calluserdata;
1300 rc = copy_to_user(argp, &cud,
1301 sizeof(cud)) ? -EFAULT : 0;
1302 break;
1303 }
1304
1305 case SIOCX25SCALLUSERDATA: {
1306 struct x25_calluserdata calluserdata;
1307
1308 rc = -EFAULT;
1309 if (copy_from_user(&calluserdata, argp,
1310 sizeof(calluserdata)))
1311 break;
1312 rc = -EINVAL;
1313 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1314 break;
1315 x25->calluserdata = calluserdata;
1316 rc = 0;
1317 break;
1318 }
1319
1320 case SIOCX25GCAUSEDIAG: {
1321 struct x25_causediag causediag;
1322 causediag = x25->causediag;
1323 rc = copy_to_user(argp, &causediag,
1324 sizeof(causediag)) ? -EFAULT : 0;
1325 break;
1326 }
1327
1328 default:
1329 rc = dev_ioctl(cmd, argp);
1330 break;
1331 }
1332
1333 return rc;
1334}
1335
1336static struct net_proto_family x25_family_ops = {
1337 .family = AF_X25,
1338 .create = x25_create,
1339 .owner = THIS_MODULE,
1340};
1341
1342static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1343 .family = AF_X25,
1344 .owner = THIS_MODULE,
1345 .release = x25_release,
1346 .bind = x25_bind,
1347 .connect = x25_connect,
1348 .socketpair = sock_no_socketpair,
1349 .accept = x25_accept,
1350 .getname = x25_getname,
1351 .poll = datagram_poll,
1352 .ioctl = x25_ioctl,
1353 .listen = x25_listen,
1354 .shutdown = sock_no_shutdown,
1355 .setsockopt = x25_setsockopt,
1356 .getsockopt = x25_getsockopt,
1357 .sendmsg = x25_sendmsg,
1358 .recvmsg = x25_recvmsg,
1359 .mmap = sock_no_mmap,
1360 .sendpage = sock_no_sendpage,
1361};
1362
1363#include <linux/smp_lock.h>
1364SOCKOPS_WRAP(x25_proto, AF_X25);
1365
1366static struct packet_type x25_packet_type = {
1367 .type = __constant_htons(ETH_P_X25),
1368 .func = x25_lapb_receive_frame,
1369};
1370
1371static struct notifier_block x25_dev_notifier = {
1372 .notifier_call = x25_device_event,
1373};
1374
1375void x25_kill_by_neigh(struct x25_neigh *nb)
1376{
1377 struct sock *s;
1378 struct hlist_node *node;
1379
1380 write_lock_bh(&x25_list_lock);
1381
1382 sk_for_each(s, node, &x25_list)
1383 if (x25_sk(s)->neighbour == nb)
1384 x25_disconnect(s, ENETUNREACH, 0, 0);
1385
1386 write_unlock_bh(&x25_list_lock);
1387}
1388
1389static int __init x25_init(void)
1390{
1391 int rc = proto_register(&x25_proto, 0);
1392
1393 if (rc != 0)
1394 goto out;
1395
1396 sock_register(&x25_family_ops);
1397
1398 dev_add_pack(&x25_packet_type);
1399
1400 register_netdevice_notifier(&x25_dev_notifier);
1401
1402 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1403
1404#ifdef CONFIG_SYSCTL
1405 x25_register_sysctl();
1406#endif
1407 x25_proc_init();
1408out:
1409 return rc;
1410}
1411module_init(x25_init);
1412
1413static void __exit x25_exit(void)
1414{
1415 x25_proc_exit();
1416 x25_link_free();
1417 x25_route_free();
1418
1419#ifdef CONFIG_SYSCTL
1420 x25_unregister_sysctl();
1421#endif
1422
1423 unregister_netdevice_notifier(&x25_dev_notifier);
1424
1425 dev_remove_pack(&x25_packet_type);
1426
1427 sock_unregister(AF_X25);
1428 proto_unregister(&x25_proto);
1429}
1430module_exit(x25_exit);
1431
1432MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1433MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1434MODULE_LICENSE("GPL");
1435MODULE_ALIAS_NETPROTO(PF_X25);
diff --git a/net/x25/sysctl_net_x25.c b/net/x25/sysctl_net_x25.c
new file mode 100644
index 000000000000..aabda59c824e
--- /dev/null
+++ b/net/x25/sysctl_net_x25.c
@@ -0,0 +1,107 @@
1/* -*- linux-c -*-
2 * sysctl_net_x25.c: sysctl interface to net X.25 subsystem.
3 *
4 * Begun April 1, 1996, Mike Shaver.
5 * Added /proc/sys/net/x25 directory entry (empty =) ). [MS]
6 */
7
8#include <linux/sysctl.h>
9#include <linux/skbuff.h>
10#include <linux/socket.h>
11#include <linux/netdevice.h>
12#include <linux/init.h>
13#include <net/x25.h>
14
15static int min_timer[] = { 1 * HZ };
16static int max_timer[] = { 300 * HZ };
17
18static struct ctl_table_header *x25_table_header;
19
20static struct ctl_table x25_table[] = {
21 {
22 .ctl_name = NET_X25_RESTART_REQUEST_TIMEOUT,
23 .procname = "restart_request_timeout",
24 .data = &sysctl_x25_restart_request_timeout,
25 .maxlen = sizeof(int),
26 .mode = 0644,
27 .proc_handler = &proc_dointvec_minmax,
28 .strategy = &sysctl_intvec,
29 .extra1 = &min_timer,
30 .extra2 = &max_timer,
31 },
32 {
33 .ctl_name = NET_X25_CALL_REQUEST_TIMEOUT,
34 .procname = "call_request_timeout",
35 .data = &sysctl_x25_call_request_timeout,
36 .maxlen = sizeof(int),
37 .mode = 0644,
38 .proc_handler = &proc_dointvec_minmax,
39 .strategy = &sysctl_intvec,
40 .extra1 = &min_timer,
41 .extra2 = &max_timer,
42 },
43 {
44 .ctl_name = NET_X25_RESET_REQUEST_TIMEOUT,
45 .procname = "reset_request_timeout",
46 .data = &sysctl_x25_reset_request_timeout,
47 .maxlen = sizeof(int),
48 .mode = 0644,
49 .proc_handler = &proc_dointvec_minmax,
50 .strategy = &sysctl_intvec,
51 .extra1 = &min_timer,
52 .extra2 = &max_timer,
53 },
54 {
55 .ctl_name = NET_X25_CLEAR_REQUEST_TIMEOUT,
56 .procname = "clear_request_timeout",
57 .data = &sysctl_x25_clear_request_timeout,
58 .maxlen = sizeof(int),
59 .mode = 0644,
60 .proc_handler = &proc_dointvec_minmax,
61 .strategy = &sysctl_intvec,
62 .extra1 = &min_timer,
63 .extra2 = &max_timer,
64 },
65 {
66 .ctl_name = NET_X25_ACK_HOLD_BACK_TIMEOUT,
67 .procname = "acknowledgement_hold_back_timeout",
68 .data = &sysctl_x25_ack_holdback_timeout,
69 .maxlen = sizeof(int),
70 .mode = 0644,
71 .proc_handler = &proc_dointvec_minmax,
72 .strategy = &sysctl_intvec,
73 .extra1 = &min_timer,
74 .extra2 = &max_timer,
75 },
76 { 0, },
77};
78
79static struct ctl_table x25_dir_table[] = {
80 {
81 .ctl_name = NET_X25,
82 .procname = "x25",
83 .mode = 0555,
84 .child = x25_table,
85 },
86 { 0, },
87};
88
89static struct ctl_table x25_root_table[] = {
90 {
91 .ctl_name = CTL_NET,
92 .procname = "net",
93 .mode = 0555,
94 .child = x25_dir_table,
95 },
96 { 0, },
97};
98
99void __init x25_register_sysctl(void)
100{
101 x25_table_header = register_sysctl_table(x25_root_table, 1);
102}
103
104void x25_unregister_sysctl(void)
105{
106 unregister_sysctl_table(x25_table_header);
107}
diff --git a/net/x25/x25_dev.c b/net/x25/x25_dev.c
new file mode 100644
index 000000000000..36fc3bf6d882
--- /dev/null
+++ b/net/x25/x25_dev.c
@@ -0,0 +1,207 @@
1/*
2 * X.25 Packet Layer release 002
3 *
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
6 *
7 * This code REQUIRES 2.1.15 or higher
8 *
9 * This module:
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 *
15 * History
16 * X.25 001 Jonathan Naylor Started coding.
17 * 2000-09-04 Henner Eisen Prevent freeing a dangling skb.
18 */
19
20#include <linux/config.h>
21#include <linux/kernel.h>
22#include <linux/netdevice.h>
23#include <linux/skbuff.h>
24#include <net/sock.h>
25#include <linux/if_arp.h>
26#include <net/x25.h>
27
28static int x25_receive_data(struct sk_buff *skb, struct x25_neigh *nb)
29{
30 struct sock *sk;
31 unsigned short frametype;
32 unsigned int lci;
33
34 frametype = skb->data[2];
35 lci = ((skb->data[0] << 8) & 0xF00) + ((skb->data[1] << 0) & 0x0FF);
36
37 /*
38 * LCI of zero is always for us, and its always a link control
39 * frame.
40 */
41 if (lci == 0) {
42 x25_link_control(skb, nb, frametype);
43 return 0;
44 }
45
46 /*
47 * Find an existing socket.
48 */
49 if ((sk = x25_find_socket(lci, nb)) != NULL) {
50 int queued = 1;
51
52 skb->h.raw = skb->data;
53 bh_lock_sock(sk);
54 if (!sock_owned_by_user(sk)) {
55 queued = x25_process_rx_frame(sk, skb);
56 } else {
57 sk_add_backlog(sk, skb);
58 }
59 bh_unlock_sock(sk);
60 return queued;
61 }
62
63 /*
64 * Is is a Call Request ? if so process it.
65 */
66 if (frametype == X25_CALL_REQUEST)
67 return x25_rx_call_request(skb, nb, lci);
68
69 /*
70 * Its not a Call Request, nor is it a control frame.
71 * Let caller throw it away.
72 */
73/*
74 x25_transmit_clear_request(nb, lci, 0x0D);
75*/
76
77 if (frametype != X25_CLEAR_CONFIRMATION)
78 printk(KERN_DEBUG "x25_receive_data(): unknown frame type %2x\n",frametype);
79
80 return 0;
81}
82
83int x25_lapb_receive_frame(struct sk_buff *skb, struct net_device *dev,
84 struct packet_type *ptype)
85{
86 struct sk_buff *nskb;
87 struct x25_neigh *nb;
88
89 nskb = skb_copy(skb, GFP_ATOMIC);
90 if (!nskb)
91 goto drop;
92 kfree_skb(skb);
93 skb = nskb;
94
95 /*
96 * Packet received from unrecognised device, throw it away.
97 */
98 nb = x25_get_neigh(dev);
99 if (!nb) {
100 printk(KERN_DEBUG "X.25: unknown neighbour - %s\n", dev->name);
101 goto drop;
102 }
103
104 switch (skb->data[0]) {
105 case 0x00:
106 skb_pull(skb, 1);
107 if (x25_receive_data(skb, nb)) {
108 x25_neigh_put(nb);
109 goto out;
110 }
111 break;
112 case 0x01:
113 x25_link_established(nb);
114 break;
115 case 0x02:
116 x25_link_terminated(nb);
117 break;
118 }
119 x25_neigh_put(nb);
120drop:
121 kfree_skb(skb);
122out:
123 return 0;
124}
125
126void x25_establish_link(struct x25_neigh *nb)
127{
128 struct sk_buff *skb;
129 unsigned char *ptr;
130
131 switch (nb->dev->type) {
132 case ARPHRD_X25:
133 if ((skb = alloc_skb(1, GFP_ATOMIC)) == NULL) {
134 printk(KERN_ERR "x25_dev: out of memory\n");
135 return;
136 }
137 ptr = skb_put(skb, 1);
138 *ptr = 0x01;
139 break;
140
141#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
142 case ARPHRD_ETHER:
143 return;
144#endif
145 default:
146 return;
147 }
148
149 skb->protocol = htons(ETH_P_X25);
150 skb->dev = nb->dev;
151
152 dev_queue_xmit(skb);
153}
154
155void x25_terminate_link(struct x25_neigh *nb)
156{
157 struct sk_buff *skb;
158 unsigned char *ptr;
159
160#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
161 if (nb->dev->type == ARPHRD_ETHER)
162 return;
163#endif
164 if (nb->dev->type != ARPHRD_X25)
165 return;
166
167 skb = alloc_skb(1, GFP_ATOMIC);
168 if (!skb) {
169 printk(KERN_ERR "x25_dev: out of memory\n");
170 return;
171 }
172
173 ptr = skb_put(skb, 1);
174 *ptr = 0x02;
175
176 skb->protocol = htons(ETH_P_X25);
177 skb->dev = nb->dev;
178 dev_queue_xmit(skb);
179}
180
181void x25_send_frame(struct sk_buff *skb, struct x25_neigh *nb)
182{
183 unsigned char *dptr;
184
185 skb->nh.raw = skb->data;
186
187 switch (nb->dev->type) {
188 case ARPHRD_X25:
189 dptr = skb_push(skb, 1);
190 *dptr = 0x00;
191 break;
192
193#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
194 case ARPHRD_ETHER:
195 kfree_skb(skb);
196 return;
197#endif
198 default:
199 kfree_skb(skb);
200 return;
201 }
202
203 skb->protocol = htons(ETH_P_X25);
204 skb->dev = nb->dev;
205
206 dev_queue_xmit(skb);
207}
diff --git a/net/x25/x25_facilities.c b/net/x25/x25_facilities.c
new file mode 100644
index 000000000000..a21bdb95f9a8
--- /dev/null
+++ b/net/x25/x25_facilities.c
@@ -0,0 +1,231 @@
1/*
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
6 * screw up. It might even work.
7 *
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 Split from x25_subr.c
18 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
19 * negotiation.
20 */
21
22#include <linux/kernel.h>
23#include <linux/string.h>
24#include <linux/skbuff.h>
25#include <net/sock.h>
26#include <net/x25.h>
27
28/*
29 * Parse a set of facilities into the facilities structure. Unrecognised
30 * facilities are written to the debug log file.
31 */
32int x25_parse_facilities(struct sk_buff *skb,
33 struct x25_facilities *facilities,
34 unsigned long *vc_fac_mask)
35{
36 unsigned char *p = skb->data;
37 unsigned int len = *p++;
38
39 *vc_fac_mask = 0;
40
41 while (len > 0) {
42 switch (*p & X25_FAC_CLASS_MASK) {
43 case X25_FAC_CLASS_A:
44 switch (*p) {
45 case X25_FAC_REVERSE:
46 facilities->reverse = p[1] & 0x01;
47 *vc_fac_mask |= X25_MASK_REVERSE;
48 break;
49 case X25_FAC_THROUGHPUT:
50 facilities->throughput = p[1];
51 *vc_fac_mask |= X25_MASK_THROUGHPUT;
52 break;
53 default:
54 printk(KERN_DEBUG "X.25: unknown facility "
55 "%02X, value %02X\n",
56 p[0], p[1]);
57 break;
58 }
59 p += 2;
60 len -= 2;
61 break;
62 case X25_FAC_CLASS_B:
63 switch (*p) {
64 case X25_FAC_PACKET_SIZE:
65 facilities->pacsize_in = p[1];
66 facilities->pacsize_out = p[2];
67 *vc_fac_mask |= X25_MASK_PACKET_SIZE;
68 break;
69 case X25_FAC_WINDOW_SIZE:
70 facilities->winsize_in = p[1];
71 facilities->winsize_out = p[2];
72 *vc_fac_mask |= X25_MASK_WINDOW_SIZE;
73 break;
74 default:
75 printk(KERN_DEBUG "X.25: unknown facility "
76 "%02X, values %02X, %02X\n",
77 p[0], p[1], p[2]);
78 break;
79 }
80 p += 3;
81 len -= 3;
82 break;
83 case X25_FAC_CLASS_C:
84 printk(KERN_DEBUG "X.25: unknown facility %02X, "
85 "values %02X, %02X, %02X\n",
86 p[0], p[1], p[2], p[3]);
87 p += 4;
88 len -= 4;
89 break;
90 case X25_FAC_CLASS_D:
91 printk(KERN_DEBUG "X.25: unknown facility %02X, "
92 "length %d, values %02X, %02X, %02X, %02X\n",
93 p[0], p[1], p[2], p[3], p[4], p[5]);
94 len -= p[1] + 2;
95 p += p[1] + 2;
96 break;
97 }
98 }
99
100 return p - skb->data;
101}
102
103/*
104 * Create a set of facilities.
105 */
106int x25_create_facilities(unsigned char *buffer,
107 struct x25_facilities *facilities,
108 unsigned long facil_mask)
109{
110 unsigned char *p = buffer + 1;
111 int len;
112
113 if (!facil_mask) {
114 /*
115 * Length of the facilities field in call_req or
116 * call_accept packets
117 */
118 buffer[0] = 0;
119 len = 1; /* 1 byte for the length field */
120 return len;
121 }
122
123 if (facilities->reverse && (facil_mask & X25_MASK_REVERSE)) {
124 *p++ = X25_FAC_REVERSE;
125 *p++ = !!facilities->reverse;
126 }
127
128 if (facilities->throughput && (facil_mask & X25_MASK_THROUGHPUT)) {
129 *p++ = X25_FAC_THROUGHPUT;
130 *p++ = facilities->throughput;
131 }
132
133 if ((facilities->pacsize_in || facilities->pacsize_out) &&
134 (facil_mask & X25_MASK_PACKET_SIZE)) {
135 *p++ = X25_FAC_PACKET_SIZE;
136 *p++ = facilities->pacsize_in ? : facilities->pacsize_out;
137 *p++ = facilities->pacsize_out ? : facilities->pacsize_in;
138 }
139
140 if ((facilities->winsize_in || facilities->winsize_out) &&
141 (facil_mask & X25_MASK_WINDOW_SIZE)) {
142 *p++ = X25_FAC_WINDOW_SIZE;
143 *p++ = facilities->winsize_in ? : facilities->winsize_out;
144 *p++ = facilities->winsize_out ? : facilities->winsize_in;
145 }
146
147 len = p - buffer;
148 buffer[0] = len - 1;
149
150 return len;
151}
152
153/*
154 * Try to reach a compromise on a set of facilities.
155 *
156 * The only real problem is with reverse charging.
157 */
158int x25_negotiate_facilities(struct sk_buff *skb, struct sock *sk,
159 struct x25_facilities *new)
160{
161 struct x25_sock *x25 = x25_sk(sk);
162 struct x25_facilities *ours = &x25->facilities;
163 struct x25_facilities theirs;
164 int len;
165
166 memset(&theirs, 0, sizeof(theirs));
167 memcpy(new, ours, sizeof(*new));
168
169 len = x25_parse_facilities(skb, &theirs, &x25->vc_facil_mask);
170
171 /*
172 * They want reverse charging, we won't accept it.
173 */
174 if (theirs.reverse && ours->reverse) {
175 SOCK_DEBUG(sk, "X.25: rejecting reverse charging request");
176 return -1;
177 }
178
179 new->reverse = theirs.reverse;
180
181 if (theirs.throughput) {
182 if (theirs.throughput < ours->throughput) {
183 SOCK_DEBUG(sk, "X.25: throughput negotiated down");
184 new->throughput = theirs.throughput;
185 }
186 }
187
188 if (theirs.pacsize_in && theirs.pacsize_out) {
189 if (theirs.pacsize_in < ours->pacsize_in) {
190 SOCK_DEBUG(sk, "X.25: packet size inwards negotiated down");
191 new->pacsize_in = theirs.pacsize_in;
192 }
193 if (theirs.pacsize_out < ours->pacsize_out) {
194 SOCK_DEBUG(sk, "X.25: packet size outwards negotiated down");
195 new->pacsize_out = theirs.pacsize_out;
196 }
197 }
198
199 if (theirs.winsize_in && theirs.winsize_out) {
200 if (theirs.winsize_in < ours->winsize_in) {
201 SOCK_DEBUG(sk, "X.25: window size inwards negotiated down");
202 new->winsize_in = theirs.winsize_in;
203 }
204 if (theirs.winsize_out < ours->winsize_out) {
205 SOCK_DEBUG(sk, "X.25: window size outwards negotiated down");
206 new->winsize_out = theirs.winsize_out;
207 }
208 }
209
210 return len;
211}
212
213/*
214 * Limit values of certain facilities according to the capability of the
215 * currently attached x25 link.
216 */
217void x25_limit_facilities(struct x25_facilities *facilities,
218 struct x25_neigh *nb)
219{
220
221 if (!nb->extended) {
222 if (facilities->winsize_in > 7) {
223 printk(KERN_DEBUG "X.25: incoming winsize limited to 7\n");
224 facilities->winsize_in = 7;
225 }
226 if (facilities->winsize_out > 7) {
227 facilities->winsize_out = 7;
228 printk( KERN_DEBUG "X.25: outgoing winsize limited to 7\n");
229 }
230 }
231}
diff --git a/net/x25/x25_in.c b/net/x25/x25_in.c
new file mode 100644
index 000000000000..b0197c70a9fc
--- /dev/null
+++ b/net/x25/x25_in.c
@@ -0,0 +1,361 @@
1/*
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
6 * screw up. It might even work.
7 *
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 disconnection code.
19 * New timer architecture.
20 * 2000-03-20 Daniela Squassoni Disabling/enabling of facilities
21 * negotiation.
22 * 2000-11-10 Henner Eisen Check and reset for out-of-sequence
23 * i-frames.
24 */
25
26#include <linux/errno.h>
27#include <linux/kernel.h>
28#include <linux/string.h>
29#include <linux/skbuff.h>
30#include <net/sock.h>
31#include <net/tcp.h>
32#include <net/x25.h>
33
34static int x25_queue_rx_frame(struct sock *sk, struct sk_buff *skb, int more)
35{
36 struct sk_buff *skbo, *skbn = skb;
37 struct x25_sock *x25 = x25_sk(sk);
38
39 if (more) {
40 x25->fraglen += skb->len;
41 skb_queue_tail(&x25->fragment_queue, skb);
42 skb_set_owner_r(skb, sk);
43 return 0;
44 }
45
46 if (!more && x25->fraglen > 0) { /* End of fragment */
47 int len = x25->fraglen + skb->len;
48
49 if ((skbn = alloc_skb(len, GFP_ATOMIC)) == NULL){
50 kfree_skb(skb);
51 return 1;
52 }
53
54 skb_queue_tail(&x25->fragment_queue, skb);
55
56 skbn->h.raw = skbn->data;
57
58 skbo = skb_dequeue(&x25->fragment_queue);
59 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
60 kfree_skb(skbo);
61
62 while ((skbo =
63 skb_dequeue(&x25->fragment_queue)) != NULL) {
64 skb_pull(skbo, (x25->neighbour->extended) ?
65 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
66 memcpy(skb_put(skbn, skbo->len), skbo->data, skbo->len);
67 kfree_skb(skbo);
68 }
69
70 x25->fraglen = 0;
71 }
72
73 skb_set_owner_r(skbn, sk);
74 skb_queue_tail(&sk->sk_receive_queue, skbn);
75 if (!sock_flag(sk, SOCK_DEAD))
76 sk->sk_data_ready(sk, skbn->len);
77
78 return 0;
79}
80
81/*
82 * State machine for state 1, Awaiting Call Accepted State.
83 * The handling of the timer(s) is in file x25_timer.c.
84 * Handling of state 0 and connection release is in af_x25.c.
85 */
86static int x25_state1_machine(struct sock *sk, struct sk_buff *skb, int frametype)
87{
88 struct x25_address source_addr, dest_addr;
89
90 switch (frametype) {
91 case X25_CALL_ACCEPTED: {
92 struct x25_sock *x25 = x25_sk(sk);
93
94 x25_stop_timer(sk);
95 x25->condition = 0x00;
96 x25->vs = 0;
97 x25->va = 0;
98 x25->vr = 0;
99 x25->vl = 0;
100 x25->state = X25_STATE_3;
101 sk->sk_state = TCP_ESTABLISHED;
102 /*
103 * Parse the data in the frame.
104 */
105 skb_pull(skb, X25_STD_MIN_LEN);
106 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
107 skb_pull(skb,
108 x25_parse_facilities(skb, &x25->facilities,
109 &x25->vc_facil_mask));
110 /*
111 * Copy any Call User Data.
112 */
113 if (skb->len >= 0) {
114 memcpy(x25->calluserdata.cuddata, skb->data,
115 skb->len);
116 x25->calluserdata.cudlength = skb->len;
117 }
118 if (!sock_flag(sk, SOCK_DEAD))
119 sk->sk_state_change(sk);
120 break;
121 }
122 case X25_CLEAR_REQUEST:
123 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
124 x25_disconnect(sk, ECONNREFUSED, skb->data[3], skb->data[4]);
125 break;
126
127 default:
128 break;
129 }
130
131 return 0;
132}
133
134/*
135 * State machine for state 2, Awaiting Clear Confirmation State.
136 * The handling of the timer(s) is in file x25_timer.c
137 * Handling of state 0 and connection release is in af_x25.c.
138 */
139static int x25_state2_machine(struct sock *sk, struct sk_buff *skb, int frametype)
140{
141 switch (frametype) {
142
143 case X25_CLEAR_REQUEST:
144 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
145 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
146 break;
147
148 case X25_CLEAR_CONFIRMATION:
149 x25_disconnect(sk, 0, 0, 0);
150 break;
151
152 default:
153 break;
154 }
155
156 return 0;
157}
158
159/*
160 * State machine for state 3, Connected State.
161 * The handling of the timer(s) is in file x25_timer.c
162 * Handling of state 0 and connection release is in af_x25.c.
163 */
164static int x25_state3_machine(struct sock *sk, struct sk_buff *skb, int frametype, int ns, int nr, int q, int d, int m)
165{
166 int queued = 0;
167 int modulus;
168 struct x25_sock *x25 = x25_sk(sk);
169
170 modulus = (x25->neighbour->extended) ? X25_EMODULUS : X25_SMODULUS;
171
172 switch (frametype) {
173
174 case X25_RESET_REQUEST:
175 x25_write_internal(sk, X25_RESET_CONFIRMATION);
176 x25_stop_timer(sk);
177 x25->condition = 0x00;
178 x25->vs = 0;
179 x25->vr = 0;
180 x25->va = 0;
181 x25->vl = 0;
182 x25_requeue_frames(sk);
183 break;
184
185 case X25_CLEAR_REQUEST:
186 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
187 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
188 break;
189
190 case X25_RR:
191 case X25_RNR:
192 if (!x25_validate_nr(sk, nr)) {
193 x25_clear_queues(sk);
194 x25_write_internal(sk, X25_RESET_REQUEST);
195 x25_start_t22timer(sk);
196 x25->condition = 0x00;
197 x25->vs = 0;
198 x25->vr = 0;
199 x25->va = 0;
200 x25->vl = 0;
201 x25->state = X25_STATE_4;
202 } else {
203 x25_frames_acked(sk, nr);
204 if (frametype == X25_RNR) {
205 x25->condition |= X25_COND_PEER_RX_BUSY;
206 } else {
207 x25->condition &= ~X25_COND_PEER_RX_BUSY;
208 }
209 }
210 break;
211
212 case X25_DATA: /* XXX */
213 x25->condition &= ~X25_COND_PEER_RX_BUSY;
214 if ((ns != x25->vr) || !x25_validate_nr(sk, nr)) {
215 x25_clear_queues(sk);
216 x25_write_internal(sk, X25_RESET_REQUEST);
217 x25_start_t22timer(sk);
218 x25->condition = 0x00;
219 x25->vs = 0;
220 x25->vr = 0;
221 x25->va = 0;
222 x25->vl = 0;
223 x25->state = X25_STATE_4;
224 break;
225 }
226 x25_frames_acked(sk, nr);
227 if (ns == x25->vr) {
228 if (x25_queue_rx_frame(sk, skb, m) == 0) {
229 x25->vr = (x25->vr + 1) % modulus;
230 queued = 1;
231 } else {
232 /* Should never happen */
233 x25_clear_queues(sk);
234 x25_write_internal(sk, X25_RESET_REQUEST);
235 x25_start_t22timer(sk);
236 x25->condition = 0x00;
237 x25->vs = 0;
238 x25->vr = 0;
239 x25->va = 0;
240 x25->vl = 0;
241 x25->state = X25_STATE_4;
242 break;
243 }
244 if (atomic_read(&sk->sk_rmem_alloc) >
245 (sk->sk_rcvbuf / 2))
246 x25->condition |= X25_COND_OWN_RX_BUSY;
247 }
248 /*
249 * If the window is full Ack it immediately, else
250 * start the holdback timer.
251 */
252 if (((x25->vl + x25->facilities.winsize_in) % modulus) == x25->vr) {
253 x25->condition &= ~X25_COND_ACK_PENDING;
254 x25_stop_timer(sk);
255 x25_enquiry_response(sk);
256 } else {
257 x25->condition |= X25_COND_ACK_PENDING;
258 x25_start_t2timer(sk);
259 }
260 break;
261
262 case X25_INTERRUPT_CONFIRMATION:
263 x25->intflag = 0;
264 break;
265
266 case X25_INTERRUPT:
267 if (sock_flag(sk, SOCK_URGINLINE))
268 queued = !sock_queue_rcv_skb(sk, skb);
269 else {
270 skb_set_owner_r(skb, sk);
271 skb_queue_tail(&x25->interrupt_in_queue, skb);
272 queued = 1;
273 }
274 sk_send_sigurg(sk);
275 x25_write_internal(sk, X25_INTERRUPT_CONFIRMATION);
276 break;
277
278 default:
279 printk(KERN_WARNING "x25: unknown %02X in state 3\n", frametype);
280 break;
281 }
282
283 return queued;
284}
285
286/*
287 * State machine for state 4, Awaiting Reset Confirmation State.
288 * The handling of the timer(s) is in file x25_timer.c
289 * Handling of state 0 and connection release is in af_x25.c.
290 */
291static int x25_state4_machine(struct sock *sk, struct sk_buff *skb, int frametype)
292{
293 switch (frametype) {
294
295 case X25_RESET_REQUEST:
296 x25_write_internal(sk, X25_RESET_CONFIRMATION);
297 case X25_RESET_CONFIRMATION: {
298 struct x25_sock *x25 = x25_sk(sk);
299
300 x25_stop_timer(sk);
301 x25->condition = 0x00;
302 x25->va = 0;
303 x25->vr = 0;
304 x25->vs = 0;
305 x25->vl = 0;
306 x25->state = X25_STATE_3;
307 x25_requeue_frames(sk);
308 break;
309 }
310 case X25_CLEAR_REQUEST:
311 x25_write_internal(sk, X25_CLEAR_CONFIRMATION);
312 x25_disconnect(sk, 0, skb->data[3], skb->data[4]);
313 break;
314
315 default:
316 break;
317 }
318
319 return 0;
320}
321
322/* Higher level upcall for a LAPB frame */
323int x25_process_rx_frame(struct sock *sk, struct sk_buff *skb)
324{
325 struct x25_sock *x25 = x25_sk(sk);
326 int queued = 0, frametype, ns, nr, q, d, m;
327
328 if (x25->state == X25_STATE_0)
329 return 0;
330
331 frametype = x25_decode(sk, skb, &ns, &nr, &q, &d, &m);
332
333 switch (x25->state) {
334 case X25_STATE_1:
335 queued = x25_state1_machine(sk, skb, frametype);
336 break;
337 case X25_STATE_2:
338 queued = x25_state2_machine(sk, skb, frametype);
339 break;
340 case X25_STATE_3:
341 queued = x25_state3_machine(sk, skb, frametype, ns, nr, q, d, m);
342 break;
343 case X25_STATE_4:
344 queued = x25_state4_machine(sk, skb, frametype);
345 break;
346 }
347
348 x25_kick(sk);
349
350 return queued;
351}
352
353int x25_backlog_rcv(struct sock *sk, struct sk_buff *skb)
354{
355 int queued = x25_process_rx_frame(sk, skb);
356
357 if (!queued)
358 kfree_skb(skb);
359
360 return 0;
361}
diff --git a/net/x25/x25_link.c b/net/x25/x25_link.c
new file mode 100644
index 000000000000..0a760fe66843
--- /dev/null
+++ b/net/x25/x25_link.c
@@ -0,0 +1,401 @@
1/*
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
6 * screw up. It might even work.
7 *
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 New timer architecture.
19 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
20 * negotiation.
21 * 2000-09-04 Henner Eisen dev_hold() / dev_put() for x25_neigh.
22 */
23
24#include <linux/kernel.h>
25#include <linux/jiffies.h>
26#include <linux/timer.h>
27#include <linux/netdevice.h>
28#include <linux/skbuff.h>
29#include <asm/uaccess.h>
30#include <linux/init.h>
31#include <net/x25.h>
32
33static struct list_head x25_neigh_list = LIST_HEAD_INIT(x25_neigh_list);
34static DEFINE_RWLOCK(x25_neigh_list_lock);
35
36static void x25_t20timer_expiry(unsigned long);
37
38static void x25_transmit_restart_confirmation(struct x25_neigh *nb);
39static void x25_transmit_restart_request(struct x25_neigh *nb);
40
41/*
42 * Linux set/reset timer routines
43 */
44static inline void x25_start_t20timer(struct x25_neigh *nb)
45{
46 mod_timer(&nb->t20timer, jiffies + nb->t20);
47}
48
49static void x25_t20timer_expiry(unsigned long param)
50{
51 struct x25_neigh *nb = (struct x25_neigh *)param;
52
53 x25_transmit_restart_request(nb);
54
55 x25_start_t20timer(nb);
56}
57
58static inline void x25_stop_t20timer(struct x25_neigh *nb)
59{
60 del_timer(&nb->t20timer);
61}
62
63static inline int x25_t20timer_pending(struct x25_neigh *nb)
64{
65 return timer_pending(&nb->t20timer);
66}
67
68/*
69 * This handles all restart and diagnostic frames.
70 */
71void x25_link_control(struct sk_buff *skb, struct x25_neigh *nb,
72 unsigned short frametype)
73{
74 struct sk_buff *skbn;
75 int confirm;
76
77 switch (frametype) {
78 case X25_RESTART_REQUEST:
79 confirm = !x25_t20timer_pending(nb);
80 x25_stop_t20timer(nb);
81 nb->state = X25_LINK_STATE_3;
82 if (confirm)
83 x25_transmit_restart_confirmation(nb);
84 break;
85
86 case X25_RESTART_CONFIRMATION:
87 x25_stop_t20timer(nb);
88 nb->state = X25_LINK_STATE_3;
89 break;
90
91 case X25_DIAGNOSTIC:
92 printk(KERN_WARNING "x25: diagnostic #%d - "
93 "%02X %02X %02X\n",
94 skb->data[3], skb->data[4],
95 skb->data[5], skb->data[6]);
96 break;
97
98 default:
99 printk(KERN_WARNING "x25: received unknown %02X "
100 "with LCI 000\n", frametype);
101 break;
102 }
103
104 if (nb->state == X25_LINK_STATE_3)
105 while ((skbn = skb_dequeue(&nb->queue)) != NULL)
106 x25_send_frame(skbn, nb);
107}
108
109/*
110 * This routine is called when a Restart Request is needed
111 */
112static void x25_transmit_restart_request(struct x25_neigh *nb)
113{
114 unsigned char *dptr;
115 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
116 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
117
118 if (!skb)
119 return;
120
121 skb_reserve(skb, X25_MAX_L2_LEN);
122
123 dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
124
125 *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
126 *dptr++ = 0x00;
127 *dptr++ = X25_RESTART_REQUEST;
128 *dptr++ = 0x00;
129 *dptr++ = 0;
130
131 skb->sk = NULL;
132
133 x25_send_frame(skb, nb);
134}
135
136/*
137 * This routine is called when a Restart Confirmation is needed
138 */
139static void x25_transmit_restart_confirmation(struct x25_neigh *nb)
140{
141 unsigned char *dptr;
142 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN;
143 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
144
145 if (!skb)
146 return;
147
148 skb_reserve(skb, X25_MAX_L2_LEN);
149
150 dptr = skb_put(skb, X25_STD_MIN_LEN);
151
152 *dptr++ = nb->extended ? X25_GFI_EXTSEQ : X25_GFI_STDSEQ;
153 *dptr++ = 0x00;
154 *dptr++ = X25_RESTART_CONFIRMATION;
155
156 skb->sk = NULL;
157
158 x25_send_frame(skb, nb);
159}
160
161/*
162 * This routine is called when a Clear Request is needed outside of the context
163 * of a connected socket.
164 */
165void x25_transmit_clear_request(struct x25_neigh *nb, unsigned int lci,
166 unsigned char cause)
167{
168 unsigned char *dptr;
169 int len = X25_MAX_L2_LEN + X25_STD_MIN_LEN + 2;
170 struct sk_buff *skb = alloc_skb(len, GFP_ATOMIC);
171
172 if (!skb)
173 return;
174
175 skb_reserve(skb, X25_MAX_L2_LEN);
176
177 dptr = skb_put(skb, X25_STD_MIN_LEN + 2);
178
179 *dptr++ = ((lci >> 8) & 0x0F) | (nb->extended ?
180 X25_GFI_EXTSEQ :
181 X25_GFI_STDSEQ);
182 *dptr++ = (lci >> 0) & 0xFF;
183 *dptr++ = X25_CLEAR_REQUEST;
184 *dptr++ = cause;
185 *dptr++ = 0x00;
186
187 skb->sk = NULL;
188
189 x25_send_frame(skb, nb);
190}
191
192void x25_transmit_link(struct sk_buff *skb, struct x25_neigh *nb)
193{
194 switch (nb->state) {
195 case X25_LINK_STATE_0:
196 skb_queue_tail(&nb->queue, skb);
197 nb->state = X25_LINK_STATE_1;
198 x25_establish_link(nb);
199 break;
200 case X25_LINK_STATE_1:
201 case X25_LINK_STATE_2:
202 skb_queue_tail(&nb->queue, skb);
203 break;
204 case X25_LINK_STATE_3:
205 x25_send_frame(skb, nb);
206 break;
207 }
208}
209
210/*
211 * Called when the link layer has become established.
212 */
213void x25_link_established(struct x25_neigh *nb)
214{
215 switch (nb->state) {
216 case X25_LINK_STATE_0:
217 nb->state = X25_LINK_STATE_2;
218 break;
219 case X25_LINK_STATE_1:
220 x25_transmit_restart_request(nb);
221 nb->state = X25_LINK_STATE_2;
222 x25_start_t20timer(nb);
223 break;
224 }
225}
226
227/*
228 * Called when the link layer has terminated, or an establishment
229 * request has failed.
230 */
231
232void x25_link_terminated(struct x25_neigh *nb)
233{
234 nb->state = X25_LINK_STATE_0;
235 /* Out of order: clear existing virtual calls (X.25 03/93 4.6.3) */
236 x25_kill_by_neigh(nb);
237}
238
239/*
240 * Add a new device.
241 */
242void x25_link_device_up(struct net_device *dev)
243{
244 struct x25_neigh *nb = kmalloc(sizeof(*nb), GFP_ATOMIC);
245
246 if (!nb)
247 return;
248
249 skb_queue_head_init(&nb->queue);
250
251 init_timer(&nb->t20timer);
252 nb->t20timer.data = (unsigned long)nb;
253 nb->t20timer.function = &x25_t20timer_expiry;
254
255 dev_hold(dev);
256 nb->dev = dev;
257 nb->state = X25_LINK_STATE_0;
258 nb->extended = 0;
259 /*
260 * Enables negotiation
261 */
262 nb->global_facil_mask = X25_MASK_REVERSE |
263 X25_MASK_THROUGHPUT |
264 X25_MASK_PACKET_SIZE |
265 X25_MASK_WINDOW_SIZE;
266 nb->t20 = sysctl_x25_restart_request_timeout;
267 atomic_set(&nb->refcnt, 1);
268
269 write_lock_bh(&x25_neigh_list_lock);
270 list_add(&nb->node, &x25_neigh_list);
271 write_unlock_bh(&x25_neigh_list_lock);
272}
273
274/**
275 * __x25_remove_neigh - remove neighbour from x25_neigh_list
276 * @nb - neigh to remove
277 *
278 * Remove neighbour from x25_neigh_list. If it was there.
279 * Caller must hold x25_neigh_list_lock.
280 */
281static void __x25_remove_neigh(struct x25_neigh *nb)
282{
283 skb_queue_purge(&nb->queue);
284 x25_stop_t20timer(nb);
285
286 if (nb->node.next) {
287 list_del(&nb->node);
288 x25_neigh_put(nb);
289 }
290}
291
292/*
293 * A device has been removed, remove its links.
294 */
295void x25_link_device_down(struct net_device *dev)
296{
297 struct x25_neigh *nb;
298 struct list_head *entry, *tmp;
299
300 write_lock_bh(&x25_neigh_list_lock);
301
302 list_for_each_safe(entry, tmp, &x25_neigh_list) {
303 nb = list_entry(entry, struct x25_neigh, node);
304
305 if (nb->dev == dev) {
306 __x25_remove_neigh(nb);
307 dev_put(dev);
308 }
309 }
310
311 write_unlock_bh(&x25_neigh_list_lock);
312}
313
314/*
315 * Given a device, return the neighbour address.
316 */
317struct x25_neigh *x25_get_neigh(struct net_device *dev)
318{
319 struct x25_neigh *nb, *use = NULL;
320 struct list_head *entry;
321
322 read_lock_bh(&x25_neigh_list_lock);
323 list_for_each(entry, &x25_neigh_list) {
324 nb = list_entry(entry, struct x25_neigh, node);
325
326 if (nb->dev == dev) {
327 use = nb;
328 break;
329 }
330 }
331
332 if (use)
333 x25_neigh_hold(use);
334 read_unlock_bh(&x25_neigh_list_lock);
335 return use;
336}
337
338/*
339 * Handle the ioctls that control the subscription functions.
340 */
341int x25_subscr_ioctl(unsigned int cmd, void __user *arg)
342{
343 struct x25_subscrip_struct x25_subscr;
344 struct x25_neigh *nb;
345 struct net_device *dev;
346 int rc = -EINVAL;
347
348 if (cmd != SIOCX25GSUBSCRIP && cmd != SIOCX25SSUBSCRIP)
349 goto out;
350
351 rc = -EFAULT;
352 if (copy_from_user(&x25_subscr, arg, sizeof(x25_subscr)))
353 goto out;
354
355 rc = -EINVAL;
356 if ((dev = x25_dev_get(x25_subscr.device)) == NULL)
357 goto out;
358
359 if ((nb = x25_get_neigh(dev)) == NULL)
360 goto out_dev_put;
361
362 dev_put(dev);
363
364 if (cmd == SIOCX25GSUBSCRIP) {
365 x25_subscr.extended = nb->extended;
366 x25_subscr.global_facil_mask = nb->global_facil_mask;
367 rc = copy_to_user(arg, &x25_subscr,
368 sizeof(x25_subscr)) ? -EFAULT : 0;
369 } else {
370 rc = -EINVAL;
371 if (!(x25_subscr.extended && x25_subscr.extended != 1)) {
372 rc = 0;
373 nb->extended = x25_subscr.extended;
374 nb->global_facil_mask = x25_subscr.global_facil_mask;
375 }
376 }
377 x25_neigh_put(nb);
378out:
379 return rc;
380out_dev_put:
381 dev_put(dev);
382 goto out;
383}
384
385
386/*
387 * Release all memory associated with X.25 neighbour structures.
388 */
389void __exit x25_link_free(void)
390{
391 struct x25_neigh *nb;
392 struct list_head *entry, *tmp;
393
394 write_lock_bh(&x25_neigh_list_lock);
395
396 list_for_each_safe(entry, tmp, &x25_neigh_list) {
397 nb = list_entry(entry, struct x25_neigh, node);
398 __x25_remove_neigh(nb);
399 }
400 write_unlock_bh(&x25_neigh_list_lock);
401}
diff --git a/net/x25/x25_out.c b/net/x25/x25_out.c
new file mode 100644
index 000000000000..a2e62cea819a
--- /dev/null
+++ b/net/x25/x25_out.c
@@ -0,0 +1,226 @@
1/*
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
6 * screw up. It might even work.
7 *
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 New timer architecture.
19 * 2000-09-04 Henner Eisen Prevented x25_output() skb leakage.
20 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
21 * 2000-11-10 Henner Eisen x25_send_iframe(): re-queued frames
22 * needed cleaned seq-number fields.
23 */
24
25#include <linux/socket.h>
26#include <linux/kernel.h>
27#include <linux/string.h>
28#include <linux/skbuff.h>
29#include <net/sock.h>
30#include <net/x25.h>
31
32static int x25_pacsize_to_bytes(unsigned int pacsize)
33{
34 int bytes = 1;
35
36 if (!pacsize)
37 return 128;
38
39 while (pacsize-- > 0)
40 bytes *= 2;
41
42 return bytes;
43}
44
45/*
46 * This is where all X.25 information frames pass.
47 *
48 * Returns the amount of user data bytes sent on success
49 * or a negative error code on failure.
50 */
51int x25_output(struct sock *sk, struct sk_buff *skb)
52{
53 struct sk_buff *skbn;
54 unsigned char header[X25_EXT_MIN_LEN];
55 int err, frontlen, len;
56 int sent=0, noblock = X25_SKB_CB(skb)->flags & MSG_DONTWAIT;
57 struct x25_sock *x25 = x25_sk(sk);
58 int header_len = x25->neighbour->extended ? X25_EXT_MIN_LEN :
59 X25_STD_MIN_LEN;
60 int max_len = x25_pacsize_to_bytes(x25->facilities.pacsize_out);
61
62 if (skb->len - header_len > max_len) {
63 /* Save a copy of the Header */
64 memcpy(header, skb->data, header_len);
65 skb_pull(skb, header_len);
66
67 frontlen = skb_headroom(skb);
68
69 while (skb->len > 0) {
70 if ((skbn = sock_alloc_send_skb(sk, frontlen + max_len,
71 noblock, &err)) == NULL){
72 if (err == -EWOULDBLOCK && noblock){
73 kfree_skb(skb);
74 return sent;
75 }
76 SOCK_DEBUG(sk, "x25_output: fragment alloc"
77 " failed, err=%d, %d bytes "
78 "sent\n", err, sent);
79 return err;
80 }
81
82 skb_reserve(skbn, frontlen);
83
84 len = max_len > skb->len ? skb->len : max_len;
85
86 /* Copy the user data */
87 memcpy(skb_put(skbn, len), skb->data, len);
88 skb_pull(skb, len);
89
90 /* Duplicate the Header */
91 skb_push(skbn, header_len);
92 memcpy(skbn->data, header, header_len);
93
94 if (skb->len > 0) {
95 if (x25->neighbour->extended)
96 skbn->data[3] |= X25_EXT_M_BIT;
97 else
98 skbn->data[2] |= X25_STD_M_BIT;
99 }
100
101 skb_queue_tail(&sk->sk_write_queue, skbn);
102 sent += len;
103 }
104
105 kfree_skb(skb);
106 } else {
107 skb_queue_tail(&sk->sk_write_queue, skb);
108 sent = skb->len - header_len;
109 }
110 return sent;
111}
112
113/*
114 * This procedure is passed a buffer descriptor for an iframe. It builds
115 * the rest of the control part of the frame and then writes it out.
116 */
117static void x25_send_iframe(struct sock *sk, struct sk_buff *skb)
118{
119 struct x25_sock *x25 = x25_sk(sk);
120
121 if (!skb)
122 return;
123
124 if (x25->neighbour->extended) {
125 skb->data[2] = (x25->vs << 1) & 0xFE;
126 skb->data[3] &= X25_EXT_M_BIT;
127 skb->data[3] |= (x25->vr << 1) & 0xFE;
128 } else {
129 skb->data[2] &= X25_STD_M_BIT;
130 skb->data[2] |= (x25->vs << 1) & 0x0E;
131 skb->data[2] |= (x25->vr << 5) & 0xE0;
132 }
133
134 x25_transmit_link(skb, x25->neighbour);
135}
136
137void x25_kick(struct sock *sk)
138{
139 struct sk_buff *skb, *skbn;
140 unsigned short start, end;
141 int modulus;
142 struct x25_sock *x25 = x25_sk(sk);
143
144 if (x25->state != X25_STATE_3)
145 return;
146
147 /*
148 * Transmit interrupt data.
149 */
150 if (!x25->intflag && skb_peek(&x25->interrupt_out_queue) != NULL) {
151 x25->intflag = 1;
152 skb = skb_dequeue(&x25->interrupt_out_queue);
153 x25_transmit_link(skb, x25->neighbour);
154 }
155
156 if (x25->condition & X25_COND_PEER_RX_BUSY)
157 return;
158
159 if (!skb_peek(&sk->sk_write_queue))
160 return;
161
162 modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
163
164 start = skb_peek(&x25->ack_queue) ? x25->vs : x25->va;
165 end = (x25->va + x25->facilities.winsize_out) % modulus;
166
167 if (start == end)
168 return;
169
170 x25->vs = start;
171
172 /*
173 * Transmit data until either we're out of data to send or
174 * the window is full.
175 */
176
177 skb = skb_dequeue(&sk->sk_write_queue);
178
179 do {
180 if ((skbn = skb_clone(skb, GFP_ATOMIC)) == NULL) {
181 skb_queue_head(&sk->sk_write_queue, skb);
182 break;
183 }
184
185 skb_set_owner_w(skbn, sk);
186
187 /*
188 * Transmit the frame copy.
189 */
190 x25_send_iframe(sk, skbn);
191
192 x25->vs = (x25->vs + 1) % modulus;
193
194 /*
195 * Requeue the original data frame.
196 */
197 skb_queue_tail(&x25->ack_queue, skb);
198
199 } while (x25->vs != end &&
200 (skb = skb_dequeue(&sk->sk_write_queue)) != NULL);
201
202 x25->vl = x25->vr;
203 x25->condition &= ~X25_COND_ACK_PENDING;
204
205 x25_stop_timer(sk);
206}
207
208/*
209 * The following routines are taken from page 170 of the 7th ARRL Computer
210 * Networking Conference paper, as is the whole state machine.
211 */
212
213void x25_enquiry_response(struct sock *sk)
214{
215 struct x25_sock *x25 = x25_sk(sk);
216
217 if (x25->condition & X25_COND_OWN_RX_BUSY)
218 x25_write_internal(sk, X25_RNR);
219 else
220 x25_write_internal(sk, X25_RR);
221
222 x25->vl = x25->vr;
223 x25->condition &= ~X25_COND_ACK_PENDING;
224
225 x25_stop_timer(sk);
226}
diff --git a/net/x25/x25_proc.c b/net/x25/x25_proc.c
new file mode 100644
index 000000000000..dfb80116c59f
--- /dev/null
+++ b/net/x25/x25_proc.c
@@ -0,0 +1,256 @@
1/*
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
6 * screw up. It might even work.
7 *
8 * This code REQUIRES 2.4 with seq_file support
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 * 2002/10/06 Arnaldo Carvalho de Melo seq_file support
18 */
19
20#include <linux/config.h>
21#include <linux/init.h>
22#include <linux/proc_fs.h>
23#include <linux/seq_file.h>
24#include <net/sock.h>
25#include <net/x25.h>
26
27#ifdef CONFIG_PROC_FS
28static __inline__ struct x25_route *x25_get_route_idx(loff_t pos)
29{
30 struct list_head *route_entry;
31 struct x25_route *rt = NULL;
32
33 list_for_each(route_entry, &x25_route_list) {
34 rt = list_entry(route_entry, struct x25_route, node);
35 if (!pos--)
36 goto found;
37 }
38 rt = NULL;
39found:
40 return rt;
41}
42
43static void *x25_seq_route_start(struct seq_file *seq, loff_t *pos)
44{
45 loff_t l = *pos;
46
47 read_lock_bh(&x25_route_list_lock);
48 return l ? x25_get_route_idx(--l) : SEQ_START_TOKEN;
49}
50
51static void *x25_seq_route_next(struct seq_file *seq, void *v, loff_t *pos)
52{
53 struct x25_route *rt;
54
55 ++*pos;
56 if (v == SEQ_START_TOKEN) {
57 rt = NULL;
58 if (!list_empty(&x25_route_list))
59 rt = list_entry(x25_route_list.next,
60 struct x25_route, node);
61 goto out;
62 }
63 rt = v;
64 if (rt->node.next != &x25_route_list)
65 rt = list_entry(rt->node.next, struct x25_route, node);
66 else
67 rt = NULL;
68out:
69 return rt;
70}
71
72static void x25_seq_route_stop(struct seq_file *seq, void *v)
73{
74 read_unlock_bh(&x25_route_list_lock);
75}
76
77static int x25_seq_route_show(struct seq_file *seq, void *v)
78{
79 struct x25_route *rt;
80
81 if (v == SEQ_START_TOKEN) {
82 seq_puts(seq, "Address Digits Device\n");
83 goto out;
84 }
85
86 rt = v;
87 seq_printf(seq, "%-15s %-6d %-5s\n",
88 rt->address.x25_addr, rt->sigdigits,
89 rt->dev ? rt->dev->name : "???");
90out:
91 return 0;
92}
93
94static __inline__ struct sock *x25_get_socket_idx(loff_t pos)
95{
96 struct sock *s;
97 struct hlist_node *node;
98
99 sk_for_each(s, node, &x25_list)
100 if (!pos--)
101 goto found;
102 s = NULL;
103found:
104 return s;
105}
106
107static void *x25_seq_socket_start(struct seq_file *seq, loff_t *pos)
108{
109 loff_t l = *pos;
110
111 read_lock_bh(&x25_list_lock);
112 return l ? x25_get_socket_idx(--l) : SEQ_START_TOKEN;
113}
114
115static void *x25_seq_socket_next(struct seq_file *seq, void *v, loff_t *pos)
116{
117 struct sock *s;
118
119 ++*pos;
120 if (v == SEQ_START_TOKEN) {
121 s = sk_head(&x25_list);
122 goto out;
123 }
124 s = sk_next(v);
125out:
126 return s;
127}
128
129static void x25_seq_socket_stop(struct seq_file *seq, void *v)
130{
131 read_unlock_bh(&x25_list_lock);
132}
133
134static int x25_seq_socket_show(struct seq_file *seq, void *v)
135{
136 struct sock *s;
137 struct x25_sock *x25;
138 struct net_device *dev;
139 const char *devname;
140
141 if (v == SEQ_START_TOKEN) {
142 seq_printf(seq, "dest_addr src_addr dev lci st vs vr "
143 "va t t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
144 goto out;
145 }
146
147 s = v;
148 x25 = x25_sk(s);
149
150 if (!x25->neighbour || (dev = x25->neighbour->dev) == NULL)
151 devname = "???";
152 else
153 devname = x25->neighbour->dev->name;
154
155 seq_printf(seq, "%-10s %-10s %-5s %3.3X %d %d %d %d %3lu %3lu "
156 "%3lu %3lu %3lu %5d %5d %ld\n",
157 !x25->dest_addr.x25_addr[0] ? "*" : x25->dest_addr.x25_addr,
158 !x25->source_addr.x25_addr[0] ? "*" : x25->source_addr.x25_addr,
159 devname, x25->lci & 0x0FFF, x25->state, x25->vs, x25->vr,
160 x25->va, x25_display_timer(s) / HZ, x25->t2 / HZ,
161 x25->t21 / HZ, x25->t22 / HZ, x25->t23 / HZ,
162 atomic_read(&s->sk_wmem_alloc),
163 atomic_read(&s->sk_rmem_alloc),
164 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
165out:
166 return 0;
167}
168
169static struct seq_operations x25_seq_route_ops = {
170 .start = x25_seq_route_start,
171 .next = x25_seq_route_next,
172 .stop = x25_seq_route_stop,
173 .show = x25_seq_route_show,
174};
175
176static struct seq_operations x25_seq_socket_ops = {
177 .start = x25_seq_socket_start,
178 .next = x25_seq_socket_next,
179 .stop = x25_seq_socket_stop,
180 .show = x25_seq_socket_show,
181};
182
183static int x25_seq_socket_open(struct inode *inode, struct file *file)
184{
185 return seq_open(file, &x25_seq_socket_ops);
186}
187
188static int x25_seq_route_open(struct inode *inode, struct file *file)
189{
190 return seq_open(file, &x25_seq_route_ops);
191}
192
193static struct file_operations x25_seq_socket_fops = {
194 .owner = THIS_MODULE,
195 .open = x25_seq_socket_open,
196 .read = seq_read,
197 .llseek = seq_lseek,
198 .release = seq_release,
199};
200
201static struct file_operations x25_seq_route_fops = {
202 .owner = THIS_MODULE,
203 .open = x25_seq_route_open,
204 .read = seq_read,
205 .llseek = seq_lseek,
206 .release = seq_release,
207};
208
209static struct proc_dir_entry *x25_proc_dir;
210
211int __init x25_proc_init(void)
212{
213 struct proc_dir_entry *p;
214 int rc = -ENOMEM;
215
216 x25_proc_dir = proc_mkdir("x25", proc_net);
217 if (!x25_proc_dir)
218 goto out;
219
220 p = create_proc_entry("route", S_IRUGO, x25_proc_dir);
221 if (!p)
222 goto out_route;
223 p->proc_fops = &x25_seq_route_fops;
224
225 p = create_proc_entry("socket", S_IRUGO, x25_proc_dir);
226 if (!p)
227 goto out_socket;
228 p->proc_fops = &x25_seq_socket_fops;
229 rc = 0;
230out:
231 return rc;
232out_socket:
233 remove_proc_entry("route", x25_proc_dir);
234out_route:
235 remove_proc_entry("x25", proc_net);
236 goto out;
237}
238
239void __exit x25_proc_exit(void)
240{
241 remove_proc_entry("route", x25_proc_dir);
242 remove_proc_entry("socket", x25_proc_dir);
243 remove_proc_entry("x25", proc_net);
244}
245
246#else /* CONFIG_PROC_FS */
247
248int __init x25_proc_init(void)
249{
250 return 0;
251}
252
253void __exit x25_proc_exit(void)
254{
255}
256#endif /* CONFIG_PROC_FS */
diff --git a/net/x25/x25_route.c b/net/x25/x25_route.c
new file mode 100644
index 000000000000..6c5d37517035
--- /dev/null
+++ b/net/x25/x25_route.c
@@ -0,0 +1,221 @@
1/*
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
6 * screw up. It might even work.
7 *
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 */
19
20#include <linux/config.h>
21#include <linux/if_arp.h>
22#include <linux/init.h>
23#include <net/x25.h>
24
25struct list_head x25_route_list = LIST_HEAD_INIT(x25_route_list);
26DEFINE_RWLOCK(x25_route_list_lock);
27
28/*
29 * Add a new route.
30 */
31static int x25_add_route(struct x25_address *address, unsigned int sigdigits,
32 struct net_device *dev)
33{
34 struct x25_route *rt;
35 struct list_head *entry;
36 int rc = -EINVAL;
37
38 write_lock_bh(&x25_route_list_lock);
39
40 list_for_each(entry, &x25_route_list) {
41 rt = list_entry(entry, struct x25_route, node);
42
43 if (!memcmp(&rt->address, address, sigdigits) &&
44 rt->sigdigits == sigdigits)
45 goto out;
46 }
47
48 rt = kmalloc(sizeof(*rt), GFP_ATOMIC);
49 rc = -ENOMEM;
50 if (!rt)
51 goto out;
52
53 strcpy(rt->address.x25_addr, "000000000000000");
54 memcpy(rt->address.x25_addr, address->x25_addr, sigdigits);
55
56 rt->sigdigits = sigdigits;
57 rt->dev = dev;
58 atomic_set(&rt->refcnt, 1);
59
60 list_add(&rt->node, &x25_route_list);
61 rc = 0;
62out:
63 write_unlock_bh(&x25_route_list_lock);
64 return rc;
65}
66
67/**
68 * __x25_remove_route - remove route from x25_route_list
69 * @rt - route to remove
70 *
71 * Remove route from x25_route_list. If it was there.
72 * Caller must hold x25_route_list_lock.
73 */
74static void __x25_remove_route(struct x25_route *rt)
75{
76 if (rt->node.next) {
77 list_del(&rt->node);
78 x25_route_put(rt);
79 }
80}
81
82static int x25_del_route(struct x25_address *address, unsigned int sigdigits,
83 struct net_device *dev)
84{
85 struct x25_route *rt;
86 struct list_head *entry;
87 int rc = -EINVAL;
88
89 write_lock_bh(&x25_route_list_lock);
90
91 list_for_each(entry, &x25_route_list) {
92 rt = list_entry(entry, struct x25_route, node);
93
94 if (!memcmp(&rt->address, address, sigdigits) &&
95 rt->sigdigits == sigdigits && rt->dev == dev) {
96 __x25_remove_route(rt);
97 rc = 0;
98 break;
99 }
100 }
101
102 write_unlock_bh(&x25_route_list_lock);
103 return rc;
104}
105
106/*
107 * A device has been removed, remove its routes.
108 */
109void x25_route_device_down(struct net_device *dev)
110{
111 struct x25_route *rt;
112 struct list_head *entry, *tmp;
113
114 write_lock_bh(&x25_route_list_lock);
115
116 list_for_each_safe(entry, tmp, &x25_route_list) {
117 rt = list_entry(entry, struct x25_route, node);
118
119 if (rt->dev == dev)
120 __x25_remove_route(rt);
121 }
122 write_unlock_bh(&x25_route_list_lock);
123}
124
125/*
126 * Check that the device given is a valid X.25 interface that is "up".
127 */
128struct net_device *x25_dev_get(char *devname)
129{
130 struct net_device *dev = dev_get_by_name(devname);
131
132 if (dev &&
133 (!(dev->flags & IFF_UP) || (dev->type != ARPHRD_X25
134#if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
135 && dev->type != ARPHRD_ETHER
136#endif
137 )))
138 dev_put(dev);
139
140 return dev;
141}
142
143/**
144 * x25_get_route - Find a route given an X.25 address.
145 * @addr - address to find a route for
146 *
147 * Find a route given an X.25 address.
148 */
149struct x25_route *x25_get_route(struct x25_address *addr)
150{
151 struct x25_route *rt, *use = NULL;
152 struct list_head *entry;
153
154 read_lock_bh(&x25_route_list_lock);
155
156 list_for_each(entry, &x25_route_list) {
157 rt = list_entry(entry, struct x25_route, node);
158
159 if (!memcmp(&rt->address, addr, rt->sigdigits)) {
160 if (!use)
161 use = rt;
162 else if (rt->sigdigits > use->sigdigits)
163 use = rt;
164 }
165 }
166
167 if (use)
168 x25_route_hold(use);
169
170 read_unlock_bh(&x25_route_list_lock);
171 return use;
172}
173
174/*
175 * Handle the ioctls that control the routing functions.
176 */
177int x25_route_ioctl(unsigned int cmd, void __user *arg)
178{
179 struct x25_route_struct rt;
180 struct net_device *dev;
181 int rc = -EINVAL;
182
183 if (cmd != SIOCADDRT && cmd != SIOCDELRT)
184 goto out;
185
186 rc = -EFAULT;
187 if (copy_from_user(&rt, arg, sizeof(rt)))
188 goto out;
189
190 rc = -EINVAL;
191 if (rt.sigdigits < 0 || rt.sigdigits > 15)
192 goto out;
193
194 dev = x25_dev_get(rt.device);
195 if (!dev)
196 goto out;
197
198 if (cmd == SIOCADDRT)
199 rc = x25_add_route(&rt.address, rt.sigdigits, dev);
200 else
201 rc = x25_del_route(&rt.address, rt.sigdigits, dev);
202 dev_put(dev);
203out:
204 return rc;
205}
206
207/*
208 * Release all memory associated with X.25 routing structures.
209 */
210void __exit x25_route_free(void)
211{
212 struct x25_route *rt;
213 struct list_head *entry, *tmp;
214
215 write_lock_bh(&x25_route_list_lock);
216 list_for_each_safe(entry, tmp, &x25_route_list) {
217 rt = list_entry(entry, struct x25_route, node);
218 __x25_remove_route(rt);
219 }
220 write_unlock_bh(&x25_route_list_lock);
221}
diff --git a/net/x25/x25_subr.c b/net/x25/x25_subr.c
new file mode 100644
index 000000000000..183fea3bba67
--- /dev/null
+++ b/net/x25/x25_subr.c
@@ -0,0 +1,374 @@
1/*
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
6 * screw up. It might even work.
7 *
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 disconnection processing.
19 * mar/20/00 Daniela Squassoni Disabling/enabling of facilities
20 * negotiation.
21 * jun/24/01 Arnaldo C. Melo use skb_queue_purge, cleanups
22 */
23
24#include <linux/kernel.h>
25#include <linux/string.h>
26#include <linux/skbuff.h>
27#include <net/sock.h>
28#include <net/tcp.h>
29#include <net/x25.h>
30
31/*
32 * This routine purges all of the queues of frames.
33 */
34void x25_clear_queues(struct sock *sk)
35{
36 struct x25_sock *x25 = x25_sk(sk);
37
38 skb_queue_purge(&sk->sk_write_queue);
39 skb_queue_purge(&x25->ack_queue);
40 skb_queue_purge(&x25->interrupt_in_queue);
41 skb_queue_purge(&x25->interrupt_out_queue);
42 skb_queue_purge(&x25->fragment_queue);
43}
44
45
46/*
47 * This routine purges the input queue of those frames that have been
48 * acknowledged. This replaces the boxes labelled "V(a) <- N(r)" on the
49 * SDL diagram.
50*/
51void x25_frames_acked(struct sock *sk, unsigned short nr)
52{
53 struct sk_buff *skb;
54 struct x25_sock *x25 = x25_sk(sk);
55 int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
56
57 /*
58 * Remove all the ack-ed frames from the ack queue.
59 */
60 if (x25->va != nr)
61 while (skb_peek(&x25->ack_queue) && x25->va != nr) {
62 skb = skb_dequeue(&x25->ack_queue);
63 kfree_skb(skb);
64 x25->va = (x25->va + 1) % modulus;
65 }
66}
67
68void x25_requeue_frames(struct sock *sk)
69{
70 struct sk_buff *skb, *skb_prev = NULL;
71
72 /*
73 * Requeue all the un-ack-ed frames on the output queue to be picked
74 * up by x25_kick. This arrangement handles the possibility of an empty
75 * output queue.
76 */
77 while ((skb = skb_dequeue(&x25_sk(sk)->ack_queue)) != NULL) {
78 if (!skb_prev)
79 skb_queue_head(&sk->sk_write_queue, skb);
80 else
81 skb_append(skb_prev, skb);
82 skb_prev = skb;
83 }
84}
85
86/*
87 * Validate that the value of nr is between va and vs. Return true or
88 * false for testing.
89 */
90int x25_validate_nr(struct sock *sk, unsigned short nr)
91{
92 struct x25_sock *x25 = x25_sk(sk);
93 unsigned short vc = x25->va;
94 int modulus = x25->neighbour->extended ? X25_EMODULUS : X25_SMODULUS;
95
96 while (vc != x25->vs) {
97 if (nr == vc)
98 return 1;
99 vc = (vc + 1) % modulus;
100 }
101
102 return nr == x25->vs ? 1 : 0;
103}
104
105/*
106 * This routine is called when the packet layer internally generates a
107 * control frame.
108 */
109void x25_write_internal(struct sock *sk, int frametype)
110{
111 struct x25_sock *x25 = x25_sk(sk);
112 struct sk_buff *skb;
113 unsigned char *dptr;
114 unsigned char facilities[X25_MAX_FAC_LEN];
115 unsigned char addresses[1 + X25_ADDR_LEN];
116 unsigned char lci1, lci2;
117 /*
118 * Default safe frame size.
119 */
120 int len = X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
121
122 /*
123 * Adjust frame size.
124 */
125 switch (frametype) {
126 case X25_CALL_REQUEST:
127 len += 1 + X25_ADDR_LEN + X25_MAX_FAC_LEN +
128 X25_MAX_CUD_LEN;
129 break;
130 case X25_CALL_ACCEPTED:
131 len += 1 + X25_MAX_FAC_LEN + X25_MAX_CUD_LEN;
132 break;
133 case X25_CLEAR_REQUEST:
134 case X25_RESET_REQUEST:
135 len += 2;
136 break;
137 case X25_RR:
138 case X25_RNR:
139 case X25_REJ:
140 case X25_CLEAR_CONFIRMATION:
141 case X25_INTERRUPT_CONFIRMATION:
142 case X25_RESET_CONFIRMATION:
143 break;
144 default:
145 printk(KERN_ERR "X.25: invalid frame type %02X\n",
146 frametype);
147 return;
148 }
149
150 if ((skb = alloc_skb(len, GFP_ATOMIC)) == NULL)
151 return;
152
153 /*
154 * Space for Ethernet and 802.2 LLC headers.
155 */
156 skb_reserve(skb, X25_MAX_L2_LEN);
157
158 /*
159 * Make space for the GFI and LCI, and fill them in.
160 */
161 dptr = skb_put(skb, 2);
162
163 lci1 = (x25->lci >> 8) & 0x0F;
164 lci2 = (x25->lci >> 0) & 0xFF;
165
166 if (x25->neighbour->extended) {
167 *dptr++ = lci1 | X25_GFI_EXTSEQ;
168 *dptr++ = lci2;
169 } else {
170 *dptr++ = lci1 | X25_GFI_STDSEQ;
171 *dptr++ = lci2;
172 }
173
174 /*
175 * Now fill in the frame type specific information.
176 */
177 switch (frametype) {
178
179 case X25_CALL_REQUEST:
180 dptr = skb_put(skb, 1);
181 *dptr++ = X25_CALL_REQUEST;
182 len = x25_addr_aton(addresses, &x25->dest_addr,
183 &x25->source_addr);
184 dptr = skb_put(skb, len);
185 memcpy(dptr, addresses, len);
186 len = x25_create_facilities(facilities,
187 &x25->facilities,
188 x25->neighbour->global_facil_mask);
189 dptr = skb_put(skb, len);
190 memcpy(dptr, facilities, len);
191 dptr = skb_put(skb, x25->calluserdata.cudlength);
192 memcpy(dptr, x25->calluserdata.cuddata,
193 x25->calluserdata.cudlength);
194 x25->calluserdata.cudlength = 0;
195 break;
196
197 case X25_CALL_ACCEPTED:
198 dptr = skb_put(skb, 2);
199 *dptr++ = X25_CALL_ACCEPTED;
200 *dptr++ = 0x00; /* Address lengths */
201 len = x25_create_facilities(facilities,
202 &x25->facilities,
203 x25->vc_facil_mask);
204 dptr = skb_put(skb, len);
205 memcpy(dptr, facilities, len);
206 dptr = skb_put(skb, x25->calluserdata.cudlength);
207 memcpy(dptr, x25->calluserdata.cuddata,
208 x25->calluserdata.cudlength);
209 x25->calluserdata.cudlength = 0;
210 break;
211
212 case X25_CLEAR_REQUEST:
213 case X25_RESET_REQUEST:
214 dptr = skb_put(skb, 3);
215 *dptr++ = frametype;
216 *dptr++ = 0x00; /* XXX */
217 *dptr++ = 0x00; /* XXX */
218 break;
219
220 case X25_RR:
221 case X25_RNR:
222 case X25_REJ:
223 if (x25->neighbour->extended) {
224 dptr = skb_put(skb, 2);
225 *dptr++ = frametype;
226 *dptr++ = (x25->vr << 1) & 0xFE;
227 } else {
228 dptr = skb_put(skb, 1);
229 *dptr = frametype;
230 *dptr++ |= (x25->vr << 5) & 0xE0;
231 }
232 break;
233
234 case X25_CLEAR_CONFIRMATION:
235 case X25_INTERRUPT_CONFIRMATION:
236 case X25_RESET_CONFIRMATION:
237 dptr = skb_put(skb, 1);
238 *dptr = frametype;
239 break;
240 }
241
242 x25_transmit_link(skb, x25->neighbour);
243}
244
245/*
246 * Unpick the contents of the passed X.25 Packet Layer frame.
247 */
248int x25_decode(struct sock *sk, struct sk_buff *skb, int *ns, int *nr, int *q,
249 int *d, int *m)
250{
251 struct x25_sock *x25 = x25_sk(sk);
252 unsigned char *frame = skb->data;
253
254 *ns = *nr = *q = *d = *m = 0;
255
256 switch (frame[2]) {
257 case X25_CALL_REQUEST:
258 case X25_CALL_ACCEPTED:
259 case X25_CLEAR_REQUEST:
260 case X25_CLEAR_CONFIRMATION:
261 case X25_INTERRUPT:
262 case X25_INTERRUPT_CONFIRMATION:
263 case X25_RESET_REQUEST:
264 case X25_RESET_CONFIRMATION:
265 case X25_RESTART_REQUEST:
266 case X25_RESTART_CONFIRMATION:
267 case X25_REGISTRATION_REQUEST:
268 case X25_REGISTRATION_CONFIRMATION:
269 case X25_DIAGNOSTIC:
270 return frame[2];
271 }
272
273 if (x25->neighbour->extended) {
274 if (frame[2] == X25_RR ||
275 frame[2] == X25_RNR ||
276 frame[2] == X25_REJ) {
277 *nr = (frame[3] >> 1) & 0x7F;
278 return frame[2];
279 }
280 } else {
281 if ((frame[2] & 0x1F) == X25_RR ||
282 (frame[2] & 0x1F) == X25_RNR ||
283 (frame[2] & 0x1F) == X25_REJ) {
284 *nr = (frame[2] >> 5) & 0x07;
285 return frame[2] & 0x1F;
286 }
287 }
288
289 if (x25->neighbour->extended) {
290 if ((frame[2] & 0x01) == X25_DATA) {
291 *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
292 *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
293 *m = (frame[3] & X25_EXT_M_BIT) == X25_EXT_M_BIT;
294 *nr = (frame[3] >> 1) & 0x7F;
295 *ns = (frame[2] >> 1) & 0x7F;
296 return X25_DATA;
297 }
298 } else {
299 if ((frame[2] & 0x01) == X25_DATA) {
300 *q = (frame[0] & X25_Q_BIT) == X25_Q_BIT;
301 *d = (frame[0] & X25_D_BIT) == X25_D_BIT;
302 *m = (frame[2] & X25_STD_M_BIT) == X25_STD_M_BIT;
303 *nr = (frame[2] >> 5) & 0x07;
304 *ns = (frame[2] >> 1) & 0x07;
305 return X25_DATA;
306 }
307 }
308
309 printk(KERN_DEBUG "X.25: invalid PLP frame %02X %02X %02X\n",
310 frame[0], frame[1], frame[2]);
311
312 return X25_ILLEGAL;
313}
314
315void x25_disconnect(struct sock *sk, int reason, unsigned char cause,
316 unsigned char diagnostic)
317{
318 struct x25_sock *x25 = x25_sk(sk);
319
320 x25_clear_queues(sk);
321 x25_stop_timer(sk);
322
323 x25->lci = 0;
324 x25->state = X25_STATE_0;
325
326 x25->causediag.cause = cause;
327 x25->causediag.diagnostic = diagnostic;
328
329 sk->sk_state = TCP_CLOSE;
330 sk->sk_err = reason;
331 sk->sk_shutdown |= SEND_SHUTDOWN;
332
333 if (!sock_flag(sk, SOCK_DEAD)) {
334 sk->sk_state_change(sk);
335 sock_set_flag(sk, SOCK_DEAD);
336 }
337}
338
339/*
340 * Clear an own-rx-busy condition and tell the peer about this, provided
341 * that there is a significant amount of free receive buffer space available.
342 */
343void x25_check_rbuf(struct sock *sk)
344{
345 struct x25_sock *x25 = x25_sk(sk);
346
347 if (atomic_read(&sk->sk_rmem_alloc) < (sk->sk_rcvbuf / 2) &&
348 (x25->condition & X25_COND_OWN_RX_BUSY)) {
349 x25->condition &= ~X25_COND_OWN_RX_BUSY;
350 x25->condition &= ~X25_COND_ACK_PENDING;
351 x25->vl = x25->vr;
352 x25_write_internal(sk, X25_RR);
353 x25_stop_timer(sk);
354 }
355}
356
357/*
358 * Compare 2 calluserdata structures, used to find correct listening sockets
359 * when call user data is used.
360 */
361int x25_check_calluserdata(struct x25_calluserdata *ours, struct x25_calluserdata *theirs)
362{
363 int i;
364 if (ours->cudlength != theirs->cudlength)
365 return 0;
366
367 for (i=0;i<ours->cudlength;i++) {
368 if (ours->cuddata[i] != theirs->cuddata[i]) {
369 return 0;
370 }
371 }
372 return 1;
373}
374
diff --git a/net/x25/x25_timer.c b/net/x25/x25_timer.c
new file mode 100644
index 000000000000..d6a21a3ad80e
--- /dev/null
+++ b/net/x25/x25_timer.c
@@ -0,0 +1,176 @@
1/*
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
6 * screw up. It might even work.
7 *
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 New timer architecture.
19 * Centralised disconnection processing.
20 */
21
22#include <linux/errno.h>
23#include <linux/jiffies.h>
24#include <linux/timer.h>
25#include <net/sock.h>
26#include <net/tcp.h>
27#include <net/x25.h>
28
29static void x25_heartbeat_expiry(unsigned long);
30static void x25_timer_expiry(unsigned long);
31
32void x25_init_timers(struct sock *sk)
33{
34 struct x25_sock *x25 = x25_sk(sk);
35
36 init_timer(&x25->timer);
37 x25->timer.data = (unsigned long)sk;
38 x25->timer.function = &x25_timer_expiry;
39
40 /* initialized by sock_init_data */
41 sk->sk_timer.data = (unsigned long)sk;
42 sk->sk_timer.function = &x25_heartbeat_expiry;
43}
44
45void x25_start_heartbeat(struct sock *sk)
46{
47 mod_timer(&sk->sk_timer, jiffies + 5 * HZ);
48}
49
50void x25_stop_heartbeat(struct sock *sk)
51{
52 del_timer(&sk->sk_timer);
53}
54
55void x25_start_t2timer(struct sock *sk)
56{
57 struct x25_sock *x25 = x25_sk(sk);
58
59 mod_timer(&x25->timer, jiffies + x25->t2);
60}
61
62void x25_start_t21timer(struct sock *sk)
63{
64 struct x25_sock *x25 = x25_sk(sk);
65
66 mod_timer(&x25->timer, jiffies + x25->t21);
67}
68
69void x25_start_t22timer(struct sock *sk)
70{
71 struct x25_sock *x25 = x25_sk(sk);
72
73 mod_timer(&x25->timer, jiffies + x25->t22);
74}
75
76void x25_start_t23timer(struct sock *sk)
77{
78 struct x25_sock *x25 = x25_sk(sk);
79
80 mod_timer(&x25->timer, jiffies + x25->t23);
81}
82
83void x25_stop_timer(struct sock *sk)
84{
85 del_timer(&x25_sk(sk)->timer);
86}
87
88unsigned long x25_display_timer(struct sock *sk)
89{
90 struct x25_sock *x25 = x25_sk(sk);
91
92 if (!timer_pending(&x25->timer))
93 return 0;
94
95 return x25->timer.expires - jiffies;
96}
97
98static void x25_heartbeat_expiry(unsigned long param)
99{
100 struct sock *sk = (struct sock *)param;
101
102 bh_lock_sock(sk);
103 if (sock_owned_by_user(sk)) /* can currently only occur in state 3 */
104 goto restart_heartbeat;
105
106 switch (x25_sk(sk)->state) {
107
108 case X25_STATE_0:
109 /*
110 * Magic here: If we listen() and a new link dies
111 * before it is accepted() it isn't 'dead' so doesn't
112 * get removed.
113 */
114 if (sock_flag(sk, SOCK_DESTROY) ||
115 (sk->sk_state == TCP_LISTEN &&
116 sock_flag(sk, SOCK_DEAD))) {
117 x25_destroy_socket(sk);
118 goto unlock;
119 }
120 break;
121
122 case X25_STATE_3:
123 /*
124 * Check for the state of the receive buffer.
125 */
126 x25_check_rbuf(sk);
127 break;
128 }
129restart_heartbeat:
130 x25_start_heartbeat(sk);
131unlock:
132 bh_unlock_sock(sk);
133}
134
135/*
136 * Timer has expired, it may have been T2, T21, T22, or T23. We can tell
137 * by the state machine state.
138 */
139static inline void x25_do_timer_expiry(struct sock * sk)
140{
141 struct x25_sock *x25 = x25_sk(sk);
142
143 switch (x25->state) {
144
145 case X25_STATE_3: /* T2 */
146 if (x25->condition & X25_COND_ACK_PENDING) {
147 x25->condition &= ~X25_COND_ACK_PENDING;
148 x25_enquiry_response(sk);
149 }
150 break;
151
152 case X25_STATE_1: /* T21 */
153 case X25_STATE_4: /* T22 */
154 x25_write_internal(sk, X25_CLEAR_REQUEST);
155 x25->state = X25_STATE_2;
156 x25_start_t23timer(sk);
157 break;
158
159 case X25_STATE_2: /* T23 */
160 x25_disconnect(sk, ETIMEDOUT, 0, 0);
161 break;
162 }
163}
164
165static void x25_timer_expiry(unsigned long param)
166{
167 struct sock *sk = (struct sock *)param;
168
169 bh_lock_sock(sk);
170 if (sock_owned_by_user(sk)) { /* can currently only occur in state 3 */
171 if (x25_sk(sk)->state == X25_STATE_3)
172 x25_start_t2timer(sk);
173 } else
174 x25_do_timer_expiry(sk);
175 bh_unlock_sock(sk);
176}