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/errno.h>
41 #include <linux/kernel.h>
42 #include <linux/sched.h>
43 #include <linux/timer.h>
44 #include <linux/string.h>
45 #include <linux/net.h>
46 #include <linux/netdevice.h>
47 #include <linux/if_arp.h>
48 #include <linux/skbuff.h>
50 #include <net/tcp_states.h>
51 #include <asm/uaccess.h>
52 #include <linux/fcntl.h>
53 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
54 #include <linux/notifier.h>
55 #include <linux/init.h>
58 int sysctl_x25_restart_request_timeout
= X25_DEFAULT_T20
;
59 int sysctl_x25_call_request_timeout
= X25_DEFAULT_T21
;
60 int sysctl_x25_reset_request_timeout
= X25_DEFAULT_T22
;
61 int sysctl_x25_clear_request_timeout
= X25_DEFAULT_T23
;
62 int sysctl_x25_ack_holdback_timeout
= X25_DEFAULT_T2
;
65 DEFINE_RWLOCK(x25_list_lock
);
67 static struct proto_ops x25_proto_ops
;
69 static struct x25_address null_x25_address
= {" "};
71 int x25_addr_ntoa(unsigned char *p
, struct x25_address
*called_addr
,
72 struct x25_address
*calling_addr
)
74 int called_len
, calling_len
;
75 char *called
, *calling
;
78 called_len
= (*p
>> 0) & 0x0F;
79 calling_len
= (*p
>> 4) & 0x0F;
81 called
= called_addr
->x25_addr
;
82 calling
= calling_addr
->x25_addr
;
85 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
88 *called
++ = ((*p
>> 0) & 0x0F) + '0';
91 *called
++ = ((*p
>> 4) & 0x0F) + '0';
95 *calling
++ = ((*p
>> 0) & 0x0F) + '0';
98 *calling
++ = ((*p
>> 4) & 0x0F) + '0';
103 *called
= *calling
= '\0';
105 return 1 + (called_len
+ calling_len
+ 1) / 2;
108 int x25_addr_aton(unsigned char *p
, struct x25_address
*called_addr
,
109 struct x25_address
*calling_addr
)
111 unsigned int called_len
, calling_len
;
112 char *called
, *calling
;
115 called
= called_addr
->x25_addr
;
116 calling
= calling_addr
->x25_addr
;
118 called_len
= strlen(called
);
119 calling_len
= strlen(calling
);
121 *p
++ = (calling_len
<< 4) | (called_len
<< 0);
123 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
124 if (i
< called_len
) {
126 *p
|= (*called
++ - '0') << 0;
130 *p
|= (*called
++ - '0') << 4;
134 *p
|= (*calling
++ - '0') << 0;
138 *p
|= (*calling
++ - '0') << 4;
143 return 1 + (called_len
+ calling_len
+ 1) / 2;
147 * Socket removal during an interrupt is now safe.
149 static void x25_remove_socket(struct sock
*sk
)
151 write_lock_bh(&x25_list_lock
);
152 sk_del_node_init(sk
);
153 write_unlock_bh(&x25_list_lock
);
157 * Kill all bound sockets on a dropped device.
159 static void x25_kill_by_device(struct net_device
*dev
)
162 struct hlist_node
*node
;
164 write_lock_bh(&x25_list_lock
);
166 sk_for_each(s
, node
, &x25_list
)
167 if (x25_sk(s
)->neighbour
&& x25_sk(s
)->neighbour
->dev
== dev
)
168 x25_disconnect(s
, ENETUNREACH
, 0, 0);
170 write_unlock_bh(&x25_list_lock
);
174 * Handle device status changes.
176 static int x25_device_event(struct notifier_block
*this, unsigned long event
,
179 struct net_device
*dev
= ptr
;
180 struct x25_neigh
*nb
;
182 if (dev
->type
== ARPHRD_X25
183 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
184 || dev
->type
== ARPHRD_ETHER
189 x25_link_device_up(dev
);
191 case NETDEV_GOING_DOWN
:
192 nb
= x25_get_neigh(dev
);
194 x25_terminate_link(nb
);
199 x25_kill_by_device(dev
);
200 x25_route_device_down(dev
);
201 x25_link_device_down(dev
);
210 * Add a socket to the bound sockets list.
212 static void x25_insert_socket(struct sock
*sk
)
214 write_lock_bh(&x25_list_lock
);
215 sk_add_node(sk
, &x25_list
);
216 write_unlock_bh(&x25_list_lock
);
220 * Find a socket that wants to accept the Call Request we just
221 * received. Check the full list for an address/cud match.
222 * If no cuds match return the next_best thing, an address match.
223 * Note: if a listening socket has cud set it must only get calls
226 static struct sock
*x25_find_listener(struct x25_address
*addr
,
230 struct sock
*next_best
;
231 struct hlist_node
*node
;
233 read_lock_bh(&x25_list_lock
);
236 sk_for_each(s
, node
, &x25_list
)
237 if ((!strcmp(addr
->x25_addr
,
238 x25_sk(s
)->source_addr
.x25_addr
) ||
239 !strcmp(addr
->x25_addr
,
240 null_x25_address
.x25_addr
)) &&
241 s
->sk_state
== TCP_LISTEN
) {
243 * Found a listening socket, now check the incoming
244 * call user data vs this sockets call user data
246 if(skb
->len
> 0 && x25_sk(s
)->cudmatchlength
> 0) {
247 if((memcmp(x25_sk(s
)->calluserdata
.cuddata
,
249 x25_sk(s
)->cudmatchlength
)) == 0) {
263 read_unlock_bh(&x25_list_lock
);
268 * Find a connected X.25 socket given my LCI and neighbour.
270 static struct sock
*__x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
273 struct hlist_node
*node
;
275 sk_for_each(s
, node
, &x25_list
)
276 if (x25_sk(s
)->lci
== lci
&& x25_sk(s
)->neighbour
== nb
) {
285 struct sock
*x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
289 read_lock_bh(&x25_list_lock
);
290 s
= __x25_find_socket(lci
, nb
);
291 read_unlock_bh(&x25_list_lock
);
296 * Find a unique LCI for a given device.
298 static unsigned int x25_new_lci(struct x25_neigh
*nb
)
300 unsigned int lci
= 1;
303 read_lock_bh(&x25_list_lock
);
305 while ((sk
= __x25_find_socket(lci
, nb
)) != NULL
) {
313 read_unlock_bh(&x25_list_lock
);
320 void x25_destroy_socket(struct sock
*);
323 * handler for deferred kills.
325 static void x25_destroy_timer(unsigned long data
)
327 x25_destroy_socket((struct sock
*)data
);
331 * This is called from user mode and the timers. Thus it protects itself
332 * against interrupt users but doesn't worry about being called during
333 * work. Once it is removed from the queue no interrupt or bottom half
334 * will touch it and we are (fairly 8-) ) safe.
335 * Not static as it's used by the timer
337 void x25_destroy_socket(struct sock
*sk
)
343 x25_stop_heartbeat(sk
);
346 x25_remove_socket(sk
);
347 x25_clear_queues(sk
); /* Flush the queues */
349 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
350 if (skb
->sk
!= sk
) { /* A pending connection */
352 * Queue the unaccepted socket for death
354 sock_set_flag(skb
->sk
, SOCK_DEAD
);
355 x25_start_heartbeat(skb
->sk
);
356 x25_sk(skb
->sk
)->state
= X25_STATE_0
;
362 if (atomic_read(&sk
->sk_wmem_alloc
) ||
363 atomic_read(&sk
->sk_rmem_alloc
)) {
364 /* Defer: outstanding buffers */
365 sk
->sk_timer
.expires
= jiffies
+ 10 * HZ
;
366 sk
->sk_timer
.function
= x25_destroy_timer
;
367 sk
->sk_timer
.data
= (unsigned long)sk
;
368 add_timer(&sk
->sk_timer
);
370 /* drop last reference so sock_put will free */
379 * Handling for system calls applied via the various interfaces to a
380 * X.25 socket object.
383 static int x25_setsockopt(struct socket
*sock
, int level
, int optname
,
384 char __user
*optval
, int optlen
)
387 struct sock
*sk
= sock
->sk
;
388 int rc
= -ENOPROTOOPT
;
390 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
394 if (optlen
< sizeof(int))
398 if (get_user(opt
, (int __user
*)optval
))
401 x25_sk(sk
)->qbitincl
= !!opt
;
407 static int x25_getsockopt(struct socket
*sock
, int level
, int optname
,
408 char __user
*optval
, int __user
*optlen
)
410 struct sock
*sk
= sock
->sk
;
411 int val
, len
, rc
= -ENOPROTOOPT
;
413 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
417 if (get_user(len
, optlen
))
420 len
= min_t(unsigned int, len
, sizeof(int));
427 if (put_user(len
, optlen
))
430 val
= x25_sk(sk
)->qbitincl
;
431 rc
= copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
436 static int x25_listen(struct socket
*sock
, int backlog
)
438 struct sock
*sk
= sock
->sk
;
439 int rc
= -EOPNOTSUPP
;
441 if (sk
->sk_state
!= TCP_LISTEN
) {
442 memset(&x25_sk(sk
)->dest_addr
, 0, X25_ADDR_LEN
);
443 sk
->sk_max_ack_backlog
= backlog
;
444 sk
->sk_state
= TCP_LISTEN
;
451 static struct proto x25_proto
= {
453 .owner
= THIS_MODULE
,
454 .obj_size
= sizeof(struct x25_sock
),
457 static struct sock
*x25_alloc_socket(void)
459 struct x25_sock
*x25
;
460 struct sock
*sk
= sk_alloc(AF_X25
, GFP_ATOMIC
, &x25_proto
, 1);
465 sock_init_data(NULL
, sk
);
468 skb_queue_head_init(&x25
->ack_queue
);
469 skb_queue_head_init(&x25
->fragment_queue
);
470 skb_queue_head_init(&x25
->interrupt_in_queue
);
471 skb_queue_head_init(&x25
->interrupt_out_queue
);
476 void x25_init_timers(struct sock
*sk
);
478 static int x25_create(struct socket
*sock
, int protocol
)
481 struct x25_sock
*x25
;
482 int rc
= -ESOCKTNOSUPPORT
;
484 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
)
488 if ((sk
= x25_alloc_socket()) == NULL
)
493 sock_init_data(sock
, sk
);
497 sock
->ops
= &x25_proto_ops
;
498 sk
->sk_protocol
= protocol
;
499 sk
->sk_backlog_rcv
= x25_backlog_rcv
;
501 x25
->t21
= sysctl_x25_call_request_timeout
;
502 x25
->t22
= sysctl_x25_reset_request_timeout
;
503 x25
->t23
= sysctl_x25_clear_request_timeout
;
504 x25
->t2
= sysctl_x25_ack_holdback_timeout
;
505 x25
->state
= X25_STATE_0
;
506 x25
->cudmatchlength
= 0;
507 x25
->accptapprv
= X25_DENY_ACCPT_APPRV
; /* normally no cud */
510 x25
->facilities
.winsize_in
= X25_DEFAULT_WINDOW_SIZE
;
511 x25
->facilities
.winsize_out
= X25_DEFAULT_WINDOW_SIZE
;
512 x25
->facilities
.pacsize_in
= X25_DEFAULT_PACKET_SIZE
;
513 x25
->facilities
.pacsize_out
= X25_DEFAULT_PACKET_SIZE
;
514 x25
->facilities
.throughput
= X25_DEFAULT_THROUGHPUT
;
515 x25
->facilities
.reverse
= X25_DEFAULT_REVERSE
;
521 static struct sock
*x25_make_new(struct sock
*osk
)
523 struct sock
*sk
= NULL
;
524 struct x25_sock
*x25
, *ox25
;
526 if (osk
->sk_type
!= SOCK_SEQPACKET
)
529 if ((sk
= x25_alloc_socket()) == NULL
)
534 sk
->sk_type
= osk
->sk_type
;
535 sk
->sk_socket
= osk
->sk_socket
;
536 sk
->sk_priority
= osk
->sk_priority
;
537 sk
->sk_protocol
= osk
->sk_protocol
;
538 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
539 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
540 sk
->sk_state
= TCP_ESTABLISHED
;
541 sk
->sk_sleep
= osk
->sk_sleep
;
542 sk
->sk_backlog_rcv
= osk
->sk_backlog_rcv
;
544 if (sock_flag(osk
, SOCK_ZAPPED
))
545 sock_set_flag(sk
, SOCK_ZAPPED
);
547 if (sock_flag(osk
, SOCK_DBG
))
548 sock_set_flag(sk
, SOCK_DBG
);
551 x25
->t21
= ox25
->t21
;
552 x25
->t22
= ox25
->t22
;
553 x25
->t23
= ox25
->t23
;
555 x25
->facilities
= ox25
->facilities
;
556 x25
->qbitincl
= ox25
->qbitincl
;
557 x25
->cudmatchlength
= ox25
->cudmatchlength
;
558 x25
->accptapprv
= ox25
->accptapprv
;
565 static int x25_release(struct socket
*sock
)
567 struct sock
*sk
= sock
->sk
;
568 struct x25_sock
*x25
;
575 switch (x25
->state
) {
579 x25_disconnect(sk
, 0, 0, 0);
580 x25_destroy_socket(sk
);
586 x25_clear_queues(sk
);
587 x25_write_internal(sk
, X25_CLEAR_REQUEST
);
588 x25_start_t23timer(sk
);
589 x25
->state
= X25_STATE_2
;
590 sk
->sk_state
= TCP_CLOSE
;
591 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
592 sk
->sk_state_change(sk
);
593 sock_set_flag(sk
, SOCK_DEAD
);
594 sock_set_flag(sk
, SOCK_DESTROY
);
599 sk
->sk_socket
= NULL
; /* Not used, but we should do this */
604 static int x25_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
606 struct sock
*sk
= sock
->sk
;
607 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
609 if (!sock_flag(sk
, SOCK_ZAPPED
) ||
610 addr_len
!= sizeof(struct sockaddr_x25
) ||
611 addr
->sx25_family
!= AF_X25
)
614 x25_sk(sk
)->source_addr
= addr
->sx25_addr
;
615 x25_insert_socket(sk
);
616 sock_reset_flag(sk
, SOCK_ZAPPED
);
617 SOCK_DEBUG(sk
, "x25_bind: socket is bound\n");
622 static int x25_wait_for_connection_establishment(struct sock
*sk
)
624 DECLARE_WAITQUEUE(wait
, current
);
627 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
629 __set_current_state(TASK_INTERRUPTIBLE
);
631 if (signal_pending(current
))
635 sk
->sk_socket
->state
= SS_UNCONNECTED
;
639 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
646 __set_current_state(TASK_RUNNING
);
647 remove_wait_queue(sk
->sk_sleep
, &wait
);
651 static int x25_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
652 int addr_len
, int flags
)
654 struct sock
*sk
= sock
->sk
;
655 struct x25_sock
*x25
= x25_sk(sk
);
656 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
657 struct x25_route
*rt
;
661 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
662 sock
->state
= SS_CONNECTED
;
663 goto out
; /* Connect completed during a ERESTARTSYS event */
667 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
668 sock
->state
= SS_UNCONNECTED
;
672 rc
= -EISCONN
; /* No reconnect on a seqpacket socket */
673 if (sk
->sk_state
== TCP_ESTABLISHED
)
676 sk
->sk_state
= TCP_CLOSE
;
677 sock
->state
= SS_UNCONNECTED
;
680 if (addr_len
!= sizeof(struct sockaddr_x25
) ||
681 addr
->sx25_family
!= AF_X25
)
685 rt
= x25_get_route(&addr
->sx25_addr
);
689 x25
->neighbour
= x25_get_neigh(rt
->dev
);
693 x25_limit_facilities(&x25
->facilities
, x25
->neighbour
);
695 x25
->lci
= x25_new_lci(x25
->neighbour
);
700 if (sock_flag(sk
, SOCK_ZAPPED
)) /* Must bind first - autobinding does not work */
703 if (!strcmp(x25
->source_addr
.x25_addr
, null_x25_address
.x25_addr
))
704 memset(&x25
->source_addr
, '\0', X25_ADDR_LEN
);
706 x25
->dest_addr
= addr
->sx25_addr
;
708 /* Move to connecting socket, start sending Connect Requests */
709 sock
->state
= SS_CONNECTING
;
710 sk
->sk_state
= TCP_SYN_SENT
;
712 x25
->state
= X25_STATE_1
;
714 x25_write_internal(sk
, X25_CALL_REQUEST
);
716 x25_start_heartbeat(sk
);
717 x25_start_t21timer(sk
);
721 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
724 rc
= x25_wait_for_connection_establishment(sk
);
728 sock
->state
= SS_CONNECTED
;
732 x25_neigh_put(x25
->neighbour
);
740 static int x25_wait_for_data(struct sock
*sk
, int timeout
)
742 DECLARE_WAITQUEUE(wait
, current
);
745 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
747 __set_current_state(TASK_INTERRUPTIBLE
);
748 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
751 if (signal_pending(current
))
757 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
759 timeout
= schedule_timeout(timeout
);
764 __set_current_state(TASK_RUNNING
);
765 remove_wait_queue(sk
->sk_sleep
, &wait
);
769 static int x25_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
771 struct sock
*sk
= sock
->sk
;
776 if (!sk
|| sk
->sk_state
!= TCP_LISTEN
)
780 if (sk
->sk_type
!= SOCK_SEQPACKET
)
784 rc
= x25_wait_for_data(sk
, sk
->sk_rcvtimeo
);
787 skb
= skb_dequeue(&sk
->sk_receive_queue
);
792 newsk
->sk_socket
= newsock
;
793 newsk
->sk_sleep
= &newsock
->wait
;
795 /* Now attach up the new socket */
798 sk
->sk_ack_backlog
--;
800 newsock
->state
= SS_CONNECTED
;
808 static int x25_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
809 int *uaddr_len
, int peer
)
811 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)uaddr
;
812 struct sock
*sk
= sock
->sk
;
813 struct x25_sock
*x25
= x25_sk(sk
);
816 if (sk
->sk_state
!= TCP_ESTABLISHED
)
818 sx25
->sx25_addr
= x25
->dest_addr
;
820 sx25
->sx25_addr
= x25
->source_addr
;
822 sx25
->sx25_family
= AF_X25
;
823 *uaddr_len
= sizeof(*sx25
);
828 int x25_rx_call_request(struct sk_buff
*skb
, struct x25_neigh
*nb
,
833 struct x25_sock
*makex25
;
834 struct x25_address source_addr
, dest_addr
;
835 struct x25_facilities facilities
;
839 * Remove the LCI and frame type.
841 skb_pull(skb
, X25_STD_MIN_LEN
);
844 * Extract the X.25 addresses and convert them to ASCII strings,
847 skb_pull(skb
, x25_addr_ntoa(skb
->data
, &source_addr
, &dest_addr
));
850 * Get the length of the facilities, skip past them for the moment
851 * get the call user data because this is needed to determine
852 * the correct listener
854 len
= skb
->data
[0] + 1;
858 * Find a listener for the particular address/cud pair.
860 sk
= x25_find_listener(&source_addr
,skb
);
864 * We can't accept the Call Request.
866 if (sk
== NULL
|| sk_acceptq_is_full(sk
))
867 goto out_clear_request
;
870 * Try to reach a compromise on the requested facilities.
872 if ((len
= x25_negotiate_facilities(skb
, sk
, &facilities
)) == -1)
876 * current neighbour/link might impose additional limits
877 * on certain facilties
880 x25_limit_facilities(&facilities
, nb
);
883 * Try to create a new socket.
885 make
= x25_make_new(sk
);
890 * Remove the facilities
895 make
->sk_state
= TCP_ESTABLISHED
;
897 makex25
= x25_sk(make
);
899 makex25
->dest_addr
= dest_addr
;
900 makex25
->source_addr
= source_addr
;
901 makex25
->neighbour
= nb
;
902 makex25
->facilities
= facilities
;
903 makex25
->vc_facil_mask
= x25_sk(sk
)->vc_facil_mask
;
904 /* ensure no reverse facil on accept */
905 makex25
->vc_facil_mask
&= ~X25_MASK_REVERSE
;
906 makex25
->cudmatchlength
= x25_sk(sk
)->cudmatchlength
;
908 /* Normally all calls are accepted immediatly */
909 if(makex25
->accptapprv
& X25_DENY_ACCPT_APPRV
) {
910 x25_write_internal(make
, X25_CALL_ACCEPTED
);
911 makex25
->state
= X25_STATE_3
;
915 * Incoming Call User Data.
918 memcpy(makex25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
919 makex25
->calluserdata
.cudlength
= skb
->len
;
922 sk
->sk_ack_backlog
++;
924 x25_insert_socket(make
);
926 skb_queue_head(&sk
->sk_receive_queue
, skb
);
928 x25_start_heartbeat(make
);
930 if (!sock_flag(sk
, SOCK_DEAD
))
931 sk
->sk_data_ready(sk
, skb
->len
);
940 x25_transmit_clear_request(nb
, lci
, 0x01);
944 static int x25_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
945 struct msghdr
*msg
, size_t len
)
947 struct sock
*sk
= sock
->sk
;
948 struct x25_sock
*x25
= x25_sk(sk
);
949 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
950 struct sockaddr_x25 sx25
;
952 unsigned char *asmptr
;
953 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
955 int qbit
= 0, rc
= -EINVAL
;
957 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_OOB
|MSG_EOR
|MSG_CMSG_COMPAT
))
960 /* we currently don't support segmented records at the user interface */
961 if (!(msg
->msg_flags
& (MSG_EOR
|MSG_OOB
)))
965 if (sock_flag(sk
, SOCK_ZAPPED
))
969 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
970 send_sig(SIGPIPE
, current
, 0);
980 if (msg
->msg_namelen
< sizeof(sx25
))
982 memcpy(&sx25
, usx25
, sizeof(sx25
));
984 if (strcmp(x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
))
987 if (sx25
.sx25_family
!= AF_X25
)
991 * FIXME 1003.1g - if the socket is like this because
992 * it has become closed (not started closed) we ought
996 if (sk
->sk_state
!= TCP_ESTABLISHED
)
999 sx25
.sx25_family
= AF_X25
;
1000 sx25
.sx25_addr
= x25
->dest_addr
;
1003 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
1005 /* Build a packet */
1006 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
1008 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
1011 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
1013 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
1016 X25_SKB_CB(skb
)->flags
= msg
->msg_flags
;
1018 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
1021 * Put the data on the end
1023 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
1025 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1027 rc
= memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1032 * If the Q BIT Include socket option is in force, the first
1033 * byte of the user data is the logical value of the Q Bit.
1035 if (x25
->qbitincl
) {
1036 qbit
= skb
->data
[0];
1041 * Push down the X.25 header
1043 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
1045 if (msg
->msg_flags
& MSG_OOB
) {
1046 if (x25
->neighbour
->extended
) {
1047 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1048 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1049 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1050 *asmptr
++ = X25_INTERRUPT
;
1052 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1053 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1054 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1055 *asmptr
++ = X25_INTERRUPT
;
1058 if (x25
->neighbour
->extended
) {
1059 /* Build an Extended X.25 header */
1060 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
1061 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1062 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1063 *asmptr
++ = X25_DATA
;
1064 *asmptr
++ = X25_DATA
;
1066 /* Build an Standard X.25 header */
1067 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1068 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1069 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1070 *asmptr
++ = X25_DATA
;
1074 skb
->data
[0] |= X25_Q_BIT
;
1077 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
1078 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
1081 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1084 if (msg
->msg_flags
& MSG_OOB
)
1085 skb_queue_tail(&x25
->interrupt_out_queue
, skb
);
1087 len
= x25_output(sk
, skb
);
1090 else if (x25
->qbitincl
)
1095 * lock_sock() is currently only used to serialize this x25_kick()
1096 * against input-driven x25_kick() calls. It currently only blocks
1097 * incoming packets for this socket and does not protect against
1098 * any other socket state changes and is not called from anywhere
1099 * else. As x25_kick() cannot block and as long as all socket
1100 * operations are BKL-wrapped, we don't need take to care about
1101 * purging the backlog queue in x25_release().
1103 * Using lock_sock() to protect all socket operations entirely
1104 * (and making the whole x25 stack SMP aware) unfortunately would
1105 * require major changes to {send,recv}msg and skb allocation methods.
1120 static int x25_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1121 struct msghdr
*msg
, size_t size
,
1124 struct sock
*sk
= sock
->sk
;
1125 struct x25_sock
*x25
= x25_sk(sk
);
1126 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
1129 struct sk_buff
*skb
;
1130 unsigned char *asmptr
;
1134 * This works for seqpacket too. The receiver has ordered the queue for
1135 * us! We do one quick check first though
1137 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1140 if (flags
& MSG_OOB
) {
1142 if (sock_flag(sk
, SOCK_URGINLINE
) ||
1143 !skb_peek(&x25
->interrupt_in_queue
))
1146 skb
= skb_dequeue(&x25
->interrupt_in_queue
);
1148 skb_pull(skb
, X25_STD_MIN_LEN
);
1151 * No Q bit information on Interrupt data.
1153 if (x25
->qbitincl
) {
1154 asmptr
= skb_push(skb
, 1);
1158 msg
->msg_flags
|= MSG_OOB
;
1160 /* Now we can treat all alike */
1161 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1162 flags
& MSG_DONTWAIT
, &rc
);
1166 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1168 skb_pull(skb
, x25
->neighbour
->extended
?
1169 X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1171 if (x25
->qbitincl
) {
1172 asmptr
= skb_push(skb
, 1);
1177 skb
->h
.raw
= skb
->data
;
1181 if (copied
> size
) {
1183 msg
->msg_flags
|= MSG_TRUNC
;
1186 /* Currently, each datagram always contains a complete record */
1187 msg
->msg_flags
|= MSG_EOR
;
1189 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1191 goto out_free_dgram
;
1194 sx25
->sx25_family
= AF_X25
;
1195 sx25
->sx25_addr
= x25
->dest_addr
;
1198 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1205 skb_free_datagram(sk
, skb
);
1211 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1213 struct sock
*sk
= sock
->sk
;
1214 struct x25_sock
*x25
= x25_sk(sk
);
1215 void __user
*argp
= (void __user
*)arg
;
1220 int amount
= sk
->sk_sndbuf
-
1221 atomic_read(&sk
->sk_wmem_alloc
);
1224 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1229 struct sk_buff
*skb
;
1232 * These two are safe on a single CPU system as
1233 * only user tasks fiddle here
1235 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1237 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1244 rc
= sock_get_timestamp(sk
,
1245 (struct timeval __user
*)argp
);
1249 case SIOCGIFDSTADDR
:
1250 case SIOCSIFDSTADDR
:
1251 case SIOCGIFBRDADDR
:
1252 case SIOCSIFBRDADDR
:
1253 case SIOCGIFNETMASK
:
1254 case SIOCSIFNETMASK
:
1262 if (!capable(CAP_NET_ADMIN
))
1264 rc
= x25_route_ioctl(cmd
, argp
);
1266 case SIOCX25GSUBSCRIP
:
1267 rc
= x25_subscr_ioctl(cmd
, argp
);
1269 case SIOCX25SSUBSCRIP
:
1271 if (!capable(CAP_NET_ADMIN
))
1273 rc
= x25_subscr_ioctl(cmd
, argp
);
1275 case SIOCX25GFACILITIES
: {
1276 struct x25_facilities fac
= x25
->facilities
;
1277 rc
= copy_to_user(argp
, &fac
,
1278 sizeof(fac
)) ? -EFAULT
: 0;
1282 case SIOCX25SFACILITIES
: {
1283 struct x25_facilities facilities
;
1285 if (copy_from_user(&facilities
, argp
,
1286 sizeof(facilities
)))
1289 if (sk
->sk_state
!= TCP_LISTEN
&&
1290 sk
->sk_state
!= TCP_CLOSE
)
1292 if (facilities
.pacsize_in
< X25_PS16
||
1293 facilities
.pacsize_in
> X25_PS4096
)
1295 if (facilities
.pacsize_out
< X25_PS16
||
1296 facilities
.pacsize_out
> X25_PS4096
)
1298 if (facilities
.winsize_in
< 1 ||
1299 facilities
.winsize_in
> 127)
1301 if (facilities
.throughput
< 0x03 ||
1302 facilities
.throughput
> 0xDD)
1304 if (facilities
.reverse
&&
1305 (facilities
.reverse
| 0x81)!= 0x81)
1307 x25
->facilities
= facilities
;
1312 case SIOCX25GCALLUSERDATA
: {
1313 struct x25_calluserdata cud
= x25
->calluserdata
;
1314 rc
= copy_to_user(argp
, &cud
,
1315 sizeof(cud
)) ? -EFAULT
: 0;
1319 case SIOCX25SCALLUSERDATA
: {
1320 struct x25_calluserdata calluserdata
;
1323 if (copy_from_user(&calluserdata
, argp
,
1324 sizeof(calluserdata
)))
1327 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1329 x25
->calluserdata
= calluserdata
;
1334 case SIOCX25GCAUSEDIAG
: {
1335 struct x25_causediag causediag
;
1336 causediag
= x25
->causediag
;
1337 rc
= copy_to_user(argp
, &causediag
,
1338 sizeof(causediag
)) ? -EFAULT
: 0;
1342 case SIOCX25SCUDMATCHLEN
: {
1343 struct x25_subaddr sub_addr
;
1345 if(sk
->sk_state
!= TCP_CLOSE
)
1348 if (copy_from_user(&sub_addr
, argp
,
1352 if(sub_addr
.cudmatchlength
> X25_MAX_CUD_LEN
)
1354 x25
->cudmatchlength
= sub_addr
.cudmatchlength
;
1359 case SIOCX25CALLACCPTAPPRV
: {
1361 if (sk
->sk_state
!= TCP_CLOSE
)
1363 x25
->accptapprv
= X25_ALLOW_ACCPT_APPRV
;
1368 case SIOCX25SENDCALLACCPT
: {
1370 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1372 if (x25
->accptapprv
) /* must call accptapprv above */
1374 x25_write_internal(sk
, X25_CALL_ACCEPTED
);
1375 x25
->state
= X25_STATE_3
;
1381 rc
= dev_ioctl(cmd
, argp
);
1388 static struct net_proto_family x25_family_ops
= {
1390 .create
= x25_create
,
1391 .owner
= THIS_MODULE
,
1394 static struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1396 .owner
= THIS_MODULE
,
1397 .release
= x25_release
,
1399 .connect
= x25_connect
,
1400 .socketpair
= sock_no_socketpair
,
1401 .accept
= x25_accept
,
1402 .getname
= x25_getname
,
1403 .poll
= datagram_poll
,
1405 .listen
= x25_listen
,
1406 .shutdown
= sock_no_shutdown
,
1407 .setsockopt
= x25_setsockopt
,
1408 .getsockopt
= x25_getsockopt
,
1409 .sendmsg
= x25_sendmsg
,
1410 .recvmsg
= x25_recvmsg
,
1411 .mmap
= sock_no_mmap
,
1412 .sendpage
= sock_no_sendpage
,
1415 #include <linux/smp_lock.h>
1416 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1418 static struct packet_type x25_packet_type
= {
1419 .type
= __constant_htons(ETH_P_X25
),
1420 .func
= x25_lapb_receive_frame
,
1423 static struct notifier_block x25_dev_notifier
= {
1424 .notifier_call
= x25_device_event
,
1427 void x25_kill_by_neigh(struct x25_neigh
*nb
)
1430 struct hlist_node
*node
;
1432 write_lock_bh(&x25_list_lock
);
1434 sk_for_each(s
, node
, &x25_list
)
1435 if (x25_sk(s
)->neighbour
== nb
)
1436 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1438 write_unlock_bh(&x25_list_lock
);
1441 static int __init
x25_init(void)
1443 int rc
= proto_register(&x25_proto
, 0);
1448 sock_register(&x25_family_ops
);
1450 dev_add_pack(&x25_packet_type
);
1452 register_netdevice_notifier(&x25_dev_notifier
);
1454 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1456 #ifdef CONFIG_SYSCTL
1457 x25_register_sysctl();
1463 module_init(x25_init
);
1465 static void __exit
x25_exit(void)
1471 #ifdef CONFIG_SYSCTL
1472 x25_unregister_sysctl();
1475 unregister_netdevice_notifier(&x25_dev_notifier
);
1477 dev_remove_pack(&x25_packet_type
);
1479 sock_unregister(AF_X25
);
1480 proto_unregister(&x25_proto
);
1482 module_exit(x25_exit
);
1484 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1485 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1486 MODULE_LICENSE("GPL");
1487 MODULE_ALIAS_NETPROTO(PF_X25
);