2 * X.25 Packet Layer release 002
4 * This is ALPHA test software. This code may break your machine, randomly fail to work with new
5 * releases, misbehave and/or generally screw up. It might even work.
7 * This code REQUIRES 2.1.15 or higher
10 * This module is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 * X.25 001 Jonathan Naylor Started coding.
17 * X.25 002 Jonathan Naylor Centralised disconnect handling.
18 * New timer architecture.
21 #include <linux/config.h>
22 #if defined(CONFIG_X25) || defined(CONFIG_X25_MODULE)
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
28 #include <linux/kernel.h>
29 #include <linux/sched.h>
30 #include <linux/timer.h>
31 #include <linux/string.h>
32 #include <linux/sockios.h>
33 #include <linux/net.h>
34 #include <linux/stat.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/if_arp.h>
38 #include <linux/skbuff.h>
40 #include <asm/segment.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <linux/fcntl.h>
44 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
46 #include <linux/interrupt.h>
47 #include <linux/notifier.h>
48 #include <linux/proc_fs.h>
49 #include <linux/init.h>
52 int sysctl_x25_restart_request_timeout
= X25_DEFAULT_T20
;
53 int sysctl_x25_call_request_timeout
= X25_DEFAULT_T21
;
54 int sysctl_x25_reset_request_timeout
= X25_DEFAULT_T22
;
55 int sysctl_x25_clear_request_timeout
= X25_DEFAULT_T23
;
56 int sysctl_x25_ack_holdback_timeout
= X25_DEFAULT_T2
;
58 static struct sock
*volatile x25_list
= NULL
;
60 static struct proto_ops x25_proto_ops
;
62 static x25_address null_x25_address
= {" "};
64 int x25_addr_ntoa(unsigned char *p
, x25_address
*called_addr
, x25_address
*calling_addr
)
66 int called_len
, calling_len
;
67 char *called
, *calling
;
70 called_len
= (*p
>> 0) & 0x0F;
71 calling_len
= (*p
>> 4) & 0x0F;
73 called
= called_addr
->x25_addr
;
74 calling
= calling_addr
->x25_addr
;
77 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
80 *called
++ = ((*p
>> 0) & 0x0F) + '0';
83 *called
++ = ((*p
>> 4) & 0x0F) + '0';
87 *calling
++ = ((*p
>> 0) & 0x0F) + '0';
90 *calling
++ = ((*p
>> 4) & 0x0F) + '0';
98 return 1 + (called_len
+ calling_len
+ 1) / 2;
101 int x25_addr_aton(unsigned char *p
, x25_address
*called_addr
, x25_address
*calling_addr
)
103 unsigned int called_len
, calling_len
;
104 char *called
, *calling
;
107 called
= called_addr
->x25_addr
;
108 calling
= calling_addr
->x25_addr
;
110 called_len
= strlen(called
);
111 calling_len
= strlen(calling
);
113 *p
++ = (calling_len
<< 4) | (called_len
<< 0);
115 for (i
= 0; i
< (called_len
+ calling_len
); i
++) {
116 if (i
< called_len
) {
118 *p
|= (*called
++ - '0') << 0;
122 *p
|= (*called
++ - '0') << 4;
126 *p
|= (*calling
++ - '0') << 0;
130 *p
|= (*calling
++ - '0') << 4;
135 return 1 + (called_len
+ calling_len
+ 1) / 2;
139 * Socket removal during an interrupt is now safe.
141 static void x25_remove_socket(struct sock
*sk
)
149 if ((s
= x25_list
) == sk
) {
151 restore_flags(flags
);
155 while (s
!= NULL
&& s
->next
!= NULL
) {
158 restore_flags(flags
);
165 restore_flags(flags
);
169 * Kill all bound sockets on a dropped device.
171 static void x25_kill_by_device(struct net_device
*dev
)
175 for (s
= x25_list
; s
!= NULL
; s
= s
->next
)
176 if (s
->protinfo
.x25
->neighbour
&&
177 s
->protinfo
.x25
->neighbour
->dev
== dev
)
178 x25_disconnect(s
, ENETUNREACH
, 0, 0);
182 * Handle device status changes.
184 static int x25_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
186 struct net_device
*dev
= (struct net_device
*)ptr
;
188 if (dev
->type
== ARPHRD_X25
189 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
190 || dev
->type
== ARPHRD_ETHER
195 x25_link_device_up(dev
);
198 x25_kill_by_device(dev
);
199 x25_route_device_down(dev
);
200 x25_link_device_down(dev
);
209 * Add a socket to the bound sockets list.
211 static void x25_insert_socket(struct sock
*sk
)
221 restore_flags(flags
);
225 * Find a socket that wants to accept the Call Request we just
228 static struct sock
*x25_find_listener(x25_address
*addr
)
236 for (s
= x25_list
; s
!= NULL
; s
= s
->next
) {
237 if ((strcmp(addr
->x25_addr
, s
->protinfo
.x25
->source_addr
.x25_addr
) == 0 ||
238 strcmp(addr
->x25_addr
, null_x25_address
.x25_addr
) == 0) &&
239 s
->state
== TCP_LISTEN
) {
240 restore_flags(flags
);
245 restore_flags(flags
);
250 * Find a connected X.25 socket given my LCI and neighbour.
252 struct sock
*x25_find_socket(unsigned int lci
, struct x25_neigh
*neigh
)
260 for (s
= x25_list
; s
!= NULL
; s
= s
->next
) {
261 if (s
->protinfo
.x25
->lci
== lci
&& s
->protinfo
.x25
->neighbour
== neigh
) {
262 restore_flags(flags
);
267 restore_flags(flags
);
272 * Find a unique LCI for a given device.
274 unsigned int x25_new_lci(struct x25_neigh
*neigh
)
276 unsigned int lci
= 1;
278 while (x25_find_socket(lci
, neigh
) != NULL
) {
280 if (lci
== 4096) return 0;
289 void x25_destroy_socket(struct sock
*);
292 * handler for deferred kills.
294 static void x25_destroy_timer(unsigned long data
)
296 x25_destroy_socket((struct sock
*)data
);
300 * This is called from user mode and the timers. Thus it protects itself against
301 * interrupt users but doesn't worry about being called during work.
302 * Once it is removed from the queue no interrupt or bottom half will
303 * touch it and we are (fairly 8-) ) safe.
305 void x25_destroy_socket(struct sock
*sk
) /* Not static as it's used by the timer */
313 x25_stop_heartbeat(sk
);
316 x25_remove_socket(sk
);
317 x25_clear_queues(sk
); /* Flush the queues */
319 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
320 if (skb
->sk
!= sk
) { /* A pending connection */
321 skb
->sk
->dead
= 1; /* Queue the unaccepted socket for death */
322 x25_start_heartbeat(skb
->sk
);
323 skb
->sk
->protinfo
.x25
->state
= X25_STATE_0
;
329 if (atomic_read(&sk
->wmem_alloc
) != 0 || atomic_read(&sk
->rmem_alloc
) != 0) {
330 /* Defer: outstanding buffers */
331 init_timer(&sk
->timer
);
332 sk
->timer
.expires
= jiffies
+ 10 * HZ
;
333 sk
->timer
.function
= x25_destroy_timer
;
334 sk
->timer
.data
= (unsigned long)sk
;
335 add_timer(&sk
->timer
);
341 restore_flags(flags
);
345 * Handling for system calls applied via the various interfaces to a
346 * X.25 socket object.
349 static int x25_setsockopt(struct socket
*sock
, int level
, int optname
,
350 char *optval
, int optlen
)
352 struct sock
*sk
= sock
->sk
;
355 if (level
!= SOL_X25
)
358 if (optlen
< sizeof(int))
361 if (get_user(opt
, (int *)optval
))
366 sk
->protinfo
.x25
->qbitincl
= opt
? 1 : 0;
374 static int x25_getsockopt(struct socket
*sock
, int level
, int optname
,
375 char *optval
, int *optlen
)
377 struct sock
*sk
= sock
->sk
;
381 if (level
!= SOL_X25
)
384 if (get_user(len
, optlen
))
389 val
= sk
->protinfo
.x25
->qbitincl
;
396 len
= min(len
, sizeof(int));
398 if (put_user(len
, optlen
))
401 if (copy_to_user(optval
, &val
, len
))
407 static int x25_listen(struct socket
*sock
, int backlog
)
409 struct sock
*sk
= sock
->sk
;
411 if (sk
->state
!= TCP_LISTEN
) {
412 memset(&sk
->protinfo
.x25
->dest_addr
, '\0', X25_ADDR_LEN
);
413 sk
->max_ack_backlog
= backlog
;
414 sk
->state
= TCP_LISTEN
;
421 static struct sock
*x25_alloc_socket(void)
426 if ((sk
= sk_alloc(AF_X25
, GFP_ATOMIC
, 1)) == NULL
)
429 if ((x25
= kmalloc(sizeof(*x25
), GFP_ATOMIC
)) == NULL
) {
434 memset(x25
, 0x00, sizeof(*x25
));
437 sk
->protinfo
.x25
= x25
;
441 sock_init_data(NULL
, sk
);
443 skb_queue_head_init(&x25
->ack_queue
);
444 skb_queue_head_init(&x25
->fragment_queue
);
445 skb_queue_head_init(&x25
->interrupt_in_queue
);
446 skb_queue_head_init(&x25
->interrupt_out_queue
);
451 static int x25_create(struct socket
*sock
, int protocol
)
456 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
457 return -ESOCKTNOSUPPORT
;
459 if ((sk
= x25_alloc_socket()) == NULL
)
462 x25
= sk
->protinfo
.x25
;
464 sock_init_data(sock
, sk
);
466 init_timer(&x25
->timer
);
468 sock
->ops
= &x25_proto_ops
;
469 sk
->protocol
= protocol
;
471 x25
->t21
= sysctl_x25_call_request_timeout
;
472 x25
->t22
= sysctl_x25_reset_request_timeout
;
473 x25
->t23
= sysctl_x25_clear_request_timeout
;
474 x25
->t2
= sysctl_x25_ack_holdback_timeout
;
475 x25
->state
= X25_STATE_0
;
477 x25
->facilities
.winsize_in
= X25_DEFAULT_WINDOW_SIZE
;
478 x25
->facilities
.winsize_out
= X25_DEFAULT_WINDOW_SIZE
;
479 x25
->facilities
.pacsize_in
= X25_DEFAULT_PACKET_SIZE
;
480 x25
->facilities
.pacsize_out
= X25_DEFAULT_PACKET_SIZE
;
481 x25
->facilities
.throughput
= X25_DEFAULT_THROUGHPUT
;
482 x25
->facilities
.reverse
= X25_DEFAULT_REVERSE
;
487 static struct sock
*x25_make_new(struct sock
*osk
)
492 if (osk
->type
!= SOCK_SEQPACKET
)
495 if ((sk
= x25_alloc_socket()) == NULL
)
498 x25
= sk
->protinfo
.x25
;
500 sk
->type
= osk
->type
;
501 sk
->socket
= osk
->socket
;
502 sk
->priority
= osk
->priority
;
503 sk
->protocol
= osk
->protocol
;
504 sk
->rcvbuf
= osk
->rcvbuf
;
505 sk
->sndbuf
= osk
->sndbuf
;
506 sk
->debug
= osk
->debug
;
507 sk
->state
= TCP_ESTABLISHED
;
508 sk
->sleep
= osk
->sleep
;
509 sk
->zapped
= osk
->zapped
;
511 x25
->t21
= osk
->protinfo
.x25
->t21
;
512 x25
->t22
= osk
->protinfo
.x25
->t22
;
513 x25
->t23
= osk
->protinfo
.x25
->t23
;
514 x25
->t2
= osk
->protinfo
.x25
->t2
;
516 x25
->facilities
= osk
->protinfo
.x25
->facilities
;
518 x25
->qbitincl
= osk
->protinfo
.x25
->qbitincl
;
520 init_timer(&x25
->timer
);
525 static int x25_release(struct socket
*sock
)
527 struct sock
*sk
= sock
->sk
;
529 if (sk
== NULL
) return 0;
531 switch (sk
->protinfo
.x25
->state
) {
535 x25_disconnect(sk
, 0, 0, 0);
536 x25_destroy_socket(sk
);
542 x25_clear_queues(sk
);
543 x25_write_internal(sk
, X25_CLEAR_REQUEST
);
544 x25_start_t23timer(sk
);
545 sk
->protinfo
.x25
->state
= X25_STATE_2
;
546 sk
->state
= TCP_CLOSE
;
547 sk
->shutdown
|= SEND_SHUTDOWN
;
548 sk
->state_change(sk
);
558 sk
->socket
= NULL
; /* Not used, but we should do this */
563 static int x25_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
565 struct sock
*sk
= sock
->sk
;
566 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
571 if (addr_len
!= sizeof(struct sockaddr_x25
))
574 if (addr
->sx25_family
!= AF_X25
)
577 sk
->protinfo
.x25
->source_addr
= addr
->sx25_addr
;
579 x25_insert_socket(sk
);
583 SOCK_DEBUG(sk
, "x25_bind: socket is bound\n");
588 static int x25_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
590 struct sock
*sk
= sock
->sk
;
591 struct sockaddr_x25
*addr
= (struct sockaddr_x25
*)uaddr
;
592 struct net_device
*dev
;
594 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
595 sock
->state
= SS_CONNECTED
;
596 return 0; /* Connect completed during a ERESTARTSYS event */
599 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
600 sock
->state
= SS_UNCONNECTED
;
601 return -ECONNREFUSED
;
604 if (sk
->state
== TCP_ESTABLISHED
)
605 return -EISCONN
; /* No reconnect on a seqpacket socket */
607 sk
->state
= TCP_CLOSE
;
608 sock
->state
= SS_UNCONNECTED
;
610 if (addr_len
!= sizeof(struct sockaddr_x25
))
613 if (addr
->sx25_family
!= AF_X25
)
616 if ((dev
= x25_get_route(&addr
->sx25_addr
)) == NULL
)
619 if ((sk
->protinfo
.x25
->neighbour
= x25_get_neigh(dev
)) == NULL
)
622 x25_limit_facilities(&sk
->protinfo
.x25
->facilities
,
623 sk
->protinfo
.x25
->neighbour
);
625 if ((sk
->protinfo
.x25
->lci
= x25_new_lci(sk
->protinfo
.x25
->neighbour
)) == 0)
628 if (sk
->zapped
) /* Must bind first - autobinding does not work */
631 if (strcmp(sk
->protinfo
.x25
->source_addr
.x25_addr
, null_x25_address
.x25_addr
) == 0)
632 memset(&sk
->protinfo
.x25
->source_addr
, '\0', X25_ADDR_LEN
);
634 sk
->protinfo
.x25
->dest_addr
= addr
->sx25_addr
;
636 /* Move to connecting socket, start sending Connect Requests */
637 sock
->state
= SS_CONNECTING
;
638 sk
->state
= TCP_SYN_SENT
;
640 sk
->protinfo
.x25
->state
= X25_STATE_1
;
642 x25_write_internal(sk
, X25_CALL_REQUEST
);
644 x25_start_heartbeat(sk
);
645 x25_start_t21timer(sk
);
648 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
651 cli(); /* To avoid races on the sleep */
654 * A Clear Request or timeout or failed routing will go to closed.
656 while (sk
->state
== TCP_SYN_SENT
) {
657 interruptible_sleep_on(sk
->sleep
);
658 if (signal_pending(current
)) {
664 if (sk
->state
!= TCP_ESTABLISHED
) {
666 sock
->state
= SS_UNCONNECTED
;
667 return sock_error(sk
); /* Always set at this point */
670 sock
->state
= SS_CONNECTED
;
677 static int x25_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
683 if ((sk
= sock
->sk
) == NULL
)
686 if (sk
->type
!= SOCK_SEQPACKET
)
689 if (sk
->state
!= TCP_LISTEN
)
693 * The write queue this time is holding sockets ready to use
694 * hooked into the CALL INDICATION we saved
698 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
699 if (flags
& O_NONBLOCK
) {
703 interruptible_sleep_on(sk
->sleep
);
704 if (signal_pending(current
)) {
709 } while (skb
== NULL
);
713 newsk
->socket
= newsock
;
714 newsk
->sleep
= &newsock
->wait
;
717 /* Now attach up the new socket */
726 static int x25_getname(struct socket
*sock
, struct sockaddr
*uaddr
, int *uaddr_len
, int peer
)
728 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)uaddr
;
729 struct sock
*sk
= sock
->sk
;
732 if (sk
->state
!= TCP_ESTABLISHED
)
734 sx25
->sx25_addr
= sk
->protinfo
.x25
->dest_addr
;
736 sx25
->sx25_addr
= sk
->protinfo
.x25
->source_addr
;
739 sx25
->sx25_family
= AF_X25
;
740 *uaddr_len
= sizeof(struct sockaddr_x25
);
745 int x25_rx_call_request(struct sk_buff
*skb
, struct x25_neigh
*neigh
, unsigned int lci
)
749 x25_address source_addr
, dest_addr
;
750 struct x25_facilities facilities
;
754 * Remove the LCI and frame type.
756 skb_pull(skb
, X25_STD_MIN_LEN
);
759 * Extract the X.25 addresses and convert them to ASCII strings,
762 skb_pull(skb
, x25_addr_ntoa(skb
->data
, &source_addr
, &dest_addr
));
765 * Find a listener for the particular address.
767 sk
= x25_find_listener(&source_addr
);
770 * We can't accept the Call Request.
772 if (sk
== NULL
|| sk
->ack_backlog
== sk
->max_ack_backlog
) {
773 x25_transmit_clear_request(neigh
, lci
, 0x01);
778 * Try to reach a compromise on the requested facilities.
780 if ((len
= x25_negotiate_facilities(skb
, sk
, &facilities
)) == -1) {
781 x25_transmit_clear_request(neigh
, lci
, 0x01);
786 * current neighbour/link might impose additional limits
787 * on certain facilties
790 x25_limit_facilities(&facilities
,neigh
);
793 * Try to create a new socket.
795 if ((make
= x25_make_new(sk
)) == NULL
) {
796 x25_transmit_clear_request(neigh
, lci
, 0x01);
801 * Remove the facilities, leaving any Call User Data.
806 make
->state
= TCP_ESTABLISHED
;
808 make
->protinfo
.x25
->lci
= lci
;
809 make
->protinfo
.x25
->dest_addr
= dest_addr
;
810 make
->protinfo
.x25
->source_addr
= source_addr
;
811 make
->protinfo
.x25
->neighbour
= neigh
;
812 make
->protinfo
.x25
->facilities
= facilities
;
814 x25_write_internal(make
, X25_CALL_ACCEPTED
);
817 * Incoming Call User Data.
820 memcpy(make
->protinfo
.x25
->calluserdata
.cuddata
, skb
->data
, skb
->len
);
821 make
->protinfo
.x25
->calluserdata
.cudlength
= skb
->len
;
824 make
->protinfo
.x25
->state
= X25_STATE_3
;
829 x25_insert_socket(make
);
831 skb_queue_head(&sk
->receive_queue
, skb
);
833 x25_start_heartbeat(make
);
836 sk
->data_ready(sk
, skb
->len
);
841 static int x25_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
, struct scm_cookie
*scm
)
843 struct sock
*sk
= sock
->sk
;
844 struct sockaddr_x25
*usx25
= (struct sockaddr_x25
*)msg
->msg_name
;
846 struct sockaddr_x25 sx25
;
848 unsigned char *asmptr
;
851 if (msg
->msg_flags
& ~(MSG_DONTWAIT
| MSG_OOB
))
855 return -EADDRNOTAVAIL
;
857 if (sk
->shutdown
& SEND_SHUTDOWN
) {
858 send_sig(SIGPIPE
, current
, 0);
862 if (sk
->protinfo
.x25
->neighbour
== NULL
)
866 if (msg
->msg_namelen
< sizeof(sx25
))
869 if (strcmp(sk
->protinfo
.x25
->dest_addr
.x25_addr
, sx25
.sx25_addr
.x25_addr
) != 0)
871 if (sx25
.sx25_family
!= AF_X25
)
875 * FIXME 1003.1g - if the socket is like this because
876 * it has become closed (not started closed) we ought
879 if (sk
->state
!= TCP_ESTABLISHED
)
882 sx25
.sx25_family
= AF_X25
;
883 sx25
.sx25_addr
= sk
->protinfo
.x25
->dest_addr
;
886 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: Addresses built.\n");
889 SOCK_DEBUG(sk
, "x25_sendmsg: sendto: building packet.\n");
891 if ((msg
->msg_flags
& MSG_OOB
) && len
> 32)
894 size
= len
+ X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
;
896 if ((skb
= sock_alloc_send_skb(sk
, size
, 0, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
899 skb_reserve(skb
, X25_MAX_L2_LEN
+ X25_EXT_MIN_LEN
);
902 * Put the data on the end
904 SOCK_DEBUG(sk
, "x25_sendmsg: Copying user data\n");
906 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
908 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
911 * If the Q BIT Include socket option is in force, the first
912 * byte of the user data is the logical value of the Q Bit.
914 if (sk
->protinfo
.x25
->qbitincl
) {
920 * Push down the X.25 header
922 SOCK_DEBUG(sk
, "x25_sendmsg: Building X.25 Header.\n");
924 if (msg
->msg_flags
& MSG_OOB
) {
925 if (sk
->protinfo
.x25
->neighbour
->extended
) {
926 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
927 *asmptr
++ = ((sk
->protinfo
.x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
928 *asmptr
++ = (sk
->protinfo
.x25
->lci
>> 0) & 0xFF;
929 *asmptr
++ = X25_INTERRUPT
;
931 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
932 *asmptr
++ = ((sk
->protinfo
.x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
933 *asmptr
++ = (sk
->protinfo
.x25
->lci
>> 0) & 0xFF;
934 *asmptr
++ = X25_INTERRUPT
;
937 if (sk
->protinfo
.x25
->neighbour
->extended
) {
938 /* Build an Extended X.25 header */
939 asmptr
= skb_push(skb
, X25_EXT_MIN_LEN
);
940 *asmptr
++ = ((sk
->protinfo
.x25
->lci
>> 8) & 0x0F) | X25_GFI_EXTSEQ
;
941 *asmptr
++ = (sk
->protinfo
.x25
->lci
>> 0) & 0xFF;
942 *asmptr
++ = X25_DATA
;
943 *asmptr
++ = X25_DATA
;
945 /* Build an Standard X.25 header */
946 asmptr
= skb_push(skb
, X25_STD_MIN_LEN
);
947 *asmptr
++ = ((sk
->protinfo
.x25
->lci
>> 8) & 0x0F) | X25_GFI_STDSEQ
;
948 *asmptr
++ = (sk
->protinfo
.x25
->lci
>> 0) & 0xFF;
949 *asmptr
++ = X25_DATA
;
953 skb
->data
[0] |= X25_Q_BIT
;
956 SOCK_DEBUG(sk
, "x25_sendmsg: Built header.\n");
957 SOCK_DEBUG(sk
, "x25_sendmsg: Transmitting buffer\n");
959 if (sk
->state
!= TCP_ESTABLISHED
) {
964 if (msg
->msg_flags
& MSG_OOB
) {
965 skb_queue_tail(&sk
->protinfo
.x25
->interrupt_out_queue
, skb
);
976 static int x25_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
, int flags
, struct scm_cookie
*scm
)
978 struct sock
*sk
= sock
->sk
;
979 struct sockaddr_x25
*sx25
= (struct sockaddr_x25
*)msg
->msg_name
;
982 unsigned char *asmptr
;
986 * This works for seqpacket too. The receiver has ordered the queue for
987 * us! We do one quick check first though
989 if (sk
->state
!= TCP_ESTABLISHED
)
992 if (flags
& MSG_OOB
) {
993 if (sk
->urginline
|| skb_peek(&sk
->protinfo
.x25
->interrupt_in_queue
) == NULL
)
996 skb
= skb_dequeue(&sk
->protinfo
.x25
->interrupt_in_queue
);
998 skb_pull(skb
, X25_STD_MIN_LEN
);
1001 * No Q bit information on Interrupt data.
1003 if (sk
->protinfo
.x25
->qbitincl
) {
1004 asmptr
= skb_push(skb
, 1);
1008 msg
->msg_flags
|= MSG_OOB
;
1010 /* Now we can treat all alike */
1011 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1014 qbit
= (skb
->data
[0] & X25_Q_BIT
) == X25_Q_BIT
;
1016 skb_pull(skb
, (sk
->protinfo
.x25
->neighbour
->extended
) ? X25_EXT_MIN_LEN
: X25_STD_MIN_LEN
);
1018 if (sk
->protinfo
.x25
->qbitincl
) {
1019 asmptr
= skb_push(skb
, 1);
1024 skb
->h
.raw
= skb
->data
;
1028 if (copied
> size
) {
1030 msg
->msg_flags
|= MSG_TRUNC
;
1033 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1036 sx25
->sx25_family
= AF_X25
;
1037 sx25
->sx25_addr
= sk
->protinfo
.x25
->dest_addr
;
1040 msg
->msg_namelen
= sizeof(struct sockaddr_x25
);
1042 skb_free_datagram(sk
, skb
);
1048 static int x25_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1050 struct sock
*sk
= sock
->sk
;
1055 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1058 if (put_user(amount
, (unsigned int *)arg
))
1064 struct sk_buff
*skb
;
1066 /* These two are safe on a single CPU system as only user tasks fiddle here */
1067 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1069 if (put_user(amount
, (unsigned int *)arg
))
1076 if (sk
->stamp
.tv_sec
== 0)
1078 if (copy_to_user((void *)arg
, &sk
->stamp
, sizeof(struct timeval
)))
1086 case SIOCGIFDSTADDR
:
1087 case SIOCSIFDSTADDR
:
1088 case SIOCGIFBRDADDR
:
1089 case SIOCSIFBRDADDR
:
1090 case SIOCGIFNETMASK
:
1091 case SIOCSIFNETMASK
:
1098 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1099 return x25_route_ioctl(cmd
, (void *)arg
);
1101 case SIOCX25GSUBSCRIP
:
1102 return x25_subscr_ioctl(cmd
, (void *)arg
);
1104 case SIOCX25SSUBSCRIP
:
1105 if (!suser()) return -EPERM
;
1106 return x25_subscr_ioctl(cmd
, (void *)arg
);
1108 case SIOCX25GFACILITIES
: {
1109 struct x25_facilities facilities
;
1110 facilities
= sk
->protinfo
.x25
->facilities
;
1111 if (copy_to_user((void *)arg
, &facilities
, sizeof(facilities
)))
1116 case SIOCX25SFACILITIES
: {
1117 struct x25_facilities facilities
;
1118 if (copy_from_user(&facilities
, (void *)arg
, sizeof(facilities
)))
1120 if (sk
->state
!= TCP_LISTEN
&& sk
->state
!= TCP_CLOSE
)
1122 if (facilities
.pacsize_in
< X25_PS16
|| facilities
.pacsize_in
> X25_PS4096
)
1124 if (facilities
.pacsize_out
< X25_PS16
|| facilities
.pacsize_out
> X25_PS4096
)
1126 if (facilities
.winsize_in
< 1 || facilities
.winsize_in
> 127)
1128 if (facilities
.throughput
< 0x03 || facilities
.throughput
> 0x2C)
1130 if (facilities
.reverse
!= 0 && facilities
.reverse
!= 1)
1132 sk
->protinfo
.x25
->facilities
= facilities
;
1136 case SIOCX25GCALLUSERDATA
: {
1137 struct x25_calluserdata calluserdata
;
1138 calluserdata
= sk
->protinfo
.x25
->calluserdata
;
1139 if (copy_to_user((void *)arg
, &calluserdata
, sizeof(calluserdata
)))
1144 case SIOCX25SCALLUSERDATA
: {
1145 struct x25_calluserdata calluserdata
;
1146 if (copy_from_user(&calluserdata
, (void *)arg
, sizeof(calluserdata
)))
1148 if (calluserdata
.cudlength
> X25_MAX_CUD_LEN
)
1150 sk
->protinfo
.x25
->calluserdata
= calluserdata
;
1154 case SIOCX25GCAUSEDIAG
: {
1155 struct x25_causediag causediag
;
1156 causediag
= sk
->protinfo
.x25
->causediag
;
1157 if (copy_to_user((void *)arg
, &causediag
, sizeof(causediag
)))
1163 return dev_ioctl(cmd
, (void *)arg
);
1170 static int x25_get_info(char *buffer
, char **start
, off_t offset
, int length
, int dummy
)
1173 struct net_device
*dev
;
1174 const char *devname
;
1181 len
+= sprintf(buffer
, "dest_addr src_addr dev lci st vs vr va t t2 t21 t22 t23 Snd-Q Rcv-Q inode\n");
1183 for (s
= x25_list
; s
!= NULL
; s
= s
->next
) {
1184 if (s
->protinfo
.x25
->neighbour
== NULL
|| (dev
= s
->protinfo
.x25
->neighbour
->dev
) == NULL
)
1187 devname
= s
->protinfo
.x25
->neighbour
->dev
->name
;
1189 len
+= sprintf(buffer
+ len
, "%-10s %-10s %-5s %3.3X %d %d %d %d %3lu %3lu %3lu %3lu %3lu %5d %5d %ld\n",
1190 (s
->protinfo
.x25
->dest_addr
.x25_addr
[0] == '\0') ? "*" : s
->protinfo
.x25
->dest_addr
.x25_addr
,
1191 (s
->protinfo
.x25
->source_addr
.x25_addr
[0] == '\0') ? "*" : s
->protinfo
.x25
->source_addr
.x25_addr
,
1193 s
->protinfo
.x25
->lci
& 0x0FFF,
1194 s
->protinfo
.x25
->state
,
1195 s
->protinfo
.x25
->vs
,
1196 s
->protinfo
.x25
->vr
,
1197 s
->protinfo
.x25
->va
,
1198 x25_display_timer(s
) / HZ
,
1199 s
->protinfo
.x25
->t2
/ HZ
,
1200 s
->protinfo
.x25
->t21
/ HZ
,
1201 s
->protinfo
.x25
->t22
/ HZ
,
1202 s
->protinfo
.x25
->t23
/ HZ
,
1203 atomic_read(&s
->wmem_alloc
),
1204 atomic_read(&s
->rmem_alloc
),
1205 s
->socket
!= NULL
? s
->socket
->inode
->i_ino
: 0L);
1214 if (pos
> offset
+ length
)
1220 *start
= buffer
+ (offset
- begin
);
1221 len
-= (offset
- begin
);
1223 if (len
> length
) len
= length
;
1228 struct net_proto_family x25_family_ops
= {
1233 static struct proto_ops
SOCKOPS_WRAPPED(x25_proto_ops
) = {
1254 #include <linux/smp_lock.h>
1255 SOCKOPS_WRAP(x25_proto
, AF_X25
);
1258 static struct packet_type x25_packet_type
=
1260 0, /* MUTTER ntohs(ETH_P_X25),*/
1262 x25_lapb_receive_frame
,
1267 struct notifier_block x25_dev_notifier
= {
1272 void x25_kill_by_neigh(struct x25_neigh
*neigh
)
1276 for( s
=x25_list
; s
!= NULL
; s
=s
->next
){
1277 if( s
->protinfo
.x25
->neighbour
== neigh
)
1278 x25_disconnect(s
, ENETUNREACH
, 0, 0);
1282 #ifdef CONFIG_PROC_FS
1283 static struct proc_dir_entry proc_net_x25
= {
1284 PROC_NET_X25
, 3, "x25",
1285 S_IFREG
| S_IRUGO
, 1, 0, 0,
1286 0, &proc_net_inode_operations
,
1289 static struct proc_dir_entry proc_net_x25_routes
= {
1290 PROC_NET_X25_ROUTES
, 10, "x25_routes",
1291 S_IFREG
| S_IRUGO
, 1, 0, 0,
1292 0, &proc_net_inode_operations
,
1297 void __init
x25_proto_init(struct net_proto
*pro
)
1299 sock_register(&x25_family_ops
);
1301 x25_packet_type
.type
= htons(ETH_P_X25
);
1302 dev_add_pack(&x25_packet_type
);
1304 register_netdevice_notifier(&x25_dev_notifier
);
1306 printk(KERN_INFO
"X.25 for Linux. Version 0.2 for Linux 2.1.15\n");
1308 #ifdef CONFIG_SYSCTL
1309 x25_register_sysctl();
1312 #ifdef CONFIG_PROC_FS
1313 proc_net_register(&proc_net_x25
);
1314 proc_net_register(&proc_net_x25_routes
);
1321 MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
1322 MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
1324 int init_module(void)
1326 struct net_device
*dev
;
1328 x25_proto_init(NULL
);
1331 * Register any pre existing devices.
1333 read_lock(&dev_base_lock
);
1334 for (dev
= dev_base
; dev
!= NULL
; dev
= dev
->next
) {
1335 if ((dev
->flags
& IFF_UP
) && (dev
->type
== ARPHRD_X25
1336 #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
1337 || dev
->type
== ARPHRD_ETHER
1340 x25_link_device_up(dev
);
1342 read_unlock(&dev_base_lock
);
1347 void cleanup_module(void)
1350 #ifdef CONFIG_PROC_FS
1351 proc_net_unregister(PROC_NET_X25
);
1352 proc_net_unregister(PROC_NET_X25_ROUTES
);
1358 #ifdef CONFIG_SYSCTL
1359 x25_unregister_sysctl();
1362 unregister_netdevice_notifier(&x25_dev_notifier
);
1364 dev_remove_pack(&x25_packet_type
);
1366 sock_unregister(AF_X25
);