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)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <linux/stat.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/skbuff.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
39 #include <linux/interrupt.h>
40 #include <linux/notifier.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
48 static int rose_ndevs = 10;
50 int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
51 int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
52 int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
53 int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
54 int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
55 int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
56 int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
57 int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
58 int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
59 int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
61 static HLIST_HEAD(rose_list);
62 static DEFINE_SPINLOCK(rose_list_lock);
64 static struct proto_ops rose_proto_ops;
66 ax25_address rose_callsign;
69 * Convert a ROSE address into text.
71 const char *rose2asc(const rose_address *addr)
73 static char buffer[11];
75 if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
76 addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
77 addr->rose_addr[4] == 0x00) {
80 sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
81 addr->rose_addr[1] & 0xFF,
82 addr->rose_addr[2] & 0xFF,
83 addr->rose_addr[3] & 0xFF,
84 addr->rose_addr[4] & 0xFF);
91 * Compare two ROSE addresses, 0 == equal.
93 int rosecmp(rose_address *addr1, rose_address *addr2)
97 for (i = 0; i < 5; i++)
98 if (addr1->rose_addr[i] != addr2->rose_addr[i])
105 * Compare two ROSE addresses for only mask digits, 0 == equal.
107 int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
114 for (i = 0; i < mask; i++) {
118 if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
121 if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
130 * Socket removal during an interrupt is now safe.
132 static void rose_remove_socket(struct sock *sk)
134 spin_lock_bh(&rose_list_lock);
135 sk_del_node_init(sk);
136 spin_unlock_bh(&rose_list_lock);
140 * Kill all bound sockets on a broken link layer connection to a
141 * particular neighbour.
143 void rose_kill_by_neigh(struct rose_neigh *neigh)
146 struct hlist_node *node;
148 spin_lock_bh(&rose_list_lock);
149 sk_for_each(s, node, &rose_list) {
150 struct rose_sock *rose = rose_sk(s);
152 if (rose->neighbour == neigh) {
153 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
154 rose->neighbour->use--;
155 rose->neighbour = NULL;
158 spin_unlock_bh(&rose_list_lock);
162 * Kill all bound sockets on a dropped device.
164 static void rose_kill_by_device(struct net_device *dev)
167 struct hlist_node *node;
169 spin_lock_bh(&rose_list_lock);
170 sk_for_each(s, node, &rose_list) {
171 struct rose_sock *rose = rose_sk(s);
173 if (rose->device == dev) {
174 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
175 rose->neighbour->use--;
179 spin_unlock_bh(&rose_list_lock);
183 * Handle device status changes.
185 static int rose_device_event(struct notifier_block *this, unsigned long event,
188 struct net_device *dev = (struct net_device *)ptr;
190 if (event != NETDEV_DOWN)
195 rose_kill_by_device(dev);
198 rose_link_device_down(dev);
199 rose_rt_device_down(dev);
207 * Add a socket to the bound sockets list.
209 static void rose_insert_socket(struct sock *sk)
212 spin_lock_bh(&rose_list_lock);
213 sk_add_node(sk, &rose_list);
214 spin_unlock_bh(&rose_list_lock);
218 * Find a socket that wants to accept the Call Request we just
221 static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
224 struct hlist_node *node;
226 spin_lock_bh(&rose_list_lock);
227 sk_for_each(s, node, &rose_list) {
228 struct rose_sock *rose = rose_sk(s);
230 if (!rosecmp(&rose->source_addr, addr) &&
231 !ax25cmp(&rose->source_call, call) &&
232 !rose->source_ndigis && s->sk_state == TCP_LISTEN)
236 sk_for_each(s, node, &rose_list) {
237 struct rose_sock *rose = rose_sk(s);
239 if (!rosecmp(&rose->source_addr, addr) &&
240 !ax25cmp(&rose->source_call, &null_ax25_address) &&
241 s->sk_state == TCP_LISTEN)
246 spin_unlock_bh(&rose_list_lock);
251 * Find a connected ROSE socket given my LCI and device.
253 struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
256 struct hlist_node *node;
258 spin_lock_bh(&rose_list_lock);
259 sk_for_each(s, node, &rose_list) {
260 struct rose_sock *rose = rose_sk(s);
262 if (rose->lci == lci && rose->neighbour == neigh)
267 spin_unlock_bh(&rose_list_lock);
272 * Find a unique LCI for a given device.
274 unsigned int rose_new_lci(struct rose_neigh *neigh)
278 if (neigh->dce_mode) {
279 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
280 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
283 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
284 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
294 void rose_destroy_socket(struct sock *);
297 * Handler for deferred kills.
299 static void rose_destroy_timer(unsigned long data)
301 rose_destroy_socket((struct sock *)data);
305 * This is called from user mode and the timers. Thus it protects itself
306 * against interrupt users but doesn't worry about being called during
307 * work. Once it is removed from the queue no interrupt or bottom half
308 * will touch it and we are (fairly 8-) ) safe.
310 void rose_destroy_socket(struct sock *sk)
314 rose_remove_socket(sk);
315 rose_stop_heartbeat(sk);
316 rose_stop_idletimer(sk);
319 rose_clear_queues(sk); /* Flush the queues */
321 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
322 if (skb->sk != sk) { /* A pending connection */
323 /* Queue the unaccepted socket for death */
324 sock_set_flag(skb->sk, SOCK_DEAD);
325 rose_start_heartbeat(skb->sk);
326 rose_sk(skb->sk)->state = ROSE_STATE_0;
332 if (atomic_read(&sk->sk_wmem_alloc) ||
333 atomic_read(&sk->sk_rmem_alloc)) {
334 /* Defer: outstanding buffers */
335 init_timer(&sk->sk_timer);
336 sk->sk_timer.expires = jiffies + 10 * HZ;
337 sk->sk_timer.function = rose_destroy_timer;
338 sk->sk_timer.data = (unsigned long)sk;
339 add_timer(&sk->sk_timer);
345 * Handling for system calls applied via the various interfaces to a
346 * ROSE socket object.
349 static int rose_setsockopt(struct socket *sock, int level, int optname,
350 char __user *optval, int optlen)
352 struct sock *sk = sock->sk;
353 struct rose_sock *rose = rose_sk(sk);
356 if (level != SOL_ROSE)
359 if (optlen < sizeof(int))
362 if (get_user(opt, (int __user *)optval))
367 rose->defer = opt ? 1 : 0;
397 rose->idle = opt * 60 * HZ;
401 rose->qbitincl = opt ? 1 : 0;
409 static int rose_getsockopt(struct socket *sock, int level, int optname,
410 char __user *optval, int __user *optlen)
412 struct sock *sk = sock->sk;
413 struct rose_sock *rose = rose_sk(sk);
417 if (level != SOL_ROSE)
420 if (get_user(len, optlen))
448 val = rose->idle / (60 * HZ);
452 val = rose->qbitincl;
459 len = min_t(unsigned int, len, sizeof(int));
461 if (put_user(len, optlen))
464 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
467 static int rose_listen(struct socket *sock, int backlog)
469 struct sock *sk = sock->sk;
471 if (sk->sk_state != TCP_LISTEN) {
472 struct rose_sock *rose = rose_sk(sk);
474 rose->dest_ndigis = 0;
475 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
476 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
477 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
478 sk->sk_max_ack_backlog = backlog;
479 sk->sk_state = TCP_LISTEN;
486 static struct proto rose_proto = {
488 .owner = THIS_MODULE,
489 .obj_size = sizeof(struct rose_sock),
492 static int rose_create(struct socket *sock, int protocol)
495 struct rose_sock *rose;
497 if (sock->type != SOCK_SEQPACKET || protocol != 0)
498 return -ESOCKTNOSUPPORT;
500 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
505 sock_init_data(sock, sk);
507 skb_queue_head_init(&rose->ack_queue);
509 skb_queue_head_init(&rose->frag_queue);
513 sock->ops = &rose_proto_ops;
514 sk->sk_protocol = protocol;
516 init_timer(&rose->timer);
517 init_timer(&rose->idletimer);
519 rose->t1 = sysctl_rose_call_request_timeout;
520 rose->t2 = sysctl_rose_reset_request_timeout;
521 rose->t3 = sysctl_rose_clear_request_timeout;
522 rose->hb = sysctl_rose_ack_hold_back_timeout;
523 rose->idle = sysctl_rose_no_activity_timeout;
525 rose->state = ROSE_STATE_0;
530 static struct sock *rose_make_new(struct sock *osk)
533 struct rose_sock *rose, *orose;
535 if (osk->sk_type != SOCK_SEQPACKET)
538 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
543 sock_init_data(NULL, sk);
545 skb_queue_head_init(&rose->ack_queue);
547 skb_queue_head_init(&rose->frag_queue);
551 sk->sk_type = osk->sk_type;
552 sk->sk_socket = osk->sk_socket;
553 sk->sk_priority = osk->sk_priority;
554 sk->sk_protocol = osk->sk_protocol;
555 sk->sk_rcvbuf = osk->sk_rcvbuf;
556 sk->sk_sndbuf = osk->sk_sndbuf;
557 sk->sk_state = TCP_ESTABLISHED;
558 sk->sk_sleep = osk->sk_sleep;
560 if (sock_flag(osk, SOCK_ZAPPED))
561 sock_set_flag(sk, SOCK_ZAPPED);
563 if (sock_flag(osk, SOCK_DBG))
564 sock_set_flag(sk, SOCK_DBG);
566 init_timer(&rose->timer);
567 init_timer(&rose->idletimer);
569 orose = rose_sk(osk);
570 rose->t1 = orose->t1;
571 rose->t2 = orose->t2;
572 rose->t3 = orose->t3;
573 rose->hb = orose->hb;
574 rose->idle = orose->idle;
575 rose->defer = orose->defer;
576 rose->device = orose->device;
577 rose->qbitincl = orose->qbitincl;
582 static int rose_release(struct socket *sock)
584 struct sock *sk = sock->sk;
585 struct rose_sock *rose;
587 if (sk == NULL) return 0;
591 switch (rose->state) {
593 rose_disconnect(sk, 0, -1, -1);
594 rose_destroy_socket(sk);
598 rose->neighbour->use--;
599 rose_disconnect(sk, 0, -1, -1);
600 rose_destroy_socket(sk);
607 rose_clear_queues(sk);
608 rose_stop_idletimer(sk);
609 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
610 rose_start_t3timer(sk);
611 rose->state = ROSE_STATE_2;
612 sk->sk_state = TCP_CLOSE;
613 sk->sk_shutdown |= SEND_SHUTDOWN;
614 sk->sk_state_change(sk);
615 sock_set_flag(sk, SOCK_DEAD);
616 sock_set_flag(sk, SOCK_DESTROY);
628 static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
630 struct sock *sk = sock->sk;
631 struct rose_sock *rose = rose_sk(sk);
632 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
633 struct net_device *dev;
634 ax25_address *user, *source;
637 if (!sock_flag(sk, SOCK_ZAPPED))
640 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
643 if (addr->srose_family != AF_ROSE)
646 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
649 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
652 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
653 SOCK_DEBUG(sk, "ROSE: bind failed: invalid address\n");
654 return -EADDRNOTAVAIL;
657 source = &addr->srose_call;
659 if ((user = ax25_findbyuid(current->euid)) == NULL) {
660 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
665 rose->source_addr = addr->srose_addr;
666 rose->source_call = *user;
668 rose->source_ndigis = addr->srose_ndigis;
670 if (addr_len == sizeof(struct full_sockaddr_rose)) {
671 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
672 for (n = 0 ; n < addr->srose_ndigis ; n++)
673 rose->source_digis[n] = full_addr->srose_digis[n];
675 if (rose->source_ndigis == 1) {
676 rose->source_digis[0] = addr->srose_digi;
680 rose_insert_socket(sk);
682 sock_reset_flag(sk, SOCK_ZAPPED);
683 SOCK_DEBUG(sk, "ROSE: socket is bound\n");
687 static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
689 struct sock *sk = sock->sk;
690 struct rose_sock *rose = rose_sk(sk);
691 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
692 unsigned char cause, diagnostic;
694 struct net_device *dev;
697 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
698 sock->state = SS_CONNECTED;
699 return 0; /* Connect completed during a ERESTARTSYS event */
702 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
703 sock->state = SS_UNCONNECTED;
704 return -ECONNREFUSED;
707 if (sk->sk_state == TCP_ESTABLISHED)
708 return -EISCONN; /* No reconnect on a seqpacket socket */
710 sk->sk_state = TCP_CLOSE;
711 sock->state = SS_UNCONNECTED;
713 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
716 if (addr->srose_family != AF_ROSE)
719 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
722 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
725 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
726 if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
729 rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
731 if (!rose->neighbour)
734 rose->lci = rose_new_lci(rose->neighbour);
738 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
739 sock_reset_flag(sk, SOCK_ZAPPED);
741 if ((dev = rose_dev_first()) == NULL)
744 if ((user = ax25_findbyuid(current->euid)) == NULL)
747 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
748 rose->source_call = *user;
751 rose_insert_socket(sk); /* Finish the bind */
754 rose->dest_addr = addr->srose_addr;
755 rose->dest_call = addr->srose_call;
756 rose->rand = ((long)rose & 0xFFFF) + rose->lci;
757 rose->dest_ndigis = addr->srose_ndigis;
759 if (addr_len == sizeof(struct full_sockaddr_rose)) {
760 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
761 for (n = 0 ; n < addr->srose_ndigis ; n++)
762 rose->dest_digis[n] = full_addr->srose_digis[n];
764 if (rose->dest_ndigis == 1) {
765 rose->dest_digis[0] = addr->srose_digi;
769 /* Move to connecting socket, start sending Connect Requests */
770 sock->state = SS_CONNECTING;
771 sk->sk_state = TCP_SYN_SENT;
773 rose->state = ROSE_STATE_1;
775 rose->neighbour->use++;
777 rose_write_internal(sk, ROSE_CALL_REQUEST);
778 rose_start_heartbeat(sk);
779 rose_start_t1timer(sk);
782 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
786 * A Connect Ack with Choke or timeout or failed routing will go to
789 if (sk->sk_state == TCP_SYN_SENT) {
790 struct task_struct *tsk = current;
791 DECLARE_WAITQUEUE(wait, tsk);
793 add_wait_queue(sk->sk_sleep, &wait);
795 set_current_state(TASK_INTERRUPTIBLE);
796 if (sk->sk_state != TCP_SYN_SENT)
798 if (!signal_pending(tsk)) {
802 current->state = TASK_RUNNING;
803 remove_wait_queue(sk->sk_sleep, &wait);
806 current->state = TASK_RUNNING;
807 remove_wait_queue(sk->sk_sleep, &wait);
810 if (sk->sk_state != TCP_ESTABLISHED) {
811 sock->state = SS_UNCONNECTED;
812 return sock_error(sk); /* Always set at this point */
815 sock->state = SS_CONNECTED;
820 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
822 struct task_struct *tsk = current;
823 DECLARE_WAITQUEUE(wait, tsk);
829 if ((sk = sock->sk) == NULL)
833 if (sk->sk_type != SOCK_SEQPACKET) {
838 if (sk->sk_state != TCP_LISTEN) {
844 * The write queue this time is holding sockets ready to use
845 * hooked into the SABM we saved
847 add_wait_queue(sk->sk_sleep, &wait);
849 skb = skb_dequeue(&sk->sk_receive_queue);
853 current->state = TASK_INTERRUPTIBLE;
855 if (flags & O_NONBLOCK) {
856 current->state = TASK_RUNNING;
857 remove_wait_queue(sk->sk_sleep, &wait);
860 if (!signal_pending(tsk)) {
867 current->state = TASK_RUNNING;
868 remove_wait_queue(sk->sk_sleep, &wait);
871 newsk->sk_socket = newsock;
872 newsk->sk_sleep = &newsock->wait;
874 /* Now attach up the new socket */
877 sk->sk_ack_backlog--;
886 static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
887 int *uaddr_len, int peer)
889 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
890 struct sock *sk = sock->sk;
891 struct rose_sock *rose = rose_sk(sk);
895 if (sk->sk_state != TCP_ESTABLISHED)
897 srose->srose_family = AF_ROSE;
898 srose->srose_addr = rose->dest_addr;
899 srose->srose_call = rose->dest_call;
900 srose->srose_ndigis = rose->dest_ndigis;
901 for (n = 0; n < rose->dest_ndigis; n++)
902 srose->srose_digis[n] = rose->dest_digis[n];
904 srose->srose_family = AF_ROSE;
905 srose->srose_addr = rose->source_addr;
906 srose->srose_call = rose->source_call;
907 srose->srose_ndigis = rose->source_ndigis;
908 for (n = 0; n < rose->source_ndigis; n++)
909 srose->srose_digis[n] = rose->source_digis[n];
912 *uaddr_len = sizeof(struct full_sockaddr_rose);
916 int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
920 struct rose_sock *make_rose;
921 struct rose_facilities_struct facilities;
924 skb->sk = NULL; /* Initially we don't know who it's for */
927 * skb->data points to the rose frame start
929 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
931 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
932 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
933 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
934 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
938 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
941 * We can't accept the Call Request.
943 if (sk == NULL || sk_acceptq_is_full(sk) ||
944 (make = rose_make_new(sk)) == NULL) {
945 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
950 make->sk_state = TCP_ESTABLISHED;
951 make_rose = rose_sk(make);
953 make_rose->lci = lci;
954 make_rose->dest_addr = facilities.dest_addr;
955 make_rose->dest_call = facilities.dest_call;
956 make_rose->dest_ndigis = facilities.dest_ndigis;
957 for (n = 0 ; n < facilities.dest_ndigis ; n++)
958 make_rose->dest_digis[n] = facilities.dest_digis[n];
959 make_rose->source_addr = facilities.source_addr;
960 make_rose->source_call = facilities.source_call;
961 make_rose->source_ndigis = facilities.source_ndigis;
962 for (n = 0 ; n < facilities.source_ndigis ; n++)
963 make_rose->source_digis[n]= facilities.source_digis[n];
964 make_rose->neighbour = neigh;
965 make_rose->device = dev;
966 make_rose->facilities = facilities;
968 make_rose->neighbour->use++;
970 if (rose_sk(sk)->defer) {
971 make_rose->state = ROSE_STATE_5;
973 rose_write_internal(make, ROSE_CALL_ACCEPTED);
974 make_rose->state = ROSE_STATE_3;
975 rose_start_idletimer(make);
978 make_rose->condition = 0x00;
983 sk->sk_ack_backlog++;
985 rose_insert_socket(make);
987 skb_queue_head(&sk->sk_receive_queue, skb);
989 rose_start_heartbeat(make);
991 if (!sock_flag(sk, SOCK_DEAD))
992 sk->sk_data_ready(sk, skb->len);
997 static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
998 struct msghdr *msg, size_t len)
1000 struct sock *sk = sock->sk;
1001 struct rose_sock *rose = rose_sk(sk);
1002 struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1004 struct full_sockaddr_rose srose;
1005 struct sk_buff *skb;
1006 unsigned char *asmptr;
1007 int n, size, qbit = 0;
1009 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1012 if (sock_flag(sk, SOCK_ZAPPED))
1013 return -EADDRNOTAVAIL;
1015 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1016 send_sig(SIGPIPE, current, 0);
1020 if (rose->neighbour == NULL || rose->device == NULL)
1021 return -ENETUNREACH;
1023 if (usrose != NULL) {
1024 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1026 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1027 memcpy(&srose, usrose, msg->msg_namelen);
1028 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1029 ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1031 if (srose.srose_ndigis != rose->dest_ndigis)
1033 if (srose.srose_ndigis == rose->dest_ndigis) {
1034 for (n = 0 ; n < srose.srose_ndigis ; n++)
1035 if (ax25cmp(&rose->dest_digis[n],
1036 &srose.srose_digis[n]))
1039 if (srose.srose_family != AF_ROSE)
1042 if (sk->sk_state != TCP_ESTABLISHED)
1045 srose.srose_family = AF_ROSE;
1046 srose.srose_addr = rose->dest_addr;
1047 srose.srose_call = rose->dest_call;
1048 srose.srose_ndigis = rose->dest_ndigis;
1049 for (n = 0 ; n < rose->dest_ndigis ; n++)
1050 srose.srose_digis[n] = rose->dest_digis[n];
1053 SOCK_DEBUG(sk, "ROSE: sendto: Addresses built.\n");
1055 /* Build a packet */
1056 SOCK_DEBUG(sk, "ROSE: sendto: building packet.\n");
1057 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1059 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1062 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1065 * Put the data on the end
1067 SOCK_DEBUG(sk, "ROSE: Appending user data\n");
1069 asmptr = skb->h.raw = skb_put(skb, len);
1071 err = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1078 * If the Q BIT Include socket option is in force, the first
1079 * byte of the user data is the logical value of the Q Bit.
1081 if (rose->qbitincl) {
1082 qbit = skb->data[0];
1087 * Push down the ROSE header
1089 asmptr = skb_push(skb, ROSE_MIN_LEN);
1091 SOCK_DEBUG(sk, "ROSE: Building Network Header.\n");
1093 /* Build a ROSE Network header */
1094 asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1095 asmptr[1] = (rose->lci >> 0) & 0xFF;
1096 asmptr[2] = ROSE_DATA;
1099 asmptr[0] |= ROSE_Q_BIT;
1101 SOCK_DEBUG(sk, "ROSE: Built header.\n");
1103 SOCK_DEBUG(sk, "ROSE: Transmitting buffer\n");
1105 if (sk->sk_state != TCP_ESTABLISHED) {
1111 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1112 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1113 unsigned char header[ROSE_MIN_LEN];
1114 struct sk_buff *skbn;
1118 /* Save a copy of the Header */
1119 memcpy(header, skb->data, ROSE_MIN_LEN);
1120 skb_pull(skb, ROSE_MIN_LEN);
1122 frontlen = skb_headroom(skb);
1124 while (skb->len > 0) {
1125 if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1134 skb_reserve(skbn, frontlen);
1136 lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1138 /* Copy the user data */
1139 memcpy(skb_put(skbn, lg), skb->data, lg);
1142 /* Duplicate the Header */
1143 skb_push(skbn, ROSE_MIN_LEN);
1144 memcpy(skbn->data, header, ROSE_MIN_LEN);
1147 skbn->data[2] |= M_BIT;
1149 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1155 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
1158 skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
1167 static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1168 struct msghdr *msg, size_t size, int flags)
1170 struct sock *sk = sock->sk;
1171 struct rose_sock *rose = rose_sk(sk);
1172 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1174 unsigned char *asmptr;
1175 struct sk_buff *skb;
1179 * This works for seqpacket too. The receiver has ordered the queue for
1180 * us! We do one quick check first though
1182 if (sk->sk_state != TCP_ESTABLISHED)
1185 /* Now we can treat all alike */
1186 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1189 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1191 skb_pull(skb, ROSE_MIN_LEN);
1193 if (rose->qbitincl) {
1194 asmptr = skb_push(skb, 1);
1198 skb->h.raw = skb->data;
1201 if (copied > size) {
1203 msg->msg_flags |= MSG_TRUNC;
1206 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1208 if (srose != NULL) {
1209 srose->srose_family = AF_ROSE;
1210 srose->srose_addr = rose->dest_addr;
1211 srose->srose_call = rose->dest_call;
1212 srose->srose_ndigis = rose->dest_ndigis;
1213 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) {
1214 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1215 for (n = 0 ; n < rose->dest_ndigis ; n++)
1216 full_srose->srose_digis[n] = rose->dest_digis[n];
1217 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1219 if (rose->dest_ndigis >= 1) {
1220 srose->srose_ndigis = 1;
1221 srose->srose_digi = rose->dest_digis[0];
1223 msg->msg_namelen = sizeof(struct sockaddr_rose);
1227 skb_free_datagram(sk, skb);
1233 static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1235 struct sock *sk = sock->sk;
1236 struct rose_sock *rose = rose_sk(sk);
1237 void __user *argp = (void __user *)arg;
1242 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1245 return put_user(amount, (unsigned int __user *)argp);
1249 struct sk_buff *skb;
1251 /* These two are safe on a single CPU system as only user tasks fiddle here */
1252 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1254 return put_user(amount, (unsigned int __user *)argp);
1259 return sock_get_timestamp(sk, (struct timeval __user *)argp);
1264 case SIOCGIFDSTADDR:
1265 case SIOCSIFDSTADDR:
1266 case SIOCGIFBRDADDR:
1267 case SIOCSIFBRDADDR:
1268 case SIOCGIFNETMASK:
1269 case SIOCSIFNETMASK:
1277 if (!capable(CAP_NET_ADMIN))
1279 return rose_rt_ioctl(cmd, argp);
1281 case SIOCRSGCAUSE: {
1282 struct rose_cause_struct rose_cause;
1283 rose_cause.cause = rose->cause;
1284 rose_cause.diagnostic = rose->diagnostic;
1285 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1288 case SIOCRSSCAUSE: {
1289 struct rose_cause_struct rose_cause;
1290 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1292 rose->cause = rose_cause.cause;
1293 rose->diagnostic = rose_cause.diagnostic;
1298 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1299 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1300 ax25_listen_release(&rose_callsign, NULL);
1301 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1303 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1304 ax25_listen_register(&rose_callsign, NULL);
1308 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1311 if (rose->state == ROSE_STATE_5) {
1312 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1313 rose_start_idletimer(sk);
1314 rose->condition = 0x00;
1319 rose->state = ROSE_STATE_3;
1324 return dev_ioctl(cmd, argp);
1330 #ifdef CONFIG_PROC_FS
1331 static void *rose_info_start(struct seq_file *seq, loff_t *pos)
1335 struct hlist_node *node;
1337 spin_lock_bh(&rose_list_lock);
1339 return SEQ_START_TOKEN;
1342 sk_for_each(s, node, &rose_list) {
1350 static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1354 return (v == SEQ_START_TOKEN) ? sk_head(&rose_list)
1355 : sk_next((struct sock *)v);
1358 static void rose_info_stop(struct seq_file *seq, void *v)
1360 spin_unlock_bh(&rose_list_lock);
1363 static int rose_info_show(struct seq_file *seq, void *v)
1365 if (v == SEQ_START_TOKEN)
1367 "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");
1371 struct rose_sock *rose = rose_sk(s);
1372 const char *devname, *callsign;
1373 const struct net_device *dev = rose->device;
1378 devname = dev->name;
1380 seq_printf(seq, "%-10s %-9s ",
1381 rose2asc(&rose->dest_addr),
1382 ax2asc(&rose->dest_call));
1384 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1385 callsign = "??????-?";
1387 callsign = ax2asc(&rose->source_call);
1390 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1391 rose2asc(&rose->source_addr),
1395 (rose->neighbour) ? rose->neighbour->number : 0,
1400 ax25_display_timer(&rose->timer) / HZ,
1405 ax25_display_timer(&rose->idletimer) / (60 * HZ),
1406 rose->idle / (60 * HZ),
1407 atomic_read(&s->sk_wmem_alloc),
1408 atomic_read(&s->sk_rmem_alloc),
1409 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1415 static struct seq_operations rose_info_seqops = {
1416 .start = rose_info_start,
1417 .next = rose_info_next,
1418 .stop = rose_info_stop,
1419 .show = rose_info_show,
1422 static int rose_info_open(struct inode *inode, struct file *file)
1424 return seq_open(file, &rose_info_seqops);
1427 static struct file_operations rose_info_fops = {
1428 .owner = THIS_MODULE,
1429 .open = rose_info_open,
1431 .llseek = seq_lseek,
1432 .release = seq_release,
1434 #endif /* CONFIG_PROC_FS */
1436 static struct net_proto_family rose_family_ops = {
1438 .create = rose_create,
1439 .owner = THIS_MODULE,
1442 static struct proto_ops rose_proto_ops = {
1444 .owner = THIS_MODULE,
1445 .release = rose_release,
1447 .connect = rose_connect,
1448 .socketpair = sock_no_socketpair,
1449 .accept = rose_accept,
1450 .getname = rose_getname,
1451 .poll = datagram_poll,
1452 .ioctl = rose_ioctl,
1453 .listen = rose_listen,
1454 .shutdown = sock_no_shutdown,
1455 .setsockopt = rose_setsockopt,
1456 .getsockopt = rose_getsockopt,
1457 .sendmsg = rose_sendmsg,
1458 .recvmsg = rose_recvmsg,
1459 .mmap = sock_no_mmap,
1460 .sendpage = sock_no_sendpage,
1463 static struct notifier_block rose_dev_notifier = {
1464 .notifier_call = rose_device_event,
1467 static struct net_device **dev_rose;
1469 static const char banner[] = KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.4\n";
1471 static int __init rose_proto_init(void)
1474 int rc = proto_register(&rose_proto, 0);
1479 rose_callsign = null_ax25_address;
1481 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1482 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1486 dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1487 if (dev_rose == NULL) {
1488 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1492 memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device*));
1493 for (i = 0; i < rose_ndevs; i++) {
1494 struct net_device *dev;
1495 char name[IFNAMSIZ];
1497 sprintf(name, "rose%d", i);
1498 dev = alloc_netdev(sizeof(struct net_device_stats),
1501 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
1504 if (register_netdev(dev)) {
1505 printk(KERN_ERR "ROSE: netdevice regeistration failed\n");
1512 sock_register(&rose_family_ops);
1513 register_netdevice_notifier(&rose_dev_notifier);
1516 ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
1517 ax25_linkfail_register(rose_link_failed);
1519 #ifdef CONFIG_SYSCTL
1520 rose_register_sysctl();
1522 rose_loopback_init();
1524 rose_add_loopback_neigh();
1526 proc_net_fops_create("rose", S_IRUGO, &rose_info_fops);
1527 proc_net_fops_create("rose_neigh", S_IRUGO, &rose_neigh_fops);
1528 proc_net_fops_create("rose_nodes", S_IRUGO, &rose_nodes_fops);
1529 proc_net_fops_create("rose_routes", S_IRUGO, &rose_routes_fops);
1534 unregister_netdev(dev_rose[i]);
1535 free_netdev(dev_rose[i]);
1538 proto_unregister(&rose_proto);
1541 module_init(rose_proto_init);
1543 module_param(rose_ndevs, int, 0);
1544 MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1546 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1547 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1548 MODULE_LICENSE("GPL");
1549 MODULE_ALIAS_NETPROTO(PF_ROSE);
1551 static void __exit rose_exit(void)
1555 proc_net_remove("rose");
1556 proc_net_remove("rose_neigh");
1557 proc_net_remove("rose_nodes");
1558 proc_net_remove("rose_routes");
1559 rose_loopback_clear();
1563 ax25_protocol_release(AX25_P_ROSE);
1564 ax25_linkfail_release(rose_link_failed);
1566 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1567 ax25_listen_release(&rose_callsign, NULL);
1569 #ifdef CONFIG_SYSCTL
1570 rose_unregister_sysctl();
1572 unregister_netdevice_notifier(&rose_dev_notifier);
1574 sock_unregister(PF_ROSE);
1576 for (i = 0; i < rose_ndevs; i++) {
1577 struct net_device *dev = dev_rose[i];
1580 unregister_netdev(dev);
1586 proto_unregister(&rose_proto);
1589 module_exit(rose_exit);