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
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.
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/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>
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>
54 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
55 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
56 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
57 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
58 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
61 DEFINE_RWLOCK(x25_list_lock);
63 static struct proto_ops x25_proto_ops;
65 static struct x25_address null_x25_address = {" "};
67 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
68 struct x25_address *calling_addr)
70 int called_len, calling_len;
71 char *called, *calling;
74 called_len = (*p >> 0) & 0x0F;
75 calling_len = (*p >> 4) & 0x0F;
77 called = called_addr->x25_addr;
78 calling = calling_addr->x25_addr;
81 for (i = 0; i < (called_len + calling_len); i++) {
84 *called++ = ((*p >> 0) & 0x0F) + '0';
87 *called++ = ((*p >> 4) & 0x0F) + '0';
91 *calling++ = ((*p >> 0) & 0x0F) + '0';
94 *calling++ = ((*p >> 4) & 0x0F) + '0';
99 *called = *calling = '\0';
101 return 1 + (called_len + calling_len + 1) / 2;
104 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
105 struct x25_address *calling_addr)
107 unsigned int called_len, calling_len;
108 char *called, *calling;
111 called = called_addr->x25_addr;
112 calling = calling_addr->x25_addr;
114 called_len = strlen(called);
115 calling_len = strlen(calling);
117 *p++ = (calling_len << 4) | (called_len << 0);
119 for (i = 0; i < (called_len + calling_len); i++) {
120 if (i < called_len) {
122 *p |= (*called++ - '0') << 0;
126 *p |= (*called++ - '0') << 4;
130 *p |= (*calling++ - '0') << 0;
134 *p |= (*calling++ - '0') << 4;
139 return 1 + (called_len + calling_len + 1) / 2;
143 * Socket removal during an interrupt is now safe.
145 static void x25_remove_socket(struct sock *sk)
147 write_lock_bh(&x25_list_lock);
148 sk_del_node_init(sk);
149 write_unlock_bh(&x25_list_lock);
153 * Kill all bound sockets on a dropped device.
155 static void x25_kill_by_device(struct net_device *dev)
158 struct hlist_node *node;
160 write_lock_bh(&x25_list_lock);
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);
166 write_unlock_bh(&x25_list_lock);
170 * Handle device status changes.
172 static int x25_device_event(struct notifier_block *this, unsigned long event,
175 struct net_device *dev = ptr;
176 struct x25_neigh *nb;
178 if (dev->type == ARPHRD_X25
179 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
180 || dev->type == ARPHRD_ETHER
185 x25_link_device_up(dev);
187 case NETDEV_GOING_DOWN:
188 nb = x25_get_neigh(dev);
190 x25_terminate_link(nb);
195 x25_kill_by_device(dev);
196 x25_route_device_down(dev);
197 x25_link_device_down(dev);
206 * Add a socket to the bound sockets list.
208 static void x25_insert_socket(struct sock *sk)
210 write_lock_bh(&x25_list_lock);
211 sk_add_node(sk, &x25_list);
212 write_unlock_bh(&x25_list_lock);
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
222 static struct sock *x25_find_listener(struct x25_address *addr, struct x25_calluserdata *calluserdata)
225 struct sock *next_best;
226 struct hlist_node *node;
228 read_lock_bh(&x25_list_lock);
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) {
239 * Found a listening socket, now check the incoming
240 * call user data vs this sockets call user data
242 if (x25_check_calluserdata(&x25_sk(s)->calluserdata, calluserdata)) {
246 if (x25_sk(s)->calluserdata.cudlength == 0) {
257 read_unlock_bh(&x25_list_lock);
262 * Find a connected X.25 socket given my LCI and neighbour.
264 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
267 struct hlist_node *node;
269 sk_for_each(s, node, &x25_list)
270 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
279 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
283 read_lock_bh(&x25_list_lock);
284 s = __x25_find_socket(lci, nb);
285 read_unlock_bh(&x25_list_lock);
290 * Find a unique LCI for a given device.
292 static unsigned int x25_new_lci(struct x25_neigh *nb)
294 unsigned int lci = 1;
297 read_lock_bh(&x25_list_lock);
299 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
307 read_unlock_bh(&x25_list_lock);
314 void x25_destroy_socket(struct sock *);
317 * handler for deferred kills.
319 static void x25_destroy_timer(unsigned long data)
321 x25_destroy_socket((struct sock *)data);
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
331 void x25_destroy_socket(struct sock *sk)
337 x25_stop_heartbeat(sk);
340 x25_remove_socket(sk);
341 x25_clear_queues(sk); /* Flush the queues */
343 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
344 if (skb->sk != sk) { /* A pending connection */
346 * Queue the unaccepted socket for death
348 sock_set_flag(skb->sk, SOCK_DEAD);
349 x25_start_heartbeat(skb->sk);
350 x25_sk(skb->sk)->state = X25_STATE_0;
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);
364 /* drop last reference so sock_put will free */
373 * Handling for system calls applied via the various interfaces to a
374 * X.25 socket object.
377 static int x25_setsockopt(struct socket *sock, int level, int optname,
378 char __user *optval, int optlen)
381 struct sock *sk = sock->sk;
382 int rc = -ENOPROTOOPT;
384 if (level != SOL_X25 || optname != X25_QBITINCL)
388 if (optlen < sizeof(int))
392 if (get_user(opt, (int __user *)optval))
395 x25_sk(sk)->qbitincl = !!opt;
401 static int x25_getsockopt(struct socket *sock, int level, int optname,
402 char __user *optval, int __user *optlen)
404 struct sock *sk = sock->sk;
405 int val, len, rc = -ENOPROTOOPT;
407 if (level != SOL_X25 || optname != X25_QBITINCL)
411 if (get_user(len, optlen))
414 len = min_t(unsigned int, len, sizeof(int));
421 if (put_user(len, optlen))
424 val = x25_sk(sk)->qbitincl;
425 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
430 static int x25_listen(struct socket *sock, int backlog)
432 struct sock *sk = sock->sk;
433 int rc = -EOPNOTSUPP;
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;
445 static struct sock *x25_alloc_socket(void)
448 struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, 1, NULL);
453 x25 = sk->sk_protinfo = kmalloc(sizeof(*x25), GFP_ATOMIC);
457 memset(x25, 0, sizeof(*x25));
461 sock_init_data(NULL, sk);
462 sk_set_owner(sk, THIS_MODULE);
464 skb_queue_head_init(&x25->ack_queue);
465 skb_queue_head_init(&x25->fragment_queue);
466 skb_queue_head_init(&x25->interrupt_in_queue);
467 skb_queue_head_init(&x25->interrupt_out_queue);
476 void x25_init_timers(struct sock *sk);
478 static int x25_create(struct socket *sock, int protocol)
482 int rc = -ESOCKTNOSUPPORT;
484 if (sock->type != SOCK_SEQPACKET || protocol)
488 if ((sk = x25_alloc_socket()) == NULL)
493 sk->sk_socket = sock;
494 sk->sk_type = sock->type;
495 sk->sk_sleep = &sock->wait;
500 sock->ops = &x25_proto_ops;
501 sk->sk_protocol = protocol;
502 sk->sk_backlog_rcv = x25_backlog_rcv;
504 x25->t21 = sysctl_x25_call_request_timeout;
505 x25->t22 = sysctl_x25_reset_request_timeout;
506 x25->t23 = sysctl_x25_clear_request_timeout;
507 x25->t2 = sysctl_x25_ack_holdback_timeout;
508 x25->state = X25_STATE_0;
510 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
511 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
512 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
513 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
514 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
515 x25->facilities.reverse = X25_DEFAULT_REVERSE;
521 static struct sock *x25_make_new(struct sock *osk)
523 struct sock *sk = NULL;
524 struct x25_opt *x25, *ox25;
526 if (osk->sk_type != SOCK_SEQPACKET)
529 if ((sk = x25_alloc_socket()) == NULL)
534 sk->sk_type = osk->sk_type;
535 sk->sk_socket = osk->sk_socket;
536 sk->sk_priority = osk->sk_priority;
537 sk->sk_protocol = osk->sk_protocol;
538 sk->sk_rcvbuf = osk->sk_rcvbuf;
539 sk->sk_sndbuf = osk->sk_sndbuf;
540 sk->sk_debug = osk->sk_debug;
541 sk->sk_state = TCP_ESTABLISHED;
542 sk->sk_sleep = osk->sk_sleep;
543 sk->sk_zapped = osk->sk_zapped;
544 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
547 x25->t21 = ox25->t21;
548 x25->t22 = ox25->t22;
549 x25->t23 = ox25->t23;
551 x25->facilities = ox25->facilities;
552 x25->qbitincl = ox25->qbitincl;
559 static int x25_release(struct socket *sock)
561 struct sock *sk = sock->sk;
569 switch (x25->state) {
573 x25_disconnect(sk, 0, 0, 0);
574 x25_destroy_socket(sk);
580 x25_clear_queues(sk);
581 x25_write_internal(sk, X25_CLEAR_REQUEST);
582 x25_start_t23timer(sk);
583 x25->state = X25_STATE_2;
584 sk->sk_state = TCP_CLOSE;
585 sk->sk_shutdown |= SEND_SHUTDOWN;
586 sk->sk_state_change(sk);
587 sock_set_flag(sk, SOCK_DEAD);
588 sock_set_flag(sk, SOCK_DESTROY);
593 sk->sk_socket = NULL; /* Not used, but we should do this */
598 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
600 struct sock *sk = sock->sk;
601 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
603 if (!sk->sk_zapped ||
604 addr_len != sizeof(struct sockaddr_x25) ||
605 addr->sx25_family != AF_X25)
608 x25_sk(sk)->source_addr = addr->sx25_addr;
609 x25_insert_socket(sk);
611 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
616 static int x25_wait_for_connection_establishment(struct sock *sk)
618 DECLARE_WAITQUEUE(wait, current);
621 add_wait_queue_exclusive(sk->sk_sleep, &wait);
623 __set_current_state(TASK_INTERRUPTIBLE);
625 if (signal_pending(current))
629 sk->sk_socket->state = SS_UNCONNECTED;
633 if (sk->sk_state != TCP_ESTABLISHED) {
640 __set_current_state(TASK_RUNNING);
641 remove_wait_queue(sk->sk_sleep, &wait);
645 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
646 int addr_len, int flags)
648 struct sock *sk = sock->sk;
649 struct x25_opt *x25 = x25_sk(sk);
650 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
651 struct x25_route *rt;
655 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
656 sock->state = SS_CONNECTED;
657 goto out; /* Connect completed during a ERESTARTSYS event */
661 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
662 sock->state = SS_UNCONNECTED;
666 rc = -EISCONN; /* No reconnect on a seqpacket socket */
667 if (sk->sk_state == TCP_ESTABLISHED)
670 sk->sk_state = TCP_CLOSE;
671 sock->state = SS_UNCONNECTED;
674 if (addr_len != sizeof(struct sockaddr_x25) ||
675 addr->sx25_family != AF_X25)
679 rt = x25_get_route(&addr->sx25_addr);
683 x25->neighbour = x25_get_neigh(rt->dev);
687 x25_limit_facilities(&x25->facilities, x25->neighbour);
689 x25->lci = x25_new_lci(x25->neighbour);
694 if (sk->sk_zapped) /* Must bind first - autobinding does not work */
697 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
698 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
700 x25->dest_addr = addr->sx25_addr;
702 /* Move to connecting socket, start sending Connect Requests */
703 sock->state = SS_CONNECTING;
704 sk->sk_state = TCP_SYN_SENT;
706 x25->state = X25_STATE_1;
708 x25_write_internal(sk, X25_CALL_REQUEST);
710 x25_start_heartbeat(sk);
711 x25_start_t21timer(sk);
715 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
718 rc = x25_wait_for_connection_establishment(sk);
722 sock->state = SS_CONNECTED;
726 x25_neigh_put(x25->neighbour);
734 static int x25_wait_for_data(struct sock *sk, int timeout)
736 DECLARE_WAITQUEUE(wait, current);
739 add_wait_queue_exclusive(sk->sk_sleep, &wait);
741 __set_current_state(TASK_INTERRUPTIBLE);
742 if (sk->sk_shutdown & RCV_SHUTDOWN)
745 if (signal_pending(current))
751 if (skb_queue_empty(&sk->sk_receive_queue)) {
753 timeout = schedule_timeout(timeout);
758 __set_current_state(TASK_RUNNING);
759 remove_wait_queue(sk->sk_sleep, &wait);
763 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
765 struct sock *sk = sock->sk;
770 if (!sk || sk->sk_state != TCP_LISTEN)
774 if (sk->sk_type != SOCK_SEQPACKET)
778 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
781 skb = skb_dequeue(&sk->sk_receive_queue);
786 newsk->sk_socket = newsock;
787 newsk->sk_sleep = &newsock->wait;
789 /* Now attach up the new socket */
792 sk->sk_ack_backlog--;
794 newsock->state = SS_CONNECTED;
802 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
803 int *uaddr_len, int peer)
805 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
806 struct sock *sk = sock->sk;
807 struct x25_opt *x25 = x25_sk(sk);
810 if (sk->sk_state != TCP_ESTABLISHED)
812 sx25->sx25_addr = x25->dest_addr;
814 sx25->sx25_addr = x25->source_addr;
816 sx25->sx25_family = AF_X25;
817 *uaddr_len = sizeof(*sx25);
822 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
827 struct x25_opt *makex25;
828 struct x25_address source_addr, dest_addr;
829 struct x25_facilities facilities;
830 struct x25_calluserdata calluserdata;
834 * Remove the LCI and frame type.
836 skb_pull(skb, X25_STD_MIN_LEN);
839 * Extract the X.25 addresses and convert them to ASCII strings,
842 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
845 * Get the length of the facilities, skip past them for the moment
846 * get the call user data because this is needed to determine
847 * the correct listener
849 len = skb->data[0] + 1;
853 * Incoming Call User Data.
856 memcpy(calluserdata.cuddata, skb->data, skb->len);
857 calluserdata.cudlength = skb->len;
863 * Find a listener for the particular address/cud pair.
865 sk = x25_find_listener(&source_addr,&calluserdata);
868 * We can't accept the Call Request.
870 if (!sk || sk->sk_ack_backlog == sk->sk_max_ack_backlog)
871 goto out_clear_request;
874 * Try to reach a compromise on the requested facilities.
876 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
880 * current neighbour/link might impose additional limits
881 * on certain facilties
884 x25_limit_facilities(&facilities, nb);
887 * Try to create a new socket.
889 make = x25_make_new(sk);
894 * Remove the facilities
899 make->sk_state = TCP_ESTABLISHED;
901 makex25 = x25_sk(make);
903 makex25->dest_addr = dest_addr;
904 makex25->source_addr = source_addr;
905 makex25->neighbour = nb;
906 makex25->facilities = facilities;
907 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
908 makex25->calluserdata = calluserdata;
910 x25_write_internal(make, X25_CALL_ACCEPTED);
912 makex25->state = X25_STATE_3;
914 sk->sk_ack_backlog++;
916 x25_insert_socket(make);
918 skb_queue_head(&sk->sk_receive_queue, skb);
920 x25_start_heartbeat(make);
922 if (!sock_flag(sk, SOCK_DEAD))
923 sk->sk_data_ready(sk, skb->len);
932 x25_transmit_clear_request(nb, lci, 0x01);
936 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
937 struct msghdr *msg, size_t len)
939 struct sock *sk = sock->sk;
940 struct x25_opt *x25 = x25_sk(sk);
941 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
942 struct sockaddr_x25 sx25;
944 unsigned char *asmptr;
945 int noblock = msg->msg_flags & MSG_DONTWAIT;
947 int qbit = 0, rc = -EINVAL;
949 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
952 /* we currently don't support segmented records at the user interface */
953 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
961 if (sk->sk_shutdown & SEND_SHUTDOWN) {
962 send_sig(SIGPIPE, current, 0);
972 if (msg->msg_namelen < sizeof(sx25))
974 memcpy(&sx25, usx25, sizeof(sx25));
976 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
979 if (sx25.sx25_family != AF_X25)
983 * FIXME 1003.1g - if the socket is like this because
984 * it has become closed (not started closed) we ought
988 if (sk->sk_state != TCP_ESTABLISHED)
991 sx25.sx25_family = AF_X25;
992 sx25.sx25_addr = x25->dest_addr;
995 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
998 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1000 if ((msg->msg_flags & MSG_OOB) && len > 32)
1003 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1005 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1008 X25_SKB_CB(skb)->flags = msg->msg_flags;
1010 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1013 * Put the data on the end
1015 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1017 asmptr = skb->h.raw = skb_put(skb, len);
1019 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1024 * If the Q BIT Include socket option is in force, the first
1025 * byte of the user data is the logical value of the Q Bit.
1027 if (x25->qbitincl) {
1028 qbit = skb->data[0];
1033 * Push down the X.25 header
1035 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1037 if (msg->msg_flags & MSG_OOB) {
1038 if (x25->neighbour->extended) {
1039 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1040 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1041 *asmptr++ = (x25->lci >> 0) & 0xFF;
1042 *asmptr++ = X25_INTERRUPT;
1044 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1045 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1046 *asmptr++ = (x25->lci >> 0) & 0xFF;
1047 *asmptr++ = X25_INTERRUPT;
1050 if (x25->neighbour->extended) {
1051 /* Build an Extended X.25 header */
1052 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1053 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1054 *asmptr++ = (x25->lci >> 0) & 0xFF;
1055 *asmptr++ = X25_DATA;
1056 *asmptr++ = X25_DATA;
1058 /* Build an Standard X.25 header */
1059 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1060 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1061 *asmptr++ = (x25->lci >> 0) & 0xFF;
1062 *asmptr++ = X25_DATA;
1066 skb->data[0] |= X25_Q_BIT;
1069 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1070 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1073 if (sk->sk_state != TCP_ESTABLISHED)
1076 if (msg->msg_flags & MSG_OOB)
1077 skb_queue_tail(&x25->interrupt_out_queue, skb);
1079 len = x25_output(sk, skb);
1082 else if (x25->qbitincl)
1087 * lock_sock() is currently only used to serialize this x25_kick()
1088 * against input-driven x25_kick() calls. It currently only blocks
1089 * incoming packets for this socket and does not protect against
1090 * any other socket state changes and is not called from anywhere
1091 * else. As x25_kick() cannot block and as long as all socket
1092 * operations are BKL-wrapped, we don't need take to care about
1093 * purging the backlog queue in x25_release().
1095 * Using lock_sock() to protect all socket operations entirely
1096 * (and making the whole x25 stack SMP aware) unfortunately would
1097 * require major changes to {send,recv}msg and skb allocation methods.
1112 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1113 struct msghdr *msg, size_t size,
1116 struct sock *sk = sock->sk;
1117 struct x25_opt *x25 = x25_sk(sk);
1118 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1121 struct sk_buff *skb;
1122 unsigned char *asmptr;
1126 * This works for seqpacket too. The receiver has ordered the queue for
1127 * us! We do one quick check first though
1129 if (sk->sk_state != TCP_ESTABLISHED)
1132 if (flags & MSG_OOB) {
1134 if (sock_flag(sk, SOCK_URGINLINE) ||
1135 !skb_peek(&x25->interrupt_in_queue))
1138 skb = skb_dequeue(&x25->interrupt_in_queue);
1140 skb_pull(skb, X25_STD_MIN_LEN);
1143 * No Q bit information on Interrupt data.
1145 if (x25->qbitincl) {
1146 asmptr = skb_push(skb, 1);
1150 msg->msg_flags |= MSG_OOB;
1152 /* Now we can treat all alike */
1153 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1154 flags & MSG_DONTWAIT, &rc);
1158 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1160 skb_pull(skb, x25->neighbour->extended ?
1161 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1163 if (x25->qbitincl) {
1164 asmptr = skb_push(skb, 1);
1169 skb->h.raw = skb->data;
1173 if (copied > size) {
1175 msg->msg_flags |= MSG_TRUNC;
1178 /* Currently, each datagram always contains a complete record */
1179 msg->msg_flags |= MSG_EOR;
1181 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1183 goto out_free_dgram;
1186 sx25->sx25_family = AF_X25;
1187 sx25->sx25_addr = x25->dest_addr;
1190 msg->msg_namelen = sizeof(struct sockaddr_x25);
1197 skb_free_datagram(sk, skb);
1203 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1205 struct sock *sk = sock->sk;
1206 struct x25_opt *x25 = x25_sk(sk);
1207 void __user *argp = (void __user *)arg;
1212 int amount = sk->sk_sndbuf -
1213 atomic_read(&sk->sk_wmem_alloc);
1216 rc = put_user(amount, (unsigned int __user *)argp);
1221 struct sk_buff *skb;
1224 * These two are safe on a single CPU system as
1225 * only user tasks fiddle here
1227 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1229 rc = put_user(amount, (unsigned int __user *)argp);
1236 rc = sock_get_timestamp(sk,
1237 (struct timeval __user *)argp);
1241 case SIOCGIFDSTADDR:
1242 case SIOCSIFDSTADDR:
1243 case SIOCGIFBRDADDR:
1244 case SIOCSIFBRDADDR:
1245 case SIOCGIFNETMASK:
1246 case SIOCSIFNETMASK:
1254 if (!capable(CAP_NET_ADMIN))
1256 rc = x25_route_ioctl(cmd, argp);
1258 case SIOCX25GSUBSCRIP:
1259 rc = x25_subscr_ioctl(cmd, argp);
1261 case SIOCX25SSUBSCRIP:
1263 if (!capable(CAP_NET_ADMIN))
1265 rc = x25_subscr_ioctl(cmd, argp);
1267 case SIOCX25GFACILITIES: {
1268 struct x25_facilities fac = x25->facilities;
1269 rc = copy_to_user(argp, &fac,
1270 sizeof(fac)) ? -EFAULT : 0;
1274 case SIOCX25SFACILITIES: {
1275 struct x25_facilities facilities;
1277 if (copy_from_user(&facilities, argp,
1278 sizeof(facilities)))
1281 if (sk->sk_state != TCP_LISTEN &&
1282 sk->sk_state != TCP_CLOSE)
1284 if (facilities.pacsize_in < X25_PS16 ||
1285 facilities.pacsize_in > X25_PS4096)
1287 if (facilities.pacsize_out < X25_PS16 ||
1288 facilities.pacsize_out > X25_PS4096)
1290 if (facilities.winsize_in < 1 ||
1291 facilities.winsize_in > 127)
1293 if (facilities.throughput < 0x03 ||
1294 facilities.throughput > 0xDD)
1296 if (facilities.reverse && facilities.reverse != 1)
1298 x25->facilities = facilities;
1303 case SIOCX25GCALLUSERDATA: {
1304 struct x25_calluserdata cud = x25->calluserdata;
1305 rc = copy_to_user(argp, &cud,
1306 sizeof(cud)) ? -EFAULT : 0;
1310 case SIOCX25SCALLUSERDATA: {
1311 struct x25_calluserdata calluserdata;
1314 if (copy_from_user(&calluserdata, argp,
1315 sizeof(calluserdata)))
1318 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1320 x25->calluserdata = calluserdata;
1325 case SIOCX25GCAUSEDIAG: {
1326 struct x25_causediag causediag;
1327 causediag = x25->causediag;
1328 rc = copy_to_user(argp, &causediag,
1329 sizeof(causediag)) ? -EFAULT : 0;
1334 rc = dev_ioctl(cmd, argp);
1341 static struct net_proto_family x25_family_ops = {
1343 .create = x25_create,
1344 .owner = THIS_MODULE,
1347 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1349 .owner = THIS_MODULE,
1350 .release = x25_release,
1352 .connect = x25_connect,
1353 .socketpair = sock_no_socketpair,
1354 .accept = x25_accept,
1355 .getname = x25_getname,
1356 .poll = datagram_poll,
1358 .listen = x25_listen,
1359 .shutdown = sock_no_shutdown,
1360 .setsockopt = x25_setsockopt,
1361 .getsockopt = x25_getsockopt,
1362 .sendmsg = x25_sendmsg,
1363 .recvmsg = x25_recvmsg,
1364 .mmap = sock_no_mmap,
1365 .sendpage = sock_no_sendpage,
1368 #include <linux/smp_lock.h>
1369 SOCKOPS_WRAP(x25_proto, AF_X25);
1371 static struct packet_type x25_packet_type = {
1372 .type = __constant_htons(ETH_P_X25),
1373 .func = x25_lapb_receive_frame,
1376 static struct notifier_block x25_dev_notifier = {
1377 .notifier_call = x25_device_event,
1380 void x25_kill_by_neigh(struct x25_neigh *nb)
1383 struct hlist_node *node;
1385 write_lock_bh(&x25_list_lock);
1387 sk_for_each(s, node, &x25_list)
1388 if (x25_sk(s)->neighbour == nb)
1389 x25_disconnect(s, ENETUNREACH, 0, 0);
1391 write_unlock_bh(&x25_list_lock);
1394 static int __init x25_init(void)
1396 sock_register(&x25_family_ops);
1398 dev_add_pack(&x25_packet_type);
1400 register_netdevice_notifier(&x25_dev_notifier);
1402 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1404 #ifdef CONFIG_SYSCTL
1405 x25_register_sysctl();
1410 module_init(x25_init);
1412 static void __exit x25_exit(void)
1418 #ifdef CONFIG_SYSCTL
1419 x25_unregister_sysctl();
1422 unregister_netdevice_notifier(&x25_dev_notifier);
1424 dev_remove_pack(&x25_packet_type);
1426 sock_unregister(AF_X25);
1428 module_exit(x25_exit);
1430 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1431 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1432 MODULE_LICENSE("GPL");
1433 MODULE_ALIAS_NETPROTO(PF_X25);