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/smp_lock.h>
44 #include <linux/timer.h>
45 #include <linux/string.h>
46 #include <linux/net.h>
47 #include <linux/netdevice.h>
48 #include <linux/if_arp.h>
49 #include <linux/skbuff.h>
50 #include <linux/slab.h>
52 #include <net/tcp_states.h>
53 #include <asm/uaccess.h>
54 #include <linux/fcntl.h>
55 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
56 #include <linux/notifier.h>
57 #include <linux/init.h>
58 #include <linux/compat.h>
59 #include <linux/ctype.h>
62 #include <net/compat.h>
64 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
65 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
66 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
67 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
68 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
69 int sysctl_x25_forward = 0;
72 DEFINE_RWLOCK(x25_list_lock);
74 static const struct proto_ops x25_proto_ops;
76 static struct x25_address null_x25_address = {" "};
79 struct compat_x25_subscrip_struct {
80 char device[200-sizeof(compat_ulong_t)];
81 compat_ulong_t global_facil_mask;
82 compat_uint_t extended;
86 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
87 struct x25_address *calling_addr)
89 unsigned int called_len, calling_len;
90 char *called, *calling;
93 called_len = (*p >> 0) & 0x0F;
94 calling_len = (*p >> 4) & 0x0F;
96 called = called_addr->x25_addr;
97 calling = calling_addr->x25_addr;
100 for (i = 0; i < (called_len + calling_len); i++) {
101 if (i < called_len) {
103 *called++ = ((*p >> 0) & 0x0F) + '0';
106 *called++ = ((*p >> 4) & 0x0F) + '0';
110 *calling++ = ((*p >> 0) & 0x0F) + '0';
113 *calling++ = ((*p >> 4) & 0x0F) + '0';
118 *called = *calling = '\0';
120 return 1 + (called_len + calling_len + 1) / 2;
123 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
124 struct x25_address *calling_addr)
126 unsigned int called_len, calling_len;
127 char *called, *calling;
130 called = called_addr->x25_addr;
131 calling = calling_addr->x25_addr;
133 called_len = strlen(called);
134 calling_len = strlen(calling);
136 *p++ = (calling_len << 4) | (called_len << 0);
138 for (i = 0; i < (called_len + calling_len); i++) {
139 if (i < called_len) {
141 *p |= (*called++ - '0') << 0;
145 *p |= (*called++ - '0') << 4;
149 *p |= (*calling++ - '0') << 0;
153 *p |= (*calling++ - '0') << 4;
158 return 1 + (called_len + calling_len + 1) / 2;
162 * Socket removal during an interrupt is now safe.
164 static void x25_remove_socket(struct sock *sk)
166 write_lock_bh(&x25_list_lock);
167 sk_del_node_init(sk);
168 write_unlock_bh(&x25_list_lock);
172 * Kill all bound sockets on a dropped device.
174 static void x25_kill_by_device(struct net_device *dev)
177 struct hlist_node *node;
179 write_lock_bh(&x25_list_lock);
181 sk_for_each(s, node, &x25_list)
182 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
183 x25_disconnect(s, ENETUNREACH, 0, 0);
185 write_unlock_bh(&x25_list_lock);
189 * Handle device status changes.
191 static int x25_device_event(struct notifier_block *this, unsigned long event,
194 struct net_device *dev = ptr;
195 struct x25_neigh *nb;
197 if (!net_eq(dev_net(dev), &init_net))
200 if (dev->type == ARPHRD_X25
201 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
202 || dev->type == ARPHRD_ETHER
207 x25_link_device_up(dev);
209 case NETDEV_GOING_DOWN:
210 nb = x25_get_neigh(dev);
212 x25_terminate_link(nb);
217 x25_kill_by_device(dev);
218 x25_route_device_down(dev);
219 x25_link_device_down(dev);
228 * Add a socket to the bound sockets list.
230 static void x25_insert_socket(struct sock *sk)
232 write_lock_bh(&x25_list_lock);
233 sk_add_node(sk, &x25_list);
234 write_unlock_bh(&x25_list_lock);
238 * Find a socket that wants to accept the Call Request we just
239 * received. Check the full list for an address/cud match.
240 * If no cuds match return the next_best thing, an address match.
241 * Note: if a listening socket has cud set it must only get calls
244 static struct sock *x25_find_listener(struct x25_address *addr,
248 struct sock *next_best;
249 struct hlist_node *node;
251 read_lock_bh(&x25_list_lock);
254 sk_for_each(s, node, &x25_list)
255 if ((!strcmp(addr->x25_addr,
256 x25_sk(s)->source_addr.x25_addr) ||
257 !strcmp(addr->x25_addr,
258 null_x25_address.x25_addr)) &&
259 s->sk_state == TCP_LISTEN) {
261 * Found a listening socket, now check the incoming
262 * call user data vs this sockets call user data
264 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
265 if((memcmp(x25_sk(s)->calluserdata.cuddata,
267 x25_sk(s)->cudmatchlength)) == 0) {
281 read_unlock_bh(&x25_list_lock);
286 * Find a connected X.25 socket given my LCI and neighbour.
288 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
291 struct hlist_node *node;
293 sk_for_each(s, node, &x25_list)
294 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
303 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
307 read_lock_bh(&x25_list_lock);
308 s = __x25_find_socket(lci, nb);
309 read_unlock_bh(&x25_list_lock);
314 * Find a unique LCI for a given device.
316 static unsigned int x25_new_lci(struct x25_neigh *nb)
318 unsigned int lci = 1;
321 read_lock_bh(&x25_list_lock);
323 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
331 read_unlock_bh(&x25_list_lock);
338 static void __x25_destroy_socket(struct sock *);
341 * handler for deferred kills.
343 static void x25_destroy_timer(unsigned long data)
345 x25_destroy_socket_from_timer((struct sock *)data);
349 * This is called from user mode and the timers. Thus it protects itself
350 * against interrupt users but doesn't worry about being called during
351 * work. Once it is removed from the queue no interrupt or bottom half
352 * will touch it and we are (fairly 8-) ) safe.
353 * Not static as it's used by the timer
355 static void __x25_destroy_socket(struct sock *sk)
359 x25_stop_heartbeat(sk);
362 x25_remove_socket(sk);
363 x25_clear_queues(sk); /* Flush the queues */
365 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
366 if (skb->sk != sk) { /* A pending connection */
368 * Queue the unaccepted socket for death
370 sock_set_flag(skb->sk, SOCK_DEAD);
371 x25_start_heartbeat(skb->sk);
372 x25_sk(skb->sk)->state = X25_STATE_0;
378 if (sk_has_allocations(sk)) {
379 /* Defer: outstanding buffers */
380 sk->sk_timer.expires = jiffies + 10 * HZ;
381 sk->sk_timer.function = x25_destroy_timer;
382 sk->sk_timer.data = (unsigned long)sk;
383 add_timer(&sk->sk_timer);
385 /* drop last reference so sock_put will free */
390 void x25_destroy_socket_from_timer(struct sock *sk)
394 __x25_destroy_socket(sk);
399 static void x25_destroy_socket(struct sock *sk)
403 __x25_destroy_socket(sk);
409 * Handling for system calls applied via the various interfaces to a
410 * X.25 socket object.
413 static int x25_setsockopt(struct socket *sock, int level, int optname,
414 char __user *optval, unsigned int optlen)
417 struct sock *sk = sock->sk;
418 int rc = -ENOPROTOOPT;
421 if (level != SOL_X25 || optname != X25_QBITINCL)
425 if (optlen < sizeof(int))
429 if (get_user(opt, (int __user *)optval))
432 x25_sk(sk)->qbitincl = !!opt;
439 static int x25_getsockopt(struct socket *sock, int level, int optname,
440 char __user *optval, int __user *optlen)
442 struct sock *sk = sock->sk;
443 int val, len, rc = -ENOPROTOOPT;
446 if (level != SOL_X25 || optname != X25_QBITINCL)
450 if (get_user(len, optlen))
453 len = min_t(unsigned int, len, sizeof(int));
460 if (put_user(len, optlen))
463 val = x25_sk(sk)->qbitincl;
464 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
470 static int x25_listen(struct socket *sock, int backlog)
472 struct sock *sk = sock->sk;
473 int rc = -EOPNOTSUPP;
476 if (sk->sk_state != TCP_LISTEN) {
477 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
478 sk->sk_max_ack_backlog = backlog;
479 sk->sk_state = TCP_LISTEN;
487 static struct proto x25_proto = {
489 .owner = THIS_MODULE,
490 .obj_size = sizeof(struct x25_sock),
493 static struct sock *x25_alloc_socket(struct net *net)
495 struct x25_sock *x25;
496 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
501 sock_init_data(NULL, sk);
504 skb_queue_head_init(&x25->ack_queue);
505 skb_queue_head_init(&x25->fragment_queue);
506 skb_queue_head_init(&x25->interrupt_in_queue);
507 skb_queue_head_init(&x25->interrupt_out_queue);
512 static int x25_create(struct net *net, struct socket *sock, int protocol,
516 struct x25_sock *x25;
517 int rc = -EAFNOSUPPORT;
519 if (!net_eq(net, &init_net))
522 rc = -ESOCKTNOSUPPORT;
523 if (sock->type != SOCK_SEQPACKET)
531 if ((sk = x25_alloc_socket(net)) == NULL)
536 sock_init_data(sock, sk);
540 sock->ops = &x25_proto_ops;
541 sk->sk_protocol = protocol;
542 sk->sk_backlog_rcv = x25_backlog_rcv;
544 x25->t21 = sysctl_x25_call_request_timeout;
545 x25->t22 = sysctl_x25_reset_request_timeout;
546 x25->t23 = sysctl_x25_clear_request_timeout;
547 x25->t2 = sysctl_x25_ack_holdback_timeout;
548 x25->state = X25_STATE_0;
549 x25->cudmatchlength = 0;
550 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
553 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
554 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
555 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
556 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
557 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
558 x25->facilities.reverse = X25_DEFAULT_REVERSE;
559 x25->dte_facilities.calling_len = 0;
560 x25->dte_facilities.called_len = 0;
561 memset(x25->dte_facilities.called_ae, '\0',
562 sizeof(x25->dte_facilities.called_ae));
563 memset(x25->dte_facilities.calling_ae, '\0',
564 sizeof(x25->dte_facilities.calling_ae));
571 static struct sock *x25_make_new(struct sock *osk)
573 struct sock *sk = NULL;
574 struct x25_sock *x25, *ox25;
576 if (osk->sk_type != SOCK_SEQPACKET)
579 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
584 sk->sk_type = osk->sk_type;
585 sk->sk_priority = osk->sk_priority;
586 sk->sk_protocol = osk->sk_protocol;
587 sk->sk_rcvbuf = osk->sk_rcvbuf;
588 sk->sk_sndbuf = osk->sk_sndbuf;
589 sk->sk_state = TCP_ESTABLISHED;
590 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
591 sock_copy_flags(sk, osk);
594 x25->t21 = ox25->t21;
595 x25->t22 = ox25->t22;
596 x25->t23 = ox25->t23;
598 x25->facilities = ox25->facilities;
599 x25->qbitincl = ox25->qbitincl;
600 x25->dte_facilities = ox25->dte_facilities;
601 x25->cudmatchlength = ox25->cudmatchlength;
602 x25->accptapprv = ox25->accptapprv;
609 static int x25_release(struct socket *sock)
611 struct sock *sk = sock->sk;
612 struct x25_sock *x25;
620 switch (x25->state) {
624 x25_disconnect(sk, 0, 0, 0);
625 x25_destroy_socket(sk);
631 x25_clear_queues(sk);
632 x25_write_internal(sk, X25_CLEAR_REQUEST);
633 x25_start_t23timer(sk);
634 x25->state = X25_STATE_2;
635 sk->sk_state = TCP_CLOSE;
636 sk->sk_shutdown |= SEND_SHUTDOWN;
637 sk->sk_state_change(sk);
638 sock_set_flag(sk, SOCK_DEAD);
639 sock_set_flag(sk, SOCK_DESTROY);
649 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
651 struct sock *sk = sock->sk;
652 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
656 if (!sock_flag(sk, SOCK_ZAPPED) ||
657 addr_len != sizeof(struct sockaddr_x25) ||
658 addr->sx25_family != AF_X25) {
663 len = strlen(addr->sx25_addr.x25_addr);
664 for (i = 0; i < len; i++) {
665 if (!isdigit(addr->sx25_addr.x25_addr[i])) {
671 x25_sk(sk)->source_addr = addr->sx25_addr;
672 x25_insert_socket(sk);
673 sock_reset_flag(sk, SOCK_ZAPPED);
674 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
680 static int x25_wait_for_connection_establishment(struct sock *sk)
682 DECLARE_WAITQUEUE(wait, current);
685 add_wait_queue_exclusive(sk->sk_sleep, &wait);
687 __set_current_state(TASK_INTERRUPTIBLE);
689 if (signal_pending(current))
693 sk->sk_socket->state = SS_UNCONNECTED;
697 if (sk->sk_state != TCP_ESTABLISHED) {
704 __set_current_state(TASK_RUNNING);
705 remove_wait_queue(sk->sk_sleep, &wait);
709 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
710 int addr_len, int flags)
712 struct sock *sk = sock->sk;
713 struct x25_sock *x25 = x25_sk(sk);
714 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
715 struct x25_route *rt;
720 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
721 sock->state = SS_CONNECTED;
722 goto out; /* Connect completed during a ERESTARTSYS event */
726 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
727 sock->state = SS_UNCONNECTED;
731 rc = -EISCONN; /* No reconnect on a seqpacket socket */
732 if (sk->sk_state == TCP_ESTABLISHED)
735 sk->sk_state = TCP_CLOSE;
736 sock->state = SS_UNCONNECTED;
739 if (addr_len != sizeof(struct sockaddr_x25) ||
740 addr->sx25_family != AF_X25)
744 rt = x25_get_route(&addr->sx25_addr);
748 x25->neighbour = x25_get_neigh(rt->dev);
752 x25_limit_facilities(&x25->facilities, x25->neighbour);
754 x25->lci = x25_new_lci(x25->neighbour);
759 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
762 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
763 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
765 x25->dest_addr = addr->sx25_addr;
767 /* Move to connecting socket, start sending Connect Requests */
768 sock->state = SS_CONNECTING;
769 sk->sk_state = TCP_SYN_SENT;
771 x25->state = X25_STATE_1;
773 x25_write_internal(sk, X25_CALL_REQUEST);
775 x25_start_heartbeat(sk);
776 x25_start_t21timer(sk);
780 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
783 rc = x25_wait_for_connection_establishment(sk);
787 sock->state = SS_CONNECTED;
791 x25_neigh_put(x25->neighbour);
800 static int x25_wait_for_data(struct sock *sk, long timeout)
802 DECLARE_WAITQUEUE(wait, current);
805 add_wait_queue_exclusive(sk->sk_sleep, &wait);
807 __set_current_state(TASK_INTERRUPTIBLE);
808 if (sk->sk_shutdown & RCV_SHUTDOWN)
811 if (signal_pending(current))
817 if (skb_queue_empty(&sk->sk_receive_queue)) {
819 timeout = schedule_timeout(timeout);
824 __set_current_state(TASK_RUNNING);
825 remove_wait_queue(sk->sk_sleep, &wait);
829 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
831 struct sock *sk = sock->sk;
837 if (!sk || sk->sk_state != TCP_LISTEN)
841 if (sk->sk_type != SOCK_SEQPACKET)
845 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
848 skb = skb_dequeue(&sk->sk_receive_queue);
853 sock_graft(newsk, newsock);
855 /* Now attach up the new socket */
858 sk->sk_ack_backlog--;
859 newsock->state = SS_CONNECTED;
868 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
869 int *uaddr_len, int peer)
871 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
872 struct sock *sk = sock->sk;
873 struct x25_sock *x25 = x25_sk(sk);
878 if (sk->sk_state != TCP_ESTABLISHED) {
882 sx25->sx25_addr = x25->dest_addr;
884 sx25->sx25_addr = x25->source_addr;
886 sx25->sx25_family = AF_X25;
887 *uaddr_len = sizeof(*sx25);
894 static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
900 rc = datagram_poll(file, sock, wait);
906 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
911 struct x25_sock *makex25;
912 struct x25_address source_addr, dest_addr;
913 struct x25_facilities facilities;
914 struct x25_dte_facilities dte_facilities;
915 int len, addr_len, rc;
918 * Remove the LCI and frame type.
920 skb_pull(skb, X25_STD_MIN_LEN);
923 * Extract the X.25 addresses and convert them to ASCII strings,
926 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
927 skb_pull(skb, addr_len);
930 * Get the length of the facilities, skip past them for the moment
931 * get the call user data because this is needed to determine
932 * the correct listener
934 len = skb->data[0] + 1;
938 * Find a listener for the particular address/cud pair.
940 sk = x25_find_listener(&source_addr,skb);
943 if (sk != NULL && sk_acceptq_is_full(sk)) {
948 * We dont have any listeners for this incoming call.
952 skb_push(skb, addr_len + X25_STD_MIN_LEN);
953 if (sysctl_x25_forward &&
954 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
956 /* Call was forwarded, dont process it any more */
961 /* No listeners, can't forward, clear the call */
962 goto out_clear_request;
967 * Try to reach a compromise on the requested facilities.
969 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
974 * current neighbour/link might impose additional limits
975 * on certain facilties
978 x25_limit_facilities(&facilities, nb);
981 * Try to create a new socket.
983 make = x25_make_new(sk);
988 * Remove the facilities
993 make->sk_state = TCP_ESTABLISHED;
995 makex25 = x25_sk(make);
997 makex25->dest_addr = dest_addr;
998 makex25->source_addr = source_addr;
999 makex25->neighbour = nb;
1000 makex25->facilities = facilities;
1001 makex25->dte_facilities= dte_facilities;
1002 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1003 /* ensure no reverse facil on accept */
1004 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1005 /* ensure no calling address extension on accept */
1006 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1007 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
1009 /* Normally all calls are accepted immediatly */
1010 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1011 x25_write_internal(make, X25_CALL_ACCEPTED);
1012 makex25->state = X25_STATE_3;
1016 * Incoming Call User Data.
1018 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1019 makex25->calluserdata.cudlength = skb->len;
1021 sk->sk_ack_backlog++;
1023 x25_insert_socket(make);
1025 skb_queue_head(&sk->sk_receive_queue, skb);
1027 x25_start_heartbeat(make);
1029 if (!sock_flag(sk, SOCK_DEAD))
1030 sk->sk_data_ready(sk, skb->len);
1039 x25_transmit_clear_request(nb, lci, 0x01);
1043 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1044 struct msghdr *msg, size_t len)
1046 struct sock *sk = sock->sk;
1047 struct x25_sock *x25 = x25_sk(sk);
1048 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1049 struct sockaddr_x25 sx25;
1050 struct sk_buff *skb;
1051 unsigned char *asmptr;
1052 int noblock = msg->msg_flags & MSG_DONTWAIT;
1054 int qbit = 0, rc = -EINVAL;
1057 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1060 /* we currently don't support segmented records at the user interface */
1061 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1064 rc = -EADDRNOTAVAIL;
1065 if (sock_flag(sk, SOCK_ZAPPED))
1069 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1070 send_sig(SIGPIPE, current, 0);
1075 if (!x25->neighbour)
1080 if (msg->msg_namelen < sizeof(sx25))
1082 memcpy(&sx25, usx25, sizeof(sx25));
1084 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1087 if (sx25.sx25_family != AF_X25)
1091 * FIXME 1003.1g - if the socket is like this because
1092 * it has become closed (not started closed) we ought
1093 * to SIGPIPE, EPIPE;
1096 if (sk->sk_state != TCP_ESTABLISHED)
1099 sx25.sx25_family = AF_X25;
1100 sx25.sx25_addr = x25->dest_addr;
1103 /* Sanity check the packet size */
1109 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1111 /* Build a packet */
1112 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1114 if ((msg->msg_flags & MSG_OOB) && len > 32)
1117 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1119 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1122 X25_SKB_CB(skb)->flags = msg->msg_flags;
1124 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1127 * Put the data on the end
1129 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1131 skb_reset_transport_header(skb);
1134 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1139 * If the Q BIT Include socket option is in force, the first
1140 * byte of the user data is the logical value of the Q Bit.
1142 if (x25->qbitincl) {
1143 qbit = skb->data[0];
1148 * Push down the X.25 header
1150 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1152 if (msg->msg_flags & MSG_OOB) {
1153 if (x25->neighbour->extended) {
1154 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1155 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1156 *asmptr++ = (x25->lci >> 0) & 0xFF;
1157 *asmptr++ = X25_INTERRUPT;
1159 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1160 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1161 *asmptr++ = (x25->lci >> 0) & 0xFF;
1162 *asmptr++ = X25_INTERRUPT;
1165 if (x25->neighbour->extended) {
1166 /* Build an Extended X.25 header */
1167 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1168 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1169 *asmptr++ = (x25->lci >> 0) & 0xFF;
1170 *asmptr++ = X25_DATA;
1171 *asmptr++ = X25_DATA;
1173 /* Build an Standard X.25 header */
1174 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1175 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1176 *asmptr++ = (x25->lci >> 0) & 0xFF;
1177 *asmptr++ = X25_DATA;
1181 skb->data[0] |= X25_Q_BIT;
1184 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1185 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1188 if (sk->sk_state != TCP_ESTABLISHED)
1191 if (msg->msg_flags & MSG_OOB)
1192 skb_queue_tail(&x25->interrupt_out_queue, skb);
1194 rc = x25_output(sk, skb);
1198 else if (x25->qbitincl)
1203 * lock_sock() is currently only used to serialize this x25_kick()
1204 * against input-driven x25_kick() calls. It currently only blocks
1205 * incoming packets for this socket and does not protect against
1206 * any other socket state changes and is not called from anywhere
1207 * else. As x25_kick() cannot block and as long as all socket
1208 * operations are BKL-wrapped, we don't need take to care about
1209 * purging the backlog queue in x25_release().
1211 * Using lock_sock() to protect all socket operations entirely
1212 * (and making the whole x25 stack SMP aware) unfortunately would
1213 * require major changes to {send,recv}msg and skb allocation methods.
1229 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1230 struct msghdr *msg, size_t size,
1233 struct sock *sk = sock->sk;
1234 struct x25_sock *x25 = x25_sk(sk);
1235 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1238 struct sk_buff *skb;
1239 unsigned char *asmptr;
1244 * This works for seqpacket too. The receiver has ordered the queue for
1245 * us! We do one quick check first though
1247 if (sk->sk_state != TCP_ESTABLISHED)
1250 if (flags & MSG_OOB) {
1252 if (sock_flag(sk, SOCK_URGINLINE) ||
1253 !skb_peek(&x25->interrupt_in_queue))
1256 skb = skb_dequeue(&x25->interrupt_in_queue);
1258 skb_pull(skb, X25_STD_MIN_LEN);
1261 * No Q bit information on Interrupt data.
1263 if (x25->qbitincl) {
1264 asmptr = skb_push(skb, 1);
1268 msg->msg_flags |= MSG_OOB;
1270 /* Now we can treat all alike */
1271 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1272 flags & MSG_DONTWAIT, &rc);
1276 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1278 skb_pull(skb, x25->neighbour->extended ?
1279 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1281 if (x25->qbitincl) {
1282 asmptr = skb_push(skb, 1);
1287 skb_reset_transport_header(skb);
1290 if (copied > size) {
1292 msg->msg_flags |= MSG_TRUNC;
1295 /* Currently, each datagram always contains a complete record */
1296 msg->msg_flags |= MSG_EOR;
1298 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1300 goto out_free_dgram;
1303 sx25->sx25_family = AF_X25;
1304 sx25->sx25_addr = x25->dest_addr;
1307 msg->msg_namelen = sizeof(struct sockaddr_x25);
1314 skb_free_datagram(sk, skb);
1321 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1323 struct sock *sk = sock->sk;
1324 struct x25_sock *x25 = x25_sk(sk);
1325 void __user *argp = (void __user *)arg;
1331 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1335 rc = put_user(amount, (unsigned int __user *)argp);
1340 struct sk_buff *skb;
1343 * These two are safe on a single CPU system as
1344 * only user tasks fiddle here
1346 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1348 rc = put_user(amount, (unsigned int __user *)argp);
1355 rc = sock_get_timestamp(sk,
1356 (struct timeval __user *)argp);
1361 rc = sock_get_timestampns(sk,
1362 (struct timespec __user *)argp);
1366 case SIOCGIFDSTADDR:
1367 case SIOCSIFDSTADDR:
1368 case SIOCGIFBRDADDR:
1369 case SIOCSIFBRDADDR:
1370 case SIOCGIFNETMASK:
1371 case SIOCSIFNETMASK:
1379 if (!capable(CAP_NET_ADMIN))
1381 rc = x25_route_ioctl(cmd, argp);
1383 case SIOCX25GSUBSCRIP:
1384 rc = x25_subscr_ioctl(cmd, argp);
1386 case SIOCX25SSUBSCRIP:
1388 if (!capable(CAP_NET_ADMIN))
1390 rc = x25_subscr_ioctl(cmd, argp);
1392 case SIOCX25GFACILITIES: {
1393 struct x25_facilities fac = x25->facilities;
1394 rc = copy_to_user(argp, &fac,
1395 sizeof(fac)) ? -EFAULT : 0;
1399 case SIOCX25SFACILITIES: {
1400 struct x25_facilities facilities;
1402 if (copy_from_user(&facilities, argp,
1403 sizeof(facilities)))
1406 if (sk->sk_state != TCP_LISTEN &&
1407 sk->sk_state != TCP_CLOSE)
1409 if (facilities.pacsize_in < X25_PS16 ||
1410 facilities.pacsize_in > X25_PS4096)
1412 if (facilities.pacsize_out < X25_PS16 ||
1413 facilities.pacsize_out > X25_PS4096)
1415 if (facilities.winsize_in < 1 ||
1416 facilities.winsize_in > 127)
1418 if (facilities.throughput < 0x03 ||
1419 facilities.throughput > 0xDD)
1421 if (facilities.reverse &&
1422 (facilities.reverse & 0x81) != 0x81)
1424 x25->facilities = facilities;
1429 case SIOCX25GDTEFACILITIES: {
1430 rc = copy_to_user(argp, &x25->dte_facilities,
1431 sizeof(x25->dte_facilities));
1437 case SIOCX25SDTEFACILITIES: {
1438 struct x25_dte_facilities dtefacs;
1440 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1443 if (sk->sk_state != TCP_LISTEN &&
1444 sk->sk_state != TCP_CLOSE)
1446 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1448 if (dtefacs.calling_ae == NULL)
1450 if (dtefacs.called_len > X25_MAX_AE_LEN)
1452 if (dtefacs.called_ae == NULL)
1454 x25->dte_facilities = dtefacs;
1459 case SIOCX25GCALLUSERDATA: {
1460 struct x25_calluserdata cud = x25->calluserdata;
1461 rc = copy_to_user(argp, &cud,
1462 sizeof(cud)) ? -EFAULT : 0;
1466 case SIOCX25SCALLUSERDATA: {
1467 struct x25_calluserdata calluserdata;
1470 if (copy_from_user(&calluserdata, argp,
1471 sizeof(calluserdata)))
1474 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1476 x25->calluserdata = calluserdata;
1481 case SIOCX25GCAUSEDIAG: {
1482 struct x25_causediag causediag;
1483 causediag = x25->causediag;
1484 rc = copy_to_user(argp, &causediag,
1485 sizeof(causediag)) ? -EFAULT : 0;
1489 case SIOCX25SCAUSEDIAG: {
1490 struct x25_causediag causediag;
1492 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1494 x25->causediag = causediag;
1500 case SIOCX25SCUDMATCHLEN: {
1501 struct x25_subaddr sub_addr;
1503 if(sk->sk_state != TCP_CLOSE)
1506 if (copy_from_user(&sub_addr, argp,
1510 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1512 x25->cudmatchlength = sub_addr.cudmatchlength;
1517 case SIOCX25CALLACCPTAPPRV: {
1519 if (sk->sk_state != TCP_CLOSE)
1521 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1526 case SIOCX25SENDCALLACCPT: {
1528 if (sk->sk_state != TCP_ESTABLISHED)
1530 if (x25->accptapprv) /* must call accptapprv above */
1532 x25_write_internal(sk, X25_CALL_ACCEPTED);
1533 x25->state = X25_STATE_3;
1547 static const struct net_proto_family x25_family_ops = {
1549 .create = x25_create,
1550 .owner = THIS_MODULE,
1553 #ifdef CONFIG_COMPAT
1554 static int compat_x25_subscr_ioctl(unsigned int cmd,
1555 struct compat_x25_subscrip_struct __user *x25_subscr32)
1557 struct compat_x25_subscrip_struct x25_subscr;
1558 struct x25_neigh *nb;
1559 struct net_device *dev;
1563 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1567 dev = x25_dev_get(x25_subscr.device);
1571 nb = x25_get_neigh(dev);
1577 if (cmd == SIOCX25GSUBSCRIP) {
1578 x25_subscr.extended = nb->extended;
1579 x25_subscr.global_facil_mask = nb->global_facil_mask;
1580 rc = copy_to_user(x25_subscr32, &x25_subscr,
1581 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1584 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1586 nb->extended = x25_subscr.extended;
1587 nb->global_facil_mask = x25_subscr.global_facil_mask;
1598 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1601 void __user *argp = compat_ptr(arg);
1602 struct sock *sk = sock->sk;
1604 int rc = -ENOIOCTLCMD;
1609 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1615 rc = compat_sock_get_timestamp(sk,
1616 (struct timeval __user*)argp);
1623 rc = compat_sock_get_timestampns(sk,
1624 (struct timespec __user*)argp);
1629 case SIOCGIFDSTADDR:
1630 case SIOCSIFDSTADDR:
1631 case SIOCGIFBRDADDR:
1632 case SIOCSIFBRDADDR:
1633 case SIOCGIFNETMASK:
1634 case SIOCSIFNETMASK:
1642 if (!capable(CAP_NET_ADMIN))
1645 rc = x25_route_ioctl(cmd, argp);
1648 case SIOCX25GSUBSCRIP:
1650 rc = compat_x25_subscr_ioctl(cmd, argp);
1653 case SIOCX25SSUBSCRIP:
1655 if (!capable(CAP_NET_ADMIN))
1658 rc = compat_x25_subscr_ioctl(cmd, argp);
1661 case SIOCX25GFACILITIES:
1662 case SIOCX25SFACILITIES:
1663 case SIOCX25GDTEFACILITIES:
1664 case SIOCX25SDTEFACILITIES:
1665 case SIOCX25GCALLUSERDATA:
1666 case SIOCX25SCALLUSERDATA:
1667 case SIOCX25GCAUSEDIAG:
1668 case SIOCX25SCAUSEDIAG:
1669 case SIOCX25SCUDMATCHLEN:
1670 case SIOCX25CALLACCPTAPPRV:
1671 case SIOCX25SENDCALLACCPT:
1672 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1682 static const struct proto_ops x25_proto_ops = {
1684 .owner = THIS_MODULE,
1685 .release = x25_release,
1687 .connect = x25_connect,
1688 .socketpair = sock_no_socketpair,
1689 .accept = x25_accept,
1690 .getname = x25_getname,
1691 .poll = x25_datagram_poll,
1693 #ifdef CONFIG_COMPAT
1694 .compat_ioctl = compat_x25_ioctl,
1696 .listen = x25_listen,
1697 .shutdown = sock_no_shutdown,
1698 .setsockopt = x25_setsockopt,
1699 .getsockopt = x25_getsockopt,
1700 .sendmsg = x25_sendmsg,
1701 .recvmsg = x25_recvmsg,
1702 .mmap = sock_no_mmap,
1703 .sendpage = sock_no_sendpage,
1706 static struct packet_type x25_packet_type __read_mostly = {
1707 .type = cpu_to_be16(ETH_P_X25),
1708 .func = x25_lapb_receive_frame,
1711 static struct notifier_block x25_dev_notifier = {
1712 .notifier_call = x25_device_event,
1715 void x25_kill_by_neigh(struct x25_neigh *nb)
1718 struct hlist_node *node;
1720 write_lock_bh(&x25_list_lock);
1722 sk_for_each(s, node, &x25_list)
1723 if (x25_sk(s)->neighbour == nb)
1724 x25_disconnect(s, ENETUNREACH, 0, 0);
1726 write_unlock_bh(&x25_list_lock);
1728 /* Remove any related forwards */
1729 x25_clear_forward_by_dev(nb->dev);
1732 static int __init x25_init(void)
1734 int rc = proto_register(&x25_proto, 0);
1739 rc = sock_register(&x25_family_ops);
1743 dev_add_pack(&x25_packet_type);
1745 rc = register_netdevice_notifier(&x25_dev_notifier);
1749 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
1751 x25_register_sysctl();
1752 rc = x25_proc_init();
1758 unregister_netdevice_notifier(&x25_dev_notifier);
1760 sock_unregister(AF_X25);
1762 proto_unregister(&x25_proto);
1765 module_init(x25_init);
1767 static void __exit x25_exit(void)
1773 x25_unregister_sysctl();
1775 unregister_netdevice_notifier(&x25_dev_notifier);
1777 dev_remove_pack(&x25_packet_type);
1779 sock_unregister(AF_X25);
1780 proto_unregister(&x25_proto);
1782 module_exit(x25_exit);
1784 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1785 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1786 MODULE_LICENSE("GPL");
1787 MODULE_ALIAS_NETPROTO(PF_X25);