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;
59 int sysctl_netrom_reset_circuit = NR_DEFAULT_RESET;
61 static unsigned short circuit = 0x101;
63 static HLIST_HEAD(nr_list);
64 static DEFINE_SPINLOCK(nr_list_lock);
66 static const struct proto_ops nr_proto_ops;
69 * Socket removal during an interrupt is now safe.
71 static void nr_remove_socket(struct sock *sk)
73 spin_lock_bh(&nr_list_lock);
75 spin_unlock_bh(&nr_list_lock);
79 * Kill all bound sockets on a dropped device.
81 static void nr_kill_by_device(struct net_device *dev)
84 struct hlist_node *node;
86 spin_lock_bh(&nr_list_lock);
87 sk_for_each(s, node, &nr_list)
88 if (nr_sk(s)->device == dev)
89 nr_disconnect(s, ENETUNREACH);
90 spin_unlock_bh(&nr_list_lock);
94 * Handle device status changes.
96 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
98 struct net_device *dev = (struct net_device *)ptr;
100 if (event != NETDEV_DOWN)
103 nr_kill_by_device(dev);
104 nr_rt_device_down(dev);
110 * Add a socket to the bound sockets list.
112 static void nr_insert_socket(struct sock *sk)
114 spin_lock_bh(&nr_list_lock);
115 sk_add_node(sk, &nr_list);
116 spin_unlock_bh(&nr_list_lock);
120 * Find a socket that wants to accept the Connect Request we just
123 static struct sock *nr_find_listener(ax25_address *addr)
126 struct hlist_node *node;
128 spin_lock_bh(&nr_list_lock);
129 sk_for_each(s, node, &nr_list)
130 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
131 s->sk_state == TCP_LISTEN) {
137 spin_unlock_bh(&nr_list_lock);
142 * Find a connected NET/ROM socket given my circuit IDs.
144 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
147 struct hlist_node *node;
149 spin_lock_bh(&nr_list_lock);
150 sk_for_each(s, node, &nr_list) {
151 struct nr_sock *nr = nr_sk(s);
153 if (nr->my_index == index && nr->my_id == id) {
160 spin_unlock_bh(&nr_list_lock);
165 * Find a connected NET/ROM socket given their circuit IDs.
167 static struct sock *nr_find_peer(unsigned char index, unsigned char id,
171 struct hlist_node *node;
173 spin_lock_bh(&nr_list_lock);
174 sk_for_each(s, node, &nr_list) {
175 struct nr_sock *nr = nr_sk(s);
177 if (nr->your_index == index && nr->your_id == id &&
178 !ax25cmp(&nr->dest_addr, dest)) {
185 spin_unlock_bh(&nr_list_lock);
190 * Find next free circuit ID.
192 static unsigned short nr_find_next_circuit(void)
194 unsigned short id = circuit;
202 if (i != 0 && j != 0) {
203 if ((sk=nr_find_socket(i, j)) == NULL)
217 void nr_destroy_socket(struct sock *);
220 * Handler for deferred kills.
222 static void nr_destroy_timer(unsigned long data)
224 struct sock *sk=(struct sock *)data;
227 nr_destroy_socket(sk);
233 * This is called from user mode and the timers. Thus it protects itself
234 * against interrupt users but doesn't worry about being called during
235 * work. Once it is removed from the queue no interrupt or bottom half
236 * will touch it and we are (fairly 8-) ) safe.
238 void nr_destroy_socket(struct sock *sk)
242 nr_remove_socket(sk);
244 nr_stop_heartbeat(sk);
248 nr_stop_idletimer(sk);
250 nr_clear_queues(sk); /* Flush the queues */
252 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
253 if (skb->sk != sk) { /* A pending connection */
254 /* Queue the unaccepted socket for death */
255 sock_set_flag(skb->sk, SOCK_DEAD);
256 nr_start_heartbeat(skb->sk);
257 nr_sk(skb->sk)->state = NR_STATE_0;
263 if (atomic_read(&sk->sk_wmem_alloc) ||
264 atomic_read(&sk->sk_rmem_alloc)) {
265 /* Defer: outstanding buffers */
266 sk->sk_timer.function = nr_destroy_timer;
267 sk->sk_timer.expires = jiffies + 2 * HZ;
268 add_timer(&sk->sk_timer);
274 * Handling for system calls applied via the various interfaces to a
275 * NET/ROM socket object.
278 static int nr_setsockopt(struct socket *sock, int level, int optname,
279 char __user *optval, int optlen)
281 struct sock *sk = sock->sk;
282 struct nr_sock *nr = nr_sk(sk);
285 if (level != SOL_NETROM)
288 if (optlen < sizeof(int))
291 if (get_user(opt, (int __user *)optval))
308 if (opt < 1 || opt > 31)
322 nr->idle = opt * 60 * HZ;
330 static int nr_getsockopt(struct socket *sock, int level, int optname,
331 char __user *optval, int __user *optlen)
333 struct sock *sk = sock->sk;
334 struct nr_sock *nr = nr_sk(sk);
338 if (level != SOL_NETROM)
341 if (get_user(len, optlen))
365 val = nr->idle / (60 * HZ);
372 len = min_t(unsigned int, len, sizeof(int));
374 if (put_user(len, optlen))
377 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
380 static int nr_listen(struct socket *sock, int backlog)
382 struct sock *sk = sock->sk;
385 if (sk->sk_state != TCP_LISTEN) {
386 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
387 sk->sk_max_ack_backlog = backlog;
388 sk->sk_state = TCP_LISTEN;
397 static struct proto nr_proto = {
399 .owner = THIS_MODULE,
400 .obj_size = sizeof(struct nr_sock),
403 static int nr_create(struct socket *sock, int protocol)
408 if (sock->type != SOCK_SEQPACKET || protocol != 0)
409 return -ESOCKTNOSUPPORT;
411 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, &nr_proto, 1)) == NULL)
416 sock_init_data(sock, sk);
418 sock->ops = &nr_proto_ops;
419 sk->sk_protocol = protocol;
421 skb_queue_head_init(&nr->ack_queue);
422 skb_queue_head_init(&nr->reseq_queue);
423 skb_queue_head_init(&nr->frag_queue);
427 nr->t1 = sysctl_netrom_transport_timeout;
428 nr->t2 = sysctl_netrom_transport_acknowledge_delay;
429 nr->n2 = sysctl_netrom_transport_maximum_tries;
430 nr->t4 = sysctl_netrom_transport_busy_delay;
431 nr->idle = sysctl_netrom_transport_no_activity_timeout;
432 nr->window = sysctl_netrom_transport_requested_window_size;
435 nr->state = NR_STATE_0;
440 static struct sock *nr_make_new(struct sock *osk)
443 struct nr_sock *nr, *onr;
445 if (osk->sk_type != SOCK_SEQPACKET)
448 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, osk->sk_prot, 1)) == NULL)
453 sock_init_data(NULL, sk);
455 sk->sk_type = osk->sk_type;
456 sk->sk_socket = osk->sk_socket;
457 sk->sk_priority = osk->sk_priority;
458 sk->sk_protocol = osk->sk_protocol;
459 sk->sk_rcvbuf = osk->sk_rcvbuf;
460 sk->sk_sndbuf = osk->sk_sndbuf;
461 sk->sk_state = TCP_ESTABLISHED;
462 sk->sk_sleep = osk->sk_sleep;
463 sock_copy_flags(sk, osk);
465 skb_queue_head_init(&nr->ack_queue);
466 skb_queue_head_init(&nr->reseq_queue);
467 skb_queue_head_init(&nr->frag_queue);
477 nr->idle = onr->idle;
478 nr->window = onr->window;
480 nr->device = onr->device;
481 nr->bpqext = onr->bpqext;
486 static int nr_release(struct socket *sock)
488 struct sock *sk = sock->sk;
491 if (sk == NULL) return 0;
501 nr_disconnect(sk, 0);
502 nr_destroy_socket(sk);
508 nr_write_internal(sk, NR_DISCREQ);
509 nr_start_t1timer(sk);
512 nr_stop_idletimer(sk);
513 nr->state = NR_STATE_2;
514 sk->sk_state = TCP_CLOSE;
515 sk->sk_shutdown |= SEND_SHUTDOWN;
516 sk->sk_state_change(sk);
518 sock_set_flag(sk, SOCK_DESTROY);
519 sk->sk_socket = NULL;
523 sk->sk_socket = NULL;
534 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
536 struct sock *sk = sock->sk;
537 struct nr_sock *nr = nr_sk(sk);
538 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
539 struct net_device *dev;
540 ax25_uid_assoc *user;
541 ax25_address *source;
544 if (!sock_flag(sk, SOCK_ZAPPED)) {
548 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
552 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
556 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
560 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
561 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
563 return -EADDRNOTAVAIL;
567 * Only the super user can set an arbitrary user callsign.
569 if (addr->fsa_ax25.sax25_ndigis == 1) {
570 if (!capable(CAP_NET_BIND_SERVICE)) {
575 nr->user_addr = addr->fsa_digipeater[0];
576 nr->source_addr = addr->fsa_ax25.sax25_call;
578 source = &addr->fsa_ax25.sax25_call;
580 user = ax25_findbyuid(current->euid);
582 nr->user_addr = user->call;
585 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
590 nr->user_addr = *source;
593 nr->source_addr = *source;
597 nr_insert_socket(sk);
599 sock_reset_flag(sk, SOCK_ZAPPED);
602 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
606 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
607 int addr_len, int flags)
609 struct sock *sk = sock->sk;
610 struct nr_sock *nr = nr_sk(sk);
611 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
612 ax25_address *source = NULL;
613 ax25_uid_assoc *user;
614 struct net_device *dev;
617 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
618 sock->state = SS_CONNECTED;
620 return 0; /* Connect completed during a ERESTARTSYS event */
623 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
624 sock->state = SS_UNCONNECTED;
626 return -ECONNREFUSED;
629 if (sk->sk_state == TCP_ESTABLISHED) {
631 return -EISCONN; /* No reconnect on a seqpacket socket */
634 sk->sk_state = TCP_CLOSE;
635 sock->state = SS_UNCONNECTED;
637 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
641 if (addr->sax25_family != AF_NETROM) {
645 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
646 sock_reset_flag(sk, SOCK_ZAPPED);
648 if ((dev = nr_dev_first()) == NULL) {
652 source = (ax25_address *)dev->dev_addr;
654 user = ax25_findbyuid(current->euid);
656 nr->user_addr = user->call;
659 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
664 nr->user_addr = *source;
667 nr->source_addr = *source;
671 nr_insert_socket(sk); /* Finish the bind */
674 nr->dest_addr = addr->sax25_call;
677 circuit = nr_find_next_circuit();
680 nr->my_index = circuit / 256;
681 nr->my_id = circuit % 256;
685 /* Move to connecting socket, start sending Connect Requests */
686 sock->state = SS_CONNECTING;
687 sk->sk_state = TCP_SYN_SENT;
689 nr_establish_data_link(sk);
691 nr->state = NR_STATE_1;
693 nr_start_heartbeat(sk);
696 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
702 * A Connect Ack with Choke or timeout or failed routing will go to
705 if (sk->sk_state == TCP_SYN_SENT) {
706 struct task_struct *tsk = current;
707 DECLARE_WAITQUEUE(wait, tsk);
709 add_wait_queue(sk->sk_sleep, &wait);
711 set_current_state(TASK_INTERRUPTIBLE);
712 if (sk->sk_state != TCP_SYN_SENT)
715 if (!signal_pending(tsk)) {
720 current->state = TASK_RUNNING;
721 remove_wait_queue(sk->sk_sleep, &wait);
724 current->state = TASK_RUNNING;
725 remove_wait_queue(sk->sk_sleep, &wait);
728 if (sk->sk_state != TCP_ESTABLISHED) {
729 sock->state = SS_UNCONNECTED;
731 return sock_error(sk); /* Always set at this point */
734 sock->state = SS_CONNECTED;
740 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
742 struct task_struct *tsk = current;
743 DECLARE_WAITQUEUE(wait, tsk);
749 if ((sk = sock->sk) == NULL)
753 if (sk->sk_type != SOCK_SEQPACKET) {
758 if (sk->sk_state != TCP_LISTEN) {
764 * The write queue this time is holding sockets ready to use
765 * hooked into the SABM we saved
767 add_wait_queue(sk->sk_sleep, &wait);
769 skb = skb_dequeue(&sk->sk_receive_queue);
773 current->state = TASK_INTERRUPTIBLE;
775 if (flags & O_NONBLOCK) {
776 current->state = TASK_RUNNING;
777 remove_wait_queue(sk->sk_sleep, &wait);
780 if (!signal_pending(tsk)) {
785 current->state = TASK_RUNNING;
786 remove_wait_queue(sk->sk_sleep, &wait);
789 current->state = TASK_RUNNING;
790 remove_wait_queue(sk->sk_sleep, &wait);
793 newsk->sk_socket = newsock;
794 newsk->sk_sleep = &newsock->wait;
796 /* Now attach up the new socket */
798 sk->sk_ack_backlog--;
806 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
807 int *uaddr_len, int peer)
809 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
810 struct sock *sk = sock->sk;
811 struct nr_sock *nr = nr_sk(sk);
815 if (sk->sk_state != TCP_ESTABLISHED) {
819 sax->fsa_ax25.sax25_family = AF_NETROM;
820 sax->fsa_ax25.sax25_ndigis = 1;
821 sax->fsa_ax25.sax25_call = nr->user_addr;
822 sax->fsa_digipeater[0] = nr->dest_addr;
823 *uaddr_len = sizeof(struct full_sockaddr_ax25);
825 sax->fsa_ax25.sax25_family = AF_NETROM;
826 sax->fsa_ax25.sax25_ndigis = 0;
827 sax->fsa_ax25.sax25_call = nr->source_addr;
828 *uaddr_len = sizeof(struct sockaddr_ax25);
835 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
839 struct nr_sock *nr_make;
840 ax25_address *src, *dest, *user;
841 unsigned short circuit_index, circuit_id;
842 unsigned short peer_circuit_index, peer_circuit_id;
843 unsigned short frametype, flags, window, timeout;
846 skb->sk = NULL; /* Initially we don't know who it's for */
849 * skb->data points to the netrom frame start
852 src = (ax25_address *)(skb->data + 0);
853 dest = (ax25_address *)(skb->data + 7);
855 circuit_index = skb->data[15];
856 circuit_id = skb->data[16];
857 peer_circuit_index = skb->data[17];
858 peer_circuit_id = skb->data[18];
859 frametype = skb->data[19] & 0x0F;
860 flags = skb->data[19] & 0xF0;
863 * Check for an incoming IP over NET/ROM frame.
865 if (frametype == NR_PROTOEXT &&
866 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
867 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
868 skb->h.raw = skb->data;
870 return nr_rx_ip(skb, dev);
874 * Find an existing socket connection, based on circuit ID, if it's
875 * a Connect Request base it on their circuit ID.
877 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
878 * circuit that no longer exists at the other end ...
883 if (circuit_index == 0 && circuit_id == 0) {
884 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
885 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
887 if (frametype == NR_CONNREQ)
888 sk = nr_find_peer(circuit_index, circuit_id, src);
890 sk = nr_find_socket(circuit_index, circuit_id);
894 skb->h.raw = skb->data;
896 if (frametype == NR_CONNACK && skb->len == 22)
897 nr_sk(sk)->bpqext = 1;
899 nr_sk(sk)->bpqext = 0;
901 ret = nr_process_rx_frame(sk, skb);
907 * Now it should be a CONNREQ.
909 if (frametype != NR_CONNREQ) {
911 * Here it would be nice to be able to send a reset but
912 * NET/ROM doesn't have one. We've tried to extend the protocol
913 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
914 * apparently kills BPQ boxes... :-(
915 * So now we try to follow the established behaviour of
916 * G8PZT's Xrouter which is sending packets with command type 7
917 * as an extension of the protocol.
919 if (sysctl_netrom_reset_circuit &&
920 (frametype != NR_RESET || flags != 0))
921 nr_transmit_reset(skb, 1);
926 sk = nr_find_listener(dest);
928 user = (ax25_address *)(skb->data + 21);
930 if (sk == NULL || sk_acceptq_is_full(sk) ||
931 (make = nr_make_new(sk)) == NULL) {
932 nr_transmit_refusal(skb, 0);
938 window = skb->data[20];
941 make->sk_state = TCP_ESTABLISHED;
943 /* Fill in his circuit details */
944 nr_make = nr_sk(make);
945 nr_make->source_addr = *dest;
946 nr_make->dest_addr = *src;
947 nr_make->user_addr = *user;
949 nr_make->your_index = circuit_index;
950 nr_make->your_id = circuit_id;
953 circuit = nr_find_next_circuit();
956 nr_make->my_index = circuit / 256;
957 nr_make->my_id = circuit % 256;
961 /* Window negotiation */
962 if (window < nr_make->window)
963 nr_make->window = window;
965 /* L4 timeout negotiation */
966 if (skb->len == 37) {
967 timeout = skb->data[36] * 256 + skb->data[35];
968 if (timeout * HZ < nr_make->t1)
969 nr_make->t1 = timeout * HZ;
975 nr_write_internal(make, NR_CONNACK);
977 nr_make->condition = 0x00;
982 nr_make->state = NR_STATE_3;
983 sk->sk_ack_backlog++;
985 nr_insert_socket(make);
987 skb_queue_head(&sk->sk_receive_queue, skb);
989 nr_start_heartbeat(make);
990 nr_start_idletimer(make);
992 if (!sock_flag(sk, SOCK_DEAD))
993 sk->sk_data_ready(sk, skb->len);
999 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1000 struct msghdr *msg, size_t len)
1002 struct sock *sk = sock->sk;
1003 struct nr_sock *nr = nr_sk(sk);
1004 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1006 struct sockaddr_ax25 sax;
1007 struct sk_buff *skb;
1008 unsigned char *asmptr;
1011 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1015 if (sock_flag(sk, SOCK_ZAPPED)) {
1016 err = -EADDRNOTAVAIL;
1020 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1021 send_sig(SIGPIPE, current, 0);
1026 if (nr->device == NULL) {
1032 if (msg->msg_namelen < sizeof(sax)) {
1037 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1041 if (sax.sax25_family != AF_NETROM) {
1046 if (sk->sk_state != TCP_ESTABLISHED) {
1050 sax.sax25_family = AF_NETROM;
1051 sax.sax25_call = nr->dest_addr;
1054 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1056 /* Build a packet */
1057 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1058 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1060 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1063 skb_reserve(skb, size - len);
1066 * Push down the NET/ROM header
1069 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1070 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1072 /* Build a NET/ROM Transport header */
1074 *asmptr++ = nr->your_index;
1075 *asmptr++ = nr->your_id;
1076 *asmptr++ = 0; /* To be filled in later */
1077 *asmptr++ = 0; /* Ditto */
1078 *asmptr++ = NR_INFO;
1079 SOCK_DEBUG(sk, "Built header.\n");
1082 * Put the data on the end
1085 skb->h.raw = skb_put(skb, len);
1087 asmptr = skb->h.raw;
1088 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1090 /* User data follows immediately after the NET/ROM transport header */
1091 if (memcpy_fromiovec(asmptr, msg->msg_iov, len)) {
1097 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1099 if (sk->sk_state != TCP_ESTABLISHED) {
1105 nr_output(sk, skb); /* Shove it onto the queue */
1113 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1114 struct msghdr *msg, size_t size, int flags)
1116 struct sock *sk = sock->sk;
1117 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1119 struct sk_buff *skb;
1123 * This works for seqpacket too. The receiver has ordered the queue for
1124 * us! We do one quick check first though
1128 if (sk->sk_state != TCP_ESTABLISHED) {
1133 /* Now we can treat all alike */
1134 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1139 skb->h.raw = skb->data;
1142 if (copied > size) {
1144 msg->msg_flags |= MSG_TRUNC;
1147 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1150 sax->sax25_family = AF_NETROM;
1151 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1154 msg->msg_namelen = sizeof(*sax);
1156 skb_free_datagram(sk, skb);
1163 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1165 struct sock *sk = sock->sk;
1166 void __user *argp = (void __user *)arg;
1174 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1178 return put_user(amount, (int __user *)argp);
1182 struct sk_buff *skb;
1186 /* These two are safe on a single CPU system as only user tasks fiddle here */
1187 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1190 return put_user(amount, (int __user *)argp);
1195 ret = sock_get_timestamp(sk, argp);
1201 case SIOCGIFDSTADDR:
1202 case SIOCSIFDSTADDR:
1203 case SIOCGIFBRDADDR:
1204 case SIOCSIFBRDADDR:
1205 case SIOCGIFNETMASK:
1206 case SIOCSIFNETMASK:
1214 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1215 return nr_rt_ioctl(cmd, argp);
1218 return -ENOIOCTLCMD;
1224 #ifdef CONFIG_PROC_FS
1226 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1229 struct hlist_node *node;
1232 spin_lock_bh(&nr_list_lock);
1234 return SEQ_START_TOKEN;
1236 sk_for_each(s, node, &nr_list) {
1244 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1248 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
1249 : sk_next((struct sock *)v);
1252 static void nr_info_stop(struct seq_file *seq, void *v)
1254 spin_unlock_bh(&nr_list_lock);
1257 static int nr_info_show(struct seq_file *seq, void *v)
1260 struct net_device *dev;
1262 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(buf, &nr->user_addr));
1280 seq_printf(seq, "%-9s ", ax2asc(buf, &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(buf, &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 const 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 nr_private), name, nr_setup);
1397 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1402 if (register_netdev(dev)) {
1403 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1410 if (sock_register(&nr_family_ops)) {
1411 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1415 register_netdevice_notifier(&nr_dev_notifier);
1418 ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1419 ax25_linkfail_register(nr_link_failed);
1421 #ifdef CONFIG_SYSCTL
1422 nr_register_sysctl();
1427 proc_net_fops_create("nr", S_IRUGO, &nr_info_fops);
1428 proc_net_fops_create("nr_neigh", S_IRUGO, &nr_neigh_fops);
1429 proc_net_fops_create("nr_nodes", S_IRUGO, &nr_nodes_fops);
1434 unregister_netdev(dev_nr[i]);
1435 free_netdev(dev_nr[i]);
1438 proto_unregister(&nr_proto);
1443 module_init(nr_proto_init);
1445 module_param(nr_ndevs, int, 0);
1446 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1448 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1449 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1450 MODULE_LICENSE("GPL");
1451 MODULE_ALIAS_NETPROTO(PF_NETROM);
1453 static void __exit nr_exit(void)
1457 proc_net_remove("nr");
1458 proc_net_remove("nr_neigh");
1459 proc_net_remove("nr_nodes");
1460 nr_loopback_clear();
1464 #ifdef CONFIG_SYSCTL
1465 nr_unregister_sysctl();
1468 ax25_linkfail_release(nr_link_failed);
1469 ax25_protocol_release(AX25_P_NETROM);
1471 unregister_netdevice_notifier(&nr_dev_notifier);
1473 sock_unregister(PF_NETROM);
1475 for (i = 0; i < nr_ndevs; i++) {
1476 struct net_device *dev = dev_nr[i];
1478 unregister_netdev(dev);
1484 proto_unregister(&nr_proto);
1486 module_exit(nr_exit);