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>
49 #include <net/fib_rules.h>
51 #include <net/dn_dev.h>
52 #include <net/dn_route.h>
53 #include <net/dn_neigh.h>
54 #include <net/dn_fib.h>
56 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
58 static char dn_rt_all_end_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x04,0x00,0x00};
59 static char dn_rt_all_rt_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x03,0x00,0x00};
60 static char dn_hiord
[ETH_ALEN
] = {0xAA,0x00,0x04,0x00,0x00,0x00};
61 static unsigned char dn_eco_version
[3] = {0x02,0x00,0x00};
63 extern struct neigh_table dn_neigh_table
;
66 * decnet_address is kept in network order.
68 __le16 decnet_address
= 0;
70 static DEFINE_RWLOCK(dndev_lock
);
71 static struct net_device
*decnet_default_device
;
72 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain
);
74 static struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
);
75 static void dn_dev_delete(struct net_device
*dev
);
76 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
);
78 static int dn_eth_up(struct net_device
*);
79 static void dn_eth_down(struct net_device
*);
80 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
81 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
83 static struct dn_dev_parms dn_dev_list
[] = {
85 .type
= ARPHRD_ETHER
, /* Ethernet */
91 .ctl_name
= NET_DECNET_CONF_ETHER
,
94 .timer3
= dn_send_brd_hello
,
97 .type
= ARPHRD_IPGRE
, /* DECnet tunneled over GRE in IP */
103 .ctl_name
= NET_DECNET_CONF_GRE
,
104 .timer3
= dn_send_brd_hello
,
108 .type
= ARPHRD_X25
, /* Bog standard X.25 */
109 .mode
= DN_DEV_UCAST
,
110 .state
= DN_DEV_S_DS
,
114 .ctl_name
= NET_DECNET_CONF_X25
,
115 .timer3
= dn_send_ptp_hello
,
120 .type
= ARPHRD_PPP
, /* DECnet over PPP */
121 .mode
= DN_DEV_BCAST
,
122 .state
= DN_DEV_S_RU
,
126 .ctl_name
= NET_DECNET_CONF_PPP
,
127 .timer3
= dn_send_brd_hello
,
131 .type
= ARPHRD_DDCMP
, /* DECnet over DDCMP */
132 .mode
= DN_DEV_UCAST
,
133 .state
= DN_DEV_S_DS
,
137 .ctl_name
= NET_DECNET_CONF_DDCMP
,
138 .timer3
= dn_send_ptp_hello
,
141 .type
= ARPHRD_LOOPBACK
, /* Loopback interface - always last */
142 .mode
= DN_DEV_BCAST
,
143 .state
= DN_DEV_S_RU
,
147 .ctl_name
= NET_DECNET_CONF_LOOPBACK
,
148 .timer3
= dn_send_brd_hello
,
152 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
154 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
158 static int min_t2
[] = { 1 };
159 static int max_t2
[] = { 60 }; /* No max specified, but this seems sensible */
160 static int min_t3
[] = { 1 };
161 static int max_t3
[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
163 static int min_priority
[1];
164 static int max_priority
[] = { 127 }; /* From DECnet spec */
166 static int dn_forwarding_proc(ctl_table
*, int, struct file
*,
167 void __user
*, size_t *, loff_t
*);
168 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
169 void __user
*oldval
, size_t __user
*oldlenp
,
170 void __user
*newval
, size_t newlen
,
173 static struct dn_dev_sysctl_table
{
174 struct ctl_table_header
*sysctl_header
;
175 ctl_table dn_dev_vars
[5];
176 ctl_table dn_dev_dev
[2];
177 ctl_table dn_dev_conf_dir
[2];
178 ctl_table dn_dev_proto_dir
[2];
179 ctl_table dn_dev_root_dir
[2];
184 .ctl_name
= NET_DECNET_CONF_DEV_FORWARDING
,
185 .procname
= "forwarding",
186 .data
= (void *)DN_DEV_PARMS_OFFSET(forwarding
),
187 .maxlen
= sizeof(int),
189 .proc_handler
= dn_forwarding_proc
,
190 .strategy
= dn_forwarding_sysctl
,
193 .ctl_name
= NET_DECNET_CONF_DEV_PRIORITY
,
194 .procname
= "priority",
195 .data
= (void *)DN_DEV_PARMS_OFFSET(priority
),
196 .maxlen
= sizeof(int),
198 .proc_handler
= proc_dointvec_minmax
,
199 .strategy
= sysctl_intvec
,
200 .extra1
= &min_priority
,
201 .extra2
= &max_priority
204 .ctl_name
= NET_DECNET_CONF_DEV_T2
,
206 .data
= (void *)DN_DEV_PARMS_OFFSET(t2
),
207 .maxlen
= sizeof(int),
209 .proc_handler
= proc_dointvec_minmax
,
210 .strategy
= sysctl_intvec
,
215 .ctl_name
= NET_DECNET_CONF_DEV_T3
,
217 .data
= (void *)DN_DEV_PARMS_OFFSET(t3
),
218 .maxlen
= sizeof(int),
220 .proc_handler
= proc_dointvec_minmax
,
221 .strategy
= sysctl_intvec
,
231 .child
= dn_dev_sysctl
.dn_dev_vars
234 .ctl_name
= NET_DECNET_CONF
,
237 .child
= dn_dev_sysctl
.dn_dev_dev
240 .ctl_name
= NET_DECNET
,
241 .procname
= "decnet",
243 .child
= dn_dev_sysctl
.dn_dev_conf_dir
249 .child
= dn_dev_sysctl
.dn_dev_proto_dir
253 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
255 struct dn_dev_sysctl_table
*t
;
258 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
262 memcpy(t
, &dn_dev_sysctl
, sizeof(*t
));
264 for(i
= 0; i
< ARRAY_SIZE(t
->dn_dev_vars
) - 1; i
++) {
265 long offset
= (long)t
->dn_dev_vars
[i
].data
;
266 t
->dn_dev_vars
[i
].data
= ((char *)parms
) + offset
;
267 t
->dn_dev_vars
[i
].de
= NULL
;
271 t
->dn_dev_dev
[0].procname
= dev
->name
;
272 t
->dn_dev_dev
[0].ctl_name
= dev
->ifindex
;
274 t
->dn_dev_dev
[0].procname
= parms
->name
;
275 t
->dn_dev_dev
[0].ctl_name
= parms
->ctl_name
;
278 t
->dn_dev_dev
[0].child
= t
->dn_dev_vars
;
279 t
->dn_dev_dev
[0].de
= NULL
;
280 t
->dn_dev_conf_dir
[0].child
= t
->dn_dev_dev
;
281 t
->dn_dev_conf_dir
[0].de
= NULL
;
282 t
->dn_dev_proto_dir
[0].child
= t
->dn_dev_conf_dir
;
283 t
->dn_dev_proto_dir
[0].de
= NULL
;
284 t
->dn_dev_root_dir
[0].child
= t
->dn_dev_proto_dir
;
285 t
->dn_dev_root_dir
[0].de
= NULL
;
286 t
->dn_dev_vars
[0].extra1
= (void *)dev
;
288 t
->sysctl_header
= register_sysctl_table(t
->dn_dev_root_dir
, 0);
289 if (t
->sysctl_header
== NULL
)
295 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
298 struct dn_dev_sysctl_table
*t
= parms
->sysctl
;
299 parms
->sysctl
= NULL
;
300 unregister_sysctl_table(t
->sysctl_header
);
305 static int dn_forwarding_proc(ctl_table
*table
, int write
,
308 size_t *lenp
, loff_t
*ppos
)
310 #ifdef CONFIG_DECNET_ROUTER
311 struct net_device
*dev
= table
->extra1
;
312 struct dn_dev
*dn_db
;
316 if (table
->extra1
== NULL
)
320 old
= dn_db
->parms
.forwarding
;
322 err
= proc_dointvec(table
, write
, filep
, buffer
, lenp
, ppos
);
324 if ((err
>= 0) && write
) {
325 if (dn_db
->parms
.forwarding
< 0)
326 dn_db
->parms
.forwarding
= 0;
327 if (dn_db
->parms
.forwarding
> 2)
328 dn_db
->parms
.forwarding
= 2;
330 * What an ugly hack this is... its works, just. It
331 * would be nice if sysctl/proc were just that little
332 * bit more flexible so I don't have to write a special
333 * routine, or suffer hacks like this - SJW
335 tmp
= dn_db
->parms
.forwarding
;
336 dn_db
->parms
.forwarding
= old
;
337 if (dn_db
->parms
.down
)
338 dn_db
->parms
.down(dev
);
339 dn_db
->parms
.forwarding
= tmp
;
341 dn_db
->parms
.up(dev
);
350 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
351 void __user
*oldval
, size_t __user
*oldlenp
,
352 void __user
*newval
, size_t newlen
,
355 #ifdef CONFIG_DECNET_ROUTER
356 struct net_device
*dev
= table
->extra1
;
357 struct dn_dev
*dn_db
;
360 if (table
->extra1
== NULL
)
365 if (newval
&& newlen
) {
366 if (newlen
!= sizeof(int))
369 if (get_user(value
, (int __user
*)newval
))
376 if (dn_db
->parms
.down
)
377 dn_db
->parms
.down(dev
);
378 dn_db
->parms
.forwarding
= value
;
380 dn_db
->parms
.up(dev
);
389 #else /* CONFIG_SYSCTL */
390 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
393 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
397 #endif /* CONFIG_SYSCTL */
399 static inline __u16
mtu2blksize(struct net_device
*dev
)
401 u32 blksize
= dev
->mtu
;
402 if (blksize
> 0xffff)
405 if (dev
->type
== ARPHRD_ETHER
||
406 dev
->type
== ARPHRD_PPP
||
407 dev
->type
== ARPHRD_IPGRE
||
408 dev
->type
== ARPHRD_LOOPBACK
)
411 return (__u16
)blksize
;
414 static struct dn_ifaddr
*dn_dev_alloc_ifa(void)
416 struct dn_ifaddr
*ifa
;
418 ifa
= kzalloc(sizeof(*ifa
), GFP_KERNEL
);
423 static __inline__
void dn_dev_free_ifa(struct dn_ifaddr
*ifa
)
428 static void dn_dev_del_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
**ifap
, int destroy
)
430 struct dn_ifaddr
*ifa1
= *ifap
;
431 unsigned char mac_addr
[6];
432 struct net_device
*dev
= dn_db
->dev
;
436 *ifap
= ifa1
->ifa_next
;
438 if (dn_db
->dev
->type
== ARPHRD_ETHER
) {
439 if (ifa1
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
440 dn_dn2eth(mac_addr
, ifa1
->ifa_local
);
441 dev_mc_delete(dev
, mac_addr
, ETH_ALEN
, 0);
445 rtmsg_ifa(RTM_DELADDR
, ifa1
);
446 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_DOWN
, ifa1
);
448 dn_dev_free_ifa(ifa1
);
450 if (dn_db
->ifa_list
== NULL
)
451 dn_dev_delete(dn_db
->dev
);
455 static int dn_dev_insert_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
457 struct net_device
*dev
= dn_db
->dev
;
458 struct dn_ifaddr
*ifa1
;
459 unsigned char mac_addr
[6];
463 /* Check for duplicates */
464 for(ifa1
= dn_db
->ifa_list
; ifa1
; ifa1
= ifa1
->ifa_next
) {
465 if (ifa1
->ifa_local
== ifa
->ifa_local
)
469 if (dev
->type
== ARPHRD_ETHER
) {
470 if (ifa
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
471 dn_dn2eth(mac_addr
, ifa
->ifa_local
);
472 dev_mc_add(dev
, mac_addr
, ETH_ALEN
, 0);
477 ifa
->ifa_next
= dn_db
->ifa_list
;
478 dn_db
->ifa_list
= ifa
;
480 rtmsg_ifa(RTM_NEWADDR
, ifa
);
481 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_UP
, ifa
);
486 static int dn_dev_set_ifa(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
488 struct dn_dev
*dn_db
= dev
->dn_ptr
;
493 dn_db
= dn_dev_create(dev
, &err
);
498 ifa
->ifa_dev
= dn_db
;
500 if (dev
->flags
& IFF_LOOPBACK
)
501 ifa
->ifa_scope
= RT_SCOPE_HOST
;
503 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
505 dn_dev_free_ifa(ifa
);
510 int dn_dev_ioctl(unsigned int cmd
, void __user
*arg
)
512 char buffer
[DN_IFREQ_SIZE
];
513 struct ifreq
*ifr
= (struct ifreq
*)buffer
;
514 struct sockaddr_dn
*sdn
= (struct sockaddr_dn
*)&ifr
->ifr_addr
;
515 struct dn_dev
*dn_db
;
516 struct net_device
*dev
;
517 struct dn_ifaddr
*ifa
= NULL
, **ifap
= NULL
;
520 if (copy_from_user(ifr
, arg
, DN_IFREQ_SIZE
))
522 ifr
->ifr_name
[IFNAMSIZ
-1] = 0;
525 dev_load(ifr
->ifr_name
);
532 if (!capable(CAP_NET_ADMIN
))
534 if (sdn
->sdn_family
!= AF_DECnet
)
543 if ((dev
= __dev_get_by_name(ifr
->ifr_name
)) == NULL
) {
548 if ((dn_db
= dev
->dn_ptr
) != NULL
) {
549 for (ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
)
550 if (strcmp(ifr
->ifr_name
, ifa
->ifa_label
) == 0)
554 if (ifa
== NULL
&& cmd
!= SIOCSIFADDR
) {
555 ret
= -EADDRNOTAVAIL
;
561 *((__le16
*)sdn
->sdn_nodeaddr
) = ifa
->ifa_local
;
566 if ((ifa
= dn_dev_alloc_ifa()) == NULL
) {
570 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
572 if (ifa
->ifa_local
== dn_saddr2dn(sdn
))
574 dn_dev_del_ifa(dn_db
, ifap
, 0);
577 ifa
->ifa_local
= ifa
->ifa_address
= dn_saddr2dn(sdn
);
579 ret
= dn_dev_set_ifa(dev
, ifa
);
586 if (copy_to_user(arg
, ifr
, DN_IFREQ_SIZE
))
591 struct net_device
*dn_dev_get_default(void)
593 struct net_device
*dev
;
594 read_lock(&dndev_lock
);
595 dev
= decnet_default_device
;
602 read_unlock(&dndev_lock
);
606 int dn_dev_set_default(struct net_device
*dev
, int force
)
608 struct net_device
*old
= NULL
;
612 write_lock(&dndev_lock
);
613 if (force
|| decnet_default_device
== NULL
) {
614 old
= decnet_default_device
;
615 decnet_default_device
= dev
;
618 write_unlock(&dndev_lock
);
624 static void dn_dev_check_default(struct net_device
*dev
)
626 write_lock(&dndev_lock
);
627 if (dev
== decnet_default_device
) {
628 decnet_default_device
= NULL
;
632 write_unlock(&dndev_lock
);
637 static struct dn_dev
*dn_dev_by_index(int ifindex
)
639 struct net_device
*dev
;
640 struct dn_dev
*dn_dev
= NULL
;
641 dev
= dev_get_by_index(ifindex
);
643 dn_dev
= dev
->dn_ptr
;
650 static int dn_dev_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
652 struct rtattr
**rta
= arg
;
653 struct dn_dev
*dn_db
;
654 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
655 struct dn_ifaddr
*ifa
, **ifap
;
657 if ((dn_db
= dn_dev_by_index(ifm
->ifa_index
)) == NULL
)
658 return -EADDRNOTAVAIL
;
660 for(ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
) {
661 void *tmp
= rta
[IFA_LOCAL
-1];
662 if ((tmp
&& memcmp(RTA_DATA(tmp
), &ifa
->ifa_local
, 2)) ||
663 (rta
[IFA_LABEL
-1] && rtattr_strcmp(rta
[IFA_LABEL
-1], ifa
->ifa_label
)))
666 dn_dev_del_ifa(dn_db
, ifap
, 1);
670 return -EADDRNOTAVAIL
;
673 static int dn_dev_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
675 struct rtattr
**rta
= arg
;
676 struct net_device
*dev
;
677 struct dn_dev
*dn_db
;
678 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
679 struct dn_ifaddr
*ifa
;
682 if (rta
[IFA_LOCAL
-1] == NULL
)
685 if ((dev
= __dev_get_by_index(ifm
->ifa_index
)) == NULL
)
688 if ((dn_db
= dev
->dn_ptr
) == NULL
) {
690 dn_db
= dn_dev_create(dev
, &err
);
695 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
698 if (!rta
[IFA_ADDRESS
- 1])
699 rta
[IFA_ADDRESS
- 1] = rta
[IFA_LOCAL
- 1];
700 memcpy(&ifa
->ifa_local
, RTA_DATA(rta
[IFA_LOCAL
-1]), 2);
701 memcpy(&ifa
->ifa_address
, RTA_DATA(rta
[IFA_ADDRESS
-1]), 2);
702 ifa
->ifa_flags
= ifm
->ifa_flags
;
703 ifa
->ifa_scope
= ifm
->ifa_scope
;
704 ifa
->ifa_dev
= dn_db
;
705 if (rta
[IFA_LABEL
-1])
706 rtattr_strlcpy(ifa
->ifa_label
, rta
[IFA_LABEL
-1], IFNAMSIZ
);
708 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
710 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
712 dn_dev_free_ifa(ifa
);
716 static int dn_dev_fill_ifaddr(struct sk_buff
*skb
, struct dn_ifaddr
*ifa
,
717 u32 pid
, u32 seq
, int event
, unsigned int flags
)
719 struct ifaddrmsg
*ifm
;
720 struct nlmsghdr
*nlh
;
721 unsigned char *b
= skb
->tail
;
723 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
724 ifm
= NLMSG_DATA(nlh
);
726 ifm
->ifa_family
= AF_DECnet
;
727 ifm
->ifa_prefixlen
= 16;
728 ifm
->ifa_flags
= ifa
->ifa_flags
| IFA_F_PERMANENT
;
729 ifm
->ifa_scope
= ifa
->ifa_scope
;
730 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
731 if (ifa
->ifa_address
)
732 RTA_PUT(skb
, IFA_ADDRESS
, 2, &ifa
->ifa_address
);
734 RTA_PUT(skb
, IFA_LOCAL
, 2, &ifa
->ifa_local
);
735 if (ifa
->ifa_label
[0])
736 RTA_PUT(skb
, IFA_LABEL
, IFNAMSIZ
, &ifa
->ifa_label
);
737 nlh
->nlmsg_len
= skb
->tail
- b
;
742 skb_trim(skb
, b
- skb
->data
);
746 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
)
749 int payload
= sizeof(struct ifaddrmsg
) + 128;
752 skb
= alloc_skb(nlmsg_total_size(payload
), GFP_KERNEL
);
756 err
= dn_dev_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
762 err
= rtnl_notify(skb
, 0, RTNLGRP_DECnet_IFADDR
, NULL
, GFP_KERNEL
);
765 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR
, err
);
768 static int dn_dev_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
772 struct net_device
*dev
;
773 struct dn_dev
*dn_db
;
774 struct dn_ifaddr
*ifa
;
777 s_dn_idx
= dn_idx
= cb
->args
[1];
778 read_lock(&dev_base_lock
);
779 for(dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
784 if ((dn_db
= dev
->dn_ptr
) == NULL
)
787 for(ifa
= dn_db
->ifa_list
, dn_idx
= 0; ifa
; ifa
= ifa
->ifa_next
, dn_idx
++) {
788 if (dn_idx
< s_dn_idx
)
791 if (dn_dev_fill_ifaddr(skb
, ifa
,
792 NETLINK_CB(cb
->skb
).pid
,
800 read_unlock(&dev_base_lock
);
802 cb
->args
[1] = dn_idx
;
807 static int dn_dev_get_first(struct net_device
*dev
, __le16
*addr
)
809 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
810 struct dn_ifaddr
*ifa
;
814 ifa
= dn_db
->ifa_list
;
816 *addr
= ifa
->ifa_local
;
824 * Find a default address to bind to.
826 * This is one of those areas where the initial VMS concepts don't really
827 * map onto the Linux concepts, and since we introduced multiple addresses
828 * per interface we have to cope with slightly odd ways of finding out what
829 * "our address" really is. Mostly it's not a problem; for this we just guess
830 * a sensible default. Eventually the routing code will take care of all the
831 * nasties for us I hope.
833 int dn_dev_bind_default(__le16
*addr
)
835 struct net_device
*dev
;
837 dev
= dn_dev_get_default();
840 read_lock(&dev_base_lock
);
841 rv
= dn_dev_get_first(dev
, addr
);
842 read_unlock(&dev_base_lock
);
844 if (rv
== 0 || dev
== &loopback_dev
)
852 static void dn_send_endnode_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
854 struct endnode_hello_message
*msg
;
855 struct sk_buff
*skb
= NULL
;
857 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
859 if ((skb
= dn_alloc_skb(NULL
, sizeof(*msg
), GFP_ATOMIC
)) == NULL
)
864 msg
= (struct endnode_hello_message
*)skb_put(skb
,sizeof(*msg
));
867 memcpy(msg
->tiver
, dn_eco_version
, 3);
868 dn_dn2eth(msg
->id
, ifa
->ifa_local
);
869 msg
->iinfo
= DN_RT_INFO_ENDN
;
870 msg
->blksize
= dn_htons(mtu2blksize(dev
));
872 memset(msg
->seed
, 0, 8);
873 memcpy(msg
->neighbor
, dn_hiord
, ETH_ALEN
);
876 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
877 dn_dn2eth(msg
->neighbor
, dn
->addr
);
880 msg
->timer
= dn_htons((unsigned short)dn_db
->parms
.t3
);
883 memset(msg
->data
, 0xAA, 2);
885 pktlen
= (__le16
*)skb_push(skb
,2);
886 *pktlen
= dn_htons(skb
->len
- 2);
888 skb
->nh
.raw
= skb
->data
;
890 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, msg
->id
);
894 #define DRDELAY (5 * HZ)
896 static int dn_am_i_a_router(struct dn_neigh
*dn
, struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
898 /* First check time since device went up */
899 if ((jiffies
- dn_db
->uptime
) < DRDELAY
)
902 /* If there is no router, then yes... */
906 /* otherwise only if we have a higher priority or.. */
907 if (dn
->priority
< dn_db
->parms
.priority
)
910 /* if we have equal priority and a higher node number */
911 if (dn
->priority
!= dn_db
->parms
.priority
)
914 if (dn_ntohs(dn
->addr
) < dn_ntohs(ifa
->ifa_local
))
920 static void dn_send_router_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
923 struct dn_dev
*dn_db
= dev
->dn_ptr
;
924 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
928 unsigned char *i1
, *i2
;
932 if (mtu2blksize(dev
) < (26 + 7))
935 n
= mtu2blksize(dev
) - 26;
941 size
= 2 + 26 + 7 * n
;
943 if ((skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
)) == NULL
)
947 ptr
= skb_put(skb
, size
);
949 *ptr
++ = DN_RT_PKT_CNTL
| DN_RT_PKT_ERTH
;
950 *ptr
++ = 2; /* ECO */
953 dn_dn2eth(ptr
, ifa
->ifa_local
);
956 *ptr
++ = dn_db
->parms
.forwarding
== 1 ?
957 DN_RT_INFO_L1RT
: DN_RT_INFO_L2RT
;
958 *((__le16
*)ptr
) = dn_htons(mtu2blksize(dev
));
960 *ptr
++ = dn_db
->parms
.priority
; /* Priority */
961 *ptr
++ = 0; /* Area: Reserved */
962 *((__le16
*)ptr
) = dn_htons((unsigned short)dn_db
->parms
.t3
);
964 *ptr
++ = 0; /* MPD: Reserved */
966 memset(ptr
, 0, 7); /* Name: Reserved */
970 n
= dn_neigh_elist(dev
, ptr
, n
);
975 skb_trim(skb
, (27 + *i2
));
977 pktlen
= (__le16
*)skb_push(skb
, 2);
978 *pktlen
= dn_htons(skb
->len
- 2);
980 skb
->nh
.raw
= skb
->data
;
982 if (dn_am_i_a_router(dn
, dn_db
, ifa
)) {
983 struct sk_buff
*skb2
= skb_copy(skb
, GFP_ATOMIC
);
985 dn_rt_finish_output(skb2
, dn_rt_all_end_mcast
, src
);
989 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
992 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
994 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
996 if (dn_db
->parms
.forwarding
== 0)
997 dn_send_endnode_hello(dev
, ifa
);
999 dn_send_router_hello(dev
, ifa
);
1002 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1005 int size
= dev
->hard_header_len
+ 2 + 4 + tdlen
;
1006 struct sk_buff
*skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
);
1015 skb_push(skb
, dev
->hard_header_len
);
1016 ptr
= skb_put(skb
, 2 + 4 + tdlen
);
1018 *ptr
++ = DN_RT_PKT_HELO
;
1019 *((__le16
*)ptr
) = ifa
->ifa_local
;
1023 for(i
= 0; i
< tdlen
; i
++)
1026 dn_dn2eth(src
, ifa
->ifa_local
);
1027 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1030 static int dn_eth_up(struct net_device
*dev
)
1032 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1034 if (dn_db
->parms
.forwarding
== 0)
1035 dev_mc_add(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1037 dev_mc_add(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1041 dn_db
->use_long
= 1;
1046 static void dn_eth_down(struct net_device
*dev
)
1048 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1050 if (dn_db
->parms
.forwarding
== 0)
1051 dev_mc_delete(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1053 dev_mc_delete(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1056 static void dn_dev_set_timer(struct net_device
*dev
);
1058 static void dn_dev_timer_func(unsigned long arg
)
1060 struct net_device
*dev
= (struct net_device
*)arg
;
1061 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1062 struct dn_ifaddr
*ifa
;
1064 if (dn_db
->t3
<= dn_db
->parms
.t2
) {
1065 if (dn_db
->parms
.timer3
) {
1066 for(ifa
= dn_db
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1067 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
1068 dn_db
->parms
.timer3(dev
, ifa
);
1071 dn_db
->t3
= dn_db
->parms
.t3
;
1073 dn_db
->t3
-= dn_db
->parms
.t2
;
1076 dn_dev_set_timer(dev
);
1079 static void dn_dev_set_timer(struct net_device
*dev
)
1081 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1083 if (dn_db
->parms
.t2
> dn_db
->parms
.t3
)
1084 dn_db
->parms
.t2
= dn_db
->parms
.t3
;
1086 dn_db
->timer
.data
= (unsigned long)dev
;
1087 dn_db
->timer
.function
= dn_dev_timer_func
;
1088 dn_db
->timer
.expires
= jiffies
+ (dn_db
->parms
.t2
* HZ
);
1090 add_timer(&dn_db
->timer
);
1093 struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
)
1096 struct dn_dev_parms
*p
= dn_dev_list
;
1097 struct dn_dev
*dn_db
;
1099 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++, p
++) {
1100 if (p
->type
== dev
->type
)
1105 if (i
== DN_DEV_LIST_SIZE
)
1109 if ((dn_db
= kzalloc(sizeof(struct dn_dev
), GFP_ATOMIC
)) == NULL
)
1112 memcpy(&dn_db
->parms
, p
, sizeof(struct dn_dev_parms
));
1114 dev
->dn_ptr
= dn_db
;
1116 init_timer(&dn_db
->timer
);
1118 dn_db
->uptime
= jiffies
;
1119 if (dn_db
->parms
.up
) {
1120 if (dn_db
->parms
.up(dev
) < 0) {
1127 dn_db
->neigh_parms
= neigh_parms_alloc(dev
, &dn_neigh_table
);
1129 dn_dev_sysctl_register(dev
, &dn_db
->parms
);
1131 dn_dev_set_timer(dev
);
1139 * This processes a device up event. We only start up
1140 * the loopback device & ethernet devices with correct
1141 * MAC addreses automatically. Others must be started
1144 * FIXME: How should we configure the loopback address ? If we could dispense
1145 * with using decnet_address here and for autobind, it will be one less thing
1146 * for users to worry about setting up.
1149 void dn_dev_up(struct net_device
*dev
)
1151 struct dn_ifaddr
*ifa
;
1152 __le16 addr
= decnet_address
;
1153 int maybe_default
= 0;
1154 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1156 if ((dev
->type
!= ARPHRD_ETHER
) && (dev
->type
!= ARPHRD_LOOPBACK
))
1160 * Need to ensure that loopback device has a dn_db attached to it
1161 * to allow creation of neighbours against it, even though it might
1162 * not have a local address of its own. Might as well do the same for
1163 * all autoconfigured interfaces.
1165 if (dn_db
== NULL
) {
1167 dn_db
= dn_dev_create(dev
, &err
);
1172 if (dev
->type
== ARPHRD_ETHER
) {
1173 if (memcmp(dev
->dev_addr
, dn_hiord
, 4) != 0)
1175 addr
= dn_eth2dn(dev
->dev_addr
);
1182 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
1185 ifa
->ifa_local
= ifa
->ifa_address
= addr
;
1187 ifa
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1188 strcpy(ifa
->ifa_label
, dev
->name
);
1190 dn_dev_set_ifa(dev
, ifa
);
1193 * Automagically set the default device to the first automatically
1194 * configured ethernet card in the system.
1196 if (maybe_default
) {
1198 if (dn_dev_set_default(dev
, 0))
1203 static void dn_dev_delete(struct net_device
*dev
)
1205 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1210 del_timer_sync(&dn_db
->timer
);
1211 dn_dev_sysctl_unregister(&dn_db
->parms
);
1212 dn_dev_check_default(dev
);
1213 neigh_ifdown(&dn_neigh_table
, dev
);
1215 if (dn_db
->parms
.down
)
1216 dn_db
->parms
.down(dev
);
1220 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1221 neigh_ifdown(&dn_neigh_table
, dev
);
1224 neigh_release(dn_db
->router
);
1226 neigh_release(dn_db
->peer
);
1231 void dn_dev_down(struct net_device
*dev
)
1233 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1234 struct dn_ifaddr
*ifa
;
1239 while((ifa
= dn_db
->ifa_list
) != NULL
) {
1240 dn_dev_del_ifa(dn_db
, &dn_db
->ifa_list
, 0);
1241 dn_dev_free_ifa(ifa
);
1247 void dn_dev_init_pkt(struct sk_buff
*skb
)
1252 void dn_dev_veri_pkt(struct sk_buff
*skb
)
1257 void dn_dev_hello(struct sk_buff
*skb
)
1262 void dn_dev_devices_off(void)
1264 struct net_device
*dev
;
1267 for(dev
= dev_base
; dev
; dev
= dev
->next
)
1273 void dn_dev_devices_on(void)
1275 struct net_device
*dev
;
1278 for(dev
= dev_base
; dev
; dev
= dev
->next
) {
1279 if (dev
->flags
& IFF_UP
)
1285 int register_dnaddr_notifier(struct notifier_block
*nb
)
1287 return blocking_notifier_chain_register(&dnaddr_chain
, nb
);
1290 int unregister_dnaddr_notifier(struct notifier_block
*nb
)
1292 return blocking_notifier_chain_unregister(&dnaddr_chain
, nb
);
1295 #ifdef CONFIG_PROC_FS
1296 static inline struct net_device
*dn_dev_get_next(struct seq_file
*seq
, struct net_device
*dev
)
1300 } while(dev
&& !dev
->dn_ptr
);
1305 static struct net_device
*dn_dev_get_idx(struct seq_file
*seq
, loff_t pos
)
1307 struct net_device
*dev
;
1310 if (dev
&& !dev
->dn_ptr
)
1311 dev
= dn_dev_get_next(seq
, dev
);
1313 while(dev
&& (dev
= dn_dev_get_next(seq
, dev
)))
1319 static void *dn_dev_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1322 struct net_device
*dev
;
1323 read_lock(&dev_base_lock
);
1324 dev
= dn_dev_get_idx(seq
, *pos
- 1);
1326 read_unlock(&dev_base_lock
);
1329 return SEQ_START_TOKEN
;
1332 static void *dn_dev_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1334 struct net_device
*dev
= v
;
1337 if (v
== SEQ_START_TOKEN
) {
1338 dev
= dn_dev_seq_start(seq
, &one
);
1340 dev
= dn_dev_get_next(seq
, dev
);
1342 read_unlock(&dev_base_lock
);
1348 static void dn_dev_seq_stop(struct seq_file
*seq
, void *v
)
1350 if (v
&& v
!= SEQ_START_TOKEN
)
1351 read_unlock(&dev_base_lock
);
1354 static char *dn_type2asc(char type
)
1368 static int dn_dev_seq_show(struct seq_file
*seq
, void *v
)
1370 if (v
== SEQ_START_TOKEN
)
1371 seq_puts(seq
, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1373 struct net_device
*dev
= v
;
1374 char peer_buf
[DN_ASCBUF_LEN
];
1375 char router_buf
[DN_ASCBUF_LEN
];
1376 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1378 seq_printf(seq
, "%-8s %1s %04u %04u %04lu %04lu"
1379 " %04hu %03d %02x %-10s %-7s %-7s\n",
1380 dev
->name
? dev
->name
: "???",
1381 dn_type2asc(dn_db
->parms
.mode
),
1383 dn_db
->t3
, dn_db
->parms
.t3
,
1385 dn_db
->parms
.priority
,
1386 dn_db
->parms
.state
, dn_db
->parms
.name
,
1387 dn_db
->router
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->router
->primary_key
), router_buf
) : "",
1388 dn_db
->peer
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->peer
->primary_key
), peer_buf
) : "");
1393 static struct seq_operations dn_dev_seq_ops
= {
1394 .start
= dn_dev_seq_start
,
1395 .next
= dn_dev_seq_next
,
1396 .stop
= dn_dev_seq_stop
,
1397 .show
= dn_dev_seq_show
,
1400 static int dn_dev_seq_open(struct inode
*inode
, struct file
*file
)
1402 return seq_open(file
, &dn_dev_seq_ops
);
1405 static struct file_operations dn_dev_seq_fops
= {
1406 .owner
= THIS_MODULE
,
1407 .open
= dn_dev_seq_open
,
1409 .llseek
= seq_lseek
,
1410 .release
= seq_release
,
1413 #endif /* CONFIG_PROC_FS */
1415 static struct rtnetlink_link dnet_rtnetlink_table
[RTM_NR_MSGTYPES
] =
1417 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_newaddr
, },
1418 [RTM_DELADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_deladdr
, },
1419 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= dn_dev_dump_ifaddr
, },
1420 #ifdef CONFIG_DECNET_ROUTER
1421 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_newroute
, },
1422 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_delroute
, },
1423 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1424 .dumpit
= dn_fib_dump
, },
1425 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= dn_fib_dump_rules
, },
1427 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1428 .dumpit
= dn_cache_dump
, },
1433 static int __initdata addr
[2];
1434 module_param_array(addr
, int, NULL
, 0444);
1435 MODULE_PARM_DESC(addr
, "The DECnet address of this machine: area,node");
1437 void __init
dn_dev_init(void)
1439 if (addr
[0] > 63 || addr
[0] < 0) {
1440 printk(KERN_ERR
"DECnet: Area must be between 0 and 63");
1444 if (addr
[1] > 1023 || addr
[1] < 0) {
1445 printk(KERN_ERR
"DECnet: Node must be between 0 and 1023");
1449 decnet_address
= dn_htons((addr
[0] << 10) | addr
[1]);
1451 dn_dev_devices_on();
1453 rtnetlink_links
[PF_DECnet
] = dnet_rtnetlink_table
;
1455 proc_net_fops_create("decnet_dev", S_IRUGO
, &dn_dev_seq_fops
);
1457 #ifdef CONFIG_SYSCTL
1460 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1461 dn_dev_sysctl_register(NULL
, &dn_dev_list
[i
]);
1463 #endif /* CONFIG_SYSCTL */
1466 void __exit
dn_dev_cleanup(void)
1468 rtnetlink_links
[PF_DECnet
] = NULL
;
1470 #ifdef CONFIG_SYSCTL
1473 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1474 dn_dev_sysctl_unregister(&dn_dev_list
[i
]);
1476 #endif /* CONFIG_SYSCTL */
1478 proc_net_remove("decnet_dev");
1480 dn_dev_devices_off();