blob: 3bc82dc83b387d33f92d496d81383c9346296f9d [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>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/neighbour.h>
47#include <net/dst.h>
48#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070049#include <net/fib_rules.h>
Thomas Grafa6f01ca2006-11-24 17:14:07 -080050#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/dn.h>
52#include <net/dn_dev.h>
53#include <net/dn_route.h>
54#include <net/dn_neigh.h>
55#include <net/dn_fib.h>
56
57#define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
58
59static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
60static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
61static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
62static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
63
64extern struct neigh_table dn_neigh_table;
65
66/*
67 * decnet_address is kept in network order.
68 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080069__le16 decnet_address = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71static DEFINE_RWLOCK(dndev_lock);
72static struct net_device *decnet_default_device;
Alan Sterne041c682006-03-27 01:16:30 -080073static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
76static void dn_dev_delete(struct net_device *dev);
Thomas Grafb020b942006-11-24 17:14:31 -080077static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int dn_eth_up(struct net_device *);
80static void dn_eth_down(struct net_device *);
81static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
82static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
83
84static struct dn_dev_parms dn_dev_list[] = {
85{
86 .type = ARPHRD_ETHER, /* Ethernet */
87 .mode = DN_DEV_BCAST,
88 .state = DN_DEV_S_RU,
89 .t2 = 1,
90 .t3 = 10,
91 .name = "ethernet",
92 .ctl_name = NET_DECNET_CONF_ETHER,
93 .up = dn_eth_up,
94 .down = dn_eth_down,
95 .timer3 = dn_send_brd_hello,
96},
97{
98 .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
99 .mode = DN_DEV_BCAST,
100 .state = DN_DEV_S_RU,
101 .t2 = 1,
102 .t3 = 10,
103 .name = "ipgre",
104 .ctl_name = NET_DECNET_CONF_GRE,
105 .timer3 = dn_send_brd_hello,
106},
107#if 0
108{
109 .type = ARPHRD_X25, /* Bog standard X.25 */
110 .mode = DN_DEV_UCAST,
111 .state = DN_DEV_S_DS,
112 .t2 = 1,
113 .t3 = 120,
114 .name = "x25",
115 .ctl_name = NET_DECNET_CONF_X25,
116 .timer3 = dn_send_ptp_hello,
117},
118#endif
119#if 0
120{
121 .type = ARPHRD_PPP, /* DECnet over PPP */
122 .mode = DN_DEV_BCAST,
123 .state = DN_DEV_S_RU,
124 .t2 = 1,
125 .t3 = 10,
126 .name = "ppp",
127 .ctl_name = NET_DECNET_CONF_PPP,
128 .timer3 = dn_send_brd_hello,
129},
130#endif
131{
132 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
133 .mode = DN_DEV_UCAST,
134 .state = DN_DEV_S_DS,
135 .t2 = 1,
136 .t3 = 120,
137 .name = "ddcmp",
138 .ctl_name = NET_DECNET_CONF_DDCMP,
139 .timer3 = dn_send_ptp_hello,
140},
141{
142 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
143 .mode = DN_DEV_BCAST,
144 .state = DN_DEV_S_RU,
145 .t2 = 1,
146 .t3 = 10,
147 .name = "loopback",
148 .ctl_name = NET_DECNET_CONF_LOOPBACK,
149 .timer3 = dn_send_brd_hello,
150}
151};
152
Denis Cheng8b14a532007-09-16 16:41:29 -0700153#define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155#define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
156
157#ifdef CONFIG_SYSCTL
158
159static int min_t2[] = { 1 };
160static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
161static int min_t3[] = { 1 };
162static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
163
164static int min_priority[1];
165static int max_priority[] = { 127 }; /* From DECnet spec */
166
167static int dn_forwarding_proc(ctl_table *, int, struct file *,
168 void __user *, size_t *, loff_t *);
169static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
170 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800171 void __user *newval, size_t newlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
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 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900228 .ctl_name = 0,
229 .procname = "",
230 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 .child = dn_dev_sysctl.dn_dev_vars
232 }, {0}},
233 {{
234 .ctl_name = NET_DECNET_CONF,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900235 .procname = "conf",
236 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .child = dn_dev_sysctl.dn_dev_dev
238 }, {0}},
239 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900240 .ctl_name = NET_DECNET,
241 .procname = "decnet",
242 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .child = dn_dev_sysctl.dn_dev_conf_dir
244 }, {0}},
245 {{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900246 .ctl_name = CTL_NET,
247 .procname = "net",
248 .mode = 0555,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 .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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266
267 if (dev) {
268 t->dn_dev_dev[0].procname = dev->name;
269 t->dn_dev_dev[0].ctl_name = dev->ifindex;
270 } else {
271 t->dn_dev_dev[0].procname = parms->name;
272 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
273 }
274
275 t->dn_dev_dev[0].child = t->dn_dev_vars;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 t->dn_dev_vars[0].extra1 = (void *)dev;
280
Eric W. Biederman0b4d4142007-02-14 00:34:09 -0800281 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (t->sysctl_header == NULL)
283 kfree(t);
284 else
285 parms->sysctl = t;
286}
287
288static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
289{
290 if (parms->sysctl) {
291 struct dn_dev_sysctl_table *t = parms->sysctl;
292 parms->sysctl = NULL;
293 unregister_sysctl_table(t->sysctl_header);
294 kfree(t);
295 }
296}
297
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900298static int dn_forwarding_proc(ctl_table *table, int write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct file *filep,
300 void __user *buffer,
301 size_t *lenp, loff_t *ppos)
302{
303#ifdef CONFIG_DECNET_ROUTER
304 struct net_device *dev = table->extra1;
305 struct dn_dev *dn_db;
306 int err;
307 int tmp, old;
308
309 if (table->extra1 == NULL)
310 return -EINVAL;
311
312 dn_db = dev->dn_ptr;
313 old = dn_db->parms.forwarding;
314
315 err = proc_dointvec(table, write, filep, buffer, lenp, ppos);
316
317 if ((err >= 0) && write) {
318 if (dn_db->parms.forwarding < 0)
319 dn_db->parms.forwarding = 0;
320 if (dn_db->parms.forwarding > 2)
321 dn_db->parms.forwarding = 2;
322 /*
323 * What an ugly hack this is... its works, just. It
324 * would be nice if sysctl/proc were just that little
325 * bit more flexible so I don't have to write a special
326 * routine, or suffer hacks like this - SJW
327 */
328 tmp = dn_db->parms.forwarding;
329 dn_db->parms.forwarding = old;
330 if (dn_db->parms.down)
331 dn_db->parms.down(dev);
332 dn_db->parms.forwarding = tmp;
333 if (dn_db->parms.up)
334 dn_db->parms.up(dev);
335 }
336
337 return err;
338#else
339 return -EINVAL;
340#endif
341}
342
343static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
344 void __user *oldval, size_t __user *oldlenp,
Alexey Dobriyan1f29bcd2006-12-10 02:19:10 -0800345 void __user *newval, size_t newlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347#ifdef CONFIG_DECNET_ROUTER
348 struct net_device *dev = table->extra1;
349 struct dn_dev *dn_db;
350 int value;
351
352 if (table->extra1 == NULL)
353 return -EINVAL;
354
355 dn_db = dev->dn_ptr;
356
357 if (newval && newlen) {
358 if (newlen != sizeof(int))
359 return -EINVAL;
360
361 if (get_user(value, (int __user *)newval))
362 return -EFAULT;
363 if (value < 0)
364 return -EINVAL;
365 if (value > 2)
366 return -EINVAL;
367
368 if (dn_db->parms.down)
369 dn_db->parms.down(dev);
370 dn_db->parms.forwarding = value;
371 if (dn_db->parms.up)
372 dn_db->parms.up(dev);
373 }
374
375 return 0;
376#else
377 return -EINVAL;
378#endif
379}
380
381#else /* CONFIG_SYSCTL */
382static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
383{
384}
385static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
386{
387}
388
389#endif /* CONFIG_SYSCTL */
390
391static inline __u16 mtu2blksize(struct net_device *dev)
392{
393 u32 blksize = dev->mtu;
394 if (blksize > 0xffff)
395 blksize = 0xffff;
396
397 if (dev->type == ARPHRD_ETHER ||
398 dev->type == ARPHRD_PPP ||
399 dev->type == ARPHRD_IPGRE ||
400 dev->type == ARPHRD_LOOPBACK)
401 blksize -= 2;
402
403 return (__u16)blksize;
404}
405
406static struct dn_ifaddr *dn_dev_alloc_ifa(void)
407{
408 struct dn_ifaddr *ifa;
409
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700410 ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 return ifa;
413}
414
415static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
416{
417 kfree(ifa);
418}
419
420static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
421{
422 struct dn_ifaddr *ifa1 = *ifap;
423 unsigned char mac_addr[6];
424 struct net_device *dev = dn_db->dev;
425
426 ASSERT_RTNL();
427
428 *ifap = ifa1->ifa_next;
429
430 if (dn_db->dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800431 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 dn_dn2eth(mac_addr, ifa1->ifa_local);
433 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
434 }
435 }
436
Thomas Grafb020b942006-11-24 17:14:31 -0800437 dn_ifaddr_notify(RTM_DELADDR, ifa1);
Alan Sterne041c682006-03-27 01:16:30 -0800438 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (destroy) {
440 dn_dev_free_ifa(ifa1);
441
442 if (dn_db->ifa_list == NULL)
443 dn_dev_delete(dn_db->dev);
444 }
445}
446
447static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
448{
449 struct net_device *dev = dn_db->dev;
450 struct dn_ifaddr *ifa1;
451 unsigned char mac_addr[6];
452
453 ASSERT_RTNL();
454
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900455 /* Check for duplicates */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
457 if (ifa1->ifa_local == ifa->ifa_local)
458 return -EEXIST;
459 }
460
461 if (dev->type == ARPHRD_ETHER) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800462 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 dn_dn2eth(mac_addr, ifa->ifa_local);
464 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
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
Eric W. Biederman881d9662007-09-17 11:56:21 -0700516 dev_load(&init_net, ifr->ifr_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517#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
Eric W. Biederman881d9662007-09-17 11:56:21 -0700534 if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 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;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700632 dev = dev_get_by_index(&init_net, ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (dev) {
634 dn_dev = dev->dn_ptr;
635 dev_put(dev);
636 }
637
638 return dn_dev;
639}
640
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700641static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = {
Thomas Graf4a89c252006-11-24 17:14:51 -0800642 [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;
Pavel Emelyanov3ccd8622007-11-30 23:43:31 +1100654 int err;
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
Pavel Emelyanov3ccd8622007-11-30 23:43:31 +1100660 err = -ENODEV;
Thomas Graf4a89c252006-11-24 17:14:51 -0800661 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
Thomas Graf4a89c252006-11-24 17:14:51 -0800663 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Pavel Emelyanov3ccd8622007-11-30 23:43:31 +1100665 err = -EADDRNOTAVAIL;
Thomas Graf4a89c252006-11-24 17:14:51 -0800666 for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
667 if (tb[IFA_LOCAL] &&
668 nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
669 continue;
670
671 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 continue;
673
674 dn_dev_del_ifa(dn_db, ifap, 1);
675 return 0;
676 }
677
Thomas Graf4a89c252006-11-24 17:14:51 -0800678errout:
679 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Thomas Graf4a89c252006-11-24 17:14:51 -0800682static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Thomas Graf4a89c252006-11-24 17:14:51 -0800684 struct nlattr *tb[IFA_MAX+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 struct net_device *dev;
686 struct dn_dev *dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800687 struct ifaddrmsg *ifm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct dn_ifaddr *ifa;
Thomas Graf4a89c252006-11-24 17:14:51 -0800689 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Thomas Graf4a89c252006-11-24 17:14:51 -0800691 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
692 if (err < 0)
693 return err;
694
695 if (tb[IFA_LOCAL] == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return -EINVAL;
697
Thomas Graf4a89c252006-11-24 17:14:51 -0800698 ifm = nlmsg_data(nlh);
Eric W. Biederman881d9662007-09-17 11:56:21 -0700699 if ((dev = __dev_get_by_index(&init_net, ifm->ifa_index)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return -ENODEV;
701
702 if ((dn_db = dev->dn_ptr) == NULL) {
703 int err;
704 dn_db = dn_dev_create(dev, &err);
705 if (!dn_db)
706 return err;
707 }
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if ((ifa = dn_dev_alloc_ifa()) == NULL)
710 return -ENOBUFS;
711
Thomas Graf4a89c252006-11-24 17:14:51 -0800712 if (tb[IFA_ADDRESS] == NULL)
713 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
714
715 ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
716 ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ifa->ifa_flags = ifm->ifa_flags;
718 ifa->ifa_scope = ifm->ifa_scope;
719 ifa->ifa_dev = dn_db;
Thomas Graf4a89c252006-11-24 17:14:51 -0800720
721 if (tb[IFA_LABEL])
722 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 else
724 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
725
Thomas Graf4a89c252006-11-24 17:14:51 -0800726 err = dn_dev_insert_ifa(dn_db, ifa);
727 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 dn_dev_free_ifa(ifa);
Thomas Graf4a89c252006-11-24 17:14:51 -0800729
730 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800733static inline size_t dn_ifaddr_nlmsg_size(void)
734{
735 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
736 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
737 + nla_total_size(2) /* IFA_ADDRESS */
738 + nla_total_size(2); /* IFA_LOCAL */
739}
740
Thomas Graf4a89c252006-11-24 17:14:51 -0800741static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
742 u32 pid, u32 seq, int event, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
744 struct ifaddrmsg *ifm;
745 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Thomas Graf4a89c252006-11-24 17:14:51 -0800747 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
748 if (nlh == NULL)
Patrick McHardy26932562007-01-31 23:16:40 -0800749 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Thomas Graf4a89c252006-11-24 17:14:51 -0800751 ifm = nlmsg_data(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ifm->ifa_family = AF_DECnet;
753 ifm->ifa_prefixlen = 16;
754 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
755 ifm->ifa_scope = ifa->ifa_scope;
756 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Thomas Graf4a89c252006-11-24 17:14:51 -0800758 if (ifa->ifa_address)
759 NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
760 if (ifa->ifa_local)
761 NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
762 if (ifa->ifa_label[0])
763 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
764
765 return nlmsg_end(skb, nlh);
766
767nla_put_failure:
Patrick McHardy26932562007-01-31 23:16:40 -0800768 nlmsg_cancel(skb, nlh);
769 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Thomas Grafb020b942006-11-24 17:14:31 -0800772static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 struct sk_buff *skb;
Thomas Grafdc738dd2006-08-15 00:33:35 -0700775 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Thomas Grafa6f01ca2006-11-24 17:14:07 -0800777 skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
Thomas Grafdc738dd2006-08-15 00:33:35 -0700778 if (skb == NULL)
779 goto errout;
780
Thomas Graf4a89c252006-11-24 17:14:51 -0800781 err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800782 if (err < 0) {
783 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
784 WARN_ON(err == -EMSGSIZE);
785 kfree_skb(skb);
786 goto errout;
787 }
Thomas Grafdc738dd2006-08-15 00:33:35 -0700788 err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
789errout:
790 if (err < 0)
791 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Thomas Graf4a89c252006-11-24 17:14:51 -0800794static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Thomas Graf4a89c252006-11-24 17:14:51 -0800796 int idx, dn_idx = 0, skip_ndevs, skip_naddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct net_device *dev;
798 struct dn_dev *dn_db;
799 struct dn_ifaddr *ifa;
800
Thomas Graf4a89c252006-11-24 17:14:51 -0800801 skip_ndevs = cb->args[0];
802 skip_naddr = cb->args[1];
803
Pavel Emelianov7562f872007-05-03 15:13:45 -0700804 idx = 0;
Eric W. Biederman881d9662007-09-17 11:56:21 -0700805 for_each_netdev(&init_net, dev) {
Thomas Graf4a89c252006-11-24 17:14:51 -0800806 if (idx < skip_ndevs)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700807 goto cont;
Thomas Graf4a89c252006-11-24 17:14:51 -0800808 else if (idx > skip_ndevs) {
809 /* Only skip over addresses for first dev dumped
810 * in this iteration (idx == skip_ndevs) */
811 skip_naddr = 0;
812 }
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if ((dn_db = dev->dn_ptr) == NULL)
Pavel Emelianov7562f872007-05-03 15:13:45 -0700815 goto cont;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Thomas Graf4a89c252006-11-24 17:14:51 -0800817 for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
818 ifa = ifa->ifa_next, dn_idx++) {
819 if (dn_idx < skip_naddr)
Patrick McHardya2221f32007-09-11 10:45:15 +0200820 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Thomas Graf4a89c252006-11-24 17:14:51 -0800822 if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
823 cb->nlh->nlmsg_seq, RTM_NEWADDR,
824 NLM_F_MULTI) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto done;
826 }
Pavel Emelianov7562f872007-05-03 15:13:45 -0700827cont:
828 idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 cb->args[0] = idx;
832 cb->args[1] = dn_idx;
833
834 return skb->len;
835}
836
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800837static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
839 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
840 struct dn_ifaddr *ifa;
841 int rv = -ENODEV;
842 if (dn_db == NULL)
843 goto out;
844 ifa = dn_db->ifa_list;
845 if (ifa != NULL) {
846 *addr = ifa->ifa_local;
847 rv = 0;
848 }
849out:
850 return rv;
851}
852
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900853/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * Find a default address to bind to.
855 *
856 * This is one of those areas where the initial VMS concepts don't really
857 * map onto the Linux concepts, and since we introduced multiple addresses
858 * per interface we have to cope with slightly odd ways of finding out what
859 * "our address" really is. Mostly it's not a problem; for this we just guess
860 * a sensible default. Eventually the routing code will take care of all the
861 * nasties for us I hope.
862 */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800863int dn_dev_bind_default(__le16 *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 struct net_device *dev;
866 int rv;
867 dev = dn_dev_get_default();
868last_chance:
869 if (dev) {
870 read_lock(&dev_base_lock);
871 rv = dn_dev_get_first(dev, addr);
872 read_unlock(&dev_base_lock);
873 dev_put(dev);
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700874 if (rv == 0 || dev == init_net.loopback_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return rv;
876 }
Eric W. Biederman2774c7a2007-09-26 22:10:56 -0700877 dev = init_net.loopback_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 dev_hold(dev);
879 goto last_chance;
880}
881
882static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
883{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900884 struct endnode_hello_message *msg;
885 struct sk_buff *skb = NULL;
886 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
888
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900889 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 return;
891
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900892 skb->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900894 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900896 msg->msgflg = 0x0D;
897 memcpy(msg->tiver, dn_eco_version, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dn_dn2eth(msg->id, ifa->ifa_local);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900899 msg->iinfo = DN_RT_INFO_ENDN;
900 msg->blksize = dn_htons(mtu2blksize(dev));
901 msg->area = 0x00;
902 memset(msg->seed, 0, 8);
903 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (dn_db->router) {
906 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
907 dn_dn2eth(msg->neighbor, dn->addr);
908 }
909
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900910 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
911 msg->mpd = 0x00;
912 msg->datalen = 0x02;
913 memset(msg->data, 0xAA, 2);
914
915 pktlen = (__le16 *)skb_push(skb,2);
916 *pktlen = dn_htons(skb->len - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -0700918 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
921}
922
923
924#define DRDELAY (5 * HZ)
925
926static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
927{
928 /* First check time since device went up */
929 if ((jiffies - dn_db->uptime) < DRDELAY)
930 return 0;
931
932 /* If there is no router, then yes... */
933 if (!dn_db->router)
934 return 1;
935
936 /* otherwise only if we have a higher priority or.. */
937 if (dn->priority < dn_db->parms.priority)
938 return 1;
939
940 /* if we have equal priority and a higher node number */
941 if (dn->priority != dn_db->parms.priority)
942 return 0;
943
944 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
945 return 1;
946
947 return 0;
948}
949
950static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
951{
952 int n;
953 struct dn_dev *dn_db = dev->dn_ptr;
954 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
955 struct sk_buff *skb;
956 size_t size;
957 unsigned char *ptr;
958 unsigned char *i1, *i2;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800959 __le16 *pktlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 char *src;
961
962 if (mtu2blksize(dev) < (26 + 7))
963 return;
964
965 n = mtu2blksize(dev) - 26;
966 n /= 7;
967
968 if (n > 32)
969 n = 32;
970
971 size = 2 + 26 + 7 * n;
972
973 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
974 return;
975
976 skb->dev = dev;
977 ptr = skb_put(skb, size);
978
979 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
980 *ptr++ = 2; /* ECO */
981 *ptr++ = 0;
982 *ptr++ = 0;
983 dn_dn2eth(ptr, ifa->ifa_local);
984 src = ptr;
985 ptr += ETH_ALEN;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900986 *ptr++ = dn_db->parms.forwarding == 1 ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800988 *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ptr += 2;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900990 *ptr++ = dn_db->parms.priority; /* Priority */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *ptr++ = 0; /* Area: Reserved */
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800992 *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 ptr += 2;
994 *ptr++ = 0; /* MPD: Reserved */
995 i1 = ptr++;
996 memset(ptr, 0, 7); /* Name: Reserved */
997 ptr += 7;
998 i2 = ptr++;
999
1000 n = dn_neigh_elist(dev, ptr, n);
1001
1002 *i2 = 7 * n;
1003 *i1 = 8 + *i2;
1004
1005 skb_trim(skb, (27 + *i2));
1006
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001007 pktlen = (__le16 *)skb_push(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *pktlen = dn_htons(skb->len - 2);
1009
Arnaldo Carvalho de Meloc1d2bbe2007-04-10 20:45:18 -07001010 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (dn_am_i_a_router(dn, dn_db, ifa)) {
1013 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
1014 if (skb2) {
1015 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
1016 }
1017 }
1018
1019 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1020}
1021
1022static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1023{
1024 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1025
1026 if (dn_db->parms.forwarding == 0)
1027 dn_send_endnode_hello(dev, ifa);
1028 else
1029 dn_send_router_hello(dev, ifa);
1030}
1031
1032static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1033{
1034 int tdlen = 16;
1035 int size = dev->hard_header_len + 2 + 4 + tdlen;
1036 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1037 int i;
1038 unsigned char *ptr;
1039 char src[ETH_ALEN];
1040
1041 if (skb == NULL)
1042 return ;
1043
1044 skb->dev = dev;
1045 skb_push(skb, dev->hard_header_len);
1046 ptr = skb_put(skb, 2 + 4 + tdlen);
1047
1048 *ptr++ = DN_RT_PKT_HELO;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001049 *((__le16 *)ptr) = ifa->ifa_local;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 ptr += 2;
1051 *ptr++ = tdlen;
1052
1053 for(i = 0; i < tdlen; i++)
1054 *ptr++ = 0252;
1055
1056 dn_dn2eth(src, ifa->ifa_local);
1057 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1058}
1059
1060static int dn_eth_up(struct net_device *dev)
1061{
1062 struct dn_dev *dn_db = dev->dn_ptr;
1063
1064 if (dn_db->parms.forwarding == 0)
1065 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1066 else
1067 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 dn_db->use_long = 1;
1070
1071 return 0;
1072}
1073
1074static void dn_eth_down(struct net_device *dev)
1075{
1076 struct dn_dev *dn_db = dev->dn_ptr;
1077
1078 if (dn_db->parms.forwarding == 0)
1079 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1080 else
1081 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1082}
1083
1084static void dn_dev_set_timer(struct net_device *dev);
1085
1086static void dn_dev_timer_func(unsigned long arg)
1087{
1088 struct net_device *dev = (struct net_device *)arg;
1089 struct dn_dev *dn_db = dev->dn_ptr;
1090 struct dn_ifaddr *ifa;
1091
1092 if (dn_db->t3 <= dn_db->parms.t2) {
1093 if (dn_db->parms.timer3) {
1094 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1095 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1096 dn_db->parms.timer3(dev, ifa);
1097 }
1098 }
1099 dn_db->t3 = dn_db->parms.t3;
1100 } else {
1101 dn_db->t3 -= dn_db->parms.t2;
1102 }
1103
1104 dn_dev_set_timer(dev);
1105}
1106
1107static void dn_dev_set_timer(struct net_device *dev)
1108{
1109 struct dn_dev *dn_db = dev->dn_ptr;
1110
1111 if (dn_db->parms.t2 > dn_db->parms.t3)
1112 dn_db->parms.t2 = dn_db->parms.t3;
1113
1114 dn_db->timer.data = (unsigned long)dev;
1115 dn_db->timer.function = dn_dev_timer_func;
1116 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1117
1118 add_timer(&dn_db->timer);
1119}
1120
1121struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1122{
1123 int i;
1124 struct dn_dev_parms *p = dn_dev_list;
1125 struct dn_dev *dn_db;
1126
1127 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1128 if (p->type == dev->type)
1129 break;
1130 }
1131
1132 *err = -ENODEV;
1133 if (i == DN_DEV_LIST_SIZE)
1134 return NULL;
1135
1136 *err = -ENOBUFS;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001137 if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return NULL;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1141 smp_wmb();
1142 dev->dn_ptr = dn_db;
1143 dn_db->dev = dev;
1144 init_timer(&dn_db->timer);
1145
1146 dn_db->uptime = jiffies;
Eric W. Biederman95743de2007-01-25 15:51:51 -08001147
1148 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1149 if (!dn_db->neigh_parms) {
1150 dev->dn_ptr = NULL;
1151 kfree(dn_db);
1152 return NULL;
1153 }
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (dn_db->parms.up) {
1156 if (dn_db->parms.up(dev) < 0) {
Eric W. Biederman95743de2007-01-25 15:51:51 -08001157 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 dev->dn_ptr = NULL;
1159 kfree(dn_db);
1160 return NULL;
1161 }
1162 }
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 dn_dev_sysctl_register(dev, &dn_db->parms);
1165
1166 dn_dev_set_timer(dev);
1167
1168 *err = 0;
1169 return dn_db;
1170}
1171
1172
1173/*
1174 * This processes a device up event. We only start up
1175 * the loopback device & ethernet devices with correct
1176 * MAC addreses automatically. Others must be started
1177 * specifically.
1178 *
1179 * FIXME: How should we configure the loopback address ? If we could dispense
1180 * with using decnet_address here and for autobind, it will be one less thing
1181 * for users to worry about setting up.
1182 */
1183
1184void dn_dev_up(struct net_device *dev)
1185{
1186 struct dn_ifaddr *ifa;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001187 __le16 addr = decnet_address;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 int maybe_default = 0;
1189 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1190
1191 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1192 return;
1193
1194 /*
1195 * Need to ensure that loopback device has a dn_db attached to it
1196 * to allow creation of neighbours against it, even though it might
1197 * not have a local address of its own. Might as well do the same for
1198 * all autoconfigured interfaces.
1199 */
1200 if (dn_db == NULL) {
1201 int err;
1202 dn_db = dn_dev_create(dev, &err);
1203 if (dn_db == NULL)
1204 return;
1205 }
1206
1207 if (dev->type == ARPHRD_ETHER) {
1208 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1209 return;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001210 addr = dn_eth2dn(dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 maybe_default = 1;
1212 }
1213
1214 if (addr == 0)
1215 return;
1216
1217 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1218 return;
1219
1220 ifa->ifa_local = ifa->ifa_address = addr;
1221 ifa->ifa_flags = 0;
1222 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1223 strcpy(ifa->ifa_label, dev->name);
1224
1225 dn_dev_set_ifa(dev, ifa);
1226
1227 /*
1228 * Automagically set the default device to the first automatically
1229 * configured ethernet card in the system.
1230 */
1231 if (maybe_default) {
1232 dev_hold(dev);
1233 if (dn_dev_set_default(dev, 0))
1234 dev_put(dev);
1235 }
1236}
1237
1238static void dn_dev_delete(struct net_device *dev)
1239{
1240 struct dn_dev *dn_db = dev->dn_ptr;
1241
1242 if (dn_db == NULL)
1243 return;
1244
1245 del_timer_sync(&dn_db->timer);
1246 dn_dev_sysctl_unregister(&dn_db->parms);
1247 dn_dev_check_default(dev);
1248 neigh_ifdown(&dn_neigh_table, dev);
1249
1250 if (dn_db->parms.down)
1251 dn_db->parms.down(dev);
1252
1253 dev->dn_ptr = NULL;
1254
1255 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1256 neigh_ifdown(&dn_neigh_table, dev);
1257
1258 if (dn_db->router)
1259 neigh_release(dn_db->router);
1260 if (dn_db->peer)
1261 neigh_release(dn_db->peer);
1262
1263 kfree(dn_db);
1264}
1265
1266void dn_dev_down(struct net_device *dev)
1267{
1268 struct dn_dev *dn_db = dev->dn_ptr;
1269 struct dn_ifaddr *ifa;
1270
1271 if (dn_db == NULL)
1272 return;
1273
1274 while((ifa = dn_db->ifa_list) != NULL) {
1275 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1276 dn_dev_free_ifa(ifa);
1277 }
1278
1279 dn_dev_delete(dev);
1280}
1281
1282void dn_dev_init_pkt(struct sk_buff *skb)
1283{
1284 return;
1285}
1286
1287void dn_dev_veri_pkt(struct sk_buff *skb)
1288{
1289 return;
1290}
1291
1292void dn_dev_hello(struct sk_buff *skb)
1293{
1294 return;
1295}
1296
1297void dn_dev_devices_off(void)
1298{
1299 struct net_device *dev;
1300
1301 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07001302 for_each_netdev(&init_net, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 dn_dev_down(dev);
1304 rtnl_unlock();
1305
1306}
1307
1308void dn_dev_devices_on(void)
1309{
1310 struct net_device *dev;
1311
1312 rtnl_lock();
Eric W. Biederman881d9662007-09-17 11:56:21 -07001313 for_each_netdev(&init_net, dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if (dev->flags & IFF_UP)
1315 dn_dev_up(dev);
1316 }
1317 rtnl_unlock();
1318}
1319
1320int register_dnaddr_notifier(struct notifier_block *nb)
1321{
Alan Sterne041c682006-03-27 01:16:30 -08001322 return blocking_notifier_chain_register(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323}
1324
1325int unregister_dnaddr_notifier(struct notifier_block *nb)
1326{
Alan Sterne041c682006-03-27 01:16:30 -08001327 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328}
1329
1330#ifdef CONFIG_PROC_FS
Pavel Emelianov7562f872007-05-03 15:13:45 -07001331static inline int is_dn_dev(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001333 return dev->dn_ptr != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
1336static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1337{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001338 int i;
1339 struct net_device *dev;
1340
1341 read_lock(&dev_base_lock);
1342
1343 if (*pos == 0)
1344 return SEQ_START_TOKEN;
1345
1346 i = 1;
Eric W. Biederman881d9662007-09-17 11:56:21 -07001347 for_each_netdev(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001348 if (!is_dn_dev(dev))
1349 continue;
1350
1351 if (i++ == *pos)
1352 return dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 }
Pavel Emelianov7562f872007-05-03 15:13:45 -07001354
1355 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1359{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001360 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 ++*pos;
Pavel Emelianov7562f872007-05-03 15:13:45 -07001363
1364 dev = (struct net_device *)v;
1365 if (v == SEQ_START_TOKEN)
Eric W. Biederman881d9662007-09-17 11:56:21 -07001366 dev = net_device_entry(&init_net.dev_base_head);
Pavel Emelianov7562f872007-05-03 15:13:45 -07001367
Eric W. Biederman881d9662007-09-17 11:56:21 -07001368 for_each_netdev_continue(&init_net, dev) {
Pavel Emelianov7562f872007-05-03 15:13:45 -07001369 if (!is_dn_dev(dev))
1370 continue;
1371
1372 return dev;
1373 }
1374
1375 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
1378static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1379{
Pavel Emelianov7562f872007-05-03 15:13:45 -07001380 read_unlock(&dev_base_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
1382
1383static char *dn_type2asc(char type)
1384{
1385 switch(type) {
1386 case DN_DEV_BCAST:
1387 return "B";
1388 case DN_DEV_UCAST:
1389 return "U";
1390 case DN_DEV_MPOINT:
1391 return "M";
1392 }
1393
1394 return "?";
1395}
1396
1397static int dn_dev_seq_show(struct seq_file *seq, void *v)
1398{
1399 if (v == SEQ_START_TOKEN)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001400 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 else {
1402 struct net_device *dev = v;
1403 char peer_buf[DN_ASCBUF_LEN];
1404 char router_buf[DN_ASCBUF_LEN];
1405 struct dn_dev *dn_db = dev->dn_ptr;
1406
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001407 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 " %04hu %03d %02x %-10s %-7s %-7s\n",
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001409 dev->name ? dev->name : "???",
1410 dn_type2asc(dn_db->parms.mode),
1411 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 dn_db->t3, dn_db->parms.t3,
1413 mtu2blksize(dev),
1414 dn_db->parms.priority,
1415 dn_db->parms.state, dn_db->parms.name,
Steven Whitehousec4ea94a2006-03-20 22:42:39 -08001416 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1417 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 return 0;
1420}
1421
Philippe De Muyter56b3d972007-07-10 23:07:31 -07001422static const struct seq_operations dn_dev_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 .start = dn_dev_seq_start,
1424 .next = dn_dev_seq_next,
1425 .stop = dn_dev_seq_stop,
1426 .show = dn_dev_seq_show,
1427};
1428
1429static int dn_dev_seq_open(struct inode *inode, struct file *file)
1430{
1431 return seq_open(file, &dn_dev_seq_ops);
1432}
1433
Arjan van de Ven9a321442007-02-12 00:55:35 -08001434static const struct file_operations dn_dev_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 .owner = THIS_MODULE,
1436 .open = dn_dev_seq_open,
1437 .read = seq_read,
1438 .llseek = seq_lseek,
1439 .release = seq_release,
1440};
1441
1442#endif /* CONFIG_PROC_FS */
1443
Alexey Dobriyan4e058062007-11-05 21:30:11 -08001444static int addr[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445module_param_array(addr, int, NULL, 0444);
1446MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1447
1448void __init dn_dev_init(void)
1449{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001450 if (addr[0] > 63 || addr[0] < 0) {
1451 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1452 return;
1453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001455 if (addr[1] > 1023 || addr[1] < 0) {
1456 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1457 return;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +09001460 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 dn_dev_devices_on();
1463
Thomas Graffa34ddd2007-03-22 11:57:46 -07001464 rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL);
1465 rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL);
1466 rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001468 proc_net_fops_create(&init_net, "decnet_dev", S_IRUGO, &dn_dev_seq_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470#ifdef CONFIG_SYSCTL
1471 {
1472 int i;
1473 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1474 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1475 }
1476#endif /* CONFIG_SYSCTL */
1477}
1478
1479void __exit dn_dev_cleanup(void)
1480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481#ifdef CONFIG_SYSCTL
1482 {
1483 int i;
1484 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1485 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1486 }
1487#endif /* CONFIG_SYSCTL */
1488
Eric W. Biederman457c4cb2007-09-12 12:01:34 +02001489 proc_net_remove(&init_net, "decnet_dev");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 dn_dev_devices_off();
1492}