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.
25 #include <linux/config.h>
26 #if defined(CONFIG_ROSE) || defined(CONFIG_ROSE_MODULE)
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/socket.h>
32 #include <linux/kernel.h>
33 #include <linux/sched.h>
34 #include <linux/timer.h>
35 #include <linux/string.h>
36 #include <linux/sockios.h>
37 #include <linux/net.h>
38 #include <linux/stat.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/if_arp.h>
43 #include <linux/skbuff.h>
45 #include <asm/segment.h>
46 #include <asm/system.h>
47 #include <asm/uaccess.h>
48 #include <linux/fcntl.h>
49 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
51 #include <linux/interrupt.h>
52 #include <linux/notifier.h>
54 #include <linux/proc_fs.h>
57 #include <linux/init.h>
61 int sysctl_rose_restart_request_timeout
= ROSE_DEFAULT_T0
;
62 int sysctl_rose_call_request_timeout
= ROSE_DEFAULT_T1
;
63 int sysctl_rose_reset_request_timeout
= ROSE_DEFAULT_T2
;
64 int sysctl_rose_clear_request_timeout
= ROSE_DEFAULT_T3
;
65 int sysctl_rose_no_activity_timeout
= ROSE_DEFAULT_IDLE
;
66 int sysctl_rose_ack_hold_back_timeout
= ROSE_DEFAULT_HB
;
67 int sysctl_rose_routing_control
= ROSE_DEFAULT_ROUTING
;
68 int sysctl_rose_link_fail_timeout
= ROSE_DEFAULT_FAIL_TIMEOUT
;
69 int sysctl_rose_maximum_vcs
= ROSE_DEFAULT_MAXVC
;
70 int sysctl_rose_window_size
= ROSE_DEFAULT_WINDOW_SIZE
;
72 static struct sock
*volatile rose_list
= NULL
;
74 static struct proto_ops rose_proto_ops
;
76 ax25_address rose_callsign
;
79 * Convert a ROSE address into text.
81 char *rose2asc(rose_address
*addr
)
83 static char buffer
[11];
85 if (addr
->rose_addr
[0] == 0x00 && addr
->rose_addr
[1] == 0x00 &&
86 addr
->rose_addr
[2] == 0x00 && addr
->rose_addr
[3] == 0x00 &&
87 addr
->rose_addr
[4] == 0x00) {
90 sprintf(buffer
, "%02X%02X%02X%02X%02X", addr
->rose_addr
[0] & 0xFF,
91 addr
->rose_addr
[1] & 0xFF,
92 addr
->rose_addr
[2] & 0xFF,
93 addr
->rose_addr
[3] & 0xFF,
94 addr
->rose_addr
[4] & 0xFF);
101 * Compare two ROSE addresses, 0 == equal.
103 int rosecmp(rose_address
*addr1
, rose_address
*addr2
)
107 for (i
= 0; i
< 5; i
++)
108 if (addr1
->rose_addr
[i
] != addr2
->rose_addr
[i
])
115 * Compare two ROSE addresses for only mask digits, 0 == equal.
117 int rosecmpm(rose_address
*addr1
, rose_address
*addr2
, unsigned short mask
)
124 for (i
= 0; i
< mask
; i
++) {
128 if ((addr1
->rose_addr
[j
] & 0x0F) != (addr2
->rose_addr
[j
] & 0x0F))
131 if ((addr1
->rose_addr
[j
] & 0xF0) != (addr2
->rose_addr
[j
] & 0xF0))
139 static void rose_free_sock(struct sock
*sk
)
146 static struct sock
*rose_alloc_sock(void)
151 if ((sk
= sk_alloc(PF_ROSE
, GFP_ATOMIC
, 1)) == NULL
)
154 if ((rose
= kmalloc(sizeof(*rose
), GFP_ATOMIC
)) == NULL
) {
161 memset(rose
, 0x00, sizeof(*rose
));
163 sk
->protinfo
.rose
= rose
;
170 * Socket removal during an interrupt is now safe.
172 static void rose_remove_socket(struct sock
*sk
)
177 save_flags(flags
); cli();
179 if ((s
= rose_list
) == sk
) {
181 restore_flags(flags
);
185 while (s
!= NULL
&& s
->next
!= NULL
) {
188 restore_flags(flags
);
195 restore_flags(flags
);
199 * Kill all bound sockets on a broken link layer connection to a
200 * particular neighbour.
202 void rose_kill_by_neigh(struct rose_neigh
*neigh
)
206 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
207 if (s
->protinfo
.rose
->neighbour
== neigh
) {
208 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
209 s
->protinfo
.rose
->neighbour
->use
--;
210 s
->protinfo
.rose
->neighbour
= NULL
;
216 * Kill all bound sockets on a dropped device.
218 static void rose_kill_by_device(struct net_device
*dev
)
222 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
223 if (s
->protinfo
.rose
->device
== dev
) {
224 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
225 s
->protinfo
.rose
->neighbour
->use
--;
226 s
->protinfo
.rose
->device
= NULL
;
232 * Handle device status changes.
234 static int rose_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
236 struct net_device
*dev
= (struct net_device
*)ptr
;
238 if (event
!= NETDEV_DOWN
)
243 rose_kill_by_device(dev
);
246 rose_link_device_down(dev
);
247 rose_rt_device_down(dev
);
255 * Add a socket to the bound sockets list.
257 static void rose_insert_socket(struct sock
*sk
)
261 save_flags(flags
); cli();
263 sk
->next
= rose_list
;
266 restore_flags(flags
);
270 * Find a socket that wants to accept the Call Request we just
273 static struct sock
*rose_find_listener(rose_address
*addr
, ax25_address
*call
)
278 save_flags(flags
); cli();
280 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
281 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
) {
282 restore_flags(flags
);
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
, &null_ax25_address
) == 0 && s
->state
== TCP_LISTEN
) {
289 restore_flags(flags
);
294 restore_flags(flags
);
299 * Find a connected ROSE socket given my LCI and device.
301 struct sock
*rose_find_socket(unsigned int lci
, struct rose_neigh
*neigh
)
306 save_flags(flags
); cli();
308 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
309 if (s
->protinfo
.rose
->lci
== lci
&& s
->protinfo
.rose
->neighbour
== neigh
) {
310 restore_flags(flags
);
315 restore_flags(flags
);
321 * Find a unique LCI for a given device.
323 unsigned int rose_new_lci(struct rose_neigh
*neigh
)
327 if (neigh
->dce_mode
) {
328 for (lci
= 1; lci
<= sysctl_rose_maximum_vcs
; lci
++)
329 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
332 for (lci
= sysctl_rose_maximum_vcs
; lci
> 0; lci
--)
333 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
343 void rose_destroy_socket(struct sock
*);
346 * Handler for deferred kills.
348 static void rose_destroy_timer(unsigned long data
)
350 rose_destroy_socket((struct sock
*)data
);
354 * This is called from user mode and the timers. Thus it protects itself against
355 * interrupt users but doesn't worry about being called during work.
356 * Once it is removed from the queue no interrupt or bottom half will
357 * touch it and we are (fairly 8-) ) safe.
359 void rose_destroy_socket(struct sock
*sk
) /* Not static as it's used by the timer */
364 save_flags(flags
); cli();
366 rose_stop_heartbeat(sk
);
367 rose_stop_idletimer(sk
);
370 rose_remove_socket(sk
);
371 rose_clear_queues(sk
); /* Flush the queues */
373 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
374 if (skb
->sk
!= sk
) { /* A pending connection */
375 skb
->sk
->dead
= 1; /* Queue the unaccepted socket for death */
376 rose_start_heartbeat(skb
->sk
);
377 skb
->sk
->protinfo
.rose
->state
= ROSE_STATE_0
;
383 if (atomic_read(&sk
->wmem_alloc
) != 0 || atomic_read(&sk
->rmem_alloc
) != 0) {
384 /* Defer: outstanding buffers */
385 init_timer(&sk
->timer
);
386 sk
->timer
.expires
= jiffies
+ 10 * HZ
;
387 sk
->timer
.function
= rose_destroy_timer
;
388 sk
->timer
.data
= (unsigned long)sk
;
389 add_timer(&sk
->timer
);
394 restore_flags(flags
);
398 * Handling for system calls applied via the various interfaces to a
399 * ROSE socket object.
402 static int rose_setsockopt(struct socket
*sock
, int level
, int optname
,
403 char *optval
, int optlen
)
405 struct sock
*sk
= sock
->sk
;
408 if (level
!= SOL_ROSE
)
411 if (optlen
< sizeof(int))
414 if (get_user(opt
, (int *)optval
))
419 sk
->protinfo
.rose
->defer
= opt
? 1 : 0;
425 sk
->protinfo
.rose
->t1
= opt
* HZ
;
431 sk
->protinfo
.rose
->t2
= opt
* HZ
;
437 sk
->protinfo
.rose
->t3
= opt
* HZ
;
443 sk
->protinfo
.rose
->hb
= opt
* HZ
;
449 sk
->protinfo
.rose
->idle
= opt
* 60 * HZ
;
453 sk
->protinfo
.rose
->qbitincl
= opt
? 1 : 0;
461 static int rose_getsockopt(struct socket
*sock
, int level
, int optname
,
462 char *optval
, int *optlen
)
464 struct sock
*sk
= sock
->sk
;
468 if (level
!= SOL_ROSE
)
471 if (get_user(len
, optlen
))
476 val
= sk
->protinfo
.rose
->defer
;
480 val
= sk
->protinfo
.rose
->t1
/ HZ
;
484 val
= sk
->protinfo
.rose
->t2
/ HZ
;
488 val
= sk
->protinfo
.rose
->t3
/ HZ
;
492 val
= sk
->protinfo
.rose
->hb
/ HZ
;
496 val
= sk
->protinfo
.rose
->idle
/ (60 * HZ
);
500 val
= sk
->protinfo
.rose
->qbitincl
;
507 len
= min(len
, sizeof(int));
509 if (put_user(len
, optlen
))
512 if (copy_to_user(optval
, &val
, len
))
518 static int rose_listen(struct socket
*sock
, int backlog
)
520 struct sock
*sk
= sock
->sk
;
522 if (sk
->state
!= TCP_LISTEN
) {
523 sk
->protinfo
.rose
->dest_ndigis
= 0;
524 memset(&sk
->protinfo
.rose
->dest_addr
, '\0', ROSE_ADDR_LEN
);
525 memset(&sk
->protinfo
.rose
->dest_call
, '\0', AX25_ADDR_LEN
);
526 memset(sk
->protinfo
.rose
->dest_digis
, '\0', AX25_ADDR_LEN
*ROSE_MAX_DIGIS
);
527 sk
->max_ack_backlog
= backlog
;
528 sk
->state
= TCP_LISTEN
;
535 static int rose_create(struct socket
*sock
, int protocol
)
540 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
541 return -ESOCKTNOSUPPORT
;
543 if ((sk
= rose_alloc_sock()) == NULL
)
546 rose
= sk
->protinfo
.rose
;
548 sock_init_data(sock
, sk
);
550 skb_queue_head_init(&rose
->ack_queue
);
552 skb_queue_head_init(&rose
->frag_queue
);
556 sock
->ops
= &rose_proto_ops
;
557 sk
->protocol
= protocol
;
559 init_timer(&rose
->timer
);
560 init_timer(&rose
->idletimer
);
562 rose
->t1
= sysctl_rose_call_request_timeout
;
563 rose
->t2
= sysctl_rose_reset_request_timeout
;
564 rose
->t3
= sysctl_rose_clear_request_timeout
;
565 rose
->hb
= sysctl_rose_ack_hold_back_timeout
;
566 rose
->idle
= sysctl_rose_no_activity_timeout
;
568 rose
->state
= ROSE_STATE_0
;
573 static struct sock
*rose_make_new(struct sock
*osk
)
578 if (osk
->type
!= SOCK_SEQPACKET
)
581 if ((sk
= rose_alloc_sock()) == NULL
)
584 rose
= sk
->protinfo
.rose
;
586 sock_init_data(NULL
, sk
);
588 skb_queue_head_init(&rose
->ack_queue
);
590 skb_queue_head_init(&rose
->frag_queue
);
594 sk
->type
= osk
->type
;
595 sk
->socket
= osk
->socket
;
596 sk
->priority
= osk
->priority
;
597 sk
->protocol
= osk
->protocol
;
598 sk
->rcvbuf
= osk
->rcvbuf
;
599 sk
->sndbuf
= osk
->sndbuf
;
600 sk
->debug
= osk
->debug
;
601 sk
->state
= TCP_ESTABLISHED
;
602 sk
->sleep
= osk
->sleep
;
603 sk
->zapped
= osk
->zapped
;
605 init_timer(&rose
->timer
);
606 init_timer(&rose
->idletimer
);
608 rose
->t1
= osk
->protinfo
.rose
->t1
;
609 rose
->t2
= osk
->protinfo
.rose
->t2
;
610 rose
->t3
= osk
->protinfo
.rose
->t3
;
611 rose
->hb
= osk
->protinfo
.rose
->hb
;
612 rose
->idle
= osk
->protinfo
.rose
->idle
;
614 rose
->defer
= osk
->protinfo
.rose
->defer
;
615 rose
->device
= osk
->protinfo
.rose
->device
;
616 rose
->qbitincl
= osk
->protinfo
.rose
->qbitincl
;
621 static int rose_release(struct socket
*sock
)
623 struct sock
*sk
= sock
->sk
;
625 if (sk
== NULL
) return 0;
627 switch (sk
->protinfo
.rose
->state
) {
630 rose_disconnect(sk
, 0, -1, -1);
631 rose_destroy_socket(sk
);
635 sk
->protinfo
.rose
->neighbour
->use
--;
636 rose_disconnect(sk
, 0, -1, -1);
637 rose_destroy_socket(sk
);
644 rose_clear_queues(sk
);
645 rose_stop_idletimer(sk
);
646 rose_write_internal(sk
, ROSE_CLEAR_REQUEST
);
647 rose_start_t3timer(sk
);
648 sk
->protinfo
.rose
->state
= ROSE_STATE_2
;
649 sk
->state
= TCP_CLOSE
;
650 sk
->shutdown
|= SEND_SHUTDOWN
;
651 sk
->state_change(sk
);
661 sk
->socket
= NULL
; /* Not used, but we should do this. **/
666 static int rose_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
668 struct sock
*sk
= sock
->sk
;
669 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
670 struct net_device
*dev
;
671 ax25_address
*user
, *source
;
677 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
680 if (addr
->srose_family
!= AF_ROSE
)
683 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
686 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
689 if ((dev
= rose_dev_get(&addr
->srose_addr
)) == NULL
) {
690 SOCK_DEBUG(sk
, "ROSE: bind failed: invalid address\n");
691 return -EADDRNOTAVAIL
;
694 source
= &addr
->srose_call
;
696 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
) {
697 if (ax25_uid_policy
&& !suser())
702 sk
->protinfo
.rose
->source_addr
= addr
->srose_addr
;
703 sk
->protinfo
.rose
->source_call
= *user
;
704 sk
->protinfo
.rose
->device
= dev
;
705 sk
->protinfo
.rose
->source_ndigis
= addr
->srose_ndigis
;
707 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
708 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
709 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
710 sk
->protinfo
.rose
->source_digis
[n
] = full_addr
->srose_digis
[n
];
712 if (sk
->protinfo
.rose
->source_ndigis
== 1) {
713 sk
->protinfo
.rose
->source_digis
[0] = addr
->srose_digi
;
717 rose_insert_socket(sk
);
720 SOCK_DEBUG(sk
, "ROSE: socket is bound\n");
724 static int rose_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
726 struct sock
*sk
= sock
->sk
;
727 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
728 unsigned char cause
, diagnostic
;
730 struct net_device
*dev
;
733 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
734 sock
->state
= SS_CONNECTED
;
735 return 0; /* Connect completed during a ERESTARTSYS event */
738 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
739 sock
->state
= SS_UNCONNECTED
;
740 return -ECONNREFUSED
;
743 if (sk
->state
== TCP_ESTABLISHED
)
744 return -EISCONN
; /* No reconnect on a seqpacket socket */
746 sk
->state
= TCP_CLOSE
;
747 sock
->state
= SS_UNCONNECTED
;
749 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
752 if (addr
->srose_family
!= AF_ROSE
)
755 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
758 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
761 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
762 if ((sk
->protinfo
.rose
->source_ndigis
+ addr
->srose_ndigis
) > ROSE_MAX_DIGIS
)
765 if ((sk
->protinfo
.rose
->neighbour
= rose_get_neigh(&addr
->srose_addr
, &cause
, &diagnostic
)) == NULL
)
768 if ((sk
->protinfo
.rose
->lci
= rose_new_lci(sk
->protinfo
.rose
->neighbour
)) == 0)
771 if (sk
->zapped
) { /* Must bind first - autobinding in this may or may not work */
774 if ((dev
= rose_dev_first()) == NULL
)
777 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
)
780 memcpy(&sk
->protinfo
.rose
->source_addr
, dev
->dev_addr
, ROSE_ADDR_LEN
);
781 sk
->protinfo
.rose
->source_call
= *user
;
782 sk
->protinfo
.rose
->device
= dev
;
784 rose_insert_socket(sk
); /* Finish the bind */
787 sk
->protinfo
.rose
->dest_addr
= addr
->srose_addr
;
788 sk
->protinfo
.rose
->dest_call
= addr
->srose_call
;
789 sk
->protinfo
.rose
->rand
= ((int)sk
->protinfo
.rose
& 0xFFFF) + sk
->protinfo
.rose
->lci
;
790 sk
->protinfo
.rose
->dest_ndigis
= addr
->srose_ndigis
;
792 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
793 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
794 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
795 sk
->protinfo
.rose
->dest_digis
[n
] = full_addr
->srose_digis
[n
];
797 if (sk
->protinfo
.rose
->dest_ndigis
== 1) {
798 sk
->protinfo
.rose
->dest_digis
[0] = addr
->srose_digi
;
802 /* Move to connecting socket, start sending Connect Requests */
803 sock
->state
= SS_CONNECTING
;
804 sk
->state
= TCP_SYN_SENT
;
806 sk
->protinfo
.rose
->state
= ROSE_STATE_1
;
808 sk
->protinfo
.rose
->neighbour
->use
++;
810 rose_write_internal(sk
, ROSE_CALL_REQUEST
);
811 rose_start_heartbeat(sk
);
812 rose_start_t1timer(sk
);
815 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
818 cli(); /* To avoid races on the sleep */
821 * A Connect Ack with Choke or timeout or failed routing will go to closed.
823 while (sk
->state
== TCP_SYN_SENT
) {
824 interruptible_sleep_on(sk
->sleep
);
825 if (signal_pending(current
)) {
831 if (sk
->state
!= TCP_ESTABLISHED
) {
833 sock
->state
= SS_UNCONNECTED
;
834 return sock_error(sk
); /* Always set at this point */
837 sock
->state
= SS_CONNECTED
;
844 static int rose_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
850 if ((sk
= sock
->sk
) == NULL
)
853 if (sk
->type
!= SOCK_SEQPACKET
)
856 if (sk
->state
!= TCP_LISTEN
)
860 * The write queue this time is holding sockets ready to use
861 * hooked into the SABM we saved
865 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
866 if (flags
& O_NONBLOCK
) {
870 interruptible_sleep_on(sk
->sleep
);
871 if (signal_pending(current
)) {
876 } while (skb
== NULL
);
880 newsk
->socket
= newsock
;
881 newsk
->sleep
= &newsock
->wait
;
884 /* Now attach up the new socket */
893 static int rose_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
894 int *uaddr_len
, int peer
)
896 struct sockaddr_rose
*srose
= (struct sockaddr_rose
*)uaddr
;
897 struct sock
*sk
= sock
->sk
;
901 if (sk
->state
!= TCP_ESTABLISHED
)
903 srose
->srose_family
= AF_ROSE
;
904 srose
->srose_ndigis
= 0;
905 srose
->srose_addr
= sk
->protinfo
.rose
->dest_addr
;
906 srose
->srose_call
= sk
->protinfo
.rose
->dest_call
;
907 srose
->srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
908 if (*uaddr_len
>= sizeof(struct full_sockaddr_rose
)) {
909 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)uaddr
;
910 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
911 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
912 *uaddr_len
= sizeof(struct full_sockaddr_rose
);
914 if (sk
->protinfo
.rose
->dest_ndigis
>= 1) {
915 srose
->srose_ndigis
= 1;
916 srose
->srose_digi
= sk
->protinfo
.rose
->dest_digis
[0];
918 *uaddr_len
= sizeof(struct sockaddr_rose
);
921 srose
->srose_family
= AF_ROSE
;
922 srose
->srose_ndigis
= 0;
923 srose
->srose_addr
= sk
->protinfo
.rose
->source_addr
;
924 srose
->srose_call
= sk
->protinfo
.rose
->source_call
;
925 srose
->srose_ndigis
= sk
->protinfo
.rose
->source_ndigis
;
926 if (*uaddr_len
>= sizeof(struct full_sockaddr_rose
)) {
927 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)uaddr
;
928 for (n
= 0 ; n
< sk
->protinfo
.rose
->source_ndigis
; n
++)
929 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->source_digis
[n
];
930 *uaddr_len
= sizeof(struct full_sockaddr_rose
);
932 if (sk
->protinfo
.rose
->source_ndigis
>= 1) {
933 srose
->srose_ndigis
= 1;
934 srose
->srose_digi
= sk
->protinfo
.rose
->source_digis
[sk
->protinfo
.rose
->source_ndigis
-1];
936 *uaddr_len
= sizeof(struct sockaddr_rose
);
943 int rose_rx_call_request(struct sk_buff
*skb
, struct net_device
*dev
, struct rose_neigh
*neigh
, unsigned int lci
)
947 struct rose_facilities_struct facilities
;
950 skb
->sk
= NULL
; /* Initially we don't know who it's for */
953 * skb->data points to the rose frame start
955 memset(&facilities
, 0x00, sizeof(struct rose_facilities_struct
));
957 len
= (((skb
->data
[3] >> 4) & 0x0F) + 1) / 2;
958 len
+= (((skb
->data
[3] >> 0) & 0x0F) + 1) / 2;
959 if (!rose_parse_facilities(skb
->data
+ len
+ 4, &facilities
)) {
960 rose_transmit_clear_request(neigh
, lci
, ROSE_INVALID_FACILITY
, 76);
964 sk
= rose_find_listener(&facilities
.source_addr
, &facilities
.source_call
);
967 * We can't accept the Call Request.
969 if (sk
== NULL
|| sk
->ack_backlog
== sk
->max_ack_backlog
|| (make
= rose_make_new(sk
)) == NULL
) {
970 rose_transmit_clear_request(neigh
, lci
, ROSE_NETWORK_CONGESTION
, 120);
975 make
->state
= TCP_ESTABLISHED
;
977 make
->protinfo
.rose
->lci
= lci
;
978 make
->protinfo
.rose
->dest_addr
= facilities
.dest_addr
;
979 make
->protinfo
.rose
->dest_call
= facilities
.dest_call
;
980 make
->protinfo
.rose
->dest_ndigis
= facilities
.dest_ndigis
;
981 for (n
= 0 ; n
< facilities
.dest_ndigis
; n
++)
982 make
->protinfo
.rose
->dest_digis
[n
] = facilities
.dest_digis
[n
];
983 make
->protinfo
.rose
->source_addr
= facilities
.source_addr
;
984 make
->protinfo
.rose
->source_call
= facilities
.source_call
;
985 make
->protinfo
.rose
->source_ndigis
= facilities
.source_ndigis
;
986 for (n
= 0 ; n
< facilities
.source_ndigis
; n
++)
987 make
->protinfo
.rose
->source_digis
[n
]= facilities
.source_digis
[n
];
988 make
->protinfo
.rose
->neighbour
= neigh
;
989 make
->protinfo
.rose
->device
= dev
;
990 make
->protinfo
.rose
->facilities
= facilities
;
992 make
->protinfo
.rose
->neighbour
->use
++;
994 if (sk
->protinfo
.rose
->defer
) {
995 make
->protinfo
.rose
->state
= ROSE_STATE_5
;
997 rose_write_internal(make
, ROSE_CALL_ACCEPTED
);
998 make
->protinfo
.rose
->state
= ROSE_STATE_3
;
999 rose_start_idletimer(make
);
1002 make
->protinfo
.rose
->condition
= 0x00;
1003 make
->protinfo
.rose
->vs
= 0;
1004 make
->protinfo
.rose
->va
= 0;
1005 make
->protinfo
.rose
->vr
= 0;
1006 make
->protinfo
.rose
->vl
= 0;
1010 rose_insert_socket(make
);
1012 skb_queue_head(&sk
->receive_queue
, skb
);
1014 rose_start_heartbeat(make
);
1017 sk
->data_ready(sk
, skb
->len
);
1022 static int rose_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
1023 struct scm_cookie
*scm
)
1025 struct sock
*sk
= sock
->sk
;
1026 struct sockaddr_rose
*usrose
= (struct sockaddr_rose
*)msg
->msg_name
;
1028 struct full_sockaddr_rose srose
;
1029 struct sk_buff
*skb
;
1030 unsigned char *asmptr
;
1031 int n
, size
, qbit
= 0;
1033 if (msg
->msg_flags
& ~MSG_DONTWAIT
)
1037 return -EADDRNOTAVAIL
;
1039 if (sk
->shutdown
& SEND_SHUTDOWN
) {
1040 send_sig(SIGPIPE
, current
, 0);
1044 if (sk
->protinfo
.rose
->neighbour
== NULL
|| sk
->protinfo
.rose
->device
== NULL
)
1045 return -ENETUNREACH
;
1047 if (usrose
!= NULL
) {
1048 if (msg
->msg_namelen
!= sizeof(struct sockaddr_rose
) && msg
->msg_namelen
!= sizeof(struct full_sockaddr_rose
))
1050 memset(&srose
, 0, sizeof(struct full_sockaddr_rose
));
1051 memcpy(&srose
, usrose
, msg
->msg_namelen
);
1052 if (rosecmp(&sk
->protinfo
.rose
->dest_addr
, &srose
.srose_addr
) != 0 ||
1053 ax25cmp(&sk
->protinfo
.rose
->dest_call
, &srose
.srose_call
) != 0)
1055 if (srose
.srose_ndigis
!= sk
->protinfo
.rose
->dest_ndigis
)
1057 if (srose
.srose_ndigis
== sk
->protinfo
.rose
->dest_ndigis
) {
1058 for (n
= 0 ; n
< srose
.srose_ndigis
; n
++)
1059 if (ax25cmp(&sk
->protinfo
.rose
->dest_digis
[n
], &srose
.srose_digis
[n
]) != 0)
1062 if (srose
.srose_family
!= AF_ROSE
)
1065 if (sk
->state
!= TCP_ESTABLISHED
)
1068 srose
.srose_family
= AF_ROSE
;
1069 srose
.srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1070 srose
.srose_call
= sk
->protinfo
.rose
->dest_call
;
1071 srose
.srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1072 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1073 srose
.srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1076 SOCK_DEBUG(sk
, "ROSE: sendto: Addresses built.\n");
1078 /* Build a packet */
1079 SOCK_DEBUG(sk
, "ROSE: sendto: building packet.\n");
1080 size
= len
+ AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
;
1082 if ((skb
= sock_alloc_send_skb(sk
, size
, 0, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1085 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
);
1088 * Put the data on the end
1090 SOCK_DEBUG(sk
, "ROSE: Appending user data\n");
1092 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1094 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1097 * If the Q BIT Include socket option is in force, the first
1098 * byte of the user data is the logical value of the Q Bit.
1100 if (sk
->protinfo
.rose
->qbitincl
) {
1101 qbit
= skb
->data
[0];
1106 * Push down the ROSE header
1108 asmptr
= skb_push(skb
, ROSE_MIN_LEN
);
1110 SOCK_DEBUG(sk
, "ROSE: Building Network Header.\n");
1112 /* Build a ROSE Network header */
1113 asmptr
[0] = ((sk
->protinfo
.rose
->lci
>> 8) & 0x0F) | ROSE_GFI
;
1114 asmptr
[1] = (sk
->protinfo
.rose
->lci
>> 0) & 0xFF;
1115 asmptr
[2] = ROSE_DATA
;
1118 asmptr
[0] |= ROSE_Q_BIT
;
1120 SOCK_DEBUG(sk
, "ROSE: Built header.\n");
1122 SOCK_DEBUG(sk
, "ROSE: Transmitting buffer\n");
1124 if (sk
->state
!= TCP_ESTABLISHED
) {
1130 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1131 if (skb
->len
- ROSE_MIN_LEN
> ROSE_PACLEN
) {
1132 unsigned char header
[ROSE_MIN_LEN
];
1133 struct sk_buff
*skbn
;
1137 /* Save a copy of the Header */
1138 memcpy(header
, skb
->data
, ROSE_MIN_LEN
);
1139 skb_pull(skb
, ROSE_MIN_LEN
);
1141 frontlen
= skb_headroom(skb
);
1143 while (skb
->len
> 0) {
1144 if ((skbn
= sock_alloc_send_skb(sk
, frontlen
+ ROSE_PACLEN
, 0, 0, &err
)) == NULL
)
1151 skb_reserve(skbn
, frontlen
);
1153 lg
= (ROSE_PACLEN
> skb
->len
) ? skb
->len
: ROSE_PACLEN
;
1155 /* Copy the user data */
1156 memcpy(skb_put(skbn
, lg
), skb
->data
, lg
);
1159 /* Duplicate the Header */
1160 skb_push(skbn
, ROSE_MIN_LEN
);
1161 memcpy(skbn
->data
, header
, ROSE_MIN_LEN
);
1164 skbn
->data
[2] |= M_BIT
;
1166 skb_queue_tail(&sk
->write_queue
, skbn
); /* Throw it on the queue */
1170 kfree_skb(skb
, FREE_WRITE
);
1172 skb_queue_tail(&sk
->write_queue
, skb
); /* Throw it on the queue */
1175 skb_queue_tail(&sk
->write_queue
, skb
); /* Shove it onto the queue */
1184 static int rose_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1185 int flags
, struct scm_cookie
*scm
)
1187 struct sock
*sk
= sock
->sk
;
1188 struct sockaddr_rose
*srose
= (struct sockaddr_rose
*)msg
->msg_name
;
1190 unsigned char *asmptr
;
1191 struct sk_buff
*skb
;
1195 * This works for seqpacket too. The receiver has ordered the queue for
1196 * us! We do one quick check first though
1198 if (sk
->state
!= TCP_ESTABLISHED
)
1201 /* Now we can treat all alike */
1202 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1205 qbit
= (skb
->data
[0] & ROSE_Q_BIT
) == ROSE_Q_BIT
;
1207 skb_pull(skb
, ROSE_MIN_LEN
);
1209 if (sk
->protinfo
.rose
->qbitincl
) {
1210 asmptr
= skb_push(skb
, 1);
1214 skb
->h
.raw
= skb
->data
;
1217 if (copied
> size
) {
1219 msg
->msg_flags
|= MSG_TRUNC
;
1222 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1224 if (srose
!= NULL
) {
1225 srose
->srose_family
= AF_ROSE
;
1226 srose
->srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1227 srose
->srose_call
= sk
->protinfo
.rose
->dest_call
;
1228 srose
->srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1229 if (msg
->msg_namelen
>= sizeof(struct full_sockaddr_rose
)) {
1230 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)msg
->msg_name
;
1231 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1232 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1233 msg
->msg_namelen
= sizeof(struct full_sockaddr_rose
);
1235 if (sk
->protinfo
.rose
->dest_ndigis
>= 1) {
1236 srose
->srose_ndigis
= 1;
1237 srose
->srose_digi
= sk
->protinfo
.rose
->dest_digis
[0];
1239 msg
->msg_namelen
= sizeof(struct sockaddr_rose
);
1243 skb_free_datagram(sk
, skb
);
1249 static int rose_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1251 struct sock
*sk
= sock
->sk
;
1256 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1259 if (put_user(amount
, (unsigned int *)arg
))
1265 struct sk_buff
*skb
;
1267 /* These two are safe on a single CPU system as only user tasks fiddle here */
1268 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1270 if (put_user(amount
, (unsigned int *)arg
))
1277 if (sk
->stamp
.tv_sec
== 0)
1279 if (copy_to_user((void *)arg
, &sk
->stamp
, sizeof(struct timeval
)))
1287 case SIOCGIFDSTADDR
:
1288 case SIOCSIFDSTADDR
:
1289 case SIOCGIFBRDADDR
:
1290 case SIOCSIFBRDADDR
:
1291 case SIOCGIFNETMASK
:
1292 case SIOCSIFNETMASK
:
1300 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1301 return rose_rt_ioctl(cmd
, (void *)arg
);
1303 case SIOCRSGCAUSE
: {
1304 struct rose_cause_struct rose_cause
;
1305 rose_cause
.cause
= sk
->protinfo
.rose
->cause
;
1306 rose_cause
.diagnostic
= sk
->protinfo
.rose
->diagnostic
;
1307 if (copy_to_user((void *)arg
, &rose_cause
, sizeof(struct rose_cause_struct
)))
1312 case SIOCRSSCAUSE
: {
1313 struct rose_cause_struct rose_cause
;
1314 if (copy_from_user(&rose_cause
, (void *)arg
, sizeof(struct rose_cause_struct
)))
1316 sk
->protinfo
.rose
->cause
= rose_cause
.cause
;
1317 sk
->protinfo
.rose
->diagnostic
= rose_cause
.diagnostic
;
1322 if (!suser()) return -EPERM
;
1323 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1324 ax25_listen_release(&rose_callsign
, NULL
);
1325 if (copy_from_user(&rose_callsign
, (void *)arg
, sizeof(ax25_address
)))
1327 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1328 ax25_listen_register(&rose_callsign
, NULL
);
1332 if (copy_to_user((void *)arg
, &rose_callsign
, sizeof(ax25_address
)))
1337 if (sk
->protinfo
.rose
->state
== ROSE_STATE_5
) {
1338 rose_write_internal(sk
, ROSE_CALL_ACCEPTED
);
1339 rose_start_idletimer(sk
);
1340 sk
->protinfo
.rose
->condition
= 0x00;
1341 sk
->protinfo
.rose
->vs
= 0;
1342 sk
->protinfo
.rose
->va
= 0;
1343 sk
->protinfo
.rose
->vr
= 0;
1344 sk
->protinfo
.rose
->vl
= 0;
1345 sk
->protinfo
.rose
->state
= ROSE_STATE_3
;
1350 return dev_ioctl(cmd
, (void *)arg
);
1357 static int rose_get_info(char *buffer
, char **start
, off_t offset
, int length
, int dummy
)
1360 struct net_device
*dev
;
1361 const char *devname
, *callsign
;
1368 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");
1370 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
1371 if ((dev
= s
->protinfo
.rose
->device
) == NULL
)
1374 devname
= dev
->name
;
1376 len
+= sprintf(buffer
+ len
, "%-10s %-9s ",
1377 rose2asc(&s
->protinfo
.rose
->dest_addr
),
1378 ax2asc(&s
->protinfo
.rose
->dest_call
));
1380 if (ax25cmp(&s
->protinfo
.rose
->source_call
, &null_ax25_address
) == 0)
1381 callsign
= "??????-?";
1383 callsign
= ax2asc(&s
->protinfo
.rose
->source_call
);
1385 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",
1386 rose2asc(&s
->protinfo
.rose
->source_addr
),
1389 s
->protinfo
.rose
->lci
& 0x0FFF,
1390 (s
->protinfo
.rose
->neighbour
) ? s
->protinfo
.rose
->neighbour
->number
: 0,
1391 s
->protinfo
.rose
->state
,
1392 s
->protinfo
.rose
->vs
,
1393 s
->protinfo
.rose
->vr
,
1394 s
->protinfo
.rose
->va
,
1395 ax25_display_timer(&s
->protinfo
.rose
->timer
) / HZ
,
1396 s
->protinfo
.rose
->t1
/ HZ
,
1397 s
->protinfo
.rose
->t2
/ HZ
,
1398 s
->protinfo
.rose
->t3
/ HZ
,
1399 s
->protinfo
.rose
->hb
/ HZ
,
1400 ax25_display_timer(&s
->protinfo
.rose
->idletimer
) / (60 * HZ
),
1401 s
->protinfo
.rose
->idle
/ (60 * HZ
),
1402 atomic_read(&s
->wmem_alloc
),
1403 atomic_read(&s
->rmem_alloc
),
1404 s
->socket
!= NULL
? s
->socket
->inode
->i_ino
: 0L);
1413 if (pos
> offset
+ length
)
1419 *start
= buffer
+ (offset
- begin
);
1420 len
-= (offset
- begin
);
1422 if (len
> length
) len
= length
;
1427 static struct net_proto_family rose_family_ops
= {
1432 static struct proto_ops
SOCKOPS_WRAPPED(rose_proto_ops
) = {
1453 #include <linux/smp_lock.h>
1454 SOCKOPS_WRAP(rose_proto
, PF_ROSE
);
1456 static struct notifier_block rose_dev_notifier
= {
1461 #ifdef CONFIG_PROC_FS
1462 static struct proc_dir_entry proc_net_rose
= {
1463 PROC_NET_RS
, 4, "rose",
1464 S_IFREG
| S_IRUGO
, 1, 0, 0,
1465 0, &proc_net_inode_operations
,
1468 static struct proc_dir_entry proc_net_rose_neigh
= {
1469 PROC_NET_RS_NEIGH
, 10, "rose_neigh",
1470 S_IFREG
| S_IRUGO
, 1, 0, 0,
1471 0, &proc_net_inode_operations
,
1474 static struct proc_dir_entry proc_net_rose_nodes
= {
1475 PROC_NET_RS_NODES
, 10, "rose_nodes",
1476 S_IFREG
| S_IRUGO
, 1, 0, 0,
1477 0, &proc_net_inode_operations
,
1480 static struct proc_dir_entry proc_net_rose_routes
= {
1481 PROC_NET_RS_ROUTES
, 11, "rose_routes",
1482 S_IFREG
| S_IRUGO
, 1, 0, 0,
1483 0, &proc_net_inode_operations
,
1484 rose_routes_get_info
1488 static struct net_device
*dev_rose
;
1490 void __init
rose_proto_init(struct net_proto
*pro
)
1494 rose_callsign
= null_ax25_address
;
1496 if ((dev_rose
= kmalloc(rose_ndevs
* sizeof(struct net_device
), GFP_KERNEL
)) == NULL
) {
1497 printk(KERN_ERR
"ROSE: rose_proto_init - unable to allocate device structure\n");
1501 memset(dev_rose
, 0x00, rose_ndevs
* sizeof(struct net_device
));
1503 for (i
= 0; i
< rose_ndevs
; i
++) {
1504 dev_rose
[i
].name
= kmalloc(20, GFP_KERNEL
);
1505 if(dev_rose
[i
].name
== NULL
)
1507 printk(KERN_ERR
"Rose: unable to register ROSE devices.\n");
1510 sprintf(dev_rose
[i
].name
, "rose%d", i
);
1511 dev_rose
[i
].init
= rose_init
;
1512 register_netdev(&dev_rose
[i
]);
1515 sock_register(&rose_family_ops
);
1516 register_netdevice_notifier(&rose_dev_notifier
);
1517 printk(KERN_INFO
"F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.1\n");
1519 ax25_protocol_register(AX25_P_ROSE
, rose_route_frame
);
1520 ax25_linkfail_register(rose_link_failed
);
1522 #ifdef CONFIG_SYSCTL
1523 rose_register_sysctl();
1525 rose_loopback_init();
1527 rose_add_loopback_neigh();
1529 #ifdef CONFIG_PROC_FS
1530 proc_net_register(&proc_net_rose
);
1531 proc_net_register(&proc_net_rose_neigh
);
1532 proc_net_register(&proc_net_rose_nodes
);
1533 proc_net_register(&proc_net_rose_routes
);
1540 MODULE_PARM(rose_ndevs
, "i");
1541 MODULE_PARM_DESC(rose_ndevs
, "number of ROSE devices");
1543 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1544 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1546 int init_module(void)
1548 rose_proto_init(NULL
);
1553 void cleanup_module(void)
1557 #ifdef CONFIG_PROC_FS
1558 proc_net_unregister(PROC_NET_RS
);
1559 proc_net_unregister(PROC_NET_RS_NEIGH
);
1560 proc_net_unregister(PROC_NET_RS_NODES
);
1561 proc_net_unregister(PROC_NET_RS_ROUTES
);
1563 rose_loopback_clear();
1567 ax25_protocol_release(AX25_P_ROSE
);
1568 ax25_linkfail_release(rose_link_failed
);
1570 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1571 ax25_listen_release(&rose_callsign
, NULL
);
1573 #ifdef CONFIG_SYSCTL
1574 rose_unregister_sysctl();
1576 unregister_netdevice_notifier(&rose_dev_notifier
);
1578 sock_unregister(PF_ROSE
);
1580 for (i
= 0; i
< rose_ndevs
; i
++) {
1581 if (dev_rose
[i
].priv
!= NULL
) {
1582 kfree(dev_rose
[i
].priv
);
1583 dev_rose
[i
].priv
= NULL
;
1584 unregister_netdev(&dev_rose
[i
]);
1586 kfree(dev_rose
[i
].name
);