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/module.h>
39 #include <linux/capability.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>
56 #include <linux/compat.h>
59 #include <net/compat.h>
61 int sysctl_x25_restart_request_timeout
= X25_DEFAULT_T20
;
62 int sysctl_x25_call_request_timeout
= X25_DEFAULT_T21
;
63 int sysctl_x25_reset_request_timeout
= X25_DEFAULT_T22
;
64 int sysctl_x25_clear_request_timeout
= X25_DEFAULT_T23
;
65 int sysctl_x25_ack_holdback_timeout
= X25_DEFAULT_T2
;
66 int sysctl_x25_forward
= 0;
69 DEFINE_RWLOCK(x25_list_lock
);
71 static const struct proto_ops x25_proto_ops
;
73 static struct x25_address null_x25_address
= {" "};
76 struct compat_x25_subscrip_struct
{
77 char device
[200-sizeof(compat_ulong_t
)];
78 compat_ulong_t global_facil_mask
;
79 compat_uint_t extended
;
83 int x25_addr_ntoa(unsigned char *p
, struct x25_address
*called_addr
,
84 struct x25_address
*calling_addr
)
86 int called_len
, calling_len
;
87 char *called
, *calling
;
90 called_len
= (*p
>> 0) & 0x0F;
91 calling_len
= (*p
>> 4) & 0x0F;
93 called
= called_addr
->x25_addr
;
94 calling
= calling_addr
->x25_addr
;
97 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
100 *called
++ = ((*p
>> 0) & 0x0F) + '0';
103 *called
++ = ((*p
>> 4) & 0x0F) + '0';
107 *calling
++ = ((*p
>> 0) & 0x0F) + '0';
110 *calling
++ = ((*p
>> 4) & 0x0F) + '0';
115 *called
= *calling
= '\0';
117 return 1 + (called_len
+ calling_len
+ 1) / 2;
120 int x25_addr_aton(unsigned char *p
, struct x25_address
*called_addr
,
121 struct x25_address
*calling_addr
)
123 unsigned int called_len
, calling_len
;
124 char *called
, *calling
;
127 called
= called_addr
->x25_addr
;
128 calling
= calling_addr
->x25_addr
;
130 called_len
= strlen(called
);
131 calling_len
= strlen(calling
);
133 *p
++ = (calling_len
<< 4) | (called_len
<< 0);
135 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
136 if (i
< called_len
) {
138 *p
|= (*called
++ - '0') << 0;
142 *p
|= (*called
++ - '0') << 4;
146 *p
|= (*calling
++ - '0') << 0;
150 *p
|= (*calling
++ - '0') << 4;
155 return 1 + (called_len
+ calling_len
+ 1) / 2;
159 * Socket removal during an interrupt is now safe.
161 static void x25_remove_socket(struct sock
*sk
)
163 write_lock_bh(&x25_list_lock
);
164 sk_del_node_init(sk
);
165 write_unlock_bh(&x25_list_lock
);
169 * Kill all bound sockets on a dropped device.
171 static void x25_kill_by_device(struct net_device
*dev
)
174 struct hlist_node
*node
;
176 write_lock_bh(&x25_list_lock
);
178 sk_for_each(s
, node
, &x25_list
)
179 if (x25_sk(s
)->neighbour
&& x25_sk(s
)->neighbour
->dev
== dev
)
180 x25_disconnect(s
, ENETUNREACH
, 0, 0);
182 write_unlock_bh(&x25_list_lock
);
186 * Handle device status changes.
188 static int x25_device_event(struct notifier_block
*this, unsigned long event
,
191 struct net_device
*dev
= ptr
;
192 struct x25_neigh
*nb
;
194 if (dev
->type
== ARPHRD_X25
195 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
196 || dev
->type
== ARPHRD_ETHER
201 x25_link_device_up(dev
);
203 case NETDEV_GOING_DOWN
:
204 nb
= x25_get_neigh(dev
);
206 x25_terminate_link(nb
);
211 x25_kill_by_device(dev
);
212 x25_route_device_down(dev
);
213 x25_link_device_down(dev
);
222 * Add a socket to the bound sockets list.
224 static void x25_insert_socket(struct sock
*sk
)
226 write_lock_bh(&x25_list_lock
);
227 sk_add_node(sk
, &x25_list
);
228 write_unlock_bh(&x25_list_lock
);
232 * Find a socket that wants to accept the Call Request we just
233 * received. Check the full list for an address/cud match.
234 * If no cuds match return the next_best thing, an address match.
235 * Note: if a listening socket has cud set it must only get calls
238 static struct sock
*x25_find_listener(struct x25_address
*addr
,
242 struct sock
*next_best
;
243 struct hlist_node
*node
;
245 read_lock_bh(&x25_list_lock
);
248 sk_for_each(s
, node
, &x25_list
)
249 if ((!strcmp(addr
->x25_addr
,
250 x25_sk(s
)->source_addr
.x25_addr
) ||
251 !strcmp(addr
->x25_addr
,
252 null_x25_address
.x25_addr
)) &&
253 s
->sk_state
== TCP_LISTEN
) {
255 * Found a listening socket, now check the incoming
256 * call user data vs this sockets call user data
258 if(skb
->len
> 0 && x25_sk(s
)->cudmatchlength
> 0) {
259 if((memcmp(x25_sk(s
)->calluserdata
.cuddata
,
261 x25_sk(s
)->cudmatchlength
)) == 0) {
275 read_unlock_bh(&x25_list_lock
);
280 * Find a connected X.25 socket given my LCI and neighbour.
282 static struct sock
*__x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
285 struct hlist_node
*node
;
287 sk_for_each(s
, node
, &x25_list
)
288 if (x25_sk(s
)->lci
== lci
&& x25_sk(s
)->neighbour
== nb
) {
297 struct sock
*x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
301 read_lock_bh(&x25_list_lock
);
302 s
= __x25_find_socket(lci
, nb
);
303 read_unlock_bh(&x25_list_lock
);
308 * Find a unique LCI for a given device.
310 static unsigned int x25_new_lci(struct x25_neigh
*nb
)
312 unsigned int lci
= 1;
315 read_lock_bh(&x25_list_lock
);
317 while ((sk
= __x25_find_socket(lci
, nb
)) != NULL
) {
325 read_unlock_bh(&x25_list_lock
);
332 void x25_destroy_socket(struct sock
*);
335 * handler for deferred kills.
337 static void x25_destroy_timer(unsigned long data
)
339 x25_destroy_socket((struct sock
*)data
);
343 * This is called from user mode and the timers. Thus it protects itself
344 * against interrupt users but doesn't worry about being called during
345 * work. Once it is removed from the queue no interrupt or bottom half
346 * will touch it and we are (fairly 8-) ) safe.
347 * Not static as it's used by the timer
349 void x25_destroy_socket(struct sock
*sk
)
355 x25_stop_heartbeat(sk
);
358 x25_remove_socket(sk
);
359 x25_clear_queues(sk
); /* Flush the queues */
361 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
362 if (skb
->sk
!= sk
) { /* A pending connection */
364 * Queue the unaccepted socket for death
366 sock_set_flag(skb
->sk
, SOCK_DEAD
);
367 x25_start_heartbeat(skb
->sk
);
368 x25_sk(skb
->sk
)->state
= X25_STATE_0
;
374 if (sk_has_allocations(sk
)) {
375 /* Defer: outstanding buffers */
376 sk
->sk_timer
.expires
= jiffies
+ 10 * HZ
;
377 sk
->sk_timer
.function
= x25_destroy_timer
;
378 sk
->sk_timer
.data
= (unsigned long)sk
;
379 add_timer(&sk
->sk_timer
);
381 /* drop last reference so sock_put will free */
390 * Handling for system calls applied via the various interfaces to a
391 * X.25 socket object.
394 static int x25_setsockopt(struct socket
*sock
, int level
, int optname
,
395 char __user
*optval
, int optlen
)
398 struct sock
*sk
= sock
->sk
;
399 int rc
= -ENOPROTOOPT
;
401 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
405 if (optlen
< sizeof(int))
409 if (get_user(opt
, (int __user
*)optval
))
412 x25_sk(sk
)->qbitincl
= !!opt
;
418 static int x25_getsockopt(struct socket
*sock
, int level
, int optname
,
419 char __user
*optval
, int __user
*optlen
)
421 struct sock
*sk
= sock
->sk
;
422 int val
, len
, rc
= -ENOPROTOOPT
;
424 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
428 if (get_user(len
, optlen
))
431 len
= min_t(unsigned int, len
, sizeof(int));
438 if (put_user(len
, optlen
))
441 val
= x25_sk(sk
)->qbitincl
;
442 rc
= copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
447 static int x25_listen(struct socket
*sock
, int backlog
)
449 struct sock
*sk
= sock
->sk
;
450 int rc
= -EOPNOTSUPP
;
452 if (sk
->sk_state
!= TCP_LISTEN
) {
453 memset(&x25_sk(sk
)->dest_addr
, 0, X25_ADDR_LEN
);
454 sk
->sk_max_ack_backlog
= backlog
;
455 sk
->sk_state
= TCP_LISTEN
;
462 static struct proto x25_proto
= {
464 .owner
= THIS_MODULE
,
465 .obj_size
= sizeof(struct x25_sock
),
468 static struct sock
*x25_alloc_socket(void)
470 struct x25_sock
*x25
;
471 struct sock
*sk
= sk_alloc(AF_X25
, GFP_ATOMIC
, &x25_proto
, 1);
476 sock_init_data(NULL
, sk
);
479 skb_queue_head_init(&x25
->ack_queue
);
480 skb_queue_head_init(&x25
->fragment_queue
);
481 skb_queue_head_init(&x25
->interrupt_in_queue
);
482 skb_queue_head_init(&x25
->interrupt_out_queue
);
487 static int x25_create(struct socket
*sock
, int protocol
)
490 struct x25_sock
*x25
;
491 int rc
= -ESOCKTNOSUPPORT
;
493 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
)
497 if ((sk
= x25_alloc_socket()) == NULL
)
502 sock_init_data(sock
, sk
);
506 sock
->ops
= &x25_proto_ops
;
507 sk
->sk_protocol
= protocol
;
508 sk
->sk_backlog_rcv
= x25_backlog_rcv
;
510 x25
->t21
= sysctl_x25_call_request_timeout
;
511 x25
->t22
= sysctl_x25_reset_request_timeout
;
512 x25
->t23
= sysctl_x25_clear_request_timeout
;
513 x25
->t2
= sysctl_x25_ack_holdback_timeout
;
514 x25
->state
= X25_STATE_0
;
515 x25
->cudmatchlength
= 0;
516 x25
->accptapprv
= X25_DENY_ACCPT_APPRV
; /* normally no cud */
519 x25
->facilities
.winsize_in
= X25_DEFAULT_WINDOW_SIZE
;
520 x25
->facilities
.winsize_out
= X25_DEFAULT_WINDOW_SIZE
;
521 x25
->facilities
.pacsize_in
= X25_DEFAULT_PACKET_SIZE
;
522 x25
->facilities
.pacsize_out
= X25_DEFAULT_PACKET_SIZE
;
523 x25
->facilities
.throughput
= X25_DEFAULT_THROUGHPUT
;
524 x25
->facilities
.reverse
= X25_DEFAULT_REVERSE
;
525 x25
->dte_facilities
.calling_len
= 0;
526 x25
->dte_facilities
.called_len
= 0;
527 memset(x25
->dte_facilities
.called_ae
, '\0',
528 sizeof(x25
->dte_facilities
.called_ae
));
529 memset(x25
->dte_facilities
.calling_ae
, '\0',
530 sizeof(x25
->dte_facilities
.calling_ae
));
537 static struct sock
*x25_make_new(struct sock
*osk
)
539 struct sock
*sk
= NULL
;
540 struct x25_sock
*x25
, *ox25
;
542 if (osk
->sk_type
!= SOCK_SEQPACKET
)
545 if ((sk
= x25_alloc_socket()) == NULL
)
550 sk
->sk_type
= osk
->sk_type
;
551 sk
->sk_socket
= osk
->sk_socket
;
552 sk
->sk_priority
= osk
->sk_priority
;
553 sk
->sk_protocol
= osk
->sk_protocol
;
554 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
555 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
556 sk
->sk_state
= TCP_ESTABLISHED
;
557 sk
->sk_sleep
= osk
->sk_sleep
;
558 sk
->sk_backlog_rcv
= osk
->sk_backlog_rcv
;
559 sock_copy_flags(sk
, osk
);
562 x25
->t21
= ox25
->t21
;
563 x25
->t22
= ox25
->t22
;
564 x25
->t23
= ox25
->t23
;
566 x25
->facilities
= ox25
->facilities
;
567 x25
->qbitincl
= ox25
->qbitincl
;
568 x25
->dte_facilities
= ox25
->dte_facilities
;
569 x25
->cudmatchlength
= ox25
->cudmatchlength
;
570 x25
->accptapprv
= ox25
->accptapprv
;
577 static int x25_release(struct socket
*sock
)
579 struct sock
*sk
= sock
->sk
;
580 struct x25_sock
*x25
;
587 switch (x25
->state
) {
591 x25_disconnect(sk
, 0, 0, 0);
592 x25_destroy_socket(sk
);
598 x25_clear_queues(sk
);
599 x25_write_internal(sk
, X25_CLEAR_REQUEST
);
600 x25_start_t23timer(sk
);
601 x25
->state
= X25_STATE_2
;
602 sk
->sk_state
= TCP_CLOSE
;
603 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
604 sk
->sk_state_change(sk
);
605 sock_set_flag(sk
, SOCK_DEAD
);
606 sock_set_flag(sk
, SOCK_DESTROY
);
611 sk
->sk_socket
= NULL
; /* Not used, but we should do this */
616 static int x25_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
618 struct sock
*sk
= sock
->sk
;
619 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
621 if (!sock_flag(sk
, SOCK_ZAPPED
) ||
622 addr_len
!= sizeof(struct sockaddr_x25
) ||
623 addr
->sx25_family
!= AF_X25
)
626 x25_sk(sk
)->source_addr
= addr
->sx25_addr
;
627 x25_insert_socket(sk
);
628 sock_reset_flag(sk
, SOCK_ZAPPED
);
629 SOCK_DEBUG(sk
, "x25_bind: socket is bound\n");
634 static int x25_wait_for_connection_establishment(struct sock
*sk
)
636 DECLARE_WAITQUEUE(wait
, current
);
639 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
641 __set_current_state(TASK_INTERRUPTIBLE
);
643 if (signal_pending(current
))
647 sk
->sk_socket
->state
= SS_UNCONNECTED
;
651 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
658 __set_current_state(TASK_RUNNING
);
659 remove_wait_queue(sk
->sk_sleep
, &wait
);
663 static int x25_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
664 int addr_len
, int flags
)
666 struct sock
*sk
= sock
->sk
;
667 struct x25_sock
*x25
= x25_sk(sk
);
668 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
669 struct x25_route
*rt
;
673 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
674 sock
->state
= SS_CONNECTED
;
675 goto out
; /* Connect completed during a ERESTARTSYS event */
679 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
680 sock
->state
= SS_UNCONNECTED
;
684 rc
= -EISCONN
; /* No reconnect on a seqpacket socket */
685 if (sk
->sk_state
== TCP_ESTABLISHED
)
688 sk
->sk_state
= TCP_CLOSE
;
689 sock
->state
= SS_UNCONNECTED
;
692 if (addr_len
!= sizeof(struct sockaddr_x25
) ||
693 addr
->sx25_family
!= AF_X25
)
697 rt
= x25_get_route(&addr
->sx25_addr
);
701 x25
->neighbour
= x25_get_neigh(rt
->dev
);
705 x25_limit_facilities(&x25
->facilities
, x25
->neighbour
);
707 x25
->lci
= x25_new_lci(x25
->neighbour
);
712 if (sock_flag(sk
, SOCK_ZAPPED
)) /* Must bind first - autobinding does not work */
715 if (!strcmp(x25
->source_addr
.x25_addr
, null_x25_address
.x25_addr
))
716 memset(&x25
->source_addr
, '\0', X25_ADDR_LEN
);
718 x25
->dest_addr
= addr
->sx25_addr
;
720 /* Move to connecting socket, start sending Connect Requests */
721 sock
->state
= SS_CONNECTING
;
722 sk
->sk_state
= TCP_SYN_SENT
;
724 x25
->state
= X25_STATE_1
;
726 x25_write_internal(sk
, X25_CALL_REQUEST
);
728 x25_start_heartbeat(sk
);
729 x25_start_t21timer(sk
);
733 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
736 rc
= x25_wait_for_connection_establishment(sk
);
740 sock
->state
= SS_CONNECTED
;
744 x25_neigh_put(x25
->neighbour
);
752 static int x25_wait_for_data(struct sock
*sk
, long timeout
)
754 DECLARE_WAITQUEUE(wait
, current
);
757 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
759 __set_current_state(TASK_INTERRUPTIBLE
);
760 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
763 if (signal_pending(current
))
769 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
771 timeout
= schedule_timeout(timeout
);
776 __set_current_state(TASK_RUNNING
);
777 remove_wait_queue(sk
->sk_sleep
, &wait
);
781 static int x25_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
783 struct sock
*sk
= sock
->sk
;
788 if (!sk
|| sk
->sk_state
!= TCP_LISTEN
)
792 if (sk
->sk_type
!= SOCK_SEQPACKET
)
796 rc
= x25_wait_for_data(sk
, sk
->sk_rcvtimeo
);
799 skb
= skb_dequeue(&sk
->sk_receive_queue
);
804 newsk
->sk_socket
= newsock
;
805 newsk
->sk_sleep
= &newsock
->wait
;
807 /* Now attach up the new socket */
810 sk
->sk_ack_backlog
--;
812 newsock
->state
= SS_CONNECTED
;
820 static int x25_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
821 int *uaddr_len
, int peer
)
823 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)uaddr
;
824 struct sock
*sk
= sock
->sk
;
825 struct x25_sock
*x25
= x25_sk(sk
);
828 if (sk
->sk_state
!= TCP_ESTABLISHED
)
830 sx25
->sx25_addr
= x25
->dest_addr
;
832 sx25
->sx25_addr
= x25
->source_addr
;
834 sx25
->sx25_family
= AF_X25
;
835 *uaddr_len
= sizeof(*sx25
);
840 int x25_rx_call_request(struct sk_buff
*skb
, struct x25_neigh
*nb
,
845 struct x25_sock
*makex25
;
846 struct x25_address source_addr
, dest_addr
;
847 struct x25_facilities facilities
;
848 struct x25_dte_facilities dte_facilities
;
849 int len
, addr_len
, rc
;
852 * Remove the LCI and frame type.
854 skb_pull(skb
, X25_STD_MIN_LEN
);
857 * Extract the X.25 addresses and convert them to ASCII strings,
860 addr_len
= x25_addr_ntoa(skb
->data
, &source_addr
, &dest_addr
);
861 skb_pull(skb
, addr_len
);
864 * Get the length of the facilities, skip past them for the moment
865 * get the call user data because this is needed to determine
866 * the correct listener
868 len
= skb
->data
[0] + 1;
872 * Find a listener for the particular address/cud pair.
874 sk
= x25_find_listener(&source_addr
,skb
);
877 if (sk
!= NULL
&& sk_acceptq_is_full(sk
)) {
882 * We dont have any listeners for this incoming call.
886 skb_push(skb
, addr_len
+ X25_STD_MIN_LEN
);
887 if (sysctl_x25_forward
&&
888 x25_forward_call(&dest_addr
, nb
, skb
, lci
) > 0)
890 /* Call was forwarded, dont process it any more */
895 /* No listeners, can't forward, clear the call */
896 goto out_clear_request
;
901 * Try to reach a compromise on the requested facilities.
903 len
= x25_negotiate_facilities(skb
, sk
, &facilities
, &dte_facilities
);
908 * current neighbour/link might impose additional limits
909 * on certain facilties
912 x25_limit_facilities(&facilities
, nb
);
915 * Try to create a new socket.
917 make
= x25_make_new(sk
);
922 * Remove the facilities
927 make
->sk_state
= TCP_ESTABLISHED
;
929 makex25
= x25_sk(make
);
931 makex25
->dest_addr
= dest_addr
;
932 makex25
->source_addr
= source_addr
;
933 makex25
->neighbour
= nb
;
934 makex25
->facilities
= facilities
;
935 makex25
->dte_facilities
= dte_facilities
;
936 makex25
->vc_facil_mask
= x25_sk(sk
)->vc_facil_mask
;
937 /* ensure no reverse facil on accept */
938 makex25
->vc_facil_mask
&= ~X25_MASK_REVERSE
;
939 /* ensure no calling address extension on accept */
940 makex25
->vc_facil_mask
&= ~X25_MASK_CALLING_AE
;
941 makex25
->cudmatchlength
= x25_sk(sk
)->cudmatchlength
;
943 /* Normally all calls are accepted immediatly */
944 if(makex25
->accptapprv
& X25_DENY_ACCPT_APPRV
) {
945 x25_write_internal(make
, X25_CALL_ACCEPTED
);
946 makex25
->state
= X25_STATE_3
;
950 * Incoming Call User Data.
953 skb_copy_from_linear_data(skb
, makex25
->calluserdata
.cuddata
, skb
->len
);
954 makex25
->calluserdata
.cudlength
= skb
->len
;
957 sk
->sk_ack_backlog
++;
959 x25_insert_socket(make
);
961 skb_queue_head(&sk
->sk_receive_queue
, skb
);
963 x25_start_heartbeat(make
);
965 if (!sock_flag(sk
, SOCK_DEAD
))
966 sk
->sk_data_ready(sk
, skb
->len
);
975 x25_transmit_clear_request(nb
, lci
, 0x01);
979 static int x25_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
980 struct msghdr
*msg
, size_t len
)
982 struct sock
*sk
= sock
->sk
;
983 struct x25_sock
*x25
= x25_sk(sk
);
984 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
985 struct sockaddr_x25 sx25
;
987 unsigned char *asmptr
;
988 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
990 int qbit
= 0, rc
= -EINVAL
;
992 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_OOB
|MSG_EOR
|MSG_CMSG_COMPAT
))
995 /* we currently don't support segmented records at the user interface */
996 if (!(msg
->msg_flags
& (MSG_EOR
|MSG_OOB
)))
1000 if (sock_flag(sk
, SOCK_ZAPPED
))
1004 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1005 send_sig(SIGPIPE
, current
, 0);
1010 if (!x25
->neighbour
)
1015 if (msg
->msg_namelen
< sizeof(sx25
))
1017 memcpy(&sx25
, usx25
, sizeof(sx25
));
1019 if (strcmp(x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
))
1022 if (sx25
.sx25_family
!= AF_X25
)
1026 * FIXME 1003.1g - if the socket is like this because
1027 * it has become closed (not started closed) we ought
1028 * to SIGPIPE, EPIPE;
1031 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1034 sx25
.sx25_family
= AF_X25
;
1035 sx25
.sx25_addr
= x25
->dest_addr
;
1038 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
1040 /* Build a packet */
1041 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
1043 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
1046 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
1048 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
1051 X25_SKB_CB(skb
)->flags
= msg
->msg_flags
;
1053 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
1056 * Put the data on the end
1058 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
1060 skb_reset_transport_header(skb
);
1063 rc
= memcpy_fromiovec(skb_transport_header(skb
), msg
->msg_iov
, len
);
1068 * If the Q BIT Include socket option is in force, the first
1069 * byte of the user data is the logical value of the Q Bit.
1071 if (x25
->qbitincl
) {
1072 qbit
= skb
->data
[0];
1077 * Push down the X.25 header
1079 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
1081 if (msg
->msg_flags
& MSG_OOB
) {
1082 if (x25
->neighbour
->extended
) {
1083 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1084 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1085 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1086 *asmptr
++ = X25_INTERRUPT
;
1088 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1089 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1090 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1091 *asmptr
++ = X25_INTERRUPT
;
1094 if (x25
->neighbour
->extended
) {
1095 /* Build an Extended X.25 header */
1096 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
1097 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1098 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1099 *asmptr
++ = X25_DATA
;
1100 *asmptr
++ = X25_DATA
;
1102 /* Build an Standard X.25 header */
1103 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1104 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1105 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1106 *asmptr
++ = X25_DATA
;
1110 skb
->data
[0] |= X25_Q_BIT
;
1113 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
1114 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
1117 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1120 if (msg
->msg_flags
& MSG_OOB
)
1121 skb_queue_tail(&x25
->interrupt_out_queue
, skb
);
1123 len
= x25_output(sk
, skb
);
1126 else if (x25
->qbitincl
)
1131 * lock_sock() is currently only used to serialize this x25_kick()
1132 * against input-driven x25_kick() calls. It currently only blocks
1133 * incoming packets for this socket and does not protect against
1134 * any other socket state changes and is not called from anywhere
1135 * else. As x25_kick() cannot block and as long as all socket
1136 * operations are BKL-wrapped, we don't need take to care about
1137 * purging the backlog queue in x25_release().
1139 * Using lock_sock() to protect all socket operations entirely
1140 * (and making the whole x25 stack SMP aware) unfortunately would
1141 * require major changes to {send,recv}msg and skb allocation methods.
1156 static int x25_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1157 struct msghdr
*msg
, size_t size
,
1160 struct sock
*sk
= sock
->sk
;
1161 struct x25_sock
*x25
= x25_sk(sk
);
1162 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
1165 struct sk_buff
*skb
;
1166 unsigned char *asmptr
;
1170 * This works for seqpacket too. The receiver has ordered the queue for
1171 * us! We do one quick check first though
1173 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1176 if (flags
& MSG_OOB
) {
1178 if (sock_flag(sk
, SOCK_URGINLINE
) ||
1179 !skb_peek(&x25
->interrupt_in_queue
))
1182 skb
= skb_dequeue(&x25
->interrupt_in_queue
);
1184 skb_pull(skb
, X25_STD_MIN_LEN
);
1187 * No Q bit information on Interrupt data.
1189 if (x25
->qbitincl
) {
1190 asmptr
= skb_push(skb
, 1);
1194 msg
->msg_flags
|= MSG_OOB
;
1196 /* Now we can treat all alike */
1197 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1198 flags
& MSG_DONTWAIT
, &rc
);
1202 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1204 skb_pull(skb
, x25
->neighbour
->extended
?
1205 X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1207 if (x25
->qbitincl
) {
1208 asmptr
= skb_push(skb
, 1);
1213 skb_reset_transport_header(skb
);
1216 if (copied
> size
) {
1218 msg
->msg_flags
|= MSG_TRUNC
;
1221 /* Currently, each datagram always contains a complete record */
1222 msg
->msg_flags
|= MSG_EOR
;
1224 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1226 goto out_free_dgram
;
1229 sx25
->sx25_family
= AF_X25
;
1230 sx25
->sx25_addr
= x25
->dest_addr
;
1233 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1240 skb_free_datagram(sk
, skb
);
1246 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1248 struct sock
*sk
= sock
->sk
;
1249 struct x25_sock
*x25
= x25_sk(sk
);
1250 void __user
*argp
= (void __user
*)arg
;
1255 int amount
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1259 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1264 struct sk_buff
*skb
;
1267 * These two are safe on a single CPU system as
1268 * only user tasks fiddle here
1270 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1272 rc
= put_user(amount
, (unsigned int __user
*)argp
);
1279 rc
= sock_get_timestamp(sk
,
1280 (struct timeval __user
*)argp
);
1285 rc
= sock_get_timestampns(sk
,
1286 (struct timespec __user
*)argp
);
1290 case SIOCGIFDSTADDR
:
1291 case SIOCSIFDSTADDR
:
1292 case SIOCGIFBRDADDR
:
1293 case SIOCSIFBRDADDR
:
1294 case SIOCGIFNETMASK
:
1295 case SIOCSIFNETMASK
:
1303 if (!capable(CAP_NET_ADMIN
))
1305 rc
= x25_route_ioctl(cmd
, argp
);
1307 case SIOCX25GSUBSCRIP
:
1308 rc
= x25_subscr_ioctl(cmd
, argp
);
1310 case SIOCX25SSUBSCRIP
:
1312 if (!capable(CAP_NET_ADMIN
))
1314 rc
= x25_subscr_ioctl(cmd
, argp
);
1316 case SIOCX25GFACILITIES
: {
1317 struct x25_facilities fac
= x25
->facilities
;
1318 rc
= copy_to_user(argp
, &fac
,
1319 sizeof(fac
)) ? -EFAULT
: 0;
1323 case SIOCX25SFACILITIES
: {
1324 struct x25_facilities facilities
;
1326 if (copy_from_user(&facilities
, argp
,
1327 sizeof(facilities
)))
1330 if (sk
->sk_state
!= TCP_LISTEN
&&
1331 sk
->sk_state
!= TCP_CLOSE
)
1333 if (facilities
.pacsize_in
< X25_PS16
||
1334 facilities
.pacsize_in
> X25_PS4096
)
1336 if (facilities
.pacsize_out
< X25_PS16
||
1337 facilities
.pacsize_out
> X25_PS4096
)
1339 if (facilities
.winsize_in
< 1 ||
1340 facilities
.winsize_in
> 127)
1342 if (facilities
.throughput
< 0x03 ||
1343 facilities
.throughput
> 0xDD)
1345 if (facilities
.reverse
&&
1346 (facilities
.reverse
| 0x81)!= 0x81)
1348 x25
->facilities
= facilities
;
1353 case SIOCX25GDTEFACILITIES
: {
1354 rc
= copy_to_user(argp
, &x25
->dte_facilities
,
1355 sizeof(x25
->dte_facilities
));
1361 case SIOCX25SDTEFACILITIES
: {
1362 struct x25_dte_facilities dtefacs
;
1364 if (copy_from_user(&dtefacs
, argp
, sizeof(dtefacs
)))
1367 if (sk
->sk_state
!= TCP_LISTEN
&&
1368 sk
->sk_state
!= TCP_CLOSE
)
1370 if (dtefacs
.calling_len
> X25_MAX_AE_LEN
)
1372 if (dtefacs
.calling_ae
== NULL
)
1374 if (dtefacs
.called_len
> X25_MAX_AE_LEN
)
1376 if (dtefacs
.called_ae
== NULL
)
1378 x25
->dte_facilities
= dtefacs
;
1383 case SIOCX25GCALLUSERDATA
: {
1384 struct x25_calluserdata cud
= x25
->calluserdata
;
1385 rc
= copy_to_user(argp
, &cud
,
1386 sizeof(cud
)) ? -EFAULT
: 0;
1390 case SIOCX25SCALLUSERDATA
: {
1391 struct x25_calluserdata calluserdata
;
1394 if (copy_from_user(&calluserdata
, argp
,
1395 sizeof(calluserdata
)))
1398 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1400 x25
->calluserdata
= calluserdata
;
1405 case SIOCX25GCAUSEDIAG
: {
1406 struct x25_causediag causediag
;
1407 causediag
= x25
->causediag
;
1408 rc
= copy_to_user(argp
, &causediag
,
1409 sizeof(causediag
)) ? -EFAULT
: 0;
1413 case SIOCX25SCUDMATCHLEN
: {
1414 struct x25_subaddr sub_addr
;
1416 if(sk
->sk_state
!= TCP_CLOSE
)
1419 if (copy_from_user(&sub_addr
, argp
,
1423 if(sub_addr
.cudmatchlength
> X25_MAX_CUD_LEN
)
1425 x25
->cudmatchlength
= sub_addr
.cudmatchlength
;
1430 case SIOCX25CALLACCPTAPPRV
: {
1432 if (sk
->sk_state
!= TCP_CLOSE
)
1434 x25
->accptapprv
= X25_ALLOW_ACCPT_APPRV
;
1439 case SIOCX25SENDCALLACCPT
: {
1441 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1443 if (x25
->accptapprv
) /* must call accptapprv above */
1445 x25_write_internal(sk
, X25_CALL_ACCEPTED
);
1446 x25
->state
= X25_STATE_3
;
1459 static struct net_proto_family x25_family_ops
= {
1461 .create
= x25_create
,
1462 .owner
= THIS_MODULE
,
1465 #ifdef CONFIG_COMPAT
1466 static int compat_x25_subscr_ioctl(unsigned int cmd
,
1467 struct compat_x25_subscrip_struct __user
*x25_subscr32
)
1469 struct compat_x25_subscrip_struct x25_subscr
;
1470 struct x25_neigh
*nb
;
1471 struct net_device
*dev
;
1475 if (copy_from_user(&x25_subscr
, x25_subscr32
, sizeof(*x25_subscr32
)))
1479 dev
= x25_dev_get(x25_subscr
.device
);
1483 nb
= x25_get_neigh(dev
);
1489 if (cmd
== SIOCX25GSUBSCRIP
) {
1490 x25_subscr
.extended
= nb
->extended
;
1491 x25_subscr
.global_facil_mask
= nb
->global_facil_mask
;
1492 rc
= copy_to_user(x25_subscr32
, &x25_subscr
,
1493 sizeof(*x25_subscr32
)) ? -EFAULT
: 0;
1496 if (x25_subscr
.extended
== 0 || x25_subscr
.extended
== 1) {
1498 nb
->extended
= x25_subscr
.extended
;
1499 nb
->global_facil_mask
= x25_subscr
.global_facil_mask
;
1510 static int compat_x25_ioctl(struct socket
*sock
, unsigned int cmd
,
1513 void __user
*argp
= compat_ptr(arg
);
1514 struct sock
*sk
= sock
->sk
;
1516 int rc
= -ENOIOCTLCMD
;
1521 rc
= x25_ioctl(sock
, cmd
, (unsigned long)argp
);
1526 rc
= compat_sock_get_timestamp(sk
,
1527 (struct timeval __user
*)argp
);
1532 rc
= compat_sock_get_timestampns(sk
,
1533 (struct timespec __user
*)argp
);
1537 case SIOCGIFDSTADDR
:
1538 case SIOCSIFDSTADDR
:
1539 case SIOCGIFBRDADDR
:
1540 case SIOCSIFBRDADDR
:
1541 case SIOCGIFNETMASK
:
1542 case SIOCSIFNETMASK
:
1550 if (!capable(CAP_NET_ADMIN
))
1552 rc
= x25_route_ioctl(cmd
, argp
);
1554 case SIOCX25GSUBSCRIP
:
1555 rc
= compat_x25_subscr_ioctl(cmd
, argp
);
1557 case SIOCX25SSUBSCRIP
:
1559 if (!capable(CAP_NET_ADMIN
))
1561 rc
= compat_x25_subscr_ioctl(cmd
, argp
);
1563 case SIOCX25GFACILITIES
:
1564 case SIOCX25SFACILITIES
:
1565 case SIOCX25GDTEFACILITIES
:
1566 case SIOCX25SDTEFACILITIES
:
1567 case SIOCX25GCALLUSERDATA
:
1568 case SIOCX25SCALLUSERDATA
:
1569 case SIOCX25GCAUSEDIAG
:
1570 case SIOCX25SCUDMATCHLEN
:
1571 case SIOCX25CALLACCPTAPPRV
:
1572 case SIOCX25SENDCALLACCPT
:
1573 rc
= x25_ioctl(sock
, cmd
, (unsigned long)argp
);
1583 static const struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1585 .owner
= THIS_MODULE
,
1586 .release
= x25_release
,
1588 .connect
= x25_connect
,
1589 .socketpair
= sock_no_socketpair
,
1590 .accept
= x25_accept
,
1591 .getname
= x25_getname
,
1592 .poll
= datagram_poll
,
1594 #ifdef CONFIG_COMPAT
1595 .compat_ioctl
= compat_x25_ioctl
,
1597 .listen
= x25_listen
,
1598 .shutdown
= sock_no_shutdown
,
1599 .setsockopt
= x25_setsockopt
,
1600 .getsockopt
= x25_getsockopt
,
1601 .sendmsg
= x25_sendmsg
,
1602 .recvmsg
= x25_recvmsg
,
1603 .mmap
= sock_no_mmap
,
1604 .sendpage
= sock_no_sendpage
,
1607 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1609 static struct packet_type x25_packet_type
= {
1610 .type
= __constant_htons(ETH_P_X25
),
1611 .func
= x25_lapb_receive_frame
,
1614 static struct notifier_block x25_dev_notifier
= {
1615 .notifier_call
= x25_device_event
,
1618 void x25_kill_by_neigh(struct x25_neigh
*nb
)
1621 struct hlist_node
*node
;
1623 write_lock_bh(&x25_list_lock
);
1625 sk_for_each(s
, node
, &x25_list
)
1626 if (x25_sk(s
)->neighbour
== nb
)
1627 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1629 write_unlock_bh(&x25_list_lock
);
1631 /* Remove any related forwards */
1632 x25_clear_forward_by_dev(nb
->dev
);
1635 static int __init
x25_init(void)
1637 int rc
= proto_register(&x25_proto
, 0);
1642 sock_register(&x25_family_ops
);
1644 dev_add_pack(&x25_packet_type
);
1646 register_netdevice_notifier(&x25_dev_notifier
);
1648 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1650 #ifdef CONFIG_SYSCTL
1651 x25_register_sysctl();
1657 module_init(x25_init
);
1659 static void __exit
x25_exit(void)
1665 #ifdef CONFIG_SYSCTL
1666 x25_unregister_sysctl();
1669 unregister_netdevice_notifier(&x25_dev_notifier
);
1671 dev_remove_pack(&x25_packet_type
);
1673 sock_unregister(AF_X25
);
1674 proto_unregister(&x25_proto
);
1676 module_exit(x25_exit
);
1678 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1679 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1680 MODULE_LICENSE("GPL");
1681 MODULE_ALIAS_NETPROTO(PF_X25
);