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>
51 #include <net/tcp_states.h>
52 #include <asm/uaccess.h>
53 #include <linux/fcntl.h>
54 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
55 #include <linux/notifier.h>
56 #include <linux/init.h>
57 #include <linux/compat.h>
60 #include <net/compat.h>
62 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
63 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
64 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
65 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
66 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
67 int sysctl_x25_forward = 0;
70 DEFINE_RWLOCK(x25_list_lock);
72 static const struct proto_ops x25_proto_ops;
74 static struct x25_address null_x25_address = {" "};
77 struct compat_x25_subscrip_struct {
78 char device[200-sizeof(compat_ulong_t)];
79 compat_ulong_t global_facil_mask;
80 compat_uint_t extended;
85 int x25_parse_address_block(struct sk_buff *skb,
86 struct x25_address *called_addr,
87 struct x25_address *calling_addr)
94 /* packet has no address block */
100 needed = 1 + (len >> 4) + (len & 0x0f);
102 if (skb->len < needed) {
103 /* packet is too short to hold the addresses it claims
109 return x25_addr_ntoa(skb->data, called_addr, calling_addr);
112 *called_addr->x25_addr = 0;
113 *calling_addr->x25_addr = 0;
119 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
120 struct x25_address *calling_addr)
122 unsigned int called_len, calling_len;
123 char *called, *calling;
126 called_len = (*p >> 0) & 0x0F;
127 calling_len = (*p >> 4) & 0x0F;
129 called = called_addr->x25_addr;
130 calling = calling_addr->x25_addr;
133 for (i = 0; i < (called_len + calling_len); i++) {
134 if (i < called_len) {
136 *called++ = ((*p >> 0) & 0x0F) + '0';
139 *called++ = ((*p >> 4) & 0x0F) + '0';
143 *calling++ = ((*p >> 0) & 0x0F) + '0';
146 *calling++ = ((*p >> 4) & 0x0F) + '0';
151 *called = *calling = '\0';
153 return 1 + (called_len + calling_len + 1) / 2;
156 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
157 struct x25_address *calling_addr)
159 unsigned int called_len, calling_len;
160 char *called, *calling;
163 called = called_addr->x25_addr;
164 calling = calling_addr->x25_addr;
166 called_len = strlen(called);
167 calling_len = strlen(calling);
169 *p++ = (calling_len << 4) | (called_len << 0);
171 for (i = 0; i < (called_len + calling_len); i++) {
172 if (i < called_len) {
174 *p |= (*called++ - '0') << 0;
178 *p |= (*called++ - '0') << 4;
182 *p |= (*calling++ - '0') << 0;
186 *p |= (*calling++ - '0') << 4;
191 return 1 + (called_len + calling_len + 1) / 2;
195 * Socket removal during an interrupt is now safe.
197 static void x25_remove_socket(struct sock *sk)
199 write_lock_bh(&x25_list_lock);
200 sk_del_node_init(sk);
201 write_unlock_bh(&x25_list_lock);
205 * Kill all bound sockets on a dropped device.
207 static void x25_kill_by_device(struct net_device *dev)
210 struct hlist_node *node;
212 write_lock_bh(&x25_list_lock);
214 sk_for_each(s, node, &x25_list)
215 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
216 x25_disconnect(s, ENETUNREACH, 0, 0);
218 write_unlock_bh(&x25_list_lock);
222 * Handle device status changes.
224 static int x25_device_event(struct notifier_block *this, unsigned long event,
227 struct net_device *dev = ptr;
228 struct x25_neigh *nb;
230 if (!net_eq(dev_net(dev), &init_net))
233 if (dev->type == ARPHRD_X25
234 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
235 || dev->type == ARPHRD_ETHER
240 x25_link_device_up(dev);
242 case NETDEV_GOING_DOWN:
243 nb = x25_get_neigh(dev);
245 x25_terminate_link(nb);
250 x25_kill_by_device(dev);
251 x25_route_device_down(dev);
252 x25_link_device_down(dev);
261 * Add a socket to the bound sockets list.
263 static void x25_insert_socket(struct sock *sk)
265 write_lock_bh(&x25_list_lock);
266 sk_add_node(sk, &x25_list);
267 write_unlock_bh(&x25_list_lock);
271 * Find a socket that wants to accept the Call Request we just
272 * received. Check the full list for an address/cud match.
273 * If no cuds match return the next_best thing, an address match.
274 * Note: if a listening socket has cud set it must only get calls
277 static struct sock *x25_find_listener(struct x25_address *addr,
281 struct sock *next_best;
282 struct hlist_node *node;
284 read_lock_bh(&x25_list_lock);
287 sk_for_each(s, node, &x25_list)
288 if ((!strcmp(addr->x25_addr,
289 x25_sk(s)->source_addr.x25_addr) ||
290 !strcmp(addr->x25_addr,
291 null_x25_address.x25_addr)) &&
292 s->sk_state == TCP_LISTEN) {
294 * Found a listening socket, now check the incoming
295 * call user data vs this sockets call user data
297 if (x25_sk(s)->cudmatchlength > 0 &&
298 skb->len >= x25_sk(s)->cudmatchlength) {
299 if((memcmp(x25_sk(s)->calluserdata.cuddata,
301 x25_sk(s)->cudmatchlength)) == 0) {
315 read_unlock_bh(&x25_list_lock);
320 * Find a connected X.25 socket given my LCI and neighbour.
322 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
325 struct hlist_node *node;
327 sk_for_each(s, node, &x25_list)
328 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
337 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
341 read_lock_bh(&x25_list_lock);
342 s = __x25_find_socket(lci, nb);
343 read_unlock_bh(&x25_list_lock);
348 * Find a unique LCI for a given device.
350 static unsigned int x25_new_lci(struct x25_neigh *nb)
352 unsigned int lci = 1;
355 read_lock_bh(&x25_list_lock);
357 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
365 read_unlock_bh(&x25_list_lock);
372 static void __x25_destroy_socket(struct sock *);
375 * handler for deferred kills.
377 static void x25_destroy_timer(unsigned long data)
379 x25_destroy_socket_from_timer((struct sock *)data);
383 * This is called from user mode and the timers. Thus it protects itself
384 * against interrupt users but doesn't worry about being called during
385 * work. Once it is removed from the queue no interrupt or bottom half
386 * will touch it and we are (fairly 8-) ) safe.
387 * Not static as it's used by the timer
389 static void __x25_destroy_socket(struct sock *sk)
393 x25_stop_heartbeat(sk);
396 x25_remove_socket(sk);
397 x25_clear_queues(sk); /* Flush the queues */
399 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
400 if (skb->sk != sk) { /* A pending connection */
402 * Queue the unaccepted socket for death
404 sock_set_flag(skb->sk, SOCK_DEAD);
405 x25_start_heartbeat(skb->sk);
406 x25_sk(skb->sk)->state = X25_STATE_0;
412 if (sk_has_allocations(sk)) {
413 /* Defer: outstanding buffers */
414 sk->sk_timer.expires = jiffies + 10 * HZ;
415 sk->sk_timer.function = x25_destroy_timer;
416 sk->sk_timer.data = (unsigned long)sk;
417 add_timer(&sk->sk_timer);
419 /* drop last reference so sock_put will free */
424 void x25_destroy_socket_from_timer(struct sock *sk)
428 __x25_destroy_socket(sk);
433 static void x25_destroy_socket(struct sock *sk)
437 __x25_destroy_socket(sk);
443 * Handling for system calls applied via the various interfaces to a
444 * X.25 socket object.
447 static int x25_setsockopt(struct socket *sock, int level, int optname,
448 char __user *optval, unsigned int optlen)
451 struct sock *sk = sock->sk;
452 int rc = -ENOPROTOOPT;
455 if (level != SOL_X25 || optname != X25_QBITINCL)
459 if (optlen < sizeof(int))
463 if (get_user(opt, (int __user *)optval))
466 x25_sk(sk)->qbitincl = !!opt;
473 static int x25_getsockopt(struct socket *sock, int level, int optname,
474 char __user *optval, int __user *optlen)
476 struct sock *sk = sock->sk;
477 int val, len, rc = -ENOPROTOOPT;
480 if (level != SOL_X25 || optname != X25_QBITINCL)
484 if (get_user(len, optlen))
487 len = min_t(unsigned int, len, sizeof(int));
494 if (put_user(len, optlen))
497 val = x25_sk(sk)->qbitincl;
498 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
504 static int x25_listen(struct socket *sock, int backlog)
506 struct sock *sk = sock->sk;
507 int rc = -EOPNOTSUPP;
510 if (sk->sk_state != TCP_LISTEN) {
511 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
512 sk->sk_max_ack_backlog = backlog;
513 sk->sk_state = TCP_LISTEN;
521 static struct proto x25_proto = {
523 .owner = THIS_MODULE,
524 .obj_size = sizeof(struct x25_sock),
527 static struct sock *x25_alloc_socket(struct net *net)
529 struct x25_sock *x25;
530 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
535 sock_init_data(NULL, sk);
538 skb_queue_head_init(&x25->ack_queue);
539 skb_queue_head_init(&x25->fragment_queue);
540 skb_queue_head_init(&x25->interrupt_in_queue);
541 skb_queue_head_init(&x25->interrupt_out_queue);
546 static int x25_create(struct net *net, struct socket *sock, int protocol,
550 struct x25_sock *x25;
551 int rc = -ESOCKTNOSUPPORT;
553 if (!net_eq(net, &init_net))
554 return -EAFNOSUPPORT;
556 if (sock->type != SOCK_SEQPACKET || protocol)
560 if ((sk = x25_alloc_socket(net)) == NULL)
565 sock_init_data(sock, sk);
569 sock->ops = &x25_proto_ops;
570 sk->sk_protocol = protocol;
571 sk->sk_backlog_rcv = x25_backlog_rcv;
573 x25->t21 = sysctl_x25_call_request_timeout;
574 x25->t22 = sysctl_x25_reset_request_timeout;
575 x25->t23 = sysctl_x25_clear_request_timeout;
576 x25->t2 = sysctl_x25_ack_holdback_timeout;
577 x25->state = X25_STATE_0;
578 x25->cudmatchlength = 0;
579 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
582 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
583 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
584 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
585 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
586 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
587 x25->facilities.reverse = X25_DEFAULT_REVERSE;
588 x25->dte_facilities.calling_len = 0;
589 x25->dte_facilities.called_len = 0;
590 memset(x25->dte_facilities.called_ae, '\0',
591 sizeof(x25->dte_facilities.called_ae));
592 memset(x25->dte_facilities.calling_ae, '\0',
593 sizeof(x25->dte_facilities.calling_ae));
600 static struct sock *x25_make_new(struct sock *osk)
602 struct sock *sk = NULL;
603 struct x25_sock *x25, *ox25;
605 if (osk->sk_type != SOCK_SEQPACKET)
608 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
613 sk->sk_type = osk->sk_type;
614 sk->sk_priority = osk->sk_priority;
615 sk->sk_protocol = osk->sk_protocol;
616 sk->sk_rcvbuf = osk->sk_rcvbuf;
617 sk->sk_sndbuf = osk->sk_sndbuf;
618 sk->sk_state = TCP_ESTABLISHED;
619 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
620 sock_copy_flags(sk, osk);
623 x25->t21 = ox25->t21;
624 x25->t22 = ox25->t22;
625 x25->t23 = ox25->t23;
627 x25->facilities = ox25->facilities;
628 x25->qbitincl = ox25->qbitincl;
629 x25->dte_facilities = ox25->dte_facilities;
630 x25->cudmatchlength = ox25->cudmatchlength;
631 x25->accptapprv = ox25->accptapprv;
638 static int x25_release(struct socket *sock)
640 struct sock *sk = sock->sk;
641 struct x25_sock *x25;
649 switch (x25->state) {
653 x25_disconnect(sk, 0, 0, 0);
654 x25_destroy_socket(sk);
660 x25_clear_queues(sk);
661 x25_write_internal(sk, X25_CLEAR_REQUEST);
662 x25_start_t23timer(sk);
663 x25->state = X25_STATE_2;
664 sk->sk_state = TCP_CLOSE;
665 sk->sk_shutdown |= SEND_SHUTDOWN;
666 sk->sk_state_change(sk);
667 sock_set_flag(sk, SOCK_DEAD);
668 sock_set_flag(sk, SOCK_DESTROY);
678 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
680 struct sock *sk = sock->sk;
681 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
685 if (!sock_flag(sk, SOCK_ZAPPED) ||
686 addr_len != sizeof(struct sockaddr_x25) ||
687 addr->sx25_family != AF_X25) {
692 x25_sk(sk)->source_addr = addr->sx25_addr;
693 x25_insert_socket(sk);
694 sock_reset_flag(sk, SOCK_ZAPPED);
695 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
701 static int x25_wait_for_connection_establishment(struct sock *sk)
703 DECLARE_WAITQUEUE(wait, current);
706 add_wait_queue_exclusive(sk->sk_sleep, &wait);
708 __set_current_state(TASK_INTERRUPTIBLE);
710 if (signal_pending(current))
714 sk->sk_socket->state = SS_UNCONNECTED;
718 if (sk->sk_state != TCP_ESTABLISHED) {
725 __set_current_state(TASK_RUNNING);
726 remove_wait_queue(sk->sk_sleep, &wait);
730 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
731 int addr_len, int flags)
733 struct sock *sk = sock->sk;
734 struct x25_sock *x25 = x25_sk(sk);
735 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
736 struct x25_route *rt;
741 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
742 sock->state = SS_CONNECTED;
743 goto out; /* Connect completed during a ERESTARTSYS event */
747 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
748 sock->state = SS_UNCONNECTED;
752 rc = -EISCONN; /* No reconnect on a seqpacket socket */
753 if (sk->sk_state == TCP_ESTABLISHED)
756 sk->sk_state = TCP_CLOSE;
757 sock->state = SS_UNCONNECTED;
760 if (addr_len != sizeof(struct sockaddr_x25) ||
761 addr->sx25_family != AF_X25)
765 rt = x25_get_route(&addr->sx25_addr);
769 x25->neighbour = x25_get_neigh(rt->dev);
773 x25_limit_facilities(&x25->facilities, x25->neighbour);
775 x25->lci = x25_new_lci(x25->neighbour);
780 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
783 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
784 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
786 x25->dest_addr = addr->sx25_addr;
788 /* Move to connecting socket, start sending Connect Requests */
789 sock->state = SS_CONNECTING;
790 sk->sk_state = TCP_SYN_SENT;
792 x25->state = X25_STATE_1;
794 x25_write_internal(sk, X25_CALL_REQUEST);
796 x25_start_heartbeat(sk);
797 x25_start_t21timer(sk);
801 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
804 rc = x25_wait_for_connection_establishment(sk);
808 sock->state = SS_CONNECTED;
812 x25_neigh_put(x25->neighbour);
821 static int x25_wait_for_data(struct sock *sk, long timeout)
823 DECLARE_WAITQUEUE(wait, current);
826 add_wait_queue_exclusive(sk->sk_sleep, &wait);
828 __set_current_state(TASK_INTERRUPTIBLE);
829 if (sk->sk_shutdown & RCV_SHUTDOWN)
832 if (signal_pending(current))
838 if (skb_queue_empty(&sk->sk_receive_queue)) {
840 timeout = schedule_timeout(timeout);
845 __set_current_state(TASK_RUNNING);
846 remove_wait_queue(sk->sk_sleep, &wait);
850 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
852 struct sock *sk = sock->sk;
858 if (!sk || sk->sk_state != TCP_LISTEN)
862 if (sk->sk_type != SOCK_SEQPACKET)
866 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
869 skb = skb_dequeue(&sk->sk_receive_queue);
874 sock_graft(newsk, newsock);
876 /* Now attach up the new socket */
879 sk->sk_ack_backlog--;
880 newsock->state = SS_CONNECTED;
889 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
890 int *uaddr_len, int peer)
892 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
893 struct sock *sk = sock->sk;
894 struct x25_sock *x25 = x25_sk(sk);
899 if (sk->sk_state != TCP_ESTABLISHED) {
903 sx25->sx25_addr = x25->dest_addr;
905 sx25->sx25_addr = x25->source_addr;
907 sx25->sx25_family = AF_X25;
908 *uaddr_len = sizeof(*sx25);
915 static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
921 rc = datagram_poll(file, sock, wait);
927 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
932 struct x25_sock *makex25;
933 struct x25_address source_addr, dest_addr;
934 struct x25_facilities facilities;
935 struct x25_dte_facilities dte_facilities;
936 int len, addr_len, rc;
939 * Remove the LCI and frame type.
941 skb_pull(skb, X25_STD_MIN_LEN);
944 * Extract the X.25 addresses and convert them to ASCII strings,
947 * Address block is mandatory in call request packets
949 addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
951 goto out_clear_request;
952 skb_pull(skb, addr_len);
955 * Get the length of the facilities, skip past them for the moment
956 * get the call user data because this is needed to determine
957 * the correct listener
959 * Facilities length is mandatory in call request packets
962 goto out_clear_request;
963 len = skb->data[0] + 1;
965 goto out_clear_request;
969 * Find a listener for the particular address/cud pair.
971 sk = x25_find_listener(&source_addr,skb);
974 if (sk != NULL && sk_acceptq_is_full(sk)) {
979 * We dont have any listeners for this incoming call.
983 skb_push(skb, addr_len + X25_STD_MIN_LEN);
984 if (sysctl_x25_forward &&
985 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
987 /* Call was forwarded, dont process it any more */
992 /* No listeners, can't forward, clear the call */
993 goto out_clear_request;
998 * Try to reach a compromise on the requested facilities.
1000 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1005 * current neighbour/link might impose additional limits
1006 * on certain facilties
1009 x25_limit_facilities(&facilities, nb);
1012 * Try to create a new socket.
1014 make = x25_make_new(sk);
1019 * Remove the facilities
1024 make->sk_state = TCP_ESTABLISHED;
1026 makex25 = x25_sk(make);
1028 makex25->dest_addr = dest_addr;
1029 makex25->source_addr = source_addr;
1030 makex25->neighbour = nb;
1031 makex25->facilities = facilities;
1032 makex25->dte_facilities= dte_facilities;
1033 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1034 /* ensure no reverse facil on accept */
1035 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1036 /* ensure no calling address extension on accept */
1037 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1038 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
1040 /* Normally all calls are accepted immediatly */
1041 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
1042 x25_write_internal(make, X25_CALL_ACCEPTED);
1043 makex25->state = X25_STATE_3;
1047 * Incoming Call User Data.
1049 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1050 makex25->calluserdata.cudlength = skb->len;
1052 sk->sk_ack_backlog++;
1054 x25_insert_socket(make);
1056 skb_queue_head(&sk->sk_receive_queue, skb);
1058 x25_start_heartbeat(make);
1060 if (!sock_flag(sk, SOCK_DEAD))
1061 sk->sk_data_ready(sk, skb->len);
1070 x25_transmit_clear_request(nb, lci, 0x01);
1074 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
1075 struct msghdr *msg, size_t len)
1077 struct sock *sk = sock->sk;
1078 struct x25_sock *x25 = x25_sk(sk);
1079 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
1080 struct sockaddr_x25 sx25;
1081 struct sk_buff *skb;
1082 unsigned char *asmptr;
1083 int noblock = msg->msg_flags & MSG_DONTWAIT;
1085 int qbit = 0, rc = -EINVAL;
1088 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
1091 /* we currently don't support segmented records at the user interface */
1092 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1095 rc = -EADDRNOTAVAIL;
1096 if (sock_flag(sk, SOCK_ZAPPED))
1100 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1101 send_sig(SIGPIPE, current, 0);
1106 if (!x25->neighbour)
1111 if (msg->msg_namelen < sizeof(sx25))
1113 memcpy(&sx25, usx25, sizeof(sx25));
1115 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1118 if (sx25.sx25_family != AF_X25)
1122 * FIXME 1003.1g - if the socket is like this because
1123 * it has become closed (not started closed) we ought
1124 * to SIGPIPE, EPIPE;
1127 if (sk->sk_state != TCP_ESTABLISHED)
1130 sx25.sx25_family = AF_X25;
1131 sx25.sx25_addr = x25->dest_addr;
1134 /* Sanity check the packet size */
1140 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1142 /* Build a packet */
1143 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1145 if ((msg->msg_flags & MSG_OOB) && len > 32)
1148 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1150 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1153 X25_SKB_CB(skb)->flags = msg->msg_flags;
1155 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1158 * Put the data on the end
1160 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1162 skb_reset_transport_header(skb);
1165 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1170 * If the Q BIT Include socket option is in force, the first
1171 * byte of the user data is the logical value of the Q Bit.
1173 if (x25->qbitincl) {
1174 qbit = skb->data[0];
1179 * Push down the X.25 header
1181 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1183 if (msg->msg_flags & MSG_OOB) {
1184 if (x25->neighbour->extended) {
1185 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1186 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1187 *asmptr++ = (x25->lci >> 0) & 0xFF;
1188 *asmptr++ = X25_INTERRUPT;
1190 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1191 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1192 *asmptr++ = (x25->lci >> 0) & 0xFF;
1193 *asmptr++ = X25_INTERRUPT;
1196 if (x25->neighbour->extended) {
1197 /* Build an Extended X.25 header */
1198 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1199 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1200 *asmptr++ = (x25->lci >> 0) & 0xFF;
1201 *asmptr++ = X25_DATA;
1202 *asmptr++ = X25_DATA;
1204 /* Build an Standard X.25 header */
1205 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1206 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1207 *asmptr++ = (x25->lci >> 0) & 0xFF;
1208 *asmptr++ = X25_DATA;
1212 skb->data[0] |= X25_Q_BIT;
1215 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1216 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1219 if (sk->sk_state != TCP_ESTABLISHED)
1222 if (msg->msg_flags & MSG_OOB)
1223 skb_queue_tail(&x25->interrupt_out_queue, skb);
1225 rc = x25_output(sk, skb);
1229 else if (x25->qbitincl)
1234 * lock_sock() is currently only used to serialize this x25_kick()
1235 * against input-driven x25_kick() calls. It currently only blocks
1236 * incoming packets for this socket and does not protect against
1237 * any other socket state changes and is not called from anywhere
1238 * else. As x25_kick() cannot block and as long as all socket
1239 * operations are BKL-wrapped, we don't need take to care about
1240 * purging the backlog queue in x25_release().
1242 * Using lock_sock() to protect all socket operations entirely
1243 * (and making the whole x25 stack SMP aware) unfortunately would
1244 * require major changes to {send,recv}msg and skb allocation methods.
1260 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1261 struct msghdr *msg, size_t size,
1264 struct sock *sk = sock->sk;
1265 struct x25_sock *x25 = x25_sk(sk);
1266 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1269 struct sk_buff *skb;
1270 unsigned char *asmptr;
1275 * This works for seqpacket too. The receiver has ordered the queue for
1276 * us! We do one quick check first though
1278 if (sk->sk_state != TCP_ESTABLISHED)
1281 if (flags & MSG_OOB) {
1283 if (sock_flag(sk, SOCK_URGINLINE) ||
1284 !skb_peek(&x25->interrupt_in_queue))
1287 skb = skb_dequeue(&x25->interrupt_in_queue);
1289 skb_pull(skb, X25_STD_MIN_LEN);
1292 * No Q bit information on Interrupt data.
1294 if (x25->qbitincl) {
1295 asmptr = skb_push(skb, 1);
1299 msg->msg_flags |= MSG_OOB;
1301 /* Now we can treat all alike */
1302 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1303 flags & MSG_DONTWAIT, &rc);
1307 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1309 skb_pull(skb, x25->neighbour->extended ?
1310 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1312 if (x25->qbitincl) {
1313 asmptr = skb_push(skb, 1);
1318 skb_reset_transport_header(skb);
1321 if (copied > size) {
1323 msg->msg_flags |= MSG_TRUNC;
1326 /* Currently, each datagram always contains a complete record */
1327 msg->msg_flags |= MSG_EOR;
1329 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1331 goto out_free_dgram;
1334 sx25->sx25_family = AF_X25;
1335 sx25->sx25_addr = x25->dest_addr;
1338 msg->msg_namelen = sizeof(struct sockaddr_x25);
1345 skb_free_datagram(sk, skb);
1352 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1354 struct sock *sk = sock->sk;
1355 struct x25_sock *x25 = x25_sk(sk);
1356 void __user *argp = (void __user *)arg;
1362 int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
1366 rc = put_user(amount, (unsigned int __user *)argp);
1371 struct sk_buff *skb;
1374 * These two are safe on a single CPU system as
1375 * only user tasks fiddle here
1377 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1379 rc = put_user(amount, (unsigned int __user *)argp);
1386 rc = sock_get_timestamp(sk,
1387 (struct timeval __user *)argp);
1392 rc = sock_get_timestampns(sk,
1393 (struct timespec __user *)argp);
1397 case SIOCGIFDSTADDR:
1398 case SIOCSIFDSTADDR:
1399 case SIOCGIFBRDADDR:
1400 case SIOCSIFBRDADDR:
1401 case SIOCGIFNETMASK:
1402 case SIOCSIFNETMASK:
1410 if (!capable(CAP_NET_ADMIN))
1412 rc = x25_route_ioctl(cmd, argp);
1414 case SIOCX25GSUBSCRIP:
1415 rc = x25_subscr_ioctl(cmd, argp);
1417 case SIOCX25SSUBSCRIP:
1419 if (!capable(CAP_NET_ADMIN))
1421 rc = x25_subscr_ioctl(cmd, argp);
1423 case SIOCX25GFACILITIES: {
1424 struct x25_facilities fac = x25->facilities;
1425 rc = copy_to_user(argp, &fac,
1426 sizeof(fac)) ? -EFAULT : 0;
1430 case SIOCX25SFACILITIES: {
1431 struct x25_facilities facilities;
1433 if (copy_from_user(&facilities, argp,
1434 sizeof(facilities)))
1437 if (sk->sk_state != TCP_LISTEN &&
1438 sk->sk_state != TCP_CLOSE)
1440 if (facilities.pacsize_in < X25_PS16 ||
1441 facilities.pacsize_in > X25_PS4096)
1443 if (facilities.pacsize_out < X25_PS16 ||
1444 facilities.pacsize_out > X25_PS4096)
1446 if (facilities.winsize_in < 1 ||
1447 facilities.winsize_in > 127)
1449 if (facilities.throughput < 0x03 ||
1450 facilities.throughput > 0xDD)
1452 if (facilities.reverse &&
1453 (facilities.reverse & 0x81) != 0x81)
1455 x25->facilities = facilities;
1460 case SIOCX25GDTEFACILITIES: {
1461 rc = copy_to_user(argp, &x25->dte_facilities,
1462 sizeof(x25->dte_facilities));
1468 case SIOCX25SDTEFACILITIES: {
1469 struct x25_dte_facilities dtefacs;
1471 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1474 if (sk->sk_state != TCP_LISTEN &&
1475 sk->sk_state != TCP_CLOSE)
1477 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1479 if (dtefacs.calling_ae == NULL)
1481 if (dtefacs.called_len > X25_MAX_AE_LEN)
1483 if (dtefacs.called_ae == NULL)
1485 x25->dte_facilities = dtefacs;
1490 case SIOCX25GCALLUSERDATA: {
1491 struct x25_calluserdata cud = x25->calluserdata;
1492 rc = copy_to_user(argp, &cud,
1493 sizeof(cud)) ? -EFAULT : 0;
1497 case SIOCX25SCALLUSERDATA: {
1498 struct x25_calluserdata calluserdata;
1501 if (copy_from_user(&calluserdata, argp,
1502 sizeof(calluserdata)))
1505 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1507 x25->calluserdata = calluserdata;
1512 case SIOCX25GCAUSEDIAG: {
1513 struct x25_causediag causediag;
1514 causediag = x25->causediag;
1515 rc = copy_to_user(argp, &causediag,
1516 sizeof(causediag)) ? -EFAULT : 0;
1520 case SIOCX25SCAUSEDIAG: {
1521 struct x25_causediag causediag;
1523 if (copy_from_user(&causediag, argp, sizeof(causediag)))
1525 x25->causediag = causediag;
1531 case SIOCX25SCUDMATCHLEN: {
1532 struct x25_subaddr sub_addr;
1534 if(sk->sk_state != TCP_CLOSE)
1537 if (copy_from_user(&sub_addr, argp,
1541 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1543 x25->cudmatchlength = sub_addr.cudmatchlength;
1548 case SIOCX25CALLACCPTAPPRV: {
1550 if (sk->sk_state != TCP_CLOSE)
1552 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1557 case SIOCX25SENDCALLACCPT: {
1559 if (sk->sk_state != TCP_ESTABLISHED)
1561 if (x25->accptapprv) /* must call accptapprv above */
1563 x25_write_internal(sk, X25_CALL_ACCEPTED);
1564 x25->state = X25_STATE_3;
1578 static const struct net_proto_family x25_family_ops = {
1580 .create = x25_create,
1581 .owner = THIS_MODULE,
1584 #ifdef CONFIG_COMPAT
1585 static int compat_x25_subscr_ioctl(unsigned int cmd,
1586 struct compat_x25_subscrip_struct __user *x25_subscr32)
1588 struct compat_x25_subscrip_struct x25_subscr;
1589 struct x25_neigh *nb;
1590 struct net_device *dev;
1594 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1598 dev = x25_dev_get(x25_subscr.device);
1602 nb = x25_get_neigh(dev);
1608 if (cmd == SIOCX25GSUBSCRIP) {
1609 x25_subscr.extended = nb->extended;
1610 x25_subscr.global_facil_mask = nb->global_facil_mask;
1611 rc = copy_to_user(x25_subscr32, &x25_subscr,
1612 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1615 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1617 nb->extended = x25_subscr.extended;
1618 nb->global_facil_mask = x25_subscr.global_facil_mask;
1629 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1632 void __user *argp = compat_ptr(arg);
1633 struct sock *sk = sock->sk;
1635 int rc = -ENOIOCTLCMD;
1640 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1646 rc = compat_sock_get_timestamp(sk,
1647 (struct timeval __user*)argp);
1654 rc = compat_sock_get_timestampns(sk,
1655 (struct timespec __user*)argp);
1660 case SIOCGIFDSTADDR:
1661 case SIOCSIFDSTADDR:
1662 case SIOCGIFBRDADDR:
1663 case SIOCSIFBRDADDR:
1664 case SIOCGIFNETMASK:
1665 case SIOCSIFNETMASK:
1673 if (!capable(CAP_NET_ADMIN))
1676 rc = x25_route_ioctl(cmd, argp);
1679 case SIOCX25GSUBSCRIP:
1681 rc = compat_x25_subscr_ioctl(cmd, argp);
1684 case SIOCX25SSUBSCRIP:
1686 if (!capable(CAP_NET_ADMIN))
1689 rc = compat_x25_subscr_ioctl(cmd, argp);
1692 case SIOCX25GFACILITIES:
1693 case SIOCX25SFACILITIES:
1694 case SIOCX25GDTEFACILITIES:
1695 case SIOCX25SDTEFACILITIES:
1696 case SIOCX25GCALLUSERDATA:
1697 case SIOCX25SCALLUSERDATA:
1698 case SIOCX25GCAUSEDIAG:
1699 case SIOCX25SCAUSEDIAG:
1700 case SIOCX25SCUDMATCHLEN:
1701 case SIOCX25CALLACCPTAPPRV:
1702 case SIOCX25SENDCALLACCPT:
1703 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1713 static const struct proto_ops x25_proto_ops = {
1715 .owner = THIS_MODULE,
1716 .release = x25_release,
1718 .connect = x25_connect,
1719 .socketpair = sock_no_socketpair,
1720 .accept = x25_accept,
1721 .getname = x25_getname,
1722 .poll = x25_datagram_poll,
1724 #ifdef CONFIG_COMPAT
1725 .compat_ioctl = compat_x25_ioctl,
1727 .listen = x25_listen,
1728 .shutdown = sock_no_shutdown,
1729 .setsockopt = x25_setsockopt,
1730 .getsockopt = x25_getsockopt,
1731 .sendmsg = x25_sendmsg,
1732 .recvmsg = x25_recvmsg,
1733 .mmap = sock_no_mmap,
1734 .sendpage = sock_no_sendpage,
1737 static struct packet_type x25_packet_type __read_mostly = {
1738 .type = cpu_to_be16(ETH_P_X25),
1739 .func = x25_lapb_receive_frame,
1742 static struct notifier_block x25_dev_notifier = {
1743 .notifier_call = x25_device_event,
1746 void x25_kill_by_neigh(struct x25_neigh *nb)
1749 struct hlist_node *node;
1751 write_lock_bh(&x25_list_lock);
1753 sk_for_each(s, node, &x25_list)
1754 if (x25_sk(s)->neighbour == nb)
1755 x25_disconnect(s, ENETUNREACH, 0, 0);
1757 write_unlock_bh(&x25_list_lock);
1759 /* Remove any related forwards */
1760 x25_clear_forward_by_dev(nb->dev);
1763 static int __init x25_init(void)
1765 int rc = proto_register(&x25_proto, 0);
1770 rc = sock_register(&x25_family_ops);
1774 dev_add_pack(&x25_packet_type);
1776 rc = register_netdevice_notifier(&x25_dev_notifier);
1780 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
1782 x25_register_sysctl();
1783 rc = x25_proc_init();
1789 unregister_netdevice_notifier(&x25_dev_notifier);
1791 sock_unregister(AF_X25);
1793 proto_unregister(&x25_proto);
1796 module_init(x25_init);
1798 static void __exit x25_exit(void)
1804 x25_unregister_sysctl();
1806 unregister_netdevice_notifier(&x25_dev_notifier);
1808 dev_remove_pack(&x25_packet_type);
1810 sock_unregister(AF_X25);
1811 proto_unregister(&x25_proto);
1813 module_exit(x25_exit);
1815 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1816 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1817 MODULE_LICENSE("GPL");
1818 MODULE_ALIAS_NETPROTO(PF_X25);