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