blob: 5c2a9951b638346691b4801d27be5d036018c155 [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,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800170 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
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 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900227 .ctl_name = 0,
228 .procname = "",
229 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 .child = dn_dev_sysctl.dn_dev_vars
231 }, {0}},
232 {{
233 .ctl_name = NET_DECNET_CONF,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900234 .procname = "conf",
235 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 .child = dn_dev_sysctl.dn_dev_dev
237 }, {0}},
238 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900239 .ctl_name = NET_DECNET,
240 .procname = "decnet",
241 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .child = dn_dev_sysctl.dn_dev_conf_dir
243 }, {0}},
244 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900245 .ctl_name = CTL_NET,
246 .procname = "net",
247 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .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
Arnaldo Carvalho de Meloc66b7212006-11-17 12:29:21 -0200257 t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (t == NULL)
259 return;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
262 long offset = (long)t->dn_dev_vars[i].data;
263 t->dn_dev_vars[i].data = ((char *)parms) + offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
266 if (dev) {
267 t->dn_dev_dev[0].procname = dev->name;
268 t->dn_dev_dev[0].ctl_name = dev->ifindex;
269 } else {
270 t->dn_dev_dev[0].procname = parms->name;
271 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
272 }
273
274 t->dn_dev_dev[0].child = t->dn_dev_vars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 t->dn_dev_vars[0].extra1 = (void *)dev;
279
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800280 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (t->sysctl_header == NULL)
282 kfree(t);
283 else
284 parms->sysctl = t;
285}
286
287static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
288{
289 if (parms->sysctl) {
290 struct dn_dev_sysctl_table *t = parms->sysctl;
291 parms->sysctl = NULL;
292 unregister_sysctl_table(t->sysctl_header);
293 kfree(t);
294 }
295}
296
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900297static int dn_forwarding_proc(ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 struct file *filep,
299 void __user *buffer,
300 size_t *lenp, loff_t *ppos)
301{
302#ifdef CONFIG_DECNET_ROUTER
303 struct net_device *dev = table->extra1;
304 struct dn_dev *dn_db;
305 int err;
306 int tmp, old;
307
308 if (table->extra1 == NULL)
309 return -EINVAL;
310
311 dn_db = dev->dn_ptr;
312 old = dn_db->parms.forwarding;
313
314 err = proc_dointvec(table, write, filep, buffer, lenp, ppos);
315
316 if ((err >= 0) && write) {
317 if (dn_db->parms.forwarding < 0)
318 dn_db->parms.forwarding = 0;
319 if (dn_db->parms.forwarding > 2)
320 dn_db->parms.forwarding = 2;
321 /*
322 * What an ugly hack this is... its works, just. It
323 * would be nice if sysctl/proc were just that little
324 * bit more flexible so I don't have to write a special
325 * routine, or suffer hacks like this - SJW
326 */
327 tmp = dn_db->parms.forwarding;
328 dn_db->parms.forwarding = old;
329 if (dn_db->parms.down)
330 dn_db->parms.down(dev);
331 dn_db->parms.forwarding = tmp;
332 if (dn_db->parms.up)
333 dn_db->parms.up(dev);
334 }
335
336 return err;
337#else
338 return -EINVAL;
339#endif
340}
341
342static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
343 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800344 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346#ifdef CONFIG_DECNET_ROUTER
347 struct net_device *dev = table->extra1;
348 struct dn_dev *dn_db;
349 int value;
350
351 if (table->extra1 == NULL)
352 return -EINVAL;
353
354 dn_db = dev->dn_ptr;
355
356 if (newval && newlen) {
357 if (newlen != sizeof(int))
358 return -EINVAL;
359
360 if (get_user(value, (int __user *)newval))
361 return -EFAULT;
362 if (value < 0)
363 return -EINVAL;
364 if (value > 2)
365 return -EINVAL;
366
367 if (dn_db->parms.down)
368 dn_db->parms.down(dev);
369 dn_db->parms.forwarding = value;
370 if (dn_db->parms.up)
371 dn_db->parms.up(dev);
372 }
373
374 return 0;
375#else
376 return -EINVAL;
377#endif
378}
379
380#else /* CONFIG_SYSCTL */
381static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
382{
383}
384static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
385{
386}
387
388#endif /* CONFIG_SYSCTL */
389
390static inline __u16 mtu2blksize(struct net_device *dev)
391{
392 u32 blksize = dev->mtu;
393 if (blksize > 0xffff)
394 blksize = 0xffff;
395
396 if (dev->type == ARPHRD_ETHER ||
397 dev->type == ARPHRD_PPP ||
398 dev->type == ARPHRD_IPGRE ||
399 dev->type == ARPHRD_LOOPBACK)
400 blksize -= 2;
401
402 return (__u16)blksize;
403}
404
405static struct dn_ifaddr *dn_dev_alloc_ifa(void)
406{
407 struct dn_ifaddr *ifa;
408
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700409 ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 return ifa;
412}
413
414static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
415{
416 kfree(ifa);
417}
418
419static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
420{
421 struct dn_ifaddr *ifa1 = *ifap;
422 unsigned char mac_addr[6];
423 struct net_device *dev = dn_db->dev;
424
425 ASSERT_RTNL();
426
427 *ifap = ifa1->ifa_next;
428
429 if (dn_db->dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800430 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 dn_dn2eth(mac_addr, ifa1->ifa_local);
432 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
433 }
434 }
435
Thomas Grafb020b942006-11-24 17:14:31 -0800436 dn_ifaddr_notify(RTM_DELADDR, ifa1);
Alan Sterne041c682006-03-27 01:16:30 -0800437 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (destroy) {
439 dn_dev_free_ifa(ifa1);
440
441 if (dn_db->ifa_list == NULL)
442 dn_dev_delete(dn_db->dev);
443 }
444}
445
446static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
447{
448 struct net_device *dev = dn_db->dev;
449 struct dn_ifaddr *ifa1;
450 unsigned char mac_addr[6];
451
452 ASSERT_RTNL();
453
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900454 /* Check for duplicates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
456 if (ifa1->ifa_local == ifa->ifa_local)
457 return -EEXIST;
458 }
459
460 if (dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800461 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 dn_dn2eth(mac_addr, ifa->ifa_local);
463 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
464 dev_mc_upload(dev);
465 }
466 }
467
468 ifa->ifa_next = dn_db->ifa_list;
469 dn_db->ifa_list = ifa;
470
Thomas Grafb020b942006-11-24 17:14:31 -0800471 dn_ifaddr_notify(RTM_NEWADDR, ifa);
Alan Sterne041c682006-03-27 01:16:30 -0800472 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 return 0;
475}
476
477static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
478{
479 struct dn_dev *dn_db = dev->dn_ptr;
480 int rv;
481
482 if (dn_db == NULL) {
483 int err;
484 dn_db = dn_dev_create(dev, &err);
485 if (dn_db == NULL)
486 return err;
487 }
488
489 ifa->ifa_dev = dn_db;
490
491 if (dev->flags & IFF_LOOPBACK)
492 ifa->ifa_scope = RT_SCOPE_HOST;
493
494 rv = dn_dev_insert_ifa(dn_db, ifa);
495 if (rv)
496 dn_dev_free_ifa(ifa);
497 return rv;
498}
499
500
501int dn_dev_ioctl(unsigned int cmd, void __user *arg)
502{
503 char buffer[DN_IFREQ_SIZE];
504 struct ifreq *ifr = (struct ifreq *)buffer;
505 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
506 struct dn_dev *dn_db;
507 struct net_device *dev;
508 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
509 int ret = 0;
510
511 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
512 return -EFAULT;
513 ifr->ifr_name[IFNAMSIZ-1] = 0;
514
515#ifdef CONFIG_KMOD
516 dev_load(ifr->ifr_name);
517#endif
518
519 switch(cmd) {
520 case SIOCGIFADDR:
521 break;
522 case SIOCSIFADDR:
523 if (!capable(CAP_NET_ADMIN))
524 return -EACCES;
525 if (sdn->sdn_family != AF_DECnet)
526 return -EINVAL;
527 break;
528 default:
529 return -EINVAL;
530 }
531
532 rtnl_lock();
533
534 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
535 ret = -ENODEV;
536 goto done;
537 }
538
539 if ((dn_db = dev->dn_ptr) != NULL) {
540 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
541 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
542 break;
543 }
544
545 if (ifa == NULL && cmd != SIOCSIFADDR) {
546 ret = -EADDRNOTAVAIL;
547 goto done;
548 }
549
550 switch(cmd) {
551 case SIOCGIFADDR:
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800552 *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 goto rarok;
554
555 case SIOCSIFADDR:
556 if (!ifa) {
557 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
558 ret = -ENOBUFS;
559 break;
560 }
561 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
562 } else {
563 if (ifa->ifa_local == dn_saddr2dn(sdn))
564 break;
565 dn_dev_del_ifa(dn_db, ifap, 0);
566 }
567
568 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
569
570 ret = dn_dev_set_ifa(dev, ifa);
571 }
572done:
573 rtnl_unlock();
574
575 return ret;
576rarok:
577 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
578 ret = -EFAULT;
579 goto done;
580}
581
582struct net_device *dn_dev_get_default(void)
583{
584 struct net_device *dev;
585 read_lock(&dndev_lock);
586 dev = decnet_default_device;
587 if (dev) {
588 if (dev->dn_ptr)
589 dev_hold(dev);
590 else
591 dev = NULL;
592 }
593 read_unlock(&dndev_lock);
594 return dev;
595}
596
597int dn_dev_set_default(struct net_device *dev, int force)
598{
599 struct net_device *old = NULL;
600 int rv = -EBUSY;
601 if (!dev->dn_ptr)
602 return -ENODEV;
603 write_lock(&dndev_lock);
604 if (force || decnet_default_device == NULL) {
605 old = decnet_default_device;
606 decnet_default_device = dev;
607 rv = 0;
608 }
609 write_unlock(&dndev_lock);
610 if (old)
Patrick Caulfield6a57b2e2006-03-29 13:57:31 -0800611 dev_put(old);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return rv;
613}
614
615static void dn_dev_check_default(struct net_device *dev)
616{
617 write_lock(&dndev_lock);
618 if (dev == decnet_default_device) {
619 decnet_default_device = NULL;
620 } else {
621 dev = NULL;
622 }
623 write_unlock(&dndev_lock);
624 if (dev)
625 dev_put(dev);
626}
627
628static struct dn_dev *dn_dev_by_index(int ifindex)
629{
630 struct net_device *dev;
631 struct dn_dev *dn_dev = NULL;
632 dev = dev_get_by_index(ifindex);
633 if (dev) {
634 dn_dev = dev->dn_ptr;
635 dev_put(dev);
636 }
637
638 return dn_dev;
639}
640
Thomas Graf4a89c252006-11-24 17:14:51 -0800641static struct nla_policy dn_ifa_policy[IFA_MAX+1] __read_mostly = {
642 [IFA_ADDRESS] = { .type = NLA_U16 },
643 [IFA_LOCAL] = { .type = NLA_U16 },
644 [IFA_LABEL] = { .type = NLA_STRING,
645 .len = IFNAMSIZ - 1 },
646};
647
648static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Thomas Graf4a89c252006-11-24 17:14:51 -0800650 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct dn_dev *dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800652 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 struct dn_ifaddr *ifa, **ifap;
Thomas Graf4a89c252006-11-24 17:14:51 -0800654 int err = -EADDRNOTAVAIL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Thomas Graf4a89c252006-11-24 17:14:51 -0800656 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
657 if (err < 0)
658 goto errout;
659
660 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
Thomas Graf4a89c252006-11-24 17:14:51 -0800662 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Thomas Graf4a89c252006-11-24 17:14:51 -0800664 for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
665 if (tb[IFA_LOCAL] &&
666 nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
667 continue;
668
669 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
671
672 dn_dev_del_ifa(dn_db, ifap, 1);
673 return 0;
674 }
675
Thomas Graf4a89c252006-11-24 17:14:51 -0800676errout:
677 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Thomas Graf4a89c252006-11-24 17:14:51 -0800680static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Thomas Graf4a89c252006-11-24 17:14:51 -0800682 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct net_device *dev;
684 struct dn_dev *dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800685 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 struct dn_ifaddr *ifa;
Thomas Graf4a89c252006-11-24 17:14:51 -0800687 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Thomas Graf4a89c252006-11-24 17:14:51 -0800689 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
690 if (err < 0)
691 return err;
692
693 if (tb[IFA_LOCAL] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695
Thomas Graf4a89c252006-11-24 17:14:51 -0800696 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
698 return -ENODEV;
699
700 if ((dn_db = dev->dn_ptr) == NULL) {
701 int err;
702 dn_db = dn_dev_create(dev, &err);
703 if (!dn_db)
704 return err;
705 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if ((ifa = dn_dev_alloc_ifa()) == NULL)
708 return -ENOBUFS;
709
Thomas Graf4a89c252006-11-24 17:14:51 -0800710 if (tb[IFA_ADDRESS] == NULL)
711 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
712
713 ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
714 ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ifa->ifa_flags = ifm->ifa_flags;
716 ifa->ifa_scope = ifm->ifa_scope;
717 ifa->ifa_dev = dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800718
719 if (tb[IFA_LABEL])
720 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 else
722 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
723
Thomas Graf4a89c252006-11-24 17:14:51 -0800724 err = dn_dev_insert_ifa(dn_db, ifa);
725 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 dn_dev_free_ifa(ifa);
Thomas Graf4a89c252006-11-24 17:14:51 -0800727
728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800731static inline size_t dn_ifaddr_nlmsg_size(void)
732{
733 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
734 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
735 + nla_total_size(2) /* IFA_ADDRESS */
736 + nla_total_size(2); /* IFA_LOCAL */
737}
738
Thomas Graf4a89c252006-11-24 17:14:51 -0800739static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
740 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
742 struct ifaddrmsg *ifm;
743 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Thomas Graf4a89c252006-11-24 17:14:51 -0800745 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
746 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800747 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Thomas Graf4a89c252006-11-24 17:14:51 -0800749 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 ifm->ifa_family = AF_DECnet;
751 ifm->ifa_prefixlen = 16;
752 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
753 ifm->ifa_scope = ifa->ifa_scope;
754 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Thomas Graf4a89c252006-11-24 17:14:51 -0800756 if (ifa->ifa_address)
757 NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
758 if (ifa->ifa_local)
759 NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
760 if (ifa->ifa_label[0])
761 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
762
763 return nlmsg_end(skb, nlh);
764
765nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800766 nlmsg_cancel(skb, nlh);
767 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Thomas Grafb020b942006-11-24 17:14:31 -0800770static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct sk_buff *skb;
Thomas Grafdc738dd2006-08-15 00:33:35 -0700773 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800775 skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
Thomas Grafdc738dd2006-08-15 00:33:35 -0700776 if (skb == NULL)
777 goto errout;
778
Thomas Graf4a89c252006-11-24 17:14:51 -0800779 err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800780 if (err < 0) {
781 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
782 WARN_ON(err == -EMSGSIZE);
783 kfree_skb(skb);
784 goto errout;
785 }
Thomas Grafdc738dd2006-08-15 00:33:35 -0700786 err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
787errout:
788 if (err < 0)
789 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790}
791
Thomas Graf4a89c252006-11-24 17:14:51 -0800792static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Thomas Graf4a89c252006-11-24 17:14:51 -0800794 int idx, dn_idx = 0, skip_ndevs, skip_naddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 struct net_device *dev;
796 struct dn_dev *dn_db;
797 struct dn_ifaddr *ifa;
798
Thomas Graf4a89c252006-11-24 17:14:51 -0800799 skip_ndevs = cb->args[0];
800 skip_naddr = cb->args[1];
801
Thomas Graf4a89c252006-11-24 17:14:51 -0800802 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
803 if (idx < skip_ndevs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 continue;
Thomas Graf4a89c252006-11-24 17:14:51 -0800805 else if (idx > skip_ndevs) {
806 /* Only skip over addresses for first dev dumped
807 * in this iteration (idx == skip_ndevs) */
808 skip_naddr = 0;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if ((dn_db = dev->dn_ptr) == NULL)
812 continue;
813
Thomas Graf4a89c252006-11-24 17:14:51 -0800814 for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
815 ifa = ifa->ifa_next, dn_idx++) {
816 if (dn_idx < skip_naddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 continue;
818
Thomas Graf4a89c252006-11-24 17:14:51 -0800819 if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
820 cb->nlh->nlmsg_seq, RTM_NEWADDR,
821 NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto done;
823 }
824 }
825done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 cb->args[0] = idx;
827 cb->args[1] = dn_idx;
828
829 return skb->len;
830}
831
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800832static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
835 struct dn_ifaddr *ifa;
836 int rv = -ENODEV;
837 if (dn_db == NULL)
838 goto out;
839 ifa = dn_db->ifa_list;
840 if (ifa != NULL) {
841 *addr = ifa->ifa_local;
842 rv = 0;
843 }
844out:
845 return rv;
846}
847
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900848/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 * Find a default address to bind to.
850 *
851 * This is one of those areas where the initial VMS concepts don't really
852 * map onto the Linux concepts, and since we introduced multiple addresses
853 * per interface we have to cope with slightly odd ways of finding out what
854 * "our address" really is. Mostly it's not a problem; for this we just guess
855 * a sensible default. Eventually the routing code will take care of all the
856 * nasties for us I hope.
857 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800858int dn_dev_bind_default(__le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 struct net_device *dev;
861 int rv;
862 dev = dn_dev_get_default();
863last_chance:
864 if (dev) {
865 read_lock(&dev_base_lock);
866 rv = dn_dev_get_first(dev, addr);
867 read_unlock(&dev_base_lock);
868 dev_put(dev);
869 if (rv == 0 || dev == &loopback_dev)
870 return rv;
871 }
872 dev = &loopback_dev;
873 dev_hold(dev);
874 goto last_chance;
875}
876
877static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
878{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900879 struct endnode_hello_message *msg;
880 struct sk_buff *skb = NULL;
881 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
883
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900884 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return;
886
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900887 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900889 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900891 msg->msgflg = 0x0D;
892 memcpy(msg->tiver, dn_eco_version, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 dn_dn2eth(msg->id, ifa->ifa_local);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900894 msg->iinfo = DN_RT_INFO_ENDN;
895 msg->blksize = dn_htons(mtu2blksize(dev));
896 msg->area = 0x00;
897 memset(msg->seed, 0, 8);
898 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (dn_db->router) {
901 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
902 dn_dn2eth(msg->neighbor, dn->addr);
903 }
904
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900905 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
906 msg->mpd = 0x00;
907 msg->datalen = 0x02;
908 memset(msg->data, 0xAA, 2);
909
910 pktlen = (__le16 *)skb_push(skb,2);
911 *pktlen = dn_htons(skb->len - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700913 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
916}
917
918
919#define DRDELAY (5 * HZ)
920
921static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
922{
923 /* First check time since device went up */
924 if ((jiffies - dn_db->uptime) < DRDELAY)
925 return 0;
926
927 /* If there is no router, then yes... */
928 if (!dn_db->router)
929 return 1;
930
931 /* otherwise only if we have a higher priority or.. */
932 if (dn->priority < dn_db->parms.priority)
933 return 1;
934
935 /* if we have equal priority and a higher node number */
936 if (dn->priority != dn_db->parms.priority)
937 return 0;
938
939 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
940 return 1;
941
942 return 0;
943}
944
945static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
946{
947 int n;
948 struct dn_dev *dn_db = dev->dn_ptr;
949 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
950 struct sk_buff *skb;
951 size_t size;
952 unsigned char *ptr;
953 unsigned char *i1, *i2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800954 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 char *src;
956
957 if (mtu2blksize(dev) < (26 + 7))
958 return;
959
960 n = mtu2blksize(dev) - 26;
961 n /= 7;
962
963 if (n > 32)
964 n = 32;
965
966 size = 2 + 26 + 7 * n;
967
968 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
969 return;
970
971 skb->dev = dev;
972 ptr = skb_put(skb, size);
973
974 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
975 *ptr++ = 2; /* ECO */
976 *ptr++ = 0;
977 *ptr++ = 0;
978 dn_dn2eth(ptr, ifa->ifa_local);
979 src = ptr;
980 ptr += ETH_ALEN;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900981 *ptr++ = dn_db->parms.forwarding == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800983 *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ptr += 2;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900985 *ptr++ = dn_db->parms.priority; /* Priority */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 *ptr++ = 0; /* Area: Reserved */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800987 *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ptr += 2;
989 *ptr++ = 0; /* MPD: Reserved */
990 i1 = ptr++;
991 memset(ptr, 0, 7); /* Name: Reserved */
992 ptr += 7;
993 i2 = ptr++;
994
995 n = dn_neigh_elist(dev, ptr, n);
996
997 *i2 = 7 * n;
998 *i1 = 8 + *i2;
999
1000 skb_trim(skb, (27 + *i2));
1001
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001002 pktlen = (__le16 *)skb_push(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 *pktlen = dn_htons(skb->len - 2);
1004
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001005 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (dn_am_i_a_router(dn, dn_db, ifa)) {
1008 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
1009 if (skb2) {
1010 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
1011 }
1012 }
1013
1014 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1015}
1016
1017static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1018{
1019 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1020
1021 if (dn_db->parms.forwarding == 0)
1022 dn_send_endnode_hello(dev, ifa);
1023 else
1024 dn_send_router_hello(dev, ifa);
1025}
1026
1027static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1028{
1029 int tdlen = 16;
1030 int size = dev->hard_header_len + 2 + 4 + tdlen;
1031 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1032 int i;
1033 unsigned char *ptr;
1034 char src[ETH_ALEN];
1035
1036 if (skb == NULL)
1037 return ;
1038
1039 skb->dev = dev;
1040 skb_push(skb, dev->hard_header_len);
1041 ptr = skb_put(skb, 2 + 4 + tdlen);
1042
1043 *ptr++ = DN_RT_PKT_HELO;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001044 *((__le16 *)ptr) = ifa->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 ptr += 2;
1046 *ptr++ = tdlen;
1047
1048 for(i = 0; i < tdlen; i++)
1049 *ptr++ = 0252;
1050
1051 dn_dn2eth(src, ifa->ifa_local);
1052 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1053}
1054
1055static int dn_eth_up(struct net_device *dev)
1056{
1057 struct dn_dev *dn_db = dev->dn_ptr;
1058
1059 if (dn_db->parms.forwarding == 0)
1060 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1061 else
1062 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1063
1064 dev_mc_upload(dev);
1065
1066 dn_db->use_long = 1;
1067
1068 return 0;
1069}
1070
1071static void dn_eth_down(struct net_device *dev)
1072{
1073 struct dn_dev *dn_db = dev->dn_ptr;
1074
1075 if (dn_db->parms.forwarding == 0)
1076 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1077 else
1078 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1079}
1080
1081static void dn_dev_set_timer(struct net_device *dev);
1082
1083static void dn_dev_timer_func(unsigned long arg)
1084{
1085 struct net_device *dev = (struct net_device *)arg;
1086 struct dn_dev *dn_db = dev->dn_ptr;
1087 struct dn_ifaddr *ifa;
1088
1089 if (dn_db->t3 <= dn_db->parms.t2) {
1090 if (dn_db->parms.timer3) {
1091 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1092 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1093 dn_db->parms.timer3(dev, ifa);
1094 }
1095 }
1096 dn_db->t3 = dn_db->parms.t3;
1097 } else {
1098 dn_db->t3 -= dn_db->parms.t2;
1099 }
1100
1101 dn_dev_set_timer(dev);
1102}
1103
1104static void dn_dev_set_timer(struct net_device *dev)
1105{
1106 struct dn_dev *dn_db = dev->dn_ptr;
1107
1108 if (dn_db->parms.t2 > dn_db->parms.t3)
1109 dn_db->parms.t2 = dn_db->parms.t3;
1110
1111 dn_db->timer.data = (unsigned long)dev;
1112 dn_db->timer.function = dn_dev_timer_func;
1113 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1114
1115 add_timer(&dn_db->timer);
1116}
1117
1118struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1119{
1120 int i;
1121 struct dn_dev_parms *p = dn_dev_list;
1122 struct dn_dev *dn_db;
1123
1124 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1125 if (p->type == dev->type)
1126 break;
1127 }
1128
1129 *err = -ENODEV;
1130 if (i == DN_DEV_LIST_SIZE)
1131 return NULL;
1132
1133 *err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001134 if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return NULL;
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1138 smp_wmb();
1139 dev->dn_ptr = dn_db;
1140 dn_db->dev = dev;
1141 init_timer(&dn_db->timer);
1142
1143 dn_db->uptime = jiffies;
Eric W. Biederman95743de2007-01-25 15:51:51 -08001144
1145 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1146 if (!dn_db->neigh_parms) {
1147 dev->dn_ptr = NULL;
1148 kfree(dn_db);
1149 return NULL;
1150 }
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (dn_db->parms.up) {
1153 if (dn_db->parms.up(dev) < 0) {
Eric W. Biederman95743de2007-01-25 15:51:51 -08001154 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 dev->dn_ptr = NULL;
1156 kfree(dn_db);
1157 return NULL;
1158 }
1159 }
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 dn_dev_sysctl_register(dev, &dn_db->parms);
1162
1163 dn_dev_set_timer(dev);
1164
1165 *err = 0;
1166 return dn_db;
1167}
1168
1169
1170/*
1171 * This processes a device up event. We only start up
1172 * the loopback device & ethernet devices with correct
1173 * MAC addreses automatically. Others must be started
1174 * specifically.
1175 *
1176 * FIXME: How should we configure the loopback address ? If we could dispense
1177 * with using decnet_address here and for autobind, it will be one less thing
1178 * for users to worry about setting up.
1179 */
1180
1181void dn_dev_up(struct net_device *dev)
1182{
1183 struct dn_ifaddr *ifa;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001184 __le16 addr = decnet_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 int maybe_default = 0;
1186 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1187
1188 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1189 return;
1190
1191 /*
1192 * Need to ensure that loopback device has a dn_db attached to it
1193 * to allow creation of neighbours against it, even though it might
1194 * not have a local address of its own. Might as well do the same for
1195 * all autoconfigured interfaces.
1196 */
1197 if (dn_db == NULL) {
1198 int err;
1199 dn_db = dn_dev_create(dev, &err);
1200 if (dn_db == NULL)
1201 return;
1202 }
1203
1204 if (dev->type == ARPHRD_ETHER) {
1205 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1206 return;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001207 addr = dn_eth2dn(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 maybe_default = 1;
1209 }
1210
1211 if (addr == 0)
1212 return;
1213
1214 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1215 return;
1216
1217 ifa->ifa_local = ifa->ifa_address = addr;
1218 ifa->ifa_flags = 0;
1219 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1220 strcpy(ifa->ifa_label, dev->name);
1221
1222 dn_dev_set_ifa(dev, ifa);
1223
1224 /*
1225 * Automagically set the default device to the first automatically
1226 * configured ethernet card in the system.
1227 */
1228 if (maybe_default) {
1229 dev_hold(dev);
1230 if (dn_dev_set_default(dev, 0))
1231 dev_put(dev);
1232 }
1233}
1234
1235static void dn_dev_delete(struct net_device *dev)
1236{
1237 struct dn_dev *dn_db = dev->dn_ptr;
1238
1239 if (dn_db == NULL)
1240 return;
1241
1242 del_timer_sync(&dn_db->timer);
1243 dn_dev_sysctl_unregister(&dn_db->parms);
1244 dn_dev_check_default(dev);
1245 neigh_ifdown(&dn_neigh_table, dev);
1246
1247 if (dn_db->parms.down)
1248 dn_db->parms.down(dev);
1249
1250 dev->dn_ptr = NULL;
1251
1252 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1253 neigh_ifdown(&dn_neigh_table, dev);
1254
1255 if (dn_db->router)
1256 neigh_release(dn_db->router);
1257 if (dn_db->peer)
1258 neigh_release(dn_db->peer);
1259
1260 kfree(dn_db);
1261}
1262
1263void dn_dev_down(struct net_device *dev)
1264{
1265 struct dn_dev *dn_db = dev->dn_ptr;
1266 struct dn_ifaddr *ifa;
1267
1268 if (dn_db == NULL)
1269 return;
1270
1271 while((ifa = dn_db->ifa_list) != NULL) {
1272 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1273 dn_dev_free_ifa(ifa);
1274 }
1275
1276 dn_dev_delete(dev);
1277}
1278
1279void dn_dev_init_pkt(struct sk_buff *skb)
1280{
1281 return;
1282}
1283
1284void dn_dev_veri_pkt(struct sk_buff *skb)
1285{
1286 return;
1287}
1288
1289void dn_dev_hello(struct sk_buff *skb)
1290{
1291 return;
1292}
1293
1294void dn_dev_devices_off(void)
1295{
1296 struct net_device *dev;
1297
1298 rtnl_lock();
1299 for(dev = dev_base; dev; dev = dev->next)
1300 dn_dev_down(dev);
1301 rtnl_unlock();
1302
1303}
1304
1305void dn_dev_devices_on(void)
1306{
1307 struct net_device *dev;
1308
1309 rtnl_lock();
1310 for(dev = dev_base; dev; dev = dev->next) {
1311 if (dev->flags & IFF_UP)
1312 dn_dev_up(dev);
1313 }
1314 rtnl_unlock();
1315}
1316
1317int register_dnaddr_notifier(struct notifier_block *nb)
1318{
Alan Sterne041c682006-03-27 01:16:30 -08001319 return blocking_notifier_chain_register(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
1322int unregister_dnaddr_notifier(struct notifier_block *nb)
1323{
Alan Sterne041c682006-03-27 01:16:30 -08001324 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
1327#ifdef CONFIG_PROC_FS
1328static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1329{
1330 do {
1331 dev = dev->next;
1332 } while(dev && !dev->dn_ptr);
1333
1334 return dev;
1335}
1336
1337static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1338{
1339 struct net_device *dev;
1340
1341 dev = dev_base;
1342 if (dev && !dev->dn_ptr)
1343 dev = dn_dev_get_next(seq, dev);
1344 if (pos) {
1345 while(dev && (dev = dn_dev_get_next(seq, dev)))
1346 --pos;
1347 }
1348 return dev;
1349}
1350
1351static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1352{
1353 if (*pos) {
1354 struct net_device *dev;
1355 read_lock(&dev_base_lock);
1356 dev = dn_dev_get_idx(seq, *pos - 1);
1357 if (dev == NULL)
1358 read_unlock(&dev_base_lock);
1359 return dev;
1360 }
1361 return SEQ_START_TOKEN;
1362}
1363
1364static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1365{
1366 struct net_device *dev = v;
1367 loff_t one = 1;
1368
1369 if (v == SEQ_START_TOKEN) {
1370 dev = dn_dev_seq_start(seq, &one);
1371 } else {
1372 dev = dn_dev_get_next(seq, dev);
1373 if (dev == NULL)
1374 read_unlock(&dev_base_lock);
1375 }
1376 ++*pos;
1377 return dev;
1378}
1379
1380static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1381{
1382 if (v && v != SEQ_START_TOKEN)
1383 read_unlock(&dev_base_lock);
1384}
1385
1386static char *dn_type2asc(char type)
1387{
1388 switch(type) {
1389 case DN_DEV_BCAST:
1390 return "B";
1391 case DN_DEV_UCAST:
1392 return "U";
1393 case DN_DEV_MPOINT:
1394 return "M";
1395 }
1396
1397 return "?";
1398}
1399
1400static int dn_dev_seq_show(struct seq_file *seq, void *v)
1401{
1402 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001403 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 else {
1405 struct net_device *dev = v;
1406 char peer_buf[DN_ASCBUF_LEN];
1407 char router_buf[DN_ASCBUF_LEN];
1408 struct dn_dev *dn_db = dev->dn_ptr;
1409
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001410 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 " %04hu %03d %02x %-10s %-7s %-7s\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001412 dev->name ? dev->name : "???",
1413 dn_type2asc(dn_db->parms.mode),
1414 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 dn_db->t3, dn_db->parms.t3,
1416 mtu2blksize(dev),
1417 dn_db->parms.priority,
1418 dn_db->parms.state, dn_db->parms.name,
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001419 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1420 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
1422 return 0;
1423}
1424
1425static struct seq_operations dn_dev_seq_ops = {
1426 .start = dn_dev_seq_start,
1427 .next = dn_dev_seq_next,
1428 .stop = dn_dev_seq_stop,
1429 .show = dn_dev_seq_show,
1430};
1431
1432static int dn_dev_seq_open(struct inode *inode, struct file *file)
1433{
1434 return seq_open(file, &dn_dev_seq_ops);
1435}
1436
Arjan van de Ven9a321442007-02-12 00:55:35 -08001437static const struct file_operations dn_dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 .owner = THIS_MODULE,
1439 .open = dn_dev_seq_open,
1440 .read = seq_read,
1441 .llseek = seq_lseek,
1442 .release = seq_release,
1443};
1444
1445#endif /* CONFIG_PROC_FS */
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447static int __initdata addr[2];
1448module_param_array(addr, int, NULL, 0444);
1449MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1450
1451void __init dn_dev_init(void)
1452{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001453 if (addr[0] > 63 || addr[0] < 0) {
1454 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1455 return;
1456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001458 if (addr[1] > 1023 || addr[1] < 0) {
1459 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1460 return;
1461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001463 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 dn_dev_devices_on();
1466
Thomas Graffa34ddd2007-03-22 11:57:46 -07001467 rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL);
1468 rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL);
1469 rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1472
1473#ifdef CONFIG_SYSCTL
1474 {
1475 int i;
1476 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1477 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1478 }
1479#endif /* CONFIG_SYSCTL */
1480}
1481
1482void __exit dn_dev_cleanup(void)
1483{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484#ifdef CONFIG_SYSCTL
1485 {
1486 int i;
1487 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1488 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1489 }
1490#endif /* CONFIG_SYSCTL */
1491
1492 proc_net_remove("decnet_dev");
1493
1494 dn_dev_devices_off();
1495}