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