2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
8 * Authors: Steve Whitehouse <SteveW@ACM.org>
9 * Eduardo Marcelo Serrat <emserrat@geocities.com>
12 * Steve Whitehouse : Devices now see incoming frames so they
13 * can mark on who it came from.
14 * Steve Whitehouse : Fixed bug in creating neighbours. Each neighbour
15 * can now have a device specific setup func.
16 * Steve Whitehouse : Added /proc/sys/net/decnet/conf/<dev>/
17 * Steve Whitehouse : Fixed bug which sometimes killed timer
18 * Steve Whitehouse : Multiple ifaddr support
19 * Steve Whitehouse : SIOCGIFCONF is now a compile time option
20 * Steve Whitehouse : /proc/sys/net/decnet/conf/<sys>/forwarding
21 * Steve Whitehouse : Removed timer1 - it's a user space issue now
22 * Patrick Caulfield : Fixed router hello message format
23 * Steve Whitehouse : Got rid of constant sizes for blksize for
24 * devices. All mtu based now.
27 #include <linux/capability.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/net.h>
32 #include <linux/netdevice.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/timer.h>
36 #include <linux/string.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_arp.h>
39 #include <linux/if_ether.h>
40 #include <linux/skbuff.h>
41 #include <linux/rtnetlink.h>
42 #include <linux/sysctl.h>
43 #include <linux/notifier.h>
44 #include <asm/uaccess.h>
45 #include <asm/system.h>
46 #include <net/neighbour.h>
50 #include <net/dn_dev.h>
51 #include <net/dn_route.h>
52 #include <net/dn_neigh.h>
53 #include <net/dn_fib.h>
55 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
57 static char dn_rt_all_end_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x04,0x00,0x00};
58 static char dn_rt_all_rt_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x03,0x00,0x00};
59 static char dn_hiord
[ETH_ALEN
] = {0xAA,0x00,0x04,0x00,0x00,0x00};
60 static unsigned char dn_eco_version
[3] = {0x02,0x00,0x00};
62 extern struct neigh_table dn_neigh_table
;
65 * decnet_address is kept in network order.
67 __le16 decnet_address
= 0;
69 static DEFINE_RWLOCK(dndev_lock
);
70 static struct net_device
*decnet_default_device
;
71 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain
);
73 static struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
);
74 static void dn_dev_delete(struct net_device
*dev
);
75 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
);
77 static int dn_eth_up(struct net_device
*);
78 static void dn_eth_down(struct net_device
*);
79 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
80 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
82 static struct dn_dev_parms dn_dev_list
[] = {
84 .type
= ARPHRD_ETHER
, /* Ethernet */
90 .ctl_name
= NET_DECNET_CONF_ETHER
,
93 .timer3
= dn_send_brd_hello
,
96 .type
= ARPHRD_IPGRE
, /* DECnet tunneled over GRE in IP */
102 .ctl_name
= NET_DECNET_CONF_GRE
,
103 .timer3
= dn_send_brd_hello
,
107 .type
= ARPHRD_X25
, /* Bog standard X.25 */
108 .mode
= DN_DEV_UCAST
,
109 .state
= DN_DEV_S_DS
,
113 .ctl_name
= NET_DECNET_CONF_X25
,
114 .timer3
= dn_send_ptp_hello
,
119 .type
= ARPHRD_PPP
, /* DECnet over PPP */
120 .mode
= DN_DEV_BCAST
,
121 .state
= DN_DEV_S_RU
,
125 .ctl_name
= NET_DECNET_CONF_PPP
,
126 .timer3
= dn_send_brd_hello
,
130 .type
= ARPHRD_DDCMP
, /* DECnet over DDCMP */
131 .mode
= DN_DEV_UCAST
,
132 .state
= DN_DEV_S_DS
,
136 .ctl_name
= NET_DECNET_CONF_DDCMP
,
137 .timer3
= dn_send_ptp_hello
,
140 .type
= ARPHRD_LOOPBACK
, /* Loopback interface - always last */
141 .mode
= DN_DEV_BCAST
,
142 .state
= DN_DEV_S_RU
,
146 .ctl_name
= NET_DECNET_CONF_LOOPBACK
,
147 .timer3
= dn_send_brd_hello
,
151 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
153 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
157 static int min_t2
[] = { 1 };
158 static int max_t2
[] = { 60 }; /* No max specified, but this seems sensible */
159 static int min_t3
[] = { 1 };
160 static int max_t3
[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
162 static int min_priority
[1];
163 static int max_priority
[] = { 127 }; /* From DECnet spec */
165 static int dn_forwarding_proc(ctl_table
*, int, struct file
*,
166 void __user
*, size_t *, loff_t
*);
167 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
168 void __user
*oldval
, size_t __user
*oldlenp
,
169 void __user
*newval
, size_t newlen
,
172 static struct dn_dev_sysctl_table
{
173 struct ctl_table_header
*sysctl_header
;
174 ctl_table dn_dev_vars
[5];
175 ctl_table dn_dev_dev
[2];
176 ctl_table dn_dev_conf_dir
[2];
177 ctl_table dn_dev_proto_dir
[2];
178 ctl_table dn_dev_root_dir
[2];
183 .ctl_name
= NET_DECNET_CONF_DEV_FORWARDING
,
184 .procname
= "forwarding",
185 .data
= (void *)DN_DEV_PARMS_OFFSET(forwarding
),
186 .maxlen
= sizeof(int),
188 .proc_handler
= dn_forwarding_proc
,
189 .strategy
= dn_forwarding_sysctl
,
192 .ctl_name
= NET_DECNET_CONF_DEV_PRIORITY
,
193 .procname
= "priority",
194 .data
= (void *)DN_DEV_PARMS_OFFSET(priority
),
195 .maxlen
= sizeof(int),
197 .proc_handler
= proc_dointvec_minmax
,
198 .strategy
= sysctl_intvec
,
199 .extra1
= &min_priority
,
200 .extra2
= &max_priority
203 .ctl_name
= NET_DECNET_CONF_DEV_T2
,
205 .data
= (void *)DN_DEV_PARMS_OFFSET(t2
),
206 .maxlen
= sizeof(int),
208 .proc_handler
= proc_dointvec_minmax
,
209 .strategy
= sysctl_intvec
,
214 .ctl_name
= NET_DECNET_CONF_DEV_T3
,
216 .data
= (void *)DN_DEV_PARMS_OFFSET(t3
),
217 .maxlen
= sizeof(int),
219 .proc_handler
= proc_dointvec_minmax
,
220 .strategy
= sysctl_intvec
,
230 .child
= dn_dev_sysctl
.dn_dev_vars
233 .ctl_name
= NET_DECNET_CONF
,
236 .child
= dn_dev_sysctl
.dn_dev_dev
239 .ctl_name
= NET_DECNET
,
240 .procname
= "decnet",
242 .child
= dn_dev_sysctl
.dn_dev_conf_dir
248 .child
= dn_dev_sysctl
.dn_dev_proto_dir
252 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
254 struct dn_dev_sysctl_table
*t
;
257 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
261 memcpy(t
, &dn_dev_sysctl
, sizeof(*t
));
263 for(i
= 0; i
< ARRAY_SIZE(t
->dn_dev_vars
) - 1; i
++) {
264 long offset
= (long)t
->dn_dev_vars
[i
].data
;
265 t
->dn_dev_vars
[i
].data
= ((char *)parms
) + offset
;
266 t
->dn_dev_vars
[i
].de
= NULL
;
270 t
->dn_dev_dev
[0].procname
= dev
->name
;
271 t
->dn_dev_dev
[0].ctl_name
= dev
->ifindex
;
273 t
->dn_dev_dev
[0].procname
= parms
->name
;
274 t
->dn_dev_dev
[0].ctl_name
= parms
->ctl_name
;
277 t
->dn_dev_dev
[0].child
= t
->dn_dev_vars
;
278 t
->dn_dev_dev
[0].de
= NULL
;
279 t
->dn_dev_conf_dir
[0].child
= t
->dn_dev_dev
;
280 t
->dn_dev_conf_dir
[0].de
= NULL
;
281 t
->dn_dev_proto_dir
[0].child
= t
->dn_dev_conf_dir
;
282 t
->dn_dev_proto_dir
[0].de
= NULL
;
283 t
->dn_dev_root_dir
[0].child
= t
->dn_dev_proto_dir
;
284 t
->dn_dev_root_dir
[0].de
= NULL
;
285 t
->dn_dev_vars
[0].extra1
= (void *)dev
;
287 t
->sysctl_header
= register_sysctl_table(t
->dn_dev_root_dir
, 0);
288 if (t
->sysctl_header
== NULL
)
294 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
297 struct dn_dev_sysctl_table
*t
= parms
->sysctl
;
298 parms
->sysctl
= NULL
;
299 unregister_sysctl_table(t
->sysctl_header
);
304 static int dn_forwarding_proc(ctl_table
*table
, int write
,
307 size_t *lenp
, loff_t
*ppos
)
309 #ifdef CONFIG_DECNET_ROUTER
310 struct net_device
*dev
= table
->extra1
;
311 struct dn_dev
*dn_db
;
315 if (table
->extra1
== NULL
)
319 old
= dn_db
->parms
.forwarding
;
321 err
= proc_dointvec(table
, write
, filep
, buffer
, lenp
, ppos
);
323 if ((err
>= 0) && write
) {
324 if (dn_db
->parms
.forwarding
< 0)
325 dn_db
->parms
.forwarding
= 0;
326 if (dn_db
->parms
.forwarding
> 2)
327 dn_db
->parms
.forwarding
= 2;
329 * What an ugly hack this is... its works, just. It
330 * would be nice if sysctl/proc were just that little
331 * bit more flexible so I don't have to write a special
332 * routine, or suffer hacks like this - SJW
334 tmp
= dn_db
->parms
.forwarding
;
335 dn_db
->parms
.forwarding
= old
;
336 if (dn_db
->parms
.down
)
337 dn_db
->parms
.down(dev
);
338 dn_db
->parms
.forwarding
= tmp
;
340 dn_db
->parms
.up(dev
);
349 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
350 void __user
*oldval
, size_t __user
*oldlenp
,
351 void __user
*newval
, size_t newlen
,
354 #ifdef CONFIG_DECNET_ROUTER
355 struct net_device
*dev
= table
->extra1
;
356 struct dn_dev
*dn_db
;
359 if (table
->extra1
== NULL
)
364 if (newval
&& newlen
) {
365 if (newlen
!= sizeof(int))
368 if (get_user(value
, (int __user
*)newval
))
375 if (dn_db
->parms
.down
)
376 dn_db
->parms
.down(dev
);
377 dn_db
->parms
.forwarding
= value
;
379 dn_db
->parms
.up(dev
);
388 #else /* CONFIG_SYSCTL */
389 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
392 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
396 #endif /* CONFIG_SYSCTL */
398 static inline __u16
mtu2blksize(struct net_device
*dev
)
400 u32 blksize
= dev
->mtu
;
401 if (blksize
> 0xffff)
404 if (dev
->type
== ARPHRD_ETHER
||
405 dev
->type
== ARPHRD_PPP
||
406 dev
->type
== ARPHRD_IPGRE
||
407 dev
->type
== ARPHRD_LOOPBACK
)
410 return (__u16
)blksize
;
413 static struct dn_ifaddr
*dn_dev_alloc_ifa(void)
415 struct dn_ifaddr
*ifa
;
417 ifa
= kzalloc(sizeof(*ifa
), GFP_KERNEL
);
422 static __inline__
void dn_dev_free_ifa(struct dn_ifaddr
*ifa
)
427 static void dn_dev_del_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
**ifap
, int destroy
)
429 struct dn_ifaddr
*ifa1
= *ifap
;
430 unsigned char mac_addr
[6];
431 struct net_device
*dev
= dn_db
->dev
;
435 *ifap
= ifa1
->ifa_next
;
437 if (dn_db
->dev
->type
== ARPHRD_ETHER
) {
438 if (ifa1
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
439 dn_dn2eth(mac_addr
, ifa1
->ifa_local
);
440 dev_mc_delete(dev
, mac_addr
, ETH_ALEN
, 0);
444 rtmsg_ifa(RTM_DELADDR
, ifa1
);
445 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_DOWN
, ifa1
);
447 dn_dev_free_ifa(ifa1
);
449 if (dn_db
->ifa_list
== NULL
)
450 dn_dev_delete(dn_db
->dev
);
454 static int dn_dev_insert_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
456 struct net_device
*dev
= dn_db
->dev
;
457 struct dn_ifaddr
*ifa1
;
458 unsigned char mac_addr
[6];
462 /* Check for duplicates */
463 for(ifa1
= dn_db
->ifa_list
; ifa1
; ifa1
= ifa1
->ifa_next
) {
464 if (ifa1
->ifa_local
== ifa
->ifa_local
)
468 if (dev
->type
== ARPHRD_ETHER
) {
469 if (ifa
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
470 dn_dn2eth(mac_addr
, ifa
->ifa_local
);
471 dev_mc_add(dev
, mac_addr
, ETH_ALEN
, 0);
476 ifa
->ifa_next
= dn_db
->ifa_list
;
477 dn_db
->ifa_list
= ifa
;
479 rtmsg_ifa(RTM_NEWADDR
, ifa
);
480 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_UP
, ifa
);
485 static int dn_dev_set_ifa(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
487 struct dn_dev
*dn_db
= dev
->dn_ptr
;
492 dn_db
= dn_dev_create(dev
, &err
);
497 ifa
->ifa_dev
= dn_db
;
499 if (dev
->flags
& IFF_LOOPBACK
)
500 ifa
->ifa_scope
= RT_SCOPE_HOST
;
502 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
504 dn_dev_free_ifa(ifa
);
509 int dn_dev_ioctl(unsigned int cmd
, void __user
*arg
)
511 char buffer
[DN_IFREQ_SIZE
];
512 struct ifreq
*ifr
= (struct ifreq
*)buffer
;
513 struct sockaddr_dn
*sdn
= (struct sockaddr_dn
*)&ifr
->ifr_addr
;
514 struct dn_dev
*dn_db
;
515 struct net_device
*dev
;
516 struct dn_ifaddr
*ifa
= NULL
, **ifap
= NULL
;
519 if (copy_from_user(ifr
, arg
, DN_IFREQ_SIZE
))
521 ifr
->ifr_name
[IFNAMSIZ
-1] = 0;
524 dev_load(ifr
->ifr_name
);
531 if (!capable(CAP_NET_ADMIN
))
533 if (sdn
->sdn_family
!= AF_DECnet
)
542 if ((dev
= __dev_get_by_name(ifr
->ifr_name
)) == NULL
) {
547 if ((dn_db
= dev
->dn_ptr
) != NULL
) {
548 for (ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
)
549 if (strcmp(ifr
->ifr_name
, ifa
->ifa_label
) == 0)
553 if (ifa
== NULL
&& cmd
!= SIOCSIFADDR
) {
554 ret
= -EADDRNOTAVAIL
;
560 *((__le16
*)sdn
->sdn_nodeaddr
) = ifa
->ifa_local
;
565 if ((ifa
= dn_dev_alloc_ifa()) == NULL
) {
569 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
571 if (ifa
->ifa_local
== dn_saddr2dn(sdn
))
573 dn_dev_del_ifa(dn_db
, ifap
, 0);
576 ifa
->ifa_local
= ifa
->ifa_address
= dn_saddr2dn(sdn
);
578 ret
= dn_dev_set_ifa(dev
, ifa
);
585 if (copy_to_user(arg
, ifr
, DN_IFREQ_SIZE
))
590 struct net_device
*dn_dev_get_default(void)
592 struct net_device
*dev
;
593 read_lock(&dndev_lock
);
594 dev
= decnet_default_device
;
601 read_unlock(&dndev_lock
);
605 int dn_dev_set_default(struct net_device
*dev
, int force
)
607 struct net_device
*old
= NULL
;
611 write_lock(&dndev_lock
);
612 if (force
|| decnet_default_device
== NULL
) {
613 old
= decnet_default_device
;
614 decnet_default_device
= dev
;
617 write_unlock(&dndev_lock
);
623 static void dn_dev_check_default(struct net_device
*dev
)
625 write_lock(&dndev_lock
);
626 if (dev
== decnet_default_device
) {
627 decnet_default_device
= NULL
;
631 write_unlock(&dndev_lock
);
636 static struct dn_dev
*dn_dev_by_index(int ifindex
)
638 struct net_device
*dev
;
639 struct dn_dev
*dn_dev
= NULL
;
640 dev
= dev_get_by_index(ifindex
);
642 dn_dev
= dev
->dn_ptr
;
649 static int dn_dev_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
651 struct rtattr
**rta
= arg
;
652 struct dn_dev
*dn_db
;
653 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
654 struct dn_ifaddr
*ifa
, **ifap
;
656 if ((dn_db
= dn_dev_by_index(ifm
->ifa_index
)) == NULL
)
657 return -EADDRNOTAVAIL
;
659 for(ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
) {
660 void *tmp
= rta
[IFA_LOCAL
-1];
661 if ((tmp
&& memcmp(RTA_DATA(tmp
), &ifa
->ifa_local
, 2)) ||
662 (rta
[IFA_LABEL
-1] && rtattr_strcmp(rta
[IFA_LABEL
-1], ifa
->ifa_label
)))
665 dn_dev_del_ifa(dn_db
, ifap
, 1);
669 return -EADDRNOTAVAIL
;
672 static int dn_dev_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
674 struct rtattr
**rta
= arg
;
675 struct net_device
*dev
;
676 struct dn_dev
*dn_db
;
677 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
678 struct dn_ifaddr
*ifa
;
681 if (rta
[IFA_LOCAL
-1] == NULL
)
684 if ((dev
= __dev_get_by_index(ifm
->ifa_index
)) == NULL
)
687 if ((dn_db
= dev
->dn_ptr
) == NULL
) {
689 dn_db
= dn_dev_create(dev
, &err
);
694 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
697 if (!rta
[IFA_ADDRESS
- 1])
698 rta
[IFA_ADDRESS
- 1] = rta
[IFA_LOCAL
- 1];
699 memcpy(&ifa
->ifa_local
, RTA_DATA(rta
[IFA_LOCAL
-1]), 2);
700 memcpy(&ifa
->ifa_address
, RTA_DATA(rta
[IFA_ADDRESS
-1]), 2);
701 ifa
->ifa_flags
= ifm
->ifa_flags
;
702 ifa
->ifa_scope
= ifm
->ifa_scope
;
703 ifa
->ifa_dev
= dn_db
;
704 if (rta
[IFA_LABEL
-1])
705 rtattr_strlcpy(ifa
->ifa_label
, rta
[IFA_LABEL
-1], IFNAMSIZ
);
707 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
709 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
711 dn_dev_free_ifa(ifa
);
715 static int dn_dev_fill_ifaddr(struct sk_buff
*skb
, struct dn_ifaddr
*ifa
,
716 u32 pid
, u32 seq
, int event
, unsigned int flags
)
718 struct ifaddrmsg
*ifm
;
719 struct nlmsghdr
*nlh
;
720 unsigned char *b
= skb
->tail
;
722 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
723 ifm
= NLMSG_DATA(nlh
);
725 ifm
->ifa_family
= AF_DECnet
;
726 ifm
->ifa_prefixlen
= 16;
727 ifm
->ifa_flags
= ifa
->ifa_flags
| IFA_F_PERMANENT
;
728 ifm
->ifa_scope
= ifa
->ifa_scope
;
729 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
730 if (ifa
->ifa_address
)
731 RTA_PUT(skb
, IFA_ADDRESS
, 2, &ifa
->ifa_address
);
733 RTA_PUT(skb
, IFA_LOCAL
, 2, &ifa
->ifa_local
);
734 if (ifa
->ifa_label
[0])
735 RTA_PUT(skb
, IFA_LABEL
, IFNAMSIZ
, &ifa
->ifa_label
);
736 nlh
->nlmsg_len
= skb
->tail
- b
;
741 skb_trim(skb
, b
- skb
->data
);
745 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
)
748 int size
= NLMSG_SPACE(sizeof(struct ifaddrmsg
)+128);
750 skb
= alloc_skb(size
, GFP_KERNEL
);
752 netlink_set_err(rtnl
, 0, RTNLGRP_DECnet_IFADDR
, ENOBUFS
);
755 if (dn_dev_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0) < 0) {
757 netlink_set_err(rtnl
, 0, RTNLGRP_DECnet_IFADDR
, EINVAL
);
760 NETLINK_CB(skb
).dst_group
= RTNLGRP_DECnet_IFADDR
;
761 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_DECnet_IFADDR
, GFP_KERNEL
);
764 static int dn_dev_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
768 struct net_device
*dev
;
769 struct dn_dev
*dn_db
;
770 struct dn_ifaddr
*ifa
;
773 s_dn_idx
= dn_idx
= cb
->args
[1];
774 read_lock(&dev_base_lock
);
775 for(dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
780 if ((dn_db
= dev
->dn_ptr
) == NULL
)
783 for(ifa
= dn_db
->ifa_list
, dn_idx
= 0; ifa
; ifa
= ifa
->ifa_next
, dn_idx
++) {
784 if (dn_idx
< s_dn_idx
)
787 if (dn_dev_fill_ifaddr(skb
, ifa
,
788 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
, __le16
*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(__le16
*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
;
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
= (__le16
*)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
;
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 *((__le16
*)ptr
) = dn_htons(mtu2blksize(dev
));
956 *ptr
++ = dn_db
->parms
.priority
; /* Priority */
957 *ptr
++ = 0; /* Area: Reserved */
958 *((__le16
*)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
= (__le16
*)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 *((__le16
*)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
= kzalloc(sizeof(struct dn_dev
), GFP_ATOMIC
)) == NULL
)
1108 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 __le16 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_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
);
1217 neigh_ifdown(&dn_neigh_table
, dev
);
1220 neigh_release(dn_db
->router
);
1222 neigh_release(dn_db
->peer
);
1227 void dn_dev_down(struct net_device
*dev
)
1229 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1230 struct dn_ifaddr
*ifa
;
1235 while((ifa
= dn_db
->ifa_list
) != NULL
) {
1236 dn_dev_del_ifa(dn_db
, &dn_db
->ifa_list
, 0);
1237 dn_dev_free_ifa(ifa
);
1243 void dn_dev_init_pkt(struct sk_buff
*skb
)
1248 void dn_dev_veri_pkt(struct sk_buff
*skb
)
1253 void dn_dev_hello(struct sk_buff
*skb
)
1258 void dn_dev_devices_off(void)
1260 struct net_device
*dev
;
1263 for(dev
= dev_base
; dev
; dev
= dev
->next
)
1269 void dn_dev_devices_on(void)
1271 struct net_device
*dev
;
1274 for(dev
= dev_base
; dev
; dev
= dev
->next
) {
1275 if (dev
->flags
& IFF_UP
)
1281 int register_dnaddr_notifier(struct notifier_block
*nb
)
1283 return blocking_notifier_chain_register(&dnaddr_chain
, nb
);
1286 int unregister_dnaddr_notifier(struct notifier_block
*nb
)
1288 return blocking_notifier_chain_unregister(&dnaddr_chain
, nb
);
1291 #ifdef CONFIG_PROC_FS
1292 static inline struct net_device
*dn_dev_get_next(struct seq_file
*seq
, struct net_device
*dev
)
1296 } while(dev
&& !dev
->dn_ptr
);
1301 static struct net_device
*dn_dev_get_idx(struct seq_file
*seq
, loff_t pos
)
1303 struct net_device
*dev
;
1306 if (dev
&& !dev
->dn_ptr
)
1307 dev
= dn_dev_get_next(seq
, dev
);
1309 while(dev
&& (dev
= dn_dev_get_next(seq
, dev
)))
1315 static void *dn_dev_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1318 struct net_device
*dev
;
1319 read_lock(&dev_base_lock
);
1320 dev
= dn_dev_get_idx(seq
, *pos
- 1);
1322 read_unlock(&dev_base_lock
);
1325 return SEQ_START_TOKEN
;
1328 static void *dn_dev_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1330 struct net_device
*dev
= v
;
1333 if (v
== SEQ_START_TOKEN
) {
1334 dev
= dn_dev_seq_start(seq
, &one
);
1336 dev
= dn_dev_get_next(seq
, dev
);
1338 read_unlock(&dev_base_lock
);
1344 static void dn_dev_seq_stop(struct seq_file
*seq
, void *v
)
1346 if (v
&& v
!= SEQ_START_TOKEN
)
1347 read_unlock(&dev_base_lock
);
1350 static char *dn_type2asc(char type
)
1364 static int dn_dev_seq_show(struct seq_file
*seq
, void *v
)
1366 if (v
== SEQ_START_TOKEN
)
1367 seq_puts(seq
, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1369 struct net_device
*dev
= v
;
1370 char peer_buf
[DN_ASCBUF_LEN
];
1371 char router_buf
[DN_ASCBUF_LEN
];
1372 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1374 seq_printf(seq
, "%-8s %1s %04u %04u %04lu %04lu"
1375 " %04hu %03d %02x %-10s %-7s %-7s\n",
1376 dev
->name
? dev
->name
: "???",
1377 dn_type2asc(dn_db
->parms
.mode
),
1379 dn_db
->t3
, dn_db
->parms
.t3
,
1381 dn_db
->parms
.priority
,
1382 dn_db
->parms
.state
, dn_db
->parms
.name
,
1383 dn_db
->router
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->router
->primary_key
), router_buf
) : "",
1384 dn_db
->peer
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->peer
->primary_key
), peer_buf
) : "");
1389 static struct seq_operations dn_dev_seq_ops
= {
1390 .start
= dn_dev_seq_start
,
1391 .next
= dn_dev_seq_next
,
1392 .stop
= dn_dev_seq_stop
,
1393 .show
= dn_dev_seq_show
,
1396 static int dn_dev_seq_open(struct inode
*inode
, struct file
*file
)
1398 return seq_open(file
, &dn_dev_seq_ops
);
1401 static struct file_operations dn_dev_seq_fops
= {
1402 .owner
= THIS_MODULE
,
1403 .open
= dn_dev_seq_open
,
1405 .llseek
= seq_lseek
,
1406 .release
= seq_release
,
1409 #endif /* CONFIG_PROC_FS */
1411 static struct rtnetlink_link dnet_rtnetlink_table
[RTM_NR_MSGTYPES
] =
1413 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_newaddr
, },
1414 [RTM_DELADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_deladdr
, },
1415 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= dn_dev_dump_ifaddr
, },
1416 #ifdef CONFIG_DECNET_ROUTER
1417 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_newroute
, },
1418 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_delroute
, },
1419 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1420 .dumpit
= dn_fib_dump
, },
1421 [RTM_NEWRULE
- RTM_BASE
] = { .doit
= dn_fib_rtm_newrule
, },
1422 [RTM_DELRULE
- RTM_BASE
] = { .doit
= dn_fib_rtm_delrule
, },
1423 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= dn_fib_dump_rules
, },
1425 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1426 .dumpit
= dn_cache_dump
, },
1431 static int __initdata addr
[2];
1432 module_param_array(addr
, int, NULL
, 0444);
1433 MODULE_PARM_DESC(addr
, "The DECnet address of this machine: area,node");
1435 void __init
dn_dev_init(void)
1437 if (addr
[0] > 63 || addr
[0] < 0) {
1438 printk(KERN_ERR
"DECnet: Area must be between 0 and 63");
1442 if (addr
[1] > 1023 || addr
[1] < 0) {
1443 printk(KERN_ERR
"DECnet: Node must be between 0 and 1023");
1447 decnet_address
= dn_htons((addr
[0] << 10) | addr
[1]);
1449 dn_dev_devices_on();
1451 rtnetlink_links
[PF_DECnet
] = dnet_rtnetlink_table
;
1453 proc_net_fops_create("decnet_dev", S_IRUGO
, &dn_dev_seq_fops
);
1455 #ifdef CONFIG_SYSCTL
1458 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1459 dn_dev_sysctl_register(NULL
, &dn_dev_list
[i
]);
1461 #endif /* CONFIG_SYSCTL */
1464 void __exit
dn_dev_cleanup(void)
1466 rtnetlink_links
[PF_DECnet
] = NULL
;
1468 #ifdef CONFIG_SYSCTL
1471 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1472 dn_dev_sysctl_unregister(&dn_dev_list
[i
]);
1474 #endif /* CONFIG_SYSCTL */
1476 proc_net_remove("decnet_dev");
1478 dn_dev_devices_off();