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.
8 * Authors: Steve Whitehouse <SteveW@ACM.org>
9 * Eduardo Marcelo Serrat <emserrat@geocities.com>
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 <asm/uaccess.h>
44 #include <asm/system.h>
45 #include <net/neighbour.h>
48 #include <net/fib_rules.h>
49 #include <net/netlink.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>
56 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
58 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
59 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
60 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
61 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
63 extern struct neigh_table dn_neigh_table;
66 * decnet_address is kept in network order.
68 __le16 decnet_address = 0;
70 static DEFINE_RWLOCK(dndev_lock);
71 static struct net_device *decnet_default_device;
72 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
74 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
75 static void dn_dev_delete(struct net_device *dev);
76 static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa);
78 static int dn_eth_up(struct net_device *);
79 static void dn_eth_down(struct net_device *);
80 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
81 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
83 static struct dn_dev_parms dn_dev_list[] = {
85 .type = ARPHRD_ETHER, /* Ethernet */
91 .ctl_name = NET_DECNET_CONF_ETHER,
94 .timer3 = dn_send_brd_hello,
97 .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
103 .ctl_name = NET_DECNET_CONF_GRE,
104 .timer3 = dn_send_brd_hello,
108 .type = ARPHRD_X25, /* Bog standard X.25 */
109 .mode = DN_DEV_UCAST,
110 .state = DN_DEV_S_DS,
114 .ctl_name = NET_DECNET_CONF_X25,
115 .timer3 = dn_send_ptp_hello,
120 .type = ARPHRD_PPP, /* DECnet over PPP */
121 .mode = DN_DEV_BCAST,
122 .state = DN_DEV_S_RU,
126 .ctl_name = NET_DECNET_CONF_PPP,
127 .timer3 = dn_send_brd_hello,
131 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
132 .mode = DN_DEV_UCAST,
133 .state = DN_DEV_S_DS,
137 .ctl_name = NET_DECNET_CONF_DDCMP,
138 .timer3 = dn_send_ptp_hello,
141 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
142 .mode = DN_DEV_BCAST,
143 .state = DN_DEV_S_RU,
147 .ctl_name = NET_DECNET_CONF_LOOPBACK,
148 .timer3 = dn_send_brd_hello,
152 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
154 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
158 static int min_t2[] = { 1 };
159 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
160 static int min_t3[] = { 1 };
161 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
163 static int min_priority[1];
164 static int max_priority[] = { 127 }; /* From DECnet spec */
166 static int dn_forwarding_proc(ctl_table *, int, struct file *,
167 void __user *, size_t *, loff_t *);
168 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
169 void __user *oldval, size_t __user *oldlenp,
170 void __user *newval, size_t newlen);
172 static 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];
183 .ctl_name = NET_DECNET_CONF_DEV_FORWARDING,
184 .procname = "forwarding",
185 .data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
186 .maxlen = sizeof(int),
188 .proc_handler = dn_forwarding_proc,
189 .strategy = dn_forwarding_sysctl,
192 .ctl_name = NET_DECNET_CONF_DEV_PRIORITY,
193 .procname = "priority",
194 .data = (void *)DN_DEV_PARMS_OFFSET(priority),
195 .maxlen = sizeof(int),
197 .proc_handler = proc_dointvec_minmax,
198 .strategy = sysctl_intvec,
199 .extra1 = &min_priority,
200 .extra2 = &max_priority
203 .ctl_name = NET_DECNET_CONF_DEV_T2,
205 .data = (void *)DN_DEV_PARMS_OFFSET(t2),
206 .maxlen = sizeof(int),
208 .proc_handler = proc_dointvec_minmax,
209 .strategy = sysctl_intvec,
214 .ctl_name = NET_DECNET_CONF_DEV_T3,
216 .data = (void *)DN_DEV_PARMS_OFFSET(t3),
217 .maxlen = sizeof(int),
219 .proc_handler = proc_dointvec_minmax,
220 .strategy = sysctl_intvec,
230 .child = dn_dev_sysctl.dn_dev_vars
233 .ctl_name = NET_DECNET_CONF,
236 .child = dn_dev_sysctl.dn_dev_dev
239 .ctl_name = NET_DECNET,
240 .procname = "decnet",
242 .child = dn_dev_sysctl.dn_dev_conf_dir
248 .child = dn_dev_sysctl.dn_dev_proto_dir
252 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
254 struct dn_dev_sysctl_table *t;
257 t = kmemdup(&dn_dev_sysctl, sizeof(*t), GFP_KERNEL);
261 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;
264 t->dn_dev_vars[i].de = NULL;
268 t->dn_dev_dev[0].procname = dev->name;
269 t->dn_dev_dev[0].ctl_name = dev->ifindex;
271 t->dn_dev_dev[0].procname = parms->name;
272 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
275 t->dn_dev_dev[0].child = t->dn_dev_vars;
276 t->dn_dev_dev[0].de = NULL;
277 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
278 t->dn_dev_conf_dir[0].de = NULL;
279 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
280 t->dn_dev_proto_dir[0].de = NULL;
281 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
282 t->dn_dev_root_dir[0].de = NULL;
283 t->dn_dev_vars[0].extra1 = (void *)dev;
285 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
286 if (t->sysctl_header == NULL)
292 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
295 struct dn_dev_sysctl_table *t = parms->sysctl;
296 parms->sysctl = NULL;
297 unregister_sysctl_table(t->sysctl_header);
302 static int dn_forwarding_proc(ctl_table *table, int write,
305 size_t *lenp, loff_t *ppos)
307 #ifdef CONFIG_DECNET_ROUTER
308 struct net_device *dev = table->extra1;
309 struct dn_dev *dn_db;
313 if (table->extra1 == NULL)
317 old = dn_db->parms.forwarding;
319 err = proc_dointvec(table, write, filep, buffer, lenp, ppos);
321 if ((err >= 0) && write) {
322 if (dn_db->parms.forwarding < 0)
323 dn_db->parms.forwarding = 0;
324 if (dn_db->parms.forwarding > 2)
325 dn_db->parms.forwarding = 2;
327 * What an ugly hack this is... its works, just. It
328 * would be nice if sysctl/proc were just that little
329 * bit more flexible so I don't have to write a special
330 * routine, or suffer hacks like this - SJW
332 tmp = dn_db->parms.forwarding;
333 dn_db->parms.forwarding = old;
334 if (dn_db->parms.down)
335 dn_db->parms.down(dev);
336 dn_db->parms.forwarding = tmp;
338 dn_db->parms.up(dev);
347 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
348 void __user *oldval, size_t __user *oldlenp,
349 void __user *newval, size_t newlen)
351 #ifdef CONFIG_DECNET_ROUTER
352 struct net_device *dev = table->extra1;
353 struct dn_dev *dn_db;
356 if (table->extra1 == NULL)
361 if (newval && newlen) {
362 if (newlen != sizeof(int))
365 if (get_user(value, (int __user *)newval))
372 if (dn_db->parms.down)
373 dn_db->parms.down(dev);
374 dn_db->parms.forwarding = value;
376 dn_db->parms.up(dev);
385 #else /* CONFIG_SYSCTL */
386 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
389 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
393 #endif /* CONFIG_SYSCTL */
395 static inline __u16 mtu2blksize(struct net_device *dev)
397 u32 blksize = dev->mtu;
398 if (blksize > 0xffff)
401 if (dev->type == ARPHRD_ETHER ||
402 dev->type == ARPHRD_PPP ||
403 dev->type == ARPHRD_IPGRE ||
404 dev->type == ARPHRD_LOOPBACK)
407 return (__u16)blksize;
410 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
412 struct dn_ifaddr *ifa;
414 ifa = kzalloc(sizeof(*ifa), GFP_KERNEL);
419 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
424 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
426 struct dn_ifaddr *ifa1 = *ifap;
427 unsigned char mac_addr[6];
428 struct net_device *dev = dn_db->dev;
432 *ifap = ifa1->ifa_next;
434 if (dn_db->dev->type == ARPHRD_ETHER) {
435 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
436 dn_dn2eth(mac_addr, ifa1->ifa_local);
437 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
441 dn_ifaddr_notify(RTM_DELADDR, ifa1);
442 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
444 dn_dev_free_ifa(ifa1);
446 if (dn_db->ifa_list == NULL)
447 dn_dev_delete(dn_db->dev);
451 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
453 struct net_device *dev = dn_db->dev;
454 struct dn_ifaddr *ifa1;
455 unsigned char mac_addr[6];
459 /* Check for duplicates */
460 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
461 if (ifa1->ifa_local == ifa->ifa_local)
465 if (dev->type == ARPHRD_ETHER) {
466 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
467 dn_dn2eth(mac_addr, ifa->ifa_local);
468 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
473 ifa->ifa_next = dn_db->ifa_list;
474 dn_db->ifa_list = ifa;
476 dn_ifaddr_notify(RTM_NEWADDR, ifa);
477 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
482 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
484 struct dn_dev *dn_db = dev->dn_ptr;
489 dn_db = dn_dev_create(dev, &err);
494 ifa->ifa_dev = dn_db;
496 if (dev->flags & IFF_LOOPBACK)
497 ifa->ifa_scope = RT_SCOPE_HOST;
499 rv = dn_dev_insert_ifa(dn_db, ifa);
501 dn_dev_free_ifa(ifa);
506 int dn_dev_ioctl(unsigned int cmd, void __user *arg)
508 char buffer[DN_IFREQ_SIZE];
509 struct ifreq *ifr = (struct ifreq *)buffer;
510 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
511 struct dn_dev *dn_db;
512 struct net_device *dev;
513 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
516 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
518 ifr->ifr_name[IFNAMSIZ-1] = 0;
521 dev_load(ifr->ifr_name);
528 if (!capable(CAP_NET_ADMIN))
530 if (sdn->sdn_family != AF_DECnet)
539 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
544 if ((dn_db = dev->dn_ptr) != NULL) {
545 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
546 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
550 if (ifa == NULL && cmd != SIOCSIFADDR) {
551 ret = -EADDRNOTAVAIL;
557 *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
562 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
566 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
568 if (ifa->ifa_local == dn_saddr2dn(sdn))
570 dn_dev_del_ifa(dn_db, ifap, 0);
573 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
575 ret = dn_dev_set_ifa(dev, ifa);
582 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
587 struct net_device *dn_dev_get_default(void)
589 struct net_device *dev;
590 read_lock(&dndev_lock);
591 dev = decnet_default_device;
598 read_unlock(&dndev_lock);
602 int dn_dev_set_default(struct net_device *dev, int force)
604 struct net_device *old = NULL;
608 write_lock(&dndev_lock);
609 if (force || decnet_default_device == NULL) {
610 old = decnet_default_device;
611 decnet_default_device = dev;
614 write_unlock(&dndev_lock);
620 static void dn_dev_check_default(struct net_device *dev)
622 write_lock(&dndev_lock);
623 if (dev == decnet_default_device) {
624 decnet_default_device = NULL;
628 write_unlock(&dndev_lock);
633 static struct dn_dev *dn_dev_by_index(int ifindex)
635 struct net_device *dev;
636 struct dn_dev *dn_dev = NULL;
637 dev = dev_get_by_index(ifindex);
639 dn_dev = dev->dn_ptr;
646 static struct nla_policy dn_ifa_policy[IFA_MAX+1] __read_mostly = {
647 [IFA_ADDRESS] = { .type = NLA_U16 },
648 [IFA_LOCAL] = { .type = NLA_U16 },
649 [IFA_LABEL] = { .type = NLA_STRING,
650 .len = IFNAMSIZ - 1 },
653 static int dn_nl_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
655 struct nlattr *tb[IFA_MAX+1];
656 struct dn_dev *dn_db;
657 struct ifaddrmsg *ifm;
658 struct dn_ifaddr *ifa, **ifap;
659 int err = -EADDRNOTAVAIL;
661 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
665 ifm = nlmsg_data(nlh);
666 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
669 for (ifap = &dn_db->ifa_list; (ifa = *ifap); ifap = &ifa->ifa_next) {
671 nla_memcmp(tb[IFA_LOCAL], &ifa->ifa_local, 2))
674 if (tb[IFA_LABEL] && nla_strcmp(tb[IFA_LABEL], ifa->ifa_label))
677 dn_dev_del_ifa(dn_db, ifap, 1);
685 static int dn_nl_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
687 struct nlattr *tb[IFA_MAX+1];
688 struct net_device *dev;
689 struct dn_dev *dn_db;
690 struct ifaddrmsg *ifm;
691 struct dn_ifaddr *ifa;
694 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFA_MAX, dn_ifa_policy);
698 if (tb[IFA_LOCAL] == NULL)
701 ifm = nlmsg_data(nlh);
702 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
705 if ((dn_db = dev->dn_ptr) == NULL) {
707 dn_db = dn_dev_create(dev, &err);
712 if ((ifa = dn_dev_alloc_ifa()) == NULL)
715 if (tb[IFA_ADDRESS] == NULL)
716 tb[IFA_ADDRESS] = tb[IFA_LOCAL];
718 ifa->ifa_local = nla_get_le16(tb[IFA_LOCAL]);
719 ifa->ifa_address = nla_get_le16(tb[IFA_ADDRESS]);
720 ifa->ifa_flags = ifm->ifa_flags;
721 ifa->ifa_scope = ifm->ifa_scope;
722 ifa->ifa_dev = dn_db;
725 nla_strlcpy(ifa->ifa_label, tb[IFA_LABEL], IFNAMSIZ);
727 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
729 err = dn_dev_insert_ifa(dn_db, ifa);
731 dn_dev_free_ifa(ifa);
736 static inline size_t dn_ifaddr_nlmsg_size(void)
738 return NLMSG_ALIGN(sizeof(struct ifaddrmsg))
739 + nla_total_size(IFNAMSIZ) /* IFA_LABEL */
740 + nla_total_size(2) /* IFA_ADDRESS */
741 + nla_total_size(2); /* IFA_LOCAL */
744 static int dn_nl_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
745 u32 pid, u32 seq, int event, unsigned int flags)
747 struct ifaddrmsg *ifm;
748 struct nlmsghdr *nlh;
750 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*ifm), flags);
754 ifm = nlmsg_data(nlh);
755 ifm->ifa_family = AF_DECnet;
756 ifm->ifa_prefixlen = 16;
757 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
758 ifm->ifa_scope = ifa->ifa_scope;
759 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
761 if (ifa->ifa_address)
762 NLA_PUT_LE16(skb, IFA_ADDRESS, ifa->ifa_address);
764 NLA_PUT_LE16(skb, IFA_LOCAL, ifa->ifa_local);
765 if (ifa->ifa_label[0])
766 NLA_PUT_STRING(skb, IFA_LABEL, ifa->ifa_label);
768 return nlmsg_end(skb, nlh);
771 nlmsg_cancel(skb, nlh);
775 static void dn_ifaddr_notify(int event, struct dn_ifaddr *ifa)
780 skb = alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL);
784 err = dn_nl_fill_ifaddr(skb, ifa, 0, 0, event, 0);
786 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
787 WARN_ON(err == -EMSGSIZE);
791 err = rtnl_notify(skb, 0, RTNLGRP_DECnet_IFADDR, NULL, GFP_KERNEL);
794 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR, err);
797 static int dn_nl_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
799 int idx, dn_idx = 0, skip_ndevs, skip_naddr;
800 struct net_device *dev;
801 struct dn_dev *dn_db;
802 struct dn_ifaddr *ifa;
804 skip_ndevs = cb->args[0];
805 skip_naddr = cb->args[1];
807 read_lock(&dev_base_lock);
808 for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
809 if (idx < skip_ndevs)
811 else if (idx > skip_ndevs) {
812 /* Only skip over addresses for first dev dumped
813 * in this iteration (idx == skip_ndevs) */
817 if ((dn_db = dev->dn_ptr) == NULL)
820 for (ifa = dn_db->ifa_list, dn_idx = 0; ifa;
821 ifa = ifa->ifa_next, dn_idx++) {
822 if (dn_idx < skip_naddr)
825 if (dn_nl_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid,
826 cb->nlh->nlmsg_seq, RTM_NEWADDR,
832 read_unlock(&dev_base_lock);
835 cb->args[1] = dn_idx;
840 static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
842 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
843 struct dn_ifaddr *ifa;
847 ifa = dn_db->ifa_list;
849 *addr = ifa->ifa_local;
857 * Find a default address to bind to.
859 * This is one of those areas where the initial VMS concepts don't really
860 * map onto the Linux concepts, and since we introduced multiple addresses
861 * per interface we have to cope with slightly odd ways of finding out what
862 * "our address" really is. Mostly it's not a problem; for this we just guess
863 * a sensible default. Eventually the routing code will take care of all the
864 * nasties for us I hope.
866 int dn_dev_bind_default(__le16 *addr)
868 struct net_device *dev;
870 dev = dn_dev_get_default();
873 read_lock(&dev_base_lock);
874 rv = dn_dev_get_first(dev, addr);
875 read_unlock(&dev_base_lock);
877 if (rv == 0 || dev == &loopback_dev)
885 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
887 struct endnode_hello_message *msg;
888 struct sk_buff *skb = NULL;
890 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
892 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
897 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
900 memcpy(msg->tiver, dn_eco_version, 3);
901 dn_dn2eth(msg->id, ifa->ifa_local);
902 msg->iinfo = DN_RT_INFO_ENDN;
903 msg->blksize = dn_htons(mtu2blksize(dev));
905 memset(msg->seed, 0, 8);
906 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
909 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
910 dn_dn2eth(msg->neighbor, dn->addr);
913 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
916 memset(msg->data, 0xAA, 2);
918 pktlen = (__le16 *)skb_push(skb,2);
919 *pktlen = dn_htons(skb->len - 2);
921 skb->nh.raw = skb->data;
923 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
927 #define DRDELAY (5 * HZ)
929 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
931 /* First check time since device went up */
932 if ((jiffies - dn_db->uptime) < DRDELAY)
935 /* If there is no router, then yes... */
939 /* otherwise only if we have a higher priority or.. */
940 if (dn->priority < dn_db->parms.priority)
943 /* if we have equal priority and a higher node number */
944 if (dn->priority != dn_db->parms.priority)
947 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
953 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
956 struct dn_dev *dn_db = dev->dn_ptr;
957 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
961 unsigned char *i1, *i2;
965 if (mtu2blksize(dev) < (26 + 7))
968 n = mtu2blksize(dev) - 26;
974 size = 2 + 26 + 7 * n;
976 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
980 ptr = skb_put(skb, size);
982 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
983 *ptr++ = 2; /* ECO */
986 dn_dn2eth(ptr, ifa->ifa_local);
989 *ptr++ = dn_db->parms.forwarding == 1 ?
990 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
991 *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
993 *ptr++ = dn_db->parms.priority; /* Priority */
994 *ptr++ = 0; /* Area: Reserved */
995 *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
997 *ptr++ = 0; /* MPD: Reserved */
999 memset(ptr, 0, 7); /* Name: Reserved */
1003 n = dn_neigh_elist(dev, ptr, n);
1008 skb_trim(skb, (27 + *i2));
1010 pktlen = (__le16 *)skb_push(skb, 2);
1011 *pktlen = dn_htons(skb->len - 2);
1013 skb->nh.raw = skb->data;
1015 if (dn_am_i_a_router(dn, dn_db, ifa)) {
1016 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
1018 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
1022 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1025 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1027 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1029 if (dn_db->parms.forwarding == 0)
1030 dn_send_endnode_hello(dev, ifa);
1032 dn_send_router_hello(dev, ifa);
1035 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1038 int size = dev->hard_header_len + 2 + 4 + tdlen;
1039 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1048 skb_push(skb, dev->hard_header_len);
1049 ptr = skb_put(skb, 2 + 4 + tdlen);
1051 *ptr++ = DN_RT_PKT_HELO;
1052 *((__le16 *)ptr) = ifa->ifa_local;
1056 for(i = 0; i < tdlen; i++)
1059 dn_dn2eth(src, ifa->ifa_local);
1060 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1063 static int dn_eth_up(struct net_device *dev)
1065 struct dn_dev *dn_db = dev->dn_ptr;
1067 if (dn_db->parms.forwarding == 0)
1068 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1070 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1074 dn_db->use_long = 1;
1079 static void dn_eth_down(struct net_device *dev)
1081 struct dn_dev *dn_db = dev->dn_ptr;
1083 if (dn_db->parms.forwarding == 0)
1084 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1086 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1089 static void dn_dev_set_timer(struct net_device *dev);
1091 static void dn_dev_timer_func(unsigned long arg)
1093 struct net_device *dev = (struct net_device *)arg;
1094 struct dn_dev *dn_db = dev->dn_ptr;
1095 struct dn_ifaddr *ifa;
1097 if (dn_db->t3 <= dn_db->parms.t2) {
1098 if (dn_db->parms.timer3) {
1099 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1100 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1101 dn_db->parms.timer3(dev, ifa);
1104 dn_db->t3 = dn_db->parms.t3;
1106 dn_db->t3 -= dn_db->parms.t2;
1109 dn_dev_set_timer(dev);
1112 static void dn_dev_set_timer(struct net_device *dev)
1114 struct dn_dev *dn_db = dev->dn_ptr;
1116 if (dn_db->parms.t2 > dn_db->parms.t3)
1117 dn_db->parms.t2 = dn_db->parms.t3;
1119 dn_db->timer.data = (unsigned long)dev;
1120 dn_db->timer.function = dn_dev_timer_func;
1121 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1123 add_timer(&dn_db->timer);
1126 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1129 struct dn_dev_parms *p = dn_dev_list;
1130 struct dn_dev *dn_db;
1132 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1133 if (p->type == dev->type)
1138 if (i == DN_DEV_LIST_SIZE)
1142 if ((dn_db = kzalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1145 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1147 dev->dn_ptr = dn_db;
1149 init_timer(&dn_db->timer);
1151 dn_db->uptime = jiffies;
1153 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1154 if (!dn_db->neigh_parms) {
1160 if (dn_db->parms.up) {
1161 if (dn_db->parms.up(dev) < 0) {
1162 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1169 dn_dev_sysctl_register(dev, &dn_db->parms);
1171 dn_dev_set_timer(dev);
1179 * This processes a device up event. We only start up
1180 * the loopback device & ethernet devices with correct
1181 * MAC addreses automatically. Others must be started
1184 * FIXME: How should we configure the loopback address ? If we could dispense
1185 * with using decnet_address here and for autobind, it will be one less thing
1186 * for users to worry about setting up.
1189 void dn_dev_up(struct net_device *dev)
1191 struct dn_ifaddr *ifa;
1192 __le16 addr = decnet_address;
1193 int maybe_default = 0;
1194 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1196 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1200 * Need to ensure that loopback device has a dn_db attached to it
1201 * to allow creation of neighbours against it, even though it might
1202 * not have a local address of its own. Might as well do the same for
1203 * all autoconfigured interfaces.
1205 if (dn_db == NULL) {
1207 dn_db = dn_dev_create(dev, &err);
1212 if (dev->type == ARPHRD_ETHER) {
1213 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1215 addr = dn_eth2dn(dev->dev_addr);
1222 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1225 ifa->ifa_local = ifa->ifa_address = addr;
1227 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1228 strcpy(ifa->ifa_label, dev->name);
1230 dn_dev_set_ifa(dev, ifa);
1233 * Automagically set the default device to the first automatically
1234 * configured ethernet card in the system.
1236 if (maybe_default) {
1238 if (dn_dev_set_default(dev, 0))
1243 static void dn_dev_delete(struct net_device *dev)
1245 struct dn_dev *dn_db = dev->dn_ptr;
1250 del_timer_sync(&dn_db->timer);
1251 dn_dev_sysctl_unregister(&dn_db->parms);
1252 dn_dev_check_default(dev);
1253 neigh_ifdown(&dn_neigh_table, dev);
1255 if (dn_db->parms.down)
1256 dn_db->parms.down(dev);
1260 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1261 neigh_ifdown(&dn_neigh_table, dev);
1264 neigh_release(dn_db->router);
1266 neigh_release(dn_db->peer);
1271 void dn_dev_down(struct net_device *dev)
1273 struct dn_dev *dn_db = dev->dn_ptr;
1274 struct dn_ifaddr *ifa;
1279 while((ifa = dn_db->ifa_list) != NULL) {
1280 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1281 dn_dev_free_ifa(ifa);
1287 void dn_dev_init_pkt(struct sk_buff *skb)
1292 void dn_dev_veri_pkt(struct sk_buff *skb)
1297 void dn_dev_hello(struct sk_buff *skb)
1302 void dn_dev_devices_off(void)
1304 struct net_device *dev;
1307 for(dev = dev_base; dev; dev = dev->next)
1313 void dn_dev_devices_on(void)
1315 struct net_device *dev;
1318 for(dev = dev_base; dev; dev = dev->next) {
1319 if (dev->flags & IFF_UP)
1325 int register_dnaddr_notifier(struct notifier_block *nb)
1327 return blocking_notifier_chain_register(&dnaddr_chain, nb);
1330 int unregister_dnaddr_notifier(struct notifier_block *nb)
1332 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1335 #ifdef CONFIG_PROC_FS
1336 static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1340 } while(dev && !dev->dn_ptr);
1345 static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1347 struct net_device *dev;
1350 if (dev && !dev->dn_ptr)
1351 dev = dn_dev_get_next(seq, dev);
1353 while(dev && (dev = dn_dev_get_next(seq, dev)))
1359 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1362 struct net_device *dev;
1363 read_lock(&dev_base_lock);
1364 dev = dn_dev_get_idx(seq, *pos - 1);
1366 read_unlock(&dev_base_lock);
1369 return SEQ_START_TOKEN;
1372 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1374 struct net_device *dev = v;
1377 if (v == SEQ_START_TOKEN) {
1378 dev = dn_dev_seq_start(seq, &one);
1380 dev = dn_dev_get_next(seq, dev);
1382 read_unlock(&dev_base_lock);
1388 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1390 if (v && v != SEQ_START_TOKEN)
1391 read_unlock(&dev_base_lock);
1394 static char *dn_type2asc(char type)
1408 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1410 if (v == SEQ_START_TOKEN)
1411 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1413 struct net_device *dev = v;
1414 char peer_buf[DN_ASCBUF_LEN];
1415 char router_buf[DN_ASCBUF_LEN];
1416 struct dn_dev *dn_db = dev->dn_ptr;
1418 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
1419 " %04hu %03d %02x %-10s %-7s %-7s\n",
1420 dev->name ? dev->name : "???",
1421 dn_type2asc(dn_db->parms.mode),
1423 dn_db->t3, dn_db->parms.t3,
1425 dn_db->parms.priority,
1426 dn_db->parms.state, dn_db->parms.name,
1427 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1428 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1433 static struct seq_operations dn_dev_seq_ops = {
1434 .start = dn_dev_seq_start,
1435 .next = dn_dev_seq_next,
1436 .stop = dn_dev_seq_stop,
1437 .show = dn_dev_seq_show,
1440 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1442 return seq_open(file, &dn_dev_seq_ops);
1445 static struct file_operations dn_dev_seq_fops = {
1446 .owner = THIS_MODULE,
1447 .open = dn_dev_seq_open,
1449 .llseek = seq_lseek,
1450 .release = seq_release,
1453 #endif /* CONFIG_PROC_FS */
1455 static struct rtnetlink_link dnet_rtnetlink_table[RTM_NR_MSGTYPES] =
1457 [RTM_NEWADDR - RTM_BASE] = { .doit = dn_nl_newaddr, },
1458 [RTM_DELADDR - RTM_BASE] = { .doit = dn_nl_deladdr, },
1459 [RTM_GETADDR - RTM_BASE] = { .dumpit = dn_nl_dump_ifaddr, },
1460 #ifdef CONFIG_DECNET_ROUTER
1461 [RTM_NEWROUTE - RTM_BASE] = { .doit = dn_fib_rtm_newroute, },
1462 [RTM_DELROUTE - RTM_BASE] = { .doit = dn_fib_rtm_delroute, },
1463 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
1464 .dumpit = dn_fib_dump, },
1465 [RTM_GETRULE - RTM_BASE] = { .dumpit = dn_fib_dump_rules, },
1467 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
1468 .dumpit = dn_cache_dump, },
1473 static int __initdata addr[2];
1474 module_param_array(addr, int, NULL, 0444);
1475 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1477 void __init dn_dev_init(void)
1479 if (addr[0] > 63 || addr[0] < 0) {
1480 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1484 if (addr[1] > 1023 || addr[1] < 0) {
1485 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1489 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1491 dn_dev_devices_on();
1493 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1495 proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1497 #ifdef CONFIG_SYSCTL
1500 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1501 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1503 #endif /* CONFIG_SYSCTL */
1506 void __exit dn_dev_cleanup(void)
1508 rtnetlink_links[PF_DECnet] = NULL;
1510 #ifdef CONFIG_SYSCTL
1513 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1514 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1516 #endif /* CONFIG_SYSCTL */
1518 proc_net_remove("decnet_dev");
1520 dn_dev_devices_off();