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) offsetof(struct dn_dev_parms, 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
,
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];
180 .ctl_name
= NET_DECNET_CONF_DEV_FORWARDING
,
181 .procname
= "forwarding",
182 .data
= (void *)DN_DEV_PARMS_OFFSET(forwarding
),
183 .maxlen
= sizeof(int),
185 .proc_handler
= dn_forwarding_proc
,
186 .strategy
= dn_forwarding_sysctl
,
189 .ctl_name
= NET_DECNET_CONF_DEV_PRIORITY
,
190 .procname
= "priority",
191 .data
= (void *)DN_DEV_PARMS_OFFSET(priority
),
192 .maxlen
= sizeof(int),
194 .proc_handler
= proc_dointvec_minmax
,
195 .strategy
= sysctl_intvec
,
196 .extra1
= &min_priority
,
197 .extra2
= &max_priority
200 .ctl_name
= NET_DECNET_CONF_DEV_T2
,
202 .data
= (void *)DN_DEV_PARMS_OFFSET(t2
),
203 .maxlen
= sizeof(int),
205 .proc_handler
= proc_dointvec_minmax
,
206 .strategy
= sysctl_intvec
,
211 .ctl_name
= NET_DECNET_CONF_DEV_T3
,
213 .data
= (void *)DN_DEV_PARMS_OFFSET(t3
),
214 .maxlen
= sizeof(int),
216 .proc_handler
= proc_dointvec_minmax
,
217 .strategy
= sysctl_intvec
,
225 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
227 struct dn_dev_sysctl_table
*t
;
230 #define DN_CTL_PATH_DEV 3
232 struct ctl_path dn_ctl_path
[] = {
233 { .procname
= "net", .ctl_name
= CTL_NET
, },
234 { .procname
= "decnet", .ctl_name
= NET_DECNET
, },
235 { .procname
= "conf", .ctl_name
= NET_DECNET_CONF
, },
240 t
= kmemdup(&dn_dev_sysctl
, sizeof(*t
), GFP_KERNEL
);
244 for(i
= 0; i
< ARRAY_SIZE(t
->dn_dev_vars
) - 1; i
++) {
245 long offset
= (long)t
->dn_dev_vars
[i
].data
;
246 t
->dn_dev_vars
[i
].data
= ((char *)parms
) + offset
;
250 dn_ctl_path
[DN_CTL_PATH_DEV
].procname
= dev
->name
;
251 dn_ctl_path
[DN_CTL_PATH_DEV
].ctl_name
= dev
->ifindex
;
253 dn_ctl_path
[DN_CTL_PATH_DEV
].procname
= parms
->name
;
254 dn_ctl_path
[DN_CTL_PATH_DEV
].ctl_name
= parms
->ctl_name
;
257 t
->dn_dev_vars
[0].extra1
= (void *)dev
;
259 t
->sysctl_header
= register_sysctl_paths(dn_ctl_path
, t
->dn_dev_vars
);
260 if (t
->sysctl_header
== NULL
)
266 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
269 struct dn_dev_sysctl_table
*t
= parms
->sysctl
;
270 parms
->sysctl
= NULL
;
271 unregister_sysctl_table(t
->sysctl_header
);
276 static int dn_forwarding_proc(ctl_table
*table
, int write
,
279 size_t *lenp
, loff_t
*ppos
)
281 #ifdef CONFIG_DECNET_ROUTER
282 struct net_device
*dev
= table
->extra1
;
283 struct dn_dev
*dn_db
;
287 if (table
->extra1
== NULL
)
291 old
= dn_db
->parms
.forwarding
;
293 err
= proc_dointvec(table
, write
, filep
, buffer
, lenp
, ppos
);
295 if ((err
>= 0) && write
) {
296 if (dn_db
->parms
.forwarding
< 0)
297 dn_db
->parms
.forwarding
= 0;
298 if (dn_db
->parms
.forwarding
> 2)
299 dn_db
->parms
.forwarding
= 2;
301 * What an ugly hack this is... its works, just. It
302 * would be nice if sysctl/proc were just that little
303 * bit more flexible so I don't have to write a special
304 * routine, or suffer hacks like this - SJW
306 tmp
= dn_db
->parms
.forwarding
;
307 dn_db
->parms
.forwarding
= old
;
308 if (dn_db
->parms
.down
)
309 dn_db
->parms
.down(dev
);
310 dn_db
->parms
.forwarding
= tmp
;
312 dn_db
->parms
.up(dev
);
321 static int dn_forwarding_sysctl(ctl_table
*table
,
322 void __user
*oldval
, size_t __user
*oldlenp
,
323 void __user
*newval
, size_t newlen
)
325 #ifdef CONFIG_DECNET_ROUTER
326 struct net_device
*dev
= table
->extra1
;
327 struct dn_dev
*dn_db
;
330 if (table
->extra1
== NULL
)
335 if (newval
&& newlen
) {
336 if (newlen
!= sizeof(int))
339 if (get_user(value
, (int __user
*)newval
))
346 if (dn_db
->parms
.down
)
347 dn_db
->parms
.down(dev
);
348 dn_db
->parms
.forwarding
= value
;
350 dn_db
->parms
.up(dev
);
359 #else /* CONFIG_SYSCTL */
360 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
363 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
367 #endif /* CONFIG_SYSCTL */
369 static inline __u16
mtu2blksize(struct net_device
*dev
)
371 u32 blksize
= dev
->mtu
;
372 if (blksize
> 0xffff)
375 if (dev
->type
== ARPHRD_ETHER
||
376 dev
->type
== ARPHRD_PPP
||
377 dev
->type
== ARPHRD_IPGRE
||
378 dev
->type
== ARPHRD_LOOPBACK
)
381 return (__u16
)blksize
;
384 static struct dn_ifaddr
*dn_dev_alloc_ifa(void)
386 struct dn_ifaddr
*ifa
;
388 ifa
= kzalloc(sizeof(*ifa
), GFP_KERNEL
);
393 static __inline__
void dn_dev_free_ifa(struct dn_ifaddr
*ifa
)
398 static void dn_dev_del_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
**ifap
, int destroy
)
400 struct dn_ifaddr
*ifa1
= *ifap
;
401 unsigned char mac_addr
[6];
402 struct net_device
*dev
= dn_db
->dev
;
406 *ifap
= ifa1
->ifa_next
;
408 if (dn_db
->dev
->type
== ARPHRD_ETHER
) {
409 if (ifa1
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
410 dn_dn2eth(mac_addr
, ifa1
->ifa_local
);
411 dev_mc_delete(dev
, mac_addr
, ETH_ALEN
, 0);
415 dn_ifaddr_notify(RTM_DELADDR
, ifa1
);
416 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_DOWN
, ifa1
);
418 dn_dev_free_ifa(ifa1
);
420 if (dn_db
->ifa_list
== NULL
)
421 dn_dev_delete(dn_db
->dev
);
425 static int dn_dev_insert_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
427 struct net_device
*dev
= dn_db
->dev
;
428 struct dn_ifaddr
*ifa1
;
429 unsigned char mac_addr
[6];
433 /* Check for duplicates */
434 for(ifa1
= dn_db
->ifa_list
; ifa1
; ifa1
= ifa1
->ifa_next
) {
435 if (ifa1
->ifa_local
== ifa
->ifa_local
)
439 if (dev
->type
== ARPHRD_ETHER
) {
440 if (ifa
->ifa_local
!= dn_eth2dn(dev
->dev_addr
)) {
441 dn_dn2eth(mac_addr
, ifa
->ifa_local
);
442 dev_mc_add(dev
, mac_addr
, ETH_ALEN
, 0);
446 ifa
->ifa_next
= dn_db
->ifa_list
;
447 dn_db
->ifa_list
= ifa
;
449 dn_ifaddr_notify(RTM_NEWADDR
, ifa
);
450 blocking_notifier_call_chain(&dnaddr_chain
, NETDEV_UP
, ifa
);
455 static int dn_dev_set_ifa(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
457 struct dn_dev
*dn_db
= dev
->dn_ptr
;
462 dn_db
= dn_dev_create(dev
, &err
);
467 ifa
->ifa_dev
= dn_db
;
469 if (dev
->flags
& IFF_LOOPBACK
)
470 ifa
->ifa_scope
= RT_SCOPE_HOST
;
472 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
474 dn_dev_free_ifa(ifa
);
479 int dn_dev_ioctl(unsigned int cmd
, void __user
*arg
)
481 char buffer
[DN_IFREQ_SIZE
];
482 struct ifreq
*ifr
= (struct ifreq
*)buffer
;
483 struct sockaddr_dn
*sdn
= (struct sockaddr_dn
*)&ifr
->ifr_addr
;
484 struct dn_dev
*dn_db
;
485 struct net_device
*dev
;
486 struct dn_ifaddr
*ifa
= NULL
, **ifap
= NULL
;
489 if (copy_from_user(ifr
, arg
, DN_IFREQ_SIZE
))
491 ifr
->ifr_name
[IFNAMSIZ
-1] = 0;
493 dev_load(&init_net
, ifr
->ifr_name
);
499 if (!capable(CAP_NET_ADMIN
))
501 if (sdn
->sdn_family
!= AF_DECnet
)
510 if ((dev
= __dev_get_by_name(&init_net
, ifr
->ifr_name
)) == NULL
) {
515 if ((dn_db
= dev
->dn_ptr
) != NULL
) {
516 for (ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
)
517 if (strcmp(ifr
->ifr_name
, ifa
->ifa_label
) == 0)
521 if (ifa
== NULL
&& cmd
!= SIOCSIFADDR
) {
522 ret
= -EADDRNOTAVAIL
;
528 *((__le16
*)sdn
->sdn_nodeaddr
) = ifa
->ifa_local
;
533 if ((ifa
= dn_dev_alloc_ifa()) == NULL
) {
537 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
539 if (ifa
->ifa_local
== dn_saddr2dn(sdn
))
541 dn_dev_del_ifa(dn_db
, ifap
, 0);
544 ifa
->ifa_local
= ifa
->ifa_address
= dn_saddr2dn(sdn
);
546 ret
= dn_dev_set_ifa(dev
, ifa
);
553 if (copy_to_user(arg
, ifr
, DN_IFREQ_SIZE
))
558 struct net_device
*dn_dev_get_default(void)
560 struct net_device
*dev
;
561 read_lock(&dndev_lock
);
562 dev
= decnet_default_device
;
569 read_unlock(&dndev_lock
);
573 int dn_dev_set_default(struct net_device
*dev
, int force
)
575 struct net_device
*old
= NULL
;
579 write_lock(&dndev_lock
);
580 if (force
|| decnet_default_device
== NULL
) {
581 old
= decnet_default_device
;
582 decnet_default_device
= dev
;
585 write_unlock(&dndev_lock
);
591 static void dn_dev_check_default(struct net_device
*dev
)
593 write_lock(&dndev_lock
);
594 if (dev
== decnet_default_device
) {
595 decnet_default_device
= NULL
;
599 write_unlock(&dndev_lock
);
604 static struct dn_dev
*dn_dev_by_index(int ifindex
)
606 struct net_device
*dev
;
607 struct dn_dev
*dn_dev
= NULL
;
608 dev
= dev_get_by_index(&init_net
, ifindex
);
610 dn_dev
= dev
->dn_ptr
;
617 static const struct nla_policy dn_ifa_policy
[IFA_MAX
+1] = {
618 [IFA_ADDRESS
] = { .type
= NLA_U16
},
619 [IFA_LOCAL
] = { .type
= NLA_U16
},
620 [IFA_LABEL
] = { .type
= NLA_STRING
,
621 .len
= IFNAMSIZ
- 1 },
624 static int dn_nl_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
626 struct net
*net
= sock_net(skb
->sk
);
627 struct nlattr
*tb
[IFA_MAX
+1];
628 struct dn_dev
*dn_db
;
629 struct ifaddrmsg
*ifm
;
630 struct dn_ifaddr
*ifa
, **ifap
;
633 if (net
!= &init_net
)
636 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, dn_ifa_policy
);
641 ifm
= nlmsg_data(nlh
);
642 if ((dn_db
= dn_dev_by_index(ifm
->ifa_index
)) == NULL
)
645 err
= -EADDRNOTAVAIL
;
646 for (ifap
= &dn_db
->ifa_list
; (ifa
= *ifap
); ifap
= &ifa
->ifa_next
) {
648 nla_memcmp(tb
[IFA_LOCAL
], &ifa
->ifa_local
, 2))
651 if (tb
[IFA_LABEL
] && nla_strcmp(tb
[IFA_LABEL
], ifa
->ifa_label
))
654 dn_dev_del_ifa(dn_db
, ifap
, 1);
662 static int dn_nl_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
664 struct net
*net
= sock_net(skb
->sk
);
665 struct nlattr
*tb
[IFA_MAX
+1];
666 struct net_device
*dev
;
667 struct dn_dev
*dn_db
;
668 struct ifaddrmsg
*ifm
;
669 struct dn_ifaddr
*ifa
;
672 if (net
!= &init_net
)
675 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFA_MAX
, dn_ifa_policy
);
679 if (tb
[IFA_LOCAL
] == NULL
)
682 ifm
= nlmsg_data(nlh
);
683 if ((dev
= __dev_get_by_index(&init_net
, ifm
->ifa_index
)) == NULL
)
686 if ((dn_db
= dev
->dn_ptr
) == NULL
) {
688 dn_db
= dn_dev_create(dev
, &err
);
693 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
696 if (tb
[IFA_ADDRESS
] == NULL
)
697 tb
[IFA_ADDRESS
] = tb
[IFA_LOCAL
];
699 ifa
->ifa_local
= nla_get_le16(tb
[IFA_LOCAL
]);
700 ifa
->ifa_address
= nla_get_le16(tb
[IFA_ADDRESS
]);
701 ifa
->ifa_flags
= ifm
->ifa_flags
;
702 ifa
->ifa_scope
= ifm
->ifa_scope
;
703 ifa
->ifa_dev
= dn_db
;
706 nla_strlcpy(ifa
->ifa_label
, tb
[IFA_LABEL
], IFNAMSIZ
);
708 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
710 err
= dn_dev_insert_ifa(dn_db
, ifa
);
712 dn_dev_free_ifa(ifa
);
717 static inline size_t dn_ifaddr_nlmsg_size(void)
719 return NLMSG_ALIGN(sizeof(struct ifaddrmsg
))
720 + nla_total_size(IFNAMSIZ
) /* IFA_LABEL */
721 + nla_total_size(2) /* IFA_ADDRESS */
722 + nla_total_size(2); /* IFA_LOCAL */
725 static int dn_nl_fill_ifaddr(struct sk_buff
*skb
, struct dn_ifaddr
*ifa
,
726 u32 pid
, u32 seq
, int event
, unsigned int flags
)
728 struct ifaddrmsg
*ifm
;
729 struct nlmsghdr
*nlh
;
731 nlh
= nlmsg_put(skb
, pid
, seq
, event
, sizeof(*ifm
), flags
);
735 ifm
= nlmsg_data(nlh
);
736 ifm
->ifa_family
= AF_DECnet
;
737 ifm
->ifa_prefixlen
= 16;
738 ifm
->ifa_flags
= ifa
->ifa_flags
| IFA_F_PERMANENT
;
739 ifm
->ifa_scope
= ifa
->ifa_scope
;
740 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
742 if (ifa
->ifa_address
)
743 NLA_PUT_LE16(skb
, IFA_ADDRESS
, ifa
->ifa_address
);
745 NLA_PUT_LE16(skb
, IFA_LOCAL
, ifa
->ifa_local
);
746 if (ifa
->ifa_label
[0])
747 NLA_PUT_STRING(skb
, IFA_LABEL
, ifa
->ifa_label
);
749 return nlmsg_end(skb
, nlh
);
752 nlmsg_cancel(skb
, nlh
);
756 static void dn_ifaddr_notify(int event
, struct dn_ifaddr
*ifa
)
761 skb
= alloc_skb(dn_ifaddr_nlmsg_size(), GFP_KERNEL
);
765 err
= dn_nl_fill_ifaddr(skb
, ifa
, 0, 0, event
, 0);
767 /* -EMSGSIZE implies BUG in dn_ifaddr_nlmsg_size() */
768 WARN_ON(err
== -EMSGSIZE
);
772 err
= rtnl_notify(skb
, &init_net
, 0, RTNLGRP_DECnet_IFADDR
, NULL
, GFP_KERNEL
);
775 rtnl_set_sk_err(&init_net
, RTNLGRP_DECnet_IFADDR
, err
);
778 static int dn_nl_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
780 struct net
*net
= sock_net(skb
->sk
);
781 int idx
, dn_idx
= 0, skip_ndevs
, skip_naddr
;
782 struct net_device
*dev
;
783 struct dn_dev
*dn_db
;
784 struct dn_ifaddr
*ifa
;
786 if (net
!= &init_net
)
789 skip_ndevs
= cb
->args
[0];
790 skip_naddr
= cb
->args
[1];
793 for_each_netdev(&init_net
, dev
) {
794 if (idx
< skip_ndevs
)
796 else if (idx
> skip_ndevs
) {
797 /* Only skip over addresses for first dev dumped
798 * in this iteration (idx == skip_ndevs) */
802 if ((dn_db
= dev
->dn_ptr
) == NULL
)
805 for (ifa
= dn_db
->ifa_list
, dn_idx
= 0; ifa
;
806 ifa
= ifa
->ifa_next
, dn_idx
++) {
807 if (dn_idx
< skip_naddr
)
810 if (dn_nl_fill_ifaddr(skb
, ifa
, NETLINK_CB(cb
->skb
).pid
,
811 cb
->nlh
->nlmsg_seq
, RTM_NEWADDR
,
820 cb
->args
[1] = dn_idx
;
825 static int dn_dev_get_first(struct net_device
*dev
, __le16
*addr
)
827 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
828 struct dn_ifaddr
*ifa
;
832 ifa
= dn_db
->ifa_list
;
834 *addr
= ifa
->ifa_local
;
842 * Find a default address to bind to.
844 * This is one of those areas where the initial VMS concepts don't really
845 * map onto the Linux concepts, and since we introduced multiple addresses
846 * per interface we have to cope with slightly odd ways of finding out what
847 * "our address" really is. Mostly it's not a problem; for this we just guess
848 * a sensible default. Eventually the routing code will take care of all the
849 * nasties for us I hope.
851 int dn_dev_bind_default(__le16
*addr
)
853 struct net_device
*dev
;
855 dev
= dn_dev_get_default();
858 read_lock(&dev_base_lock
);
859 rv
= dn_dev_get_first(dev
, addr
);
860 read_unlock(&dev_base_lock
);
862 if (rv
== 0 || dev
== init_net
.loopback_dev
)
865 dev
= init_net
.loopback_dev
;
870 static void dn_send_endnode_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
872 struct endnode_hello_message
*msg
;
873 struct sk_buff
*skb
= NULL
;
875 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
877 if ((skb
= dn_alloc_skb(NULL
, sizeof(*msg
), GFP_ATOMIC
)) == NULL
)
882 msg
= (struct endnode_hello_message
*)skb_put(skb
,sizeof(*msg
));
885 memcpy(msg
->tiver
, dn_eco_version
, 3);
886 dn_dn2eth(msg
->id
, ifa
->ifa_local
);
887 msg
->iinfo
= DN_RT_INFO_ENDN
;
888 msg
->blksize
= cpu_to_le16(mtu2blksize(dev
));
890 memset(msg
->seed
, 0, 8);
891 memcpy(msg
->neighbor
, dn_hiord
, ETH_ALEN
);
894 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
895 dn_dn2eth(msg
->neighbor
, dn
->addr
);
898 msg
->timer
= cpu_to_le16((unsigned short)dn_db
->parms
.t3
);
901 memset(msg
->data
, 0xAA, 2);
903 pktlen
= (__le16
*)skb_push(skb
,2);
904 *pktlen
= cpu_to_le16(skb
->len
- 2);
906 skb_reset_network_header(skb
);
908 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, msg
->id
);
912 #define DRDELAY (5 * HZ)
914 static int dn_am_i_a_router(struct dn_neigh
*dn
, struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
916 /* First check time since device went up */
917 if ((jiffies
- dn_db
->uptime
) < DRDELAY
)
920 /* If there is no router, then yes... */
924 /* otherwise only if we have a higher priority or.. */
925 if (dn
->priority
< dn_db
->parms
.priority
)
928 /* if we have equal priority and a higher node number */
929 if (dn
->priority
!= dn_db
->parms
.priority
)
932 if (le16_to_cpu(dn
->addr
) < le16_to_cpu(ifa
->ifa_local
))
938 static void dn_send_router_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
941 struct dn_dev
*dn_db
= dev
->dn_ptr
;
942 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
946 unsigned char *i1
, *i2
;
950 if (mtu2blksize(dev
) < (26 + 7))
953 n
= mtu2blksize(dev
) - 26;
959 size
= 2 + 26 + 7 * n
;
961 if ((skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
)) == NULL
)
965 ptr
= skb_put(skb
, size
);
967 *ptr
++ = DN_RT_PKT_CNTL
| DN_RT_PKT_ERTH
;
968 *ptr
++ = 2; /* ECO */
971 dn_dn2eth(ptr
, ifa
->ifa_local
);
974 *ptr
++ = dn_db
->parms
.forwarding
== 1 ?
975 DN_RT_INFO_L1RT
: DN_RT_INFO_L2RT
;
976 *((__le16
*)ptr
) = cpu_to_le16(mtu2blksize(dev
));
978 *ptr
++ = dn_db
->parms
.priority
; /* Priority */
979 *ptr
++ = 0; /* Area: Reserved */
980 *((__le16
*)ptr
) = cpu_to_le16((unsigned short)dn_db
->parms
.t3
);
982 *ptr
++ = 0; /* MPD: Reserved */
984 memset(ptr
, 0, 7); /* Name: Reserved */
988 n
= dn_neigh_elist(dev
, ptr
, n
);
993 skb_trim(skb
, (27 + *i2
));
995 pktlen
= (__le16
*)skb_push(skb
, 2);
996 *pktlen
= cpu_to_le16(skb
->len
- 2);
998 skb_reset_network_header(skb
);
1000 if (dn_am_i_a_router(dn
, dn_db
, ifa
)) {
1001 struct sk_buff
*skb2
= skb_copy(skb
, GFP_ATOMIC
);
1003 dn_rt_finish_output(skb2
, dn_rt_all_end_mcast
, src
);
1007 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1010 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1012 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1014 if (dn_db
->parms
.forwarding
== 0)
1015 dn_send_endnode_hello(dev
, ifa
);
1017 dn_send_router_hello(dev
, ifa
);
1020 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1023 int size
= dev
->hard_header_len
+ 2 + 4 + tdlen
;
1024 struct sk_buff
*skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
);
1033 skb_push(skb
, dev
->hard_header_len
);
1034 ptr
= skb_put(skb
, 2 + 4 + tdlen
);
1036 *ptr
++ = DN_RT_PKT_HELO
;
1037 *((__le16
*)ptr
) = ifa
->ifa_local
;
1041 for(i
= 0; i
< tdlen
; i
++)
1044 dn_dn2eth(src
, ifa
->ifa_local
);
1045 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1048 static int dn_eth_up(struct net_device
*dev
)
1050 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1052 if (dn_db
->parms
.forwarding
== 0)
1053 dev_mc_add(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1055 dev_mc_add(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1057 dn_db
->use_long
= 1;
1062 static void dn_eth_down(struct net_device
*dev
)
1064 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1066 if (dn_db
->parms
.forwarding
== 0)
1067 dev_mc_delete(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1069 dev_mc_delete(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1072 static void dn_dev_set_timer(struct net_device
*dev
);
1074 static void dn_dev_timer_func(unsigned long arg
)
1076 struct net_device
*dev
= (struct net_device
*)arg
;
1077 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1078 struct dn_ifaddr
*ifa
;
1080 if (dn_db
->t3
<= dn_db
->parms
.t2
) {
1081 if (dn_db
->parms
.timer3
) {
1082 for(ifa
= dn_db
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1083 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
1084 dn_db
->parms
.timer3(dev
, ifa
);
1087 dn_db
->t3
= dn_db
->parms
.t3
;
1089 dn_db
->t3
-= dn_db
->parms
.t2
;
1092 dn_dev_set_timer(dev
);
1095 static void dn_dev_set_timer(struct net_device
*dev
)
1097 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1099 if (dn_db
->parms
.t2
> dn_db
->parms
.t3
)
1100 dn_db
->parms
.t2
= dn_db
->parms
.t3
;
1102 dn_db
->timer
.data
= (unsigned long)dev
;
1103 dn_db
->timer
.function
= dn_dev_timer_func
;
1104 dn_db
->timer
.expires
= jiffies
+ (dn_db
->parms
.t2
* HZ
);
1106 add_timer(&dn_db
->timer
);
1109 static struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
)
1112 struct dn_dev_parms
*p
= dn_dev_list
;
1113 struct dn_dev
*dn_db
;
1115 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++, p
++) {
1116 if (p
->type
== dev
->type
)
1121 if (i
== DN_DEV_LIST_SIZE
)
1125 if ((dn_db
= kzalloc(sizeof(struct dn_dev
), GFP_ATOMIC
)) == NULL
)
1128 memcpy(&dn_db
->parms
, p
, sizeof(struct dn_dev_parms
));
1130 dev
->dn_ptr
= dn_db
;
1132 init_timer(&dn_db
->timer
);
1134 dn_db
->uptime
= jiffies
;
1136 dn_db
->neigh_parms
= neigh_parms_alloc(dev
, &dn_neigh_table
);
1137 if (!dn_db
->neigh_parms
) {
1143 if (dn_db
->parms
.up
) {
1144 if (dn_db
->parms
.up(dev
) < 0) {
1145 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1152 dn_dev_sysctl_register(dev
, &dn_db
->parms
);
1154 dn_dev_set_timer(dev
);
1162 * This processes a device up event. We only start up
1163 * the loopback device & ethernet devices with correct
1164 * MAC addreses automatically. Others must be started
1167 * FIXME: How should we configure the loopback address ? If we could dispense
1168 * with using decnet_address here and for autobind, it will be one less thing
1169 * for users to worry about setting up.
1172 void dn_dev_up(struct net_device
*dev
)
1174 struct dn_ifaddr
*ifa
;
1175 __le16 addr
= decnet_address
;
1176 int maybe_default
= 0;
1177 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1179 if ((dev
->type
!= ARPHRD_ETHER
) && (dev
->type
!= ARPHRD_LOOPBACK
))
1183 * Need to ensure that loopback device has a dn_db attached to it
1184 * to allow creation of neighbours against it, even though it might
1185 * not have a local address of its own. Might as well do the same for
1186 * all autoconfigured interfaces.
1188 if (dn_db
== NULL
) {
1190 dn_db
= dn_dev_create(dev
, &err
);
1195 if (dev
->type
== ARPHRD_ETHER
) {
1196 if (memcmp(dev
->dev_addr
, dn_hiord
, 4) != 0)
1198 addr
= dn_eth2dn(dev
->dev_addr
);
1205 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
1208 ifa
->ifa_local
= ifa
->ifa_address
= addr
;
1210 ifa
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1211 strcpy(ifa
->ifa_label
, dev
->name
);
1213 dn_dev_set_ifa(dev
, ifa
);
1216 * Automagically set the default device to the first automatically
1217 * configured ethernet card in the system.
1219 if (maybe_default
) {
1221 if (dn_dev_set_default(dev
, 0))
1226 static void dn_dev_delete(struct net_device
*dev
)
1228 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1233 del_timer_sync(&dn_db
->timer
);
1234 dn_dev_sysctl_unregister(&dn_db
->parms
);
1235 dn_dev_check_default(dev
);
1236 neigh_ifdown(&dn_neigh_table
, dev
);
1238 if (dn_db
->parms
.down
)
1239 dn_db
->parms
.down(dev
);
1243 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1244 neigh_ifdown(&dn_neigh_table
, dev
);
1247 neigh_release(dn_db
->router
);
1249 neigh_release(dn_db
->peer
);
1254 void dn_dev_down(struct net_device
*dev
)
1256 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1257 struct dn_ifaddr
*ifa
;
1262 while((ifa
= dn_db
->ifa_list
) != NULL
) {
1263 dn_dev_del_ifa(dn_db
, &dn_db
->ifa_list
, 0);
1264 dn_dev_free_ifa(ifa
);
1270 void dn_dev_init_pkt(struct sk_buff
*skb
)
1275 void dn_dev_veri_pkt(struct sk_buff
*skb
)
1280 void dn_dev_hello(struct sk_buff
*skb
)
1285 void dn_dev_devices_off(void)
1287 struct net_device
*dev
;
1290 for_each_netdev(&init_net
, dev
)
1296 void dn_dev_devices_on(void)
1298 struct net_device
*dev
;
1301 for_each_netdev(&init_net
, dev
) {
1302 if (dev
->flags
& IFF_UP
)
1308 int register_dnaddr_notifier(struct notifier_block
*nb
)
1310 return blocking_notifier_chain_register(&dnaddr_chain
, nb
);
1313 int unregister_dnaddr_notifier(struct notifier_block
*nb
)
1315 return blocking_notifier_chain_unregister(&dnaddr_chain
, nb
);
1318 #ifdef CONFIG_PROC_FS
1319 static inline int is_dn_dev(struct net_device
*dev
)
1321 return dev
->dn_ptr
!= NULL
;
1324 static void *dn_dev_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1327 struct net_device
*dev
;
1329 read_lock(&dev_base_lock
);
1332 return SEQ_START_TOKEN
;
1335 for_each_netdev(&init_net
, dev
) {
1336 if (!is_dn_dev(dev
))
1346 static void *dn_dev_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1348 struct net_device
*dev
;
1352 dev
= (struct net_device
*)v
;
1353 if (v
== SEQ_START_TOKEN
)
1354 dev
= net_device_entry(&init_net
.dev_base_head
);
1356 for_each_netdev_continue(&init_net
, dev
) {
1357 if (!is_dn_dev(dev
))
1366 static void dn_dev_seq_stop(struct seq_file
*seq
, void *v
)
1368 read_unlock(&dev_base_lock
);
1371 static char *dn_type2asc(char type
)
1385 static int dn_dev_seq_show(struct seq_file
*seq
, void *v
)
1387 if (v
== SEQ_START_TOKEN
)
1388 seq_puts(seq
, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1390 struct net_device
*dev
= v
;
1391 char peer_buf
[DN_ASCBUF_LEN
];
1392 char router_buf
[DN_ASCBUF_LEN
];
1393 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1395 seq_printf(seq
, "%-8s %1s %04u %04u %04lu %04lu"
1396 " %04hu %03d %02x %-10s %-7s %-7s\n",
1397 dev
->name
? dev
->name
: "???",
1398 dn_type2asc(dn_db
->parms
.mode
),
1400 dn_db
->t3
, dn_db
->parms
.t3
,
1402 dn_db
->parms
.priority
,
1403 dn_db
->parms
.state
, dn_db
->parms
.name
,
1404 dn_db
->router
? dn_addr2asc(le16_to_cpu(*(__le16
*)dn_db
->router
->primary_key
), router_buf
) : "",
1405 dn_db
->peer
? dn_addr2asc(le16_to_cpu(*(__le16
*)dn_db
->peer
->primary_key
), peer_buf
) : "");
1410 static const struct seq_operations dn_dev_seq_ops
= {
1411 .start
= dn_dev_seq_start
,
1412 .next
= dn_dev_seq_next
,
1413 .stop
= dn_dev_seq_stop
,
1414 .show
= dn_dev_seq_show
,
1417 static int dn_dev_seq_open(struct inode
*inode
, struct file
*file
)
1419 return seq_open(file
, &dn_dev_seq_ops
);
1422 static const struct file_operations dn_dev_seq_fops
= {
1423 .owner
= THIS_MODULE
,
1424 .open
= dn_dev_seq_open
,
1426 .llseek
= seq_lseek
,
1427 .release
= seq_release
,
1430 #endif /* CONFIG_PROC_FS */
1433 module_param_array(addr
, int, NULL
, 0444);
1434 MODULE_PARM_DESC(addr
, "The DECnet address of this machine: area,node");
1436 void __init
dn_dev_init(void)
1438 if (addr
[0] > 63 || addr
[0] < 0) {
1439 printk(KERN_ERR
"DECnet: Area must be between 0 and 63");
1443 if (addr
[1] > 1023 || addr
[1] < 0) {
1444 printk(KERN_ERR
"DECnet: Node must be between 0 and 1023");
1448 decnet_address
= cpu_to_le16((addr
[0] << 10) | addr
[1]);
1450 dn_dev_devices_on();
1452 rtnl_register(PF_DECnet
, RTM_NEWADDR
, dn_nl_newaddr
, NULL
);
1453 rtnl_register(PF_DECnet
, RTM_DELADDR
, dn_nl_deladdr
, NULL
);
1454 rtnl_register(PF_DECnet
, RTM_GETADDR
, NULL
, dn_nl_dump_ifaddr
);
1456 proc_net_fops_create(&init_net
, "decnet_dev", S_IRUGO
, &dn_dev_seq_fops
);
1458 #ifdef CONFIG_SYSCTL
1461 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1462 dn_dev_sysctl_register(NULL
, &dn_dev_list
[i
]);
1464 #endif /* CONFIG_SYSCTL */
1467 void __exit
dn_dev_cleanup(void)
1469 #ifdef CONFIG_SYSCTL
1472 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1473 dn_dev_sysctl_unregister(&dn_dev_list
[i
]);
1475 #endif /* CONFIG_SYSCTL */
1477 proc_net_remove(&init_net
, "decnet_dev");
1479 dn_dev_devices_off();