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 const 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
;
543 sock_copy_flags(sk
, osk
);
546 x25
->t21
= ox25
->t21
;
547 x25
->t22
= ox25
->t22
;
548 x25
->t23
= ox25
->t23
;
550 x25
->facilities
= ox25
->facilities
;
551 x25
->qbitincl
= ox25
->qbitincl
;
552 x25
->cudmatchlength
= ox25
->cudmatchlength
;
553 x25
->accptapprv
= ox25
->accptapprv
;
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 /* Normally all calls are accepted immediatly */
904 if(makex25
->accptapprv
& X25_DENY_ACCPT_APPRV
) {
905 x25_write_internal(make
, X25_CALL_ACCEPTED
);
906 makex25
->state
= X25_STATE_3
;
910 * Incoming Call User Data.
913 memcpy(makex25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
914 makex25
->calluserdata
.cudlength
= skb
->len
;
917 sk
->sk_ack_backlog
++;
919 x25_insert_socket(make
);
921 skb_queue_head(&sk
->sk_receive_queue
, skb
);
923 x25_start_heartbeat(make
);
925 if (!sock_flag(sk
, SOCK_DEAD
))
926 sk
->sk_data_ready(sk
, skb
->len
);
935 x25_transmit_clear_request(nb
, lci
, 0x01);
939 static int x25_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
940 struct msghdr
*msg
, size_t len
)
942 struct sock
*sk
= sock
->sk
;
943 struct x25_sock
*x25
= x25_sk(sk
);
944 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
945 struct sockaddr_x25 sx25
;
947 unsigned char *asmptr
;
948 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
950 int qbit
= 0, rc
= -EINVAL
;
952 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_OOB
|MSG_EOR
|MSG_CMSG_COMPAT
))
955 /* we currently don't support segmented records at the user interface */
956 if (!(msg
->msg_flags
& (MSG_EOR
|MSG_OOB
)))
960 if (sock_flag(sk
, SOCK_ZAPPED
))
964 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
965 send_sig(SIGPIPE
, current
, 0);
975 if (msg
->msg_namelen
< sizeof(sx25
))
977 memcpy(&sx25
, usx25
, sizeof(sx25
));
979 if (strcmp(x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
))
982 if (sx25
.sx25_family
!= AF_X25
)
986 * FIXME 1003.1g - if the socket is like this because
987 * it has become closed (not started closed) we ought
991 if (sk
->sk_state
!= TCP_ESTABLISHED
)
994 sx25
.sx25_family
= AF_X25
;
995 sx25
.sx25_addr
= x25
->dest_addr
;
998 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
1000 /* Build a packet */
1001 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
1003 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
1006 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
1008 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
1011 X25_SKB_CB(skb
)->flags
= msg
->msg_flags
;
1013 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
1016 * Put the data on the end
1018 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
1020 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1022 rc
= memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1027 * If the Q BIT Include socket option is in force, the first
1028 * byte of the user data is the logical value of the Q Bit.
1030 if (x25
->qbitincl
) {
1031 qbit
= skb
->data
[0];
1036 * Push down the X.25 header
1038 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
1040 if (msg
->msg_flags
& MSG_OOB
) {
1041 if (x25
->neighbour
->extended
) {
1042 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1043 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1044 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1045 *asmptr
++ = X25_INTERRUPT
;
1047 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1048 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1049 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1050 *asmptr
++ = X25_INTERRUPT
;
1053 if (x25
->neighbour
->extended
) {
1054 /* Build an Extended X.25 header */
1055 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
1056 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1057 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1058 *asmptr
++ = X25_DATA
;
1059 *asmptr
++ = X25_DATA
;
1061 /* Build an Standard X.25 header */
1062 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1063 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1064 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1065 *asmptr
++ = X25_DATA
;
1069 skb
->data
[0] |= X25_Q_BIT
;
1072 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
1073 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
1076 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1079 if (msg
->msg_flags
& MSG_OOB
)
1080 skb_queue_tail(&x25
->interrupt_out_queue
, skb
);
1082 len
= x25_output(sk
, skb
);
1085 else if (x25
->qbitincl
)
1090 * lock_sock() is currently only used to serialize this x25_kick()
1091 * against input-driven x25_kick() calls. It currently only blocks
1092 * incoming packets for this socket and does not protect against
1093 * any other socket state changes and is not called from anywhere
1094 * else. As x25_kick() cannot block and as long as all socket
1095 * operations are BKL-wrapped, we don't need take to care about
1096 * purging the backlog queue in x25_release().
1098 * Using lock_sock() to protect all socket operations entirely
1099 * (and making the whole x25 stack SMP aware) unfortunately would
1100 * require major changes to {send,recv}msg and skb allocation methods.
1115 static int x25_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1116 struct msghdr
*msg
, size_t size
,
1119 struct sock
*sk
= sock
->sk
;
1120 struct x25_sock
*x25
= x25_sk(sk
);
1121 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
1124 struct sk_buff
*skb
;
1125 unsigned char *asmptr
;
1129 * This works for seqpacket too. The receiver has ordered the queue for
1130 * us! We do one quick check first though
1132 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1135 if (flags
& MSG_OOB
) {
1137 if (sock_flag(sk
, SOCK_URGINLINE
) ||
1138 !skb_peek(&x25
->interrupt_in_queue
))
1141 skb
= skb_dequeue(&x25
->interrupt_in_queue
);
1143 skb_pull(skb
, X25_STD_MIN_LEN
);
1146 * No Q bit information on Interrupt data.
1148 if (x25
->qbitincl
) {
1149 asmptr
= skb_push(skb
, 1);
1153 msg
->msg_flags
|= MSG_OOB
;
1155 /* Now we can treat all alike */
1156 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1157 flags
& MSG_DONTWAIT
, &rc
);
1161 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1163 skb_pull(skb
, x25
->neighbour
->extended
?
1164 X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1166 if (x25
->qbitincl
) {
1167 asmptr
= skb_push(skb
, 1);
1172 skb
->h
.raw
= skb
->data
;
1176 if (copied
> size
) {
1178 msg
->msg_flags
|= MSG_TRUNC
;
1181 /* Currently, each datagram always contains a complete record */
1182 msg
->msg_flags
|= MSG_EOR
;
1184 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1186 goto out_free_dgram
;
1189 sx25
->sx25_family
= AF_X25
;
1190 sx25
->sx25_addr
= x25
->dest_addr
;
1193 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1200 skb_free_datagram(sk
, skb
);
1206 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1208 struct sock
*sk
= sock
->sk
;
1209 struct x25_sock
*x25
= x25_sk(sk
);
1210 void __user
*argp
= (void __user
*)arg
;
1215 int amount
= sk
->sk_sndbuf
-
1216 atomic_read(&sk
->sk_wmem_alloc
);
1219 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1224 struct sk_buff
*skb
;
1227 * These two are safe on a single CPU system as
1228 * only user tasks fiddle here
1230 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1232 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1239 rc
= sock_get_timestamp(sk
,
1240 (struct timeval __user
*)argp
);
1244 case SIOCGIFDSTADDR
:
1245 case SIOCSIFDSTADDR
:
1246 case SIOCGIFBRDADDR
:
1247 case SIOCSIFBRDADDR
:
1248 case SIOCGIFNETMASK
:
1249 case SIOCSIFNETMASK
:
1257 if (!capable(CAP_NET_ADMIN
))
1259 rc
= x25_route_ioctl(cmd
, argp
);
1261 case SIOCX25GSUBSCRIP
:
1262 rc
= x25_subscr_ioctl(cmd
, argp
);
1264 case SIOCX25SSUBSCRIP
:
1266 if (!capable(CAP_NET_ADMIN
))
1268 rc
= x25_subscr_ioctl(cmd
, argp
);
1270 case SIOCX25GFACILITIES
: {
1271 struct x25_facilities fac
= x25
->facilities
;
1272 rc
= copy_to_user(argp
, &fac
,
1273 sizeof(fac
)) ? -EFAULT
: 0;
1277 case SIOCX25SFACILITIES
: {
1278 struct x25_facilities facilities
;
1280 if (copy_from_user(&facilities
, argp
,
1281 sizeof(facilities
)))
1284 if (sk
->sk_state
!= TCP_LISTEN
&&
1285 sk
->sk_state
!= TCP_CLOSE
)
1287 if (facilities
.pacsize_in
< X25_PS16
||
1288 facilities
.pacsize_in
> X25_PS4096
)
1290 if (facilities
.pacsize_out
< X25_PS16
||
1291 facilities
.pacsize_out
> X25_PS4096
)
1293 if (facilities
.winsize_in
< 1 ||
1294 facilities
.winsize_in
> 127)
1296 if (facilities
.throughput
< 0x03 ||
1297 facilities
.throughput
> 0xDD)
1299 if (facilities
.reverse
&&
1300 (facilities
.reverse
| 0x81)!= 0x81)
1302 x25
->facilities
= facilities
;
1307 case SIOCX25GCALLUSERDATA
: {
1308 struct x25_calluserdata cud
= x25
->calluserdata
;
1309 rc
= copy_to_user(argp
, &cud
,
1310 sizeof(cud
)) ? -EFAULT
: 0;
1314 case SIOCX25SCALLUSERDATA
: {
1315 struct x25_calluserdata calluserdata
;
1318 if (copy_from_user(&calluserdata
, argp
,
1319 sizeof(calluserdata
)))
1322 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1324 x25
->calluserdata
= calluserdata
;
1329 case SIOCX25GCAUSEDIAG
: {
1330 struct x25_causediag causediag
;
1331 causediag
= x25
->causediag
;
1332 rc
= copy_to_user(argp
, &causediag
,
1333 sizeof(causediag
)) ? -EFAULT
: 0;
1337 case SIOCX25SCUDMATCHLEN
: {
1338 struct x25_subaddr sub_addr
;
1340 if(sk
->sk_state
!= TCP_CLOSE
)
1343 if (copy_from_user(&sub_addr
, argp
,
1347 if(sub_addr
.cudmatchlength
> X25_MAX_CUD_LEN
)
1349 x25
->cudmatchlength
= sub_addr
.cudmatchlength
;
1354 case SIOCX25CALLACCPTAPPRV
: {
1356 if (sk
->sk_state
!= TCP_CLOSE
)
1358 x25
->accptapprv
= X25_ALLOW_ACCPT_APPRV
;
1363 case SIOCX25SENDCALLACCPT
: {
1365 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1367 if (x25
->accptapprv
) /* must call accptapprv above */
1369 x25_write_internal(sk
, X25_CALL_ACCEPTED
);
1370 x25
->state
= X25_STATE_3
;
1383 static struct net_proto_family x25_family_ops
= {
1385 .create
= x25_create
,
1386 .owner
= THIS_MODULE
,
1389 static const struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1391 .owner
= THIS_MODULE
,
1392 .release
= x25_release
,
1394 .connect
= x25_connect
,
1395 .socketpair
= sock_no_socketpair
,
1396 .accept
= x25_accept
,
1397 .getname
= x25_getname
,
1398 .poll
= datagram_poll
,
1400 .listen
= x25_listen
,
1401 .shutdown
= sock_no_shutdown
,
1402 .setsockopt
= x25_setsockopt
,
1403 .getsockopt
= x25_getsockopt
,
1404 .sendmsg
= x25_sendmsg
,
1405 .recvmsg
= x25_recvmsg
,
1406 .mmap
= sock_no_mmap
,
1407 .sendpage
= sock_no_sendpage
,
1410 #include <linux/smp_lock.h>
1411 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1413 static struct packet_type x25_packet_type
= {
1414 .type
= __constant_htons(ETH_P_X25
),
1415 .func
= x25_lapb_receive_frame
,
1418 static struct notifier_block x25_dev_notifier
= {
1419 .notifier_call
= x25_device_event
,
1422 void x25_kill_by_neigh(struct x25_neigh
*nb
)
1425 struct hlist_node
*node
;
1427 write_lock_bh(&x25_list_lock
);
1429 sk_for_each(s
, node
, &x25_list
)
1430 if (x25_sk(s
)->neighbour
== nb
)
1431 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1433 write_unlock_bh(&x25_list_lock
);
1436 static int __init
x25_init(void)
1438 int rc
= proto_register(&x25_proto
, 0);
1443 sock_register(&x25_family_ops
);
1445 dev_add_pack(&x25_packet_type
);
1447 register_netdevice_notifier(&x25_dev_notifier
);
1449 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1451 #ifdef CONFIG_SYSCTL
1452 x25_register_sysctl();
1458 module_init(x25_init
);
1460 static void __exit
x25_exit(void)
1466 #ifdef CONFIG_SYSCTL
1467 x25_unregister_sysctl();
1470 unregister_netdevice_notifier(&x25_dev_notifier
);
1472 dev_remove_pack(&x25_packet_type
);
1474 sock_unregister(AF_X25
);
1475 proto_unregister(&x25_proto
);
1477 module_exit(x25_exit
);
1479 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1480 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1481 MODULE_LICENSE("GPL");
1482 MODULE_ALIAS_NETPROTO(PF_X25
);