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/config.h>
28 #include <linux/capability.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/net.h>
33 #include <linux/netdevice.h>
34 #include <linux/proc_fs.h>
35 #include <linux/seq_file.h>
36 #include <linux/timer.h>
37 #include <linux/string.h>
38 #include <linux/if_arp.h>
39 #include <linux/if_ether.h>
40 #include <linux/skbuff.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/sysctl.h>
43 #include <linux/notifier.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46 #include <net/neighbour.h>
50 #include <net/dn_dev.h>
51 #include <net/dn_route.h>
52 #include <net/dn_neigh.h>
53 #include <net/dn_fib.h>
55 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
57 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
58 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
59 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
60 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
62 extern struct neigh_table dn_neigh_table;
65 * decnet_address is kept in network order.
67 __le16 decnet_address = 0;
69 static DEFINE_RWLOCK(dndev_lock);
70 static struct net_device *decnet_default_device;
71 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain);
73 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
74 static void dn_dev_delete(struct net_device *dev);
75 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa);
77 static int dn_eth_up(struct net_device *);
78 static void dn_eth_down(struct net_device *);
79 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
80 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
82 static struct dn_dev_parms dn_dev_list[] = {
84 .type = ARPHRD_ETHER, /* Ethernet */
90 .ctl_name = NET_DECNET_CONF_ETHER,
93 .timer3 = dn_send_brd_hello,
96 .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
102 .ctl_name = NET_DECNET_CONF_GRE,
103 .timer3 = dn_send_brd_hello,
107 .type = ARPHRD_X25, /* Bog standard X.25 */
108 .mode = DN_DEV_UCAST,
109 .state = DN_DEV_S_DS,
113 .ctl_name = NET_DECNET_CONF_X25,
114 .timer3 = dn_send_ptp_hello,
119 .type = ARPHRD_PPP, /* DECnet over PPP */
120 .mode = DN_DEV_BCAST,
121 .state = DN_DEV_S_RU,
125 .ctl_name = NET_DECNET_CONF_PPP,
126 .timer3 = dn_send_brd_hello,
130 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
131 .mode = DN_DEV_UCAST,
132 .state = DN_DEV_S_DS,
136 .ctl_name = NET_DECNET_CONF_DDCMP,
137 .timer3 = dn_send_ptp_hello,
140 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
141 .mode = DN_DEV_BCAST,
142 .state = DN_DEV_S_RU,
146 .ctl_name = NET_DECNET_CONF_LOOPBACK,
147 .timer3 = dn_send_brd_hello,
151 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
153 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
157 static int min_t2[] = { 1 };
158 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
159 static int min_t3[] = { 1 };
160 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
162 static int min_priority[1];
163 static int max_priority[] = { 127 }; /* From DECnet spec */
165 static int dn_forwarding_proc(ctl_table *, int, struct file *,
166 void __user *, size_t *, loff_t *);
167 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
168 void __user *oldval, size_t __user *oldlenp,
169 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 = kmalloc(sizeof(*t), GFP_KERNEL);
261 memcpy(t, &dn_dev_sysctl, sizeof(*t));
263 for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
264 long offset = (long)t->dn_dev_vars[i].data;
265 t->dn_dev_vars[i].data = ((char *)parms) + offset;
266 t->dn_dev_vars[i].de = NULL;
270 t->dn_dev_dev[0].procname = dev->name;
271 t->dn_dev_dev[0].ctl_name = dev->ifindex;
273 t->dn_dev_dev[0].procname = parms->name;
274 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
277 t->dn_dev_dev[0].child = t->dn_dev_vars;
278 t->dn_dev_dev[0].de = NULL;
279 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
280 t->dn_dev_conf_dir[0].de = NULL;
281 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
282 t->dn_dev_proto_dir[0].de = NULL;
283 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
284 t->dn_dev_root_dir[0].de = NULL;
285 t->dn_dev_vars[0].extra1 = (void *)dev;
287 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
288 if (t->sysctl_header == NULL)
294 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
297 struct dn_dev_sysctl_table *t = parms->sysctl;
298 parms->sysctl = NULL;
299 unregister_sysctl_table(t->sysctl_header);
304 static int dn_forwarding_proc(ctl_table *table, int write,
307 size_t *lenp, loff_t *ppos)
309 #ifdef CONFIG_DECNET_ROUTER
310 struct net_device *dev = table->extra1;
311 struct dn_dev *dn_db;
315 if (table->extra1 == NULL)
319 old = dn_db->parms.forwarding;
321 err = proc_dointvec(table, write, filep, buffer, lenp, ppos);
323 if ((err >= 0) && write) {
324 if (dn_db->parms.forwarding < 0)
325 dn_db->parms.forwarding = 0;
326 if (dn_db->parms.forwarding > 2)
327 dn_db->parms.forwarding = 2;
329 * What an ugly hack this is... its works, just. It
330 * would be nice if sysctl/proc were just that little
331 * bit more flexible so I don't have to write a special
332 * routine, or suffer hacks like this - SJW
334 tmp = dn_db->parms.forwarding;
335 dn_db->parms.forwarding = old;
336 if (dn_db->parms.down)
337 dn_db->parms.down(dev);
338 dn_db->parms.forwarding = tmp;
340 dn_db->parms.up(dev);
349 static int dn_forwarding_sysctl(ctl_table *table, int __user *name, int nlen,
350 void __user *oldval, size_t __user *oldlenp,
351 void __user *newval, size_t newlen,
354 #ifdef CONFIG_DECNET_ROUTER
355 struct net_device *dev = table->extra1;
356 struct dn_dev *dn_db;
359 if (table->extra1 == NULL)
364 if (newval && newlen) {
365 if (newlen != sizeof(int))
368 if (get_user(value, (int __user *)newval))
375 if (dn_db->parms.down)
376 dn_db->parms.down(dev);
377 dn_db->parms.forwarding = value;
379 dn_db->parms.up(dev);
388 #else /* CONFIG_SYSCTL */
389 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
392 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
396 #endif /* CONFIG_SYSCTL */
398 static inline __u16 mtu2blksize(struct net_device *dev)
400 u32 blksize = dev->mtu;
401 if (blksize > 0xffff)
404 if (dev->type == ARPHRD_ETHER ||
405 dev->type == ARPHRD_PPP ||
406 dev->type == ARPHRD_IPGRE ||
407 dev->type == ARPHRD_LOOPBACK)
410 return (__u16)blksize;
413 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
415 struct dn_ifaddr *ifa;
417 ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
420 memset(ifa, 0, sizeof(*ifa));
426 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
431 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
433 struct dn_ifaddr *ifa1 = *ifap;
434 unsigned char mac_addr[6];
435 struct net_device *dev = dn_db->dev;
439 *ifap = ifa1->ifa_next;
441 if (dn_db->dev->type == ARPHRD_ETHER) {
442 if (ifa1->ifa_local != dn_eth2dn(dev->dev_addr)) {
443 dn_dn2eth(mac_addr, ifa1->ifa_local);
444 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
448 rtmsg_ifa(RTM_DELADDR, ifa1);
449 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
451 dn_dev_free_ifa(ifa1);
453 if (dn_db->ifa_list == NULL)
454 dn_dev_delete(dn_db->dev);
458 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
460 struct net_device *dev = dn_db->dev;
461 struct dn_ifaddr *ifa1;
462 unsigned char mac_addr[6];
466 /* Check for duplicates */
467 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
468 if (ifa1->ifa_local == ifa->ifa_local)
472 if (dev->type == ARPHRD_ETHER) {
473 if (ifa->ifa_local != dn_eth2dn(dev->dev_addr)) {
474 dn_dn2eth(mac_addr, ifa->ifa_local);
475 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
480 ifa->ifa_next = dn_db->ifa_list;
481 dn_db->ifa_list = ifa;
483 rtmsg_ifa(RTM_NEWADDR, ifa);
484 blocking_notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
489 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
491 struct dn_dev *dn_db = dev->dn_ptr;
496 dn_db = dn_dev_create(dev, &err);
501 ifa->ifa_dev = dn_db;
503 if (dev->flags & IFF_LOOPBACK)
504 ifa->ifa_scope = RT_SCOPE_HOST;
506 rv = dn_dev_insert_ifa(dn_db, ifa);
508 dn_dev_free_ifa(ifa);
513 int dn_dev_ioctl(unsigned int cmd, void __user *arg)
515 char buffer[DN_IFREQ_SIZE];
516 struct ifreq *ifr = (struct ifreq *)buffer;
517 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
518 struct dn_dev *dn_db;
519 struct net_device *dev;
520 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
523 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
525 ifr->ifr_name[IFNAMSIZ-1] = 0;
528 dev_load(ifr->ifr_name);
535 if (!capable(CAP_NET_ADMIN))
537 if (sdn->sdn_family != AF_DECnet)
546 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
551 if ((dn_db = dev->dn_ptr) != NULL) {
552 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
553 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
557 if (ifa == NULL && cmd != SIOCSIFADDR) {
558 ret = -EADDRNOTAVAIL;
564 *((__le16 *)sdn->sdn_nodeaddr) = ifa->ifa_local;
569 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
573 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
575 if (ifa->ifa_local == dn_saddr2dn(sdn))
577 dn_dev_del_ifa(dn_db, ifap, 0);
580 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
582 ret = dn_dev_set_ifa(dev, ifa);
589 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
594 struct net_device *dn_dev_get_default(void)
596 struct net_device *dev;
597 read_lock(&dndev_lock);
598 dev = decnet_default_device;
605 read_unlock(&dndev_lock);
609 int dn_dev_set_default(struct net_device *dev, int force)
611 struct net_device *old = NULL;
615 write_lock(&dndev_lock);
616 if (force || decnet_default_device == NULL) {
617 old = decnet_default_device;
618 decnet_default_device = dev;
621 write_unlock(&dndev_lock);
627 static void dn_dev_check_default(struct net_device *dev)
629 write_lock(&dndev_lock);
630 if (dev == decnet_default_device) {
631 decnet_default_device = NULL;
635 write_unlock(&dndev_lock);
640 static struct dn_dev *dn_dev_by_index(int ifindex)
642 struct net_device *dev;
643 struct dn_dev *dn_dev = NULL;
644 dev = dev_get_by_index(ifindex);
646 dn_dev = dev->dn_ptr;
653 static int dn_dev_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
655 struct rtattr **rta = arg;
656 struct dn_dev *dn_db;
657 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
658 struct dn_ifaddr *ifa, **ifap;
660 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
661 return -EADDRNOTAVAIL;
663 for(ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) {
664 void *tmp = rta[IFA_LOCAL-1];
665 if ((tmp && memcmp(RTA_DATA(tmp), &ifa->ifa_local, 2)) ||
666 (rta[IFA_LABEL-1] && rtattr_strcmp(rta[IFA_LABEL-1], ifa->ifa_label)))
669 dn_dev_del_ifa(dn_db, ifap, 1);
673 return -EADDRNOTAVAIL;
676 static int dn_dev_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
678 struct rtattr **rta = arg;
679 struct net_device *dev;
680 struct dn_dev *dn_db;
681 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
682 struct dn_ifaddr *ifa;
685 if (rta[IFA_LOCAL-1] == NULL)
688 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
691 if ((dn_db = dev->dn_ptr) == NULL) {
693 dn_db = dn_dev_create(dev, &err);
698 if ((ifa = dn_dev_alloc_ifa()) == NULL)
701 if (!rta[IFA_ADDRESS - 1])
702 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
703 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 2);
704 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 2);
705 ifa->ifa_flags = ifm->ifa_flags;
706 ifa->ifa_scope = ifm->ifa_scope;
707 ifa->ifa_dev = dn_db;
708 if (rta[IFA_LABEL-1])
709 rtattr_strlcpy(ifa->ifa_label, rta[IFA_LABEL-1], IFNAMSIZ);
711 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
713 rv = dn_dev_insert_ifa(dn_db, ifa);
715 dn_dev_free_ifa(ifa);
719 static int dn_dev_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
720 u32 pid, u32 seq, int event, unsigned int flags)
722 struct ifaddrmsg *ifm;
723 struct nlmsghdr *nlh;
724 unsigned char *b = skb->tail;
726 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*ifm), flags);
727 ifm = NLMSG_DATA(nlh);
729 ifm->ifa_family = AF_DECnet;
730 ifm->ifa_prefixlen = 16;
731 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
732 ifm->ifa_scope = ifa->ifa_scope;
733 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
734 if (ifa->ifa_address)
735 RTA_PUT(skb, IFA_ADDRESS, 2, &ifa->ifa_address);
737 RTA_PUT(skb, IFA_LOCAL, 2, &ifa->ifa_local);
738 if (ifa->ifa_label[0])
739 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
740 nlh->nlmsg_len = skb->tail - b;
745 skb_trim(skb, b - skb->data);
749 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa)
752 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
754 skb = alloc_skb(size, GFP_KERNEL);
756 netlink_set_err(rtnl, 0, RTNLGRP_DECnet_IFADDR, ENOBUFS);
759 if (dn_dev_fill_ifaddr(skb, ifa, 0, 0, event, 0) < 0) {
761 netlink_set_err(rtnl, 0, RTNLGRP_DECnet_IFADDR, EINVAL);
764 NETLINK_CB(skb).dst_group = RTNLGRP_DECnet_IFADDR;
765 netlink_broadcast(rtnl, skb, 0, RTNLGRP_DECnet_IFADDR, GFP_KERNEL);
768 static int dn_dev_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
772 struct net_device *dev;
773 struct dn_dev *dn_db;
774 struct dn_ifaddr *ifa;
777 s_dn_idx = dn_idx = cb->args[1];
778 read_lock(&dev_base_lock);
779 for(dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
784 if ((dn_db = dev->dn_ptr) == NULL)
787 for(ifa = dn_db->ifa_list, dn_idx = 0; ifa; ifa = ifa->ifa_next, dn_idx++) {
788 if (dn_idx < s_dn_idx)
791 if (dn_dev_fill_ifaddr(skb, ifa,
792 NETLINK_CB(cb->skb).pid,
800 read_unlock(&dev_base_lock);
802 cb->args[1] = dn_idx;
807 static int dn_dev_get_first(struct net_device *dev, __le16 *addr)
809 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
810 struct dn_ifaddr *ifa;
814 ifa = dn_db->ifa_list;
816 *addr = ifa->ifa_local;
824 * Find a default address to bind to.
826 * This is one of those areas where the initial VMS concepts don't really
827 * map onto the Linux concepts, and since we introduced multiple addresses
828 * per interface we have to cope with slightly odd ways of finding out what
829 * "our address" really is. Mostly it's not a problem; for this we just guess
830 * a sensible default. Eventually the routing code will take care of all the
831 * nasties for us I hope.
833 int dn_dev_bind_default(__le16 *addr)
835 struct net_device *dev;
837 dev = dn_dev_get_default();
840 read_lock(&dev_base_lock);
841 rv = dn_dev_get_first(dev, addr);
842 read_unlock(&dev_base_lock);
844 if (rv == 0 || dev == &loopback_dev)
852 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
854 struct endnode_hello_message *msg;
855 struct sk_buff *skb = NULL;
857 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
859 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
864 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
867 memcpy(msg->tiver, dn_eco_version, 3);
868 dn_dn2eth(msg->id, ifa->ifa_local);
869 msg->iinfo = DN_RT_INFO_ENDN;
870 msg->blksize = dn_htons(mtu2blksize(dev));
872 memset(msg->seed, 0, 8);
873 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
876 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
877 dn_dn2eth(msg->neighbor, dn->addr);
880 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
883 memset(msg->data, 0xAA, 2);
885 pktlen = (__le16 *)skb_push(skb,2);
886 *pktlen = dn_htons(skb->len - 2);
888 skb->nh.raw = skb->data;
890 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
894 #define DRDELAY (5 * HZ)
896 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
898 /* First check time since device went up */
899 if ((jiffies - dn_db->uptime) < DRDELAY)
902 /* If there is no router, then yes... */
906 /* otherwise only if we have a higher priority or.. */
907 if (dn->priority < dn_db->parms.priority)
910 /* if we have equal priority and a higher node number */
911 if (dn->priority != dn_db->parms.priority)
914 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
920 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
923 struct dn_dev *dn_db = dev->dn_ptr;
924 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
928 unsigned char *i1, *i2;
932 if (mtu2blksize(dev) < (26 + 7))
935 n = mtu2blksize(dev) - 26;
941 size = 2 + 26 + 7 * n;
943 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
947 ptr = skb_put(skb, size);
949 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
950 *ptr++ = 2; /* ECO */
953 dn_dn2eth(ptr, ifa->ifa_local);
956 *ptr++ = dn_db->parms.forwarding == 1 ?
957 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
958 *((__le16 *)ptr) = dn_htons(mtu2blksize(dev));
960 *ptr++ = dn_db->parms.priority; /* Priority */
961 *ptr++ = 0; /* Area: Reserved */
962 *((__le16 *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
964 *ptr++ = 0; /* MPD: Reserved */
966 memset(ptr, 0, 7); /* Name: Reserved */
970 n = dn_neigh_elist(dev, ptr, n);
975 skb_trim(skb, (27 + *i2));
977 pktlen = (__le16 *)skb_push(skb, 2);
978 *pktlen = dn_htons(skb->len - 2);
980 skb->nh.raw = skb->data;
982 if (dn_am_i_a_router(dn, dn_db, ifa)) {
983 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
985 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
989 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
992 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
994 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
996 if (dn_db->parms.forwarding == 0)
997 dn_send_endnode_hello(dev, ifa);
999 dn_send_router_hello(dev, ifa);
1002 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1005 int size = dev->hard_header_len + 2 + 4 + tdlen;
1006 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1015 skb_push(skb, dev->hard_header_len);
1016 ptr = skb_put(skb, 2 + 4 + tdlen);
1018 *ptr++ = DN_RT_PKT_HELO;
1019 *((__le16 *)ptr) = ifa->ifa_local;
1023 for(i = 0; i < tdlen; i++)
1026 dn_dn2eth(src, ifa->ifa_local);
1027 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1030 static int dn_eth_up(struct net_device *dev)
1032 struct dn_dev *dn_db = dev->dn_ptr;
1034 if (dn_db->parms.forwarding == 0)
1035 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1037 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1041 dn_db->use_long = 1;
1046 static void dn_eth_down(struct net_device *dev)
1048 struct dn_dev *dn_db = dev->dn_ptr;
1050 if (dn_db->parms.forwarding == 0)
1051 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1053 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1056 static void dn_dev_set_timer(struct net_device *dev);
1058 static void dn_dev_timer_func(unsigned long arg)
1060 struct net_device *dev = (struct net_device *)arg;
1061 struct dn_dev *dn_db = dev->dn_ptr;
1062 struct dn_ifaddr *ifa;
1064 if (dn_db->t3 <= dn_db->parms.t2) {
1065 if (dn_db->parms.timer3) {
1066 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1067 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1068 dn_db->parms.timer3(dev, ifa);
1071 dn_db->t3 = dn_db->parms.t3;
1073 dn_db->t3 -= dn_db->parms.t2;
1076 dn_dev_set_timer(dev);
1079 static void dn_dev_set_timer(struct net_device *dev)
1081 struct dn_dev *dn_db = dev->dn_ptr;
1083 if (dn_db->parms.t2 > dn_db->parms.t3)
1084 dn_db->parms.t2 = dn_db->parms.t3;
1086 dn_db->timer.data = (unsigned long)dev;
1087 dn_db->timer.function = dn_dev_timer_func;
1088 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1090 add_timer(&dn_db->timer);
1093 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1096 struct dn_dev_parms *p = dn_dev_list;
1097 struct dn_dev *dn_db;
1099 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1100 if (p->type == dev->type)
1105 if (i == DN_DEV_LIST_SIZE)
1109 if ((dn_db = kmalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1112 memset(dn_db, 0, sizeof(struct dn_dev));
1113 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1115 dev->dn_ptr = dn_db;
1117 init_timer(&dn_db->timer);
1119 dn_db->uptime = jiffies;
1120 if (dn_db->parms.up) {
1121 if (dn_db->parms.up(dev) < 0) {
1128 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1130 dn_dev_sysctl_register(dev, &dn_db->parms);
1132 dn_dev_set_timer(dev);
1140 * This processes a device up event. We only start up
1141 * the loopback device & ethernet devices with correct
1142 * MAC addreses automatically. Others must be started
1145 * FIXME: How should we configure the loopback address ? If we could dispense
1146 * with using decnet_address here and for autobind, it will be one less thing
1147 * for users to worry about setting up.
1150 void dn_dev_up(struct net_device *dev)
1152 struct dn_ifaddr *ifa;
1153 __le16 addr = decnet_address;
1154 int maybe_default = 0;
1155 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1157 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1161 * Need to ensure that loopback device has a dn_db attached to it
1162 * to allow creation of neighbours against it, even though it might
1163 * not have a local address of its own. Might as well do the same for
1164 * all autoconfigured interfaces.
1166 if (dn_db == NULL) {
1168 dn_db = dn_dev_create(dev, &err);
1173 if (dev->type == ARPHRD_ETHER) {
1174 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1176 addr = dn_eth2dn(dev->dev_addr);
1183 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1186 ifa->ifa_local = ifa->ifa_address = addr;
1188 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1189 strcpy(ifa->ifa_label, dev->name);
1191 dn_dev_set_ifa(dev, ifa);
1194 * Automagically set the default device to the first automatically
1195 * configured ethernet card in the system.
1197 if (maybe_default) {
1199 if (dn_dev_set_default(dev, 0))
1204 static void dn_dev_delete(struct net_device *dev)
1206 struct dn_dev *dn_db = dev->dn_ptr;
1211 del_timer_sync(&dn_db->timer);
1212 dn_dev_sysctl_unregister(&dn_db->parms);
1213 dn_dev_check_default(dev);
1214 neigh_ifdown(&dn_neigh_table, dev);
1216 if (dn_db->parms.down)
1217 dn_db->parms.down(dev);
1221 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1222 neigh_ifdown(&dn_neigh_table, dev);
1225 neigh_release(dn_db->router);
1227 neigh_release(dn_db->peer);
1232 void dn_dev_down(struct net_device *dev)
1234 struct dn_dev *dn_db = dev->dn_ptr;
1235 struct dn_ifaddr *ifa;
1240 while((ifa = dn_db->ifa_list) != NULL) {
1241 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1242 dn_dev_free_ifa(ifa);
1248 void dn_dev_init_pkt(struct sk_buff *skb)
1253 void dn_dev_veri_pkt(struct sk_buff *skb)
1258 void dn_dev_hello(struct sk_buff *skb)
1263 void dn_dev_devices_off(void)
1265 struct net_device *dev;
1268 for(dev = dev_base; dev; dev = dev->next)
1274 void dn_dev_devices_on(void)
1276 struct net_device *dev;
1279 for(dev = dev_base; dev; dev = dev->next) {
1280 if (dev->flags & IFF_UP)
1286 int register_dnaddr_notifier(struct notifier_block *nb)
1288 return blocking_notifier_chain_register(&dnaddr_chain, nb);
1291 int unregister_dnaddr_notifier(struct notifier_block *nb)
1293 return blocking_notifier_chain_unregister(&dnaddr_chain, nb);
1296 #ifdef CONFIG_PROC_FS
1297 static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1301 } while(dev && !dev->dn_ptr);
1306 static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1308 struct net_device *dev;
1311 if (dev && !dev->dn_ptr)
1312 dev = dn_dev_get_next(seq, dev);
1314 while(dev && (dev = dn_dev_get_next(seq, dev)))
1320 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1323 struct net_device *dev;
1324 read_lock(&dev_base_lock);
1325 dev = dn_dev_get_idx(seq, *pos - 1);
1327 read_unlock(&dev_base_lock);
1330 return SEQ_START_TOKEN;
1333 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1335 struct net_device *dev = v;
1338 if (v == SEQ_START_TOKEN) {
1339 dev = dn_dev_seq_start(seq, &one);
1341 dev = dn_dev_get_next(seq, dev);
1343 read_unlock(&dev_base_lock);
1349 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1351 if (v && v != SEQ_START_TOKEN)
1352 read_unlock(&dev_base_lock);
1355 static char *dn_type2asc(char type)
1369 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1371 if (v == SEQ_START_TOKEN)
1372 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1374 struct net_device *dev = v;
1375 char peer_buf[DN_ASCBUF_LEN];
1376 char router_buf[DN_ASCBUF_LEN];
1377 struct dn_dev *dn_db = dev->dn_ptr;
1379 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
1380 " %04hu %03d %02x %-10s %-7s %-7s\n",
1381 dev->name ? dev->name : "???",
1382 dn_type2asc(dn_db->parms.mode),
1384 dn_db->t3, dn_db->parms.t3,
1386 dn_db->parms.priority,
1387 dn_db->parms.state, dn_db->parms.name,
1388 dn_db->router ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->router->primary_key), router_buf) : "",
1389 dn_db->peer ? dn_addr2asc(dn_ntohs(*(__le16 *)dn_db->peer->primary_key), peer_buf) : "");
1394 static struct seq_operations dn_dev_seq_ops = {
1395 .start = dn_dev_seq_start,
1396 .next = dn_dev_seq_next,
1397 .stop = dn_dev_seq_stop,
1398 .show = dn_dev_seq_show,
1401 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1403 return seq_open(file, &dn_dev_seq_ops);
1406 static struct file_operations dn_dev_seq_fops = {
1407 .owner = THIS_MODULE,
1408 .open = dn_dev_seq_open,
1410 .llseek = seq_lseek,
1411 .release = seq_release,
1414 #endif /* CONFIG_PROC_FS */
1416 static struct rtnetlink_link dnet_rtnetlink_table[RTM_NR_MSGTYPES] =
1418 [RTM_NEWADDR - RTM_BASE] = { .doit = dn_dev_rtm_newaddr, },
1419 [RTM_DELADDR - RTM_BASE] = { .doit = dn_dev_rtm_deladdr, },
1420 [RTM_GETADDR - RTM_BASE] = { .dumpit = dn_dev_dump_ifaddr, },
1421 #ifdef CONFIG_DECNET_ROUTER
1422 [RTM_NEWROUTE - RTM_BASE] = { .doit = dn_fib_rtm_newroute, },
1423 [RTM_DELROUTE - RTM_BASE] = { .doit = dn_fib_rtm_delroute, },
1424 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
1425 .dumpit = dn_fib_dump, },
1426 [RTM_NEWRULE - RTM_BASE] = { .doit = dn_fib_rtm_newrule, },
1427 [RTM_DELRULE - RTM_BASE] = { .doit = dn_fib_rtm_delrule, },
1428 [RTM_GETRULE - RTM_BASE] = { .dumpit = dn_fib_dump_rules, },
1430 [RTM_GETROUTE - RTM_BASE] = { .doit = dn_cache_getroute,
1431 .dumpit = dn_cache_dump, },
1436 static int __initdata addr[2];
1437 module_param_array(addr, int, NULL, 0444);
1438 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1440 void __init dn_dev_init(void)
1442 if (addr[0] > 63 || addr[0] < 0) {
1443 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1447 if (addr[1] > 1023 || addr[1] < 0) {
1448 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1452 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1454 dn_dev_devices_on();
1456 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1458 proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1460 #ifdef CONFIG_SYSCTL
1463 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1464 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1466 #endif /* CONFIG_SYSCTL */
1469 void __exit dn_dev_cleanup(void)
1471 rtnetlink_links[PF_DECnet] = NULL;
1473 #ifdef CONFIG_SYSCTL
1476 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1477 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1479 #endif /* CONFIG_SYSCTL */
1481 proc_net_remove("decnet_dev");
1483 dn_dev_devices_off();