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
32 * 2005-04-02 Shaun Pereira Selective sub address matching
34 * 2005-04-15 Shaun Pereira Fast select with no restriction on
38 #include <linux/module.h>
39 #include <linux/capability.h>
40 #include <linux/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43 #include <linux/timer.h>
44 #include <linux/string.h>
45 #include <linux/net.h>
46 #include <linux/netdevice.h>
47 #include <linux/if_arp.h>
48 #include <linux/skbuff.h>
50 #include <net/tcp_states.h>
51 #include <asm/uaccess.h>
52 #include <linux/fcntl.h>
53 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
54 #include <linux/notifier.h>
55 #include <linux/init.h>
56 #include <linux/compat.h>
59 #include <net/compat.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;
66 int sysctl_x25_forward = 0;
69 DEFINE_RWLOCK(x25_list_lock);
71 static const struct proto_ops x25_proto_ops;
73 static struct x25_address null_x25_address = {" "};
76 struct compat_x25_subscrip_struct {
77 char device[200-sizeof(compat_ulong_t)];
78 compat_ulong_t global_facil_mask;
79 compat_uint_t extended;
83 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
84 struct x25_address *calling_addr)
86 unsigned int called_len, calling_len;
87 char *called, *calling;
90 called_len = (*p >> 0) & 0x0F;
91 calling_len = (*p >> 4) & 0x0F;
93 called = called_addr->x25_addr;
94 calling = calling_addr->x25_addr;
97 for (i = 0; i < (called_len + calling_len); i++) {
100 *called++ = ((*p >> 0) & 0x0F) + '0';
103 *called++ = ((*p >> 4) & 0x0F) + '0';
107 *calling++ = ((*p >> 0) & 0x0F) + '0';
110 *calling++ = ((*p >> 4) & 0x0F) + '0';
115 *called = *calling = '\0';
117 return 1 + (called_len + calling_len + 1) / 2;
120 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
121 struct x25_address *calling_addr)
123 unsigned int called_len, calling_len;
124 char *called, *calling;
127 called = called_addr->x25_addr;
128 calling = calling_addr->x25_addr;
130 called_len = strlen(called);
131 calling_len = strlen(calling);
133 *p++ = (calling_len << 4) | (called_len << 0);
135 for (i = 0; i < (called_len + calling_len); i++) {
136 if (i < called_len) {
138 *p |= (*called++ - '0') << 0;
142 *p |= (*called++ - '0') << 4;
146 *p |= (*calling++ - '0') << 0;
150 *p |= (*calling++ - '0') << 4;
155 return 1 + (called_len + calling_len + 1) / 2;
159 * Socket removal during an interrupt is now safe.
161 static void x25_remove_socket(struct sock *sk)
163 write_lock_bh(&x25_list_lock);
164 sk_del_node_init(sk);
165 write_unlock_bh(&x25_list_lock);
169 * Kill all bound sockets on a dropped device.
171 static void x25_kill_by_device(struct net_device *dev)
174 struct hlist_node *node;
176 write_lock_bh(&x25_list_lock);
178 sk_for_each(s, node, &x25_list)
179 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
180 x25_disconnect(s, ENETUNREACH, 0, 0);
182 write_unlock_bh(&x25_list_lock);
186 * Handle device status changes.
188 static int x25_device_event(struct notifier_block *this, unsigned long event,
191 struct net_device *dev = ptr;
192 struct x25_neigh *nb;
194 if (!net_eq(dev_net(dev), &init_net))
197 if (dev->type == ARPHRD_X25
198 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
199 || dev->type == ARPHRD_ETHER
204 x25_link_device_up(dev);
206 case NETDEV_GOING_DOWN:
207 nb = x25_get_neigh(dev);
209 x25_terminate_link(nb);
214 x25_kill_by_device(dev);
215 x25_route_device_down(dev);
216 x25_link_device_down(dev);
225 * Add a socket to the bound sockets list.
227 static void x25_insert_socket(struct sock *sk)
229 write_lock_bh(&x25_list_lock);
230 sk_add_node(sk, &x25_list);
231 write_unlock_bh(&x25_list_lock);
235 * Find a socket that wants to accept the Call Request we just
236 * received. Check the full list for an address/cud match.
237 * If no cuds match return the next_best thing, an address match.
238 * Note: if a listening socket has cud set it must only get calls
241 static struct sock *x25_find_listener(struct x25_address *addr,
245 struct sock *next_best;
246 struct hlist_node *node;
248 read_lock_bh(&x25_list_lock);
251 sk_for_each(s, node, &x25_list)
252 if ((!strcmp(addr->x25_addr,
253 x25_sk(s)->source_addr.x25_addr) ||
254 !strcmp(addr->x25_addr,
255 null_x25_address.x25_addr)) &&
256 s->sk_state == TCP_LISTEN) {
258 * Found a listening socket, now check the incoming
259 * call user data vs this sockets call user data
261 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
262 if((memcmp(x25_sk(s)->calluserdata.cuddata,
264 x25_sk(s)->cudmatchlength)) == 0) {
278 read_unlock_bh(&x25_list_lock);
283 * Find a connected X.25 socket given my LCI and neighbour.
285 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
288 struct hlist_node *node;
290 sk_for_each(s, node, &x25_list)
291 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
300 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
304 read_lock_bh(&x25_list_lock);
305 s = __x25_find_socket(lci, nb);
306 read_unlock_bh(&x25_list_lock);
311 * Find a unique LCI for a given device.
313 static unsigned int x25_new_lci(struct x25_neigh *nb)
315 unsigned int lci = 1;
318 read_lock_bh(&x25_list_lock);
320 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
328 read_unlock_bh(&x25_list_lock);
335 static void __x25_destroy_socket(struct sock *);
338 * handler for deferred kills.
340 static void x25_destroy_timer(unsigned long data)
342 x25_destroy_socket_from_timer((struct sock *)data);
346 * This is called from user mode and the timers. Thus it protects itself
347 * against interrupt users but doesn't worry about being called during
348 * work. Once it is removed from the queue no interrupt or bottom half
349 * will touch it and we are (fairly 8-) ) safe.
350 * Not static as it's used by the timer
352 static void __x25_destroy_socket(struct sock *sk)
356 x25_stop_heartbeat(sk);
359 x25_remove_socket(sk);
360 x25_clear_queues(sk); /* Flush the queues */
362 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
363 if (skb->sk != sk) { /* A pending connection */
365 * Queue the unaccepted socket for death
367 sock_set_flag(skb->sk, SOCK_DEAD);
368 x25_start_heartbeat(skb->sk);
369 x25_sk(skb->sk)->state = X25_STATE_0;
375 if (atomic_read(&sk->sk_wmem_alloc) ||
376 atomic_read(&sk->sk_rmem_alloc)) {
377 /* Defer: outstanding buffers */
378 sk->sk_timer.expires = jiffies + 10 * HZ;
379 sk->sk_timer.function = x25_destroy_timer;
380 sk->sk_timer.data = (unsigned long)sk;
381 add_timer(&sk->sk_timer);
383 /* drop last reference so sock_put will free */
388 void x25_destroy_socket_from_timer(struct sock *sk)
392 __x25_destroy_socket(sk);
397 static void x25_destroy_socket(struct sock *sk)
401 __x25_destroy_socket(sk);
407 * Handling for system calls applied via the various interfaces to a
408 * X.25 socket object.
411 static int x25_setsockopt(struct socket *sock, int level, int optname,
412 char __user *optval, int optlen)
415 struct sock *sk = sock->sk;
416 int rc = -ENOPROTOOPT;
418 if (level != SOL_X25 || optname != X25_QBITINCL)
422 if (optlen < sizeof(int))
426 if (get_user(opt, (int __user *)optval))
429 x25_sk(sk)->qbitincl = !!opt;
435 static int x25_getsockopt(struct socket *sock, int level, int optname,
436 char __user *optval, int __user *optlen)
438 struct sock *sk = sock->sk;
439 int val, len, rc = -ENOPROTOOPT;
441 if (level != SOL_X25 || optname != X25_QBITINCL)
445 if (get_user(len, optlen))
448 len = min_t(unsigned int, len, sizeof(int));
455 if (put_user(len, optlen))
458 val = x25_sk(sk)->qbitincl;
459 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
464 static int x25_listen(struct socket *sock, int backlog)
466 struct sock *sk = sock->sk;
467 int rc = -EOPNOTSUPP;
469 if (sk->sk_state != TCP_LISTEN) {
470 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
471 sk->sk_max_ack_backlog = backlog;
472 sk->sk_state = TCP_LISTEN;
479 static struct proto x25_proto = {
481 .owner = THIS_MODULE,
482 .obj_size = sizeof(struct x25_sock),
485 static struct sock *x25_alloc_socket(struct net *net)
487 struct x25_sock *x25;
488 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
493 sock_init_data(NULL, sk);
496 skb_queue_head_init(&x25->ack_queue);
497 skb_queue_head_init(&x25->fragment_queue);
498 skb_queue_head_init(&x25->interrupt_in_queue);
499 skb_queue_head_init(&x25->interrupt_out_queue);
504 static int x25_create(struct net *net, struct socket *sock, int protocol)
507 struct x25_sock *x25;
508 int rc = -ESOCKTNOSUPPORT;
510 if (net != &init_net)
511 return -EAFNOSUPPORT;
513 if (sock->type != SOCK_SEQPACKET || protocol)
517 if ((sk = x25_alloc_socket(net)) == NULL)
522 sock_init_data(sock, sk);
526 sock->ops = &x25_proto_ops;
527 sk->sk_protocol = protocol;
528 sk->sk_backlog_rcv = x25_backlog_rcv;
530 x25->t21 = sysctl_x25_call_request_timeout;
531 x25->t22 = sysctl_x25_reset_request_timeout;
532 x25->t23 = sysctl_x25_clear_request_timeout;
533 x25->t2 = sysctl_x25_ack_holdback_timeout;
534 x25->state = X25_STATE_0;
535 x25->cudmatchlength = 0;
536 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
539 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
540 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
541 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
542 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
543 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
544 x25->facilities.reverse = X25_DEFAULT_REVERSE;
545 x25->dte_facilities.calling_len = 0;
546 x25->dte_facilities.called_len = 0;
547 memset(x25->dte_facilities.called_ae, '\0',
548 sizeof(x25->dte_facilities.called_ae));
549 memset(x25->dte_facilities.calling_ae, '\0',
550 sizeof(x25->dte_facilities.calling_ae));
557 static struct sock *x25_make_new(struct sock *osk)
559 struct sock *sk = NULL;
560 struct x25_sock *x25, *ox25;
562 if (osk->sk_type != SOCK_SEQPACKET)
565 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
570 sk->sk_type = osk->sk_type;
571 sk->sk_priority = osk->sk_priority;
572 sk->sk_protocol = osk->sk_protocol;
573 sk->sk_rcvbuf = osk->sk_rcvbuf;
574 sk->sk_sndbuf = osk->sk_sndbuf;
575 sk->sk_state = TCP_ESTABLISHED;
576 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
577 sock_copy_flags(sk, osk);
580 x25->t21 = ox25->t21;
581 x25->t22 = ox25->t22;
582 x25->t23 = ox25->t23;
584 x25->facilities = ox25->facilities;
585 x25->qbitincl = ox25->qbitincl;
586 x25->dte_facilities = ox25->dte_facilities;
587 x25->cudmatchlength = ox25->cudmatchlength;
588 x25->accptapprv = ox25->accptapprv;
595 static int x25_release(struct socket *sock)
597 struct sock *sk = sock->sk;
598 struct x25_sock *x25;
605 switch (x25->state) {
609 x25_disconnect(sk, 0, 0, 0);
610 x25_destroy_socket(sk);
616 x25_clear_queues(sk);
617 x25_write_internal(sk, X25_CLEAR_REQUEST);
618 x25_start_t23timer(sk);
619 x25->state = X25_STATE_2;
620 sk->sk_state = TCP_CLOSE;
621 sk->sk_shutdown |= SEND_SHUTDOWN;
622 sk->sk_state_change(sk);
623 sock_set_flag(sk, SOCK_DEAD);
624 sock_set_flag(sk, SOCK_DESTROY);
633 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
635 struct sock *sk = sock->sk;
636 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
638 if (!sock_flag(sk, SOCK_ZAPPED) ||
639 addr_len != sizeof(struct sockaddr_x25) ||
640 addr->sx25_family != AF_X25)
643 x25_sk(sk)->source_addr = addr->sx25_addr;
644 x25_insert_socket(sk);
645 sock_reset_flag(sk, SOCK_ZAPPED);
646 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
651 static int x25_wait_for_connection_establishment(struct sock *sk)
653 DECLARE_WAITQUEUE(wait, current);
656 add_wait_queue_exclusive(sk->sk_sleep, &wait);
658 __set_current_state(TASK_INTERRUPTIBLE);
660 if (signal_pending(current))
664 sk->sk_socket->state = SS_UNCONNECTED;
668 if (sk->sk_state != TCP_ESTABLISHED) {
675 __set_current_state(TASK_RUNNING);
676 remove_wait_queue(sk->sk_sleep, &wait);
680 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
681 int addr_len, int flags)
683 struct sock *sk = sock->sk;
684 struct x25_sock *x25 = x25_sk(sk);
685 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
686 struct x25_route *rt;
690 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
691 sock->state = SS_CONNECTED;
692 goto out; /* Connect completed during a ERESTARTSYS event */
696 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
697 sock->state = SS_UNCONNECTED;
701 rc = -EISCONN; /* No reconnect on a seqpacket socket */
702 if (sk->sk_state == TCP_ESTABLISHED)
705 sk->sk_state = TCP_CLOSE;
706 sock->state = SS_UNCONNECTED;
709 if (addr_len != sizeof(struct sockaddr_x25) ||
710 addr->sx25_family != AF_X25)
714 rt = x25_get_route(&addr->sx25_addr);
718 x25->neighbour = x25_get_neigh(rt->dev);
722 x25_limit_facilities(&x25->facilities, x25->neighbour);
724 x25->lci = x25_new_lci(x25->neighbour);
729 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
732 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
733 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
735 x25->dest_addr = addr->sx25_addr;
737 /* Move to connecting socket, start sending Connect Requests */
738 sock->state = SS_CONNECTING;
739 sk->sk_state = TCP_SYN_SENT;
741 x25->state = X25_STATE_1;
743 x25_write_internal(sk, X25_CALL_REQUEST);
745 x25_start_heartbeat(sk);
746 x25_start_t21timer(sk);
750 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
753 rc = x25_wait_for_connection_establishment(sk);
757 sock->state = SS_CONNECTED;
761 x25_neigh_put(x25->neighbour);
769 static int x25_wait_for_data(struct sock *sk, long timeout)
771 DECLARE_WAITQUEUE(wait, current);
774 add_wait_queue_exclusive(sk->sk_sleep, &wait);
776 __set_current_state(TASK_INTERRUPTIBLE);
777 if (sk->sk_shutdown & RCV_SHUTDOWN)
780 if (signal_pending(current))
786 if (skb_queue_empty(&sk->sk_receive_queue)) {
788 timeout = schedule_timeout(timeout);
793 __set_current_state(TASK_RUNNING);
794 remove_wait_queue(sk->sk_sleep, &wait);
798 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
800 struct sock *sk = sock->sk;
805 if (!sk || sk->sk_state != TCP_LISTEN)
809 if (sk->sk_type != SOCK_SEQPACKET)
813 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
816 skb = skb_dequeue(&sk->sk_receive_queue);
821 sock_graft(newsk, newsock);
823 /* Now attach up the new socket */
826 sk->sk_ack_backlog--;
827 newsock->state = SS_CONNECTED;
835 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
836 int *uaddr_len, int peer)
838 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
839 struct sock *sk = sock->sk;
840 struct x25_sock *x25 = x25_sk(sk);
843 if (sk->sk_state != TCP_ESTABLISHED)
845 sx25->sx25_addr = x25->dest_addr;
847 sx25->sx25_addr = x25->source_addr;
849 sx25->sx25_family = AF_X25;
850 *uaddr_len = sizeof(*sx25);
855 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
860 struct x25_sock *makex25;
861 struct x25_address source_addr, dest_addr;
862 struct x25_facilities facilities;
863 struct x25_dte_facilities dte_facilities;
864 int len, addr_len, rc;
867 * Remove the LCI and frame type.
869 skb_pull(skb, X25_STD_MIN_LEN);
872 * Extract the X.25 addresses and convert them to ASCII strings,
875 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
876 skb_pull(skb, addr_len);
879 * Get the length of the facilities, skip past them for the moment
880 * get the call user data because this is needed to determine
881 * the correct listener
883 len = skb->data[0] + 1;
887 * Find a listener for the particular address/cud pair.
889 sk = x25_find_listener(&source_addr,skb);
892 if (sk != NULL && sk_acceptq_is_full(sk)) {
897 * We dont have any listeners for this incoming call.
901 skb_push(skb, addr_len + X25_STD_MIN_LEN);
902 if (sysctl_x25_forward &&
903 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
905 /* Call was forwarded, dont process it any more */
910 /* No listeners, can't forward, clear the call */
911 goto out_clear_request;
916 * Try to reach a compromise on the requested facilities.
918 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
923 * current neighbour/link might impose additional limits
924 * on certain facilties
927 x25_limit_facilities(&facilities, nb);
930 * Try to create a new socket.
932 make = x25_make_new(sk);
937 * Remove the facilities
942 make->sk_state = TCP_ESTABLISHED;
944 makex25 = x25_sk(make);
946 makex25->dest_addr = dest_addr;
947 makex25->source_addr = source_addr;
948 makex25->neighbour = nb;
949 makex25->facilities = facilities;
950 makex25->dte_facilities= dte_facilities;
951 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
952 /* ensure no reverse facil on accept */
953 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
954 /* ensure no calling address extension on accept */
955 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
956 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
958 /* Normally all calls are accepted immediatly */
959 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
960 x25_write_internal(make, X25_CALL_ACCEPTED);
961 makex25->state = X25_STATE_3;
965 * Incoming Call User Data.
968 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
969 makex25->calluserdata.cudlength = skb->len;
972 sk->sk_ack_backlog++;
974 x25_insert_socket(make);
976 skb_queue_head(&sk->sk_receive_queue, skb);
978 x25_start_heartbeat(make);
980 if (!sock_flag(sk, SOCK_DEAD))
981 sk->sk_data_ready(sk, skb->len);
990 x25_transmit_clear_request(nb, lci, 0x01);
994 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
995 struct msghdr *msg, size_t len)
997 struct sock *sk = sock->sk;
998 struct x25_sock *x25 = x25_sk(sk);
999 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1000 struct sockaddr_x25 sx25;
1001 struct sk_buff *skb;
1002 unsigned char *asmptr;
1003 int noblock = msg->msg_flags & MSG_DONTWAIT;
1005 int qbit = 0, rc = -EINVAL;
1007 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1010 /* we currently don't support segmented records at the user interface */
1011 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1014 rc = -EADDRNOTAVAIL;
1015 if (sock_flag(sk, SOCK_ZAPPED))
1019 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1020 send_sig(SIGPIPE, current, 0);
1025 if (!x25->neighbour)
1030 if (msg->msg_namelen < sizeof(sx25))
1032 memcpy(&sx25, usx25, sizeof(sx25));
1034 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1037 if (sx25.sx25_family != AF_X25)
1041 * FIXME 1003.1g - if the socket is like this because
1042 * it has become closed (not started closed) we ought
1043 * to SIGPIPE, EPIPE;
1046 if (sk->sk_state != TCP_ESTABLISHED)
1049 sx25.sx25_family = AF_X25;
1050 sx25.sx25_addr = x25->dest_addr;
1053 /* Sanity check the packet size */
1059 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1061 /* Build a packet */
1062 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1064 if ((msg->msg_flags & MSG_OOB) && len > 32)
1067 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1069 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1072 X25_SKB_CB(skb)->flags = msg->msg_flags;
1074 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1077 * Put the data on the end
1079 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1081 skb_reset_transport_header(skb);
1084 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1089 * If the Q BIT Include socket option is in force, the first
1090 * byte of the user data is the logical value of the Q Bit.
1092 if (x25->qbitincl) {
1093 qbit = skb->data[0];
1098 * Push down the X.25 header
1100 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1102 if (msg->msg_flags & MSG_OOB) {
1103 if (x25->neighbour->extended) {
1104 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1105 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1106 *asmptr++ = (x25->lci >> 0) & 0xFF;
1107 *asmptr++ = X25_INTERRUPT;
1109 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1110 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1111 *asmptr++ = (x25->lci >> 0) & 0xFF;
1112 *asmptr++ = X25_INTERRUPT;
1115 if (x25->neighbour->extended) {
1116 /* Build an Extended X.25 header */
1117 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1118 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1119 *asmptr++ = (x25->lci >> 0) & 0xFF;
1120 *asmptr++ = X25_DATA;
1121 *asmptr++ = X25_DATA;
1123 /* Build an Standard X.25 header */
1124 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1125 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1126 *asmptr++ = (x25->lci >> 0) & 0xFF;
1127 *asmptr++ = X25_DATA;
1131 skb->data[0] |= X25_Q_BIT;
1134 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1135 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1138 if (sk->sk_state != TCP_ESTABLISHED)
1141 if (msg->msg_flags & MSG_OOB)
1142 skb_queue_tail(&x25->interrupt_out_queue, skb);
1144 len = x25_output(sk, skb);
1147 else if (x25->qbitincl)
1152 * lock_sock() is currently only used to serialize this x25_kick()
1153 * against input-driven x25_kick() calls. It currently only blocks
1154 * incoming packets for this socket and does not protect against
1155 * any other socket state changes and is not called from anywhere
1156 * else. As x25_kick() cannot block and as long as all socket
1157 * operations are BKL-wrapped, we don't need take to care about
1158 * purging the backlog queue in x25_release().
1160 * Using lock_sock() to protect all socket operations entirely
1161 * (and making the whole x25 stack SMP aware) unfortunately would
1162 * require major changes to {send,recv}msg and skb allocation methods.
1177 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1178 struct msghdr *msg, size_t size,
1181 struct sock *sk = sock->sk;
1182 struct x25_sock *x25 = x25_sk(sk);
1183 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1186 struct sk_buff *skb;
1187 unsigned char *asmptr;
1191 * This works for seqpacket too. The receiver has ordered the queue for
1192 * us! We do one quick check first though
1194 if (sk->sk_state != TCP_ESTABLISHED)
1197 if (flags & MSG_OOB) {
1199 if (sock_flag(sk, SOCK_URGINLINE) ||
1200 !skb_peek(&x25->interrupt_in_queue))
1203 skb = skb_dequeue(&x25->interrupt_in_queue);
1205 skb_pull(skb, X25_STD_MIN_LEN);
1208 * No Q bit information on Interrupt data.
1210 if (x25->qbitincl) {
1211 asmptr = skb_push(skb, 1);
1215 msg->msg_flags |= MSG_OOB;
1217 /* Now we can treat all alike */
1218 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1219 flags & MSG_DONTWAIT, &rc);
1223 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1225 skb_pull(skb, x25->neighbour->extended ?
1226 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1228 if (x25->qbitincl) {
1229 asmptr = skb_push(skb, 1);
1234 skb_reset_transport_header(skb);
1237 if (copied > size) {
1239 msg->msg_flags |= MSG_TRUNC;
1242 /* Currently, each datagram always contains a complete record */
1243 msg->msg_flags |= MSG_EOR;
1245 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1247 goto out_free_dgram;
1250 sx25->sx25_family = AF_X25;
1251 sx25->sx25_addr = x25->dest_addr;
1254 msg->msg_namelen = sizeof(struct sockaddr_x25);
1261 skb_free_datagram(sk, skb);
1267 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1269 struct sock *sk = sock->sk;
1270 struct x25_sock *x25 = x25_sk(sk);
1271 void __user *argp = (void __user *)arg;
1276 int amount = sk->sk_sndbuf -
1277 atomic_read(&sk->sk_wmem_alloc);
1280 rc = put_user(amount, (unsigned int __user *)argp);
1285 struct sk_buff *skb;
1288 * These two are safe on a single CPU system as
1289 * only user tasks fiddle here
1291 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1293 rc = put_user(amount, (unsigned int __user *)argp);
1300 rc = sock_get_timestamp(sk,
1301 (struct timeval __user *)argp);
1306 rc = sock_get_timestampns(sk,
1307 (struct timespec __user *)argp);
1311 case SIOCGIFDSTADDR:
1312 case SIOCSIFDSTADDR:
1313 case SIOCGIFBRDADDR:
1314 case SIOCSIFBRDADDR:
1315 case SIOCGIFNETMASK:
1316 case SIOCSIFNETMASK:
1324 if (!capable(CAP_NET_ADMIN))
1326 rc = x25_route_ioctl(cmd, argp);
1328 case SIOCX25GSUBSCRIP:
1329 rc = x25_subscr_ioctl(cmd, argp);
1331 case SIOCX25SSUBSCRIP:
1333 if (!capable(CAP_NET_ADMIN))
1335 rc = x25_subscr_ioctl(cmd, argp);
1337 case SIOCX25GFACILITIES: {
1338 struct x25_facilities fac = x25->facilities;
1339 rc = copy_to_user(argp, &fac,
1340 sizeof(fac)) ? -EFAULT : 0;
1344 case SIOCX25SFACILITIES: {
1345 struct x25_facilities facilities;
1347 if (copy_from_user(&facilities, argp,
1348 sizeof(facilities)))
1351 if (sk->sk_state != TCP_LISTEN &&
1352 sk->sk_state != TCP_CLOSE)
1354 if (facilities.pacsize_in < X25_PS16 ||
1355 facilities.pacsize_in > X25_PS4096)
1357 if (facilities.pacsize_out < X25_PS16 ||
1358 facilities.pacsize_out > X25_PS4096)
1360 if (facilities.winsize_in < 1 ||
1361 facilities.winsize_in > 127)
1363 if (facilities.throughput < 0x03 ||
1364 facilities.throughput > 0xDD)
1366 if (facilities.reverse &&
1367 (facilities.reverse | 0x81)!= 0x81)
1369 x25->facilities = facilities;
1374 case SIOCX25GDTEFACILITIES: {
1375 rc = copy_to_user(argp, &x25->dte_facilities,
1376 sizeof(x25->dte_facilities));
1382 case SIOCX25SDTEFACILITIES: {
1383 struct x25_dte_facilities dtefacs;
1385 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1388 if (sk->sk_state != TCP_LISTEN &&
1389 sk->sk_state != TCP_CLOSE)
1391 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1393 if (dtefacs.calling_ae == NULL)
1395 if (dtefacs.called_len > X25_MAX_AE_LEN)
1397 if (dtefacs.called_ae == NULL)
1399 x25->dte_facilities = dtefacs;
1404 case SIOCX25GCALLUSERDATA: {
1405 struct x25_calluserdata cud = x25->calluserdata;
1406 rc = copy_to_user(argp, &cud,
1407 sizeof(cud)) ? -EFAULT : 0;
1411 case SIOCX25SCALLUSERDATA: {
1412 struct x25_calluserdata calluserdata;
1415 if (copy_from_user(&calluserdata, argp,
1416 sizeof(calluserdata)))
1419 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1421 x25->calluserdata = calluserdata;
1426 case SIOCX25GCAUSEDIAG: {
1427 struct x25_causediag causediag;
1428 causediag = x25->causediag;
1429 rc = copy_to_user(argp, &causediag,
1430 sizeof(causediag)) ? -EFAULT : 0;
1434 case SIOCX25SCUDMATCHLEN: {
1435 struct x25_subaddr sub_addr;
1437 if(sk->sk_state != TCP_CLOSE)
1440 if (copy_from_user(&sub_addr, argp,
1444 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1446 x25->cudmatchlength = sub_addr.cudmatchlength;
1451 case SIOCX25CALLACCPTAPPRV: {
1453 if (sk->sk_state != TCP_CLOSE)
1455 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1460 case SIOCX25SENDCALLACCPT: {
1462 if (sk->sk_state != TCP_ESTABLISHED)
1464 if (x25->accptapprv) /* must call accptapprv above */
1466 x25_write_internal(sk, X25_CALL_ACCEPTED);
1467 x25->state = X25_STATE_3;
1480 static struct net_proto_family x25_family_ops = {
1482 .create = x25_create,
1483 .owner = THIS_MODULE,
1486 #ifdef CONFIG_COMPAT
1487 static int compat_x25_subscr_ioctl(unsigned int cmd,
1488 struct compat_x25_subscrip_struct __user *x25_subscr32)
1490 struct compat_x25_subscrip_struct x25_subscr;
1491 struct x25_neigh *nb;
1492 struct net_device *dev;
1496 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1500 dev = x25_dev_get(x25_subscr.device);
1504 nb = x25_get_neigh(dev);
1510 if (cmd == SIOCX25GSUBSCRIP) {
1511 x25_subscr.extended = nb->extended;
1512 x25_subscr.global_facil_mask = nb->global_facil_mask;
1513 rc = copy_to_user(x25_subscr32, &x25_subscr,
1514 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1517 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1519 nb->extended = x25_subscr.extended;
1520 nb->global_facil_mask = x25_subscr.global_facil_mask;
1531 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1534 void __user *argp = compat_ptr(arg);
1535 struct sock *sk = sock->sk;
1537 int rc = -ENOIOCTLCMD;
1542 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1547 rc = compat_sock_get_timestamp(sk,
1548 (struct timeval __user*)argp);
1553 rc = compat_sock_get_timestampns(sk,
1554 (struct timespec __user*)argp);
1558 case SIOCGIFDSTADDR:
1559 case SIOCSIFDSTADDR:
1560 case SIOCGIFBRDADDR:
1561 case SIOCSIFBRDADDR:
1562 case SIOCGIFNETMASK:
1563 case SIOCSIFNETMASK:
1571 if (!capable(CAP_NET_ADMIN))
1573 rc = x25_route_ioctl(cmd, argp);
1575 case SIOCX25GSUBSCRIP:
1576 rc = compat_x25_subscr_ioctl(cmd, argp);
1578 case SIOCX25SSUBSCRIP:
1580 if (!capable(CAP_NET_ADMIN))
1582 rc = compat_x25_subscr_ioctl(cmd, argp);
1584 case SIOCX25GFACILITIES:
1585 case SIOCX25SFACILITIES:
1586 case SIOCX25GDTEFACILITIES:
1587 case SIOCX25SDTEFACILITIES:
1588 case SIOCX25GCALLUSERDATA:
1589 case SIOCX25SCALLUSERDATA:
1590 case SIOCX25GCAUSEDIAG:
1591 case SIOCX25SCUDMATCHLEN:
1592 case SIOCX25CALLACCPTAPPRV:
1593 case SIOCX25SENDCALLACCPT:
1594 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1604 static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1606 .owner = THIS_MODULE,
1607 .release = x25_release,
1609 .connect = x25_connect,
1610 .socketpair = sock_no_socketpair,
1611 .accept = x25_accept,
1612 .getname = x25_getname,
1613 .poll = datagram_poll,
1615 #ifdef CONFIG_COMPAT
1616 .compat_ioctl = compat_x25_ioctl,
1618 .listen = x25_listen,
1619 .shutdown = sock_no_shutdown,
1620 .setsockopt = x25_setsockopt,
1621 .getsockopt = x25_getsockopt,
1622 .sendmsg = x25_sendmsg,
1623 .recvmsg = x25_recvmsg,
1624 .mmap = sock_no_mmap,
1625 .sendpage = sock_no_sendpage,
1628 SOCKOPS_WRAP(x25_proto, AF_X25);
1630 static struct packet_type x25_packet_type = {
1631 .type = __constant_htons(ETH_P_X25),
1632 .func = x25_lapb_receive_frame,
1635 static struct notifier_block x25_dev_notifier = {
1636 .notifier_call = x25_device_event,
1639 void x25_kill_by_neigh(struct x25_neigh *nb)
1642 struct hlist_node *node;
1644 write_lock_bh(&x25_list_lock);
1646 sk_for_each(s, node, &x25_list)
1647 if (x25_sk(s)->neighbour == nb)
1648 x25_disconnect(s, ENETUNREACH, 0, 0);
1650 write_unlock_bh(&x25_list_lock);
1652 /* Remove any related forwards */
1653 x25_clear_forward_by_dev(nb->dev);
1656 static int __init x25_init(void)
1658 int rc = proto_register(&x25_proto, 0);
1663 sock_register(&x25_family_ops);
1665 dev_add_pack(&x25_packet_type);
1667 register_netdevice_notifier(&x25_dev_notifier);
1669 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
1671 #ifdef CONFIG_SYSCTL
1672 x25_register_sysctl();
1678 module_init(x25_init);
1680 static void __exit x25_exit(void)
1686 #ifdef CONFIG_SYSCTL
1687 x25_unregister_sysctl();
1690 unregister_netdevice_notifier(&x25_dev_notifier);
1692 dev_remove_pack(&x25_packet_type);
1694 sock_unregister(AF_X25);
1695 proto_unregister(&x25_proto);
1697 module_exit(x25_exit);
1699 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1700 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1701 MODULE_LICENSE("GPL");
1702 MODULE_ALIAS_NETPROTO(PF_X25);