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 * NET/ROM 001 Jonathan(G4KLX) Cloned from the AX25 code.
14 * NET/ROM 002 Darryl(G7LED) Fixes and address enhancement.
15 * Jonathan(G4KLX) Complete bind re-think.
16 * Alan(GW4PTS) Trivial tweaks into new format.
17 * NET/ROM 003 Jonathan(G4KLX) Added G8BPQ extensions.
18 * Added NET/ROM routing ioctl.
19 * Darryl(G7LED) Fix autobinding (on connect).
20 * Fixed nr_release(), set TCP_CLOSE, wakeup app
21 * context, THEN make the sock dead.
22 * Circuit ID check before allocating it on
24 * Alan(GW4PTS) sendmsg/recvmsg only. Fixed connect clear bug
25 * inherited from AX.25
26 * NET/ROM 004 Jonathan(G4KLX) Converted to module.
27 * NET/ROM 005 Jonathan(G4KLX) Linux 2.1
28 * Alan(GW4PTS) Started POSIXisms
29 * NET/ROM 006 Alan(GW4PTS) Brought in line with the ANK changes
30 * Jonathan(G4KLX) Removed hdrincl.
31 * NET/ROM 007 Jonathan(G4KLX) New timer architecture.
32 * Impmented Idle timer.
33 * Arnaldo C. Melo s/suser/capable/, micro cleanups
36 #include <linux/config.h>
37 #if defined(CONFIG_NETROM) || defined(CONFIG_NETROM_MODULE)
38 #include <linux/module.h>
39 #include <linux/errno.h>
40 #include <linux/types.h>
41 #include <linux/socket.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
45 #include <linux/timer.h>
46 #include <linux/string.h>
47 #include <linux/sockios.h>
48 #include <linux/net.h>
49 #include <linux/stat.h>
51 #include <linux/inet.h>
52 #include <linux/netdevice.h>
53 #include <linux/if_arp.h>
54 #include <linux/skbuff.h>
56 #include <asm/uaccess.h>
57 #include <asm/system.h>
58 #include <linux/fcntl.h>
59 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
61 #include <linux/interrupt.h>
62 #include <linux/notifier.h>
63 #include <net/netrom.h>
64 #include <linux/proc_fs.h>
67 #include <linux/init.h>
71 int sysctl_netrom_default_path_quality
= NR_DEFAULT_QUAL
;
72 int sysctl_netrom_obsolescence_count_initialiser
= NR_DEFAULT_OBS
;
73 int sysctl_netrom_network_ttl_initialiser
= NR_DEFAULT_TTL
;
74 int sysctl_netrom_transport_timeout
= NR_DEFAULT_T1
;
75 int sysctl_netrom_transport_maximum_tries
= NR_DEFAULT_N2
;
76 int sysctl_netrom_transport_acknowledge_delay
= NR_DEFAULT_T2
;
77 int sysctl_netrom_transport_busy_delay
= NR_DEFAULT_T4
;
78 int sysctl_netrom_transport_requested_window_size
= NR_DEFAULT_WINDOW
;
79 int sysctl_netrom_transport_no_activity_timeout
= NR_DEFAULT_IDLE
;
80 int sysctl_netrom_routing_control
= NR_DEFAULT_ROUTING
;
81 int sysctl_netrom_link_fails_count
= NR_DEFAULT_FAILS
;
83 static unsigned short circuit
= 0x101;
85 static struct sock
*volatile nr_list
= NULL
;
87 static struct proto_ops nr_proto_ops
;
89 static void nr_free_sock(struct sock
*sk
)
96 static struct sock
*nr_alloc_sock(void)
101 if ((sk
= sk_alloc(PF_NETROM
, GFP_ATOMIC
, 1)) == NULL
)
104 if ((nr
= kmalloc(sizeof(*nr
), GFP_ATOMIC
)) == NULL
) {
111 memset(nr
, 0x00, sizeof(*nr
));
113 sk
->protinfo
.nr
= nr
;
120 * Socket removal during an interrupt is now safe.
122 static void nr_remove_socket(struct sock
*sk
)
127 save_flags(flags
); cli();
129 if ((s
= nr_list
) == sk
) {
131 restore_flags(flags
);
135 while (s
!= NULL
&& s
->next
!= NULL
) {
138 restore_flags(flags
);
145 restore_flags(flags
);
149 * Kill all bound sockets on a dropped device.
151 static void nr_kill_by_device(struct net_device
*dev
)
155 for (s
= nr_list
; s
!= NULL
; s
= s
->next
) {
156 if (s
->protinfo
.nr
->device
== dev
)
157 nr_disconnect(s
, ENETUNREACH
);
162 * Handle device status changes.
164 static int nr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
166 struct net_device
*dev
= (struct net_device
*)ptr
;
168 if (event
!= NETDEV_DOWN
)
171 nr_kill_by_device(dev
);
172 nr_rt_device_down(dev
);
178 * Add a socket to the bound sockets list.
180 static void nr_insert_socket(struct sock
*sk
)
184 save_flags(flags
); cli();
189 restore_flags(flags
);
193 * Find a socket that wants to accept the Connect Request we just
196 static struct sock
*nr_find_listener(ax25_address
*addr
)
204 for (s
= nr_list
; s
!= NULL
; s
= s
->next
) {
205 if (ax25cmp(&s
->protinfo
.nr
->source_addr
, addr
) == 0 && s
->state
== TCP_LISTEN
) {
206 restore_flags(flags
);
211 restore_flags(flags
);
216 * Find a connected NET/ROM socket given my circuit IDs.
218 static struct sock
*nr_find_socket(unsigned char index
, unsigned char id
)
226 for (s
= nr_list
; s
!= NULL
; s
= s
->next
) {
227 if (s
->protinfo
.nr
->my_index
== index
&& s
->protinfo
.nr
->my_id
== id
) {
228 restore_flags(flags
);
233 restore_flags(flags
);
239 * Find a connected NET/ROM socket given their circuit IDs.
241 static struct sock
*nr_find_peer(unsigned char index
, unsigned char id
, ax25_address
*dest
)
249 for (s
= nr_list
; s
!= NULL
; s
= s
->next
) {
250 if (s
->protinfo
.nr
->your_index
== index
&& s
->protinfo
.nr
->your_id
== id
&& ax25cmp(&s
->protinfo
.nr
->dest_addr
, dest
) == 0) {
251 restore_flags(flags
);
256 restore_flags(flags
);
262 * Find next free circuit ID.
264 static unsigned short nr_find_next_circuit(void)
266 unsigned short id
= circuit
;
273 if (i
!= 0 && j
!= 0)
274 if (nr_find_socket(i
, j
) == NULL
)
286 void nr_destroy_socket(struct sock
*);
289 * Handler for deferred kills.
291 static void nr_destroy_timer(unsigned long data
)
293 nr_destroy_socket((struct sock
*)data
);
297 * This is called from user mode and the timers. Thus it protects itself against
298 * interrupt users but doesn't worry about being called during work.
299 * Once it is removed from the queue no interrupt or bottom half will
300 * touch it and we are (fairly 8-) ) safe.
302 void nr_destroy_socket(struct sock
*sk
) /* Not static as it's used by the timer */
307 save_flags(flags
); cli();
309 nr_stop_heartbeat(sk
);
313 nr_stop_idletimer(sk
);
315 nr_remove_socket(sk
);
316 nr_clear_queues(sk
); /* Flush the queues */
318 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
319 if (skb
->sk
!= sk
) { /* A pending connection */
320 skb
->sk
->dead
= 1; /* Queue the unaccepted socket for death */
321 nr_start_heartbeat(skb
->sk
);
322 skb
->sk
->protinfo
.nr
->state
= NR_STATE_0
;
328 if (atomic_read(&sk
->wmem_alloc
) != 0 || atomic_read(&sk
->rmem_alloc
) != 0) {
329 /* Defer: outstanding buffers */
330 init_timer(&sk
->timer
);
331 sk
->timer
.expires
= jiffies
+ 10 * HZ
;
332 sk
->timer
.function
= nr_destroy_timer
;
333 sk
->timer
.data
= (unsigned long)sk
;
334 add_timer(&sk
->timer
);
339 restore_flags(flags
);
343 * Handling for system calls applied via the various interfaces to a
344 * NET/ROM socket object.
347 static int nr_setsockopt(struct socket
*sock
, int level
, int optname
,
348 char *optval
, int optlen
)
350 struct sock
*sk
= sock
->sk
;
353 if (level
!= SOL_NETROM
)
356 if (optlen
< sizeof(int))
359 if (get_user(opt
, (int *)optval
))
366 sk
->protinfo
.nr
->t1
= opt
* HZ
;
372 sk
->protinfo
.nr
->t2
= opt
* HZ
;
376 if (opt
< 1 || opt
> 31)
378 sk
->protinfo
.nr
->n2
= opt
;
384 sk
->protinfo
.nr
->t4
= opt
* HZ
;
390 sk
->protinfo
.nr
->idle
= opt
* 60 * HZ
;
398 static int nr_getsockopt(struct socket
*sock
, int level
, int optname
,
399 char *optval
, int *optlen
)
401 struct sock
*sk
= sock
->sk
;
405 if (level
!= SOL_NETROM
)
408 if (get_user(len
, optlen
))
413 val
= sk
->protinfo
.nr
->t1
/ HZ
;
417 val
= sk
->protinfo
.nr
->t2
/ HZ
;
421 val
= sk
->protinfo
.nr
->n2
;
425 val
= sk
->protinfo
.nr
->t4
/ HZ
;
429 val
= sk
->protinfo
.nr
->idle
/ (60 * HZ
);
436 len
= min(len
, sizeof(int));
438 if (put_user(len
, optlen
))
441 return copy_to_user(optval
, &val
, len
) ? -EFAULT
: 0;
444 static int nr_listen(struct socket
*sock
, int backlog
)
446 struct sock
*sk
= sock
->sk
;
448 if (sk
->state
!= TCP_LISTEN
) {
449 memset(&sk
->protinfo
.nr
->user_addr
, '\0', AX25_ADDR_LEN
);
450 sk
->max_ack_backlog
= backlog
;
451 sk
->state
= TCP_LISTEN
;
458 static int nr_create(struct socket
*sock
, int protocol
)
463 if (sock
->type
!= SOCK_SEQPACKET
|| protocol
!= 0)
464 return -ESOCKTNOSUPPORT
;
466 if ((sk
= nr_alloc_sock()) == NULL
)
469 nr
= sk
->protinfo
.nr
;
471 sock_init_data(sock
, sk
);
473 sock
->ops
= &nr_proto_ops
;
474 sk
->protocol
= protocol
;
476 skb_queue_head_init(&nr
->ack_queue
);
477 skb_queue_head_init(&nr
->reseq_queue
);
478 skb_queue_head_init(&nr
->frag_queue
);
480 init_timer(&nr
->t1timer
);
481 init_timer(&nr
->t2timer
);
482 init_timer(&nr
->t4timer
);
483 init_timer(&nr
->idletimer
);
485 nr
->t1
= sysctl_netrom_transport_timeout
;
486 nr
->t2
= sysctl_netrom_transport_acknowledge_delay
;
487 nr
->n2
= sysctl_netrom_transport_maximum_tries
;
488 nr
->t4
= sysctl_netrom_transport_busy_delay
;
489 nr
->idle
= sysctl_netrom_transport_no_activity_timeout
;
490 nr
->window
= sysctl_netrom_transport_requested_window_size
;
493 nr
->state
= NR_STATE_0
;
498 static struct sock
*nr_make_new(struct sock
*osk
)
503 if (osk
->type
!= SOCK_SEQPACKET
)
506 if ((sk
= nr_alloc_sock()) == NULL
)
509 nr
= sk
->protinfo
.nr
;
511 sock_init_data(NULL
, sk
);
513 sk
->type
= osk
->type
;
514 sk
->socket
= osk
->socket
;
515 sk
->priority
= osk
->priority
;
516 sk
->protocol
= osk
->protocol
;
517 sk
->rcvbuf
= osk
->rcvbuf
;
518 sk
->sndbuf
= osk
->sndbuf
;
519 sk
->debug
= osk
->debug
;
520 sk
->state
= TCP_ESTABLISHED
;
521 sk
->sleep
= osk
->sleep
;
522 sk
->zapped
= osk
->zapped
;
524 skb_queue_head_init(&nr
->ack_queue
);
525 skb_queue_head_init(&nr
->reseq_queue
);
526 skb_queue_head_init(&nr
->frag_queue
);
528 init_timer(&nr
->t1timer
);
529 init_timer(&nr
->t2timer
);
530 init_timer(&nr
->t4timer
);
531 init_timer(&nr
->idletimer
);
533 nr
->t1
= osk
->protinfo
.nr
->t1
;
534 nr
->t2
= osk
->protinfo
.nr
->t2
;
535 nr
->n2
= osk
->protinfo
.nr
->n2
;
536 nr
->t4
= osk
->protinfo
.nr
->t4
;
537 nr
->idle
= osk
->protinfo
.nr
->idle
;
538 nr
->window
= osk
->protinfo
.nr
->window
;
540 nr
->device
= osk
->protinfo
.nr
->device
;
541 nr
->bpqext
= osk
->protinfo
.nr
->bpqext
;
546 static int nr_release(struct socket
*sock
)
548 struct sock
*sk
= sock
->sk
;
550 if (sk
== NULL
) return 0;
552 switch (sk
->protinfo
.nr
->state
) {
557 nr_disconnect(sk
, 0);
558 nr_destroy_socket(sk
);
563 sk
->protinfo
.nr
->n2count
= 0;
564 nr_write_internal(sk
, NR_DISCREQ
);
565 nr_start_t1timer(sk
);
568 nr_stop_idletimer(sk
);
569 sk
->protinfo
.nr
->state
= NR_STATE_2
;
570 sk
->state
= TCP_CLOSE
;
571 sk
->shutdown
|= SEND_SHUTDOWN
;
572 sk
->state_change(sk
);
588 static int nr_bind(struct socket
*sock
, struct sockaddr
*uaddr
, int addr_len
)
590 struct sock
*sk
= sock
->sk
;
591 struct full_sockaddr_ax25
*addr
= (struct full_sockaddr_ax25
*)uaddr
;
592 struct net_device
*dev
;
593 ax25_address
*user
, *source
;
598 if (addr_len
< sizeof(struct sockaddr_ax25
) || addr_len
> sizeof(struct
602 if (addr_len
< (addr
->fsa_ax25
.sax25_ndigis
* sizeof(ax25_address
) + sizeof(struct sockaddr_ax25
)))
605 if (addr
->fsa_ax25
.sax25_family
!= AF_NETROM
)
608 if ((dev
= nr_dev_get(&addr
->fsa_ax25
.sax25_call
)) == NULL
) {
609 SOCK_DEBUG(sk
, "NET/ROM: bind failed: invalid node callsign\n");
610 return -EADDRNOTAVAIL
;
614 * Only the super user can set an arbitrary user callsign.
616 if (addr
->fsa_ax25
.sax25_ndigis
== 1) {
617 if (!capable(CAP_NET_BIND_SERVICE
))
619 sk
->protinfo
.nr
->user_addr
= addr
->fsa_digipeater
[0];
620 sk
->protinfo
.nr
->source_addr
= addr
->fsa_ax25
.sax25_call
;
622 source
= &addr
->fsa_ax25
.sax25_call
;
624 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
) {
625 if (ax25_uid_policy
&& !capable(CAP_NET_BIND_SERVICE
))
630 sk
->protinfo
.nr
->user_addr
= *user
;
631 sk
->protinfo
.nr
->source_addr
= *source
;
634 sk
->protinfo
.nr
->device
= dev
;
635 nr_insert_socket(sk
);
638 SOCK_DEBUG(sk
, "NET/ROM: socket is bound\n");
642 static int nr_connect(struct socket
*sock
, struct sockaddr
*uaddr
,
643 int addr_len
, int flags
)
645 struct sock
*sk
= sock
->sk
;
646 struct sockaddr_ax25
*addr
= (struct sockaddr_ax25
*)uaddr
;
647 ax25_address
*user
, *source
= NULL
;
648 struct net_device
*dev
;
650 if (sk
->state
== TCP_ESTABLISHED
&& sock
->state
== SS_CONNECTING
) {
651 sock
->state
= SS_CONNECTED
;
652 return 0; /* Connect completed during a ERESTARTSYS event */
655 if (sk
->state
== TCP_CLOSE
&& sock
->state
== SS_CONNECTING
) {
656 sock
->state
= SS_UNCONNECTED
;
657 return -ECONNREFUSED
;
660 if (sk
->state
== TCP_ESTABLISHED
)
661 return -EISCONN
; /* No reconnect on a seqpacket socket */
663 sk
->state
= TCP_CLOSE
;
664 sock
->state
= SS_UNCONNECTED
;
666 if (addr_len
!= sizeof(struct sockaddr_ax25
) && addr_len
!= sizeof(struct full_sockaddr_ax25
))
669 if (addr
->sax25_family
!= AF_NETROM
)
672 if (sk
->zapped
) { /* Must bind first - autobinding in this may or may not work */
675 if ((dev
= nr_dev_first()) == NULL
)
678 source
= (ax25_address
*)dev
->dev_addr
;
680 if ((user
= ax25_findbyuid(current
->euid
)) == NULL
) {
681 if (ax25_uid_policy
&& !capable(CAP_NET_ADMIN
))
686 sk
->protinfo
.nr
->user_addr
= *user
;
687 sk
->protinfo
.nr
->source_addr
= *source
;
688 sk
->protinfo
.nr
->device
= dev
;
690 nr_insert_socket(sk
); /* Finish the bind */
693 sk
->protinfo
.nr
->dest_addr
= addr
->sax25_call
;
695 circuit
= nr_find_next_circuit();
697 sk
->protinfo
.nr
->my_index
= circuit
/ 256;
698 sk
->protinfo
.nr
->my_id
= circuit
% 256;
702 /* Move to connecting socket, start sending Connect Requests */
703 sock
->state
= SS_CONNECTING
;
704 sk
->state
= TCP_SYN_SENT
;
706 nr_establish_data_link(sk
);
708 sk
->protinfo
.nr
->state
= NR_STATE_1
;
710 nr_start_heartbeat(sk
);
713 if (sk
->state
!= TCP_ESTABLISHED
&& (flags
& O_NONBLOCK
))
716 cli(); /* To avoid races on the sleep */
719 * A Connect Ack with Choke or timeout or failed routing will go to closed.
721 while (sk
->state
== TCP_SYN_SENT
) {
722 interruptible_sleep_on(sk
->sleep
);
723 if (signal_pending(current
)) {
729 if (sk
->state
!= TCP_ESTABLISHED
) {
731 sock
->state
= SS_UNCONNECTED
;
732 return sock_error(sk
); /* Always set at this point */
735 sock
->state
= SS_CONNECTED
;
742 static int nr_accept(struct socket
*sock
, struct socket
*newsock
, int flags
)
748 if ((sk
= sock
->sk
) == NULL
)
751 if (sk
->type
!= SOCK_SEQPACKET
)
754 if (sk
->state
!= TCP_LISTEN
)
758 * The write queue this time is holding sockets ready to use
759 * hooked into the SABM we saved
763 if ((skb
= skb_dequeue(&sk
->receive_queue
)) == NULL
) {
764 if (flags
& O_NONBLOCK
) {
768 interruptible_sleep_on(sk
->sleep
);
769 if (signal_pending(current
)) {
774 } while (skb
== NULL
);
778 newsk
->socket
= newsock
;
779 newsk
->sleep
= &newsock
->wait
;
782 /* Now attach up the new socket */
790 static int nr_getname(struct socket
*sock
, struct sockaddr
*uaddr
,
791 int *uaddr_len
, int peer
)
793 struct full_sockaddr_ax25
*sax
= (struct full_sockaddr_ax25
*)uaddr
;
794 struct sock
*sk
= sock
->sk
;
797 if (sk
->state
!= TCP_ESTABLISHED
)
799 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
800 sax
->fsa_ax25
.sax25_ndigis
= 1;
801 sax
->fsa_ax25
.sax25_call
= sk
->protinfo
.nr
->user_addr
;
802 sax
->fsa_digipeater
[0] = sk
->protinfo
.nr
->dest_addr
;
803 *uaddr_len
= sizeof(struct full_sockaddr_ax25
);
805 sax
->fsa_ax25
.sax25_family
= AF_NETROM
;
806 sax
->fsa_ax25
.sax25_ndigis
= 0;
807 sax
->fsa_ax25
.sax25_call
= sk
->protinfo
.nr
->source_addr
;
808 *uaddr_len
= sizeof(struct sockaddr_ax25
);
814 int nr_rx_frame(struct sk_buff
*skb
, struct net_device
*dev
)
818 ax25_address
*src
, *dest
, *user
;
819 unsigned short circuit_index
, circuit_id
;
820 unsigned short peer_circuit_index
, peer_circuit_id
;
821 unsigned short frametype
, flags
, window
, timeout
;
823 skb
->sk
= NULL
; /* Initially we don't know who it's for */
826 * skb->data points to the netrom frame start
829 src
= (ax25_address
*)(skb
->data
+ 0);
830 dest
= (ax25_address
*)(skb
->data
+ 7);
832 circuit_index
= skb
->data
[15];
833 circuit_id
= skb
->data
[16];
834 peer_circuit_index
= skb
->data
[17];
835 peer_circuit_id
= skb
->data
[18];
836 frametype
= skb
->data
[19] & 0x0F;
837 flags
= skb
->data
[19] & 0xF0;
841 * Check for an incoming IP over NET/ROM frame.
843 if (frametype
== NR_PROTOEXT
&& circuit_index
== NR_PROTO_IP
&& circuit_id
== NR_PROTO_IP
) {
844 skb_pull(skb
, NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
);
845 skb
->h
.raw
= skb
->data
;
847 return nr_rx_ip(skb
, dev
);
852 * Find an existing socket connection, based on circuit ID, if it's
853 * a Connect Request base it on their circuit ID.
855 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
856 * circuit that no longer exists at the other end ...
861 if (circuit_index
== 0 && circuit_id
== 0) {
862 if (frametype
== NR_CONNACK
&& flags
== NR_CHOKE_FLAG
)
863 sk
= nr_find_peer(peer_circuit_index
, peer_circuit_id
, src
);
865 if (frametype
== NR_CONNREQ
)
866 sk
= nr_find_peer(circuit_index
, circuit_id
, src
);
868 sk
= nr_find_socket(circuit_index
, circuit_id
);
872 skb
->h
.raw
= skb
->data
;
874 if (frametype
== NR_CONNACK
&& skb
->len
== 22)
875 sk
->protinfo
.nr
->bpqext
= 1;
877 sk
->protinfo
.nr
->bpqext
= 0;
879 return nr_process_rx_frame(sk
, skb
);
883 * Now it should be a CONNREQ.
885 if (frametype
!= NR_CONNREQ
) {
887 * Here it would be nice to be able to send a reset but
888 * NET/ROM doesn't have one. The following hack would
889 * have been a way to extend the protocol but apparently
890 * it kills BPQ boxes... :-(
894 * Never reply to a CONNACK/CHOKE.
896 if (frametype
!= NR_CONNACK
|| flags
!= NR_CHOKE_FLAG
)
897 nr_transmit_refusal(skb
, 1);
902 sk
= nr_find_listener(dest
);
904 user
= (ax25_address
*)(skb
->data
+ 21);
906 if (sk
== NULL
|| sk
->ack_backlog
== sk
->max_ack_backlog
|| (make
= nr_make_new(sk
)) == NULL
) {
907 nr_transmit_refusal(skb
, 0);
911 window
= skb
->data
[20];
914 make
->state
= TCP_ESTABLISHED
;
916 /* Fill in his circuit details */
917 make
->protinfo
.nr
->source_addr
= *dest
;
918 make
->protinfo
.nr
->dest_addr
= *src
;
919 make
->protinfo
.nr
->user_addr
= *user
;
921 make
->protinfo
.nr
->your_index
= circuit_index
;
922 make
->protinfo
.nr
->your_id
= circuit_id
;
924 circuit
= nr_find_next_circuit();
926 make
->protinfo
.nr
->my_index
= circuit
/ 256;
927 make
->protinfo
.nr
->my_id
= circuit
% 256;
931 /* Window negotiation */
932 if (window
< make
->protinfo
.nr
->window
)
933 make
->protinfo
.nr
->window
= window
;
935 /* L4 timeout negotiation */
936 if (skb
->len
== 37) {
937 timeout
= skb
->data
[36] * 256 + skb
->data
[35];
938 if (timeout
* HZ
< make
->protinfo
.nr
->t1
)
939 make
->protinfo
.nr
->t1
= timeout
* HZ
;
940 make
->protinfo
.nr
->bpqext
= 1;
942 make
->protinfo
.nr
->bpqext
= 0;
945 nr_write_internal(make
, NR_CONNACK
);
947 make
->protinfo
.nr
->condition
= 0x00;
948 make
->protinfo
.nr
->vs
= 0;
949 make
->protinfo
.nr
->va
= 0;
950 make
->protinfo
.nr
->vr
= 0;
951 make
->protinfo
.nr
->vl
= 0;
952 make
->protinfo
.nr
->state
= NR_STATE_3
;
956 nr_insert_socket(make
);
958 skb_queue_head(&sk
->receive_queue
, skb
);
960 nr_start_heartbeat(make
);
961 nr_start_idletimer(make
);
964 sk
->data_ready(sk
, skb
->len
);
969 static int nr_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
, struct scm_cookie
*scm
)
971 struct sock
*sk
= sock
->sk
;
972 struct sockaddr_ax25
*usax
= (struct sockaddr_ax25
*)msg
->msg_name
;
974 struct sockaddr_ax25 sax
;
976 unsigned char *asmptr
;
979 if (msg
->msg_flags
& ~(MSG_DONTWAIT
|MSG_EOR
))
983 return -EADDRNOTAVAIL
;
985 if (sk
->shutdown
& SEND_SHUTDOWN
) {
986 send_sig(SIGPIPE
, current
, 0);
990 if (sk
->protinfo
.nr
->device
== NULL
)
994 if (msg
->msg_namelen
< sizeof(sax
))
997 if (ax25cmp(&sk
->protinfo
.nr
->dest_addr
, &sax
.sax25_call
) != 0)
999 if (sax
.sax25_family
!= AF_NETROM
)
1002 if (sk
->state
!= TCP_ESTABLISHED
)
1004 sax
.sax25_family
= AF_NETROM
;
1005 sax
.sax25_call
= sk
->protinfo
.nr
->dest_addr
;
1008 SOCK_DEBUG(sk
, "NET/ROM: sendto: Addresses built.\n");
1010 /* Build a packet */
1011 SOCK_DEBUG(sk
, "NET/ROM: sendto: building packet.\n");
1012 size
= len
+ AX25_BPQ_HEADER_LEN
+ AX25_MAX_HEADER_LEN
+ NR_NETWORK_LEN
+ NR_TRANSPORT_LEN
;
1014 if ((skb
= sock_alloc_send_skb(sk
, size
, 0, msg
->msg_flags
& MSG_DONTWAIT
, &err
)) == NULL
)
1017 skb_reserve(skb
, size
- len
);
1020 * Push down the NET/ROM header
1023 asmptr
= skb_push(skb
, NR_TRANSPORT_LEN
);
1024 SOCK_DEBUG(sk
, "Building NET/ROM Header.\n");
1026 /* Build a NET/ROM Transport header */
1028 *asmptr
++ = sk
->protinfo
.nr
->your_index
;
1029 *asmptr
++ = sk
->protinfo
.nr
->your_id
;
1030 *asmptr
++ = 0; /* To be filled in later */
1031 *asmptr
++ = 0; /* Ditto */
1032 *asmptr
++ = NR_INFO
;
1033 SOCK_DEBUG(sk
, "Built header.\n");
1036 * Put the data on the end
1039 skb
->h
.raw
= skb_put(skb
, len
);
1041 asmptr
= skb
->h
.raw
;
1042 SOCK_DEBUG(sk
, "NET/ROM: Appending user data\n");
1044 /* User data follows immediately after the NET/ROM transport header */
1045 memcpy_fromiovec(asmptr
, msg
->msg_iov
, len
);
1046 SOCK_DEBUG(sk
, "NET/ROM: Transmitting buffer\n");
1048 if (sk
->state
!= TCP_ESTABLISHED
) {
1053 nr_output(sk
, skb
); /* Shove it onto the queue */
1058 static int nr_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int size
,
1059 int flags
, struct scm_cookie
*scm
)
1061 struct sock
*sk
= sock
->sk
;
1062 struct sockaddr_ax25
*sax
= (struct sockaddr_ax25
*)msg
->msg_name
;
1064 struct sk_buff
*skb
;
1068 * This works for seqpacket too. The receiver has ordered the queue for
1069 * us! We do one quick check first though
1072 if (sk
->state
!= TCP_ESTABLISHED
)
1075 /* Now we can treat all alike */
1076 if ((skb
= skb_recv_datagram(sk
, flags
& ~MSG_DONTWAIT
, flags
& MSG_DONTWAIT
, &er
)) == NULL
)
1079 skb
->h
.raw
= skb
->data
;
1082 if (copied
> size
) {
1084 msg
->msg_flags
|= MSG_TRUNC
;
1087 skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
1090 sax
->sax25_family
= AF_NETROM
;
1091 memcpy(sax
->sax25_call
.ax25_call
, skb
->data
+ 7, AX25_ADDR_LEN
);
1094 msg
->msg_namelen
= sizeof(*sax
);
1096 skb_free_datagram(sk
, skb
);
1102 static int nr_ioctl(struct socket
*sock
, unsigned int cmd
, unsigned long arg
)
1104 struct sock
*sk
= sock
->sk
;
1109 amount
= sk
->sndbuf
- atomic_read(&sk
->wmem_alloc
);
1112 return put_user(amount
, (int *)arg
);
1116 struct sk_buff
*skb
;
1118 /* These two are safe on a single CPU system as only user tasks fiddle here */
1119 if ((skb
= skb_peek(&sk
->receive_queue
)) != NULL
)
1121 return put_user(amount
, (int *)arg
);
1126 if (sk
->stamp
.tv_sec
== 0)
1128 return copy_to_user((void *)arg
, &sk
->stamp
, sizeof(struct timeval
)) ? -EFAULT
: 0;
1134 case SIOCGIFDSTADDR
:
1135 case SIOCSIFDSTADDR
:
1136 case SIOCGIFBRDADDR
:
1137 case SIOCSIFBRDADDR
:
1138 case SIOCGIFNETMASK
:
1139 case SIOCSIFNETMASK
:
1147 if (!capable(CAP_NET_ADMIN
)) return -EPERM
;
1148 return nr_rt_ioctl(cmd
, (void *)arg
);
1151 return dev_ioctl(cmd
, (void *)arg
);
1158 static int nr_get_info(char *buffer
, char **start
, off_t offset
, int length
)
1161 struct net_device
*dev
;
1162 const char *devname
;
1169 len
+= sprintf(buffer
, "user_addr dest_node src_node dev my your st vs vr va t1 t2 t4 idle n2 wnd Snd-Q Rcv-Q inode\n");
1171 for (s
= nr_list
; s
!= NULL
; s
= s
->next
) {
1172 if ((dev
= s
->protinfo
.nr
->device
) == NULL
)
1175 devname
= dev
->name
;
1177 len
+= sprintf(buffer
+ len
, "%-9s ",
1178 ax2asc(&s
->protinfo
.nr
->user_addr
));
1179 len
+= sprintf(buffer
+ len
, "%-9s ",
1180 ax2asc(&s
->protinfo
.nr
->dest_addr
));
1181 len
+= sprintf(buffer
+ len
, "%-9s %-3s %02X/%02X %02X/%02X %2d %3d %3d %3d %3lu/%03lu %2lu/%02lu %3lu/%03lu %3lu/%03lu %2d/%02d %3d %5d %5d %ld\n",
1182 ax2asc(&s
->protinfo
.nr
->source_addr
),
1184 s
->protinfo
.nr
->my_index
,
1185 s
->protinfo
.nr
->my_id
,
1186 s
->protinfo
.nr
->your_index
,
1187 s
->protinfo
.nr
->your_id
,
1188 s
->protinfo
.nr
->state
,
1192 ax25_display_timer(&s
->protinfo
.nr
->t1timer
) / HZ
,
1193 s
->protinfo
.nr
->t1
/ HZ
,
1194 ax25_display_timer(&s
->protinfo
.nr
->t2timer
) / HZ
,
1195 s
->protinfo
.nr
->t2
/ HZ
,
1196 ax25_display_timer(&s
->protinfo
.nr
->t4timer
) / HZ
,
1197 s
->protinfo
.nr
->t4
/ HZ
,
1198 ax25_display_timer(&s
->protinfo
.nr
->idletimer
) / (60 * HZ
),
1199 s
->protinfo
.nr
->idle
/ (60 * HZ
),
1200 s
->protinfo
.nr
->n2count
,
1202 s
->protinfo
.nr
->window
,
1203 atomic_read(&s
->wmem_alloc
),
1204 atomic_read(&s
->rmem_alloc
),
1205 s
->socket
!= NULL
? s
->socket
->inode
->i_ino
: 0L);
1214 if (pos
> offset
+ length
)
1220 *start
= buffer
+ (offset
- begin
);
1221 len
-= (offset
- begin
);
1223 if (len
> length
) len
= length
;
1228 static struct net_proto_family nr_family_ops
=
1234 static struct proto_ops
SOCKOPS_WRAPPED(nr_proto_ops
) = {
1237 release
: nr_release
,
1239 connect
: nr_connect
,
1240 socketpair
: sock_no_socketpair
,
1242 getname
: nr_getname
,
1243 poll
: datagram_poll
,
1246 shutdown
: sock_no_shutdown
,
1247 setsockopt
: nr_setsockopt
,
1248 getsockopt
: nr_getsockopt
,
1249 sendmsg
: nr_sendmsg
,
1250 recvmsg
: nr_recvmsg
,
1254 #include <linux/smp_lock.h>
1255 SOCKOPS_WRAP(nr_proto
, PF_NETROM
);
1257 static struct notifier_block nr_dev_notifier
= {
1262 static struct net_device
*dev_nr
;
1264 static int __init
nr_proto_init(void)
1268 if ((dev_nr
= kmalloc(nr_ndevs
* sizeof(struct net_device
), GFP_KERNEL
)) == NULL
) {
1269 printk(KERN_ERR
"NET/ROM: nr_proto_init - unable to allocate device structure\n");
1273 memset(dev_nr
, 0x00, nr_ndevs
* sizeof(struct net_device
));
1275 for (i
= 0; i
< nr_ndevs
; i
++) {
1276 sprintf(dev_nr
[i
].name
, "nr%d", i
);
1277 dev_nr
[i
].init
= nr_init
;
1278 register_netdev(&dev_nr
[i
]);
1281 sock_register(&nr_family_ops
);
1282 register_netdevice_notifier(&nr_dev_notifier
);
1283 printk(KERN_INFO
"G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.1\n");
1285 ax25_protocol_register(AX25_P_NETROM
, nr_route_frame
);
1286 ax25_linkfail_register(nr_link_failed
);
1288 #ifdef CONFIG_SYSCTL
1289 nr_register_sysctl();
1294 #ifdef CONFIG_PROC_FS
1295 proc_net_create("nr", 0, nr_get_info
);
1296 proc_net_create("nr_neigh", 0, nr_neigh_get_info
);
1297 proc_net_create("nr_nodes", 0, nr_nodes_get_info
);
1302 module_init(nr_proto_init
);
1308 MODULE_PARM(nr_ndevs
, "i");
1309 MODULE_PARM_DESC(nr_ndevs
, "number of NET/ROM devices");
1311 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1312 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1314 static void nr_exit(void)
1318 #ifdef CONFIG_PROC_FS
1319 proc_net_remove("nr");
1320 proc_net_remove("nr_neigh");
1321 proc_net_remove("nr_nodes");
1323 nr_loopback_clear();
1327 ax25_protocol_release(AX25_P_NETROM
);
1328 ax25_linkfail_release(nr_link_failed
);
1330 unregister_netdevice_notifier(&nr_dev_notifier
);
1332 #ifdef CONFIG_SYSCTL
1333 nr_unregister_sysctl();
1335 sock_unregister(PF_NETROM
);
1337 for (i
= 0; i
< nr_ndevs
; i
++) {
1338 if (dev_nr
[i
].priv
!= NULL
) {
1339 kfree(dev_nr
[i
].priv
);
1340 dev_nr
[i
].priv
= NULL
;
1341 unregister_netdev(&dev_nr
[i
]);
1343 kfree(dev_nr
[i
].name
);
1348 module_exit(nr_exit
);