Import 2.3.25pre1
[davej-history.git] / net / decnet / dn_dev.c
blob435f7859d2578856583ee68e46c6700acdfab166
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
22 #include <linux/config.h>
23 #include <linux/net.h>
24 #include <linux/netdevice.h>
25 #include <linux/proc_fs.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/if_arp.h>
29 #include <linux/if_ether.h>
30 #include <linux/init.h>
31 #include <linux/skbuff.h>
32 #include <linux/rtnetlink.h>
33 #include <linux/sysctl.h>
34 #include <asm/uaccess.h>
35 #include <net/neighbour.h>
36 #include <net/dst.h>
37 #include <net/dn.h>
38 #include <net/dn_dev.h>
39 #include <net/dn_route.h>
40 #include <net/dn_neigh.h>
41 #include <net/dn_fib.h>
43 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
45 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
46 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
47 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
48 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
50 extern struct neigh_table dn_neigh_table;
52 struct net_device *decnet_default_device = NULL;
54 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
55 static void dn_dev_delete(struct net_device *dev);
56 #ifdef CONFIG_RTNETLINK
57 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa);
58 #endif
60 static int dn_eth_up(struct net_device *);
61 static void dn_send_brd_hello(struct net_device *dev);
62 static void dn_send_ptp_hello(struct net_device *dev);
64 static struct dn_dev_parms dn_dev_list[] = {
66 ARPHRD_ETHER, /* Ethernet */
67 DN_DEV_BCAST,
68 DN_DEV_S_RU,
70 1498,
71 10,
73 10,
75 "ethernet",
76 NET_DECNET_CONF_ETHER,
77 dn_eth_up,
78 NULL,
79 NULL,
80 dn_send_brd_hello,
81 NULL
84 ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
85 DN_DEV_BCAST,
86 DN_DEV_S_RU,
88 1400,
89 10,
91 10,
93 "ipgre",
94 NET_DECNET_CONF_GRE,
95 NULL,
96 NULL,
97 NULL,
98 dn_send_brd_hello,
99 NULL
101 #if 0
103 ARPHRD_X25, /* Bog standard X.25 */
104 DN_DEV_UCAST,
105 DN_DEV_S_DS,
107 230,
108 10 * 60,
110 120,
112 "x25",
113 NET_DECNET_CONF_X25,
114 NULL,
115 NULL,
116 NULL,
117 dn_send_ptp_hello,
118 NULL
120 #endif
121 #if 0
123 ARPHRD_PPP, /* DECnet over PPP */
124 DN_DEV_BCAST,
125 DN_DEV_S_RU,
127 230,
132 "ppp",
133 NET_DECNET_CONF_PPP,
134 NULL,
135 NULL,
136 NULL,
137 dn_send_brd_hello,
138 NULL
140 #endif
141 #if 0
143 ARPHRD_DDCMP, /* DECnet over DDCMP */
144 DN_DEV_UCAST,
145 DN_DEV_S_DS,
147 230,
148 10 * 60,
150 120,
152 "ddcmp",
153 NET_DECNET_CONF_DDCMP,
154 NULL,
155 NULL,
156 NULL,
157 dn_send_ptp_hello,
158 NULL
160 #endif
162 ARPHRD_LOOPBACK, /* Loopback interface - always last */
163 DN_DEV_BCAST,
164 DN_DEV_S_RU,
166 1498,
171 "loopback",
172 NET_DECNET_CONF_LOOPBACK,
173 NULL,
174 NULL,
175 NULL,
176 dn_send_brd_hello,
177 NULL
181 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
183 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
185 #ifdef CONFIG_SYSCTL
187 static int min_t2[] = { 1 };
188 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
189 static int min_t3[] = { 1 };
190 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
191 #ifdef CONFIG_DECNET_ROUTER
192 static int min_t1[] = { 1 };
193 static int max_t1[] = { 8191 }; /* No max specified, so made it the same as t3 */
194 static int min_cost[] = { 0 };
195 static int max_cost[] = { 25 }; /* From DECnet spec */
196 static int min_priority[] = { 0 };
197 static int max_priority[] = { 127 }; /* From DECnet spec */
198 #endif /* CONFIG_DECNET_ROUTER */
200 static struct dn_dev_sysctl_table {
201 struct ctl_table_header *sysctl_header;
202 #ifdef CONFIG_DECNET_ROUTER
203 ctl_table dn_dev_vars[6];
204 #else
205 ctl_table dn_dev_vars[3];
206 #endif
207 ctl_table dn_dev_dev[2];
208 ctl_table dn_dev_conf_dir[2];
209 ctl_table dn_dev_proto_dir[2];
210 ctl_table dn_dev_root_dir[2];
211 } dn_dev_sysctl = {
212 NULL,
214 #ifdef CONFIG_DECNET_ROUTER
215 {NET_DECNET_CONF_DEV_COST, "cost", (void *)DN_DEV_PARMS_OFFSET(cost),
216 sizeof(int), 0644, NULL,
217 proc_dointvec_minmax, sysctl_intvec,
218 NULL, &min_cost, &max_cost},
219 {NET_DECNET_CONF_DEV_PRIORITY, "priority", (void *)DN_DEV_PARMS_OFFSET(priority),
220 sizeof(int), 0644, NULL,
221 proc_dointvec_minmax, sysctl_intvec,
222 NULL, &min_priority, &max_priority},
223 {NET_DECNET_CONF_DEV_T1, "t1", (void *)DN_DEV_PARMS_OFFSET(t1),
224 sizeof(int), 0644, NULL,
225 proc_dointvec_minmax, sysctl_intvec,
226 NULL, &min_t1, &max_t1},
227 #endif
228 {NET_DECNET_CONF_DEV_T2, "t2", (void *)DN_DEV_PARMS_OFFSET(t2),
229 sizeof(int), 0644, NULL,
230 proc_dointvec_minmax, sysctl_intvec,
231 NULL, &min_t2, &max_t2},
232 {NET_DECNET_CONF_DEV_T3, "t3", (void *)DN_DEV_PARMS_OFFSET(t3),
233 sizeof(int), 0644, NULL,
234 proc_dointvec_minmax, sysctl_intvec,
235 NULL, &min_t3, &max_t3},
238 {{0, "", NULL, 0, 0555, dn_dev_sysctl.dn_dev_vars}, {0}},
239 {{NET_DECNET_CONF, "conf", NULL, 0, 0555, dn_dev_sysctl.dn_dev_dev}, {0}},
240 {{NET_DECNET, "decnet", NULL, 0, 0555, dn_dev_sysctl.dn_dev_conf_dir}, {0}},
241 {{CTL_NET, "net", NULL, 0, 0555, dn_dev_sysctl.dn_dev_proto_dir}, {0}}
244 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
246 struct dn_dev_sysctl_table *t;
247 int i;
249 t = kmalloc(sizeof(*t), GFP_KERNEL);
250 if (t == NULL)
251 return;
253 memcpy(t, &dn_dev_sysctl, sizeof(*t));
255 for(i = 0; i < (sizeof(t->dn_dev_vars)/sizeof(t->dn_dev_vars[0]) - 1); i++) {
256 long offset = (long)t->dn_dev_vars[i].data;
257 t->dn_dev_vars[i].data = ((char *)parms) + offset;
258 t->dn_dev_vars[i].de = NULL;
261 if (dev) {
262 t->dn_dev_dev[0].procname = dev->name;
263 t->dn_dev_dev[0].ctl_name = dev->ifindex;
264 } else {
265 t->dn_dev_dev[0].procname = parms->name;
266 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
269 t->dn_dev_dev[0].child = t->dn_dev_vars;
270 t->dn_dev_dev[0].de = NULL;
271 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
272 t->dn_dev_conf_dir[0].de = NULL;
273 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
274 t->dn_dev_proto_dir[0].de = NULL;
275 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
276 t->dn_dev_root_dir[0].de = NULL;
278 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
279 if (t->sysctl_header == NULL)
280 kfree(t);
281 else
282 parms->sysctl = t;
285 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
287 if (parms->sysctl) {
288 struct dn_dev_sysctl_table *t = parms->sysctl;
289 parms->sysctl = NULL;
290 unregister_sysctl_table(t->sysctl_header);
291 kfree(t);
294 #endif
296 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
298 struct dn_ifaddr *ifa;
300 ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
302 if (ifa) {
303 memset(ifa, 0, sizeof(*ifa));
306 return ifa;
309 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
311 kfree_s(ifa, sizeof(*ifa));
314 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
316 struct dn_ifaddr *ifa1 = *ifap;
318 *ifap = ifa1->ifa_next;
320 #ifdef CONFIG_RTNETLINK
321 rtmsg_ifa(RTM_DELADDR, ifa1);
322 #endif /* CONFIG_RTNETLINK */
324 if (destroy) {
325 dn_dev_free_ifa(ifa1);
327 if (dn_db->ifa_list == NULL)
328 dn_dev_delete(dn_db->dev);
332 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
335 * FIXME: Duplicate check here.
338 ifa->ifa_next = dn_db->ifa_list;
339 dn_db->ifa_list = ifa;
341 #ifdef CONFIG_RTNETLINK
342 rtmsg_ifa(RTM_NEWADDR, ifa);
343 #endif /* CONFIG_RTNETLINK */
345 return 0;
348 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
350 struct dn_dev *dn_db = dev->dn_ptr;
352 if (dn_db == NULL) {
353 int err;
354 dn_db = dn_dev_create(dev, &err);
355 if (dn_db == NULL)
356 return err;
359 ifa->ifa_dev = dn_db;
361 if (dev->flags & IFF_LOOPBACK)
362 ifa->ifa_scope = RT_SCOPE_HOST;
364 return dn_dev_insert_ifa(dn_db, ifa);
367 static struct dn_dev *dn_dev_by_index(int ifindex)
369 struct net_device *dev;
370 struct dn_dev *dn_dev = NULL;
371 dev = dev_get_by_index(ifindex);
372 if (dev) {
373 dn_dev = dev->dn_ptr;
374 dev_put(dev);
377 return dn_dev;
381 int dn_dev_ioctl(unsigned int cmd, void *arg)
383 char buffer[DN_IFREQ_SIZE];
384 struct ifreq *ifr = (struct ifreq *)buffer;
385 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
386 struct dn_dev *dn_db;
387 struct net_device *dev;
388 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
389 int exclusive = 0;
390 int ret = 0;
392 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
393 return -EFAULT;
394 ifr->ifr_name[IFNAMSIZ-1] = 0;
396 #ifdef CONFIG_KMOD
397 dev_load(ifr->ifr_name);
398 #endif
400 switch(cmd) {
401 case SIOCGIFADDR:
402 break;
403 case SIOCSIFADDR:
404 if (!capable(CAP_NET_ADMIN))
405 return -EACCES;
406 if (sdn->sdn_family != AF_DECnet)
407 return -EINVAL;
408 rtnl_lock();
409 exclusive = 1;
410 break;
411 default:
412 return -EINVAL;
415 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
416 ret = -ENODEV;
417 goto done;
420 if ((dn_db = dev->dn_ptr) != NULL) {
421 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
422 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
423 break;
426 if (ifa == NULL && cmd != SIOCSIFADDR) {
427 ret = -EADDRNOTAVAIL;
428 goto done;
431 switch(cmd) {
432 case SIOCGIFADDR:
433 *((dn_address *)sdn->sdn_nodeaddr) = ifa->ifa_local;
434 goto rarok;
436 case SIOCSIFADDR:
437 if (!ifa) {
438 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
439 ret = -ENOBUFS;
440 break;
442 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
443 } else {
444 if (ifa->ifa_local == dn_saddr2dn(sdn))
445 break;
446 dn_dev_del_ifa(dn_db, ifap, 0);
449 ifa->ifa_local = dn_saddr2dn(sdn);
451 ret = dn_dev_set_ifa(dev, ifa);
453 done:
454 if (exclusive)
455 rtnl_unlock();
457 return ret;
458 rarok:
459 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
460 return -EFAULT;
462 return 0;
465 #ifdef CONFIG_RTNETLINK
467 static int dn_dev_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
469 struct rtattr **rta = arg;
470 struct dn_dev *dn_db;
471 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
472 struct dn_ifaddr *ifa, **ifap;
474 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
475 return -EADDRNOTAVAIL;
477 for(ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) {
478 void *tmp = rta[IFA_LOCAL-1];
479 if ((tmp && memcmp(RTA_DATA(tmp), &ifa->ifa_local, 2)) ||
480 (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)))
481 continue;
483 dn_dev_del_ifa(dn_db, ifap, 1);
484 return 0;
487 return -EADDRNOTAVAIL;
490 static int dn_dev_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
492 struct rtattr **rta = arg;
493 struct net_device *dev;
494 struct dn_dev *dn_db;
495 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
496 struct dn_ifaddr *ifa;
498 if (rta[IFA_LOCAL-1] == NULL)
499 return -EINVAL;
501 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
502 return -ENODEV;
504 if ((dn_db = dev->dn_ptr) == NULL) {
505 int err;
506 dn_db = dn_dev_create(dev, &err);
507 if (!dn_db)
508 return err;
511 if ((ifa = dn_dev_alloc_ifa()) == NULL)
512 return -ENOBUFS;
514 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 2);
515 ifa->ifa_flags = ifm->ifa_flags;
516 ifa->ifa_scope = ifm->ifa_scope;
517 ifa->ifa_dev = dn_db;
518 if (rta[IFA_LABEL-1])
519 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
520 else
521 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
523 return dn_dev_insert_ifa(dn_db, ifa);
526 static int dn_dev_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
527 u32 pid, u32 seq, int event)
529 struct ifaddrmsg *ifm;
530 struct nlmsghdr *nlh;
531 unsigned char *b = skb->tail;
533 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
534 ifm = NLMSG_DATA(nlh);
536 ifm->ifa_family = AF_DECnet;
537 ifm->ifa_prefixlen = 16;
538 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
539 ifm->ifa_scope = ifa->ifa_scope;
540 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
541 RTA_PUT(skb, IFA_LOCAL, 2, &ifa->ifa_local);
542 if (ifa->ifa_label[0])
543 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
544 nlh->nlmsg_len = skb->tail - b;
545 return skb->len;
547 nlmsg_failure:
548 rtattr_failure:
549 skb_trim(skb, b - skb->data);
550 return -1;
553 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa)
555 struct sk_buff *skb;
556 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
558 skb = alloc_skb(size, GFP_KERNEL);
559 if (!skb) {
560 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, ENOBUFS);
561 return;
563 if (dn_dev_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
564 kfree_skb(skb);
565 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, EINVAL);
566 return;
568 NETLINK_CB(skb).dst_groups = RTMGRP_DECnet_IFADDR;
569 netlink_broadcast(rtnl, skb, 0, RTMGRP_DECnet_IFADDR, GFP_KERNEL);
572 static int dn_dev_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
573 int type, u32 pid, u32 seq)
575 struct ifinfomsg *r;
576 struct nlmsghdr *nlh;
577 unsigned char *b = skb->tail;
578 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
579 unsigned char priority = dn_db->parms.priority;
580 unsigned short cost = dn_db->parms.cost;
582 nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
583 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
584 r = NLMSG_DATA(nlh);
586 r->ifi_family = AF_DECnet;
587 r->ifi_type = dev->type;
588 r->ifi_index = dev->ifindex;
589 r->ifi_flags = dev->flags;
590 r->ifi_change = ~0U;
592 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
593 RTA_PUT(skb, IFLA_COST, sizeof(unsigned short), &cost);
594 RTA_PUT(skb, IFLA_PRIORITY, sizeof(unsigned char), &priority);
596 nlh->nlmsg_len = skb->tail - b;
597 return skb->len;
599 nlmsg_failure:
600 rtattr_failure:
601 skb_trim(skb, b - skb->data);
602 return -1;
605 static int dn_dev_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
607 int idx, dn_idx;
608 int s_idx, s_dn_idx;
609 struct net_device *dev;
610 struct dn_dev *dn_db;
611 struct dn_ifaddr *ifa;
613 s_idx = cb->args[0];
614 s_dn_idx = dn_idx = cb->args[1];
615 read_lock(&dev_base_lock);
616 for(dev = dev_base, idx = 0; dev; dev = dev->next) {
617 if ((dn_db = dev->dn_ptr) == NULL)
618 continue;
619 idx++;
620 if (idx < s_idx)
621 continue;
622 if (idx > s_idx)
623 s_dn_idx = 0;
624 if ((dn_db = dev->dn_ptr) == NULL)
625 continue;
627 for(ifa = dn_db->ifa_list, dn_idx = 0; ifa; ifa = ifa->ifa_next, dn_idx++) {
628 if (dn_idx < s_dn_idx)
629 continue;
631 if (dn_dev_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0)
632 goto done;
635 done:
636 read_unlock(&dev_base_lock);
637 cb->args[0] = idx;
638 cb->args[1] = dn_idx;
640 return skb->len;
643 static int dn_dev_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
645 int idx;
646 int s_idx = cb->args[0];
647 struct net_device *dev;
649 read_lock(&dev_base_lock);
650 for(dev=dev_base, idx=0; dev; dev = dev->next) {
651 if (!dev->dn_ptr)
652 continue;
653 idx++;
654 if (idx < s_idx)
655 continue;
656 if (dn_dev_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq) <= 0)
657 break;
659 read_unlock(&dev_base_lock);
660 cb->args[0] = idx;
662 return skb->len;
665 static void dn_dev_ifinfo(int type, struct net_device *dev)
667 struct sk_buff *skb;
668 int size = NLMSG_GOODSIZE;
670 skb = alloc_skb(size, GFP_KERNEL);
671 if (!skb)
672 return;
674 if (dn_dev_fill_ifinfo(skb, dev, type, 0, 0) < 0) {
675 kfree_skb(skb);
676 return;
679 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
680 netlink_broadcast(rtnl, skb, 0, NETLINK_CB(skb).dst_groups, GFP_KERNEL);
682 #endif /* CONFIG_RTNETLINK */
684 static void dn_send_endnode_hello(struct net_device *dev)
686 struct endnode_hello_message *msg;
687 struct sk_buff *skb = NULL;
688 unsigned short int *pktlen;
689 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
691 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
692 return;
694 skb->dev = dev;
696 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
698 msg->msgflg = 0x0D;
699 memcpy(msg->tiver, dn_eco_version, 3);
700 memcpy(msg->id, decnet_ether_address, 6);
701 msg->iinfo = DN_RT_INFO_ENDN;
702 msg->blksize = dn_htons(dn_db->parms.blksize);
703 msg->area = 0x00;
704 memset(msg->seed, 0, 8);
705 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
707 if (dn_db->router) {
708 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
709 dn_dn2eth(msg->neighbor, dn->addr);
712 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
713 msg->mpd = 0x00;
714 msg->datalen = 0x02;
715 memset(msg->data, 0xAA, 2);
717 pktlen = (unsigned short *)skb_push(skb,2);
718 *pktlen = dn_htons(skb->len - 2);
720 skb->nh.raw = skb->data;
722 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
726 #ifdef CONFIG_DECNET_ROUTER
728 #define DRDELAY (5 * HZ)
730 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db)
732 /* First check time since device went up */
733 if ((jiffies - dn_db->uptime) < DRDELAY)
734 return 0;
736 /* If there is no router, then yes... */
737 if (!dn_db->router)
738 return 1;
740 /* otherwise only if we have a higher priority or.. */
741 if (dn->priority < dn_db->parms.priority)
742 return 1;
744 /* if we have equal priority and a higher node number */
745 if (dn->priority != dn_db->parms.priority)
746 return 0;
748 if (dn_ntohs(dn->addr) < dn_ntohs(decnet_address))
749 return 1;
751 return 0;
754 static void dn_send_router_hello(struct net_device *dev)
756 int n;
757 struct dn_dev *dn_db = dev->dn_ptr;
758 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
759 struct sk_buff *skb;
760 size_t size;
761 unsigned char *ptr;
762 unsigned char *i1, *i2;
763 unsigned short *pktlen;
765 if (dn_db->parms.blksize < (26 + 7))
766 return;
768 n = dn_db->parms.blksize - 26;
769 n /= 7;
771 if (n > 32)
772 n = 32;
774 size = 2 + 26 + 7 * n;
776 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
777 return;
779 skb->dev = dev;
780 ptr = skb_put(skb, size);
782 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
783 *ptr++ = 2; /* ECO */
784 *ptr++ = 0;
785 *ptr++ = 0;
786 memcpy(ptr, decnet_ether_address, ETH_ALEN);
787 ptr += ETH_ALEN;
788 *ptr++ = (unsigned char)decnet_node_type;
789 *((unsigned short *)ptr) = dn_htons(dn_db->parms.blksize);
790 ptr += 2;
791 *ptr++ = 0; /* Priority */
792 *ptr++ = 0; /* Area: Reserved */
793 *((unsigned short *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
794 ptr += 2;
795 *ptr++ = 0; /* MPD: Reserved */
796 i1 = ptr++;
797 memset(ptr, 0, 7); /* Name: Reserved */
798 i2 = ptr++;
800 n = dn_neigh_elist(dev, ptr, n);
802 *i2 = 7 * n;
803 *i1 = 8 + *i2;
805 skb_trim(skb, (26 + *i2));
807 pktlen = (unsigned short *)skb_push(skb, 2);
808 *pktlen = dn_htons(skb->len - 2);
810 skb->nh.raw = skb->data;
812 if (dn_am_i_a_router(dn, dn_db)) {
813 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
814 if (skb2) {
815 dn_rt_finish_output(skb2, dn_rt_all_end_mcast);
819 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
822 static void dn_send_brd_hello(struct net_device *dev)
824 if (decnet_node_type == DN_RT_INFO_ENDN)
825 dn_send_endnode_hello(dev);
826 else
827 dn_send_router_hello(dev);
829 #else
830 static void dn_send_brd_hello(struct net_device *dev)
832 dn_send_endnode_hello(dev);
834 #endif
836 static void dn_send_ptp_hello(struct net_device *dev)
838 int tdlen = 16;
839 int size = dev->hard_header_len + 2 + 4 + tdlen;
840 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
841 /* struct dn_dev *dn_db = dev->dn_ptr; */
842 unsigned char *ptr;
843 int i;
845 if (skb == NULL)
846 return ;
848 skb->dev = dev;
849 skb_push(skb, dev->hard_header_len);
850 ptr = skb_put(skb, 2 + 4 + tdlen);
852 *ptr++ = DN_RT_PKT_HELO;
853 *((dn_address *)ptr) = decnet_address;
854 ptr += 2;
855 *ptr++ = tdlen;
857 for(i = 0; i < tdlen; i++)
858 *ptr++ = 0252;
860 #if 0
861 if (dn_db->router) {
862 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
863 if (memcmp(dn->addr, decnet_ether_address, ETH_ALEN) == 0) {
864 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
865 dn_rt_finish_output(skb2, dn_rt_all_end_mcast);
868 #endif
870 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
873 static int dn_eth_up(struct net_device *dev)
875 struct dn_dev *dn_db = dev->dn_ptr;
877 if (decnet_node_type == DN_RT_INFO_ENDN)
878 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
880 if (decnet_node_type == DN_RT_INFO_L1RT || decnet_node_type == DN_RT_INFO_L2RT)
881 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
883 dev_mc_upload(dev);
885 dn_db->use_long = 1;
887 return 0;
890 static void dn_dev_set_timer(struct net_device *dev);
892 static void dn_dev_timer_func(unsigned long arg)
894 struct net_device *dev = (struct net_device *)arg;
895 struct dn_dev *dn_db = dev->dn_ptr;
897 #ifdef CONFIG_DECNET_ROUTER
898 if (decnet_node_type == DN_RT_INFO_L1RT || decnet_node_type == DN_RT_INFO_L2RT) {
899 if (dn_db->t1 <= dn_db->parms.t2) {
900 if (dn_db->parms.timer1)
901 dn_db->parms.timer1(dev);
902 dn_db->t1 = dn_db->parms.t1;
903 } else {
904 dn_db->t1 -= dn_db->parms.t2;
907 #endif /* CONFIG_DECNET_ROUTER */
909 if (dn_db->t3 <= dn_db->parms.t2) {
910 if (dn_db->parms.timer3)
911 dn_db->parms.timer3(dev);
912 dn_db->t3 = dn_db->parms.t3;
913 } else {
914 dn_db->t3 -= dn_db->parms.t2;
917 dn_dev_set_timer(dev);
920 static void dn_dev_set_timer(struct net_device *dev)
922 struct dn_dev *dn_db = dev->dn_ptr;
924 #ifdef CONFIG_DECNET_ROUTER
925 if (dn_db->parms.t2 > dn_db->parms.t1)
926 dn_db->parms.t2 = dn_db->parms.t1;
927 #endif
929 if (dn_db->parms.t2 > dn_db->parms.t3)
930 dn_db->parms.t2 = dn_db->parms.t3;
932 dn_db->timer.data = (unsigned long)dev;
933 dn_db->timer.function = dn_dev_timer_func;
934 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
936 add_timer(&dn_db->timer);
939 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
941 int i;
942 struct dn_dev_parms *p = dn_dev_list;
943 struct dn_dev *dn_db;
945 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
946 if (p->type == dev->type)
947 break;
950 *err = -ENODEV;
951 if (i == DN_DEV_LIST_SIZE)
952 return NULL;
954 *err = -ENOBUFS;
955 if ((dn_db = kmalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
956 return NULL;
958 memset(dn_db, 0, sizeof(struct dn_dev));
959 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
960 dev->dn_ptr = dn_db;
961 dn_db->dev = dev;
962 init_timer(&dn_db->timer);
964 memcpy(dn_db->addr, decnet_ether_address, ETH_ALEN); /* To go... */
966 dn_db->uptime = jiffies;
967 if (dn_db->parms.up) {
968 if (dn_db->parms.up(dev) < 0) {
969 dev->dn_ptr = NULL;
970 kfree(dn_db);
971 return NULL;
975 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
976 /* dn_db->neigh_parms->neigh_setup = dn_db->parms.neigh_setup; */
978 dn_dev_sysctl_register(dev, &dn_db->parms);
980 dn_dev_set_timer(dev);
982 #ifdef CONFIG_RTNETLINK
983 dn_dev_ifinfo(RTM_NEWLINK, dev);
984 #endif
986 *err = 0;
987 return dn_db;
992 * This processes a device up event. We only start up
993 * the loopback device & ethernet devices with correct
994 * MAC addreses automatically. Others must be started
995 * specifically.
997 void dn_dev_up(struct net_device *dev)
999 struct dn_ifaddr *ifa;
1001 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
1002 return;
1004 if (dev->type == ARPHRD_ETHER)
1005 if (memcmp(dev->dev_addr, decnet_ether_address, ETH_ALEN) != 0)
1006 return;
1008 if ((ifa = dn_dev_alloc_ifa()) == NULL)
1009 return;
1011 ifa->ifa_local = decnet_address;
1012 ifa->ifa_flags = 0;
1013 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1014 strcpy(ifa->ifa_label, dev->name);
1016 dn_dev_set_ifa(dev, ifa);
1019 static void dn_dev_delete(struct net_device *dev)
1021 struct dn_dev *dn_db = dev->dn_ptr;
1023 if (dn_db == NULL)
1024 return;
1026 del_timer(&dn_db->timer);
1027 synchronize_bh();
1029 #ifdef CONFIG_RTNETLINK
1030 dn_dev_ifinfo(RTM_DELLINK, dev);
1031 #endif
1033 dn_dev_sysctl_unregister(&dn_db->parms);
1035 neigh_ifdown(&dn_neigh_table, dev);
1037 if (dev == decnet_default_device)
1038 decnet_default_device = NULL;
1040 if (dn_db->parms.down)
1041 dn_db->parms.down(dev);
1043 dev->dn_ptr = NULL;
1045 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1047 if (dn_db->router)
1048 neigh_release(dn_db->router);
1049 if (dn_db->peer)
1050 neigh_release(dn_db->peer);
1052 kfree(dn_db);
1055 void dn_dev_down(struct net_device *dev)
1057 struct dn_dev *dn_db = dev->dn_ptr;
1058 struct dn_ifaddr *ifa;
1060 if (dn_db == NULL)
1061 return;
1063 while((ifa = dn_db->ifa_list) != NULL) {
1064 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1065 dn_dev_free_ifa(ifa);
1068 dn_dev_delete(dev);
1071 void dn_dev_init_pkt(struct sk_buff *skb)
1073 return;
1076 void dn_dev_veri_pkt(struct sk_buff *skb)
1078 return;
1081 void dn_dev_hello(struct sk_buff *skb)
1083 return;
1086 void dn_dev_devices_off(void)
1088 struct net_device *dev;
1090 for(dev = dev_base; dev; dev = dev->next)
1091 dn_dev_down(dev);
1095 void dn_dev_devices_on(void)
1097 struct net_device *dev;
1099 for(dev = dev_base; dev; dev = dev->next) {
1100 if (dev->flags & IFF_UP)
1101 dn_dev_up(dev);
1106 #ifdef CONFIG_DECNET_SIOCGIFCONF
1108 * Now we support multiple addresses per interface.
1109 * Since we don't want to break existing code, you have to enable
1110 * it as a compile time option. Probably you should use the
1111 * rtnetlink interface instead.
1113 int dnet_gifconf(struct net_device *dev, char *buf, int len)
1115 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1116 struct dn_ifaddr *ifa;
1117 struct ifreq *ifr = (struct ifreq *)buf;
1118 int done = 0;
1120 if ((dn_db == NULL) || ((ifa = dn_db->ifa_list) == NULL))
1121 return 0;
1123 for(; ifa; ifa = ifa->ifa_next) {
1124 if (!ifr) {
1125 done += sizeof(DN_IFREQ_SIZE);
1126 continue;
1128 if (len < DN_IFREQ_SIZE)
1129 return done;
1130 memset(ifr, 0, DN_IFREQ_SIZE);
1132 if (ifa->ifa_label)
1133 strcpy(ifr->ifr_name, ifa->ifa_label);
1134 else
1135 strcpy(ifr->ifr_name, dev->name);
1137 (*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_family = AF_DECnet;
1138 (*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_add.a_len = 2;
1139 (*(dn_address *)(*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_add.a_addr) = ifa->ifa_local;
1141 ifr = (struct ifreq *)((char *)ifr + DN_IFREQ_SIZE);
1142 len -= DN_IFREQ_SIZE;
1143 done += DN_IFREQ_SIZE;
1146 return done;
1148 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1151 #ifdef CONFIG_PROC_FS
1153 static char *dn_type2asc(char type)
1155 switch(type) {
1156 case DN_DEV_BCAST:
1157 return "B";
1158 case DN_DEV_UCAST:
1159 return "U";
1160 case DN_DEV_MPOINT:
1161 return "M";
1164 return "?";
1167 static int decnet_dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
1169 struct dn_dev *dn_db;
1170 struct net_device *dev;
1171 int len = 0;
1172 off_t pos = 0;
1173 off_t begin = 0;
1174 char peer_buf[DN_ASCBUF_LEN];
1175 char router_buf[DN_ASCBUF_LEN];
1178 len += sprintf(buffer, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1180 read_lock(&dev_base_lock);
1181 for (dev = dev_base; dev; dev = dev->next) {
1182 if ((dn_db = (struct dn_dev *)dev->dn_ptr) == NULL)
1183 continue;
1185 len += sprintf(buffer + len, "%-8s %1s %04lu %04lu %04lu %04lu %04hu %03d %02x %-10s %-7s %-7s\n",
1186 dev->name ? dev->name : "???",
1187 dn_type2asc(dn_db->parms.mode),
1188 dn_db->t1, dn_db->parms.t1,
1189 dn_db->t3, dn_db->parms.t3,
1190 dn_db->parms.blksize,
1191 dn_db->parms.priority,
1192 dn_db->parms.state, dn_db->parms.name,
1193 dn_db->router ? dn_addr2asc(dn_eth2dn(dn_db->router->primary_key), router_buf) : "",
1194 dn_db->peer ? dn_addr2asc(dn_eth2dn(dn_db->peer->primary_key), peer_buf) : "");
1197 pos = begin + len;
1199 if (pos < offset) {
1200 len = 0;
1201 begin = pos;
1203 if (pos > offset + length)
1204 break;
1207 read_unlock(&dev_base_lock);
1209 *start = buffer + (offset - begin);
1210 len -= (offset - begin);
1212 if (len > length) len = length;
1214 return(len);
1217 #endif /* CONFIG_PROC_FS */
1219 #ifdef CONFIG_RTNETLINK
1220 static struct rtnetlink_link dnet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
1222 { NULL, NULL, },
1223 { NULL, NULL, },
1224 { NULL, dn_dev_dump_ifinfo, },
1225 { NULL, NULL, },
1227 { dn_dev_rtm_newaddr, NULL, },
1228 { dn_dev_rtm_deladdr, NULL, },
1229 { NULL, dn_dev_dump_ifaddr, },
1230 { NULL, NULL, },
1231 #ifdef CONFIG_DECNET_ROUTER
1232 { dn_fib_rtm_newroute, NULL, },
1233 { dn_fib_rtm_delroute, NULL, },
1234 { dn_cache_getroute, dn_fib_dump, },
1235 { NULL, NULL, },
1236 #else
1237 { NULL, NULL, },
1238 { NULL, NULL, },
1239 { dn_cache_getroute, dn_cache_dump, },
1240 { NULL, NULL, },
1241 #endif
1242 { NULL, NULL, },
1243 { NULL, NULL, },
1244 { NULL, NULL, },
1245 { NULL, NULL, },
1247 { NULL, NULL, },
1248 { NULL, NULL, },
1249 { NULL, NULL, },
1250 { NULL, NULL, }
1252 #endif /* CONFIG_RTNETLINK */
1254 void __init dn_dev_init(void)
1257 dn_dev_devices_on();
1258 #ifdef CONFIG_DECNET_SIOCGIFCONF
1259 register_gifconf(PF_DECnet, dnet_gifconf);
1260 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1262 #ifdef CONFIG_RTNETLINK
1263 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1264 #endif /* CONFIG_RTNETLINK */
1266 #ifdef CONFIG_PROC_FS
1267 proc_net_create("decnet_dev", 0, decnet_dev_get_info);
1268 #endif /* CONFIG_PROC_FS */
1270 #ifdef CONFIG_SYSCTL
1272 int i;
1273 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1274 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1276 #endif /* CONFIG_SYSCTL */
1279 #if defined(CONFIG_DECNET_MODULE)
1280 void dn_dev_cleanup(void)
1282 #ifdef CONFIG_RTNETLINK
1283 rtnetlink_links[PF_DECnet] = NULL;
1284 #endif /* CONFIG_RTNETLINK */
1286 #ifdef CONFIG_DECNET_SIOCGIFCONF
1287 unregister_gifconf(PF_DECnet);
1288 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1290 #ifdef CONFIG_SYSCTL
1292 int i;
1293 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1294 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1296 #endif /* CONFIG_SYSCTL */
1298 #ifdef CONFIG_PROC_FS
1299 proc_net_remove("decnet_dev");
1300 #endif /* CONFIG_PROC_FS */
1302 dn_dev_devices_off();
1304 #endif /* CONFIG_DECNET_MODULE */