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/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/capability.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>
30 #include <net/net_namespace.h>
32 #include <asm/uaccess.h>
33 #include <asm/system.h>
34 #include <linux/fcntl.h>
35 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
37 #include <linux/interrupt.h>
38 #include <linux/notifier.h>
39 #include <net/netrom.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
43 #include <net/tcp_states.h>
45 #include <linux/init.h>
47 static int nr_ndevs = 4;
49 int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
50 int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
51 int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
52 int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
53 int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
54 int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
55 int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
56 int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
57 int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
58 int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
59 int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
60 int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET;
62 static unsigned short circuit = 0x101;
64 static HLIST_HEAD(nr_list);
65 static DEFINE_SPINLOCK(nr_list_lock);
67 static const struct proto_ops nr_proto_ops;
70 * NETROM network devices are virtual network devices encapsulating NETROM
71 * frames into AX.25 which will be sent through an AX.25 device, so form a
72 * special "super class" of normal net devices; split their locks off into a
73 * separate class since they always nest.
75 static struct lock_class_key nr_netdev_xmit_lock_key;
78 * Socket removal during an interrupt is now safe.
80 static void nr_remove_socket(struct sock *sk)
82 spin_lock_bh(&nr_list_lock);
84 spin_unlock_bh(&nr_list_lock);
88 * Kill all bound sockets on a dropped device.
90 static void nr_kill_by_device(struct net_device *dev)
93 struct hlist_node *node;
95 spin_lock_bh(&nr_list_lock);
96 sk_for_each(s, node, &nr_list)
97 if (nr_sk(s)->device == dev)
98 nr_disconnect(s, ENETUNREACH);
99 spin_unlock_bh(&nr_list_lock);
103 * Handle device status changes.
105 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
107 struct net_device *dev = (struct net_device *)ptr;
109 if (dev_net(dev) != &init_net)
112 if (event != NETDEV_DOWN)
115 nr_kill_by_device(dev);
116 nr_rt_device_down(dev);
122 * Add a socket to the bound sockets list.
124 static void nr_insert_socket(struct sock *sk)
126 spin_lock_bh(&nr_list_lock);
127 sk_add_node(sk, &nr_list);
128 spin_unlock_bh(&nr_list_lock);
132 * Find a socket that wants to accept the Connect Request we just
135 static struct sock *nr_find_listener(ax25_address *addr)
138 struct hlist_node *node;
140 spin_lock_bh(&nr_list_lock);
141 sk_for_each(s, node, &nr_list)
142 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
143 s->sk_state == TCP_LISTEN) {
149 spin_unlock_bh(&nr_list_lock);
154 * Find a connected NET/ROM socket given my circuit IDs.
156 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
159 struct hlist_node *node;
161 spin_lock_bh(&nr_list_lock);
162 sk_for_each(s, node, &nr_list) {
163 struct nr_sock *nr = nr_sk(s);
165 if (nr->my_index == index && nr->my_id == id) {
172 spin_unlock_bh(&nr_list_lock);
177 * Find a connected NET/ROM socket given their circuit IDs.
179 static struct sock *nr_find_peer(unsigned char index, unsigned char id,
183 struct hlist_node *node;
185 spin_lock_bh(&nr_list_lock);
186 sk_for_each(s, node, &nr_list) {
187 struct nr_sock *nr = nr_sk(s);
189 if (nr->your_index == index && nr->your_id == id &&
190 !ax25cmp(&nr->dest_addr, dest)) {
197 spin_unlock_bh(&nr_list_lock);
202 * Find next free circuit ID.
204 static unsigned short nr_find_next_circuit(void)
206 unsigned short id = circuit;
214 if (i != 0 && j != 0) {
215 if ((sk=nr_find_socket(i, j)) == NULL)
229 void nr_destroy_socket(struct sock *);
232 * Handler for deferred kills.
234 static void nr_destroy_timer(unsigned long data)
236 struct sock *sk=(struct sock *)data;
239 nr_destroy_socket(sk);
245 * This is called from user mode and the timers. Thus it protects itself
246 * against interrupt users but doesn't worry about being called during
247 * work. Once it is removed from the queue no interrupt or bottom half
248 * will touch it and we are (fairly 8-) ) safe.
250 void nr_destroy_socket(struct sock *sk)
254 nr_remove_socket(sk);
256 nr_stop_heartbeat(sk);
260 nr_stop_idletimer(sk);
262 nr_clear_queues(sk); /* Flush the queues */
264 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
265 if (skb->sk != sk) { /* A pending connection */
266 /* Queue the unaccepted socket for death */
267 sock_set_flag(skb->sk, SOCK_DEAD);
268 nr_start_heartbeat(skb->sk);
269 nr_sk(skb->sk)->state = NR_STATE_0;
275 if (atomic_read(&sk->sk_wmem_alloc) ||
276 atomic_read(&sk->sk_rmem_alloc)) {
277 /* Defer: outstanding buffers */
278 sk->sk_timer.function = nr_destroy_timer;
279 sk->sk_timer.expires = jiffies + 2 * HZ;
280 add_timer(&sk->sk_timer);
286 * Handling for system calls applied via the various interfaces to a
287 * NET/ROM socket object.
290 static int nr_setsockopt(struct socket *sock, int level, int optname,
291 char __user *optval, int optlen)
293 struct sock *sk = sock->sk;
294 struct nr_sock *nr = nr_sk(sk);
297 if (level != SOL_NETROM)
300 if (optlen < sizeof(int))
303 if (get_user(opt, (int __user *)optval))
320 if (opt < 1 || opt > 31)
334 nr->idle = opt * 60 * HZ;
342 static int nr_getsockopt(struct socket *sock, int level, int optname,
343 char __user *optval, int __user *optlen)
345 struct sock *sk = sock->sk;
346 struct nr_sock *nr = nr_sk(sk);
350 if (level != SOL_NETROM)
353 if (get_user(len, optlen))
377 val = nr->idle / (60 * HZ);
384 len = min_t(unsigned int, len, sizeof(int));
386 if (put_user(len, optlen))
389 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
392 static int nr_listen(struct socket *sock, int backlog)
394 struct sock *sk = sock->sk;
397 if (sk->sk_state != TCP_LISTEN) {
398 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
399 sk->sk_max_ack_backlog = backlog;
400 sk->sk_state = TCP_LISTEN;
409 static struct proto nr_proto = {
411 .owner = THIS_MODULE,
412 .obj_size = sizeof(struct nr_sock),
415 static int nr_create(struct net *net, struct socket *sock, int protocol)
420 if (net != &init_net)
421 return -EAFNOSUPPORT;
423 if (sock->type != SOCK_SEQPACKET || protocol != 0)
424 return -ESOCKTNOSUPPORT;
426 sk = sk_alloc(net, PF_NETROM, GFP_ATOMIC, &nr_proto);
432 sock_init_data(sock, sk);
434 sock->ops = &nr_proto_ops;
435 sk->sk_protocol = protocol;
437 skb_queue_head_init(&nr->ack_queue);
438 skb_queue_head_init(&nr->reseq_queue);
439 skb_queue_head_init(&nr->frag_queue);
444 msecs_to_jiffies(sysctl_netrom_transport_timeout);
446 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
448 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
450 msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
452 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
453 nr->window = sysctl_netrom_transport_requested_window_size;
456 nr->state = NR_STATE_0;
461 static struct sock *nr_make_new(struct sock *osk)
464 struct nr_sock *nr, *onr;
466 if (osk->sk_type != SOCK_SEQPACKET)
469 sk = sk_alloc(sock_net(osk), PF_NETROM, GFP_ATOMIC, osk->sk_prot);
475 sock_init_data(NULL, sk);
477 sk->sk_type = osk->sk_type;
478 sk->sk_priority = osk->sk_priority;
479 sk->sk_protocol = osk->sk_protocol;
480 sk->sk_rcvbuf = osk->sk_rcvbuf;
481 sk->sk_sndbuf = osk->sk_sndbuf;
482 sk->sk_state = TCP_ESTABLISHED;
483 sock_copy_flags(sk, osk);
485 skb_queue_head_init(&nr->ack_queue);
486 skb_queue_head_init(&nr->reseq_queue);
487 skb_queue_head_init(&nr->frag_queue);
497 nr->idle = onr->idle;
498 nr->window = onr->window;
500 nr->device = onr->device;
501 nr->bpqext = onr->bpqext;
506 static int nr_release(struct socket *sock)
508 struct sock *sk = sock->sk;
511 if (sk == NULL) return 0;
521 nr_disconnect(sk, 0);
522 nr_destroy_socket(sk);
528 nr_write_internal(sk, NR_DISCREQ);
529 nr_start_t1timer(sk);
532 nr_stop_idletimer(sk);
533 nr->state = NR_STATE_2;
534 sk->sk_state = TCP_CLOSE;
535 sk->sk_shutdown |= SEND_SHUTDOWN;
536 sk->sk_state_change(sk);
538 sock_set_flag(sk, SOCK_DESTROY);
552 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
554 struct sock *sk = sock->sk;
555 struct nr_sock *nr = nr_sk(sk);
556 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
557 struct net_device *dev;
558 ax25_uid_assoc *user;
559 ax25_address *source;
562 if (!sock_flag(sk, SOCK_ZAPPED)) {
566 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
570 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
574 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
578 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
579 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
581 return -EADDRNOTAVAIL;
585 * Only the super user can set an arbitrary user callsign.
587 if (addr->fsa_ax25.sax25_ndigis == 1) {
588 if (!capable(CAP_NET_BIND_SERVICE)) {
593 nr->user_addr = addr->fsa_digipeater[0];
594 nr->source_addr = addr->fsa_ax25.sax25_call;
596 source = &addr->fsa_ax25.sax25_call;
598 user = ax25_findbyuid(current->euid);
600 nr->user_addr = user->call;
603 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
608 nr->user_addr = *source;
611 nr->source_addr = *source;
615 nr_insert_socket(sk);
617 sock_reset_flag(sk, SOCK_ZAPPED);
620 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
624 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
625 int addr_len, int flags)
627 struct sock *sk = sock->sk;
628 struct nr_sock *nr = nr_sk(sk);
629 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
630 ax25_address *source = NULL;
631 ax25_uid_assoc *user;
632 struct net_device *dev;
636 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
637 sock->state = SS_CONNECTED;
638 goto out_release; /* Connect completed during a ERESTARTSYS event */
641 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
642 sock->state = SS_UNCONNECTED;
647 if (sk->sk_state == TCP_ESTABLISHED) {
648 err = -EISCONN; /* No reconnect on a seqpacket socket */
652 sk->sk_state = TCP_CLOSE;
653 sock->state = SS_UNCONNECTED;
655 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
659 if (addr->sax25_family != AF_NETROM) {
663 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
664 sock_reset_flag(sk, SOCK_ZAPPED);
666 if ((dev = nr_dev_first()) == NULL) {
670 source = (ax25_address *)dev->dev_addr;
672 user = ax25_findbyuid(current->euid);
674 nr->user_addr = user->call;
677 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
682 nr->user_addr = *source;
685 nr->source_addr = *source;
689 nr_insert_socket(sk); /* Finish the bind */
692 nr->dest_addr = addr->sax25_call;
695 circuit = nr_find_next_circuit();
698 nr->my_index = circuit / 256;
699 nr->my_id = circuit % 256;
703 /* Move to connecting socket, start sending Connect Requests */
704 sock->state = SS_CONNECTING;
705 sk->sk_state = TCP_SYN_SENT;
707 nr_establish_data_link(sk);
709 nr->state = NR_STATE_1;
711 nr_start_heartbeat(sk);
714 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
720 * A Connect Ack with Choke or timeout or failed routing will go to
723 if (sk->sk_state == TCP_SYN_SENT) {
727 prepare_to_wait(sk->sk_sleep, &wait,
729 if (sk->sk_state != TCP_SYN_SENT)
731 if (!signal_pending(current)) {
740 finish_wait(sk->sk_sleep, &wait);
745 if (sk->sk_state != TCP_ESTABLISHED) {
746 sock->state = SS_UNCONNECTED;
747 err = sock_error(sk); /* Always set at this point */
751 sock->state = SS_CONNECTED;
759 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
767 if ((sk = sock->sk) == NULL)
771 if (sk->sk_type != SOCK_SEQPACKET) {
776 if (sk->sk_state != TCP_LISTEN) {
782 * The write queue this time is holding sockets ready to use
783 * hooked into the SABM we saved
786 prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
787 skb = skb_dequeue(&sk->sk_receive_queue);
791 if (flags & O_NONBLOCK) {
795 if (!signal_pending(current)) {
804 finish_wait(sk->sk_sleep, &wait);
809 sock_graft(newsk, newsock);
811 /* Now attach up the new socket */
813 sk_acceptq_removed(sk);
821 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
822 int *uaddr_len, int peer)
824 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
825 struct sock *sk = sock->sk;
826 struct nr_sock *nr = nr_sk(sk);
830 if (sk->sk_state != TCP_ESTABLISHED) {
834 sax->fsa_ax25.sax25_family = AF_NETROM;
835 sax->fsa_ax25.sax25_ndigis = 1;
836 sax->fsa_ax25.sax25_call = nr->user_addr;
837 sax->fsa_digipeater[0] = nr->dest_addr;
838 *uaddr_len = sizeof(struct full_sockaddr_ax25);
840 sax->fsa_ax25.sax25_family = AF_NETROM;
841 sax->fsa_ax25.sax25_ndigis = 0;
842 sax->fsa_ax25.sax25_call = nr->source_addr;
843 *uaddr_len = sizeof(struct sockaddr_ax25);
850 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
854 struct nr_sock *nr_make;
855 ax25_address *src, *dest, *user;
856 unsigned short circuit_index, circuit_id;
857 unsigned short peer_circuit_index, peer_circuit_id;
858 unsigned short frametype, flags, window, timeout;
861 skb->sk = NULL; /* Initially we don't know who it's for */
864 * skb->data points to the netrom frame start
867 src = (ax25_address *)(skb->data + 0);
868 dest = (ax25_address *)(skb->data + 7);
870 circuit_index = skb->data[15];
871 circuit_id = skb->data[16];
872 peer_circuit_index = skb->data[17];
873 peer_circuit_id = skb->data[18];
874 frametype = skb->data[19] & 0x0F;
875 flags = skb->data[19] & 0xF0;
878 * Check for an incoming IP over NET/ROM frame.
880 if (frametype == NR_PROTOEXT &&
881 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
882 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
883 skb_reset_transport_header(skb);
885 return nr_rx_ip(skb, dev);
889 * Find an existing socket connection, based on circuit ID, if it's
890 * a Connect Request base it on their circuit ID.
892 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
893 * circuit that no longer exists at the other end ...
898 if (circuit_index == 0 && circuit_id == 0) {
899 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
900 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
902 if (frametype == NR_CONNREQ)
903 sk = nr_find_peer(circuit_index, circuit_id, src);
905 sk = nr_find_socket(circuit_index, circuit_id);
909 skb_reset_transport_header(skb);
911 if (frametype == NR_CONNACK && skb->len == 22)
912 nr_sk(sk)->bpqext = 1;
914 nr_sk(sk)->bpqext = 0;
916 ret = nr_process_rx_frame(sk, skb);
922 * Now it should be a CONNREQ.
924 if (frametype != NR_CONNREQ) {
926 * Here it would be nice to be able to send a reset but
927 * NET/ROM doesn't have one. We've tried to extend the protocol
928 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
929 * apparently kills BPQ boxes... :-(
930 * So now we try to follow the established behaviour of
931 * G8PZT's Xrouter which is sending packets with command type 7
932 * as an extension of the protocol.
934 if (sysctl_netrom_reset_circuit &&
935 (frametype != NR_RESET || flags != 0))
936 nr_transmit_reset(skb, 1);
941 sk = nr_find_listener(dest);
943 user = (ax25_address *)(skb->data + 21);
945 if (sk == NULL || sk_acceptq_is_full(sk) ||
946 (make = nr_make_new(sk)) == NULL) {
947 nr_transmit_refusal(skb, 0);
953 window = skb->data[20];
956 make->sk_state = TCP_ESTABLISHED;
958 /* Fill in his circuit details */
959 nr_make = nr_sk(make);
960 nr_make->source_addr = *dest;
961 nr_make->dest_addr = *src;
962 nr_make->user_addr = *user;
964 nr_make->your_index = circuit_index;
965 nr_make->your_id = circuit_id;
968 circuit = nr_find_next_circuit();
971 nr_make->my_index = circuit / 256;
972 nr_make->my_id = circuit % 256;
976 /* Window negotiation */
977 if (window < nr_make->window)
978 nr_make->window = window;
980 /* L4 timeout negotiation */
981 if (skb->len == 37) {
982 timeout = skb->data[36] * 256 + skb->data[35];
983 if (timeout * HZ < nr_make->t1)
984 nr_make->t1 = timeout * HZ;
990 nr_write_internal(make, NR_CONNACK);
992 nr_make->condition = 0x00;
997 nr_make->state = NR_STATE_3;
998 sk_acceptq_added(sk);
999 skb_queue_head(&sk->sk_receive_queue, skb);
1001 if (!sock_flag(sk, SOCK_DEAD))
1002 sk->sk_data_ready(sk, skb->len);
1006 nr_insert_socket(make);
1008 nr_start_heartbeat(make);
1009 nr_start_idletimer(make);
1014 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1015 struct msghdr *msg, size_t len)
1017 struct sock *sk = sock->sk;
1018 struct nr_sock *nr = nr_sk(sk);
1019 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1021 struct sockaddr_ax25 sax;
1022 struct sk_buff *skb;
1023 unsigned char *asmptr;
1026 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1030 if (sock_flag(sk, SOCK_ZAPPED)) {
1031 err = -EADDRNOTAVAIL;
1035 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1036 send_sig(SIGPIPE, current, 0);
1041 if (nr->device == NULL) {
1047 if (msg->msg_namelen < sizeof(sax)) {
1052 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1056 if (sax.sax25_family != AF_NETROM) {
1061 if (sk->sk_state != TCP_ESTABLISHED) {
1065 sax.sax25_family = AF_NETROM;
1066 sax.sax25_call = nr->dest_addr;
1069 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1071 /* Build a packet */
1072 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1073 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1075 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1078 skb_reserve(skb, size - len);
1079 skb_reset_transport_header(skb);
1082 * Push down the NET/ROM header
1085 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1086 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1088 /* Build a NET/ROM Transport header */
1090 *asmptr++ = nr->your_index;
1091 *asmptr++ = nr->your_id;
1092 *asmptr++ = 0; /* To be filled in later */
1093 *asmptr++ = 0; /* Ditto */
1094 *asmptr++ = NR_INFO;
1095 SOCK_DEBUG(sk, "Built header.\n");
1098 * Put the data on the end
1102 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1104 /* User data follows immediately after the NET/ROM transport header */
1105 if (memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len)) {
1111 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1113 if (sk->sk_state != TCP_ESTABLISHED) {
1119 nr_output(sk, skb); /* Shove it onto the queue */
1127 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1128 struct msghdr *msg, size_t size, int flags)
1130 struct sock *sk = sock->sk;
1131 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1133 struct sk_buff *skb;
1137 * This works for seqpacket too. The receiver has ordered the queue for
1138 * us! We do one quick check first though
1142 if (sk->sk_state != TCP_ESTABLISHED) {
1147 /* Now we can treat all alike */
1148 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1153 skb_reset_transport_header(skb);
1156 if (copied > size) {
1158 msg->msg_flags |= MSG_TRUNC;
1161 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1164 sax->sax25_family = AF_NETROM;
1165 skb_copy_from_linear_data_offset(skb, 7, sax->sax25_call.ax25_call,
1169 msg->msg_namelen = sizeof(*sax);
1171 skb_free_datagram(sk, skb);
1178 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1180 struct sock *sk = sock->sk;
1181 void __user *argp = (void __user *)arg;
1189 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1193 return put_user(amount, (int __user *)argp);
1197 struct sk_buff *skb;
1201 /* These two are safe on a single CPU system as only user tasks fiddle here */
1202 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1205 return put_user(amount, (int __user *)argp);
1210 ret = sock_get_timestamp(sk, argp);
1216 ret = sock_get_timestampns(sk, argp);
1222 case SIOCGIFDSTADDR:
1223 case SIOCSIFDSTADDR:
1224 case SIOCGIFBRDADDR:
1225 case SIOCSIFBRDADDR:
1226 case SIOCGIFNETMASK:
1227 case SIOCSIFNETMASK:
1235 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1236 return nr_rt_ioctl(cmd, argp);
1239 return -ENOIOCTLCMD;
1245 #ifdef CONFIG_PROC_FS
1247 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1250 struct hlist_node *node;
1253 spin_lock_bh(&nr_list_lock);
1255 return SEQ_START_TOKEN;
1257 sk_for_each(s, node, &nr_list) {
1265 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1269 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
1270 : sk_next((struct sock *)v);
1273 static void nr_info_stop(struct seq_file *seq, void *v)
1275 spin_unlock_bh(&nr_list_lock);
1278 static int nr_info_show(struct seq_file *seq, void *v)
1281 struct net_device *dev;
1283 const char *devname;
1286 if (v == SEQ_START_TOKEN)
1288 "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");
1295 if ((dev = nr->device) == NULL)
1298 devname = dev->name;
1300 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1301 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
1303 "%-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",
1304 ax2asc(buf, &nr->source_addr),
1314 ax25_display_timer(&nr->t1timer) / HZ,
1316 ax25_display_timer(&nr->t2timer) / HZ,
1318 ax25_display_timer(&nr->t4timer) / HZ,
1320 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1321 nr->idle / (60 * HZ),
1325 atomic_read(&s->sk_wmem_alloc),
1326 atomic_read(&s->sk_rmem_alloc),
1327 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1334 static const struct seq_operations nr_info_seqops = {
1335 .start = nr_info_start,
1336 .next = nr_info_next,
1337 .stop = nr_info_stop,
1338 .show = nr_info_show,
1341 static int nr_info_open(struct inode *inode, struct file *file)
1343 return seq_open(file, &nr_info_seqops);
1346 static const struct file_operations nr_info_fops = {
1347 .owner = THIS_MODULE,
1348 .open = nr_info_open,
1350 .llseek = seq_lseek,
1351 .release = seq_release,
1353 #endif /* CONFIG_PROC_FS */
1355 static struct net_proto_family nr_family_ops = {
1356 .family = PF_NETROM,
1357 .create = nr_create,
1358 .owner = THIS_MODULE,
1361 static const struct proto_ops nr_proto_ops = {
1362 .family = PF_NETROM,
1363 .owner = THIS_MODULE,
1364 .release = nr_release,
1366 .connect = nr_connect,
1367 .socketpair = sock_no_socketpair,
1368 .accept = nr_accept,
1369 .getname = nr_getname,
1370 .poll = datagram_poll,
1372 .listen = nr_listen,
1373 .shutdown = sock_no_shutdown,
1374 .setsockopt = nr_setsockopt,
1375 .getsockopt = nr_getsockopt,
1376 .sendmsg = nr_sendmsg,
1377 .recvmsg = nr_recvmsg,
1378 .mmap = sock_no_mmap,
1379 .sendpage = sock_no_sendpage,
1382 static struct notifier_block nr_dev_notifier = {
1383 .notifier_call = nr_device_event,
1386 static struct net_device **dev_nr;
1388 static struct ax25_protocol nr_pid = {
1389 .pid = AX25_P_NETROM,
1390 .func = nr_route_frame
1393 static struct ax25_linkfail nr_linkfail_notifier = {
1394 .func = nr_link_failed,
1397 static int __init nr_proto_init(void)
1400 int rc = proto_register(&nr_proto, 0);
1405 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1406 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1410 dev_nr = kzalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1411 if (dev_nr == NULL) {
1412 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1416 for (i = 0; i < nr_ndevs; i++) {
1417 char name[IFNAMSIZ];
1418 struct net_device *dev;
1420 sprintf(name, "nr%d", i);
1421 dev = alloc_netdev(sizeof(struct nr_private), name, nr_setup);
1423 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1428 if (register_netdev(dev)) {
1429 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1433 lockdep_set_class(&dev->_xmit_lock, &nr_netdev_xmit_lock_key);
1437 if (sock_register(&nr_family_ops)) {
1438 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1442 register_netdevice_notifier(&nr_dev_notifier);
1444 ax25_register_pid(&nr_pid);
1445 ax25_linkfail_register(&nr_linkfail_notifier);
1447 #ifdef CONFIG_SYSCTL
1448 nr_register_sysctl();
1453 proc_net_fops_create(&init_net, "nr", S_IRUGO, &nr_info_fops);
1454 proc_net_fops_create(&init_net, "nr_neigh", S_IRUGO, &nr_neigh_fops);
1455 proc_net_fops_create(&init_net, "nr_nodes", S_IRUGO, &nr_nodes_fops);
1460 unregister_netdev(dev_nr[i]);
1461 free_netdev(dev_nr[i]);
1464 proto_unregister(&nr_proto);
1469 module_init(nr_proto_init);
1471 module_param(nr_ndevs, int, 0);
1472 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1474 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1475 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1476 MODULE_LICENSE("GPL");
1477 MODULE_ALIAS_NETPROTO(PF_NETROM);
1479 static void __exit nr_exit(void)
1483 proc_net_remove(&init_net, "nr");
1484 proc_net_remove(&init_net, "nr_neigh");
1485 proc_net_remove(&init_net, "nr_nodes");
1486 nr_loopback_clear();
1490 #ifdef CONFIG_SYSCTL
1491 nr_unregister_sysctl();
1494 ax25_linkfail_release(&nr_linkfail_notifier);
1495 ax25_protocol_release(AX25_P_NETROM);
1497 unregister_netdevice_notifier(&nr_dev_notifier);
1499 sock_unregister(PF_NETROM);
1501 for (i = 0; i < nr_ndevs; i++) {
1502 struct net_device *dev = dev_nr[i];
1504 unregister_netdev(dev);
1510 proto_unregister(&nr_proto);
1512 module_exit(nr_exit);