4 * This code REQUIRES 2.1.15 or higher/ NET3.038
7 * This module is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * ROSE 001 Jonathan(G4KLX) Cloned from af_netrom.c.
14 * Alan(GW4PTS) Hacked up for newer API stuff
15 * Terry (VK2KTJ) Added support for variable length
17 * ROSE 002 Jonathan(G4KLX) Changed hdrincl to qbitincl.
18 * Added random number facilities entry.
19 * Variable number of ROSE devices.
20 * ROSE 003 Jonathan(G4KLX) New timer architecture.
21 * Implemented idle timer.
22 * Added use count to neighbour.
23 * Tomi(OH2BNS) Fixed rose_getname().
24 * Arnaldo C. Melo s/suser/capable/ + micro cleanups
25 * Joroen (PE1RXQ) Use sock_orphan() on release.
27 * ROSE 0.63 Jean-Paul(F6FBB) Fixed wrong length of L3 packets
28 * Added CLEAR_REQUEST facilities
29 * ROSE 0.64 Jean-Paul(F6FBB) Fixed null pointer in rose_kill_by_device
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/errno.h>
36 #include <linux/types.h>
37 #include <linux/socket.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/timer.h>
42 #include <linux/string.h>
43 #include <linux/sockios.h>
44 #include <linux/net.h>
45 #include <linux/stat.h>
47 #include <linux/inet.h>
48 #include <linux/netdevice.h>
49 #include <linux/if_arp.h>
50 #include <linux/skbuff.h>
52 #include <asm/segment.h>
53 #include <asm/system.h>
54 #include <asm/uaccess.h>
55 #include <linux/fcntl.h>
56 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
58 #include <linux/interrupt.h>
59 #include <linux/notifier.h>
61 #include <linux/proc_fs.h>
67 int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
68 int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
69 int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
70 int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
71 int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
72 int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
73 int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
74 int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
75 int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
76 int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
78 static struct sock *rose_list;
80 static struct proto_ops rose_proto_ops;
82 ax25_address rose_callsign;
85 * Convert a ROSE address into text.
87 char *rose2asc(rose_address *addr)
89 static char buffer[11];
91 if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
92 addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
93 addr->rose_addr[4] == 0x00) {
96 sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
97 addr->rose_addr[1] & 0xFF,
98 addr->rose_addr[2] & 0xFF,
99 addr->rose_addr[3] & 0xFF,
100 addr->rose_addr[4] & 0xFF);
107 * Compare two ROSE addresses, 0 == equal.
109 int rosecmp(rose_address *addr1, rose_address *addr2)
113 for (i = 0; i < 5; i++)
114 if (addr1->rose_addr[i] != addr2->rose_addr[i])
121 * Compare two ROSE addresses for only mask digits, 0 == equal.
123 int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
130 for (i = 0; i < mask; i++) {
134 if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
137 if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
145 static void rose_free_sock(struct sock *sk)
152 static struct sock *rose_alloc_sock(void)
157 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, 1)) == NULL)
160 if ((rose = kmalloc(sizeof(*rose), GFP_ATOMIC)) == NULL) {
167 memset(rose, 0x00, sizeof(*rose));
169 sk->protinfo.rose = rose;
176 * Socket removal during an interrupt is now safe.
178 static void rose_remove_socket(struct sock *sk)
183 save_flags(flags); cli();
185 if ((s = rose_list) == sk) {
187 restore_flags(flags);
191 while (s != NULL && s->next != NULL) {
194 restore_flags(flags);
201 restore_flags(flags);
205 * Kill all bound sockets on a broken link layer connection to a
206 * particular neighbour.
208 void rose_kill_by_neigh(struct rose_neigh *neigh)
212 for (s = rose_list; s != NULL; s = s->next) {
213 if (s->protinfo.rose->neighbour == neigh) {
214 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
215 s->protinfo.rose->neighbour->use--;
216 s->protinfo.rose->neighbour = NULL;
222 * Kill all bound sockets on a dropped device.
224 static void rose_kill_by_device(struct net_device *dev)
228 for (s = rose_list; s != NULL; s = s->next) {
229 if (s->protinfo.rose->device == dev) {
230 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
231 if (s->protinfo.rose->neighbour)
232 s->protinfo.rose->neighbour->use--;
233 s->protinfo.rose->device = NULL;
239 * Handle device status changes.
241 static int rose_device_event(struct notifier_block *this, unsigned long event, void *ptr)
243 struct net_device *dev = (struct net_device *)ptr;
245 if (event != NETDEV_DOWN)
250 rose_kill_by_device(dev);
253 rose_link_device_down(dev);
254 rose_rt_device_down(dev);
262 * Add a socket to the bound sockets list.
264 static void rose_insert_socket(struct sock *sk)
268 save_flags(flags); cli();
270 sk->next = rose_list;
273 restore_flags(flags);
277 * Find a socket that wants to accept the Call Request we just
280 static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
285 save_flags(flags); cli();
287 for (s = rose_list; s != NULL; s = s->next) {
288 if (rosecmp(&s->protinfo.rose->source_addr, addr) == 0 && ax25cmp(&s->protinfo.rose->source_call, call) == 0 && s->protinfo.rose->source_ndigis == 0 && s->state == TCP_LISTEN) {
289 restore_flags(flags);
294 for (s = rose_list; s != NULL; s = s->next) {
295 if (rosecmp(&s->protinfo.rose->source_addr, addr) == 0 && ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0 && s->state == TCP_LISTEN) {
296 restore_flags(flags);
301 restore_flags(flags);
306 * Find a connected ROSE socket given my LCI and device.
308 struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
313 save_flags(flags); cli();
315 for (s = rose_list; s != NULL; s = s->next) {
316 if (s->protinfo.rose->lci == lci && s->protinfo.rose->neighbour == neigh) {
317 restore_flags(flags);
322 restore_flags(flags);
328 * Find a unique LCI for a given device.
330 unsigned int rose_new_lci(struct rose_neigh *neigh)
334 if (neigh->dce_mode) {
335 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
336 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
339 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
340 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
350 void rose_destroy_socket(struct sock *);
353 * Handler for deferred kills.
355 static void rose_destroy_timer(unsigned long data)
357 rose_destroy_socket((struct sock *)data);
361 * This is called from user mode and the timers. Thus it protects itself against
362 * interrupt users but doesn't worry about being called during work.
363 * Once it is removed from the queue no interrupt or bottom half will
364 * touch it and we are (fairly 8-) ) safe.
366 void rose_destroy_socket(struct sock *sk) /* Not static as it's used by the timer */
371 save_flags(flags); cli();
373 rose_stop_heartbeat(sk);
374 rose_stop_idletimer(sk);
377 rose_remove_socket(sk);
378 rose_clear_queues(sk); /* Flush the queues */
380 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
381 if (skb->sk != sk) { /* A pending connection */
382 skb->sk->dead = 1; /* Queue the unaccepted socket for death */
383 rose_start_heartbeat(skb->sk);
384 skb->sk->protinfo.rose->state = ROSE_STATE_0;
390 if (atomic_read(&sk->wmem_alloc) != 0 || atomic_read(&sk->rmem_alloc) != 0) {
391 /* Defer: outstanding buffers */
392 init_timer(&sk->timer);
393 sk->timer.expires = jiffies + 10 * HZ;
394 sk->timer.function = rose_destroy_timer;
395 sk->timer.data = (unsigned long)sk;
396 add_timer(&sk->timer);
401 restore_flags(flags);
405 * Handling for system calls applied via the various interfaces to a
406 * ROSE socket object.
409 static int rose_setsockopt(struct socket *sock, int level, int optname,
410 char *optval, int optlen)
412 struct sock *sk = sock->sk;
415 if (level != SOL_ROSE)
418 if (optlen < sizeof(int))
421 if (get_user(opt, (int *)optval))
426 sk->protinfo.rose->defer = opt ? 1 : 0;
432 sk->protinfo.rose->t1 = opt * HZ;
438 sk->protinfo.rose->t2 = opt * HZ;
444 sk->protinfo.rose->t3 = opt * HZ;
450 sk->protinfo.rose->hb = opt * HZ;
456 sk->protinfo.rose->idle = opt * 60 * HZ;
460 sk->protinfo.rose->qbitincl = opt ? 1 : 0;
468 static int rose_getsockopt(struct socket *sock, int level, int optname,
469 char *optval, int *optlen)
471 struct sock *sk = sock->sk;
475 if (level != SOL_ROSE)
478 if (get_user(len, optlen))
486 val = sk->protinfo.rose->defer;
490 val = sk->protinfo.rose->t1 / HZ;
494 val = sk->protinfo.rose->t2 / HZ;
498 val = sk->protinfo.rose->t3 / HZ;
502 val = sk->protinfo.rose->hb / HZ;
506 val = sk->protinfo.rose->idle / (60 * HZ);
510 val = sk->protinfo.rose->qbitincl;
517 len = min_t(unsigned int, len, sizeof(int));
519 if (put_user(len, optlen))
522 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
525 static int rose_listen(struct socket *sock, int backlog)
527 struct sock *sk = sock->sk;
529 if (sk->state != TCP_LISTEN) {
530 sk->protinfo.rose->dest_ndigis = 0;
531 memset(&sk->protinfo.rose->dest_addr, '\0', ROSE_ADDR_LEN);
532 memset(&sk->protinfo.rose->dest_call, '\0', AX25_ADDR_LEN);
533 memset(sk->protinfo.rose->dest_digis, '\0', AX25_ADDR_LEN*ROSE_MAX_DIGIS);
534 sk->max_ack_backlog = backlog;
535 sk->state = TCP_LISTEN;
542 static int rose_create(struct socket *sock, int protocol)
547 if (sock->type != SOCK_SEQPACKET || protocol != 0)
548 return -ESOCKTNOSUPPORT;
550 if ((sk = rose_alloc_sock()) == NULL)
553 rose = sk->protinfo.rose;
555 sock_init_data(sock, sk);
557 skb_queue_head_init(&rose->ack_queue);
559 skb_queue_head_init(&rose->frag_queue);
563 sock->ops = &rose_proto_ops;
564 sk->protocol = protocol;
566 init_timer(&rose->timer);
567 init_timer(&rose->idletimer);
569 rose->t1 = sysctl_rose_call_request_timeout;
570 rose->t2 = sysctl_rose_reset_request_timeout;
571 rose->t3 = sysctl_rose_clear_request_timeout;
572 rose->hb = sysctl_rose_ack_hold_back_timeout;
573 rose->idle = sysctl_rose_no_activity_timeout;
575 rose->state = ROSE_STATE_0;
580 static struct sock *rose_make_new(struct sock *osk)
585 if (osk->type != SOCK_SEQPACKET)
588 if ((sk = rose_alloc_sock()) == NULL)
591 rose = sk->protinfo.rose;
593 sock_init_data(NULL, sk);
595 skb_queue_head_init(&rose->ack_queue);
597 skb_queue_head_init(&rose->frag_queue);
601 sk->type = osk->type;
602 sk->socket = osk->socket;
603 sk->priority = osk->priority;
604 sk->protocol = osk->protocol;
605 sk->rcvbuf = osk->rcvbuf;
606 sk->sndbuf = osk->sndbuf;
607 sk->debug = osk->debug;
608 sk->state = TCP_ESTABLISHED;
609 sk->sleep = osk->sleep;
610 sk->zapped = osk->zapped;
612 init_timer(&rose->timer);
613 init_timer(&rose->idletimer);
615 rose->t1 = osk->protinfo.rose->t1;
616 rose->t2 = osk->protinfo.rose->t2;
617 rose->t3 = osk->protinfo.rose->t3;
618 rose->hb = osk->protinfo.rose->hb;
619 rose->idle = osk->protinfo.rose->idle;
621 rose->defer = osk->protinfo.rose->defer;
622 rose->device = osk->protinfo.rose->device;
623 rose->qbitincl = osk->protinfo.rose->qbitincl;
628 static int rose_release(struct socket *sock)
630 struct sock *sk = sock->sk;
632 if (sk == NULL) return 0;
634 switch (sk->protinfo.rose->state) {
637 rose_disconnect(sk, 0, -1, -1);
638 rose_destroy_socket(sk);
642 sk->protinfo.rose->neighbour->use--;
643 rose_disconnect(sk, 0, -1, -1);
644 rose_destroy_socket(sk);
651 rose_clear_queues(sk);
652 rose_stop_idletimer(sk);
653 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
654 rose_start_t3timer(sk);
655 sk->protinfo.rose->state = ROSE_STATE_2;
656 sk->state = TCP_CLOSE;
657 sk->shutdown |= SEND_SHUTDOWN;
658 sk->state_change(sk);
673 static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
675 struct sock *sk = sock->sk;
676 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
677 struct net_device *dev;
678 ax25_address *user, *source;
684 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
687 if (addr->srose_family != AF_ROSE)
690 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
693 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
696 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
697 SOCK_DEBUG(sk, "ROSE: bind failed: invalid address\n");
698 return -EADDRNOTAVAIL;
701 source = &addr->srose_call;
703 if ((user = ax25_findbyuid(current->euid)) == NULL) {
704 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
709 sk->protinfo.rose->source_addr = addr->srose_addr;
710 sk->protinfo.rose->source_call = *user;
711 sk->protinfo.rose->device = dev;
712 sk->protinfo.rose->source_ndigis = addr->srose_ndigis;
714 if (addr_len == sizeof(struct full_sockaddr_rose)) {
715 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
716 for (n = 0 ; n < addr->srose_ndigis ; n++)
717 sk->protinfo.rose->source_digis[n] = full_addr->srose_digis[n];
719 if (sk->protinfo.rose->source_ndigis == 1) {
720 sk->protinfo.rose->source_digis[0] = addr->srose_digi;
724 rose_insert_socket(sk);
727 SOCK_DEBUG(sk, "ROSE: socket is bound\n");
731 static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
733 struct sock *sk = sock->sk;
734 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
735 unsigned char cause, diagnostic;
737 struct net_device *dev;
740 if (sk->state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
741 sock->state = SS_CONNECTED;
742 return 0; /* Connect completed during a ERESTARTSYS event */
745 if (sk->state == TCP_CLOSE && sock->state == SS_CONNECTING) {
746 sock->state = SS_UNCONNECTED;
747 return -ECONNREFUSED;
750 if (sk->state == TCP_ESTABLISHED)
751 return -EISCONN; /* No reconnect on a seqpacket socket */
753 sk->state = TCP_CLOSE;
754 sock->state = SS_UNCONNECTED;
756 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
759 if (addr->srose_family != AF_ROSE)
762 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
765 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
768 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
769 if ((sk->protinfo.rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
772 if ((sk->protinfo.rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause, &diagnostic)) == NULL)
775 if ((sk->protinfo.rose->lci = rose_new_lci(sk->protinfo.rose->neighbour)) == 0)
778 if (sk->zapped) { /* Must bind first - autobinding in this may or may not work */
781 if ((dev = rose_dev_first()) == NULL)
784 if ((user = ax25_findbyuid(current->euid)) == NULL)
787 memcpy(&sk->protinfo.rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
788 sk->protinfo.rose->source_call = *user;
789 sk->protinfo.rose->device = dev;
791 rose_insert_socket(sk); /* Finish the bind */
794 sk->protinfo.rose->dest_addr = addr->srose_addr;
795 sk->protinfo.rose->dest_call = addr->srose_call;
796 sk->protinfo.rose->rand = ((int)sk->protinfo.rose & 0xFFFF) + sk->protinfo.rose->lci;
797 sk->protinfo.rose->dest_ndigis = addr->srose_ndigis;
799 if (addr_len == sizeof(struct full_sockaddr_rose)) {
800 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
801 for (n = 0 ; n < addr->srose_ndigis ; n++)
802 sk->protinfo.rose->dest_digis[n] = full_addr->srose_digis[n];
804 if (sk->protinfo.rose->dest_ndigis == 1) {
805 sk->protinfo.rose->dest_digis[0] = addr->srose_digi;
809 /* Move to connecting socket, start sending Connect Requests */
810 sock->state = SS_CONNECTING;
811 sk->state = TCP_SYN_SENT;
813 sk->protinfo.rose->state = ROSE_STATE_1;
815 sk->protinfo.rose->neighbour->use++;
817 rose_write_internal(sk, ROSE_CALL_REQUEST);
818 rose_start_heartbeat(sk);
819 rose_start_t1timer(sk);
822 if (sk->state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
825 cli(); /* To avoid races on the sleep */
828 * A Connect Ack with Choke or timeout or failed routing will go to closed.
830 while (sk->state == TCP_SYN_SENT) {
831 interruptible_sleep_on(sk->sleep);
832 if (signal_pending(current)) {
838 if (sk->state != TCP_ESTABLISHED) {
840 sock->state = SS_UNCONNECTED;
841 return sock_error(sk); /* Always set at this point */
844 sock->state = SS_CONNECTED;
851 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
857 if ((sk = sock->sk) == NULL)
860 if (sk->type != SOCK_SEQPACKET)
863 if (sk->state != TCP_LISTEN)
867 * The write queue this time is holding sockets ready to use
868 * hooked into the SABM we saved
872 if ((skb = skb_dequeue(&sk->receive_queue)) == NULL) {
873 if (flags & O_NONBLOCK) {
877 interruptible_sleep_on(sk->sleep);
878 if (signal_pending(current)) {
883 } while (skb == NULL);
887 newsk->socket = newsock;
888 newsk->sleep = &newsock->wait;
891 /* Now attach up the new socket */
900 static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
901 int *uaddr_len, int peer)
903 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
904 struct sock *sk = sock->sk;
908 if (sk->state != TCP_ESTABLISHED)
910 srose->srose_family = AF_ROSE;
911 srose->srose_addr = sk->protinfo.rose->dest_addr;
912 srose->srose_call = sk->protinfo.rose->dest_call;
913 srose->srose_ndigis = sk->protinfo.rose->dest_ndigis;
914 for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
915 srose->srose_digis[n] = sk->protinfo.rose->dest_digis[n];
917 srose->srose_family = AF_ROSE;
918 srose->srose_addr = sk->protinfo.rose->source_addr;
919 srose->srose_call = sk->protinfo.rose->source_call;
920 srose->srose_ndigis = sk->protinfo.rose->source_ndigis;
921 for (n = 0 ; n < sk->protinfo.rose->source_ndigis ; n++)
922 srose->srose_digis[n] = sk->protinfo.rose->source_digis[n];
925 *uaddr_len = sizeof(struct full_sockaddr_rose);
929 int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
933 struct rose_facilities_struct facilities;
936 skb->sk = NULL; /* Initially we don't know who it's for */
939 * skb->data points to the rose frame start
941 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
943 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
944 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
945 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
946 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
950 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
953 * We can't accept the Call Request.
955 if (sk == NULL || sk->ack_backlog == sk->max_ack_backlog || (make = rose_make_new(sk)) == NULL) {
956 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
961 make->state = TCP_ESTABLISHED;
963 make->protinfo.rose->lci = lci;
964 make->protinfo.rose->dest_addr = facilities.dest_addr;
965 make->protinfo.rose->dest_call = facilities.dest_call;
966 make->protinfo.rose->dest_ndigis = facilities.dest_ndigis;
967 for (n = 0 ; n < facilities.dest_ndigis ; n++)
968 make->protinfo.rose->dest_digis[n] = facilities.dest_digis[n];
969 make->protinfo.rose->source_addr = facilities.source_addr;
970 make->protinfo.rose->source_call = facilities.source_call;
971 make->protinfo.rose->source_ndigis = facilities.source_ndigis;
972 for (n = 0 ; n < facilities.source_ndigis ; n++)
973 make->protinfo.rose->source_digis[n]= facilities.source_digis[n];
974 make->protinfo.rose->neighbour = neigh;
975 make->protinfo.rose->device = dev;
976 make->protinfo.rose->facilities = facilities;
978 make->protinfo.rose->neighbour->use++;
980 if (sk->protinfo.rose->defer) {
981 make->protinfo.rose->state = ROSE_STATE_5;
983 rose_write_internal(make, ROSE_CALL_ACCEPTED);
984 make->protinfo.rose->state = ROSE_STATE_3;
985 rose_start_idletimer(make);
988 make->protinfo.rose->condition = 0x00;
989 make->protinfo.rose->vs = 0;
990 make->protinfo.rose->va = 0;
991 make->protinfo.rose->vr = 0;
992 make->protinfo.rose->vl = 0;
996 rose_insert_socket(make);
998 skb_queue_head(&sk->receive_queue, skb);
1000 rose_start_heartbeat(make);
1003 sk->data_ready(sk, skb->len);
1008 static int rose_sendmsg(struct socket *sock, struct msghdr *msg, int len,
1009 struct scm_cookie *scm)
1011 struct sock *sk = sock->sk;
1012 struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1014 struct full_sockaddr_rose srose;
1015 struct sk_buff *skb;
1016 unsigned char *asmptr;
1017 int n, size, qbit = 0;
1019 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR))
1023 return -EADDRNOTAVAIL;
1025 if (sk->shutdown & SEND_SHUTDOWN) {
1026 send_sig(SIGPIPE, current, 0);
1030 if (sk->protinfo.rose->neighbour == NULL || sk->protinfo.rose->device == NULL)
1031 return -ENETUNREACH;
1033 if (usrose != NULL) {
1034 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1036 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1037 memcpy(&srose, usrose, msg->msg_namelen);
1038 if (rosecmp(&sk->protinfo.rose->dest_addr, &srose.srose_addr) != 0 ||
1039 ax25cmp(&sk->protinfo.rose->dest_call, &srose.srose_call) != 0)
1041 if (srose.srose_ndigis != sk->protinfo.rose->dest_ndigis)
1043 if (srose.srose_ndigis == sk->protinfo.rose->dest_ndigis) {
1044 for (n = 0 ; n < srose.srose_ndigis ; n++)
1045 if (ax25cmp(&sk->protinfo.rose->dest_digis[n], &srose.srose_digis[n]) != 0)
1048 if (srose.srose_family != AF_ROSE)
1051 if (sk->state != TCP_ESTABLISHED)
1054 srose.srose_family = AF_ROSE;
1055 srose.srose_addr = sk->protinfo.rose->dest_addr;
1056 srose.srose_call = sk->protinfo.rose->dest_call;
1057 srose.srose_ndigis = sk->protinfo.rose->dest_ndigis;
1058 for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
1059 srose.srose_digis[n] = sk->protinfo.rose->dest_digis[n];
1062 SOCK_DEBUG(sk, "ROSE: sendto: Addresses built.\n");
1064 /* Build a packet */
1065 SOCK_DEBUG(sk, "ROSE: sendto: building packet.\n");
1066 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1068 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1071 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1074 * Put the data on the end
1076 SOCK_DEBUG(sk, "ROSE: Appending user data\n");
1078 asmptr = skb->h.raw = skb_put(skb, len);
1080 memcpy_fromiovec(asmptr, msg->msg_iov, len);
1083 * If the Q BIT Include socket option is in force, the first
1084 * byte of the user data is the logical value of the Q Bit.
1086 if (sk->protinfo.rose->qbitincl) {
1087 qbit = skb->data[0];
1092 * Push down the ROSE header
1094 asmptr = skb_push(skb, ROSE_MIN_LEN);
1096 SOCK_DEBUG(sk, "ROSE: Building Network Header.\n");
1098 /* Build a ROSE Network header */
1099 asmptr[0] = ((sk->protinfo.rose->lci >> 8) & 0x0F) | ROSE_GFI;
1100 asmptr[1] = (sk->protinfo.rose->lci >> 0) & 0xFF;
1101 asmptr[2] = ROSE_DATA;
1104 asmptr[0] |= ROSE_Q_BIT;
1106 SOCK_DEBUG(sk, "ROSE: Built header.\n");
1108 SOCK_DEBUG(sk, "ROSE: Transmitting buffer\n");
1110 if (sk->state != TCP_ESTABLISHED) {
1116 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1117 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1118 unsigned char header[ROSE_MIN_LEN];
1119 struct sk_buff *skbn;
1123 /* Save a copy of the Header */
1124 memcpy(header, skb->data, ROSE_MIN_LEN);
1125 skb_pull(skb, ROSE_MIN_LEN);
1127 frontlen = skb_headroom(skb);
1129 while (skb->len > 0) {
1130 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->write_queue, skbn); /* Throw it on the queue */
1156 kfree_skb(skb, FREE_WRITE);
1158 skb_queue_tail(&sk->write_queue, skb); /* Throw it on the queue */
1161 skb_queue_tail(&sk->write_queue, skb); /* Shove it onto the queue */
1170 static int rose_recvmsg(struct socket *sock, struct msghdr *msg, int size,
1171 int flags, struct scm_cookie *scm)
1173 struct sock *sk = sock->sk;
1174 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1176 unsigned char *asmptr;
1177 struct sk_buff *skb;
1181 * This works for seqpacket too. The receiver has ordered the queue for
1182 * us! We do one quick check first though
1184 if (sk->state != TCP_ESTABLISHED)
1187 /* Now we can treat all alike */
1188 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1191 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1193 skb_pull(skb, ROSE_MIN_LEN);
1195 if (sk->protinfo.rose->qbitincl) {
1196 asmptr = skb_push(skb, 1);
1200 skb->h.raw = skb->data;
1203 if (copied > size) {
1205 msg->msg_flags |= MSG_TRUNC;
1208 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1210 if (srose != NULL) {
1211 srose->srose_family = AF_ROSE;
1212 srose->srose_addr = sk->protinfo.rose->dest_addr;
1213 srose->srose_call = sk->protinfo.rose->dest_call;
1214 srose->srose_ndigis = sk->protinfo.rose->dest_ndigis;
1215 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) {
1216 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1217 for (n = 0 ; n < sk->protinfo.rose->dest_ndigis ; n++)
1218 full_srose->srose_digis[n] = sk->protinfo.rose->dest_digis[n];
1219 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1221 if (sk->protinfo.rose->dest_ndigis >= 1) {
1222 srose->srose_ndigis = 1;
1223 srose->srose_digi = sk->protinfo.rose->dest_digis[0];
1225 msg->msg_namelen = sizeof(struct sockaddr_rose);
1229 skb_free_datagram(sk, skb);
1235 static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1237 struct sock *sk = sock->sk;
1242 amount = sk->sndbuf - atomic_read(&sk->wmem_alloc);
1245 return put_user(amount, (unsigned int *)arg);
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->receive_queue)) != NULL)
1254 return put_user(amount, (unsigned int *)arg);
1259 if (sk->stamp.tv_sec == 0)
1261 return copy_to_user((void *)arg, &sk->stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1267 case SIOCGIFDSTADDR:
1268 case SIOCSIFDSTADDR:
1269 case SIOCGIFBRDADDR:
1270 case SIOCSIFBRDADDR:
1271 case SIOCGIFNETMASK:
1272 case SIOCSIFNETMASK:
1280 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1281 return rose_rt_ioctl(cmd, (void *)arg);
1283 case SIOCRSGCAUSE: {
1284 struct rose_cause_struct rose_cause;
1285 rose_cause.cause = sk->protinfo.rose->cause;
1286 rose_cause.diagnostic = sk->protinfo.rose->diagnostic;
1287 return copy_to_user((void *)arg, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1290 case SIOCRSSCAUSE: {
1291 struct rose_cause_struct rose_cause;
1292 if (copy_from_user(&rose_cause, (void *)arg, sizeof(struct rose_cause_struct)))
1294 sk->protinfo.rose->cause = rose_cause.cause;
1295 sk->protinfo.rose->diagnostic = rose_cause.diagnostic;
1300 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1301 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1302 ax25_listen_release(&rose_callsign, NULL);
1303 if (copy_from_user(&rose_callsign, (void *)arg, sizeof(ax25_address)))
1305 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1306 ax25_listen_register(&rose_callsign, NULL);
1310 return copy_to_user((void *)arg, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1313 if (sk->protinfo.rose->state == ROSE_STATE_5) {
1314 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1315 rose_start_idletimer(sk);
1316 sk->protinfo.rose->condition = 0x00;
1317 sk->protinfo.rose->vs = 0;
1318 sk->protinfo.rose->va = 0;
1319 sk->protinfo.rose->vr = 0;
1320 sk->protinfo.rose->vl = 0;
1321 sk->protinfo.rose->state = ROSE_STATE_3;
1326 return dev_ioctl(cmd, (void *)arg);
1333 static int rose_get_info(char *buffer, char **start, off_t offset, int length)
1336 struct net_device *dev;
1337 const char *devname, *callsign;
1344 len += sprintf(buffer, "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");
1346 for (s = rose_list; s != NULL; s = s->next) {
1347 if ((dev = s->protinfo.rose->device) == NULL)
1350 devname = dev->name;
1352 len += sprintf(buffer + len, "%-10s %-9s ",
1353 rose2asc(&s->protinfo.rose->dest_addr),
1354 ax2asc(&s->protinfo.rose->dest_call));
1356 if (ax25cmp(&s->protinfo.rose->source_call, &null_ax25_address) == 0)
1357 callsign = "??????-?";
1359 callsign = ax2asc(&s->protinfo.rose->source_call);
1361 len += sprintf(buffer + len, "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1362 rose2asc(&s->protinfo.rose->source_addr),
1365 s->protinfo.rose->lci & 0x0FFF,
1366 (s->protinfo.rose->neighbour) ? s->protinfo.rose->neighbour->number : 0,
1367 s->protinfo.rose->state,
1368 s->protinfo.rose->vs,
1369 s->protinfo.rose->vr,
1370 s->protinfo.rose->va,
1371 ax25_display_timer(&s->protinfo.rose->timer) / HZ,
1372 s->protinfo.rose->t1 / HZ,
1373 s->protinfo.rose->t2 / HZ,
1374 s->protinfo.rose->t3 / HZ,
1375 s->protinfo.rose->hb / HZ,
1376 ax25_display_timer(&s->protinfo.rose->idletimer) / (60 * HZ),
1377 s->protinfo.rose->idle / (60 * HZ),
1378 atomic_read(&s->wmem_alloc),
1379 atomic_read(&s->rmem_alloc),
1380 s->socket != NULL ? s->socket->inode->i_ino : 0L);
1389 if (pos > offset + length)
1395 *start = buffer + (offset - begin);
1396 len -= (offset - begin);
1398 if (len > length) len = length;
1403 static struct net_proto_family rose_family_ops = {
1405 create: rose_create,
1408 static struct proto_ops SOCKOPS_WRAPPED(rose_proto_ops) = {
1411 release: rose_release,
1413 connect: rose_connect,
1414 socketpair: sock_no_socketpair,
1415 accept: rose_accept,
1416 getname: rose_getname,
1417 poll: datagram_poll,
1419 listen: rose_listen,
1420 shutdown: sock_no_shutdown,
1421 setsockopt: rose_setsockopt,
1422 getsockopt: rose_getsockopt,
1423 sendmsg: rose_sendmsg,
1424 recvmsg: rose_recvmsg,
1426 sendpage: sock_no_sendpage,
1429 #include <linux/smp_lock.h>
1430 SOCKOPS_WRAP(rose_proto, PF_ROSE);
1432 static struct notifier_block rose_dev_notifier = {
1433 notifier_call: rose_device_event,
1436 static struct net_device *dev_rose;
1438 static const char banner[] = KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.64 for AX25.037 Linux 2.4\n";
1440 static int __init rose_proto_init(void)
1444 rose_callsign = null_ax25_address;
1446 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device)) {
1447 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1451 if ((dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device), GFP_KERNEL)) == NULL) {
1452 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1456 memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device));
1458 for (i = 0; i < rose_ndevs; i++) {
1459 sprintf(dev_rose[i].name, "rose%d", i);
1460 dev_rose[i].init = rose_init;
1461 register_netdev(&dev_rose[i]);
1464 sock_register(&rose_family_ops);
1465 register_netdevice_notifier(&rose_dev_notifier);
1468 ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
1469 ax25_linkfail_register(rose_link_failed);
1471 #ifdef CONFIG_SYSCTL
1472 rose_register_sysctl();
1474 rose_loopback_init();
1476 rose_add_loopback_neigh();
1478 proc_net_create("rose", 0, rose_get_info);
1479 proc_net_create("rose_neigh", 0, rose_neigh_get_info);
1480 proc_net_create("rose_nodes", 0, rose_nodes_get_info);
1481 proc_net_create("rose_routes", 0, rose_routes_get_info);
1484 module_init(rose_proto_init);
1488 MODULE_PARM(rose_ndevs, "i");
1489 MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1491 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1492 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1493 MODULE_LICENSE("GPL");
1495 static void __exit rose_exit(void)
1499 proc_net_remove("rose");
1500 proc_net_remove("rose_neigh");
1501 proc_net_remove("rose_nodes");
1502 proc_net_remove("rose_routes");
1503 rose_loopback_clear();
1507 ax25_protocol_release(AX25_P_ROSE);
1508 ax25_linkfail_release(rose_link_failed);
1510 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1511 ax25_listen_release(&rose_callsign, NULL);
1513 #ifdef CONFIG_SYSCTL
1514 rose_unregister_sysctl();
1516 unregister_netdevice_notifier(&rose_dev_notifier);
1518 sock_unregister(PF_ROSE);
1520 for (i = 0; i < rose_ndevs; i++) {
1521 if (dev_rose[i].priv != NULL) {
1522 kfree(dev_rose[i].priv);
1523 dev_rose[i].priv = NULL;
1524 unregister_netdev(&dev_rose[i]);
1526 kfree(dev_rose[i].name);
1531 module_exit(rose_exit);