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/types.h>
38 #include <linux/socket.h>
40 #include <linux/kernel.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/string.h>
44 #include <linux/sockios.h>
45 #include <linux/net.h>
46 #include <linux/stat.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_arp.h>
50 #include <linux/skbuff.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/fcntl.h>
56 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
58 #include <linux/interrupt.h>
59 #include <linux/notifier.h>
60 #include <linux/init.h>
63 int sysctl_x25_restart_request_timeout
= X25_DEFAULT_T20
;
64 int sysctl_x25_call_request_timeout
= X25_DEFAULT_T21
;
65 int sysctl_x25_reset_request_timeout
= X25_DEFAULT_T22
;
66 int sysctl_x25_clear_request_timeout
= X25_DEFAULT_T23
;
67 int sysctl_x25_ack_holdback_timeout
= X25_DEFAULT_T2
;
70 rwlock_t x25_list_lock
= RW_LOCK_UNLOCKED
;
72 static struct proto_ops x25_proto_ops
;
74 static struct x25_address null_x25_address
= {" "};
76 int x25_addr_ntoa(unsigned char *p
, struct x25_address
*called_addr
,
77 struct x25_address
*calling_addr
)
79 int called_len
, calling_len
;
80 char *called
, *calling
;
83 called_len
= (*p
>> 0) & 0x0F;
84 calling_len
= (*p
>> 4) & 0x0F;
86 called
= called_addr
->x25_addr
;
87 calling
= calling_addr
->x25_addr
;
90 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
93 *called
++ = ((*p
>> 0) & 0x0F) + '0';
96 *called
++ = ((*p
>> 4) & 0x0F) + '0';
100 *calling
++ = ((*p
>> 0) & 0x0F) + '0';
103 *calling
++ = ((*p
>> 4) & 0x0F) + '0';
108 *called
= *calling
= '\0';
110 return 1 + (called_len
+ calling_len
+ 1) / 2;
113 int x25_addr_aton(unsigned char *p
, struct x25_address
*called_addr
,
114 struct x25_address
*calling_addr
)
116 unsigned int called_len
, calling_len
;
117 char *called
, *calling
;
120 called
= called_addr
->x25_addr
;
121 calling
= calling_addr
->x25_addr
;
123 called_len
= strlen(called
);
124 calling_len
= strlen(calling
);
126 *p
++ = (calling_len
<< 4) | (called_len
<< 0);
128 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
129 if (i
< called_len
) {
131 *p
|= (*called
++ - '0') << 0;
135 *p
|= (*called
++ - '0') << 4;
139 *p
|= (*calling
++ - '0') << 0;
143 *p
|= (*calling
++ - '0') << 4;
148 return 1 + (called_len
+ calling_len
+ 1) / 2;
152 * Socket removal during an interrupt is now safe.
154 static void x25_remove_socket(struct sock
*sk
)
156 write_lock_bh(&x25_list_lock
);
157 sk_del_node_init(sk
);
158 write_unlock_bh(&x25_list_lock
);
162 * Kill all bound sockets on a dropped device.
164 static void x25_kill_by_device(struct net_device
*dev
)
167 struct hlist_node
*node
;
169 write_lock_bh(&x25_list_lock
);
171 sk_for_each(s
, node
, &x25_list
)
172 if (x25_sk(s
)->neighbour
&& x25_sk(s
)->neighbour
->dev
== dev
)
173 x25_disconnect(s
, ENETUNREACH
, 0, 0);
175 write_unlock_bh(&x25_list_lock
);
179 * Handle device status changes.
181 static int x25_device_event(struct notifier_block
*this, unsigned long event
,
184 struct net_device
*dev
= ptr
;
185 struct x25_neigh
*nb
;
187 if (dev
->type
== ARPHRD_X25
188 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
189 || dev
->type
== ARPHRD_ETHER
194 x25_link_device_up(dev
);
196 case NETDEV_GOING_DOWN
:
197 nb
= x25_get_neigh(dev
);
199 x25_terminate_link(nb
);
204 x25_kill_by_device(dev
);
205 x25_route_device_down(dev
);
206 x25_link_device_down(dev
);
215 * Add a socket to the bound sockets list.
217 static void x25_insert_socket(struct sock
*sk
)
219 write_lock_bh(&x25_list_lock
);
220 sk_add_node(sk
, &x25_list
);
221 write_unlock_bh(&x25_list_lock
);
225 * Find a socket that wants to accept the Call Request we just
228 static struct sock
*x25_find_listener(struct x25_address
*addr
)
231 struct hlist_node
*node
;
233 read_lock_bh(&x25_list_lock
);
235 sk_for_each(s
, node
, &x25_list
)
236 if ((!strcmp(addr
->x25_addr
,
237 x25_sk(s
)->source_addr
.x25_addr
) ||
238 !strcmp(addr
->x25_addr
,
239 null_x25_address
.x25_addr
)) &&
240 s
->sk_state
== TCP_LISTEN
) {
246 read_unlock_bh(&x25_list_lock
);
251 * Find a connected X.25 socket given my LCI and neighbour.
253 struct sock
*__x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
256 struct hlist_node
*node
;
258 sk_for_each(s
, node
, &x25_list
)
259 if (x25_sk(s
)->lci
== lci
&& x25_sk(s
)->neighbour
== nb
) {
268 struct sock
*x25_find_socket(unsigned int lci
, struct x25_neigh
*nb
)
272 read_lock_bh(&x25_list_lock
);
273 s
= __x25_find_socket(lci
, nb
);
274 read_unlock_bh(&x25_list_lock
);
279 * Find a unique LCI for a given device.
281 unsigned int x25_new_lci(struct x25_neigh
*nb
)
283 unsigned int lci
= 1;
286 read_lock_bh(&x25_list_lock
);
288 while ((sk
= __x25_find_socket(lci
, nb
)) != NULL
) {
296 read_unlock_bh(&x25_list_lock
);
303 void x25_destroy_socket(struct sock
*);
306 * handler for deferred kills.
308 static void x25_destroy_timer(unsigned long data
)
310 x25_destroy_socket((struct sock
*)data
);
314 * This is called from user mode and the timers. Thus it protects itself
315 * against interrupt users but doesn't worry about being called during
316 * work. Once it is removed from the queue no interrupt or bottom half
317 * will touch it and we are (fairly 8-) ) safe.
318 * Not static as it's used by the timer
320 void x25_destroy_socket(struct sock
*sk
)
326 x25_stop_heartbeat(sk
);
329 x25_remove_socket(sk
);
330 x25_clear_queues(sk
); /* Flush the queues */
332 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
333 if (skb
->sk
!= sk
) { /* A pending connection */
335 * Queue the unaccepted socket for death
337 sock_set_flag(skb
->sk
, SOCK_DEAD
);
338 x25_start_heartbeat(skb
->sk
);
339 x25_sk(skb
->sk
)->state
= X25_STATE_0
;
345 if (atomic_read(&sk
->sk_wmem_alloc
) ||
346 atomic_read(&sk
->sk_rmem_alloc
)) {
347 /* Defer: outstanding buffers */
348 init_timer(&sk
->sk_timer
);
349 sk
->sk_timer
.expires
= jiffies
+ 10 * HZ
;
350 sk
->sk_timer
.function
= x25_destroy_timer
;
351 sk
->sk_timer
.data
= (unsigned long)sk
;
352 add_timer(&sk
->sk_timer
);
360 * Handling for system calls applied via the various interfaces to a
361 * X.25 socket object.
364 static int x25_setsockopt(struct socket
*sock
, int level
, int optname
,
365 char *optval
, int optlen
)
368 struct sock
*sk
= sock
->sk
;
369 int rc
= -ENOPROTOOPT
;
371 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
375 if (optlen
< sizeof(int))
379 if (get_user(opt
, (int *)optval
))
382 x25_sk(sk
)->qbitincl
= !!opt
;
388 static int x25_getsockopt(struct socket
*sock
, int level
, int optname
,
389 char *optval
, int *optlen
)
391 struct sock
*sk
= sock
->sk
;
392 int val
, len
, rc
= -ENOPROTOOPT
;
394 if (level
!= SOL_X25
|| optname
!= X25_QBITINCL
)
398 if (get_user(len
, optlen
))
401 len
= min_t(unsigned int, len
, sizeof(int));
408 if (put_user(len
, optlen
))
411 val
= x25_sk(sk
)->qbitincl
;
412 rc
= copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
417 static int x25_listen(struct socket
*sock
, int backlog
)
419 struct sock
*sk
= sock
->sk
;
420 int rc
= -EOPNOTSUPP
;
422 if (sk
->sk_state
!= TCP_LISTEN
) {
423 memset(&x25_sk(sk
)->dest_addr
, 0, X25_ADDR_LEN
);
424 sk
->sk_max_ack_backlog
= backlog
;
425 sk
->sk_state
= TCP_LISTEN
;
432 static struct sock
*x25_alloc_socket(void)
435 struct sock
*sk
= sk_alloc(AF_X25
, GFP_ATOMIC
, 1, NULL
);
440 x25
= x25_sk(sk
) = kmalloc(sizeof(*x25
), GFP_ATOMIC
);
444 memset(x25
, 0, sizeof(*x25
));
448 sock_init_data(NULL
, sk
);
450 skb_queue_head_init(&x25
->ack_queue
);
451 skb_queue_head_init(&x25
->fragment_queue
);
452 skb_queue_head_init(&x25
->interrupt_in_queue
);
453 skb_queue_head_init(&x25
->interrupt_out_queue
);
462 static int x25_create(struct socket
*sock
, int protocol
)
466 int rc
= -ESOCKTNOSUPPORT
;
468 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
)
472 if ((sk
= x25_alloc_socket()) == NULL
)
477 sock_init_data(sock
, sk
);
479 init_timer(&x25
->timer
);
481 sock
->ops
= &x25_proto_ops
;
482 sk
->sk_protocol
= protocol
;
483 sk
->sk_backlog_rcv
= x25_backlog_rcv
;
485 x25
->t21
= sysctl_x25_call_request_timeout
;
486 x25
->t22
= sysctl_x25_reset_request_timeout
;
487 x25
->t23
= sysctl_x25_clear_request_timeout
;
488 x25
->t2
= sysctl_x25_ack_holdback_timeout
;
489 x25
->state
= X25_STATE_0
;
491 x25
->facilities
.winsize_in
= X25_DEFAULT_WINDOW_SIZE
;
492 x25
->facilities
.winsize_out
= X25_DEFAULT_WINDOW_SIZE
;
493 x25
->facilities
.pacsize_in
= X25_DEFAULT_PACKET_SIZE
;
494 x25
->facilities
.pacsize_out
= X25_DEFAULT_PACKET_SIZE
;
495 x25
->facilities
.throughput
= X25_DEFAULT_THROUGHPUT
;
496 x25
->facilities
.reverse
= X25_DEFAULT_REVERSE
;
502 static struct sock
*x25_make_new(struct sock
*osk
)
504 struct sock
*sk
= NULL
;
505 struct x25_opt
*x25
, *ox25
;
507 if (osk
->sk_type
!= SOCK_SEQPACKET
)
510 if ((sk
= x25_alloc_socket()) == NULL
)
515 sk
->sk_type
= osk
->sk_type
;
516 sk
->sk_socket
= osk
->sk_socket
;
517 sk
->sk_priority
= osk
->sk_priority
;
518 sk
->sk_protocol
= osk
->sk_protocol
;
519 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
520 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
521 sk
->sk_debug
= osk
->sk_debug
;
522 sk
->sk_state
= TCP_ESTABLISHED
;
523 sk
->sk_sleep
= osk
->sk_sleep
;
524 sk
->sk_zapped
= osk
->sk_zapped
;
525 sk
->sk_backlog_rcv
= osk
->sk_backlog_rcv
;
528 x25
->t21
= ox25
->t21
;
529 x25
->t22
= ox25
->t22
;
530 x25
->t23
= ox25
->t23
;
532 x25
->facilities
= ox25
->facilities
;
533 x25
->qbitincl
= ox25
->qbitincl
;
535 init_timer(&x25
->timer
);
540 static int x25_release(struct socket
*sock
)
542 struct sock
*sk
= sock
->sk
;
550 switch (x25
->state
) {
554 x25_disconnect(sk
, 0, 0, 0);
555 x25_destroy_socket(sk
);
561 x25_clear_queues(sk
);
562 x25_write_internal(sk
, X25_CLEAR_REQUEST
);
563 x25_start_t23timer(sk
);
564 x25
->state
= X25_STATE_2
;
565 sk
->sk_state
= TCP_CLOSE
;
566 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
567 sk
->sk_state_change(sk
);
568 sock_set_flag(sk
, SOCK_DEAD
);
569 sock_set_flag(sk
, SOCK_DESTROY
);
574 sk
->sk_socket
= NULL
; /* Not used, but we should do this */
579 static int x25_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
581 struct sock
*sk
= sock
->sk
;
582 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
584 if (!sk
->sk_zapped
||
585 addr_len
!= sizeof(struct sockaddr_x25
) ||
586 addr
->sx25_family
!= AF_X25
)
589 x25_sk(sk
)->source_addr
= addr
->sx25_addr
;
590 x25_insert_socket(sk
);
592 SOCK_DEBUG(sk
, "x25_bind: socket is bound\n");
597 static int x25_wait_for_connection_establishment(struct sock
*sk
)
599 DECLARE_WAITQUEUE(wait
, current
);
602 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
604 __set_current_state(TASK_INTERRUPTIBLE
);
606 if (signal_pending(current
))
610 sk
->sk_socket
->state
= SS_UNCONNECTED
;
614 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
621 __set_current_state(TASK_RUNNING
);
622 remove_wait_queue(sk
->sk_sleep
, &wait
);
626 static int x25_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
627 int addr_len
, int flags
)
629 struct sock
*sk
= sock
->sk
;
630 struct x25_opt
*x25
= x25_sk(sk
);
631 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
632 struct x25_route
*rt
;
636 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
637 sock
->state
= SS_CONNECTED
;
638 goto out
; /* Connect completed during a ERESTARTSYS event */
642 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
643 sock
->state
= SS_UNCONNECTED
;
647 rc
= -EISCONN
; /* No reconnect on a seqpacket socket */
648 if (sk
->sk_state
== TCP_ESTABLISHED
)
651 sk
->sk_state
= TCP_CLOSE
;
652 sock
->state
= SS_UNCONNECTED
;
655 if (addr_len
!= sizeof(struct sockaddr_x25
) ||
656 addr
->sx25_family
!= AF_X25
)
660 rt
= x25_get_route(&addr
->sx25_addr
);
664 x25
->neighbour
= x25_get_neigh(rt
->dev
);
668 x25_limit_facilities(&x25
->facilities
, x25
->neighbour
);
670 x25
->lci
= x25_new_lci(x25
->neighbour
);
675 if (sk
->sk_zapped
) /* Must bind first - autobinding does not work */
678 if (!strcmp(x25
->source_addr
.x25_addr
, null_x25_address
.x25_addr
))
679 memset(&x25
->source_addr
, '\0', X25_ADDR_LEN
);
681 x25
->dest_addr
= addr
->sx25_addr
;
683 /* Move to connecting socket, start sending Connect Requests */
684 sock
->state
= SS_CONNECTING
;
685 sk
->sk_state
= TCP_SYN_SENT
;
687 x25
->state
= X25_STATE_1
;
689 x25_write_internal(sk
, X25_CALL_REQUEST
);
691 x25_start_heartbeat(sk
);
692 x25_start_t21timer(sk
);
696 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
699 rc
= x25_wait_for_connection_establishment(sk
);
703 sock
->state
= SS_CONNECTED
;
707 x25_neigh_put(x25
->neighbour
);
715 static int x25_wait_for_data(struct sock
*sk
, int timeout
)
717 DECLARE_WAITQUEUE(wait
, current
);
720 add_wait_queue_exclusive(sk
->sk_sleep
, &wait
);
722 __set_current_state(TASK_INTERRUPTIBLE
);
723 if (sk
->sk_shutdown
& RCV_SHUTDOWN
)
726 if (signal_pending(current
))
732 if (skb_queue_empty(&sk
->sk_receive_queue
)) {
734 timeout
= schedule_timeout(timeout
);
739 __set_current_state(TASK_RUNNING
);
740 remove_wait_queue(sk
->sk_sleep
, &wait
);
744 static int x25_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
746 struct sock
*sk
= sock
->sk
;
751 if (!sk
|| sk
->sk_state
!= TCP_LISTEN
)
755 if (sk
->sk_type
!= SOCK_SEQPACKET
)
758 rc
= x25_wait_for_data(sk
, sk
->sk_rcvtimeo
);
761 skb
= skb_dequeue(&sk
->sk_receive_queue
);
766 newsk
->sk_pair
= NULL
;
767 newsk
->sk_socket
= newsock
;
768 newsk
->sk_sleep
= &newsock
->wait
;
770 /* Now attach up the new socket */
773 sk
->sk_ack_backlog
--;
775 newsock
->state
= SS_CONNECTED
;
781 static int x25_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
782 int *uaddr_len
, int peer
)
784 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)uaddr
;
785 struct sock
*sk
= sock
->sk
;
786 struct x25_opt
*x25
= x25_sk(sk
);
789 if (sk
->sk_state
!= TCP_ESTABLISHED
)
791 sx25
->sx25_addr
= x25
->dest_addr
;
793 sx25
->sx25_addr
= x25
->source_addr
;
795 sx25
->sx25_family
= AF_X25
;
796 *uaddr_len
= sizeof(*sx25
);
801 int x25_rx_call_request(struct sk_buff
*skb
, struct x25_neigh
*nb
,
806 struct x25_opt
*makex25
;
807 struct x25_address source_addr
, dest_addr
;
808 struct x25_facilities facilities
;
812 * Remove the LCI and frame type.
814 skb_pull(skb
, X25_STD_MIN_LEN
);
817 * Extract the X.25 addresses and convert them to ASCII strings,
820 skb_pull(skb
, x25_addr_ntoa(skb
->data
, &source_addr
, &dest_addr
));
823 * Find a listener for the particular address.
825 sk
= x25_find_listener(&source_addr
);
828 * We can't accept the Call Request.
830 if (!sk
|| sk
->sk_ack_backlog
== sk
->sk_max_ack_backlog
)
831 goto out_clear_request
;
834 * Try to reach a compromise on the requested facilities.
836 if ((len
= x25_negotiate_facilities(skb
, sk
, &facilities
)) == -1)
840 * current neighbour/link might impose additional limits
841 * on certain facilties
844 x25_limit_facilities(&facilities
, nb
);
847 * Try to create a new socket.
849 make
= x25_make_new(sk
);
854 * Remove the facilities, leaving any Call User Data.
859 make
->sk_state
= TCP_ESTABLISHED
;
861 makex25
= x25_sk(make
);
863 makex25
->dest_addr
= dest_addr
;
864 makex25
->source_addr
= source_addr
;
865 makex25
->neighbour
= nb
;
866 makex25
->facilities
= facilities
;
867 makex25
->vc_facil_mask
= x25_sk(sk
)->vc_facil_mask
;
869 x25_write_internal(make
, X25_CALL_ACCEPTED
);
872 * Incoming Call User Data.
875 memcpy(makex25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
876 makex25
->calluserdata
.cudlength
= skb
->len
;
879 makex25
->state
= X25_STATE_3
;
881 sk
->sk_ack_backlog
++;
884 x25_insert_socket(make
);
886 skb_queue_head(&sk
->sk_receive_queue
, skb
);
888 x25_start_heartbeat(make
);
890 if (!sock_flag(sk
, SOCK_DEAD
))
891 sk
->sk_data_ready(sk
, skb
->len
);
900 x25_transmit_clear_request(nb
, lci
, 0x01);
904 static int x25_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
905 struct msghdr
*msg
, int len
)
907 struct sock
*sk
= sock
->sk
;
908 struct x25_opt
*x25
= x25_sk(sk
);
909 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
910 struct sockaddr_x25 sx25
;
912 unsigned char *asmptr
;
913 int noblock
= msg
->msg_flags
& MSG_DONTWAIT
;
914 int size
, qbit
= 0, rc
= -EINVAL
;
916 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_OOB
| MSG_EOR
))
919 /* we currently don't support segmented records at the user interface */
920 if (!(msg
->msg_flags
& (MSG_EOR
|MSG_OOB
)))
928 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
929 send_sig(SIGPIPE
, current
, 0);
939 if (msg
->msg_namelen
< sizeof(sx25
))
941 memcpy(&sx25
, usx25
, sizeof(sx25
));
943 if (strcmp(x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
))
946 if (sx25
.sx25_family
!= AF_X25
)
950 * FIXME 1003.1g - if the socket is like this because
951 * it has become closed (not started closed) we ought
955 if (sk
->sk_state
!= TCP_ESTABLISHED
)
958 sx25
.sx25_family
= AF_X25
;
959 sx25
.sx25_addr
= x25
->dest_addr
;
962 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
965 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
967 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
970 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
972 skb
= sock_alloc_send_skb(sk
, size
, noblock
, &rc
);
975 X25_SKB_CB(skb
)->flags
= msg
->msg_flags
;
977 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
980 * Put the data on the end
982 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
984 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
986 rc
= memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
991 * If the Q BIT Include socket option is in force, the first
992 * byte of the user data is the logical value of the Q Bit.
1000 * Push down the X.25 header
1002 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
1004 if (msg
->msg_flags
& MSG_OOB
) {
1005 if (x25
->neighbour
->extended
) {
1006 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1007 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1008 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1009 *asmptr
++ = X25_INTERRUPT
;
1011 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1012 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1013 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1014 *asmptr
++ = X25_INTERRUPT
;
1017 if (x25
->neighbour
->extended
) {
1018 /* Build an Extended X.25 header */
1019 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
1020 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
1021 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1022 *asmptr
++ = X25_DATA
;
1023 *asmptr
++ = X25_DATA
;
1025 /* Build an Standard X.25 header */
1026 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
1027 *asmptr
++ = ((x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
1028 *asmptr
++ = (x25
->lci
>> 0) & 0xFF;
1029 *asmptr
++ = X25_DATA
;
1033 skb
->data
[0] |= X25_Q_BIT
;
1036 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
1037 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
1040 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1043 if (msg
->msg_flags
& MSG_OOB
)
1044 skb_queue_tail(&x25
->interrupt_out_queue
, skb
);
1046 len
= x25_output(sk
, skb
);
1049 else if (x25
->qbitincl
)
1054 * lock_sock() is currently only used to serialize this x25_kick()
1055 * against input-driven x25_kick() calls. It currently only blocks
1056 * incoming packets for this socket and does not protect against
1057 * any other socket state changes and is not called from anywhere
1058 * else. As x25_kick() cannot block and as long as all socket
1059 * operations are BKL-wrapped, we don't need take to care about
1060 * purging the backlog queue in x25_release().
1062 * Using lock_sock() to protect all socket operations entirely
1063 * (and making the whole x25 stack SMP aware) unfortunately would
1064 * require major changes to {send,recv}msg and skb allocation methods.
1079 static int x25_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1080 struct msghdr
*msg
, int size
,
1083 struct sock
*sk
= sock
->sk
;
1084 struct x25_opt
*x25
= x25_sk(sk
);
1085 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
1087 struct sk_buff
*skb
;
1088 unsigned char *asmptr
;
1092 * This works for seqpacket too. The receiver has ordered the queue for
1093 * us! We do one quick check first though
1095 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1098 if (flags
& MSG_OOB
) {
1100 if (sock_flag(sk
, SOCK_URGINLINE
) ||
1101 !skb_peek(&x25
->interrupt_in_queue
))
1104 skb
= skb_dequeue(&x25
->interrupt_in_queue
);
1106 skb_pull(skb
, X25_STD_MIN_LEN
);
1109 * No Q bit information on Interrupt data.
1111 if (x25
->qbitincl
) {
1112 asmptr
= skb_push(skb
, 1);
1116 msg
->msg_flags
|= MSG_OOB
;
1118 /* Now we can treat all alike */
1119 skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
,
1120 flags
& MSG_DONTWAIT
, &rc
);
1124 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1126 skb_pull(skb
, x25
->neighbour
->extended
?
1127 X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1129 if (x25
->qbitincl
) {
1130 asmptr
= skb_push(skb
, 1);
1135 skb
->h
.raw
= skb
->data
;
1139 if (copied
> size
) {
1141 msg
->msg_flags
|= MSG_TRUNC
;
1144 /* Currently, each datagram always contains a complete record */
1145 msg
->msg_flags
|= MSG_EOR
;
1147 rc
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1149 goto out_free_dgram
;
1152 sx25
->sx25_family
= AF_X25
;
1153 sx25
->sx25_addr
= x25
->dest_addr
;
1156 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1163 skb_free_datagram(sk
, skb
);
1169 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1171 struct sock
*sk
= sock
->sk
;
1172 struct x25_opt
*x25
= x25_sk(sk
);
1177 int amount
= sk
->sk_sndbuf
-
1178 atomic_read(&sk
->sk_wmem_alloc
);
1181 rc
= put_user(amount
, (unsigned int *)arg
);
1186 struct sk_buff
*skb
;
1189 * These two are safe on a single CPU system as
1190 * only user tasks fiddle here
1192 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1194 rc
= put_user(amount
, (unsigned int *)arg
);
1201 if (!sk
->sk_stamp
.tv_sec
)
1203 rc
= copy_to_user((void *)arg
, &sk
->sk_stamp
,
1204 sizeof(struct timeval
)) ? -EFAULT
: 0;
1210 case SIOCGIFDSTADDR
:
1211 case SIOCSIFDSTADDR
:
1212 case SIOCGIFBRDADDR
:
1213 case SIOCSIFBRDADDR
:
1214 case SIOCGIFNETMASK
:
1215 case SIOCSIFNETMASK
:
1223 if (!capable(CAP_NET_ADMIN
))
1225 rc
= x25_route_ioctl(cmd
, (void *)arg
);
1227 case SIOCX25GSUBSCRIP
:
1228 rc
= x25_subscr_ioctl(cmd
, (void *)arg
);
1230 case SIOCX25SSUBSCRIP
:
1232 if (!capable(CAP_NET_ADMIN
))
1234 rc
= x25_subscr_ioctl(cmd
, (void *)arg
);
1236 case SIOCX25GFACILITIES
: {
1237 struct x25_facilities fac
= x25
->facilities
;
1238 rc
= copy_to_user((void *)arg
, &fac
,
1239 sizeof(fac
)) ? -EFAULT
: 0;
1243 case SIOCX25SFACILITIES
: {
1244 struct x25_facilities facilities
;
1246 if (copy_from_user(&facilities
, (void *)arg
,
1247 sizeof(facilities
)))
1250 if (sk
->sk_state
!= TCP_LISTEN
&&
1251 sk
->sk_state
!= TCP_CLOSE
)
1253 if (facilities
.pacsize_in
< X25_PS16
||
1254 facilities
.pacsize_in
> X25_PS4096
)
1256 if (facilities
.pacsize_out
< X25_PS16
||
1257 facilities
.pacsize_out
> X25_PS4096
)
1259 if (facilities
.winsize_in
< 1 ||
1260 facilities
.winsize_in
> 127)
1262 if (facilities
.throughput
< 0x03 ||
1263 facilities
.throughput
> 0xDD)
1265 if (facilities
.reverse
&& facilities
.reverse
!= 1)
1267 x25
->facilities
= facilities
;
1272 case SIOCX25GCALLUSERDATA
: {
1273 struct x25_calluserdata cud
= x25
->calluserdata
;
1274 rc
= copy_to_user((void *)arg
, &cud
,
1275 sizeof(cud
)) ? -EFAULT
: 0;
1279 case SIOCX25SCALLUSERDATA
: {
1280 struct x25_calluserdata calluserdata
;
1283 if (copy_from_user(&calluserdata
, (void *)arg
,
1284 sizeof(calluserdata
)))
1287 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1289 x25
->calluserdata
= calluserdata
;
1294 case SIOCX25GCAUSEDIAG
: {
1295 struct x25_causediag causediag
;
1296 causediag
= x25
->causediag
;
1297 rc
= copy_to_user((void *)arg
, &causediag
,
1298 sizeof(causediag
)) ? -EFAULT
: 0;
1303 rc
= dev_ioctl(cmd
, (void *)arg
);
1310 struct net_proto_family x25_family_ops
= {
1312 .create
= x25_create
,
1313 .owner
= THIS_MODULE
,
1316 static struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1318 .owner
= THIS_MODULE
,
1319 .release
= x25_release
,
1321 .connect
= x25_connect
,
1322 .socketpair
= sock_no_socketpair
,
1323 .accept
= x25_accept
,
1324 .getname
= x25_getname
,
1325 .poll
= datagram_poll
,
1327 .listen
= x25_listen
,
1328 .shutdown
= sock_no_shutdown
,
1329 .setsockopt
= x25_setsockopt
,
1330 .getsockopt
= x25_getsockopt
,
1331 .sendmsg
= x25_sendmsg
,
1332 .recvmsg
= x25_recvmsg
,
1333 .mmap
= sock_no_mmap
,
1334 .sendpage
= sock_no_sendpage
,
1337 #include <linux/smp_lock.h>
1338 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1340 static struct packet_type x25_packet_type
= {
1341 .type
= __constant_htons(ETH_P_X25
),
1342 .func
= x25_lapb_receive_frame
,
1345 struct notifier_block x25_dev_notifier
= {
1346 .notifier_call
= x25_device_event
,
1349 void x25_kill_by_neigh(struct x25_neigh
*nb
)
1352 struct hlist_node
*node
;
1354 write_lock_bh(&x25_list_lock
);
1356 sk_for_each(s
, node
, &x25_list
)
1357 if (x25_sk(s
)->neighbour
== nb
)
1358 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1360 write_unlock_bh(&x25_list_lock
);
1363 static int __init
x25_init(void)
1366 struct net_device
*dev
;
1368 sock_register(&x25_family_ops
);
1370 dev_add_pack(&x25_packet_type
);
1372 register_netdevice_notifier(&x25_dev_notifier
);
1374 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1376 #ifdef CONFIG_SYSCTL
1377 x25_register_sysctl();
1383 * Register any pre existing devices.
1385 read_lock(&dev_base_lock
);
1386 for (dev
= dev_base
; dev
; dev
= dev
->next
) {
1387 if ((dev
->flags
& IFF_UP
) && (dev
->type
== ARPHRD_X25
1388 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1389 || dev
->type
== ARPHRD_ETHER
1392 x25_link_device_up(dev
);
1394 read_unlock(&dev_base_lock
);
1398 module_init(x25_init
);
1400 static void __exit
x25_exit(void)
1406 #ifdef CONFIG_SYSCTL
1407 x25_unregister_sysctl();
1410 unregister_netdevice_notifier(&x25_dev_notifier
);
1412 dev_remove_pack(&x25_packet_type
);
1414 sock_unregister(AF_X25
);
1416 module_exit(x25_exit
);
1418 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1419 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1420 MODULE_LICENSE("GPL");