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/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_arp.h>
38 #include <linux/if_ether.h>
39 #include <linux/skbuff.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/sysctl.h>
42 #include <linux/notifier.h>
43 #include <asm/uaccess.h>
44 #include <net/neighbour.h>
48 #include <net/dn_dev.h>
49 #include <net/dn_route.h>
50 #include <net/dn_neigh.h>
51 #include <net/dn_fib.h>
53 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
55 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
56 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
57 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
58 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
60 extern struct neigh_table dn_neigh_table;
63 * decnet_address is kept in network order.
65 dn_address decnet_address = 0;
67 static rwlock_t dndev_lock = RW_LOCK_UNLOCKED;
68 static struct net_device *decnet_default_device;
69 static struct notifier_block *dnaddr_chain;
71 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
72 static void dn_dev_delete(struct net_device *dev);
73 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa);
75 static int dn_eth_up(struct net_device *);
76 static void dn_eth_down(struct net_device *);
77 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa);
78 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa);
80 static struct dn_dev_parms dn_dev_list[] = {
82 .type = ARPHRD_ETHER, /* Ethernet */
88 .ctl_name = NET_DECNET_CONF_ETHER,
91 .timer3 = dn_send_brd_hello,
94 .type = ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
100 .ctl_name = NET_DECNET_CONF_GRE,
101 .timer3 = dn_send_brd_hello,
105 .type = ARPHRD_X25, /* Bog standard X.25 */
106 .mode = DN_DEV_UCAST,
107 .state = DN_DEV_S_DS,
111 .ctl_name = NET_DECNET_CONF_X25,
112 .timer3 = dn_send_ptp_hello,
117 .type = ARPHRD_PPP, /* DECnet over PPP */
118 .mode = DN_DEV_BCAST,
119 .state = DN_DEV_S_RU,
123 .ctl_name = NET_DECNET_CONF_PPP,
124 .timer3 = dn_send_brd_hello,
128 .type = ARPHRD_DDCMP, /* DECnet over DDCMP */
129 .mode = DN_DEV_UCAST,
130 .state = DN_DEV_S_DS,
134 .ctl_name = NET_DECNET_CONF_DDCMP,
135 .timer3 = dn_send_ptp_hello,
138 .type = ARPHRD_LOOPBACK, /* Loopback interface - always last */
139 .mode = DN_DEV_BCAST,
140 .state = DN_DEV_S_RU,
144 .ctl_name = NET_DECNET_CONF_LOOPBACK,
145 .timer3 = dn_send_brd_hello,
149 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
151 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
155 static int min_t2[] = { 1 };
156 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
157 static int min_t3[] = { 1 };
158 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
160 static int min_priority[1];
161 static int max_priority[] = { 127 }; /* From DECnet spec */
163 static int dn_forwarding_proc(ctl_table *, int, struct file *,
165 static int dn_forwarding_sysctl(ctl_table *table, int *name, int nlen,
166 void *oldval, size_t *oldlenp,
167 void *newval, size_t newlen,
170 static struct dn_dev_sysctl_table {
171 struct ctl_table_header *sysctl_header;
172 ctl_table dn_dev_vars[5];
173 ctl_table dn_dev_dev[2];
174 ctl_table dn_dev_conf_dir[2];
175 ctl_table dn_dev_proto_dir[2];
176 ctl_table dn_dev_root_dir[2];
181 .ctl_name = NET_DECNET_CONF_DEV_FORWARDING,
182 .procname = "forwarding",
183 .data = (void *)DN_DEV_PARMS_OFFSET(forwarding),
184 .maxlen = sizeof(int),
186 .proc_handler = dn_forwarding_proc,
187 .strategy = dn_forwarding_sysctl,
190 .ctl_name = NET_DECNET_CONF_DEV_PRIORITY,
191 .procname = "priority",
192 .data = (void *)DN_DEV_PARMS_OFFSET(priority),
193 .maxlen = sizeof(int),
195 .proc_handler = proc_dointvec_minmax,
196 .strategy = sysctl_intvec,
197 .extra1 = &min_priority,
198 .extra2 = &max_priority
201 .ctl_name = NET_DECNET_CONF_DEV_T2,
203 .data = (void *)DN_DEV_PARMS_OFFSET(t2),
204 .maxlen = sizeof(int),
206 .proc_handler = proc_dointvec_minmax,
207 .strategy = sysctl_intvec,
212 .ctl_name = NET_DECNET_CONF_DEV_T3,
214 .data = (void *)DN_DEV_PARMS_OFFSET(t3),
215 .maxlen = sizeof(int),
217 .proc_handler = proc_dointvec_minmax,
218 .strategy = sysctl_intvec,
228 .child = dn_dev_sysctl.dn_dev_vars
231 .ctl_name = NET_DECNET_CONF,
234 .child = dn_dev_sysctl.dn_dev_dev
237 .ctl_name = NET_DECNET,
238 .procname = "decnet",
240 .child = dn_dev_sysctl.dn_dev_conf_dir
246 .child = dn_dev_sysctl.dn_dev_proto_dir
250 static inline __u16 mtu2blksize(struct net_device *dev)
252 u32 blksize = dev->mtu;
253 if (blksize > 0xffff)
256 if (dev->type == ARPHRD_ETHER ||
257 dev->type == ARPHRD_PPP ||
258 dev->type == ARPHRD_IPGRE ||
259 dev->type == ARPHRD_LOOPBACK)
262 return (__u16)blksize;
265 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
267 struct dn_dev_sysctl_table *t;
270 t = kmalloc(sizeof(*t), GFP_KERNEL);
274 memcpy(t, &dn_dev_sysctl, sizeof(*t));
276 for(i = 0; i < ARRAY_SIZE(t->dn_dev_vars) - 1; i++) {
277 long offset = (long)t->dn_dev_vars[i].data;
278 t->dn_dev_vars[i].data = ((char *)parms) + offset;
279 t->dn_dev_vars[i].de = NULL;
283 t->dn_dev_dev[0].procname = dev->name;
284 t->dn_dev_dev[0].ctl_name = dev->ifindex;
286 t->dn_dev_dev[0].procname = parms->name;
287 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
290 t->dn_dev_dev[0].child = t->dn_dev_vars;
291 t->dn_dev_dev[0].de = NULL;
292 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
293 t->dn_dev_conf_dir[0].de = NULL;
294 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
295 t->dn_dev_proto_dir[0].de = NULL;
296 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
297 t->dn_dev_root_dir[0].de = NULL;
298 t->dn_dev_vars[0].extra1 = (void *)dev;
300 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
301 if (t->sysctl_header == NULL)
307 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
310 struct dn_dev_sysctl_table *t = parms->sysctl;
311 parms->sysctl = NULL;
312 unregister_sysctl_table(t->sysctl_header);
317 struct net_device *dn_dev_get_default(void)
319 struct net_device *dev;
320 read_lock(&dndev_lock);
321 dev = decnet_default_device;
328 read_unlock(&dndev_lock);
332 int dn_dev_set_default(struct net_device *dev, int force)
334 struct net_device *old = NULL;
338 write_lock(&dndev_lock);
339 if (force || decnet_default_device == NULL) {
340 old = decnet_default_device;
341 decnet_default_device = dev;
344 write_unlock(&dndev_lock);
350 static void dn_dev_check_default(struct net_device *dev)
352 write_lock(&dndev_lock);
353 if (dev == decnet_default_device) {
354 decnet_default_device = NULL;
358 write_unlock(&dndev_lock);
363 static int dn_forwarding_proc(ctl_table *table, int write,
365 void *buffer, size_t *lenp)
367 #ifdef CONFIG_DECNET_ROUTER
368 struct net_device *dev = table->extra1;
369 struct dn_dev *dn_db;
373 if (table->extra1 == NULL)
377 old = dn_db->parms.forwarding;
379 err = proc_dointvec(table, write, filep, buffer, lenp);
381 if ((err >= 0) && write) {
382 if (dn_db->parms.forwarding < 0)
383 dn_db->parms.forwarding = 0;
384 if (dn_db->parms.forwarding > 2)
385 dn_db->parms.forwarding = 2;
387 * What an ugly hack this is... its works, just. It
388 * would be nice if sysctl/proc were just that little
389 * bit more flexible so I don't have to write a special
390 * routine, or suffer hacks like this - SJW
392 tmp = dn_db->parms.forwarding;
393 dn_db->parms.forwarding = old;
394 if (dn_db->parms.down)
395 dn_db->parms.down(dev);
396 dn_db->parms.forwarding = tmp;
398 dn_db->parms.up(dev);
407 static int dn_forwarding_sysctl(ctl_table *table, int *name, int nlen,
408 void *oldval, size_t *oldlenp,
409 void *newval, size_t newlen,
412 #ifdef CONFIG_DECNET_ROUTER
413 struct net_device *dev = table->extra1;
414 struct dn_dev *dn_db;
417 if (table->extra1 == NULL)
422 if (newval && newlen) {
423 if (newlen != sizeof(int))
426 if (get_user(value, (int *)newval))
433 if (dn_db->parms.down)
434 dn_db->parms.down(dev);
435 dn_db->parms.forwarding = value;
437 dn_db->parms.up(dev);
446 #else /* CONFIG_SYSCTL */
447 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
450 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
454 #endif /* CONFIG_SYSCTL */
456 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
458 struct dn_ifaddr *ifa;
460 ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
463 memset(ifa, 0, sizeof(*ifa));
469 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
474 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
476 struct dn_ifaddr *ifa1 = *ifap;
477 unsigned char mac_addr[6];
478 struct net_device *dev = dn_db->dev;
482 *ifap = ifa1->ifa_next;
484 if (dn_db->dev->type == ARPHRD_ETHER) {
485 if (ifa1->ifa_local != dn_htons(dn_eth2dn(dev->dev_addr))) {
486 dn_dn2eth(mac_addr, ifa1->ifa_local);
487 dev_mc_delete(dev, mac_addr, ETH_ALEN, 0);
491 rtmsg_ifa(RTM_DELADDR, ifa1);
492 notifier_call_chain(&dnaddr_chain, NETDEV_DOWN, ifa1);
494 dn_dev_free_ifa(ifa1);
496 if (dn_db->ifa_list == NULL)
497 dn_dev_delete(dn_db->dev);
501 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
503 struct net_device *dev = dn_db->dev;
504 struct dn_ifaddr *ifa1;
505 unsigned char mac_addr[6];
509 /* Check for duplicates */
510 for(ifa1 = dn_db->ifa_list; ifa1; ifa1 = ifa1->ifa_next) {
511 if (ifa1->ifa_local == ifa->ifa_local)
515 if (dev->type == ARPHRD_ETHER) {
516 if (ifa->ifa_local != dn_htons(dn_eth2dn(dev->dev_addr))) {
517 dn_dn2eth(mac_addr, ifa->ifa_local);
518 dev_mc_add(dev, mac_addr, ETH_ALEN, 0);
523 ifa->ifa_next = dn_db->ifa_list;
524 dn_db->ifa_list = ifa;
526 rtmsg_ifa(RTM_NEWADDR, ifa);
527 notifier_call_chain(&dnaddr_chain, NETDEV_UP, ifa);
532 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
534 struct dn_dev *dn_db = dev->dn_ptr;
539 dn_db = dn_dev_create(dev, &err);
544 ifa->ifa_dev = dn_db;
546 if (dev->flags & IFF_LOOPBACK)
547 ifa->ifa_scope = RT_SCOPE_HOST;
549 rv = dn_dev_insert_ifa(dn_db, ifa);
551 dn_dev_free_ifa(ifa);
556 int dn_dev_ioctl(unsigned int cmd, void *arg)
558 char buffer[DN_IFREQ_SIZE];
559 struct ifreq *ifr = (struct ifreq *)buffer;
560 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
561 struct dn_dev *dn_db;
562 struct net_device *dev;
563 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
566 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
568 ifr->ifr_name[IFNAMSIZ-1] = 0;
571 dev_load(ifr->ifr_name);
578 if (!capable(CAP_NET_ADMIN))
580 if (sdn->sdn_family != AF_DECnet)
589 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
594 if ((dn_db = dev->dn_ptr) != NULL) {
595 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
596 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
600 if (ifa == NULL && cmd != SIOCSIFADDR) {
601 ret = -EADDRNOTAVAIL;
607 *((dn_address *)sdn->sdn_nodeaddr) = ifa->ifa_local;
612 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
616 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
618 if (ifa->ifa_local == dn_saddr2dn(sdn))
620 dn_dev_del_ifa(dn_db, ifap, 0);
623 ifa->ifa_local = ifa->ifa_address = dn_saddr2dn(sdn);
625 ret = dn_dev_set_ifa(dev, ifa);
632 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
637 static struct dn_dev *dn_dev_by_index(int ifindex)
639 struct net_device *dev;
640 struct dn_dev *dn_dev = NULL;
641 dev = dev_get_by_index(ifindex);
643 dn_dev = dev->dn_ptr;
650 static int dn_dev_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
652 struct rtattr **rta = arg;
653 struct dn_dev *dn_db;
654 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
655 struct dn_ifaddr *ifa, **ifap;
657 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
658 return -EADDRNOTAVAIL;
660 for(ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) {
661 void *tmp = rta[IFA_LOCAL-1];
662 if ((tmp && memcmp(RTA_DATA(tmp), &ifa->ifa_local, 2)) ||
663 (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)))
666 dn_dev_del_ifa(dn_db, ifap, 1);
670 return -EADDRNOTAVAIL;
673 static int dn_dev_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
675 struct rtattr **rta = arg;
676 struct net_device *dev;
677 struct dn_dev *dn_db;
678 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
679 struct dn_ifaddr *ifa;
682 if (rta[IFA_LOCAL-1] == NULL)
685 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
688 if ((dn_db = dev->dn_ptr) == NULL) {
690 dn_db = dn_dev_create(dev, &err);
695 if ((ifa = dn_dev_alloc_ifa()) == NULL)
698 if (!rta[IFA_ADDRESS - 1])
699 rta[IFA_ADDRESS - 1] = rta[IFA_LOCAL - 1];
700 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 2);
701 memcpy(&ifa->ifa_address, RTA_DATA(rta[IFA_ADDRESS-1]), 2);
702 ifa->ifa_flags = ifm->ifa_flags;
703 ifa->ifa_scope = ifm->ifa_scope;
704 ifa->ifa_dev = dn_db;
705 if (rta[IFA_LABEL-1])
706 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
708 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
710 rv = dn_dev_insert_ifa(dn_db, ifa);
712 dn_dev_free_ifa(ifa);
716 static int dn_dev_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
717 u32 pid, u32 seq, int event)
719 struct ifaddrmsg *ifm;
720 struct nlmsghdr *nlh;
721 unsigned char *b = skb->tail;
723 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
724 ifm = NLMSG_DATA(nlh);
726 ifm->ifa_family = AF_DECnet;
727 ifm->ifa_prefixlen = 16;
728 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
729 ifm->ifa_scope = ifa->ifa_scope;
730 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
731 if (ifa->ifa_address)
732 RTA_PUT(skb, IFA_ADDRESS, 2, &ifa->ifa_address);
734 RTA_PUT(skb, IFA_LOCAL, 2, &ifa->ifa_local);
735 if (ifa->ifa_label[0])
736 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
737 nlh->nlmsg_len = skb->tail - b;
742 skb_trim(skb, b - skb->data);
746 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa)
749 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
751 skb = alloc_skb(size, GFP_KERNEL);
753 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, ENOBUFS);
756 if (dn_dev_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
758 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, EINVAL);
761 NETLINK_CB(skb).dst_groups = RTMGRP_DECnet_IFADDR;
762 netlink_broadcast(rtnl, skb, 0, RTMGRP_DECnet_IFADDR, GFP_KERNEL);
765 static int dn_dev_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
769 struct net_device *dev;
770 struct dn_dev *dn_db;
771 struct dn_ifaddr *ifa;
774 s_dn_idx = dn_idx = cb->args[1];
775 read_lock(&dev_base_lock);
776 for(dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
781 if ((dn_db = dev->dn_ptr) == NULL)
784 for(ifa = dn_db->ifa_list, dn_idx = 0; ifa; ifa = ifa->ifa_next, dn_idx++) {
785 if (dn_idx < s_dn_idx)
788 if (dn_dev_fill_ifaddr(skb, ifa,
789 NETLINK_CB(cb->skb).pid,
796 read_unlock(&dev_base_lock);
798 cb->args[1] = dn_idx;
803 static int dn_dev_get_first(struct net_device *dev, dn_address *addr)
805 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
806 struct dn_ifaddr *ifa;
810 ifa = dn_db->ifa_list;
812 *addr = ifa->ifa_local;
820 * Find a default address to bind to.
822 * This is one of those areas where the initial VMS concepts don't really
823 * map onto the Linux concepts, and since we introduced multiple addresses
824 * per interface we have to cope with slightly odd ways of finding out what
825 * "our address" really is. Mostly it's not a problem; for this we just guess
826 * a sensible default. Eventually the routing code will take care of all the
827 * nasties for us I hope.
829 int dn_dev_bind_default(dn_address *addr)
831 struct net_device *dev;
833 dev = dn_dev_get_default();
836 read_lock(&dev_base_lock);
837 rv = dn_dev_get_first(dev, addr);
838 read_unlock(&dev_base_lock);
840 if (rv == 0 || dev == &loopback_dev)
848 static void dn_send_endnode_hello(struct net_device *dev, struct dn_ifaddr *ifa)
850 struct endnode_hello_message *msg;
851 struct sk_buff *skb = NULL;
852 unsigned short int *pktlen;
853 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
855 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
860 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
863 memcpy(msg->tiver, dn_eco_version, 3);
864 dn_dn2eth(msg->id, ifa->ifa_local);
865 msg->iinfo = DN_RT_INFO_ENDN;
866 msg->blksize = dn_htons(mtu2blksize(dev));
868 memset(msg->seed, 0, 8);
869 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
872 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
873 dn_dn2eth(msg->neighbor, dn->addr);
876 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
879 memset(msg->data, 0xAA, 2);
881 pktlen = (unsigned short *)skb_push(skb,2);
882 *pktlen = dn_htons(skb->len - 2);
884 skb->nh.raw = skb->data;
886 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, msg->id);
890 #define DRDELAY (5 * HZ)
892 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db, struct dn_ifaddr *ifa)
894 /* First check time since device went up */
895 if ((jiffies - dn_db->uptime) < DRDELAY)
898 /* If there is no router, then yes... */
902 /* otherwise only if we have a higher priority or.. */
903 if (dn->priority < dn_db->parms.priority)
906 /* if we have equal priority and a higher node number */
907 if (dn->priority != dn_db->parms.priority)
910 if (dn_ntohs(dn->addr) < dn_ntohs(ifa->ifa_local))
916 static void dn_send_router_hello(struct net_device *dev, struct dn_ifaddr *ifa)
919 struct dn_dev *dn_db = dev->dn_ptr;
920 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
924 unsigned char *i1, *i2;
925 unsigned short *pktlen;
928 if (mtu2blksize(dev) < (26 + 7))
931 n = mtu2blksize(dev) - 26;
937 size = 2 + 26 + 7 * n;
939 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
943 ptr = skb_put(skb, size);
945 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
946 *ptr++ = 2; /* ECO */
949 dn_dn2eth(ptr, ifa->ifa_local);
952 *ptr++ = dn_db->parms.forwarding == 1 ?
953 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
954 *((unsigned short *)ptr) = dn_htons(mtu2blksize(dev));
956 *ptr++ = dn_db->parms.priority; /* Priority */
957 *ptr++ = 0; /* Area: Reserved */
958 *((unsigned short *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
960 *ptr++ = 0; /* MPD: Reserved */
962 memset(ptr, 0, 7); /* Name: Reserved */
966 n = dn_neigh_elist(dev, ptr, n);
971 skb_trim(skb, (27 + *i2));
973 pktlen = (unsigned short *)skb_push(skb, 2);
974 *pktlen = dn_htons(skb->len - 2);
976 skb->nh.raw = skb->data;
978 if (dn_am_i_a_router(dn, dn_db, ifa)) {
979 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
981 dn_rt_finish_output(skb2, dn_rt_all_end_mcast, src);
985 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
988 static void dn_send_brd_hello(struct net_device *dev, struct dn_ifaddr *ifa)
990 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
992 if (dn_db->parms.forwarding == 0)
993 dn_send_endnode_hello(dev, ifa);
995 dn_send_router_hello(dev, ifa);
998 static void dn_send_ptp_hello(struct net_device *dev, struct dn_ifaddr *ifa)
1001 int size = dev->hard_header_len + 2 + 4 + tdlen;
1002 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
1011 skb_push(skb, dev->hard_header_len);
1012 ptr = skb_put(skb, 2 + 4 + tdlen);
1014 *ptr++ = DN_RT_PKT_HELO;
1015 *((dn_address *)ptr) = ifa->ifa_local;
1019 for(i = 0; i < tdlen; i++)
1022 dn_dn2eth(src, ifa->ifa_local);
1023 dn_rt_finish_output(skb, dn_rt_all_rt_mcast, src);
1026 static int dn_eth_up(struct net_device *dev)
1028 struct dn_dev *dn_db = dev->dn_ptr;
1030 if (dn_db->parms.forwarding == 0)
1031 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1033 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1037 dn_db->use_long = 1;
1042 static void dn_eth_down(struct net_device *dev)
1044 struct dn_dev *dn_db = dev->dn_ptr;
1046 if (dn_db->parms.forwarding == 0)
1047 dev_mc_delete(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
1049 dev_mc_delete(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
1052 static void dn_dev_set_timer(struct net_device *dev);
1054 static void dn_dev_timer_func(unsigned long arg)
1056 struct net_device *dev = (struct net_device *)arg;
1057 struct dn_dev *dn_db = dev->dn_ptr;
1058 struct dn_ifaddr *ifa;
1060 if (dn_db->t3 <= dn_db->parms.t2) {
1061 if (dn_db->parms.timer3) {
1062 for(ifa = dn_db->ifa_list; ifa; ifa = ifa->ifa_next) {
1063 if (!(ifa->ifa_flags & IFA_F_SECONDARY))
1064 dn_db->parms.timer3(dev, ifa);
1067 dn_db->t3 = dn_db->parms.t3;
1069 dn_db->t3 -= dn_db->parms.t2;
1072 dn_dev_set_timer(dev);
1075 static void dn_dev_set_timer(struct net_device *dev)
1077 struct dn_dev *dn_db = dev->dn_ptr;
1079 if (dn_db->parms.t2 > dn_db->parms.t3)
1080 dn_db->parms.t2 = dn_db->parms.t3;
1082 dn_db->timer.data = (unsigned long)dev;
1083 dn_db->timer.function = dn_dev_timer_func;
1084 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
1086 add_timer(&dn_db->timer);
1089 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
1092 struct dn_dev_parms *p = dn_dev_list;
1093 struct dn_dev *dn_db;
1095 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
1096 if (p->type == dev->type)
1101 if (i == DN_DEV_LIST_SIZE)
1105 if ((dn_db = kmalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
1108 memset(dn_db, 0, sizeof(struct dn_dev));
1109 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
1110 dev->dn_ptr = dn_db;
1112 init_timer(&dn_db->timer);
1114 dn_db->uptime = jiffies;
1115 if (dn_db->parms.up) {
1116 if (dn_db->parms.up(dev) < 0) {
1123 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
1125 dn_dev_sysctl_register(dev, &dn_db->parms);
1127 dn_dev_set_timer(dev);
1135 * This processes a device up event. We only start up
1136 * the loopback device & ethernet devices with correct
1137 * MAC addreses automatically. Others must be started
1140 * FIXME: How should we configure the loopback address ? If we could dispense
1141 * with using decnet_address here and for autobind, it will be one less thing
1142 * for users to worry about setting up.
1145 void dn_dev_up(struct net_device *dev)
1147 struct dn_ifaddr *ifa;
1148 dn_address addr = decnet_address;
1149 int maybe_default = 0;
1150 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1152 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1156 * Need to ensure that loopback device has a dn_db attached to it
1157 * to allow creation of neighbours against it, even though it might
1158 * not have a local address of its own. Might as well do the same for
1159 * all autoconfigured interfaces.
1161 if (dn_db == NULL) {
1163 dn_db = dn_dev_create(dev, &err);
1168 if (dev->type == ARPHRD_ETHER) {
1169 if (memcmp(dev->dev_addr, dn_hiord, 4) != 0)
1171 addr = dn_htons(dn_eth2dn(dev->dev_addr));
1178 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1181 ifa->ifa_local = ifa->ifa_address = addr;
1183 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1184 strcpy(ifa->ifa_label, dev->name);
1186 dn_dev_set_ifa(dev, ifa);
1189 * Automagically set the default device to the first automatically
1190 * configured ethernet card in the system.
1192 if (maybe_default) {
1194 if (dn_dev_set_default(dev, 0))
1199 static void dn_dev_delete(struct net_device *dev)
1201 struct dn_dev *dn_db = dev->dn_ptr;
1206 del_timer_sync(&dn_db->timer);
1207 dn_dev_sysctl_unregister(&dn_db->parms);
1208 dn_dev_check_default(dev);
1209 neigh_ifdown(&dn_neigh_table, dev);
1211 if (dn_db->parms.down)
1212 dn_db->parms.down(dev);
1216 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1219 neigh_release(dn_db->router);
1221 neigh_release(dn_db->peer);
1226 void dn_dev_down(struct net_device *dev)
1228 struct dn_dev *dn_db = dev->dn_ptr;
1229 struct dn_ifaddr *ifa;
1234 while((ifa = dn_db->ifa_list) != NULL) {
1235 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1236 dn_dev_free_ifa(ifa);
1242 void dn_dev_init_pkt(struct sk_buff *skb)
1247 void dn_dev_veri_pkt(struct sk_buff *skb)
1252 void dn_dev_hello(struct sk_buff *skb)
1257 void dn_dev_devices_off(void)
1259 struct net_device *dev;
1262 for(dev = dev_base; dev; dev = dev->next)
1268 void dn_dev_devices_on(void)
1270 struct net_device *dev;
1273 for(dev = dev_base; dev; dev = dev->next) {
1274 if (dev->flags & IFF_UP)
1280 int register_dnaddr_notifier(struct notifier_block *nb)
1282 return notifier_chain_register(&dnaddr_chain, nb);
1285 int unregister_dnaddr_notifier(struct notifier_block *nb)
1287 return notifier_chain_unregister(&dnaddr_chain, nb);
1290 #ifdef CONFIG_DECNET_SIOCGIFCONF
1292 * Now we support multiple addresses per interface.
1293 * Since we don't want to break existing code, you have to enable
1294 * it as a compile time option. Probably you should use the
1295 * rtnetlink interface instead.
1297 int dnet_gifconf(struct net_device *dev, char __user *buf, int len)
1299 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1300 struct dn_ifaddr *ifa;
1301 char buffer[DN_IFREQ_SIZE];
1302 struct ifreq *ifr = (struct ifreq *)buffer;
1303 struct sockaddr_dn *addr = (struct sockaddr_dn *)&ifr->ifr_addr;
1306 if ((dn_db == NULL) || ((ifa = dn_db->ifa_list) == NULL))
1309 for(; ifa; ifa = ifa->ifa_next) {
1311 done += sizeof(DN_IFREQ_SIZE);
1314 if (len < DN_IFREQ_SIZE)
1316 memset(buffer, 0, DN_IFREQ_SIZE);
1319 strcpy(ifr->ifr_name, ifa->ifa_label);
1321 strcpy(ifr->ifr_name, dev->name);
1323 addr->sdn_family = AF_DECnet;
1324 addr->sdn_add.a_len = 2;
1325 memcpy(addr->sdn_add.a_addr, &ifa->ifa_local,
1326 sizeof(dn_address));
1328 if (copy_to_user(buf, buffer, DN_IFREQ_SIZE)) {
1333 buf += DN_IFREQ_SIZE;
1334 len -= DN_IFREQ_SIZE;
1335 done += DN_IFREQ_SIZE;
1340 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1343 #ifdef CONFIG_PROC_FS
1344 static inline struct net_device *dn_dev_get_next(struct seq_file *seq, struct net_device *dev)
1348 } while(dev && !dev->dn_ptr);
1353 static struct net_device *dn_dev_get_idx(struct seq_file *seq, loff_t pos)
1355 struct net_device *dev;
1358 if (dev && !dev->dn_ptr)
1359 dev = dn_dev_get_next(seq, dev);
1361 while(dev && (dev = dn_dev_get_next(seq, dev)))
1367 static void *dn_dev_seq_start(struct seq_file *seq, loff_t *pos)
1370 struct net_device *dev;
1371 read_lock(&dev_base_lock);
1372 dev = dn_dev_get_idx(seq, *pos - 1);
1374 read_unlock(&dev_base_lock);
1377 return SEQ_START_TOKEN;
1380 static void *dn_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1382 struct net_device *dev = v;
1385 if (v == SEQ_START_TOKEN) {
1386 dev = dn_dev_seq_start(seq, &one);
1388 dev = dn_dev_get_next(seq, dev);
1390 read_unlock(&dev_base_lock);
1396 static void dn_dev_seq_stop(struct seq_file *seq, void *v)
1398 if (v && v != SEQ_START_TOKEN)
1399 read_unlock(&dev_base_lock);
1402 static char *dn_type2asc(char type)
1416 static int dn_dev_seq_show(struct seq_file *seq, void *v)
1418 if (v == SEQ_START_TOKEN)
1419 seq_puts(seq, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1421 struct net_device *dev = v;
1422 char peer_buf[DN_ASCBUF_LEN];
1423 char router_buf[DN_ASCBUF_LEN];
1424 struct dn_dev *dn_db = dev->dn_ptr;
1426 seq_printf(seq, "%-8s %1s %04u %04u %04lu %04lu"
1427 " %04hu %03d %02x %-10s %-7s %-7s\n",
1428 dev->name ? dev->name : "???",
1429 dn_type2asc(dn_db->parms.mode),
1431 dn_db->t3, dn_db->parms.t3,
1433 dn_db->parms.priority,
1434 dn_db->parms.state, dn_db->parms.name,
1435 dn_db->router ? dn_addr2asc(dn_ntohs(*(dn_address *)dn_db->router->primary_key), router_buf) : "",
1436 dn_db->peer ? dn_addr2asc(dn_ntohs(*(dn_address *)dn_db->peer->primary_key), peer_buf) : "");
1441 static struct seq_operations dn_dev_seq_ops = {
1442 .start = dn_dev_seq_start,
1443 .next = dn_dev_seq_next,
1444 .stop = dn_dev_seq_stop,
1445 .show = dn_dev_seq_show,
1448 static int dn_dev_seq_open(struct inode *inode, struct file *file)
1450 return seq_open(file, &dn_dev_seq_ops);
1453 static struct file_operations dn_dev_seq_fops = {
1454 .owner = THIS_MODULE,
1455 .open = dn_dev_seq_open,
1457 .llseek = seq_lseek,
1458 .release = seq_release,
1461 #endif /* CONFIG_PROC_FS */
1463 static struct rtnetlink_link dnet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
1465 [4] = { .doit = dn_dev_rtm_newaddr, },
1466 [5] = { .doit = dn_dev_rtm_deladdr, },
1467 [6] = { .dumpit = dn_dev_dump_ifaddr, },
1469 #ifdef CONFIG_DECNET_ROUTER
1470 [8] = { .doit = dn_fib_rtm_newroute, },
1471 [9] = { .doit = dn_fib_rtm_delroute, },
1472 [10] = { .doit = dn_cache_getroute, .dumpit = dn_fib_dump, },
1473 [16] = { .doit = dn_fib_rtm_newrule, },
1474 [17] = { .doit = dn_fib_rtm_delrule, },
1475 [18] = { .dumpit = dn_fib_dump_rules, },
1477 [10] = { .doit = dn_cache_getroute, .dumpit = dn_cache_dump, },
1482 static int __initdata addr[2];
1483 static int __initdata num;
1484 module_param_array(addr, int, num, 0444);
1485 MODULE_PARM_DESC(addr, "The DECnet address of this machine: area,node");
1487 void __init dn_dev_init(void)
1489 if (addr[0] > 63 || addr[0] < 0) {
1490 printk(KERN_ERR "DECnet: Area must be between 0 and 63");
1494 if (addr[1] > 1023 || addr[1] < 0) {
1495 printk(KERN_ERR "DECnet: Node must be between 0 and 1023");
1499 decnet_address = dn_htons((addr[0] << 10) | addr[1]);
1501 dn_dev_devices_on();
1502 #ifdef CONFIG_DECNET_SIOCGIFCONF
1503 register_gifconf(PF_DECnet, dnet_gifconf);
1504 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1506 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1508 proc_net_fops_create("decnet_dev", S_IRUGO, &dn_dev_seq_fops);
1510 #ifdef CONFIG_SYSCTL
1513 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1514 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1516 #endif /* CONFIG_SYSCTL */
1519 void __exit dn_dev_cleanup(void)
1521 rtnetlink_links[PF_DECnet] = NULL;
1523 #ifdef CONFIG_DECNET_SIOCGIFCONF
1524 unregister_gifconf(PF_DECnet);
1525 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1527 #ifdef CONFIG_SYSCTL
1530 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1531 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1533 #endif /* CONFIG_SYSCTL */
1535 proc_net_remove("decnet_dev");
1537 dn_dev_devices_off();