Merge with Linux 2.3.40.
[linux-2.6/linux-mips.git] / net / decnet / dn_dev.c
blob80bf05b5f937eb270b1fc849087df243e740f9d7
1 /*
2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * DECnet Device Layer
8 * Authors: Steve Whitehouse <SteveW@ACM.org>
9 * Eduardo Marcelo Serrat <emserrat@geocities.com>
11 * Changes:
12 * Steve Whitehouse : Devices now see incoming frames so they
13 * can mark on who it came from.
14 * Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
15 * can now have a device specific setup func.
16 * Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
17 * Steve Whitehouse : Fixed bug which sometimes killed timer
18 * Steve Whitehouse : Multiple ifaddr support
19 * Steve Whitehouse : SIOCGIFCONF is now a compile time option
20 * Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
21 * Steve Whitehouse : Removed timer1 - its a user space issue now
24 #include <linux/config.h>
25 #include <linux/net.h>
26 #include <linux/netdevice.h>
27 #include <linux/proc_fs.h>
28 #include <linux/timer.h>
29 #include <linux/string.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/skbuff.h>
34 #include <linux/rtnetlink.h>
35 #include <linux/sysctl.h>
36 #include <asm/uaccess.h>
37 #include <net/neighbour.h>
38 #include <net/dst.h>
39 #include <net/dn.h>
40 #include <net/dn_dev.h>
41 #include <net/dn_route.h>
42 #include <net/dn_neigh.h>
43 #include <net/dn_fib.h>
45 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
47 static char dn_rt_all_end_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x04,0x00,0x00};
48 static char dn_rt_all_rt_mcast[ETH_ALEN] = {0xAB,0x00,0x00,0x03,0x00,0x00};
49 static char dn_hiord[ETH_ALEN] = {0xAA,0x00,0x04,0x00,0x00,0x00};
50 static unsigned char dn_eco_version[3] = {0x02,0x00,0x00};
52 extern struct neigh_table dn_neigh_table;
54 struct net_device *decnet_default_device = NULL;
56 static struct dn_dev *dn_dev_create(struct net_device *dev, int *err);
57 static void dn_dev_delete(struct net_device *dev);
58 #ifdef CONFIG_RTNETLINK
59 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa);
60 #endif
62 static int dn_eth_up(struct net_device *);
63 static void dn_send_brd_hello(struct net_device *dev);
64 #if 0
65 static void dn_send_ptp_hello(struct net_device *dev);
66 #endif
68 static struct dn_dev_parms dn_dev_list[] = {
70 ARPHRD_ETHER, /* Ethernet */
71 DN_DEV_BCAST,
72 DN_DEV_S_RU,
74 1498,
76 10,
78 "ethernet",
79 NET_DECNET_CONF_ETHER,
80 dn_eth_up,
81 NULL,
82 dn_send_brd_hello,
83 NULL
86 ARPHRD_IPGRE, /* DECnet tunneled over GRE in IP */
87 DN_DEV_BCAST,
88 DN_DEV_S_RU,
90 1400,
92 10,
94 "ipgre",
95 NET_DECNET_CONF_GRE,
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,
109 120,
111 "x25",
112 NET_DECNET_CONF_X25,
113 NULL,
114 NULL,
115 dn_send_ptp_hello,
116 NULL
118 #endif
119 #if 0
121 ARPHRD_PPP, /* DECnet over PPP */
122 DN_DEV_BCAST,
123 DN_DEV_S_RU,
125 230,
129 "ppp",
130 NET_DECNET_CONF_PPP,
131 NULL,
132 NULL,
133 dn_send_brd_hello,
134 NULL
136 #endif
137 #if 0
139 ARPHRD_DDCMP, /* DECnet over DDCMP */
140 DN_DEV_UCAST,
141 DN_DEV_S_DS,
143 230,
145 120,
147 "ddcmp",
148 NET_DECNET_CONF_DDCMP,
149 NULL,
150 NULL,
151 dn_send_ptp_hello,
152 NULL
154 #endif
156 ARPHRD_LOOPBACK, /* Loopback interface - always last */
157 DN_DEV_BCAST,
158 DN_DEV_S_RU,
160 1498,
164 "loopback",
165 NET_DECNET_CONF_LOOPBACK,
166 NULL,
167 NULL,
168 dn_send_brd_hello,
169 NULL
173 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
175 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
177 #ifdef CONFIG_SYSCTL
179 static int min_t2[] = { 1 };
180 static int max_t2[] = { 60 }; /* No max specified, but this seems sensible */
181 static int min_t3[] = { 1 };
182 static int max_t3[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
184 static int min_priority[] = { 0 };
185 static int max_priority[] = { 127 }; /* From DECnet spec */
187 static int dn_forwarding_proc(ctl_table *, int, struct file *,
188 void *, size_t *);
189 static int dn_forwarding_sysctl(ctl_table *table, int *name, int nlen,
190 void *oldval, size_t *oldlenp,
191 void *newval, size_t newlen,
192 void **context);
194 static struct dn_dev_sysctl_table {
195 struct ctl_table_header *sysctl_header;
196 ctl_table dn_dev_vars[5];
197 ctl_table dn_dev_dev[2];
198 ctl_table dn_dev_conf_dir[2];
199 ctl_table dn_dev_proto_dir[2];
200 ctl_table dn_dev_root_dir[2];
201 } dn_dev_sysctl = {
202 NULL,
204 {NET_DECNET_CONF_DEV_FORWARDING, "forwarding",
205 (void *)DN_DEV_PARMS_OFFSET(forwarding),
206 sizeof(int), 0644, NULL,
207 dn_forwarding_proc, dn_forwarding_sysctl,
208 NULL, NULL, NULL},
209 {NET_DECNET_CONF_DEV_PRIORITY, "priority",
210 (void *)DN_DEV_PARMS_OFFSET(priority),
211 sizeof(int), 0644, NULL,
212 proc_dointvec_minmax, sysctl_intvec,
213 NULL, &min_priority, &max_priority},
214 {NET_DECNET_CONF_DEV_T2, "t2", (void *)DN_DEV_PARMS_OFFSET(t2),
215 sizeof(int), 0644, NULL,
216 proc_dointvec_minmax, sysctl_intvec,
217 NULL, &min_t2, &max_t2},
218 {NET_DECNET_CONF_DEV_T3, "t3", (void *)DN_DEV_PARMS_OFFSET(t3),
219 sizeof(int), 0644, NULL,
220 proc_dointvec_minmax, sysctl_intvec,
221 NULL, &min_t3, &max_t3},
224 {{0, "", NULL, 0, 0555, dn_dev_sysctl.dn_dev_vars}, {0}},
225 {{NET_DECNET_CONF, "conf", NULL, 0, 0555, dn_dev_sysctl.dn_dev_dev}, {0}},
226 {{NET_DECNET, "decnet", NULL, 0, 0555, dn_dev_sysctl.dn_dev_conf_dir}, {0}},
227 {{CTL_NET, "net", NULL, 0, 0555, dn_dev_sysctl.dn_dev_proto_dir}, {0}}
230 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
232 struct dn_dev_sysctl_table *t;
233 int i;
235 t = kmalloc(sizeof(*t), GFP_KERNEL);
236 if (t == NULL)
237 return;
239 memcpy(t, &dn_dev_sysctl, sizeof(*t));
241 for(i = 0; i < (sizeof(t->dn_dev_vars)/sizeof(t->dn_dev_vars[0]) - 1); i++) {
242 long offset = (long)t->dn_dev_vars[i].data;
243 t->dn_dev_vars[i].data = ((char *)parms) + offset;
244 t->dn_dev_vars[i].de = NULL;
247 if (dev) {
248 t->dn_dev_dev[0].procname = dev->name;
249 t->dn_dev_dev[0].ctl_name = dev->ifindex;
250 } else {
251 t->dn_dev_dev[0].procname = parms->name;
252 t->dn_dev_dev[0].ctl_name = parms->ctl_name;
255 t->dn_dev_dev[0].child = t->dn_dev_vars;
256 t->dn_dev_dev[0].de = NULL;
257 t->dn_dev_conf_dir[0].child = t->dn_dev_dev;
258 t->dn_dev_conf_dir[0].de = NULL;
259 t->dn_dev_proto_dir[0].child = t->dn_dev_conf_dir;
260 t->dn_dev_proto_dir[0].de = NULL;
261 t->dn_dev_root_dir[0].child = t->dn_dev_proto_dir;
262 t->dn_dev_root_dir[0].de = NULL;
263 t->dn_dev_vars[0].extra1 = (void *)dev;
265 t->sysctl_header = register_sysctl_table(t->dn_dev_root_dir, 0);
266 if (t->sysctl_header == NULL)
267 kfree(t);
268 else
269 parms->sysctl = t;
272 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
274 if (parms->sysctl) {
275 struct dn_dev_sysctl_table *t = parms->sysctl;
276 parms->sysctl = NULL;
277 unregister_sysctl_table(t->sysctl_header);
278 kfree(t);
283 static int dn_forwarding_proc(ctl_table *table, int write,
284 struct file *filep,
285 void *buffer, size_t *lenp)
287 #ifdef CONFIG_DECNET_ROUTER
288 struct net_device *dev = table->extra1;
289 struct dn_dev *dn_db;
290 int err;
291 int tmp, old;
293 if (table->extra1 == NULL)
294 return -EINVAL;
296 dn_db = dev->dn_ptr;
297 old = dn_db->parms.forwarding;
299 err = proc_dointvec(table, write, filep, buffer, lenp);
301 if ((err >= 0) && write) {
302 if (dn_db->parms.forwarding < 0)
303 dn_db->parms.forwarding = 0;
304 if (dn_db->parms.forwarding > 2)
305 dn_db->parms.forwarding = 2;
307 * What an ugly hack this is... its works, just. It
308 * would be nice if sysctl/proc were just that little
309 * bit more flexible so I don't have to write a special
310 * routine, or suffer hacks like this - SJW
312 tmp = dn_db->parms.forwarding;
313 dn_db->parms.forwarding = old;
314 if (dn_db->parms.down)
315 dn_db->parms.down(dev);
316 dn_db->parms.forwarding = tmp;
317 if (dn_db->parms.up)
318 dn_db->parms.up(dev);
321 return err;
322 #else
323 return -EINVAL;
324 #endif
327 static int dn_forwarding_sysctl(ctl_table *table, int *name, int nlen,
328 void *oldval, size_t *oldlenp,
329 void *newval, size_t newlen,
330 void **context)
332 #ifdef CONFIG_DECNET_ROUTER
333 struct net_device *dev = table->extra1;
334 struct dn_dev *dn_db;
335 int value;
337 if (table->extra1 == NULL)
338 return -EINVAL;
340 dn_db = dev->dn_ptr;
342 if (newval && newlen) {
343 if (newlen != sizeof(int))
344 return -EINVAL;
346 get_user(value, (int *)newval);
347 if (value < 0)
348 return -EINVAL;
349 if (value > 2)
350 return -EINVAL;
352 if (dn_db->parms.down)
353 dn_db->parms.down(dev);
354 dn_db->parms.forwarding = value;
355 if (dn_db->parms.up)
356 dn_db->parms.up(dev);
359 return 0;
360 #else
361 return -EINVAL;
362 #endif
365 #else /* CONFIG_SYSCTL */
366 static void dn_dev_sysctl_unregister(struct dn_dev_parms *parms)
369 static void dn_dev_sysctl_register(struct net_device *dev, struct dn_dev_parms *parms)
373 #endif /* CONFIG_SYSCTL */
375 static struct dn_ifaddr *dn_dev_alloc_ifa(void)
377 struct dn_ifaddr *ifa;
379 ifa = kmalloc(sizeof(*ifa), GFP_KERNEL);
381 if (ifa) {
382 memset(ifa, 0, sizeof(*ifa));
385 return ifa;
388 static __inline__ void dn_dev_free_ifa(struct dn_ifaddr *ifa)
390 kfree_s(ifa, sizeof(*ifa));
393 static void dn_dev_del_ifa(struct dn_dev *dn_db, struct dn_ifaddr **ifap, int destroy)
395 struct dn_ifaddr *ifa1 = *ifap;
397 *ifap = ifa1->ifa_next;
399 #ifdef CONFIG_RTNETLINK
400 rtmsg_ifa(RTM_DELADDR, ifa1);
401 #endif /* CONFIG_RTNETLINK */
403 if (destroy) {
404 dn_dev_free_ifa(ifa1);
406 if (dn_db->ifa_list == NULL)
407 dn_dev_delete(dn_db->dev);
411 static int dn_dev_insert_ifa(struct dn_dev *dn_db, struct dn_ifaddr *ifa)
414 * FIXME: Duplicate check here.
417 ifa->ifa_next = dn_db->ifa_list;
418 dn_db->ifa_list = ifa;
420 #ifdef CONFIG_RTNETLINK
421 rtmsg_ifa(RTM_NEWADDR, ifa);
422 #endif /* CONFIG_RTNETLINK */
424 return 0;
427 static int dn_dev_set_ifa(struct net_device *dev, struct dn_ifaddr *ifa)
429 struct dn_dev *dn_db = dev->dn_ptr;
431 if (dn_db == NULL) {
432 int err;
433 dn_db = dn_dev_create(dev, &err);
434 if (dn_db == NULL)
435 return err;
438 ifa->ifa_dev = dn_db;
440 if (dev->flags & IFF_LOOPBACK)
441 ifa->ifa_scope = RT_SCOPE_HOST;
443 return dn_dev_insert_ifa(dn_db, ifa);
447 int dn_dev_ioctl(unsigned int cmd, void *arg)
449 char buffer[DN_IFREQ_SIZE];
450 struct ifreq *ifr = (struct ifreq *)buffer;
451 struct sockaddr_dn *sdn = (struct sockaddr_dn *)&ifr->ifr_addr;
452 struct dn_dev *dn_db;
453 struct net_device *dev;
454 struct dn_ifaddr *ifa = NULL, **ifap = NULL;
455 int exclusive = 0;
456 int ret = 0;
458 if (copy_from_user(ifr, arg, DN_IFREQ_SIZE))
459 return -EFAULT;
460 ifr->ifr_name[IFNAMSIZ-1] = 0;
462 #ifdef CONFIG_KMOD
463 dev_load(ifr->ifr_name);
464 #endif
466 switch(cmd) {
467 case SIOCGIFADDR:
468 break;
469 case SIOCSIFADDR:
470 if (!capable(CAP_NET_ADMIN))
471 return -EACCES;
472 if (sdn->sdn_family != AF_DECnet)
473 return -EINVAL;
474 rtnl_lock();
475 exclusive = 1;
476 break;
477 default:
478 return -EINVAL;
481 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL) {
482 ret = -ENODEV;
483 goto done;
486 if ((dn_db = dev->dn_ptr) != NULL) {
487 for (ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next)
488 if (strcmp(ifr->ifr_name, ifa->ifa_label) == 0)
489 break;
492 if (ifa == NULL && cmd != SIOCSIFADDR) {
493 ret = -EADDRNOTAVAIL;
494 goto done;
497 switch(cmd) {
498 case SIOCGIFADDR:
499 *((dn_address *)sdn->sdn_nodeaddr) = ifa->ifa_local;
500 goto rarok;
502 case SIOCSIFADDR:
503 if (!ifa) {
504 if ((ifa = dn_dev_alloc_ifa()) == NULL) {
505 ret = -ENOBUFS;
506 break;
508 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
509 } else {
510 if (ifa->ifa_local == dn_saddr2dn(sdn))
511 break;
512 dn_dev_del_ifa(dn_db, ifap, 0);
515 ifa->ifa_local = dn_saddr2dn(sdn);
517 ret = dn_dev_set_ifa(dev, ifa);
519 done:
520 if (exclusive)
521 rtnl_unlock();
523 return ret;
524 rarok:
525 if (copy_to_user(arg, ifr, DN_IFREQ_SIZE))
526 return -EFAULT;
528 return 0;
531 #ifdef CONFIG_RTNETLINK
532 static struct dn_dev *dn_dev_by_index(int ifindex)
534 struct net_device *dev;
535 struct dn_dev *dn_dev = NULL;
536 dev = dev_get_by_index(ifindex);
537 if (dev) {
538 dn_dev = dev->dn_ptr;
539 dev_put(dev);
542 return dn_dev;
545 static int dn_dev_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
547 struct rtattr **rta = arg;
548 struct dn_dev *dn_db;
549 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
550 struct dn_ifaddr *ifa, **ifap;
552 if ((dn_db = dn_dev_by_index(ifm->ifa_index)) == NULL)
553 return -EADDRNOTAVAIL;
555 for(ifap = &dn_db->ifa_list; (ifa=*ifap) != NULL; ifap = &ifa->ifa_next) {
556 void *tmp = rta[IFA_LOCAL-1];
557 if ((tmp && memcmp(RTA_DATA(tmp), &ifa->ifa_local, 2)) ||
558 (rta[IFA_LABEL-1] && strcmp(RTA_DATA(rta[IFA_LABEL-1]), ifa->ifa_label)))
559 continue;
561 dn_dev_del_ifa(dn_db, ifap, 1);
562 return 0;
565 return -EADDRNOTAVAIL;
568 static int dn_dev_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
570 struct rtattr **rta = arg;
571 struct net_device *dev;
572 struct dn_dev *dn_db;
573 struct ifaddrmsg *ifm = NLMSG_DATA(nlh);
574 struct dn_ifaddr *ifa;
576 if (rta[IFA_LOCAL-1] == NULL)
577 return -EINVAL;
579 if ((dev = __dev_get_by_index(ifm->ifa_index)) == NULL)
580 return -ENODEV;
582 if ((dn_db = dev->dn_ptr) == NULL) {
583 int err;
584 dn_db = dn_dev_create(dev, &err);
585 if (!dn_db)
586 return err;
589 if ((ifa = dn_dev_alloc_ifa()) == NULL)
590 return -ENOBUFS;
592 memcpy(&ifa->ifa_local, RTA_DATA(rta[IFA_LOCAL-1]), 2);
593 ifa->ifa_flags = ifm->ifa_flags;
594 ifa->ifa_scope = ifm->ifa_scope;
595 ifa->ifa_dev = dn_db;
596 if (rta[IFA_LABEL-1])
597 memcpy(ifa->ifa_label, RTA_DATA(rta[IFA_LABEL-1]), IFNAMSIZ);
598 else
599 memcpy(ifa->ifa_label, dev->name, IFNAMSIZ);
601 return dn_dev_insert_ifa(dn_db, ifa);
604 static int dn_dev_fill_ifaddr(struct sk_buff *skb, struct dn_ifaddr *ifa,
605 u32 pid, u32 seq, int event)
607 struct ifaddrmsg *ifm;
608 struct nlmsghdr *nlh;
609 unsigned char *b = skb->tail;
611 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(*ifm));
612 ifm = NLMSG_DATA(nlh);
614 ifm->ifa_family = AF_DECnet;
615 ifm->ifa_prefixlen = 16;
616 ifm->ifa_flags = ifa->ifa_flags | IFA_F_PERMANENT;
617 ifm->ifa_scope = ifa->ifa_scope;
618 ifm->ifa_index = ifa->ifa_dev->dev->ifindex;
619 RTA_PUT(skb, IFA_LOCAL, 2, &ifa->ifa_local);
620 if (ifa->ifa_label[0])
621 RTA_PUT(skb, IFA_LABEL, IFNAMSIZ, &ifa->ifa_label);
622 nlh->nlmsg_len = skb->tail - b;
623 return skb->len;
625 nlmsg_failure:
626 rtattr_failure:
627 skb_trim(skb, b - skb->data);
628 return -1;
631 static void rtmsg_ifa(int event, struct dn_ifaddr *ifa)
633 struct sk_buff *skb;
634 int size = NLMSG_SPACE(sizeof(struct ifaddrmsg)+128);
636 skb = alloc_skb(size, GFP_KERNEL);
637 if (!skb) {
638 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, ENOBUFS);
639 return;
641 if (dn_dev_fill_ifaddr(skb, ifa, 0, 0, event) < 0) {
642 kfree_skb(skb);
643 netlink_set_err(rtnl, 0, RTMGRP_DECnet_IFADDR, EINVAL);
644 return;
646 NETLINK_CB(skb).dst_groups = RTMGRP_DECnet_IFADDR;
647 netlink_broadcast(rtnl, skb, 0, RTMGRP_DECnet_IFADDR, GFP_KERNEL);
650 static int dn_dev_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
652 int idx, dn_idx;
653 int s_idx, s_dn_idx;
654 struct net_device *dev;
655 struct dn_dev *dn_db;
656 struct dn_ifaddr *ifa;
658 s_idx = cb->args[0];
659 s_dn_idx = dn_idx = cb->args[1];
660 read_lock(&dev_base_lock);
661 for(dev = dev_base, idx = 0; dev; dev = dev->next) {
662 if ((dn_db = dev->dn_ptr) == NULL)
663 continue;
664 idx++;
665 if (idx < s_idx)
666 continue;
667 if (idx > s_idx)
668 s_dn_idx = 0;
669 if ((dn_db = dev->dn_ptr) == NULL)
670 continue;
672 for(ifa = dn_db->ifa_list, dn_idx = 0; ifa; ifa = ifa->ifa_next, dn_idx++) {
673 if (dn_idx < s_dn_idx)
674 continue;
676 if (dn_dev_fill_ifaddr(skb, ifa, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, RTM_NEWADDR) <= 0)
677 goto done;
680 done:
681 read_unlock(&dev_base_lock);
682 cb->args[0] = idx;
683 cb->args[1] = dn_idx;
685 return skb->len;
688 #endif /* CONFIG_RTNETLINK */
690 static void dn_send_endnode_hello(struct net_device *dev)
692 struct endnode_hello_message *msg;
693 struct sk_buff *skb = NULL;
694 unsigned short int *pktlen;
695 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
697 if ((skb = dn_alloc_skb(NULL, sizeof(*msg), GFP_ATOMIC)) == NULL)
698 return;
700 skb->dev = dev;
702 msg = (struct endnode_hello_message *)skb_put(skb,sizeof(*msg));
704 msg->msgflg = 0x0D;
705 memcpy(msg->tiver, dn_eco_version, 3);
706 memcpy(msg->id, decnet_ether_address, 6);
707 msg->iinfo = DN_RT_INFO_ENDN;
708 msg->blksize = dn_htons(dn_db->parms.blksize);
709 msg->area = 0x00;
710 memset(msg->seed, 0, 8);
711 memcpy(msg->neighbor, dn_hiord, ETH_ALEN);
713 if (dn_db->router) {
714 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
715 dn_dn2eth(msg->neighbor, dn->addr);
718 msg->timer = dn_htons((unsigned short)dn_db->parms.t3);
719 msg->mpd = 0x00;
720 msg->datalen = 0x02;
721 memset(msg->data, 0xAA, 2);
723 pktlen = (unsigned short *)skb_push(skb,2);
724 *pktlen = dn_htons(skb->len - 2);
726 skb->nh.raw = skb->data;
728 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
732 #ifdef CONFIG_DECNET_ROUTER
734 #define DRDELAY (5 * HZ)
736 static int dn_am_i_a_router(struct dn_neigh *dn, struct dn_dev *dn_db)
738 /* First check time since device went up */
739 if ((jiffies - dn_db->uptime) < DRDELAY)
740 return 0;
742 /* If there is no router, then yes... */
743 if (!dn_db->router)
744 return 1;
746 /* otherwise only if we have a higher priority or.. */
747 if (dn->priority < dn_db->parms.priority)
748 return 1;
750 /* if we have equal priority and a higher node number */
751 if (dn->priority != dn_db->parms.priority)
752 return 0;
754 if (dn_ntohs(dn->addr) < dn_ntohs(decnet_address))
755 return 1;
757 return 0;
760 static void dn_send_router_hello(struct net_device *dev)
762 int n;
763 struct dn_dev *dn_db = dev->dn_ptr;
764 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
765 struct sk_buff *skb;
766 size_t size;
767 unsigned char *ptr;
768 unsigned char *i1, *i2;
769 unsigned short *pktlen;
771 if (dn_db->parms.blksize < (26 + 7))
772 return;
774 n = dn_db->parms.blksize - 26;
775 n /= 7;
777 if (n > 32)
778 n = 32;
780 size = 2 + 26 + 7 * n;
782 if ((skb = dn_alloc_skb(NULL, size, GFP_ATOMIC)) == NULL)
783 return;
785 skb->dev = dev;
786 ptr = skb_put(skb, size);
788 *ptr++ = DN_RT_PKT_CNTL | DN_RT_PKT_ERTH;
789 *ptr++ = 2; /* ECO */
790 *ptr++ = 0;
791 *ptr++ = 0;
792 memcpy(ptr, decnet_ether_address, ETH_ALEN);
793 ptr += ETH_ALEN;
794 *ptr++ = dn_db->parms.forwarding == 1 ?
795 DN_RT_INFO_L1RT : DN_RT_INFO_L2RT;
796 *((unsigned short *)ptr) = dn_htons(dn_db->parms.blksize);
797 ptr += 2;
798 *ptr++ = 0; /* Priority */
799 *ptr++ = 0; /* Area: Reserved */
800 *((unsigned short *)ptr) = dn_htons((unsigned short)dn_db->parms.t3);
801 ptr += 2;
802 *ptr++ = 0; /* MPD: Reserved */
803 i1 = ptr++;
804 memset(ptr, 0, 7); /* Name: Reserved */
805 i2 = ptr++;
807 n = dn_neigh_elist(dev, ptr, n);
809 *i2 = 7 * n;
810 *i1 = 8 + *i2;
812 skb_trim(skb, (26 + *i2));
814 pktlen = (unsigned short *)skb_push(skb, 2);
815 *pktlen = dn_htons(skb->len - 2);
817 skb->nh.raw = skb->data;
819 if (dn_am_i_a_router(dn, dn_db)) {
820 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
821 if (skb2) {
822 dn_rt_finish_output(skb2, dn_rt_all_end_mcast);
826 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
829 static void dn_send_brd_hello(struct net_device *dev)
831 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
833 if (dn_db->parms.forwarding == 0)
834 dn_send_endnode_hello(dev);
835 else
836 dn_send_router_hello(dev);
838 #else
839 static void dn_send_brd_hello(struct net_device *dev)
841 dn_send_endnode_hello(dev);
843 #endif
845 #if 0
846 static void dn_send_ptp_hello(struct net_device *dev)
848 int tdlen = 16;
849 int size = dev->hard_header_len + 2 + 4 + tdlen;
850 struct sk_buff *skb = dn_alloc_skb(NULL, size, GFP_ATOMIC);
851 struct dn_dev *dn_db = dev->dn_ptr;
852 int i;
853 unsigned char *ptr;
854 struct dn_neigh *dn = (struct dn_neigh *)dn_db->router;
856 if (skb == NULL)
857 return ;
859 skb->dev = dev;
860 skb_push(skb, dev->hard_header_len);
861 ptr = skb_put(skb, 2 + 4 + tdlen);
863 *ptr++ = DN_RT_PKT_HELO;
864 *((dn_address *)ptr) = decnet_address;
865 ptr += 2;
866 *ptr++ = tdlen;
868 for(i = 0; i < tdlen; i++)
869 *ptr++ = 0252;
871 if (dn_am_i_a_router(dn, dn_db)) {
872 struct sk_buff *skb2 = skb_copy(skb, GFP_ATOMIC);
873 if (skb2) {
874 dn_rt_finish_output(skb2, dn_rt_all_end_mcast);
878 dn_rt_finish_output(skb, dn_rt_all_rt_mcast);
880 #endif
882 static int dn_eth_up(struct net_device *dev)
884 struct dn_dev *dn_db = dev->dn_ptr;
886 if (dn_db->parms.forwarding == 0)
887 dev_mc_add(dev, dn_rt_all_end_mcast, ETH_ALEN, 0);
888 else
889 dev_mc_add(dev, dn_rt_all_rt_mcast, ETH_ALEN, 0);
891 dev_mc_upload(dev);
893 dn_db->use_long = 1;
895 return 0;
898 static void dn_dev_set_timer(struct net_device *dev);
900 static void dn_dev_timer_func(unsigned long arg)
902 struct net_device *dev = (struct net_device *)arg;
903 struct dn_dev *dn_db = dev->dn_ptr;
905 if (dn_db->t3 <= dn_db->parms.t2) {
906 if (dn_db->parms.timer3)
907 dn_db->parms.timer3(dev);
908 dn_db->t3 = dn_db->parms.t3;
909 } else {
910 dn_db->t3 -= dn_db->parms.t2;
913 dn_dev_set_timer(dev);
916 static void dn_dev_set_timer(struct net_device *dev)
918 struct dn_dev *dn_db = dev->dn_ptr;
920 if (dn_db->parms.t2 > dn_db->parms.t3)
921 dn_db->parms.t2 = dn_db->parms.t3;
923 dn_db->timer.data = (unsigned long)dev;
924 dn_db->timer.function = dn_dev_timer_func;
925 dn_db->timer.expires = jiffies + (dn_db->parms.t2 * HZ);
927 add_timer(&dn_db->timer);
930 struct dn_dev *dn_dev_create(struct net_device *dev, int *err)
932 int i;
933 struct dn_dev_parms *p = dn_dev_list;
934 struct dn_dev *dn_db;
936 for(i = 0; i < DN_DEV_LIST_SIZE; i++, p++) {
937 if (p->type == dev->type)
938 break;
941 *err = -ENODEV;
942 if (i == DN_DEV_LIST_SIZE)
943 return NULL;
945 *err = -ENOBUFS;
946 if ((dn_db = kmalloc(sizeof(struct dn_dev), GFP_ATOMIC)) == NULL)
947 return NULL;
949 memset(dn_db, 0, sizeof(struct dn_dev));
950 memcpy(&dn_db->parms, p, sizeof(struct dn_dev_parms));
951 dev->dn_ptr = dn_db;
952 dn_db->dev = dev;
953 init_timer(&dn_db->timer);
955 memcpy(dn_db->addr, decnet_ether_address, ETH_ALEN); /* To go... */
957 dn_db->uptime = jiffies;
958 if (dn_db->parms.up) {
959 if (dn_db->parms.up(dev) < 0) {
960 dev->dn_ptr = NULL;
961 kfree(dn_db);
962 return NULL;
966 dn_db->neigh_parms = neigh_parms_alloc(dev, &dn_neigh_table);
967 /* dn_db->neigh_parms->neigh_setup = dn_db->parms.neigh_setup; */
969 dn_dev_sysctl_register(dev, &dn_db->parms);
971 dn_dev_set_timer(dev);
973 *err = 0;
974 return dn_db;
979 * This processes a device up event. We only start up
980 * the loopback device & ethernet devices with correct
981 * MAC addreses automatically. Others must be started
982 * specifically.
984 void dn_dev_up(struct net_device *dev)
986 struct dn_ifaddr *ifa;
988 if ((dev->type != ARPHRD_ETHER) && (dev->type != ARPHRD_LOOPBACK))
989 return;
991 if (dev->type == ARPHRD_ETHER)
992 if (memcmp(dev->dev_addr, decnet_ether_address, ETH_ALEN) != 0)
993 return;
995 if ((ifa = dn_dev_alloc_ifa()) == NULL)
996 return;
998 ifa->ifa_local = decnet_address;
999 ifa->ifa_flags = 0;
1000 ifa->ifa_scope = RT_SCOPE_UNIVERSE;
1001 strcpy(ifa->ifa_label, dev->name);
1003 dn_dev_set_ifa(dev, ifa);
1006 static void dn_dev_delete(struct net_device *dev)
1008 struct dn_dev *dn_db = dev->dn_ptr;
1010 if (dn_db == NULL)
1011 return;
1013 del_timer(&dn_db->timer);
1014 synchronize_bh();
1016 dn_dev_sysctl_unregister(&dn_db->parms);
1018 neigh_ifdown(&dn_neigh_table, dev);
1020 if (dev == decnet_default_device)
1021 decnet_default_device = NULL;
1023 if (dn_db->parms.down)
1024 dn_db->parms.down(dev);
1026 dev->dn_ptr = NULL;
1028 neigh_parms_release(&dn_neigh_table, dn_db->neigh_parms);
1030 if (dn_db->router)
1031 neigh_release(dn_db->router);
1032 if (dn_db->peer)
1033 neigh_release(dn_db->peer);
1035 kfree(dn_db);
1038 void dn_dev_down(struct net_device *dev)
1040 struct dn_dev *dn_db = dev->dn_ptr;
1041 struct dn_ifaddr *ifa;
1043 if (dn_db == NULL)
1044 return;
1046 while((ifa = dn_db->ifa_list) != NULL) {
1047 dn_dev_del_ifa(dn_db, &dn_db->ifa_list, 0);
1048 dn_dev_free_ifa(ifa);
1051 dn_dev_delete(dev);
1054 void dn_dev_init_pkt(struct sk_buff *skb)
1056 return;
1059 void dn_dev_veri_pkt(struct sk_buff *skb)
1061 return;
1064 void dn_dev_hello(struct sk_buff *skb)
1066 return;
1069 void dn_dev_devices_off(void)
1071 struct net_device *dev;
1073 for(dev = dev_base; dev; dev = dev->next)
1074 dn_dev_down(dev);
1078 void dn_dev_devices_on(void)
1080 struct net_device *dev;
1082 for(dev = dev_base; dev; dev = dev->next) {
1083 if (dev->flags & IFF_UP)
1084 dn_dev_up(dev);
1089 #ifdef CONFIG_DECNET_SIOCGIFCONF
1091 * Now we support multiple addresses per interface.
1092 * Since we don't want to break existing code, you have to enable
1093 * it as a compile time option. Probably you should use the
1094 * rtnetlink interface instead.
1096 int dnet_gifconf(struct net_device *dev, char *buf, int len)
1098 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
1099 struct dn_ifaddr *ifa;
1100 struct ifreq *ifr = (struct ifreq *)buf;
1101 int done = 0;
1103 if ((dn_db == NULL) || ((ifa = dn_db->ifa_list) == NULL))
1104 return 0;
1106 for(; ifa; ifa = ifa->ifa_next) {
1107 if (!ifr) {
1108 done += sizeof(DN_IFREQ_SIZE);
1109 continue;
1111 if (len < DN_IFREQ_SIZE)
1112 return done;
1113 memset(ifr, 0, DN_IFREQ_SIZE);
1115 if (ifa->ifa_label)
1116 strcpy(ifr->ifr_name, ifa->ifa_label);
1117 else
1118 strcpy(ifr->ifr_name, dev->name);
1120 (*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_family = AF_DECnet;
1121 (*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_add.a_len = 2;
1122 (*(dn_address *)(*(struct sockaddr_dn *) &ifr->ifr_addr).sdn_add.a_addr) = ifa->ifa_local;
1124 ifr = (struct ifreq *)((char *)ifr + DN_IFREQ_SIZE);
1125 len -= DN_IFREQ_SIZE;
1126 done += DN_IFREQ_SIZE;
1129 return done;
1131 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1134 #ifdef CONFIG_PROC_FS
1136 static char *dn_type2asc(char type)
1138 switch(type) {
1139 case DN_DEV_BCAST:
1140 return "B";
1141 case DN_DEV_UCAST:
1142 return "U";
1143 case DN_DEV_MPOINT:
1144 return "M";
1147 return "?";
1150 static int decnet_dev_get_info(char *buffer, char **start, off_t offset, int length)
1152 struct dn_dev *dn_db;
1153 struct net_device *dev;
1154 int len = 0;
1155 off_t pos = 0;
1156 off_t begin = 0;
1157 char peer_buf[DN_ASCBUF_LEN];
1158 char router_buf[DN_ASCBUF_LEN];
1161 len += sprintf(buffer, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1163 read_lock(&dev_base_lock);
1164 for (dev = dev_base; dev; dev = dev->next) {
1165 if ((dn_db = (struct dn_dev *)dev->dn_ptr) == NULL)
1166 continue;
1168 len += sprintf(buffer + len, "%-8s %1s %04u %04u %04lu %04lu %04hu %03d %02x %-10s %-7s %-7s\n",
1169 dev->name ? dev->name : "???",
1170 dn_type2asc(dn_db->parms.mode),
1171 0, 0,
1172 dn_db->t3, dn_db->parms.t3,
1173 dn_db->parms.blksize,
1174 dn_db->parms.priority,
1175 dn_db->parms.state, dn_db->parms.name,
1176 dn_db->router ? dn_addr2asc(dn_eth2dn(dn_db->router->primary_key), router_buf) : "",
1177 dn_db->peer ? dn_addr2asc(dn_eth2dn(dn_db->peer->primary_key), peer_buf) : "");
1180 pos = begin + len;
1182 if (pos < offset) {
1183 len = 0;
1184 begin = pos;
1186 if (pos > offset + length)
1187 break;
1190 read_unlock(&dev_base_lock);
1192 *start = buffer + (offset - begin);
1193 len -= (offset - begin);
1195 if (len > length) len = length;
1197 return(len);
1200 #endif /* CONFIG_PROC_FS */
1202 #ifdef CONFIG_RTNETLINK
1203 static struct rtnetlink_link dnet_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
1205 { NULL, NULL, },
1206 { NULL, NULL, },
1207 { NULL, NULL, },
1208 { NULL, NULL, },
1210 { dn_dev_rtm_newaddr, NULL, },
1211 { dn_dev_rtm_deladdr, NULL, },
1212 { NULL, dn_dev_dump_ifaddr, },
1213 { NULL, NULL, },
1215 #ifdef CONFIG_DECNET_ROUTER
1216 { dn_fib_rtm_newroute, NULL, },
1217 { dn_fib_rtm_delroute, NULL, },
1218 { dn_cache_getroute, dn_fib_dump, },
1219 { NULL, NULL, },
1220 #else
1221 { NULL, NULL, },
1222 { NULL, NULL, },
1223 { dn_cache_getroute, dn_cache_dump, },
1224 { NULL, NULL, },
1225 #endif
1226 { NULL, NULL, },
1227 { NULL, NULL, },
1228 { NULL, NULL, },
1229 { NULL, NULL, },
1231 #ifdef CONFIG_DECNET_ROUTER
1232 { dn_fib_rtm_newrule, NULL, },
1233 { dn_fib_rtm_delrule, NULL, },
1234 { NULL, dn_fib_dump_rules, },
1235 { NULL, NULL, }
1236 #else
1237 { NULL, NULL, },
1238 { NULL, NULL, },
1239 { NULL, NULL, },
1240 { NULL, NULL, }
1241 #endif
1243 #endif /* CONFIG_RTNETLINK */
1245 void __init dn_dev_init(void)
1248 dn_dev_devices_on();
1249 #ifdef CONFIG_DECNET_SIOCGIFCONF
1250 register_gifconf(PF_DECnet, dnet_gifconf);
1251 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1253 #ifdef CONFIG_RTNETLINK
1254 rtnetlink_links[PF_DECnet] = dnet_rtnetlink_table;
1255 #endif /* CONFIG_RTNETLINK */
1257 #ifdef CONFIG_PROC_FS
1258 proc_net_create("decnet_dev", 0, decnet_dev_get_info);
1259 #endif /* CONFIG_PROC_FS */
1261 #ifdef CONFIG_SYSCTL
1263 int i;
1264 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1265 dn_dev_sysctl_register(NULL, &dn_dev_list[i]);
1267 #endif /* CONFIG_SYSCTL */
1270 void __exit dn_dev_cleanup(void)
1272 #ifdef CONFIG_RTNETLINK
1273 rtnetlink_links[PF_DECnet] = NULL;
1274 #endif /* CONFIG_RTNETLINK */
1276 #ifdef CONFIG_DECNET_SIOCGIFCONF
1277 unregister_gifconf(PF_DECnet);
1278 #endif /* CONFIG_DECNET_SIOCGIFCONF */
1280 #ifdef CONFIG_SYSCTL
1282 int i;
1283 for(i = 0; i < DN_DEV_LIST_SIZE; i++)
1284 dn_dev_sysctl_unregister(&dn_dev_list[i]);
1286 #endif /* CONFIG_SYSCTL */
1288 #ifdef CONFIG_PROC_FS
1289 proc_net_remove("decnet_dev");
1290 #endif /* CONFIG_PROC_FS */
1292 dn_dev_devices_off();