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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
10 * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
13 #include <linux/config.h>
14 #include <linux/capability.h>
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/errno.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
24 #include <linux/spinlock.h>
25 #include <linux/timer.h>
26 #include <linux/string.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/stat.h>
31 #include <linux/inet.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/skbuff.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38 #include <linux/fcntl.h>
39 #include <linux/termios.h>
41 #include <linux/interrupt.h>
42 #include <linux/notifier.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <net/tcp_states.h>
50 static int rose_ndevs = 10;
52 int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
53 int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
54 int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
55 int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
56 int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
57 int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
58 int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
59 int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
60 int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
61 int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
63 static HLIST_HEAD(rose_list);
64 static DEFINE_SPINLOCK(rose_list_lock);
66 static struct proto_ops rose_proto_ops;
68 ax25_address rose_callsign;
71 * Convert a ROSE address into text.
73 const char *rose2asc(const rose_address *addr)
75 static char buffer[11];
77 if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
78 addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
79 addr->rose_addr[4] == 0x00) {
82 sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
83 addr->rose_addr[1] & 0xFF,
84 addr->rose_addr[2] & 0xFF,
85 addr->rose_addr[3] & 0xFF,
86 addr->rose_addr[4] & 0xFF);
93 * Compare two ROSE addresses, 0 == equal.
95 int rosecmp(rose_address *addr1, rose_address *addr2)
99 for (i = 0; i < 5; i++)
100 if (addr1->rose_addr[i] != addr2->rose_addr[i])
107 * Compare two ROSE addresses for only mask digits, 0 == equal.
109 int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
116 for (i = 0; i < mask; i++) {
120 if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
123 if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
132 * Socket removal during an interrupt is now safe.
134 static void rose_remove_socket(struct sock *sk)
136 spin_lock_bh(&rose_list_lock);
137 sk_del_node_init(sk);
138 spin_unlock_bh(&rose_list_lock);
142 * Kill all bound sockets on a broken link layer connection to a
143 * particular neighbour.
145 void rose_kill_by_neigh(struct rose_neigh *neigh)
148 struct hlist_node *node;
150 spin_lock_bh(&rose_list_lock);
151 sk_for_each(s, node, &rose_list) {
152 struct rose_sock *rose = rose_sk(s);
154 if (rose->neighbour == neigh) {
155 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
156 rose->neighbour->use--;
157 rose->neighbour = NULL;
160 spin_unlock_bh(&rose_list_lock);
164 * Kill all bound sockets on a dropped device.
166 static void rose_kill_by_device(struct net_device *dev)
169 struct hlist_node *node;
171 spin_lock_bh(&rose_list_lock);
172 sk_for_each(s, node, &rose_list) {
173 struct rose_sock *rose = rose_sk(s);
175 if (rose->device == dev) {
176 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
177 rose->neighbour->use--;
181 spin_unlock_bh(&rose_list_lock);
185 * Handle device status changes.
187 static int rose_device_event(struct notifier_block *this, unsigned long event,
190 struct net_device *dev = (struct net_device *)ptr;
192 if (event != NETDEV_DOWN)
197 rose_kill_by_device(dev);
200 rose_link_device_down(dev);
201 rose_rt_device_down(dev);
209 * Add a socket to the bound sockets list.
211 static void rose_insert_socket(struct sock *sk)
214 spin_lock_bh(&rose_list_lock);
215 sk_add_node(sk, &rose_list);
216 spin_unlock_bh(&rose_list_lock);
220 * Find a socket that wants to accept the Call Request we just
223 static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
226 struct hlist_node *node;
228 spin_lock_bh(&rose_list_lock);
229 sk_for_each(s, node, &rose_list) {
230 struct rose_sock *rose = rose_sk(s);
232 if (!rosecmp(&rose->source_addr, addr) &&
233 !ax25cmp(&rose->source_call, call) &&
234 !rose->source_ndigis && s->sk_state == TCP_LISTEN)
238 sk_for_each(s, node, &rose_list) {
239 struct rose_sock *rose = rose_sk(s);
241 if (!rosecmp(&rose->source_addr, addr) &&
242 !ax25cmp(&rose->source_call, &null_ax25_address) &&
243 s->sk_state == TCP_LISTEN)
248 spin_unlock_bh(&rose_list_lock);
253 * Find a connected ROSE socket given my LCI and device.
255 struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
258 struct hlist_node *node;
260 spin_lock_bh(&rose_list_lock);
261 sk_for_each(s, node, &rose_list) {
262 struct rose_sock *rose = rose_sk(s);
264 if (rose->lci == lci && rose->neighbour == neigh)
269 spin_unlock_bh(&rose_list_lock);
274 * Find a unique LCI for a given device.
276 unsigned int rose_new_lci(struct rose_neigh *neigh)
280 if (neigh->dce_mode) {
281 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
282 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
285 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
286 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
296 void rose_destroy_socket(struct sock *);
299 * Handler for deferred kills.
301 static void rose_destroy_timer(unsigned long data)
303 rose_destroy_socket((struct sock *)data);
307 * This is called from user mode and the timers. Thus it protects itself
308 * against interrupt users but doesn't worry about being called during
309 * work. Once it is removed from the queue no interrupt or bottom half
310 * will touch it and we are (fairly 8-) ) safe.
312 void rose_destroy_socket(struct sock *sk)
316 rose_remove_socket(sk);
317 rose_stop_heartbeat(sk);
318 rose_stop_idletimer(sk);
321 rose_clear_queues(sk); /* Flush the queues */
323 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
324 if (skb->sk != sk) { /* A pending connection */
325 /* Queue the unaccepted socket for death */
326 sock_set_flag(skb->sk, SOCK_DEAD);
327 rose_start_heartbeat(skb->sk);
328 rose_sk(skb->sk)->state = ROSE_STATE_0;
334 if (atomic_read(&sk->sk_wmem_alloc) ||
335 atomic_read(&sk->sk_rmem_alloc)) {
336 /* Defer: outstanding buffers */
337 init_timer(&sk->sk_timer);
338 sk->sk_timer.expires = jiffies + 10 * HZ;
339 sk->sk_timer.function = rose_destroy_timer;
340 sk->sk_timer.data = (unsigned long)sk;
341 add_timer(&sk->sk_timer);
347 * Handling for system calls applied via the various interfaces to a
348 * ROSE socket object.
351 static int rose_setsockopt(struct socket *sock, int level, int optname,
352 char __user *optval, int optlen)
354 struct sock *sk = sock->sk;
355 struct rose_sock *rose = rose_sk(sk);
358 if (level != SOL_ROSE)
361 if (optlen < sizeof(int))
364 if (get_user(opt, (int __user *)optval))
369 rose->defer = opt ? 1 : 0;
399 rose->idle = opt * 60 * HZ;
403 rose->qbitincl = opt ? 1 : 0;
411 static int rose_getsockopt(struct socket *sock, int level, int optname,
412 char __user *optval, int __user *optlen)
414 struct sock *sk = sock->sk;
415 struct rose_sock *rose = rose_sk(sk);
419 if (level != SOL_ROSE)
422 if (get_user(len, optlen))
450 val = rose->idle / (60 * HZ);
454 val = rose->qbitincl;
461 len = min_t(unsigned int, len, sizeof(int));
463 if (put_user(len, optlen))
466 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
469 static int rose_listen(struct socket *sock, int backlog)
471 struct sock *sk = sock->sk;
473 if (sk->sk_state != TCP_LISTEN) {
474 struct rose_sock *rose = rose_sk(sk);
476 rose->dest_ndigis = 0;
477 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
478 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
479 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
480 sk->sk_max_ack_backlog = backlog;
481 sk->sk_state = TCP_LISTEN;
488 static struct proto rose_proto = {
490 .owner = THIS_MODULE,
491 .obj_size = sizeof(struct rose_sock),
494 static int rose_create(struct socket *sock, int protocol)
497 struct rose_sock *rose;
499 if (sock->type != SOCK_SEQPACKET || protocol != 0)
500 return -ESOCKTNOSUPPORT;
502 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
507 sock_init_data(sock, sk);
509 skb_queue_head_init(&rose->ack_queue);
511 skb_queue_head_init(&rose->frag_queue);
515 sock->ops = &rose_proto_ops;
516 sk->sk_protocol = protocol;
518 init_timer(&rose->timer);
519 init_timer(&rose->idletimer);
521 rose->t1 = msecs_to_jiffies(sysctl_rose_call_request_timeout);
522 rose->t2 = msecs_to_jiffies(sysctl_rose_reset_request_timeout);
523 rose->t3 = msecs_to_jiffies(sysctl_rose_clear_request_timeout);
524 rose->hb = msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout);
525 rose->idle = msecs_to_jiffies(sysctl_rose_no_activity_timeout);
527 rose->state = ROSE_STATE_0;
532 static struct sock *rose_make_new(struct sock *osk)
535 struct rose_sock *rose, *orose;
537 if (osk->sk_type != SOCK_SEQPACKET)
540 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
545 sock_init_data(NULL, sk);
547 skb_queue_head_init(&rose->ack_queue);
549 skb_queue_head_init(&rose->frag_queue);
553 sk->sk_type = osk->sk_type;
554 sk->sk_socket = osk->sk_socket;
555 sk->sk_priority = osk->sk_priority;
556 sk->sk_protocol = osk->sk_protocol;
557 sk->sk_rcvbuf = osk->sk_rcvbuf;
558 sk->sk_sndbuf = osk->sk_sndbuf;
559 sk->sk_state = TCP_ESTABLISHED;
560 sk->sk_sleep = osk->sk_sleep;
561 sock_copy_flags(sk, osk);
563 init_timer(&rose->timer);
564 init_timer(&rose->idletimer);
566 orose = rose_sk(osk);
567 rose->t1 = orose->t1;
568 rose->t2 = orose->t2;
569 rose->t3 = orose->t3;
570 rose->hb = orose->hb;
571 rose->idle = orose->idle;
572 rose->defer = orose->defer;
573 rose->device = orose->device;
574 rose->qbitincl = orose->qbitincl;
579 static int rose_release(struct socket *sock)
581 struct sock *sk = sock->sk;
582 struct rose_sock *rose;
584 if (sk == NULL) return 0;
588 switch (rose->state) {
590 rose_disconnect(sk, 0, -1, -1);
591 rose_destroy_socket(sk);
595 rose->neighbour->use--;
596 rose_disconnect(sk, 0, -1, -1);
597 rose_destroy_socket(sk);
604 rose_clear_queues(sk);
605 rose_stop_idletimer(sk);
606 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
607 rose_start_t3timer(sk);
608 rose->state = ROSE_STATE_2;
609 sk->sk_state = TCP_CLOSE;
610 sk->sk_shutdown |= SEND_SHUTDOWN;
611 sk->sk_state_change(sk);
612 sock_set_flag(sk, SOCK_DEAD);
613 sock_set_flag(sk, SOCK_DESTROY);
625 static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
627 struct sock *sk = sock->sk;
628 struct rose_sock *rose = rose_sk(sk);
629 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
630 struct net_device *dev;
631 ax25_address *source;
632 ax25_uid_assoc *user;
635 if (!sock_flag(sk, SOCK_ZAPPED))
638 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
641 if (addr->srose_family != AF_ROSE)
644 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
647 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
650 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
651 SOCK_DEBUG(sk, "ROSE: bind failed: invalid address\n");
652 return -EADDRNOTAVAIL;
655 source = &addr->srose_call;
657 user = ax25_findbyuid(current->euid);
659 rose->source_call = user->call;
662 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
664 rose->source_call = *source;
667 rose->source_addr = addr->srose_addr;
669 rose->source_ndigis = addr->srose_ndigis;
671 if (addr_len == sizeof(struct full_sockaddr_rose)) {
672 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
673 for (n = 0 ; n < addr->srose_ndigis ; n++)
674 rose->source_digis[n] = full_addr->srose_digis[n];
676 if (rose->source_ndigis == 1) {
677 rose->source_digis[0] = addr->srose_digi;
681 rose_insert_socket(sk);
683 sock_reset_flag(sk, SOCK_ZAPPED);
684 SOCK_DEBUG(sk, "ROSE: socket is bound\n");
688 static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
690 struct sock *sk = sock->sk;
691 struct rose_sock *rose = rose_sk(sk);
692 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
693 unsigned char cause, diagnostic;
694 struct net_device *dev;
695 ax25_uid_assoc *user;
698 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
699 sock->state = SS_CONNECTED;
700 return 0; /* Connect completed during a ERESTARTSYS event */
703 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
704 sock->state = SS_UNCONNECTED;
705 return -ECONNREFUSED;
708 if (sk->sk_state == TCP_ESTABLISHED)
709 return -EISCONN; /* No reconnect on a seqpacket socket */
711 sk->sk_state = TCP_CLOSE;
712 sock->state = SS_UNCONNECTED;
714 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
717 if (addr->srose_family != AF_ROSE)
720 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
723 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
726 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
727 if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
730 rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
732 if (!rose->neighbour)
735 rose->lci = rose_new_lci(rose->neighbour);
739 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
740 sock_reset_flag(sk, SOCK_ZAPPED);
742 if ((dev = rose_dev_first()) == NULL)
745 user = ax25_findbyuid(current->euid);
749 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
750 rose->source_call = user->call;
754 rose_insert_socket(sk); /* Finish the bind */
757 rose->dest_addr = addr->srose_addr;
758 rose->dest_call = addr->srose_call;
759 rose->rand = ((long)rose & 0xFFFF) + rose->lci;
760 rose->dest_ndigis = addr->srose_ndigis;
762 if (addr_len == sizeof(struct full_sockaddr_rose)) {
763 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
764 for (n = 0 ; n < addr->srose_ndigis ; n++)
765 rose->dest_digis[n] = full_addr->srose_digis[n];
767 if (rose->dest_ndigis == 1) {
768 rose->dest_digis[0] = addr->srose_digi;
772 /* Move to connecting socket, start sending Connect Requests */
773 sock->state = SS_CONNECTING;
774 sk->sk_state = TCP_SYN_SENT;
776 rose->state = ROSE_STATE_1;
778 rose->neighbour->use++;
780 rose_write_internal(sk, ROSE_CALL_REQUEST);
781 rose_start_heartbeat(sk);
782 rose_start_t1timer(sk);
785 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
789 * A Connect Ack with Choke or timeout or failed routing will go to
792 if (sk->sk_state == TCP_SYN_SENT) {
793 struct task_struct *tsk = current;
794 DECLARE_WAITQUEUE(wait, tsk);
796 add_wait_queue(sk->sk_sleep, &wait);
798 set_current_state(TASK_INTERRUPTIBLE);
799 if (sk->sk_state != TCP_SYN_SENT)
801 if (!signal_pending(tsk)) {
805 current->state = TASK_RUNNING;
806 remove_wait_queue(sk->sk_sleep, &wait);
809 current->state = TASK_RUNNING;
810 remove_wait_queue(sk->sk_sleep, &wait);
813 if (sk->sk_state != TCP_ESTABLISHED) {
814 sock->state = SS_UNCONNECTED;
815 return sock_error(sk); /* Always set at this point */
818 sock->state = SS_CONNECTED;
823 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
825 struct task_struct *tsk = current;
826 DECLARE_WAITQUEUE(wait, tsk);
832 if ((sk = sock->sk) == NULL)
836 if (sk->sk_type != SOCK_SEQPACKET) {
841 if (sk->sk_state != TCP_LISTEN) {
847 * The write queue this time is holding sockets ready to use
848 * hooked into the SABM we saved
850 add_wait_queue(sk->sk_sleep, &wait);
852 skb = skb_dequeue(&sk->sk_receive_queue);
856 current->state = TASK_INTERRUPTIBLE;
858 if (flags & O_NONBLOCK) {
859 current->state = TASK_RUNNING;
860 remove_wait_queue(sk->sk_sleep, &wait);
863 if (!signal_pending(tsk)) {
870 current->state = TASK_RUNNING;
871 remove_wait_queue(sk->sk_sleep, &wait);
874 newsk->sk_socket = newsock;
875 newsk->sk_sleep = &newsock->wait;
877 /* Now attach up the new socket */
880 sk->sk_ack_backlog--;
889 static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
890 int *uaddr_len, int peer)
892 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
893 struct sock *sk = sock->sk;
894 struct rose_sock *rose = rose_sk(sk);
898 if (sk->sk_state != TCP_ESTABLISHED)
900 srose->srose_family = AF_ROSE;
901 srose->srose_addr = rose->dest_addr;
902 srose->srose_call = rose->dest_call;
903 srose->srose_ndigis = rose->dest_ndigis;
904 for (n = 0; n < rose->dest_ndigis; n++)
905 srose->srose_digis[n] = rose->dest_digis[n];
907 srose->srose_family = AF_ROSE;
908 srose->srose_addr = rose->source_addr;
909 srose->srose_call = rose->source_call;
910 srose->srose_ndigis = rose->source_ndigis;
911 for (n = 0; n < rose->source_ndigis; n++)
912 srose->srose_digis[n] = rose->source_digis[n];
915 *uaddr_len = sizeof(struct full_sockaddr_rose);
919 int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
923 struct rose_sock *make_rose;
924 struct rose_facilities_struct facilities;
927 skb->sk = NULL; /* Initially we don't know who it's for */
930 * skb->data points to the rose frame start
932 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
934 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
935 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
936 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
937 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
941 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
944 * We can't accept the Call Request.
946 if (sk == NULL || sk_acceptq_is_full(sk) ||
947 (make = rose_make_new(sk)) == NULL) {
948 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
953 make->sk_state = TCP_ESTABLISHED;
954 make_rose = rose_sk(make);
956 make_rose->lci = lci;
957 make_rose->dest_addr = facilities.dest_addr;
958 make_rose->dest_call = facilities.dest_call;
959 make_rose->dest_ndigis = facilities.dest_ndigis;
960 for (n = 0 ; n < facilities.dest_ndigis ; n++)
961 make_rose->dest_digis[n] = facilities.dest_digis[n];
962 make_rose->source_addr = facilities.source_addr;
963 make_rose->source_call = facilities.source_call;
964 make_rose->source_ndigis = facilities.source_ndigis;
965 for (n = 0 ; n < facilities.source_ndigis ; n++)
966 make_rose->source_digis[n]= facilities.source_digis[n];
967 make_rose->neighbour = neigh;
968 make_rose->device = dev;
969 make_rose->facilities = facilities;
971 make_rose->neighbour->use++;
973 if (rose_sk(sk)->defer) {
974 make_rose->state = ROSE_STATE_5;
976 rose_write_internal(make, ROSE_CALL_ACCEPTED);
977 make_rose->state = ROSE_STATE_3;
978 rose_start_idletimer(make);
981 make_rose->condition = 0x00;
986 sk->sk_ack_backlog++;
988 rose_insert_socket(make);
990 skb_queue_head(&sk->sk_receive_queue, skb);
992 rose_start_heartbeat(make);
994 if (!sock_flag(sk, SOCK_DEAD))
995 sk->sk_data_ready(sk, skb->len);
1000 static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
1001 struct msghdr *msg, size_t len)
1003 struct sock *sk = sock->sk;
1004 struct rose_sock *rose = rose_sk(sk);
1005 struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1007 struct full_sockaddr_rose srose;
1008 struct sk_buff *skb;
1009 unsigned char *asmptr;
1010 int n, size, qbit = 0;
1012 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1015 if (sock_flag(sk, SOCK_ZAPPED))
1016 return -EADDRNOTAVAIL;
1018 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1019 send_sig(SIGPIPE, current, 0);
1023 if (rose->neighbour == NULL || rose->device == NULL)
1024 return -ENETUNREACH;
1026 if (usrose != NULL) {
1027 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1029 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1030 memcpy(&srose, usrose, msg->msg_namelen);
1031 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1032 ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1034 if (srose.srose_ndigis != rose->dest_ndigis)
1036 if (srose.srose_ndigis == rose->dest_ndigis) {
1037 for (n = 0 ; n < srose.srose_ndigis ; n++)
1038 if (ax25cmp(&rose->dest_digis[n],
1039 &srose.srose_digis[n]))
1042 if (srose.srose_family != AF_ROSE)
1045 if (sk->sk_state != TCP_ESTABLISHED)
1048 srose.srose_family = AF_ROSE;
1049 srose.srose_addr = rose->dest_addr;
1050 srose.srose_call = rose->dest_call;
1051 srose.srose_ndigis = rose->dest_ndigis;
1052 for (n = 0 ; n < rose->dest_ndigis ; n++)
1053 srose.srose_digis[n] = rose->dest_digis[n];
1056 SOCK_DEBUG(sk, "ROSE: sendto: Addresses built.\n");
1058 /* Build a packet */
1059 SOCK_DEBUG(sk, "ROSE: sendto: building packet.\n");
1060 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1062 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1065 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1068 * Put the data on the end
1070 SOCK_DEBUG(sk, "ROSE: Appending user data\n");
1072 asmptr = skb->h.raw = skb_put(skb, len);
1074 err = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1081 * If the Q BIT Include socket option is in force, the first
1082 * byte of the user data is the logical value of the Q Bit.
1084 if (rose->qbitincl) {
1085 qbit = skb->data[0];
1090 * Push down the ROSE header
1092 asmptr = skb_push(skb, ROSE_MIN_LEN);
1094 SOCK_DEBUG(sk, "ROSE: Building Network Header.\n");
1096 /* Build a ROSE Network header */
1097 asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1098 asmptr[1] = (rose->lci >> 0) & 0xFF;
1099 asmptr[2] = ROSE_DATA;
1102 asmptr[0] |= ROSE_Q_BIT;
1104 SOCK_DEBUG(sk, "ROSE: Built header.\n");
1106 SOCK_DEBUG(sk, "ROSE: Transmitting buffer\n");
1108 if (sk->sk_state != TCP_ESTABLISHED) {
1114 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1115 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1116 unsigned char header[ROSE_MIN_LEN];
1117 struct sk_buff *skbn;
1121 /* Save a copy of the Header */
1122 memcpy(header, skb->data, ROSE_MIN_LEN);
1123 skb_pull(skb, ROSE_MIN_LEN);
1125 frontlen = skb_headroom(skb);
1127 while (skb->len > 0) {
1128 if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1137 skb_reserve(skbn, frontlen);
1139 lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1141 /* Copy the user data */
1142 memcpy(skb_put(skbn, lg), skb->data, lg);
1145 /* Duplicate the Header */
1146 skb_push(skbn, ROSE_MIN_LEN);
1147 memcpy(skbn->data, header, ROSE_MIN_LEN);
1150 skbn->data[2] |= M_BIT;
1152 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1158 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
1161 skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
1170 static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1171 struct msghdr *msg, size_t size, int flags)
1173 struct sock *sk = sock->sk;
1174 struct rose_sock *rose = rose_sk(sk);
1175 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1177 unsigned char *asmptr;
1178 struct sk_buff *skb;
1182 * This works for seqpacket too. The receiver has ordered the queue for
1183 * us! We do one quick check first though
1185 if (sk->sk_state != TCP_ESTABLISHED)
1188 /* Now we can treat all alike */
1189 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1192 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1194 skb_pull(skb, ROSE_MIN_LEN);
1196 if (rose->qbitincl) {
1197 asmptr = skb_push(skb, 1);
1201 skb->h.raw = skb->data;
1204 if (copied > size) {
1206 msg->msg_flags |= MSG_TRUNC;
1209 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1211 if (srose != NULL) {
1212 srose->srose_family = AF_ROSE;
1213 srose->srose_addr = rose->dest_addr;
1214 srose->srose_call = rose->dest_call;
1215 srose->srose_ndigis = rose->dest_ndigis;
1216 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) {
1217 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1218 for (n = 0 ; n < rose->dest_ndigis ; n++)
1219 full_srose->srose_digis[n] = rose->dest_digis[n];
1220 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1222 if (rose->dest_ndigis >= 1) {
1223 srose->srose_ndigis = 1;
1224 srose->srose_digi = rose->dest_digis[0];
1226 msg->msg_namelen = sizeof(struct sockaddr_rose);
1230 skb_free_datagram(sk, skb);
1236 static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1238 struct sock *sk = sock->sk;
1239 struct rose_sock *rose = rose_sk(sk);
1240 void __user *argp = (void __user *)arg;
1245 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1248 return put_user(amount, (unsigned int __user *) argp);
1252 struct sk_buff *skb;
1254 /* These two are safe on a single CPU system as only user tasks fiddle here */
1255 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1257 return put_user(amount, (unsigned int __user *) argp);
1261 return sock_get_timestamp(sk, (struct timeval __user *) argp);
1265 case SIOCGIFDSTADDR:
1266 case SIOCSIFDSTADDR:
1267 case SIOCGIFBRDADDR:
1268 case SIOCSIFBRDADDR:
1269 case SIOCGIFNETMASK:
1270 case SIOCSIFNETMASK:
1278 if (!capable(CAP_NET_ADMIN))
1280 return rose_rt_ioctl(cmd, argp);
1282 case SIOCRSGCAUSE: {
1283 struct rose_cause_struct rose_cause;
1284 rose_cause.cause = rose->cause;
1285 rose_cause.diagnostic = rose->diagnostic;
1286 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1289 case SIOCRSSCAUSE: {
1290 struct rose_cause_struct rose_cause;
1291 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1293 rose->cause = rose_cause.cause;
1294 rose->diagnostic = rose_cause.diagnostic;
1299 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1300 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1301 ax25_listen_release(&rose_callsign, NULL);
1302 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1304 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1305 ax25_listen_register(&rose_callsign, NULL);
1309 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1312 if (rose->state == ROSE_STATE_5) {
1313 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1314 rose_start_idletimer(sk);
1315 rose->condition = 0x00;
1320 rose->state = ROSE_STATE_3;
1325 return -ENOIOCTLCMD;
1331 #ifdef CONFIG_PROC_FS
1332 static void *rose_info_start(struct seq_file *seq, loff_t *pos)
1336 struct hlist_node *node;
1338 spin_lock_bh(&rose_list_lock);
1340 return SEQ_START_TOKEN;
1343 sk_for_each(s, node, &rose_list) {
1351 static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1355 return (v == SEQ_START_TOKEN) ? sk_head(&rose_list)
1356 : sk_next((struct sock *)v);
1359 static void rose_info_stop(struct seq_file *seq, void *v)
1361 spin_unlock_bh(&rose_list_lock);
1364 static int rose_info_show(struct seq_file *seq, void *v)
1368 if (v == SEQ_START_TOKEN)
1370 "dest_addr dest_call src_addr src_call dev lci neigh st vs vr va t t1 t2 t3 hb idle Snd-Q Rcv-Q inode\n");
1374 struct rose_sock *rose = rose_sk(s);
1375 const char *devname, *callsign;
1376 const struct net_device *dev = rose->device;
1381 devname = dev->name;
1383 seq_printf(seq, "%-10s %-9s ",
1384 rose2asc(&rose->dest_addr),
1385 ax2asc(buf, &rose->dest_call));
1387 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1388 callsign = "??????-?";
1390 callsign = ax2asc(buf, &rose->source_call);
1393 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1394 rose2asc(&rose->source_addr),
1398 (rose->neighbour) ? rose->neighbour->number : 0,
1403 ax25_display_timer(&rose->timer) / HZ,
1408 ax25_display_timer(&rose->idletimer) / (60 * HZ),
1409 rose->idle / (60 * HZ),
1410 atomic_read(&s->sk_wmem_alloc),
1411 atomic_read(&s->sk_rmem_alloc),
1412 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1418 static struct seq_operations rose_info_seqops = {
1419 .start = rose_info_start,
1420 .next = rose_info_next,
1421 .stop = rose_info_stop,
1422 .show = rose_info_show,
1425 static int rose_info_open(struct inode *inode, struct file *file)
1427 return seq_open(file, &rose_info_seqops);
1430 static struct file_operations rose_info_fops = {
1431 .owner = THIS_MODULE,
1432 .open = rose_info_open,
1434 .llseek = seq_lseek,
1435 .release = seq_release,
1437 #endif /* CONFIG_PROC_FS */
1439 static struct net_proto_family rose_family_ops = {
1441 .create = rose_create,
1442 .owner = THIS_MODULE,
1445 static struct proto_ops rose_proto_ops = {
1447 .owner = THIS_MODULE,
1448 .release = rose_release,
1450 .connect = rose_connect,
1451 .socketpair = sock_no_socketpair,
1452 .accept = rose_accept,
1453 .getname = rose_getname,
1454 .poll = datagram_poll,
1455 .ioctl = rose_ioctl,
1456 .listen = rose_listen,
1457 .shutdown = sock_no_shutdown,
1458 .setsockopt = rose_setsockopt,
1459 .getsockopt = rose_getsockopt,
1460 .sendmsg = rose_sendmsg,
1461 .recvmsg = rose_recvmsg,
1462 .mmap = sock_no_mmap,
1463 .sendpage = sock_no_sendpage,
1466 static struct notifier_block rose_dev_notifier = {
1467 .notifier_call = rose_device_event,
1470 static struct net_device **dev_rose;
1472 static int __init rose_proto_init(void)
1477 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1478 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1483 rc = proto_register(&rose_proto, 0);
1487 rose_callsign = null_ax25_address;
1489 dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1490 if (dev_rose == NULL) {
1491 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1493 goto out_proto_unregister;
1496 memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device*));
1497 for (i = 0; i < rose_ndevs; i++) {
1498 struct net_device *dev;
1499 char name[IFNAMSIZ];
1501 sprintf(name, "rose%d", i);
1502 dev = alloc_netdev(sizeof(struct net_device_stats),
1505 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
1509 rc = register_netdev(dev);
1511 printk(KERN_ERR "ROSE: netdevice registration failed\n");
1518 sock_register(&rose_family_ops);
1519 register_netdevice_notifier(&rose_dev_notifier);
1521 ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
1522 ax25_linkfail_register(rose_link_failed);
1524 #ifdef CONFIG_SYSCTL
1525 rose_register_sysctl();
1527 rose_loopback_init();
1529 rose_add_loopback_neigh();
1531 proc_net_fops_create("rose", S_IRUGO, &rose_info_fops);
1532 proc_net_fops_create("rose_neigh", S_IRUGO, &rose_neigh_fops);
1533 proc_net_fops_create("rose_nodes", S_IRUGO, &rose_nodes_fops);
1534 proc_net_fops_create("rose_routes", S_IRUGO, &rose_routes_fops);
1539 unregister_netdev(dev_rose[i]);
1540 free_netdev(dev_rose[i]);
1543 out_proto_unregister:
1544 proto_unregister(&rose_proto);
1547 module_init(rose_proto_init);
1549 module_param(rose_ndevs, int, 0);
1550 MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1552 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1553 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1554 MODULE_LICENSE("GPL");
1555 MODULE_ALIAS_NETPROTO(PF_ROSE);
1557 static void __exit rose_exit(void)
1561 proc_net_remove("rose");
1562 proc_net_remove("rose_neigh");
1563 proc_net_remove("rose_nodes");
1564 proc_net_remove("rose_routes");
1565 rose_loopback_clear();
1569 ax25_protocol_release(AX25_P_ROSE);
1570 ax25_linkfail_release(rose_link_failed);
1572 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1573 ax25_listen_release(&rose_callsign, NULL);
1575 #ifdef CONFIG_SYSCTL
1576 rose_unregister_sysctl();
1578 unregister_netdevice_notifier(&rose_dev_notifier);
1580 sock_unregister(PF_ROSE);
1582 for (i = 0; i < rose_ndevs; i++) {
1583 struct net_device *dev = dev_rose[i];
1586 unregister_netdev(dev);
1592 proto_unregister(&rose_proto);
1595 module_exit(rose_exit);