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/if_arp.h>
58 #include <linux/init.h>
62 int sysctl_rose_restart_request_timeout
= ROSE_DEFAULT_T0
;
63 int sysctl_rose_call_request_timeout
= ROSE_DEFAULT_T1
;
64 int sysctl_rose_reset_request_timeout
= ROSE_DEFAULT_T2
;
65 int sysctl_rose_clear_request_timeout
= ROSE_DEFAULT_T3
;
66 int sysctl_rose_no_activity_timeout
= ROSE_DEFAULT_IDLE
;
67 int sysctl_rose_ack_hold_back_timeout
= ROSE_DEFAULT_HB
;
68 int sysctl_rose_routing_control
= ROSE_DEFAULT_ROUTING
;
69 int sysctl_rose_link_fail_timeout
= ROSE_DEFAULT_FAIL_TIMEOUT
;
70 int sysctl_rose_maximum_vcs
= ROSE_DEFAULT_MAXVC
;
71 int sysctl_rose_window_size
= ROSE_DEFAULT_WINDOW_SIZE
;
73 static struct sock
*volatile rose_list
= NULL
;
75 static struct proto_ops rose_proto_ops
;
77 ax25_address rose_callsign
;
80 * Convert a ROSE address into text.
82 char *rose2asc(rose_address
*addr
)
84 static char buffer
[11];
86 if (addr
->rose_addr
[0] == 0x00 && addr
->rose_addr
[1] == 0x00 &&
87 addr
->rose_addr
[2] == 0x00 && addr
->rose_addr
[3] == 0x00 &&
88 addr
->rose_addr
[4] == 0x00) {
91 sprintf(buffer
, "%02X%02X%02X%02X%02X", addr
->rose_addr
[0] & 0xFF,
92 addr
->rose_addr
[1] & 0xFF,
93 addr
->rose_addr
[2] & 0xFF,
94 addr
->rose_addr
[3] & 0xFF,
95 addr
->rose_addr
[4] & 0xFF);
102 * Compare two ROSE addresses, 0 == equal.
104 int rosecmp(rose_address
*addr1
, rose_address
*addr2
)
108 for (i
= 0; i
< 5; i
++)
109 if (addr1
->rose_addr
[i
] != addr2
->rose_addr
[i
])
116 * Compare two ROSE addresses for only mask digits, 0 == equal.
118 int rosecmpm(rose_address
*addr1
, rose_address
*addr2
, unsigned short mask
)
125 for (i
= 0; i
< mask
; i
++) {
129 if ((addr1
->rose_addr
[j
] & 0x0F) != (addr2
->rose_addr
[j
] & 0x0F))
132 if ((addr1
->rose_addr
[j
] & 0xF0) != (addr2
->rose_addr
[j
] & 0xF0))
140 static void rose_free_sock(struct sock
*sk
)
147 static struct sock
*rose_alloc_sock(void)
152 if ((sk
= sk_alloc(PF_ROSE
, GFP_ATOMIC
, 1)) == NULL
)
155 if ((rose
= kmalloc(sizeof(*rose
), GFP_ATOMIC
)) == NULL
) {
162 memset(rose
, 0x00, sizeof(*rose
));
164 sk
->protinfo
.rose
= rose
;
171 * Socket removal during an interrupt is now safe.
173 static void rose_remove_socket(struct sock
*sk
)
178 save_flags(flags
); cli();
180 if ((s
= rose_list
) == sk
) {
182 restore_flags(flags
);
186 while (s
!= NULL
&& s
->next
!= NULL
) {
189 restore_flags(flags
);
196 restore_flags(flags
);
200 * Kill all bound sockets on a broken link layer connection to a
201 * particular neighbour.
203 void rose_kill_by_neigh(struct rose_neigh
*neigh
)
207 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
208 if (s
->protinfo
.rose
->neighbour
== neigh
) {
209 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
210 s
->protinfo
.rose
->neighbour
->use
--;
211 s
->protinfo
.rose
->neighbour
= NULL
;
217 * Kill all bound sockets on a dropped device.
219 static void rose_kill_by_device(struct device
*dev
)
223 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
224 if (s
->protinfo
.rose
->device
== dev
) {
225 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
226 s
->protinfo
.rose
->neighbour
->use
--;
227 s
->protinfo
.rose
->device
= NULL
;
233 * Handle device status changes.
235 static int rose_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
237 struct device
*dev
= (struct device
*)ptr
;
239 if (event
!= NETDEV_DOWN
)
244 rose_kill_by_device(dev
);
247 rose_link_device_down(dev
);
248 rose_rt_device_down(dev
);
256 * Add a socket to the bound sockets list.
258 static void rose_insert_socket(struct sock
*sk
)
262 save_flags(flags
); cli();
264 sk
->next
= rose_list
;
267 restore_flags(flags
);
271 * Find a socket that wants to accept the Call Request we just
274 static struct sock
*rose_find_listener(rose_address
*addr
, ax25_address
*call
)
279 save_flags(flags
); cli();
281 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
282 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
) {
283 restore_flags(flags
);
288 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
289 if (rosecmp(&s
->protinfo
.rose
->source_addr
, addr
) == 0 && ax25cmp(&s
->protinfo
.rose
->source_call
, &null_ax25_address
) == 0 && s
->state
== TCP_LISTEN
) {
290 restore_flags(flags
);
295 restore_flags(flags
);
300 * Find a connected ROSE socket given my LCI and device.
302 struct sock
*rose_find_socket(unsigned int lci
, struct rose_neigh
*neigh
)
307 save_flags(flags
); cli();
309 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
310 if (s
->protinfo
.rose
->lci
== lci
&& s
->protinfo
.rose
->neighbour
== neigh
) {
311 restore_flags(flags
);
316 restore_flags(flags
);
322 * Find a unique LCI for a given device.
324 unsigned int rose_new_lci(struct rose_neigh
*neigh
)
328 if (neigh
->dce_mode
) {
329 for (lci
= 1; lci
<= sysctl_rose_maximum_vcs
; lci
++)
330 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
333 for (lci
= sysctl_rose_maximum_vcs
; lci
> 0; lci
--)
334 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
344 void rose_destroy_socket(struct sock
*);
347 * Handler for deferred kills.
349 static void rose_destroy_timer(unsigned long data
)
351 rose_destroy_socket((struct sock
*)data
);
355 * This is called from user mode and the timers. Thus it protects itself against
356 * interrupt users but doesn't worry about being called during work.
357 * Once it is removed from the queue no interrupt or bottom half will
358 * touch it and we are (fairly 8-) ) safe.
360 void rose_destroy_socket(struct sock
*sk
) /* Not static as it's used by the timer */
365 save_flags(flags
); cli();
367 rose_stop_heartbeat(sk
);
368 rose_stop_idletimer(sk
);
371 rose_remove_socket(sk
);
372 rose_clear_queues(sk
); /* Flush the queues */
374 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
375 if (skb
->sk
!= sk
) { /* A pending connection */
376 skb
->sk
->dead
= 1; /* Queue the unaccepted socket for death */
377 rose_start_heartbeat(skb
->sk
);
378 skb
->sk
->protinfo
.rose
->state
= ROSE_STATE_0
;
384 if (atomic_read(&sk
->wmem_alloc
) != 0 || atomic_read(&sk
->rmem_alloc
) != 0) {
385 /* Defer: outstanding buffers */
386 init_timer(&sk
->timer
);
387 sk
->timer
.expires
= jiffies
+ 10 * HZ
;
388 sk
->timer
.function
= rose_destroy_timer
;
389 sk
->timer
.data
= (unsigned long)sk
;
390 add_timer(&sk
->timer
);
395 restore_flags(flags
);
399 * Handling for system calls applied via the various interfaces to a
400 * ROSE socket object.
403 static int rose_setsockopt(struct socket
*sock
, int level
, int optname
,
404 char *optval
, int optlen
)
406 struct sock
*sk
= sock
->sk
;
409 if (level
!= SOL_ROSE
)
412 if (optlen
< sizeof(int))
415 if (get_user(opt
, (int *)optval
))
420 sk
->protinfo
.rose
->defer
= opt
? 1 : 0;
426 sk
->protinfo
.rose
->t1
= opt
* HZ
;
432 sk
->protinfo
.rose
->t2
= opt
* HZ
;
438 sk
->protinfo
.rose
->t3
= opt
* HZ
;
444 sk
->protinfo
.rose
->hb
= opt
* HZ
;
450 sk
->protinfo
.rose
->idle
= opt
* 60 * HZ
;
454 sk
->protinfo
.rose
->qbitincl
= opt
? 1 : 0;
462 static int rose_getsockopt(struct socket
*sock
, int level
, int optname
,
463 char *optval
, int *optlen
)
465 struct sock
*sk
= sock
->sk
;
469 if (level
!= SOL_ROSE
)
472 if (get_user(len
, optlen
))
477 val
= sk
->protinfo
.rose
->defer
;
481 val
= sk
->protinfo
.rose
->t1
/ HZ
;
485 val
= sk
->protinfo
.rose
->t2
/ HZ
;
489 val
= sk
->protinfo
.rose
->t3
/ HZ
;
493 val
= sk
->protinfo
.rose
->hb
/ HZ
;
497 val
= sk
->protinfo
.rose
->idle
/ (60 * HZ
);
501 val
= sk
->protinfo
.rose
->qbitincl
;
508 len
= min(len
, sizeof(int));
510 if (put_user(len
, optlen
))
513 if (copy_to_user(optval
, &val
, len
))
519 static int rose_listen(struct socket
*sock
, int backlog
)
521 struct sock
*sk
= sock
->sk
;
523 if (sk
->state
!= TCP_LISTEN
) {
524 sk
->protinfo
.rose
->dest_ndigis
= 0;
525 memset(&sk
->protinfo
.rose
->dest_addr
, '\0', ROSE_ADDR_LEN
);
526 memset(&sk
->protinfo
.rose
->dest_call
, '\0', AX25_ADDR_LEN
);
527 memset(sk
->protinfo
.rose
->dest_digis
, '\0', AX25_ADDR_LEN
*ROSE_MAX_DIGIS
);
528 sk
->max_ack_backlog
= backlog
;
529 sk
->state
= TCP_LISTEN
;
536 static int rose_create(struct socket
*sock
, int protocol
)
541 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
542 return -ESOCKTNOSUPPORT
;
544 if ((sk
= rose_alloc_sock()) == NULL
)
547 rose
= sk
->protinfo
.rose
;
549 sock_init_data(sock
, sk
);
551 skb_queue_head_init(&rose
->ack_queue
);
553 skb_queue_head_init(&rose
->frag_queue
);
557 sock
->ops
= &rose_proto_ops
;
558 sk
->protocol
= protocol
;
560 init_timer(&rose
->timer
);
561 init_timer(&rose
->idletimer
);
563 rose
->t1
= sysctl_rose_call_request_timeout
;
564 rose
->t2
= sysctl_rose_reset_request_timeout
;
565 rose
->t3
= sysctl_rose_clear_request_timeout
;
566 rose
->hb
= sysctl_rose_ack_hold_back_timeout
;
567 rose
->idle
= sysctl_rose_no_activity_timeout
;
569 rose
->state
= ROSE_STATE_0
;
574 static struct sock
*rose_make_new(struct sock
*osk
)
579 if (osk
->type
!= SOCK_SEQPACKET
)
582 if ((sk
= rose_alloc_sock()) == NULL
)
585 rose
= sk
->protinfo
.rose
;
587 sock_init_data(NULL
, sk
);
589 skb_queue_head_init(&rose
->ack_queue
);
591 skb_queue_head_init(&rose
->frag_queue
);
595 sk
->type
= osk
->type
;
596 sk
->socket
= osk
->socket
;
597 sk
->priority
= osk
->priority
;
598 sk
->protocol
= osk
->protocol
;
599 sk
->rcvbuf
= osk
->rcvbuf
;
600 sk
->sndbuf
= osk
->sndbuf
;
601 sk
->debug
= osk
->debug
;
602 sk
->state
= TCP_ESTABLISHED
;
603 sk
->sleep
= osk
->sleep
;
604 sk
->zapped
= osk
->zapped
;
606 init_timer(&rose
->timer
);
607 init_timer(&rose
->idletimer
);
609 rose
->t1
= osk
->protinfo
.rose
->t1
;
610 rose
->t2
= osk
->protinfo
.rose
->t2
;
611 rose
->t3
= osk
->protinfo
.rose
->t3
;
612 rose
->hb
= osk
->protinfo
.rose
->hb
;
613 rose
->idle
= osk
->protinfo
.rose
->idle
;
615 rose
->defer
= osk
->protinfo
.rose
->defer
;
616 rose
->device
= osk
->protinfo
.rose
->device
;
617 rose
->qbitincl
= osk
->protinfo
.rose
->qbitincl
;
622 static int rose_release(struct socket
*sock
, struct socket
*peer
)
624 struct sock
*sk
= sock
->sk
;
626 if (sk
== NULL
) return 0;
628 switch (sk
->protinfo
.rose
->state
) {
631 rose_disconnect(sk
, 0, -1, -1);
632 rose_destroy_socket(sk
);
636 sk
->protinfo
.rose
->neighbour
->use
--;
637 rose_disconnect(sk
, 0, -1, -1);
638 rose_destroy_socket(sk
);
645 rose_clear_queues(sk
);
646 rose_stop_idletimer(sk
);
647 rose_write_internal(sk
, ROSE_CLEAR_REQUEST
);
648 rose_start_t3timer(sk
);
649 sk
->protinfo
.rose
->state
= ROSE_STATE_2
;
650 sk
->state
= TCP_CLOSE
;
651 sk
->shutdown
|= SEND_SHUTDOWN
;
652 sk
->state_change(sk
);
662 sk
->socket
= NULL
; /* Not used, but we should do this. **/
667 static int rose_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
669 struct sock
*sk
= sock
->sk
;
670 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
672 ax25_address
*user
, *source
;
678 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
681 if (addr
->srose_family
!= AF_ROSE
)
684 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
687 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
690 if ((dev
= rose_dev_get(&addr
->srose_addr
)) == NULL
) {
691 SOCK_DEBUG(sk
, "ROSE: bind failed: invalid address\n");
692 return -EADDRNOTAVAIL
;
695 source
= &addr
->srose_call
;
697 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
) {
698 if (ax25_uid_policy
&& !suser())
703 sk
->protinfo
.rose
->source_addr
= addr
->srose_addr
;
704 sk
->protinfo
.rose
->source_call
= *user
;
705 sk
->protinfo
.rose
->device
= dev
;
706 sk
->protinfo
.rose
->source_ndigis
= addr
->srose_ndigis
;
708 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
709 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
710 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
711 sk
->protinfo
.rose
->source_digis
[n
] = full_addr
->srose_digis
[n
];
713 if (sk
->protinfo
.rose
->source_ndigis
== 1) {
714 sk
->protinfo
.rose
->source_digis
[0] = addr
->srose_digi
;
718 rose_insert_socket(sk
);
721 SOCK_DEBUG(sk
, "ROSE: socket is bound\n");
725 static int rose_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
727 struct sock
*sk
= sock
->sk
;
728 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
729 unsigned char cause
, diagnostic
;
734 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
735 sock
->state
= SS_CONNECTED
;
736 return 0; /* Connect completed during a ERESTARTSYS event */
739 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
740 sock
->state
= SS_UNCONNECTED
;
741 return -ECONNREFUSED
;
744 if (sk
->state
== TCP_ESTABLISHED
)
745 return -EISCONN
; /* No reconnect on a seqpacket socket */
747 sk
->state
= TCP_CLOSE
;
748 sock
->state
= SS_UNCONNECTED
;
750 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
753 if (addr
->srose_family
!= AF_ROSE
)
756 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
759 if (addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
762 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
763 if ((sk
->protinfo
.rose
->source_ndigis
+ addr
->srose_ndigis
) > ROSE_MAX_DIGIS
)
766 if ((sk
->protinfo
.rose
->neighbour
= rose_get_neigh(&addr
->srose_addr
, &cause
, &diagnostic
)) == NULL
)
769 if ((sk
->protinfo
.rose
->lci
= rose_new_lci(sk
->protinfo
.rose
->neighbour
)) == 0)
772 if (sk
->zapped
) { /* Must bind first - autobinding in this may or may not work */
775 if ((dev
= rose_dev_first()) == NULL
)
778 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
)
781 memcpy(&sk
->protinfo
.rose
->source_addr
, dev
->dev_addr
, ROSE_ADDR_LEN
);
782 sk
->protinfo
.rose
->source_call
= *user
;
783 sk
->protinfo
.rose
->device
= dev
;
785 rose_insert_socket(sk
); /* Finish the bind */
788 sk
->protinfo
.rose
->dest_addr
= addr
->srose_addr
;
789 sk
->protinfo
.rose
->dest_call
= addr
->srose_call
;
790 sk
->protinfo
.rose
->rand
= ((int)sk
->protinfo
.rose
& 0xFFFF) + sk
->protinfo
.rose
->lci
;
791 sk
->protinfo
.rose
->dest_ndigis
= addr
->srose_ndigis
;
793 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
794 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
795 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
796 sk
->protinfo
.rose
->dest_digis
[n
] = full_addr
->srose_digis
[n
];
798 if (sk
->protinfo
.rose
->dest_ndigis
== 1) {
799 sk
->protinfo
.rose
->dest_digis
[0] = addr
->srose_digi
;
803 /* Move to connecting socket, start sending Connect Requests */
804 sock
->state
= SS_CONNECTING
;
805 sk
->state
= TCP_SYN_SENT
;
807 sk
->protinfo
.rose
->state
= ROSE_STATE_1
;
809 sk
->protinfo
.rose
->neighbour
->use
++;
811 rose_write_internal(sk
, ROSE_CALL_REQUEST
);
812 rose_start_heartbeat(sk
);
813 rose_start_t1timer(sk
);
816 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
819 cli(); /* To avoid races on the sleep */
822 * A Connect Ack with Choke or timeout or failed routing will go to closed.
824 while (sk
->state
== TCP_SYN_SENT
) {
825 interruptible_sleep_on(sk
->sleep
);
826 if (signal_pending(current
)) {
832 if (sk
->state
!= TCP_ESTABLISHED
) {
834 sock
->state
= SS_UNCONNECTED
;
835 return sock_error(sk
); /* Always set at this point */
838 sock
->state
= SS_CONNECTED
;
845 static int rose_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
851 if (newsock
->sk
!= NULL
)
852 rose_destroy_socket(newsock
->sk
);
856 if ((sk
= sock
->sk
) == NULL
)
859 if (sk
->type
!= SOCK_SEQPACKET
)
862 if (sk
->state
!= TCP_LISTEN
)
866 * The write queue this time is holding sockets ready to use
867 * hooked into the SABM we saved
871 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
872 if (flags
& O_NONBLOCK
) {
876 interruptible_sleep_on(sk
->sleep
);
877 if (signal_pending(current
)) {
882 } while (skb
== NULL
);
886 newsk
->socket
= newsock
;
887 newsk
->sleep
= &newsock
->wait
;
890 /* Now attach up the new socket */
899 static int rose_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
900 int *uaddr_len
, int peer
)
902 struct sockaddr_rose
*srose
= (struct sockaddr_rose
*)uaddr
;
903 struct sock
*sk
= sock
->sk
;
907 if (sk
->state
!= TCP_ESTABLISHED
)
909 srose
->srose_family
= AF_ROSE
;
910 srose
->srose_ndigis
= 0;
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 if (*uaddr_len
>= sizeof(struct full_sockaddr_rose
)) {
915 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)uaddr
;
916 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
917 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
918 *uaddr_len
= sizeof(struct full_sockaddr_rose
);
920 if (sk
->protinfo
.rose
->dest_ndigis
>= 1) {
921 srose
->srose_ndigis
= 1;
922 srose
->srose_digi
= sk
->protinfo
.rose
->dest_digis
[0];
924 *uaddr_len
= sizeof(struct sockaddr_rose
);
927 srose
->srose_family
= AF_ROSE
;
928 srose
->srose_ndigis
= 0;
929 srose
->srose_addr
= sk
->protinfo
.rose
->source_addr
;
930 srose
->srose_call
= sk
->protinfo
.rose
->source_call
;
931 srose
->srose_ndigis
= sk
->protinfo
.rose
->source_ndigis
;
932 if (*uaddr_len
>= sizeof(struct full_sockaddr_rose
)) {
933 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)uaddr
;
934 for (n
= 0 ; n
< sk
->protinfo
.rose
->source_ndigis
; n
++)
935 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->source_digis
[n
];
936 *uaddr_len
= sizeof(struct full_sockaddr_rose
);
938 if (sk
->protinfo
.rose
->source_ndigis
>= 1) {
939 srose
->srose_ndigis
= 1;
940 srose
->srose_digi
= sk
->protinfo
.rose
->source_digis
[sk
->protinfo
.rose
->source_ndigis
-1];
942 *uaddr_len
= sizeof(struct sockaddr_rose
);
949 int rose_rx_call_request(struct sk_buff
*skb
, struct device
*dev
, struct rose_neigh
*neigh
, unsigned int lci
)
953 struct rose_facilities_struct facilities
;
956 skb
->sk
= NULL
; /* Initially we don't know who it's for */
959 * skb->data points to the rose frame start
961 memset(&facilities
, 0x00, sizeof(struct rose_facilities_struct
));
963 len
= (((skb
->data
[3] >> 4) & 0x0F) + 1) / 2;
964 len
+= (((skb
->data
[3] >> 0) & 0x0F) + 1) / 2;
965 if (!rose_parse_facilities(skb
->data
+ len
+ 4, &facilities
)) {
966 rose_transmit_clear_request(neigh
, lci
, ROSE_INVALID_FACILITY
, 76);
970 sk
= rose_find_listener(&facilities
.source_addr
, &facilities
.source_call
);
973 * We can't accept the Call Request.
975 if (sk
== NULL
|| sk
->ack_backlog
== sk
->max_ack_backlog
|| (make
= rose_make_new(sk
)) == NULL
) {
976 rose_transmit_clear_request(neigh
, lci
, ROSE_NETWORK_CONGESTION
, 120);
981 make
->state
= TCP_ESTABLISHED
;
983 make
->protinfo
.rose
->lci
= lci
;
984 make
->protinfo
.rose
->dest_addr
= facilities
.dest_addr
;
985 make
->protinfo
.rose
->dest_call
= facilities
.dest_call
;
986 make
->protinfo
.rose
->dest_ndigis
= facilities
.dest_ndigis
;
987 for (n
= 0 ; n
< facilities
.dest_ndigis
; n
++)
988 make
->protinfo
.rose
->dest_digis
[n
] = facilities
.dest_digis
[n
];
989 make
->protinfo
.rose
->source_addr
= facilities
.source_addr
;
990 make
->protinfo
.rose
->source_call
= facilities
.source_call
;
991 make
->protinfo
.rose
->source_ndigis
= facilities
.source_ndigis
;
992 for (n
= 0 ; n
< facilities
.source_ndigis
; n
++)
993 make
->protinfo
.rose
->source_digis
[n
]= facilities
.source_digis
[n
];
994 make
->protinfo
.rose
->neighbour
= neigh
;
995 make
->protinfo
.rose
->device
= dev
;
996 make
->protinfo
.rose
->facilities
= facilities
;
998 make
->protinfo
.rose
->neighbour
->use
++;
1000 if (sk
->protinfo
.rose
->defer
) {
1001 make
->protinfo
.rose
->state
= ROSE_STATE_5
;
1003 rose_write_internal(make
, ROSE_CALL_ACCEPTED
);
1004 make
->protinfo
.rose
->state
= ROSE_STATE_3
;
1005 rose_start_idletimer(make
);
1008 make
->protinfo
.rose
->condition
= 0x00;
1009 make
->protinfo
.rose
->vs
= 0;
1010 make
->protinfo
.rose
->va
= 0;
1011 make
->protinfo
.rose
->vr
= 0;
1012 make
->protinfo
.rose
->vl
= 0;
1016 rose_insert_socket(make
);
1018 skb_queue_head(&sk
->receive_queue
, skb
);
1020 rose_start_heartbeat(make
);
1023 sk
->data_ready(sk
, skb
->len
);
1028 static int rose_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
1029 struct scm_cookie
*scm
)
1031 struct sock
*sk
= sock
->sk
;
1032 struct sockaddr_rose
*usrose
= (struct sockaddr_rose
*)msg
->msg_name
;
1034 struct full_sockaddr_rose srose
;
1035 struct sk_buff
*skb
;
1036 unsigned char *asmptr
;
1037 int n
, size
, qbit
= 0;
1039 if (msg
->msg_flags
& ~MSG_DONTWAIT
)
1043 return -EADDRNOTAVAIL
;
1045 if (sk
->shutdown
& SEND_SHUTDOWN
) {
1046 send_sig(SIGPIPE
, current
, 0);
1050 if (sk
->protinfo
.rose
->neighbour
== NULL
|| sk
->protinfo
.rose
->device
== NULL
)
1051 return -ENETUNREACH
;
1053 if (usrose
!= NULL
) {
1054 if (msg
->msg_namelen
!= sizeof(struct sockaddr_rose
) && msg
->msg_namelen
!= sizeof(struct full_sockaddr_rose
))
1056 memset(&srose
, 0, sizeof(struct full_sockaddr_rose
));
1057 memcpy(&srose
, usrose
, msg
->msg_namelen
);
1058 if (rosecmp(&sk
->protinfo
.rose
->dest_addr
, &srose
.srose_addr
) != 0 ||
1059 ax25cmp(&sk
->protinfo
.rose
->dest_call
, &srose
.srose_call
) != 0)
1061 if (srose
.srose_ndigis
!= sk
->protinfo
.rose
->dest_ndigis
)
1063 if (srose
.srose_ndigis
== sk
->protinfo
.rose
->dest_ndigis
) {
1064 for (n
= 0 ; n
< srose
.srose_ndigis
; n
++)
1065 if (ax25cmp(&sk
->protinfo
.rose
->dest_digis
[n
], &srose
.srose_digis
[n
]) != 0)
1068 if (srose
.srose_family
!= AF_ROSE
)
1071 if (sk
->state
!= TCP_ESTABLISHED
)
1074 srose
.srose_family
= AF_ROSE
;
1075 srose
.srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1076 srose
.srose_call
= sk
->protinfo
.rose
->dest_call
;
1077 srose
.srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1078 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1079 srose
.srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1082 SOCK_DEBUG(sk
, "ROSE: sendto: Addresses built.\n");
1084 /* Build a packet */
1085 SOCK_DEBUG(sk
, "ROSE: sendto: building packet.\n");
1086 size
= len
+ AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
;
1088 if ((skb
= sock_alloc_send_skb(sk
, size
, 0, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1091 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
);
1094 * Put the data on the end
1096 SOCK_DEBUG(sk
, "ROSE: Appending user data\n");
1098 asmptr
= skb
->h
.raw
= skb_put(skb
, len
);
1100 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1103 * If the Q BIT Include socket option is in force, the first
1104 * byte of the user data is the logical value of the Q Bit.
1106 if (sk
->protinfo
.rose
->qbitincl
) {
1107 qbit
= skb
->data
[0];
1112 * Push down the ROSE header
1114 asmptr
= skb_push(skb
, ROSE_MIN_LEN
);
1116 SOCK_DEBUG(sk
, "ROSE: Building Network Header.\n");
1118 /* Build a ROSE Network header */
1119 asmptr
[0] = ((sk
->protinfo
.rose
->lci
>> 8) & 0x0F) | ROSE_GFI
;
1120 asmptr
[1] = (sk
->protinfo
.rose
->lci
>> 0) & 0xFF;
1121 asmptr
[2] = ROSE_DATA
;
1124 asmptr
[0] |= ROSE_Q_BIT
;
1126 SOCK_DEBUG(sk
, "ROSE: Built header.\n");
1128 SOCK_DEBUG(sk
, "ROSE: Transmitting buffer\n");
1130 if (sk
->state
!= TCP_ESTABLISHED
) {
1136 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1137 if (skb
->len
- ROSE_MIN_LEN
> ROSE_PACLEN
) {
1138 unsigned char header
[ROSE_MIN_LEN
];
1139 struct sk_buff
*skbn
;
1143 /* Save a copy of the Header */
1144 memcpy(header
, skb
->data
, ROSE_MIN_LEN
);
1145 skb_pull(skb
, ROSE_MIN_LEN
);
1147 frontlen
= skb_headroom(skb
);
1149 while (skb
->len
> 0) {
1150 if ((skbn
= sock_alloc_send_skb(sk
, frontlen
+ ROSE_PACLEN
, 0, 0, &err
)) == NULL
)
1157 skb_reserve(skbn
, frontlen
);
1159 lg
= (ROSE_PACLEN
> skb
->len
) ? skb
->len
: ROSE_PACLEN
;
1161 /* Copy the user data */
1162 memcpy(skb_put(skbn
, lg
), skb
->data
, lg
);
1165 /* Duplicate the Header */
1166 skb_push(skbn
, ROSE_MIN_LEN
);
1167 memcpy(skbn
->data
, header
, ROSE_MIN_LEN
);
1170 skbn
->data
[2] |= M_BIT
;
1172 skb_queue_tail(&sk
->write_queue
, skbn
); /* Throw it on the queue */
1176 kfree_skb(skb
, FREE_WRITE
);
1178 skb_queue_tail(&sk
->write_queue
, skb
); /* Throw it on the queue */
1181 skb_queue_tail(&sk
->write_queue
, skb
); /* Shove it onto the queue */
1190 static int rose_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1191 int flags
, struct scm_cookie
*scm
)
1193 struct sock
*sk
= sock
->sk
;
1194 struct sockaddr_rose
*srose
= (struct sockaddr_rose
*)msg
->msg_name
;
1196 unsigned char *asmptr
;
1197 struct sk_buff
*skb
;
1201 * This works for seqpacket too. The receiver has ordered the queue for
1202 * us! We do one quick check first though
1204 if (sk
->state
!= TCP_ESTABLISHED
)
1207 /* Now we can treat all alike */
1208 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1211 qbit
= (skb
->data
[0] & ROSE_Q_BIT
) == ROSE_Q_BIT
;
1213 skb_pull(skb
, ROSE_MIN_LEN
);
1215 if (sk
->protinfo
.rose
->qbitincl
) {
1216 asmptr
= skb_push(skb
, 1);
1220 skb
->h
.raw
= skb
->data
;
1223 if (copied
> size
) {
1225 msg
->msg_flags
|= MSG_TRUNC
;
1228 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1230 if (srose
!= NULL
) {
1231 srose
->srose_family
= AF_ROSE
;
1232 srose
->srose_addr
= sk
->protinfo
.rose
->dest_addr
;
1233 srose
->srose_call
= sk
->protinfo
.rose
->dest_call
;
1234 srose
->srose_ndigis
= sk
->protinfo
.rose
->dest_ndigis
;
1235 if (msg
->msg_namelen
>= sizeof(struct full_sockaddr_rose
)) {
1236 struct full_sockaddr_rose
*full_srose
= (struct full_sockaddr_rose
*)msg
->msg_name
;
1237 for (n
= 0 ; n
< sk
->protinfo
.rose
->dest_ndigis
; n
++)
1238 full_srose
->srose_digis
[n
] = sk
->protinfo
.rose
->dest_digis
[n
];
1239 msg
->msg_namelen
= sizeof(struct full_sockaddr_rose
);
1241 if (sk
->protinfo
.rose
->dest_ndigis
>= 1) {
1242 srose
->srose_ndigis
= 1;
1243 srose
->srose_digi
= sk
->protinfo
.rose
->dest_digis
[0];
1245 msg
->msg_namelen
= sizeof(struct sockaddr_rose
);
1249 skb_free_datagram(sk
, skb
);
1254 static int rose_shutdown(struct socket
*sk
, int how
)
1259 static int rose_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1261 struct sock
*sk
= sock
->sk
;
1266 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1269 if (put_user(amount
, (unsigned int *)arg
))
1275 struct sk_buff
*skb
;
1277 /* These two are safe on a single CPU system as only user tasks fiddle here */
1278 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1280 if (put_user(amount
, (unsigned int *)arg
))
1287 if (sk
->stamp
.tv_sec
== 0)
1289 if (copy_to_user((void *)arg
, &sk
->stamp
, sizeof(struct timeval
)))
1297 case SIOCGIFDSTADDR
:
1298 case SIOCSIFDSTADDR
:
1299 case SIOCGIFBRDADDR
:
1300 case SIOCSIFBRDADDR
:
1301 case SIOCGIFNETMASK
:
1302 case SIOCSIFNETMASK
:
1310 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1311 return rose_rt_ioctl(cmd
, (void *)arg
);
1313 case SIOCRSGCAUSE
: {
1314 struct rose_cause_struct rose_cause
;
1315 rose_cause
.cause
= sk
->protinfo
.rose
->cause
;
1316 rose_cause
.diagnostic
= sk
->protinfo
.rose
->diagnostic
;
1317 if (copy_to_user((void *)arg
, &rose_cause
, sizeof(struct rose_cause_struct
)))
1322 case SIOCRSSCAUSE
: {
1323 struct rose_cause_struct rose_cause
;
1324 if (copy_from_user(&rose_cause
, (void *)arg
, sizeof(struct rose_cause_struct
)))
1326 sk
->protinfo
.rose
->cause
= rose_cause
.cause
;
1327 sk
->protinfo
.rose
->diagnostic
= rose_cause
.diagnostic
;
1332 if (!suser()) return -EPERM
;
1333 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1334 ax25_listen_release(&rose_callsign
, NULL
);
1335 if (copy_from_user(&rose_callsign
, (void *)arg
, sizeof(ax25_address
)))
1337 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1338 ax25_listen_register(&rose_callsign
, NULL
);
1342 if (copy_to_user((void *)arg
, &rose_callsign
, sizeof(ax25_address
)))
1347 if (sk
->protinfo
.rose
->state
== ROSE_STATE_5
) {
1348 rose_write_internal(sk
, ROSE_CALL_ACCEPTED
);
1349 rose_start_idletimer(sk
);
1350 sk
->protinfo
.rose
->condition
= 0x00;
1351 sk
->protinfo
.rose
->vs
= 0;
1352 sk
->protinfo
.rose
->va
= 0;
1353 sk
->protinfo
.rose
->vr
= 0;
1354 sk
->protinfo
.rose
->vl
= 0;
1355 sk
->protinfo
.rose
->state
= ROSE_STATE_3
;
1360 return dev_ioctl(cmd
, (void *)arg
);
1367 static int rose_get_info(char *buffer
, char **start
, off_t offset
, int length
, int dummy
)
1371 const char *devname
, *callsign
;
1378 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");
1380 for (s
= rose_list
; s
!= NULL
; s
= s
->next
) {
1381 if ((dev
= s
->protinfo
.rose
->device
) == NULL
)
1384 devname
= dev
->name
;
1386 len
+= sprintf(buffer
+ len
, "%-10s %-9s ",
1387 rose2asc(&s
->protinfo
.rose
->dest_addr
),
1388 ax2asc(&s
->protinfo
.rose
->dest_call
));
1390 if (ax25cmp(&s
->protinfo
.rose
->source_call
, &null_ax25_address
) == 0)
1391 callsign
= "??????-?";
1393 callsign
= ax2asc(&s
->protinfo
.rose
->source_call
);
1395 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",
1396 rose2asc(&s
->protinfo
.rose
->source_addr
),
1399 s
->protinfo
.rose
->lci
& 0x0FFF,
1400 (s
->protinfo
.rose
->neighbour
) ? s
->protinfo
.rose
->neighbour
->number
: 0,
1401 s
->protinfo
.rose
->state
,
1402 s
->protinfo
.rose
->vs
,
1403 s
->protinfo
.rose
->vr
,
1404 s
->protinfo
.rose
->va
,
1405 ax25_display_timer(&s
->protinfo
.rose
->timer
) / HZ
,
1406 s
->protinfo
.rose
->t1
/ HZ
,
1407 s
->protinfo
.rose
->t2
/ HZ
,
1408 s
->protinfo
.rose
->t3
/ HZ
,
1409 s
->protinfo
.rose
->hb
/ HZ
,
1410 ax25_display_timer(&s
->protinfo
.rose
->idletimer
) / (60 * HZ
),
1411 s
->protinfo
.rose
->idle
/ (60 * HZ
),
1412 atomic_read(&s
->wmem_alloc
),
1413 atomic_read(&s
->rmem_alloc
),
1414 s
->socket
!= NULL
? s
->socket
->inode
->i_ino
: 0L);
1423 if (pos
> offset
+ length
)
1429 *start
= buffer
+ (offset
- begin
);
1430 len
-= (offset
- begin
);
1432 if (len
> length
) len
= length
;
1437 static struct net_proto_family rose_family_ops
= {
1442 static struct proto_ops rose_proto_ops
= {
1463 static struct notifier_block rose_dev_notifier
= {
1468 #ifdef CONFIG_PROC_FS
1469 static struct proc_dir_entry proc_net_rose
= {
1470 PROC_NET_RS
, 4, "rose",
1471 S_IFREG
| S_IRUGO
, 1, 0, 0,
1472 0, &proc_net_inode_operations
,
1475 static struct proc_dir_entry proc_net_rose_neigh
= {
1476 PROC_NET_RS_NEIGH
, 10, "rose_neigh",
1477 S_IFREG
| S_IRUGO
, 1, 0, 0,
1478 0, &proc_net_inode_operations
,
1481 static struct proc_dir_entry proc_net_rose_nodes
= {
1482 PROC_NET_RS_NODES
, 10, "rose_nodes",
1483 S_IFREG
| S_IRUGO
, 1, 0, 0,
1484 0, &proc_net_inode_operations
,
1487 static struct proc_dir_entry proc_net_rose_routes
= {
1488 PROC_NET_RS_ROUTES
, 11, "rose_routes",
1489 S_IFREG
| S_IRUGO
, 1, 0, 0,
1490 0, &proc_net_inode_operations
,
1491 rose_routes_get_info
1495 static struct device
*dev_rose
;
1497 __initfunc(void rose_proto_init(struct net_proto
*pro
))
1501 rose_callsign
= null_ax25_address
;
1503 if ((dev_rose
= kmalloc(rose_ndevs
* sizeof(struct device
), GFP_KERNEL
)) == NULL
) {
1504 printk(KERN_ERR
"ROSE: rose_proto_init - unable to allocate device structure\n");
1508 memset(dev_rose
, 0x00, rose_ndevs
* sizeof(struct device
));
1510 for (i
= 0; i
< rose_ndevs
; i
++) {
1511 dev_rose
[i
].name
= kmalloc(20, GFP_KERNEL
);
1512 sprintf(dev_rose
[i
].name
, "rose%d", i
);
1513 dev_rose
[i
].init
= rose_init
;
1514 register_netdev(&dev_rose
[i
]);
1517 sock_register(&rose_family_ops
);
1518 register_netdevice_notifier(&rose_dev_notifier
);
1519 printk(KERN_INFO
"F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.1\n");
1521 ax25_protocol_register(AX25_P_ROSE
, rose_route_frame
);
1522 ax25_linkfail_register(rose_link_failed
);
1524 #ifdef CONFIG_SYSCTL
1525 rose_register_sysctl();
1527 rose_loopback_init();
1529 rose_add_loopback_neigh();
1531 #ifdef CONFIG_PROC_FS
1532 proc_net_register(&proc_net_rose
);
1533 proc_net_register(&proc_net_rose_neigh
);
1534 proc_net_register(&proc_net_rose_nodes
);
1535 proc_net_register(&proc_net_rose_routes
);
1542 MODULE_PARM(rose_ndevs
, "i");
1543 MODULE_PARM_DESC(rose_ndevs
, "number of ROSE devices");
1545 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1546 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1548 int init_module(void)
1550 rose_proto_init(NULL
);
1555 void cleanup_module(void)
1559 #ifdef CONFIG_PROC_FS
1560 proc_net_unregister(PROC_NET_RS
);
1561 proc_net_unregister(PROC_NET_RS_NEIGH
);
1562 proc_net_unregister(PROC_NET_RS_NODES
);
1563 proc_net_unregister(PROC_NET_RS_ROUTES
);
1565 rose_loopback_clear();
1569 ax25_protocol_release(AX25_P_ROSE
);
1570 ax25_linkfail_release(rose_link_failed
);
1572 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1573 ax25_listen_release(&rose_callsign
, NULL
);
1575 #ifdef CONFIG_SYSCTL
1576 rose_unregister_sysctl();
1578 unregister_netdevice_notifier(&rose_dev_notifier
);
1580 sock_unregister(PF_ROSE
);
1582 for (i
= 0; i
< rose_ndevs
; i
++) {
1583 if (dev_rose
[i
].priv
!= NULL
) {
1584 kfree(dev_rose
[i
].priv
);
1585 dev_rose
[i
].priv
= NULL
;
1586 unregister_netdev(&dev_rose
[i
]);
1588 kfree(dev_rose
[i
].name
);