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 sk->sk_socket = sock;
495 sk->sk_type = sock->type;
496 sk->sk_sleep = &sock->wait;
501 sock->ops = &x25_proto_ops;
502 sk->sk_protocol = protocol;
503 sk->sk_backlog_rcv = x25_backlog_rcv;
505 x25->t21 = sysctl_x25_call_request_timeout;
506 x25->t22 = sysctl_x25_reset_request_timeout;
507 x25->t23 = sysctl_x25_clear_request_timeout;
508 x25->t2 = sysctl_x25_ack_holdback_timeout;
509 x25->state = X25_STATE_0;
510 x25->cudmatchlength = 0;
511 x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */
514 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
515 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
516 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
517 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
518 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
519 x25->facilities.reverse = X25_DEFAULT_REVERSE;
525 static struct sock *x25_make_new(struct sock *osk)
527 struct sock *sk = NULL;
528 struct x25_sock *x25, *ox25;
530 if (osk->sk_type != SOCK_SEQPACKET)
533 if ((sk = x25_alloc_socket()) == NULL)
538 sk->sk_type = osk->sk_type;
539 sk->sk_socket = osk->sk_socket;
540 sk->sk_priority = osk->sk_priority;
541 sk->sk_protocol = osk->sk_protocol;
542 sk->sk_rcvbuf = osk->sk_rcvbuf;
543 sk->sk_sndbuf = osk->sk_sndbuf;
544 sk->sk_state = TCP_ESTABLISHED;
545 sk->sk_sleep = osk->sk_sleep;
546 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
547 sock_copy_flags(sk, osk);
550 x25->t21 = ox25->t21;
551 x25->t22 = ox25->t22;
552 x25->t23 = ox25->t23;
554 x25->facilities = ox25->facilities;
555 x25->qbitincl = ox25->qbitincl;
556 x25->cudmatchlength = ox25->cudmatchlength;
557 x25->accptapprv = ox25->accptapprv;
564 static int x25_release(struct socket *sock)
566 struct sock *sk = sock->sk;
567 struct x25_sock *x25;
574 switch (x25->state) {
578 x25_disconnect(sk, 0, 0, 0);
579 x25_destroy_socket(sk);
585 x25_clear_queues(sk);
586 x25_write_internal(sk, X25_CLEAR_REQUEST);
587 x25_start_t23timer(sk);
588 x25->state = X25_STATE_2;
589 sk->sk_state = TCP_CLOSE;
590 sk->sk_shutdown |= SEND_SHUTDOWN;
591 sk->sk_state_change(sk);
592 sock_set_flag(sk, SOCK_DEAD);
593 sock_set_flag(sk, SOCK_DESTROY);
598 sk->sk_socket = NULL; /* Not used, but we should do this */
603 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
605 struct sock *sk = sock->sk;
606 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
608 if (!sock_flag(sk, SOCK_ZAPPED) ||
609 addr_len != sizeof(struct sockaddr_x25) ||
610 addr->sx25_family != AF_X25)
613 x25_sk(sk)->source_addr = addr->sx25_addr;
614 x25_insert_socket(sk);
615 sock_reset_flag(sk, SOCK_ZAPPED);
616 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
621 static int x25_wait_for_connection_establishment(struct sock *sk)
623 DECLARE_WAITQUEUE(wait, current);
626 add_wait_queue_exclusive(sk->sk_sleep, &wait);
628 __set_current_state(TASK_INTERRUPTIBLE);
630 if (signal_pending(current))
634 sk->sk_socket->state = SS_UNCONNECTED;
638 if (sk->sk_state != TCP_ESTABLISHED) {
645 __set_current_state(TASK_RUNNING);
646 remove_wait_queue(sk->sk_sleep, &wait);
650 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
651 int addr_len, int flags)
653 struct sock *sk = sock->sk;
654 struct x25_sock *x25 = x25_sk(sk);
655 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
656 struct x25_route *rt;
660 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
661 sock->state = SS_CONNECTED;
662 goto out; /* Connect completed during a ERESTARTSYS event */
666 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
667 sock->state = SS_UNCONNECTED;
671 rc = -EISCONN; /* No reconnect on a seqpacket socket */
672 if (sk->sk_state == TCP_ESTABLISHED)
675 sk->sk_state = TCP_CLOSE;
676 sock->state = SS_UNCONNECTED;
679 if (addr_len != sizeof(struct sockaddr_x25) ||
680 addr->sx25_family != AF_X25)
684 rt = x25_get_route(&addr->sx25_addr);
688 x25->neighbour = x25_get_neigh(rt->dev);
692 x25_limit_facilities(&x25->facilities, x25->neighbour);
694 x25->lci = x25_new_lci(x25->neighbour);
699 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
702 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
703 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
705 x25->dest_addr = addr->sx25_addr;
707 /* Move to connecting socket, start sending Connect Requests */
708 sock->state = SS_CONNECTING;
709 sk->sk_state = TCP_SYN_SENT;
711 x25->state = X25_STATE_1;
713 x25_write_internal(sk, X25_CALL_REQUEST);
715 x25_start_heartbeat(sk);
716 x25_start_t21timer(sk);
720 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
723 rc = x25_wait_for_connection_establishment(sk);
727 sock->state = SS_CONNECTED;
731 x25_neigh_put(x25->neighbour);
739 static int x25_wait_for_data(struct sock *sk, int timeout)
741 DECLARE_WAITQUEUE(wait, current);
744 add_wait_queue_exclusive(sk->sk_sleep, &wait);
746 __set_current_state(TASK_INTERRUPTIBLE);
747 if (sk->sk_shutdown & RCV_SHUTDOWN)
750 if (signal_pending(current))
756 if (skb_queue_empty(&sk->sk_receive_queue)) {
758 timeout = schedule_timeout(timeout);
763 __set_current_state(TASK_RUNNING);
764 remove_wait_queue(sk->sk_sleep, &wait);
768 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
770 struct sock *sk = sock->sk;
775 if (!sk || sk->sk_state != TCP_LISTEN)
779 if (sk->sk_type != SOCK_SEQPACKET)
783 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
786 skb = skb_dequeue(&sk->sk_receive_queue);
791 newsk->sk_socket = newsock;
792 newsk->sk_sleep = &newsock->wait;
794 /* Now attach up the new socket */
797 sk->sk_ack_backlog--;
799 newsock->state = SS_CONNECTED;
807 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
808 int *uaddr_len, int peer)
810 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
811 struct sock *sk = sock->sk;
812 struct x25_sock *x25 = x25_sk(sk);
815 if (sk->sk_state != TCP_ESTABLISHED)
817 sx25->sx25_addr = x25->dest_addr;
819 sx25->sx25_addr = x25->source_addr;
821 sx25->sx25_family = AF_X25;
822 *uaddr_len = sizeof(*sx25);
827 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
832 struct x25_sock *makex25;
833 struct x25_address source_addr, dest_addr;
834 struct x25_facilities facilities;
838 * Remove the LCI and frame type.
840 skb_pull(skb, X25_STD_MIN_LEN);
843 * Extract the X.25 addresses and convert them to ASCII strings,
846 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
849 * Get the length of the facilities, skip past them for the moment
850 * get the call user data because this is needed to determine
851 * the correct listener
853 len = skb->data[0] + 1;
857 * Find a listener for the particular address/cud pair.
859 sk = x25_find_listener(&source_addr,skb);
863 * We can't accept the Call Request.
865 if (sk == NULL || sk_acceptq_is_full(sk))
866 goto out_clear_request;
869 * Try to reach a compromise on the requested facilities.
871 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
875 * current neighbour/link might impose additional limits
876 * on certain facilties
879 x25_limit_facilities(&facilities, nb);
882 * Try to create a new socket.
884 make = x25_make_new(sk);
889 * Remove the facilities
894 make->sk_state = TCP_ESTABLISHED;
896 makex25 = x25_sk(make);
898 makex25->dest_addr = dest_addr;
899 makex25->source_addr = source_addr;
900 makex25->neighbour = nb;
901 makex25->facilities = facilities;
902 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
903 /* ensure no reverse facil on accept */
904 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
905 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
907 /* Normally all calls are accepted immediatly */
908 if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
909 x25_write_internal(make, X25_CALL_ACCEPTED);
910 makex25->state = X25_STATE_3;
914 * Incoming Call User Data.
917 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
918 makex25->calluserdata.cudlength = skb->len;
921 sk->sk_ack_backlog++;
923 x25_insert_socket(make);
925 skb_queue_head(&sk->sk_receive_queue, skb);
927 x25_start_heartbeat(make);
929 if (!sock_flag(sk, SOCK_DEAD))
930 sk->sk_data_ready(sk, skb->len);
939 x25_transmit_clear_request(nb, lci, 0x01);
943 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
944 struct msghdr *msg, size_t len)
946 struct sock *sk = sock->sk;
947 struct x25_sock *x25 = x25_sk(sk);
948 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
949 struct sockaddr_x25 sx25;
951 unsigned char *asmptr;
952 int noblock = msg->msg_flags & MSG_DONTWAIT;
954 int qbit = 0, rc = -EINVAL;
956 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
959 /* we currently don't support segmented records at the user interface */
960 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
964 if (sock_flag(sk, SOCK_ZAPPED))
968 if (sk->sk_shutdown & SEND_SHUTDOWN) {
969 send_sig(SIGPIPE, current, 0);
979 if (msg->msg_namelen < sizeof(sx25))
981 memcpy(&sx25, usx25, sizeof(sx25));
983 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
986 if (sx25.sx25_family != AF_X25)
990 * FIXME 1003.1g - if the socket is like this because
991 * it has become closed (not started closed) we ought
995 if (sk->sk_state != TCP_ESTABLISHED)
998 sx25.sx25_family = AF_X25;
999 sx25.sx25_addr = x25->dest_addr;
1002 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
1004 /* Build a packet */
1005 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1007 if ((msg->msg_flags & MSG_OOB) && len > 32)
1010 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1012 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1015 X25_SKB_CB(skb)->flags = msg->msg_flags;
1017 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1020 * Put the data on the end
1022 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1024 asmptr = skb->h.raw = skb_put(skb, len);
1026 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1031 * If the Q BIT Include socket option is in force, the first
1032 * byte of the user data is the logical value of the Q Bit.
1034 if (x25->qbitincl) {
1035 qbit = skb->data[0];
1040 * Push down the X.25 header
1042 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1044 if (msg->msg_flags & MSG_OOB) {
1045 if (x25->neighbour->extended) {
1046 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1047 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1048 *asmptr++ = (x25->lci >> 0) & 0xFF;
1049 *asmptr++ = X25_INTERRUPT;
1051 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1052 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1053 *asmptr++ = (x25->lci >> 0) & 0xFF;
1054 *asmptr++ = X25_INTERRUPT;
1057 if (x25->neighbour->extended) {
1058 /* Build an Extended X.25 header */
1059 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1060 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1061 *asmptr++ = (x25->lci >> 0) & 0xFF;
1062 *asmptr++ = X25_DATA;
1063 *asmptr++ = X25_DATA;
1065 /* Build an Standard X.25 header */
1066 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1067 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1068 *asmptr++ = (x25->lci >> 0) & 0xFF;
1069 *asmptr++ = X25_DATA;
1073 skb->data[0] |= X25_Q_BIT;
1076 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1077 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1080 if (sk->sk_state != TCP_ESTABLISHED)
1083 if (msg->msg_flags & MSG_OOB)
1084 skb_queue_tail(&x25->interrupt_out_queue, skb);
1086 len = x25_output(sk, skb);
1089 else if (x25->qbitincl)
1094 * lock_sock() is currently only used to serialize this x25_kick()
1095 * against input-driven x25_kick() calls. It currently only blocks
1096 * incoming packets for this socket and does not protect against
1097 * any other socket state changes and is not called from anywhere
1098 * else. As x25_kick() cannot block and as long as all socket
1099 * operations are BKL-wrapped, we don't need take to care about
1100 * purging the backlog queue in x25_release().
1102 * Using lock_sock() to protect all socket operations entirely
1103 * (and making the whole x25 stack SMP aware) unfortunately would
1104 * require major changes to {send,recv}msg and skb allocation methods.
1119 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1120 struct msghdr *msg, size_t size,
1123 struct sock *sk = sock->sk;
1124 struct x25_sock *x25 = x25_sk(sk);
1125 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1128 struct sk_buff *skb;
1129 unsigned char *asmptr;
1133 * This works for seqpacket too. The receiver has ordered the queue for
1134 * us! We do one quick check first though
1136 if (sk->sk_state != TCP_ESTABLISHED)
1139 if (flags & MSG_OOB) {
1141 if (sock_flag(sk, SOCK_URGINLINE) ||
1142 !skb_peek(&x25->interrupt_in_queue))
1145 skb = skb_dequeue(&x25->interrupt_in_queue);
1147 skb_pull(skb, X25_STD_MIN_LEN);
1150 * No Q bit information on Interrupt data.
1152 if (x25->qbitincl) {
1153 asmptr = skb_push(skb, 1);
1157 msg->msg_flags |= MSG_OOB;
1159 /* Now we can treat all alike */
1160 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1161 flags & MSG_DONTWAIT, &rc);
1165 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1167 skb_pull(skb, x25->neighbour->extended ?
1168 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1170 if (x25->qbitincl) {
1171 asmptr = skb_push(skb, 1);
1176 skb->h.raw = skb->data;
1180 if (copied > size) {
1182 msg->msg_flags |= MSG_TRUNC;
1185 /* Currently, each datagram always contains a complete record */
1186 msg->msg_flags |= MSG_EOR;
1188 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1190 goto out_free_dgram;
1193 sx25->sx25_family = AF_X25;
1194 sx25->sx25_addr = x25->dest_addr;
1197 msg->msg_namelen = sizeof(struct sockaddr_x25);
1204 skb_free_datagram(sk, skb);
1210 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1212 struct sock *sk = sock->sk;
1213 struct x25_sock *x25 = x25_sk(sk);
1214 void __user *argp = (void __user *)arg;
1219 int amount = sk->sk_sndbuf -
1220 atomic_read(&sk->sk_wmem_alloc);
1223 rc = put_user(amount, (unsigned int __user *)argp);
1228 struct sk_buff *skb;
1231 * These two are safe on a single CPU system as
1232 * only user tasks fiddle here
1234 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1236 rc = put_user(amount, (unsigned int __user *)argp);
1243 rc = sock_get_timestamp(sk,
1244 (struct timeval __user *)argp);
1248 case SIOCGIFDSTADDR:
1249 case SIOCSIFDSTADDR:
1250 case SIOCGIFBRDADDR:
1251 case SIOCSIFBRDADDR:
1252 case SIOCGIFNETMASK:
1253 case SIOCSIFNETMASK:
1261 if (!capable(CAP_NET_ADMIN))
1263 rc = x25_route_ioctl(cmd, argp);
1265 case SIOCX25GSUBSCRIP:
1266 rc = x25_subscr_ioctl(cmd, argp);
1268 case SIOCX25SSUBSCRIP:
1270 if (!capable(CAP_NET_ADMIN))
1272 rc = x25_subscr_ioctl(cmd, argp);
1274 case SIOCX25GFACILITIES: {
1275 struct x25_facilities fac = x25->facilities;
1276 rc = copy_to_user(argp, &fac,
1277 sizeof(fac)) ? -EFAULT : 0;
1281 case SIOCX25SFACILITIES: {
1282 struct x25_facilities facilities;
1284 if (copy_from_user(&facilities, argp,
1285 sizeof(facilities)))
1288 if (sk->sk_state != TCP_LISTEN &&
1289 sk->sk_state != TCP_CLOSE)
1291 if (facilities.pacsize_in < X25_PS16 ||
1292 facilities.pacsize_in > X25_PS4096)
1294 if (facilities.pacsize_out < X25_PS16 ||
1295 facilities.pacsize_out > X25_PS4096)
1297 if (facilities.winsize_in < 1 ||
1298 facilities.winsize_in > 127)
1300 if (facilities.throughput < 0x03 ||
1301 facilities.throughput > 0xDD)
1303 if (facilities.reverse &&
1304 (facilities.reverse | 0x81)!= 0x81)
1306 x25->facilities = facilities;
1311 case SIOCX25GCALLUSERDATA: {
1312 struct x25_calluserdata cud = x25->calluserdata;
1313 rc = copy_to_user(argp, &cud,
1314 sizeof(cud)) ? -EFAULT : 0;
1318 case SIOCX25SCALLUSERDATA: {
1319 struct x25_calluserdata calluserdata;
1322 if (copy_from_user(&calluserdata, argp,
1323 sizeof(calluserdata)))
1326 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1328 x25->calluserdata = calluserdata;
1333 case SIOCX25GCAUSEDIAG: {
1334 struct x25_causediag causediag;
1335 causediag = x25->causediag;
1336 rc = copy_to_user(argp, &causediag,
1337 sizeof(causediag)) ? -EFAULT : 0;
1341 case SIOCX25SCUDMATCHLEN: {
1342 struct x25_subaddr sub_addr;
1344 if(sk->sk_state != TCP_CLOSE)
1347 if (copy_from_user(&sub_addr, argp,
1351 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1353 x25->cudmatchlength = sub_addr.cudmatchlength;
1358 case SIOCX25CALLACCPTAPPRV: {
1360 if (sk->sk_state != TCP_CLOSE)
1362 x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1367 case SIOCX25SENDCALLACCPT: {
1369 if (sk->sk_state != TCP_ESTABLISHED)
1371 if (x25->accptapprv) /* must call accptapprv above */
1373 x25_write_internal(sk, X25_CALL_ACCEPTED);
1374 x25->state = X25_STATE_3;
1387 static struct net_proto_family x25_family_ops = {
1389 .create = x25_create,
1390 .owner = THIS_MODULE,
1393 static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1395 .owner = THIS_MODULE,
1396 .release = x25_release,
1398 .connect = x25_connect,
1399 .socketpair = sock_no_socketpair,
1400 .accept = x25_accept,
1401 .getname = x25_getname,
1402 .poll = datagram_poll,
1404 .listen = x25_listen,
1405 .shutdown = sock_no_shutdown,
1406 .setsockopt = x25_setsockopt,
1407 .getsockopt = x25_getsockopt,
1408 .sendmsg = x25_sendmsg,
1409 .recvmsg = x25_recvmsg,
1410 .mmap = sock_no_mmap,
1411 .sendpage = sock_no_sendpage,
1414 #include <linux/smp_lock.h>
1415 SOCKOPS_WRAP(x25_proto, AF_X25);
1417 static struct packet_type x25_packet_type = {
1418 .type = __constant_htons(ETH_P_X25),
1419 .func = x25_lapb_receive_frame,
1422 static struct notifier_block x25_dev_notifier = {
1423 .notifier_call = x25_device_event,
1426 void x25_kill_by_neigh(struct x25_neigh *nb)
1429 struct hlist_node *node;
1431 write_lock_bh(&x25_list_lock);
1433 sk_for_each(s, node, &x25_list)
1434 if (x25_sk(s)->neighbour == nb)
1435 x25_disconnect(s, ENETUNREACH, 0, 0);
1437 write_unlock_bh(&x25_list_lock);
1440 static int __init x25_init(void)
1442 int rc = proto_register(&x25_proto, 0);
1447 sock_register(&x25_family_ops);
1449 dev_add_pack(&x25_packet_type);
1451 register_netdevice_notifier(&x25_dev_notifier);
1453 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1455 #ifdef CONFIG_SYSCTL
1456 x25_register_sysctl();
1462 module_init(x25_init);
1464 static void __exit x25_exit(void)
1470 #ifdef CONFIG_SYSCTL
1471 x25_unregister_sysctl();
1474 unregister_netdevice_notifier(&x25_dev_notifier);
1476 dev_remove_pack(&x25_packet_type);
1478 sock_unregister(AF_X25);
1479 proto_unregister(&x25_proto);
1481 module_exit(x25_exit);
1483 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1484 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1485 MODULE_LICENSE("GPL");
1486 MODULE_ALIAS_NETPROTO(PF_X25);