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