1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
5 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
6 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
7 * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
10 #include <linux/capability.h>
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
18 #include <linux/slab.h>
19 #include <linux/kernel.h>
20 #include <linux/sched/signal.h>
21 #include <linux/spinlock.h>
22 #include <linux/timer.h>
23 #include <linux/string.h>
24 #include <linux/sockios.h>
25 #include <linux/net.h>
26 #include <linux/stat.h>
27 #include <net/net_namespace.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/skbuff.h>
34 #include <linux/uaccess.h>
35 #include <linux/fcntl.h>
36 #include <linux/termios.h>
38 #include <linux/interrupt.h>
39 #include <linux/notifier.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <net/tcp_states.h>
47 static int rose_ndevs
= 10;
49 int sysctl_rose_restart_request_timeout
= ROSE_DEFAULT_T0
;
50 int sysctl_rose_call_request_timeout
= ROSE_DEFAULT_T1
;
51 int sysctl_rose_reset_request_timeout
= ROSE_DEFAULT_T2
;
52 int sysctl_rose_clear_request_timeout
= ROSE_DEFAULT_T3
;
53 int sysctl_rose_no_activity_timeout
= ROSE_DEFAULT_IDLE
;
54 int sysctl_rose_ack_hold_back_timeout
= ROSE_DEFAULT_HB
;
55 int sysctl_rose_routing_control
= ROSE_DEFAULT_ROUTING
;
56 int sysctl_rose_link_fail_timeout
= ROSE_DEFAULT_FAIL_TIMEOUT
;
57 int sysctl_rose_maximum_vcs
= ROSE_DEFAULT_MAXVC
;
58 int sysctl_rose_window_size
= ROSE_DEFAULT_WINDOW_SIZE
;
60 static HLIST_HEAD(rose_list
);
61 static DEFINE_SPINLOCK(rose_list_lock
);
63 static const struct proto_ops rose_proto_ops
;
65 ax25_address rose_callsign
;
68 * Convert a ROSE address into text.
70 char *rose2asc(char *buf
, const rose_address
*addr
)
72 if (addr
->rose_addr
[0] == 0x00 && addr
->rose_addr
[1] == 0x00 &&
73 addr
->rose_addr
[2] == 0x00 && addr
->rose_addr
[3] == 0x00 &&
74 addr
->rose_addr
[4] == 0x00) {
77 sprintf(buf
, "%02X%02X%02X%02X%02X", addr
->rose_addr
[0] & 0xFF,
78 addr
->rose_addr
[1] & 0xFF,
79 addr
->rose_addr
[2] & 0xFF,
80 addr
->rose_addr
[3] & 0xFF,
81 addr
->rose_addr
[4] & 0xFF);
88 * Compare two ROSE addresses, 0 == equal.
90 int rosecmp(rose_address
*addr1
, rose_address
*addr2
)
94 for (i
= 0; i
< 5; i
++)
95 if (addr1
->rose_addr
[i
] != addr2
->rose_addr
[i
])
102 * Compare two ROSE addresses for only mask digits, 0 == equal.
104 int rosecmpm(rose_address
*addr1
, rose_address
*addr2
, unsigned short mask
)
111 for (i
= 0; i
< mask
; i
++) {
115 if ((addr1
->rose_addr
[j
] & 0x0F) != (addr2
->rose_addr
[j
] & 0x0F))
118 if ((addr1
->rose_addr
[j
] & 0xF0) != (addr2
->rose_addr
[j
] & 0xF0))
127 * Socket removal during an interrupt is now safe.
129 static void rose_remove_socket(struct sock
*sk
)
131 spin_lock_bh(&rose_list_lock
);
132 sk_del_node_init(sk
);
133 spin_unlock_bh(&rose_list_lock
);
137 * Kill all bound sockets on a broken link layer connection to a
138 * particular neighbour.
140 void rose_kill_by_neigh(struct rose_neigh
*neigh
)
144 spin_lock_bh(&rose_list_lock
);
145 sk_for_each(s
, &rose_list
) {
146 struct rose_sock
*rose
= rose_sk(s
);
148 if (rose
->neighbour
== neigh
) {
149 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
150 rose
->neighbour
->use
--;
151 rose
->neighbour
= NULL
;
154 spin_unlock_bh(&rose_list_lock
);
158 * Kill all bound sockets on a dropped device.
160 static void rose_kill_by_device(struct net_device
*dev
)
164 spin_lock_bh(&rose_list_lock
);
165 sk_for_each(s
, &rose_list
) {
166 struct rose_sock
*rose
= rose_sk(s
);
168 if (rose
->device
== dev
) {
169 rose_disconnect(s
, ENETUNREACH
, ROSE_OUT_OF_ORDER
, 0);
171 rose
->neighbour
->use
--;
175 spin_unlock_bh(&rose_list_lock
);
179 * Handle device status changes.
181 static int rose_device_event(struct notifier_block
*this,
182 unsigned long event
, void *ptr
)
184 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
186 if (!net_eq(dev_net(dev
), &init_net
))
189 if (event
!= NETDEV_DOWN
)
194 rose_kill_by_device(dev
);
197 rose_link_device_down(dev
);
198 rose_rt_device_down(dev
);
206 * Add a socket to the bound sockets list.
208 static void rose_insert_socket(struct sock
*sk
)
211 spin_lock_bh(&rose_list_lock
);
212 sk_add_node(sk
, &rose_list
);
213 spin_unlock_bh(&rose_list_lock
);
217 * Find a socket that wants to accept the Call Request we just
220 static struct sock
*rose_find_listener(rose_address
*addr
, ax25_address
*call
)
224 spin_lock_bh(&rose_list_lock
);
225 sk_for_each(s
, &rose_list
) {
226 struct rose_sock
*rose
= rose_sk(s
);
228 if (!rosecmp(&rose
->source_addr
, addr
) &&
229 !ax25cmp(&rose
->source_call
, call
) &&
230 !rose
->source_ndigis
&& s
->sk_state
== TCP_LISTEN
)
234 sk_for_each(s
, &rose_list
) {
235 struct rose_sock
*rose
= rose_sk(s
);
237 if (!rosecmp(&rose
->source_addr
, addr
) &&
238 !ax25cmp(&rose
->source_call
, &null_ax25_address
) &&
239 s
->sk_state
== TCP_LISTEN
)
244 spin_unlock_bh(&rose_list_lock
);
249 * Find a connected ROSE socket given my LCI and device.
251 struct sock
*rose_find_socket(unsigned int lci
, struct rose_neigh
*neigh
)
255 spin_lock_bh(&rose_list_lock
);
256 sk_for_each(s
, &rose_list
) {
257 struct rose_sock
*rose
= rose_sk(s
);
259 if (rose
->lci
== lci
&& rose
->neighbour
== neigh
)
264 spin_unlock_bh(&rose_list_lock
);
269 * Find a unique LCI for a given device.
271 unsigned int rose_new_lci(struct rose_neigh
*neigh
)
275 if (neigh
->dce_mode
) {
276 for (lci
= 1; lci
<= sysctl_rose_maximum_vcs
; lci
++)
277 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
280 for (lci
= sysctl_rose_maximum_vcs
; lci
> 0; lci
--)
281 if (rose_find_socket(lci
, neigh
) == NULL
&& rose_route_free_lci(lci
, neigh
) == NULL
)
291 void rose_destroy_socket(struct sock
*);
294 * Handler for deferred kills.
296 static void rose_destroy_timer(struct timer_list
*t
)
298 struct sock
*sk
= from_timer(sk
, t
, sk_timer
);
300 rose_destroy_socket(sk
);
304 * This is called from user mode and the timers. Thus it protects itself
305 * against interrupt users but doesn't worry about being called during
306 * work. Once it is removed from the queue no interrupt or bottom half
307 * will touch it and we are (fairly 8-) ) safe.
309 void rose_destroy_socket(struct sock
*sk
)
313 rose_remove_socket(sk
);
314 rose_stop_heartbeat(sk
);
315 rose_stop_idletimer(sk
);
318 rose_clear_queues(sk
); /* Flush the queues */
320 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
321 if (skb
->sk
!= sk
) { /* A pending connection */
322 /* Queue the unaccepted socket for death */
323 sock_set_flag(skb
->sk
, SOCK_DEAD
);
324 rose_start_heartbeat(skb
->sk
);
325 rose_sk(skb
->sk
)->state
= ROSE_STATE_0
;
331 if (sk_has_allocations(sk
)) {
332 /* Defer: outstanding buffers */
333 timer_setup(&sk
->sk_timer
, rose_destroy_timer
, 0);
334 sk
->sk_timer
.expires
= jiffies
+ 10 * HZ
;
335 add_timer(&sk
->sk_timer
);
341 * Handling for system calls applied via the various interfaces to a
342 * ROSE socket object.
345 static int rose_setsockopt(struct socket
*sock
, int level
, int optname
,
346 char __user
*optval
, unsigned int optlen
)
348 struct sock
*sk
= sock
->sk
;
349 struct rose_sock
*rose
= rose_sk(sk
);
352 if (level
!= SOL_ROSE
)
355 if (optlen
< sizeof(int))
358 if (get_user(opt
, (int __user
*)optval
))
363 rose
->defer
= opt
? 1 : 0;
393 rose
->idle
= opt
* 60 * HZ
;
397 rose
->qbitincl
= opt
? 1 : 0;
405 static int rose_getsockopt(struct socket
*sock
, int level
, int optname
,
406 char __user
*optval
, int __user
*optlen
)
408 struct sock
*sk
= sock
->sk
;
409 struct rose_sock
*rose
= rose_sk(sk
);
413 if (level
!= SOL_ROSE
)
416 if (get_user(len
, optlen
))
444 val
= rose
->idle
/ (60 * HZ
);
448 val
= rose
->qbitincl
;
455 len
= min_t(unsigned int, len
, sizeof(int));
457 if (put_user(len
, optlen
))
460 return copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
463 static int rose_listen(struct socket
*sock
, int backlog
)
465 struct sock
*sk
= sock
->sk
;
467 if (sk
->sk_state
!= TCP_LISTEN
) {
468 struct rose_sock
*rose
= rose_sk(sk
);
470 rose
->dest_ndigis
= 0;
471 memset(&rose
->dest_addr
, 0, ROSE_ADDR_LEN
);
472 memset(&rose
->dest_call
, 0, AX25_ADDR_LEN
);
473 memset(rose
->dest_digis
, 0, AX25_ADDR_LEN
* ROSE_MAX_DIGIS
);
474 sk
->sk_max_ack_backlog
= backlog
;
475 sk
->sk_state
= TCP_LISTEN
;
482 static struct proto rose_proto
= {
484 .owner
= THIS_MODULE
,
485 .obj_size
= sizeof(struct rose_sock
),
488 static int rose_create(struct net
*net
, struct socket
*sock
, int protocol
,
492 struct rose_sock
*rose
;
494 if (!net_eq(net
, &init_net
))
495 return -EAFNOSUPPORT
;
497 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
498 return -ESOCKTNOSUPPORT
;
500 sk
= sk_alloc(net
, PF_ROSE
, GFP_ATOMIC
, &rose_proto
, kern
);
506 sock_init_data(sock
, sk
);
508 skb_queue_head_init(&rose
->ack_queue
);
510 skb_queue_head_init(&rose
->frag_queue
);
514 sock
->ops
= &rose_proto_ops
;
515 sk
->sk_protocol
= protocol
;
517 timer_setup(&rose
->timer
, NULL
, 0);
518 timer_setup(&rose
->idletimer
, NULL
, 0);
520 rose
->t1
= msecs_to_jiffies(sysctl_rose_call_request_timeout
);
521 rose
->t2
= msecs_to_jiffies(sysctl_rose_reset_request_timeout
);
522 rose
->t3
= msecs_to_jiffies(sysctl_rose_clear_request_timeout
);
523 rose
->hb
= msecs_to_jiffies(sysctl_rose_ack_hold_back_timeout
);
524 rose
->idle
= msecs_to_jiffies(sysctl_rose_no_activity_timeout
);
526 rose
->state
= ROSE_STATE_0
;
531 static struct sock
*rose_make_new(struct sock
*osk
)
534 struct rose_sock
*rose
, *orose
;
536 if (osk
->sk_type
!= SOCK_SEQPACKET
)
539 sk
= sk_alloc(sock_net(osk
), PF_ROSE
, GFP_ATOMIC
, &rose_proto
, 0);
545 sock_init_data(NULL
, sk
);
547 skb_queue_head_init(&rose
->ack_queue
);
549 skb_queue_head_init(&rose
->frag_queue
);
553 sk
->sk_type
= osk
->sk_type
;
554 sk
->sk_priority
= osk
->sk_priority
;
555 sk
->sk_protocol
= osk
->sk_protocol
;
556 sk
->sk_rcvbuf
= osk
->sk_rcvbuf
;
557 sk
->sk_sndbuf
= osk
->sk_sndbuf
;
558 sk
->sk_state
= TCP_ESTABLISHED
;
559 sock_copy_flags(sk
, osk
);
561 timer_setup(&rose
->timer
, NULL
, 0);
562 timer_setup(&rose
->idletimer
, NULL
, 0);
564 orose
= rose_sk(osk
);
565 rose
->t1
= orose
->t1
;
566 rose
->t2
= orose
->t2
;
567 rose
->t3
= orose
->t3
;
568 rose
->hb
= orose
->hb
;
569 rose
->idle
= orose
->idle
;
570 rose
->defer
= orose
->defer
;
571 rose
->device
= orose
->device
;
572 rose
->qbitincl
= orose
->qbitincl
;
577 static int rose_release(struct socket
*sock
)
579 struct sock
*sk
= sock
->sk
;
580 struct rose_sock
*rose
;
582 if (sk
== NULL
) return 0;
589 switch (rose
->state
) {
592 rose_disconnect(sk
, 0, -1, -1);
594 rose_destroy_socket(sk
);
598 rose
->neighbour
->use
--;
600 rose_disconnect(sk
, 0, -1, -1);
602 rose_destroy_socket(sk
);
609 rose_clear_queues(sk
);
610 rose_stop_idletimer(sk
);
611 rose_write_internal(sk
, ROSE_CLEAR_REQUEST
);
612 rose_start_t3timer(sk
);
613 rose
->state
= ROSE_STATE_2
;
614 sk
->sk_state
= TCP_CLOSE
;
615 sk
->sk_shutdown
|= SEND_SHUTDOWN
;
616 sk
->sk_state_change(sk
);
617 sock_set_flag(sk
, SOCK_DEAD
);
618 sock_set_flag(sk
, SOCK_DESTROY
);
632 static int rose_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
634 struct sock
*sk
= sock
->sk
;
635 struct rose_sock
*rose
= rose_sk(sk
);
636 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
637 struct net_device
*dev
;
638 ax25_address
*source
;
639 ax25_uid_assoc
*user
;
642 if (!sock_flag(sk
, SOCK_ZAPPED
))
645 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
648 if (addr
->srose_family
!= AF_ROSE
)
651 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
654 if ((unsigned int) addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
657 if ((dev
= rose_dev_get(&addr
->srose_addr
)) == NULL
)
658 return -EADDRNOTAVAIL
;
660 source
= &addr
->srose_call
;
662 user
= ax25_findbyuid(current_euid());
664 rose
->source_call
= user
->call
;
667 if (ax25_uid_policy
&& !capable(CAP_NET_BIND_SERVICE
)) {
671 rose
->source_call
= *source
;
674 rose
->source_addr
= addr
->srose_addr
;
676 rose
->source_ndigis
= addr
->srose_ndigis
;
678 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
679 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
680 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
681 rose
->source_digis
[n
] = full_addr
->srose_digis
[n
];
683 if (rose
->source_ndigis
== 1) {
684 rose
->source_digis
[0] = addr
->srose_digi
;
688 rose_insert_socket(sk
);
690 sock_reset_flag(sk
, SOCK_ZAPPED
);
695 static int rose_connect(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
, int flags
)
697 struct sock
*sk
= sock
->sk
;
698 struct rose_sock
*rose
= rose_sk(sk
);
699 struct sockaddr_rose
*addr
= (struct sockaddr_rose
*)uaddr
;
700 unsigned char cause
, diagnostic
;
701 struct net_device
*dev
;
702 ax25_uid_assoc
*user
;
705 if (addr_len
!= sizeof(struct sockaddr_rose
) && addr_len
!= sizeof(struct full_sockaddr_rose
))
708 if (addr
->srose_family
!= AF_ROSE
)
711 if (addr_len
== sizeof(struct sockaddr_rose
) && addr
->srose_ndigis
> 1)
714 if ((unsigned int) addr
->srose_ndigis
> ROSE_MAX_DIGIS
)
717 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
718 if ((rose
->source_ndigis
+ addr
->srose_ndigis
) > ROSE_MAX_DIGIS
)
723 if (sk
->sk_state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
724 /* Connect completed during a ERESTARTSYS event */
725 sock
->state
= SS_CONNECTED
;
729 if (sk
->sk_state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
730 sock
->state
= SS_UNCONNECTED
;
735 if (sk
->sk_state
== TCP_ESTABLISHED
) {
736 /* No reconnect on a seqpacket socket */
741 sk
->sk_state
= TCP_CLOSE
;
742 sock
->state
= SS_UNCONNECTED
;
744 rose
->neighbour
= rose_get_neigh(&addr
->srose_addr
, &cause
,
746 if (!rose
->neighbour
) {
751 rose
->lci
= rose_new_lci(rose
->neighbour
);
757 if (sock_flag(sk
, SOCK_ZAPPED
)) { /* Must bind first - autobinding in this may or may not work */
758 sock_reset_flag(sk
, SOCK_ZAPPED
);
760 if ((dev
= rose_dev_first()) == NULL
) {
765 user
= ax25_findbyuid(current_euid());
771 memcpy(&rose
->source_addr
, dev
->dev_addr
, ROSE_ADDR_LEN
);
772 rose
->source_call
= user
->call
;
776 rose_insert_socket(sk
); /* Finish the bind */
778 rose
->dest_addr
= addr
->srose_addr
;
779 rose
->dest_call
= addr
->srose_call
;
780 rose
->rand
= ((long)rose
& 0xFFFF) + rose
->lci
;
781 rose
->dest_ndigis
= addr
->srose_ndigis
;
783 if (addr_len
== sizeof(struct full_sockaddr_rose
)) {
784 struct full_sockaddr_rose
*full_addr
= (struct full_sockaddr_rose
*)uaddr
;
785 for (n
= 0 ; n
< addr
->srose_ndigis
; n
++)
786 rose
->dest_digis
[n
] = full_addr
->srose_digis
[n
];
788 if (rose
->dest_ndigis
== 1) {
789 rose
->dest_digis
[0] = addr
->srose_digi
;
793 /* Move to connecting socket, start sending Connect Requests */
794 sock
->state
= SS_CONNECTING
;
795 sk
->sk_state
= TCP_SYN_SENT
;
797 rose
->state
= ROSE_STATE_1
;
799 rose
->neighbour
->use
++;
801 rose_write_internal(sk
, ROSE_CALL_REQUEST
);
802 rose_start_heartbeat(sk
);
803 rose_start_t1timer(sk
);
806 if (sk
->sk_state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
)) {
812 * A Connect Ack with Choke or timeout or failed routing will go to
815 if (sk
->sk_state
== TCP_SYN_SENT
) {
819 prepare_to_wait(sk_sleep(sk
), &wait
,
821 if (sk
->sk_state
!= TCP_SYN_SENT
)
823 if (!signal_pending(current
)) {
832 finish_wait(sk_sleep(sk
), &wait
);
838 if (sk
->sk_state
!= TCP_ESTABLISHED
) {
839 sock
->state
= SS_UNCONNECTED
;
840 err
= sock_error(sk
); /* Always set at this point */
844 sock
->state
= SS_CONNECTED
;
852 static int rose_accept(struct socket
*sock
, struct socket
*newsock
, int flags
,
861 if ((sk
= sock
->sk
) == NULL
)
865 if (sk
->sk_type
!= SOCK_SEQPACKET
) {
870 if (sk
->sk_state
!= TCP_LISTEN
) {
876 * The write queue this time is holding sockets ready to use
877 * hooked into the SABM we saved
880 prepare_to_wait(sk_sleep(sk
), &wait
, TASK_INTERRUPTIBLE
);
882 skb
= skb_dequeue(&sk
->sk_receive_queue
);
886 if (flags
& O_NONBLOCK
) {
890 if (!signal_pending(current
)) {
899 finish_wait(sk_sleep(sk
), &wait
);
904 sock_graft(newsk
, newsock
);
906 /* Now attach up the new socket */
909 sk_acceptq_removed(sk
);
917 static int rose_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
920 struct full_sockaddr_rose
*srose
= (struct full_sockaddr_rose
*)uaddr
;
921 struct sock
*sk
= sock
->sk
;
922 struct rose_sock
*rose
= rose_sk(sk
);
925 memset(srose
, 0, sizeof(*srose
));
927 if (sk
->sk_state
!= TCP_ESTABLISHED
)
929 srose
->srose_family
= AF_ROSE
;
930 srose
->srose_addr
= rose
->dest_addr
;
931 srose
->srose_call
= rose
->dest_call
;
932 srose
->srose_ndigis
= rose
->dest_ndigis
;
933 for (n
= 0; n
< rose
->dest_ndigis
; n
++)
934 srose
->srose_digis
[n
] = rose
->dest_digis
[n
];
936 srose
->srose_family
= AF_ROSE
;
937 srose
->srose_addr
= rose
->source_addr
;
938 srose
->srose_call
= rose
->source_call
;
939 srose
->srose_ndigis
= rose
->source_ndigis
;
940 for (n
= 0; n
< rose
->source_ndigis
; n
++)
941 srose
->srose_digis
[n
] = rose
->source_digis
[n
];
944 return sizeof(struct full_sockaddr_rose
);
947 int rose_rx_call_request(struct sk_buff
*skb
, struct net_device
*dev
, struct rose_neigh
*neigh
, unsigned int lci
)
951 struct rose_sock
*make_rose
;
952 struct rose_facilities_struct facilities
;
955 skb
->sk
= NULL
; /* Initially we don't know who it's for */
958 * skb->data points to the rose frame start
960 memset(&facilities
, 0x00, sizeof(struct rose_facilities_struct
));
962 if (!rose_parse_facilities(skb
->data
+ ROSE_CALL_REQ_FACILITIES_OFF
,
963 skb
->len
- ROSE_CALL_REQ_FACILITIES_OFF
,
965 rose_transmit_clear_request(neigh
, lci
, ROSE_INVALID_FACILITY
, 76);
969 sk
= rose_find_listener(&facilities
.source_addr
, &facilities
.source_call
);
972 * We can't accept the Call Request.
974 if (sk
== NULL
|| sk_acceptq_is_full(sk
) ||
975 (make
= rose_make_new(sk
)) == NULL
) {
976 rose_transmit_clear_request(neigh
, lci
, ROSE_NETWORK_CONGESTION
, 120);
981 make
->sk_state
= TCP_ESTABLISHED
;
982 make_rose
= rose_sk(make
);
984 make_rose
->lci
= lci
;
985 make_rose
->dest_addr
= facilities
.dest_addr
;
986 make_rose
->dest_call
= facilities
.dest_call
;
987 make_rose
->dest_ndigis
= facilities
.dest_ndigis
;
988 for (n
= 0 ; n
< facilities
.dest_ndigis
; n
++)
989 make_rose
->dest_digis
[n
] = facilities
.dest_digis
[n
];
990 make_rose
->source_addr
= facilities
.source_addr
;
991 make_rose
->source_call
= facilities
.source_call
;
992 make_rose
->source_ndigis
= facilities
.source_ndigis
;
993 for (n
= 0 ; n
< facilities
.source_ndigis
; n
++)
994 make_rose
->source_digis
[n
] = facilities
.source_digis
[n
];
995 make_rose
->neighbour
= neigh
;
996 make_rose
->device
= dev
;
997 make_rose
->facilities
= facilities
;
999 make_rose
->neighbour
->use
++;
1001 if (rose_sk(sk
)->defer
) {
1002 make_rose
->state
= ROSE_STATE_5
;
1004 rose_write_internal(make
, ROSE_CALL_ACCEPTED
);
1005 make_rose
->state
= ROSE_STATE_3
;
1006 rose_start_idletimer(make
);
1009 make_rose
->condition
= 0x00;
1014 sk_acceptq_added(sk
);
1016 rose_insert_socket(make
);
1018 skb_queue_head(&sk
->sk_receive_queue
, skb
);
1020 rose_start_heartbeat(make
);
1022 if (!sock_flag(sk
, SOCK_DEAD
))
1023 sk
->sk_data_ready(sk
);
1028 static int rose_sendmsg(struct socket
*sock
, struct msghdr
*msg
, size_t len
)
1030 struct sock
*sk
= sock
->sk
;
1031 struct rose_sock
*rose
= rose_sk(sk
);
1032 DECLARE_SOCKADDR(struct sockaddr_rose
*, usrose
, 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
|MSG_EOR
|MSG_CMSG_COMPAT
))
1042 if (sock_flag(sk
, SOCK_ZAPPED
))
1043 return -EADDRNOTAVAIL
;
1045 if (sk
->sk_shutdown
& SEND_SHUTDOWN
) {
1046 send_sig(SIGPIPE
, current
, 0);
1050 if (rose
->neighbour
== NULL
|| 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(&rose
->dest_addr
, &srose
.srose_addr
) != 0 ||
1059 ax25cmp(&rose
->dest_call
, &srose
.srose_call
) != 0)
1061 if (srose
.srose_ndigis
!= rose
->dest_ndigis
)
1063 if (srose
.srose_ndigis
== rose
->dest_ndigis
) {
1064 for (n
= 0 ; n
< srose
.srose_ndigis
; n
++)
1065 if (ax25cmp(&rose
->dest_digis
[n
],
1066 &srose
.srose_digis
[n
]))
1069 if (srose
.srose_family
!= AF_ROSE
)
1072 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1075 srose
.srose_family
= AF_ROSE
;
1076 srose
.srose_addr
= rose
->dest_addr
;
1077 srose
.srose_call
= rose
->dest_call
;
1078 srose
.srose_ndigis
= rose
->dest_ndigis
;
1079 for (n
= 0 ; n
< rose
->dest_ndigis
; n
++)
1080 srose
.srose_digis
[n
] = rose
->dest_digis
[n
];
1083 /* Build a packet */
1084 /* Sanity check the packet size */
1088 size
= len
+ AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
;
1090 if ((skb
= sock_alloc_send_skb(sk
, size
, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1093 skb_reserve(skb
, AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ ROSE_MIN_LEN
);
1096 * Put the data on the end
1099 skb_reset_transport_header(skb
);
1102 err
= memcpy_from_msg(skb_transport_header(skb
), msg
, len
);
1109 * If the Q BIT Include socket option is in force, the first
1110 * byte of the user data is the logical value of the Q Bit.
1112 if (rose
->qbitincl
) {
1113 qbit
= skb
->data
[0];
1118 * Push down the ROSE header
1120 asmptr
= skb_push(skb
, ROSE_MIN_LEN
);
1122 /* Build a ROSE Network header */
1123 asmptr
[0] = ((rose
->lci
>> 8) & 0x0F) | ROSE_GFI
;
1124 asmptr
[1] = (rose
->lci
>> 0) & 0xFF;
1125 asmptr
[2] = ROSE_DATA
;
1128 asmptr
[0] |= ROSE_Q_BIT
;
1130 if (sk
->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 skb_copy_from_linear_data(skb
, header
, 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, &err
)) == NULL
) {
1159 skb_reserve(skbn
, frontlen
);
1161 lg
= (ROSE_PACLEN
> skb
->len
) ? skb
->len
: ROSE_PACLEN
;
1163 /* Copy the user data */
1164 skb_copy_from_linear_data(skb
, skb_put(skbn
, lg
), lg
);
1167 /* Duplicate the Header */
1168 skb_push(skbn
, ROSE_MIN_LEN
);
1169 skb_copy_to_linear_data(skbn
, header
, ROSE_MIN_LEN
);
1172 skbn
->data
[2] |= M_BIT
;
1174 skb_queue_tail(&sk
->sk_write_queue
, skbn
); /* Throw it on the queue */
1180 skb_queue_tail(&sk
->sk_write_queue
, skb
); /* Throw it on the queue */
1183 skb_queue_tail(&sk
->sk_write_queue
, skb
); /* Shove it onto the queue */
1192 static int rose_recvmsg(struct socket
*sock
, struct msghdr
*msg
, size_t size
,
1195 struct sock
*sk
= sock
->sk
;
1196 struct rose_sock
*rose
= rose_sk(sk
);
1198 unsigned char *asmptr
;
1199 struct sk_buff
*skb
;
1203 * This works for seqpacket too. The receiver has ordered the queue for
1204 * us! We do one quick check first though
1206 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1209 /* Now we can treat all alike */
1210 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1213 qbit
= (skb
->data
[0] & ROSE_Q_BIT
) == ROSE_Q_BIT
;
1215 skb_pull(skb
, ROSE_MIN_LEN
);
1217 if (rose
->qbitincl
) {
1218 asmptr
= skb_push(skb
, 1);
1222 skb_reset_transport_header(skb
);
1225 if (copied
> size
) {
1227 msg
->msg_flags
|= MSG_TRUNC
;
1230 skb_copy_datagram_msg(skb
, 0, msg
, copied
);
1232 if (msg
->msg_name
) {
1233 struct sockaddr_rose
*srose
;
1234 DECLARE_SOCKADDR(struct full_sockaddr_rose
*, full_srose
,
1237 memset(msg
->msg_name
, 0, sizeof(struct full_sockaddr_rose
));
1238 srose
= msg
->msg_name
;
1239 srose
->srose_family
= AF_ROSE
;
1240 srose
->srose_addr
= rose
->dest_addr
;
1241 srose
->srose_call
= rose
->dest_call
;
1242 srose
->srose_ndigis
= rose
->dest_ndigis
;
1243 for (n
= 0 ; n
< rose
->dest_ndigis
; n
++)
1244 full_srose
->srose_digis
[n
] = rose
->dest_digis
[n
];
1245 msg
->msg_namelen
= sizeof(struct full_sockaddr_rose
);
1248 skb_free_datagram(sk
, skb
);
1254 static int rose_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1256 struct sock
*sk
= sock
->sk
;
1257 struct rose_sock
*rose
= rose_sk(sk
);
1258 void __user
*argp
= (void __user
*)arg
;
1264 amount
= sk
->sk_sndbuf
- sk_wmem_alloc_get(sk
);
1267 return put_user(amount
, (unsigned int __user
*) argp
);
1271 struct sk_buff
*skb
;
1273 /* These two are safe on a single CPU system as only user tasks fiddle here */
1274 if ((skb
= skb_peek(&sk
->sk_receive_queue
)) != NULL
)
1276 return put_user(amount
, (unsigned int __user
*) argp
);
1281 case SIOCGIFDSTADDR
:
1282 case SIOCSIFDSTADDR
:
1283 case SIOCGIFBRDADDR
:
1284 case SIOCSIFBRDADDR
:
1285 case SIOCGIFNETMASK
:
1286 case SIOCSIFNETMASK
:
1294 if (!capable(CAP_NET_ADMIN
))
1296 return rose_rt_ioctl(cmd
, argp
);
1298 case SIOCRSGCAUSE
: {
1299 struct rose_cause_struct rose_cause
;
1300 rose_cause
.cause
= rose
->cause
;
1301 rose_cause
.diagnostic
= rose
->diagnostic
;
1302 return copy_to_user(argp
, &rose_cause
, sizeof(struct rose_cause_struct
)) ? -EFAULT
: 0;
1305 case SIOCRSSCAUSE
: {
1306 struct rose_cause_struct rose_cause
;
1307 if (copy_from_user(&rose_cause
, argp
, sizeof(struct rose_cause_struct
)))
1309 rose
->cause
= rose_cause
.cause
;
1310 rose
->diagnostic
= rose_cause
.diagnostic
;
1315 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1316 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1317 ax25_listen_release(&rose_callsign
, NULL
);
1318 if (copy_from_user(&rose_callsign
, argp
, sizeof(ax25_address
)))
1320 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1321 return ax25_listen_register(&rose_callsign
, NULL
);
1326 return copy_to_user(argp
, &rose_callsign
, sizeof(ax25_address
)) ? -EFAULT
: 0;
1329 if (rose
->state
== ROSE_STATE_5
) {
1330 rose_write_internal(sk
, ROSE_CALL_ACCEPTED
);
1331 rose_start_idletimer(sk
);
1332 rose
->condition
= 0x00;
1337 rose
->state
= ROSE_STATE_3
;
1342 return -ENOIOCTLCMD
;
1348 #ifdef CONFIG_PROC_FS
1349 static void *rose_info_start(struct seq_file
*seq
, loff_t
*pos
)
1350 __acquires(rose_list_lock
)
1352 spin_lock_bh(&rose_list_lock
);
1353 return seq_hlist_start_head(&rose_list
, *pos
);
1356 static void *rose_info_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1358 return seq_hlist_next(v
, &rose_list
, pos
);
1361 static void rose_info_stop(struct seq_file
*seq
, void *v
)
1362 __releases(rose_list_lock
)
1364 spin_unlock_bh(&rose_list_lock
);
1367 static int rose_info_show(struct seq_file
*seq
, void *v
)
1369 char buf
[11], rsbuf
[11];
1371 if (v
== SEQ_START_TOKEN
)
1373 "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");
1376 struct sock
*s
= sk_entry(v
);
1377 struct rose_sock
*rose
= rose_sk(s
);
1378 const char *devname
, *callsign
;
1379 const struct net_device
*dev
= rose
->device
;
1384 devname
= dev
->name
;
1386 seq_printf(seq
, "%-10s %-9s ",
1387 rose2asc(rsbuf
, &rose
->dest_addr
),
1388 ax2asc(buf
, &rose
->dest_call
));
1390 if (ax25cmp(&rose
->source_call
, &null_ax25_address
) == 0)
1391 callsign
= "??????-?";
1393 callsign
= ax2asc(buf
, &rose
->source_call
);
1396 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1397 rose2asc(rsbuf
, &rose
->source_addr
),
1401 (rose
->neighbour
) ? rose
->neighbour
->number
: 0,
1406 ax25_display_timer(&rose
->timer
) / HZ
,
1411 ax25_display_timer(&rose
->idletimer
) / (60 * HZ
),
1412 rose
->idle
/ (60 * HZ
),
1413 sk_wmem_alloc_get(s
),
1414 sk_rmem_alloc_get(s
),
1415 s
->sk_socket
? SOCK_INODE(s
->sk_socket
)->i_ino
: 0L);
1421 static const struct seq_operations rose_info_seqops
= {
1422 .start
= rose_info_start
,
1423 .next
= rose_info_next
,
1424 .stop
= rose_info_stop
,
1425 .show
= rose_info_show
,
1427 #endif /* CONFIG_PROC_FS */
1429 static const struct net_proto_family rose_family_ops
= {
1431 .create
= rose_create
,
1432 .owner
= THIS_MODULE
,
1435 static const struct proto_ops rose_proto_ops
= {
1437 .owner
= THIS_MODULE
,
1438 .release
= rose_release
,
1440 .connect
= rose_connect
,
1441 .socketpair
= sock_no_socketpair
,
1442 .accept
= rose_accept
,
1443 .getname
= rose_getname
,
1444 .poll
= datagram_poll
,
1445 .ioctl
= rose_ioctl
,
1446 .gettstamp
= sock_gettstamp
,
1447 .listen
= rose_listen
,
1448 .shutdown
= sock_no_shutdown
,
1449 .setsockopt
= rose_setsockopt
,
1450 .getsockopt
= rose_getsockopt
,
1451 .sendmsg
= rose_sendmsg
,
1452 .recvmsg
= rose_recvmsg
,
1453 .mmap
= sock_no_mmap
,
1454 .sendpage
= sock_no_sendpage
,
1457 static struct notifier_block rose_dev_notifier
= {
1458 .notifier_call
= rose_device_event
,
1461 static struct net_device
**dev_rose
;
1463 static struct ax25_protocol rose_pid
= {
1465 .func
= rose_route_frame
1468 static struct ax25_linkfail rose_linkfail_notifier
= {
1469 .func
= rose_link_failed
1472 static int __init
rose_proto_init(void)
1477 if (rose_ndevs
> 0x7FFFFFFF/sizeof(struct net_device
*)) {
1478 printk(KERN_ERR
"ROSE: rose_proto_init - rose_ndevs parameter too large\n");
1483 rc
= proto_register(&rose_proto
, 0);
1487 rose_callsign
= null_ax25_address
;
1489 dev_rose
= kcalloc(rose_ndevs
, sizeof(struct net_device
*),
1491 if (dev_rose
== NULL
) {
1492 printk(KERN_ERR
"ROSE: rose_proto_init - unable to allocate device structure\n");
1494 goto out_proto_unregister
;
1497 for (i
= 0; i
< rose_ndevs
; i
++) {
1498 struct net_device
*dev
;
1499 char name
[IFNAMSIZ
];
1501 sprintf(name
, "rose%d", i
);
1502 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, rose_setup
);
1504 printk(KERN_ERR
"ROSE: rose_proto_init - unable to allocate memory\n");
1508 rc
= register_netdev(dev
);
1510 printk(KERN_ERR
"ROSE: netdevice registration failed\n");
1517 sock_register(&rose_family_ops
);
1518 register_netdevice_notifier(&rose_dev_notifier
);
1520 ax25_register_pid(&rose_pid
);
1521 ax25_linkfail_register(&rose_linkfail_notifier
);
1523 #ifdef CONFIG_SYSCTL
1524 rose_register_sysctl();
1526 rose_loopback_init();
1528 rose_add_loopback_neigh();
1530 proc_create_seq("rose", 0444, init_net
.proc_net
, &rose_info_seqops
);
1531 proc_create_seq("rose_neigh", 0444, init_net
.proc_net
,
1532 &rose_neigh_seqops
);
1533 proc_create_seq("rose_nodes", 0444, init_net
.proc_net
,
1535 proc_create_seq("rose_routes", 0444, init_net
.proc_net
,
1536 &rose_route_seqops
);
1541 unregister_netdev(dev_rose
[i
]);
1542 free_netdev(dev_rose
[i
]);
1545 out_proto_unregister
:
1546 proto_unregister(&rose_proto
);
1549 module_init(rose_proto_init
);
1551 module_param(rose_ndevs
, int, 0);
1552 MODULE_PARM_DESC(rose_ndevs
, "number of ROSE devices");
1554 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1555 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1556 MODULE_LICENSE("GPL");
1557 MODULE_ALIAS_NETPROTO(PF_ROSE
);
1559 static void __exit
rose_exit(void)
1563 remove_proc_entry("rose", init_net
.proc_net
);
1564 remove_proc_entry("rose_neigh", init_net
.proc_net
);
1565 remove_proc_entry("rose_nodes", init_net
.proc_net
);
1566 remove_proc_entry("rose_routes", init_net
.proc_net
);
1567 rose_loopback_clear();
1571 ax25_protocol_release(AX25_P_ROSE
);
1572 ax25_linkfail_release(&rose_linkfail_notifier
);
1574 if (ax25cmp(&rose_callsign
, &null_ax25_address
) != 0)
1575 ax25_listen_release(&rose_callsign
, NULL
);
1577 #ifdef CONFIG_SYSCTL
1578 rose_unregister_sysctl();
1580 unregister_netdevice_notifier(&rose_dev_notifier
);
1582 sock_unregister(PF_ROSE
);
1584 for (i
= 0; i
< rose_ndevs
; i
++) {
1585 struct net_device
*dev
= dev_rose
[i
];
1588 unregister_netdev(dev
);
1594 proto_unregister(&rose_proto
);
1597 module_exit(rose_exit
);