[PATCH] aic7xxx_osm build fix
[cris-mirror.git] / net / netrom / af_netrom.c
blob31ed4a9a1d066c0cdef1c3e5b6371fa3678037d4
1 /*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * Copyright Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright Darryl Miles G7LED (dlm@g7led.demon.co.uk)
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/moduleparam.h>
14 #include <linux/errno.h>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/in.h>
18 #include <linux/kernel.h>
19 #include <linux/sched.h>
20 #include <linux/timer.h>
21 #include <linux/string.h>
22 #include <linux/sockios.h>
23 #include <linux/net.h>
24 #include <linux/stat.h>
25 #include <net/ax25.h>
26 #include <linux/inet.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_arp.h>
29 #include <linux/skbuff.h>
30 #include <net/sock.h>
31 #include <asm/uaccess.h>
32 #include <asm/system.h>
33 #include <linux/fcntl.h>
34 #include <linux/termios.h> /* For TIOCINQ/OUTQ */
35 #include <linux/mm.h>
36 #include <linux/interrupt.h>
37 #include <linux/notifier.h>
38 #include <net/netrom.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <net/ip.h>
42 #include <net/tcp.h>
43 #include <net/arp.h>
44 #include <linux/init.h>
46 static int nr_ndevs = 4;
48 int sysctl_netrom_default_path_quality = NR_DEFAULT_QUAL;
49 int sysctl_netrom_obsolescence_count_initialiser = NR_DEFAULT_OBS;
50 int sysctl_netrom_network_ttl_initialiser = NR_DEFAULT_TTL;
51 int sysctl_netrom_transport_timeout = NR_DEFAULT_T1;
52 int sysctl_netrom_transport_maximum_tries = NR_DEFAULT_N2;
53 int sysctl_netrom_transport_acknowledge_delay = NR_DEFAULT_T2;
54 int sysctl_netrom_transport_busy_delay = NR_DEFAULT_T4;
55 int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
56 int sysctl_netrom_transport_no_activity_timeout = NR_DEFAULT_IDLE;
57 int sysctl_netrom_routing_control = NR_DEFAULT_ROUTING;
58 int sysctl_netrom_link_fails_count = NR_DEFAULT_FAILS;
60 static unsigned short circuit = 0x101;
62 static HLIST_HEAD(nr_list);
63 static DEFINE_SPINLOCK(nr_list_lock);
65 static struct proto_ops nr_proto_ops;
68 * Socket removal during an interrupt is now safe.
70 static void nr_remove_socket(struct sock *sk)
72 spin_lock_bh(&nr_list_lock);
73 sk_del_node_init(sk);
74 spin_unlock_bh(&nr_list_lock);
78 * Kill all bound sockets on a dropped device.
80 static void nr_kill_by_device(struct net_device *dev)
82 struct sock *s;
83 struct hlist_node *node;
85 spin_lock_bh(&nr_list_lock);
86 sk_for_each(s, node, &nr_list)
87 if (nr_sk(s)->device == dev)
88 nr_disconnect(s, ENETUNREACH);
89 spin_unlock_bh(&nr_list_lock);
93 * Handle device status changes.
95 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
97 struct net_device *dev = (struct net_device *)ptr;
99 if (event != NETDEV_DOWN)
100 return NOTIFY_DONE;
102 nr_kill_by_device(dev);
103 nr_rt_device_down(dev);
105 return NOTIFY_DONE;
109 * Add a socket to the bound sockets list.
111 static void nr_insert_socket(struct sock *sk)
113 spin_lock_bh(&nr_list_lock);
114 sk_add_node(sk, &nr_list);
115 spin_unlock_bh(&nr_list_lock);
119 * Find a socket that wants to accept the Connect Request we just
120 * received.
122 static struct sock *nr_find_listener(ax25_address *addr)
124 struct sock *s;
125 struct hlist_node *node;
127 spin_lock_bh(&nr_list_lock);
128 sk_for_each(s, node, &nr_list)
129 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
130 s->sk_state == TCP_LISTEN) {
131 bh_lock_sock(s);
132 goto found;
134 s = NULL;
135 found:
136 spin_unlock_bh(&nr_list_lock);
137 return s;
141 * Find a connected NET/ROM socket given my circuit IDs.
143 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
145 struct sock *s;
146 struct hlist_node *node;
148 spin_lock_bh(&nr_list_lock);
149 sk_for_each(s, node, &nr_list) {
150 struct nr_sock *nr = nr_sk(s);
152 if (nr->my_index == index && nr->my_id == id) {
153 bh_lock_sock(s);
154 goto found;
157 s = NULL;
158 found:
159 spin_unlock_bh(&nr_list_lock);
160 return s;
164 * Find a connected NET/ROM socket given their circuit IDs.
166 static struct sock *nr_find_peer(unsigned char index, unsigned char id,
167 ax25_address *dest)
169 struct sock *s;
170 struct hlist_node *node;
172 spin_lock_bh(&nr_list_lock);
173 sk_for_each(s, node, &nr_list) {
174 struct nr_sock *nr = nr_sk(s);
176 if (nr->your_index == index && nr->your_id == id &&
177 !ax25cmp(&nr->dest_addr, dest)) {
178 bh_lock_sock(s);
179 goto found;
182 s = NULL;
183 found:
184 spin_unlock_bh(&nr_list_lock);
185 return s;
189 * Find next free circuit ID.
191 static unsigned short nr_find_next_circuit(void)
193 unsigned short id = circuit;
194 unsigned char i, j;
195 struct sock *sk;
197 for (;;) {
198 i = id / 256;
199 j = id % 256;
201 if (i != 0 && j != 0) {
202 if ((sk=nr_find_socket(i, j)) == NULL)
203 break;
204 bh_unlock_sock(sk);
207 id++;
210 return id;
214 * Deferred destroy.
216 void nr_destroy_socket(struct sock *);
219 * Handler for deferred kills.
221 static void nr_destroy_timer(unsigned long data)
223 struct sock *sk=(struct sock *)data;
224 bh_lock_sock(sk);
225 sock_hold(sk);
226 nr_destroy_socket(sk);
227 bh_unlock_sock(sk);
228 sock_put(sk);
232 * This is called from user mode and the timers. Thus it protects itself
233 * against interrupt users but doesn't worry about being called during
234 * work. Once it is removed from the queue no interrupt or bottom half
235 * will touch it and we are (fairly 8-) ) safe.
237 void nr_destroy_socket(struct sock *sk)
239 struct sk_buff *skb;
241 nr_remove_socket(sk);
243 nr_stop_heartbeat(sk);
244 nr_stop_t1timer(sk);
245 nr_stop_t2timer(sk);
246 nr_stop_t4timer(sk);
247 nr_stop_idletimer(sk);
249 nr_clear_queues(sk); /* Flush the queues */
251 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
252 if (skb->sk != sk) { /* A pending connection */
253 /* Queue the unaccepted socket for death */
254 sock_set_flag(skb->sk, SOCK_DEAD);
255 nr_start_heartbeat(skb->sk);
256 nr_sk(skb->sk)->state = NR_STATE_0;
259 kfree_skb(skb);
262 if (atomic_read(&sk->sk_wmem_alloc) ||
263 atomic_read(&sk->sk_rmem_alloc)) {
264 /* Defer: outstanding buffers */
265 sk->sk_timer.function = nr_destroy_timer;
266 sk->sk_timer.expires = jiffies + 2 * HZ;
267 add_timer(&sk->sk_timer);
268 } else
269 sock_put(sk);
273 * Handling for system calls applied via the various interfaces to a
274 * NET/ROM socket object.
277 static int nr_setsockopt(struct socket *sock, int level, int optname,
278 char __user *optval, int optlen)
280 struct sock *sk = sock->sk;
281 struct nr_sock *nr = nr_sk(sk);
282 int opt;
284 if (level != SOL_NETROM)
285 return -ENOPROTOOPT;
287 if (optlen < sizeof(int))
288 return -EINVAL;
290 if (get_user(opt, (int __user *)optval))
291 return -EFAULT;
293 switch (optname) {
294 case NETROM_T1:
295 if (opt < 1)
296 return -EINVAL;
297 nr->t1 = opt * HZ;
298 return 0;
300 case NETROM_T2:
301 if (opt < 1)
302 return -EINVAL;
303 nr->t2 = opt * HZ;
304 return 0;
306 case NETROM_N2:
307 if (opt < 1 || opt > 31)
308 return -EINVAL;
309 nr->n2 = opt;
310 return 0;
312 case NETROM_T4:
313 if (opt < 1)
314 return -EINVAL;
315 nr->t4 = opt * HZ;
316 return 0;
318 case NETROM_IDLE:
319 if (opt < 0)
320 return -EINVAL;
321 nr->idle = opt * 60 * HZ;
322 return 0;
324 default:
325 return -ENOPROTOOPT;
329 static int nr_getsockopt(struct socket *sock, int level, int optname,
330 char __user *optval, int __user *optlen)
332 struct sock *sk = sock->sk;
333 struct nr_sock *nr = nr_sk(sk);
334 int val = 0;
335 int len;
337 if (level != SOL_NETROM)
338 return -ENOPROTOOPT;
340 if (get_user(len, optlen))
341 return -EFAULT;
343 if (len < 0)
344 return -EINVAL;
346 switch (optname) {
347 case NETROM_T1:
348 val = nr->t1 / HZ;
349 break;
351 case NETROM_T2:
352 val = nr->t2 / HZ;
353 break;
355 case NETROM_N2:
356 val = nr->n2;
357 break;
359 case NETROM_T4:
360 val = nr->t4 / HZ;
361 break;
363 case NETROM_IDLE:
364 val = nr->idle / (60 * HZ);
365 break;
367 default:
368 return -ENOPROTOOPT;
371 len = min_t(unsigned int, len, sizeof(int));
373 if (put_user(len, optlen))
374 return -EFAULT;
376 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
379 static int nr_listen(struct socket *sock, int backlog)
381 struct sock *sk = sock->sk;
383 lock_sock(sk);
384 if (sk->sk_state != TCP_LISTEN) {
385 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
386 sk->sk_max_ack_backlog = backlog;
387 sk->sk_state = TCP_LISTEN;
388 release_sock(sk);
389 return 0;
391 release_sock(sk);
393 return -EOPNOTSUPP;
396 static struct proto nr_proto = {
397 .name = "NETROM",
398 .owner = THIS_MODULE,
399 .obj_size = sizeof(struct nr_sock),
402 static int nr_create(struct socket *sock, int protocol)
404 struct sock *sk;
405 struct nr_sock *nr;
407 if (sock->type != SOCK_SEQPACKET || protocol != 0)
408 return -ESOCKTNOSUPPORT;
410 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, &nr_proto, 1)) == NULL)
411 return -ENOMEM;
413 nr = nr_sk(sk);
415 sock_init_data(sock, sk);
417 sock->ops = &nr_proto_ops;
418 sk->sk_protocol = protocol;
420 skb_queue_head_init(&nr->ack_queue);
421 skb_queue_head_init(&nr->reseq_queue);
422 skb_queue_head_init(&nr->frag_queue);
424 nr_init_timers(sk);
426 nr->t1 = sysctl_netrom_transport_timeout;
427 nr->t2 = sysctl_netrom_transport_acknowledge_delay;
428 nr->n2 = sysctl_netrom_transport_maximum_tries;
429 nr->t4 = sysctl_netrom_transport_busy_delay;
430 nr->idle = sysctl_netrom_transport_no_activity_timeout;
431 nr->window = sysctl_netrom_transport_requested_window_size;
433 nr->bpqext = 1;
434 nr->state = NR_STATE_0;
436 return 0;
439 static struct sock *nr_make_new(struct sock *osk)
441 struct sock *sk;
442 struct nr_sock *nr, *onr;
444 if (osk->sk_type != SOCK_SEQPACKET)
445 return NULL;
447 if ((sk = sk_alloc(PF_NETROM, GFP_ATOMIC, osk->sk_prot, 1)) == NULL)
448 return NULL;
450 nr = nr_sk(sk);
452 sock_init_data(NULL, sk);
454 sk->sk_type = osk->sk_type;
455 sk->sk_socket = osk->sk_socket;
456 sk->sk_priority = osk->sk_priority;
457 sk->sk_protocol = osk->sk_protocol;
458 sk->sk_rcvbuf = osk->sk_rcvbuf;
459 sk->sk_sndbuf = osk->sk_sndbuf;
460 sk->sk_state = TCP_ESTABLISHED;
461 sk->sk_sleep = osk->sk_sleep;
463 if (sock_flag(osk, SOCK_ZAPPED))
464 sock_set_flag(sk, SOCK_ZAPPED);
466 if (sock_flag(osk, SOCK_DBG))
467 sock_set_flag(sk, SOCK_DBG);
469 skb_queue_head_init(&nr->ack_queue);
470 skb_queue_head_init(&nr->reseq_queue);
471 skb_queue_head_init(&nr->frag_queue);
473 nr_init_timers(sk);
475 onr = nr_sk(osk);
477 nr->t1 = onr->t1;
478 nr->t2 = onr->t2;
479 nr->n2 = onr->n2;
480 nr->t4 = onr->t4;
481 nr->idle = onr->idle;
482 nr->window = onr->window;
484 nr->device = onr->device;
485 nr->bpqext = onr->bpqext;
487 return sk;
490 static int nr_release(struct socket *sock)
492 struct sock *sk = sock->sk;
493 struct nr_sock *nr;
495 if (sk == NULL) return 0;
497 sock_hold(sk);
498 lock_sock(sk);
499 nr = nr_sk(sk);
501 switch (nr->state) {
502 case NR_STATE_0:
503 case NR_STATE_1:
504 case NR_STATE_2:
505 nr_disconnect(sk, 0);
506 nr_destroy_socket(sk);
507 break;
509 case NR_STATE_3:
510 nr_clear_queues(sk);
511 nr->n2count = 0;
512 nr_write_internal(sk, NR_DISCREQ);
513 nr_start_t1timer(sk);
514 nr_stop_t2timer(sk);
515 nr_stop_t4timer(sk);
516 nr_stop_idletimer(sk);
517 nr->state = NR_STATE_2;
518 sk->sk_state = TCP_CLOSE;
519 sk->sk_shutdown |= SEND_SHUTDOWN;
520 sk->sk_state_change(sk);
521 sock_orphan(sk);
522 sock_set_flag(sk, SOCK_DESTROY);
523 sk->sk_socket = NULL;
524 break;
526 default:
527 sk->sk_socket = NULL;
528 break;
531 sock->sk = NULL;
532 release_sock(sk);
533 sock_put(sk);
535 return 0;
538 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
540 struct sock *sk = sock->sk;
541 struct nr_sock *nr = nr_sk(sk);
542 struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
543 struct net_device *dev;
544 ax25_address *user, *source;
546 lock_sock(sk);
547 if (!sock_flag(sk, SOCK_ZAPPED)) {
548 release_sock(sk);
549 return -EINVAL;
551 if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
552 release_sock(sk);
553 return -EINVAL;
555 if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
556 release_sock(sk);
557 return -EINVAL;
559 if (addr->fsa_ax25.sax25_family != AF_NETROM) {
560 release_sock(sk);
561 return -EINVAL;
563 if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
564 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
565 release_sock(sk);
566 return -EADDRNOTAVAIL;
570 * Only the super user can set an arbitrary user callsign.
572 if (addr->fsa_ax25.sax25_ndigis == 1) {
573 if (!capable(CAP_NET_BIND_SERVICE)) {
574 dev_put(dev);
575 release_sock(sk);
576 return -EACCES;
578 nr->user_addr = addr->fsa_digipeater[0];
579 nr->source_addr = addr->fsa_ax25.sax25_call;
580 } else {
581 source = &addr->fsa_ax25.sax25_call;
583 if ((user = ax25_findbyuid(current->euid)) == NULL) {
584 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
585 release_sock(sk);
586 dev_put(dev);
587 return -EPERM;
589 user = source;
592 nr->user_addr = *user;
593 nr->source_addr = *source;
596 nr->device = dev;
597 nr_insert_socket(sk);
599 sock_reset_flag(sk, SOCK_ZAPPED);
600 dev_put(dev);
601 release_sock(sk);
602 SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
603 return 0;
606 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
607 int addr_len, int flags)
609 struct sock *sk = sock->sk;
610 struct nr_sock *nr = nr_sk(sk);
611 struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
612 ax25_address *user, *source = NULL;
613 struct net_device *dev;
615 lock_sock(sk);
616 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
617 sock->state = SS_CONNECTED;
618 release_sock(sk);
619 return 0; /* Connect completed during a ERESTARTSYS event */
622 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
623 sock->state = SS_UNCONNECTED;
624 release_sock(sk);
625 return -ECONNREFUSED;
628 if (sk->sk_state == TCP_ESTABLISHED) {
629 release_sock(sk);
630 return -EISCONN; /* No reconnect on a seqpacket socket */
633 sk->sk_state = TCP_CLOSE;
634 sock->state = SS_UNCONNECTED;
636 if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
637 release_sock(sk);
638 return -EINVAL;
640 if (addr->sax25_family != AF_NETROM) {
641 release_sock(sk);
642 return -EINVAL;
644 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
645 sock_reset_flag(sk, SOCK_ZAPPED);
647 if ((dev = nr_dev_first()) == NULL) {
648 release_sock(sk);
649 return -ENETUNREACH;
651 source = (ax25_address *)dev->dev_addr;
653 if ((user = ax25_findbyuid(current->euid)) == NULL) {
654 if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
655 dev_put(dev);
656 release_sock(sk);
657 return -EPERM;
659 user = source;
662 nr->user_addr = *user;
663 nr->source_addr = *source;
664 nr->device = dev;
666 dev_put(dev);
667 nr_insert_socket(sk); /* Finish the bind */
670 nr->dest_addr = addr->sax25_call;
672 release_sock(sk);
673 circuit = nr_find_next_circuit();
674 lock_sock(sk);
676 nr->my_index = circuit / 256;
677 nr->my_id = circuit % 256;
679 circuit++;
681 /* Move to connecting socket, start sending Connect Requests */
682 sock->state = SS_CONNECTING;
683 sk->sk_state = TCP_SYN_SENT;
685 nr_establish_data_link(sk);
687 nr->state = NR_STATE_1;
689 nr_start_heartbeat(sk);
691 /* Now the loop */
692 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
693 release_sock(sk);
694 return -EINPROGRESS;
698 * A Connect Ack with Choke or timeout or failed routing will go to
699 * closed.
701 if (sk->sk_state == TCP_SYN_SENT) {
702 struct task_struct *tsk = current;
703 DECLARE_WAITQUEUE(wait, tsk);
705 add_wait_queue(sk->sk_sleep, &wait);
706 for (;;) {
707 set_current_state(TASK_INTERRUPTIBLE);
708 if (sk->sk_state != TCP_SYN_SENT)
709 break;
710 release_sock(sk);
711 if (!signal_pending(tsk)) {
712 schedule();
713 lock_sock(sk);
714 continue;
716 current->state = TASK_RUNNING;
717 remove_wait_queue(sk->sk_sleep, &wait);
718 return -ERESTARTSYS;
720 current->state = TASK_RUNNING;
721 remove_wait_queue(sk->sk_sleep, &wait);
724 if (sk->sk_state != TCP_ESTABLISHED) {
725 sock->state = SS_UNCONNECTED;
726 release_sock(sk);
727 return sock_error(sk); /* Always set at this point */
730 sock->state = SS_CONNECTED;
731 release_sock(sk);
733 return 0;
736 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
738 struct task_struct *tsk = current;
739 DECLARE_WAITQUEUE(wait, tsk);
740 struct sk_buff *skb;
741 struct sock *newsk;
742 struct sock *sk;
743 int err = 0;
745 if ((sk = sock->sk) == NULL)
746 return -EINVAL;
748 lock_sock(sk);
749 if (sk->sk_type != SOCK_SEQPACKET) {
750 err = -EOPNOTSUPP;
751 goto out;
754 if (sk->sk_state != TCP_LISTEN) {
755 err = -EINVAL;
756 goto out;
760 * The write queue this time is holding sockets ready to use
761 * hooked into the SABM we saved
763 add_wait_queue(sk->sk_sleep, &wait);
764 for (;;) {
765 skb = skb_dequeue(&sk->sk_receive_queue);
766 if (skb)
767 break;
769 current->state = TASK_INTERRUPTIBLE;
770 release_sock(sk);
771 if (flags & O_NONBLOCK) {
772 current->state = TASK_RUNNING;
773 remove_wait_queue(sk->sk_sleep, &wait);
774 return -EWOULDBLOCK;
776 if (!signal_pending(tsk)) {
777 schedule();
778 lock_sock(sk);
779 continue;
781 current->state = TASK_RUNNING;
782 remove_wait_queue(sk->sk_sleep, &wait);
783 return -ERESTARTSYS;
785 current->state = TASK_RUNNING;
786 remove_wait_queue(sk->sk_sleep, &wait);
788 newsk = skb->sk;
789 newsk->sk_socket = newsock;
790 newsk->sk_sleep = &newsock->wait;
792 /* Now attach up the new socket */
793 kfree_skb(skb);
794 sk->sk_ack_backlog--;
795 newsock->sk = newsk;
797 out:
798 release_sock(sk);
799 return err;
802 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
803 int *uaddr_len, int peer)
805 struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
806 struct sock *sk = sock->sk;
807 struct nr_sock *nr = nr_sk(sk);
809 lock_sock(sk);
810 if (peer != 0) {
811 if (sk->sk_state != TCP_ESTABLISHED) {
812 release_sock(sk);
813 return -ENOTCONN;
815 sax->fsa_ax25.sax25_family = AF_NETROM;
816 sax->fsa_ax25.sax25_ndigis = 1;
817 sax->fsa_ax25.sax25_call = nr->user_addr;
818 sax->fsa_digipeater[0] = nr->dest_addr;
819 *uaddr_len = sizeof(struct full_sockaddr_ax25);
820 } else {
821 sax->fsa_ax25.sax25_family = AF_NETROM;
822 sax->fsa_ax25.sax25_ndigis = 0;
823 sax->fsa_ax25.sax25_call = nr->source_addr;
824 *uaddr_len = sizeof(struct sockaddr_ax25);
826 release_sock(sk);
828 return 0;
831 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
833 struct sock *sk;
834 struct sock *make;
835 struct nr_sock *nr_make;
836 ax25_address *src, *dest, *user;
837 unsigned short circuit_index, circuit_id;
838 unsigned short peer_circuit_index, peer_circuit_id;
839 unsigned short frametype, flags, window, timeout;
840 int ret;
842 skb->sk = NULL; /* Initially we don't know who it's for */
845 * skb->data points to the netrom frame start
848 src = (ax25_address *)(skb->data + 0);
849 dest = (ax25_address *)(skb->data + 7);
851 circuit_index = skb->data[15];
852 circuit_id = skb->data[16];
853 peer_circuit_index = skb->data[17];
854 peer_circuit_id = skb->data[18];
855 frametype = skb->data[19] & 0x0F;
856 flags = skb->data[19] & 0xF0;
858 #ifdef CONFIG_INET
860 * Check for an incoming IP over NET/ROM frame.
862 if (frametype == NR_PROTOEXT && circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
863 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
864 skb->h.raw = skb->data;
866 return nr_rx_ip(skb, dev);
868 #endif
871 * Find an existing socket connection, based on circuit ID, if it's
872 * a Connect Request base it on their circuit ID.
874 * Circuit ID 0/0 is not valid but it could still be a "reset" for a
875 * circuit that no longer exists at the other end ...
878 sk = NULL;
880 if (circuit_index == 0 && circuit_id == 0) {
881 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
882 sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
883 } else {
884 if (frametype == NR_CONNREQ)
885 sk = nr_find_peer(circuit_index, circuit_id, src);
886 else
887 sk = nr_find_socket(circuit_index, circuit_id);
890 if (sk != NULL) {
891 skb->h.raw = skb->data;
893 if (frametype == NR_CONNACK && skb->len == 22)
894 nr_sk(sk)->bpqext = 1;
895 else
896 nr_sk(sk)->bpqext = 0;
898 ret = nr_process_rx_frame(sk, skb);
899 bh_unlock_sock(sk);
900 return ret;
904 * Now it should be a CONNREQ.
906 if (frametype != NR_CONNREQ) {
908 * Here it would be nice to be able to send a reset but
909 * NET/ROM doesn't have one. The following hack would
910 * have been a way to extend the protocol but apparently
911 * it kills BPQ boxes... :-(
913 #if 0
915 * Never reply to a CONNACK/CHOKE.
917 if (frametype != NR_CONNACK || flags != NR_CHOKE_FLAG)
918 nr_transmit_refusal(skb, 1);
919 #endif
920 return 0;
923 sk = nr_find_listener(dest);
925 user = (ax25_address *)(skb->data + 21);
927 if (sk == NULL || sk_acceptq_is_full(sk) ||
928 (make = nr_make_new(sk)) == NULL) {
929 nr_transmit_refusal(skb, 0);
930 if (sk)
931 bh_unlock_sock(sk);
932 return 0;
935 window = skb->data[20];
937 skb->sk = make;
938 make->sk_state = TCP_ESTABLISHED;
940 /* Fill in his circuit details */
941 nr_make = nr_sk(make);
942 nr_make->source_addr = *dest;
943 nr_make->dest_addr = *src;
944 nr_make->user_addr = *user;
946 nr_make->your_index = circuit_index;
947 nr_make->your_id = circuit_id;
949 bh_unlock_sock(sk);
950 circuit = nr_find_next_circuit();
951 bh_lock_sock(sk);
953 nr_make->my_index = circuit / 256;
954 nr_make->my_id = circuit % 256;
956 circuit++;
958 /* Window negotiation */
959 if (window < nr_make->window)
960 nr_make->window = window;
962 /* L4 timeout negotiation */
963 if (skb->len == 37) {
964 timeout = skb->data[36] * 256 + skb->data[35];
965 if (timeout * HZ < nr_make->t1)
966 nr_make->t1 = timeout * HZ;
967 nr_make->bpqext = 1;
968 } else {
969 nr_make->bpqext = 0;
972 nr_write_internal(make, NR_CONNACK);
974 nr_make->condition = 0x00;
975 nr_make->vs = 0;
976 nr_make->va = 0;
977 nr_make->vr = 0;
978 nr_make->vl = 0;
979 nr_make->state = NR_STATE_3;
980 sk->sk_ack_backlog++;
982 nr_insert_socket(make);
984 skb_queue_head(&sk->sk_receive_queue, skb);
986 nr_start_heartbeat(make);
987 nr_start_idletimer(make);
989 if (!sock_flag(sk, SOCK_DEAD))
990 sk->sk_data_ready(sk, skb->len);
992 bh_unlock_sock(sk);
993 return 1;
996 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
997 struct msghdr *msg, size_t len)
999 struct sock *sk = sock->sk;
1000 struct nr_sock *nr = nr_sk(sk);
1001 struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1002 int err;
1003 struct sockaddr_ax25 sax;
1004 struct sk_buff *skb;
1005 unsigned char *asmptr;
1006 int size;
1008 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1009 return -EINVAL;
1011 lock_sock(sk);
1012 if (sock_flag(sk, SOCK_ZAPPED)) {
1013 err = -EADDRNOTAVAIL;
1014 goto out;
1017 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1018 send_sig(SIGPIPE, current, 0);
1019 err = -EPIPE;
1020 goto out;
1023 if (nr->device == NULL) {
1024 err = -ENETUNREACH;
1025 goto out;
1028 if (usax) {
1029 if (msg->msg_namelen < sizeof(sax)) {
1030 err = -EINVAL;
1031 goto out;
1033 sax = *usax;
1034 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1035 err = -EISCONN;
1036 goto out;
1038 if (sax.sax25_family != AF_NETROM) {
1039 err = -EINVAL;
1040 goto out;
1042 } else {
1043 if (sk->sk_state != TCP_ESTABLISHED) {
1044 err = -ENOTCONN;
1045 goto out;
1047 sax.sax25_family = AF_NETROM;
1048 sax.sax25_call = nr->dest_addr;
1051 SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1053 /* Build a packet */
1054 SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1055 size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1057 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1058 goto out;
1060 skb_reserve(skb, size - len);
1063 * Push down the NET/ROM header
1066 asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1067 SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1069 /* Build a NET/ROM Transport header */
1071 *asmptr++ = nr->your_index;
1072 *asmptr++ = nr->your_id;
1073 *asmptr++ = 0; /* To be filled in later */
1074 *asmptr++ = 0; /* Ditto */
1075 *asmptr++ = NR_INFO;
1076 SOCK_DEBUG(sk, "Built header.\n");
1079 * Put the data on the end
1082 skb->h.raw = skb_put(skb, len);
1084 asmptr = skb->h.raw;
1085 SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1087 /* User data follows immediately after the NET/ROM transport header */
1088 if (memcpy_fromiovec(asmptr, msg->msg_iov, len)) {
1089 kfree_skb(skb);
1090 err = -EFAULT;
1091 goto out;
1094 SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1096 if (sk->sk_state != TCP_ESTABLISHED) {
1097 kfree_skb(skb);
1098 err = -ENOTCONN;
1099 goto out;
1102 nr_output(sk, skb); /* Shove it onto the queue */
1104 err = len;
1105 out:
1106 release_sock(sk);
1107 return err;
1110 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1111 struct msghdr *msg, size_t size, int flags)
1113 struct sock *sk = sock->sk;
1114 struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1115 size_t copied;
1116 struct sk_buff *skb;
1117 int er;
1120 * This works for seqpacket too. The receiver has ordered the queue for
1121 * us! We do one quick check first though
1124 lock_sock(sk);
1125 if (sk->sk_state != TCP_ESTABLISHED) {
1126 release_sock(sk);
1127 return -ENOTCONN;
1130 /* Now we can treat all alike */
1131 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1132 release_sock(sk);
1133 return er;
1136 skb->h.raw = skb->data;
1137 copied = skb->len;
1139 if (copied > size) {
1140 copied = size;
1141 msg->msg_flags |= MSG_TRUNC;
1144 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1146 if (sax != NULL) {
1147 sax->sax25_family = AF_NETROM;
1148 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1151 msg->msg_namelen = sizeof(*sax);
1153 skb_free_datagram(sk, skb);
1155 release_sock(sk);
1156 return copied;
1160 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1162 struct sock *sk = sock->sk;
1163 void __user *argp = (void __user *)arg;
1164 int ret;
1166 lock_sock(sk);
1167 switch (cmd) {
1168 case TIOCOUTQ: {
1169 long amount;
1170 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1171 if (amount < 0)
1172 amount = 0;
1173 release_sock(sk);
1174 return put_user(amount, (int __user *)argp);
1177 case TIOCINQ: {
1178 struct sk_buff *skb;
1179 long amount = 0L;
1180 /* These two are safe on a single CPU system as only user tasks fiddle here */
1181 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1182 amount = skb->len;
1183 release_sock(sk);
1184 return put_user(amount, (int __user *)argp);
1187 case SIOCGSTAMP:
1188 ret = -EINVAL;
1189 if (sk != NULL)
1190 ret = sock_get_timestamp(sk, argp);
1191 release_sock(sk);
1192 return ret;
1194 case SIOCGIFADDR:
1195 case SIOCSIFADDR:
1196 case SIOCGIFDSTADDR:
1197 case SIOCSIFDSTADDR:
1198 case SIOCGIFBRDADDR:
1199 case SIOCSIFBRDADDR:
1200 case SIOCGIFNETMASK:
1201 case SIOCSIFNETMASK:
1202 case SIOCGIFMETRIC:
1203 case SIOCSIFMETRIC:
1204 release_sock(sk);
1205 return -EINVAL;
1207 case SIOCADDRT:
1208 case SIOCDELRT:
1209 case SIOCNRDECOBS:
1210 release_sock(sk);
1211 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1212 return nr_rt_ioctl(cmd, argp);
1214 default:
1215 release_sock(sk);
1216 return dev_ioctl(cmd, argp);
1218 release_sock(sk);
1220 return 0;
1223 #ifdef CONFIG_PROC_FS
1225 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1227 struct sock *s;
1228 struct hlist_node *node;
1229 int i = 1;
1231 spin_lock_bh(&nr_list_lock);
1232 if (*pos == 0)
1233 return SEQ_START_TOKEN;
1235 sk_for_each(s, node, &nr_list) {
1236 if (i == *pos)
1237 return s;
1238 ++i;
1240 return NULL;
1243 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1245 ++*pos;
1247 return (v == SEQ_START_TOKEN) ? sk_head(&nr_list)
1248 : sk_next((struct sock *)v);
1251 static void nr_info_stop(struct seq_file *seq, void *v)
1253 spin_unlock_bh(&nr_list_lock);
1256 static int nr_info_show(struct seq_file *seq, void *v)
1258 struct sock *s = v;
1259 struct net_device *dev;
1260 struct nr_sock *nr;
1261 const char *devname;
1263 if (v == SEQ_START_TOKEN)
1264 seq_puts(seq,
1265 "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");
1267 else {
1269 bh_lock_sock(s);
1270 nr = nr_sk(s);
1272 if ((dev = nr->device) == NULL)
1273 devname = "???";
1274 else
1275 devname = dev->name;
1277 seq_printf(seq, "%-9s ", ax2asc(&nr->user_addr));
1278 seq_printf(seq, "%-9s ", ax2asc(&nr->dest_addr));
1279 seq_printf(seq,
1280 "%-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",
1281 ax2asc(&nr->source_addr),
1282 devname,
1283 nr->my_index,
1284 nr->my_id,
1285 nr->your_index,
1286 nr->your_id,
1287 nr->state,
1288 nr->vs,
1289 nr->vr,
1290 nr->va,
1291 ax25_display_timer(&nr->t1timer) / HZ,
1292 nr->t1 / HZ,
1293 ax25_display_timer(&nr->t2timer) / HZ,
1294 nr->t2 / HZ,
1295 ax25_display_timer(&nr->t4timer) / HZ,
1296 nr->t4 / HZ,
1297 ax25_display_timer(&nr->idletimer) / (60 * HZ),
1298 nr->idle / (60 * HZ),
1299 nr->n2count,
1300 nr->n2,
1301 nr->window,
1302 atomic_read(&s->sk_wmem_alloc),
1303 atomic_read(&s->sk_rmem_alloc),
1304 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1306 bh_unlock_sock(s);
1308 return 0;
1311 static struct seq_operations nr_info_seqops = {
1312 .start = nr_info_start,
1313 .next = nr_info_next,
1314 .stop = nr_info_stop,
1315 .show = nr_info_show,
1318 static int nr_info_open(struct inode *inode, struct file *file)
1320 return seq_open(file, &nr_info_seqops);
1323 static struct file_operations nr_info_fops = {
1324 .owner = THIS_MODULE,
1325 .open = nr_info_open,
1326 .read = seq_read,
1327 .llseek = seq_lseek,
1328 .release = seq_release,
1330 #endif /* CONFIG_PROC_FS */
1332 static struct net_proto_family nr_family_ops = {
1333 .family = PF_NETROM,
1334 .create = nr_create,
1335 .owner = THIS_MODULE,
1338 static struct proto_ops nr_proto_ops = {
1339 .family = PF_NETROM,
1340 .owner = THIS_MODULE,
1341 .release = nr_release,
1342 .bind = nr_bind,
1343 .connect = nr_connect,
1344 .socketpair = sock_no_socketpair,
1345 .accept = nr_accept,
1346 .getname = nr_getname,
1347 .poll = datagram_poll,
1348 .ioctl = nr_ioctl,
1349 .listen = nr_listen,
1350 .shutdown = sock_no_shutdown,
1351 .setsockopt = nr_setsockopt,
1352 .getsockopt = nr_getsockopt,
1353 .sendmsg = nr_sendmsg,
1354 .recvmsg = nr_recvmsg,
1355 .mmap = sock_no_mmap,
1356 .sendpage = sock_no_sendpage,
1359 static struct notifier_block nr_dev_notifier = {
1360 .notifier_call = nr_device_event,
1363 static struct net_device **dev_nr;
1365 static char banner[] __initdata = KERN_INFO "G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n";
1367 static int __init nr_proto_init(void)
1369 int i;
1370 int rc = proto_register(&nr_proto, 0);
1372 if (rc != 0)
1373 goto out;
1375 if (nr_ndevs > 0x7fffffff/sizeof(struct net_device *)) {
1376 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1377 return -1;
1380 dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1381 if (dev_nr == NULL) {
1382 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1383 return -1;
1386 memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device *));
1388 for (i = 0; i < nr_ndevs; i++) {
1389 char name[IFNAMSIZ];
1390 struct net_device *dev;
1392 sprintf(name, "nr%d", i);
1393 dev = alloc_netdev(sizeof(struct net_device_stats), name,
1394 nr_setup);
1395 if (!dev) {
1396 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1397 goto fail;
1400 dev->base_addr = i;
1401 if (register_netdev(dev)) {
1402 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1403 free_netdev(dev);
1404 goto fail;
1406 dev_nr[i] = dev;
1409 if (sock_register(&nr_family_ops)) {
1410 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1411 goto fail;
1414 register_netdevice_notifier(&nr_dev_notifier);
1415 printk(banner);
1417 ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1418 ax25_linkfail_register(nr_link_failed);
1420 #ifdef CONFIG_SYSCTL
1421 nr_register_sysctl();
1422 #endif
1424 nr_loopback_init();
1426 proc_net_fops_create("nr", S_IRUGO, &nr_info_fops);
1427 proc_net_fops_create("nr_neigh", S_IRUGO, &nr_neigh_fops);
1428 proc_net_fops_create("nr_nodes", S_IRUGO, &nr_nodes_fops);
1429 out:
1430 return rc;
1431 fail:
1432 while (--i >= 0) {
1433 unregister_netdev(dev_nr[i]);
1434 free_netdev(dev_nr[i]);
1436 kfree(dev_nr);
1437 proto_unregister(&nr_proto);
1438 rc = -1;
1439 goto out;
1442 module_init(nr_proto_init);
1444 module_param(nr_ndevs, int, 0);
1445 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1447 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1448 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1449 MODULE_LICENSE("GPL");
1450 MODULE_ALIAS_NETPROTO(PF_NETROM);
1452 static void __exit nr_exit(void)
1454 int i;
1456 proc_net_remove("nr");
1457 proc_net_remove("nr_neigh");
1458 proc_net_remove("nr_nodes");
1459 nr_loopback_clear();
1461 nr_rt_free();
1463 #ifdef CONFIG_SYSCTL
1464 nr_unregister_sysctl();
1465 #endif
1467 ax25_linkfail_release(nr_link_failed);
1468 ax25_protocol_release(AX25_P_NETROM);
1470 unregister_netdevice_notifier(&nr_dev_notifier);
1472 sock_unregister(PF_NETROM);
1474 for (i = 0; i < nr_ndevs; i++) {
1475 struct net_device *dev = dev_nr[i];
1476 if (dev) {
1477 unregister_netdev(dev);
1478 free_netdev(dev);
1482 kfree(dev_nr);
1483 proto_unregister(&nr_proto);
1485 module_exit(nr_exit);