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 (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 void x25_destroy_socket(struct sock *);
338 * handler for deferred kills.
340 static void x25_destroy_timer(unsigned long data)
342 x25_destroy_socket((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 void x25_destroy_socket(struct sock *sk)
358 x25_stop_heartbeat(sk);
361 x25_remove_socket(sk);
362 x25_clear_queues(sk); /* Flush the queues */
364 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
365 if (skb->sk != sk) { /* A pending connection */
367 * Queue the unaccepted socket for death
369 sock_set_flag(skb->sk, SOCK_DEAD);
370 x25_start_heartbeat(skb->sk);
371 x25_sk(skb->sk)->state = X25_STATE_0;
377 if (atomic_read(&sk->sk_wmem_alloc) ||
378 atomic_read(&sk->sk_rmem_alloc)) {
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 */
394 * Handling for system calls applied via the various interfaces to a
395 * X.25 socket object.
398 static int x25_setsockopt(struct socket *sock, int level, int optname,
399 char __user *optval, int optlen)
402 struct sock *sk = sock->sk;
403 int rc = -ENOPROTOOPT;
405 if (level != SOL_X25 || optname != X25_QBITINCL)
409 if (optlen < sizeof(int))
413 if (get_user(opt, (int __user *)optval))
416 x25_sk(sk)->qbitincl = !!opt;
422 static int x25_getsockopt(struct socket *sock, int level, int optname,
423 char __user *optval, int __user *optlen)
425 struct sock *sk = sock->sk;
426 int val, len, rc = -ENOPROTOOPT;
428 if (level != SOL_X25 || optname != X25_QBITINCL)
432 if (get_user(len, optlen))
435 len = min_t(unsigned int, len, sizeof(int));
442 if (put_user(len, optlen))
445 val = x25_sk(sk)->qbitincl;
446 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
451 static int x25_listen(struct socket *sock, int backlog)
453 struct sock *sk = sock->sk;
454 int rc = -EOPNOTSUPP;
456 if (sk->sk_state != TCP_LISTEN) {
457 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
458 sk->sk_max_ack_backlog = backlog;
459 sk->sk_state = TCP_LISTEN;
466 static struct proto x25_proto = {
468 .owner = THIS_MODULE,
469 .obj_size = sizeof(struct x25_sock),
472 static struct sock *x25_alloc_socket(struct net *net)
474 struct x25_sock *x25;
475 struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
480 sock_init_data(NULL, sk);
483 skb_queue_head_init(&x25->ack_queue);
484 skb_queue_head_init(&x25->fragment_queue);
485 skb_queue_head_init(&x25->interrupt_in_queue);
486 skb_queue_head_init(&x25->interrupt_out_queue);
491 static int x25_create(struct net *net, struct socket *sock, int protocol)
494 struct x25_sock *x25;
495 int rc = -ESOCKTNOSUPPORT;
497 if (net != &init_net)
498 return -EAFNOSUPPORT;
500 if (sock->type != SOCK_SEQPACKET || protocol)
504 if ((sk = x25_alloc_socket(net)) == NULL)
509 sock_init_data(sock, sk);
513 sock->ops = &x25_proto_ops;
514 sk->sk_protocol = protocol;
515 sk->sk_backlog_rcv = x25_backlog_rcv;
517 x25->t21 = sysctl_x25_call_request_timeout;
518 x25->t22 = sysctl_x25_reset_request_timeout;
519 x25->t23 = sysctl_x25_clear_request_timeout;
520 x25->t2 = sysctl_x25_ack_holdback_timeout;
521 x25->state = X25_STATE_0;
522 x25->cudmatchlength = 0;
523 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
526 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
527 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
528 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
529 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
530 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
531 x25->facilities.reverse = X25_DEFAULT_REVERSE;
532 x25->dte_facilities.calling_len = 0;
533 x25->dte_facilities.called_len = 0;
534 memset(x25->dte_facilities.called_ae, '\0',
535 sizeof(x25->dte_facilities.called_ae));
536 memset(x25->dte_facilities.calling_ae, '\0',
537 sizeof(x25->dte_facilities.calling_ae));
544 static struct sock *x25_make_new(struct sock *osk)
546 struct sock *sk = NULL;
547 struct x25_sock *x25, *ox25;
549 if (osk->sk_type != SOCK_SEQPACKET)
552 if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
557 sk->sk_type = osk->sk_type;
558 sk->sk_priority = osk->sk_priority;
559 sk->sk_protocol = osk->sk_protocol;
560 sk->sk_rcvbuf = osk->sk_rcvbuf;
561 sk->sk_sndbuf = osk->sk_sndbuf;
562 sk->sk_state = TCP_ESTABLISHED;
563 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
564 sock_copy_flags(sk, osk);
567 x25->t21 = ox25->t21;
568 x25->t22 = ox25->t22;
569 x25->t23 = ox25->t23;
571 x25->facilities = ox25->facilities;
572 x25->qbitincl = ox25->qbitincl;
573 x25->dte_facilities = ox25->dte_facilities;
574 x25->cudmatchlength = ox25->cudmatchlength;
575 x25->accptapprv = ox25->accptapprv;
582 static int x25_release(struct socket *sock)
584 struct sock *sk = sock->sk;
585 struct x25_sock *x25;
592 switch (x25->state) {
596 x25_disconnect(sk, 0, 0, 0);
597 x25_destroy_socket(sk);
603 x25_clear_queues(sk);
604 x25_write_internal(sk, X25_CLEAR_REQUEST);
605 x25_start_t23timer(sk);
606 x25->state = X25_STATE_2;
607 sk->sk_state = TCP_CLOSE;
608 sk->sk_shutdown |= SEND_SHUTDOWN;
609 sk->sk_state_change(sk);
610 sock_set_flag(sk, SOCK_DEAD);
611 sock_set_flag(sk, SOCK_DESTROY);
616 sk->sk_socket = NULL; /* Not used, but we should do this */
621 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
623 struct sock *sk = sock->sk;
624 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
626 if (!sock_flag(sk, SOCK_ZAPPED) ||
627 addr_len != sizeof(struct sockaddr_x25) ||
628 addr->sx25_family != AF_X25)
631 x25_sk(sk)->source_addr = addr->sx25_addr;
632 x25_insert_socket(sk);
633 sock_reset_flag(sk, SOCK_ZAPPED);
634 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
639 static int x25_wait_for_connection_establishment(struct sock *sk)
641 DECLARE_WAITQUEUE(wait, current);
644 add_wait_queue_exclusive(sk->sk_sleep, &wait);
646 __set_current_state(TASK_INTERRUPTIBLE);
648 if (signal_pending(current))
652 sk->sk_socket->state = SS_UNCONNECTED;
656 if (sk->sk_state != TCP_ESTABLISHED) {
663 __set_current_state(TASK_RUNNING);
664 remove_wait_queue(sk->sk_sleep, &wait);
668 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
669 int addr_len, int flags)
671 struct sock *sk = sock->sk;
672 struct x25_sock *x25 = x25_sk(sk);
673 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
674 struct x25_route *rt;
678 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
679 sock->state = SS_CONNECTED;
680 goto out; /* Connect completed during a ERESTARTSYS event */
684 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
685 sock->state = SS_UNCONNECTED;
689 rc = -EISCONN; /* No reconnect on a seqpacket socket */
690 if (sk->sk_state == TCP_ESTABLISHED)
693 sk->sk_state = TCP_CLOSE;
694 sock->state = SS_UNCONNECTED;
697 if (addr_len != sizeof(struct sockaddr_x25) ||
698 addr->sx25_family != AF_X25)
702 rt = x25_get_route(&addr->sx25_addr);
706 x25->neighbour = x25_get_neigh(rt->dev);
710 x25_limit_facilities(&x25->facilities, x25->neighbour);
712 x25->lci = x25_new_lci(x25->neighbour);
717 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
720 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
721 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
723 x25->dest_addr = addr->sx25_addr;
725 /* Move to connecting socket, start sending Connect Requests */
726 sock->state = SS_CONNECTING;
727 sk->sk_state = TCP_SYN_SENT;
729 x25->state = X25_STATE_1;
731 x25_write_internal(sk, X25_CALL_REQUEST);
733 x25_start_heartbeat(sk);
734 x25_start_t21timer(sk);
738 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
741 rc = x25_wait_for_connection_establishment(sk);
745 sock->state = SS_CONNECTED;
749 x25_neigh_put(x25->neighbour);
757 static int x25_wait_for_data(struct sock *sk, long timeout)
759 DECLARE_WAITQUEUE(wait, current);
762 add_wait_queue_exclusive(sk->sk_sleep, &wait);
764 __set_current_state(TASK_INTERRUPTIBLE);
765 if (sk->sk_shutdown & RCV_SHUTDOWN)
768 if (signal_pending(current))
774 if (skb_queue_empty(&sk->sk_receive_queue)) {
776 timeout = schedule_timeout(timeout);
781 __set_current_state(TASK_RUNNING);
782 remove_wait_queue(sk->sk_sleep, &wait);
786 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
788 struct sock *sk = sock->sk;
793 if (!sk || sk->sk_state != TCP_LISTEN)
797 if (sk->sk_type != SOCK_SEQPACKET)
801 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
804 skb = skb_dequeue(&sk->sk_receive_queue);
809 sock_graft(newsk, newsock);
811 /* Now attach up the new socket */
814 sk->sk_ack_backlog--;
815 newsock->state = SS_CONNECTED;
823 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
824 int *uaddr_len, int peer)
826 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
827 struct sock *sk = sock->sk;
828 struct x25_sock *x25 = x25_sk(sk);
831 if (sk->sk_state != TCP_ESTABLISHED)
833 sx25->sx25_addr = x25->dest_addr;
835 sx25->sx25_addr = x25->source_addr;
837 sx25->sx25_family = AF_X25;
838 *uaddr_len = sizeof(*sx25);
843 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
848 struct x25_sock *makex25;
849 struct x25_address source_addr, dest_addr;
850 struct x25_facilities facilities;
851 struct x25_dte_facilities dte_facilities;
852 int len, addr_len, rc;
855 * Remove the LCI and frame type.
857 skb_pull(skb, X25_STD_MIN_LEN);
860 * Extract the X.25 addresses and convert them to ASCII strings,
863 addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
864 skb_pull(skb, addr_len);
867 * Get the length of the facilities, skip past them for the moment
868 * get the call user data because this is needed to determine
869 * the correct listener
871 len = skb->data[0] + 1;
875 * Find a listener for the particular address/cud pair.
877 sk = x25_find_listener(&source_addr,skb);
880 if (sk != NULL && sk_acceptq_is_full(sk)) {
885 * We dont have any listeners for this incoming call.
889 skb_push(skb, addr_len + X25_STD_MIN_LEN);
890 if (sysctl_x25_forward &&
891 x25_forward_call(&dest_addr, nb, skb, lci) > 0)
893 /* Call was forwarded, dont process it any more */
898 /* No listeners, can't forward, clear the call */
899 goto out_clear_request;
904 * Try to reach a compromise on the requested facilities.
906 len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
911 * current neighbour/link might impose additional limits
912 * on certain facilties
915 x25_limit_facilities(&facilities, nb);
918 * Try to create a new socket.
920 make = x25_make_new(sk);
925 * Remove the facilities
930 make->sk_state = TCP_ESTABLISHED;
932 makex25 = x25_sk(make);
934 makex25->dest_addr = dest_addr;
935 makex25->source_addr = source_addr;
936 makex25->neighbour = nb;
937 makex25->facilities = facilities;
938 makex25->dte_facilities= dte_facilities;
939 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
940 /* ensure no reverse facil on accept */
941 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
942 /* ensure no calling address extension on accept */
943 makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
944 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
946 /* Normally all calls are accepted immediatly */
947 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
948 x25_write_internal(make, X25_CALL_ACCEPTED);
949 makex25->state = X25_STATE_3;
953 * Incoming Call User Data.
956 skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
957 makex25->calluserdata.cudlength = skb->len;
960 sk->sk_ack_backlog++;
962 x25_insert_socket(make);
964 skb_queue_head(&sk->sk_receive_queue, skb);
966 x25_start_heartbeat(make);
968 if (!sock_flag(sk, SOCK_DEAD))
969 sk->sk_data_ready(sk, skb->len);
978 x25_transmit_clear_request(nb, lci, 0x01);
982 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
983 struct msghdr *msg, size_t len)
985 struct sock *sk = sock->sk;
986 struct x25_sock *x25 = x25_sk(sk);
987 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
988 struct sockaddr_x25 sx25;
990 unsigned char *asmptr;
991 int noblock = msg->msg_flags & MSG_DONTWAIT;
993 int qbit = 0, rc = -EINVAL;
995 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
998 /* we currently don't support segmented records at the user interface */
999 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
1002 rc = -EADDRNOTAVAIL;
1003 if (sock_flag(sk, SOCK_ZAPPED))
1007 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1008 send_sig(SIGPIPE, current, 0);
1013 if (!x25->neighbour)
1018 if (msg->msg_namelen < sizeof(sx25))
1020 memcpy(&sx25, usx25, sizeof(sx25));
1022 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
1025 if (sx25.sx25_family != AF_X25)
1029 * FIXME 1003.1g - if the socket is like this because
1030 * it has become closed (not started closed) we ought
1031 * to SIGPIPE, EPIPE;
1034 if (sk->sk_state != TCP_ESTABLISHED)
1037 sx25.sx25_family = AF_X25;
1038 sx25.sx25_addr = x25->dest_addr;
1041 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1043 /* Build a packet */
1044 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1046 if ((msg->msg_flags & MSG_OOB) && len > 32)
1049 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1051 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1054 X25_SKB_CB(skb)->flags = msg->msg_flags;
1056 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1059 * Put the data on the end
1061 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1063 skb_reset_transport_header(skb);
1066 rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
1071 * If the Q BIT Include socket option is in force, the first
1072 * byte of the user data is the logical value of the Q Bit.
1074 if (x25->qbitincl) {
1075 qbit = skb->data[0];
1080 * Push down the X.25 header
1082 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1084 if (msg->msg_flags & MSG_OOB) {
1085 if (x25->neighbour->extended) {
1086 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1087 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1088 *asmptr++ = (x25->lci >> 0) & 0xFF;
1089 *asmptr++ = X25_INTERRUPT;
1091 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1092 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1093 *asmptr++ = (x25->lci >> 0) & 0xFF;
1094 *asmptr++ = X25_INTERRUPT;
1097 if (x25->neighbour->extended) {
1098 /* Build an Extended X.25 header */
1099 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1100 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1101 *asmptr++ = (x25->lci >> 0) & 0xFF;
1102 *asmptr++ = X25_DATA;
1103 *asmptr++ = X25_DATA;
1105 /* Build an Standard X.25 header */
1106 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1107 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1108 *asmptr++ = (x25->lci >> 0) & 0xFF;
1109 *asmptr++ = X25_DATA;
1113 skb->data[0] |= X25_Q_BIT;
1116 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1117 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1120 if (sk->sk_state != TCP_ESTABLISHED)
1123 if (msg->msg_flags & MSG_OOB)
1124 skb_queue_tail(&x25->interrupt_out_queue, skb);
1126 len = x25_output(sk, skb);
1129 else if (x25->qbitincl)
1134 * lock_sock() is currently only used to serialize this x25_kick()
1135 * against input-driven x25_kick() calls. It currently only blocks
1136 * incoming packets for this socket and does not protect against
1137 * any other socket state changes and is not called from anywhere
1138 * else. As x25_kick() cannot block and as long as all socket
1139 * operations are BKL-wrapped, we don't need take to care about
1140 * purging the backlog queue in x25_release().
1142 * Using lock_sock() to protect all socket operations entirely
1143 * (and making the whole x25 stack SMP aware) unfortunately would
1144 * require major changes to {send,recv}msg and skb allocation methods.
1159 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1160 struct msghdr *msg, size_t size,
1163 struct sock *sk = sock->sk;
1164 struct x25_sock *x25 = x25_sk(sk);
1165 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1168 struct sk_buff *skb;
1169 unsigned char *asmptr;
1173 * This works for seqpacket too. The receiver has ordered the queue for
1174 * us! We do one quick check first though
1176 if (sk->sk_state != TCP_ESTABLISHED)
1179 if (flags & MSG_OOB) {
1181 if (sock_flag(sk, SOCK_URGINLINE) ||
1182 !skb_peek(&x25->interrupt_in_queue))
1185 skb = skb_dequeue(&x25->interrupt_in_queue);
1187 skb_pull(skb, X25_STD_MIN_LEN);
1190 * No Q bit information on Interrupt data.
1192 if (x25->qbitincl) {
1193 asmptr = skb_push(skb, 1);
1197 msg->msg_flags |= MSG_OOB;
1199 /* Now we can treat all alike */
1200 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1201 flags & MSG_DONTWAIT, &rc);
1205 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1207 skb_pull(skb, x25->neighbour->extended ?
1208 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1210 if (x25->qbitincl) {
1211 asmptr = skb_push(skb, 1);
1216 skb_reset_transport_header(skb);
1219 if (copied > size) {
1221 msg->msg_flags |= MSG_TRUNC;
1224 /* Currently, each datagram always contains a complete record */
1225 msg->msg_flags |= MSG_EOR;
1227 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1229 goto out_free_dgram;
1232 sx25->sx25_family = AF_X25;
1233 sx25->sx25_addr = x25->dest_addr;
1236 msg->msg_namelen = sizeof(struct sockaddr_x25);
1243 skb_free_datagram(sk, skb);
1249 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1251 struct sock *sk = sock->sk;
1252 struct x25_sock *x25 = x25_sk(sk);
1253 void __user *argp = (void __user *)arg;
1258 int amount = sk->sk_sndbuf -
1259 atomic_read(&sk->sk_wmem_alloc);
1262 rc = put_user(amount, (unsigned int __user *)argp);
1267 struct sk_buff *skb;
1270 * These two are safe on a single CPU system as
1271 * only user tasks fiddle here
1273 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1275 rc = put_user(amount, (unsigned int __user *)argp);
1282 rc = sock_get_timestamp(sk,
1283 (struct timeval __user *)argp);
1288 rc = sock_get_timestampns(sk,
1289 (struct timespec __user *)argp);
1293 case SIOCGIFDSTADDR:
1294 case SIOCSIFDSTADDR:
1295 case SIOCGIFBRDADDR:
1296 case SIOCSIFBRDADDR:
1297 case SIOCGIFNETMASK:
1298 case SIOCSIFNETMASK:
1306 if (!capable(CAP_NET_ADMIN))
1308 rc = x25_route_ioctl(cmd, argp);
1310 case SIOCX25GSUBSCRIP:
1311 rc = x25_subscr_ioctl(cmd, argp);
1313 case SIOCX25SSUBSCRIP:
1315 if (!capable(CAP_NET_ADMIN))
1317 rc = x25_subscr_ioctl(cmd, argp);
1319 case SIOCX25GFACILITIES: {
1320 struct x25_facilities fac = x25->facilities;
1321 rc = copy_to_user(argp, &fac,
1322 sizeof(fac)) ? -EFAULT : 0;
1326 case SIOCX25SFACILITIES: {
1327 struct x25_facilities facilities;
1329 if (copy_from_user(&facilities, argp,
1330 sizeof(facilities)))
1333 if (sk->sk_state != TCP_LISTEN &&
1334 sk->sk_state != TCP_CLOSE)
1336 if (facilities.pacsize_in < X25_PS16 ||
1337 facilities.pacsize_in > X25_PS4096)
1339 if (facilities.pacsize_out < X25_PS16 ||
1340 facilities.pacsize_out > X25_PS4096)
1342 if (facilities.winsize_in < 1 ||
1343 facilities.winsize_in > 127)
1345 if (facilities.throughput < 0x03 ||
1346 facilities.throughput > 0xDD)
1348 if (facilities.reverse &&
1349 (facilities.reverse | 0x81)!= 0x81)
1351 x25->facilities = facilities;
1356 case SIOCX25GDTEFACILITIES: {
1357 rc = copy_to_user(argp, &x25->dte_facilities,
1358 sizeof(x25->dte_facilities));
1364 case SIOCX25SDTEFACILITIES: {
1365 struct x25_dte_facilities dtefacs;
1367 if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1370 if (sk->sk_state != TCP_LISTEN &&
1371 sk->sk_state != TCP_CLOSE)
1373 if (dtefacs.calling_len > X25_MAX_AE_LEN)
1375 if (dtefacs.calling_ae == NULL)
1377 if (dtefacs.called_len > X25_MAX_AE_LEN)
1379 if (dtefacs.called_ae == NULL)
1381 x25->dte_facilities = dtefacs;
1386 case SIOCX25GCALLUSERDATA: {
1387 struct x25_calluserdata cud = x25->calluserdata;
1388 rc = copy_to_user(argp, &cud,
1389 sizeof(cud)) ? -EFAULT : 0;
1393 case SIOCX25SCALLUSERDATA: {
1394 struct x25_calluserdata calluserdata;
1397 if (copy_from_user(&calluserdata, argp,
1398 sizeof(calluserdata)))
1401 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1403 x25->calluserdata = calluserdata;
1408 case SIOCX25GCAUSEDIAG: {
1409 struct x25_causediag causediag;
1410 causediag = x25->causediag;
1411 rc = copy_to_user(argp, &causediag,
1412 sizeof(causediag)) ? -EFAULT : 0;
1416 case SIOCX25SCUDMATCHLEN: {
1417 struct x25_subaddr sub_addr;
1419 if(sk->sk_state != TCP_CLOSE)
1422 if (copy_from_user(&sub_addr, argp,
1426 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1428 x25->cudmatchlength = sub_addr.cudmatchlength;
1433 case SIOCX25CALLACCPTAPPRV: {
1435 if (sk->sk_state != TCP_CLOSE)
1437 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1442 case SIOCX25SENDCALLACCPT: {
1444 if (sk->sk_state != TCP_ESTABLISHED)
1446 if (x25->accptapprv) /* must call accptapprv above */
1448 x25_write_internal(sk, X25_CALL_ACCEPTED);
1449 x25->state = X25_STATE_3;
1462 static struct net_proto_family x25_family_ops = {
1464 .create = x25_create,
1465 .owner = THIS_MODULE,
1468 #ifdef CONFIG_COMPAT
1469 static int compat_x25_subscr_ioctl(unsigned int cmd,
1470 struct compat_x25_subscrip_struct __user *x25_subscr32)
1472 struct compat_x25_subscrip_struct x25_subscr;
1473 struct x25_neigh *nb;
1474 struct net_device *dev;
1478 if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
1482 dev = x25_dev_get(x25_subscr.device);
1486 nb = x25_get_neigh(dev);
1492 if (cmd == SIOCX25GSUBSCRIP) {
1493 x25_subscr.extended = nb->extended;
1494 x25_subscr.global_facil_mask = nb->global_facil_mask;
1495 rc = copy_to_user(x25_subscr32, &x25_subscr,
1496 sizeof(*x25_subscr32)) ? -EFAULT : 0;
1499 if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
1501 nb->extended = x25_subscr.extended;
1502 nb->global_facil_mask = x25_subscr.global_facil_mask;
1513 static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
1516 void __user *argp = compat_ptr(arg);
1517 struct sock *sk = sock->sk;
1519 int rc = -ENOIOCTLCMD;
1524 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1529 rc = compat_sock_get_timestamp(sk,
1530 (struct timeval __user*)argp);
1535 rc = compat_sock_get_timestampns(sk,
1536 (struct timespec __user*)argp);
1540 case SIOCGIFDSTADDR:
1541 case SIOCSIFDSTADDR:
1542 case SIOCGIFBRDADDR:
1543 case SIOCSIFBRDADDR:
1544 case SIOCGIFNETMASK:
1545 case SIOCSIFNETMASK:
1553 if (!capable(CAP_NET_ADMIN))
1555 rc = x25_route_ioctl(cmd, argp);
1557 case SIOCX25GSUBSCRIP:
1558 rc = compat_x25_subscr_ioctl(cmd, argp);
1560 case SIOCX25SSUBSCRIP:
1562 if (!capable(CAP_NET_ADMIN))
1564 rc = compat_x25_subscr_ioctl(cmd, argp);
1566 case SIOCX25GFACILITIES:
1567 case SIOCX25SFACILITIES:
1568 case SIOCX25GDTEFACILITIES:
1569 case SIOCX25SDTEFACILITIES:
1570 case SIOCX25GCALLUSERDATA:
1571 case SIOCX25SCALLUSERDATA:
1572 case SIOCX25GCAUSEDIAG:
1573 case SIOCX25SCUDMATCHLEN:
1574 case SIOCX25CALLACCPTAPPRV:
1575 case SIOCX25SENDCALLACCPT:
1576 rc = x25_ioctl(sock, cmd, (unsigned long)argp);
1586 static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1588 .owner = THIS_MODULE,
1589 .release = x25_release,
1591 .connect = x25_connect,
1592 .socketpair = sock_no_socketpair,
1593 .accept = x25_accept,
1594 .getname = x25_getname,
1595 .poll = datagram_poll,
1597 #ifdef CONFIG_COMPAT
1598 .compat_ioctl = compat_x25_ioctl,
1600 .listen = x25_listen,
1601 .shutdown = sock_no_shutdown,
1602 .setsockopt = x25_setsockopt,
1603 .getsockopt = x25_getsockopt,
1604 .sendmsg = x25_sendmsg,
1605 .recvmsg = x25_recvmsg,
1606 .mmap = sock_no_mmap,
1607 .sendpage = sock_no_sendpage,
1610 SOCKOPS_WRAP(x25_proto, AF_X25);
1612 static struct packet_type x25_packet_type = {
1613 .type = __constant_htons(ETH_P_X25),
1614 .func = x25_lapb_receive_frame,
1617 static struct notifier_block x25_dev_notifier = {
1618 .notifier_call = x25_device_event,
1621 void x25_kill_by_neigh(struct x25_neigh *nb)
1624 struct hlist_node *node;
1626 write_lock_bh(&x25_list_lock);
1628 sk_for_each(s, node, &x25_list)
1629 if (x25_sk(s)->neighbour == nb)
1630 x25_disconnect(s, ENETUNREACH, 0, 0);
1632 write_unlock_bh(&x25_list_lock);
1634 /* Remove any related forwards */
1635 x25_clear_forward_by_dev(nb->dev);
1638 static int __init x25_init(void)
1640 int rc = proto_register(&x25_proto, 0);
1645 sock_register(&x25_family_ops);
1647 dev_add_pack(&x25_packet_type);
1649 register_netdevice_notifier(&x25_dev_notifier);
1651 printk(KERN_INFO "X.25 for Linux Version 0.2\n");
1653 #ifdef CONFIG_SYSCTL
1654 x25_register_sysctl();
1660 module_init(x25_init);
1662 static void __exit x25_exit(void)
1668 #ifdef CONFIG_SYSCTL
1669 x25_unregister_sysctl();
1672 unregister_netdevice_notifier(&x25_dev_notifier);
1674 dev_remove_pack(&x25_packet_type);
1676 sock_unregister(AF_X25);
1677 proto_unregister(&x25_proto);
1679 module_exit(x25_exit);
1681 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1682 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1683 MODULE_LICENSE("GPL");
1684 MODULE_ALIAS_NETPROTO(PF_X25);