2 * X.25 Packet Layer release 002
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.
7 * This code REQUIRES 2.1.15 or higher
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.
16 * X.25 001 Jonathan Naylor Started coding.
17 * X.25 002 Jonathan Naylor Centralised disconnect handling.
18 * New timer architecture.
19 * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
20 * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
21 * facilities negotiation and increased
22 * the throughput upper limit.
23 * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
24 * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
25 * Fixed x25_output() related skb leakage.
26 * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
27 * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
28 * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/errno.h>
34 #include <linux/types.h>
35 #include <linux/socket.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/string.h>
41 #include <linux/sockios.h>
42 #include <linux/net.h>
43 #include <linux/stat.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/if_arp.h>
47 #include <linux/skbuff.h>
49 #include <asm/segment.h>
50 #include <asm/system.h>
51 #include <asm/uaccess.h>
52 #include <linux/fcntl.h>
53 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
55 #include <linux/interrupt.h>
56 #include <linux/notifier.h>
57 #include <linux/proc_fs.h>
58 #include <linux/init.h>
61 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
62 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
63 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
64 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
65 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
67 static struct sock *volatile x25_list /* = NULL initially */;
69 static struct proto_ops x25_proto_ops;
71 static x25_address null_x25_address = {" "};
73 int x25_addr_ntoa(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
75 int called_len, calling_len;
76 char *called, *calling;
79 called_len = (*p >> 0) & 0x0F;
80 calling_len = (*p >> 4) & 0x0F;
82 called = called_addr->x25_addr;
83 calling = calling_addr->x25_addr;
86 for (i = 0; i < (called_len + calling_len); i++) {
89 *called++ = ((*p >> 0) & 0x0F) + '0';
92 *called++ = ((*p >> 4) & 0x0F) + '0';
96 *calling++ = ((*p >> 0) & 0x0F) + '0';
99 *calling++ = ((*p >> 4) & 0x0F) + '0';
107 return 1 + (called_len + calling_len + 1) / 2;
110 int x25_addr_aton(unsigned char *p, x25_address *called_addr, x25_address *calling_addr)
112 unsigned int called_len, calling_len;
113 char *called, *calling;
116 called = called_addr->x25_addr;
117 calling = calling_addr->x25_addr;
119 called_len = strlen(called);
120 calling_len = strlen(calling);
122 *p++ = (calling_len << 4) | (called_len << 0);
124 for (i = 0; i < (called_len + calling_len); i++) {
125 if (i < called_len) {
127 *p |= (*called++ - '0') << 0;
131 *p |= (*called++ - '0') << 4;
135 *p |= (*calling++ - '0') << 0;
139 *p |= (*calling++ - '0') << 4;
144 return 1 + (called_len + calling_len + 1) / 2;
148 * Socket removal during an interrupt is now safe.
150 static void x25_remove_socket(struct sock *sk)
158 if ((s = x25_list) == sk) {
160 restore_flags(flags);
164 while (s != NULL && s->next != NULL) {
167 restore_flags(flags);
174 restore_flags(flags);
178 * Kill all bound sockets on a dropped device.
180 static void x25_kill_by_device(struct net_device *dev)
184 for (s = x25_list; s != NULL; s = s->next)
185 if (s->protinfo.x25->neighbour &&
186 s->protinfo.x25->neighbour->dev == dev)
187 x25_disconnect(s, ENETUNREACH, 0, 0);
191 * Handle device status changes.
193 static int x25_device_event(struct notifier_block *this, unsigned long event, void *ptr)
195 struct net_device *dev = (struct net_device *)ptr;
196 struct x25_neigh *neigh;
198 if (dev->type == ARPHRD_X25
199 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
200 || dev->type == ARPHRD_ETHER
205 x25_link_device_up(dev);
207 case NETDEV_GOING_DOWN:
208 if ((neigh = x25_get_neigh(dev)))
209 x25_terminate_link(neigh);
212 x25_kill_by_device(dev);
213 x25_route_device_down(dev);
214 x25_link_device_down(dev);
223 * Add a socket to the bound sockets list.
225 static void x25_insert_socket(struct sock *sk)
235 restore_flags(flags);
239 * Find a socket that wants to accept the Call Request we just
242 static struct sock *x25_find_listener(x25_address *addr)
250 for (s = x25_list; s != NULL; s = s->next) {
251 if ((strcmp(addr->x25_addr, s->protinfo.x25->source_addr.x25_addr) == 0 ||
252 strcmp(addr->x25_addr, null_x25_address.x25_addr) == 0) &&
253 s->state == TCP_LISTEN) {
254 restore_flags(flags);
259 restore_flags(flags);
264 * Find a connected X.25 socket given my LCI and neighbour.
266 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *neigh)
274 for (s = x25_list; s != NULL; s = s->next) {
275 if (s->protinfo.x25->lci == lci && s->protinfo.x25->neighbour == neigh) {
276 restore_flags(flags);
281 restore_flags(flags);
286 * Find a unique LCI for a given device.
288 unsigned int x25_new_lci(struct x25_neigh *neigh)
290 unsigned int lci = 1;
292 while (x25_find_socket(lci, neigh) != NULL) {
294 if (lci == 4096) return 0;
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 against
315 * interrupt users but doesn't worry about being called during work.
316 * Once it is removed from the queue no interrupt or bottom half will
317 * touch it and we are (fairly 8-) ) safe.
319 void x25_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */
327 x25_stop_heartbeat(sk);
330 x25_remove_socket(sk);
331 x25_clear_queues(sk); /* Flush the queues */
333 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
334 if (skb->sk != sk) { /* A pending connection */
335 skb->sk->dead = 1; /* Queue the unaccepted socket for death */
336 x25_start_heartbeat(skb->sk);
337 skb->sk->protinfo.x25->state = X25_STATE_0;
343 if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
344 /* Defer: outstanding buffers */
345 init_timer(&sk->timer);
346 sk->timer.expires = jiffies + 10 * HZ;
347 sk->timer.function = x25_destroy_timer;
348 sk->timer.data = (unsigned long)sk;
349 add_timer(&sk->timer);
355 restore_flags(flags);
359 * Handling for system calls applied via the various interfaces to a
360 * X.25 socket object.
363 static int x25_setsockopt(struct socket *sock, int level, int optname,
364 char *optval, int optlen)
366 struct sock *sk = sock->sk;
369 if (level != SOL_X25)
372 if (optlen < sizeof(int))
375 if (get_user(opt, (int *)optval))
380 sk->protinfo.x25->qbitincl = opt ? 1 : 0;
388 static int x25_getsockopt(struct socket *sock, int level, int optname,
389 char *optval, int *optlen)
391 struct sock *sk = sock->sk;
395 if (level != SOL_X25)
398 if (get_user(len, optlen))
403 val = sk->protinfo.x25->qbitincl;
410 len = min_t(unsigned int, len, sizeof(int));
415 if (put_user(len, optlen))
418 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
421 static int x25_listen(struct socket *sock, int backlog)
423 struct sock *sk = sock->sk;
425 if (sk->state != TCP_LISTEN) {
426 memset(&sk->protinfo.x25->dest_addr, '\0', X25_ADDR_LEN);
427 sk->max_ack_backlog = backlog;
428 sk->state = TCP_LISTEN;
435 static struct sock *x25_alloc_socket(void)
440 if ((sk = sk_alloc(AF_X25, GFP_ATOMIC, 1)) == NULL)
443 if ((x25 = kmalloc(sizeof(*x25), GFP_ATOMIC)) == NULL) {
448 memset(x25, 0x00, sizeof(*x25));
451 sk->protinfo.x25 = x25;
455 sock_init_data(NULL, sk);
457 skb_queue_head_init(&x25->ack_queue);
458 skb_queue_head_init(&x25->fragment_queue);
459 skb_queue_head_init(&x25->interrupt_in_queue);
460 skb_queue_head_init(&x25->interrupt_out_queue);
465 static int x25_create(struct socket *sock, int protocol)
470 if (sock->type != SOCK_SEQPACKET || protocol != 0)
471 return -ESOCKTNOSUPPORT;
473 if ((sk = x25_alloc_socket()) == NULL)
476 x25 = sk->protinfo.x25;
478 sock_init_data(sock, sk);
480 init_timer(&x25->timer);
482 sock->ops = &x25_proto_ops;
483 sk->protocol = protocol;
484 sk->backlog_rcv = x25_backlog_rcv;
486 x25->t21 = sysctl_x25_call_request_timeout;
487 x25->t22 = sysctl_x25_reset_request_timeout;
488 x25->t23 = sysctl_x25_clear_request_timeout;
489 x25->t2 = sysctl_x25_ack_holdback_timeout;
490 x25->state = X25_STATE_0;
492 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
493 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
494 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
495 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
496 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
497 x25->facilities.reverse = X25_DEFAULT_REVERSE;
502 static struct sock *x25_make_new(struct sock *osk)
507 if (osk->type != SOCK_SEQPACKET)
510 if ((sk = x25_alloc_socket()) == NULL)
513 x25 = sk->protinfo.x25;
515 sk->type = osk->type;
516 sk->socket = osk->socket;
517 sk->priority = osk->priority;
518 sk->protocol = osk->protocol;
519 sk->rcvbuf = osk->rcvbuf;
520 sk->sndbuf = osk->sndbuf;
521 sk->debug = osk->debug;
522 sk->state = TCP_ESTABLISHED;
523 sk->sleep = osk->sleep;
524 sk->zapped = osk->zapped;
525 sk->backlog_rcv = osk->backlog_rcv;
527 x25->t21 = osk->protinfo.x25->t21;
528 x25->t22 = osk->protinfo.x25->t22;
529 x25->t23 = osk->protinfo.x25->t23;
530 x25->t2 = osk->protinfo.x25->t2;
532 x25->facilities = osk->protinfo.x25->facilities;
534 x25->qbitincl = osk->protinfo.x25->qbitincl;
536 init_timer(&x25->timer);
541 static int x25_release(struct socket *sock)
543 struct sock *sk = sock->sk;
545 if (sk == NULL) return 0;
547 switch (sk->protinfo.x25->state) {
551 x25_disconnect(sk, 0, 0, 0);
552 x25_destroy_socket(sk);
558 x25_clear_queues(sk);
559 x25_write_internal(sk, X25_CLEAR_REQUEST);
560 x25_start_t23timer(sk);
561 sk->protinfo.x25->state = X25_STATE_2;
562 sk->state = TCP_CLOSE;
563 sk->shutdown |= SEND_SHUTDOWN;
564 sk->state_change(sk);
574 sk->socket = NULL; /* Not used, but we should do this */
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;
587 if (addr_len != sizeof(struct sockaddr_x25))
590 if (addr->sx25_family != AF_X25)
593 sk->protinfo.x25->source_addr = addr->sx25_addr;
595 x25_insert_socket(sk);
599 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
604 static int x25_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
606 struct sock *sk = sock->sk;
607 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
608 struct net_device *dev;
610 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
611 sock->state = SS_CONNECTED;
612 return 0; /* Connect completed during a ERESTARTSYS event */
615 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
616 sock->state = SS_UNCONNECTED;
617 return -ECONNREFUSED;
620 if (sk->state == TCP_ESTABLISHED)
621 return -EISCONN; /* No reconnect on a seqpacket socket */
623 sk->state = TCP_CLOSE;
624 sock->state = SS_UNCONNECTED;
626 if (addr_len != sizeof(struct sockaddr_x25))
629 if (addr->sx25_family != AF_X25)
632 if ((dev = x25_get_route(&addr->sx25_addr)) == NULL)
635 if ((sk->protinfo.x25->neighbour = x25_get_neigh(dev)) == NULL)
638 x25_limit_facilities(&sk->protinfo.x25->facilities,
639 sk->protinfo.x25->neighbour);
641 if ((sk->protinfo.x25->lci = x25_new_lci(sk->protinfo.x25->neighbour)) == 0)
644 if (sk->zapped) /* Must bind first - autobinding does not work */
647 if (strcmp(sk->protinfo.x25->source_addr.x25_addr, null_x25_address.x25_addr) == 0)
648 memset(&sk->protinfo.x25->source_addr, '\0', X25_ADDR_LEN);
650 sk->protinfo.x25->dest_addr = addr->sx25_addr;
652 /* Move to connecting socket, start sending Connect Requests */
653 sock->state = SS_CONNECTING;
654 sk->state = TCP_SYN_SENT;
656 sk->protinfo.x25->state = X25_STATE_1;
658 x25_write_internal(sk, X25_CALL_REQUEST);
660 x25_start_heartbeat(sk);
661 x25_start_t21timer(sk);
664 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
667 cli(); /* To avoid races on the sleep */
670 * A Clear Request or timeout or failed routing will go to closed.
672 while (sk->state == TCP_SYN_SENT) {
673 interruptible_sleep_on(sk->sleep);
674 if (signal_pending(current)) {
680 if (sk->state != TCP_ESTABLISHED) {
682 sock->state = SS_UNCONNECTED;
683 return sock_error(sk); /* Always set at this point */
686 sock->state = SS_CONNECTED;
693 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
699 if ((sk = sock->sk) == NULL)
702 if (sk->type != SOCK_SEQPACKET)
705 if (sk->state != TCP_LISTEN)
709 * The write queue this time is holding sockets ready to use
710 * hooked into the CALL INDICATION we saved
714 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
715 if (flags & O_NONBLOCK) {
719 interruptible_sleep_on(sk->sleep);
720 if (signal_pending(current)) {
725 } while (skb == NULL);
729 newsk->socket = newsock;
730 newsk->sleep = &newsock->wait;
733 /* Now attach up the new socket */
738 newsock->state = SS_CONNECTED;
743 static int x25_getname(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer)
745 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
746 struct sock *sk = sock->sk;
749 if (sk->state != TCP_ESTABLISHED)
751 sx25->sx25_addr = sk->protinfo.x25->dest_addr;
753 sx25->sx25_addr = sk->protinfo.x25->source_addr;
756 sx25->sx25_family = AF_X25;
757 *uaddr_len = sizeof(struct sockaddr_x25);
762 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *neigh, unsigned int lci)
766 x25_address source_addr, dest_addr;
767 struct x25_facilities facilities;
771 * Remove the LCI and frame type.
773 skb_pull(skb, X25_STD_MIN_LEN);
776 * Extract the X.25 addresses and convert them to ASCII strings,
779 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
782 * Find a listener for the particular address.
784 sk = x25_find_listener(&source_addr);
787 * We can't accept the Call Request.
789 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog) {
790 x25_transmit_clear_request(neigh, lci, 0x01);
795 * Try to reach a compromise on the requested facilities.
797 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1) {
798 x25_transmit_clear_request(neigh, lci, 0x01);
803 * current neighbour/link might impose additional limits
804 * on certain facilties
807 x25_limit_facilities(&facilities,neigh);
810 * Try to create a new socket.
812 if ((make = x25_make_new(sk)) == NULL) {
813 x25_transmit_clear_request(neigh, lci, 0x01);
818 * Remove the facilities, leaving any Call User Data.
823 make->state = TCP_ESTABLISHED;
825 make->protinfo.x25->lci = lci;
826 make->protinfo.x25->dest_addr = dest_addr;
827 make->protinfo.x25->source_addr = source_addr;
828 make->protinfo.x25->neighbour = neigh;
829 make->protinfo.x25->facilities = facilities;
830 make->protinfo.x25->vc_facil_mask = sk->protinfo.x25->vc_facil_mask;
832 x25_write_internal(make, X25_CALL_ACCEPTED);
835 * Incoming Call User Data.
838 memcpy(make->protinfo.x25->calluserdata.cuddata, skb->data, skb->len);
839 make->protinfo.x25->calluserdata.cudlength = skb->len;
842 make->protinfo.x25->state = X25_STATE_3;
847 x25_insert_socket(make);
849 skb_queue_head(&sk->receive_queue, skb);
851 x25_start_heartbeat(make);
854 sk->data_ready(sk, skb->len);
859 static int x25_sendmsg(struct socket *sock, struct msghdr *msg, int len, struct scm_cookie *scm)
861 struct sock *sk = sock->sk;
862 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
864 struct sockaddr_x25 sx25;
866 unsigned char *asmptr;
869 if (msg->msg_flags & ~(MSG_DONTWAIT | MSG_OOB | MSG_EOR))
872 /* we currently don't support segmented records at the user interface */
873 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
877 return -EADDRNOTAVAIL;
879 if (sk->shutdown & SEND_SHUTDOWN) {
880 send_sig(SIGPIPE, current, 0);
884 if (sk->protinfo.x25->neighbour == NULL)
888 if (msg->msg_namelen < sizeof(sx25))
891 if (strcmp(sk->protinfo.x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr) != 0)
893 if (sx25.sx25_family != AF_X25)
897 * FIXME 1003.1g - if the socket is like this because
898 * it has become closed (not started closed) we ought
901 if (sk->state != TCP_ESTABLISHED)
904 sx25.sx25_family = AF_X25;
905 sx25.sx25_addr = sk->protinfo.x25->dest_addr;
908 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
911 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
913 if ((msg->msg_flags & MSG_OOB) && len > 32)
916 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
918 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
920 X25_SKB_CB(skb)->flags = msg->msg_flags;
922 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
925 * Put the data on the end
927 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
929 asmptr = skb->h.raw = skb_put(skb, len);
931 memcpy_fromiovec(asmptr, msg->msg_iov, len);
934 * If the Q BIT Include socket option is in force, the first
935 * byte of the user data is the logical value of the Q Bit.
937 if (sk->protinfo.x25->qbitincl) {
943 * Push down the X.25 header
945 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
947 if (msg->msg_flags & MSG_OOB) {
948 if (sk->protinfo.x25->neighbour->extended) {
949 asmptr = skb_push(skb, X25_STD_MIN_LEN);
950 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
951 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
952 *asmptr++ = X25_INTERRUPT;
954 asmptr = skb_push(skb, X25_STD_MIN_LEN);
955 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
956 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
957 *asmptr++ = X25_INTERRUPT;
960 if (sk->protinfo.x25->neighbour->extended) {
961 /* Build an Extended X.25 header */
962 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
963 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
964 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
965 *asmptr++ = X25_DATA;
966 *asmptr++ = X25_DATA;
968 /* Build an Standard X.25 header */
969 asmptr = skb_push(skb, X25_STD_MIN_LEN);
970 *asmptr++ = ((sk->protinfo.x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
971 *asmptr++ = (sk->protinfo.x25->lci >> 0) & 0xFF;
972 *asmptr++ = X25_DATA;
976 skb->data[0] |= X25_Q_BIT;
979 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
980 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
982 if (sk->state != TCP_ESTABLISHED) {
987 if (msg->msg_flags & MSG_OOB) {
988 skb_queue_tail(&sk->protinfo.x25->interrupt_out_queue, skb);
990 len = x25_output(sk, skb);
994 if(sk->protinfo.x25->qbitincl) len++;
999 * lock_sock() is currently only used to serialize this x25_kick()
1000 * against input-driven x25_kick() calls. It currently only blocks
1001 * incoming packets for this socket and does not protect against
1002 * any other socket state changes and is not called from anywhere
1003 * else. As x25_kick() cannot block and as long as all socket
1004 * operations are BKL-wrapped, we don't need take to care about
1005 * purging the backlog queue in x25_release().
1007 * Using lock_sock() to protect all socket operations entirely
1008 * (and making the whole x25 stack SMP aware) unfortunately would
1009 * require major changes to {send,recv}msg and skb allocation methods.
1020 static int x25_recvmsg(struct socket *sock, struct msghdr *msg, int size, int flags, struct scm_cookie *scm)
1022 struct sock *sk = sock->sk;
1023 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1025 struct sk_buff *skb;
1026 unsigned char *asmptr;
1030 * This works for seqpacket too. The receiver has ordered the queue for
1031 * us! We do one quick check first though
1033 if (sk->state != TCP_ESTABLISHED)
1036 if (flags & MSG_OOB) {
1037 if (sk->urginline || skb_peek(&sk->protinfo.x25->interrupt_in_queue) == NULL)
1040 skb = skb_dequeue(&sk->protinfo.x25->interrupt_in_queue);
1042 skb_pull(skb, X25_STD_MIN_LEN);
1045 * No Q bit information on Interrupt data.
1047 if (sk->protinfo.x25->qbitincl) {
1048 asmptr = skb_push(skb, 1);
1052 msg->msg_flags |= MSG_OOB;
1054 /* Now we can treat all alike */
1055 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1058 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1060 skb_pull(skb, (sk->protinfo.x25->neighbour->extended) ? X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1062 if (sk->protinfo.x25->qbitincl) {
1063 asmptr = skb_push(skb, 1);
1068 skb->h.raw = skb->data;
1072 if (copied > size) {
1074 msg->msg_flags |= MSG_TRUNC;
1077 /* Currently, each datagram always contains a complete record */
1078 msg->msg_flags |= MSG_EOR;
1080 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1083 sx25->sx25_family = AF_X25;
1084 sx25->sx25_addr = sk->protinfo.x25->dest_addr;
1087 msg->msg_namelen = sizeof(struct sockaddr_x25);
1089 skb_free_datagram(sk, skb);
1098 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1100 struct sock *sk = sock->sk;
1105 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1108 return put_user(amount, (unsigned int *)arg);
1112 struct sk_buff *skb;
1114 /* These two are safe on a single CPU system as only user tasks fiddle here */
1115 if ((skb = skb_peek(&sk->receive_queue)) != NULL)
1117 return put_user(amount, (unsigned int *)arg);
1122 if (sk->stamp.tv_sec == 0)
1124 return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1130 case SIOCGIFDSTADDR:
1131 case SIOCSIFDSTADDR:
1132 case SIOCGIFBRDADDR:
1133 case SIOCSIFBRDADDR:
1134 case SIOCGIFNETMASK:
1135 case SIOCSIFNETMASK:
1142 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1143 return x25_route_ioctl(cmd, (void *)arg);
1145 case SIOCX25GSUBSCRIP:
1146 return x25_subscr_ioctl(cmd, (void *)arg);
1148 case SIOCX25SSUBSCRIP:
1149 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1150 return x25_subscr_ioctl(cmd, (void *)arg);
1152 case SIOCX25GFACILITIES: {
1153 struct x25_facilities facilities;
1154 facilities = sk->protinfo.x25->facilities;
1155 return copy_to_user((void *)arg, &facilities, sizeof(facilities)) ? -EFAULT : 0;
1158 case SIOCX25SFACILITIES: {
1159 struct x25_facilities facilities;
1160 if (copy_from_user(&facilities, (void *)arg, sizeof(facilities)))
1162 if (sk->state != TCP_LISTEN && sk->state != TCP_CLOSE)
1164 if (facilities.pacsize_in < X25_PS16 || facilities.pacsize_in > X25_PS4096)
1166 if (facilities.pacsize_out < X25_PS16 || facilities.pacsize_out > X25_PS4096)
1168 if (facilities.winsize_in < 1 || facilities.winsize_in > 127)
1170 if (facilities.throughput < 0x03 || facilities.throughput > 0xDD)
1172 if (facilities.reverse != 0 && facilities.reverse != 1)
1174 sk->protinfo.x25->facilities = facilities;
1178 case SIOCX25GCALLUSERDATA: {
1179 struct x25_calluserdata calluserdata;
1180 calluserdata = sk->protinfo.x25->calluserdata;
1181 return copy_to_user((void *)arg, &calluserdata, sizeof(calluserdata)) ? -EFAULT : 0;
1184 case SIOCX25SCALLUSERDATA: {
1185 struct x25_calluserdata calluserdata;
1186 if (copy_from_user(&calluserdata, (void *)arg, sizeof(calluserdata)))
1188 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1190 sk->protinfo.x25->calluserdata = calluserdata;
1194 case SIOCX25GCAUSEDIAG: {
1195 struct x25_causediag causediag;
1196 causediag = sk->protinfo.x25->causediag;
1197 return copy_to_user((void *)arg, &causediag, sizeof(causediag)) ? -EFAULT : 0;
1201 return dev_ioctl(cmd, (void *)arg);
1208 static int x25_get_info(char *buffer, char **start, off_t offset, int length)
1211 struct net_device *dev;
1212 const char *devname;
1219 len += sprintf(buffer, "dest_addr src_addr dev lci st vs vr va t t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
1221 for (s = x25_list; s != NULL; s = s->next) {
1222 if (s->protinfo.x25->neighbour == NULL || (dev = s->protinfo.x25->neighbour->dev) == NULL)
1225 devname = s->protinfo.x25->neighbour->dev->name;
1227 len += sprintf(buffer + len, "%-10s %-10s %-5s %3.3X %d %d %d %d %3lu %3lu %3lu %3lu %3lu %5d %5d %ld\n",
1228 (s->protinfo.x25->dest_addr.x25_addr[0] == '\0') ? "*" : s->protinfo.x25->dest_addr.x25_addr,
1229 (s->protinfo.x25->source_addr.x25_addr[0] == '\0') ? "*" : s->protinfo.x25->source_addr.x25_addr,
1231 s->protinfo.x25->lci & 0x0FFF,
1232 s->protinfo.x25->state,
1233 s->protinfo.x25->vs,
1234 s->protinfo.x25->vr,
1235 s->protinfo.x25->va,
1236 x25_display_timer(s) / HZ,
1237 s->protinfo.x25->t2 / HZ,
1238 s->protinfo.x25->t21 / HZ,
1239 s->protinfo.x25->t22 / HZ,
1240 s->protinfo.x25->t23 / HZ,
1241 atomic_read(&s->wmem_alloc),
1242 atomic_read(&s->rmem_alloc),
1243 s->socket != NULL ? s->socket->inode->i_ino : 0L);
1252 if (pos > offset + length)
1258 *start = buffer + (offset - begin);
1259 len -= (offset - begin);
1261 if (len > length) len = length;
1266 struct net_proto_family x25_family_ops = {
1271 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1274 release: x25_release,
1276 connect: x25_connect,
1277 socketpair: sock_no_socketpair,
1279 getname: x25_getname,
1280 poll: datagram_poll,
1283 shutdown: sock_no_shutdown,
1284 setsockopt: x25_setsockopt,
1285 getsockopt: x25_getsockopt,
1286 sendmsg: x25_sendmsg,
1287 recvmsg: x25_recvmsg,
1289 sendpage: sock_no_sendpage,
1292 #include <linux/smp_lock.h>
1293 SOCKOPS_WRAP(x25_proto, AF_X25);
1296 static struct packet_type x25_packet_type = {
1297 type: __constant_htons(ETH_P_X25),
1298 func: x25_lapb_receive_frame,
1301 struct notifier_block x25_dev_notifier = {
1302 notifier_call: x25_device_event,
1305 void x25_kill_by_neigh(struct x25_neigh *neigh)
1309 for( s=x25_list; s != NULL; s=s->next){
1310 if( s->protinfo.x25->neighbour == neigh )
1311 x25_disconnect(s, ENETUNREACH, 0, 0);
1315 static int __init x25_init(void)
1318 struct net_device *dev;
1320 sock_register(&x25_family_ops);
1322 dev_add_pack(&x25_packet_type);
1324 register_netdevice_notifier(&x25_dev_notifier);
1326 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1328 #ifdef CONFIG_SYSCTL
1329 x25_register_sysctl();
1332 proc_net_create("x25", 0, x25_get_info);
1333 proc_net_create("x25_routes", 0, x25_routes_get_info);
1337 * Register any pre existing devices.
1339 read_lock(&dev_base_lock);
1340 for (dev = dev_base; dev != NULL; dev = dev->next) {
1341 if ((dev->flags & IFF_UP) && (dev->type == ARPHRD_X25
1342 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1343 || dev->type == ARPHRD_ETHER
1346 x25_link_device_up(dev);
1348 read_unlock(&dev_base_lock);
1352 module_init(x25_init);
1358 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1359 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1360 MODULE_LICENSE("GPL");
1362 static void __exit x25_exit(void)
1365 proc_net_remove("x25");
1366 proc_net_remove("x25_routes");
1371 #ifdef CONFIG_SYSCTL
1372 x25_unregister_sysctl();
1375 unregister_netdevice_notifier(&x25_dev_notifier);
1377 dev_remove_pack(&x25_packet_type);
1379 sock_unregister(AF_X25);
1381 module_exit(x25_exit);