Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / net / decnet / dn_dev.c
blob3b726f31c64c0b88efcfacd4d64df7177260ad5c
1 /*
2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * DECnet Device Layer
8 * Authors: Steve Whitehouse <SteveW@ACM.org>
9 * Eduardo Marcelo Serrat <emserrat@geocities.com>
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.
27 #include <linux/capability.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/net.h>
32 #include <linux/netdevice.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/timer.h>
36 #include <linux/string.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_arp.h>
39 #include <linux/if_ether.h>
40 #include <linux/skbuff.h>
41 #include <linux/sysctl.h>
42 #include <linux/notifier.h>
43 #include <linux/slab.h>
44 #include <asm/uaccess.h>
45 #include <net/net_namespace.h>
46 #include <net/neighbour.h>
47 #include <net/dst.h>
48 #include <net/flow.h>
49 #include <net/fib_rules.h>
50 #include <net/netlink.h>
51 #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>
57 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
59 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
60 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
61 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
62 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
64 extern struct neigh_table dn_neigh_table;
67 * decnet_address is kept in network order.
69 __le16 decnet_address = 0;
71 static DEFINE_SPINLOCK(dndev_lock);
72 static struct net_device *decnet_default_device;
73 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
75 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
76 static void dn_dev_delete(struct net_device *dev);
77 static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
79 static int dn_eth_up(struct net_device *);
80 static void dn_eth_down(struct net_device *);
81 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
82 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
84 static struct dn_dev_parms dn_dev_list[] = {
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 .up = dn_eth_up,
93 .down = dn_eth_down,
94 .timer3 = dn_send_brd_hello,
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 .timer3 = dn_send_brd_hello,
105 #if 0
107 .type = ARPHRD_X25, /* Bog standard X.25 */
108 .mode = DN_DEV_UCAST,
109 .state = DN_DEV_S_DS,
110 .t2 = 1,
111 .t3 = 120,
112 .name = "x25",
113 .timer3 = dn_send_ptp_hello,
115 #endif
116 #if 0
118 .type = ARPHRD_PPP, /* DECnet over PPP */
119 .mode = DN_DEV_BCAST,
120 .state = DN_DEV_S_RU,
121 .t2 = 1,
122 .t3 = 10,
123 .name = "ppp",
124 .timer3 = dn_send_brd_hello,
126 #endif
128 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
129 .mode = DN_DEV_UCAST,
130 .state = DN_DEV_S_DS,
131 .t2 = 1,
132 .t3 = 120,
133 .name = "ddcmp",
134 .timer3 = dn_send_ptp_hello,
137 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
138 .mode = DN_DEV_BCAST,
139 .state = DN_DEV_S_RU,
140 .t2 = 1,
141 .t3 = 10,
142 .name = "loopback",
143 .timer3 = dn_send_brd_hello,
147 #define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)
149 #define DN_DEV_PARMS_OFFSET(x) offsetof(struct dn_dev_parms, x)
151 #ifdef CONFIG_SYSCTL
153 static int min_t2[] = { 1 };
154 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
155 static int min_t3[] = { 1 };
156 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
158 static int min_priority[1];
159 static int max_priority[] = { 127 }; /* From DECnet spec */
161 static int dn_forwarding_proc(struct ctl_table *, int,
162 void __user *, size_t *, loff_t *);
163 static struct dn_dev_sysctl_table {
164 struct ctl_table_header *sysctl_header;
165 struct ctl_table dn_dev_vars[5];
166 } dn_dev_sysctl = {
167 NULL,
170 .procname = "forwarding",
171 .data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
172 .maxlen = sizeof(int),
173 .mode = 0644,
174 .proc_handler = dn_forwarding_proc,
177 .procname = "priority",
178 .data = (void *)DN_DEV_PARMS_OFFSET(priority),
179 .maxlen = sizeof(int),
180 .mode = 0644,
181 .proc_handler = proc_dointvec_minmax,
182 .extra1 = &min_priority,
183 .extra2 = &max_priority
186 .procname = "t2",
187 .data = (void *)DN_DEV_PARMS_OFFSET(t2),
188 .maxlen = sizeof(int),
189 .mode = 0644,
190 .proc_handler = proc_dointvec_minmax,
191 .extra1 = &min_t2,
192 .extra2 = &max_t2
195 .procname = "t3",
196 .data = (void *)DN_DEV_PARMS_OFFSET(t3),
197 .maxlen = sizeof(int),
198 .mode = 0644,
199 .proc_handler = proc_dointvec_minmax,
200 .extra1 = &min_t3,
201 .extra2 = &max_t3
207 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
209 struct dn_dev_sysctl_table *t;
210 int i;
212 char path[sizeof("net/decnet/conf/") + IFNAMSIZ];
214 t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
215 if (t == NULL)
216 return;
218 for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
219 long offset = (long)t->dn_dev_vars[i].data;
220 t->dn_dev_vars[i].data = ((char *)parms) + offset;
223 snprintf(path, sizeof(path), "net/decnet/conf/%s",
224 dev? dev->name : parms->name);
226 t->dn_dev_vars[0].extra1 = (void *)dev;
228 t->sysctl_header = register_net_sysctl(&init_net, path, t->dn_dev_vars);
229 if (t->sysctl_header == NULL)
230 kfree(t);
231 else
232 parms->sysctl = t;
235 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
237 if (parms->sysctl) {
238 struct dn_dev_sysctl_table *t = parms->sysctl;
239 parms->sysctl = NULL;
240 unregister_net_sysctl_table(t->sysctl_header);
241 kfree(t);
245 static int dn_forwarding_proc(struct ctl_table *table, int write,
246 void __user *buffer,
247 size_t *lenp, loff_t *ppos)
249 #ifdef CONFIG_DECNET_ROUTER
250 struct net_device *dev = table->extra1;
251 struct dn_dev *dn_db;
252 int err;
253 int tmp, old;
255 if (table->extra1 == NULL)
256 return -EINVAL;
258 dn_db = rcu_dereference_raw(dev->dn_ptr);
259 old = dn_db->parms.forwarding;
261 err = proc_dointvec(table, write, buffer, lenp, ppos);
263 if ((err >= 0) && write) {
264 if (dn_db->parms.forwarding < 0)
265 dn_db->parms.forwarding = 0;
266 if (dn_db->parms.forwarding > 2)
267 dn_db->parms.forwarding = 2;
269 * What an ugly hack this is... its works, just. It
270 * would be nice if sysctl/proc were just that little
271 * bit more flexible so I don't have to write a special
272 * routine, or suffer hacks like this - SJW
274 tmp = dn_db->parms.forwarding;
275 dn_db->parms.forwarding = old;
276 if (dn_db->parms.down)
277 dn_db->parms.down(dev);
278 dn_db->parms.forwarding = tmp;
279 if (dn_db->parms.up)
280 dn_db->parms.up(dev);
283 return err;
284 #else
285 return -EINVAL;
286 #endif
289 #else /* CONFIG_SYSCTL */
290 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
293 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
297 #endif /* CONFIG_SYSCTL */
299 static inline __u16 mtu2blksize(struct net_device *dev)
301 u32 blksize = dev->mtu;
302 if (blksize > 0xffff)
303 blksize = 0xffff;
305 if (dev->type == ARPHRD_ETHER ||
306 dev->type == ARPHRD_PPP ||
307 dev->type == ARPHRD_IPGRE ||
308 dev->type == ARPHRD_LOOPBACK)
309 blksize -= 2;
311 return (__u16)blksize;
314 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
316 struct dn_ifaddr *ifa;
318 ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
320 return ifa;
323 static void dn_dev_free_ifa(struct dn_ifaddr *ifa)
325 kfree_rcu(ifa, rcu);
328 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr __rcu **ifap, int destroy)
330 struct dn_ifaddr *ifa1 = rtnl_dereference(*ifap);
331 unsigned char mac_addr[6];
332 struct net_device *dev = dn_db->dev;
334 ASSERT_RTNL();
336 *ifap = ifa1->ifa_next;
338 if (dn_db->dev->type == ARPHRD_ETHER) {
339 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
340 dn_dn2eth(mac_addr, ifa1->ifa_local);
341 dev_mc_del(dev, mac_addr);
345 dn_ifaddr_notify(RTM_DELADDR, ifa1);
346 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
347 if (destroy) {
348 dn_dev_free_ifa(ifa1);
350 if (dn_db->ifa_list == NULL)
351 dn_dev_delete(dn_db->dev);
355 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
357 struct net_device *dev = dn_db->dev;
358 struct dn_ifaddr *ifa1;
359 unsigned char mac_addr[6];
361 ASSERT_RTNL();
363 /* Check for duplicates */
364 for (ifa1 = rtnl_dereference(dn_db->ifa_list);
365 ifa1 != NULL;
366 ifa1 = rtnl_dereference(ifa1->ifa_next)) {
367 if (ifa1->ifa_local == ifa->ifa_local)
368 return -EEXIST;
371 if (dev->type == ARPHRD_ETHER) {
372 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
373 dn_dn2eth(mac_addr, ifa->ifa_local);
374 dev_mc_add(dev, mac_addr);
378 ifa->ifa_next = dn_db->ifa_list;
379 rcu_assign_pointer(dn_db->ifa_list, ifa);
381 dn_ifaddr_notify(RTM_NEWADDR, ifa);
382 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
384 return 0;
387 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
389 struct dn_dev *dn_db = rtnl_dereference(dev->dn_ptr);
390 int rv;
392 if (dn_db == NULL) {
393 int err;
394 dn_db = dn_dev_create(dev, &err);
395 if (dn_db == NULL)
396 return err;
399 ifa->ifa_dev = dn_db;
401 if (dev->flags & IFF_LOOPBACK)
402 ifa->ifa_scope = RT_SCOPE_HOST;
404 rv = dn_dev_insert_ifa(dn_db, ifa);
405 if (rv)
406 dn_dev_free_ifa(ifa);
407 return rv;
411 int dn_dev_ioctl(unsigned int cmd, void __user *arg)
413 char buffer[DN_IFREQ_SIZE];
414 struct ifreq *ifr = (struct ifreq *)buffer;
415 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
416 struct dn_dev *dn_db;
417 struct net_device *dev;
418 struct dn_ifaddr *ifa = NULL;
419 struct dn_ifaddr __rcu **ifap = NULL;
420 int ret = 0;
422 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
423 return -EFAULT;
424 ifr->ifr_name[IFNAMSIZ-1] = 0;
426 dev_load(&init_net, ifr->ifr_name);
428 switch (cmd) {
429 case SIOCGIFADDR:
430 break;
431 case SIOCSIFADDR:
432 if (!capable(CAP_NET_ADMIN))
433 return -EACCES;
434 if (sdn->sdn_family != AF_DECnet)
435 return -EINVAL;
436 break;
437 default:
438 return -EINVAL;
441 rtnl_lock();
443 if ((dev = __dev_get_by_name(&init_net, ifr->ifr_name)) == NULL) {
444 ret = -ENODEV;
445 goto done;
448 if ((dn_db = rtnl_dereference(dev->dn_ptr)) != NULL) {
449 for (ifap = &dn_db->ifa_list;
450 (ifa = rtnl_dereference(*ifap)) != NULL;
451 ifap = &ifa->ifa_next)
452 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
453 break;
456 if (ifa == NULL && cmd != SIOCSIFADDR) {
457 ret = -EADDRNOTAVAIL;
458 goto done;
461 switch (cmd) {
462 case SIOCGIFADDR:
463 *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
464 goto rarok;
466 case SIOCSIFADDR:
467 if (!ifa) {
468 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
469 ret = -ENOBUFS;
470 break;
472 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
473 } else {
474 if (ifa->ifa_local == dn_saddr2dn(sdn))
475 break;
476 dn_dev_del_ifa(dn_db, ifap, 0);
479 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
481 ret = dn_dev_set_ifa(dev, ifa);
483 done:
484 rtnl_unlock();
486 return ret;
487 rarok:
488 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
489 ret = -EFAULT;
490 goto done;
493 struct net_device *dn_dev_get_default(void)
495 struct net_device *dev;
497 spin_lock(&dndev_lock);
498 dev = decnet_default_device;
499 if (dev) {
500 if (dev->dn_ptr)
501 dev_hold(dev);
502 else
503 dev = NULL;
505 spin_unlock(&dndev_lock);
507 return dev;
510 int dn_dev_set_default(struct net_device *dev, int force)
512 struct net_device *old = NULL;
513 int rv = -EBUSY;
514 if (!dev->dn_ptr)
515 return -ENODEV;
517 spin_lock(&dndev_lock);
518 if (force || decnet_default_device == NULL) {
519 old = decnet_default_device;
520 decnet_default_device = dev;
521 rv = 0;
523 spin_unlock(&dndev_lock);
525 if (old)
526 dev_put(old);
527 return rv;
530 static void dn_dev_check_default(struct net_device *dev)
532 spin_lock(&dndev_lock);
533 if (dev == decnet_default_device) {
534 decnet_default_device = NULL;
535 } else {
536 dev = NULL;
538 spin_unlock(&dndev_lock);
540 if (dev)
541 dev_put(dev);
545 * Called with RTNL
547 static struct dn_dev *dn_dev_by_index(int ifindex)
549 struct net_device *dev;
550 struct dn_dev *dn_dev = NULL;
552 dev = __dev_get_by_index(&init_net, ifindex);
553 if (dev)
554 dn_dev = rtnl_dereference(dev->dn_ptr);
556 return dn_dev;
559 static const struct nla_policy dn_ifa_policy[IFA_MAX+1] = {
560 [IFA_ADDRESS] = { .type = NLA_U16 },
561 [IFA_LOCAL] = { .type = NLA_U16 },
562 [IFA_LABEL] = { .type = NLA_STRING,
563 .len = IFNAMSIZ - 1 },
564 [IFA_FLAGS] = { .type = NLA_U32 },
567 static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh)
569 struct net *net = sock_net(skb->sk);
570 struct nlattr *tb[IFA_MAX+1];
571 struct dn_dev *dn_db;
572 struct ifaddrmsg *ifm;
573 struct dn_ifaddr *ifa;
574 struct dn_ifaddr __rcu **ifap;
575 int err = -EINVAL;
577 if (!netlink_capable(skb, CAP_NET_ADMIN))
578 return -EPERM;
580 if (!net_eq(net, &init_net))
581 goto errout;
583 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
584 if (err < 0)
585 goto errout;
587 err = -ENODEV;
588 ifm = nlmsg_data(nlh);
589 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
590 goto errout;
592 err = -EADDRNOTAVAIL;
593 for (ifap = &dn_db->ifa_list;
594 (ifa = rtnl_dereference(*ifap)) != NULL;
595 ifap = &ifa->ifa_next) {
596 if (tb[IFA_LOCAL] &&
597 nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
598 continue;
600 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
601 continue;
603 dn_dev_del_ifa(dn_db, ifap, 1);
604 return 0;
607 errout:
608 return err;
611 static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh)
613 struct net *net = sock_net(skb->sk);
614 struct nlattr *tb[IFA_MAX+1];
615 struct net_device *dev;
616 struct dn_dev *dn_db;
617 struct ifaddrmsg *ifm;
618 struct dn_ifaddr *ifa;
619 int err;
621 if (!netlink_capable(skb, CAP_NET_ADMIN))
622 return -EPERM;
624 if (!net_eq(net, &init_net))
625 return -EINVAL;
627 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
628 if (err < 0)
629 return err;
631 if (tb[IFA_LOCAL] == NULL)
632 return -EINVAL;
634 ifm = nlmsg_data(nlh);
635 if ((dev = __dev_get_by_index(&init_net, ifm->ifa_index)) == NULL)
636 return -ENODEV;
638 if ((dn_db = rtnl_dereference(dev->dn_ptr)) == NULL) {
639 dn_db = dn_dev_create(dev, &err);
640 if (!dn_db)
641 return err;
644 if ((ifa = dn_dev_alloc_ifa()) == NULL)
645 return -ENOBUFS;
647 if (tb[IFA_ADDRESS] == NULL)
648 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
650 ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
651 ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
652 ifa->ifa_flags = tb[IFA_FLAGS] ? nla_get_u32(tb[IFA_FLAGS]) :
653 ifm->ifa_flags;
654 ifa->ifa_scope = ifm->ifa_scope;
655 ifa->ifa_dev = dn_db;
657 if (tb[IFA_LABEL])
658 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
659 else
660 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
662 err = dn_dev_insert_ifa(dn_db, ifa);
663 if (err)
664 dn_dev_free_ifa(ifa);
666 return err;
669 static inline size_t dn_ifaddr_nlmsg_size(void)
671 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
672 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
673 + nla_total_size(2) /* IFA_ADDRESS */
674 + nla_total_size(2) /* IFA_LOCAL */
675 + nla_total_size(4); /* IFA_FLAGS */
678 static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
679 u32 portid, u32 seq, int event, unsigned int flags)
681 struct ifaddrmsg *ifm;
682 struct nlmsghdr *nlh;
683 u32 ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
685 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*ifm), flags);
686 if (nlh == NULL)
687 return -EMSGSIZE;
689 ifm = nlmsg_data(nlh);
690 ifm->ifa_family = AF_DECnet;
691 ifm->ifa_prefixlen = 16;
692 ifm->ifa_flags = ifa_flags;
693 ifm->ifa_scope = ifa->ifa_scope;
694 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
696 if ((ifa->ifa_address &&
697 nla_put_le16(skb, IFA_ADDRESS, ifa->ifa_address)) ||
698 (ifa->ifa_local &&
699 nla_put_le16(skb, IFA_LOCAL, ifa->ifa_local)) ||
700 (ifa->ifa_label[0] &&
701 nla_put_string(skb, IFA_LABEL, ifa->ifa_label)) ||
702 nla_put_u32(skb, IFA_FLAGS, ifa_flags))
703 goto nla_put_failure;
704 return nlmsg_end(skb, nlh);
706 nla_put_failure:
707 nlmsg_cancel(skb, nlh);
708 return -EMSGSIZE;
711 static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
713 struct sk_buff *skb;
714 int err = -ENOBUFS;
716 skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
717 if (skb == NULL)
718 goto errout;
720 err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
721 if (err < 0) {
722 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
723 WARN_ON(err == -EMSGSIZE);
724 kfree_skb(skb);
725 goto errout;
727 rtnl_notify(skb, &init_net, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
728 return;
729 errout:
730 if (err < 0)
731 rtnl_set_sk_err(&init_net, RTNLGRP_DECnet_IFADDR, err);
734 static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
736 struct net *net = sock_net(skb->sk);
737 int idx, dn_idx = 0, skip_ndevs, skip_naddr;
738 struct net_device *dev;
739 struct dn_dev *dn_db;
740 struct dn_ifaddr *ifa;
742 if (!net_eq(net, &init_net))
743 return 0;
745 skip_ndevs = cb->args[0];
746 skip_naddr = cb->args[1];
748 idx = 0;
749 rcu_read_lock();
750 for_each_netdev_rcu(&init_net, dev) {
751 if (idx < skip_ndevs)
752 goto cont;
753 else if (idx > skip_ndevs) {
754 /* Only skip over addresses for first dev dumped
755 * in this iteration (idx == skip_ndevs) */
756 skip_naddr = 0;
759 if ((dn_db = rcu_dereference(dev->dn_ptr)) == NULL)
760 goto cont;
762 for (ifa = rcu_dereference(dn_db->ifa_list), dn_idx = 0; ifa;
763 ifa = rcu_dereference(ifa->ifa_next), dn_idx++) {
764 if (dn_idx < skip_naddr)
765 continue;
767 if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).portid,
768 cb->nlh->nlmsg_seq, RTM_NEWADDR,
769 NLM_F_MULTI) < 0)
770 goto done;
772 cont:
773 idx++;
775 done:
776 rcu_read_unlock();
777 cb->args[0] = idx;
778 cb->args[1] = dn_idx;
780 return skb->len;
783 static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
785 struct dn_dev *dn_db;
786 struct dn_ifaddr *ifa;
787 int rv = -ENODEV;
789 rcu_read_lock();
790 dn_db = rcu_dereference(dev->dn_ptr);
791 if (dn_db == NULL)
792 goto out;
794 ifa = rcu_dereference(dn_db->ifa_list);
795 if (ifa != NULL) {
796 *addr = ifa->ifa_local;
797 rv = 0;
799 out:
800 rcu_read_unlock();
801 return rv;
805 * Find a default address to bind to.
807 * This is one of those areas where the initial VMS concepts don't really
808 * map onto the Linux concepts, and since we introduced multiple addresses
809 * per interface we have to cope with slightly odd ways of finding out what
810 * "our address" really is. Mostly it's not a problem; for this we just guess
811 * a sensible default. Eventually the routing code will take care of all the
812 * nasties for us I hope.
814 int dn_dev_bind_default(__le16 *addr)
816 struct net_device *dev;
817 int rv;
818 dev = dn_dev_get_default();
819 last_chance:
820 if (dev) {
821 rv = dn_dev_get_first(dev, addr);
822 dev_put(dev);
823 if (rv == 0 || dev == init_net.loopback_dev)
824 return rv;
826 dev = init_net.loopback_dev;
827 dev_hold(dev);
828 goto last_chance;
831 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
833 struct endnode_hello_message *msg;
834 struct sk_buff *skb = NULL;
835 __le16 *pktlen;
836 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
838 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
839 return;
841 skb->dev = dev;
843 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
845 msg->msgflg = 0x0D;
846 memcpy(msg->tiver, dn_eco_version, 3);
847 dn_dn2eth(msg->id, ifa->ifa_local);
848 msg->iinfo = DN_RT_INFO_ENDN;
849 msg->blksize = cpu_to_le16(mtu2blksize(dev));
850 msg->area = 0x00;
851 memset(msg->seed, 0, 8);
852 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
854 if (dn_db->router) {
855 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
856 dn_dn2eth(msg->neighbor, dn->addr);
859 msg->timer = cpu_to_le16((unsigned short)dn_db->parms.t3);
860 msg->mpd = 0x00;
861 msg->datalen = 0x02;
862 memset(msg->data, 0xAA, 2);
864 pktlen = (__le16 *)skb_push(skb,2);
865 *pktlen = cpu_to_le16(skb->len - 2);
867 skb_reset_network_header(skb);
869 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
873 #define DRDELAY (5 * HZ)
875 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
877 /* First check time since device went up */
878 if ((jiffies - dn_db->uptime) < DRDELAY)
879 return 0;
881 /* If there is no router, then yes... */
882 if (!dn_db->router)
883 return 1;
885 /* otherwise only if we have a higher priority or.. */
886 if (dn->priority < dn_db->parms.priority)
887 return 1;
889 /* if we have equal priority and a higher node number */
890 if (dn->priority != dn_db->parms.priority)
891 return 0;
893 if (le16_to_cpu(dn->addr) < le16_to_cpu(ifa->ifa_local))
894 return 1;
896 return 0;
899 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
901 int n;
902 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
903 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
904 struct sk_buff *skb;
905 size_t size;
906 unsigned char *ptr;
907 unsigned char *i1, *i2;
908 __le16 *pktlen;
909 char *src;
911 if (mtu2blksize(dev) < (26 + 7))
912 return;
914 n = mtu2blksize(dev) - 26;
915 n /= 7;
917 if (n > 32)
918 n = 32;
920 size = 2 + 26 + 7 * n;
922 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
923 return;
925 skb->dev = dev;
926 ptr = skb_put(skb, size);
928 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
929 *ptr++ = 2; /* ECO */
930 *ptr++ = 0;
931 *ptr++ = 0;
932 dn_dn2eth(ptr, ifa->ifa_local);
933 src = ptr;
934 ptr += ETH_ALEN;
935 *ptr++ = dn_db->parms.forwarding == 1 ?
936 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
937 *((__le16 *)ptr) = cpu_to_le16(mtu2blksize(dev));
938 ptr += 2;
939 *ptr++ = dn_db->parms.priority; /* Priority */
940 *ptr++ = 0; /* Area: Reserved */
941 *((__le16 *)ptr) = cpu_to_le16((unsigned short)dn_db->parms.t3);
942 ptr += 2;
943 *ptr++ = 0; /* MPD: Reserved */
944 i1 = ptr++;
945 memset(ptr, 0, 7); /* Name: Reserved */
946 ptr += 7;
947 i2 = ptr++;
949 n = dn_neigh_elist(dev, ptr, n);
951 *i2 = 7 * n;
952 *i1 = 8 + *i2;
954 skb_trim(skb, (27 + *i2));
956 pktlen = (__le16 *)skb_push(skb, 2);
957 *pktlen = cpu_to_le16(skb->len - 2);
959 skb_reset_network_header(skb);
961 if (dn_am_i_a_router(dn, dn_db, ifa)) {
962 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
963 if (skb2) {
964 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
968 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
971 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
973 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
975 if (dn_db->parms.forwarding == 0)
976 dn_send_endnode_hello(dev, ifa);
977 else
978 dn_send_router_hello(dev, ifa);
981 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
983 int tdlen = 16;
984 int size = dev->hard_header_len + 2 + 4 + tdlen;
985 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
986 int i;
987 unsigned char *ptr;
988 char src[ETH_ALEN];
990 if (skb == NULL)
991 return ;
993 skb->dev = dev;
994 skb_push(skb, dev->hard_header_len);
995 ptr = skb_put(skb, 2 + 4 + tdlen);
997 *ptr++ = DN_RT_PKT_HELO;
998 *((__le16 *)ptr) = ifa->ifa_local;
999 ptr += 2;
1000 *ptr++ = tdlen;
1002 for(i = 0; i < tdlen; i++)
1003 *ptr++ = 0252;
1005 dn_dn2eth(src, ifa->ifa_local);
1006 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1009 static int dn_eth_up(struct net_device *dev)
1011 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
1013 if (dn_db->parms.forwarding == 0)
1014 dev_mc_add(dev, dn_rt_all_end_mcast);
1015 else
1016 dev_mc_add(dev, dn_rt_all_rt_mcast);
1018 dn_db->use_long = 1;
1020 return 0;
1023 static void dn_eth_down(struct net_device *dev)
1025 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
1027 if (dn_db->parms.forwarding == 0)
1028 dev_mc_del(dev, dn_rt_all_end_mcast);
1029 else
1030 dev_mc_del(dev, dn_rt_all_rt_mcast);
1033 static void dn_dev_set_timer(struct net_device *dev);
1035 static void dn_dev_timer_func(unsigned long arg)
1037 struct net_device *dev = (struct net_device *)arg;
1038 struct dn_dev *dn_db;
1039 struct dn_ifaddr *ifa;
1041 rcu_read_lock();
1042 dn_db = rcu_dereference(dev->dn_ptr);
1043 if (dn_db->t3 <= dn_db->parms.t2) {
1044 if (dn_db->parms.timer3) {
1045 for (ifa = rcu_dereference(dn_db->ifa_list);
1046 ifa;
1047 ifa = rcu_dereference(ifa->ifa_next)) {
1048 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1049 dn_db->parms.timer3(dev, ifa);
1052 dn_db->t3 = dn_db->parms.t3;
1053 } else {
1054 dn_db->t3 -= dn_db->parms.t2;
1056 rcu_read_unlock();
1057 dn_dev_set_timer(dev);
1060 static void dn_dev_set_timer(struct net_device *dev)
1062 struct dn_dev *dn_db = rcu_dereference_raw(dev->dn_ptr);
1064 if (dn_db->parms.t2 > dn_db->parms.t3)
1065 dn_db->parms.t2 = dn_db->parms.t3;
1067 dn_db->timer.data = (unsigned long)dev;
1068 dn_db->timer.function = dn_dev_timer_func;
1069 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1071 add_timer(&dn_db->timer);
1074 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1076 int i;
1077 struct dn_dev_parms *p = dn_dev_list;
1078 struct dn_dev *dn_db;
1080 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1081 if (p->type == dev->type)
1082 break;
1085 *err = -ENODEV;
1086 if (i == DN_DEV_LIST_SIZE)
1087 return NULL;
1089 *err = -ENOBUFS;
1090 if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1091 return NULL;
1093 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1095 rcu_assign_pointer(dev->dn_ptr, dn_db);
1096 dn_db->dev = dev;
1097 init_timer(&dn_db->timer);
1099 dn_db->uptime = jiffies;
1101 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1102 if (!dn_db->neigh_parms) {
1103 RCU_INIT_POINTER(dev->dn_ptr, NULL);
1104 kfree(dn_db);
1105 return NULL;
1108 if (dn_db->parms.up) {
1109 if (dn_db->parms.up(dev) < 0) {
1110 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1111 dev->dn_ptr = NULL;
1112 kfree(dn_db);
1113 return NULL;
1117 dn_dev_sysctl_register(dev, &dn_db->parms);
1119 dn_dev_set_timer(dev);
1121 *err = 0;
1122 return dn_db;
1127 * This processes a device up event. We only start up
1128 * the loopback device & ethernet devices with correct
1129 * MAC addresses automatically. Others must be started
1130 * specifically.
1132 * FIXME: How should we configure the loopback address ? If we could dispense
1133 * with using decnet_address here and for autobind, it will be one less thing
1134 * for users to worry about setting up.
1137 void dn_dev_up(struct net_device *dev)
1139 struct dn_ifaddr *ifa;
1140 __le16 addr = decnet_address;
1141 int maybe_default = 0;
1142 struct dn_dev *dn_db = rtnl_dereference(dev->dn_ptr);
1144 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1145 return;
1148 * Need to ensure that loopback device has a dn_db attached to it
1149 * to allow creation of neighbours against it, even though it might
1150 * not have a local address of its own. Might as well do the same for
1151 * all autoconfigured interfaces.
1153 if (dn_db == NULL) {
1154 int err;
1155 dn_db = dn_dev_create(dev, &err);
1156 if (dn_db == NULL)
1157 return;
1160 if (dev->type == ARPHRD_ETHER) {
1161 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1162 return;
1163 addr = dn_eth2dn(dev->dev_addr);
1164 maybe_default = 1;
1167 if (addr == 0)
1168 return;
1170 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1171 return;
1173 ifa->ifa_local = ifa->ifa_address = addr;
1174 ifa->ifa_flags = 0;
1175 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1176 strcpy(ifa->ifa_label, dev->name);
1178 dn_dev_set_ifa(dev, ifa);
1181 * Automagically set the default device to the first automatically
1182 * configured ethernet card in the system.
1184 if (maybe_default) {
1185 dev_hold(dev);
1186 if (dn_dev_set_default(dev, 0))
1187 dev_put(dev);
1191 static void dn_dev_delete(struct net_device *dev)
1193 struct dn_dev *dn_db = rtnl_dereference(dev->dn_ptr);
1195 if (dn_db == NULL)
1196 return;
1198 del_timer_sync(&dn_db->timer);
1199 dn_dev_sysctl_unregister(&dn_db->parms);
1200 dn_dev_check_default(dev);
1201 neigh_ifdown(&dn_neigh_table, dev);
1203 if (dn_db->parms.down)
1204 dn_db->parms.down(dev);
1206 dev->dn_ptr = NULL;
1208 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1209 neigh_ifdown(&dn_neigh_table, dev);
1211 if (dn_db->router)
1212 neigh_release(dn_db->router);
1213 if (dn_db->peer)
1214 neigh_release(dn_db->peer);
1216 kfree(dn_db);
1219 void dn_dev_down(struct net_device *dev)
1221 struct dn_dev *dn_db = rtnl_dereference(dev->dn_ptr);
1222 struct dn_ifaddr *ifa;
1224 if (dn_db == NULL)
1225 return;
1227 while ((ifa = rtnl_dereference(dn_db->ifa_list)) != NULL) {
1228 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1229 dn_dev_free_ifa(ifa);
1232 dn_dev_delete(dev);
1235 void dn_dev_init_pkt(struct sk_buff *skb)
1239 void dn_dev_veri_pkt(struct sk_buff *skb)
1243 void dn_dev_hello(struct sk_buff *skb)
1247 void dn_dev_devices_off(void)
1249 struct net_device *dev;
1251 rtnl_lock();
1252 for_each_netdev(&init_net, dev)
1253 dn_dev_down(dev);
1254 rtnl_unlock();
1258 void dn_dev_devices_on(void)
1260 struct net_device *dev;
1262 rtnl_lock();
1263 for_each_netdev(&init_net, dev) {
1264 if (dev->flags & IFF_UP)
1265 dn_dev_up(dev);
1267 rtnl_unlock();
1270 int register_dnaddr_notifier(struct notifier_block *nb)
1272 return blocking_notifier_chain_register(&dnaddr_chain, nb);
1275 int unregister_dnaddr_notifier(struct notifier_block *nb)
1277 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1280 #ifdef CONFIG_PROC_FS
1281 static inline int is_dn_dev(struct net_device *dev)
1283 return dev->dn_ptr != NULL;
1286 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1287 __acquires(RCU)
1289 int i;
1290 struct net_device *dev;
1292 rcu_read_lock();
1294 if (*pos == 0)
1295 return SEQ_START_TOKEN;
1297 i = 1;
1298 for_each_netdev_rcu(&init_net, dev) {
1299 if (!is_dn_dev(dev))
1300 continue;
1302 if (i++ == *pos)
1303 return dev;
1306 return NULL;
1309 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1311 struct net_device *dev;
1313 ++*pos;
1315 dev = v;
1316 if (v == SEQ_START_TOKEN)
1317 dev = net_device_entry(&init_net.dev_base_head);
1319 for_each_netdev_continue_rcu(&init_net, dev) {
1320 if (!is_dn_dev(dev))
1321 continue;
1323 return dev;
1326 return NULL;
1329 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1330 __releases(RCU)
1332 rcu_read_unlock();
1335 static char *dn_type2asc(char type)
1337 switch (type) {
1338 case DN_DEV_BCAST:
1339 return "B";
1340 case DN_DEV_UCAST:
1341 return "U";
1342 case DN_DEV_MPOINT:
1343 return "M";
1346 return "?";
1349 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1351 if (v == SEQ_START_TOKEN)
1352 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1353 else {
1354 struct net_device *dev = v;
1355 char peer_buf[DN_ASCBUF_LEN];
1356 char router_buf[DN_ASCBUF_LEN];
1357 struct dn_dev *dn_db = rcu_dereference(dev->dn_ptr);
1359 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
1360 " %04hu %03d %02x %-10s %-7s %-7s\n",
1361 dev->name ? dev->name : "???",
1362 dn_type2asc(dn_db->parms.mode),
1363 0, 0,
1364 dn_db->t3, dn_db->parms.t3,
1365 mtu2blksize(dev),
1366 dn_db->parms.priority,
1367 dn_db->parms.state, dn_db->parms.name,
1368 dn_db->router ? dn_addr2asc(le16_to_cpu(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1369 dn_db->peer ? dn_addr2asc(le16_to_cpu(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1371 return 0;
1374 static const struct seq_operations dn_dev_seq_ops = {
1375 .start = dn_dev_seq_start,
1376 .next = dn_dev_seq_next,
1377 .stop = dn_dev_seq_stop,
1378 .show = dn_dev_seq_show,
1381 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1383 return seq_open(file, &dn_dev_seq_ops);
1386 static const struct file_operations dn_dev_seq_fops = {
1387 .owner = THIS_MODULE,
1388 .open = dn_dev_seq_open,
1389 .read = seq_read,
1390 .llseek = seq_lseek,
1391 .release = seq_release,
1394 #endif /* CONFIG_PROC_FS */
1396 static int addr[2];
1397 module_param_array(addr, int, NULL, 0444);
1398 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1400 void __init dn_dev_init(void)
1402 if (addr[0] > 63 || addr[0] < 0) {
1403 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1404 return;
1407 if (addr[1] > 1023 || addr[1] < 0) {
1408 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1409 return;
1412 decnet_address = cpu_to_le16((addr[0] << 10) | addr[1]);
1414 dn_dev_devices_on();
1416 rtnl_register(PF_DECnet, RTM_NEWADDR, dn_nl_newaddr, NULL, NULL);
1417 rtnl_register(PF_DECnet, RTM_DELADDR, dn_nl_deladdr, NULL, NULL);
1418 rtnl_register(PF_DECnet, RTM_GETADDR, NULL, dn_nl_dump_ifaddr, NULL);
1420 proc_create("decnet_dev", S_IRUGO, init_net.proc_net, &dn_dev_seq_fops);
1422 #ifdef CONFIG_SYSCTL
1424 int i;
1425 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1426 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1428 #endif /* CONFIG_SYSCTL */
1431 void __exit dn_dev_cleanup(void)
1433 #ifdef CONFIG_SYSCTL
1435 int i;
1436 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1437 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1439 #endif /* CONFIG_SYSCTL */
1441 remove_proc_entry("decnet_dev", init_net.proc_net);
1443 dn_dev_devices_off();