2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
24 #include <linux/stat.h>
26 #include <linux/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/skbuff.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
34 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
36 #include <linux/interrupt.h>
37 #include <linux/notifier.h>
38 #include <net/netrom.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
42 #include <net/tcp_states.h>
44 #include <linux/init.h>
46 static int nr_ndevs
= 4;
48 int sysctl_netrom_default_path_quality
= NR_DEFAULT_QUAL
;
49 int sysctl_netrom_obsolescence_count_initialiser
= NR_DEFAULT_OBS
;
50 int sysctl_netrom_network_ttl_initialiser
= NR_DEFAULT_TTL
;
51 int sysctl_netrom_transport_timeout
= NR_DEFAULT_T1
;
52 int sysctl_netrom_transport_maximum_tries
= NR_DEFAULT_N2
;
53 int sysctl_netrom_transport_acknowledge_delay
= NR_DEFAULT_T2
;
54 int sysctl_netrom_transport_busy_delay
= NR_DEFAULT_T4
;
55 int sysctl_netrom_transport_requested_window_size
= NR_DEFAULT_WINDOW
;
56 int sysctl_netrom_transport_no_activity_timeout
= NR_DEFAULT_IDLE
;
57 int sysctl_netrom_routing_control
= NR_DEFAULT_ROUTING
;
58 int sysctl_netrom_link_fails_count
= NR_DEFAULT_FAILS
;
60 static unsigned short circuit
= 0x101;
62 static HLIST_HEAD(nr_list
);
63 static DEFINE_SPINLOCK(nr_list_lock
);
65 static struct proto_ops nr_proto_ops
;
68 * Socket removal during an interrupt is now safe.
70 static void nr_remove_socket(struct sock
*sk
)
72 spin_lock_bh(&nr_list_lock
);
74 spin_unlock_bh(&nr_list_lock
);
78 * Kill all bound sockets on a dropped device.
80 static void nr_kill_by_device(struct net_device
*dev
)
83 struct hlist_node
*node
;
85 spin_lock_bh(&nr_list_lock
);
86 sk_for_each(s
, node
, &nr_list
)
87 if (nr_sk(s
)->device
== dev
)
88 nr_disconnect(s
, ENETUNREACH
);
89 spin_unlock_bh(&nr_list_lock
);
93 * Handle device status changes.
95 static int nr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
97 struct net_device
*dev
= (struct net_device
*)ptr
;
99 if (event
!= NETDEV_DOWN
)
102 nr_kill_by_device(dev
);
103 nr_rt_device_down(dev
);
109 * Add a socket to the bound sockets list.
111 static void nr_insert_socket(struct sock
*sk
)
113 spin_lock_bh(&nr_list_lock
);
114 sk_add_node(sk
, &nr_list
);
115 spin_unlock_bh(&nr_list_lock
);
119 * Find a socket that wants to accept the Connect Request we just
122 static struct sock
*nr_find_listener(ax25_address
*addr
)
125 struct hlist_node
*node
;
127 spin_lock_bh(&nr_list_lock
);
128 sk_for_each(s
, node
, &nr_list
)
129 if (!ax25cmp(&nr_sk(s
)->source_addr
, addr
) &&
130 s
->sk_state
== TCP_LISTEN
) {
136 spin_unlock_bh(&nr_list_lock
);
141 * Find a connected NET/ROM socket given my circuit IDs.
143 static struct sock
*nr_find_socket(unsigned char index
, unsigned char id
)
146 struct hlist_node
*node
;
148 spin_lock_bh(&nr_list_lock
);
149 sk_for_each(s
, node
, &nr_list
) {
150 struct nr_sock
*nr
= nr_sk(s
);
152 if (nr
->my_index
== index
&& nr
->my_id
== id
) {
159 spin_unlock_bh(&nr_list_lock
);
164 * Find a connected NET/ROM socket given their circuit IDs.
166 static struct sock
*nr_find_peer(unsigned char index
, unsigned char id
,
170 struct hlist_node
*node
;
172 spin_lock_bh(&nr_list_lock
);
173 sk_for_each(s
, node
, &nr_list
) {
174 struct nr_sock
*nr
= nr_sk(s
);
176 if (nr
->your_index
== index
&& nr
->your_id
== id
&&
177 !ax25cmp(&nr
->dest_addr
, dest
)) {
184 spin_unlock_bh(&nr_list_lock
);
189 * Find next free circuit ID.
191 static unsigned short nr_find_next_circuit(void)
193 unsigned short id
= circuit
;
201 if (i
!= 0 && j
!= 0) {
202 if ((sk
=nr_find_socket(i
, j
)) == NULL
)
216 void nr_destroy_socket(struct sock
*);
219 * Handler for deferred kills.
221 static void nr_destroy_timer(unsigned long data
)
223 struct sock
*sk
=(struct sock
*)data
;
226 nr_destroy_socket(sk
);
232 * This is called from user mode and the timers. Thus it protects itself
233 * against interrupt users but doesn't worry about being called during
234 * work. Once it is removed from the queue no interrupt or bottom half
235 * will touch it and we are (fairly 8-) ) safe.
237 void nr_destroy_socket(struct sock
*sk
)
241 nr_remove_socket(sk
);
243 nr_stop_heartbeat(sk
);
247 nr_stop_idletimer(sk
);
249 nr_clear_queues(sk
); /* Flush the queues */
251 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
252 if (skb
->sk
!= sk
) { /* A pending connection */
253 /* Queue the unaccepted socket for death */
254 sock_set_flag(skb
->sk
, SOCK_DEAD
);
255 nr_start_heartbeat(skb
->sk
);
256 nr_sk(skb
->sk
)->state
= NR_STATE_0
;
262 if (atomic_read(&sk
->sk_wmem_alloc
) ||
263 atomic_read(&sk
->sk_rmem_alloc
)) {
264 /* Defer: outstanding buffers */
265 sk
->sk_timer
.function
= nr_destroy_timer
;
266 sk
->sk_timer
.expires
= jiffies
+ 2 * HZ
;
267 add_timer(&sk
->sk_timer
);
273 * Handling for system calls applied via the various interfaces to a
274 * NET/ROM socket object.
277 static int nr_setsockopt(struct socket
*sock
, int level
, int optname
,
278 char __user
*optval
, int optlen
)
280 struct sock
*sk
= sock
->sk
;
281 struct nr_sock
*nr
= nr_sk(sk
);
284 if (level
!= SOL_NETROM
)
287 if (optlen
< sizeof(int))
290 if (get_user(opt
, (int __user
*)optval
))
307 if (opt
< 1 || opt
> 31)
321 nr
->idle
= opt
* 60 * HZ
;
329 static int nr_getsockopt(struct socket
*sock
, int level
, int optname
,
330 char __user
*optval
, int __user
*optlen
)
332 struct sock
*sk
= sock
->sk
;
333 struct nr_sock
*nr
= nr_sk(sk
);
337 if (level
!= SOL_NETROM
)
340 if (get_user(len
, optlen
))
364 val
= nr
->idle
/ (60 * HZ
);
371 len
= min_t(unsigned int, len
, sizeof(int));
373 if (put_user(len
, optlen
))
376 return copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
379 static int nr_listen(struct socket
*sock
, int backlog
)
381 struct sock
*sk
= sock
->sk
;
384 if (sk
->sk_state
!= TCP_LISTEN
) {
385 memset(&nr_sk(sk
)->user_addr
, 0, AX25_ADDR_LEN
);
386 sk
->sk_max_ack_backlog
= backlog
;
387 sk
->sk_state
= TCP_LISTEN
;
396 static struct proto nr_proto
= {
398 .owner
= THIS_MODULE
,
399 .obj_size
= sizeof(struct nr_sock
),
402 static int nr_create(struct socket
*sock
, int protocol
)
407 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
408 return -ESOCKTNOSUPPORT
;
410 if ((sk
= sk_alloc(PF_NETROM
, GFP_ATOMIC
, &nr_proto
, 1)) == NULL
)
415 sock_init_data(sock
, sk
);
417 sock
->ops
= &nr_proto_ops
;
418 sk
->sk_protocol
= protocol
;
420 skb_queue_head_init(&nr
->ack_queue
);
421 skb_queue_head_init(&nr
->reseq_queue
);
422 skb_queue_head_init(&nr
->frag_queue
);
426 nr
->t1
= sysctl_netrom_transport_timeout
;
427 nr
->t2
= sysctl_netrom_transport_acknowledge_delay
;
428 nr
->n2
= sysctl_netrom_transport_maximum_tries
;
429 nr
->t4
= sysctl_netrom_transport_busy_delay
;
430 nr
->idle
= sysctl_netrom_transport_no_activity_timeout
;
431 nr
->window
= sysctl_netrom_transport_requested_window_size
;
434 nr
->state
= NR_STATE_0
;
439 static struct sock
*nr_make_new(struct sock
*osk
)
442 struct nr_sock
*nr
, *onr
;
444 if (osk
->sk_type
!= SOCK_SEQPACKET
)
447 if ((sk
= sk_alloc(PF_NETROM
, GFP_ATOMIC
, osk
->sk_prot
, 1)) == NULL
)
452 sock_init_data(NULL
, sk
);
454 sk
->sk_type
= osk
->sk_type
;
455 sk
->sk_socket
= osk
->sk_socket
;
456 sk
->sk_priority
= osk
->sk_priority
;
457 sk
->sk_protocol
= osk
->sk_protocol
;
458 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
459 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
460 sk
->sk_state
= TCP_ESTABLISHED
;
461 sk
->sk_sleep
= osk
->sk_sleep
;
462 sock_copy_flags(sk
, osk
);
464 skb_queue_head_init(&nr
->ack_queue
);
465 skb_queue_head_init(&nr
->reseq_queue
);
466 skb_queue_head_init(&nr
->frag_queue
);
476 nr
->idle
= onr
->idle
;
477 nr
->window
= onr
->window
;
479 nr
->device
= onr
->device
;
480 nr
->bpqext
= onr
->bpqext
;
485 static int nr_release(struct socket
*sock
)
487 struct sock
*sk
= sock
->sk
;
490 if (sk
== NULL
) return 0;
500 nr_disconnect(sk
, 0);
501 nr_destroy_socket(sk
);
507 nr_write_internal(sk
, NR_DISCREQ
);
508 nr_start_t1timer(sk
);
511 nr_stop_idletimer(sk
);
512 nr
->state
= NR_STATE_2
;
513 sk
->sk_state
= TCP_CLOSE
;
514 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
515 sk
->sk_state_change(sk
);
517 sock_set_flag(sk
, SOCK_DESTROY
);
518 sk
->sk_socket
= NULL
;
522 sk
->sk_socket
= NULL
;
533 static int nr_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
535 struct sock
*sk
= sock
->sk
;
536 struct nr_sock
*nr
= nr_sk(sk
);
537 struct full_sockaddr_ax25
*addr
= (struct full_sockaddr_ax25
*)uaddr
;
538 struct net_device
*dev
;
539 ax25_uid_assoc
*user
;
540 ax25_address
*source
;
543 if (!sock_flag(sk
, SOCK_ZAPPED
)) {
547 if (addr_len
< sizeof(struct sockaddr_ax25
) || addr_len
> sizeof(struct full_sockaddr_ax25
)) {
551 if (addr_len
< (addr
->fsa_ax25
.sax25_ndigis
* sizeof(ax25_address
) + sizeof(struct sockaddr_ax25
))) {
555 if (addr
->fsa_ax25
.sax25_family
!= AF_NETROM
) {
559 if ((dev
= nr_dev_get(&addr
->fsa_ax25
.sax25_call
)) == NULL
) {
560 SOCK_DEBUG(sk
, "NET/ROM: bind failed: invalid node callsign\n");
562 return -EADDRNOTAVAIL
;
566 * Only the super user can set an arbitrary user callsign.
568 if (addr
->fsa_ax25
.sax25_ndigis
== 1) {
569 if (!capable(CAP_NET_BIND_SERVICE
)) {
574 nr
->user_addr
= addr
->fsa_digipeater
[0];
575 nr
->source_addr
= addr
->fsa_ax25
.sax25_call
;
577 source
= &addr
->fsa_ax25
.sax25_call
;
579 user
= ax25_findbyuid(current
->euid
);
581 nr
->user_addr
= user
->call
;
584 if (ax25_uid_policy
&& !capable(CAP_NET_BIND_SERVICE
)) {
589 nr
->user_addr
= *source
;
592 nr
->source_addr
= *source
;
596 nr_insert_socket(sk
);
598 sock_reset_flag(sk
, SOCK_ZAPPED
);
601 SOCK_DEBUG(sk
, "NET/ROM: socket is bound\n");
605 static int nr_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
606 int addr_len
, int flags
)
608 struct sock
*sk
= sock
->sk
;
609 struct nr_sock
*nr
= nr_sk(sk
);
610 struct sockaddr_ax25
*addr
= (struct sockaddr_ax25
*)uaddr
;
611 ax25_address
*source
= NULL
;
612 ax25_uid_assoc
*user
;
613 struct net_device
*dev
;
616 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
617 sock
->state
= SS_CONNECTED
;
619 return 0; /* Connect completed during a ERESTARTSYS event */
622 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
623 sock
->state
= SS_UNCONNECTED
;
625 return -ECONNREFUSED
;
628 if (sk
->sk_state
== TCP_ESTABLISHED
) {
630 return -EISCONN
; /* No reconnect on a seqpacket socket */
633 sk
->sk_state
= TCP_CLOSE
;
634 sock
->state
= SS_UNCONNECTED
;
636 if (addr_len
!= sizeof(struct sockaddr_ax25
) && addr_len
!= sizeof(struct full_sockaddr_ax25
)) {
640 if (addr
->sax25_family
!= AF_NETROM
) {
644 if (sock_flag(sk
, SOCK_ZAPPED
)) { /* Must bind first - autobinding in this may or may not work */
645 sock_reset_flag(sk
, SOCK_ZAPPED
);
647 if ((dev
= nr_dev_first()) == NULL
) {
651 source
= (ax25_address
*)dev
->dev_addr
;
653 user
= ax25_findbyuid(current
->euid
);
655 nr
->user_addr
= user
->call
;
658 if (ax25_uid_policy
&& !capable(CAP_NET_ADMIN
)) {
663 nr
->user_addr
= *source
;
666 nr
->source_addr
= *source
;
670 nr_insert_socket(sk
); /* Finish the bind */
673 nr
->dest_addr
= addr
->sax25_call
;
676 circuit
= nr_find_next_circuit();
679 nr
->my_index
= circuit
/ 256;
680 nr
->my_id
= circuit
% 256;
684 /* Move to connecting socket, start sending Connect Requests */
685 sock
->state
= SS_CONNECTING
;
686 sk
->sk_state
= TCP_SYN_SENT
;
688 nr_establish_data_link(sk
);
690 nr
->state
= NR_STATE_1
;
692 nr_start_heartbeat(sk
);
695 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
)) {
701 * A Connect Ack with Choke or timeout or failed routing will go to
704 if (sk
->sk_state
== TCP_SYN_SENT
) {
705 struct task_struct
*tsk
= current
;
706 DECLARE_WAITQUEUE(wait
, tsk
);
708 add_wait_queue(sk
->sk_sleep
, &wait
);
710 set_current_state(TASK_INTERRUPTIBLE
);
711 if (sk
->sk_state
!= TCP_SYN_SENT
)
714 if (!signal_pending(tsk
)) {
719 current
->state
= TASK_RUNNING
;
720 remove_wait_queue(sk
->sk_sleep
, &wait
);
723 current
->state
= TASK_RUNNING
;
724 remove_wait_queue(sk
->sk_sleep
, &wait
);
727 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
728 sock
->state
= SS_UNCONNECTED
;
730 return sock_error(sk
); /* Always set at this point */
733 sock
->state
= SS_CONNECTED
;
739 static int nr_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
741 struct task_struct
*tsk
= current
;
742 DECLARE_WAITQUEUE(wait
, tsk
);
748 if ((sk
= sock
->sk
) == NULL
)
752 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
757 if (sk
->sk_state
!= TCP_LISTEN
) {
763 * The write queue this time is holding sockets ready to use
764 * hooked into the SABM we saved
766 add_wait_queue(sk
->sk_sleep
, &wait
);
768 skb
= skb_dequeue(&sk
->sk_receive_queue
);
772 current
->state
= TASK_INTERRUPTIBLE
;
774 if (flags
& O_NONBLOCK
) {
775 current
->state
= TASK_RUNNING
;
776 remove_wait_queue(sk
->sk_sleep
, &wait
);
779 if (!signal_pending(tsk
)) {
784 current
->state
= TASK_RUNNING
;
785 remove_wait_queue(sk
->sk_sleep
, &wait
);
788 current
->state
= TASK_RUNNING
;
789 remove_wait_queue(sk
->sk_sleep
, &wait
);
792 newsk
->sk_socket
= newsock
;
793 newsk
->sk_sleep
= &newsock
->wait
;
795 /* Now attach up the new socket */
797 sk
->sk_ack_backlog
--;
805 static int nr_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
806 int *uaddr_len
, int peer
)
808 struct full_sockaddr_ax25
*sax
= (struct full_sockaddr_ax25
*)uaddr
;
809 struct sock
*sk
= sock
->sk
;
810 struct nr_sock
*nr
= nr_sk(sk
);
814 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
818 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
819 sax
->fsa_ax25
.sax25_ndigis
= 1;
820 sax
->fsa_ax25
.sax25_call
= nr
->user_addr
;
821 sax
->fsa_digipeater
[0] = nr
->dest_addr
;
822 *uaddr_len
= sizeof(struct full_sockaddr_ax25
);
824 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
825 sax
->fsa_ax25
.sax25_ndigis
= 0;
826 sax
->fsa_ax25
.sax25_call
= nr
->source_addr
;
827 *uaddr_len
= sizeof(struct sockaddr_ax25
);
834 int nr_rx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
838 struct nr_sock
*nr_make
;
839 ax25_address
*src
, *dest
, *user
;
840 unsigned short circuit_index
, circuit_id
;
841 unsigned short peer_circuit_index
, peer_circuit_id
;
842 unsigned short frametype
, flags
, window
, timeout
;
845 skb
->sk
= NULL
; /* Initially we don't know who it's for */
848 * skb->data points to the netrom frame start
851 src
= (ax25_address
*)(skb
->data
+ 0);
852 dest
= (ax25_address
*)(skb
->data
+ 7);
854 circuit_index
= skb
->data
[15];
855 circuit_id
= skb
->data
[16];
856 peer_circuit_index
= skb
->data
[17];
857 peer_circuit_id
= skb
->data
[18];
858 frametype
= skb
->data
[19] & 0x0F;
859 flags
= skb
->data
[19] & 0xF0;
862 * Check for an incoming IP over NET/ROM frame.
864 if (frametype
== NR_PROTOEXT
&&
865 circuit_index
== NR_PROTO_IP
&& circuit_id
== NR_PROTO_IP
) {
866 skb_pull(skb
, NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
);
867 skb
->h
.raw
= skb
->data
;
869 return nr_rx_ip(skb
, dev
);
873 * Find an existing socket connection, based on circuit ID, if it's
874 * a Connect Request base it on their circuit ID.
876 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
877 * circuit that no longer exists at the other end ...
882 if (circuit_index
== 0 && circuit_id
== 0) {
883 if (frametype
== NR_CONNACK
&& flags
== NR_CHOKE_FLAG
)
884 sk
= nr_find_peer(peer_circuit_index
, peer_circuit_id
, src
);
886 if (frametype
== NR_CONNREQ
)
887 sk
= nr_find_peer(circuit_index
, circuit_id
, src
);
889 sk
= nr_find_socket(circuit_index
, circuit_id
);
893 skb
->h
.raw
= skb
->data
;
895 if (frametype
== NR_CONNACK
&& skb
->len
== 22)
896 nr_sk(sk
)->bpqext
= 1;
898 nr_sk(sk
)->bpqext
= 0;
900 ret
= nr_process_rx_frame(sk
, skb
);
906 * Now it should be a CONNREQ.
908 if (frametype
!= NR_CONNREQ
) {
910 * Here it would be nice to be able to send a reset but
911 * NET/ROM doesn't have one. The following hack would
912 * have been a way to extend the protocol but apparently
913 * it kills BPQ boxes... :-(
917 * Never reply to a CONNACK/CHOKE.
919 if (frametype
!= NR_CONNACK
|| flags
!= NR_CHOKE_FLAG
)
920 nr_transmit_refusal(skb
, 1);
925 sk
= nr_find_listener(dest
);
927 user
= (ax25_address
*)(skb
->data
+ 21);
929 if (sk
== NULL
|| sk_acceptq_is_full(sk
) ||
930 (make
= nr_make_new(sk
)) == NULL
) {
931 nr_transmit_refusal(skb
, 0);
937 window
= skb
->data
[20];
940 make
->sk_state
= TCP_ESTABLISHED
;
942 /* Fill in his circuit details */
943 nr_make
= nr_sk(make
);
944 nr_make
->source_addr
= *dest
;
945 nr_make
->dest_addr
= *src
;
946 nr_make
->user_addr
= *user
;
948 nr_make
->your_index
= circuit_index
;
949 nr_make
->your_id
= circuit_id
;
952 circuit
= nr_find_next_circuit();
955 nr_make
->my_index
= circuit
/ 256;
956 nr_make
->my_id
= circuit
% 256;
960 /* Window negotiation */
961 if (window
< nr_make
->window
)
962 nr_make
->window
= window
;
964 /* L4 timeout negotiation */
965 if (skb
->len
== 37) {
966 timeout
= skb
->data
[36] * 256 + skb
->data
[35];
967 if (timeout
* HZ
< nr_make
->t1
)
968 nr_make
->t1
= timeout
* HZ
;
974 nr_write_internal(make
, NR_CONNACK
);
976 nr_make
->condition
= 0x00;
981 nr_make
->state
= NR_STATE_3
;
982 sk
->sk_ack_backlog
++;
984 nr_insert_socket(make
);
986 skb_queue_head(&sk
->sk_receive_queue
, skb
);
988 nr_start_heartbeat(make
);
989 nr_start_idletimer(make
);
991 if (!sock_flag(sk
, SOCK_DEAD
))
992 sk
->sk_data_ready(sk
, skb
->len
);
998 static int nr_sendmsg(struct kiocb
*iocb
, struct socket
*sock
,
999 struct msghdr
*msg
, size_t len
)
1001 struct sock
*sk
= sock
->sk
;
1002 struct nr_sock
*nr
= nr_sk(sk
);
1003 struct sockaddr_ax25
*usax
= (struct sockaddr_ax25
*)msg
->msg_name
;
1005 struct sockaddr_ax25 sax
;
1006 struct sk_buff
*skb
;
1007 unsigned char *asmptr
;
1010 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_CMSG_COMPAT
))
1014 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1015 err
= -EADDRNOTAVAIL
;
1019 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1020 send_sig(SIGPIPE
, current
, 0);
1025 if (nr
->device
== NULL
) {
1031 if (msg
->msg_namelen
< sizeof(sax
)) {
1036 if (ax25cmp(&nr
->dest_addr
, &sax
.sax25_call
) != 0) {
1040 if (sax
.sax25_family
!= AF_NETROM
) {
1045 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1049 sax
.sax25_family
= AF_NETROM
;
1050 sax
.sax25_call
= nr
->dest_addr
;
1053 SOCK_DEBUG(sk
, "NET/ROM: sendto: Addresses built.\n");
1055 /* Build a packet */
1056 SOCK_DEBUG(sk
, "NET/ROM: sendto: building packet.\n");
1057 size
= len
+ NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
;
1059 if ((skb
= sock_alloc_send_skb(sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1062 skb_reserve(skb
, size
- len
);
1065 * Push down the NET/ROM header
1068 asmptr
= skb_push(skb
, NR_TRANSPORT_LEN
);
1069 SOCK_DEBUG(sk
, "Building NET/ROM Header.\n");
1071 /* Build a NET/ROM Transport header */
1073 *asmptr
++ = nr
->your_index
;
1074 *asmptr
++ = nr
->your_id
;
1075 *asmptr
++ = 0; /* To be filled in later */
1076 *asmptr
++ = 0; /* Ditto */
1077 *asmptr
++ = NR_INFO
;
1078 SOCK_DEBUG(sk
, "Built header.\n");
1081 * Put the data on the end
1084 skb
->h
.raw
= skb_put(skb
, len
);
1086 asmptr
= skb
->h
.raw
;
1087 SOCK_DEBUG(sk
, "NET/ROM: Appending user data\n");
1089 /* User data follows immediately after the NET/ROM transport header */
1090 if (memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
)) {
1096 SOCK_DEBUG(sk
, "NET/ROM: Transmitting buffer\n");
1098 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1104 nr_output(sk
, skb
); /* Shove it onto the queue */
1112 static int nr_recvmsg(struct kiocb
*iocb
, struct socket
*sock
,
1113 struct msghdr
*msg
, size_t size
, int flags
)
1115 struct sock
*sk
= sock
->sk
;
1116 struct sockaddr_ax25
*sax
= (struct sockaddr_ax25
*)msg
->msg_name
;
1118 struct sk_buff
*skb
;
1122 * This works for seqpacket too. The receiver has ordered the queue for
1123 * us! We do one quick check first though
1127 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1132 /* Now we can treat all alike */
1133 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
) {
1138 skb
->h
.raw
= skb
->data
;
1141 if (copied
> size
) {
1143 msg
->msg_flags
|= MSG_TRUNC
;
1146 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1149 sax
->sax25_family
= AF_NETROM
;
1150 memcpy(sax
->sax25_call
.ax25_call
, skb
->data
+ 7, AX25_ADDR_LEN
);
1153 msg
->msg_namelen
= sizeof(*sax
);
1155 skb_free_datagram(sk
, skb
);
1162 static int nr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1164 struct sock
*sk
= sock
->sk
;
1165 void __user
*argp
= (void __user
*)arg
;
1172 amount
= sk
->sk_sndbuf
- atomic_read(&sk
->sk_wmem_alloc
);
1176 return put_user(amount
, (int __user
*)argp
);
1180 struct sk_buff
*skb
;
1182 /* These two are safe on a single CPU system as only user tasks fiddle here */
1183 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1186 return put_user(amount
, (int __user
*)argp
);
1192 ret
= sock_get_timestamp(sk
, argp
);
1198 case SIOCGIFDSTADDR
:
1199 case SIOCSIFDSTADDR
:
1200 case SIOCGIFBRDADDR
:
1201 case SIOCSIFBRDADDR
:
1202 case SIOCGIFNETMASK
:
1203 case SIOCSIFNETMASK
:
1213 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1214 return nr_rt_ioctl(cmd
, argp
);
1218 return dev_ioctl(cmd
, argp
);
1225 #ifdef CONFIG_PROC_FS
1227 static void *nr_info_start(struct seq_file
*seq
, loff_t
*pos
)
1230 struct hlist_node
*node
;
1233 spin_lock_bh(&nr_list_lock
);
1235 return SEQ_START_TOKEN
;
1237 sk_for_each(s
, node
, &nr_list
) {
1245 static void *nr_info_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1249 return (v
== SEQ_START_TOKEN
) ? sk_head(&nr_list
)
1250 : sk_next((struct sock
*)v
);
1253 static void nr_info_stop(struct seq_file
*seq
, void *v
)
1255 spin_unlock_bh(&nr_list_lock
);
1258 static int nr_info_show(struct seq_file
*seq
, void *v
)
1261 struct net_device
*dev
;
1263 const char *devname
;
1265 if (v
== SEQ_START_TOKEN
)
1267 "user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n");
1274 if ((dev
= nr
->device
) == NULL
)
1277 devname
= dev
->name
;
1279 seq_printf(seq
, "%-9s ", ax2asc(&nr
->user_addr
));
1280 seq_printf(seq
, "%-9s ", ax2asc(&nr
->dest_addr
));
1282 "%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n",
1283 ax2asc(&nr
->source_addr
),
1293 ax25_display_timer(&nr
->t1timer
) / HZ
,
1295 ax25_display_timer(&nr
->t2timer
) / HZ
,
1297 ax25_display_timer(&nr
->t4timer
) / HZ
,
1299 ax25_display_timer(&nr
->idletimer
) / (60 * HZ
),
1300 nr
->idle
/ (60 * HZ
),
1304 atomic_read(&s
->sk_wmem_alloc
),
1305 atomic_read(&s
->sk_rmem_alloc
),
1306 s
->sk_socket
? SOCK_INODE(s
->sk_socket
)->i_ino
: 0L);
1313 static struct seq_operations nr_info_seqops
= {
1314 .start
= nr_info_start
,
1315 .next
= nr_info_next
,
1316 .stop
= nr_info_stop
,
1317 .show
= nr_info_show
,
1320 static int nr_info_open(struct inode
*inode
, struct file
*file
)
1322 return seq_open(file
, &nr_info_seqops
);
1325 static struct file_operations nr_info_fops
= {
1326 .owner
= THIS_MODULE
,
1327 .open
= nr_info_open
,
1329 .llseek
= seq_lseek
,
1330 .release
= seq_release
,
1332 #endif /* CONFIG_PROC_FS */
1334 static struct net_proto_family nr_family_ops
= {
1335 .family
= PF_NETROM
,
1336 .create
= nr_create
,
1337 .owner
= THIS_MODULE
,
1340 static struct proto_ops nr_proto_ops
= {
1341 .family
= PF_NETROM
,
1342 .owner
= THIS_MODULE
,
1343 .release
= nr_release
,
1345 .connect
= nr_connect
,
1346 .socketpair
= sock_no_socketpair
,
1347 .accept
= nr_accept
,
1348 .getname
= nr_getname
,
1349 .poll
= datagram_poll
,
1351 .listen
= nr_listen
,
1352 .shutdown
= sock_no_shutdown
,
1353 .setsockopt
= nr_setsockopt
,
1354 .getsockopt
= nr_getsockopt
,
1355 .sendmsg
= nr_sendmsg
,
1356 .recvmsg
= nr_recvmsg
,
1357 .mmap
= sock_no_mmap
,
1358 .sendpage
= sock_no_sendpage
,
1361 static struct notifier_block nr_dev_notifier
= {
1362 .notifier_call
= nr_device_event
,
1365 static struct net_device
**dev_nr
;
1367 static char banner
[] __initdata
= KERN_INFO
"G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n";
1369 static int __init
nr_proto_init(void)
1372 int rc
= proto_register(&nr_proto
, 0);
1377 if (nr_ndevs
> 0x7fffffff/sizeof(struct net_device
*)) {
1378 printk(KERN_ERR
"NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1382 dev_nr
= kmalloc(nr_ndevs
* sizeof(struct net_device
*), GFP_KERNEL
);
1383 if (dev_nr
== NULL
) {
1384 printk(KERN_ERR
"NET/ROM: nr_proto_init - unable to allocate device array\n");
1388 memset(dev_nr
, 0x00, nr_ndevs
* sizeof(struct net_device
*));
1390 for (i
= 0; i
< nr_ndevs
; i
++) {
1391 char name
[IFNAMSIZ
];
1392 struct net_device
*dev
;
1394 sprintf(name
, "nr%d", i
);
1395 dev
= alloc_netdev(sizeof(struct net_device_stats
), name
,
1398 printk(KERN_ERR
"NET/ROM: nr_proto_init - unable to allocate device structure\n");
1403 if (register_netdev(dev
)) {
1404 printk(KERN_ERR
"NET/ROM: nr_proto_init - unable to register network device\n");
1411 if (sock_register(&nr_family_ops
)) {
1412 printk(KERN_ERR
"NET/ROM: nr_proto_init - unable to register socket family\n");
1416 register_netdevice_notifier(&nr_dev_notifier
);
1419 ax25_protocol_register(AX25_P_NETROM
, nr_route_frame
);
1420 ax25_linkfail_register(nr_link_failed
);
1422 #ifdef CONFIG_SYSCTL
1423 nr_register_sysctl();
1428 proc_net_fops_create("nr", S_IRUGO
, &nr_info_fops
);
1429 proc_net_fops_create("nr_neigh", S_IRUGO
, &nr_neigh_fops
);
1430 proc_net_fops_create("nr_nodes", S_IRUGO
, &nr_nodes_fops
);
1435 unregister_netdev(dev_nr
[i
]);
1436 free_netdev(dev_nr
[i
]);
1439 proto_unregister(&nr_proto
);
1444 module_init(nr_proto_init
);
1446 module_param(nr_ndevs
, int, 0);
1447 MODULE_PARM_DESC(nr_ndevs
, "number of NET/ROM devices");
1449 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1450 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1451 MODULE_LICENSE("GPL");
1452 MODULE_ALIAS_NETPROTO(PF_NETROM
);
1454 static void __exit
nr_exit(void)
1458 proc_net_remove("nr");
1459 proc_net_remove("nr_neigh");
1460 proc_net_remove("nr_nodes");
1461 nr_loopback_clear();
1465 #ifdef CONFIG_SYSCTL
1466 nr_unregister_sysctl();
1469 ax25_linkfail_release(nr_link_failed
);
1470 ax25_protocol_release(AX25_P_NETROM
);
1472 unregister_netdevice_notifier(&nr_dev_notifier
);
1474 sock_unregister(PF_NETROM
);
1476 for (i
= 0; i
< nr_ndevs
; i
++) {
1477 struct net_device
*dev
= dev_nr
[i
];
1479 unregister_netdev(dev
);
1485 proto_unregister(&nr_proto
);
1487 module_exit(nr_exit
);