blob: 0b9d4c9551543df799890cca4887f4d6adeec00f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#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>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070048#include <net/fib_rules.h>
Thomas Grafa6f01ca2006-11-24 17:14:07 -080049#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/dn.h>
51#include <net/dn_dev.h>
52#include <net/dn_route.h>
53#include <net/dn_neigh.h>
54#include <net/dn_fib.h>
55
56#define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
57
58static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
59static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
60static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
61static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
62
63extern struct neigh_table dn_neigh_table;
64
65/*
66 * decnet_address is kept in network order.
67 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080068__le16 decnet_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70static DEFINE_RWLOCK(dndev_lock);
71static struct net_device *decnet_default_device;
Alan Sterne041c682006-03-27 01:16:30 -080072static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
75static void dn_dev_delete(struct net_device *dev);
Thomas Grafb020b942006-11-24 17:14:31 -080076static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static int dn_eth_up(struct net_device *);
79static void dn_eth_down(struct net_device *);
80static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
81static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
82
83static struct dn_dev_parms dn_dev_list[] = {
84{
85 .type = ARPHRD_ETHER, /* Ethernet */
86 .mode = DN_DEV_BCAST,
87 .state = DN_DEV_S_RU,
88 .t2 = 1,
89 .t3 = 10,
90 .name = "ethernet",
91 .ctl_name = NET_DECNET_CONF_ETHER,
92 .up = dn_eth_up,
93 .down = dn_eth_down,
94 .timer3 = dn_send_brd_hello,
95},
96{
97 .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
98 .mode = DN_DEV_BCAST,
99 .state = DN_DEV_S_RU,
100 .t2 = 1,
101 .t3 = 10,
102 .name = "ipgre",
103 .ctl_name = NET_DECNET_CONF_GRE,
104 .timer3 = dn_send_brd_hello,
105},
106#if 0
107{
108 .type = ARPHRD_X25, /* Bog standard X.25 */
109 .mode = DN_DEV_UCAST,
110 .state = DN_DEV_S_DS,
111 .t2 = 1,
112 .t3 = 120,
113 .name = "x25",
114 .ctl_name = NET_DECNET_CONF_X25,
115 .timer3 = dn_send_ptp_hello,
116},
117#endif
118#if 0
119{
120 .type = ARPHRD_PPP, /* DECnet over PPP */
121 .mode = DN_DEV_BCAST,
122 .state = DN_DEV_S_RU,
123 .t2 = 1,
124 .t3 = 10,
125 .name = "ppp",
126 .ctl_name = NET_DECNET_CONF_PPP,
127 .timer3 = dn_send_brd_hello,
128},
129#endif
130{
131 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
132 .mode = DN_DEV_UCAST,
133 .state = DN_DEV_S_DS,
134 .t2 = 1,
135 .t3 = 120,
136 .name = "ddcmp",
137 .ctl_name = NET_DECNET_CONF_DDCMP,
138 .timer3 = dn_send_ptp_hello,
139},
140{
141 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
142 .mode = DN_DEV_BCAST,
143 .state = DN_DEV_S_RU,
144 .t2 = 1,
145 .t3 = 10,
146 .name = "loopback",
147 .ctl_name = NET_DECNET_CONF_LOOPBACK,
148 .timer3 = dn_send_brd_hello,
149}
150};
151
152#define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
153
154#define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
155
156#ifdef CONFIG_SYSCTL
157
158static int min_t2[] = { 1 };
159static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
160static int min_t3[] = { 1 };
161static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
162
163static int min_priority[1];
164static int max_priority[] = { 127 }; /* From DECnet spec */
165
166static int dn_forwarding_proc(ctl_table *, int, struct file *,
167 void __user *, size_t *, loff_t *);
168static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
169 void __user *oldval, size_t __user *oldlenp,
170 void __user *newval, size_t newlen,
171 void **context);
172
173static struct dn_dev_sysctl_table {
174 struct ctl_table_header *sysctl_header;
175 ctl_table dn_dev_vars[5];
176 ctl_table dn_dev_dev[2];
177 ctl_table dn_dev_conf_dir[2];
178 ctl_table dn_dev_proto_dir[2];
179 ctl_table dn_dev_root_dir[2];
180} dn_dev_sysctl = {
181 NULL,
182 {
183 {
184 .ctl_name = NET_DECNET_CONF_DEV_FORWARDING,
185 .procname = "forwarding",
186 .data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
187 .maxlen = sizeof(int),
188 .mode = 0644,
189 .proc_handler = dn_forwarding_proc,
190 .strategy = dn_forwarding_sysctl,
191 },
192 {
193 .ctl_name = NET_DECNET_CONF_DEV_PRIORITY,
194 .procname = "priority",
195 .data = (void *)DN_DEV_PARMS_OFFSET(priority),
196 .maxlen = sizeof(int),
197 .mode = 0644,
198 .proc_handler = proc_dointvec_minmax,
199 .strategy = sysctl_intvec,
200 .extra1 = &min_priority,
201 .extra2 = &max_priority
202 },
203 {
204 .ctl_name = NET_DECNET_CONF_DEV_T2,
205 .procname = "t2",
206 .data = (void *)DN_DEV_PARMS_OFFSET(t2),
207 .maxlen = sizeof(int),
208 .mode = 0644,
209 .proc_handler = proc_dointvec_minmax,
210 .strategy = sysctl_intvec,
211 .extra1 = &min_t2,
212 .extra2 = &max_t2
213 },
214 {
215 .ctl_name = NET_DECNET_CONF_DEV_T3,
216 .procname = "t3",
217 .data = (void *)DN_DEV_PARMS_OFFSET(t3),
218 .maxlen = sizeof(int),
219 .mode = 0644,
220 .proc_handler = proc_dointvec_minmax,
221 .strategy = sysctl_intvec,
222 .extra1 = &min_t3,
223 .extra2 = &max_t3
224 },
225 {0}
226 },
227 {{
228 .ctl_name = 0,
229 .procname = "",
230 .mode = 0555,
231 .child = dn_dev_sysctl.dn_dev_vars
232 }, {0}},
233 {{
234 .ctl_name = NET_DECNET_CONF,
235 .procname = "conf",
236 .mode = 0555,
237 .child = dn_dev_sysctl.dn_dev_dev
238 }, {0}},
239 {{
240 .ctl_name = NET_DECNET,
241 .procname = "decnet",
242 .mode = 0555,
243 .child = dn_dev_sysctl.dn_dev_conf_dir
244 }, {0}},
245 {{
246 .ctl_name = CTL_NET,
247 .procname = "net",
248 .mode = 0555,
249 .child = dn_dev_sysctl.dn_dev_proto_dir
250 }, {0}}
251};
252
253static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
254{
255 struct dn_dev_sysctl_table *t;
256 int i;
257
Arnaldo Carvalho de Meloc66b7212006-11-17 12:29:21 -0200258 t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (t == NULL)
260 return;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 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
293static 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
303static 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
348static 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 */
388static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
389{
390}
391static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
392{
393}
394
395#endif /* CONFIG_SYSCTL */
396
397static 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
412static struct dn_ifaddr *dn_dev_alloc_ifa(void)
413{
414 struct dn_ifaddr *ifa;
415
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700416 ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 return ifa;
419}
420
421static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
422{
423 kfree(ifa);
424}
425
426static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
427{
428 struct dn_ifaddr *ifa1 = *ifap;
429 unsigned char mac_addr[6];
430 struct net_device *dev = dn_db->dev;
431
432 ASSERT_RTNL();
433
434 *ifap = ifa1->ifa_next;
435
436 if (dn_db->dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800437 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 dn_dn2eth(mac_addr, ifa1->ifa_local);
439 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
440 }
441 }
442
Thomas Grafb020b942006-11-24 17:14:31 -0800443 dn_ifaddr_notify(RTM_DELADDR, ifa1);
Alan Sterne041c682006-03-27 01:16:30 -0800444 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (destroy) {
446 dn_dev_free_ifa(ifa1);
447
448 if (dn_db->ifa_list == NULL)
449 dn_dev_delete(dn_db->dev);
450 }
451}
452
453static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
454{
455 struct net_device *dev = dn_db->dev;
456 struct dn_ifaddr *ifa1;
457 unsigned char mac_addr[6];
458
459 ASSERT_RTNL();
460
461 /* Check for duplicates */
462 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
463 if (ifa1->ifa_local == ifa->ifa_local)
464 return -EEXIST;
465 }
466
467 if (dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800468 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dn_dn2eth(mac_addr, ifa->ifa_local);
470 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
471 dev_mc_upload(dev);
472 }
473 }
474
475 ifa->ifa_next = dn_db->ifa_list;
476 dn_db->ifa_list = ifa;
477
Thomas Grafb020b942006-11-24 17:14:31 -0800478 dn_ifaddr_notify(RTM_NEWADDR, ifa);
Alan Sterne041c682006-03-27 01:16:30 -0800479 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return 0;
482}
483
484static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
485{
486 struct dn_dev *dn_db = dev->dn_ptr;
487 int rv;
488
489 if (dn_db == NULL) {
490 int err;
491 dn_db = dn_dev_create(dev, &err);
492 if (dn_db == NULL)
493 return err;
494 }
495
496 ifa->ifa_dev = dn_db;
497
498 if (dev->flags & IFF_LOOPBACK)
499 ifa->ifa_scope = RT_SCOPE_HOST;
500
501 rv = dn_dev_insert_ifa(dn_db, ifa);
502 if (rv)
503 dn_dev_free_ifa(ifa);
504 return rv;
505}
506
507
508int dn_dev_ioctl(unsigned int cmd, void __user *arg)
509{
510 char buffer[DN_IFREQ_SIZE];
511 struct ifreq *ifr = (struct ifreq *)buffer;
512 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
513 struct dn_dev *dn_db;
514 struct net_device *dev;
515 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
516 int ret = 0;
517
518 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
519 return -EFAULT;
520 ifr->ifr_name[IFNAMSIZ-1] = 0;
521
522#ifdef CONFIG_KMOD
523 dev_load(ifr->ifr_name);
524#endif
525
526 switch(cmd) {
527 case SIOCGIFADDR:
528 break;
529 case SIOCSIFADDR:
530 if (!capable(CAP_NET_ADMIN))
531 return -EACCES;
532 if (sdn->sdn_family != AF_DECnet)
533 return -EINVAL;
534 break;
535 default:
536 return -EINVAL;
537 }
538
539 rtnl_lock();
540
541 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
542 ret = -ENODEV;
543 goto done;
544 }
545
546 if ((dn_db = dev->dn_ptr) != NULL) {
547 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
548 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
549 break;
550 }
551
552 if (ifa == NULL && cmd != SIOCSIFADDR) {
553 ret = -EADDRNOTAVAIL;
554 goto done;
555 }
556
557 switch(cmd) {
558 case SIOCGIFADDR:
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800559 *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto rarok;
561
562 case SIOCSIFADDR:
563 if (!ifa) {
564 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
565 ret = -ENOBUFS;
566 break;
567 }
568 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
569 } else {
570 if (ifa->ifa_local == dn_saddr2dn(sdn))
571 break;
572 dn_dev_del_ifa(dn_db, ifap, 0);
573 }
574
575 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
576
577 ret = dn_dev_set_ifa(dev, ifa);
578 }
579done:
580 rtnl_unlock();
581
582 return ret;
583rarok:
584 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
585 ret = -EFAULT;
586 goto done;
587}
588
589struct net_device *dn_dev_get_default(void)
590{
591 struct net_device *dev;
592 read_lock(&dndev_lock);
593 dev = decnet_default_device;
594 if (dev) {
595 if (dev->dn_ptr)
596 dev_hold(dev);
597 else
598 dev = NULL;
599 }
600 read_unlock(&dndev_lock);
601 return dev;
602}
603
604int dn_dev_set_default(struct net_device *dev, int force)
605{
606 struct net_device *old = NULL;
607 int rv = -EBUSY;
608 if (!dev->dn_ptr)
609 return -ENODEV;
610 write_lock(&dndev_lock);
611 if (force || decnet_default_device == NULL) {
612 old = decnet_default_device;
613 decnet_default_device = dev;
614 rv = 0;
615 }
616 write_unlock(&dndev_lock);
617 if (old)
Patrick Caulfield6a57b2e2006-03-29 13:57:31 -0800618 dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return rv;
620}
621
622static void dn_dev_check_default(struct net_device *dev)
623{
624 write_lock(&dndev_lock);
625 if (dev == decnet_default_device) {
626 decnet_default_device = NULL;
627 } else {
628 dev = NULL;
629 }
630 write_unlock(&dndev_lock);
631 if (dev)
632 dev_put(dev);
633}
634
635static struct dn_dev *dn_dev_by_index(int ifindex)
636{
637 struct net_device *dev;
638 struct dn_dev *dn_dev = NULL;
639 dev = dev_get_by_index(ifindex);
640 if (dev) {
641 dn_dev = dev->dn_ptr;
642 dev_put(dev);
643 }
644
645 return dn_dev;
646}
647
Thomas Graf4a89c252006-11-24 17:14:51 -0800648static struct nla_policy dn_ifa_policy[IFA_MAX+1] __read_mostly = {
649 [IFA_ADDRESS] = { .type = NLA_U16 },
650 [IFA_LOCAL] = { .type = NLA_U16 },
651 [IFA_LABEL] = { .type = NLA_STRING,
652 .len = IFNAMSIZ - 1 },
653};
654
655static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Thomas Graf4a89c252006-11-24 17:14:51 -0800657 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 struct dn_dev *dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800659 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 struct dn_ifaddr *ifa, **ifap;
Thomas Graf4a89c252006-11-24 17:14:51 -0800661 int err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Thomas Graf4a89c252006-11-24 17:14:51 -0800663 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
664 if (err < 0)
665 goto errout;
666
667 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
Thomas Graf4a89c252006-11-24 17:14:51 -0800669 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Thomas Graf4a89c252006-11-24 17:14:51 -0800671 for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
672 if (tb[IFA_LOCAL] &&
673 nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
674 continue;
675
676 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 continue;
678
679 dn_dev_del_ifa(dn_db, ifap, 1);
680 return 0;
681 }
682
Thomas Graf4a89c252006-11-24 17:14:51 -0800683errout:
684 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Thomas Graf4a89c252006-11-24 17:14:51 -0800687static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Thomas Graf4a89c252006-11-24 17:14:51 -0800689 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct net_device *dev;
691 struct dn_dev *dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800692 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct dn_ifaddr *ifa;
Thomas Graf4a89c252006-11-24 17:14:51 -0800694 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Thomas Graf4a89c252006-11-24 17:14:51 -0800696 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
697 if (err < 0)
698 return err;
699
700 if (tb[IFA_LOCAL] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return -EINVAL;
702
Thomas Graf4a89c252006-11-24 17:14:51 -0800703 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
705 return -ENODEV;
706
707 if ((dn_db = dev->dn_ptr) == NULL) {
708 int err;
709 dn_db = dn_dev_create(dev, &err);
710 if (!dn_db)
711 return err;
712 }
713
714 if ((ifa = dn_dev_alloc_ifa()) == NULL)
715 return -ENOBUFS;
716
Thomas Graf4a89c252006-11-24 17:14:51 -0800717 if (tb[IFA_ADDRESS] == NULL)
718 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
719
720 ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
721 ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ifa->ifa_flags = ifm->ifa_flags;
723 ifa->ifa_scope = ifm->ifa_scope;
724 ifa->ifa_dev = dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800725
726 if (tb[IFA_LABEL])
727 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 else
729 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
730
Thomas Graf4a89c252006-11-24 17:14:51 -0800731 err = dn_dev_insert_ifa(dn_db, ifa);
732 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 dn_dev_free_ifa(ifa);
Thomas Graf4a89c252006-11-24 17:14:51 -0800734
735 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800738static inline size_t dn_ifaddr_nlmsg_size(void)
739{
740 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
741 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
742 + nla_total_size(2) /* IFA_ADDRESS */
743 + nla_total_size(2); /* IFA_LOCAL */
744}
745
Thomas Graf4a89c252006-11-24 17:14:51 -0800746static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
747 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
749 struct ifaddrmsg *ifm;
750 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Thomas Graf4a89c252006-11-24 17:14:51 -0800752 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
753 if (nlh == NULL)
754 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Thomas Graf4a89c252006-11-24 17:14:51 -0800756 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 ifm->ifa_family = AF_DECnet;
758 ifm->ifa_prefixlen = 16;
759 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
760 ifm->ifa_scope = ifa->ifa_scope;
761 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Thomas Graf4a89c252006-11-24 17:14:51 -0800763 if (ifa->ifa_address)
764 NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
765 if (ifa->ifa_local)
766 NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
767 if (ifa->ifa_label[0])
768 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
769
770 return nlmsg_end(skb, nlh);
771
772nla_put_failure:
773 return nlmsg_cancel(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Thomas Grafb020b942006-11-24 17:14:31 -0800776static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
778 struct sk_buff *skb;
Thomas Grafdc738dd2006-08-15 00:33:35 -0700779 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800781 skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
Thomas Grafdc738dd2006-08-15 00:33:35 -0700782 if (skb == NULL)
783 goto errout;
784
Thomas Graf4a89c252006-11-24 17:14:51 -0800785 err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800786 /* failure implies BUG in dn_ifaddr_nlmsg_size() */
787 BUG_ON(err < 0);
Thomas Grafdc738dd2006-08-15 00:33:35 -0700788
789 err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
790errout:
791 if (err < 0)
792 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Thomas Graf4a89c252006-11-24 17:14:51 -0800795static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Thomas Graf4a89c252006-11-24 17:14:51 -0800797 int idx, dn_idx = 0, skip_ndevs, skip_naddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct net_device *dev;
799 struct dn_dev *dn_db;
800 struct dn_ifaddr *ifa;
801
Thomas Graf4a89c252006-11-24 17:14:51 -0800802 skip_ndevs = cb->args[0];
803 skip_naddr = cb->args[1];
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 read_lock(&dev_base_lock);
Thomas Graf4a89c252006-11-24 17:14:51 -0800806 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
807 if (idx < skip_ndevs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 continue;
Thomas Graf4a89c252006-11-24 17:14:51 -0800809 else if (idx > skip_ndevs) {
810 /* Only skip over addresses for first dev dumped
811 * in this iteration (idx == skip_ndevs) */
812 skip_naddr = 0;
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if ((dn_db = dev->dn_ptr) == NULL)
816 continue;
817
Thomas Graf4a89c252006-11-24 17:14:51 -0800818 for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
819 ifa = ifa->ifa_next, dn_idx++) {
820 if (dn_idx < skip_naddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 continue;
822
Thomas Graf4a89c252006-11-24 17:14:51 -0800823 if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
824 cb->nlh->nlmsg_seq, RTM_NEWADDR,
825 NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto done;
827 }
828 }
829done:
830 read_unlock(&dev_base_lock);
Thomas Graf4a89c252006-11-24 17:14:51 -0800831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 cb->args[0] = idx;
833 cb->args[1] = dn_idx;
834
835 return skb->len;
836}
837
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800838static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
841 struct dn_ifaddr *ifa;
842 int rv = -ENODEV;
843 if (dn_db == NULL)
844 goto out;
845 ifa = dn_db->ifa_list;
846 if (ifa != NULL) {
847 *addr = ifa->ifa_local;
848 rv = 0;
849 }
850out:
851 return rv;
852}
853
854/*
855 * Find a default address to bind to.
856 *
857 * This is one of those areas where the initial VMS concepts don't really
858 * map onto the Linux concepts, and since we introduced multiple addresses
859 * per interface we have to cope with slightly odd ways of finding out what
860 * "our address" really is. Mostly it's not a problem; for this we just guess
861 * a sensible default. Eventually the routing code will take care of all the
862 * nasties for us I hope.
863 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800864int dn_dev_bind_default(__le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct net_device *dev;
867 int rv;
868 dev = dn_dev_get_default();
869last_chance:
870 if (dev) {
871 read_lock(&dev_base_lock);
872 rv = dn_dev_get_first(dev, addr);
873 read_unlock(&dev_base_lock);
874 dev_put(dev);
875 if (rv == 0 || dev == &loopback_dev)
876 return rv;
877 }
878 dev = &loopback_dev;
879 dev_hold(dev);
880 goto last_chance;
881}
882
883static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
884{
885 struct endnode_hello_message *msg;
886 struct sk_buff *skb = NULL;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800887 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
889
890 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
891 return;
892
893 skb->dev = dev;
894
895 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
896
897 msg->msgflg = 0x0D;
898 memcpy(msg->tiver, dn_eco_version, 3);
899 dn_dn2eth(msg->id, ifa->ifa_local);
900 msg->iinfo = DN_RT_INFO_ENDN;
901 msg->blksize = dn_htons(mtu2blksize(dev));
902 msg->area = 0x00;
903 memset(msg->seed, 0, 8);
904 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
905
906 if (dn_db->router) {
907 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
908 dn_dn2eth(msg->neighbor, dn->addr);
909 }
910
911 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
912 msg->mpd = 0x00;
913 msg->datalen = 0x02;
914 memset(msg->data, 0xAA, 2);
915
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800916 pktlen = (__le16 *)skb_push(skb,2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 *pktlen = dn_htons(skb->len - 2);
918
919 skb->nh.raw = skb->data;
920
921 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
922}
923
924
925#define DRDELAY (5 * HZ)
926
927static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
928{
929 /* First check time since device went up */
930 if ((jiffies - dn_db->uptime) < DRDELAY)
931 return 0;
932
933 /* If there is no router, then yes... */
934 if (!dn_db->router)
935 return 1;
936
937 /* otherwise only if we have a higher priority or.. */
938 if (dn->priority < dn_db->parms.priority)
939 return 1;
940
941 /* if we have equal priority and a higher node number */
942 if (dn->priority != dn_db->parms.priority)
943 return 0;
944
945 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
946 return 1;
947
948 return 0;
949}
950
951static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
952{
953 int n;
954 struct dn_dev *dn_db = dev->dn_ptr;
955 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
956 struct sk_buff *skb;
957 size_t size;
958 unsigned char *ptr;
959 unsigned char *i1, *i2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800960 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 char *src;
962
963 if (mtu2blksize(dev) < (26 + 7))
964 return;
965
966 n = mtu2blksize(dev) - 26;
967 n /= 7;
968
969 if (n > 32)
970 n = 32;
971
972 size = 2 + 26 + 7 * n;
973
974 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
975 return;
976
977 skb->dev = dev;
978 ptr = skb_put(skb, size);
979
980 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
981 *ptr++ = 2; /* ECO */
982 *ptr++ = 0;
983 *ptr++ = 0;
984 dn_dn2eth(ptr, ifa->ifa_local);
985 src = ptr;
986 ptr += ETH_ALEN;
987 *ptr++ = dn_db->parms.forwarding == 1 ?
988 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800989 *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 ptr += 2;
991 *ptr++ = dn_db->parms.priority; /* Priority */
992 *ptr++ = 0; /* Area: Reserved */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800993 *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ptr += 2;
995 *ptr++ = 0; /* MPD: Reserved */
996 i1 = ptr++;
997 memset(ptr, 0, 7); /* Name: Reserved */
998 ptr += 7;
999 i2 = ptr++;
1000
1001 n = dn_neigh_elist(dev, ptr, n);
1002
1003 *i2 = 7 * n;
1004 *i1 = 8 + *i2;
1005
1006 skb_trim(skb, (27 + *i2));
1007
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001008 pktlen = (__le16 *)skb_push(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 *pktlen = dn_htons(skb->len - 2);
1010
1011 skb->nh.raw = skb->data;
1012
1013 if (dn_am_i_a_router(dn, dn_db, ifa)) {
1014 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
1015 if (skb2) {
1016 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
1017 }
1018 }
1019
1020 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1021}
1022
1023static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1024{
1025 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1026
1027 if (dn_db->parms.forwarding == 0)
1028 dn_send_endnode_hello(dev, ifa);
1029 else
1030 dn_send_router_hello(dev, ifa);
1031}
1032
1033static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1034{
1035 int tdlen = 16;
1036 int size = dev->hard_header_len + 2 + 4 + tdlen;
1037 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1038 int i;
1039 unsigned char *ptr;
1040 char src[ETH_ALEN];
1041
1042 if (skb == NULL)
1043 return ;
1044
1045 skb->dev = dev;
1046 skb_push(skb, dev->hard_header_len);
1047 ptr = skb_put(skb, 2 + 4 + tdlen);
1048
1049 *ptr++ = DN_RT_PKT_HELO;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001050 *((__le16 *)ptr) = ifa->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 ptr += 2;
1052 *ptr++ = tdlen;
1053
1054 for(i = 0; i < tdlen; i++)
1055 *ptr++ = 0252;
1056
1057 dn_dn2eth(src, ifa->ifa_local);
1058 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1059}
1060
1061static int dn_eth_up(struct net_device *dev)
1062{
1063 struct dn_dev *dn_db = dev->dn_ptr;
1064
1065 if (dn_db->parms.forwarding == 0)
1066 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1067 else
1068 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1069
1070 dev_mc_upload(dev);
1071
1072 dn_db->use_long = 1;
1073
1074 return 0;
1075}
1076
1077static void dn_eth_down(struct net_device *dev)
1078{
1079 struct dn_dev *dn_db = dev->dn_ptr;
1080
1081 if (dn_db->parms.forwarding == 0)
1082 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1083 else
1084 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1085}
1086
1087static void dn_dev_set_timer(struct net_device *dev);
1088
1089static void dn_dev_timer_func(unsigned long arg)
1090{
1091 struct net_device *dev = (struct net_device *)arg;
1092 struct dn_dev *dn_db = dev->dn_ptr;
1093 struct dn_ifaddr *ifa;
1094
1095 if (dn_db->t3 <= dn_db->parms.t2) {
1096 if (dn_db->parms.timer3) {
1097 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1098 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1099 dn_db->parms.timer3(dev, ifa);
1100 }
1101 }
1102 dn_db->t3 = dn_db->parms.t3;
1103 } else {
1104 dn_db->t3 -= dn_db->parms.t2;
1105 }
1106
1107 dn_dev_set_timer(dev);
1108}
1109
1110static void dn_dev_set_timer(struct net_device *dev)
1111{
1112 struct dn_dev *dn_db = dev->dn_ptr;
1113
1114 if (dn_db->parms.t2 > dn_db->parms.t3)
1115 dn_db->parms.t2 = dn_db->parms.t3;
1116
1117 dn_db->timer.data = (unsigned long)dev;
1118 dn_db->timer.function = dn_dev_timer_func;
1119 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1120
1121 add_timer(&dn_db->timer);
1122}
1123
1124struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1125{
1126 int i;
1127 struct dn_dev_parms *p = dn_dev_list;
1128 struct dn_dev *dn_db;
1129
1130 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1131 if (p->type == dev->type)
1132 break;
1133 }
1134
1135 *err = -ENODEV;
1136 if (i == DN_DEV_LIST_SIZE)
1137 return NULL;
1138
1139 *err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001140 if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 return NULL;
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1144 smp_wmb();
1145 dev->dn_ptr = dn_db;
1146 dn_db->dev = dev;
1147 init_timer(&dn_db->timer);
1148
1149 dn_db->uptime = jiffies;
1150 if (dn_db->parms.up) {
1151 if (dn_db->parms.up(dev) < 0) {
1152 dev->dn_ptr = NULL;
1153 kfree(dn_db);
1154 return NULL;
1155 }
1156 }
1157
1158 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1159
1160 dn_dev_sysctl_register(dev, &dn_db->parms);
1161
1162 dn_dev_set_timer(dev);
1163
1164 *err = 0;
1165 return dn_db;
1166}
1167
1168
1169/*
1170 * This processes a device up event. We only start up
1171 * the loopback device & ethernet devices with correct
1172 * MAC addreses automatically. Others must be started
1173 * specifically.
1174 *
1175 * FIXME: How should we configure the loopback address ? If we could dispense
1176 * with using decnet_address here and for autobind, it will be one less thing
1177 * for users to worry about setting up.
1178 */
1179
1180void dn_dev_up(struct net_device *dev)
1181{
1182 struct dn_ifaddr *ifa;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001183 __le16 addr = decnet_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int maybe_default = 0;
1185 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1186
1187 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1188 return;
1189
1190 /*
1191 * Need to ensure that loopback device has a dn_db attached to it
1192 * to allow creation of neighbours against it, even though it might
1193 * not have a local address of its own. Might as well do the same for
1194 * all autoconfigured interfaces.
1195 */
1196 if (dn_db == NULL) {
1197 int err;
1198 dn_db = dn_dev_create(dev, &err);
1199 if (dn_db == NULL)
1200 return;
1201 }
1202
1203 if (dev->type == ARPHRD_ETHER) {
1204 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1205 return;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001206 addr = dn_eth2dn(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 maybe_default = 1;
1208 }
1209
1210 if (addr == 0)
1211 return;
1212
1213 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1214 return;
1215
1216 ifa->ifa_local = ifa->ifa_address = addr;
1217 ifa->ifa_flags = 0;
1218 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1219 strcpy(ifa->ifa_label, dev->name);
1220
1221 dn_dev_set_ifa(dev, ifa);
1222
1223 /*
1224 * Automagically set the default device to the first automatically
1225 * configured ethernet card in the system.
1226 */
1227 if (maybe_default) {
1228 dev_hold(dev);
1229 if (dn_dev_set_default(dev, 0))
1230 dev_put(dev);
1231 }
1232}
1233
1234static void dn_dev_delete(struct net_device *dev)
1235{
1236 struct dn_dev *dn_db = dev->dn_ptr;
1237
1238 if (dn_db == NULL)
1239 return;
1240
1241 del_timer_sync(&dn_db->timer);
1242 dn_dev_sysctl_unregister(&dn_db->parms);
1243 dn_dev_check_default(dev);
1244 neigh_ifdown(&dn_neigh_table, dev);
1245
1246 if (dn_db->parms.down)
1247 dn_db->parms.down(dev);
1248
1249 dev->dn_ptr = NULL;
1250
1251 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1252 neigh_ifdown(&dn_neigh_table, dev);
1253
1254 if (dn_db->router)
1255 neigh_release(dn_db->router);
1256 if (dn_db->peer)
1257 neigh_release(dn_db->peer);
1258
1259 kfree(dn_db);
1260}
1261
1262void dn_dev_down(struct net_device *dev)
1263{
1264 struct dn_dev *dn_db = dev->dn_ptr;
1265 struct dn_ifaddr *ifa;
1266
1267 if (dn_db == NULL)
1268 return;
1269
1270 while((ifa = dn_db->ifa_list) != NULL) {
1271 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1272 dn_dev_free_ifa(ifa);
1273 }
1274
1275 dn_dev_delete(dev);
1276}
1277
1278void dn_dev_init_pkt(struct sk_buff *skb)
1279{
1280 return;
1281}
1282
1283void dn_dev_veri_pkt(struct sk_buff *skb)
1284{
1285 return;
1286}
1287
1288void dn_dev_hello(struct sk_buff *skb)
1289{
1290 return;
1291}
1292
1293void dn_dev_devices_off(void)
1294{
1295 struct net_device *dev;
1296
1297 rtnl_lock();
1298 for(dev = dev_base; dev; dev = dev->next)
1299 dn_dev_down(dev);
1300 rtnl_unlock();
1301
1302}
1303
1304void dn_dev_devices_on(void)
1305{
1306 struct net_device *dev;
1307
1308 rtnl_lock();
1309 for(dev = dev_base; dev; dev = dev->next) {
1310 if (dev->flags & IFF_UP)
1311 dn_dev_up(dev);
1312 }
1313 rtnl_unlock();
1314}
1315
1316int register_dnaddr_notifier(struct notifier_block *nb)
1317{
Alan Sterne041c682006-03-27 01:16:30 -08001318 return blocking_notifier_chain_register(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
1321int unregister_dnaddr_notifier(struct notifier_block *nb)
1322{
Alan Sterne041c682006-03-27 01:16:30 -08001323 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324}
1325
1326#ifdef CONFIG_PROC_FS
1327static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1328{
1329 do {
1330 dev = dev->next;
1331 } while(dev && !dev->dn_ptr);
1332
1333 return dev;
1334}
1335
1336static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1337{
1338 struct net_device *dev;
1339
1340 dev = dev_base;
1341 if (dev && !dev->dn_ptr)
1342 dev = dn_dev_get_next(seq, dev);
1343 if (pos) {
1344 while(dev && (dev = dn_dev_get_next(seq, dev)))
1345 --pos;
1346 }
1347 return dev;
1348}
1349
1350static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1351{
1352 if (*pos) {
1353 struct net_device *dev;
1354 read_lock(&dev_base_lock);
1355 dev = dn_dev_get_idx(seq, *pos - 1);
1356 if (dev == NULL)
1357 read_unlock(&dev_base_lock);
1358 return dev;
1359 }
1360 return SEQ_START_TOKEN;
1361}
1362
1363static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1364{
1365 struct net_device *dev = v;
1366 loff_t one = 1;
1367
1368 if (v == SEQ_START_TOKEN) {
1369 dev = dn_dev_seq_start(seq, &one);
1370 } else {
1371 dev = dn_dev_get_next(seq, dev);
1372 if (dev == NULL)
1373 read_unlock(&dev_base_lock);
1374 }
1375 ++*pos;
1376 return dev;
1377}
1378
1379static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1380{
1381 if (v && v != SEQ_START_TOKEN)
1382 read_unlock(&dev_base_lock);
1383}
1384
1385static char *dn_type2asc(char type)
1386{
1387 switch(type) {
1388 case DN_DEV_BCAST:
1389 return "B";
1390 case DN_DEV_UCAST:
1391 return "U";
1392 case DN_DEV_MPOINT:
1393 return "M";
1394 }
1395
1396 return "?";
1397}
1398
1399static int dn_dev_seq_show(struct seq_file *seq, void *v)
1400{
1401 if (v == SEQ_START_TOKEN)
1402 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1403 else {
1404 struct net_device *dev = v;
1405 char peer_buf[DN_ASCBUF_LEN];
1406 char router_buf[DN_ASCBUF_LEN];
1407 struct dn_dev *dn_db = dev->dn_ptr;
1408
1409 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
1410 " %04hu %03d %02x %-10s %-7s %-7s\n",
1411 dev->name ? dev->name : "???",
1412 dn_type2asc(dn_db->parms.mode),
1413 0, 0,
1414 dn_db->t3, dn_db->parms.t3,
1415 mtu2blksize(dev),
1416 dn_db->parms.priority,
1417 dn_db->parms.state, dn_db->parms.name,
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001418 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1419 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421 return 0;
1422}
1423
1424static struct seq_operations dn_dev_seq_ops = {
1425 .start = dn_dev_seq_start,
1426 .next = dn_dev_seq_next,
1427 .stop = dn_dev_seq_stop,
1428 .show = dn_dev_seq_show,
1429};
1430
1431static int dn_dev_seq_open(struct inode *inode, struct file *file)
1432{
1433 return seq_open(file, &dn_dev_seq_ops);
1434}
1435
1436static struct file_operations dn_dev_seq_fops = {
1437 .owner = THIS_MODULE,
1438 .open = dn_dev_seq_open,
1439 .read = seq_read,
1440 .llseek = seq_lseek,
1441 .release = seq_release,
1442};
1443
1444#endif /* CONFIG_PROC_FS */
1445
Thomas Grafdb46edc2005-05-03 14:29:39 -07001446static struct rtnetlink_link dnet_rtnetlink_table[RTM_NR_MSGTYPES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
Thomas Graf4a89c252006-11-24 17:14:51 -08001448 [RTM_NEWADDR - RTM_BASE] = { .doit = dn_nl_newaddr, },
1449 [RTM_DELADDR - RTM_BASE] = { .doit = dn_nl_deladdr, },
1450 [RTM_GETADDR - RTM_BASE] = { .dumpit = dn_nl_dump_ifaddr, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451#ifdef CONFIG_DECNET_ROUTER
Thomas Grafdb46edc2005-05-03 14:29:39 -07001452 [RTM_NEWROUTE - RTM_BASE] = { .doit = dn_fib_rtm_newroute, },
1453 [RTM_DELROUTE - RTM_BASE] = { .doit = dn_fib_rtm_delroute, },
1454 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
1455 .dumpit = dn_fib_dump, },
Thomas Grafdb46edc2005-05-03 14:29:39 -07001456 [RTM_GETRULE - RTM_BASE] = { .dumpit = dn_fib_dump_rules, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457#else
Thomas Grafdb46edc2005-05-03 14:29:39 -07001458 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
Rafael J. Wysocki25ae3f52005-05-05 13:13:29 -07001459 .dumpit = dn_cache_dump, },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#endif
1461
1462};
1463
1464static int __initdata addr[2];
1465module_param_array(addr, int, NULL, 0444);
1466MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1467
1468void __init dn_dev_init(void)
1469{
1470 if (addr[0] > 63 || addr[0] < 0) {
1471 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1472 return;
1473 }
1474
1475 if (addr[1] > 1023 || addr[1] < 0) {
1476 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1477 return;
1478 }
1479
1480 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1481
1482 dn_dev_devices_on();
1483
1484 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1485
1486 proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1487
1488#ifdef CONFIG_SYSCTL
1489 {
1490 int i;
1491 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1492 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1493 }
1494#endif /* CONFIG_SYSCTL */
1495}
1496
1497void __exit dn_dev_cleanup(void)
1498{
1499 rtnetlink_links[PF_DECnet] = NULL;
1500
1501#ifdef CONFIG_SYSCTL
1502 {
1503 int i;
1504 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1505 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1506 }
1507#endif /* CONFIG_SYSCTL */
1508
1509 proc_net_remove("decnet_dev");
1510
1511 dn_dev_devices_off();
1512}