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