~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netrom/af_netrom.c

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.14 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.57 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.138 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.193 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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.
  6  *
  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)
 10  */
 11 #include <linux/config.h>
 12 #include <linux/module.h>
 13 #include <linux/errno.h>
 14 #include <linux/types.h>
 15 #include <linux/socket.h>
 16 #include <linux/in.h>
 17 #include <linux/kernel.h>
 18 #include <linux/sched.h>
 19 #include <linux/timer.h>
 20 #include <linux/string.h>
 21 #include <linux/sockios.h>
 22 #include <linux/net.h>
 23 #include <linux/stat.h>
 24 #include <net/ax25.h>
 25 #include <linux/inet.h>
 26 #include <linux/netdevice.h>
 27 #include <linux/if_arp.h>
 28 #include <linux/skbuff.h>
 29 #include <net/sock.h>
 30 #include <asm/uaccess.h>
 31 #include <asm/system.h>
 32 #include <linux/fcntl.h>
 33 #include <linux/termios.h>      /* For TIOCINQ/OUTQ */
 34 #include <linux/mm.h>
 35 #include <linux/interrupt.h>
 36 #include <linux/notifier.h>
 37 #include <net/netrom.h>
 38 #include <linux/proc_fs.h>
 39 #include <linux/seq_file.h>
 40 #include <net/ip.h>
 41 #include <net/tcp.h>
 42 #include <net/arp.h>
 43 #include <linux/init.h>
 44 
 45 int nr_ndevs = 4;
 46 
 47 int sysctl_netrom_default_path_quality            = NR_DEFAULT_QUAL;
 48 int sysctl_netrom_obsolescence_count_initialiser  = NR_DEFAULT_OBS;
 49 int sysctl_netrom_network_ttl_initialiser         = NR_DEFAULT_TTL;
 50 int sysctl_netrom_transport_timeout               = NR_DEFAULT_T1;
 51 int sysctl_netrom_transport_maximum_tries         = NR_DEFAULT_N2;
 52 int sysctl_netrom_transport_acknowledge_delay     = NR_DEFAULT_T2;
 53 int sysctl_netrom_transport_busy_delay            = NR_DEFAULT_T4;
 54 int sysctl_netrom_transport_requested_window_size = NR_DEFAULT_WINDOW;
 55 int sysctl_netrom_transport_no_activity_timeout   = NR_DEFAULT_IDLE;
 56 int sysctl_netrom_routing_control                 = NR_DEFAULT_ROUTING;
 57 int sysctl_netrom_link_fails_count                = NR_DEFAULT_FAILS;
 58 
 59 static unsigned short circuit = 0x101;
 60 
 61 static HLIST_HEAD(nr_list);
 62 static spinlock_t nr_list_lock = SPIN_LOCK_UNLOCKED;
 63 
 64 static struct proto_ops nr_proto_ops;
 65 void nr_init_timers(struct sock *sk);
 66 
 67 static struct sock *nr_alloc_sock(void)
 68 {
 69         nr_cb *nr;
 70         struct sock *sk = sk_alloc(PF_NETROM, GFP_ATOMIC, 1, NULL);
 71 
 72         if (!sk)
 73                 goto out;
 74 
 75         nr = nr_sk(sk) = kmalloc(sizeof(*nr), GFP_ATOMIC);
 76         if (!nr)
 77                 goto frees;
 78 
 79         memset(nr, 0x00, sizeof(*nr));
 80         nr->sk = sk;
 81 out:
 82         return sk;
 83 frees:
 84         sk_free(sk);
 85         sk = NULL;
 86         goto out;
 87 }
 88 
 89 /*
 90  *      Socket removal during an interrupt is now safe.
 91  */
 92 static void nr_remove_socket(struct sock *sk)
 93 {
 94         spin_lock_bh(&nr_list_lock);
 95         sk_del_node_init(sk);
 96         spin_unlock_bh(&nr_list_lock);
 97 }
 98 
 99 /*
100  *      Kill all bound sockets on a dropped device.
101  */
102 static void nr_kill_by_device(struct net_device *dev)
103 {
104         struct sock *s;
105         struct hlist_node *node;
106 
107         spin_lock_bh(&nr_list_lock);
108         sk_for_each(s, node, &nr_list)
109                 if (nr_sk(s)->device == dev)
110                         nr_disconnect(s, ENETUNREACH);
111         spin_unlock_bh(&nr_list_lock);
112 }
113 
114 /*
115  *      Handle device status changes.
116  */
117 static int nr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
118 {
119         struct net_device *dev = (struct net_device *)ptr;
120 
121         if (event != NETDEV_DOWN)
122                 return NOTIFY_DONE;
123 
124         nr_kill_by_device(dev);
125         nr_rt_device_down(dev);
126         
127         return NOTIFY_DONE;
128 }
129 
130 /*
131  *      Add a socket to the bound sockets list.
132  */
133 static void nr_insert_socket(struct sock *sk)
134 {
135         spin_lock_bh(&nr_list_lock);
136         sk_add_node(sk, &nr_list);
137         spin_unlock_bh(&nr_list_lock);
138 }
139 
140 /*
141  *      Find a socket that wants to accept the Connect Request we just
142  *      received.
143  */
144 static struct sock *nr_find_listener(ax25_address *addr)
145 {
146         struct sock *s;
147         struct hlist_node *node;
148 
149         spin_lock_bh(&nr_list_lock);
150         sk_for_each(s, node, &nr_list)
151                 if (!ax25cmp(&nr_sk(s)->source_addr, addr) &&
152                     s->sk_state == TCP_LISTEN) {
153                         bh_lock_sock(s);
154                         goto found;
155                 }
156         s = NULL;
157 found:
158         spin_unlock_bh(&nr_list_lock);
159         return s;
160 }
161 
162 /*
163  *      Find a connected NET/ROM socket given my circuit IDs.
164  */
165 static struct sock *nr_find_socket(unsigned char index, unsigned char id)
166 {
167         struct sock *s;
168         struct hlist_node *node;
169 
170         spin_lock_bh(&nr_list_lock);
171         sk_for_each(s, node, &nr_list) {
172                 nr_cb *nr = nr_sk(s);
173                 
174                 if (nr->my_index == index && nr->my_id == id) {
175                         bh_lock_sock(s);
176                         goto found;
177                 }
178         }
179         s = NULL;
180 found:
181         spin_unlock_bh(&nr_list_lock);
182         return s;
183 }
184 
185 /*
186  *      Find a connected NET/ROM socket given their circuit IDs.
187  */
188 static struct sock *nr_find_peer(unsigned char index, unsigned char id,
189         ax25_address *dest)
190 {
191         struct sock *s;
192         struct hlist_node *node;
193 
194         spin_lock_bh(&nr_list_lock);
195         sk_for_each(s, node, &nr_list) {
196                 nr_cb *nr = nr_sk(s);
197                 
198                 if (nr->your_index == index && nr->your_id == id &&
199                     !ax25cmp(&nr->dest_addr, dest)) {
200                         bh_lock_sock(s);
201                         goto found;
202                 }
203         }
204         s = NULL;
205 found:
206         spin_unlock_bh(&nr_list_lock);
207         return s;
208 }
209 
210 /*
211  *      Find next free circuit ID.
212  */
213 static unsigned short nr_find_next_circuit(void)
214 {
215         unsigned short id = circuit;
216         unsigned char i, j;
217         struct sock *sk;
218 
219         for (;;) {
220                 i = id / 256;
221                 j = id % 256;
222 
223                 if (i != 0 && j != 0) {
224                         if ((sk=nr_find_socket(i, j)) == NULL)
225                                 break;
226                         bh_unlock_sock(sk);
227                 }
228 
229                 id++;
230         }
231 
232         return id;
233 }
234 
235 /*
236  *      Deferred destroy.
237  */
238 void nr_destroy_socket(struct sock *);
239 
240 /*
241  *      Handler for deferred kills.
242  */
243 static void nr_destroy_timer(unsigned long data)
244 {
245         struct sock *sk=(struct sock *)data;
246         bh_lock_sock(sk);
247         sock_hold(sk);
248         nr_destroy_socket(sk);
249         bh_unlock_sock(sk);
250         sock_put(sk);
251 }
252 
253 /*
254  *      This is called from user mode and the timers. Thus it protects itself
255  *      against interrupt users but doesn't worry about being called during
256  *      work. Once it is removed from the queue no interrupt or bottom half
257  *      will touch it and we are (fairly 8-) ) safe.
258  */
259 void nr_destroy_socket(struct sock *sk)
260 {
261         struct sk_buff *skb;
262 
263         nr_remove_socket(sk);
264 
265         nr_stop_heartbeat(sk);
266         nr_stop_t1timer(sk);
267         nr_stop_t2timer(sk);
268         nr_stop_t4timer(sk);
269         nr_stop_idletimer(sk);
270 
271         nr_clear_queues(sk);            /* Flush the queues */
272 
273         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
274                 if (skb->sk != sk) { /* A pending connection */
275                         /* Queue the unaccepted socket for death */
276                         sock_set_flag(skb->sk, SOCK_DEAD);
277                         nr_start_heartbeat(skb->sk);
278                         nr_sk(skb->sk)->state = NR_STATE_0;
279                 }
280 
281                 kfree_skb(skb);
282         }
283 
284         if (atomic_read(&sk->sk_wmem_alloc) ||
285             atomic_read(&sk->sk_rmem_alloc)) {
286                 /* Defer: outstanding buffers */
287                 sk->sk_timer.function = nr_destroy_timer;
288                 sk->sk_timer.expires  = jiffies + 2 * HZ;
289                 add_timer(&sk->sk_timer);
290         } else
291                 sock_put(sk);
292 }
293 
294 /*
295  *      Handling for system calls applied via the various interfaces to a
296  *      NET/ROM socket object.
297  */
298 
299 static int nr_setsockopt(struct socket *sock, int level, int optname,
300         char *optval, int optlen)
301 {
302         struct sock *sk = sock->sk;
303         nr_cb *nr = nr_sk(sk);
304         int opt;
305 
306         if (level != SOL_NETROM)
307                 return -ENOPROTOOPT;
308 
309         if (optlen < sizeof(int))
310                 return -EINVAL;
311 
312         if (get_user(opt, (int *)optval))
313                 return -EFAULT;
314 
315         switch (optname) {
316         case NETROM_T1:
317                 if (opt < 1)
318                         return -EINVAL;
319                 nr->t1 = opt * HZ;
320                 return 0;
321 
322         case NETROM_T2:
323                 if (opt < 1)
324                         return -EINVAL;
325                 nr->t2 = opt * HZ;
326                 return 0;
327 
328         case NETROM_N2:
329                 if (opt < 1 || opt > 31)
330                         return -EINVAL;
331                 nr->n2 = opt;
332                 return 0;
333 
334         case NETROM_T4:
335                 if (opt < 1)
336                         return -EINVAL;
337                 nr->t4 = opt * HZ;
338                 return 0;
339 
340         case NETROM_IDLE:
341                 if (opt < 0)
342                         return -EINVAL;
343                 nr->idle = opt * 60 * HZ;
344                 return 0;
345 
346         default:
347                 return -ENOPROTOOPT;
348         }
349 }
350 
351 static int nr_getsockopt(struct socket *sock, int level, int optname,
352         char *optval, int *optlen)
353 {
354         struct sock *sk = sock->sk;
355         nr_cb *nr = nr_sk(sk);
356         int val = 0;
357         int len; 
358 
359         if (level != SOL_NETROM)
360                 return -ENOPROTOOPT;
361         
362         if (get_user(len, optlen))
363                 return -EFAULT;
364 
365         if (len < 0)
366                 return -EINVAL;
367                 
368         switch (optname) {
369         case NETROM_T1:
370                 val = nr->t1 / HZ;
371                 break;
372 
373         case NETROM_T2:
374                 val = nr->t2 / HZ;
375                 break;
376 
377         case NETROM_N2:
378                 val = nr->n2;
379                 break;
380 
381         case NETROM_T4:
382                 val = nr->t4 / HZ;
383                 break;
384 
385         case NETROM_IDLE:
386                 val = nr->idle / (60 * HZ);
387                 break;
388 
389         default:
390                 return -ENOPROTOOPT;
391         }
392 
393         len = min_t(unsigned int, len, sizeof(int));
394 
395         if (put_user(len, optlen))
396                 return -EFAULT;
397 
398         return copy_to_user(optval, &val, len) ? -EFAULT : 0;
399 }
400 
401 static int nr_listen(struct socket *sock, int backlog)
402 {
403         struct sock *sk = sock->sk;
404 
405         lock_sock(sk);
406         if (sk->sk_state != TCP_LISTEN) {
407                 memset(&nr_sk(sk)->user_addr, 0, AX25_ADDR_LEN);
408                 sk->sk_max_ack_backlog = backlog;
409                 sk->sk_state           = TCP_LISTEN;
410                 release_sock(sk);
411                 return 0;
412         }
413         release_sock(sk);
414 
415         return -EOPNOTSUPP;
416 }
417 
418 static int nr_create(struct socket *sock, int protocol)
419 {
420         struct sock *sk;
421         nr_cb *nr;
422 
423         if (sock->type != SOCK_SEQPACKET || protocol != 0)
424                 return -ESOCKTNOSUPPORT;
425 
426         if ((sk = nr_alloc_sock()) == NULL)
427                 return -ENOMEM;
428 
429         nr = nr_sk(sk);
430 
431         sock_init_data(sock, sk);
432         sk_set_owner(sk, THIS_MODULE);
433 
434         sock->ops    = &nr_proto_ops;
435         sk->sk_protocol = protocol;
436 
437         skb_queue_head_init(&nr->ack_queue);
438         skb_queue_head_init(&nr->reseq_queue);
439         skb_queue_head_init(&nr->frag_queue);
440 
441         nr_init_timers(sk);
442 
443         nr->t1     = sysctl_netrom_transport_timeout;
444         nr->t2     = sysctl_netrom_transport_acknowledge_delay;
445         nr->n2     = sysctl_netrom_transport_maximum_tries;
446         nr->t4     = sysctl_netrom_transport_busy_delay;
447         nr->idle   = sysctl_netrom_transport_no_activity_timeout;
448         nr->window = sysctl_netrom_transport_requested_window_size;
449 
450         nr->bpqext = 1;
451         nr->state  = NR_STATE_0;
452 
453         return 0;
454 }
455 
456 static struct sock *nr_make_new(struct sock *osk)
457 {
458         struct sock *sk;
459         nr_cb *nr, *onr;
460 
461         if (osk->sk_type != SOCK_SEQPACKET)
462                 return NULL;
463 
464         if ((sk = nr_alloc_sock()) == NULL)
465                 return NULL;
466 
467         nr = nr_sk(sk);
468 
469         sock_init_data(NULL, sk);
470         sk_set_owner(sk, THIS_MODULE);
471 
472         sk->sk_type     = osk->sk_type;
473         sk->sk_socket   = osk->sk_socket;
474         sk->sk_priority = osk->sk_priority;
475         sk->sk_protocol = osk->sk_protocol;
476         sk->sk_rcvbuf   = osk->sk_rcvbuf;
477         sk->sk_sndbuf   = osk->sk_sndbuf;
478         sk->sk_debug    = osk->sk_debug;
479         sk->sk_state    = TCP_ESTABLISHED;
480         sk->sk_sleep    = osk->sk_sleep;
481         sk->sk_zapped   = osk->sk_zapped;
482 
483         skb_queue_head_init(&nr->ack_queue);
484         skb_queue_head_init(&nr->reseq_queue);
485         skb_queue_head_init(&nr->frag_queue);
486 
487         nr_init_timers(sk);
488 
489         onr = nr_sk(osk);
490 
491         nr->t1      = onr->t1;
492         nr->t2      = onr->t2;
493         nr->n2      = onr->n2;
494         nr->t4      = onr->t4;
495         nr->idle    = onr->idle;
496         nr->window  = onr->window;
497 
498         nr->device  = onr->device;
499         nr->bpqext  = onr->bpqext;
500 
501         return sk;
502 }
503 
504 static int nr_release(struct socket *sock)
505 {
506         struct sock *sk = sock->sk;
507         nr_cb *nr;
508 
509         if (sk == NULL) return 0;
510 
511         sock_hold(sk);
512         lock_sock(sk);
513         nr = nr_sk(sk);
514 
515         switch (nr->state) {
516         case NR_STATE_0:
517         case NR_STATE_1:
518         case NR_STATE_2:
519                 nr_disconnect(sk, 0);
520                 nr_destroy_socket(sk);
521                 break;
522 
523         case NR_STATE_3:
524                 nr_clear_queues(sk);
525                 nr->n2count = 0;
526                 nr_write_internal(sk, NR_DISCREQ);
527                 nr_start_t1timer(sk);
528                 nr_stop_t2timer(sk);
529                 nr_stop_t4timer(sk);
530                 nr_stop_idletimer(sk);
531                 nr->state    = NR_STATE_2;
532                 sk->sk_state    = TCP_CLOSE;
533                 sk->sk_shutdown |= SEND_SHUTDOWN;
534                 sk->sk_state_change(sk);
535                 sock_set_flag(sk, SOCK_DEAD);
536                 sock_set_flag(sk, SOCK_DESTROY);
537                 sk->sk_socket   = NULL;
538                 break;
539 
540         default:
541                 sk->sk_socket = NULL;
542                 break;
543         }
544 
545         sock->sk   = NULL;      
546         release_sock(sk);
547         sock_put(sk);
548 
549         return 0;
550 }
551 
552 static int nr_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
553 {
554         struct sock *sk = sock->sk;
555         nr_cb *nr = nr_sk(sk);
556         struct full_sockaddr_ax25 *addr = (struct full_sockaddr_ax25 *)uaddr;
557         struct net_device *dev;
558         ax25_address *user, *source;
559 
560         lock_sock(sk);
561         if (!sk->sk_zapped) {
562                 release_sock(sk);
563                 return -EINVAL;
564         }
565         if (addr_len < sizeof(struct sockaddr_ax25) || addr_len > sizeof(struct full_sockaddr_ax25)) {
566                 release_sock(sk);
567                 return -EINVAL;
568         }
569         if (addr_len < (addr->fsa_ax25.sax25_ndigis * sizeof(ax25_address) + sizeof(struct sockaddr_ax25))) {
570                 release_sock(sk);
571                 return -EINVAL;
572         }
573         if (addr->fsa_ax25.sax25_family != AF_NETROM) {
574                 release_sock(sk);
575                 return -EINVAL;
576         }
577         if ((dev = nr_dev_get(&addr->fsa_ax25.sax25_call)) == NULL) {
578                 SOCK_DEBUG(sk, "NET/ROM: bind failed: invalid node callsign\n");
579                 release_sock(sk);
580                 return -EADDRNOTAVAIL;
581         }
582 
583         /*
584          * Only the super user can set an arbitrary user callsign.
585          */
586         if (addr->fsa_ax25.sax25_ndigis == 1) {
587                 if (!capable(CAP_NET_BIND_SERVICE)) {
588                         dev_put(dev);
589                         release_sock(sk);
590                         return -EACCES;
591                 }
592                 nr->user_addr   = addr->fsa_digipeater[0];
593                 nr->source_addr = addr->fsa_ax25.sax25_call;
594         } else {
595                 source = &addr->fsa_ax25.sax25_call;
596 
597                 if ((user = ax25_findbyuid(current->euid)) == NULL) {
598                         if (ax25_uid_policy && !capable(CAP_NET_BIND_SERVICE)) {
599                                 release_sock(sk);
600                                 dev_put(dev);
601                                 return -EPERM;
602                         }
603                         user = source;
604                 }
605 
606                 nr->user_addr   = *user;
607                 nr->source_addr = *source;
608         }
609 
610         nr->device = dev;
611         nr_insert_socket(sk);
612 
613         sk->sk_zapped = 0;
614         dev_put(dev);
615         release_sock(sk);
616         SOCK_DEBUG(sk, "NET/ROM: socket is bound\n");
617         return 0;
618 }
619 
620 static int nr_connect(struct socket *sock, struct sockaddr *uaddr,
621         int addr_len, int flags)
622 {
623         struct sock *sk = sock->sk;
624         nr_cb *nr = nr_sk(sk);
625         struct sockaddr_ax25 *addr = (struct sockaddr_ax25 *)uaddr;
626         ax25_address *user, *source = NULL;
627         struct net_device *dev;
628 
629         lock_sock(sk);
630         if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
631                 sock->state = SS_CONNECTED;
632                 release_sock(sk);
633                 return 0;       /* Connect completed during a ERESTARTSYS event */
634         }
635 
636         if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
637                 sock->state = SS_UNCONNECTED;
638                 release_sock(sk);
639                 return -ECONNREFUSED;
640         }
641 
642         if (sk->sk_state == TCP_ESTABLISHED) {
643                 release_sock(sk);
644                 return -EISCONN;        /* No reconnect on a seqpacket socket */
645         }
646 
647         sk->sk_state   = TCP_CLOSE;     
648         sock->state = SS_UNCONNECTED;
649 
650         if (addr_len != sizeof(struct sockaddr_ax25) && addr_len != sizeof(struct full_sockaddr_ax25)) {
651                 release_sock(sk);
652                 return -EINVAL;
653         }
654         if (addr->sax25_family != AF_NETROM) {
655                 release_sock(sk);
656                 return -EINVAL;
657         }
658         if (sk->sk_zapped) {    /* Must bind first - autobinding in this may or may not work */
659                 sk->sk_zapped = 0;
660 
661                 if ((dev = nr_dev_first()) == NULL) {
662                         release_sock(sk);
663                         return -ENETUNREACH;
664                 }
665                 source = (ax25_address *)dev->dev_addr;
666 
667                 if ((user = ax25_findbyuid(current->euid)) == NULL) {
668                         if (ax25_uid_policy && !capable(CAP_NET_ADMIN)) {
669                                 dev_put(dev);
670                                 release_sock(sk);
671                                 return -EPERM;
672                         }
673                         user = source;
674                 }
675 
676                 nr->user_addr   = *user;
677                 nr->source_addr = *source;
678                 nr->device      = dev;
679 
680                 dev_put(dev);
681                 nr_insert_socket(sk);           /* Finish the bind */
682         }
683 
684         nr->dest_addr = addr->sax25_call;
685 
686         release_sock(sk);
687         circuit = nr_find_next_circuit();
688         lock_sock(sk);
689 
690         nr->my_index = circuit / 256;
691         nr->my_id    = circuit % 256;
692 
693         circuit++;
694 
695         /* Move to connecting socket, start sending Connect Requests */
696         sock->state  = SS_CONNECTING;
697         sk->sk_state = TCP_SYN_SENT;
698 
699         nr_establish_data_link(sk);
700 
701         nr->state = NR_STATE_1;
702 
703         nr_start_heartbeat(sk);
704 
705         /* Now the loop */
706         if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) {
707                 release_sock(sk);
708                 return -EINPROGRESS;
709         }
710                 
711         /*
712          * A Connect Ack with Choke or timeout or failed routing will go to
713          * closed.
714          */
715         if (sk->sk_state == TCP_SYN_SENT) {
716                 struct task_struct *tsk = current;
717                 DECLARE_WAITQUEUE(wait, tsk);
718 
719                 add_wait_queue(sk->sk_sleep, &wait);
720                 for (;;) {
721                         set_current_state(TASK_INTERRUPTIBLE);
722                         if (sk->sk_state != TCP_SYN_SENT)
723                                 break;
724                         release_sock(sk);
725                         if (!signal_pending(tsk)) {
726                                 schedule();
727                                 lock_sock(sk);
728                                 continue;
729                         }
730                         return -ERESTARTSYS;
731                 }
732                 current->state = TASK_RUNNING;
733                 remove_wait_queue(sk->sk_sleep, &wait);
734         }
735 
736         if (sk->sk_state != TCP_ESTABLISHED) {
737                 sock->state = SS_UNCONNECTED;
738                 release_sock(sk);
739                 return sock_error(sk);  /* Always set at this point */
740         }
741 
742         sock->state = SS_CONNECTED;
743         release_sock(sk);
744 
745         return 0;
746 }
747 
748 static int nr_accept(struct socket *sock, struct socket *newsock, int flags)
749 {
750         struct task_struct *tsk = current;
751         DECLARE_WAITQUEUE(wait, tsk);
752         struct sk_buff *skb;
753         struct sock *newsk;
754         struct sock *sk;
755         int err = 0;
756 
757         if ((sk = sock->sk) == NULL)
758                 return -EINVAL;
759 
760         lock_sock(sk);
761         if (sk->sk_type != SOCK_SEQPACKET) {
762                 err = -EOPNOTSUPP;
763                 goto out;
764         }
765 
766         if (sk->sk_state != TCP_LISTEN) {
767                 err = -EINVAL;
768                 goto out;
769         }
770 
771         /*
772          *      The write queue this time is holding sockets ready to use
773          *      hooked into the SABM we saved
774          */
775         add_wait_queue(sk->sk_sleep, &wait);
776         for (;;) {
777                 skb = skb_dequeue(&sk->sk_receive_queue);
778                 if (skb)
779                         break;
780 
781                 current->state = TASK_INTERRUPTIBLE;
782                 release_sock(sk);
783                 if (flags & O_NONBLOCK)
784                         return -EWOULDBLOCK;
785                 if (!signal_pending(tsk)) {
786                         schedule();
787                         lock_sock(sk);
788                         continue;
789                 }
790                 return -ERESTARTSYS;
791         }
792         current->state = TASK_RUNNING;
793         remove_wait_queue(sk->sk_sleep, &wait);
794 
795         newsk = skb->sk;
796         newsk->sk_pair = NULL;
797         newsk->sk_socket = newsock;
798         newsk->sk_sleep = &newsock->wait;
799 
800         /* Now attach up the new socket */
801         kfree_skb(skb);
802         sk->sk_ack_backlog--;
803         newsock->sk = newsk;
804 
805 out:
806         release_sock(sk);
807         return err;
808 }
809 
810 static int nr_getname(struct socket *sock, struct sockaddr *uaddr,
811         int *uaddr_len, int peer)
812 {
813         struct full_sockaddr_ax25 *sax = (struct full_sockaddr_ax25 *)uaddr;
814         struct sock *sk = sock->sk;
815         nr_cb *nr = nr_sk(sk);
816 
817         lock_sock(sk);
818         if (peer != 0) {
819                 if (sk->sk_state != TCP_ESTABLISHED) {
820                         release_sock(sk);
821                         return -ENOTCONN;
822                 }
823                 sax->fsa_ax25.sax25_family = AF_NETROM;
824                 sax->fsa_ax25.sax25_ndigis = 1;
825                 sax->fsa_ax25.sax25_call   = nr->user_addr;
826                 sax->fsa_digipeater[0]     = nr->dest_addr;
827                 *uaddr_len = sizeof(struct full_sockaddr_ax25);
828         } else {
829                 sax->fsa_ax25.sax25_family = AF_NETROM;
830                 sax->fsa_ax25.sax25_ndigis = 0;
831                 sax->fsa_ax25.sax25_call   = nr->source_addr;
832                 *uaddr_len = sizeof(struct sockaddr_ax25);
833         }
834         release_sock(sk);
835 
836         return 0;
837 }
838 
839 int nr_rx_frame(struct sk_buff *skb, struct net_device *dev)
840 {
841         struct sock *sk;
842         struct sock *make;      
843         nr_cb *nr_make;
844         ax25_address *src, *dest, *user;
845         unsigned short circuit_index, circuit_id;
846         unsigned short peer_circuit_index, peer_circuit_id;
847         unsigned short frametype, flags, window, timeout;
848         int ret;
849 
850         skb->sk = NULL;         /* Initially we don't know who it's for */
851 
852         /*
853          *      skb->data points to the netrom frame start
854          */
855 
856         src  = (ax25_address *)(skb->data + 0);
857         dest = (ax25_address *)(skb->data + 7);
858 
859         circuit_index      = skb->data[15];
860         circuit_id         = skb->data[16];
861         peer_circuit_index = skb->data[17];
862         peer_circuit_id    = skb->data[18];
863         frametype          = skb->data[19] & 0x0F;
864         flags              = skb->data[19] & 0xF0;
865 
866 #ifdef CONFIG_INET
867         /*
868          * Check for an incoming IP over NET/ROM frame.
869          */
870         if (frametype == NR_PROTOEXT && circuit_index == NR_PROTO_IP && circuit_id == NR_PROTO_IP) {
871                 skb_pull(skb, NR_NETWORK_LEN + NR_TRANSPORT_LEN);
872                 skb->h.raw = skb->data;
873 
874                 return nr_rx_ip(skb, dev);
875         }
876 #endif
877 
878         /*
879          * Find an existing socket connection, based on circuit ID, if it's
880          * a Connect Request base it on their circuit ID.
881          *
882          * Circuit ID 0/0 is not valid but it could still be a "reset" for a
883          * circuit that no longer exists at the other end ...
884          */
885 
886         sk = NULL;
887 
888         if (circuit_index == 0 && circuit_id == 0) {
889                 if (frametype == NR_CONNACK && flags == NR_CHOKE_FLAG)
890                         sk = nr_find_peer(peer_circuit_index, peer_circuit_id, src);
891         } else {
892                 if (frametype == NR_CONNREQ)
893                         sk = nr_find_peer(circuit_index, circuit_id, src);
894                 else
895                         sk = nr_find_socket(circuit_index, circuit_id);
896         }
897 
898         if (sk != NULL) {
899                 skb->h.raw = skb->data;
900 
901                 if (frametype == NR_CONNACK && skb->len == 22)
902                         nr_sk(sk)->bpqext = 1;
903                 else
904                         nr_sk(sk)->bpqext = 0;
905 
906                 ret = nr_process_rx_frame(sk, skb);
907                 bh_unlock_sock(sk);
908                 return ret;
909         }
910 
911         /*
912          * Now it should be a CONNREQ.
913          */
914         if (frametype != NR_CONNREQ) {
915                 /*
916                  * Here it would be nice to be able to send a reset but
917                  * NET/ROM doesn't have one. The following hack would
918                  * have been a way to extend the protocol but apparently
919                  * it kills BPQ boxes... :-(
920                  */
921 #if 0
922                 /*
923                  * Never reply to a CONNACK/CHOKE.
924                  */
925                 if (frametype != NR_CONNACK || flags != NR_CHOKE_FLAG)
926                         nr_transmit_refusal(skb, 1);
927 #endif
928                 return 0;
929         }
930 
931         sk = nr_find_listener(dest);
932 
933         user = (ax25_address *)(skb->data + 21);
934 
935         if (!sk || sk->sk_ack_backlog == sk->sk_max_ack_backlog ||
936             (make = nr_make_new(sk)) == NULL) {
937                 nr_transmit_refusal(skb, 0);
938                 if (sk)
939                         bh_unlock_sock(sk);
940                 return 0;
941         }
942 
943         window = skb->data[20];
944 
945         skb->sk             = make;
946         make->sk_state      = TCP_ESTABLISHED;
947 
948         /* Fill in his circuit details */
949         nr_make = nr_sk(make);
950         nr_make->source_addr = *dest;
951         nr_make->dest_addr   = *src;
952         nr_make->user_addr   = *user;
953 
954         nr_make->your_index  = circuit_index;
955         nr_make->your_id     = circuit_id;
956 
957         bh_unlock_sock(sk);
958         circuit = nr_find_next_circuit();
959         bh_lock_sock(sk);
960 
961         nr_make->my_index    = circuit / 256;
962         nr_make->my_id       = circuit % 256;
963 
964         circuit++;
965 
966         /* Window negotiation */
967         if (window < nr_make->window)
968                 nr_make->window = window;
969 
970         /* L4 timeout negotiation */
971         if (skb->len == 37) {
972                 timeout = skb->data[36] * 256 + skb->data[35];
973                 if (timeout * HZ < nr_make->t1)
974                         nr_make->t1 = timeout * HZ;
975                 nr_make->bpqext = 1;
976         } else {
977                 nr_make->bpqext = 0;
978         }
979 
980         nr_write_internal(make, NR_CONNACK);
981 
982         nr_make->condition = 0x00;
983         nr_make->vs        = 0;
984         nr_make->va        = 0;
985         nr_make->vr        = 0;
986         nr_make->vl        = 0;
987         nr_make->state     = NR_STATE_3;
988         sk->sk_ack_backlog++;
989         make->sk_pair = sk;
990 
991         nr_insert_socket(make);
992 
993         skb_queue_head(&sk->sk_receive_queue, skb);
994 
995         nr_start_heartbeat(make);
996         nr_start_idletimer(make);
997 
998         if (!sock_flag(sk, SOCK_DEAD))
999                 sk->sk_data_ready(sk, skb->len);
1000 
1001         bh_unlock_sock(sk);
1002         return 1;
1003 }
1004 
1005 static int nr_sendmsg(struct kiocb *iocb, struct socket *sock,
1006                       struct msghdr *msg, int len)
1007 {
1008         struct sock *sk = sock->sk;
1009         nr_cb *nr = nr_sk(sk);
1010         struct sockaddr_ax25 *usax = (struct sockaddr_ax25 *)msg->msg_name;
1011         int err;
1012         struct sockaddr_ax25 sax;
1013         struct sk_buff *skb;
1014         unsigned char *asmptr;
1015         int size;
1016 
1017         if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_EOR))
1018                 return -EINVAL;
1019 
1020         lock_sock(sk);
1021         if (sk->sk_zapped) {
1022                 err = -EADDRNOTAVAIL;
1023                 goto out;
1024         }
1025 
1026         if (sk->sk_shutdown & SEND_SHUTDOWN) {
1027                 send_sig(SIGPIPE, current, 0);
1028                 err = -EPIPE;
1029                 goto out;
1030         }
1031 
1032         if (nr->device == NULL) {
1033                 err = -ENETUNREACH;
1034                 goto out;
1035         }
1036 
1037         if (usax) {
1038                 if (msg->msg_namelen < sizeof(sax)) {
1039                         err = -EINVAL;
1040                         goto out;
1041                 }
1042                 sax = *usax;
1043                 if (ax25cmp(&nr->dest_addr, &sax.sax25_call) != 0) {
1044                         err = -EISCONN;
1045                         goto out;
1046                 }
1047                 if (sax.sax25_family != AF_NETROM) {
1048                         err = -EINVAL;
1049                         goto out;
1050                 }
1051         } else {
1052                 if (sk->sk_state != TCP_ESTABLISHED) {
1053                         err = -ENOTCONN;
1054                         goto out;
1055                 }
1056                 sax.sax25_family = AF_NETROM;
1057                 sax.sax25_call   = nr->dest_addr;
1058         }
1059 
1060         SOCK_DEBUG(sk, "NET/ROM: sendto: Addresses built.\n");
1061 
1062         /* Build a packet */
1063         SOCK_DEBUG(sk, "NET/ROM: sendto: building packet.\n");
1064         size = len + NR_NETWORK_LEN + NR_TRANSPORT_LEN;
1065 
1066         if ((skb = sock_alloc_send_skb(sk, size, msg->msg_flags & MSG_DONTWAIT, &err)) == NULL)
1067                 goto out;
1068 
1069         skb_reserve(skb, size - len);
1070 
1071         /*
1072          *      Push down the NET/ROM header
1073          */
1074 
1075         asmptr = skb_push(skb, NR_TRANSPORT_LEN);
1076         SOCK_DEBUG(sk, "Building NET/ROM Header.\n");
1077 
1078         /* Build a NET/ROM Transport header */
1079 
1080         *asmptr++ = nr->your_index;
1081         *asmptr++ = nr->your_id;
1082         *asmptr++ = 0;          /* To be filled in later */
1083         *asmptr++ = 0;          /*      Ditto            */
1084         *asmptr++ = NR_INFO;
1085         SOCK_DEBUG(sk, "Built header.\n");
1086 
1087         /*
1088          *      Put the data on the end
1089          */
1090 
1091         skb->h.raw = skb_put(skb, len);
1092 
1093         asmptr = skb->h.raw;
1094         SOCK_DEBUG(sk, "NET/ROM: Appending user data\n");
1095 
1096         /* User data follows immediately after the NET/ROM transport header */
1097         memcpy_fromiovec(asmptr, msg->msg_iov, len);
1098         SOCK_DEBUG(sk, "NET/ROM: Transmitting buffer\n");
1099 
1100         if (sk->sk_state != TCP_ESTABLISHED) {
1101                 kfree_skb(skb);
1102                 err = -ENOTCONN;
1103                 goto out;
1104         }
1105 
1106         nr_output(sk, skb);     /* Shove it onto the queue */
1107 
1108         err = len;
1109 out:
1110         release_sock(sk);
1111         return err;
1112 }
1113 
1114 static int nr_recvmsg(struct kiocb *iocb, struct socket *sock,
1115                       struct msghdr *msg, int size, int flags)
1116 {
1117         struct sock *sk = sock->sk;
1118         struct sockaddr_ax25 *sax = (struct sockaddr_ax25 *)msg->msg_name;
1119         int copied;
1120         struct sk_buff *skb;
1121         int er;
1122 
1123         /*
1124          * This works for seqpacket too. The receiver has ordered the queue for
1125          * us! We do one quick check first though
1126          */
1127 
1128         lock_sock(sk);
1129         if (sk->sk_state != TCP_ESTABLISHED) {
1130                 release_sock(sk);
1131                 return -ENOTCONN;
1132         }
1133 
1134         /* Now we can treat all alike */
1135         if ((skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, flags & MSG_DONTWAIT, &er)) == NULL) {
1136                 release_sock(sk);
1137                 return er;
1138         }
1139 
1140         skb->h.raw = skb->data;
1141         copied     = skb->len;
1142 
1143         if (copied > size) {
1144                 copied = size;
1145                 msg->msg_flags |= MSG_TRUNC;
1146         }
1147 
1148         skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1149 
1150         if (sax != NULL) {
1151                 sax->sax25_family = AF_NETROM;
1152                 memcpy(sax->sax25_call.ax25_call, skb->data + 7, AX25_ADDR_LEN);
1153         }
1154 
1155         msg->msg_namelen = sizeof(*sax);
1156 
1157         skb_free_datagram(sk, skb);
1158 
1159         release_sock(sk);
1160         return copied;
1161 }
1162 
1163 
1164 static int nr_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
1165 {
1166         struct sock *sk = sock->sk;
1167         int ret;
1168 
1169         lock_sock(sk);
1170         switch (cmd) {
1171         case TIOCOUTQ: {
1172                 long amount;
1173                 amount = sk->sk_sndbuf - atomic_read(&sk->sk_wmem_alloc);
1174                 if (amount < 0)
1175                         amount = 0;
1176                 release_sock(sk);
1177                 return put_user(amount, (int *)arg);
1178         }
1179 
1180         case TIOCINQ: {
1181                 struct sk_buff *skb;
1182                 long amount = 0L;
1183                 /* These two are safe on a single CPU system as only user tasks fiddle here */
1184                 if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
1185                         amount = skb->len;
1186                 release_sock(sk);
1187                 return put_user(amount, (int *)arg);
1188         }
1189 
1190         case SIOCGSTAMP:
1191                 if (sk != NULL) {
1192                         if (!sk->sk_stamp.tv_sec) {
1193                                 release_sock(sk);
1194                                 return -ENOENT;
1195                         }
1196                         ret = copy_to_user((void *)arg, &sk->sk_stamp, sizeof(struct timeval)) ? -EFAULT : 0;
1197                         release_sock(sk);
1198                         return ret;
1199                 }
1200                 release_sock(sk);
1201                 return -EINVAL;
1202 
1203         case SIOCGIFADDR:
1204         case SIOCSIFADDR:
1205         case SIOCGIFDSTADDR:
1206         case SIOCSIFDSTADDR:
1207         case SIOCGIFBRDADDR:
1208         case SIOCSIFBRDADDR:
1209         case SIOCGIFNETMASK:
1210         case SIOCSIFNETMASK:
1211         case SIOCGIFMETRIC:
1212         case SIOCSIFMETRIC:
1213                 release_sock(sk);
1214                 return -EINVAL;
1215 
1216         case SIOCADDRT:
1217         case SIOCDELRT:
1218         case SIOCNRDECOBS:
1219                 release_sock(sk);
1220                 if (!capable(CAP_NET_ADMIN)) return -EPERM;
1221                 return nr_rt_ioctl(cmd, (void *)arg);
1222 
1223         default:
1224                 release_sock(sk);
1225                 return dev_ioctl(cmd, (void *)arg);
1226         }
1227         release_sock(sk);
1228 
1229         return 0;
1230 }
1231 
1232 #ifdef CONFIG_PROC_FS
1233 
1234 static void *nr_info_start(struct seq_file *seq, loff_t *pos)
1235 {
1236         struct sock *s;
1237         struct hlist_node *node;
1238         int i = 1;
1239 
1240         spin_lock_bh(&nr_list_lock);
1241         if (*pos == 0)
1242                 return SEQ_START_TOKEN;
1243 
1244         sk_for_each(s, node, &nr_list) {
1245                 if (i == *pos)
1246                         return s;
1247                 ++i;
1248         }
1249         return NULL;
1250 }
1251 
1252 static void *nr_info_next(struct seq_file *seq, void *v, loff_t *pos)
1253 {
1254         ++*pos;
1255 
1256         return (v == SEQ_START_TOKEN) ? sk_head(&nr_list) 
1257                 : sk_next((struct sock *)v);
1258 }
1259         
1260 static void nr_info_stop(struct seq_file *seq, void *v)
1261 {
1262         spin_unlock_bh(&nr_list_lock);
1263 }
1264 
1265 static int nr_info_show(struct seq_file *seq, void *v)
1266 {
1267         struct sock *s = v;
1268         struct net_device *dev;
1269         nr_cb *nr;
1270         const char *devname;
1271 
1272         if (v == SEQ_START_TOKEN)
1273                 seq_puts(seq,
1274 "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");
1275 
1276         else {
1277 
1278                 bh_lock_sock(s);
1279                 nr = nr_sk(s);
1280 
1281                 if ((dev = nr->device) == NULL)
1282                         devname = "???";
1283                 else
1284                         devname = dev->name;
1285 
1286                 seq_printf(seq, "%-9s ", ax2asc(&nr->user_addr));
1287                 seq_printf(seq, "%-9s ", ax2asc(&nr->dest_addr));
1288                 seq_printf(seq, 
1289 "%-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",
1290                         ax2asc(&nr->source_addr),
1291                         devname,
1292                         nr->my_index,
1293                         nr->my_id,
1294                         nr->your_index,
1295                         nr->your_id,
1296                         nr->state,
1297                         nr->vs,
1298                         nr->vr,
1299                         nr->va,
1300                         ax25_display_timer(&nr->t1timer) / HZ,
1301                         nr->t1 / HZ,
1302                         ax25_display_timer(&nr->t2timer) / HZ,
1303                         nr->t2 / HZ,
1304                         ax25_display_timer(&nr->t4timer) / HZ,
1305                         nr->t4 / HZ,
1306                         ax25_display_timer(&nr->idletimer) / (60 * HZ),
1307                         nr->idle / (60 * HZ),
1308                         nr->n2count,
1309                         nr->n2,
1310                         nr->window,
1311                         atomic_read(&s->sk_wmem_alloc),
1312                         atomic_read(&s->sk_rmem_alloc),
1313                         s->sk_socket ? SOCK_INODE(s->sk_socket)->i_ino : 0L);
1314 
1315                 bh_unlock_sock(s);
1316         }
1317         return 0;
1318 }
1319 
1320 static struct seq_operations nr_info_seqops = {
1321         .start = nr_info_start,
1322         .next = nr_info_next,
1323         .stop = nr_info_stop,
1324         .show = nr_info_show,
1325 };
1326  
1327 static int nr_info_open(struct inode *inode, struct file *file)
1328 {
1329         return seq_open(file, &nr_info_seqops);
1330 }
1331  
1332 static struct file_operations nr_info_fops = {
1333         .owner = THIS_MODULE,
1334         .open = nr_info_open,
1335         .read = seq_read,
1336         .llseek = seq_lseek,
1337         .release = seq_release,
1338 };
1339 #endif  /* CONFIG_PROC_FS */
1340 
1341 static struct net_proto_family nr_family_ops = {
1342         .family         =       PF_NETROM,
1343         .create         =       nr_create,
1344         .owner          =       THIS_MODULE,
1345 };
1346 
1347 static struct proto_ops nr_proto_ops = {
1348         .family         =       PF_NETROM,
1349         .owner          =       THIS_MODULE,
1350         .release        =       nr_release,
1351         .bind           =       nr_bind,
1352         .connect        =       nr_connect,
1353         .socketpair     =       sock_no_socketpair,
1354         .accept         =       nr_accept,
1355         .getname        =       nr_getname,
1356         .poll           =       datagram_poll,
1357         .ioctl          =       nr_ioctl,
1358         .listen         =       nr_listen,
1359         .shutdown       =       sock_no_shutdown,
1360         .setsockopt     =       nr_setsockopt,
1361         .getsockopt     =       nr_getsockopt,
1362         .sendmsg        =       nr_sendmsg,
1363         .recvmsg        =       nr_recvmsg,
1364         .mmap           =       sock_no_mmap,
1365         .sendpage       =       sock_no_sendpage,
1366 };
1367 
1368 static struct notifier_block nr_dev_notifier = {
1369         .notifier_call  =       nr_device_event,
1370 };
1371 
1372 static struct net_device **dev_nr;
1373 
1374 static char banner[] __initdata = KERN_INFO "G4KLX NET/ROM for Linux. Version 0.7 for AX25.037 Linux 2.4\n";
1375 
1376 static int __init nr_proto_init(void)
1377 {
1378         int i;
1379 
1380         if (nr_ndevs > 0x7fffffff/sizeof(struct net_device)) {
1381                 printk(KERN_ERR "NET/ROM: nr_proto_init - nr_ndevs parameter to large\n");
1382                 return -1;
1383         }
1384 
1385         dev_nr = kmalloc(nr_ndevs * sizeof(struct net_device *), GFP_KERNEL);
1386         if (dev_nr == NULL) {
1387                 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device array\n");
1388                 return -1;
1389         }
1390 
1391         memset(dev_nr, 0x00, nr_ndevs * sizeof(struct net_device *));
1392 
1393         for (i = 0; i < nr_ndevs; i++) {
1394                 char name[IFNAMSIZ];
1395                 struct net_device *dev;
1396 
1397                 sprintf(name, "nr%d", i);
1398                 dev = alloc_netdev(sizeof(struct net_device_stats), name,
1399                                           nr_setup);
1400                 if (!dev) {
1401                         printk(KERN_ERR "NET/ROM: nr_proto_init - unable to allocate device structure\n");
1402                         goto fail;
1403                 }
1404                 
1405                 dev->base_addr = i;
1406                 if (register_netdev(dev)) {
1407                         printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register network device\n");
1408                         goto fail;
1409                 }
1410                 dev_nr[i] = dev;
1411         }
1412 
1413         if (sock_register(&nr_family_ops)) {
1414                 printk(KERN_ERR "NET/ROM: nr_proto_init - unable to register socket family\n");
1415                 goto fail;
1416         }
1417                 
1418         register_netdevice_notifier(&nr_dev_notifier);
1419         printk(banner);
1420 
1421         ax25_protocol_register(AX25_P_NETROM, nr_route_frame);
1422         ax25_linkfail_register(nr_link_failed);
1423 
1424 #ifdef CONFIG_SYSCTL
1425         nr_register_sysctl();
1426 #endif
1427 
1428         nr_loopback_init();
1429 
1430         proc_net_fops_create("nr", S_IRUGO, &nr_info_fops);
1431         proc_net_fops_create("nr_neigh", S_IRUGO, &nr_neigh_fops);
1432         proc_net_fops_create("nr_nodes", S_IRUGO, &nr_nodes_fops);
1433         return 0;
1434 
1435  fail:
1436         while (--i >= 0)
1437                 unregister_netdev(dev_nr[i]);
1438         kfree(dev_nr);
1439         return -1;
1440 }
1441 
1442 module_init(nr_proto_init);
1443 
1444 
1445 MODULE_PARM(nr_ndevs, "i");
1446 MODULE_PARM_DESC(nr_ndevs, "number of NET/ROM devices");
1447 
1448 MODULE_AUTHOR("Jonathan Naylor G4KLX <g4klx@g4klx.demon.co.uk>");
1449 MODULE_DESCRIPTION("The amateur radio NET/ROM network and transport layer protocol");
1450 MODULE_LICENSE("GPL");
1451 MODULE_ALIAS_NETPROTO(PF_NETROM);
1452 
1453 static void __exit nr_exit(void)
1454 {
1455         int i;
1456 
1457         proc_net_remove("nr");
1458         proc_net_remove("nr_neigh");
1459         proc_net_remove("nr_nodes");
1460         nr_loopback_clear();
1461 
1462         nr_rt_free();
1463 
1464 #ifdef CONFIG_SYSCTL
1465         nr_unregister_sysctl();
1466 #endif
1467 
1468         ax25_linkfail_release(nr_link_failed);
1469         ax25_protocol_release(AX25_P_NETROM);
1470 
1471         unregister_netdevice_notifier(&nr_dev_notifier);
1472 
1473         sock_unregister(PF_NETROM);
1474 
1475         for (i = 0; i < nr_ndevs; i++) {
1476                 struct net_device *dev = dev_nr[i];
1477                 if (dev) 
1478                         unregister_netdev(dev);
1479         }
1480 
1481         kfree(dev_nr);
1482 }
1483 module_exit(nr_exit);
1484 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp