[ALSA] es1688 - Use platform_device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / rose / af_rose.c
blob829fdbc4400be21af83e3846fd913cd746fdca3b
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 (C) Jonathan Naylor G4KLX (g4klx@g4klx.demon.co.uk)
8 * Copyright (C) Alan Cox GW4PTS (alan@lxorguk.ukuu.org.uk)
9 * Copyright (C) Terry Dawson VK2KTJ (terry@animats.net)
10 * Copyright (C) Tomi Manninen OH2BNS (oh2bns@sral.fi)
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/errno.h>
17 #include <linux/types.h>
18 #include <linux/socket.h>
19 #include <linux/in.h>
20 #include <linux/kernel.h>
21 #include <linux/sched.h>
22 #include <linux/spinlock.h>
23 #include <linux/timer.h>
24 #include <linux/string.h>
25 #include <linux/sockios.h>
26 #include <linux/net.h>
27 #include <linux/stat.h>
28 #include <net/ax25.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/skbuff.h>
33 #include <net/sock.h>
34 #include <asm/system.h>
35 #include <asm/uaccess.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
38 #include <linux/mm.h>
39 #include <linux/interrupt.h>
40 #include <linux/notifier.h>
41 #include <net/rose.h>
42 #include <linux/proc_fs.h>
43 #include <linux/seq_file.h>
44 #include <net/tcp_states.h>
45 #include <net/ip.h>
46 #include <net/arp.h>
48 static int rose_ndevs = 10;
50 int sysctl_rose_restart_request_timeout = ROSE_DEFAULT_T0;
51 int sysctl_rose_call_request_timeout = ROSE_DEFAULT_T1;
52 int sysctl_rose_reset_request_timeout = ROSE_DEFAULT_T2;
53 int sysctl_rose_clear_request_timeout = ROSE_DEFAULT_T3;
54 int sysctl_rose_no_activity_timeout = ROSE_DEFAULT_IDLE;
55 int sysctl_rose_ack_hold_back_timeout = ROSE_DEFAULT_HB;
56 int sysctl_rose_routing_control = ROSE_DEFAULT_ROUTING;
57 int sysctl_rose_link_fail_timeout = ROSE_DEFAULT_FAIL_TIMEOUT;
58 int sysctl_rose_maximum_vcs = ROSE_DEFAULT_MAXVC;
59 int sysctl_rose_window_size = ROSE_DEFAULT_WINDOW_SIZE;
61 static HLIST_HEAD(rose_list);
62 static DEFINE_SPINLOCK(rose_list_lock);
64 static struct proto_ops rose_proto_ops;
66 ax25_address rose_callsign;
69 * Convert a ROSE address into text.
71 const char *rose2asc(const rose_address *addr)
73 static char buffer[11];
75 if (addr->rose_addr[0] == 0x00 && addr->rose_addr[1] == 0x00 &&
76 addr->rose_addr[2] == 0x00 && addr->rose_addr[3] == 0x00 &&
77 addr->rose_addr[4] == 0x00) {
78 strcpy(buffer, "*");
79 } else {
80 sprintf(buffer, "%02X%02X%02X%02X%02X", addr->rose_addr[0] & 0xFF,
81 addr->rose_addr[1] & 0xFF,
82 addr->rose_addr[2] & 0xFF,
83 addr->rose_addr[3] & 0xFF,
84 addr->rose_addr[4] & 0xFF);
87 return buffer;
91 * Compare two ROSE addresses, 0 == equal.
93 int rosecmp(rose_address *addr1, rose_address *addr2)
95 int i;
97 for (i = 0; i < 5; i++)
98 if (addr1->rose_addr[i] != addr2->rose_addr[i])
99 return 1;
101 return 0;
105 * Compare two ROSE addresses for only mask digits, 0 == equal.
107 int rosecmpm(rose_address *addr1, rose_address *addr2, unsigned short mask)
109 int i, j;
111 if (mask > 10)
112 return 1;
114 for (i = 0; i < mask; i++) {
115 j = i / 2;
117 if ((i % 2) != 0) {
118 if ((addr1->rose_addr[j] & 0x0F) != (addr2->rose_addr[j] & 0x0F))
119 return 1;
120 } else {
121 if ((addr1->rose_addr[j] & 0xF0) != (addr2->rose_addr[j] & 0xF0))
122 return 1;
126 return 0;
130 * Socket removal during an interrupt is now safe.
132 static void rose_remove_socket(struct sock *sk)
134 spin_lock_bh(&rose_list_lock);
135 sk_del_node_init(sk);
136 spin_unlock_bh(&rose_list_lock);
140 * Kill all bound sockets on a broken link layer connection to a
141 * particular neighbour.
143 void rose_kill_by_neigh(struct rose_neigh *neigh)
145 struct sock *s;
146 struct hlist_node *node;
148 spin_lock_bh(&rose_list_lock);
149 sk_for_each(s, node, &rose_list) {
150 struct rose_sock *rose = rose_sk(s);
152 if (rose->neighbour == neigh) {
153 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
154 rose->neighbour->use--;
155 rose->neighbour = NULL;
158 spin_unlock_bh(&rose_list_lock);
162 * Kill all bound sockets on a dropped device.
164 static void rose_kill_by_device(struct net_device *dev)
166 struct sock *s;
167 struct hlist_node *node;
169 spin_lock_bh(&rose_list_lock);
170 sk_for_each(s, node, &rose_list) {
171 struct rose_sock *rose = rose_sk(s);
173 if (rose->device == dev) {
174 rose_disconnect(s, ENETUNREACH, ROSE_OUT_OF_ORDER, 0);
175 rose->neighbour->use--;
176 rose->device = NULL;
179 spin_unlock_bh(&rose_list_lock);
183 * Handle device status changes.
185 static int rose_device_event(struct notifier_block *this, unsigned long event,
186 void *ptr)
188 struct net_device *dev = (struct net_device *)ptr;
190 if (event != NETDEV_DOWN)
191 return NOTIFY_DONE;
193 switch (dev->type) {
194 case ARPHRD_ROSE:
195 rose_kill_by_device(dev);
196 break;
197 case ARPHRD_AX25:
198 rose_link_device_down(dev);
199 rose_rt_device_down(dev);
200 break;
203 return NOTIFY_DONE;
207 * Add a socket to the bound sockets list.
209 static void rose_insert_socket(struct sock *sk)
212 spin_lock_bh(&rose_list_lock);
213 sk_add_node(sk, &rose_list);
214 spin_unlock_bh(&rose_list_lock);
218 * Find a socket that wants to accept the Call Request we just
219 * received.
221 static struct sock *rose_find_listener(rose_address *addr, ax25_address *call)
223 struct sock *s;
224 struct hlist_node *node;
226 spin_lock_bh(&rose_list_lock);
227 sk_for_each(s, node, &rose_list) {
228 struct rose_sock *rose = rose_sk(s);
230 if (!rosecmp(&rose->source_addr, addr) &&
231 !ax25cmp(&rose->source_call, call) &&
232 !rose->source_ndigis && s->sk_state == TCP_LISTEN)
233 goto found;
236 sk_for_each(s, node, &rose_list) {
237 struct rose_sock *rose = rose_sk(s);
239 if (!rosecmp(&rose->source_addr, addr) &&
240 !ax25cmp(&rose->source_call, &null_ax25_address) &&
241 s->sk_state == TCP_LISTEN)
242 goto found;
244 s = NULL;
245 found:
246 spin_unlock_bh(&rose_list_lock);
247 return s;
251 * Find a connected ROSE socket given my LCI and device.
253 struct sock *rose_find_socket(unsigned int lci, struct rose_neigh *neigh)
255 struct sock *s;
256 struct hlist_node *node;
258 spin_lock_bh(&rose_list_lock);
259 sk_for_each(s, node, &rose_list) {
260 struct rose_sock *rose = rose_sk(s);
262 if (rose->lci == lci && rose->neighbour == neigh)
263 goto found;
265 s = NULL;
266 found:
267 spin_unlock_bh(&rose_list_lock);
268 return s;
272 * Find a unique LCI for a given device.
274 unsigned int rose_new_lci(struct rose_neigh *neigh)
276 int lci;
278 if (neigh->dce_mode) {
279 for (lci = 1; lci <= sysctl_rose_maximum_vcs; lci++)
280 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
281 return lci;
282 } else {
283 for (lci = sysctl_rose_maximum_vcs; lci > 0; lci--)
284 if (rose_find_socket(lci, neigh) == NULL && rose_route_free_lci(lci, neigh) == NULL)
285 return lci;
288 return 0;
292 * Deferred destroy.
294 void rose_destroy_socket(struct sock *);
297 * Handler for deferred kills.
299 static void rose_destroy_timer(unsigned long data)
301 rose_destroy_socket((struct sock *)data);
305 * This is called from user mode and the timers. Thus it protects itself
306 * against interrupt users but doesn't worry about being called during
307 * work. Once it is removed from the queue no interrupt or bottom half
308 * will touch it and we are (fairly 8-) ) safe.
310 void rose_destroy_socket(struct sock *sk)
312 struct sk_buff *skb;
314 rose_remove_socket(sk);
315 rose_stop_heartbeat(sk);
316 rose_stop_idletimer(sk);
317 rose_stop_timer(sk);
319 rose_clear_queues(sk); /* Flush the queues */
321 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
322 if (skb->sk != sk) { /* A pending connection */
323 /* Queue the unaccepted socket for death */
324 sock_set_flag(skb->sk, SOCK_DEAD);
325 rose_start_heartbeat(skb->sk);
326 rose_sk(skb->sk)->state = ROSE_STATE_0;
329 kfree_skb(skb);
332 if (atomic_read(&sk->sk_wmem_alloc) ||
333 atomic_read(&sk->sk_rmem_alloc)) {
334 /* Defer: outstanding buffers */
335 init_timer(&sk->sk_timer);
336 sk->sk_timer.expires = jiffies + 10 * HZ;
337 sk->sk_timer.function = rose_destroy_timer;
338 sk->sk_timer.data = (unsigned long)sk;
339 add_timer(&sk->sk_timer);
340 } else
341 sock_put(sk);
345 * Handling for system calls applied via the various interfaces to a
346 * ROSE socket object.
349 static int rose_setsockopt(struct socket *sock, int level, int optname,
350 char __user *optval, int optlen)
352 struct sock *sk = sock->sk;
353 struct rose_sock *rose = rose_sk(sk);
354 int opt;
356 if (level != SOL_ROSE)
357 return -ENOPROTOOPT;
359 if (optlen < sizeof(int))
360 return -EINVAL;
362 if (get_user(opt, (int __user *)optval))
363 return -EFAULT;
365 switch (optname) {
366 case ROSE_DEFER:
367 rose->defer = opt ? 1 : 0;
368 return 0;
370 case ROSE_T1:
371 if (opt < 1)
372 return -EINVAL;
373 rose->t1 = opt * HZ;
374 return 0;
376 case ROSE_T2:
377 if (opt < 1)
378 return -EINVAL;
379 rose->t2 = opt * HZ;
380 return 0;
382 case ROSE_T3:
383 if (opt < 1)
384 return -EINVAL;
385 rose->t3 = opt * HZ;
386 return 0;
388 case ROSE_HOLDBACK:
389 if (opt < 1)
390 return -EINVAL;
391 rose->hb = opt * HZ;
392 return 0;
394 case ROSE_IDLE:
395 if (opt < 0)
396 return -EINVAL;
397 rose->idle = opt * 60 * HZ;
398 return 0;
400 case ROSE_QBITINCL:
401 rose->qbitincl = opt ? 1 : 0;
402 return 0;
404 default:
405 return -ENOPROTOOPT;
409 static int rose_getsockopt(struct socket *sock, int level, int optname,
410 char __user *optval, int __user *optlen)
412 struct sock *sk = sock->sk;
413 struct rose_sock *rose = rose_sk(sk);
414 int val = 0;
415 int len;
417 if (level != SOL_ROSE)
418 return -ENOPROTOOPT;
420 if (get_user(len, optlen))
421 return -EFAULT;
423 if (len < 0)
424 return -EINVAL;
426 switch (optname) {
427 case ROSE_DEFER:
428 val = rose->defer;
429 break;
431 case ROSE_T1:
432 val = rose->t1 / HZ;
433 break;
435 case ROSE_T2:
436 val = rose->t2 / HZ;
437 break;
439 case ROSE_T3:
440 val = rose->t3 / HZ;
441 break;
443 case ROSE_HOLDBACK:
444 val = rose->hb / HZ;
445 break;
447 case ROSE_IDLE:
448 val = rose->idle / (60 * HZ);
449 break;
451 case ROSE_QBITINCL:
452 val = rose->qbitincl;
453 break;
455 default:
456 return -ENOPROTOOPT;
459 len = min_t(unsigned int, len, sizeof(int));
461 if (put_user(len, optlen))
462 return -EFAULT;
464 return copy_to_user(optval, &val, len) ? -EFAULT : 0;
467 static int rose_listen(struct socket *sock, int backlog)
469 struct sock *sk = sock->sk;
471 if (sk->sk_state != TCP_LISTEN) {
472 struct rose_sock *rose = rose_sk(sk);
474 rose->dest_ndigis = 0;
475 memset(&rose->dest_addr, 0, ROSE_ADDR_LEN);
476 memset(&rose->dest_call, 0, AX25_ADDR_LEN);
477 memset(rose->dest_digis, 0, AX25_ADDR_LEN * ROSE_MAX_DIGIS);
478 sk->sk_max_ack_backlog = backlog;
479 sk->sk_state = TCP_LISTEN;
480 return 0;
483 return -EOPNOTSUPP;
486 static struct proto rose_proto = {
487 .name = "ROSE",
488 .owner = THIS_MODULE,
489 .obj_size = sizeof(struct rose_sock),
492 static int rose_create(struct socket *sock, int protocol)
494 struct sock *sk;
495 struct rose_sock *rose;
497 if (sock->type != SOCK_SEQPACKET || protocol != 0)
498 return -ESOCKTNOSUPPORT;
500 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
501 return -ENOMEM;
503 rose = rose_sk(sk);
505 sock_init_data(sock, sk);
507 skb_queue_head_init(&rose->ack_queue);
508 #ifdef M_BIT
509 skb_queue_head_init(&rose->frag_queue);
510 rose->fraglen = 0;
511 #endif
513 sock->ops = &rose_proto_ops;
514 sk->sk_protocol = protocol;
516 init_timer(&rose->timer);
517 init_timer(&rose->idletimer);
519 rose->t1 = sysctl_rose_call_request_timeout;
520 rose->t2 = sysctl_rose_reset_request_timeout;
521 rose->t3 = sysctl_rose_clear_request_timeout;
522 rose->hb = sysctl_rose_ack_hold_back_timeout;
523 rose->idle = sysctl_rose_no_activity_timeout;
525 rose->state = ROSE_STATE_0;
527 return 0;
530 static struct sock *rose_make_new(struct sock *osk)
532 struct sock *sk;
533 struct rose_sock *rose, *orose;
535 if (osk->sk_type != SOCK_SEQPACKET)
536 return NULL;
538 if ((sk = sk_alloc(PF_ROSE, GFP_ATOMIC, &rose_proto, 1)) == NULL)
539 return NULL;
541 rose = rose_sk(sk);
543 sock_init_data(NULL, sk);
545 skb_queue_head_init(&rose->ack_queue);
546 #ifdef M_BIT
547 skb_queue_head_init(&rose->frag_queue);
548 rose->fraglen = 0;
549 #endif
551 sk->sk_type = osk->sk_type;
552 sk->sk_socket = osk->sk_socket;
553 sk->sk_priority = osk->sk_priority;
554 sk->sk_protocol = osk->sk_protocol;
555 sk->sk_rcvbuf = osk->sk_rcvbuf;
556 sk->sk_sndbuf = osk->sk_sndbuf;
557 sk->sk_state = TCP_ESTABLISHED;
558 sk->sk_sleep = osk->sk_sleep;
559 sock_copy_flags(sk, osk);
561 init_timer(&rose->timer);
562 init_timer(&rose->idletimer);
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;
574 return sk;
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;
584 rose = rose_sk(sk);
586 switch (rose->state) {
587 case ROSE_STATE_0:
588 rose_disconnect(sk, 0, -1, -1);
589 rose_destroy_socket(sk);
590 break;
592 case ROSE_STATE_2:
593 rose->neighbour->use--;
594 rose_disconnect(sk, 0, -1, -1);
595 rose_destroy_socket(sk);
596 break;
598 case ROSE_STATE_1:
599 case ROSE_STATE_3:
600 case ROSE_STATE_4:
601 case ROSE_STATE_5:
602 rose_clear_queues(sk);
603 rose_stop_idletimer(sk);
604 rose_write_internal(sk, ROSE_CLEAR_REQUEST);
605 rose_start_t3timer(sk);
606 rose->state = ROSE_STATE_2;
607 sk->sk_state = TCP_CLOSE;
608 sk->sk_shutdown |= SEND_SHUTDOWN;
609 sk->sk_state_change(sk);
610 sock_set_flag(sk, SOCK_DEAD);
611 sock_set_flag(sk, SOCK_DESTROY);
612 break;
614 default:
615 break;
618 sock->sk = NULL;
620 return 0;
623 static int rose_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
625 struct sock *sk = sock->sk;
626 struct rose_sock *rose = rose_sk(sk);
627 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
628 struct net_device *dev;
629 ax25_address *source;
630 ax25_uid_assoc *user;
631 int n;
633 if (!sock_flag(sk, SOCK_ZAPPED))
634 return -EINVAL;
636 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
637 return -EINVAL;
639 if (addr->srose_family != AF_ROSE)
640 return -EINVAL;
642 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
643 return -EINVAL;
645 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
646 return -EINVAL;
648 if ((dev = rose_dev_get(&addr->srose_addr)) == NULL) {
649 SOCK_DEBUG(sk, "ROSE: bind failed: invalid address\n");
650 return -EADDRNOTAVAIL;
653 source = &addr->srose_call;
655 user = ax25_findbyuid(current->euid);
656 if (user) {
657 rose->source_call = user->call;
658 ax25_uid_put(user);
659 } else {
660 if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE))
661 return -EACCES;
662 rose->source_call = *source;
665 rose->source_addr = addr->srose_addr;
666 rose->device = dev;
667 rose->source_ndigis = addr->srose_ndigis;
669 if (addr_len == sizeof(struct full_sockaddr_rose)) {
670 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
671 for (n = 0 ; n < addr->srose_ndigis ; n++)
672 rose->source_digis[n] = full_addr->srose_digis[n];
673 } else {
674 if (rose->source_ndigis == 1) {
675 rose->source_digis[0] = addr->srose_digi;
679 rose_insert_socket(sk);
681 sock_reset_flag(sk, SOCK_ZAPPED);
682 SOCK_DEBUG(sk, "ROSE: socket is bound\n");
683 return 0;
686 static int rose_connect(struct socket *sock, struct sockaddr *uaddr, int addr_len, int flags)
688 struct sock *sk = sock->sk;
689 struct rose_sock *rose = rose_sk(sk);
690 struct sockaddr_rose *addr = (struct sockaddr_rose *)uaddr;
691 unsigned char cause, diagnostic;
692 struct net_device *dev;
693 ax25_uid_assoc *user;
694 int n;
696 if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
697 sock->state = SS_CONNECTED;
698 return 0; /* Connect completed during a ERESTARTSYS event */
701 if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
702 sock->state = SS_UNCONNECTED;
703 return -ECONNREFUSED;
706 if (sk->sk_state == TCP_ESTABLISHED)
707 return -EISCONN; /* No reconnect on a seqpacket socket */
709 sk->sk_state = TCP_CLOSE;
710 sock->state = SS_UNCONNECTED;
712 if (addr_len != sizeof(struct sockaddr_rose) && addr_len != sizeof(struct full_sockaddr_rose))
713 return -EINVAL;
715 if (addr->srose_family != AF_ROSE)
716 return -EINVAL;
718 if (addr_len == sizeof(struct sockaddr_rose) && addr->srose_ndigis > 1)
719 return -EINVAL;
721 if (addr->srose_ndigis > ROSE_MAX_DIGIS)
722 return -EINVAL;
724 /* Source + Destination digis should not exceed ROSE_MAX_DIGIS */
725 if ((rose->source_ndigis + addr->srose_ndigis) > ROSE_MAX_DIGIS)
726 return -EINVAL;
728 rose->neighbour = rose_get_neigh(&addr->srose_addr, &cause,
729 &diagnostic);
730 if (!rose->neighbour)
731 return -ENETUNREACH;
733 rose->lci = rose_new_lci(rose->neighbour);
734 if (!rose->lci)
735 return -ENETUNREACH;
737 if (sock_flag(sk, SOCK_ZAPPED)) { /* Must bind first - autobinding in this may or may not work */
738 sock_reset_flag(sk, SOCK_ZAPPED);
740 if ((dev = rose_dev_first()) == NULL)
741 return -ENETUNREACH;
743 user = ax25_findbyuid(current->euid);
744 if (!user)
745 return -EINVAL;
747 memcpy(&rose->source_addr, dev->dev_addr, ROSE_ADDR_LEN);
748 rose->source_call = user->call;
749 rose->device = dev;
750 ax25_uid_put(user);
752 rose_insert_socket(sk); /* Finish the bind */
755 rose->dest_addr = addr->srose_addr;
756 rose->dest_call = addr->srose_call;
757 rose->rand = ((long)rose & 0xFFFF) + rose->lci;
758 rose->dest_ndigis = addr->srose_ndigis;
760 if (addr_len == sizeof(struct full_sockaddr_rose)) {
761 struct full_sockaddr_rose *full_addr = (struct full_sockaddr_rose *)uaddr;
762 for (n = 0 ; n < addr->srose_ndigis ; n++)
763 rose->dest_digis[n] = full_addr->srose_digis[n];
764 } else {
765 if (rose->dest_ndigis == 1) {
766 rose->dest_digis[0] = addr->srose_digi;
770 /* Move to connecting socket, start sending Connect Requests */
771 sock->state = SS_CONNECTING;
772 sk->sk_state = TCP_SYN_SENT;
774 rose->state = ROSE_STATE_1;
776 rose->neighbour->use++;
778 rose_write_internal(sk, ROSE_CALL_REQUEST);
779 rose_start_heartbeat(sk);
780 rose_start_t1timer(sk);
782 /* Now the loop */
783 if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
784 return -EINPROGRESS;
787 * A Connect Ack with Choke or timeout or failed routing will go to
788 * closed.
790 if (sk->sk_state == TCP_SYN_SENT) {
791 struct task_struct *tsk = current;
792 DECLARE_WAITQUEUE(wait, tsk);
794 add_wait_queue(sk->sk_sleep, &wait);
795 for (;;) {
796 set_current_state(TASK_INTERRUPTIBLE);
797 if (sk->sk_state != TCP_SYN_SENT)
798 break;
799 if (!signal_pending(tsk)) {
800 schedule();
801 continue;
803 current->state = TASK_RUNNING;
804 remove_wait_queue(sk->sk_sleep, &wait);
805 return -ERESTARTSYS;
807 current->state = TASK_RUNNING;
808 remove_wait_queue(sk->sk_sleep, &wait);
811 if (sk->sk_state != TCP_ESTABLISHED) {
812 sock->state = SS_UNCONNECTED;
813 return sock_error(sk); /* Always set at this point */
816 sock->state = SS_CONNECTED;
818 return 0;
821 static int rose_accept(struct socket *sock, struct socket *newsock, int flags)
823 struct task_struct *tsk = current;
824 DECLARE_WAITQUEUE(wait, tsk);
825 struct sk_buff *skb;
826 struct sock *newsk;
827 struct sock *sk;
828 int err = 0;
830 if ((sk = sock->sk) == NULL)
831 return -EINVAL;
833 lock_sock(sk);
834 if (sk->sk_type != SOCK_SEQPACKET) {
835 err = -EOPNOTSUPP;
836 goto out;
839 if (sk->sk_state != TCP_LISTEN) {
840 err = -EINVAL;
841 goto out;
845 * The write queue this time is holding sockets ready to use
846 * hooked into the SABM we saved
848 add_wait_queue(sk->sk_sleep, &wait);
849 for (;;) {
850 skb = skb_dequeue(&sk->sk_receive_queue);
851 if (skb)
852 break;
854 current->state = TASK_INTERRUPTIBLE;
855 release_sock(sk);
856 if (flags & O_NONBLOCK) {
857 current->state = TASK_RUNNING;
858 remove_wait_queue(sk->sk_sleep, &wait);
859 return -EWOULDBLOCK;
861 if (!signal_pending(tsk)) {
862 schedule();
863 lock_sock(sk);
864 continue;
866 return -ERESTARTSYS;
868 current->state = TASK_RUNNING;
869 remove_wait_queue(sk->sk_sleep, &wait);
871 newsk = skb->sk;
872 newsk->sk_socket = newsock;
873 newsk->sk_sleep = &newsock->wait;
875 /* Now attach up the new socket */
876 skb->sk = NULL;
877 kfree_skb(skb);
878 sk->sk_ack_backlog--;
879 newsock->sk = newsk;
881 out:
882 release_sock(sk);
884 return err;
887 static int rose_getname(struct socket *sock, struct sockaddr *uaddr,
888 int *uaddr_len, int peer)
890 struct full_sockaddr_rose *srose = (struct full_sockaddr_rose *)uaddr;
891 struct sock *sk = sock->sk;
892 struct rose_sock *rose = rose_sk(sk);
893 int n;
895 if (peer != 0) {
896 if (sk->sk_state != TCP_ESTABLISHED)
897 return -ENOTCONN;
898 srose->srose_family = AF_ROSE;
899 srose->srose_addr = rose->dest_addr;
900 srose->srose_call = rose->dest_call;
901 srose->srose_ndigis = rose->dest_ndigis;
902 for (n = 0; n < rose->dest_ndigis; n++)
903 srose->srose_digis[n] = rose->dest_digis[n];
904 } else {
905 srose->srose_family = AF_ROSE;
906 srose->srose_addr = rose->source_addr;
907 srose->srose_call = rose->source_call;
908 srose->srose_ndigis = rose->source_ndigis;
909 for (n = 0; n < rose->source_ndigis; n++)
910 srose->srose_digis[n] = rose->source_digis[n];
913 *uaddr_len = sizeof(struct full_sockaddr_rose);
914 return 0;
917 int rose_rx_call_request(struct sk_buff *skb, struct net_device *dev, struct rose_neigh *neigh, unsigned int lci)
919 struct sock *sk;
920 struct sock *make;
921 struct rose_sock *make_rose;
922 struct rose_facilities_struct facilities;
923 int n, len;
925 skb->sk = NULL; /* Initially we don't know who it's for */
928 * skb->data points to the rose frame start
930 memset(&facilities, 0x00, sizeof(struct rose_facilities_struct));
932 len = (((skb->data[3] >> 4) & 0x0F) + 1) / 2;
933 len += (((skb->data[3] >> 0) & 0x0F) + 1) / 2;
934 if (!rose_parse_facilities(skb->data + len + 4, &facilities)) {
935 rose_transmit_clear_request(neigh, lci, ROSE_INVALID_FACILITY, 76);
936 return 0;
939 sk = rose_find_listener(&facilities.source_addr, &facilities.source_call);
942 * We can't accept the Call Request.
944 if (sk == NULL || sk_acceptq_is_full(sk) ||
945 (make = rose_make_new(sk)) == NULL) {
946 rose_transmit_clear_request(neigh, lci, ROSE_NETWORK_CONGESTION, 120);
947 return 0;
950 skb->sk = make;
951 make->sk_state = TCP_ESTABLISHED;
952 make_rose = rose_sk(make);
954 make_rose->lci = lci;
955 make_rose->dest_addr = facilities.dest_addr;
956 make_rose->dest_call = facilities.dest_call;
957 make_rose->dest_ndigis = facilities.dest_ndigis;
958 for (n = 0 ; n < facilities.dest_ndigis ; n++)
959 make_rose->dest_digis[n] = facilities.dest_digis[n];
960 make_rose->source_addr = facilities.source_addr;
961 make_rose->source_call = facilities.source_call;
962 make_rose->source_ndigis = facilities.source_ndigis;
963 for (n = 0 ; n < facilities.source_ndigis ; n++)
964 make_rose->source_digis[n]= facilities.source_digis[n];
965 make_rose->neighbour = neigh;
966 make_rose->device = dev;
967 make_rose->facilities = facilities;
969 make_rose->neighbour->use++;
971 if (rose_sk(sk)->defer) {
972 make_rose->state = ROSE_STATE_5;
973 } else {
974 rose_write_internal(make, ROSE_CALL_ACCEPTED);
975 make_rose->state = ROSE_STATE_3;
976 rose_start_idletimer(make);
979 make_rose->condition = 0x00;
980 make_rose->vs = 0;
981 make_rose->va = 0;
982 make_rose->vr = 0;
983 make_rose->vl = 0;
984 sk->sk_ack_backlog++;
986 rose_insert_socket(make);
988 skb_queue_head(&sk->sk_receive_queue, skb);
990 rose_start_heartbeat(make);
992 if (!sock_flag(sk, SOCK_DEAD))
993 sk->sk_data_ready(sk, skb->len);
995 return 1;
998 static int rose_sendmsg(struct kiocb *iocb, struct socket *sock,
999 struct msghdr *msg, size_t len)
1001 struct sock *sk = sock->sk;
1002 struct rose_sock *rose = rose_sk(sk);
1003 struct sockaddr_rose *usrose = (struct sockaddr_rose *)msg->msg_name;
1004 int err;
1005 struct full_sockaddr_rose srose;
1006 struct sk_buff *skb;
1007 unsigned char *asmptr;
1008 int n, size, qbit = 0;
1010 if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR|MSG_CMSG_COMPAT))
1011 return -EINVAL;
1013 if (sock_flag(sk, SOCK_ZAPPED))
1014 return -EADDRNOTAVAIL;
1016 if (sk->sk_shutdown & SEND_SHUTDOWN) {
1017 send_sig(SIGPIPE, current, 0);
1018 return -EPIPE;
1021 if (rose->neighbour == NULL || rose->device == NULL)
1022 return -ENETUNREACH;
1024 if (usrose != NULL) {
1025 if (msg->msg_namelen != sizeof(struct sockaddr_rose) && msg->msg_namelen != sizeof(struct full_sockaddr_rose))
1026 return -EINVAL;
1027 memset(&srose, 0, sizeof(struct full_sockaddr_rose));
1028 memcpy(&srose, usrose, msg->msg_namelen);
1029 if (rosecmp(&rose->dest_addr, &srose.srose_addr) != 0 ||
1030 ax25cmp(&rose->dest_call, &srose.srose_call) != 0)
1031 return -EISCONN;
1032 if (srose.srose_ndigis != rose->dest_ndigis)
1033 return -EISCONN;
1034 if (srose.srose_ndigis == rose->dest_ndigis) {
1035 for (n = 0 ; n < srose.srose_ndigis ; n++)
1036 if (ax25cmp(&rose->dest_digis[n],
1037 &srose.srose_digis[n]))
1038 return -EISCONN;
1040 if (srose.srose_family != AF_ROSE)
1041 return -EINVAL;
1042 } else {
1043 if (sk->sk_state != TCP_ESTABLISHED)
1044 return -ENOTCONN;
1046 srose.srose_family = AF_ROSE;
1047 srose.srose_addr = rose->dest_addr;
1048 srose.srose_call = rose->dest_call;
1049 srose.srose_ndigis = rose->dest_ndigis;
1050 for (n = 0 ; n < rose->dest_ndigis ; n++)
1051 srose.srose_digis[n] = rose->dest_digis[n];
1054 SOCK_DEBUG(sk, "ROSE: sendto: Addresses built.\n");
1056 /* Build a packet */
1057 SOCK_DEBUG(sk, "ROSE: sendto: building packet.\n");
1058 size = len + AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN;
1060 if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1061 return err;
1063 skb_reserve(skb, AX25_BPQ_HEADER_LEN + AX25_MAX_HEADER_LEN + ROSE_MIN_LEN);
1066 * Put the data on the end
1068 SOCK_DEBUG(sk, "ROSE: Appending user data\n");
1070 asmptr = skb->h.raw = skb_put(skb, len);
1072 err = memcpy_fromiovec(asmptr, msg->msg_iov, len);
1073 if (err) {
1074 kfree_skb(skb);
1075 return err;
1079 * If the Q BIT Include socket option is in force, the first
1080 * byte of the user data is the logical value of the Q Bit.
1082 if (rose->qbitincl) {
1083 qbit = skb->data[0];
1084 skb_pull(skb, 1);
1088 * Push down the ROSE header
1090 asmptr = skb_push(skb, ROSE_MIN_LEN);
1092 SOCK_DEBUG(sk, "ROSE: Building Network Header.\n");
1094 /* Build a ROSE Network header */
1095 asmptr[0] = ((rose->lci >> 8) & 0x0F) | ROSE_GFI;
1096 asmptr[1] = (rose->lci >> 0) & 0xFF;
1097 asmptr[2] = ROSE_DATA;
1099 if (qbit)
1100 asmptr[0] |= ROSE_Q_BIT;
1102 SOCK_DEBUG(sk, "ROSE: Built header.\n");
1104 SOCK_DEBUG(sk, "ROSE: Transmitting buffer\n");
1106 if (sk->sk_state != TCP_ESTABLISHED) {
1107 kfree_skb(skb);
1108 return -ENOTCONN;
1111 #ifdef M_BIT
1112 #define ROSE_PACLEN (256-ROSE_MIN_LEN)
1113 if (skb->len - ROSE_MIN_LEN > ROSE_PACLEN) {
1114 unsigned char header[ROSE_MIN_LEN];
1115 struct sk_buff *skbn;
1116 int frontlen;
1117 int lg;
1119 /* Save a copy of the Header */
1120 memcpy(header, skb->data, ROSE_MIN_LEN);
1121 skb_pull(skb, ROSE_MIN_LEN);
1123 frontlen = skb_headroom(skb);
1125 while (skb->len > 0) {
1126 if ((skbn = sock_alloc_send_skb(sk, frontlen + ROSE_PACLEN, 0, &err)) == NULL) {
1127 kfree_skb(skb);
1128 return err;
1131 skbn->sk = sk;
1132 skbn->free = 1;
1133 skbn->arp = 1;
1135 skb_reserve(skbn, frontlen);
1137 lg = (ROSE_PACLEN > skb->len) ? skb->len : ROSE_PACLEN;
1139 /* Copy the user data */
1140 memcpy(skb_put(skbn, lg), skb->data, lg);
1141 skb_pull(skb, lg);
1143 /* Duplicate the Header */
1144 skb_push(skbn, ROSE_MIN_LEN);
1145 memcpy(skbn->data, header, ROSE_MIN_LEN);
1147 if (skb->len > 0)
1148 skbn->data[2] |= M_BIT;
1150 skb_queue_tail(&sk->sk_write_queue, skbn); /* Throw it on the queue */
1153 skb->free = 1;
1154 kfree_skb(skb);
1155 } else {
1156 skb_queue_tail(&sk->sk_write_queue, skb); /* Throw it on the queue */
1158 #else
1159 skb_queue_tail(&sk->sk_write_queue, skb); /* Shove it onto the queue */
1160 #endif
1162 rose_kick(sk);
1164 return len;
1168 static int rose_recvmsg(struct kiocb *iocb, struct socket *sock,
1169 struct msghdr *msg, size_t size, int flags)
1171 struct sock *sk = sock->sk;
1172 struct rose_sock *rose = rose_sk(sk);
1173 struct sockaddr_rose *srose = (struct sockaddr_rose *)msg->msg_name;
1174 size_t copied;
1175 unsigned char *asmptr;
1176 struct sk_buff *skb;
1177 int n, er, qbit;
1180 * This works for seqpacket too. The receiver has ordered the queue for
1181 * us! We do one quick check first though
1183 if (sk->sk_state != TCP_ESTABLISHED)
1184 return -ENOTCONN;
1186 /* Now we can treat all alike */
1187 if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL)
1188 return er;
1190 qbit = (skb->data[0] & ROSE_Q_BIT) == ROSE_Q_BIT;
1192 skb_pull(skb, ROSE_MIN_LEN);
1194 if (rose->qbitincl) {
1195 asmptr = skb_push(skb, 1);
1196 *asmptr = qbit;
1199 skb->h.raw = skb->data;
1200 copied = skb->len;
1202 if (copied > size) {
1203 copied = size;
1204 msg->msg_flags |= MSG_TRUNC;
1207 skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1209 if (srose != NULL) {
1210 srose->srose_family = AF_ROSE;
1211 srose->srose_addr = rose->dest_addr;
1212 srose->srose_call = rose->dest_call;
1213 srose->srose_ndigis = rose->dest_ndigis;
1214 if (msg->msg_namelen >= sizeof(struct full_sockaddr_rose)) {
1215 struct full_sockaddr_rose *full_srose = (struct full_sockaddr_rose *)msg->msg_name;
1216 for (n = 0 ; n < rose->dest_ndigis ; n++)
1217 full_srose->srose_digis[n] = rose->dest_digis[n];
1218 msg->msg_namelen = sizeof(struct full_sockaddr_rose);
1219 } else {
1220 if (rose->dest_ndigis >= 1) {
1221 srose->srose_ndigis = 1;
1222 srose->srose_digi = rose->dest_digis[0];
1224 msg->msg_namelen = sizeof(struct sockaddr_rose);
1228 skb_free_datagram(sk, skb);
1230 return copied;
1234 static int rose_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1236 struct sock *sk = sock->sk;
1237 struct rose_sock *rose = rose_sk(sk);
1238 void __user *argp = (void __user *)arg;
1240 switch (cmd) {
1241 case TIOCOUTQ: {
1242 long amount;
1243 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1244 if (amount < 0)
1245 amount = 0;
1246 return put_user(amount, (unsigned int __user *) argp);
1249 case TIOCINQ: {
1250 struct sk_buff *skb;
1251 long amount = 0L;
1252 /* These two are safe on a single CPU system as only user tasks fiddle here */
1253 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1254 amount = skb->len;
1255 return put_user(amount, (unsigned int __user *) argp);
1258 case SIOCGSTAMP:
1259 return sock_get_timestamp(sk, (struct timeval __user *) argp);
1261 case SIOCGIFADDR:
1262 case SIOCSIFADDR:
1263 case SIOCGIFDSTADDR:
1264 case SIOCSIFDSTADDR:
1265 case SIOCGIFBRDADDR:
1266 case SIOCSIFBRDADDR:
1267 case SIOCGIFNETMASK:
1268 case SIOCSIFNETMASK:
1269 case SIOCGIFMETRIC:
1270 case SIOCSIFMETRIC:
1271 return -EINVAL;
1273 case SIOCADDRT:
1274 case SIOCDELRT:
1275 case SIOCRSCLRRT:
1276 if (!capable(CAP_NET_ADMIN))
1277 return -EPERM;
1278 return rose_rt_ioctl(cmd, argp);
1280 case SIOCRSGCAUSE: {
1281 struct rose_cause_struct rose_cause;
1282 rose_cause.cause = rose->cause;
1283 rose_cause.diagnostic = rose->diagnostic;
1284 return copy_to_user(argp, &rose_cause, sizeof(struct rose_cause_struct)) ? -EFAULT : 0;
1287 case SIOCRSSCAUSE: {
1288 struct rose_cause_struct rose_cause;
1289 if (copy_from_user(&rose_cause, argp, sizeof(struct rose_cause_struct)))
1290 return -EFAULT;
1291 rose->cause = rose_cause.cause;
1292 rose->diagnostic = rose_cause.diagnostic;
1293 return 0;
1296 case SIOCRSSL2CALL:
1297 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1298 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1299 ax25_listen_release(&rose_callsign, NULL);
1300 if (copy_from_user(&rose_callsign, argp, sizeof(ax25_address)))
1301 return -EFAULT;
1302 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1303 ax25_listen_register(&rose_callsign, NULL);
1304 return 0;
1306 case SIOCRSGL2CALL:
1307 return copy_to_user(argp, &rose_callsign, sizeof(ax25_address)) ? -EFAULT : 0;
1309 case SIOCRSACCEPT:
1310 if (rose->state == ROSE_STATE_5) {
1311 rose_write_internal(sk, ROSE_CALL_ACCEPTED);
1312 rose_start_idletimer(sk);
1313 rose->condition = 0x00;
1314 rose->vs = 0;
1315 rose->va = 0;
1316 rose->vr = 0;
1317 rose->vl = 0;
1318 rose->state = ROSE_STATE_3;
1320 return 0;
1322 default:
1323 return dev_ioctl(cmd, argp);
1326 return 0;
1329 #ifdef CONFIG_PROC_FS
1330 static void *rose_info_start(struct seq_file *seq, loff_t *pos)
1332 int i;
1333 struct sock *s;
1334 struct hlist_node *node;
1336 spin_lock_bh(&rose_list_lock);
1337 if (*pos == 0)
1338 return SEQ_START_TOKEN;
1340 i = 1;
1341 sk_for_each(s, node, &rose_list) {
1342 if (i == *pos)
1343 return s;
1344 ++i;
1346 return NULL;
1349 static void *rose_info_next(struct seq_file *seq, void *v, loff_t *pos)
1351 ++*pos;
1353 return (v == SEQ_START_TOKEN) ? sk_head(&rose_list)
1354 : sk_next((struct sock *)v);
1357 static void rose_info_stop(struct seq_file *seq, void *v)
1359 spin_unlock_bh(&rose_list_lock);
1362 static int rose_info_show(struct seq_file *seq, void *v)
1364 char buf[11];
1366 if (v == SEQ_START_TOKEN)
1367 seq_puts(seq,
1368 "dest_addr dest_call src_addr src_call dev lci neigh st vs vr va t t1 t2 t3 hb idle Snd-Q Rcv-Q inode\n");
1370 else {
1371 struct sock *s = v;
1372 struct rose_sock *rose = rose_sk(s);
1373 const char *devname, *callsign;
1374 const struct net_device *dev = rose->device;
1376 if (!dev)
1377 devname = "???";
1378 else
1379 devname = dev->name;
1381 seq_printf(seq, "%-10s %-9s ",
1382 rose2asc(&rose->dest_addr),
1383 ax2asc(buf, &rose->dest_call));
1385 if (ax25cmp(&rose->source_call, &null_ax25_address) == 0)
1386 callsign = "??????-?";
1387 else
1388 callsign = ax2asc(buf, &rose->source_call);
1390 seq_printf(seq,
1391 "%-10s %-9s %-5s %3.3X %05d %d %d %d %d %3lu %3lu %3lu %3lu %3lu %3lu/%03lu %5d %5d %ld\n",
1392 rose2asc(&rose->source_addr),
1393 callsign,
1394 devname,
1395 rose->lci & 0x0FFF,
1396 (rose->neighbour) ? rose->neighbour->number : 0,
1397 rose->state,
1398 rose->vs,
1399 rose->vr,
1400 rose->va,
1401 ax25_display_timer(&rose->timer) / HZ,
1402 rose->t1 / HZ,
1403 rose->t2 / HZ,
1404 rose->t3 / HZ,
1405 rose->hb / HZ,
1406 ax25_display_timer(&rose->idletimer) / (60 * HZ),
1407 rose->idle / (60 * HZ),
1408 atomic_read(&s->sk_wmem_alloc),
1409 atomic_read(&s->sk_rmem_alloc),
1410 s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1413 return 0;
1416 static struct seq_operations rose_info_seqops = {
1417 .start = rose_info_start,
1418 .next = rose_info_next,
1419 .stop = rose_info_stop,
1420 .show = rose_info_show,
1423 static int rose_info_open(struct inode *inode, struct file *file)
1425 return seq_open(file, &rose_info_seqops);
1428 static struct file_operations rose_info_fops = {
1429 .owner = THIS_MODULE,
1430 .open = rose_info_open,
1431 .read = seq_read,
1432 .llseek = seq_lseek,
1433 .release = seq_release,
1435 #endif /* CONFIG_PROC_FS */
1437 static struct net_proto_family rose_family_ops = {
1438 .family = PF_ROSE,
1439 .create = rose_create,
1440 .owner = THIS_MODULE,
1443 static struct proto_ops rose_proto_ops = {
1444 .family = PF_ROSE,
1445 .owner = THIS_MODULE,
1446 .release = rose_release,
1447 .bind = rose_bind,
1448 .connect = rose_connect,
1449 .socketpair = sock_no_socketpair,
1450 .accept = rose_accept,
1451 .getname = rose_getname,
1452 .poll = datagram_poll,
1453 .ioctl = rose_ioctl,
1454 .listen = rose_listen,
1455 .shutdown = sock_no_shutdown,
1456 .setsockopt = rose_setsockopt,
1457 .getsockopt = rose_getsockopt,
1458 .sendmsg = rose_sendmsg,
1459 .recvmsg = rose_recvmsg,
1460 .mmap = sock_no_mmap,
1461 .sendpage = sock_no_sendpage,
1464 static struct notifier_block rose_dev_notifier = {
1465 .notifier_call = rose_device_event,
1468 static struct net_device **dev_rose;
1470 static const char banner[] = KERN_INFO "F6FBB/G4KLX ROSE for Linux. Version 0.62 for AX25.037 Linux 2.4\n";
1472 static int __init rose_proto_init(void)
1474 int i;
1475 int rc;
1477 if (rose_ndevs > 0x7FFFFFFF/sizeof(struct net_device *)) {
1478 printk(KERN_ERR "ROSE: rose_proto_init - rose_ndevs parameter to large\n");
1479 rc = -EINVAL;
1480 goto out;
1483 rc = proto_register(&rose_proto, 0);
1484 if (rc != 0)
1485 goto out;
1487 rose_callsign = null_ax25_address;
1489 dev_rose = kmalloc(rose_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1490 if (dev_rose == NULL) {
1491 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate device structure\n");
1492 rc = -ENOMEM;
1493 goto out_proto_unregister;
1496 memset(dev_rose, 0x00, rose_ndevs * sizeof(struct net_device*));
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(sizeof(struct net_device_stats),
1503 name, rose_setup);
1504 if (!dev) {
1505 printk(KERN_ERR "ROSE: rose_proto_init - unable to allocate memory\n");
1506 rc = -ENOMEM;
1507 goto fail;
1509 rc = register_netdev(dev);
1510 if (rc) {
1511 printk(KERN_ERR "ROSE: netdevice registration failed\n");
1512 free_netdev(dev);
1513 goto fail;
1515 dev_rose[i] = dev;
1518 sock_register(&rose_family_ops);
1519 register_netdevice_notifier(&rose_dev_notifier);
1520 printk(banner);
1522 ax25_protocol_register(AX25_P_ROSE, rose_route_frame);
1523 ax25_linkfail_register(rose_link_failed);
1525 #ifdef CONFIG_SYSCTL
1526 rose_register_sysctl();
1527 #endif
1528 rose_loopback_init();
1530 rose_add_loopback_neigh();
1532 proc_net_fops_create("rose", S_IRUGO, &rose_info_fops);
1533 proc_net_fops_create("rose_neigh", S_IRUGO, &rose_neigh_fops);
1534 proc_net_fops_create("rose_nodes", S_IRUGO, &rose_nodes_fops);
1535 proc_net_fops_create("rose_routes", S_IRUGO, &rose_routes_fops);
1536 out:
1537 return rc;
1538 fail:
1539 while (--i >= 0) {
1540 unregister_netdev(dev_rose[i]);
1541 free_netdev(dev_rose[i]);
1543 kfree(dev_rose);
1544 out_proto_unregister:
1545 proto_unregister(&rose_proto);
1546 goto out;
1548 module_init(rose_proto_init);
1550 module_param(rose_ndevs, int, 0);
1551 MODULE_PARM_DESC(rose_ndevs, "number of ROSE devices");
1553 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1554 MODULE_DESCRIPTION("The amateur radio ROSE network layer protocol");
1555 MODULE_LICENSE("GPL");
1556 MODULE_ALIAS_NETPROTO(PF_ROSE);
1558 static void __exit rose_exit(void)
1560 int i;
1562 proc_net_remove("rose");
1563 proc_net_remove("rose_neigh");
1564 proc_net_remove("rose_nodes");
1565 proc_net_remove("rose_routes");
1566 rose_loopback_clear();
1568 rose_rt_free();
1570 ax25_protocol_release(AX25_P_ROSE);
1571 ax25_linkfail_release(rose_link_failed);
1573 if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
1574 ax25_listen_release(&rose_callsign, NULL);
1576 #ifdef CONFIG_SYSCTL
1577 rose_unregister_sysctl();
1578 #endif
1579 unregister_netdevice_notifier(&rose_dev_notifier);
1581 sock_unregister(PF_ROSE);
1583 for (i = 0; i < rose_ndevs; i++) {
1584 struct net_device *dev = dev_rose[i];
1586 if (dev) {
1587 unregister_netdev(dev);
1588 free_netdev(dev);
1592 kfree(dev_rose);
1593 proto_unregister(&rose_proto);
1596 module_exit(rose_exit);