[NET] KEY: Fix whitespace errors.
[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_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 -EMSGSIZE;
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         nlmsg_cancel(skb, nlh);
772         return -EMSGSIZE;
773 }
774
775 static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
776 {
777         struct sk_buff *skb;
778         int err = -ENOBUFS;
779
780         skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
781         if (skb == NULL)
782                 goto errout;
783
784         err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
785         if (err < 0) {
786                 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
787                 WARN_ON(err == -EMSGSIZE);
788                 kfree_skb(skb);
789                 goto errout;
790         }
791         err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
792 errout:
793         if (err < 0)
794                 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
795 }
796
797 static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
798 {
799         int idx, dn_idx = 0, skip_ndevs, skip_naddr;
800         struct net_device *dev;
801         struct dn_dev *dn_db;
802         struct dn_ifaddr *ifa;
803
804         skip_ndevs = cb->args[0];
805         skip_naddr = cb->args[1];
806
807         read_lock(&dev_base_lock);
808         for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
809                 if (idx < skip_ndevs)
810                         continue;
811                 else if (idx > skip_ndevs) {
812                         /* Only skip over addresses for first dev dumped
813                          * in this iteration (idx == skip_ndevs) */
814                         skip_naddr = 0;
815                 }
816
817                 if ((dn_db = dev->dn_ptr) == NULL)
818                         continue;
819
820                 for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
821                      ifa = ifa->ifa_next, dn_idx++) {
822                         if (dn_idx < skip_naddr)
823                                 continue;
824
825                         if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
826                                               cb->nlh->nlmsg_seq, RTM_NEWADDR,
827                                               NLM_F_MULTI) < 0)
828                                 goto done;
829                 }
830         }
831 done:
832         read_unlock(&dev_base_lock);
833
834         cb->args[0] = idx;
835         cb->args[1] = dn_idx;
836
837         return skb->len;
838 }
839
840 static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
841 {
842         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
843         struct dn_ifaddr *ifa;
844         int rv = -ENODEV;
845         if (dn_db == NULL)
846                 goto out;
847         ifa = dn_db->ifa_list;
848         if (ifa != NULL) {
849                 *addr = ifa->ifa_local;
850                 rv = 0;
851         }
852 out:
853         return rv;
854 }
855
856 /*
857  * Find a default address to bind to.
858  *
859  * This is one of those areas where the initial VMS concepts don't really
860  * map onto the Linux concepts, and since we introduced multiple addresses
861  * per interface we have to cope with slightly odd ways of finding out what
862  * "our address" really is. Mostly it's not a problem; for this we just guess
863  * a sensible default. Eventually the routing code will take care of all the
864  * nasties for us I hope.
865  */
866 int dn_dev_bind_default(__le16 *addr)
867 {
868         struct net_device *dev;
869         int rv;
870         dev = dn_dev_get_default();
871 last_chance:
872         if (dev) {
873                 read_lock(&dev_base_lock);
874                 rv = dn_dev_get_first(dev, addr);
875                 read_unlock(&dev_base_lock);
876                 dev_put(dev);
877                 if (rv == 0 || dev == &loopback_dev)
878                         return rv;
879         }
880         dev = &loopback_dev;
881         dev_hold(dev);
882         goto last_chance;
883 }
884
885 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
886 {
887         struct endnode_hello_message *msg;
888         struct sk_buff *skb = NULL;
889         __le16 *pktlen;
890         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
891
892         if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
893                 return;
894
895         skb->dev = dev;
896
897         msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
898
899         msg->msgflg  = 0x0D;
900         memcpy(msg->tiver, dn_eco_version, 3);
901         dn_dn2eth(msg->id, ifa->ifa_local);
902         msg->iinfo   = DN_RT_INFO_ENDN;
903         msg->blksize = dn_htons(mtu2blksize(dev));
904         msg->area    = 0x00;
905         memset(msg->seed, 0, 8);
906         memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
907
908         if (dn_db->router) {
909                 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
910                 dn_dn2eth(msg->neighbor, dn->addr);
911         }
912
913         msg->timer   = dn_htons((unsigned short)dn_db->parms.t3);
914         msg->mpd     = 0x00;
915         msg->datalen = 0x02;
916         memset(msg->data, 0xAA, 2);
917
918         pktlen = (__le16 *)skb_push(skb,2);
919         *pktlen = dn_htons(skb->len - 2);
920
921         skb->nh.raw = skb->data;
922
923         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
924 }
925
926
927 #define DRDELAY (5 * HZ)
928
929 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
930 {
931         /* First check time since device went up */
932         if ((jiffies - dn_db->uptime) < DRDELAY)
933                 return 0;
934
935         /* If there is no router, then yes... */
936         if (!dn_db->router)
937                 return 1;
938
939         /* otherwise only if we have a higher priority or.. */
940         if (dn->priority < dn_db->parms.priority)
941                 return 1;
942
943         /* if we have equal priority and a higher node number */
944         if (dn->priority != dn_db->parms.priority)
945                 return 0;
946
947         if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
948                 return 1;
949
950         return 0;
951 }
952
953 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
954 {
955         int n;
956         struct dn_dev *dn_db = dev->dn_ptr;
957         struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
958         struct sk_buff *skb;
959         size_t size;
960         unsigned char *ptr;
961         unsigned char *i1, *i2;
962         __le16 *pktlen;
963         char *src;
964
965         if (mtu2blksize(dev) < (26 + 7))
966                 return;
967
968         n = mtu2blksize(dev) - 26;
969         n /= 7;
970
971         if (n > 32)
972                 n = 32;
973
974         size = 2 + 26 + 7 * n;
975
976         if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
977                 return;
978
979         skb->dev = dev;
980         ptr = skb_put(skb, size);
981
982         *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
983         *ptr++ = 2; /* ECO */
984         *ptr++ = 0;
985         *ptr++ = 0;
986         dn_dn2eth(ptr, ifa->ifa_local);
987         src = ptr;
988         ptr += ETH_ALEN;
989         *ptr++ = dn_db->parms.forwarding == 1 ?
990                         DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
991         *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
992         ptr += 2;
993         *ptr++ = dn_db->parms.priority; /* Priority */
994         *ptr++ = 0; /* Area: Reserved */
995         *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
996         ptr += 2;
997         *ptr++ = 0; /* MPD: Reserved */
998         i1 = ptr++;
999         memset(ptr, 0, 7); /* Name: Reserved */
1000         ptr += 7;
1001         i2 = ptr++;
1002
1003         n = dn_neigh_elist(dev, ptr, n);
1004
1005         *i2 = 7 * n;
1006         *i1 = 8 + *i2;
1007
1008         skb_trim(skb, (27 + *i2));
1009
1010         pktlen = (__le16 *)skb_push(skb, 2);
1011         *pktlen = dn_htons(skb->len - 2);
1012
1013         skb->nh.raw = skb->data;
1014
1015         if (dn_am_i_a_router(dn, dn_db, ifa)) {
1016                 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
1017                 if (skb2) {
1018                         dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
1019                 }
1020         }
1021
1022         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1023 }
1024
1025 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1026 {
1027         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1028
1029         if (dn_db->parms.forwarding == 0)
1030                 dn_send_endnode_hello(dev, ifa);
1031         else
1032                 dn_send_router_hello(dev, ifa);
1033 }
1034
1035 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1036 {
1037         int tdlen = 16;
1038         int size = dev->hard_header_len + 2 + 4 + tdlen;
1039         struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1040         int i;
1041         unsigned char *ptr;
1042         char src[ETH_ALEN];
1043
1044         if (skb == NULL)
1045                 return ;
1046
1047         skb->dev = dev;
1048         skb_push(skb, dev->hard_header_len);
1049         ptr = skb_put(skb, 2 + 4 + tdlen);
1050
1051         *ptr++ = DN_RT_PKT_HELO;
1052         *((__le16 *)ptr) = ifa->ifa_local;
1053         ptr += 2;
1054         *ptr++ = tdlen;
1055
1056         for(i = 0; i < tdlen; i++)
1057                 *ptr++ = 0252;
1058
1059         dn_dn2eth(src, ifa->ifa_local);
1060         dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1061 }
1062
1063 static int dn_eth_up(struct net_device *dev)
1064 {
1065         struct dn_dev *dn_db = dev->dn_ptr;
1066
1067         if (dn_db->parms.forwarding == 0)
1068                 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1069         else
1070                 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1071
1072         dev_mc_upload(dev);
1073
1074         dn_db->use_long = 1;
1075
1076         return 0;
1077 }
1078
1079 static void dn_eth_down(struct net_device *dev)
1080 {
1081         struct dn_dev *dn_db = dev->dn_ptr;
1082
1083         if (dn_db->parms.forwarding == 0)
1084                 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1085         else
1086                 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1087 }
1088
1089 static void dn_dev_set_timer(struct net_device *dev);
1090
1091 static void dn_dev_timer_func(unsigned long arg)
1092 {
1093         struct net_device *dev = (struct net_device *)arg;
1094         struct dn_dev *dn_db = dev->dn_ptr;
1095         struct dn_ifaddr *ifa;
1096
1097         if (dn_db->t3 <= dn_db->parms.t2) {
1098                 if (dn_db->parms.timer3) {
1099                         for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1100                                 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1101                                         dn_db->parms.timer3(dev, ifa);
1102                         }
1103                 }
1104                 dn_db->t3 = dn_db->parms.t3;
1105         } else {
1106                 dn_db->t3 -= dn_db->parms.t2;
1107         }
1108
1109         dn_dev_set_timer(dev);
1110 }
1111
1112 static void dn_dev_set_timer(struct net_device *dev)
1113 {
1114         struct dn_dev *dn_db = dev->dn_ptr;
1115
1116         if (dn_db->parms.t2 > dn_db->parms.t3)
1117                 dn_db->parms.t2 = dn_db->parms.t3;
1118
1119         dn_db->timer.data = (unsigned long)dev;
1120         dn_db->timer.function = dn_dev_timer_func;
1121         dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1122
1123         add_timer(&dn_db->timer);
1124 }
1125
1126 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1127 {
1128         int i;
1129         struct dn_dev_parms *p = dn_dev_list;
1130         struct dn_dev *dn_db;
1131
1132         for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1133                 if (p->type == dev->type)
1134                         break;
1135         }
1136
1137         *err = -ENODEV;
1138         if (i == DN_DEV_LIST_SIZE)
1139                 return NULL;
1140
1141         *err = -ENOBUFS;
1142         if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1143                 return NULL;
1144
1145         memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1146         smp_wmb();
1147         dev->dn_ptr = dn_db;
1148         dn_db->dev = dev;
1149         init_timer(&dn_db->timer);
1150
1151         dn_db->uptime = jiffies;
1152
1153         dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1154         if (!dn_db->neigh_parms) {
1155                 dev->dn_ptr = NULL;
1156                 kfree(dn_db);
1157                 return NULL;
1158         }
1159
1160         if (dn_db->parms.up) {
1161                 if (dn_db->parms.up(dev) < 0) {
1162                         neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1163                         dev->dn_ptr = NULL;
1164                         kfree(dn_db);
1165                         return NULL;
1166                 }
1167         }
1168
1169         dn_dev_sysctl_register(dev, &dn_db->parms);
1170
1171         dn_dev_set_timer(dev);
1172
1173         *err = 0;
1174         return dn_db;
1175 }
1176
1177
1178 /*
1179  * This processes a device up event. We only start up
1180  * the loopback device & ethernet devices with correct
1181  * MAC addreses automatically. Others must be started
1182  * specifically.
1183  *
1184  * FIXME: How should we configure the loopback address ? If we could dispense
1185  * with using decnet_address here and for autobind, it will be one less thing
1186  * for users to worry about setting up.
1187  */
1188
1189 void dn_dev_up(struct net_device *dev)
1190 {
1191         struct dn_ifaddr *ifa;
1192         __le16 addr = decnet_address;
1193         int maybe_default = 0;
1194         struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1195
1196         if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1197                 return;
1198
1199         /*
1200          * Need to ensure that loopback device has a dn_db attached to it
1201          * to allow creation of neighbours against it, even though it might
1202          * not have a local address of its own. Might as well do the same for
1203          * all autoconfigured interfaces.
1204          */
1205         if (dn_db == NULL) {
1206                 int err;
1207                 dn_db = dn_dev_create(dev, &err);
1208                 if (dn_db == NULL)
1209                         return;
1210         }
1211
1212         if (dev->type == ARPHRD_ETHER) {
1213                 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1214                         return;
1215                 addr = dn_eth2dn(dev->dev_addr);
1216                 maybe_default = 1;
1217         }
1218
1219         if (addr == 0)
1220                 return;
1221
1222         if ((ifa = dn_dev_alloc_ifa()) == NULL)
1223                 return;
1224
1225         ifa->ifa_local = ifa->ifa_address = addr;
1226         ifa->ifa_flags = 0;
1227         ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1228         strcpy(ifa->ifa_label, dev->name);
1229
1230         dn_dev_set_ifa(dev, ifa);
1231
1232         /*
1233          * Automagically set the default device to the first automatically
1234          * configured ethernet card in the system.
1235          */
1236         if (maybe_default) {
1237                 dev_hold(dev);
1238                 if (dn_dev_set_default(dev, 0))
1239                         dev_put(dev);
1240         }
1241 }
1242
1243 static void dn_dev_delete(struct net_device *dev)
1244 {
1245         struct dn_dev *dn_db = dev->dn_ptr;
1246
1247         if (dn_db == NULL)
1248                 return;
1249
1250         del_timer_sync(&dn_db->timer);
1251         dn_dev_sysctl_unregister(&dn_db->parms);
1252         dn_dev_check_default(dev);
1253         neigh_ifdown(&dn_neigh_table, dev);
1254
1255         if (dn_db->parms.down)
1256                 dn_db->parms.down(dev);
1257
1258         dev->dn_ptr = NULL;
1259
1260         neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1261         neigh_ifdown(&dn_neigh_table, dev);
1262
1263         if (dn_db->router)
1264                 neigh_release(dn_db->router);
1265         if (dn_db->peer)
1266                 neigh_release(dn_db->peer);
1267
1268         kfree(dn_db);
1269 }
1270
1271 void dn_dev_down(struct net_device *dev)
1272 {
1273         struct dn_dev *dn_db = dev->dn_ptr;
1274         struct dn_ifaddr *ifa;
1275
1276         if (dn_db == NULL)
1277                 return;
1278
1279         while((ifa = dn_db->ifa_list) != NULL) {
1280                 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1281                 dn_dev_free_ifa(ifa);
1282         }
1283
1284         dn_dev_delete(dev);
1285 }
1286
1287 void dn_dev_init_pkt(struct sk_buff *skb)
1288 {
1289         return;
1290 }
1291
1292 void dn_dev_veri_pkt(struct sk_buff *skb)
1293 {
1294         return;
1295 }
1296
1297 void dn_dev_hello(struct sk_buff *skb)
1298 {
1299         return;
1300 }
1301
1302 void dn_dev_devices_off(void)
1303 {
1304         struct net_device *dev;
1305
1306         rtnl_lock();
1307         for(dev = dev_base; dev; dev = dev->next)
1308                 dn_dev_down(dev);
1309         rtnl_unlock();
1310
1311 }
1312
1313 void dn_dev_devices_on(void)
1314 {
1315         struct net_device *dev;
1316
1317         rtnl_lock();
1318         for(dev = dev_base; dev; dev = dev->next) {
1319                 if (dev->flags & IFF_UP)
1320                         dn_dev_up(dev);
1321         }
1322         rtnl_unlock();
1323 }
1324
1325 int register_dnaddr_notifier(struct notifier_block *nb)
1326 {
1327         return blocking_notifier_chain_register(&dnaddr_chain, nb);
1328 }
1329
1330 int unregister_dnaddr_notifier(struct notifier_block *nb)
1331 {
1332         return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1333 }
1334
1335 #ifdef CONFIG_PROC_FS
1336 static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1337 {
1338         do {
1339                 dev = dev->next;
1340         } while(dev && !dev->dn_ptr);
1341
1342         return dev;
1343 }
1344
1345 static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1346 {
1347         struct net_device *dev;
1348
1349         dev = dev_base;
1350         if (dev && !dev->dn_ptr)
1351                 dev = dn_dev_get_next(seq, dev);
1352         if (pos) {
1353                 while(dev && (dev = dn_dev_get_next(seq, dev)))
1354                         --pos;
1355         }
1356         return dev;
1357 }
1358
1359 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1360 {
1361         if (*pos) {
1362                 struct net_device *dev;
1363                 read_lock(&dev_base_lock);
1364                 dev = dn_dev_get_idx(seq, *pos - 1);
1365                 if (dev == NULL)
1366                         read_unlock(&dev_base_lock);
1367                 return dev;
1368         }
1369         return SEQ_START_TOKEN;
1370 }
1371
1372 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1373 {
1374         struct net_device *dev = v;
1375         loff_t one = 1;
1376
1377         if (v == SEQ_START_TOKEN) {
1378                 dev = dn_dev_seq_start(seq, &one);
1379         } else {
1380                 dev = dn_dev_get_next(seq, dev);
1381                 if (dev == NULL)
1382                         read_unlock(&dev_base_lock);
1383         }
1384         ++*pos;
1385         return dev;
1386 }
1387
1388 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1389 {
1390         if (v && v != SEQ_START_TOKEN)
1391                 read_unlock(&dev_base_lock);
1392 }
1393
1394 static char *dn_type2asc(char type)
1395 {
1396         switch(type) {
1397                 case DN_DEV_BCAST:
1398                         return "B";
1399                 case DN_DEV_UCAST:
1400                         return "U";
1401                 case DN_DEV_MPOINT:
1402                         return "M";
1403         }
1404
1405         return "?";
1406 }
1407
1408 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1409 {
1410         if (v == SEQ_START_TOKEN)
1411                 seq_puts(seq, "Name     Flags T1   Timer1 T3   Timer3 BlkSize Pri State DevType    Router Peer\n");
1412         else {
1413                 struct net_device *dev = v;
1414                 char peer_buf[DN_ASCBUF_LEN];
1415                 char router_buf[DN_ASCBUF_LEN];
1416                 struct dn_dev *dn_db = dev->dn_ptr;
1417
1418                 seq_printf(seq, "%-8s %1s     %04u %04u   %04lu %04lu"
1419                                 "   %04hu    %03d %02x    %-10s %-7s %-7s\n",
1420                                 dev->name ? dev->name : "???",
1421                                 dn_type2asc(dn_db->parms.mode),
1422                                 0, 0,
1423                                 dn_db->t3, dn_db->parms.t3,
1424                                 mtu2blksize(dev),
1425                                 dn_db->parms.priority,
1426                                 dn_db->parms.state, dn_db->parms.name,
1427                                 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1428                                 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1429         }
1430         return 0;
1431 }
1432
1433 static struct seq_operations dn_dev_seq_ops = {
1434         .start  = dn_dev_seq_start,
1435         .next   = dn_dev_seq_next,
1436         .stop   = dn_dev_seq_stop,
1437         .show   = dn_dev_seq_show,
1438 };
1439
1440 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1441 {
1442         return seq_open(file, &dn_dev_seq_ops);
1443 }
1444
1445 static struct file_operations dn_dev_seq_fops = {
1446         .owner   = THIS_MODULE,
1447         .open    = dn_dev_seq_open,
1448         .read    = seq_read,
1449         .llseek  = seq_lseek,
1450         .release = seq_release,
1451 };
1452
1453 #endif /* CONFIG_PROC_FS */
1454
1455 static struct rtnetlink_link dnet_rtnetlink_table[RTM_NR_MSGTYPES] =
1456 {
1457         [RTM_NEWADDR  - RTM_BASE] = { .doit     = dn_nl_newaddr,        },
1458         [RTM_DELADDR  - RTM_BASE] = { .doit     = dn_nl_deladdr,        },
1459         [RTM_GETADDR  - RTM_BASE] = { .dumpit   = dn_nl_dump_ifaddr,    },
1460 #ifdef CONFIG_DECNET_ROUTER
1461         [RTM_NEWROUTE - RTM_BASE] = { .doit     = dn_fib_rtm_newroute,  },
1462         [RTM_DELROUTE - RTM_BASE] = { .doit     = dn_fib_rtm_delroute,  },
1463         [RTM_GETROUTE - RTM_BASE] = { .doit     = dn_cache_getroute,
1464                                       .dumpit   = dn_fib_dump,          },
1465         [RTM_GETRULE  - RTM_BASE] = { .dumpit   = dn_fib_dump_rules,    },
1466 #else
1467         [RTM_GETROUTE - RTM_BASE] = { .doit     = dn_cache_getroute,
1468                                       .dumpit   = dn_cache_dump,        },
1469 #endif
1470
1471 };
1472
1473 static int __initdata addr[2];
1474 module_param_array(addr, int, NULL, 0444);
1475 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1476
1477 void __init dn_dev_init(void)
1478 {
1479         if (addr[0] > 63 || addr[0] < 0) {
1480                 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1481                 return;
1482         }
1483
1484         if (addr[1] > 1023 || addr[1] < 0) {
1485                 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1486                 return;
1487         }
1488
1489         decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1490
1491         dn_dev_devices_on();
1492
1493         rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1494
1495         proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1496
1497 #ifdef CONFIG_SYSCTL
1498         {
1499                 int i;
1500                 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1501                         dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1502         }
1503 #endif /* CONFIG_SYSCTL */
1504 }
1505
1506 void __exit dn_dev_cleanup(void)
1507 {
1508         rtnetlink_links[PF_DECnet] = NULL;
1509
1510 #ifdef CONFIG_SYSCTL
1511         {
1512                 int i;
1513                 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1514                         dn_dev_sysctl_unregister(&dn_dev_list[i]);
1515         }
1516 #endif /* CONFIG_SYSCTL */
1517
1518         proc_net_remove("decnet_dev");
1519
1520         dn_dev_devices_off();
1521 }