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/config.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_arp.h>
38 #include <linux/if_ether.h>
39 #include <linux/skbuff.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/sysctl.h>
42 #include <linux/notifier.h>
43 #include <asm/uaccess.h>
44 #include <asm/system.h>
45 #include <net/neighbour.h>
49 #include <net/dn_dev.h>
50 #include <net/dn_route.h>
51 #include <net/dn_neigh.h>
52 #include <net/dn_fib.h>
54 #define DN_IFREQ_SIZE (sizeof(struct ifreq) - sizeof(struct sockaddr) + sizeof(struct sockaddr_dn))
56 static char dn_rt_all_end_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x04,0x00,0x00};
57 static char dn_rt_all_rt_mcast
[ETH_ALEN
] = {0xAB,0x00,0x00,0x03,0x00,0x00};
58 static char dn_hiord
[ETH_ALEN
] = {0xAA,0x00,0x04,0x00,0x00,0x00};
59 static unsigned char dn_eco_version
[3] = {0x02,0x00,0x00};
61 extern struct neigh_table dn_neigh_table
;
64 * decnet_address is kept in network order.
66 dn_address decnet_address
= 0;
68 static DEFINE_RWLOCK(dndev_lock
);
69 static struct net_device
*decnet_default_device
;
70 static struct notifier_block
*dnaddr_chain
;
72 static struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
);
73 static void dn_dev_delete(struct net_device
*dev
);
74 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
);
76 static int dn_eth_up(struct net_device
*);
77 static void dn_eth_down(struct net_device
*);
78 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
79 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
);
81 static struct dn_dev_parms dn_dev_list
[] = {
83 .type
= ARPHRD_ETHER
, /* Ethernet */
89 .ctl_name
= NET_DECNET_CONF_ETHER
,
92 .timer3
= dn_send_brd_hello
,
95 .type
= ARPHRD_IPGRE
, /* DECnet tunneled over GRE in IP */
101 .ctl_name
= NET_DECNET_CONF_GRE
,
102 .timer3
= dn_send_brd_hello
,
106 .type
= ARPHRD_X25
, /* Bog standard X.25 */
107 .mode
= DN_DEV_UCAST
,
108 .state
= DN_DEV_S_DS
,
112 .ctl_name
= NET_DECNET_CONF_X25
,
113 .timer3
= dn_send_ptp_hello
,
118 .type
= ARPHRD_PPP
, /* DECnet over PPP */
119 .mode
= DN_DEV_BCAST
,
120 .state
= DN_DEV_S_RU
,
124 .ctl_name
= NET_DECNET_CONF_PPP
,
125 .timer3
= dn_send_brd_hello
,
129 .type
= ARPHRD_DDCMP
, /* DECnet over DDCMP */
130 .mode
= DN_DEV_UCAST
,
131 .state
= DN_DEV_S_DS
,
135 .ctl_name
= NET_DECNET_CONF_DDCMP
,
136 .timer3
= dn_send_ptp_hello
,
139 .type
= ARPHRD_LOOPBACK
, /* Loopback interface - always last */
140 .mode
= DN_DEV_BCAST
,
141 .state
= DN_DEV_S_RU
,
145 .ctl_name
= NET_DECNET_CONF_LOOPBACK
,
146 .timer3
= dn_send_brd_hello
,
150 #define DN_DEV_LIST_SIZE (sizeof(dn_dev_list)/sizeof(struct dn_dev_parms))
152 #define DN_DEV_PARMS_OFFSET(x) ((int) ((char *) &((struct dn_dev_parms *)0)->x))
156 static int min_t2
[] = { 1 };
157 static int max_t2
[] = { 60 }; /* No max specified, but this seems sensible */
158 static int min_t3
[] = { 1 };
159 static int max_t3
[] = { 8191 }; /* Must fit in 16 bits when multiplied by BCT3MULT or T3MULT */
161 static int min_priority
[1];
162 static int max_priority
[] = { 127 }; /* From DECnet spec */
164 static int dn_forwarding_proc(ctl_table
*, int, struct file
*,
165 void __user
*, size_t *, loff_t
*);
166 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
167 void __user
*oldval
, size_t __user
*oldlenp
,
168 void __user
*newval
, size_t newlen
,
171 static struct dn_dev_sysctl_table
{
172 struct ctl_table_header
*sysctl_header
;
173 ctl_table dn_dev_vars
[5];
174 ctl_table dn_dev_dev
[2];
175 ctl_table dn_dev_conf_dir
[2];
176 ctl_table dn_dev_proto_dir
[2];
177 ctl_table dn_dev_root_dir
[2];
182 .ctl_name
= NET_DECNET_CONF_DEV_FORWARDING
,
183 .procname
= "forwarding",
184 .data
= (void *)DN_DEV_PARMS_OFFSET(forwarding
),
185 .maxlen
= sizeof(int),
187 .proc_handler
= dn_forwarding_proc
,
188 .strategy
= dn_forwarding_sysctl
,
191 .ctl_name
= NET_DECNET_CONF_DEV_PRIORITY
,
192 .procname
= "priority",
193 .data
= (void *)DN_DEV_PARMS_OFFSET(priority
),
194 .maxlen
= sizeof(int),
196 .proc_handler
= proc_dointvec_minmax
,
197 .strategy
= sysctl_intvec
,
198 .extra1
= &min_priority
,
199 .extra2
= &max_priority
202 .ctl_name
= NET_DECNET_CONF_DEV_T2
,
204 .data
= (void *)DN_DEV_PARMS_OFFSET(t2
),
205 .maxlen
= sizeof(int),
207 .proc_handler
= proc_dointvec_minmax
,
208 .strategy
= sysctl_intvec
,
213 .ctl_name
= NET_DECNET_CONF_DEV_T3
,
215 .data
= (void *)DN_DEV_PARMS_OFFSET(t3
),
216 .maxlen
= sizeof(int),
218 .proc_handler
= proc_dointvec_minmax
,
219 .strategy
= sysctl_intvec
,
229 .child
= dn_dev_sysctl
.dn_dev_vars
232 .ctl_name
= NET_DECNET_CONF
,
235 .child
= dn_dev_sysctl
.dn_dev_dev
238 .ctl_name
= NET_DECNET
,
239 .procname
= "decnet",
241 .child
= dn_dev_sysctl
.dn_dev_conf_dir
247 .child
= dn_dev_sysctl
.dn_dev_proto_dir
251 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
253 struct dn_dev_sysctl_table
*t
;
256 t
= kmalloc(sizeof(*t
), GFP_KERNEL
);
260 memcpy(t
, &dn_dev_sysctl
, sizeof(*t
));
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
;
265 t
->dn_dev_vars
[i
].de
= NULL
;
269 t
->dn_dev_dev
[0].procname
= dev
->name
;
270 t
->dn_dev_dev
[0].ctl_name
= dev
->ifindex
;
272 t
->dn_dev_dev
[0].procname
= parms
->name
;
273 t
->dn_dev_dev
[0].ctl_name
= parms
->ctl_name
;
276 t
->dn_dev_dev
[0].child
= t
->dn_dev_vars
;
277 t
->dn_dev_dev
[0].de
= NULL
;
278 t
->dn_dev_conf_dir
[0].child
= t
->dn_dev_dev
;
279 t
->dn_dev_conf_dir
[0].de
= NULL
;
280 t
->dn_dev_proto_dir
[0].child
= t
->dn_dev_conf_dir
;
281 t
->dn_dev_proto_dir
[0].de
= NULL
;
282 t
->dn_dev_root_dir
[0].child
= t
->dn_dev_proto_dir
;
283 t
->dn_dev_root_dir
[0].de
= NULL
;
284 t
->dn_dev_vars
[0].extra1
= (void *)dev
;
286 t
->sysctl_header
= register_sysctl_table(t
->dn_dev_root_dir
, 0);
287 if (t
->sysctl_header
== NULL
)
293 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
296 struct dn_dev_sysctl_table
*t
= parms
->sysctl
;
297 parms
->sysctl
= NULL
;
298 unregister_sysctl_table(t
->sysctl_header
);
303 static int dn_forwarding_proc(ctl_table
*table
, int write
,
306 size_t *lenp
, loff_t
*ppos
)
308 #ifdef CONFIG_DECNET_ROUTER
309 struct net_device
*dev
= table
->extra1
;
310 struct dn_dev
*dn_db
;
314 if (table
->extra1
== NULL
)
318 old
= dn_db
->parms
.forwarding
;
320 err
= proc_dointvec(table
, write
, filep
, buffer
, lenp
, ppos
);
322 if ((err
>= 0) && write
) {
323 if (dn_db
->parms
.forwarding
< 0)
324 dn_db
->parms
.forwarding
= 0;
325 if (dn_db
->parms
.forwarding
> 2)
326 dn_db
->parms
.forwarding
= 2;
328 * What an ugly hack this is... its works, just. It
329 * would be nice if sysctl/proc were just that little
330 * bit more flexible so I don't have to write a special
331 * routine, or suffer hacks like this - SJW
333 tmp
= dn_db
->parms
.forwarding
;
334 dn_db
->parms
.forwarding
= old
;
335 if (dn_db
->parms
.down
)
336 dn_db
->parms
.down(dev
);
337 dn_db
->parms
.forwarding
= tmp
;
339 dn_db
->parms
.up(dev
);
348 static int dn_forwarding_sysctl(ctl_table
*table
, int __user
*name
, int nlen
,
349 void __user
*oldval
, size_t __user
*oldlenp
,
350 void __user
*newval
, size_t newlen
,
353 #ifdef CONFIG_DECNET_ROUTER
354 struct net_device
*dev
= table
->extra1
;
355 struct dn_dev
*dn_db
;
358 if (table
->extra1
== NULL
)
363 if (newval
&& newlen
) {
364 if (newlen
!= sizeof(int))
367 if (get_user(value
, (int __user
*)newval
))
374 if (dn_db
->parms
.down
)
375 dn_db
->parms
.down(dev
);
376 dn_db
->parms
.forwarding
= value
;
378 dn_db
->parms
.up(dev
);
387 #else /* CONFIG_SYSCTL */
388 static void dn_dev_sysctl_unregister(struct dn_dev_parms
*parms
)
391 static void dn_dev_sysctl_register(struct net_device
*dev
, struct dn_dev_parms
*parms
)
395 #endif /* CONFIG_SYSCTL */
397 static inline __u16
mtu2blksize(struct net_device
*dev
)
399 u32 blksize
= dev
->mtu
;
400 if (blksize
> 0xffff)
403 if (dev
->type
== ARPHRD_ETHER
||
404 dev
->type
== ARPHRD_PPP
||
405 dev
->type
== ARPHRD_IPGRE
||
406 dev
->type
== ARPHRD_LOOPBACK
)
409 return (__u16
)blksize
;
412 static struct dn_ifaddr
*dn_dev_alloc_ifa(void)
414 struct dn_ifaddr
*ifa
;
416 ifa
= kmalloc(sizeof(*ifa
), GFP_KERNEL
);
419 memset(ifa
, 0, sizeof(*ifa
));
425 static __inline__
void dn_dev_free_ifa(struct dn_ifaddr
*ifa
)
430 static void dn_dev_del_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
**ifap
, int destroy
)
432 struct dn_ifaddr
*ifa1
= *ifap
;
433 unsigned char mac_addr
[6];
434 struct net_device
*dev
= dn_db
->dev
;
438 *ifap
= ifa1
->ifa_next
;
440 if (dn_db
->dev
->type
== ARPHRD_ETHER
) {
441 if (ifa1
->ifa_local
!= dn_htons(dn_eth2dn(dev
->dev_addr
))) {
442 dn_dn2eth(mac_addr
, ifa1
->ifa_local
);
443 dev_mc_delete(dev
, mac_addr
, ETH_ALEN
, 0);
447 rtmsg_ifa(RTM_DELADDR
, ifa1
);
448 notifier_call_chain(&dnaddr_chain
, NETDEV_DOWN
, ifa1
);
450 dn_dev_free_ifa(ifa1
);
452 if (dn_db
->ifa_list
== NULL
)
453 dn_dev_delete(dn_db
->dev
);
457 static int dn_dev_insert_ifa(struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
459 struct net_device
*dev
= dn_db
->dev
;
460 struct dn_ifaddr
*ifa1
;
461 unsigned char mac_addr
[6];
465 /* Check for duplicates */
466 for(ifa1
= dn_db
->ifa_list
; ifa1
; ifa1
= ifa1
->ifa_next
) {
467 if (ifa1
->ifa_local
== ifa
->ifa_local
)
471 if (dev
->type
== ARPHRD_ETHER
) {
472 if (ifa
->ifa_local
!= dn_htons(dn_eth2dn(dev
->dev_addr
))) {
473 dn_dn2eth(mac_addr
, ifa
->ifa_local
);
474 dev_mc_add(dev
, mac_addr
, ETH_ALEN
, 0);
479 ifa
->ifa_next
= dn_db
->ifa_list
;
480 dn_db
->ifa_list
= ifa
;
482 rtmsg_ifa(RTM_NEWADDR
, ifa
);
483 notifier_call_chain(&dnaddr_chain
, NETDEV_UP
, ifa
);
488 static int dn_dev_set_ifa(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
490 struct dn_dev
*dn_db
= dev
->dn_ptr
;
495 dn_db
= dn_dev_create(dev
, &err
);
500 ifa
->ifa_dev
= dn_db
;
502 if (dev
->flags
& IFF_LOOPBACK
)
503 ifa
->ifa_scope
= RT_SCOPE_HOST
;
505 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
507 dn_dev_free_ifa(ifa
);
512 int dn_dev_ioctl(unsigned int cmd
, void __user
*arg
)
514 char buffer
[DN_IFREQ_SIZE
];
515 struct ifreq
*ifr
= (struct ifreq
*)buffer
;
516 struct sockaddr_dn
*sdn
= (struct sockaddr_dn
*)&ifr
->ifr_addr
;
517 struct dn_dev
*dn_db
;
518 struct net_device
*dev
;
519 struct dn_ifaddr
*ifa
= NULL
, **ifap
= NULL
;
522 if (copy_from_user(ifr
, arg
, DN_IFREQ_SIZE
))
524 ifr
->ifr_name
[IFNAMSIZ
-1] = 0;
527 dev_load(ifr
->ifr_name
);
534 if (!capable(CAP_NET_ADMIN
))
536 if (sdn
->sdn_family
!= AF_DECnet
)
545 if ((dev
= __dev_get_by_name(ifr
->ifr_name
)) == NULL
) {
550 if ((dn_db
= dev
->dn_ptr
) != NULL
) {
551 for (ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
)
552 if (strcmp(ifr
->ifr_name
, ifa
->ifa_label
) == 0)
556 if (ifa
== NULL
&& cmd
!= SIOCSIFADDR
) {
557 ret
= -EADDRNOTAVAIL
;
563 *((dn_address
*)sdn
->sdn_nodeaddr
) = ifa
->ifa_local
;
568 if ((ifa
= dn_dev_alloc_ifa()) == NULL
) {
572 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
574 if (ifa
->ifa_local
== dn_saddr2dn(sdn
))
576 dn_dev_del_ifa(dn_db
, ifap
, 0);
579 ifa
->ifa_local
= ifa
->ifa_address
= dn_saddr2dn(sdn
);
581 ret
= dn_dev_set_ifa(dev
, ifa
);
588 if (copy_to_user(arg
, ifr
, DN_IFREQ_SIZE
))
593 struct net_device
*dn_dev_get_default(void)
595 struct net_device
*dev
;
596 read_lock(&dndev_lock
);
597 dev
= decnet_default_device
;
604 read_unlock(&dndev_lock
);
608 int dn_dev_set_default(struct net_device
*dev
, int force
)
610 struct net_device
*old
= NULL
;
614 write_lock(&dndev_lock
);
615 if (force
|| decnet_default_device
== NULL
) {
616 old
= decnet_default_device
;
617 decnet_default_device
= dev
;
620 write_unlock(&dndev_lock
);
626 static void dn_dev_check_default(struct net_device
*dev
)
628 write_lock(&dndev_lock
);
629 if (dev
== decnet_default_device
) {
630 decnet_default_device
= NULL
;
634 write_unlock(&dndev_lock
);
639 static struct dn_dev
*dn_dev_by_index(int ifindex
)
641 struct net_device
*dev
;
642 struct dn_dev
*dn_dev
= NULL
;
643 dev
= dev_get_by_index(ifindex
);
645 dn_dev
= dev
->dn_ptr
;
652 static int dn_dev_rtm_deladdr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
654 struct rtattr
**rta
= arg
;
655 struct dn_dev
*dn_db
;
656 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
657 struct dn_ifaddr
*ifa
, **ifap
;
659 if ((dn_db
= dn_dev_by_index(ifm
->ifa_index
)) == NULL
)
660 return -EADDRNOTAVAIL
;
662 for(ifap
= &dn_db
->ifa_list
; (ifa
=*ifap
) != NULL
; ifap
= &ifa
->ifa_next
) {
663 void *tmp
= rta
[IFA_LOCAL
-1];
664 if ((tmp
&& memcmp(RTA_DATA(tmp
), &ifa
->ifa_local
, 2)) ||
665 (rta
[IFA_LABEL
-1] && rtattr_strcmp(rta
[IFA_LABEL
-1], ifa
->ifa_label
)))
668 dn_dev_del_ifa(dn_db
, ifap
, 1);
672 return -EADDRNOTAVAIL
;
675 static int dn_dev_rtm_newaddr(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
677 struct rtattr
**rta
= arg
;
678 struct net_device
*dev
;
679 struct dn_dev
*dn_db
;
680 struct ifaddrmsg
*ifm
= NLMSG_DATA(nlh
);
681 struct dn_ifaddr
*ifa
;
684 if (rta
[IFA_LOCAL
-1] == NULL
)
687 if ((dev
= __dev_get_by_index(ifm
->ifa_index
)) == NULL
)
690 if ((dn_db
= dev
->dn_ptr
) == NULL
) {
692 dn_db
= dn_dev_create(dev
, &err
);
697 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
700 if (!rta
[IFA_ADDRESS
- 1])
701 rta
[IFA_ADDRESS
- 1] = rta
[IFA_LOCAL
- 1];
702 memcpy(&ifa
->ifa_local
, RTA_DATA(rta
[IFA_LOCAL
-1]), 2);
703 memcpy(&ifa
->ifa_address
, RTA_DATA(rta
[IFA_ADDRESS
-1]), 2);
704 ifa
->ifa_flags
= ifm
->ifa_flags
;
705 ifa
->ifa_scope
= ifm
->ifa_scope
;
706 ifa
->ifa_dev
= dn_db
;
707 if (rta
[IFA_LABEL
-1])
708 rtattr_strlcpy(ifa
->ifa_label
, rta
[IFA_LABEL
-1], IFNAMSIZ
);
710 memcpy(ifa
->ifa_label
, dev
->name
, IFNAMSIZ
);
712 rv
= dn_dev_insert_ifa(dn_db
, ifa
);
714 dn_dev_free_ifa(ifa
);
718 static int dn_dev_fill_ifaddr(struct sk_buff
*skb
, struct dn_ifaddr
*ifa
,
719 u32 pid
, u32 seq
, int event
)
721 struct ifaddrmsg
*ifm
;
722 struct nlmsghdr
*nlh
;
723 unsigned char *b
= skb
->tail
;
725 nlh
= NLMSG_PUT(skb
, pid
, seq
, event
, sizeof(*ifm
));
726 ifm
= NLMSG_DATA(nlh
);
728 ifm
->ifa_family
= AF_DECnet
;
729 ifm
->ifa_prefixlen
= 16;
730 ifm
->ifa_flags
= ifa
->ifa_flags
| IFA_F_PERMANENT
;
731 ifm
->ifa_scope
= ifa
->ifa_scope
;
732 ifm
->ifa_index
= ifa
->ifa_dev
->dev
->ifindex
;
733 if (ifa
->ifa_address
)
734 RTA_PUT(skb
, IFA_ADDRESS
, 2, &ifa
->ifa_address
);
736 RTA_PUT(skb
, IFA_LOCAL
, 2, &ifa
->ifa_local
);
737 if (ifa
->ifa_label
[0])
738 RTA_PUT(skb
, IFA_LABEL
, IFNAMSIZ
, &ifa
->ifa_label
);
739 nlh
->nlmsg_len
= skb
->tail
- b
;
744 skb_trim(skb
, b
- skb
->data
);
748 static void rtmsg_ifa(int event
, struct dn_ifaddr
*ifa
)
751 int size
= NLMSG_SPACE(sizeof(struct ifaddrmsg
)+128);
753 skb
= alloc_skb(size
, GFP_KERNEL
);
755 netlink_set_err(rtnl
, 0, RTMGRP_DECnet_IFADDR
, ENOBUFS
);
758 if (dn_dev_fill_ifaddr(skb
, ifa
, 0, 0, event
) < 0) {
760 netlink_set_err(rtnl
, 0, RTMGRP_DECnet_IFADDR
, EINVAL
);
763 NETLINK_CB(skb
).dst_groups
= RTMGRP_DECnet_IFADDR
;
764 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_DECnet_IFADDR
, GFP_KERNEL
);
767 static int dn_dev_dump_ifaddr(struct sk_buff
*skb
, struct netlink_callback
*cb
)
771 struct net_device
*dev
;
772 struct dn_dev
*dn_db
;
773 struct dn_ifaddr
*ifa
;
776 s_dn_idx
= dn_idx
= cb
->args
[1];
777 read_lock(&dev_base_lock
);
778 for(dev
= dev_base
, idx
= 0; dev
; dev
= dev
->next
, idx
++) {
783 if ((dn_db
= dev
->dn_ptr
) == NULL
)
786 for(ifa
= dn_db
->ifa_list
, dn_idx
= 0; ifa
; ifa
= ifa
->ifa_next
, dn_idx
++) {
787 if (dn_idx
< s_dn_idx
)
790 if (dn_dev_fill_ifaddr(skb
, ifa
,
791 NETLINK_CB(cb
->skb
).pid
,
798 read_unlock(&dev_base_lock
);
800 cb
->args
[1] = dn_idx
;
805 static int dn_dev_get_first(struct net_device
*dev
, dn_address
*addr
)
807 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
808 struct dn_ifaddr
*ifa
;
812 ifa
= dn_db
->ifa_list
;
814 *addr
= ifa
->ifa_local
;
822 * Find a default address to bind to.
824 * This is one of those areas where the initial VMS concepts don't really
825 * map onto the Linux concepts, and since we introduced multiple addresses
826 * per interface we have to cope with slightly odd ways of finding out what
827 * "our address" really is. Mostly it's not a problem; for this we just guess
828 * a sensible default. Eventually the routing code will take care of all the
829 * nasties for us I hope.
831 int dn_dev_bind_default(dn_address
*addr
)
833 struct net_device
*dev
;
835 dev
= dn_dev_get_default();
838 read_lock(&dev_base_lock
);
839 rv
= dn_dev_get_first(dev
, addr
);
840 read_unlock(&dev_base_lock
);
842 if (rv
== 0 || dev
== &loopback_dev
)
850 static void dn_send_endnode_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
852 struct endnode_hello_message
*msg
;
853 struct sk_buff
*skb
= NULL
;
854 unsigned short int *pktlen
;
855 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
857 if ((skb
= dn_alloc_skb(NULL
, sizeof(*msg
), GFP_ATOMIC
)) == NULL
)
862 msg
= (struct endnode_hello_message
*)skb_put(skb
,sizeof(*msg
));
865 memcpy(msg
->tiver
, dn_eco_version
, 3);
866 dn_dn2eth(msg
->id
, ifa
->ifa_local
);
867 msg
->iinfo
= DN_RT_INFO_ENDN
;
868 msg
->blksize
= dn_htons(mtu2blksize(dev
));
870 memset(msg
->seed
, 0, 8);
871 memcpy(msg
->neighbor
, dn_hiord
, ETH_ALEN
);
874 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
875 dn_dn2eth(msg
->neighbor
, dn
->addr
);
878 msg
->timer
= dn_htons((unsigned short)dn_db
->parms
.t3
);
881 memset(msg
->data
, 0xAA, 2);
883 pktlen
= (unsigned short *)skb_push(skb
,2);
884 *pktlen
= dn_htons(skb
->len
- 2);
886 skb
->nh
.raw
= skb
->data
;
888 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, msg
->id
);
892 #define DRDELAY (5 * HZ)
894 static int dn_am_i_a_router(struct dn_neigh
*dn
, struct dn_dev
*dn_db
, struct dn_ifaddr
*ifa
)
896 /* First check time since device went up */
897 if ((jiffies
- dn_db
->uptime
) < DRDELAY
)
900 /* If there is no router, then yes... */
904 /* otherwise only if we have a higher priority or.. */
905 if (dn
->priority
< dn_db
->parms
.priority
)
908 /* if we have equal priority and a higher node number */
909 if (dn
->priority
!= dn_db
->parms
.priority
)
912 if (dn_ntohs(dn
->addr
) < dn_ntohs(ifa
->ifa_local
))
918 static void dn_send_router_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
921 struct dn_dev
*dn_db
= dev
->dn_ptr
;
922 struct dn_neigh
*dn
= (struct dn_neigh
*)dn_db
->router
;
926 unsigned char *i1
, *i2
;
927 unsigned short *pktlen
;
930 if (mtu2blksize(dev
) < (26 + 7))
933 n
= mtu2blksize(dev
) - 26;
939 size
= 2 + 26 + 7 * n
;
941 if ((skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
)) == NULL
)
945 ptr
= skb_put(skb
, size
);
947 *ptr
++ = DN_RT_PKT_CNTL
| DN_RT_PKT_ERTH
;
948 *ptr
++ = 2; /* ECO */
951 dn_dn2eth(ptr
, ifa
->ifa_local
);
954 *ptr
++ = dn_db
->parms
.forwarding
== 1 ?
955 DN_RT_INFO_L1RT
: DN_RT_INFO_L2RT
;
956 *((unsigned short *)ptr
) = dn_htons(mtu2blksize(dev
));
958 *ptr
++ = dn_db
->parms
.priority
; /* Priority */
959 *ptr
++ = 0; /* Area: Reserved */
960 *((unsigned short *)ptr
) = dn_htons((unsigned short)dn_db
->parms
.t3
);
962 *ptr
++ = 0; /* MPD: Reserved */
964 memset(ptr
, 0, 7); /* Name: Reserved */
968 n
= dn_neigh_elist(dev
, ptr
, n
);
973 skb_trim(skb
, (27 + *i2
));
975 pktlen
= (unsigned short *)skb_push(skb
, 2);
976 *pktlen
= dn_htons(skb
->len
- 2);
978 skb
->nh
.raw
= skb
->data
;
980 if (dn_am_i_a_router(dn
, dn_db
, ifa
)) {
981 struct sk_buff
*skb2
= skb_copy(skb
, GFP_ATOMIC
);
983 dn_rt_finish_output(skb2
, dn_rt_all_end_mcast
, src
);
987 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
990 static void dn_send_brd_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
992 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
994 if (dn_db
->parms
.forwarding
== 0)
995 dn_send_endnode_hello(dev
, ifa
);
997 dn_send_router_hello(dev
, ifa
);
1000 static void dn_send_ptp_hello(struct net_device
*dev
, struct dn_ifaddr
*ifa
)
1003 int size
= dev
->hard_header_len
+ 2 + 4 + tdlen
;
1004 struct sk_buff
*skb
= dn_alloc_skb(NULL
, size
, GFP_ATOMIC
);
1013 skb_push(skb
, dev
->hard_header_len
);
1014 ptr
= skb_put(skb
, 2 + 4 + tdlen
);
1016 *ptr
++ = DN_RT_PKT_HELO
;
1017 *((dn_address
*)ptr
) = ifa
->ifa_local
;
1021 for(i
= 0; i
< tdlen
; i
++)
1024 dn_dn2eth(src
, ifa
->ifa_local
);
1025 dn_rt_finish_output(skb
, dn_rt_all_rt_mcast
, src
);
1028 static int dn_eth_up(struct net_device
*dev
)
1030 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1032 if (dn_db
->parms
.forwarding
== 0)
1033 dev_mc_add(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1035 dev_mc_add(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1039 dn_db
->use_long
= 1;
1044 static void dn_eth_down(struct net_device
*dev
)
1046 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1048 if (dn_db
->parms
.forwarding
== 0)
1049 dev_mc_delete(dev
, dn_rt_all_end_mcast
, ETH_ALEN
, 0);
1051 dev_mc_delete(dev
, dn_rt_all_rt_mcast
, ETH_ALEN
, 0);
1054 static void dn_dev_set_timer(struct net_device
*dev
);
1056 static void dn_dev_timer_func(unsigned long arg
)
1058 struct net_device
*dev
= (struct net_device
*)arg
;
1059 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1060 struct dn_ifaddr
*ifa
;
1062 if (dn_db
->t3
<= dn_db
->parms
.t2
) {
1063 if (dn_db
->parms
.timer3
) {
1064 for(ifa
= dn_db
->ifa_list
; ifa
; ifa
= ifa
->ifa_next
) {
1065 if (!(ifa
->ifa_flags
& IFA_F_SECONDARY
))
1066 dn_db
->parms
.timer3(dev
, ifa
);
1069 dn_db
->t3
= dn_db
->parms
.t3
;
1071 dn_db
->t3
-= dn_db
->parms
.t2
;
1074 dn_dev_set_timer(dev
);
1077 static void dn_dev_set_timer(struct net_device
*dev
)
1079 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1081 if (dn_db
->parms
.t2
> dn_db
->parms
.t3
)
1082 dn_db
->parms
.t2
= dn_db
->parms
.t3
;
1084 dn_db
->timer
.data
= (unsigned long)dev
;
1085 dn_db
->timer
.function
= dn_dev_timer_func
;
1086 dn_db
->timer
.expires
= jiffies
+ (dn_db
->parms
.t2
* HZ
);
1088 add_timer(&dn_db
->timer
);
1091 struct dn_dev
*dn_dev_create(struct net_device
*dev
, int *err
)
1094 struct dn_dev_parms
*p
= dn_dev_list
;
1095 struct dn_dev
*dn_db
;
1097 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++, p
++) {
1098 if (p
->type
== dev
->type
)
1103 if (i
== DN_DEV_LIST_SIZE
)
1107 if ((dn_db
= kmalloc(sizeof(struct dn_dev
), GFP_ATOMIC
)) == NULL
)
1110 memset(dn_db
, 0, sizeof(struct dn_dev
));
1111 memcpy(&dn_db
->parms
, p
, sizeof(struct dn_dev_parms
));
1113 dev
->dn_ptr
= dn_db
;
1115 init_timer(&dn_db
->timer
);
1117 dn_db
->uptime
= jiffies
;
1118 if (dn_db
->parms
.up
) {
1119 if (dn_db
->parms
.up(dev
) < 0) {
1126 dn_db
->neigh_parms
= neigh_parms_alloc(dev
, &dn_neigh_table
);
1128 dn_dev_sysctl_register(dev
, &dn_db
->parms
);
1130 dn_dev_set_timer(dev
);
1138 * This processes a device up event. We only start up
1139 * the loopback device & ethernet devices with correct
1140 * MAC addreses automatically. Others must be started
1143 * FIXME: How should we configure the loopback address ? If we could dispense
1144 * with using decnet_address here and for autobind, it will be one less thing
1145 * for users to worry about setting up.
1148 void dn_dev_up(struct net_device
*dev
)
1150 struct dn_ifaddr
*ifa
;
1151 dn_address addr
= decnet_address
;
1152 int maybe_default
= 0;
1153 struct dn_dev
*dn_db
= (struct dn_dev
*)dev
->dn_ptr
;
1155 if ((dev
->type
!= ARPHRD_ETHER
) && (dev
->type
!= ARPHRD_LOOPBACK
))
1159 * Need to ensure that loopback device has a dn_db attached to it
1160 * to allow creation of neighbours against it, even though it might
1161 * not have a local address of its own. Might as well do the same for
1162 * all autoconfigured interfaces.
1164 if (dn_db
== NULL
) {
1166 dn_db
= dn_dev_create(dev
, &err
);
1171 if (dev
->type
== ARPHRD_ETHER
) {
1172 if (memcmp(dev
->dev_addr
, dn_hiord
, 4) != 0)
1174 addr
= dn_htons(dn_eth2dn(dev
->dev_addr
));
1181 if ((ifa
= dn_dev_alloc_ifa()) == NULL
)
1184 ifa
->ifa_local
= ifa
->ifa_address
= addr
;
1186 ifa
->ifa_scope
= RT_SCOPE_UNIVERSE
;
1187 strcpy(ifa
->ifa_label
, dev
->name
);
1189 dn_dev_set_ifa(dev
, ifa
);
1192 * Automagically set the default device to the first automatically
1193 * configured ethernet card in the system.
1195 if (maybe_default
) {
1197 if (dn_dev_set_default(dev
, 0))
1202 static void dn_dev_delete(struct net_device
*dev
)
1204 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1209 del_timer_sync(&dn_db
->timer
);
1210 dn_dev_sysctl_unregister(&dn_db
->parms
);
1211 dn_dev_check_default(dev
);
1212 neigh_ifdown(&dn_neigh_table
, dev
);
1214 if (dn_db
->parms
.down
)
1215 dn_db
->parms
.down(dev
);
1219 neigh_parms_release(&dn_neigh_table
, dn_db
->neigh_parms
);
1220 neigh_ifdown(&dn_neigh_table
, dev
);
1223 neigh_release(dn_db
->router
);
1225 neigh_release(dn_db
->peer
);
1230 void dn_dev_down(struct net_device
*dev
)
1232 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1233 struct dn_ifaddr
*ifa
;
1238 while((ifa
= dn_db
->ifa_list
) != NULL
) {
1239 dn_dev_del_ifa(dn_db
, &dn_db
->ifa_list
, 0);
1240 dn_dev_free_ifa(ifa
);
1246 void dn_dev_init_pkt(struct sk_buff
*skb
)
1251 void dn_dev_veri_pkt(struct sk_buff
*skb
)
1256 void dn_dev_hello(struct sk_buff
*skb
)
1261 void dn_dev_devices_off(void)
1263 struct net_device
*dev
;
1266 for(dev
= dev_base
; dev
; dev
= dev
->next
)
1272 void dn_dev_devices_on(void)
1274 struct net_device
*dev
;
1277 for(dev
= dev_base
; dev
; dev
= dev
->next
) {
1278 if (dev
->flags
& IFF_UP
)
1284 int register_dnaddr_notifier(struct notifier_block
*nb
)
1286 return notifier_chain_register(&dnaddr_chain
, nb
);
1289 int unregister_dnaddr_notifier(struct notifier_block
*nb
)
1291 return notifier_chain_unregister(&dnaddr_chain
, nb
);
1294 #ifdef CONFIG_PROC_FS
1295 static inline struct net_device
*dn_dev_get_next(struct seq_file
*seq
, struct net_device
*dev
)
1299 } while(dev
&& !dev
->dn_ptr
);
1304 static struct net_device
*dn_dev_get_idx(struct seq_file
*seq
, loff_t pos
)
1306 struct net_device
*dev
;
1309 if (dev
&& !dev
->dn_ptr
)
1310 dev
= dn_dev_get_next(seq
, dev
);
1312 while(dev
&& (dev
= dn_dev_get_next(seq
, dev
)))
1318 static void *dn_dev_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1321 struct net_device
*dev
;
1322 read_lock(&dev_base_lock
);
1323 dev
= dn_dev_get_idx(seq
, *pos
- 1);
1325 read_unlock(&dev_base_lock
);
1328 return SEQ_START_TOKEN
;
1331 static void *dn_dev_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1333 struct net_device
*dev
= v
;
1336 if (v
== SEQ_START_TOKEN
) {
1337 dev
= dn_dev_seq_start(seq
, &one
);
1339 dev
= dn_dev_get_next(seq
, dev
);
1341 read_unlock(&dev_base_lock
);
1347 static void dn_dev_seq_stop(struct seq_file
*seq
, void *v
)
1349 if (v
&& v
!= SEQ_START_TOKEN
)
1350 read_unlock(&dev_base_lock
);
1353 static char *dn_type2asc(char type
)
1367 static int dn_dev_seq_show(struct seq_file
*seq
, void *v
)
1369 if (v
== SEQ_START_TOKEN
)
1370 seq_puts(seq
, "Name Flags T1 Timer1 T3 Timer3 BlkSize Pri State DevType Router Peer\n");
1372 struct net_device
*dev
= v
;
1373 char peer_buf
[DN_ASCBUF_LEN
];
1374 char router_buf
[DN_ASCBUF_LEN
];
1375 struct dn_dev
*dn_db
= dev
->dn_ptr
;
1377 seq_printf(seq
, "%-8s %1s %04u %04u %04lu %04lu"
1378 " %04hu %03d %02x %-10s %-7s %-7s\n",
1379 dev
->name
? dev
->name
: "???",
1380 dn_type2asc(dn_db
->parms
.mode
),
1382 dn_db
->t3
, dn_db
->parms
.t3
,
1384 dn_db
->parms
.priority
,
1385 dn_db
->parms
.state
, dn_db
->parms
.name
,
1386 dn_db
->router
? dn_addr2asc(dn_ntohs(*(dn_address
*)dn_db
->router
->primary_key
), router_buf
) : "",
1387 dn_db
->peer
? dn_addr2asc(dn_ntohs(*(dn_address
*)dn_db
->peer
->primary_key
), peer_buf
) : "");
1392 static struct seq_operations dn_dev_seq_ops
= {
1393 .start
= dn_dev_seq_start
,
1394 .next
= dn_dev_seq_next
,
1395 .stop
= dn_dev_seq_stop
,
1396 .show
= dn_dev_seq_show
,
1399 static int dn_dev_seq_open(struct inode
*inode
, struct file
*file
)
1401 return seq_open(file
, &dn_dev_seq_ops
);
1404 static struct file_operations dn_dev_seq_fops
= {
1405 .owner
= THIS_MODULE
,
1406 .open
= dn_dev_seq_open
,
1408 .llseek
= seq_lseek
,
1409 .release
= seq_release
,
1412 #endif /* CONFIG_PROC_FS */
1414 static struct rtnetlink_link dnet_rtnetlink_table
[RTM_NR_MSGTYPES
] =
1416 [RTM_NEWADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_newaddr
, },
1417 [RTM_DELADDR
- RTM_BASE
] = { .doit
= dn_dev_rtm_deladdr
, },
1418 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= dn_dev_dump_ifaddr
, },
1419 #ifdef CONFIG_DECNET_ROUTER
1420 [RTM_NEWROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_newroute
, },
1421 [RTM_DELROUTE
- RTM_BASE
] = { .doit
= dn_fib_rtm_delroute
, },
1422 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1423 .dumpit
= dn_fib_dump
, },
1424 [RTM_NEWRULE
- RTM_BASE
] = { .doit
= dn_fib_rtm_newrule
, },
1425 [RTM_DELRULE
- RTM_BASE
] = { .doit
= dn_fib_rtm_delrule
, },
1426 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= dn_fib_dump_rules
, },
1428 [RTM_GETROUTE
- RTM_BASE
] = { .doit
= dn_cache_getroute
,
1429 .dumpit
= dn_cache_dump
, },
1434 static int __initdata addr
[2];
1435 module_param_array(addr
, int, NULL
, 0444);
1436 MODULE_PARM_DESC(addr
, "The DECnet address of this machine: area,node");
1438 void __init
dn_dev_init(void)
1440 if (addr
[0] > 63 || addr
[0] < 0) {
1441 printk(KERN_ERR
"DECnet: Area must be between 0 and 63");
1445 if (addr
[1] > 1023 || addr
[1] < 0) {
1446 printk(KERN_ERR
"DECnet: Node must be between 0 and 1023");
1450 decnet_address
= dn_htons((addr
[0] << 10) | addr
[1]);
1452 dn_dev_devices_on();
1454 rtnetlink_links
[PF_DECnet
] = dnet_rtnetlink_table
;
1456 proc_net_fops_create("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 rtnetlink_links
[PF_DECnet
] = NULL
;
1471 #ifdef CONFIG_SYSCTL
1474 for(i
= 0; i
< DN_DEV_LIST_SIZE
; i
++)
1475 dn_dev_sysctl_unregister(&dn_dev_list
[i
]);
1477 #endif /* CONFIG_SYSCTL */
1479 proc_net_remove("decnet_dev");
1481 dn_dev_devices_off();