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