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
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/errno.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/timer.h>
40 #include <linux/string.h>
41 #include <linux/net.h>
42 #include <linux/netdevice.h>
43 #include <linux/if_arp.h>
44 #include <linux/skbuff.h>
47 #include <asm/uaccess.h>
48 #include <linux/fcntl.h>
49 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
50 #include <linux/notifier.h>
51 #include <linux/init.h>
54 int sysctl_x25_restart_request_timeout
= X25_DEFAULT_T20
;
55 int sysctl_x25_call_request_timeout
= X25_DEFAULT_T21
;
56 int sysctl_x25_reset_request_timeout
= X25_DEFAULT_T22
;
57 int sysctl_x25_clear_request_timeout
= X25_DEFAULT_T23
;
58 int sysctl_x25_ack_holdback_timeout
= X25_DEFAULT_T2
;
61 DEFINE_RWLOCK(x25_list_lock
);
63 static struct proto_ops x25_proto_ops
;
65 static struct x25_address null_x25_address
= {" "};
67 int x25_addr_ntoa(unsigned char *p
, struct x25_address
*called_addr
,
68 struct x25_address
*calling_addr
)
70 int called_len
, calling_len
;
71 char *called
, *calling
;
74 called_len
= (*p
>> 0) & 0x0F;
75 calling_len
= (*p
>> 4) & 0x0F;
77 called
= called_addr
->x25_addr
;
78 calling
= calling_addr
->x25_addr
;
81 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
84 *called
++ = ((*p
>> 0) & 0x0F) + '0';
87 *called
++ = ((*p
>> 4) & 0x0F) + '0';
91 *calling
++ = ((*p
>> 0) & 0x0F) + '0';
94 *calling
++ = ((*p
>> 4) & 0x0F) + '0';
99 *called
= *calling
= '\0';
101 return 1 + (called_len
+ calling_len
+ 1) / 2;
104 int x25_addr_aton(unsigned char *p
, struct x25_address
*called_addr
,
105 struct x25_address
*calling_addr
)
107 unsigned int called_len
, calling_len
;
108 char *called
, *calling
;
111 called
= called_addr
->x25_addr
;
112 calling
= calling_addr
->x25_addr
;
114 called_len
= strlen(called
);
115 calling_len
= strlen(calling
);
117 *p
++ = (calling_len
<< 4) | (called_len
<< 0);
119 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
120 if (i
< called_len
) {
122 *p
|= (*called
++ - '0') << 0;
126 *p
|= (*called
++ - '0') << 4;
130 *p
|= (*calling
++ - '0') << 0;
134 *p
|= (*calling
++ - '0') << 4;
139 return 1 + (called_len
+ calling_len
+ 1) / 2;
143 * Socket removal during an interrupt is now safe.
145 static void x25_remove_socket(struct sock
*sk
)
147 write_lock_bh(&x25_list_lock
);
148 sk_del_node_init(sk
);
149 write_unlock_bh(&x25_list_lock
);
153 * Kill all bound sockets on a dropped device.
155 static void x25_kill_by_device(struct net_device
*dev
)
158 struct hlist_node
*node
;
160 write_lock_bh(&x25_list_lock
);
162 sk_for_each(s
, node
, &x25_list
)
163 if (x25_sk(s
)->neighbour
&& x25_sk(s
)->neighbour
->dev
== dev
)
164 x25_disconnect(s
, ENETUNREACH
, 0, 0);
166 write_unlock_bh(&x25_list_lock
);
170 * Handle device status changes.
172 static int x25_device_event(struct notifier_block
*this, unsigned long event
,
175 struct net_device
*dev
= ptr
;
176 struct x25_neigh
*nb
;
178 if (dev
->type
== ARPHRD_X25
179 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
180 || dev
->type
== ARPHRD_ETHER
185 x25_link_device_up(dev
);
187 case NETDEV_GOING_DOWN
:
188 nb
= x25_get_neigh(dev
);
190 x25_terminate_link(nb
);
195 x25_kill_by_device(dev
);
196 x25_route_device_down(dev
);
197 x25_link_device_down(dev
);
206 * Add a socket to the bound sockets list.
208 static void x25_insert_socket(struct sock
*sk
)
210 write_lock_bh(&x25_list_lock
);
211 sk_add_node(sk
, &x25_list
);
212 write_unlock_bh(&x25_list_lock
);
216 * Find a socket that wants to accept the Call Request we just
217 * received. Check the full list for an address/cud match.
218 * If no cuds match return the next_best thing, an address match.
219 * Note: if a listening socket has cud set it must only get calls
222 static struct sock
*x25_find_listener(struct x25_address
*addr
, struct x25_calluserdata
*calluserdata
)
225 struct sock
*next_best
;
226 struct hlist_node
*node
;
228 read_lock_bh(&x25_list_lock
);
231 sk_for_each(s
, node
, &x25_list
)
232 if ((!strcmp(addr
->x25_addr
,
233 x25_sk(s
)->source_addr
.x25_addr
) ||
234 !strcmp(addr
->x25_addr
,
235 null_x25_address
.x25_addr
)) &&
236 s
->sk_state
== TCP_LISTEN
) {
239 * Found a listening socket, now check the incoming
240 * call user data vs this sockets call user data
242 if (x25_check_calluserdata(&x25_sk(s
)->calluserdata
, calluserdata
)) {
246 if (x25_sk(s
)->calluserdata
.cudlength
== 0) {
257 read_unlock_bh(&x25_list_lock
);
262 * Find a connected X.25 socket given my LCI and neighbour.
264 static struct sock
*__x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
267 struct hlist_node
*node
;
269 sk_for_each(s
, node
, &x25_list
)
270 if (x25_sk(s
)->lci
== lci
&& x25_sk(s
)->neighbour
== nb
) {
279 struct sock
*x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
283 read_lock_bh(&x25_list_lock
);
284 s
= __x25_find_socket(lci
, nb
);
285 read_unlock_bh(&x25_list_lock
);
290 * Find a unique LCI for a given device.
292 static unsigned int x25_new_lci(struct x25_neigh
*nb
)
294 unsigned int lci
= 1;
297 read_lock_bh(&x25_list_lock
);
299 while ((sk
= __x25_find_socket(lci
, nb
)) != NULL
) {
307 read_unlock_bh(&x25_list_lock
);
314 void x25_destroy_socket(struct sock
*);
317 * handler for deferred kills.
319 static void x25_destroy_timer(unsigned long data
)
321 x25_destroy_socket((struct sock
*)data
);
325 * This is called from user mode and the timers. Thus it protects itself
326 * against interrupt users but doesn't worry about being called during
327 * work. Once it is removed from the queue no interrupt or bottom half
328 * will touch it and we are (fairly 8-) ) safe.
329 * Not static as it's used by the timer
331 void x25_destroy_socket(struct sock
*sk
)
337 x25_stop_heartbeat(sk
);
340 x25_remove_socket(sk
);
341 x25_clear_queues(sk
); /* Flush the queues */
343 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
344 if (skb
->sk
!= sk
) { /* A pending connection */
346 * Queue the unaccepted socket for death
348 sock_set_flag(skb
->sk
, SOCK_DEAD
);
349 x25_start_heartbeat(skb
->sk
);
350 x25_sk(skb
->sk
)->state
= X25_STATE_0
;
356 if (atomic_read(&sk
->sk_wmem_alloc
) ||
357 atomic_read(&sk
->sk_rmem_alloc
)) {
358 /* Defer: outstanding buffers */
359 sk
->sk_timer
.expires
= jiffies
+ 10 * HZ
;
360 sk
->sk_timer
.function
= x25_destroy_timer
;
361 sk
->sk_timer
.data
= (unsigned long)sk
;
362 add_timer(&sk
->sk_timer
);
364 /* drop last reference so sock_put will free */
373 * Handling for system calls applied via the various interfaces to a
374 * X.25 socket object.
377 static int x25_setsockopt(struct socket
*sock
, int level
, int optname
,
378 char __user
*optval
, int optlen
)
381 struct sock
*sk
= sock
->sk
;
382 int rc
= -ENOPROTOOPT
;
384 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
388 if (optlen
< sizeof(int))
392 if (get_user(opt
, (int __user
*)optval
))
395 x25_sk(sk
)->qbitincl
= !!opt
;
401 static int x25_getsockopt(struct socket
*sock
, int level
, int optname
,
402 char __user
*optval
, int __user
*optlen
)
404 struct sock
*sk
= sock
->sk
;
405 int val
, len
, rc
= -ENOPROTOOPT
;
407 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
411 if (get_user(len
, optlen
))
414 len
= min_t(unsigned int, len
, sizeof(int));
421 if (put_user(len
, optlen
))
424 val
= x25_sk(sk
)->qbitincl
;
425 rc
= copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
430 static int x25_listen(struct socket
*sock
, int backlog
)
432 struct sock
*sk
= sock
->sk
;
433 int rc
= -EOPNOTSUPP
;
435 if (sk
->sk_state
!= TCP_LISTEN
) {
436 memset(&x25_sk(sk
)->dest_addr
, 0, X25_ADDR_LEN
);
437 sk
->sk_max_ack_backlog
= backlog
;
438 sk
->sk_state
= TCP_LISTEN
;
445 static struct proto x25_proto
= {
447 .owner
= THIS_MODULE
,
448 .obj_size
= sizeof(struct x25_sock
),
451 static struct sock
*x25_alloc_socket(void)
453 struct x25_sock
*x25
;
454 struct sock
*sk
= sk_alloc(AF_X25
, GFP_ATOMIC
, &x25_proto
, 1);
459 sock_init_data(NULL
, sk
);
462 skb_queue_head_init(&x25
->ack_queue
);
463 skb_queue_head_init(&x25
->fragment_queue
);
464 skb_queue_head_init(&x25
->interrupt_in_queue
);
465 skb_queue_head_init(&x25
->interrupt_out_queue
);
470 void x25_init_timers(struct sock
*sk
);
472 static int x25_create(struct socket
*sock
, int protocol
)
475 struct x25_sock
*x25
;
476 int rc
= -ESOCKTNOSUPPORT
;
478 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
)
482 if ((sk
= x25_alloc_socket()) == NULL
)
487 sock_init_data(sock
, sk
);
491 sock
->ops
= &x25_proto_ops
;
492 sk
->sk_protocol
= protocol
;
493 sk
->sk_backlog_rcv
= x25_backlog_rcv
;
495 x25
->t21
= sysctl_x25_call_request_timeout
;
496 x25
->t22
= sysctl_x25_reset_request_timeout
;
497 x25
->t23
= sysctl_x25_clear_request_timeout
;
498 x25
->t2
= sysctl_x25_ack_holdback_timeout
;
499 x25
->state
= X25_STATE_0
;
501 x25
->facilities
.winsize_in
= X25_DEFAULT_WINDOW_SIZE
;
502 x25
->facilities
.winsize_out
= X25_DEFAULT_WINDOW_SIZE
;
503 x25
->facilities
.pacsize_in
= X25_DEFAULT_PACKET_SIZE
;
504 x25
->facilities
.pacsize_out
= X25_DEFAULT_PACKET_SIZE
;
505 x25
->facilities
.throughput
= X25_DEFAULT_THROUGHPUT
;
506 x25
->facilities
.reverse
= X25_DEFAULT_REVERSE
;
512 static struct sock
*x25_make_new(struct sock
*osk
)
514 struct sock
*sk
= NULL
;
515 struct x25_sock
*x25
, *ox25
;
517 if (osk
->sk_type
!= SOCK_SEQPACKET
)
520 if ((sk
= x25_alloc_socket()) == NULL
)
525 sk
->sk_type
= osk
->sk_type
;
526 sk
->sk_socket
= osk
->sk_socket
;
527 sk
->sk_priority
= osk
->sk_priority
;
528 sk
->sk_protocol
= osk
->sk_protocol
;
529 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
530 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
531 sk
->sk_state
= TCP_ESTABLISHED
;
532 sk
->sk_sleep
= osk
->sk_sleep
;
533 sk
->sk_backlog_rcv
= osk
->sk_backlog_rcv
;
535 if (sock_flag(osk
, SOCK_ZAPPED
))
536 sock_set_flag(sk
, SOCK_ZAPPED
);
538 if (sock_flag(osk
, SOCK_DBG
))
539 sock_set_flag(sk
, SOCK_DBG
);
542 x25
->t21
= ox25
->t21
;
543 x25
->t22
= ox25
->t22
;
544 x25
->t23
= ox25
->t23
;
546 x25
->facilities
= ox25
->facilities
;
547 x25
->qbitincl
= ox25
->qbitincl
;
554 static int x25_release(struct socket
*sock
)
556 struct sock
*sk
= sock
->sk
;
557 struct x25_sock
*x25
;
564 switch (x25
->state
) {
568 x25_disconnect(sk
, 0, 0, 0);
569 x25_destroy_socket(sk
);
575 x25_clear_queues(sk
);
576 x25_write_internal(sk
, X25_CLEAR_REQUEST
);
577 x25_start_t23timer(sk
);
578 x25
->state
= X25_STATE_2
;
579 sk
->sk_state
= TCP_CLOSE
;
580 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
581 sk
->sk_state_change(sk
);
582 sock_set_flag(sk
, SOCK_DEAD
);
583 sock_set_flag(sk
, SOCK_DESTROY
);
588 sk
->sk_socket
= NULL
; /* Not used, but we should do this */
593 static int x25_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
595 struct sock
*sk
= sock
->sk
;
596 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
598 if (!sock_flag(sk
, SOCK_ZAPPED
) ||
599 addr_len
!= sizeof(struct sockaddr_x25
) ||
600 addr
->sx25_family
!= AF_X25
)
603 x25_sk(sk
)->source_addr
= addr
->sx25_addr
;
604 x25_insert_socket(sk
);
605 sock_reset_flag(sk
, SOCK_ZAPPED
);
606 SOCK_DEBUG(sk
, "x25_bind: socket is bound\n");
611 static int x25_wait_for_connection_establishment(struct sock
*sk
)
613 DECLARE_WAITQUEUE(wait
, current
);
616 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
618 __set_current_state(TASK_INTERRUPTIBLE
);
620 if (signal_pending(current
))
624 sk
->sk_socket
->state
= SS_UNCONNECTED
;
628 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
635 __set_current_state(TASK_RUNNING
);
636 remove_wait_queue(sk
->sk_sleep
, &wait
);
640 static int x25_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
641 int addr_len
, int flags
)
643 struct sock
*sk
= sock
->sk
;
644 struct x25_sock
*x25
= x25_sk(sk
);
645 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
646 struct x25_route
*rt
;
650 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
651 sock
->state
= SS_CONNECTED
;
652 goto out
; /* Connect completed during a ERESTARTSYS event */
656 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
657 sock
->state
= SS_UNCONNECTED
;
661 rc
= -EISCONN
; /* No reconnect on a seqpacket socket */
662 if (sk
->sk_state
== TCP_ESTABLISHED
)
665 sk
->sk_state
= TCP_CLOSE
;
666 sock
->state
= SS_UNCONNECTED
;
669 if (addr_len
!= sizeof(struct sockaddr_x25
) ||
670 addr
->sx25_family
!= AF_X25
)
674 rt
= x25_get_route(&addr
->sx25_addr
);
678 x25
->neighbour
= x25_get_neigh(rt
->dev
);
682 x25_limit_facilities(&x25
->facilities
, x25
->neighbour
);
684 x25
->lci
= x25_new_lci(x25
->neighbour
);
689 if (sock_flag(sk
, SOCK_ZAPPED
)) /* Must bind first - autobinding does not work */
692 if (!strcmp(x25
->source_addr
.x25_addr
, null_x25_address
.x25_addr
))
693 memset(&x25
->source_addr
, '\0', X25_ADDR_LEN
);
695 x25
->dest_addr
= addr
->sx25_addr
;
697 /* Move to connecting socket, start sending Connect Requests */
698 sock
->state
= SS_CONNECTING
;
699 sk
->sk_state
= TCP_SYN_SENT
;
701 x25
->state
= X25_STATE_1
;
703 x25_write_internal(sk
, X25_CALL_REQUEST
);
705 x25_start_heartbeat(sk
);
706 x25_start_t21timer(sk
);
710 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
713 rc
= x25_wait_for_connection_establishment(sk
);
717 sock
->state
= SS_CONNECTED
;
721 x25_neigh_put(x25
->neighbour
);
729 static int x25_wait_for_data(struct sock
*sk
, int timeout
)
731 DECLARE_WAITQUEUE(wait
, current
);
734 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
736 __set_current_state(TASK_INTERRUPTIBLE
);
737 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
740 if (signal_pending(current
))
746 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
748 timeout
= schedule_timeout(timeout
);
753 __set_current_state(TASK_RUNNING
);
754 remove_wait_queue(sk
->sk_sleep
, &wait
);
758 static int x25_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
760 struct sock
*sk
= sock
->sk
;
765 if (!sk
|| sk
->sk_state
!= TCP_LISTEN
)
769 if (sk
->sk_type
!= SOCK_SEQPACKET
)
773 rc
= x25_wait_for_data(sk
, sk
->sk_rcvtimeo
);
776 skb
= skb_dequeue(&sk
->sk_receive_queue
);
781 newsk
->sk_socket
= newsock
;
782 newsk
->sk_sleep
= &newsock
->wait
;
784 /* Now attach up the new socket */
787 sk
->sk_ack_backlog
--;
789 newsock
->state
= SS_CONNECTED
;
797 static int x25_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
798 int *uaddr_len
, int peer
)
800 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)uaddr
;
801 struct sock
*sk
= sock
->sk
;
802 struct x25_sock
*x25
= x25_sk(sk
);
805 if (sk
->sk_state
!= TCP_ESTABLISHED
)
807 sx25
->sx25_addr
= x25
->dest_addr
;
809 sx25
->sx25_addr
= x25
->source_addr
;
811 sx25
->sx25_family
= AF_X25
;
812 *uaddr_len
= sizeof(*sx25
);
817 int x25_rx_call_request(struct sk_buff
*skb
, struct x25_neigh
*nb
,
822 struct x25_sock
*makex25
;
823 struct x25_address source_addr
, dest_addr
;
824 struct x25_facilities facilities
;
825 struct x25_calluserdata calluserdata
;
829 * Remove the LCI and frame type.
831 skb_pull(skb
, X25_STD_MIN_LEN
);
834 * Extract the X.25 addresses and convert them to ASCII strings,
837 skb_pull(skb
, x25_addr_ntoa(skb
->data
, &source_addr
, &dest_addr
));
840 * Get the length of the facilities, skip past them for the moment
841 * get the call user data because this is needed to determine
842 * the correct listener
844 len
= skb
->data
[0] + 1;
848 * Incoming Call User Data.
851 memcpy(calluserdata
.cuddata
, skb
->data
, skb
->len
);
852 calluserdata
.cudlength
= skb
->len
;
858 * Find a listener for the particular address/cud pair.
860 sk
= x25_find_listener(&source_addr
,&calluserdata
);
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 makex25
->calluserdata
= calluserdata
;
905 x25_write_internal(make
, X25_CALL_ACCEPTED
);
907 makex25
->state
= X25_STATE_3
;
909 sk
->sk_ack_backlog
++;
911 x25_insert_socket(make
);
913 skb_queue_head(&sk
->sk_receive_queue
, skb
);
915 x25_start_heartbeat(make
);
917 if (!sock_flag(sk
, SOCK_DEAD
))
918 sk
->sk_data_ready(sk
, skb
->len
);
927 x25_transmit_clear_request(nb
, lci
, 0x01);
931 static int x25_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
932 struct msghdr
*msg
, size_t len
)
934 struct sock
*sk
= sock
->sk
;
935 struct x25_sock
*x25
= x25_sk(sk
);
936 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
937 struct sockaddr_x25 sx25
;
939 unsigned char *asmptr
;
940 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
942 int qbit
= 0, rc
= -EINVAL
;
944 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_OOB
|MSG_EOR
|MSG_CMSG_COMPAT
))
947 /* we currently don't support segmented records at the user interface */
948 if (!(msg
->msg_flags
& (MSG_EOR
|MSG_OOB
)))
952 if (sock_flag(sk
, SOCK_ZAPPED
))
956 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
957 send_sig(SIGPIPE
, current
, 0);
967 if (msg
->msg_namelen
< sizeof(sx25
))
969 memcpy(&sx25
, usx25
, sizeof(sx25
));
971 if (strcmp(x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
))
974 if (sx25
.sx25_family
!= AF_X25
)
978 * FIXME 1003.1g - if the socket is like this because
979 * it has become closed (not started closed) we ought
983 if (sk
->sk_state
!= TCP_ESTABLISHED
)
986 sx25
.sx25_family
= AF_X25
;
987 sx25
.sx25_addr
= x25
->dest_addr
;
990 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
993 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
995 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
998 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
1000 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
1003 X25_SKB_CB(skb
)->flags
= msg
->msg_flags
;
1005 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
1008 * Put the data on the end
1010 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
1012 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1014 rc
= memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1019 * If the Q BIT Include socket option is in force, the first
1020 * byte of the user data is the logical value of the Q Bit.
1022 if (x25
->qbitincl
) {
1023 qbit
= skb
->data
[0];
1028 * Push down the X.25 header
1030 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
1032 if (msg
->msg_flags
& MSG_OOB
) {
1033 if (x25
->neighbour
->extended
) {
1034 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1035 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1036 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1037 *asmptr
++ = X25_INTERRUPT
;
1039 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1040 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1041 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1042 *asmptr
++ = X25_INTERRUPT
;
1045 if (x25
->neighbour
->extended
) {
1046 /* Build an Extended X.25 header */
1047 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
1048 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1049 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1050 *asmptr
++ = X25_DATA
;
1051 *asmptr
++ = X25_DATA
;
1053 /* Build an Standard X.25 header */
1054 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1055 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1056 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1057 *asmptr
++ = X25_DATA
;
1061 skb
->data
[0] |= X25_Q_BIT
;
1064 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
1065 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
1068 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1071 if (msg
->msg_flags
& MSG_OOB
)
1072 skb_queue_tail(&x25
->interrupt_out_queue
, skb
);
1074 len
= x25_output(sk
, skb
);
1077 else if (x25
->qbitincl
)
1082 * lock_sock() is currently only used to serialize this x25_kick()
1083 * against input-driven x25_kick() calls. It currently only blocks
1084 * incoming packets for this socket and does not protect against
1085 * any other socket state changes and is not called from anywhere
1086 * else. As x25_kick() cannot block and as long as all socket
1087 * operations are BKL-wrapped, we don't need take to care about
1088 * purging the backlog queue in x25_release().
1090 * Using lock_sock() to protect all socket operations entirely
1091 * (and making the whole x25 stack SMP aware) unfortunately would
1092 * require major changes to {send,recv}msg and skb allocation methods.
1107 static int x25_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1108 struct msghdr
*msg
, size_t size
,
1111 struct sock
*sk
= sock
->sk
;
1112 struct x25_sock
*x25
= x25_sk(sk
);
1113 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
1116 struct sk_buff
*skb
;
1117 unsigned char *asmptr
;
1121 * This works for seqpacket too. The receiver has ordered the queue for
1122 * us! We do one quick check first though
1124 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1127 if (flags
& MSG_OOB
) {
1129 if (sock_flag(sk
, SOCK_URGINLINE
) ||
1130 !skb_peek(&x25
->interrupt_in_queue
))
1133 skb
= skb_dequeue(&x25
->interrupt_in_queue
);
1135 skb_pull(skb
, X25_STD_MIN_LEN
);
1138 * No Q bit information on Interrupt data.
1140 if (x25
->qbitincl
) {
1141 asmptr
= skb_push(skb
, 1);
1145 msg
->msg_flags
|= MSG_OOB
;
1147 /* Now we can treat all alike */
1148 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1149 flags
& MSG_DONTWAIT
, &rc
);
1153 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1155 skb_pull(skb
, x25
->neighbour
->extended
?
1156 X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1158 if (x25
->qbitincl
) {
1159 asmptr
= skb_push(skb
, 1);
1164 skb
->h
.raw
= skb
->data
;
1168 if (copied
> size
) {
1170 msg
->msg_flags
|= MSG_TRUNC
;
1173 /* Currently, each datagram always contains a complete record */
1174 msg
->msg_flags
|= MSG_EOR
;
1176 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1178 goto out_free_dgram
;
1181 sx25
->sx25_family
= AF_X25
;
1182 sx25
->sx25_addr
= x25
->dest_addr
;
1185 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1192 skb_free_datagram(sk
, skb
);
1198 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1200 struct sock
*sk
= sock
->sk
;
1201 struct x25_sock
*x25
= x25_sk(sk
);
1202 void __user
*argp
= (void __user
*)arg
;
1207 int amount
= sk
->sk_sndbuf
-
1208 atomic_read(&sk
->sk_wmem_alloc
);
1211 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1216 struct sk_buff
*skb
;
1219 * These two are safe on a single CPU system as
1220 * only user tasks fiddle here
1222 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1224 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1231 rc
= sock_get_timestamp(sk
,
1232 (struct timeval __user
*)argp
);
1236 case SIOCGIFDSTADDR
:
1237 case SIOCSIFDSTADDR
:
1238 case SIOCGIFBRDADDR
:
1239 case SIOCSIFBRDADDR
:
1240 case SIOCGIFNETMASK
:
1241 case SIOCSIFNETMASK
:
1249 if (!capable(CAP_NET_ADMIN
))
1251 rc
= x25_route_ioctl(cmd
, argp
);
1253 case SIOCX25GSUBSCRIP
:
1254 rc
= x25_subscr_ioctl(cmd
, argp
);
1256 case SIOCX25SSUBSCRIP
:
1258 if (!capable(CAP_NET_ADMIN
))
1260 rc
= x25_subscr_ioctl(cmd
, argp
);
1262 case SIOCX25GFACILITIES
: {
1263 struct x25_facilities fac
= x25
->facilities
;
1264 rc
= copy_to_user(argp
, &fac
,
1265 sizeof(fac
)) ? -EFAULT
: 0;
1269 case SIOCX25SFACILITIES
: {
1270 struct x25_facilities facilities
;
1272 if (copy_from_user(&facilities
, argp
,
1273 sizeof(facilities
)))
1276 if (sk
->sk_state
!= TCP_LISTEN
&&
1277 sk
->sk_state
!= TCP_CLOSE
)
1279 if (facilities
.pacsize_in
< X25_PS16
||
1280 facilities
.pacsize_in
> X25_PS4096
)
1282 if (facilities
.pacsize_out
< X25_PS16
||
1283 facilities
.pacsize_out
> X25_PS4096
)
1285 if (facilities
.winsize_in
< 1 ||
1286 facilities
.winsize_in
> 127)
1288 if (facilities
.throughput
< 0x03 ||
1289 facilities
.throughput
> 0xDD)
1291 if (facilities
.reverse
&& facilities
.reverse
!= 1)
1293 x25
->facilities
= facilities
;
1298 case SIOCX25GCALLUSERDATA
: {
1299 struct x25_calluserdata cud
= x25
->calluserdata
;
1300 rc
= copy_to_user(argp
, &cud
,
1301 sizeof(cud
)) ? -EFAULT
: 0;
1305 case SIOCX25SCALLUSERDATA
: {
1306 struct x25_calluserdata calluserdata
;
1309 if (copy_from_user(&calluserdata
, argp
,
1310 sizeof(calluserdata
)))
1313 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1315 x25
->calluserdata
= calluserdata
;
1320 case SIOCX25GCAUSEDIAG
: {
1321 struct x25_causediag causediag
;
1322 causediag
= x25
->causediag
;
1323 rc
= copy_to_user(argp
, &causediag
,
1324 sizeof(causediag
)) ? -EFAULT
: 0;
1329 rc
= dev_ioctl(cmd
, argp
);
1336 static struct net_proto_family x25_family_ops
= {
1338 .create
= x25_create
,
1339 .owner
= THIS_MODULE
,
1342 static struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1344 .owner
= THIS_MODULE
,
1345 .release
= x25_release
,
1347 .connect
= x25_connect
,
1348 .socketpair
= sock_no_socketpair
,
1349 .accept
= x25_accept
,
1350 .getname
= x25_getname
,
1351 .poll
= datagram_poll
,
1353 .listen
= x25_listen
,
1354 .shutdown
= sock_no_shutdown
,
1355 .setsockopt
= x25_setsockopt
,
1356 .getsockopt
= x25_getsockopt
,
1357 .sendmsg
= x25_sendmsg
,
1358 .recvmsg
= x25_recvmsg
,
1359 .mmap
= sock_no_mmap
,
1360 .sendpage
= sock_no_sendpage
,
1363 #include <linux/smp_lock.h>
1364 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1366 static struct packet_type x25_packet_type
= {
1367 .type
= __constant_htons(ETH_P_X25
),
1368 .func
= x25_lapb_receive_frame
,
1371 static struct notifier_block x25_dev_notifier
= {
1372 .notifier_call
= x25_device_event
,
1375 void x25_kill_by_neigh(struct x25_neigh
*nb
)
1378 struct hlist_node
*node
;
1380 write_lock_bh(&x25_list_lock
);
1382 sk_for_each(s
, node
, &x25_list
)
1383 if (x25_sk(s
)->neighbour
== nb
)
1384 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1386 write_unlock_bh(&x25_list_lock
);
1389 static int __init
x25_init(void)
1391 int rc
= proto_register(&x25_proto
, 0);
1396 sock_register(&x25_family_ops
);
1398 dev_add_pack(&x25_packet_type
);
1400 register_netdevice_notifier(&x25_dev_notifier
);
1402 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1404 #ifdef CONFIG_SYSCTL
1405 x25_register_sysctl();
1411 module_init(x25_init
);
1413 static void __exit
x25_exit(void)
1419 #ifdef CONFIG_SYSCTL
1420 x25_unregister_sysctl();
1423 unregister_netdevice_notifier(&x25_dev_notifier
);
1425 dev_remove_pack(&x25_packet_type
);
1427 sock_unregister(AF_X25
);
1428 proto_unregister(&x25_proto
);
1430 module_exit(x25_exit
);
1432 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1433 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1434 MODULE_LICENSE("GPL");
1435 MODULE_ALIAS_NETPROTO(PF_X25
);