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/config.h>
39 #include <linux/module.h>
40 #include <linux/capability.h>
41 #include <linux/errno.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.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>
59 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
60 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
61 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
62 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
63 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
66 DEFINE_RWLOCK(x25_list_lock);
68 static const struct proto_ops x25_proto_ops;
70 static struct x25_address null_x25_address = {" "};
72 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
73 struct x25_address *calling_addr)
75 int called_len, calling_len;
76 char *called, *calling;
79 called_len = (*p >> 0) & 0x0F;
80 calling_len = (*p >> 4) & 0x0F;
82 called = called_addr->x25_addr;
83 calling = calling_addr->x25_addr;
86 for (i = 0; i < (called_len + calling_len); i++) {
89 *called++ = ((*p >> 0) & 0x0F) + '0';
92 *called++ = ((*p >> 4) & 0x0F) + '0';
96 *calling++ = ((*p >> 0) & 0x0F) + '0';
99 *calling++ = ((*p >> 4) & 0x0F) + '0';
104 *called = *calling = '\0';
106 return 1 + (called_len + calling_len + 1) / 2;
109 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
110 struct x25_address *calling_addr)
112 unsigned int called_len, calling_len;
113 char *called, *calling;
116 called = called_addr->x25_addr;
117 calling = calling_addr->x25_addr;
119 called_len = strlen(called);
120 calling_len = strlen(calling);
122 *p++ = (calling_len << 4) | (called_len << 0);
124 for (i = 0; i < (called_len + calling_len); i++) {
125 if (i < called_len) {
127 *p |= (*called++ - '0') << 0;
131 *p |= (*called++ - '0') << 4;
135 *p |= (*calling++ - '0') << 0;
139 *p |= (*calling++ - '0') << 4;
144 return 1 + (called_len + calling_len + 1) / 2;
148 * Socket removal during an interrupt is now safe.
150 static void x25_remove_socket(struct sock *sk)
152 write_lock_bh(&x25_list_lock);
153 sk_del_node_init(sk);
154 write_unlock_bh(&x25_list_lock);
158 * Kill all bound sockets on a dropped device.
160 static void x25_kill_by_device(struct net_device *dev)
163 struct hlist_node *node;
165 write_lock_bh(&x25_list_lock);
167 sk_for_each(s, node, &x25_list)
168 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
169 x25_disconnect(s, ENETUNREACH, 0, 0);
171 write_unlock_bh(&x25_list_lock);
175 * Handle device status changes.
177 static int x25_device_event(struct notifier_block *this, unsigned long event,
180 struct net_device *dev = ptr;
181 struct x25_neigh *nb;
183 if (dev->type == ARPHRD_X25
184 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
185 || dev->type == ARPHRD_ETHER
190 x25_link_device_up(dev);
192 case NETDEV_GOING_DOWN:
193 nb = x25_get_neigh(dev);
195 x25_terminate_link(nb);
200 x25_kill_by_device(dev);
201 x25_route_device_down(dev);
202 x25_link_device_down(dev);
211 * Add a socket to the bound sockets list.
213 static void x25_insert_socket(struct sock *sk)
215 write_lock_bh(&x25_list_lock);
216 sk_add_node(sk, &x25_list);
217 write_unlock_bh(&x25_list_lock);
221 * Find a socket that wants to accept the Call Request we just
222 * received. Check the full list for an address/cud match.
223 * If no cuds match return the next_best thing, an address match.
224 * Note: if a listening socket has cud set it must only get calls
227 static struct sock *x25_find_listener(struct x25_address *addr,
231 struct sock *next_best;
232 struct hlist_node *node;
234 read_lock_bh(&x25_list_lock);
237 sk_for_each(s, node, &x25_list)
238 if ((!strcmp(addr->x25_addr,
239 x25_sk(s)->source_addr.x25_addr) ||
240 !strcmp(addr->x25_addr,
241 null_x25_address.x25_addr)) &&
242 s->sk_state == TCP_LISTEN) {
244 * Found a listening socket, now check the incoming
245 * call user data vs this sockets call user data
247 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
248 if((memcmp(x25_sk(s)->calluserdata.cuddata,
250 x25_sk(s)->cudmatchlength)) == 0) {
264 read_unlock_bh(&x25_list_lock);
269 * Find a connected X.25 socket given my LCI and neighbour.
271 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
274 struct hlist_node *node;
276 sk_for_each(s, node, &x25_list)
277 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
286 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
290 read_lock_bh(&x25_list_lock);
291 s = __x25_find_socket(lci, nb);
292 read_unlock_bh(&x25_list_lock);
297 * Find a unique LCI for a given device.
299 static unsigned int x25_new_lci(struct x25_neigh *nb)
301 unsigned int lci = 1;
304 read_lock_bh(&x25_list_lock);
306 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
314 read_unlock_bh(&x25_list_lock);
321 void x25_destroy_socket(struct sock *);
324 * handler for deferred kills.
326 static void x25_destroy_timer(unsigned long data)
328 x25_destroy_socket((struct sock *)data);
332 * This is called from user mode and the timers. Thus it protects itself
333 * against interrupt users but doesn't worry about being called during
334 * work. Once it is removed from the queue no interrupt or bottom half
335 * will touch it and we are (fairly 8-) ) safe.
336 * Not static as it's used by the timer
338 void x25_destroy_socket(struct sock *sk)
344 x25_stop_heartbeat(sk);
347 x25_remove_socket(sk);
348 x25_clear_queues(sk); /* Flush the queues */
350 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
351 if (skb->sk != sk) { /* A pending connection */
353 * Queue the unaccepted socket for death
355 sock_set_flag(skb->sk, SOCK_DEAD);
356 x25_start_heartbeat(skb->sk);
357 x25_sk(skb->sk)->state = X25_STATE_0;
363 if (atomic_read(&sk->sk_wmem_alloc) ||
364 atomic_read(&sk->sk_rmem_alloc)) {
365 /* Defer: outstanding buffers */
366 sk->sk_timer.expires = jiffies + 10 * HZ;
367 sk->sk_timer.function = x25_destroy_timer;
368 sk->sk_timer.data = (unsigned long)sk;
369 add_timer(&sk->sk_timer);
371 /* drop last reference so sock_put will free */
380 * Handling for system calls applied via the various interfaces to a
381 * X.25 socket object.
384 static int x25_setsockopt(struct socket *sock, int level, int optname,
385 char __user *optval, int optlen)
388 struct sock *sk = sock->sk;
389 int rc = -ENOPROTOOPT;
391 if (level != SOL_X25 || optname != X25_QBITINCL)
395 if (optlen < sizeof(int))
399 if (get_user(opt, (int __user *)optval))
402 x25_sk(sk)->qbitincl = !!opt;
408 static int x25_getsockopt(struct socket *sock, int level, int optname,
409 char __user *optval, int __user *optlen)
411 struct sock *sk = sock->sk;
412 int val, len, rc = -ENOPROTOOPT;
414 if (level != SOL_X25 || optname != X25_QBITINCL)
418 if (get_user(len, optlen))
421 len = min_t(unsigned int, len, sizeof(int));
428 if (put_user(len, optlen))
431 val = x25_sk(sk)->qbitincl;
432 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
437 static int x25_listen(struct socket *sock, int backlog)
439 struct sock *sk = sock->sk;
440 int rc = -EOPNOTSUPP;
442 if (sk->sk_state != TCP_LISTEN) {
443 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
444 sk->sk_max_ack_backlog = backlog;
445 sk->sk_state = TCP_LISTEN;
452 static struct proto x25_proto = {
454 .owner = THIS_MODULE,
455 .obj_size = sizeof(struct x25_sock),
458 static struct sock *x25_alloc_socket(void)
460 struct x25_sock *x25;
461 struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1);
466 sock_init_data(NULL, sk);
469 skb_queue_head_init(&x25->ack_queue);
470 skb_queue_head_init(&x25->fragment_queue);
471 skb_queue_head_init(&x25->interrupt_in_queue);
472 skb_queue_head_init(&x25->interrupt_out_queue);
477 void x25_init_timers(struct sock *sk);
479 static int x25_create(struct socket *sock, int protocol)
482 struct x25_sock *x25;
483 int rc = -ESOCKTNOSUPPORT;
485 if (sock->type != SOCK_SEQPACKET || protocol)
489 if ((sk = x25_alloc_socket()) == NULL)
494 sock_init_data(sock, sk);
498 sock->ops = &x25_proto_ops;
499 sk->sk_protocol = protocol;
500 sk->sk_backlog_rcv = x25_backlog_rcv;
502 x25->t21 = sysctl_x25_call_request_timeout;
503 x25->t22 = sysctl_x25_reset_request_timeout;
504 x25->t23 = sysctl_x25_clear_request_timeout;
505 x25->t2 = sysctl_x25_ack_holdback_timeout;
506 x25->state = X25_STATE_0;
507 x25->cudmatchlength = 0;
508 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
511 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
512 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
513 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
514 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
515 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
516 x25->facilities.reverse = X25_DEFAULT_REVERSE;
522 static struct sock *x25_make_new(struct sock *osk)
524 struct sock *sk = NULL;
525 struct x25_sock *x25, *ox25;
527 if (osk->sk_type != SOCK_SEQPACKET)
530 if ((sk = x25_alloc_socket()) == NULL)
535 sk->sk_type = osk->sk_type;
536 sk->sk_socket = osk->sk_socket;
537 sk->sk_priority = osk->sk_priority;
538 sk->sk_protocol = osk->sk_protocol;
539 sk->sk_rcvbuf = osk->sk_rcvbuf;
540 sk->sk_sndbuf = osk->sk_sndbuf;
541 sk->sk_state = TCP_ESTABLISHED;
542 sk->sk_sleep = osk->sk_sleep;
543 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
544 sock_copy_flags(sk, osk);
547 x25->t21 = ox25->t21;
548 x25->t22 = ox25->t22;
549 x25->t23 = ox25->t23;
551 x25->facilities = ox25->facilities;
552 x25->qbitincl = ox25->qbitincl;
553 x25->cudmatchlength = ox25->cudmatchlength;
554 x25->accptapprv = ox25->accptapprv;
561 static int x25_release(struct socket *sock)
563 struct sock *sk = sock->sk;
564 struct x25_sock *x25;
571 switch (x25->state) {
575 x25_disconnect(sk, 0, 0, 0);
576 x25_destroy_socket(sk);
582 x25_clear_queues(sk);
583 x25_write_internal(sk, X25_CLEAR_REQUEST);
584 x25_start_t23timer(sk);
585 x25->state = X25_STATE_2;
586 sk->sk_state = TCP_CLOSE;
587 sk->sk_shutdown |= SEND_SHUTDOWN;
588 sk->sk_state_change(sk);
589 sock_set_flag(sk, SOCK_DEAD);
590 sock_set_flag(sk, SOCK_DESTROY);
595 sk->sk_socket = NULL; /* Not used, but we should do this */
600 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
602 struct sock *sk = sock->sk;
603 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
605 if (!sock_flag(sk, SOCK_ZAPPED) ||
606 addr_len != sizeof(struct sockaddr_x25) ||
607 addr->sx25_family != AF_X25)
610 x25_sk(sk)->source_addr = addr->sx25_addr;
611 x25_insert_socket(sk);
612 sock_reset_flag(sk, SOCK_ZAPPED);
613 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
618 static int x25_wait_for_connection_establishment(struct sock *sk)
620 DECLARE_WAITQUEUE(wait, current);
623 add_wait_queue_exclusive(sk->sk_sleep, &wait);
625 __set_current_state(TASK_INTERRUPTIBLE);
627 if (signal_pending(current))
631 sk->sk_socket->state = SS_UNCONNECTED;
635 if (sk->sk_state != TCP_ESTABLISHED) {
642 __set_current_state(TASK_RUNNING);
643 remove_wait_queue(sk->sk_sleep, &wait);
647 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
648 int addr_len, int flags)
650 struct sock *sk = sock->sk;
651 struct x25_sock *x25 = x25_sk(sk);
652 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
653 struct x25_route *rt;
657 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
658 sock->state = SS_CONNECTED;
659 goto out; /* Connect completed during a ERESTARTSYS event */
663 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
664 sock->state = SS_UNCONNECTED;
668 rc = -EISCONN; /* No reconnect on a seqpacket socket */
669 if (sk->sk_state == TCP_ESTABLISHED)
672 sk->sk_state = TCP_CLOSE;
673 sock->state = SS_UNCONNECTED;
676 if (addr_len != sizeof(struct sockaddr_x25) ||
677 addr->sx25_family != AF_X25)
681 rt = x25_get_route(&addr->sx25_addr);
685 x25->neighbour = x25_get_neigh(rt->dev);
689 x25_limit_facilities(&x25->facilities, x25->neighbour);
691 x25->lci = x25_new_lci(x25->neighbour);
696 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
699 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
700 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
702 x25->dest_addr = addr->sx25_addr;
704 /* Move to connecting socket, start sending Connect Requests */
705 sock->state = SS_CONNECTING;
706 sk->sk_state = TCP_SYN_SENT;
708 x25->state = X25_STATE_1;
710 x25_write_internal(sk, X25_CALL_REQUEST);
712 x25_start_heartbeat(sk);
713 x25_start_t21timer(sk);
717 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
720 rc = x25_wait_for_connection_establishment(sk);
724 sock->state = SS_CONNECTED;
728 x25_neigh_put(x25->neighbour);
736 static int x25_wait_for_data(struct sock *sk, int timeout)
738 DECLARE_WAITQUEUE(wait, current);
741 add_wait_queue_exclusive(sk->sk_sleep, &wait);
743 __set_current_state(TASK_INTERRUPTIBLE);
744 if (sk->sk_shutdown & RCV_SHUTDOWN)
747 if (signal_pending(current))
753 if (skb_queue_empty(&sk->sk_receive_queue)) {
755 timeout = schedule_timeout(timeout);
760 __set_current_state(TASK_RUNNING);
761 remove_wait_queue(sk->sk_sleep, &wait);
765 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
767 struct sock *sk = sock->sk;
772 if (!sk || sk->sk_state != TCP_LISTEN)
776 if (sk->sk_type != SOCK_SEQPACKET)
780 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
783 skb = skb_dequeue(&sk->sk_receive_queue);
788 newsk->sk_socket = newsock;
789 newsk->sk_sleep = &newsock->wait;
791 /* Now attach up the new socket */
794 sk->sk_ack_backlog--;
796 newsock->state = SS_CONNECTED;
804 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
805 int *uaddr_len, int peer)
807 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
808 struct sock *sk = sock->sk;
809 struct x25_sock *x25 = x25_sk(sk);
812 if (sk->sk_state != TCP_ESTABLISHED)
814 sx25->sx25_addr = x25->dest_addr;
816 sx25->sx25_addr = x25->source_addr;
818 sx25->sx25_family = AF_X25;
819 *uaddr_len = sizeof(*sx25);
824 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
829 struct x25_sock *makex25;
830 struct x25_address source_addr, dest_addr;
831 struct x25_facilities facilities;
835 * Remove the LCI and frame type.
837 skb_pull(skb, X25_STD_MIN_LEN);
840 * Extract the X.25 addresses and convert them to ASCII strings,
843 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
846 * Get the length of the facilities, skip past them for the moment
847 * get the call user data because this is needed to determine
848 * the correct listener
850 len = skb->data[0] + 1;
854 * Find a listener for the particular address/cud pair.
856 sk = x25_find_listener(&source_addr,skb);
860 * We can't accept the Call Request.
862 if (sk == NULL || sk_acceptq_is_full(sk))
863 goto out_clear_request;
866 * Try to reach a compromise on the requested facilities.
868 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
872 * current neighbour/link might impose additional limits
873 * on certain facilties
876 x25_limit_facilities(&facilities, nb);
879 * Try to create a new socket.
881 make = x25_make_new(sk);
886 * Remove the facilities
891 make->sk_state = TCP_ESTABLISHED;
893 makex25 = x25_sk(make);
895 makex25->dest_addr = dest_addr;
896 makex25->source_addr = source_addr;
897 makex25->neighbour = nb;
898 makex25->facilities = facilities;
899 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
900 /* ensure no reverse facil on accept */
901 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
902 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
904 /* Normally all calls are accepted immediatly */
905 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
906 x25_write_internal(make, X25_CALL_ACCEPTED);
907 makex25->state = X25_STATE_3;
911 * Incoming Call User Data.
914 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
915 makex25->calluserdata.cudlength = skb->len;
918 sk->sk_ack_backlog++;
920 x25_insert_socket(make);
922 skb_queue_head(&sk->sk_receive_queue, skb);
924 x25_start_heartbeat(make);
926 if (!sock_flag(sk, SOCK_DEAD))
927 sk->sk_data_ready(sk, skb->len);
936 x25_transmit_clear_request(nb, lci, 0x01);
940 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
941 struct msghdr *msg, size_t len)
943 struct sock *sk = sock->sk;
944 struct x25_sock *x25 = x25_sk(sk);
945 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
946 struct sockaddr_x25 sx25;
948 unsigned char *asmptr;
949 int noblock = msg->msg_flags & MSG_DONTWAIT;
951 int qbit = 0, rc = -EINVAL;
953 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
956 /* we currently don't support segmented records at the user interface */
957 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
961 if (sock_flag(sk, SOCK_ZAPPED))
965 if (sk->sk_shutdown & SEND_SHUTDOWN) {
966 send_sig(SIGPIPE, current, 0);
976 if (msg->msg_namelen < sizeof(sx25))
978 memcpy(&sx25, usx25, sizeof(sx25));
980 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
983 if (sx25.sx25_family != AF_X25)
987 * FIXME 1003.1g - if the socket is like this because
988 * it has become closed (not started closed) we ought
992 if (sk->sk_state != TCP_ESTABLISHED)
995 sx25.sx25_family = AF_X25;
996 sx25.sx25_addr = x25->dest_addr;
999 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1001 /* Build a packet */
1002 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1004 if ((msg->msg_flags & MSG_OOB) && len > 32)
1007 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1009 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1012 X25_SKB_CB(skb)->flags = msg->msg_flags;
1014 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1017 * Put the data on the end
1019 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1021 asmptr = skb->h.raw = skb_put(skb, len);
1023 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1028 * If the Q BIT Include socket option is in force, the first
1029 * byte of the user data is the logical value of the Q Bit.
1031 if (x25->qbitincl) {
1032 qbit = skb->data[0];
1037 * Push down the X.25 header
1039 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1041 if (msg->msg_flags & MSG_OOB) {
1042 if (x25->neighbour->extended) {
1043 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1044 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1045 *asmptr++ = (x25->lci >> 0) & 0xFF;
1046 *asmptr++ = X25_INTERRUPT;
1048 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1049 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1050 *asmptr++ = (x25->lci >> 0) & 0xFF;
1051 *asmptr++ = X25_INTERRUPT;
1054 if (x25->neighbour->extended) {
1055 /* Build an Extended X.25 header */
1056 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1057 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1058 *asmptr++ = (x25->lci >> 0) & 0xFF;
1059 *asmptr++ = X25_DATA;
1060 *asmptr++ = X25_DATA;
1062 /* Build an Standard X.25 header */
1063 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1064 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1065 *asmptr++ = (x25->lci >> 0) & 0xFF;
1066 *asmptr++ = X25_DATA;
1070 skb->data[0] |= X25_Q_BIT;
1073 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1074 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1077 if (sk->sk_state != TCP_ESTABLISHED)
1080 if (msg->msg_flags & MSG_OOB)
1081 skb_queue_tail(&x25->interrupt_out_queue, skb);
1083 len = x25_output(sk, skb);
1086 else if (x25->qbitincl)
1091 * lock_sock() is currently only used to serialize this x25_kick()
1092 * against input-driven x25_kick() calls. It currently only blocks
1093 * incoming packets for this socket and does not protect against
1094 * any other socket state changes and is not called from anywhere
1095 * else. As x25_kick() cannot block and as long as all socket
1096 * operations are BKL-wrapped, we don't need take to care about
1097 * purging the backlog queue in x25_release().
1099 * Using lock_sock() to protect all socket operations entirely
1100 * (and making the whole x25 stack SMP aware) unfortunately would
1101 * require major changes to {send,recv}msg and skb allocation methods.
1116 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1117 struct msghdr *msg, size_t size,
1120 struct sock *sk = sock->sk;
1121 struct x25_sock *x25 = x25_sk(sk);
1122 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1125 struct sk_buff *skb;
1126 unsigned char *asmptr;
1130 * This works for seqpacket too. The receiver has ordered the queue for
1131 * us! We do one quick check first though
1133 if (sk->sk_state != TCP_ESTABLISHED)
1136 if (flags & MSG_OOB) {
1138 if (sock_flag(sk, SOCK_URGINLINE) ||
1139 !skb_peek(&x25->interrupt_in_queue))
1142 skb = skb_dequeue(&x25->interrupt_in_queue);
1144 skb_pull(skb, X25_STD_MIN_LEN);
1147 * No Q bit information on Interrupt data.
1149 if (x25->qbitincl) {
1150 asmptr = skb_push(skb, 1);
1154 msg->msg_flags |= MSG_OOB;
1156 /* Now we can treat all alike */
1157 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1158 flags & MSG_DONTWAIT, &rc);
1162 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1164 skb_pull(skb, x25->neighbour->extended ?
1165 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1167 if (x25->qbitincl) {
1168 asmptr = skb_push(skb, 1);
1173 skb->h.raw = skb->data;
1177 if (copied > size) {
1179 msg->msg_flags |= MSG_TRUNC;
1182 /* Currently, each datagram always contains a complete record */
1183 msg->msg_flags |= MSG_EOR;
1185 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1187 goto out_free_dgram;
1190 sx25->sx25_family = AF_X25;
1191 sx25->sx25_addr = x25->dest_addr;
1194 msg->msg_namelen = sizeof(struct sockaddr_x25);
1201 skb_free_datagram(sk, skb);
1207 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1209 struct sock *sk = sock->sk;
1210 struct x25_sock *x25 = x25_sk(sk);
1211 void __user *argp = (void __user *)arg;
1216 int amount = sk->sk_sndbuf -
1217 atomic_read(&sk->sk_wmem_alloc);
1220 rc = put_user(amount, (unsigned int __user *)argp);
1225 struct sk_buff *skb;
1228 * These two are safe on a single CPU system as
1229 * only user tasks fiddle here
1231 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1233 rc = put_user(amount, (unsigned int __user *)argp);
1240 rc = sock_get_timestamp(sk,
1241 (struct timeval __user *)argp);
1245 case SIOCGIFDSTADDR:
1246 case SIOCSIFDSTADDR:
1247 case SIOCGIFBRDADDR:
1248 case SIOCSIFBRDADDR:
1249 case SIOCGIFNETMASK:
1250 case SIOCSIFNETMASK:
1258 if (!capable(CAP_NET_ADMIN))
1260 rc = x25_route_ioctl(cmd, argp);
1262 case SIOCX25GSUBSCRIP:
1263 rc = x25_subscr_ioctl(cmd, argp);
1265 case SIOCX25SSUBSCRIP:
1267 if (!capable(CAP_NET_ADMIN))
1269 rc = x25_subscr_ioctl(cmd, argp);
1271 case SIOCX25GFACILITIES: {
1272 struct x25_facilities fac = x25->facilities;
1273 rc = copy_to_user(argp, &fac,
1274 sizeof(fac)) ? -EFAULT : 0;
1278 case SIOCX25SFACILITIES: {
1279 struct x25_facilities facilities;
1281 if (copy_from_user(&facilities, argp,
1282 sizeof(facilities)))
1285 if (sk->sk_state != TCP_LISTEN &&
1286 sk->sk_state != TCP_CLOSE)
1288 if (facilities.pacsize_in < X25_PS16 ||
1289 facilities.pacsize_in > X25_PS4096)
1291 if (facilities.pacsize_out < X25_PS16 ||
1292 facilities.pacsize_out > X25_PS4096)
1294 if (facilities.winsize_in < 1 ||
1295 facilities.winsize_in > 127)
1297 if (facilities.throughput < 0x03 ||
1298 facilities.throughput > 0xDD)
1300 if (facilities.reverse &&
1301 (facilities.reverse | 0x81)!= 0x81)
1303 x25->facilities = facilities;
1308 case SIOCX25GCALLUSERDATA: {
1309 struct x25_calluserdata cud = x25->calluserdata;
1310 rc = copy_to_user(argp, &cud,
1311 sizeof(cud)) ? -EFAULT : 0;
1315 case SIOCX25SCALLUSERDATA: {
1316 struct x25_calluserdata calluserdata;
1319 if (copy_from_user(&calluserdata, argp,
1320 sizeof(calluserdata)))
1323 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1325 x25->calluserdata = calluserdata;
1330 case SIOCX25GCAUSEDIAG: {
1331 struct x25_causediag causediag;
1332 causediag = x25->causediag;
1333 rc = copy_to_user(argp, &causediag,
1334 sizeof(causediag)) ? -EFAULT : 0;
1338 case SIOCX25SCUDMATCHLEN: {
1339 struct x25_subaddr sub_addr;
1341 if(sk->sk_state != TCP_CLOSE)
1344 if (copy_from_user(&sub_addr, argp,
1348 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1350 x25->cudmatchlength = sub_addr.cudmatchlength;
1355 case SIOCX25CALLACCPTAPPRV: {
1357 if (sk->sk_state != TCP_CLOSE)
1359 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1364 case SIOCX25SENDCALLACCPT: {
1366 if (sk->sk_state != TCP_ESTABLISHED)
1368 if (x25->accptapprv) /* must call accptapprv above */
1370 x25_write_internal(sk, X25_CALL_ACCEPTED);
1371 x25->state = X25_STATE_3;
1384 static struct net_proto_family x25_family_ops = {
1386 .create = x25_create,
1387 .owner = THIS_MODULE,
1390 static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1392 .owner = THIS_MODULE,
1393 .release = x25_release,
1395 .connect = x25_connect,
1396 .socketpair = sock_no_socketpair,
1397 .accept = x25_accept,
1398 .getname = x25_getname,
1399 .poll = datagram_poll,
1401 .listen = x25_listen,
1402 .shutdown = sock_no_shutdown,
1403 .setsockopt = x25_setsockopt,
1404 .getsockopt = x25_getsockopt,
1405 .sendmsg = x25_sendmsg,
1406 .recvmsg = x25_recvmsg,
1407 .mmap = sock_no_mmap,
1408 .sendpage = sock_no_sendpage,
1411 #include <linux/smp_lock.h>
1412 SOCKOPS_WRAP(x25_proto, AF_X25);
1414 static struct packet_type x25_packet_type = {
1415 .type = __constant_htons(ETH_P_X25),
1416 .func = x25_lapb_receive_frame,
1419 static struct notifier_block x25_dev_notifier = {
1420 .notifier_call = x25_device_event,
1423 void x25_kill_by_neigh(struct x25_neigh *nb)
1426 struct hlist_node *node;
1428 write_lock_bh(&x25_list_lock);
1430 sk_for_each(s, node, &x25_list)
1431 if (x25_sk(s)->neighbour == nb)
1432 x25_disconnect(s, ENETUNREACH, 0, 0);
1434 write_unlock_bh(&x25_list_lock);
1437 static int __init x25_init(void)
1439 int rc = proto_register(&x25_proto, 0);
1444 sock_register(&x25_family_ops);
1446 dev_add_pack(&x25_packet_type);
1448 register_netdevice_notifier(&x25_dev_notifier);
1450 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1452 #ifdef CONFIG_SYSCTL
1453 x25_register_sysctl();
1459 module_init(x25_init);
1461 static void __exit x25_exit(void)
1467 #ifdef CONFIG_SYSCTL
1468 x25_unregister_sysctl();
1471 unregister_netdevice_notifier(&x25_dev_notifier);
1473 dev_remove_pack(&x25_packet_type);
1475 sock_unregister(AF_X25);
1476 proto_unregister(&x25_proto);
1478 module_exit(x25_exit);
1480 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1481 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1482 MODULE_LICENSE("GPL");
1483 MODULE_ALIAS_NETPROTO(PF_X25);