[DECNET]: Covert rules to use generic code
[pandora-kernel.git] / net / decnet / dn_dev.c
1 /*
2  * DECnet       An implementation of the DECnet protocol suite for the LINUX
3  *              operating system.  DECnet is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              DECnet Device Layer
7  *
8  * Authors:     Steve Whitehouse <SteveW@ACM.org>
9  *              Eduardo Marcelo Serrat <emserrat@geocities.com>
10  *
11  * Changes:
12  *          Steve Whitehouse : Devices now see incoming frames so they
13  *                             can mark on who it came from.
14  *          Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
15  *                             can now have a device specific setup func.
16  *          Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
17  *          Steve Whitehouse : Fixed bug which sometimes killed timer
18  *          Steve Whitehouse : Multiple ifaddr support
19  *          Steve Whitehouse : SIOCGIFCONF is now a compile time option
20  *          Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
21  *          Steve Whitehouse : Removed timer1 - it's a user space issue now
22  *         Patrick Caulfield : Fixed router hello message format
23  *          Steve Whitehouse : Got rid of constant sizes for blksize for
24  *                             devices. All mtu based now.
25  */
26
27 #include <linux/capability.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/net.h>
32 #include <linux/netdevice.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/timer.h>
36 #include <linux/string.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_arp.h>
39 #include <linux/if_ether.h>
40 #include <linux/skbuff.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/sysctl.h>
43 #include <linux/notifier.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46 #include <net/neighbour.h>
47 #include <net/dst.h>
48 #include <net/flow.h>
49 #include <net/fib_rules.h>
50 #include <net/dn.h>
51 #include <net/dn_dev.h>
52 #include <net/dn_route.h>
53 #include <net/dn_neigh.h>
54 #include <net/dn_fib.h>
55
56 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
57
58 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
59 static char dn_rt_all_rt_mcast[ETH_ALEN]  = {0xAB,0x00,0x00,0x03,0x00,0x00};
60 static char dn_hiord[ETH_ALEN]            = {0xAA,0x00,0x04,0x00,0x00,0x00};
61 static unsigned char dn_eco_version[3]    = {0x02,0x00,0x00};
62
63 extern struct neigh_table dn_neigh_table;
64
65 /*
66  * decnet_address is kept in network order.
67  */
68 __le16 decnet_address = 0;
69
70 static DEFINE_RWLOCK(dndev_lock);
71 static struct net_device *decnet_default_device;
72 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
73
74 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
75 static void dn_dev_delete(struct net_device *dev);
76 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa);
77
78 static int dn_eth_up(struct net_device *);
79 static void dn_eth_down(struct net_device *);
80 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
81 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
82
83 static struct dn_dev_parms dn_dev_list[] =  {
84 {
85         .type =         ARPHRD_ETHER, /* Ethernet */
86         .mode =         DN_DEV_BCAST,
87         .state =        DN_DEV_S_RU,
88         .t2 =           1,
89         .t3 =           10,
90         .name =         "ethernet",
91         .ctl_name =     NET_DECNET_CONF_ETHER,
92         .up =           dn_eth_up,
93         .down =         dn_eth_down,
94         .timer3 =       dn_send_brd_hello,
95 },
96 {
97         .type =         ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
98         .mode =         DN_DEV_BCAST,
99         .state =        DN_DEV_S_RU,
100         .t2 =           1,
101         .t3 =           10,
102         .name =         "ipgre",
103         .ctl_name =     NET_DECNET_CONF_GRE,
104         .timer3 =       dn_send_brd_hello,
105 },
106 #if 0
107 {
108         .type =         ARPHRD_X25, /* Bog standard X.25 */
109         .mode =         DN_DEV_UCAST,
110         .state =        DN_DEV_S_DS,
111         .t2 =           1,
112         .t3 =           120,
113         .name =         "x25",
114         .ctl_name =     NET_DECNET_CONF_X25,
115         .timer3 =       dn_send_ptp_hello,
116 },
117 #endif
118 #if 0
119 {
120         .type =         ARPHRD_PPP, /* DECnet over PPP */
121         .mode =         DN_DEV_BCAST,
122         .state =        DN_DEV_S_RU,
123         .t2 =           1,
124         .t3 =           10,
125         .name =         "ppp",
126         .ctl_name =     NET_DECNET_CONF_PPP,
127         .timer3 =       dn_send_brd_hello,
128 },
129 #endif
130 {
131         .type =         ARPHRD_DDCMP, /* DECnet over DDCMP */
132         .mode =         DN_DEV_UCAST,
133         .state =        DN_DEV_S_DS,
134         .t2 =           1,
135         .t3 =           120,
136         .name =         "ddcmp",
137         .ctl_name =     NET_DECNET_CONF_DDCMP,
138         .timer3 =       dn_send_ptp_hello,
139 },
140 {
141         .type =         ARPHRD_LOOPBACK, /* Loopback interface - always last */
142         .mode =         DN_DEV_BCAST,
143         .state =        DN_DEV_S_RU,
144         .t2 =           1,
145         .t3 =           10,
146         .name =         "loopback",
147         .ctl_name =     NET_DECNET_CONF_LOOPBACK,
148         .timer3 =       dn_send_brd_hello,
149 }
150 };
151
152 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
153
154 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
155
156 #ifdef CONFIG_SYSCTL
157
158 static int min_t2[] = { 1 };
159 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
160 static int min_t3[] = { 1 };
161 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
162
163 static int min_priority[1];
164 static int max_priority[] = { 127 }; /* From DECnet spec */
165
166 static int dn_forwarding_proc(ctl_table *, int, struct file *,
167                         void __user *, size_t *, loff_t *);
168 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
169                         void __user *oldval, size_t __user *oldlenp,
170                         void __user *newval, size_t newlen,
171                         void **context);
172
173 static struct dn_dev_sysctl_table {
174         struct ctl_table_header *sysctl_header;
175         ctl_table dn_dev_vars[5];
176         ctl_table dn_dev_dev[2];
177         ctl_table dn_dev_conf_dir[2];
178         ctl_table dn_dev_proto_dir[2];
179         ctl_table dn_dev_root_dir[2];
180 } dn_dev_sysctl = {
181         NULL,
182         {
183         {
184                 .ctl_name = NET_DECNET_CONF_DEV_FORWARDING,
185                 .procname = "forwarding",
186                 .data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
187                 .maxlen = sizeof(int),
188                 .mode = 0644,
189                 .proc_handler = dn_forwarding_proc,
190                 .strategy = dn_forwarding_sysctl,
191         },
192         {
193                 .ctl_name = NET_DECNET_CONF_DEV_PRIORITY,
194                 .procname = "priority",
195                 .data = (void *)DN_DEV_PARMS_OFFSET(priority),
196                 .maxlen = sizeof(int),
197                 .mode = 0644,
198                 .proc_handler = proc_dointvec_minmax,
199                 .strategy = sysctl_intvec,
200                 .extra1 = &min_priority,
201                 .extra2 = &max_priority
202         },
203         {
204                 .ctl_name = NET_DECNET_CONF_DEV_T2,
205                 .procname = "t2",
206                 .data = (void *)DN_DEV_PARMS_OFFSET(t2),
207                 .maxlen = sizeof(int),
208                 .mode = 0644,
209                 .proc_handler = proc_dointvec_minmax,
210                 .strategy = sysctl_intvec,
211                 .extra1 = &min_t2,
212                 .extra2 = &max_t2
213         },
214         {
215                 .ctl_name = NET_DECNET_CONF_DEV_T3,
216                 .procname = "t3",
217                 .data = (void *)DN_DEV_PARMS_OFFSET(t3),
218                 .maxlen = sizeof(int),
219                 .mode = 0644,
220                 .proc_handler = proc_dointvec_minmax,
221                 .strategy = sysctl_intvec,
222                 .extra1 = &min_t3,
223                 .extra2 = &max_t3
224         },
225         {0}
226         },
227         {{
228                 .ctl_name = 0, 
229                 .procname = "", 
230                 .mode = 0555, 
231                 .child = dn_dev_sysctl.dn_dev_vars
232         }, {0}},
233         {{
234                 .ctl_name = NET_DECNET_CONF,
235                 .procname = "conf", 
236                 .mode = 0555, 
237                 .child = dn_dev_sysctl.dn_dev_dev
238         }, {0}},
239         {{
240                 .ctl_name = NET_DECNET, 
241                 .procname = "decnet", 
242                 .mode = 0555, 
243                 .child = dn_dev_sysctl.dn_dev_conf_dir
244         }, {0}},
245         {{
246                 .ctl_name = CTL_NET, 
247                 .procname = "net", 
248                 .mode = 0555, 
249                 .child = dn_dev_sysctl.dn_dev_proto_dir
250         }, {0}}
251 };
252
253 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
254 {
255         struct dn_dev_sysctl_table *t;
256         int i;
257
258         t = kmalloc(sizeof(*t), GFP_KERNEL);
259         if (t == NULL)
260                 return;
261
262         memcpy(t, &dn_dev_sysctl, sizeof(*t));
263
264         for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
265                 long offset = (long)t->dn_dev_vars[i].data;
266                 t->dn_dev_vars[i].data = ((char *)parms) + offset;
267                 t->dn_dev_vars[i].de = NULL;
268         }
269
270         if (dev) {
271                 t->dn_dev_dev[0].procname = dev->name;
272                 t->dn_dev_dev[0].ctl_name = dev->ifindex;
273         } else {
274                 t->dn_dev_dev[0].procname = parms->name;
275                 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
276         }
277
278         t->dn_dev_dev[0].child = t->dn_dev_vars;
279         t->dn_dev_dev[0].de = NULL;
280         t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
281         t->dn_dev_conf_dir[0].de = NULL;
282         t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
283         t->dn_dev_proto_dir[0].de = NULL;
284         t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
285         t->dn_dev_root_dir[0].de = NULL;
286         t->dn_dev_vars[0].extra1 = (void *)dev;
287
288         t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
289         if (t->sysctl_header == NULL)
290                 kfree(t);
291         else
292                 parms->sysctl = t;
293 }
294
295 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
296 {
297         if (parms->sysctl) {
298                 struct dn_dev_sysctl_table *t = parms->sysctl;
299                 parms->sysctl = NULL;
300                 unregister_sysctl_table(t->sysctl_header);
301                 kfree(t);
302         }
303 }
304
305 static int dn_forwarding_proc(ctl_table *table, int write, 
306                                 struct file *filep,
307                                 void __user *buffer,
308                                 size_t *lenp, loff_t *ppos)
309 {
310 #ifdef CONFIG_DECNET_ROUTER
311         struct net_device *dev = table->extra1;
312         struct dn_dev *dn_db;
313         int err;
314         int tmp, old;
315
316         if (table->extra1 == NULL)
317                 return -EINVAL;
318
319         dn_db = dev->dn_ptr;
320         old = dn_db->parms.forwarding;
321
322         err = proc_dointvec(table, write, filep, buffer, lenp, ppos);
323
324         if ((err >= 0) && write) {
325                 if (dn_db->parms.forwarding < 0)
326                         dn_db->parms.forwarding = 0;
327                 if (dn_db->parms.forwarding > 2)
328                         dn_db->parms.forwarding = 2;
329                 /*
330                  * What an ugly hack this is... its works, just. It
331                  * would be nice if sysctl/proc were just that little
332                  * bit more flexible so I don't have to write a special
333                  * routine, or suffer hacks like this - SJW
334                  */
335                 tmp = dn_db->parms.forwarding;
336                 dn_db->parms.forwarding = old;
337                 if (dn_db->parms.down)
338                         dn_db->parms.down(dev);
339                 dn_db->parms.forwarding = tmp;
340                 if (dn_db->parms.up)
341                         dn_db->parms.up(dev);
342         }
343
344         return err;
345 #else
346         return -EINVAL;
347 #endif
348 }
349
350 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
351                         void __user *oldval, size_t __user *oldlenp,
352                         void __user *newval, size_t newlen,
353                         void **context)
354 {
355 #ifdef CONFIG_DECNET_ROUTER
356         struct net_device *dev = table->extra1;
357         struct dn_dev *dn_db;
358         int value;
359
360         if (table->extra1 == NULL)
361                 return -EINVAL;
362
363         dn_db = dev->dn_ptr;
364
365         if (newval && newlen) {
366                 if (newlen != sizeof(int))
367                         return -EINVAL;
368
369                 if (get_user(value, (int __user *)newval))
370                         return -EFAULT;
371                 if (value < 0)
372                         return -EINVAL;
373                 if (value > 2)
374                         return -EINVAL;
375
376                 if (dn_db->parms.down)
377                         dn_db->parms.down(dev);
378                 dn_db->parms.forwarding = value;
379                 if (dn_db->parms.up)
380                         dn_db->parms.up(dev);
381         }
382
383         return 0;
384 #else
385         return -EINVAL;
386 #endif
387 }
388
389 #else /* CONFIG_SYSCTL */
390 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
391 {
392 }
393 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
394 {
395 }
396
397 #endif /* CONFIG_SYSCTL */
398
399 static inline __u16 mtu2blksize(struct net_device *dev)
400 {
401         u32 blksize = dev->mtu;
402         if (blksize > 0xffff)
403                 blksize = 0xffff;
404
405         if (dev->type == ARPHRD_ETHER ||
406             dev->type == ARPHRD_PPP ||
407             dev->type == ARPHRD_IPGRE ||
408             dev->type == ARPHRD_LOOPBACK)
409                 blksize -= 2;
410
411         return (__u16)blksize;
412 }
413
414 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
415 {
416         struct dn_ifaddr *ifa;
417
418         ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
419
420         return ifa;
421 }
422
423 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
424 {
425         kfree(ifa);
426 }
427
428 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
429 {
430         struct dn_ifaddr *ifa1 = *ifap;
431         unsigned char mac_addr[6];
432         struct net_device *dev = dn_db->dev;
433
434         ASSERT_RTNL();
435
436         *ifap = ifa1->ifa_next;
437
438         if (dn_db->dev->type == ARPHRD_ETHER) {
439                 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
440                         dn_dn2eth(mac_addr, ifa1->ifa_local);
441                         dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
442                 }
443         }
444
445         rtmsg_ifa(RTM_DELADDR, ifa1);
446         blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
447         if (destroy) {
448                 dn_dev_free_ifa(ifa1);
449
450                 if (dn_db->ifa_list == NULL)
451                         dn_dev_delete(dn_db->dev);
452         }
453 }
454
455 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
456 {
457         struct net_device *dev = dn_db->dev;
458         struct dn_ifaddr *ifa1;
459         unsigned char mac_addr[6];
460
461         ASSERT_RTNL();
462
463         /* Check for duplicates */      
464         for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
465                 if (ifa1->ifa_local == ifa->ifa_local)
466                         return -EEXIST;
467         }
468
469         if (dev->type == ARPHRD_ETHER) {
470                 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
471                         dn_dn2eth(mac_addr, ifa->ifa_local);
472                         dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
473                         dev_mc_upload(dev);
474                 }
475         }
476
477         ifa->ifa_next = dn_db->ifa_list;
478         dn_db->ifa_list = ifa;
479
480         rtmsg_ifa(RTM_NEWADDR, ifa);
481         blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
482
483         return 0;
484 }
485
486 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
487 {
488         struct dn_dev *dn_db = dev->dn_ptr;
489         int rv;
490
491         if (dn_db == NULL) {
492                 int err;
493                 dn_db = dn_dev_create(dev, &err);
494                 if (dn_db == NULL)
495                         return err;
496         }
497
498         ifa->ifa_dev = dn_db;
499
500         if (dev->flags & IFF_LOOPBACK)
501                 ifa->ifa_scope = RT_SCOPE_HOST;
502
503         rv = dn_dev_insert_ifa(dn_db, ifa);
504         if (rv)
505                 dn_dev_free_ifa(ifa);
506         return rv;
507 }
508
509
510 int dn_dev_ioctl(unsigned int cmd, void __user *arg)
511 {
512         char buffer[DN_IFREQ_SIZE];
513         struct ifreq *ifr = (struct ifreq *)buffer;
514         struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
515         struct dn_dev *dn_db;
516         struct net_device *dev;
517         struct dn_ifaddr *ifa = NULL, **ifap = NULL;
518         int ret = 0;
519
520         if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
521                 return -EFAULT;
522         ifr->ifr_name[IFNAMSIZ-1] = 0;
523
524 #ifdef CONFIG_KMOD
525         dev_load(ifr->ifr_name);
526 #endif
527
528         switch(cmd) {
529                 case SIOCGIFADDR:
530                         break;
531                 case SIOCSIFADDR:
532                         if (!capable(CAP_NET_ADMIN))
533                                 return -EACCES;
534                         if (sdn->sdn_family != AF_DECnet)
535                                 return -EINVAL;
536                         break;
537                 default:
538                         return -EINVAL;
539         }
540
541         rtnl_lock();
542
543         if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
544                 ret = -ENODEV;
545                 goto done;
546         }
547
548         if ((dn_db = dev->dn_ptr) != NULL) {
549                 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
550                         if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
551                                 break;
552         }
553
554         if (ifa == NULL && cmd != SIOCSIFADDR) {
555                 ret = -EADDRNOTAVAIL;
556                 goto done;
557         }
558
559         switch(cmd) {
560                 case SIOCGIFADDR:
561                         *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
562                         goto rarok;
563
564                 case SIOCSIFADDR:
565                         if (!ifa) {
566                                 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
567                                         ret = -ENOBUFS;
568                                         break;
569                                 }
570                                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
571                         } else {
572                                 if (ifa->ifa_local == dn_saddr2dn(sdn))
573                                         break;
574                                 dn_dev_del_ifa(dn_db, ifap, 0);
575                         }
576
577                         ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
578
579                         ret = dn_dev_set_ifa(dev, ifa);
580         }
581 done:
582         rtnl_unlock();
583
584         return ret;
585 rarok:
586         if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
587                 ret = -EFAULT;
588         goto done;
589 }
590
591 struct net_device *dn_dev_get_default(void)
592 {
593         struct net_device *dev;
594         read_lock(&dndev_lock);
595         dev = decnet_default_device;
596         if (dev) {
597                 if (dev->dn_ptr)
598                         dev_hold(dev);
599                 else
600                         dev = NULL;
601         }
602         read_unlock(&dndev_lock);
603         return dev;
604 }
605
606 int dn_dev_set_default(struct net_device *dev, int force)
607 {
608         struct net_device *old = NULL;
609         int rv = -EBUSY;
610         if (!dev->dn_ptr)
611                 return -ENODEV;
612         write_lock(&dndev_lock);
613         if (force || decnet_default_device == NULL) {
614                 old = decnet_default_device;
615                 decnet_default_device = dev;
616                 rv = 0;
617         }
618         write_unlock(&dndev_lock);
619         if (old)
620                 dev_put(old);
621         return rv;
622 }
623
624 static void dn_dev_check_default(struct net_device *dev)
625 {
626         write_lock(&dndev_lock);
627         if (dev == decnet_default_device) {
628                 decnet_default_device = NULL;
629         } else {
630                 dev = NULL;
631         }
632         write_unlock(&dndev_lock);
633         if (dev)
634                 dev_put(dev);
635 }
636
637 static struct dn_dev *dn_dev_by_index(int ifindex)
638 {
639         struct net_device *dev;
640         struct dn_dev *dn_dev = NULL;
641         dev = dev_get_by_index(ifindex);
642         if (dev) {
643                 dn_dev = dev->dn_ptr;
644                 dev_put(dev);
645         }
646
647         return dn_dev;
648 }
649
650 static int dn_dev_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
651 {
652         struct rtattr **rta = arg;
653         struct dn_dev *dn_db;
654         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
655         struct dn_ifaddr *ifa, **ifap;
656
657         if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
658                 return -EADDRNOTAVAIL;
659
660         for(ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) {
661                 void *tmp = rta[IFA_LOCAL-1];
662                 if ((tmp && memcmp(RTA_DATA(tmp), &ifa->ifa_local, 2)) ||
663                     (rta[IFA_LABEL-1] && rtattr_strcmp(rta[IFA_LABEL-1], ifa->ifa_label)))
664                         continue;
665
666                 dn_dev_del_ifa(dn_db, ifap, 1);
667                 return 0;
668         }
669
670         return -EADDRNOTAVAIL;
671 }
672
673 static int dn_dev_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
674 {
675         struct rtattr **rta = arg;
676         struct net_device *dev;
677         struct dn_dev *dn_db;
678         struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
679         struct dn_ifaddr *ifa;
680         int rv;
681
682         if (rta[IFA_LOCAL-1] == NULL)
683                 return -EINVAL;
684
685         if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
686                 return -ENODEV;
687
688         if ((dn_db = dev->dn_ptr) == NULL) {
689                 int err;
690                 dn_db = dn_dev_create(dev, &err);
691                 if (!dn_db)
692                         return err;
693         }
694         
695         if ((ifa = dn_dev_alloc_ifa()) == NULL)
696                 return -ENOBUFS;
697
698         if (!rta[IFA_ADDRESS - 1])
699                 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
700         memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 2);
701         memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 2);
702         ifa->ifa_flags = ifm->ifa_flags;
703         ifa->ifa_scope = ifm->ifa_scope;
704         ifa->ifa_dev = dn_db;
705         if (rta[IFA_LABEL-1])
706                 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL-1], IFNAMSIZ);
707         else
708                 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
709
710         rv = dn_dev_insert_ifa(dn_db, ifa);
711         if (rv)
712                 dn_dev_free_ifa(ifa);
713         return rv;
714 }
715
716 static int dn_dev_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
717                                 u32 pid, u32 seq, int event, unsigned int flags)
718 {
719         struct ifaddrmsg *ifm;
720         struct nlmsghdr *nlh;
721         unsigned char *b = skb->tail;
722
723         nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
724         ifm = NLMSG_DATA(nlh);
725
726         ifm->ifa_family = AF_DECnet;
727         ifm->ifa_prefixlen = 16;
728         ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
729         ifm->ifa_scope = ifa->ifa_scope;
730         ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
731         if (ifa->ifa_address)
732                 RTA_PUT(skb, IFA_ADDRESS, 2, &ifa->ifa_address);
733         if (ifa->ifa_local)
734                 RTA_PUT(skb, IFA_LOCAL, 2, &ifa->ifa_local);
735         if (ifa->ifa_label[0])
736                 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
737         nlh->nlmsg_len = skb->tail - b;
738         return skb->len;
739
740 nlmsg_failure:
741 rtattr_failure:
742         skb_trim(skb, b - skb->data);
743         return -1;
744 }
745
746 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa)
747 {
748         struct sk_buff *skb;
749         int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
750
751         skb = alloc_skb(size, GFP_KERNEL);
752         if (!skb) {
753                 netlink_set_err(rtnl, 0, RTNLGRP_DECnet_IFADDR, ENOBUFS);
754                 return;
755         }
756         if (dn_dev_fill_ifaddr(skb, ifa, 0, 0, event, 0) < 0) {
757                 kfree_skb(skb);
758                 netlink_set_err(rtnl, 0, RTNLGRP_DECnet_IFADDR, EINVAL);
759                 return;
760         }
761         NETLINK_CB(skb).dst_group = RTNLGRP_DECnet_IFADDR;
762         netlink_broadcast(rtnl, skb, 0, RTNLGRP_DECnet_IFADDR, GFP_KERNEL);
763 }
764
765 static int dn_dev_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
766 {
767         int idx, dn_idx;
768         int s_idx, s_dn_idx;
769         struct net_device *dev;
770         struct dn_dev *dn_db;
771         struct dn_ifaddr *ifa;
772
773         s_idx = cb->args[0];
774         s_dn_idx = dn_idx = cb->args[1];
775         read_lock(&dev_base_lock);
776         for(dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
777                 if (idx < s_idx)
778                         continue;
779                 if (idx > s_idx)
780                         s_dn_idx = 0;
781                 if ((dn_db = dev->dn_ptr) == NULL)
782                         continue;
783
784                 for(ifa = dn_db->ifa_list, dn_idx = 0; ifa; ifa = ifa->ifa_next, dn_idx++) {
785                         if (dn_idx < s_dn_idx)
786                                 continue;
787
788                         if (dn_dev_fill_ifaddr(skb, ifa,
789                                                NETLINK_CB(cb->skb).pid,
790                                                cb->nlh->nlmsg_seq,
791                                                RTM_NEWADDR,
792                                                NLM_F_MULTI) <= 0)
793                                 goto done;
794                 }
795         }
796 done:
797         read_unlock(&dev_base_lock);
798         cb->args[0] = idx;
799         cb->args[1] = dn_idx;
800
801         return skb->len;
802 }
803
804 static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
805 {
806         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
807         struct dn_ifaddr *ifa;
808         int rv = -ENODEV;
809         if (dn_db == NULL)
810                 goto out;
811         ifa = dn_db->ifa_list;
812         if (ifa != NULL) {
813                 *addr = ifa->ifa_local;
814                 rv = 0;
815         }
816 out:
817         return rv;
818 }
819
820 /* 
821  * Find a default address to bind to.
822  *
823  * This is one of those areas where the initial VMS concepts don't really
824  * map onto the Linux concepts, and since we introduced multiple addresses
825  * per interface we have to cope with slightly odd ways of finding out what
826  * "our address" really is. Mostly it's not a problem; for this we just guess
827  * a sensible default. Eventually the routing code will take care of all the
828  * nasties for us I hope.
829  */
830 int dn_dev_bind_default(__le16 *addr)
831 {
832         struct net_device *dev;
833         int rv;
834         dev = dn_dev_get_default();
835 last_chance:
836         if (dev) {
837                 read_lock(&dev_base_lock);
838                 rv = dn_dev_get_first(dev, addr);
839                 read_unlock(&dev_base_lock);
840                 dev_put(dev);
841                 if (rv == 0 || dev == &loopback_dev)
842                         return rv;
843         }
844         dev = &loopback_dev;
845         dev_hold(dev);
846         goto last_chance;
847 }
848
849 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
850 {
851         struct endnode_hello_message *msg;
852         struct sk_buff *skb = NULL;
853         __le16 *pktlen;
854         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
855
856         if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
857                 return;
858
859         skb->dev = dev;
860
861         msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
862
863         msg->msgflg  = 0x0D;
864         memcpy(msg->tiver, dn_eco_version, 3);
865         dn_dn2eth(msg->id, ifa->ifa_local);
866         msg->iinfo   = DN_RT_INFO_ENDN;
867         msg->blksize = dn_htons(mtu2blksize(dev));
868         msg->area    = 0x00;
869         memset(msg->seed, 0, 8);
870         memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
871
872         if (dn_db->router) {
873                 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
874                 dn_dn2eth(msg->neighbor, dn->addr);
875         }
876
877         msg->timer   = dn_htons((unsigned short)dn_db->parms.t3);
878         msg->mpd     = 0x00;
879         msg->datalen = 0x02;
880         memset(msg->data, 0xAA, 2);
881         
882         pktlen = (__le16 *)skb_push(skb,2);
883         *pktlen = dn_htons(skb->len - 2);
884
885         skb->nh.raw = skb->data;
886
887         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
888 }
889
890
891 #define DRDELAY (5 * HZ)
892
893 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
894 {
895         /* First check time since device went up */
896         if ((jiffies - dn_db->uptime) < DRDELAY)
897                 return 0;
898
899         /* If there is no router, then yes... */
900         if (!dn_db->router)
901                 return 1;
902
903         /* otherwise only if we have a higher priority or.. */
904         if (dn->priority < dn_db->parms.priority)
905                 return 1;
906
907         /* if we have equal priority and a higher node number */
908         if (dn->priority != dn_db->parms.priority)
909                 return 0;
910
911         if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
912                 return 1;
913
914         return 0;
915 }
916
917 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
918 {
919         int n;
920         struct dn_dev *dn_db = dev->dn_ptr;
921         struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
922         struct sk_buff *skb;
923         size_t size;
924         unsigned char *ptr;
925         unsigned char *i1, *i2;
926         __le16 *pktlen;
927         char *src;
928
929         if (mtu2blksize(dev) < (26 + 7))
930                 return;
931
932         n = mtu2blksize(dev) - 26;
933         n /= 7;
934
935         if (n > 32)
936                 n = 32;
937
938         size = 2 + 26 + 7 * n;
939
940         if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
941                 return;
942
943         skb->dev = dev;
944         ptr = skb_put(skb, size);
945
946         *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
947         *ptr++ = 2; /* ECO */
948         *ptr++ = 0;
949         *ptr++ = 0;
950         dn_dn2eth(ptr, ifa->ifa_local);
951         src = ptr;
952         ptr += ETH_ALEN;
953         *ptr++ = dn_db->parms.forwarding == 1 ? 
954                         DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
955         *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
956         ptr += 2;
957         *ptr++ = dn_db->parms.priority; /* Priority */ 
958         *ptr++ = 0; /* Area: Reserved */
959         *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
960         ptr += 2;
961         *ptr++ = 0; /* MPD: Reserved */
962         i1 = ptr++;
963         memset(ptr, 0, 7); /* Name: Reserved */
964         ptr += 7;
965         i2 = ptr++;
966
967         n = dn_neigh_elist(dev, ptr, n);
968
969         *i2 = 7 * n;
970         *i1 = 8 + *i2;
971
972         skb_trim(skb, (27 + *i2));
973
974         pktlen = (__le16 *)skb_push(skb, 2);
975         *pktlen = dn_htons(skb->len - 2);
976
977         skb->nh.raw = skb->data;
978
979         if (dn_am_i_a_router(dn, dn_db, ifa)) {
980                 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
981                 if (skb2) {
982                         dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
983                 }
984         }
985
986         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
987 }
988
989 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
990 {
991         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
992
993         if (dn_db->parms.forwarding == 0)
994                 dn_send_endnode_hello(dev, ifa);
995         else
996                 dn_send_router_hello(dev, ifa);
997 }
998
999 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1000 {
1001         int tdlen = 16;
1002         int size = dev->hard_header_len + 2 + 4 + tdlen;
1003         struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1004         int i;
1005         unsigned char *ptr;
1006         char src[ETH_ALEN];
1007
1008         if (skb == NULL)
1009                 return ;
1010
1011         skb->dev = dev;
1012         skb_push(skb, dev->hard_header_len);
1013         ptr = skb_put(skb, 2 + 4 + tdlen);
1014
1015         *ptr++ = DN_RT_PKT_HELO;
1016         *((__le16 *)ptr) = ifa->ifa_local;
1017         ptr += 2;
1018         *ptr++ = tdlen;
1019
1020         for(i = 0; i < tdlen; i++)
1021                 *ptr++ = 0252;
1022
1023         dn_dn2eth(src, ifa->ifa_local);
1024         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1025 }
1026
1027 static int dn_eth_up(struct net_device *dev)
1028 {
1029         struct dn_dev *dn_db = dev->dn_ptr;
1030
1031         if (dn_db->parms.forwarding == 0)
1032                 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1033         else
1034                 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1035
1036         dev_mc_upload(dev);
1037
1038         dn_db->use_long = 1;
1039
1040         return 0;
1041 }
1042
1043 static void dn_eth_down(struct net_device *dev)
1044 {
1045         struct dn_dev *dn_db = dev->dn_ptr;
1046
1047         if (dn_db->parms.forwarding == 0)
1048                 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1049         else
1050                 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1051 }
1052
1053 static void dn_dev_set_timer(struct net_device *dev);
1054
1055 static void dn_dev_timer_func(unsigned long arg)
1056 {
1057         struct net_device *dev = (struct net_device *)arg;
1058         struct dn_dev *dn_db = dev->dn_ptr;
1059         struct dn_ifaddr *ifa;
1060
1061         if (dn_db->t3 <= dn_db->parms.t2) {
1062                 if (dn_db->parms.timer3) {
1063                         for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1064                                 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1065                                         dn_db->parms.timer3(dev, ifa);
1066                         }
1067                 }
1068                 dn_db->t3 = dn_db->parms.t3;
1069         } else {
1070                 dn_db->t3 -= dn_db->parms.t2;
1071         }
1072
1073         dn_dev_set_timer(dev);
1074 }
1075
1076 static void dn_dev_set_timer(struct net_device *dev)
1077 {
1078         struct dn_dev *dn_db = dev->dn_ptr;
1079
1080         if (dn_db->parms.t2 > dn_db->parms.t3)
1081                 dn_db->parms.t2 = dn_db->parms.t3;
1082
1083         dn_db->timer.data = (unsigned long)dev;
1084         dn_db->timer.function = dn_dev_timer_func;
1085         dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1086
1087         add_timer(&dn_db->timer);
1088 }
1089
1090 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1091 {
1092         int i;
1093         struct dn_dev_parms *p = dn_dev_list;
1094         struct dn_dev *dn_db;
1095
1096         for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1097                 if (p->type == dev->type)
1098                         break;
1099         }
1100
1101         *err = -ENODEV;
1102         if (i == DN_DEV_LIST_SIZE)
1103                 return NULL;
1104
1105         *err = -ENOBUFS;
1106         if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1107                 return NULL;
1108
1109         memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1110         smp_wmb();
1111         dev->dn_ptr = dn_db;
1112         dn_db->dev = dev;
1113         init_timer(&dn_db->timer);
1114
1115         dn_db->uptime = jiffies;
1116         if (dn_db->parms.up) {
1117                 if (dn_db->parms.up(dev) < 0) {
1118                         dev->dn_ptr = NULL;
1119                         kfree(dn_db);
1120                         return NULL;
1121                 }
1122         }
1123
1124         dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1125
1126         dn_dev_sysctl_register(dev, &dn_db->parms);
1127
1128         dn_dev_set_timer(dev);
1129
1130         *err = 0;
1131         return dn_db;
1132 }
1133
1134
1135 /*
1136  * This processes a device up event. We only start up
1137  * the loopback device & ethernet devices with correct
1138  * MAC addreses automatically. Others must be started
1139  * specifically.
1140  *
1141  * FIXME: How should we configure the loopback address ? If we could dispense
1142  * with using decnet_address here and for autobind, it will be one less thing
1143  * for users to worry about setting up.
1144  */
1145
1146 void dn_dev_up(struct net_device *dev)
1147 {
1148         struct dn_ifaddr *ifa;
1149         __le16 addr = decnet_address;
1150         int maybe_default = 0;
1151         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1152
1153         if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1154                 return;
1155
1156         /*
1157          * Need to ensure that loopback device has a dn_db attached to it
1158          * to allow creation of neighbours against it, even though it might
1159          * not have a local address of its own. Might as well do the same for
1160          * all autoconfigured interfaces.
1161          */
1162         if (dn_db == NULL) {
1163                 int err;
1164                 dn_db = dn_dev_create(dev, &err);
1165                 if (dn_db == NULL)
1166                         return;
1167         }
1168
1169         if (dev->type == ARPHRD_ETHER) {
1170                 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1171                         return;
1172                 addr = dn_eth2dn(dev->dev_addr);
1173                 maybe_default = 1;
1174         }
1175
1176         if (addr == 0)
1177                 return;
1178
1179         if ((ifa = dn_dev_alloc_ifa()) == NULL)
1180                 return;
1181
1182         ifa->ifa_local = ifa->ifa_address = addr;
1183         ifa->ifa_flags = 0;
1184         ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1185         strcpy(ifa->ifa_label, dev->name);
1186
1187         dn_dev_set_ifa(dev, ifa);
1188
1189         /*
1190          * Automagically set the default device to the first automatically
1191          * configured ethernet card in the system.
1192          */
1193         if (maybe_default) {
1194                 dev_hold(dev);
1195                 if (dn_dev_set_default(dev, 0))
1196                         dev_put(dev);
1197         }
1198 }
1199
1200 static void dn_dev_delete(struct net_device *dev)
1201 {
1202         struct dn_dev *dn_db = dev->dn_ptr;
1203
1204         if (dn_db == NULL)
1205                 return;
1206
1207         del_timer_sync(&dn_db->timer);
1208         dn_dev_sysctl_unregister(&dn_db->parms);
1209         dn_dev_check_default(dev);
1210         neigh_ifdown(&dn_neigh_table, dev);
1211
1212         if (dn_db->parms.down)
1213                 dn_db->parms.down(dev);
1214
1215         dev->dn_ptr = NULL;
1216
1217         neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1218         neigh_ifdown(&dn_neigh_table, dev);
1219
1220         if (dn_db->router)
1221                 neigh_release(dn_db->router);
1222         if (dn_db->peer)
1223                 neigh_release(dn_db->peer);
1224
1225         kfree(dn_db);
1226 }
1227
1228 void dn_dev_down(struct net_device *dev)
1229 {
1230         struct dn_dev *dn_db = dev->dn_ptr;
1231         struct dn_ifaddr *ifa;
1232
1233         if (dn_db == NULL)
1234                 return;
1235
1236         while((ifa = dn_db->ifa_list) != NULL) {
1237                 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1238                 dn_dev_free_ifa(ifa);
1239         }
1240
1241         dn_dev_delete(dev);
1242 }
1243
1244 void dn_dev_init_pkt(struct sk_buff *skb)
1245 {
1246         return;
1247 }
1248
1249 void dn_dev_veri_pkt(struct sk_buff *skb)
1250 {
1251         return;
1252 }
1253
1254 void dn_dev_hello(struct sk_buff *skb)
1255 {
1256         return;
1257 }
1258
1259 void dn_dev_devices_off(void)
1260 {
1261         struct net_device *dev;
1262
1263         rtnl_lock();
1264         for(dev = dev_base; dev; dev = dev->next)
1265                 dn_dev_down(dev);
1266         rtnl_unlock();
1267
1268 }
1269
1270 void dn_dev_devices_on(void)
1271 {
1272         struct net_device *dev;
1273
1274         rtnl_lock();
1275         for(dev = dev_base; dev; dev = dev->next) {
1276                 if (dev->flags & IFF_UP)
1277                         dn_dev_up(dev);
1278         }
1279         rtnl_unlock();
1280 }
1281
1282 int register_dnaddr_notifier(struct notifier_block *nb)
1283 {
1284         return blocking_notifier_chain_register(&dnaddr_chain, nb);
1285 }
1286
1287 int unregister_dnaddr_notifier(struct notifier_block *nb)
1288 {
1289         return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1290 }
1291
1292 #ifdef CONFIG_PROC_FS
1293 static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1294 {
1295         do {
1296                 dev = dev->next;
1297         } while(dev && !dev->dn_ptr);
1298
1299         return dev;
1300 }
1301
1302 static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1303 {
1304         struct net_device *dev;
1305
1306         dev = dev_base;
1307         if (dev && !dev->dn_ptr)
1308                 dev = dn_dev_get_next(seq, dev);
1309         if (pos) {
1310                 while(dev && (dev = dn_dev_get_next(seq, dev)))
1311                         --pos;
1312         }
1313         return dev;
1314 }
1315
1316 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1317 {
1318         if (*pos) {
1319                 struct net_device *dev;
1320                 read_lock(&dev_base_lock);
1321                 dev = dn_dev_get_idx(seq, *pos - 1);
1322                 if (dev == NULL)
1323                         read_unlock(&dev_base_lock);
1324                 return dev;
1325         }
1326         return SEQ_START_TOKEN;
1327 }
1328
1329 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1330 {
1331         struct net_device *dev = v;
1332         loff_t one = 1;
1333
1334         if (v == SEQ_START_TOKEN) {
1335                 dev = dn_dev_seq_start(seq, &one);
1336         } else {
1337                 dev = dn_dev_get_next(seq, dev);
1338                 if (dev == NULL)
1339                         read_unlock(&dev_base_lock);
1340         }
1341         ++*pos;
1342         return dev;
1343 }
1344
1345 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1346 {
1347         if (v && v != SEQ_START_TOKEN)
1348                 read_unlock(&dev_base_lock);
1349 }
1350
1351 static char *dn_type2asc(char type)
1352 {
1353         switch(type) {
1354                 case DN_DEV_BCAST:
1355                         return "B";
1356                 case DN_DEV_UCAST:
1357                         return "U";
1358                 case DN_DEV_MPOINT:
1359                         return "M";
1360         }
1361
1362         return "?";
1363 }
1364
1365 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1366 {
1367         if (v == SEQ_START_TOKEN)
1368                 seq_puts(seq, "Name     Flags T1   Timer1 T3   Timer3 BlkSize Pri State DevType    Router Peer\n");
1369         else {
1370                 struct net_device *dev = v;
1371                 char peer_buf[DN_ASCBUF_LEN];
1372                 char router_buf[DN_ASCBUF_LEN];
1373                 struct dn_dev *dn_db = dev->dn_ptr;
1374
1375                 seq_printf(seq, "%-8s %1s     %04u %04u   %04lu %04lu"
1376                                 "   %04hu    %03d %02x    %-10s %-7s %-7s\n",
1377                                 dev->name ? dev->name : "???",
1378                                 dn_type2asc(dn_db->parms.mode),
1379                                 0, 0,
1380                                 dn_db->t3, dn_db->parms.t3,
1381                                 mtu2blksize(dev),
1382                                 dn_db->parms.priority,
1383                                 dn_db->parms.state, dn_db->parms.name,
1384                                 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1385                                 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1386         }
1387         return 0;
1388 }
1389
1390 static struct seq_operations dn_dev_seq_ops = {
1391         .start  = dn_dev_seq_start,
1392         .next   = dn_dev_seq_next,
1393         .stop   = dn_dev_seq_stop,
1394         .show   = dn_dev_seq_show,
1395 };
1396
1397 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1398 {
1399         return seq_open(file, &dn_dev_seq_ops);
1400 }
1401
1402 static struct file_operations dn_dev_seq_fops = {
1403         .owner   = THIS_MODULE,
1404         .open    = dn_dev_seq_open,
1405         .read    = seq_read,
1406         .llseek  = seq_lseek,
1407         .release = seq_release,
1408 };
1409
1410 #endif /* CONFIG_PROC_FS */
1411
1412 static struct rtnetlink_link dnet_rtnetlink_table[RTM_NR_MSGTYPES] =
1413 {
1414         [RTM_NEWADDR  - RTM_BASE] = { .doit     = dn_dev_rtm_newaddr,   },
1415         [RTM_DELADDR  - RTM_BASE] = { .doit     = dn_dev_rtm_deladdr,   },
1416         [RTM_GETADDR  - RTM_BASE] = { .dumpit   = dn_dev_dump_ifaddr,   },
1417 #ifdef CONFIG_DECNET_ROUTER
1418         [RTM_NEWROUTE - RTM_BASE] = { .doit     = dn_fib_rtm_newroute,  },
1419         [RTM_DELROUTE - RTM_BASE] = { .doit     = dn_fib_rtm_delroute,  },
1420         [RTM_GETROUTE - RTM_BASE] = { .doit     = dn_cache_getroute,
1421                                       .dumpit   = dn_fib_dump,          },
1422         [RTM_GETRULE  - RTM_BASE] = { .dumpit   = dn_fib_dump_rules,    },
1423 #else
1424         [RTM_GETROUTE - RTM_BASE] = { .doit     = dn_cache_getroute,
1425                                       .dumpit   = dn_cache_dump,        },
1426 #endif
1427
1428 };
1429
1430 static int __initdata addr[2];
1431 module_param_array(addr, int, NULL, 0444);
1432 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1433
1434 void __init dn_dev_init(void)
1435 {
1436         if (addr[0] > 63 || addr[0] < 0) {
1437                 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1438                 return;
1439         }
1440
1441         if (addr[1] > 1023 || addr[1] < 0) {
1442                 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1443                 return;
1444         }
1445
1446         decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1447
1448         dn_dev_devices_on();
1449
1450         rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1451
1452         proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1453
1454 #ifdef CONFIG_SYSCTL
1455         {
1456                 int i;
1457                 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1458                         dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1459         }
1460 #endif /* CONFIG_SYSCTL */
1461 }
1462
1463 void __exit dn_dev_cleanup(void)
1464 {
1465         rtnetlink_links[PF_DECnet] = NULL;
1466
1467 #ifdef CONFIG_SYSCTL
1468         {
1469                 int i;
1470                 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1471                         dn_dev_sysctl_unregister(&dn_dev_list[i]);
1472         }
1473 #endif /* CONFIG_SYSCTL */
1474
1475         proc_net_remove("decnet_dev");
1476
1477         dn_dev_devices_off();
1478 }