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
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <linux/string.h>
43 #include <linux/net.h>
44 #include <linux/netdevice.h>
45 #include <linux/if_arp.h>
46 #include <linux/skbuff.h>
49 #include <asm/uaccess.h>
50 #include <linux/fcntl.h>
51 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
52 #include <linux/notifier.h>
53 #include <linux/init.h>
56 int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
57 int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
58 int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
59 int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
60 int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
63 DEFINE_RWLOCK(x25_list_lock);
65 static struct proto_ops x25_proto_ops;
67 static struct x25_address null_x25_address = {" "};
69 int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
70 struct x25_address *calling_addr)
72 int called_len, calling_len;
73 char *called, *calling;
76 called_len = (*p >> 0) & 0x0F;
77 calling_len = (*p >> 4) & 0x0F;
79 called = called_addr->x25_addr;
80 calling = calling_addr->x25_addr;
83 for (i = 0; i < (called_len + calling_len); i++) {
86 *called++ = ((*p >> 0) & 0x0F) + '0';
89 *called++ = ((*p >> 4) & 0x0F) + '0';
93 *calling++ = ((*p >> 0) & 0x0F) + '0';
96 *calling++ = ((*p >> 4) & 0x0F) + '0';
101 *called = *calling = '\0';
103 return 1 + (called_len + calling_len + 1) / 2;
106 int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
107 struct x25_address *calling_addr)
109 unsigned int called_len, calling_len;
110 char *called, *calling;
113 called = called_addr->x25_addr;
114 calling = calling_addr->x25_addr;
116 called_len = strlen(called);
117 calling_len = strlen(calling);
119 *p++ = (calling_len << 4) | (called_len << 0);
121 for (i = 0; i < (called_len + calling_len); i++) {
122 if (i < called_len) {
124 *p |= (*called++ - '0') << 0;
128 *p |= (*called++ - '0') << 4;
132 *p |= (*calling++ - '0') << 0;
136 *p |= (*calling++ - '0') << 4;
141 return 1 + (called_len + calling_len + 1) / 2;
145 * Socket removal during an interrupt is now safe.
147 static void x25_remove_socket(struct sock *sk)
149 write_lock_bh(&x25_list_lock);
150 sk_del_node_init(sk);
151 write_unlock_bh(&x25_list_lock);
155 * Kill all bound sockets on a dropped device.
157 static void x25_kill_by_device(struct net_device *dev)
160 struct hlist_node *node;
162 write_lock_bh(&x25_list_lock);
164 sk_for_each(s, node, &x25_list)
165 if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
166 x25_disconnect(s, ENETUNREACH, 0, 0);
168 write_unlock_bh(&x25_list_lock);
172 * Handle device status changes.
174 static int x25_device_event(struct notifier_block *this, unsigned long event,
177 struct net_device *dev = ptr;
178 struct x25_neigh *nb;
180 if (dev->type == ARPHRD_X25
181 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
182 || dev->type == ARPHRD_ETHER
187 x25_link_device_up(dev);
189 case NETDEV_GOING_DOWN:
190 nb = x25_get_neigh(dev);
192 x25_terminate_link(nb);
197 x25_kill_by_device(dev);
198 x25_route_device_down(dev);
199 x25_link_device_down(dev);
208 * Add a socket to the bound sockets list.
210 static void x25_insert_socket(struct sock *sk)
212 write_lock_bh(&x25_list_lock);
213 sk_add_node(sk, &x25_list);
214 write_unlock_bh(&x25_list_lock);
218 * Find a socket that wants to accept the Call Request we just
219 * received. Check the full list for an address/cud match.
220 * If no cuds match return the next_best thing, an address match.
221 * Note: if a listening socket has cud set it must only get calls
224 static struct sock *x25_find_listener(struct x25_address *addr,
228 struct sock *next_best;
229 struct hlist_node *node;
231 read_lock_bh(&x25_list_lock);
234 sk_for_each(s, node, &x25_list)
235 if ((!strcmp(addr->x25_addr,
236 x25_sk(s)->source_addr.x25_addr) ||
237 !strcmp(addr->x25_addr,
238 null_x25_address.x25_addr)) &&
239 s->sk_state == TCP_LISTEN) {
241 * Found a listening socket, now check the incoming
242 * call user data vs this sockets call user data
244 if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
245 if((memcmp(x25_sk(s)->calluserdata.cuddata,
247 x25_sk(s)->cudmatchlength)) == 0) {
261 read_unlock_bh(&x25_list_lock);
266 * Find a connected X.25 socket given my LCI and neighbour.
268 static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
271 struct hlist_node *node;
273 sk_for_each(s, node, &x25_list)
274 if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
283 struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
287 read_lock_bh(&x25_list_lock);
288 s = __x25_find_socket(lci, nb);
289 read_unlock_bh(&x25_list_lock);
294 * Find a unique LCI for a given device.
296 static unsigned int x25_new_lci(struct x25_neigh *nb)
298 unsigned int lci = 1;
301 read_lock_bh(&x25_list_lock);
303 while ((sk = __x25_find_socket(lci, nb)) != NULL) {
311 read_unlock_bh(&x25_list_lock);
318 void x25_destroy_socket(struct sock *);
321 * handler for deferred kills.
323 static void x25_destroy_timer(unsigned long data)
325 x25_destroy_socket((struct sock *)data);
329 * This is called from user mode and the timers. Thus it protects itself
330 * against interrupt users but doesn't worry about being called during
331 * work. Once it is removed from the queue no interrupt or bottom half
332 * will touch it and we are (fairly 8-) ) safe.
333 * Not static as it's used by the timer
335 void x25_destroy_socket(struct sock *sk)
341 x25_stop_heartbeat(sk);
344 x25_remove_socket(sk);
345 x25_clear_queues(sk); /* Flush the queues */
347 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
348 if (skb->sk != sk) { /* A pending connection */
350 * Queue the unaccepted socket for death
352 sock_set_flag(skb->sk, SOCK_DEAD);
353 x25_start_heartbeat(skb->sk);
354 x25_sk(skb->sk)->state = X25_STATE_0;
360 if (atomic_read(&sk->sk_wmem_alloc) ||
361 atomic_read(&sk->sk_rmem_alloc)) {
362 /* Defer: outstanding buffers */
363 sk->sk_timer.expires = jiffies + 10 * HZ;
364 sk->sk_timer.function = x25_destroy_timer;
365 sk->sk_timer.data = (unsigned long)sk;
366 add_timer(&sk->sk_timer);
368 /* drop last reference so sock_put will free */
377 * Handling for system calls applied via the various interfaces to a
378 * X.25 socket object.
381 static int x25_setsockopt(struct socket *sock, int level, int optname,
382 char __user *optval, int optlen)
385 struct sock *sk = sock->sk;
386 int rc = -ENOPROTOOPT;
388 if (level != SOL_X25 || optname != X25_QBITINCL)
392 if (optlen < sizeof(int))
396 if (get_user(opt, (int __user *)optval))
399 x25_sk(sk)->qbitincl = !!opt;
405 static int x25_getsockopt(struct socket *sock, int level, int optname,
406 char __user *optval, int __user *optlen)
408 struct sock *sk = sock->sk;
409 int val, len, rc = -ENOPROTOOPT;
411 if (level != SOL_X25 || optname != X25_QBITINCL)
415 if (get_user(len, optlen))
418 len = min_t(unsigned int, len, sizeof(int));
425 if (put_user(len, optlen))
428 val = x25_sk(sk)->qbitincl;
429 rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
434 static int x25_listen(struct socket *sock, int backlog)
436 struct sock *sk = sock->sk;
437 int rc = -EOPNOTSUPP;
439 if (sk->sk_state != TCP_LISTEN) {
440 memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
441 sk->sk_max_ack_backlog = backlog;
442 sk->sk_state = TCP_LISTEN;
449 static struct proto x25_proto = {
451 .owner = THIS_MODULE,
452 .obj_size = sizeof(struct x25_sock),
455 static struct sock *x25_alloc_socket(void)
457 struct x25_sock *x25;
458 struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1);
463 sock_init_data(NULL, sk);
466 skb_queue_head_init(&x25->ack_queue);
467 skb_queue_head_init(&x25->fragment_queue);
468 skb_queue_head_init(&x25->interrupt_in_queue);
469 skb_queue_head_init(&x25->interrupt_out_queue);
474 void x25_init_timers(struct sock *sk);
476 static int x25_create(struct socket *sock, int protocol)
479 struct x25_sock *x25;
480 int rc = -ESOCKTNOSUPPORT;
482 if (sock->type != SOCK_SEQPACKET || protocol)
486 if ((sk = x25_alloc_socket()) == NULL)
491 sock_init_data(sock, sk);
495 sock->ops = &x25_proto_ops;
496 sk->sk_protocol = protocol;
497 sk->sk_backlog_rcv = x25_backlog_rcv;
499 x25->t21 = sysctl_x25_call_request_timeout;
500 x25->t22 = sysctl_x25_reset_request_timeout;
501 x25->t23 = sysctl_x25_clear_request_timeout;
502 x25->t2 = sysctl_x25_ack_holdback_timeout;
503 x25->state = X25_STATE_0;
504 x25->cudmatchlength = 0;
506 x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
507 x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
508 x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
509 x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
510 x25->facilities.throughput = X25_DEFAULT_THROUGHPUT;
511 x25->facilities.reverse = X25_DEFAULT_REVERSE;
517 static struct sock *x25_make_new(struct sock *osk)
519 struct sock *sk = NULL;
520 struct x25_sock *x25, *ox25;
522 if (osk->sk_type != SOCK_SEQPACKET)
525 if ((sk = x25_alloc_socket()) == NULL)
530 sk->sk_type = osk->sk_type;
531 sk->sk_socket = osk->sk_socket;
532 sk->sk_priority = osk->sk_priority;
533 sk->sk_protocol = osk->sk_protocol;
534 sk->sk_rcvbuf = osk->sk_rcvbuf;
535 sk->sk_sndbuf = osk->sk_sndbuf;
536 sk->sk_state = TCP_ESTABLISHED;
537 sk->sk_sleep = osk->sk_sleep;
538 sk->sk_backlog_rcv = osk->sk_backlog_rcv;
540 if (sock_flag(osk, SOCK_ZAPPED))
541 sock_set_flag(sk, SOCK_ZAPPED);
543 if (sock_flag(osk, SOCK_DBG))
544 sock_set_flag(sk, SOCK_DBG);
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;
560 static int x25_release(struct socket *sock)
562 struct sock *sk = sock->sk;
563 struct x25_sock *x25;
570 switch (x25->state) {
574 x25_disconnect(sk, 0, 0, 0);
575 x25_destroy_socket(sk);
581 x25_clear_queues(sk);
582 x25_write_internal(sk, X25_CLEAR_REQUEST);
583 x25_start_t23timer(sk);
584 x25->state = X25_STATE_2;
585 sk->sk_state = TCP_CLOSE;
586 sk->sk_shutdown |= SEND_SHUTDOWN;
587 sk->sk_state_change(sk);
588 sock_set_flag(sk, SOCK_DEAD);
589 sock_set_flag(sk, SOCK_DESTROY);
594 sk->sk_socket = NULL; /* Not used, but we should do this */
599 static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
601 struct sock *sk = sock->sk;
602 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
604 if (!sock_flag(sk, SOCK_ZAPPED) ||
605 addr_len != sizeof(struct sockaddr_x25) ||
606 addr->sx25_family != AF_X25)
609 x25_sk(sk)->source_addr = addr->sx25_addr;
610 x25_insert_socket(sk);
611 sock_reset_flag(sk, SOCK_ZAPPED);
612 SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
617 static int x25_wait_for_connection_establishment(struct sock *sk)
619 DECLARE_WAITQUEUE(wait, current);
622 add_wait_queue_exclusive(sk->sk_sleep, &wait);
624 __set_current_state(TASK_INTERRUPTIBLE);
626 if (signal_pending(current))
630 sk->sk_socket->state = SS_UNCONNECTED;
634 if (sk->sk_state != TCP_ESTABLISHED) {
641 __set_current_state(TASK_RUNNING);
642 remove_wait_queue(sk->sk_sleep, &wait);
646 static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
647 int addr_len, int flags)
649 struct sock *sk = sock->sk;
650 struct x25_sock *x25 = x25_sk(sk);
651 struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
652 struct x25_route *rt;
656 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
657 sock->state = SS_CONNECTED;
658 goto out; /* Connect completed during a ERESTARTSYS event */
662 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
663 sock->state = SS_UNCONNECTED;
667 rc = -EISCONN; /* No reconnect on a seqpacket socket */
668 if (sk->sk_state == TCP_ESTABLISHED)
671 sk->sk_state = TCP_CLOSE;
672 sock->state = SS_UNCONNECTED;
675 if (addr_len != sizeof(struct sockaddr_x25) ||
676 addr->sx25_family != AF_X25)
680 rt = x25_get_route(&addr->sx25_addr);
684 x25->neighbour = x25_get_neigh(rt->dev);
688 x25_limit_facilities(&x25->facilities, x25->neighbour);
690 x25->lci = x25_new_lci(x25->neighbour);
695 if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
698 if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
699 memset(&x25->source_addr, '\0', X25_ADDR_LEN);
701 x25->dest_addr = addr->sx25_addr;
703 /* Move to connecting socket, start sending Connect Requests */
704 sock->state = SS_CONNECTING;
705 sk->sk_state = TCP_SYN_SENT;
707 x25->state = X25_STATE_1;
709 x25_write_internal(sk, X25_CALL_REQUEST);
711 x25_start_heartbeat(sk);
712 x25_start_t21timer(sk);
716 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
719 rc = x25_wait_for_connection_establishment(sk);
723 sock->state = SS_CONNECTED;
727 x25_neigh_put(x25->neighbour);
735 static int x25_wait_for_data(struct sock *sk, int timeout)
737 DECLARE_WAITQUEUE(wait, current);
740 add_wait_queue_exclusive(sk->sk_sleep, &wait);
742 __set_current_state(TASK_INTERRUPTIBLE);
743 if (sk->sk_shutdown & RCV_SHUTDOWN)
746 if (signal_pending(current))
752 if (skb_queue_empty(&sk->sk_receive_queue)) {
754 timeout = schedule_timeout(timeout);
759 __set_current_state(TASK_RUNNING);
760 remove_wait_queue(sk->sk_sleep, &wait);
764 static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
766 struct sock *sk = sock->sk;
771 if (!sk || sk->sk_state != TCP_LISTEN)
775 if (sk->sk_type != SOCK_SEQPACKET)
779 rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
782 skb = skb_dequeue(&sk->sk_receive_queue);
787 newsk->sk_socket = newsock;
788 newsk->sk_sleep = &newsock->wait;
790 /* Now attach up the new socket */
793 sk->sk_ack_backlog--;
795 newsock->state = SS_CONNECTED;
803 static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
804 int *uaddr_len, int peer)
806 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
807 struct sock *sk = sock->sk;
808 struct x25_sock *x25 = x25_sk(sk);
811 if (sk->sk_state != TCP_ESTABLISHED)
813 sx25->sx25_addr = x25->dest_addr;
815 sx25->sx25_addr = x25->source_addr;
817 sx25->sx25_family = AF_X25;
818 *uaddr_len = sizeof(*sx25);
823 int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
828 struct x25_sock *makex25;
829 struct x25_address source_addr, dest_addr;
830 struct x25_facilities facilities;
834 * Remove the LCI and frame type.
836 skb_pull(skb, X25_STD_MIN_LEN);
839 * Extract the X.25 addresses and convert them to ASCII strings,
842 skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr));
845 * Get the length of the facilities, skip past them for the moment
846 * get the call user data because this is needed to determine
847 * the correct listener
849 len = skb->data[0] + 1;
853 * Find a listener for the particular address/cud pair.
855 sk = x25_find_listener(&source_addr,skb);
859 * We can't accept the Call Request.
861 if (sk == NULL || sk_acceptq_is_full(sk))
862 goto out_clear_request;
865 * Try to reach a compromise on the requested facilities.
867 if ((len = x25_negotiate_facilities(skb, sk, &facilities)) == -1)
871 * current neighbour/link might impose additional limits
872 * on certain facilties
875 x25_limit_facilities(&facilities, nb);
878 * Try to create a new socket.
880 make = x25_make_new(sk);
885 * Remove the facilities
890 make->sk_state = TCP_ESTABLISHED;
892 makex25 = x25_sk(make);
894 makex25->dest_addr = dest_addr;
895 makex25->source_addr = source_addr;
896 makex25->neighbour = nb;
897 makex25->facilities = facilities;
898 makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
899 /* ensure no reverse facil on accept */
900 makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
901 makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
903 x25_write_internal(make, X25_CALL_ACCEPTED);
905 makex25->state = X25_STATE_3;
908 * Incoming Call User Data.
911 memcpy(makex25->calluserdata.cuddata, skb->data, skb->len);
912 makex25->calluserdata.cudlength = skb->len;
915 sk->sk_ack_backlog++;
917 x25_insert_socket(make);
919 skb_queue_head(&sk->sk_receive_queue, skb);
921 x25_start_heartbeat(make);
923 if (!sock_flag(sk, SOCK_DEAD))
924 sk->sk_data_ready(sk, skb->len);
933 x25_transmit_clear_request(nb, lci, 0x01);
937 static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
938 struct msghdr *msg, size_t len)
940 struct sock *sk = sock->sk;
941 struct x25_sock *x25 = x25_sk(sk);
942 struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
943 struct sockaddr_x25 sx25;
945 unsigned char *asmptr;
946 int noblock = msg->msg_flags & MSG_DONTWAIT;
948 int qbit = 0, rc = -EINVAL;
950 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
953 /* we currently don't support segmented records at the user interface */
954 if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
958 if (sock_flag(sk, SOCK_ZAPPED))
962 if (sk->sk_shutdown & SEND_SHUTDOWN) {
963 send_sig(SIGPIPE, current, 0);
973 if (msg->msg_namelen < sizeof(sx25))
975 memcpy(&sx25, usx25, sizeof(sx25));
977 if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
980 if (sx25.sx25_family != AF_X25)
984 * FIXME 1003.1g - if the socket is like this because
985 * it has become closed (not started closed) we ought
989 if (sk->sk_state != TCP_ESTABLISHED)
992 sx25.sx25_family = AF_X25;
993 sx25.sx25_addr = x25->dest_addr;
996 SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
999 SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
1001 if ((msg->msg_flags & MSG_OOB) && len > 32)
1004 size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
1006 skb = sock_alloc_send_skb(sk, size, noblock, &rc);
1009 X25_SKB_CB(skb)->flags = msg->msg_flags;
1011 skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
1014 * Put the data on the end
1016 SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
1018 asmptr = skb->h.raw = skb_put(skb, len);
1020 rc = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1025 * If the Q BIT Include socket option is in force, the first
1026 * byte of the user data is the logical value of the Q Bit.
1028 if (x25->qbitincl) {
1029 qbit = skb->data[0];
1034 * Push down the X.25 header
1036 SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
1038 if (msg->msg_flags & MSG_OOB) {
1039 if (x25->neighbour->extended) {
1040 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1041 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1042 *asmptr++ = (x25->lci >> 0) & 0xFF;
1043 *asmptr++ = X25_INTERRUPT;
1045 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1046 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1047 *asmptr++ = (x25->lci >> 0) & 0xFF;
1048 *asmptr++ = X25_INTERRUPT;
1051 if (x25->neighbour->extended) {
1052 /* Build an Extended X.25 header */
1053 asmptr = skb_push(skb, X25_EXT_MIN_LEN);
1054 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
1055 *asmptr++ = (x25->lci >> 0) & 0xFF;
1056 *asmptr++ = X25_DATA;
1057 *asmptr++ = X25_DATA;
1059 /* Build an Standard X.25 header */
1060 asmptr = skb_push(skb, X25_STD_MIN_LEN);
1061 *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
1062 *asmptr++ = (x25->lci >> 0) & 0xFF;
1063 *asmptr++ = X25_DATA;
1067 skb->data[0] |= X25_Q_BIT;
1070 SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
1071 SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
1074 if (sk->sk_state != TCP_ESTABLISHED)
1077 if (msg->msg_flags & MSG_OOB)
1078 skb_queue_tail(&x25->interrupt_out_queue, skb);
1080 len = x25_output(sk, skb);
1083 else if (x25->qbitincl)
1088 * lock_sock() is currently only used to serialize this x25_kick()
1089 * against input-driven x25_kick() calls. It currently only blocks
1090 * incoming packets for this socket and does not protect against
1091 * any other socket state changes and is not called from anywhere
1092 * else. As x25_kick() cannot block and as long as all socket
1093 * operations are BKL-wrapped, we don't need take to care about
1094 * purging the backlog queue in x25_release().
1096 * Using lock_sock() to protect all socket operations entirely
1097 * (and making the whole x25 stack SMP aware) unfortunately would
1098 * require major changes to {send,recv}msg and skb allocation methods.
1113 static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
1114 struct msghdr *msg, size_t size,
1117 struct sock *sk = sock->sk;
1118 struct x25_sock *x25 = x25_sk(sk);
1119 struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
1122 struct sk_buff *skb;
1123 unsigned char *asmptr;
1127 * This works for seqpacket too. The receiver has ordered the queue for
1128 * us! We do one quick check first though
1130 if (sk->sk_state != TCP_ESTABLISHED)
1133 if (flags & MSG_OOB) {
1135 if (sock_flag(sk, SOCK_URGINLINE) ||
1136 !skb_peek(&x25->interrupt_in_queue))
1139 skb = skb_dequeue(&x25->interrupt_in_queue);
1141 skb_pull(skb, X25_STD_MIN_LEN);
1144 * No Q bit information on Interrupt data.
1146 if (x25->qbitincl) {
1147 asmptr = skb_push(skb, 1);
1151 msg->msg_flags |= MSG_OOB;
1153 /* Now we can treat all alike */
1154 skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
1155 flags & MSG_DONTWAIT, &rc);
1159 qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
1161 skb_pull(skb, x25->neighbour->extended ?
1162 X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
1164 if (x25->qbitincl) {
1165 asmptr = skb_push(skb, 1);
1170 skb->h.raw = skb->data;
1174 if (copied > size) {
1176 msg->msg_flags |= MSG_TRUNC;
1179 /* Currently, each datagram always contains a complete record */
1180 msg->msg_flags |= MSG_EOR;
1182 rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1184 goto out_free_dgram;
1187 sx25->sx25_family = AF_X25;
1188 sx25->sx25_addr = x25->dest_addr;
1191 msg->msg_namelen = sizeof(struct sockaddr_x25);
1198 skb_free_datagram(sk, skb);
1204 static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1206 struct sock *sk = sock->sk;
1207 struct x25_sock *x25 = x25_sk(sk);
1208 void __user *argp = (void __user *)arg;
1213 int amount = sk->sk_sndbuf -
1214 atomic_read(&sk->sk_wmem_alloc);
1217 rc = put_user(amount, (unsigned int __user *)argp);
1222 struct sk_buff *skb;
1225 * These two are safe on a single CPU system as
1226 * only user tasks fiddle here
1228 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1230 rc = put_user(amount, (unsigned int __user *)argp);
1237 rc = sock_get_timestamp(sk,
1238 (struct timeval __user *)argp);
1242 case SIOCGIFDSTADDR:
1243 case SIOCSIFDSTADDR:
1244 case SIOCGIFBRDADDR:
1245 case SIOCSIFBRDADDR:
1246 case SIOCGIFNETMASK:
1247 case SIOCSIFNETMASK:
1255 if (!capable(CAP_NET_ADMIN))
1257 rc = x25_route_ioctl(cmd, argp);
1259 case SIOCX25GSUBSCRIP:
1260 rc = x25_subscr_ioctl(cmd, argp);
1262 case SIOCX25SSUBSCRIP:
1264 if (!capable(CAP_NET_ADMIN))
1266 rc = x25_subscr_ioctl(cmd, argp);
1268 case SIOCX25GFACILITIES: {
1269 struct x25_facilities fac = x25->facilities;
1270 rc = copy_to_user(argp, &fac,
1271 sizeof(fac)) ? -EFAULT : 0;
1275 case SIOCX25SFACILITIES: {
1276 struct x25_facilities facilities;
1278 if (copy_from_user(&facilities, argp,
1279 sizeof(facilities)))
1282 if (sk->sk_state != TCP_LISTEN &&
1283 sk->sk_state != TCP_CLOSE)
1285 if (facilities.pacsize_in < X25_PS16 ||
1286 facilities.pacsize_in > X25_PS4096)
1288 if (facilities.pacsize_out < X25_PS16 ||
1289 facilities.pacsize_out > X25_PS4096)
1291 if (facilities.winsize_in < 1 ||
1292 facilities.winsize_in > 127)
1294 if (facilities.throughput < 0x03 ||
1295 facilities.throughput > 0xDD)
1297 if (facilities.reverse && facilities.reverse != 1)
1299 x25->facilities = facilities;
1304 case SIOCX25GCALLUSERDATA: {
1305 struct x25_calluserdata cud = x25->calluserdata;
1306 rc = copy_to_user(argp, &cud,
1307 sizeof(cud)) ? -EFAULT : 0;
1311 case SIOCX25SCALLUSERDATA: {
1312 struct x25_calluserdata calluserdata;
1315 if (copy_from_user(&calluserdata, argp,
1316 sizeof(calluserdata)))
1319 if (calluserdata.cudlength > X25_MAX_CUD_LEN)
1321 x25->calluserdata = calluserdata;
1326 case SIOCX25GCAUSEDIAG: {
1327 struct x25_causediag causediag;
1328 causediag = x25->causediag;
1329 rc = copy_to_user(argp, &causediag,
1330 sizeof(causediag)) ? -EFAULT : 0;
1334 case SIOCX25SCUDMATCHLEN: {
1335 struct x25_subaddr sub_addr;
1337 if(sk->sk_state != TCP_CLOSE)
1340 if (copy_from_user(&sub_addr, argp,
1344 if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1346 x25->cudmatchlength = sub_addr.cudmatchlength;
1352 rc = dev_ioctl(cmd, argp);
1359 static struct net_proto_family x25_family_ops = {
1361 .create = x25_create,
1362 .owner = THIS_MODULE,
1365 static struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = {
1367 .owner = THIS_MODULE,
1368 .release = x25_release,
1370 .connect = x25_connect,
1371 .socketpair = sock_no_socketpair,
1372 .accept = x25_accept,
1373 .getname = x25_getname,
1374 .poll = datagram_poll,
1376 .listen = x25_listen,
1377 .shutdown = sock_no_shutdown,
1378 .setsockopt = x25_setsockopt,
1379 .getsockopt = x25_getsockopt,
1380 .sendmsg = x25_sendmsg,
1381 .recvmsg = x25_recvmsg,
1382 .mmap = sock_no_mmap,
1383 .sendpage = sock_no_sendpage,
1386 #include <linux/smp_lock.h>
1387 SOCKOPS_WRAP(x25_proto, AF_X25);
1389 static struct packet_type x25_packet_type = {
1390 .type = __constant_htons(ETH_P_X25),
1391 .func = x25_lapb_receive_frame,
1394 static struct notifier_block x25_dev_notifier = {
1395 .notifier_call = x25_device_event,
1398 void x25_kill_by_neigh(struct x25_neigh *nb)
1401 struct hlist_node *node;
1403 write_lock_bh(&x25_list_lock);
1405 sk_for_each(s, node, &x25_list)
1406 if (x25_sk(s)->neighbour == nb)
1407 x25_disconnect(s, ENETUNREACH, 0, 0);
1409 write_unlock_bh(&x25_list_lock);
1412 static int __init x25_init(void)
1414 int rc = proto_register(&x25_proto, 0);
1419 sock_register(&x25_family_ops);
1421 dev_add_pack(&x25_packet_type);
1423 register_netdevice_notifier(&x25_dev_notifier);
1425 printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1427 #ifdef CONFIG_SYSCTL
1428 x25_register_sysctl();
1434 module_init(x25_init);
1436 static void __exit x25_exit(void)
1442 #ifdef CONFIG_SYSCTL
1443 x25_unregister_sysctl();
1446 unregister_netdevice_notifier(&x25_dev_notifier);
1448 dev_remove_pack(&x25_packet_type);
1450 sock_unregister(AF_X25);
1451 proto_unregister(&x25_proto);
1453 module_exit(x25_exit);
1455 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1456 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1457 MODULE_LICENSE("GPL");
1458 MODULE_ALIAS_NETPROTO(PF_X25);