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
27 #include <linux/config.h>
28 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
34 #include <linux/kernel.h>
35 #include <linux/sched.h>
36 #include <linux/timer.h>
37 #include <linux/string.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
40 #include <linux/stat.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <linux/if_arp.h>
45 #include <linux/skbuff.h>
47 #include <asm/segment.h>
48 #include <asm/system.h>
49 #include <asm/uaccess.h>
50 #include <linux/fcntl.h>
51 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
53 #include <linux/interrupt.h>
54 #include <linux/notifier.h>
56 #include <linux/proc_fs.h>
59 #include <linux/init.h>
63 int sysctl_rose_restart_request_timeout
= ROSE_DEFAULT_T0
;
64 int sysctl_rose_call_request_timeout
= ROSE_DEFAULT_T1
;
65 int sysctl_rose_reset_request_timeout
= ROSE_DEFAULT_T2
;
66 int sysctl_rose_clear_request_timeout
= ROSE_DEFAULT_T3
;
67 int sysctl_rose_no_activity_timeout
= ROSE_DEFAULT_IDLE
;
68 int sysctl_rose_ack_hold_back_timeout
= ROSE_DEFAULT_HB
;
69 int sysctl_rose_routing_control
= ROSE_DEFAULT_ROUTING
;
70 int sysctl_rose_link_fail_timeout
= ROSE_DEFAULT_FAIL_TIMEOUT
;
71 int sysctl_rose_maximum_vcs
= ROSE_DEFAULT_MAXVC
;
72 int sysctl_rose_window_size
= ROSE_DEFAULT_WINDOW_SIZE
;
74 static struct sock
*volatile rose_list
= NULL
;
76 static struct proto_ops rose_proto_ops
;
78 ax25_address rose_callsign
;
81 * Convert a ROSE address into text.
83 char *rose2asc(rose_address
*addr
)
85 static char buffer
[11];
87 if (addr
->rose_addr
[0] == 0x00 && addr
->rose_addr
[1] == 0x00 &&
88 addr
->rose_addr
[2] == 0x00 && addr
->rose_addr
[3] == 0x00 &&
89 addr
->rose_addr
[4] == 0x00) {
92 sprintf(buffer
, "%02X%02X%02X%02X%02X", addr
->rose_addr
[0] & 0xFF,
93 addr
->rose_addr
[1] & 0xFF,
94 addr
->rose_addr
[2] & 0xFF,
95 addr
->rose_addr
[3] & 0xFF,
96 addr
->rose_addr
[4] & 0xFF);
103 * Compare two ROSE addresses, 0 == equal.
105 int rosecmp(rose_address
*addr1
, rose_address
*addr2
)
109 for (i
= 0; i
< 5; i
++)
110 if (addr1
->rose_addr
[i
] != addr2
->rose_addr
[i
])
117 * Compare two ROSE addresses for only mask digits, 0 == equal.
119 int rosecmpm(rose_address
*addr1
, rose_address
*addr2
, unsigned short mask
)
126 for (i
= 0; i
< mask
; i
++) {
130 if ((addr1
->rose_addr
[j
] & 0x0F) != (addr2
->rose_addr
[j
] & 0x0F))
133 if ((addr1
->rose_addr
[j
] & 0xF0) != (addr2
->rose_addr
[j
] & 0xF0))
141 static void rose_free_sock(struct sock
*sk
)
148 static struct sock
*rose_alloc_sock(void)
153 if ((sk
= sk_alloc(PF_ROSE
, GFP_ATOMIC
, 1)) == NULL
)
156 if ((rose
= kmalloc(sizeof(*rose
), GFP_ATOMIC
)) == NULL
) {
163 memset(rose
, 0x00, sizeof(*rose
));
165 sk
->protinfo
.rose
= rose
;
172 * Socket removal during an interrupt is now safe.
174 static void rose_remove_socket(struct sock
*sk
)
179 save_flags(flags
); cli();
181 if ((s
= rose_list
) == sk
) {
183 restore_flags(flags
);
187 while (s
!= NULL
&& s
->next
!= NULL
) {
190 restore_flags(flags
);
197 restore_flags(flags
);
201 * Kill all bound sockets on a broken link layer connection to a
202 * particular neighbour.
204 void rose_kill_by_neigh(struct rose_neigh
*neigh
)
208 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
209 if (s
->protinfo
.rose
->neighbour
== neigh
) {
210 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
211 s
->protinfo
.rose
->neighbour
->use
--;
212 s
->protinfo
.rose
->neighbour
= NULL
;
218 * Kill all bound sockets on a dropped device.
220 static void rose_kill_by_device(struct net_device
*dev
)
224 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
225 if (s
->protinfo
.rose
->device
== dev
) {
226 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
227 s
->protinfo
.rose
->neighbour
->use
--;
228 s
->protinfo
.rose
->device
= NULL
;
234 * Handle device status changes.
236 static int rose_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
238 struct net_device
*dev
= (struct net_device
*)ptr
;
240 if (event
!= NETDEV_DOWN
)
245 rose_kill_by_device(dev
);
248 rose_link_device_down(dev
);
249 rose_rt_device_down(dev
);
257 * Add a socket to the bound sockets list.
259 static void rose_insert_socket(struct sock
*sk
)
263 save_flags(flags
); cli();
265 sk
->next
= rose_list
;
268 restore_flags(flags
);
272 * Find a socket that wants to accept the Call Request we just
275 static struct sock
*rose_find_listener(rose_address
*addr
, ax25_address
*call
)
280 save_flags(flags
); cli();
282 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
283 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
) {
284 restore_flags(flags
);
289 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
290 if (rosecmp(&s
->protinfo
.rose
->source_addr
, addr
) == 0 && ax25cmp(&s
->protinfo
.rose
->source_call
, &null_ax25_address
) == 0 && s
->state
== TCP_LISTEN
) {
291 restore_flags(flags
);
296 restore_flags(flags
);
301 * Find a connected ROSE socket given my LCI and device.
303 struct sock
*rose_find_socket(unsigned int lci
, struct rose_neigh
*neigh
)
308 save_flags(flags
); cli();
310 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
311 if (s
->protinfo
.rose
->lci
== lci
&& s
->protinfo
.rose
->neighbour
== neigh
) {
312 restore_flags(flags
);
317 restore_flags(flags
);
323 * Find a unique LCI for a given device.
325 unsigned int rose_new_lci(struct rose_neigh
*neigh
)
329 if (neigh
->dce_mode
) {
330 for (lci
= 1; lci
<= sysctl_rose_maximum_vcs
; lci
++)
331 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
334 for (lci
= sysctl_rose_maximum_vcs
; lci
> 0; lci
--)
335 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
345 void rose_destroy_socket(struct sock
*);
348 * Handler for deferred kills.
350 static void rose_destroy_timer(unsigned long data
)
352 rose_destroy_socket((struct sock
*)data
);
356 * This is called from user mode and the timers. Thus it protects itself against
357 * interrupt users but doesn't worry about being called during work.
358 * Once it is removed from the queue no interrupt or bottom half will
359 * touch it and we are (fairly 8-) ) safe.
361 void rose_destroy_socket(struct sock
*sk
) /* Not static as it's used by the timer */
366 save_flags(flags
); cli();
368 rose_stop_heartbeat(sk
);
369 rose_stop_idletimer(sk
);
372 rose_remove_socket(sk
);
373 rose_clear_queues(sk
); /* Flush the queues */
375 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
376 if (skb
->sk
!= sk
) { /* A pending connection */
377 skb
->sk
->dead
= 1; /* Queue the unaccepted socket for death */
378 rose_start_heartbeat(skb
->sk
);
379 skb
->sk
->protinfo
.rose
->state
= ROSE_STATE_0
;
385 if (atomic_read(&sk
->wmem_alloc
) != 0 || atomic_read(&sk
->rmem_alloc
) != 0) {
386 /* Defer: outstanding buffers */
387 init_timer(&sk
->timer
);
388 sk
->timer
.expires
= jiffies
+ 10 * HZ
;
389 sk
->timer
.function
= rose_destroy_timer
;
390 sk
->timer
.data
= (unsigned long)sk
;
391 add_timer(&sk
->timer
);
396 restore_flags(flags
);
400 * Handling for system calls applied via the various interfaces to a
401 * ROSE socket object.
404 static int rose_setsockopt(struct socket
*sock
, int level
, int optname
,
405 char *optval
, int optlen
)
407 struct sock
*sk
= sock
->sk
;
410 if (level
!= SOL_ROSE
)
413 if (optlen
< sizeof(int))
416 if (get_user(opt
, (int *)optval
))
421 sk
->protinfo
.rose
->defer
= opt
? 1 : 0;
427 sk
->protinfo
.rose
->t1
= opt
* HZ
;
433 sk
->protinfo
.rose
->t2
= opt
* HZ
;
439 sk
->protinfo
.rose
->t3
= opt
* HZ
;
445 sk
->protinfo
.rose
->hb
= opt
* HZ
;
451 sk
->protinfo
.rose
->idle
= opt
* 60 * HZ
;
455 sk
->protinfo
.rose
->qbitincl
= opt
? 1 : 0;
463 static int rose_getsockopt(struct socket
*sock
, int level
, int optname
,
464 char *optval
, int *optlen
)
466 struct sock
*sk
= sock
->sk
;
470 if (level
!= SOL_ROSE
)
473 if (get_user(len
, optlen
))
478 val
= sk
->protinfo
.rose
->defer
;
482 val
= sk
->protinfo
.rose
->t1
/ HZ
;
486 val
= sk
->protinfo
.rose
->t2
/ HZ
;
490 val
= sk
->protinfo
.rose
->t3
/ HZ
;
494 val
= sk
->protinfo
.rose
->hb
/ HZ
;
498 val
= sk
->protinfo
.rose
->idle
/ (60 * HZ
);
502 val
= sk
->protinfo
.rose
->qbitincl
;
509 len
= min(len
, sizeof(int));
511 if (put_user(len
, optlen
))
514 return copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
517 static int rose_listen(struct socket
*sock
, int backlog
)
519 struct sock
*sk
= sock
->sk
;
521 if (sk
->state
!= TCP_LISTEN
) {
522 sk
->protinfo
.rose
->dest_ndigis
= 0;
523 memset(&sk
->protinfo
.rose
->dest_addr
, '\0', ROSE_ADDR_LEN
);
524 memset(&sk
->protinfo
.rose
->dest_call
, '\0', AX25_ADDR_LEN
);
525 memset(sk
->protinfo
.rose
->dest_digis
, '\0', AX25_ADDR_LEN
*ROSE_MAX_DIGIS
);
526 sk
->max_ack_backlog
= backlog
;
527 sk
->state
= TCP_LISTEN
;
534 static int rose_create(struct socket
*sock
, int protocol
)
539 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
540 return -ESOCKTNOSUPPORT
;
542 if ((sk
= rose_alloc_sock()) == NULL
)
545 rose
= sk
->protinfo
.rose
;
547 sock_init_data(sock
, sk
);
549 skb_queue_head_init(&rose
->ack_queue
);
551 skb_queue_head_init(&rose
->frag_queue
);
555 sock
->ops
= &rose_proto_ops
;
556 sk
->protocol
= protocol
;
558 init_timer(&rose
->timer
);
559 init_timer(&rose
->idletimer
);
561 rose
->t1
= sysctl_rose_call_request_timeout
;
562 rose
->t2
= sysctl_rose_reset_request_timeout
;
563 rose
->t3
= sysctl_rose_clear_request_timeout
;
564 rose
->hb
= sysctl_rose_ack_hold_back_timeout
;
565 rose
->idle
= sysctl_rose_no_activity_timeout
;
567 rose
->state
= ROSE_STATE_0
;
572 static struct sock
*rose_make_new(struct sock
*osk
)
577 if (osk
->type
!= SOCK_SEQPACKET
)
580 if ((sk
= rose_alloc_sock()) == NULL
)
583 rose
= sk
->protinfo
.rose
;
585 sock_init_data(NULL
, sk
);
587 skb_queue_head_init(&rose
->ack_queue
);
589 skb_queue_head_init(&rose
->frag_queue
);
593 sk
->type
= osk
->type
;
594 sk
->socket
= osk
->socket
;
595 sk
->priority
= osk
->priority
;
596 sk
->protocol
= osk
->protocol
;
597 sk
->rcvbuf
= osk
->rcvbuf
;
598 sk
->sndbuf
= osk
->sndbuf
;
599 sk
->debug
= osk
->debug
;
600 sk
->state
= TCP_ESTABLISHED
;
601 sk
->sleep
= osk
->sleep
;
602 sk
->zapped
= osk
->zapped
;
604 init_timer(&rose
->timer
);
605 init_timer(&rose
->idletimer
);
607 rose
->t1
= osk
->protinfo
.rose
->t1
;
608 rose
->t2
= osk
->protinfo
.rose
->t2
;
609 rose
->t3
= osk
->protinfo
.rose
->t3
;
610 rose
->hb
= osk
->protinfo
.rose
->hb
;
611 rose
->idle
= osk
->protinfo
.rose
->idle
;
613 rose
->defer
= osk
->protinfo
.rose
->defer
;
614 rose
->device
= osk
->protinfo
.rose
->device
;
615 rose
->qbitincl
= osk
->protinfo
.rose
->qbitincl
;
620 static int rose_release(struct socket
*sock
)
622 struct sock
*sk
= sock
->sk
;
624 if (sk
== NULL
) return 0;
626 switch (sk
->protinfo
.rose
->state
) {
629 rose_disconnect(sk
, 0, -1, -1);
630 rose_destroy_socket(sk
);
634 sk
->protinfo
.rose
->neighbour
->use
--;
635 rose_disconnect(sk
, 0, -1, -1);
636 rose_destroy_socket(sk
);
643 rose_clear_queues(sk
);
644 rose_stop_idletimer(sk
);
645 rose_write_internal(sk
, ROSE_CLEAR_REQUEST
);
646 rose_start_t3timer(sk
);
647 sk
->protinfo
.rose
->state
= ROSE_STATE_2
;
648 sk
->state
= TCP_CLOSE
;
649 sk
->shutdown
|= SEND_SHUTDOWN
;
650 sk
->state_change(sk
);
660 sk
->socket
= NULL
; /* Not used, but we should do this. **/
665 static int rose_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
667 struct sock
*sk
= sock
->sk
;
668 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
669 struct net_device
*dev
;
670 ax25_address
*user
, *source
;
676 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
679 if (addr
->srose_family
!= AF_ROSE
)
682 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
685 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
688 if ((dev
= rose_dev_get(&addr
->srose_addr
)) == NULL
) {
689 SOCK_DEBUG(sk
, "ROSE: bind failed: invalid address\n");
690 return -EADDRNOTAVAIL
;
693 source
= &addr
->srose_call
;
695 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
) {
696 if (ax25_uid_policy
&& !capable(CAP_NET_BIND_SERVICE
))
701 sk
->protinfo
.rose
->source_addr
= addr
->srose_addr
;
702 sk
->protinfo
.rose
->source_call
= *user
;
703 sk
->protinfo
.rose
->device
= dev
;
704 sk
->protinfo
.rose
->source_ndigis
= addr
->srose_ndigis
;
706 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
707 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
708 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
709 sk
->protinfo
.rose
->source_digis
[n
] = full_addr
->srose_digis
[n
];
711 if (sk
->protinfo
.rose
->source_ndigis
== 1) {
712 sk
->protinfo
.rose
->source_digis
[0] = addr
->srose_digi
;
716 rose_insert_socket(sk
);
719 SOCK_DEBUG(sk
, "ROSE: socket is bound\n");
723 static int rose_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
725 struct sock
*sk
= sock
->sk
;
726 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
727 unsigned char cause
, diagnostic
;
729 struct net_device
*dev
;
732 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
733 sock
->state
= SS_CONNECTED
;
734 return 0; /* Connect completed during a ERESTARTSYS event */
737 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
738 sock
->state
= SS_UNCONNECTED
;
739 return -ECONNREFUSED
;
742 if (sk
->state
== TCP_ESTABLISHED
)
743 return -EISCONN
; /* No reconnect on a seqpacket socket */
745 sk
->state
= TCP_CLOSE
;
746 sock
->state
= SS_UNCONNECTED
;
748 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
751 if (addr
->srose_family
!= AF_ROSE
)
754 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
757 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
760 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
761 if ((sk
->protinfo
.rose
->source_ndigis
+ addr
->srose_ndigis
) > ROSE_MAX_DIGIS
)
764 if ((sk
->protinfo
.rose
->neighbour
= rose_get_neigh(&addr
->srose_addr
, &cause
, &diagnostic
)) == NULL
)
767 if ((sk
->protinfo
.rose
->lci
= rose_new_lci(sk
->protinfo
.rose
->neighbour
)) == 0)
770 if (sk
->zapped
) { /* Must bind first - autobinding in this may or may not work */
773 if ((dev
= rose_dev_first()) == NULL
)
776 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
)
779 memcpy(&sk
->protinfo
.rose
->source_addr
, dev
->dev_addr
, ROSE_ADDR_LEN
);
780 sk
->protinfo
.rose
->source_call
= *user
;
781 sk
->protinfo
.rose
->device
= dev
;
783 rose_insert_socket(sk
); /* Finish the bind */
786 sk
->protinfo
.rose
->dest_addr
= addr
->srose_addr
;
787 sk
->protinfo
.rose
->dest_call
= addr
->srose_call
;
788 sk
->protinfo
.rose
->rand
= ((int)sk
->protinfo
.rose
& 0xFFFF) + sk
->protinfo
.rose
->lci
;
789 sk
->protinfo
.rose
->dest_ndigis
= addr
->srose_ndigis
;
791 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
792 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
793 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
794 sk
->protinfo
.rose
->dest_digis
[n
] = full_addr
->srose_digis
[n
];
796 if (sk
->protinfo
.rose
->dest_ndigis
== 1) {
797 sk
->protinfo
.rose
->dest_digis
[0] = addr
->srose_digi
;
801 /* Move to connecting socket, start sending Connect Requests */
802 sock
->state
= SS_CONNECTING
;
803 sk
->state
= TCP_SYN_SENT
;
805 sk
->protinfo
.rose
->state
= ROSE_STATE_1
;
807 sk
->protinfo
.rose
->neighbour
->use
++;
809 rose_write_internal(sk
, ROSE_CALL_REQUEST
);
810 rose_start_heartbeat(sk
);
811 rose_start_t1timer(sk
);
814 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
817 cli(); /* To avoid races on the sleep */
820 * A Connect Ack with Choke or timeout or failed routing will go to closed.
822 while (sk
->state
== TCP_SYN_SENT
) {
823 interruptible_sleep_on(sk
->sleep
);
824 if (signal_pending(current
)) {
830 if (sk
->state
!= TCP_ESTABLISHED
) {
832 sock
->state
= SS_UNCONNECTED
;
833 return sock_error(sk
); /* Always set at this point */
836 sock
->state
= SS_CONNECTED
;
843 static int rose_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
849 if ((sk
= sock
->sk
) == NULL
)
852 if (sk
->type
!= SOCK_SEQPACKET
)
855 if (sk
->state
!= TCP_LISTEN
)
859 * The write queue this time is holding sockets ready to use
860 * hooked into the SABM we saved
864 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
865 if (flags
& O_NONBLOCK
) {
869 interruptible_sleep_on(sk
->sleep
);
870 if (signal_pending(current
)) {
875 } while (skb
== NULL
);
879 newsk
->socket
= newsock
;
880 newsk
->sleep
= &newsock
->wait
;
883 /* Now attach up the new socket */
892 static int rose_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
893 int *uaddr_len
, int peer
)
895 struct full_sockaddr_rose
*srose
= (struct full_sockaddr_rose
*)uaddr
;
896 struct sock
*sk
= sock
->sk
;
900 if (sk
->state
!= TCP_ESTABLISHED
)
902 srose
->srose_family
= AF_ROSE
;
903 srose
->srose_addr
= sk
->protinfo
.rose
->dest_addr
;
904 srose
->srose_call
= sk
->protinfo
.rose
->dest_call
;
905 srose
->srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
906 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
907 srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
909 srose
->srose_family
= AF_ROSE
;
910 srose
->srose_addr
= sk
->protinfo
.rose
->source_addr
;
911 srose
->srose_call
= sk
->protinfo
.rose
->source_call
;
912 srose
->srose_ndigis
= sk
->protinfo
.rose
->source_ndigis
;
913 for (n
= 0 ; n
< sk
->protinfo
.rose
->source_ndigis
; n
++)
914 srose
->srose_digis
[n
] = sk
->protinfo
.rose
->source_digis
[n
];
917 *uaddr_len
= sizeof(struct full_sockaddr_rose
);
921 int rose_rx_call_request(struct sk_buff
*skb
, struct net_device
*dev
, struct rose_neigh
*neigh
, unsigned int lci
)
925 struct rose_facilities_struct facilities
;
928 skb
->sk
= NULL
; /* Initially we don't know who it's for */
931 * skb->data points to the rose frame start
933 memset(&facilities
, 0x00, sizeof(struct rose_facilities_struct
));
935 len
= (((skb
->data
[3] >> 4) & 0x0F) + 1) / 2;
936 len
+= (((skb
->data
[3] >> 0) & 0x0F) + 1) / 2;
937 if (!rose_parse_facilities(skb
->data
+ len
+ 4, &facilities
)) {
938 rose_transmit_clear_request(neigh
, lci
, ROSE_INVALID_FACILITY
, 76);
942 sk
= rose_find_listener(&facilities
.source_addr
, &facilities
.source_call
);
945 * We can't accept the Call Request.
947 if (sk
== NULL
|| sk
->ack_backlog
== sk
->max_ack_backlog
|| (make
= rose_make_new(sk
)) == NULL
) {
948 rose_transmit_clear_request(neigh
, lci
, ROSE_NETWORK_CONGESTION
, 120);
953 make
->state
= TCP_ESTABLISHED
;
955 make
->protinfo
.rose
->lci
= lci
;
956 make
->protinfo
.rose
->dest_addr
= facilities
.dest_addr
;
957 make
->protinfo
.rose
->dest_call
= facilities
.dest_call
;
958 make
->protinfo
.rose
->dest_ndigis
= facilities
.dest_ndigis
;
959 for (n
= 0 ; n
< facilities
.dest_ndigis
; n
++)
960 make
->protinfo
.rose
->dest_digis
[n
] = facilities
.dest_digis
[n
];
961 make
->protinfo
.rose
->source_addr
= facilities
.source_addr
;
962 make
->protinfo
.rose
->source_call
= facilities
.source_call
;
963 make
->protinfo
.rose
->source_ndigis
= facilities
.source_ndigis
;
964 for (n
= 0 ; n
< facilities
.source_ndigis
; n
++)
965 make
->protinfo
.rose
->source_digis
[n
]= facilities
.source_digis
[n
];
966 make
->protinfo
.rose
->neighbour
= neigh
;
967 make
->protinfo
.rose
->device
= dev
;
968 make
->protinfo
.rose
->facilities
= facilities
;
970 make
->protinfo
.rose
->neighbour
->use
++;
972 if (sk
->protinfo
.rose
->defer
) {
973 make
->protinfo
.rose
->state
= ROSE_STATE_5
;
975 rose_write_internal(make
, ROSE_CALL_ACCEPTED
);
976 make
->protinfo
.rose
->state
= ROSE_STATE_3
;
977 rose_start_idletimer(make
);
980 make
->protinfo
.rose
->condition
= 0x00;
981 make
->protinfo
.rose
->vs
= 0;
982 make
->protinfo
.rose
->va
= 0;
983 make
->protinfo
.rose
->vr
= 0;
984 make
->protinfo
.rose
->vl
= 0;
988 rose_insert_socket(make
);
990 skb_queue_head(&sk
->receive_queue
, skb
);
992 rose_start_heartbeat(make
);
995 sk
->data_ready(sk
, skb
->len
);
1000 static int rose_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
1001 struct scm_cookie
*scm
)
1003 struct sock
*sk
= sock
->sk
;
1004 struct sockaddr_rose
*usrose
= (struct sockaddr_rose
*)msg
->msg_name
;
1006 struct full_sockaddr_rose srose
;
1007 struct sk_buff
*skb
;
1008 unsigned char *asmptr
;
1009 int n
, size
, qbit
= 0;
1011 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
))
1015 return -EADDRNOTAVAIL
;
1017 if (sk
->shutdown
& SEND_SHUTDOWN
) {
1018 send_sig(SIGPIPE
, current
, 0);
1022 if (sk
->protinfo
.rose
->neighbour
== NULL
|| sk
->protinfo
.rose
->device
== NULL
)
1023 return -ENETUNREACH
;
1025 if (usrose
!= NULL
) {
1026 if (msg
->msg_namelen
!= sizeof(struct sockaddr_rose
) && msg
->msg_namelen
!= sizeof(struct full_sockaddr_rose
))
1028 memset(&srose
, 0, sizeof(struct full_sockaddr_rose
));
1029 memcpy(&srose
, usrose
, msg
->msg_namelen
);
1030 if (rosecmp(&sk
->protinfo
.rose
->dest_addr
, &srose
.srose_addr
) != 0 ||
1031 ax25cmp(&sk
->protinfo
.rose
->dest_call
, &srose
.srose_call
) != 0)
1033 if (srose
.srose_ndigis
!= sk
->protinfo
.rose
->dest_ndigis
)
1035 if (srose
.srose_ndigis
== sk
->protinfo
.rose
->dest_ndigis
) {
1036 for (n
= 0 ; n
< srose
.srose_ndigis
; n
++)
1037 if (ax25cmp(&sk
->protinfo
.rose
->dest_digis
[n
], &srose
.srose_digis
[n
]) != 0)
1040 if (srose
.srose_family
!= AF_ROSE
)
1043 if (sk
->state
!= TCP_ESTABLISHED
)
1046 srose
.srose_family
= AF_ROSE
;
1047 srose
.srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1048 srose
.srose_call
= sk
->protinfo
.rose
->dest_call
;
1049 srose
.srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1050 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1051 srose
.srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1054 SOCK_DEBUG(sk
, "ROSE: sendto: Addresses built.\n");
1056 /* Build a packet */
1057 SOCK_DEBUG(sk
, "ROSE: sendto: building packet.\n");
1058 size
= len
+ AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
;
1060 if ((skb
= sock_alloc_send_skb(sk
, size
, 0, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1063 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
);
1066 * Put the data on the end
1068 SOCK_DEBUG(sk
, "ROSE: Appending user data\n");
1070 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1072 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1075 * If the Q BIT Include socket option is in force, the first
1076 * byte of the user data is the logical value of the Q Bit.
1078 if (sk
->protinfo
.rose
->qbitincl
) {
1079 qbit
= skb
->data
[0];
1084 * Push down the ROSE header
1086 asmptr
= skb_push(skb
, ROSE_MIN_LEN
);
1088 SOCK_DEBUG(sk
, "ROSE: Building Network Header.\n");
1090 /* Build a ROSE Network header */
1091 asmptr
[0] = ((sk
->protinfo
.rose
->lci
>> 8) & 0x0F) | ROSE_GFI
;
1092 asmptr
[1] = (sk
->protinfo
.rose
->lci
>> 0) & 0xFF;
1093 asmptr
[2] = ROSE_DATA
;
1096 asmptr
[0] |= ROSE_Q_BIT
;
1098 SOCK_DEBUG(sk
, "ROSE: Built header.\n");
1100 SOCK_DEBUG(sk
, "ROSE: Transmitting buffer\n");
1102 if (sk
->state
!= TCP_ESTABLISHED
) {
1108 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1109 if (skb
->len
- ROSE_MIN_LEN
> ROSE_PACLEN
) {
1110 unsigned char header
[ROSE_MIN_LEN
];
1111 struct sk_buff
*skbn
;
1115 /* Save a copy of the Header */
1116 memcpy(header
, skb
->data
, ROSE_MIN_LEN
);
1117 skb_pull(skb
, ROSE_MIN_LEN
);
1119 frontlen
= skb_headroom(skb
);
1121 while (skb
->len
> 0) {
1122 if ((skbn
= sock_alloc_send_skb(sk
, frontlen
+ ROSE_PACLEN
, 0, 0, &err
)) == NULL
)
1129 skb_reserve(skbn
, frontlen
);
1131 lg
= (ROSE_PACLEN
> skb
->len
) ? skb
->len
: ROSE_PACLEN
;
1133 /* Copy the user data */
1134 memcpy(skb_put(skbn
, lg
), skb
->data
, lg
);
1137 /* Duplicate the Header */
1138 skb_push(skbn
, ROSE_MIN_LEN
);
1139 memcpy(skbn
->data
, header
, ROSE_MIN_LEN
);
1142 skbn
->data
[2] |= M_BIT
;
1144 skb_queue_tail(&sk
->write_queue
, skbn
); /* Throw it on the queue */
1148 kfree_skb(skb
, FREE_WRITE
);
1150 skb_queue_tail(&sk
->write_queue
, skb
); /* Throw it on the queue */
1153 skb_queue_tail(&sk
->write_queue
, skb
); /* Shove it onto the queue */
1162 static int rose_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1163 int flags
, struct scm_cookie
*scm
)
1165 struct sock
*sk
= sock
->sk
;
1166 struct sockaddr_rose
*srose
= (struct sockaddr_rose
*)msg
->msg_name
;
1168 unsigned char *asmptr
;
1169 struct sk_buff
*skb
;
1173 * This works for seqpacket too. The receiver has ordered the queue for
1174 * us! We do one quick check first though
1176 if (sk
->state
!= TCP_ESTABLISHED
)
1179 /* Now we can treat all alike */
1180 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1183 qbit
= (skb
->data
[0] & ROSE_Q_BIT
) == ROSE_Q_BIT
;
1185 skb_pull(skb
, ROSE_MIN_LEN
);
1187 if (sk
->protinfo
.rose
->qbitincl
) {
1188 asmptr
= skb_push(skb
, 1);
1192 skb
->h
.raw
= skb
->data
;
1195 if (copied
> size
) {
1197 msg
->msg_flags
|= MSG_TRUNC
;
1200 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1202 if (srose
!= NULL
) {
1203 srose
->srose_family
= AF_ROSE
;
1204 srose
->srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1205 srose
->srose_call
= sk
->protinfo
.rose
->dest_call
;
1206 srose
->srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1207 if (msg
->msg_namelen
>= sizeof(struct full_sockaddr_rose
)) {
1208 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)msg
->msg_name
;
1209 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1210 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1211 msg
->msg_namelen
= sizeof(struct full_sockaddr_rose
);
1213 if (sk
->protinfo
.rose
->dest_ndigis
>= 1) {
1214 srose
->srose_ndigis
= 1;
1215 srose
->srose_digi
= sk
->protinfo
.rose
->dest_digis
[0];
1217 msg
->msg_namelen
= sizeof(struct sockaddr_rose
);
1221 skb_free_datagram(sk
, skb
);
1227 static int rose_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1229 struct sock
*sk
= sock
->sk
;
1234 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1237 return put_user(amount
, (unsigned int *)arg
);
1241 struct sk_buff
*skb
;
1243 /* These two are safe on a single CPU system as only user tasks fiddle here */
1244 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1246 return put_user(amount
, (unsigned int *)arg
);
1251 if (sk
->stamp
.tv_sec
== 0)
1253 return copy_to_user((void *)arg
, &sk
->stamp
, sizeof(struct timeval
)) ? -EFAULT
: 0;
1259 case SIOCGIFDSTADDR
:
1260 case SIOCSIFDSTADDR
:
1261 case SIOCGIFBRDADDR
:
1262 case SIOCSIFBRDADDR
:
1263 case SIOCGIFNETMASK
:
1264 case SIOCSIFNETMASK
:
1272 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1273 return rose_rt_ioctl(cmd
, (void *)arg
);
1275 case SIOCRSGCAUSE
: {
1276 struct rose_cause_struct rose_cause
;
1277 rose_cause
.cause
= sk
->protinfo
.rose
->cause
;
1278 rose_cause
.diagnostic
= sk
->protinfo
.rose
->diagnostic
;
1279 return copy_to_user((void *)arg
, &rose_cause
, sizeof(struct rose_cause_struct
)) ? -EFAULT
: 0;
1282 case SIOCRSSCAUSE
: {
1283 struct rose_cause_struct rose_cause
;
1284 if (copy_from_user(&rose_cause
, (void *)arg
, sizeof(struct rose_cause_struct
)))
1286 sk
->protinfo
.rose
->cause
= rose_cause
.cause
;
1287 sk
->protinfo
.rose
->diagnostic
= rose_cause
.diagnostic
;
1292 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1293 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1294 ax25_listen_release(&rose_callsign
, NULL
);
1295 if (copy_from_user(&rose_callsign
, (void *)arg
, sizeof(ax25_address
)))
1297 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1298 ax25_listen_register(&rose_callsign
, NULL
);
1302 return copy_to_user((void *)arg
, &rose_callsign
, sizeof(ax25_address
)) ? -EFAULT
: 0;
1305 if (sk
->protinfo
.rose
->state
== ROSE_STATE_5
) {
1306 rose_write_internal(sk
, ROSE_CALL_ACCEPTED
);
1307 rose_start_idletimer(sk
);
1308 sk
->protinfo
.rose
->condition
= 0x00;
1309 sk
->protinfo
.rose
->vs
= 0;
1310 sk
->protinfo
.rose
->va
= 0;
1311 sk
->protinfo
.rose
->vr
= 0;
1312 sk
->protinfo
.rose
->vl
= 0;
1313 sk
->protinfo
.rose
->state
= ROSE_STATE_3
;
1318 return dev_ioctl(cmd
, (void *)arg
);
1325 static int rose_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1328 struct net_device
*dev
;
1329 const char *devname
, *callsign
;
1336 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");
1338 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
1339 if ((dev
= s
->protinfo
.rose
->device
) == NULL
)
1342 devname
= dev
->name
;
1344 len
+= sprintf(buffer
+ len
, "%-10s %-9s ",
1345 rose2asc(&s
->protinfo
.rose
->dest_addr
),
1346 ax2asc(&s
->protinfo
.rose
->dest_call
));
1348 if (ax25cmp(&s
->protinfo
.rose
->source_call
, &null_ax25_address
) == 0)
1349 callsign
= "??????-?";
1351 callsign
= ax2asc(&s
->protinfo
.rose
->source_call
);
1353 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",
1354 rose2asc(&s
->protinfo
.rose
->source_addr
),
1357 s
->protinfo
.rose
->lci
& 0x0FFF,
1358 (s
->protinfo
.rose
->neighbour
) ? s
->protinfo
.rose
->neighbour
->number
: 0,
1359 s
->protinfo
.rose
->state
,
1360 s
->protinfo
.rose
->vs
,
1361 s
->protinfo
.rose
->vr
,
1362 s
->protinfo
.rose
->va
,
1363 ax25_display_timer(&s
->protinfo
.rose
->timer
) / HZ
,
1364 s
->protinfo
.rose
->t1
/ HZ
,
1365 s
->protinfo
.rose
->t2
/ HZ
,
1366 s
->protinfo
.rose
->t3
/ HZ
,
1367 s
->protinfo
.rose
->hb
/ HZ
,
1368 ax25_display_timer(&s
->protinfo
.rose
->idletimer
) / (60 * HZ
),
1369 s
->protinfo
.rose
->idle
/ (60 * HZ
),
1370 atomic_read(&s
->wmem_alloc
),
1371 atomic_read(&s
->rmem_alloc
),
1372 s
->socket
!= NULL
? s
->socket
->inode
->i_ino
: 0L);
1381 if (pos
> offset
+ length
)
1387 *start
= buffer
+ (offset
- begin
);
1388 len
-= (offset
- begin
);
1390 if (len
> length
) len
= length
;
1395 static struct net_proto_family rose_family_ops
= {
1400 static struct proto_ops
SOCKOPS_WRAPPED(rose_proto_ops
) = {
1403 release
: rose_release
,
1405 connect
: rose_connect
,
1406 socketpair
: sock_no_socketpair
,
1407 accept
: rose_accept
,
1408 getname
: rose_getname
,
1409 poll
: datagram_poll
,
1411 listen
: rose_listen
,
1412 shutdown
: sock_no_shutdown
,
1413 setsockopt
: rose_setsockopt
,
1414 getsockopt
: rose_getsockopt
,
1415 sendmsg
: rose_sendmsg
,
1416 recvmsg
: rose_recvmsg
,
1420 #include <linux/smp_lock.h>
1421 SOCKOPS_WRAP(rose_proto
, PF_ROSE
);
1423 static struct notifier_block rose_dev_notifier
= {
1428 static struct net_device
*dev_rose
;
1430 void __init
rose_proto_init(struct net_proto
*pro
)
1434 rose_callsign
= null_ax25_address
;
1436 if ((dev_rose
= kmalloc(rose_ndevs
* sizeof(struct net_device
), GFP_KERNEL
)) == NULL
) {
1437 printk(KERN_ERR
"ROSE: rose_proto_init - unable to allocate device structure\n");
1441 memset(dev_rose
, 0x00, rose_ndevs
* sizeof(struct net_device
));
1443 for (i
= 0; i
< rose_ndevs
; i
++) {
1444 sprintf(dev_rose
[i
].name
, "rose%d", i
);
1445 dev_rose
[i
].init
= rose_init
;
1446 register_netdev(&dev_rose
[i
]);
1449 sock_register(&rose_family_ops
);
1450 register_netdevice_notifier(&rose_dev_notifier
);
1451 printk(KERN_INFO
"F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.1\n");
1453 ax25_protocol_register(AX25_P_ROSE
, rose_route_frame
);
1454 ax25_linkfail_register(rose_link_failed
);
1456 #ifdef CONFIG_SYSCTL
1457 rose_register_sysctl();
1459 rose_loopback_init();
1461 rose_add_loopback_neigh();
1463 #ifdef CONFIG_PROC_FS
1464 proc_net_create("rose", 0, rose_get_info
);
1465 proc_net_create("rose_neigh", 0, rose_neigh_get_info
);
1466 proc_net_create("rose_nodes", 0, rose_nodes_get_info
);
1467 proc_net_create("rose_routes", 0, rose_routes_get_info
);
1474 MODULE_PARM(rose_ndevs
, "i");
1475 MODULE_PARM_DESC(rose_ndevs
, "number of ROSE devices");
1477 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1478 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1480 int init_module(void)
1482 rose_proto_init(NULL
);
1487 void cleanup_module(void)
1491 #ifdef CONFIG_PROC_FS
1492 proc_net_remove("rose");
1493 proc_net_remove("rose_neigh");
1494 proc_net_remove("rose_nodes");
1495 proc_net_remove("rose_routes");
1497 rose_loopback_clear();
1501 ax25_protocol_release(AX25_P_ROSE
);
1502 ax25_linkfail_release(rose_link_failed
);
1504 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1505 ax25_listen_release(&rose_callsign
, NULL
);
1507 #ifdef CONFIG_SYSCTL
1508 rose_unregister_sysctl();
1510 unregister_netdevice_notifier(&rose_dev_notifier
);
1512 sock_unregister(PF_ROSE
);
1514 for (i
= 0; i
< rose_ndevs
; i
++) {
1515 if (dev_rose
[i
].priv
!= NULL
) {
1516 kfree(dev_rose
[i
].priv
);
1517 dev_rose
[i
].priv
= NULL
;
1518 unregister_netdev(&dev_rose
[i
]);
1520 kfree(dev_rose
[i
].name
);