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