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/sysctl.h>
42 #include <linux/notifier.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
45 #include <net/net_namespace.h>
46 #include <net/neighbour.h>
49 #include <net/fib_rules.h>
50 #include <net/netlink.h>
52 #include <net/dn_dev.h>
53 #include <net/dn_route.h>
54 #include <net/dn_neigh.h>
55 #include <net/dn_fib.h>
57 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
59 static char dn_rt_all_end_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x04,0x00,0x00};
60 static char dn_rt_all_rt_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x03,0x00,0x00};
61 static char dn_hiord
[ETH_ALEN
] = {0xAA,0x00,0x04,0x00,0x00,0x00};
62 static unsigned char dn_eco_version
[3] = {0x02,0x00,0x00};
64 extern struct neigh_table dn_neigh_table
;
67 * decnet_address is kept in network order.
69 __le16 decnet_address
= 0;
71 static DEFINE_RWLOCK(dndev_lock
);
72 static struct net_device
*decnet_default_device
;
73 static BLOCKING_NOTIFIER_HEAD(dnaddr_chain
);
75 static struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
);
76 static void dn_dev_delete(struct net_device
*dev
);
77 static void dn_ifaddr_notify(int event
, struct dn_ifaddr
*ifa
);
79 static int dn_eth_up(struct net_device
*);
80 static void dn_eth_down(struct net_device
*);
81 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
82 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
84 static struct dn_dev_parms dn_dev_list
[] = {
86 .type
= ARPHRD_ETHER
, /* Ethernet */
92 .ctl_name
= NET_DECNET_CONF_ETHER
,
95 .timer3
= dn_send_brd_hello
,
98 .type
= ARPHRD_IPGRE
, /* DECnet tunneled over GRE in IP */
100 .state
= DN_DEV_S_RU
,
104 .ctl_name
= NET_DECNET_CONF_GRE
,
105 .timer3
= dn_send_brd_hello
,
109 .type
= ARPHRD_X25
, /* Bog standard X.25 */
110 .mode
= DN_DEV_UCAST
,
111 .state
= DN_DEV_S_DS
,
115 .ctl_name
= NET_DECNET_CONF_X25
,
116 .timer3
= dn_send_ptp_hello
,
121 .type
= ARPHRD_PPP
, /* DECnet over PPP */
122 .mode
= DN_DEV_BCAST
,
123 .state
= DN_DEV_S_RU
,
127 .ctl_name
= NET_DECNET_CONF_PPP
,
128 .timer3
= dn_send_brd_hello
,
132 .type
= ARPHRD_DDCMP
, /* DECnet over DDCMP */
133 .mode
= DN_DEV_UCAST
,
134 .state
= DN_DEV_S_DS
,
138 .ctl_name
= NET_DECNET_CONF_DDCMP
,
139 .timer3
= dn_send_ptp_hello
,
142 .type
= ARPHRD_LOOPBACK
, /* Loopback interface - always last */
143 .mode
= DN_DEV_BCAST
,
144 .state
= DN_DEV_S_RU
,
148 .ctl_name
= NET_DECNET_CONF_LOOPBACK
,
149 .timer3
= dn_send_brd_hello
,
153 #define DN_DEV_LIST_SIZE ARRAY_SIZE(dn_dev_list)
155 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
159 static int min_t2
[] = { 1 };
160 static int max_t2
[] = { 60 }; /* No max specified, but this seems sensible */
161 static int min_t3
[] = { 1 };
162 static int max_t3
[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
164 static int min_priority
[1];
165 static int max_priority
[] = { 127 }; /* From DECnet spec */
167 static int dn_forwarding_proc(ctl_table
*, int, struct file
*,
168 void __user
*, size_t *, loff_t
*);
169 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
170 void __user
*oldval
, size_t __user
*oldlenp
,
171 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
= kmemdup(&dn_dev_sysctl
, sizeof(*t
), GFP_KERNEL
);
262 for(i
= 0; i
< ARRAY_SIZE(t
->dn_dev_vars
) - 1; i
++) {
263 long offset
= (long)t
->dn_dev_vars
[i
].data
;
264 t
->dn_dev_vars
[i
].data
= ((char *)parms
) + offset
;
268 t
->dn_dev_dev
[0].procname
= dev
->name
;
269 t
->dn_dev_dev
[0].ctl_name
= dev
->ifindex
;
271 t
->dn_dev_dev
[0].procname
= parms
->name
;
272 t
->dn_dev_dev
[0].ctl_name
= parms
->ctl_name
;
275 t
->dn_dev_dev
[0].child
= t
->dn_dev_vars
;
276 t
->dn_dev_conf_dir
[0].child
= t
->dn_dev_dev
;
277 t
->dn_dev_proto_dir
[0].child
= t
->dn_dev_conf_dir
;
278 t
->dn_dev_root_dir
[0].child
= t
->dn_dev_proto_dir
;
279 t
->dn_dev_vars
[0].extra1
= (void *)dev
;
281 t
->sysctl_header
= register_sysctl_table(t
->dn_dev_root_dir
);
282 if (t
->sysctl_header
== NULL
)
288 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
291 struct dn_dev_sysctl_table
*t
= parms
->sysctl
;
292 parms
->sysctl
= NULL
;
293 unregister_sysctl_table(t
->sysctl_header
);
298 static int dn_forwarding_proc(ctl_table
*table
, int write
,
301 size_t *lenp
, loff_t
*ppos
)
303 #ifdef CONFIG_DECNET_ROUTER
304 struct net_device
*dev
= table
->extra1
;
305 struct dn_dev
*dn_db
;
309 if (table
->extra1
== NULL
)
313 old
= dn_db
->parms
.forwarding
;
315 err
= proc_dointvec(table
, write
, filep
, buffer
, lenp
, ppos
);
317 if ((err
>= 0) && write
) {
318 if (dn_db
->parms
.forwarding
< 0)
319 dn_db
->parms
.forwarding
= 0;
320 if (dn_db
->parms
.forwarding
> 2)
321 dn_db
->parms
.forwarding
= 2;
323 * What an ugly hack this is... its works, just. It
324 * would be nice if sysctl/proc were just that little
325 * bit more flexible so I don't have to write a special
326 * routine, or suffer hacks like this - SJW
328 tmp
= dn_db
->parms
.forwarding
;
329 dn_db
->parms
.forwarding
= old
;
330 if (dn_db
->parms
.down
)
331 dn_db
->parms
.down(dev
);
332 dn_db
->parms
.forwarding
= tmp
;
334 dn_db
->parms
.up(dev
);
343 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
344 void __user
*oldval
, size_t __user
*oldlenp
,
345 void __user
*newval
, size_t newlen
)
347 #ifdef CONFIG_DECNET_ROUTER
348 struct net_device
*dev
= table
->extra1
;
349 struct dn_dev
*dn_db
;
352 if (table
->extra1
== NULL
)
357 if (newval
&& newlen
) {
358 if (newlen
!= sizeof(int))
361 if (get_user(value
, (int __user
*)newval
))
368 if (dn_db
->parms
.down
)
369 dn_db
->parms
.down(dev
);
370 dn_db
->parms
.forwarding
= value
;
372 dn_db
->parms
.up(dev
);
381 #else /* CONFIG_SYSCTL */
382 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
385 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
389 #endif /* CONFIG_SYSCTL */
391 static inline __u16
mtu2blksize(struct net_device
*dev
)
393 u32 blksize
= dev
->mtu
;
394 if (blksize
> 0xffff)
397 if (dev
->type
== ARPHRD_ETHER
||
398 dev
->type
== ARPHRD_PPP
||
399 dev
->type
== ARPHRD_IPGRE
||
400 dev
->type
== ARPHRD_LOOPBACK
)
403 return (__u16
)blksize
;
406 static struct dn_ifaddr
*dn_dev_alloc_ifa(void)
408 struct dn_ifaddr
*ifa
;
410 ifa
= kzalloc(sizeof(*ifa
), GFP_KERNEL
);
415 static __inline__
void dn_dev_free_ifa(struct dn_ifaddr
*ifa
)
420 static void dn_dev_del_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
**ifap
, int destroy
)
422 struct dn_ifaddr
*ifa1
= *ifap
;
423 unsigned char mac_addr
[6];
424 struct net_device
*dev
= dn_db
->dev
;
428 *ifap
= ifa1
->ifa_next
;
430 if (dn_db
->dev
->type
== ARPHRD_ETHER
) {
431 if (ifa1
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
432 dn_dn2eth(mac_addr
, ifa1
->ifa_local
);
433 dev_mc_delete(dev
, mac_addr
, ETH_ALEN
, 0);
437 dn_ifaddr_notify(RTM_DELADDR
, ifa1
);
438 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_DOWN
, ifa1
);
440 dn_dev_free_ifa(ifa1
);
442 if (dn_db
->ifa_list
== NULL
)
443 dn_dev_delete(dn_db
->dev
);
447 static int dn_dev_insert_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
449 struct net_device
*dev
= dn_db
->dev
;
450 struct dn_ifaddr
*ifa1
;
451 unsigned char mac_addr
[6];
455 /* Check for duplicates */
456 for(ifa1
= dn_db
->ifa_list
; ifa1
; ifa1
= ifa1
->ifa_next
) {
457 if (ifa1
->ifa_local
== ifa
->ifa_local
)
461 if (dev
->type
== ARPHRD_ETHER
) {
462 if (ifa
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
463 dn_dn2eth(mac_addr
, ifa
->ifa_local
);
464 dev_mc_add(dev
, mac_addr
, ETH_ALEN
, 0);
468 ifa
->ifa_next
= dn_db
->ifa_list
;
469 dn_db
->ifa_list
= ifa
;
471 dn_ifaddr_notify(RTM_NEWADDR
, ifa
);
472 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_UP
, ifa
);
477 static int dn_dev_set_ifa(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
479 struct dn_dev
*dn_db
= dev
->dn_ptr
;
484 dn_db
= dn_dev_create(dev
, &err
);
489 ifa
->ifa_dev
= dn_db
;
491 if (dev
->flags
& IFF_LOOPBACK
)
492 ifa
->ifa_scope
= RT_SCOPE_HOST
;
494 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
496 dn_dev_free_ifa(ifa
);
501 int dn_dev_ioctl(unsigned int cmd
, void __user
*arg
)
503 char buffer
[DN_IFREQ_SIZE
];
504 struct ifreq
*ifr
= (struct ifreq
*)buffer
;
505 struct sockaddr_dn
*sdn
= (struct sockaddr_dn
*)&ifr
->ifr_addr
;
506 struct dn_dev
*dn_db
;
507 struct net_device
*dev
;
508 struct dn_ifaddr
*ifa
= NULL
, **ifap
= NULL
;
511 if (copy_from_user(ifr
, arg
, DN_IFREQ_SIZE
))
513 ifr
->ifr_name
[IFNAMSIZ
-1] = 0;
516 dev_load(&init_net
, ifr
->ifr_name
);
523 if (!capable(CAP_NET_ADMIN
))
525 if (sdn
->sdn_family
!= AF_DECnet
)
534 if ((dev
= __dev_get_by_name(&init_net
, ifr
->ifr_name
)) == NULL
) {
539 if ((dn_db
= dev
->dn_ptr
) != NULL
) {
540 for (ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
)
541 if (strcmp(ifr
->ifr_name
, ifa
->ifa_label
) == 0)
545 if (ifa
== NULL
&& cmd
!= SIOCSIFADDR
) {
546 ret
= -EADDRNOTAVAIL
;
552 *((__le16
*)sdn
->sdn_nodeaddr
) = ifa
->ifa_local
;
557 if ((ifa
= dn_dev_alloc_ifa()) == NULL
) {
561 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
563 if (ifa
->ifa_local
== dn_saddr2dn(sdn
))
565 dn_dev_del_ifa(dn_db
, ifap
, 0);
568 ifa
->ifa_local
= ifa
->ifa_address
= dn_saddr2dn(sdn
);
570 ret
= dn_dev_set_ifa(dev
, ifa
);
577 if (copy_to_user(arg
, ifr
, DN_IFREQ_SIZE
))
582 struct net_device
*dn_dev_get_default(void)
584 struct net_device
*dev
;
585 read_lock(&dndev_lock
);
586 dev
= decnet_default_device
;
593 read_unlock(&dndev_lock
);
597 int dn_dev_set_default(struct net_device
*dev
, int force
)
599 struct net_device
*old
= NULL
;
603 write_lock(&dndev_lock
);
604 if (force
|| decnet_default_device
== NULL
) {
605 old
= decnet_default_device
;
606 decnet_default_device
= dev
;
609 write_unlock(&dndev_lock
);
615 static void dn_dev_check_default(struct net_device
*dev
)
617 write_lock(&dndev_lock
);
618 if (dev
== decnet_default_device
) {
619 decnet_default_device
= NULL
;
623 write_unlock(&dndev_lock
);
628 static struct dn_dev
*dn_dev_by_index(int ifindex
)
630 struct net_device
*dev
;
631 struct dn_dev
*dn_dev
= NULL
;
632 dev
= dev_get_by_index(&init_net
, ifindex
);
634 dn_dev
= dev
->dn_ptr
;
641 static const struct nla_policy dn_ifa_policy
[IFA_MAX
+1] = {
642 [IFA_ADDRESS
] = { .type
= NLA_U16
},
643 [IFA_LOCAL
] = { .type
= NLA_U16
},
644 [IFA_LABEL
] = { .type
= NLA_STRING
,
645 .len
= IFNAMSIZ
- 1 },
648 static int dn_nl_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
650 struct nlattr
*tb
[IFA_MAX
+1];
651 struct dn_dev
*dn_db
;
652 struct ifaddrmsg
*ifm
;
653 struct dn_ifaddr
*ifa
, **ifap
;
656 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, dn_ifa_policy
);
661 ifm
= nlmsg_data(nlh
);
662 if ((dn_db
= dn_dev_by_index(ifm
->ifa_index
)) == NULL
)
665 err
= -EADDRNOTAVAIL
;
666 for (ifap
= &dn_db
->ifa_list
; (ifa
= *ifap
); ifap
= &ifa
->ifa_next
) {
668 nla_memcmp(tb
[IFA_LOCAL
], &ifa
->ifa_local
, 2))
671 if (tb
[IFA_LABEL
] && nla_strcmp(tb
[IFA_LABEL
], ifa
->ifa_label
))
674 dn_dev_del_ifa(dn_db
, ifap
, 1);
682 static int dn_nl_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
684 struct nlattr
*tb
[IFA_MAX
+1];
685 struct net_device
*dev
;
686 struct dn_dev
*dn_db
;
687 struct ifaddrmsg
*ifm
;
688 struct dn_ifaddr
*ifa
;
691 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, dn_ifa_policy
);
695 if (tb
[IFA_LOCAL
] == NULL
)
698 ifm
= nlmsg_data(nlh
);
699 if ((dev
= __dev_get_by_index(&init_net
, ifm
->ifa_index
)) == NULL
)
702 if ((dn_db
= dev
->dn_ptr
) == NULL
) {
704 dn_db
= dn_dev_create(dev
, &err
);
709 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
712 if (tb
[IFA_ADDRESS
] == NULL
)
713 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
715 ifa
->ifa_local
= nla_get_le16(tb
[IFA_LOCAL
]);
716 ifa
->ifa_address
= nla_get_le16(tb
[IFA_ADDRESS
]);
717 ifa
->ifa_flags
= ifm
->ifa_flags
;
718 ifa
->ifa_scope
= ifm
->ifa_scope
;
719 ifa
->ifa_dev
= dn_db
;
722 nla_strlcpy(ifa
->ifa_label
, tb
[IFA_LABEL
], IFNAMSIZ
);
724 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
726 err
= dn_dev_insert_ifa(dn_db
, ifa
);
728 dn_dev_free_ifa(ifa
);
733 static inline size_t dn_ifaddr_nlmsg_size(void)
735 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
736 + nla_total_size(IFNAMSIZ
) /* IFA_LABEL */
737 + nla_total_size(2) /* IFA_ADDRESS */
738 + nla_total_size(2); /* IFA_LOCAL */
741 static int dn_nl_fill_ifaddr(struct sk_buff
*skb
, struct dn_ifaddr
*ifa
,
742 u32 pid
, u32 seq
, int event
, unsigned int flags
)
744 struct ifaddrmsg
*ifm
;
745 struct nlmsghdr
*nlh
;
747 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
751 ifm
= nlmsg_data(nlh
);
752 ifm
->ifa_family
= AF_DECnet
;
753 ifm
->ifa_prefixlen
= 16;
754 ifm
->ifa_flags
= ifa
->ifa_flags
| IFA_F_PERMANENT
;
755 ifm
->ifa_scope
= ifa
->ifa_scope
;
756 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
758 if (ifa
->ifa_address
)
759 NLA_PUT_LE16(skb
, IFA_ADDRESS
, ifa
->ifa_address
);
761 NLA_PUT_LE16(skb
, IFA_LOCAL
, ifa
->ifa_local
);
762 if (ifa
->ifa_label
[0])
763 NLA_PUT_STRING(skb
, IFA_LABEL
, ifa
->ifa_label
);
765 return nlmsg_end(skb
, nlh
);
768 nlmsg_cancel(skb
, nlh
);
772 static void dn_ifaddr_notify(int event
, struct dn_ifaddr
*ifa
)
777 skb
= alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL
);
781 err
= dn_nl_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
783 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
784 WARN_ON(err
== -EMSGSIZE
);
788 err
= rtnl_notify(skb
, 0, RTNLGRP_DECnet_IFADDR
, NULL
, GFP_KERNEL
);
791 rtnl_set_sk_err(RTNLGRP_DECnet_IFADDR
, err
);
794 static int dn_nl_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
796 int idx
, dn_idx
= 0, skip_ndevs
, skip_naddr
;
797 struct net_device
*dev
;
798 struct dn_dev
*dn_db
;
799 struct dn_ifaddr
*ifa
;
801 skip_ndevs
= cb
->args
[0];
802 skip_naddr
= cb
->args
[1];
805 for_each_netdev(&init_net
, dev
) {
806 if (idx
< skip_ndevs
)
808 else if (idx
> skip_ndevs
) {
809 /* Only skip over addresses for first dev dumped
810 * in this iteration (idx == skip_ndevs) */
814 if ((dn_db
= dev
->dn_ptr
) == NULL
)
817 for (ifa
= dn_db
->ifa_list
, dn_idx
= 0; ifa
;
818 ifa
= ifa
->ifa_next
, dn_idx
++) {
819 if (dn_idx
< skip_naddr
)
822 if (dn_nl_fill_ifaddr(skb
, ifa
, NETLINK_CB(cb
->skb
).pid
,
823 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
832 cb
->args
[1] = dn_idx
;
837 static int dn_dev_get_first(struct net_device
*dev
, __le16
*addr
)
839 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
840 struct dn_ifaddr
*ifa
;
844 ifa
= dn_db
->ifa_list
;
846 *addr
= ifa
->ifa_local
;
854 * Find a default address to bind to.
856 * This is one of those areas where the initial VMS concepts don't really
857 * map onto the Linux concepts, and since we introduced multiple addresses
858 * per interface we have to cope with slightly odd ways of finding out what
859 * "our address" really is. Mostly it's not a problem; for this we just guess
860 * a sensible default. Eventually the routing code will take care of all the
861 * nasties for us I hope.
863 int dn_dev_bind_default(__le16
*addr
)
865 struct net_device
*dev
;
867 dev
= dn_dev_get_default();
870 read_lock(&dev_base_lock
);
871 rv
= dn_dev_get_first(dev
, addr
);
872 read_unlock(&dev_base_lock
);
874 if (rv
== 0 || dev
== init_net
.loopback_dev
)
877 dev
= init_net
.loopback_dev
;
882 static void dn_send_endnode_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
884 struct endnode_hello_message
*msg
;
885 struct sk_buff
*skb
= NULL
;
887 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
889 if ((skb
= dn_alloc_skb(NULL
, sizeof(*msg
), GFP_ATOMIC
)) == NULL
)
894 msg
= (struct endnode_hello_message
*)skb_put(skb
,sizeof(*msg
));
897 memcpy(msg
->tiver
, dn_eco_version
, 3);
898 dn_dn2eth(msg
->id
, ifa
->ifa_local
);
899 msg
->iinfo
= DN_RT_INFO_ENDN
;
900 msg
->blksize
= dn_htons(mtu2blksize(dev
));
902 memset(msg
->seed
, 0, 8);
903 memcpy(msg
->neighbor
, dn_hiord
, ETH_ALEN
);
906 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
907 dn_dn2eth(msg
->neighbor
, dn
->addr
);
910 msg
->timer
= dn_htons((unsigned short)dn_db
->parms
.t3
);
913 memset(msg
->data
, 0xAA, 2);
915 pktlen
= (__le16
*)skb_push(skb
,2);
916 *pktlen
= dn_htons(skb
->len
- 2);
918 skb_reset_network_header(skb
);
920 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, msg
->id
);
924 #define DRDELAY (5 * HZ)
926 static int dn_am_i_a_router(struct dn_neigh
*dn
, struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
928 /* First check time since device went up */
929 if ((jiffies
- dn_db
->uptime
) < DRDELAY
)
932 /* If there is no router, then yes... */
936 /* otherwise only if we have a higher priority or.. */
937 if (dn
->priority
< dn_db
->parms
.priority
)
940 /* if we have equal priority and a higher node number */
941 if (dn
->priority
!= dn_db
->parms
.priority
)
944 if (dn_ntohs(dn
->addr
) < dn_ntohs(ifa
->ifa_local
))
950 static void dn_send_router_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
953 struct dn_dev
*dn_db
= dev
->dn_ptr
;
954 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
958 unsigned char *i1
, *i2
;
962 if (mtu2blksize(dev
) < (26 + 7))
965 n
= mtu2blksize(dev
) - 26;
971 size
= 2 + 26 + 7 * n
;
973 if ((skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
)) == NULL
)
977 ptr
= skb_put(skb
, size
);
979 *ptr
++ = DN_RT_PKT_CNTL
| DN_RT_PKT_ERTH
;
980 *ptr
++ = 2; /* ECO */
983 dn_dn2eth(ptr
, ifa
->ifa_local
);
986 *ptr
++ = dn_db
->parms
.forwarding
== 1 ?
987 DN_RT_INFO_L1RT
: DN_RT_INFO_L2RT
;
988 *((__le16
*)ptr
) = dn_htons(mtu2blksize(dev
));
990 *ptr
++ = dn_db
->parms
.priority
; /* Priority */
991 *ptr
++ = 0; /* Area: Reserved */
992 *((__le16
*)ptr
) = dn_htons((unsigned short)dn_db
->parms
.t3
);
994 *ptr
++ = 0; /* MPD: Reserved */
996 memset(ptr
, 0, 7); /* Name: Reserved */
1000 n
= dn_neigh_elist(dev
, ptr
, n
);
1005 skb_trim(skb
, (27 + *i2
));
1007 pktlen
= (__le16
*)skb_push(skb
, 2);
1008 *pktlen
= dn_htons(skb
->len
- 2);
1010 skb_reset_network_header(skb
);
1012 if (dn_am_i_a_router(dn
, dn_db
, ifa
)) {
1013 struct sk_buff
*skb2
= skb_copy(skb
, GFP_ATOMIC
);
1015 dn_rt_finish_output(skb2
, dn_rt_all_end_mcast
, src
);
1019 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1022 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1024 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1026 if (dn_db
->parms
.forwarding
== 0)
1027 dn_send_endnode_hello(dev
, ifa
);
1029 dn_send_router_hello(dev
, ifa
);
1032 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1035 int size
= dev
->hard_header_len
+ 2 + 4 + tdlen
;
1036 struct sk_buff
*skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
);
1045 skb_push(skb
, dev
->hard_header_len
);
1046 ptr
= skb_put(skb
, 2 + 4 + tdlen
);
1048 *ptr
++ = DN_RT_PKT_HELO
;
1049 *((__le16
*)ptr
) = ifa
->ifa_local
;
1053 for(i
= 0; i
< tdlen
; i
++)
1056 dn_dn2eth(src
, ifa
->ifa_local
);
1057 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1060 static int dn_eth_up(struct net_device
*dev
)
1062 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1064 if (dn_db
->parms
.forwarding
== 0)
1065 dev_mc_add(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1067 dev_mc_add(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1069 dn_db
->use_long
= 1;
1074 static void dn_eth_down(struct net_device
*dev
)
1076 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1078 if (dn_db
->parms
.forwarding
== 0)
1079 dev_mc_delete(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1081 dev_mc_delete(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1084 static void dn_dev_set_timer(struct net_device
*dev
);
1086 static void dn_dev_timer_func(unsigned long arg
)
1088 struct net_device
*dev
= (struct net_device
*)arg
;
1089 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1090 struct dn_ifaddr
*ifa
;
1092 if (dn_db
->t3
<= dn_db
->parms
.t2
) {
1093 if (dn_db
->parms
.timer3
) {
1094 for(ifa
= dn_db
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1095 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
1096 dn_db
->parms
.timer3(dev
, ifa
);
1099 dn_db
->t3
= dn_db
->parms
.t3
;
1101 dn_db
->t3
-= dn_db
->parms
.t2
;
1104 dn_dev_set_timer(dev
);
1107 static void dn_dev_set_timer(struct net_device
*dev
)
1109 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1111 if (dn_db
->parms
.t2
> dn_db
->parms
.t3
)
1112 dn_db
->parms
.t2
= dn_db
->parms
.t3
;
1114 dn_db
->timer
.data
= (unsigned long)dev
;
1115 dn_db
->timer
.function
= dn_dev_timer_func
;
1116 dn_db
->timer
.expires
= jiffies
+ (dn_db
->parms
.t2
* HZ
);
1118 add_timer(&dn_db
->timer
);
1121 struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
)
1124 struct dn_dev_parms
*p
= dn_dev_list
;
1125 struct dn_dev
*dn_db
;
1127 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++, p
++) {
1128 if (p
->type
== dev
->type
)
1133 if (i
== DN_DEV_LIST_SIZE
)
1137 if ((dn_db
= kzalloc(sizeof(struct dn_dev
), GFP_ATOMIC
)) == NULL
)
1140 memcpy(&dn_db
->parms
, p
, sizeof(struct dn_dev_parms
));
1142 dev
->dn_ptr
= dn_db
;
1144 init_timer(&dn_db
->timer
);
1146 dn_db
->uptime
= jiffies
;
1148 dn_db
->neigh_parms
= neigh_parms_alloc(dev
, &dn_neigh_table
);
1149 if (!dn_db
->neigh_parms
) {
1155 if (dn_db
->parms
.up
) {
1156 if (dn_db
->parms
.up(dev
) < 0) {
1157 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1164 dn_dev_sysctl_register(dev
, &dn_db
->parms
);
1166 dn_dev_set_timer(dev
);
1174 * This processes a device up event. We only start up
1175 * the loopback device & ethernet devices with correct
1176 * MAC addreses automatically. Others must be started
1179 * FIXME: How should we configure the loopback address ? If we could dispense
1180 * with using decnet_address here and for autobind, it will be one less thing
1181 * for users to worry about setting up.
1184 void dn_dev_up(struct net_device
*dev
)
1186 struct dn_ifaddr
*ifa
;
1187 __le16 addr
= decnet_address
;
1188 int maybe_default
= 0;
1189 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1191 if ((dev
->type
!= ARPHRD_ETHER
) && (dev
->type
!= ARPHRD_LOOPBACK
))
1195 * Need to ensure that loopback device has a dn_db attached to it
1196 * to allow creation of neighbours against it, even though it might
1197 * not have a local address of its own. Might as well do the same for
1198 * all autoconfigured interfaces.
1200 if (dn_db
== NULL
) {
1202 dn_db
= dn_dev_create(dev
, &err
);
1207 if (dev
->type
== ARPHRD_ETHER
) {
1208 if (memcmp(dev
->dev_addr
, dn_hiord
, 4) != 0)
1210 addr
= dn_eth2dn(dev
->dev_addr
);
1217 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
1220 ifa
->ifa_local
= ifa
->ifa_address
= addr
;
1222 ifa
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1223 strcpy(ifa
->ifa_label
, dev
->name
);
1225 dn_dev_set_ifa(dev
, ifa
);
1228 * Automagically set the default device to the first automatically
1229 * configured ethernet card in the system.
1231 if (maybe_default
) {
1233 if (dn_dev_set_default(dev
, 0))
1238 static void dn_dev_delete(struct net_device
*dev
)
1240 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1245 del_timer_sync(&dn_db
->timer
);
1246 dn_dev_sysctl_unregister(&dn_db
->parms
);
1247 dn_dev_check_default(dev
);
1248 neigh_ifdown(&dn_neigh_table
, dev
);
1250 if (dn_db
->parms
.down
)
1251 dn_db
->parms
.down(dev
);
1255 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1256 neigh_ifdown(&dn_neigh_table
, dev
);
1259 neigh_release(dn_db
->router
);
1261 neigh_release(dn_db
->peer
);
1266 void dn_dev_down(struct net_device
*dev
)
1268 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1269 struct dn_ifaddr
*ifa
;
1274 while((ifa
= dn_db
->ifa_list
) != NULL
) {
1275 dn_dev_del_ifa(dn_db
, &dn_db
->ifa_list
, 0);
1276 dn_dev_free_ifa(ifa
);
1282 void dn_dev_init_pkt(struct sk_buff
*skb
)
1287 void dn_dev_veri_pkt(struct sk_buff
*skb
)
1292 void dn_dev_hello(struct sk_buff
*skb
)
1297 void dn_dev_devices_off(void)
1299 struct net_device
*dev
;
1302 for_each_netdev(&init_net
, dev
)
1308 void dn_dev_devices_on(void)
1310 struct net_device
*dev
;
1313 for_each_netdev(&init_net
, dev
) {
1314 if (dev
->flags
& IFF_UP
)
1320 int register_dnaddr_notifier(struct notifier_block
*nb
)
1322 return blocking_notifier_chain_register(&dnaddr_chain
, nb
);
1325 int unregister_dnaddr_notifier(struct notifier_block
*nb
)
1327 return blocking_notifier_chain_unregister(&dnaddr_chain
, nb
);
1330 #ifdef CONFIG_PROC_FS
1331 static inline int is_dn_dev(struct net_device
*dev
)
1333 return dev
->dn_ptr
!= NULL
;
1336 static void *dn_dev_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1339 struct net_device
*dev
;
1341 read_lock(&dev_base_lock
);
1344 return SEQ_START_TOKEN
;
1347 for_each_netdev(&init_net
, dev
) {
1348 if (!is_dn_dev(dev
))
1358 static void *dn_dev_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1360 struct net_device
*dev
;
1364 dev
= (struct net_device
*)v
;
1365 if (v
== SEQ_START_TOKEN
)
1366 dev
= net_device_entry(&init_net
.dev_base_head
);
1368 for_each_netdev_continue(&init_net
, dev
) {
1369 if (!is_dn_dev(dev
))
1378 static void dn_dev_seq_stop(struct seq_file
*seq
, void *v
)
1380 read_unlock(&dev_base_lock
);
1383 static char *dn_type2asc(char type
)
1397 static int dn_dev_seq_show(struct seq_file
*seq
, void *v
)
1399 if (v
== SEQ_START_TOKEN
)
1400 seq_puts(seq
, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1402 struct net_device
*dev
= v
;
1403 char peer_buf
[DN_ASCBUF_LEN
];
1404 char router_buf
[DN_ASCBUF_LEN
];
1405 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1407 seq_printf(seq
, "%-8s %1s %04u %04u %04lu %04lu"
1408 " %04hu %03d %02x %-10s %-7s %-7s\n",
1409 dev
->name
? dev
->name
: "???",
1410 dn_type2asc(dn_db
->parms
.mode
),
1412 dn_db
->t3
, dn_db
->parms
.t3
,
1414 dn_db
->parms
.priority
,
1415 dn_db
->parms
.state
, dn_db
->parms
.name
,
1416 dn_db
->router
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->router
->primary_key
), router_buf
) : "",
1417 dn_db
->peer
? dn_addr2asc(dn_ntohs(*(__le16
*)dn_db
->peer
->primary_key
), peer_buf
) : "");
1422 static const struct seq_operations dn_dev_seq_ops
= {
1423 .start
= dn_dev_seq_start
,
1424 .next
= dn_dev_seq_next
,
1425 .stop
= dn_dev_seq_stop
,
1426 .show
= dn_dev_seq_show
,
1429 static int dn_dev_seq_open(struct inode
*inode
, struct file
*file
)
1431 return seq_open(file
, &dn_dev_seq_ops
);
1434 static const struct file_operations dn_dev_seq_fops
= {
1435 .owner
= THIS_MODULE
,
1436 .open
= dn_dev_seq_open
,
1438 .llseek
= seq_lseek
,
1439 .release
= seq_release
,
1442 #endif /* CONFIG_PROC_FS */
1445 module_param_array(addr
, int, NULL
, 0444);
1446 MODULE_PARM_DESC(addr
, "The DECnet address of this machine: area,node");
1448 void __init
dn_dev_init(void)
1450 if (addr
[0] > 63 || addr
[0] < 0) {
1451 printk(KERN_ERR
"DECnet: Area must be between 0 and 63");
1455 if (addr
[1] > 1023 || addr
[1] < 0) {
1456 printk(KERN_ERR
"DECnet: Node must be between 0 and 1023");
1460 decnet_address
= dn_htons((addr
[0] << 10) | addr
[1]);
1462 dn_dev_devices_on();
1464 rtnl_register(PF_DECnet
, RTM_NEWADDR
, dn_nl_newaddr
, NULL
);
1465 rtnl_register(PF_DECnet
, RTM_DELADDR
, dn_nl_deladdr
, NULL
);
1466 rtnl_register(PF_DECnet
, RTM_GETADDR
, NULL
, dn_nl_dump_ifaddr
);
1468 proc_net_fops_create(&init_net
, "decnet_dev", S_IRUGO
, &dn_dev_seq_fops
);
1470 #ifdef CONFIG_SYSCTL
1473 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1474 dn_dev_sysctl_register(NULL
, &dn_dev_list
[i
]);
1476 #endif /* CONFIG_SYSCTL */
1479 void __exit
dn_dev_cleanup(void)
1481 #ifdef CONFIG_SYSCTL
1484 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1485 dn_dev_sysctl_unregister(&dn_dev_list
[i
]);
1487 #endif /* CONFIG_SYSCTL */
1489 proc_net_remove(&init_net
, "decnet_dev");
1491 dn_dev_devices_off();