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>
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);
428 msecs_to_jiffies(sysctl_netrom_transport_timeout);
430 msecs_to_jiffies(sysctl_netrom_transport_acknowledge_delay);
432 msecs_to_jiffies(sysctl_netrom_transport_maximum_tries);
434 msecs_to_jiffies(sysctl_netrom_transport_busy_delay);
436 msecs_to_jiffies(sysctl_netrom_transport_no_activity_timeout);
437 nr->window = sysctl_netrom_transport_requested_window_size;
440 nr->state = NR_STATE_0;
445 static struct sock *nr_make_new(struct sock *osk)
448 struct nr_sock *nr, *onr;
450 if (osk->sk_type != SOCK_SEQPACKET)
453 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, osk->sk_prot, 1)) == NULL)
458 sock_init_data(NULL, sk);
460 sk->sk_type = osk->sk_type;
461 sk->sk_socket = osk->sk_socket;
462 sk->sk_priority = osk->sk_priority;
463 sk->sk_protocol = osk->sk_protocol;
464 sk->sk_rcvbuf = osk->sk_rcvbuf;
465 sk->sk_sndbuf = osk->sk_sndbuf;
466 sk->sk_state = TCP_ESTABLISHED;
467 sk->sk_sleep = osk->sk_sleep;
468 sock_copy_flags(sk, osk);
470 skb_queue_head_init(&nr->ack_queue);
471 skb_queue_head_init(&nr->reseq_queue);
472 skb_queue_head_init(&nr->frag_queue);
482 nr->idle = onr->idle;
483 nr->window = onr->window;
485 nr->device = onr->device;
486 nr->bpqext = onr->bpqext;
491 static int nr_release(struct socket *sock)
493 struct sock *sk = sock->sk;
496 if (sk == NULL) return 0;
506 nr_disconnect(sk, 0);
507 nr_destroy_socket(sk);
513 nr_write_internal(sk, NR_DISCREQ);
514 nr_start_t1timer(sk);
517 nr_stop_idletimer(sk);
518 nr->state = NR_STATE_2;
519 sk->sk_state = TCP_CLOSE;
520 sk->sk_shutdown |= SEND_SHUTDOWN;
521 sk->sk_state_change(sk);
523 sock_set_flag(sk, SOCK_DESTROY);
524 sk->sk_socket = NULL;
528 sk->sk_socket = NULL;
539 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
541 struct sock *sk = sock->sk;
542 struct nr_sock *nr = nr_sk(sk);
543 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
544 struct net_device *dev;
545 ax25_uid_assoc *user;
546 ax25_address *source;
549 if (!sock_flag(sk, SOCK_ZAPPED)) {
553 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
557 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
561 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
565 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
566 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
568 return -EADDRNOTAVAIL;
572 * Only the super user can set an arbitrary user callsign.
574 if (addr->fsa_ax25.sax25_ndigis == 1) {
575 if (!capable(CAP_NET_BIND_SERVICE)) {
580 nr->user_addr = addr->fsa_digipeater[0];
581 nr->source_addr = addr->fsa_ax25.sax25_call;
583 source = &addr->fsa_ax25.sax25_call;
585 user = ax25_findbyuid(current->euid);
587 nr->user_addr = user->call;
590 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
595 nr->user_addr = *source;
598 nr->source_addr = *source;
602 nr_insert_socket(sk);
604 sock_reset_flag(sk, SOCK_ZAPPED);
607 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
611 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
612 int addr_len, int flags)
614 struct sock *sk = sock->sk;
615 struct nr_sock *nr = nr_sk(sk);
616 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
617 ax25_address *source = NULL;
618 ax25_uid_assoc *user;
619 struct net_device *dev;
622 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
623 sock->state = SS_CONNECTED;
625 return 0; /* Connect completed during a ERESTARTSYS event */
628 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
629 sock->state = SS_UNCONNECTED;
631 return -ECONNREFUSED;
634 if (sk->sk_state == TCP_ESTABLISHED) {
636 return -EISCONN; /* No reconnect on a seqpacket socket */
639 sk->sk_state = TCP_CLOSE;
640 sock->state = SS_UNCONNECTED;
642 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
646 if (addr->sax25_family != AF_NETROM) {
650 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
651 sock_reset_flag(sk, SOCK_ZAPPED);
653 if ((dev = nr_dev_first()) == NULL) {
657 source = (ax25_address *)dev->dev_addr;
659 user = ax25_findbyuid(current->euid);
661 nr->user_addr = user->call;
664 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
669 nr->user_addr = *source;
672 nr->source_addr = *source;
676 nr_insert_socket(sk); /* Finish the bind */
679 nr->dest_addr = addr->sax25_call;
682 circuit = nr_find_next_circuit();
685 nr->my_index = circuit / 256;
686 nr->my_id = circuit % 256;
690 /* Move to connecting socket, start sending Connect Requests */
691 sock->state = SS_CONNECTING;
692 sk->sk_state = TCP_SYN_SENT;
694 nr_establish_data_link(sk);
696 nr->state = NR_STATE_1;
698 nr_start_heartbeat(sk);
701 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
707 * A Connect Ack with Choke or timeout or failed routing will go to
710 if (sk->sk_state == TCP_SYN_SENT) {
711 struct task_struct *tsk = current;
712 DECLARE_WAITQUEUE(wait, tsk);
714 add_wait_queue(sk->sk_sleep, &wait);
716 set_current_state(TASK_INTERRUPTIBLE);
717 if (sk->sk_state != TCP_SYN_SENT)
720 if (!signal_pending(tsk)) {
725 current->state = TASK_RUNNING;
726 remove_wait_queue(sk->sk_sleep, &wait);
729 current->state = TASK_RUNNING;
730 remove_wait_queue(sk->sk_sleep, &wait);
733 if (sk->sk_state != TCP_ESTABLISHED) {
734 sock->state = SS_UNCONNECTED;
736 return sock_error(sk); /* Always set at this point */
739 sock->state = SS_CONNECTED;
745 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
747 struct task_struct *tsk = current;
748 DECLARE_WAITQUEUE(wait, tsk);
754 if ((sk = sock->sk) == NULL)
758 if (sk->sk_type != SOCK_SEQPACKET) {
763 if (sk->sk_state != TCP_LISTEN) {
769 * The write queue this time is holding sockets ready to use
770 * hooked into the SABM we saved
772 add_wait_queue(sk->sk_sleep, &wait);
774 skb = skb_dequeue(&sk->sk_receive_queue);
778 current->state = TASK_INTERRUPTIBLE;
780 if (flags & O_NONBLOCK) {
781 current->state = TASK_RUNNING;
782 remove_wait_queue(sk->sk_sleep, &wait);
785 if (!signal_pending(tsk)) {
790 current->state = TASK_RUNNING;
791 remove_wait_queue(sk->sk_sleep, &wait);
794 current->state = TASK_RUNNING;
795 remove_wait_queue(sk->sk_sleep, &wait);
798 newsk->sk_socket = newsock;
799 newsk->sk_sleep = &newsock->wait;
801 /* Now attach up the new socket */
803 sk_acceptq_removed(sk);
811 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
812 int *uaddr_len, int peer)
814 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
815 struct sock *sk = sock->sk;
816 struct nr_sock *nr = nr_sk(sk);
820 if (sk->sk_state != TCP_ESTABLISHED) {
824 sax->fsa_ax25.sax25_family = AF_NETROM;
825 sax->fsa_ax25.sax25_ndigis = 1;
826 sax->fsa_ax25.sax25_call = nr->user_addr;
827 sax->fsa_digipeater[0] = nr->dest_addr;
828 *uaddr_len = sizeof(struct full_sockaddr_ax25);
830 sax->fsa_ax25.sax25_family = AF_NETROM;
831 sax->fsa_ax25.sax25_ndigis = 0;
832 sax->fsa_ax25.sax25_call = nr->source_addr;
833 *uaddr_len = sizeof(struct sockaddr_ax25);
840 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
844 struct nr_sock *nr_make;
845 ax25_address *src, *dest, *user;
846 unsigned short circuit_index, circuit_id;
847 unsigned short peer_circuit_index, peer_circuit_id;
848 unsigned short frametype, flags, window, timeout;
851 skb->sk = NULL; /* Initially we don't know who it's for */
854 * skb->data points to the netrom frame start
857 src = (ax25_address *)(skb->data + 0);
858 dest = (ax25_address *)(skb->data + 7);
860 circuit_index = skb->data[15];
861 circuit_id = skb->data[16];
862 peer_circuit_index = skb->data[17];
863 peer_circuit_id = skb->data[18];
864 frametype = skb->data[19] & 0x0F;
865 flags = skb->data[19] & 0xF0;
868 * Check for an incoming IP over NET/ROM frame.
870 if (frametype == NR_PROTOEXT &&
871 circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
872 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
873 skb->h.raw = skb->data;
875 return nr_rx_ip(skb, dev);
879 * Find an existing socket connection, based on circuit ID, if it's
880 * a Connect Request base it on their circuit ID.
882 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
883 * circuit that no longer exists at the other end ...
888 if (circuit_index == 0 && circuit_id == 0) {
889 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
890 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
892 if (frametype == NR_CONNREQ)
893 sk = nr_find_peer(circuit_index, circuit_id, src);
895 sk = nr_find_socket(circuit_index, circuit_id);
899 skb->h.raw = skb->data;
901 if (frametype == NR_CONNACK && skb->len == 22)
902 nr_sk(sk)->bpqext = 1;
904 nr_sk(sk)->bpqext = 0;
906 ret = nr_process_rx_frame(sk, skb);
912 * Now it should be a CONNREQ.
914 if (frametype != NR_CONNREQ) {
916 * Here it would be nice to be able to send a reset but
917 * NET/ROM doesn't have one. We've tried to extend the protocol
918 * by sending NR_CONNACK | NR_CHOKE_FLAGS replies but that
919 * apparently kills BPQ boxes... :-(
920 * So now we try to follow the established behaviour of
921 * G8PZT's Xrouter which is sending packets with command type 7
922 * as an extension of the protocol.
924 if (sysctl_netrom_reset_circuit &&
925 (frametype != NR_RESET || flags != 0))
926 nr_transmit_reset(skb, 1);
931 sk = nr_find_listener(dest);
933 user = (ax25_address *)(skb->data + 21);
935 if (sk == NULL || sk_acceptq_is_full(sk) ||
936 (make = nr_make_new(sk)) == NULL) {
937 nr_transmit_refusal(skb, 0);
943 window = skb->data[20];
946 make->sk_state = TCP_ESTABLISHED;
948 /* Fill in his circuit details */
949 nr_make = nr_sk(make);
950 nr_make->source_addr = *dest;
951 nr_make->dest_addr = *src;
952 nr_make->user_addr = *user;
954 nr_make->your_index = circuit_index;
955 nr_make->your_id = circuit_id;
958 circuit = nr_find_next_circuit();
961 nr_make->my_index = circuit / 256;
962 nr_make->my_id = circuit % 256;
966 /* Window negotiation */
967 if (window < nr_make->window)
968 nr_make->window = window;
970 /* L4 timeout negotiation */
971 if (skb->len == 37) {
972 timeout = skb->data[36] * 256 + skb->data[35];
973 if (timeout * HZ < nr_make->t1)
974 nr_make->t1 = timeout * HZ;
980 nr_write_internal(make, NR_CONNACK);
982 nr_make->condition = 0x00;
987 nr_make->state = NR_STATE_3;
988 sk_acceptq_added(sk);
990 nr_insert_socket(make);
992 skb_queue_head(&sk->sk_receive_queue, skb);
994 nr_start_heartbeat(make);
995 nr_start_idletimer(make);
997 if (!sock_flag(sk, SOCK_DEAD))
998 sk->sk_data_ready(sk, skb->len);
1004 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1005 struct msghdr *msg, size_t len)
1007 struct sock *sk = sock->sk;
1008 struct nr_sock *nr = nr_sk(sk);
1009 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1011 struct sockaddr_ax25 sax;
1012 struct sk_buff *skb;
1013 unsigned char *asmptr;
1016 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1020 if (sock_flag(sk, SOCK_ZAPPED)) {
1021 err = -EADDRNOTAVAIL;
1025 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1026 send_sig(SIGPIPE, current, 0);
1031 if (nr->device == NULL) {
1037 if (msg->msg_namelen < sizeof(sax)) {
1042 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1046 if (sax.sax25_family != AF_NETROM) {
1051 if (sk->sk_state != TCP_ESTABLISHED) {
1055 sax.sax25_family = AF_NETROM;
1056 sax.sax25_call = nr->dest_addr;
1059 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1061 /* Build a packet */
1062 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1063 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1065 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1068 skb_reserve(skb, size - len);
1071 * Push down the NET/ROM header
1074 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1075 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1077 /* Build a NET/ROM Transport header */
1079 *asmptr++ = nr->your_index;
1080 *asmptr++ = nr->your_id;
1081 *asmptr++ = 0; /* To be filled in later */
1082 *asmptr++ = 0; /* Ditto */
1083 *asmptr++ = NR_INFO;
1084 SOCK_DEBUG(sk, "Built header.\n");
1087 * Put the data on the end
1090 skb->h.raw = skb_put(skb, len);
1092 asmptr = skb->h.raw;
1093 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1095 /* User data follows immediately after the NET/ROM transport header */
1096 if (memcpy_fromiovec(asmptr, msg->msg_iov, len)) {
1102 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1104 if (sk->sk_state != TCP_ESTABLISHED) {
1110 nr_output(sk, skb); /* Shove it onto the queue */
1118 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1119 struct msghdr *msg, size_t size, int flags)
1121 struct sock *sk = sock->sk;
1122 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1124 struct sk_buff *skb;
1128 * This works for seqpacket too. The receiver has ordered the queue for
1129 * us! We do one quick check first though
1133 if (sk->sk_state != TCP_ESTABLISHED) {
1138 /* Now we can treat all alike */
1139 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1144 skb->h.raw = skb->data;
1147 if (copied > size) {
1149 msg->msg_flags |= MSG_TRUNC;
1152 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1155 sax->sax25_family = AF_NETROM;
1156 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1159 msg->msg_namelen = sizeof(*sax);
1161 skb_free_datagram(sk, skb);
1168 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1170 struct sock *sk = sock->sk;
1171 void __user *argp = (void __user *)arg;
1179 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1183 return put_user(amount, (int __user *)argp);
1187 struct sk_buff *skb;
1191 /* These two are safe on a single CPU system as only user tasks fiddle here */
1192 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1195 return put_user(amount, (int __user *)argp);
1200 ret = sock_get_timestamp(sk, argp);
1206 case SIOCGIFDSTADDR:
1207 case SIOCSIFDSTADDR:
1208 case SIOCGIFBRDADDR:
1209 case SIOCSIFBRDADDR:
1210 case SIOCGIFNETMASK:
1211 case SIOCSIFNETMASK:
1219 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1220 return nr_rt_ioctl(cmd, argp);
1223 return -ENOIOCTLCMD;
1229 #ifdef CONFIG_PROC_FS
1231 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1234 struct hlist_node *node;
1237 spin_lock_bh(&nr_list_lock);
1239 return SEQ_START_TOKEN;
1241 sk_for_each(s, node, &nr_list) {
1249 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1253 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
1254 : sk_next((struct sock *)v);
1257 static void nr_info_stop(struct seq_file *seq, void *v)
1259 spin_unlock_bh(&nr_list_lock);
1262 static int nr_info_show(struct seq_file *seq, void *v)
1265 struct net_device *dev;
1267 const char *devname;
1270 if (v == SEQ_START_TOKEN)
1272 "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");
1279 if ((dev = nr->device) == NULL)
1282 devname = dev->name;
1284 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->user_addr));
1285 seq_printf(seq, "%-9s ", ax2asc(buf, &nr->dest_addr));
1287 "%-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",
1288 ax2asc(buf, &nr->source_addr),
1298 ax25_display_timer(&nr->t1timer) / HZ,
1300 ax25_display_timer(&nr->t2timer) / HZ,
1302 ax25_display_timer(&nr->t4timer) / HZ,
1304 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1305 nr->idle / (60 * HZ),
1309 atomic_read(&s->sk_wmem_alloc),
1310 atomic_read(&s->sk_rmem_alloc),
1311 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1318 static struct seq_operations nr_info_seqops = {
1319 .start = nr_info_start,
1320 .next = nr_info_next,
1321 .stop = nr_info_stop,
1322 .show = nr_info_show,
1325 static int nr_info_open(struct inode *inode, struct file *file)
1327 return seq_open(file, &nr_info_seqops);
1330 static struct file_operations nr_info_fops = {
1331 .owner = THIS_MODULE,
1332 .open = nr_info_open,
1334 .llseek = seq_lseek,
1335 .release = seq_release,
1337 #endif /* CONFIG_PROC_FS */
1339 static struct net_proto_family nr_family_ops = {
1340 .family = PF_NETROM,
1341 .create = nr_create,
1342 .owner = THIS_MODULE,
1345 static const struct proto_ops nr_proto_ops = {
1346 .family = PF_NETROM,
1347 .owner = THIS_MODULE,
1348 .release = nr_release,
1350 .connect = nr_connect,
1351 .socketpair = sock_no_socketpair,
1352 .accept = nr_accept,
1353 .getname = nr_getname,
1354 .poll = datagram_poll,
1356 .listen = nr_listen,
1357 .shutdown = sock_no_shutdown,
1358 .setsockopt = nr_setsockopt,
1359 .getsockopt = nr_getsockopt,
1360 .sendmsg = nr_sendmsg,
1361 .recvmsg = nr_recvmsg,
1362 .mmap = sock_no_mmap,
1363 .sendpage = sock_no_sendpage,
1366 static struct notifier_block nr_dev_notifier = {
1367 .notifier_call = nr_device_event,
1370 static struct net_device **dev_nr;
1372 static int __init nr_proto_init(void)
1375 int rc = proto_register(&nr_proto, 0);
1380 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1381 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1385 dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1386 if (dev_nr == NULL) {
1387 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1391 memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device *));
1393 for (i = 0; i < nr_ndevs; i++) {
1394 char name[IFNAMSIZ];
1395 struct net_device *dev;
1397 sprintf(name, "nr%d", i);
1398 dev = alloc_netdev(sizeof(struct nr_private), name, nr_setup);
1400 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1405 if (register_netdev(dev)) {
1406 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1413 if (sock_register(&nr_family_ops)) {
1414 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1418 register_netdevice_notifier(&nr_dev_notifier);
1420 ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1421 ax25_linkfail_register(nr_link_failed);
1423 #ifdef CONFIG_SYSCTL
1424 nr_register_sysctl();
1429 proc_net_fops_create("nr", S_IRUGO, &nr_info_fops);
1430 proc_net_fops_create("nr_neigh", S_IRUGO, &nr_neigh_fops);
1431 proc_net_fops_create("nr_nodes", S_IRUGO, &nr_nodes_fops);
1436 unregister_netdev(dev_nr[i]);
1437 free_netdev(dev_nr[i]);
1440 proto_unregister(&nr_proto);
1445 module_init(nr_proto_init);
1447 module_param(nr_ndevs, int, 0);
1448 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1450 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1451 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1452 MODULE_LICENSE("GPL");
1453 MODULE_ALIAS_NETPROTO(PF_NETROM);
1455 static void __exit nr_exit(void)
1459 proc_net_remove("nr");
1460 proc_net_remove("nr_neigh");
1461 proc_net_remove("nr_nodes");
1462 nr_loopback_clear();
1466 #ifdef CONFIG_SYSCTL
1467 nr_unregister_sysctl();
1470 ax25_linkfail_release(nr_link_failed);
1471 ax25_protocol_release(AX25_P_NETROM);
1473 unregister_netdevice_notifier(&nr_dev_notifier);
1475 sock_unregister(PF_NETROM);
1477 for (i = 0; i < nr_ndevs; i++) {
1478 struct net_device *dev = dev_nr[i];
1480 unregister_netdev(dev);
1486 proto_unregister(&nr_proto);
1488 module_exit(nr_exit);