3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Routing Forwarding Information Base (Rules)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
16 #include <linux/config.h>
17 #include <linux/string.h>
18 #include <linux/net.h>
19 #include <linux/socket.h>
20 #include <linux/sockios.h>
21 #include <linux/init.h>
22 #include <linux/skbuff.h>
23 #include <linux/netlink.h>
24 #include <linux/rtnetlink.h>
25 #include <linux/proc_fs.h>
26 #include <linux/netdevice.h>
27 #include <linux/timer.h>
28 #include <linux/spinlock.h>
29 #include <linux/in_route.h>
30 #include <asm/atomic.h>
31 #include <asm/uaccess.h>
32 #include <net/neighbour.h>
36 #include <net/dn_fib.h>
37 #include <net/dn_neigh.h>
38 #include <net/dn_dev.h>
42 struct dn_fib_rule
*r_next
;
45 unsigned char r_table
;
46 unsigned char r_action
;
47 unsigned char r_dst_len
;
48 unsigned char r_src_len
;
55 #ifdef CONFIG_DECNET_ROUTE_FWMARK
59 char r_ifname
[IFNAMSIZ
];
63 static struct dn_fib_rule default_rule
= {
64 .r_clntref
= ATOMIC_INIT(2),
65 .r_preference
= 0x7fff,
66 .r_table
= RT_TABLE_MAIN
,
67 .r_action
= RTN_UNICAST
70 static struct dn_fib_rule
*dn_fib_rules
= &default_rule
;
71 static DEFINE_RWLOCK(dn_fib_rules_lock
);
74 int dn_fib_rtm_delrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
76 struct rtattr
**rta
= arg
;
77 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
78 struct dn_fib_rule
*r
, **rp
;
81 for(rp
=&dn_fib_rules
; (r
=*rp
) != NULL
; rp
= &r
->r_next
) {
82 if ((!rta
[RTA_SRC
-1] || memcmp(RTA_DATA(rta
[RTA_SRC
-1]), &r
->r_src
, 2) == 0) &&
83 rtm
->rtm_src_len
== r
->r_src_len
&&
84 rtm
->rtm_dst_len
== r
->r_dst_len
&&
85 (!rta
[RTA_DST
-1] || memcmp(RTA_DATA(rta
[RTA_DST
-1]), &r
->r_dst
, 2) == 0) &&
86 #ifdef CONFIG_DECNET_ROUTE_FWMARK
87 (!rta
[RTA_PROTOINFO
-1] || memcmp(RTA_DATA(rta
[RTA_PROTOINFO
-1]), &r
->r_fwmark
, 4) == 0) &&
89 (!rtm
->rtm_type
|| rtm
->rtm_type
== r
->r_action
) &&
90 (!rta
[RTA_PRIORITY
-1] || memcmp(RTA_DATA(rta
[RTA_PRIORITY
-1]), &r
->r_preference
, 4) == 0) &&
91 (!rta
[RTA_IIF
-1] || rtattr_strcmp(rta
[RTA_IIF
-1], r
->r_ifname
) == 0) &&
92 (!rtm
->rtm_table
|| (r
&& rtm
->rtm_table
== r
->r_table
))) {
95 if (r
== &default_rule
)
98 write_lock_bh(&dn_fib_rules_lock
);
101 write_unlock_bh(&dn_fib_rules_lock
);
111 void dn_fib_rule_put(struct dn_fib_rule
*r
)
113 if (atomic_dec_and_test(&r
->r_clntref
)) {
117 printk(KERN_DEBUG
"Attempt to free alive dn_fib_rule\n");
122 int dn_fib_rtm_newrule(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
124 struct rtattr
**rta
= arg
;
125 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
126 struct dn_fib_rule
*r
, *new_r
, **rp
;
127 unsigned char table_id
;
129 if (rtm
->rtm_src_len
> 16 || rtm
->rtm_dst_len
> 16)
132 if (rta
[RTA_IIF
-1] && RTA_PAYLOAD(rta
[RTA_IIF
-1]) > IFNAMSIZ
)
135 if (rtm
->rtm_type
== RTN_NAT
)
138 table_id
= rtm
->rtm_table
;
139 if (table_id
== RT_TABLE_UNSPEC
) {
140 struct dn_fib_table
*tb
;
141 if (rtm
->rtm_type
== RTN_UNICAST
) {
142 if ((tb
= dn_fib_empty_table()) == NULL
)
148 new_r
= kmalloc(sizeof(*new_r
), GFP_KERNEL
);
151 memset(new_r
, 0, sizeof(*new_r
));
153 memcpy(&new_r
->r_src
, RTA_DATA(rta
[RTA_SRC
-1]), 2);
155 memcpy(&new_r
->r_dst
, RTA_DATA(rta
[RTA_DST
-1]), 2);
156 if (rta
[RTA_GATEWAY
-1])
157 memcpy(&new_r
->r_srcmap
, RTA_DATA(rta
[RTA_GATEWAY
-1]), 2);
158 new_r
->r_src_len
= rtm
->rtm_src_len
;
159 new_r
->r_dst_len
= rtm
->rtm_dst_len
;
160 new_r
->r_srcmask
= dnet_make_mask(rtm
->rtm_src_len
);
161 new_r
->r_dstmask
= dnet_make_mask(rtm
->rtm_dst_len
);
162 #ifdef CONFIG_DECNET_ROUTE_FWMARK
163 if (rta
[RTA_PROTOINFO
-1])
164 memcpy(&new_r
->r_fwmark
, RTA_DATA(rta
[RTA_PROTOINFO
-1]), 4);
166 new_r
->r_action
= rtm
->rtm_type
;
167 new_r
->r_flags
= rtm
->rtm_flags
;
168 if (rta
[RTA_PRIORITY
-1])
169 memcpy(&new_r
->r_preference
, RTA_DATA(rta
[RTA_PRIORITY
-1]), 4);
170 new_r
->r_table
= table_id
;
171 if (rta
[RTA_IIF
-1]) {
172 struct net_device
*dev
;
173 rtattr_strlcpy(new_r
->r_ifname
, rta
[RTA_IIF
-1], IFNAMSIZ
);
174 new_r
->r_ifindex
= -1;
175 dev
= dev_get_by_name(new_r
->r_ifname
);
177 new_r
->r_ifindex
= dev
->ifindex
;
183 if (!new_r
->r_preference
) {
185 if (r
&& (r
= r
->r_next
) != NULL
) {
186 rp
= &dn_fib_rules
->r_next
;
188 new_r
->r_preference
= r
->r_preference
- 1;
192 while((r
=*rp
) != NULL
) {
193 if (r
->r_preference
> new_r
->r_preference
)
199 atomic_inc(&new_r
->r_clntref
);
200 write_lock_bh(&dn_fib_rules_lock
);
202 write_unlock_bh(&dn_fib_rules_lock
);
207 int dn_fib_lookup(const struct flowi
*flp
, struct dn_fib_res
*res
)
209 struct dn_fib_rule
*r
, *policy
;
210 struct dn_fib_table
*tb
;
211 dn_address saddr
= flp
->fld_src
;
212 dn_address daddr
= flp
->fld_dst
;
215 read_lock(&dn_fib_rules_lock
);
216 for(r
= dn_fib_rules
; r
; r
= r
->r_next
) {
217 if (((saddr
^r
->r_src
) & r
->r_srcmask
) ||
218 ((daddr
^r
->r_dst
) & r
->r_dstmask
) ||
219 #ifdef CONFIG_DECNET_ROUTE_FWMARK
220 (r
->r_fwmark
&& r
->r_fwmark
!= flp
->fld_fwmark
) ||
222 (r
->r_ifindex
&& r
->r_ifindex
!= flp
->iif
))
225 switch(r
->r_action
) {
230 case RTN_UNREACHABLE
:
231 read_unlock(&dn_fib_rules_lock
);
235 read_unlock(&dn_fib_rules_lock
);
238 read_unlock(&dn_fib_rules_lock
);
242 if ((tb
= dn_fib_get_table(r
->r_table
, 0)) == NULL
)
244 err
= tb
->lookup(tb
, flp
, res
);
248 atomic_inc(&policy
->r_clntref
);
249 read_unlock(&dn_fib_rules_lock
);
252 if (err
< 0 && err
!= -EAGAIN
) {
253 read_unlock(&dn_fib_rules_lock
);
258 read_unlock(&dn_fib_rules_lock
);
262 unsigned dnet_addr_type(__u16 addr
)
264 struct flowi fl
= { .nl_u
= { .dn_u
= { .daddr
= addr
} } };
265 struct dn_fib_res res
;
266 unsigned ret
= RTN_UNICAST
;
267 struct dn_fib_table
*tb
= dn_fib_tables
[RT_TABLE_LOCAL
];
272 if (!tb
->lookup(tb
, &fl
, &res
)) {
274 dn_fib_res_put(&res
);
280 __u16
dn_fib_rules_policy(__u16 saddr
, struct dn_fib_res
*res
, unsigned *flags
)
282 struct dn_fib_rule
*r
= res
->r
;
284 if (r
->r_action
== RTN_NAT
) {
285 int addrtype
= dnet_addr_type(r
->r_srcmap
);
287 if (addrtype
== RTN_NAT
) {
288 saddr
= (saddr
&~r
->r_srcmask
)|r
->r_srcmap
;
290 } else if (addrtype
== RTN_LOCAL
|| r
->r_srcmap
== 0) {
298 static void dn_fib_rules_detach(struct net_device
*dev
)
300 struct dn_fib_rule
*r
;
302 for(r
= dn_fib_rules
; r
; r
= r
->r_next
) {
303 if (r
->r_ifindex
== dev
->ifindex
) {
304 write_lock_bh(&dn_fib_rules_lock
);
306 write_unlock_bh(&dn_fib_rules_lock
);
311 static void dn_fib_rules_attach(struct net_device
*dev
)
313 struct dn_fib_rule
*r
;
315 for(r
= dn_fib_rules
; r
; r
= r
->r_next
) {
316 if (r
->r_ifindex
== -1 && strcmp(dev
->name
, r
->r_ifname
) == 0) {
317 write_lock_bh(&dn_fib_rules_lock
);
318 r
->r_ifindex
= dev
->ifindex
;
319 write_unlock_bh(&dn_fib_rules_lock
);
324 static int dn_fib_rules_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
326 struct net_device
*dev
= ptr
;
329 case NETDEV_UNREGISTER
:
330 dn_fib_rules_detach(dev
);
331 dn_fib_sync_down(0, dev
, 1);
332 case NETDEV_REGISTER
:
333 dn_fib_rules_attach(dev
);
341 static struct notifier_block dn_fib_rules_notifier
= {
342 .notifier_call
= dn_fib_rules_event
,
345 static int dn_fib_fill_rule(struct sk_buff
*skb
, struct dn_fib_rule
*r
, struct netlink_callback
*cb
)
348 struct nlmsghdr
*nlh
;
349 unsigned char *b
= skb
->tail
;
352 nlh
= NLMSG_PUT(skb
, NETLINK_CREDS(cb
->skb
)->pid
, cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
, sizeof(*rtm
));
353 rtm
= NLMSG_DATA(nlh
);
354 rtm
->rtm_family
= AF_DECnet
;
355 rtm
->rtm_dst_len
= r
->r_dst_len
;
356 rtm
->rtm_src_len
= r
->r_src_len
;
358 #ifdef CONFIG_DECNET_ROUTE_FWMARK
360 RTA_PUT(skb
, RTA_PROTOINFO
, 4, &r
->r_fwmark
);
362 rtm
->rtm_table
= r
->r_table
;
363 rtm
->rtm_protocol
= 0;
365 rtm
->rtm_type
= r
->r_action
;
366 rtm
->rtm_flags
= r
->r_flags
;
369 RTA_PUT(skb
, RTA_DST
, 2, &r
->r_dst
);
371 RTA_PUT(skb
, RTA_SRC
, 2, &r
->r_src
);
373 RTA_PUT(skb
, RTA_IIF
, IFNAMSIZ
, &r
->r_ifname
);
375 RTA_PUT(skb
, RTA_PRIORITY
, 4, &r
->r_preference
);
377 RTA_PUT(skb
, RTA_GATEWAY
, 2, &r
->r_srcmap
);
378 nlh
->nlmsg_len
= skb
->tail
- b
;
383 skb_trim(skb
, b
- skb
->data
);
387 int dn_fib_dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
)
390 int s_idx
= cb
->args
[0];
391 struct dn_fib_rule
*r
;
393 read_lock(&dn_fib_rules_lock
);
394 for(r
= dn_fib_rules
, idx
= 0; r
; r
= r
->r_next
, idx
++) {
397 if (dn_fib_fill_rule(skb
, r
, cb
) < 0)
400 read_unlock(&dn_fib_rules_lock
);
406 void __init
dn_fib_rules_init(void)
408 register_netdevice_notifier(&dn_fib_rules_notifier
);
411 void __exit
dn_fib_rules_cleanup(void)
413 unregister_netdevice_notifier(&dn_fib_rules_notifier
);