1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
6 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
8 #include <linux/module.h>
9 #include <linux/moduleparam.h>
10 #include <linux/capability.h>
11 #include <linux/errno.h>
12 #include <linux/types.h>
13 #include <linux/socket.h>
15 #include <linux/slab.h>
16 #include <linux/kernel.h>
17 #include <linux/sched/signal.h>
18 #include <linux/timer.h>
19 #include <linux/string.h>
20 #include <linux/sockios.h>
21 #include <linux/net.h>
22 #include <linux/stat.h>
24 #include <linux/inet.h>
25 #include <linux/netdevice.h>
26 #include <linux/if_arp.h>
27 #include <linux/skbuff.h>
28 #include <net/net_namespace.h>
30 #include <linux/uaccess.h>
31 #include <linux/fcntl.h>
32 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
34 #include <linux/interrupt.h>
35 #include <linux/notifier.h>
36 #include <net/netrom.h>
37 #include <linux/proc_fs.h>
38 #include <linux/seq_file.h>
40 #include <net/tcp_states.h>
42 #include <linux/init.h>
44 static int nr_ndevs
= 4;
46 int sysctl_netrom_default_path_quality
= NR_DEFAULT_QUAL
;
47 int sysctl_netrom_obsolescence_count_initialiser
= NR_DEFAULT_OBS
;
48 int sysctl_netrom_network_ttl_initialiser
= NR_DEFAULT_TTL
;
49 int sysctl_netrom_transport_timeout
= NR_DEFAULT_T1
;
50 int sysctl_netrom_transport_maximum_tries
= NR_DEFAULT_N2
;
51 int sysctl_netrom_transport_acknowledge_delay
= NR_DEFAULT_T2
;
52 int sysctl_netrom_transport_busy_delay
= NR_DEFAULT_T4
;
53 int sysctl_netrom_transport_requested_window_size
= NR_DEFAULT_WINDOW
;
54 int sysctl_netrom_transport_no_activity_timeout
= NR_DEFAULT_IDLE
;
55 int sysctl_netrom_routing_control
= NR_DEFAULT_ROUTING
;
56 int sysctl_netrom_link_fails_count
= NR_DEFAULT_FAILS
;
57 int sysctl_netrom_reset_circuit
= NR_DEFAULT_RESET
;
59 static unsigned short circuit
= 0x101;
61 static HLIST_HEAD(nr_list
);
62 static DEFINE_SPINLOCK(nr_list_lock
);
64 static const struct proto_ops nr_proto_ops
;
67 * NETROM network devices are virtual network devices encapsulating NETROM
68 * frames into AX.25 which will be sent through an AX.25 device, so form a
69 * special "super class" of normal net devices; split their locks off into a
70 * separate class since they always nest.
72 static struct lock_class_key nr_netdev_xmit_lock_key
;
73 static struct lock_class_key nr_netdev_addr_lock_key
;
75 static void nr_set_lockdep_one(struct net_device
*dev
,
76 struct netdev_queue
*txq
,
79 lockdep_set_class(&txq
->_xmit_lock
, &nr_netdev_xmit_lock_key
);
82 static void nr_set_lockdep_key(struct net_device
*dev
)
84 lockdep_set_class(&dev
->addr_list_lock
, &nr_netdev_addr_lock_key
);
85 netdev_for_each_tx_queue(dev
, nr_set_lockdep_one
, NULL
);
89 * Socket removal during an interrupt is now safe.
91 static void nr_remove_socket(struct sock
*sk
)
93 spin_lock_bh(&nr_list_lock
);
95 spin_unlock_bh(&nr_list_lock
);
99 * Kill all bound sockets on a dropped device.
101 static void nr_kill_by_device(struct net_device
*dev
)
105 spin_lock_bh(&nr_list_lock
);
106 sk_for_each(s
, &nr_list
)
107 if (nr_sk(s
)->device
== dev
)
108 nr_disconnect(s
, ENETUNREACH
);
109 spin_unlock_bh(&nr_list_lock
);
113 * Handle device status changes.
115 static int nr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
117 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
119 if (!net_eq(dev_net(dev
), &init_net
))
122 if (event
!= NETDEV_DOWN
)
125 nr_kill_by_device(dev
);
126 nr_rt_device_down(dev
);
132 * Add a socket to the bound sockets list.
134 static void nr_insert_socket(struct sock
*sk
)
136 spin_lock_bh(&nr_list_lock
);
137 sk_add_node(sk
, &nr_list
);
138 spin_unlock_bh(&nr_list_lock
);
142 * Find a socket that wants to accept the Connect Request we just
145 static struct sock
*nr_find_listener(ax25_address
*addr
)
149 spin_lock_bh(&nr_list_lock
);
150 sk_for_each(s
, &nr_list
)
151 if (!ax25cmp(&nr_sk(s
)->source_addr
, addr
) &&
152 s
->sk_state
== TCP_LISTEN
) {
158 spin_unlock_bh(&nr_list_lock
);
163 * Find a connected NET/ROM socket given my circuit IDs.
165 static struct sock
*nr_find_socket(unsigned char index
, unsigned char id
)
169 spin_lock_bh(&nr_list_lock
);
170 sk_for_each(s
, &nr_list
) {
171 struct nr_sock
*nr
= nr_sk(s
);
173 if (nr
->my_index
== index
&& nr
->my_id
== id
) {
180 spin_unlock_bh(&nr_list_lock
);
185 * Find a connected NET/ROM socket given their circuit IDs.
187 static struct sock
*nr_find_peer(unsigned char index
, unsigned char id
,
192 spin_lock_bh(&nr_list_lock
);
193 sk_for_each(s
, &nr_list
) {
194 struct nr_sock
*nr
= nr_sk(s
);
196 if (nr
->your_index
== index
&& nr
->your_id
== id
&&
197 !ax25cmp(&nr
->dest_addr
, dest
)) {
204 spin_unlock_bh(&nr_list_lock
);
209 * Find next free circuit ID.
211 static unsigned short nr_find_next_circuit(void)
213 unsigned short id
= circuit
;
221 if (i
!= 0 && j
!= 0) {
222 if ((sk
=nr_find_socket(i
, j
)) == NULL
)
236 void nr_destroy_socket(struct sock
*);
239 * Handler for deferred kills.
241 static void nr_destroy_timer(struct timer_list
*t
)
243 struct sock
*sk
= from_timer(sk
, t
, sk_timer
);
246 nr_destroy_socket(sk
);
252 * This is called from user mode and the timers. Thus it protects itself
253 * against interrupt users but doesn't worry about being called during
254 * work. Once it is removed from the queue no interrupt or bottom half
255 * will touch it and we are (fairly 8-) ) safe.
257 void nr_destroy_socket(struct sock
*sk
)
261 nr_remove_socket(sk
);
263 nr_stop_heartbeat(sk
);
267 nr_stop_idletimer(sk
);
269 nr_clear_queues(sk
); /* Flush the queues */
271 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
272 if (skb
->sk
!= sk
) { /* A pending connection */
273 /* Queue the unaccepted socket for death */
274 sock_set_flag(skb
->sk
, SOCK_DEAD
);
275 nr_start_heartbeat(skb
->sk
);
276 nr_sk(skb
->sk
)->state
= NR_STATE_0
;
282 if (sk_has_allocations(sk
)) {
283 /* Defer: outstanding buffers */
284 sk
->sk_timer
.function
= nr_destroy_timer
;
285 sk
->sk_timer
.expires
= jiffies
+ 2 * HZ
;
286 add_timer(&sk
->sk_timer
);
292 * Handling for system calls applied via the various interfaces to a
293 * NET/ROM socket object.
296 static int nr_setsockopt(struct socket
*sock
, int level
, int optname
,
297 char __user
*optval
, unsigned int optlen
)
299 struct sock
*sk
= sock
->sk
;
300 struct nr_sock
*nr
= nr_sk(sk
);
303 if (level
!= SOL_NETROM
)
306 if (optlen
< sizeof(unsigned int))
309 if (get_user(opt
, (unsigned int __user
*)optval
))
314 if (opt
< 1 || opt
> ULONG_MAX
/ HZ
)
320 if (opt
< 1 || opt
> ULONG_MAX
/ HZ
)
326 if (opt
< 1 || opt
> 31)
332 if (opt
< 1 || opt
> ULONG_MAX
/ HZ
)
338 if (opt
> ULONG_MAX
/ (60 * HZ
))
340 nr
->idle
= opt
* 60 * HZ
;
348 static int nr_getsockopt(struct socket
*sock
, int level
, int optname
,
349 char __user
*optval
, int __user
*optlen
)
351 struct sock
*sk
= sock
->sk
;
352 struct nr_sock
*nr
= nr_sk(sk
);
356 if (level
!= SOL_NETROM
)
359 if (get_user(len
, optlen
))
383 val
= nr
->idle
/ (60 * HZ
);
390 len
= min_t(unsigned int, len
, sizeof(int));
392 if (put_user(len
, optlen
))
395 return copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
398 static int nr_listen(struct socket
*sock
, int backlog
)
400 struct sock
*sk
= sock
->sk
;
403 if (sk
->sk_state
!= TCP_LISTEN
) {
404 memset(&nr_sk(sk
)->user_addr
, 0, AX25_ADDR_LEN
);
405 sk
->sk_max_ack_backlog
= backlog
;
406 sk
->sk_state
= TCP_LISTEN
;
415 static struct proto nr_proto
= {
417 .owner
= THIS_MODULE
,
418 .obj_size
= sizeof(struct nr_sock
),
421 static int nr_create(struct net
*net
, struct socket
*sock
, int protocol
,
427 if (!net_eq(net
, &init_net
))
428 return -EAFNOSUPPORT
;
430 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
431 return -ESOCKTNOSUPPORT
;
433 sk
= sk_alloc(net
, PF_NETROM
, GFP_ATOMIC
, &nr_proto
, kern
);
439 sock_init_data(sock
, sk
);
441 sock
->ops
= &nr_proto_ops
;
442 sk
->sk_protocol
= protocol
;
444 skb_queue_head_init(&nr
->ack_queue
);
445 skb_queue_head_init(&nr
->reseq_queue
);
446 skb_queue_head_init(&nr
->frag_queue
);
451 msecs_to_jiffies(sysctl_netrom_transport_timeout
);
453 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay
);
455 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries
);
457 msecs_to_jiffies(sysctl_netrom_transport_busy_delay
);
459 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout
);
460 nr
->window
= sysctl_netrom_transport_requested_window_size
;
463 nr
->state
= NR_STATE_0
;
468 static struct sock
*nr_make_new(struct sock
*osk
)
471 struct nr_sock
*nr
, *onr
;
473 if (osk
->sk_type
!= SOCK_SEQPACKET
)
476 sk
= sk_alloc(sock_net(osk
), PF_NETROM
, GFP_ATOMIC
, osk
->sk_prot
, 0);
482 sock_init_data(NULL
, sk
);
484 sk
->sk_type
= osk
->sk_type
;
485 sk
->sk_priority
= osk
->sk_priority
;
486 sk
->sk_protocol
= osk
->sk_protocol
;
487 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
488 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
489 sk
->sk_state
= TCP_ESTABLISHED
;
490 sock_copy_flags(sk
, osk
);
492 skb_queue_head_init(&nr
->ack_queue
);
493 skb_queue_head_init(&nr
->reseq_queue
);
494 skb_queue_head_init(&nr
->frag_queue
);
504 nr
->idle
= onr
->idle
;
505 nr
->window
= onr
->window
;
507 nr
->device
= onr
->device
;
508 nr
->bpqext
= onr
->bpqext
;
513 static int nr_release(struct socket
*sock
)
515 struct sock
*sk
= sock
->sk
;
518 if (sk
== NULL
) return 0;
529 nr_disconnect(sk
, 0);
530 nr_destroy_socket(sk
);
536 nr_write_internal(sk
, NR_DISCREQ
);
537 nr_start_t1timer(sk
);
540 nr_stop_idletimer(sk
);
541 nr
->state
= NR_STATE_2
;
542 sk
->sk_state
= TCP_CLOSE
;
543 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
544 sk
->sk_state_change(sk
);
545 sock_set_flag(sk
, SOCK_DESTROY
);
559 static int nr_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
561 struct sock
*sk
= sock
->sk
;
562 struct nr_sock
*nr
= nr_sk(sk
);
563 struct full_sockaddr_ax25
*addr
= (struct full_sockaddr_ax25
*)uaddr
;
564 struct net_device
*dev
;
565 ax25_uid_assoc
*user
;
566 ax25_address
*source
;
569 if (!sock_flag(sk
, SOCK_ZAPPED
)) {
573 if (addr_len
< sizeof(struct sockaddr_ax25
) || addr_len
> sizeof(struct full_sockaddr_ax25
)) {
577 if (addr_len
< (addr
->fsa_ax25
.sax25_ndigis
* sizeof(ax25_address
) + sizeof(struct sockaddr_ax25
))) {
581 if (addr
->fsa_ax25
.sax25_family
!= AF_NETROM
) {
585 if ((dev
= nr_dev_get(&addr
->fsa_ax25
.sax25_call
)) == NULL
) {
587 return -EADDRNOTAVAIL
;
591 * Only the super user can set an arbitrary user callsign.
593 if (addr
->fsa_ax25
.sax25_ndigis
== 1) {
594 if (!capable(CAP_NET_BIND_SERVICE
)) {
599 nr
->user_addr
= addr
->fsa_digipeater
[0];
600 nr
->source_addr
= addr
->fsa_ax25
.sax25_call
;
602 source
= &addr
->fsa_ax25
.sax25_call
;
604 user
= ax25_findbyuid(current_euid());
606 nr
->user_addr
= user
->call
;
609 if (ax25_uid_policy
&& !capable(CAP_NET_BIND_SERVICE
)) {
614 nr
->user_addr
= *source
;
617 nr
->source_addr
= *source
;
621 nr_insert_socket(sk
);
623 sock_reset_flag(sk
, SOCK_ZAPPED
);
630 static int nr_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
631 int addr_len
, int flags
)
633 struct sock
*sk
= sock
->sk
;
634 struct nr_sock
*nr
= nr_sk(sk
);
635 struct sockaddr_ax25
*addr
= (struct sockaddr_ax25
*)uaddr
;
636 ax25_address
*source
= NULL
;
637 ax25_uid_assoc
*user
;
638 struct net_device
*dev
;
642 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
643 sock
->state
= SS_CONNECTED
;
644 goto out_release
; /* Connect completed during a ERESTARTSYS event */
647 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
648 sock
->state
= SS_UNCONNECTED
;
653 if (sk
->sk_state
== TCP_ESTABLISHED
) {
654 err
= -EISCONN
; /* No reconnect on a seqpacket socket */
658 sk
->sk_state
= TCP_CLOSE
;
659 sock
->state
= SS_UNCONNECTED
;
661 if (addr_len
!= sizeof(struct sockaddr_ax25
) && addr_len
!= sizeof(struct full_sockaddr_ax25
)) {
665 if (addr
->sax25_family
!= AF_NETROM
) {
669 if (sock_flag(sk
, SOCK_ZAPPED
)) { /* Must bind first - autobinding in this may or may not work */
670 sock_reset_flag(sk
, SOCK_ZAPPED
);
672 if ((dev
= nr_dev_first()) == NULL
) {
676 source
= (ax25_address
*)dev
->dev_addr
;
678 user
= ax25_findbyuid(current_euid());
680 nr
->user_addr
= user
->call
;
683 if (ax25_uid_policy
&& !capable(CAP_NET_ADMIN
)) {
688 nr
->user_addr
= *source
;
691 nr
->source_addr
= *source
;
695 nr_insert_socket(sk
); /* Finish the bind */
698 nr
->dest_addr
= addr
->sax25_call
;
701 circuit
= nr_find_next_circuit();
704 nr
->my_index
= circuit
/ 256;
705 nr
->my_id
= circuit
% 256;
709 /* Move to connecting socket, start sending Connect Requests */
710 sock
->state
= SS_CONNECTING
;
711 sk
->sk_state
= TCP_SYN_SENT
;
713 nr_establish_data_link(sk
);
715 nr
->state
= NR_STATE_1
;
717 nr_start_heartbeat(sk
);
720 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
)) {
726 * A Connect Ack with Choke or timeout or failed routing will go to
729 if (sk
->sk_state
== TCP_SYN_SENT
) {
733 prepare_to_wait(sk_sleep(sk
), &wait
,
735 if (sk
->sk_state
!= TCP_SYN_SENT
)
737 if (!signal_pending(current
)) {
746 finish_wait(sk_sleep(sk
), &wait
);
751 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
752 sock
->state
= SS_UNCONNECTED
;
753 err
= sock_error(sk
); /* Always set at this point */
757 sock
->state
= SS_CONNECTED
;
765 static int nr_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
774 if ((sk
= sock
->sk
) == NULL
)
778 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
783 if (sk
->sk_state
!= TCP_LISTEN
) {
789 * The write queue this time is holding sockets ready to use
790 * hooked into the SABM we saved
793 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
794 skb
= skb_dequeue(&sk
->sk_receive_queue
);
798 if (flags
& O_NONBLOCK
) {
802 if (!signal_pending(current
)) {
811 finish_wait(sk_sleep(sk
), &wait
);
816 sock_graft(newsk
, newsock
);
818 /* Now attach up the new socket */
820 sk_acceptq_removed(sk
);
828 static int nr_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
831 struct full_sockaddr_ax25
*sax
= (struct full_sockaddr_ax25
*)uaddr
;
832 struct sock
*sk
= sock
->sk
;
833 struct nr_sock
*nr
= nr_sk(sk
);
836 memset(&sax
->fsa_ax25
, 0, sizeof(struct sockaddr_ax25
));
840 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
844 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
845 sax
->fsa_ax25
.sax25_ndigis
= 1;
846 sax
->fsa_ax25
.sax25_call
= nr
->user_addr
;
847 memset(sax
->fsa_digipeater
, 0, sizeof(sax
->fsa_digipeater
));
848 sax
->fsa_digipeater
[0] = nr
->dest_addr
;
849 uaddr_len
= sizeof(struct full_sockaddr_ax25
);
851 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
852 sax
->fsa_ax25
.sax25_ndigis
= 0;
853 sax
->fsa_ax25
.sax25_call
= nr
->source_addr
;
854 uaddr_len
= sizeof(struct sockaddr_ax25
);
861 int nr_rx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
865 struct nr_sock
*nr_make
;
866 ax25_address
*src
, *dest
, *user
;
867 unsigned short circuit_index
, circuit_id
;
868 unsigned short peer_circuit_index
, peer_circuit_id
;
869 unsigned short frametype
, flags
, window
, timeout
;
875 * skb->data points to the netrom frame start
878 src
= (ax25_address
*)(skb
->data
+ 0);
879 dest
= (ax25_address
*)(skb
->data
+ 7);
881 circuit_index
= skb
->data
[15];
882 circuit_id
= skb
->data
[16];
883 peer_circuit_index
= skb
->data
[17];
884 peer_circuit_id
= skb
->data
[18];
885 frametype
= skb
->data
[19] & 0x0F;
886 flags
= skb
->data
[19] & 0xF0;
889 * Check for an incoming IP over NET/ROM frame.
891 if (frametype
== NR_PROTOEXT
&&
892 circuit_index
== NR_PROTO_IP
&& circuit_id
== NR_PROTO_IP
) {
893 skb_pull(skb
, NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
);
894 skb_reset_transport_header(skb
);
896 return nr_rx_ip(skb
, dev
);
900 * Find an existing socket connection, based on circuit ID, if it's
901 * a Connect Request base it on their circuit ID.
903 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
904 * circuit that no longer exists at the other end ...
909 if (circuit_index
== 0 && circuit_id
== 0) {
910 if (frametype
== NR_CONNACK
&& flags
== NR_CHOKE_FLAG
)
911 sk
= nr_find_peer(peer_circuit_index
, peer_circuit_id
, src
);
913 if (frametype
== NR_CONNREQ
)
914 sk
= nr_find_peer(circuit_index
, circuit_id
, src
);
916 sk
= nr_find_socket(circuit_index
, circuit_id
);
921 skb_reset_transport_header(skb
);
923 if (frametype
== NR_CONNACK
&& skb
->len
== 22)
924 nr_sk(sk
)->bpqext
= 1;
926 nr_sk(sk
)->bpqext
= 0;
928 ret
= nr_process_rx_frame(sk
, skb
);
935 * Now it should be a CONNREQ.
937 if (frametype
!= NR_CONNREQ
) {
939 * Here it would be nice to be able to send a reset but
940 * NET/ROM doesn't have one. We've tried to extend the protocol
941 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
942 * apparently kills BPQ boxes... :-(
943 * So now we try to follow the established behaviour of
944 * G8PZT's Xrouter which is sending packets with command type 7
945 * as an extension of the protocol.
947 if (sysctl_netrom_reset_circuit
&&
948 (frametype
!= NR_RESET
|| flags
!= 0))
949 nr_transmit_reset(skb
, 1);
954 sk
= nr_find_listener(dest
);
956 user
= (ax25_address
*)(skb
->data
+ 21);
958 if (sk
== NULL
|| sk_acceptq_is_full(sk
) ||
959 (make
= nr_make_new(sk
)) == NULL
) {
960 nr_transmit_refusal(skb
, 0);
968 window
= skb
->data
[20];
971 skb
->destructor
= sock_efree
;
972 make
->sk_state
= TCP_ESTABLISHED
;
974 /* Fill in his circuit details */
975 nr_make
= nr_sk(make
);
976 nr_make
->source_addr
= *dest
;
977 nr_make
->dest_addr
= *src
;
978 nr_make
->user_addr
= *user
;
980 nr_make
->your_index
= circuit_index
;
981 nr_make
->your_id
= circuit_id
;
984 circuit
= nr_find_next_circuit();
987 nr_make
->my_index
= circuit
/ 256;
988 nr_make
->my_id
= circuit
% 256;
992 /* Window negotiation */
993 if (window
< nr_make
->window
)
994 nr_make
->window
= window
;
996 /* L4 timeout negotiation */
997 if (skb
->len
== 37) {
998 timeout
= skb
->data
[36] * 256 + skb
->data
[35];
999 if (timeout
* HZ
< nr_make
->t1
)
1000 nr_make
->t1
= timeout
* HZ
;
1001 nr_make
->bpqext
= 1;
1003 nr_make
->bpqext
= 0;
1006 nr_write_internal(make
, NR_CONNACK
);
1008 nr_make
->condition
= 0x00;
1013 nr_make
->state
= NR_STATE_3
;
1014 sk_acceptq_added(sk
);
1015 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1017 if (!sock_flag(sk
, SOCK_DEAD
))
1018 sk
->sk_data_ready(sk
);
1023 nr_insert_socket(make
);
1025 nr_start_heartbeat(make
);
1026 nr_start_idletimer(make
);
1031 static int nr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
1033 struct sock
*sk
= sock
->sk
;
1034 struct nr_sock
*nr
= nr_sk(sk
);
1035 DECLARE_SOCKADDR(struct sockaddr_ax25
*, usax
, msg
->msg_name
);
1037 struct sockaddr_ax25 sax
;
1038 struct sk_buff
*skb
;
1039 unsigned char *asmptr
;
1042 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
|MSG_CMSG_COMPAT
))
1046 if (sock_flag(sk
, SOCK_ZAPPED
)) {
1047 err
= -EADDRNOTAVAIL
;
1051 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1052 send_sig(SIGPIPE
, current
, 0);
1057 if (nr
->device
== NULL
) {
1063 if (msg
->msg_namelen
< sizeof(sax
)) {
1068 if (ax25cmp(&nr
->dest_addr
, &sax
.sax25_call
) != 0) {
1072 if (sax
.sax25_family
!= AF_NETROM
) {
1077 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1081 sax
.sax25_family
= AF_NETROM
;
1082 sax
.sax25_call
= nr
->dest_addr
;
1085 /* Build a packet - the conventional user limit is 236 bytes. We can
1086 do ludicrously large NetROM frames but must not overflow */
1092 size
= len
+ NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
;
1094 if ((skb
= sock_alloc_send_skb(sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1097 skb_reserve(skb
, size
- len
);
1098 skb_reset_transport_header(skb
);
1101 * Push down the NET/ROM header
1104 asmptr
= skb_push(skb
, NR_TRANSPORT_LEN
);
1106 /* Build a NET/ROM Transport header */
1108 *asmptr
++ = nr
->your_index
;
1109 *asmptr
++ = nr
->your_id
;
1110 *asmptr
++ = 0; /* To be filled in later */
1111 *asmptr
++ = 0; /* Ditto */
1112 *asmptr
++ = NR_INFO
;
1115 * Put the data on the end
1119 /* User data follows immediately after the NET/ROM transport header */
1120 if (memcpy_from_msg(skb_transport_header(skb
), msg
, len
)) {
1126 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1132 nr_output(sk
, skb
); /* Shove it onto the queue */
1140 static int nr_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
1143 struct sock
*sk
= sock
->sk
;
1144 DECLARE_SOCKADDR(struct sockaddr_ax25
*, sax
, msg
->msg_name
);
1146 struct sk_buff
*skb
;
1150 * This works for seqpacket too. The receiver has ordered the queue for
1151 * us! We do one quick check first though
1155 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
1160 /* Now we can treat all alike */
1161 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
) {
1166 skb_reset_transport_header(skb
);
1169 if (copied
> size
) {
1171 msg
->msg_flags
|= MSG_TRUNC
;
1174 er
= skb_copy_datagram_msg(skb
, 0, msg
, copied
);
1176 skb_free_datagram(sk
, skb
);
1182 memset(sax
, 0, sizeof(*sax
));
1183 sax
->sax25_family
= AF_NETROM
;
1184 skb_copy_from_linear_data_offset(skb
, 7, sax
->sax25_call
.ax25_call
,
1186 msg
->msg_namelen
= sizeof(*sax
);
1189 skb_free_datagram(sk
, skb
);
1196 static int nr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1198 struct sock
*sk
= sock
->sk
;
1199 void __user
*argp
= (void __user
*)arg
;
1206 amount
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1210 return put_user(amount
, (int __user
*)argp
);
1214 struct sk_buff
*skb
;
1218 /* These two are safe on a single CPU system as only user tasks fiddle here */
1219 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1222 return put_user(amount
, (int __user
*)argp
);
1227 case SIOCGIFDSTADDR
:
1228 case SIOCSIFDSTADDR
:
1229 case SIOCGIFBRDADDR
:
1230 case SIOCSIFBRDADDR
:
1231 case SIOCGIFNETMASK
:
1232 case SIOCSIFNETMASK
:
1240 if (!capable(CAP_NET_ADMIN
))
1242 return nr_rt_ioctl(cmd
, argp
);
1245 return -ENOIOCTLCMD
;
1251 #ifdef CONFIG_PROC_FS
1253 static void *nr_info_start(struct seq_file
*seq
, loff_t
*pos
)
1255 spin_lock_bh(&nr_list_lock
);
1256 return seq_hlist_start_head(&nr_list
, *pos
);
1259 static void *nr_info_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1261 return seq_hlist_next(v
, &nr_list
, pos
);
1264 static void nr_info_stop(struct seq_file
*seq
, void *v
)
1266 spin_unlock_bh(&nr_list_lock
);
1269 static int nr_info_show(struct seq_file
*seq
, void *v
)
1271 struct sock
*s
= sk_entry(v
);
1272 struct net_device
*dev
;
1274 const char *devname
;
1277 if (v
== SEQ_START_TOKEN
)
1279 "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");
1286 if ((dev
= nr
->device
) == NULL
)
1289 devname
= dev
->name
;
1291 seq_printf(seq
, "%-9s ", ax2asc(buf
, &nr
->user_addr
));
1292 seq_printf(seq
, "%-9s ", ax2asc(buf
, &nr
->dest_addr
));
1294 "%-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",
1295 ax2asc(buf
, &nr
->source_addr
),
1305 ax25_display_timer(&nr
->t1timer
) / HZ
,
1307 ax25_display_timer(&nr
->t2timer
) / HZ
,
1309 ax25_display_timer(&nr
->t4timer
) / HZ
,
1311 ax25_display_timer(&nr
->idletimer
) / (60 * HZ
),
1312 nr
->idle
/ (60 * HZ
),
1316 sk_wmem_alloc_get(s
),
1317 sk_rmem_alloc_get(s
),
1318 s
->sk_socket
? SOCK_INODE(s
->sk_socket
)->i_ino
: 0L);
1325 static const struct seq_operations nr_info_seqops
= {
1326 .start
= nr_info_start
,
1327 .next
= nr_info_next
,
1328 .stop
= nr_info_stop
,
1329 .show
= nr_info_show
,
1331 #endif /* CONFIG_PROC_FS */
1333 static const struct net_proto_family nr_family_ops
= {
1334 .family
= PF_NETROM
,
1335 .create
= nr_create
,
1336 .owner
= THIS_MODULE
,
1339 static const struct proto_ops nr_proto_ops
= {
1340 .family
= PF_NETROM
,
1341 .owner
= THIS_MODULE
,
1342 .release
= nr_release
,
1344 .connect
= nr_connect
,
1345 .socketpair
= sock_no_socketpair
,
1346 .accept
= nr_accept
,
1347 .getname
= nr_getname
,
1348 .poll
= datagram_poll
,
1350 .gettstamp
= sock_gettstamp
,
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 struct ax25_protocol nr_pid
= {
1368 .pid
= AX25_P_NETROM
,
1369 .func
= nr_route_frame
1372 static struct ax25_linkfail nr_linkfail_notifier
= {
1373 .func
= nr_link_failed
,
1376 static int __init
nr_proto_init(void)
1379 int rc
= proto_register(&nr_proto
, 0);
1384 if (nr_ndevs
> 0x7fffffff/sizeof(struct net_device
*)) {
1385 pr_err("NET/ROM: %s - nr_ndevs parameter too large\n",
1388 goto unregister_proto
;
1391 dev_nr
= kcalloc(nr_ndevs
, sizeof(struct net_device
*), GFP_KERNEL
);
1393 pr_err("NET/ROM: %s - unable to allocate device array\n",
1396 goto unregister_proto
;
1399 for (i
= 0; i
< nr_ndevs
; i
++) {
1400 char name
[IFNAMSIZ
];
1401 struct net_device
*dev
;
1403 sprintf(name
, "nr%d", i
);
1404 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, nr_setup
);
1411 rc
= register_netdev(dev
);
1416 nr_set_lockdep_key(dev
);
1420 rc
= sock_register(&nr_family_ops
);
1424 rc
= register_netdevice_notifier(&nr_dev_notifier
);
1428 ax25_register_pid(&nr_pid
);
1429 ax25_linkfail_register(&nr_linkfail_notifier
);
1431 #ifdef CONFIG_SYSCTL
1432 rc
= nr_register_sysctl();
1440 if (!proc_create_seq("nr", 0444, init_net
.proc_net
, &nr_info_seqops
))
1442 if (!proc_create_seq("nr_neigh", 0444, init_net
.proc_net
,
1445 if (!proc_create_seq("nr_nodes", 0444, init_net
.proc_net
,
1452 remove_proc_entry("nr_neigh", init_net
.proc_net
);
1454 remove_proc_entry("nr", init_net
.proc_net
);
1457 nr_loopback_clear();
1460 #ifdef CONFIG_SYSCTL
1461 nr_unregister_sysctl();
1464 ax25_linkfail_release(&nr_linkfail_notifier
);
1465 ax25_protocol_release(AX25_P_NETROM
);
1466 unregister_netdevice_notifier(&nr_dev_notifier
);
1468 sock_unregister(PF_NETROM
);
1471 unregister_netdev(dev_nr
[i
]);
1472 free_netdev(dev_nr
[i
]);
1476 proto_unregister(&nr_proto
);
1480 module_init(nr_proto_init
);
1482 module_param(nr_ndevs
, int, 0);
1483 MODULE_PARM_DESC(nr_ndevs
, "number of NET/ROM devices");
1485 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1486 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1487 MODULE_LICENSE("GPL");
1488 MODULE_ALIAS_NETPROTO(PF_NETROM
);
1490 static void __exit
nr_exit(void)
1494 remove_proc_entry("nr", init_net
.proc_net
);
1495 remove_proc_entry("nr_neigh", init_net
.proc_net
);
1496 remove_proc_entry("nr_nodes", init_net
.proc_net
);
1497 nr_loopback_clear();
1501 #ifdef CONFIG_SYSCTL
1502 nr_unregister_sysctl();
1505 ax25_linkfail_release(&nr_linkfail_notifier
);
1506 ax25_protocol_release(AX25_P_NETROM
);
1508 unregister_netdevice_notifier(&nr_dev_notifier
);
1510 sock_unregister(PF_NETROM
);
1512 for (i
= 0; i
< nr_ndevs
; i
++) {
1513 struct net_device
*dev
= dev_nr
[i
];
1515 unregister_netdev(dev
);
1521 proto_unregister(&nr_proto
);
1523 module_exit(nr_exit
);