Merge with Linux 2.5.73.
[linux-2.6/linux-mips.git] / net / x25 / af_x25.c
blob5d0a21917be424b13dfded0a27518030b411f95a
1 /*
2 * X.25 Packet Layer release 002
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.
8 * This code REQUIRES 2.1.15 or higher
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.
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
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <linux/types.h>
38 #include <linux/socket.h>
39 #include <linux/in.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/string.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/stat.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_arp.h>
50 #include <linux/skbuff.h>
51 #include <net/sock.h>
52 #include <net/tcp.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/fcntl.h>
56 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
57 #include <linux/mm.h>
58 #include <linux/interrupt.h>
59 #include <linux/notifier.h>
60 #include <linux/init.h>
61 #include <net/x25.h>
63 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
64 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
65 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
66 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
67 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
69 HLIST_HEAD(x25_list);
70 rwlock_t x25_list_lock = RW_LOCK_UNLOCKED;
72 static struct proto_ops x25_proto_ops;
74 static struct x25_address null_x25_address = {" "};
76 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
77 struct x25_address *calling_addr)
79 int called_len, calling_len;
80 char *called, *calling;
81 int i;
83 called_len = (*p >> 0) & 0x0F;
84 calling_len = (*p >> 4) & 0x0F;
86 called = called_addr->x25_addr;
87 calling = calling_addr->x25_addr;
88 p++;
90 for (i = 0; i < (called_len + calling_len); i++) {
91 if (i < called_len) {
92 if (i % 2 != 0) {
93 *called++ = ((*p >> 0) & 0x0F) + '0';
94 p++;
95 } else {
96 *called++ = ((*p >> 4) & 0x0F) + '0';
98 } else {
99 if (i % 2 != 0) {
100 *calling++ = ((*p >> 0) & 0x0F) + '0';
101 p++;
102 } else {
103 *calling++ = ((*p >> 4) & 0x0F) + '0';
108 *called = *calling = '\0';
110 return 1 + (called_len + calling_len + 1) / 2;
113 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
114 struct x25_address *calling_addr)
116 unsigned int called_len, calling_len;
117 char *called, *calling;
118 int i;
120 called = called_addr->x25_addr;
121 calling = calling_addr->x25_addr;
123 called_len = strlen(called);
124 calling_len = strlen(calling);
126 *p++ = (calling_len << 4) | (called_len << 0);
128 for (i = 0; i < (called_len + calling_len); i++) {
129 if (i < called_len) {
130 if (i % 2 != 0) {
131 *p |= (*called++ - '0') << 0;
132 p++;
133 } else {
134 *p = 0x00;
135 *p |= (*called++ - '0') << 4;
137 } else {
138 if (i % 2 != 0) {
139 *p |= (*calling++ - '0') << 0;
140 p++;
141 } else {
142 *p = 0x00;
143 *p |= (*calling++ - '0') << 4;
148 return 1 + (called_len + calling_len + 1) / 2;
152 * Socket removal during an interrupt is now safe.
154 static void x25_remove_socket(struct sock *sk)
156 write_lock_bh(&x25_list_lock);
157 sk_del_node_init(sk);
158 write_unlock_bh(&x25_list_lock);
162 * Kill all bound sockets on a dropped device.
164 static void x25_kill_by_device(struct net_device *dev)
166 struct sock *s;
167 struct hlist_node *node;
169 write_lock_bh(&x25_list_lock);
171 sk_for_each(s, node, &x25_list)
172 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
173 x25_disconnect(s, ENETUNREACH, 0, 0);
175 write_unlock_bh(&x25_list_lock);
179 * Handle device status changes.
181 static int x25_device_event(struct notifier_block *this, unsigned long event,
182 void *ptr)
184 struct net_device *dev = ptr;
185 struct x25_neigh *nb;
187 if (dev->type == ARPHRD_X25
188 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
189 || dev->type == ARPHRD_ETHER
190 #endif
192 switch (event) {
193 case NETDEV_UP:
194 x25_link_device_up(dev);
195 break;
196 case NETDEV_GOING_DOWN:
197 nb = x25_get_neigh(dev);
198 if (nb) {
199 x25_terminate_link(nb);
200 x25_neigh_put(nb);
202 break;
203 case NETDEV_DOWN:
204 x25_kill_by_device(dev);
205 x25_route_device_down(dev);
206 x25_link_device_down(dev);
207 break;
211 return NOTIFY_DONE;
215 * Add a socket to the bound sockets list.
217 static void x25_insert_socket(struct sock *sk)
219 write_lock_bh(&x25_list_lock);
220 sk_add_node(sk, &x25_list);
221 write_unlock_bh(&x25_list_lock);
225 * Find a socket that wants to accept the Call Request we just
226 * received.
228 static struct sock *x25_find_listener(struct x25_address *addr)
230 struct sock *s;
231 struct hlist_node *node;
233 read_lock_bh(&x25_list_lock);
235 sk_for_each(s, node, &x25_list)
236 if ((!strcmp(addr->x25_addr,
237 x25_sk(s)->source_addr.x25_addr) ||
238 !strcmp(addr->x25_addr,
239 null_x25_address.x25_addr)) &&
240 s->sk_state == TCP_LISTEN) {
241 sock_hold(s);
242 goto found;
244 s = NULL;
245 found:
246 read_unlock_bh(&x25_list_lock);
247 return s;
251 * Find a connected X.25 socket given my LCI and neighbour.
253 struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
255 struct sock *s;
256 struct hlist_node *node;
258 sk_for_each(s, node, &x25_list)
259 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
260 sock_hold(s);
261 goto found;
263 s = NULL;
264 found:
265 return s;
268 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
270 struct sock *s;
272 read_lock_bh(&x25_list_lock);
273 s = __x25_find_socket(lci, nb);
274 read_unlock_bh(&x25_list_lock);
275 return s;
279 * Find a unique LCI for a given device.
281 unsigned int x25_new_lci(struct x25_neigh *nb)
283 unsigned int lci = 1;
284 struct sock *sk;
286 read_lock_bh(&x25_list_lock);
288 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
289 sock_put(sk);
290 if (++lci == 4096) {
291 lci = 0;
292 break;
296 read_unlock_bh(&x25_list_lock);
297 return lci;
301 * Deferred destroy.
303 void x25_destroy_socket(struct sock *);
306 * handler for deferred kills.
308 static void x25_destroy_timer(unsigned long data)
310 x25_destroy_socket((struct sock *)data);
314 * This is called from user mode and the timers. Thus it protects itself
315 * against interrupt users but doesn't worry about being called during
316 * work. Once it is removed from the queue no interrupt or bottom half
317 * will touch it and we are (fairly 8-) ) safe.
318 * Not static as it's used by the timer
320 void x25_destroy_socket(struct sock *sk)
322 struct sk_buff *skb;
324 sock_hold(sk);
325 lock_sock(sk);
326 x25_stop_heartbeat(sk);
327 x25_stop_timer(sk);
329 x25_remove_socket(sk);
330 x25_clear_queues(sk); /* Flush the queues */
332 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
333 if (skb->sk != sk) { /* A pending connection */
335 * Queue the unaccepted socket for death
337 sock_set_flag(skb->sk, SOCK_DEAD);
338 x25_start_heartbeat(skb->sk);
339 x25_sk(skb->sk)->state = X25_STATE_0;
342 kfree_skb(skb);
345 if (atomic_read(&sk->sk_wmem_alloc) ||
346 atomic_read(&sk->sk_rmem_alloc)) {
347 /* Defer: outstanding buffers */
348 init_timer(&sk->sk_timer);
349 sk->sk_timer.expires = jiffies + 10 * HZ;
350 sk->sk_timer.function = x25_destroy_timer;
351 sk->sk_timer.data = (unsigned long)sk;
352 add_timer(&sk->sk_timer);
353 } else
354 sk_free(sk);
355 release_sock(sk);
356 sock_put(sk);
360 * Handling for system calls applied via the various interfaces to a
361 * X.25 socket object.
364 static int x25_setsockopt(struct socket *sock, int level, int optname,
365 char *optval, int optlen)
367 int opt;
368 struct sock *sk = sock->sk;
369 int rc = -ENOPROTOOPT;
371 if (level != SOL_X25 || optname != X25_QBITINCL)
372 goto out;
374 rc = -EINVAL;
375 if (optlen < sizeof(int))
376 goto out;
378 rc = -EFAULT;
379 if (get_user(opt, (int *)optval))
380 goto out;
382 x25_sk(sk)->qbitincl = !!opt;
383 rc = 0;
384 out:
385 return rc;
388 static int x25_getsockopt(struct socket *sock, int level, int optname,
389 char *optval, int *optlen)
391 struct sock *sk = sock->sk;
392 int val, len, rc = -ENOPROTOOPT;
394 if (level != SOL_X25 || optname != X25_QBITINCL)
395 goto out;
397 rc = -EFAULT;
398 if (get_user(len, optlen))
399 goto out;
401 len = min_t(unsigned int, len, sizeof(int));
403 rc = -EINVAL;
404 if (len < 0)
405 goto out;
407 rc = -EFAULT;
408 if (put_user(len, optlen))
409 goto out;
411 val = x25_sk(sk)->qbitincl;
412 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
413 out:
414 return rc;
417 static int x25_listen(struct socket *sock, int backlog)
419 struct sock *sk = sock->sk;
420 int rc = -EOPNOTSUPP;
422 if (sk->sk_state != TCP_LISTEN) {
423 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
424 sk->sk_max_ack_backlog = backlog;
425 sk->sk_state = TCP_LISTEN;
426 rc = 0;
429 return rc;
432 static struct sock *x25_alloc_socket(void)
434 struct x25_opt *x25;
435 struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, 1, NULL);
437 if (!sk)
438 goto out;
440 x25 = x25_sk(sk) = kmalloc(sizeof(*x25), GFP_ATOMIC);
441 if (!x25)
442 goto frees;
444 memset(x25, 0, sizeof(*x25));
446 x25->sk = sk;
448 sock_init_data(NULL, sk);
450 skb_queue_head_init(&x25->ack_queue);
451 skb_queue_head_init(&x25->fragment_queue);
452 skb_queue_head_init(&x25->interrupt_in_queue);
453 skb_queue_head_init(&x25->interrupt_out_queue);
454 out:
455 return sk;
456 frees:
457 sk_free(sk);
458 sk = NULL;
459 goto out;
462 static int x25_create(struct socket *sock, int protocol)
464 struct sock *sk;
465 struct x25_opt *x25;
466 int rc = -ESOCKTNOSUPPORT;
468 if (sock->type != SOCK_SEQPACKET || protocol)
469 goto out;
471 rc = -ENOMEM;
472 if ((sk = x25_alloc_socket()) == NULL)
473 goto out;
475 x25 = x25_sk(sk);
477 sock_init_data(sock, sk);
479 init_timer(&x25->timer);
481 sock->ops = &x25_proto_ops;
482 sk->sk_protocol = protocol;
483 sk->sk_backlog_rcv = x25_backlog_rcv;
485 x25->t21 = sysctl_x25_call_request_timeout;
486 x25->t22 = sysctl_x25_reset_request_timeout;
487 x25->t23 = sysctl_x25_clear_request_timeout;
488 x25->t2 = sysctl_x25_ack_holdback_timeout;
489 x25->state = X25_STATE_0;
491 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
492 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
493 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
494 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
495 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
496 x25->facilities.reverse = X25_DEFAULT_REVERSE;
497 rc = 0;
498 out:
499 return rc;
502 static struct sock *x25_make_new(struct sock *osk)
504 struct sock *sk = NULL;
505 struct x25_opt *x25, *ox25;
507 if (osk->sk_type != SOCK_SEQPACKET)
508 goto out;
510 if ((sk = x25_alloc_socket()) == NULL)
511 goto out;
513 x25 = x25_sk(sk);
515 sk->sk_type = osk->sk_type;
516 sk->sk_socket = osk->sk_socket;
517 sk->sk_priority = osk->sk_priority;
518 sk->sk_protocol = osk->sk_protocol;
519 sk->sk_rcvbuf = osk->sk_rcvbuf;
520 sk->sk_sndbuf = osk->sk_sndbuf;
521 sk->sk_debug = osk->sk_debug;
522 sk->sk_state = TCP_ESTABLISHED;
523 sk->sk_sleep = osk->sk_sleep;
524 sk->sk_zapped = osk->sk_zapped;
525 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
527 ox25 = x25_sk(osk);
528 x25->t21 = ox25->t21;
529 x25->t22 = ox25->t22;
530 x25->t23 = ox25->t23;
531 x25->t2 = ox25->t2;
532 x25->facilities = ox25->facilities;
533 x25->qbitincl = ox25->qbitincl;
535 init_timer(&x25->timer);
536 out:
537 return sk;
540 static int x25_release(struct socket *sock)
542 struct sock *sk = sock->sk;
543 struct x25_opt *x25;
545 if (!sk)
546 goto out;
548 x25 = x25_sk(sk);
550 switch (x25->state) {
552 case X25_STATE_0:
553 case X25_STATE_2:
554 x25_disconnect(sk, 0, 0, 0);
555 x25_destroy_socket(sk);
556 break;
558 case X25_STATE_1:
559 case X25_STATE_3:
560 case X25_STATE_4:
561 x25_clear_queues(sk);
562 x25_write_internal(sk, X25_CLEAR_REQUEST);
563 x25_start_t23timer(sk);
564 x25->state = X25_STATE_2;
565 sk->sk_state = TCP_CLOSE;
566 sk->sk_shutdown |= SEND_SHUTDOWN;
567 sk->sk_state_change(sk);
568 sock_set_flag(sk, SOCK_DEAD);
569 sock_set_flag(sk, SOCK_DESTROY);
570 break;
573 sock->sk = NULL;
574 sk->sk_socket = NULL; /* Not used, but we should do this */
575 out:
576 return 0;
579 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
581 struct sock *sk = sock->sk;
582 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
584 if (!sk->sk_zapped ||
585 addr_len != sizeof(struct sockaddr_x25) ||
586 addr->sx25_family != AF_X25)
587 return -EINVAL;
589 x25_sk(sk)->source_addr = addr->sx25_addr;
590 x25_insert_socket(sk);
591 sk->sk_zapped = 0;
592 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
594 return 0;
597 static int x25_wait_for_connection_establishment(struct sock *sk)
599 DECLARE_WAITQUEUE(wait, current);
600 int rc;
602 add_wait_queue_exclusive(sk->sk_sleep, &wait);
603 for (;;) {
604 __set_current_state(TASK_INTERRUPTIBLE);
605 rc = -ERESTARTSYS;
606 if (signal_pending(current))
607 break;
608 rc = sock_error(sk);
609 if (rc) {
610 sk->sk_socket->state = SS_UNCONNECTED;
611 break;
613 rc = 0;
614 if (sk->sk_state != TCP_ESTABLISHED) {
615 release_sock(sk);
616 schedule();
617 lock_sock(sk);
618 } else
619 break;
621 __set_current_state(TASK_RUNNING);
622 remove_wait_queue(sk->sk_sleep, &wait);
623 return rc;
626 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
627 int addr_len, int flags)
629 struct sock *sk = sock->sk;
630 struct x25_opt *x25 = x25_sk(sk);
631 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
632 struct x25_route *rt;
633 int rc = 0;
635 lock_sock(sk);
636 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
637 sock->state = SS_CONNECTED;
638 goto out; /* Connect completed during a ERESTARTSYS event */
641 rc = -ECONNREFUSED;
642 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
643 sock->state = SS_UNCONNECTED;
644 goto out;
647 rc = -EISCONN; /* No reconnect on a seqpacket socket */
648 if (sk->sk_state == TCP_ESTABLISHED)
649 goto out;
651 sk->sk_state = TCP_CLOSE;
652 sock->state = SS_UNCONNECTED;
654 rc = -EINVAL;
655 if (addr_len != sizeof(struct sockaddr_x25) ||
656 addr->sx25_family != AF_X25)
657 goto out;
659 rc = -ENETUNREACH;
660 rt = x25_get_route(&addr->sx25_addr);
661 if (!rt)
662 goto out;
664 x25->neighbour = x25_get_neigh(rt->dev);
665 if (!x25->neighbour)
666 goto out_put_route;
668 x25_limit_facilities(&x25->facilities, x25->neighbour);
670 x25->lci = x25_new_lci(x25->neighbour);
671 if (!x25->lci)
672 goto out_put_neigh;
674 rc = -EINVAL;
675 if (sk->sk_zapped) /* Must bind first - autobinding does not work */
676 goto out_put_neigh;
678 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
679 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
681 x25->dest_addr = addr->sx25_addr;
683 /* Move to connecting socket, start sending Connect Requests */
684 sock->state = SS_CONNECTING;
685 sk->sk_state = TCP_SYN_SENT;
687 x25->state = X25_STATE_1;
689 x25_write_internal(sk, X25_CALL_REQUEST);
691 x25_start_heartbeat(sk);
692 x25_start_t21timer(sk);
694 /* Now the loop */
695 rc = -EINPROGRESS;
696 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
697 goto out_put_neigh;
699 rc = x25_wait_for_connection_establishment(sk);
700 if (rc)
701 goto out_put_neigh;
703 sock->state = SS_CONNECTED;
704 rc = 0;
705 out_put_neigh:
706 if (rc)
707 x25_neigh_put(x25->neighbour);
708 out_put_route:
709 x25_route_put(rt);
710 out:
711 release_sock(sk);
712 return rc;
715 static int x25_wait_for_data(struct sock *sk, int timeout)
717 DECLARE_WAITQUEUE(wait, current);
718 int rc = 0;
720 add_wait_queue_exclusive(sk->sk_sleep, &wait);
721 for (;;) {
722 __set_current_state(TASK_INTERRUPTIBLE);
723 if (sk->sk_shutdown & RCV_SHUTDOWN)
724 break;
725 rc = -ERESTARTSYS;
726 if (signal_pending(current))
727 break;
728 rc = -EAGAIN;
729 if (!timeout)
730 break;
731 rc = 0;
732 if (skb_queue_empty(&sk->sk_receive_queue)) {
733 release_sock(sk);
734 timeout = schedule_timeout(timeout);
735 lock_sock(sk);
736 } else
737 break;
739 __set_current_state(TASK_RUNNING);
740 remove_wait_queue(sk->sk_sleep, &wait);
741 return rc;
744 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
746 struct sock *sk = sock->sk;
747 struct sock *newsk;
748 struct sk_buff *skb;
749 int rc = -EINVAL;
751 if (!sk || sk->sk_state != TCP_LISTEN)
752 goto out;
754 rc = -EOPNOTSUPP;
755 if (sk->sk_type != SOCK_SEQPACKET)
756 goto out;
758 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
759 if (rc)
760 goto out;
761 skb = skb_dequeue(&sk->sk_receive_queue);
762 rc = -EINVAL;
763 if (!skb->sk)
764 goto out;
765 newsk = skb->sk;
766 newsk->sk_pair = NULL;
767 newsk->sk_socket = newsock;
768 newsk->sk_sleep = &newsock->wait;
770 /* Now attach up the new socket */
771 skb->sk = NULL;
772 kfree_skb(skb);
773 sk->sk_ack_backlog--;
774 newsock->sk = newsk;
775 newsock->state = SS_CONNECTED;
776 rc = 0;
777 out:
778 return rc;
781 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
782 int *uaddr_len, int peer)
784 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
785 struct sock *sk = sock->sk;
786 struct x25_opt *x25 = x25_sk(sk);
788 if (peer) {
789 if (sk->sk_state != TCP_ESTABLISHED)
790 return -ENOTCONN;
791 sx25->sx25_addr = x25->dest_addr;
792 } else
793 sx25->sx25_addr = x25->source_addr;
795 sx25->sx25_family = AF_X25;
796 *uaddr_len = sizeof(*sx25);
798 return 0;
801 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
802 unsigned int lci)
804 struct sock *sk;
805 struct sock *make;
806 struct x25_opt *makex25;
807 struct x25_address source_addr, dest_addr;
808 struct x25_facilities facilities;
809 int len, rc;
812 * Remove the LCI and frame type.
814 skb_pull(skb, X25_STD_MIN_LEN);
817 * Extract the X.25 addresses and convert them to ASCII strings,
818 * and remove them.
820 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
823 * Find a listener for the particular address.
825 sk = x25_find_listener(&source_addr);
828 * We can't accept the Call Request.
830 if (!sk || sk->sk_ack_backlog == sk->sk_max_ack_backlog)
831 goto out_clear_request;
834 * Try to reach a compromise on the requested facilities.
836 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
837 goto out_sock_put;
840 * current neighbour/link might impose additional limits
841 * on certain facilties
844 x25_limit_facilities(&facilities, nb);
847 * Try to create a new socket.
849 make = x25_make_new(sk);
850 if (!make)
851 goto out_sock_put;
854 * Remove the facilities, leaving any Call User Data.
856 skb_pull(skb, len);
858 skb->sk = make;
859 make->sk_state = TCP_ESTABLISHED;
861 makex25 = x25_sk(make);
862 makex25->lci = lci;
863 makex25->dest_addr = dest_addr;
864 makex25->source_addr = source_addr;
865 makex25->neighbour = nb;
866 makex25->facilities = facilities;
867 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
869 x25_write_internal(make, X25_CALL_ACCEPTED);
872 * Incoming Call User Data.
874 if (skb->len >= 0) {
875 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
876 makex25->calluserdata.cudlength = skb->len;
879 makex25->state = X25_STATE_3;
881 sk->sk_ack_backlog++;
882 make->sk_pair = sk;
884 x25_insert_socket(make);
886 skb_queue_head(&sk->sk_receive_queue, skb);
888 x25_start_heartbeat(make);
890 if (!sock_flag(sk, SOCK_DEAD))
891 sk->sk_data_ready(sk, skb->len);
892 rc = 1;
893 sock_put(sk);
894 out:
895 return rc;
896 out_sock_put:
897 sock_put(sk);
898 out_clear_request:
899 rc = 0;
900 x25_transmit_clear_request(nb, lci, 0x01);
901 goto out;
904 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
905 struct msghdr *msg, int len)
907 struct sock *sk = sock->sk;
908 struct x25_opt *x25 = x25_sk(sk);
909 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
910 struct sockaddr_x25 sx25;
911 struct sk_buff *skb;
912 unsigned char *asmptr;
913 int noblock = msg->msg_flags & MSG_DONTWAIT;
914 int size, qbit = 0, rc = -EINVAL;
916 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_OOB | MSG_EOR))
917 goto out;
919 /* we currently don't support segmented records at the user interface */
920 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
921 goto out;
923 rc = -EADDRNOTAVAIL;
924 if (sk->sk_zapped)
925 goto out;
927 rc = -EPIPE;
928 if (sk->sk_shutdown & SEND_SHUTDOWN) {
929 send_sig(SIGPIPE, current, 0);
930 goto out;
933 rc = -ENETUNREACH;
934 if (!x25->neighbour)
935 goto out;
937 if (usx25) {
938 rc = -EINVAL;
939 if (msg->msg_namelen < sizeof(sx25))
940 goto out;
941 memcpy(&sx25, usx25, sizeof(sx25));
942 rc = -EISCONN;
943 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
944 goto out;
945 rc = -EINVAL;
946 if (sx25.sx25_family != AF_X25)
947 goto out;
948 } else {
950 * FIXME 1003.1g - if the socket is like this because
951 * it has become closed (not started closed) we ought
952 * to SIGPIPE, EPIPE;
954 rc = -ENOTCONN;
955 if (sk->sk_state != TCP_ESTABLISHED)
956 goto out;
958 sx25.sx25_family = AF_X25;
959 sx25.sx25_addr = x25->dest_addr;
962 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
964 /* Build a packet */
965 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
967 if ((msg->msg_flags & MSG_OOB) && len > 32)
968 len = 32;
970 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
972 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
973 if (!skb)
974 goto out;
975 X25_SKB_CB(skb)->flags = msg->msg_flags;
977 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
980 * Put the data on the end
982 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
984 asmptr = skb->h.raw = skb_put(skb, len);
986 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
987 if (rc)
988 goto out_kfree_skb;
991 * If the Q BIT Include socket option is in force, the first
992 * byte of the user data is the logical value of the Q Bit.
994 if (x25->qbitincl) {
995 qbit = skb->data[0];
996 skb_pull(skb, 1);
1000 * Push down the X.25 header
1002 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1004 if (msg->msg_flags & MSG_OOB) {
1005 if (x25->neighbour->extended) {
1006 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1007 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1008 *asmptr++ = (x25->lci >> 0) & 0xFF;
1009 *asmptr++ = X25_INTERRUPT;
1010 } else {
1011 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1012 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1013 *asmptr++ = (x25->lci >> 0) & 0xFF;
1014 *asmptr++ = X25_INTERRUPT;
1016 } else {
1017 if (x25->neighbour->extended) {
1018 /* Build an Extended X.25 header */
1019 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1020 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1021 *asmptr++ = (x25->lci >> 0) & 0xFF;
1022 *asmptr++ = X25_DATA;
1023 *asmptr++ = X25_DATA;
1024 } else {
1025 /* Build an Standard X.25 header */
1026 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1027 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1028 *asmptr++ = (x25->lci >> 0) & 0xFF;
1029 *asmptr++ = X25_DATA;
1032 if (qbit)
1033 skb->data[0] |= X25_Q_BIT;
1036 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1037 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1039 rc = -ENOTCONN;
1040 if (sk->sk_state != TCP_ESTABLISHED)
1041 goto out_kfree_skb;
1043 if (msg->msg_flags & MSG_OOB)
1044 skb_queue_tail(&x25->interrupt_out_queue, skb);
1045 else {
1046 len = x25_output(sk, skb);
1047 if (len < 0)
1048 kfree_skb(skb);
1049 else if (x25->qbitincl)
1050 len++;
1054 * lock_sock() is currently only used to serialize this x25_kick()
1055 * against input-driven x25_kick() calls. It currently only blocks
1056 * incoming packets for this socket and does not protect against
1057 * any other socket state changes and is not called from anywhere
1058 * else. As x25_kick() cannot block and as long as all socket
1059 * operations are BKL-wrapped, we don't need take to care about
1060 * purging the backlog queue in x25_release().
1062 * Using lock_sock() to protect all socket operations entirely
1063 * (and making the whole x25 stack SMP aware) unfortunately would
1064 * require major changes to {send,recv}msg and skb allocation methods.
1065 * -> 2.5 ;)
1067 lock_sock(sk);
1068 x25_kick(sk);
1069 release_sock(sk);
1070 rc = len;
1071 out:
1072 return rc;
1073 out_kfree_skb:
1074 kfree_skb(skb);
1075 goto out;
1079 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1080 struct msghdr *msg, int size,
1081 int flags)
1083 struct sock *sk = sock->sk;
1084 struct x25_opt *x25 = x25_sk(sk);
1085 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1086 int copied, qbit;
1087 struct sk_buff *skb;
1088 unsigned char *asmptr;
1089 int rc = -ENOTCONN;
1092 * This works for seqpacket too. The receiver has ordered the queue for
1093 * us! We do one quick check first though
1095 if (sk->sk_state != TCP_ESTABLISHED)
1096 goto out;
1098 if (flags & MSG_OOB) {
1099 rc = -EINVAL;
1100 if (sock_flag(sk, SOCK_URGINLINE) ||
1101 !skb_peek(&x25->interrupt_in_queue))
1102 goto out;
1104 skb = skb_dequeue(&x25->interrupt_in_queue);
1106 skb_pull(skb, X25_STD_MIN_LEN);
1109 * No Q bit information on Interrupt data.
1111 if (x25->qbitincl) {
1112 asmptr = skb_push(skb, 1);
1113 *asmptr = 0x00;
1116 msg->msg_flags |= MSG_OOB;
1117 } else {
1118 /* Now we can treat all alike */
1119 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1120 flags & MSG_DONTWAIT, &rc);
1121 if (!skb)
1122 goto out;
1124 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1126 skb_pull(skb, x25->neighbour->extended ?
1127 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1129 if (x25->qbitincl) {
1130 asmptr = skb_push(skb, 1);
1131 *asmptr = qbit;
1135 skb->h.raw = skb->data;
1137 copied = skb->len;
1139 if (copied > size) {
1140 copied = size;
1141 msg->msg_flags |= MSG_TRUNC;
1144 /* Currently, each datagram always contains a complete record */
1145 msg->msg_flags |= MSG_EOR;
1147 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1148 if (rc)
1149 goto out_free_dgram;
1151 if (sx25) {
1152 sx25->sx25_family = AF_X25;
1153 sx25->sx25_addr = x25->dest_addr;
1156 msg->msg_namelen = sizeof(struct sockaddr_x25);
1158 lock_sock(sk);
1159 x25_check_rbuf(sk);
1160 release_sock(sk);
1161 rc = copied;
1162 out_free_dgram:
1163 skb_free_datagram(sk, skb);
1164 out:
1165 return rc;
1169 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1171 struct sock *sk = sock->sk;
1172 struct x25_opt *x25 = x25_sk(sk);
1173 int rc;
1175 switch (cmd) {
1176 case TIOCOUTQ: {
1177 int amount = sk->sk_sndbuf -
1178 atomic_read(&sk->sk_wmem_alloc);
1179 if (amount < 0)
1180 amount = 0;
1181 rc = put_user(amount, (unsigned int *)arg);
1182 break;
1185 case TIOCINQ: {
1186 struct sk_buff *skb;
1187 int amount = 0;
1189 * These two are safe on a single CPU system as
1190 * only user tasks fiddle here
1192 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1193 amount = skb->len;
1194 rc = put_user(amount, (unsigned int *)arg);
1195 break;
1198 case SIOCGSTAMP:
1199 if (sk) {
1200 rc = -ENOENT;
1201 if (!sk->sk_stamp.tv_sec)
1202 break;
1203 rc = copy_to_user((void *)arg, &sk->sk_stamp,
1204 sizeof(struct timeval)) ? -EFAULT : 0;
1206 rc = -EINVAL;
1207 break;
1208 case SIOCGIFADDR:
1209 case SIOCSIFADDR:
1210 case SIOCGIFDSTADDR:
1211 case SIOCSIFDSTADDR:
1212 case SIOCGIFBRDADDR:
1213 case SIOCSIFBRDADDR:
1214 case SIOCGIFNETMASK:
1215 case SIOCSIFNETMASK:
1216 case SIOCGIFMETRIC:
1217 case SIOCSIFMETRIC:
1218 rc = -EINVAL;
1219 break;
1220 case SIOCADDRT:
1221 case SIOCDELRT:
1222 rc = -EPERM;
1223 if (!capable(CAP_NET_ADMIN))
1224 break;
1225 rc = x25_route_ioctl(cmd, (void *)arg);
1226 break;
1227 case SIOCX25GSUBSCRIP:
1228 rc = x25_subscr_ioctl(cmd, (void *)arg);
1229 break;
1230 case SIOCX25SSUBSCRIP:
1231 rc = -EPERM;
1232 if (!capable(CAP_NET_ADMIN))
1233 break;
1234 rc = x25_subscr_ioctl(cmd, (void *)arg);
1235 break;
1236 case SIOCX25GFACILITIES: {
1237 struct x25_facilities fac = x25->facilities;
1238 rc = copy_to_user((void *)arg, &fac,
1239 sizeof(fac)) ? -EFAULT : 0;
1240 break;
1243 case SIOCX25SFACILITIES: {
1244 struct x25_facilities facilities;
1245 rc = -EFAULT;
1246 if (copy_from_user(&facilities, (void *)arg,
1247 sizeof(facilities)))
1248 break;
1249 rc = -EINVAL;
1250 if (sk->sk_state != TCP_LISTEN &&
1251 sk->sk_state != TCP_CLOSE)
1252 break;
1253 if (facilities.pacsize_in < X25_PS16 ||
1254 facilities.pacsize_in > X25_PS4096)
1255 break;
1256 if (facilities.pacsize_out < X25_PS16 ||
1257 facilities.pacsize_out > X25_PS4096)
1258 break;
1259 if (facilities.winsize_in < 1 ||
1260 facilities.winsize_in > 127)
1261 break;
1262 if (facilities.throughput < 0x03 ||
1263 facilities.throughput > 0xDD)
1264 break;
1265 if (facilities.reverse && facilities.reverse != 1)
1266 break;
1267 x25->facilities = facilities;
1268 rc = 0;
1269 break;
1272 case SIOCX25GCALLUSERDATA: {
1273 struct x25_calluserdata cud = x25->calluserdata;
1274 rc = copy_to_user((void *)arg, &cud,
1275 sizeof(cud)) ? -EFAULT : 0;
1276 break;
1279 case SIOCX25SCALLUSERDATA: {
1280 struct x25_calluserdata calluserdata;
1282 rc = -EFAULT;
1283 if (copy_from_user(&calluserdata, (void *)arg,
1284 sizeof(calluserdata)))
1285 break;
1286 rc = -EINVAL;
1287 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1288 break;
1289 x25->calluserdata = calluserdata;
1290 rc = 0;
1291 break;
1294 case SIOCX25GCAUSEDIAG: {
1295 struct x25_causediag causediag;
1296 causediag = x25->causediag;
1297 rc = copy_to_user((void *)arg, &causediag,
1298 sizeof(causediag)) ? -EFAULT : 0;
1299 break;
1302 default:
1303 rc = dev_ioctl(cmd, (void *)arg);
1304 break;
1307 return rc;
1310 struct net_proto_family x25_family_ops = {
1311 .family = AF_X25,
1312 .create = x25_create,
1313 .owner = THIS_MODULE,
1316 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1317 .family = AF_X25,
1318 .owner = THIS_MODULE,
1319 .release = x25_release,
1320 .bind = x25_bind,
1321 .connect = x25_connect,
1322 .socketpair = sock_no_socketpair,
1323 .accept = x25_accept,
1324 .getname = x25_getname,
1325 .poll = datagram_poll,
1326 .ioctl = x25_ioctl,
1327 .listen = x25_listen,
1328 .shutdown = sock_no_shutdown,
1329 .setsockopt = x25_setsockopt,
1330 .getsockopt = x25_getsockopt,
1331 .sendmsg = x25_sendmsg,
1332 .recvmsg = x25_recvmsg,
1333 .mmap = sock_no_mmap,
1334 .sendpage = sock_no_sendpage,
1337 #include <linux/smp_lock.h>
1338 SOCKOPS_WRAP(x25_proto, AF_X25);
1340 static struct packet_type x25_packet_type = {
1341 .type = __constant_htons(ETH_P_X25),
1342 .func = x25_lapb_receive_frame,
1345 struct notifier_block x25_dev_notifier = {
1346 .notifier_call = x25_device_event,
1349 void x25_kill_by_neigh(struct x25_neigh *nb)
1351 struct sock *s;
1352 struct hlist_node *node;
1354 write_lock_bh(&x25_list_lock);
1356 sk_for_each(s, node, &x25_list)
1357 if (x25_sk(s)->neighbour == nb)
1358 x25_disconnect(s, ENETUNREACH, 0, 0);
1360 write_unlock_bh(&x25_list_lock);
1363 static int __init x25_init(void)
1365 #ifdef MODULE
1366 struct net_device *dev;
1367 #endif /* MODULE */
1368 sock_register(&x25_family_ops);
1370 dev_add_pack(&x25_packet_type);
1372 register_netdevice_notifier(&x25_dev_notifier);
1374 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1376 #ifdef CONFIG_SYSCTL
1377 x25_register_sysctl();
1378 #endif
1380 x25_proc_init();
1381 #ifdef MODULE
1383 * Register any pre existing devices.
1385 read_lock(&dev_base_lock);
1386 for (dev = dev_base; dev; dev = dev->next) {
1387 if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
1388 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1389 || dev->type == ARPHRD_ETHER
1390 #endif
1392 x25_link_device_up(dev);
1394 read_unlock(&dev_base_lock);
1395 #endif /* MODULE */
1396 return 0;
1398 module_init(x25_init);
1400 static void __exit x25_exit(void)
1402 x25_proc_exit();
1403 x25_link_free();
1404 x25_route_free();
1406 #ifdef CONFIG_SYSCTL
1407 x25_unregister_sysctl();
1408 #endif
1410 unregister_netdevice_notifier(&x25_dev_notifier);
1412 dev_remove_pack(&x25_packet_type);
1414 sock_unregister(AF_X25);
1416 module_exit(x25_exit);
1418 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1419 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1420 MODULE_LICENSE("GPL");