2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
6 * IPv4 Forwarding Information Base: policy rules.
8 * Version: $Id: fib_rules.c,v 1.17 2001/10/31 21:55:54 davem Exp $
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
18 * Rani Assaf : local_rule cannot be deleted
19 * Marc Boucher : routing by fwmark
22 #include <asm/uaccess.h>
23 #include <asm/system.h>
24 #include <linux/bitops.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
29 #include <linux/string.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/errno.h>
34 #include <linux/inet.h>
35 #include <linux/inetdevice.h>
36 #include <linux/netdevice.h>
37 #include <linux/if_arp.h>
38 #include <linux/proc_fs.h>
39 #include <linux/skbuff.h>
40 #include <linux/netlink.h>
41 #include <linux/init.h>
42 #include <linux/list.h>
43 #include <linux/rcupdate.h>
46 #include <net/protocol.h>
47 #include <net/route.h>
50 #include <net/ip_fib.h>
52 #define FRprintk(a...)
56 struct hlist_node hlist
;
59 unsigned char r_table
;
60 unsigned char r_action
;
61 unsigned char r_dst_len
;
62 unsigned char r_src_len
;
70 #ifdef CONFIG_IP_ROUTE_FWMARK
74 #ifdef CONFIG_NET_CLS_ROUTE
77 char r_ifname
[IFNAMSIZ
];
82 static struct fib_rule default_rule
= {
83 .r_clntref
= ATOMIC_INIT(2),
84 .r_preference
= 0x7FFF,
85 .r_table
= RT_TABLE_DEFAULT
,
86 .r_action
= RTN_UNICAST
,
89 static struct fib_rule main_rule
= {
90 .r_clntref
= ATOMIC_INIT(2),
91 .r_preference
= 0x7FFE,
92 .r_table
= RT_TABLE_MAIN
,
93 .r_action
= RTN_UNICAST
,
96 static struct fib_rule local_rule
= {
97 .r_clntref
= ATOMIC_INIT(2),
98 .r_table
= RT_TABLE_LOCAL
,
99 .r_action
= RTN_UNICAST
,
102 static struct hlist_head fib_rules
;
104 /* writer func called from netlink -- rtnl_sem hold*/
106 static void rtmsg_rule(int, struct fib_rule
*);
108 int inet_rtm_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
110 struct rtattr
**rta
= arg
;
111 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
113 struct hlist_node
*node
;
116 hlist_for_each_entry(r
, node
, &fib_rules
, hlist
) {
117 if ((!rta
[RTA_SRC
-1] || memcmp(RTA_DATA(rta
[RTA_SRC
-1]), &r
->r_src
, 4) == 0) &&
118 rtm
->rtm_src_len
== r
->r_src_len
&&
119 rtm
->rtm_dst_len
== r
->r_dst_len
&&
120 (!rta
[RTA_DST
-1] || memcmp(RTA_DATA(rta
[RTA_DST
-1]), &r
->r_dst
, 4) == 0) &&
121 rtm
->rtm_tos
== r
->r_tos
&&
122 #ifdef CONFIG_IP_ROUTE_FWMARK
123 (!rta
[RTA_PROTOINFO
-1] || memcmp(RTA_DATA(rta
[RTA_PROTOINFO
-1]), &r
->r_fwmark
, 4) == 0) &&
125 (!rtm
->rtm_type
|| rtm
->rtm_type
== r
->r_action
) &&
126 (!rta
[RTA_PRIORITY
-1] || memcmp(RTA_DATA(rta
[RTA_PRIORITY
-1]), &r
->r_preference
, 4) == 0) &&
127 (!rta
[RTA_IIF
-1] || rtattr_strcmp(rta
[RTA_IIF
-1], r
->r_ifname
) == 0) &&
128 (!rtm
->rtm_table
|| (r
&& rtm
->rtm_table
== r
->r_table
))) {
130 if (r
== &local_rule
)
133 hlist_del_rcu(&r
->hlist
);
135 rtmsg_rule(RTM_DELRULE
, r
);
144 /* Allocate new unique table id */
146 static struct fib_table
*fib_empty_table(void)
150 for (id
= 1; id
<= RT_TABLE_MAX
; id
++)
151 if (fib_tables
[id
] == NULL
)
152 return __fib_new_table(id
);
156 static inline void fib_rule_put_rcu(struct rcu_head
*head
)
158 struct fib_rule
*r
= container_of(head
, struct fib_rule
, rcu
);
162 void fib_rule_put(struct fib_rule
*r
)
164 if (atomic_dec_and_test(&r
->r_clntref
)) {
166 call_rcu(&r
->rcu
, fib_rule_put_rcu
);
168 printk("Freeing alive rule %p\n", r
);
172 /* writer func called from netlink -- rtnl_sem hold*/
174 int inet_rtm_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
176 struct rtattr
**rta
= arg
;
177 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
178 struct fib_rule
*r
, *new_r
, *last
= NULL
;
179 struct hlist_node
*node
= NULL
;
180 unsigned char table_id
;
182 if (rtm
->rtm_src_len
> 32 || rtm
->rtm_dst_len
> 32 ||
183 (rtm
->rtm_tos
& ~IPTOS_TOS_MASK
))
186 if (rta
[RTA_IIF
-1] && RTA_PAYLOAD(rta
[RTA_IIF
-1]) > IFNAMSIZ
)
189 table_id
= rtm
->rtm_table
;
190 if (table_id
== RT_TABLE_UNSPEC
) {
191 struct fib_table
*table
;
192 if (rtm
->rtm_type
== RTN_UNICAST
) {
193 if ((table
= fib_empty_table()) == NULL
)
195 table_id
= table
->tb_id
;
199 new_r
= kzalloc(sizeof(*new_r
), GFP_KERNEL
);
204 memcpy(&new_r
->r_src
, RTA_DATA(rta
[RTA_SRC
-1]), 4);
206 memcpy(&new_r
->r_dst
, RTA_DATA(rta
[RTA_DST
-1]), 4);
207 if (rta
[RTA_GATEWAY
-1])
208 memcpy(&new_r
->r_srcmap
, RTA_DATA(rta
[RTA_GATEWAY
-1]), 4);
209 new_r
->r_src_len
= rtm
->rtm_src_len
;
210 new_r
->r_dst_len
= rtm
->rtm_dst_len
;
211 new_r
->r_srcmask
= inet_make_mask(rtm
->rtm_src_len
);
212 new_r
->r_dstmask
= inet_make_mask(rtm
->rtm_dst_len
);
213 new_r
->r_tos
= rtm
->rtm_tos
;
214 #ifdef CONFIG_IP_ROUTE_FWMARK
215 if (rta
[RTA_PROTOINFO
-1])
216 memcpy(&new_r
->r_fwmark
, RTA_DATA(rta
[RTA_PROTOINFO
-1]), 4);
218 new_r
->r_action
= rtm
->rtm_type
;
219 new_r
->r_flags
= rtm
->rtm_flags
;
220 if (rta
[RTA_PRIORITY
-1])
221 memcpy(&new_r
->r_preference
, RTA_DATA(rta
[RTA_PRIORITY
-1]), 4);
222 new_r
->r_table
= table_id
;
223 if (rta
[RTA_IIF
-1]) {
224 struct net_device
*dev
;
225 rtattr_strlcpy(new_r
->r_ifname
, rta
[RTA_IIF
-1], IFNAMSIZ
);
226 new_r
->r_ifindex
= -1;
227 dev
= __dev_get_by_name(new_r
->r_ifname
);
229 new_r
->r_ifindex
= dev
->ifindex
;
231 #ifdef CONFIG_NET_CLS_ROUTE
233 memcpy(&new_r
->r_tclassid
, RTA_DATA(rta
[RTA_FLOW
-1]), 4);
235 r
= container_of(fib_rules
.first
, struct fib_rule
, hlist
);
237 if (!new_r
->r_preference
) {
238 if (r
&& r
->hlist
.next
!= NULL
) {
239 r
= container_of(r
->hlist
.next
, struct fib_rule
, hlist
);
241 new_r
->r_preference
= r
->r_preference
- 1;
245 hlist_for_each_entry(r
, node
, &fib_rules
, hlist
) {
246 if (r
->r_preference
> new_r
->r_preference
)
250 atomic_inc(&new_r
->r_clntref
);
253 hlist_add_after_rcu(&last
->hlist
, &new_r
->hlist
);
255 hlist_add_before_rcu(&new_r
->hlist
, &r
->hlist
);
257 rtmsg_rule(RTM_NEWRULE
, new_r
);
261 #ifdef CONFIG_NET_CLS_ROUTE
262 u32
fib_rules_tclass(struct fib_result
*res
)
265 return res
->r
->r_tclassid
;
270 /* callers should hold rtnl semaphore */
272 static void fib_rules_detach(struct net_device
*dev
)
274 struct hlist_node
*node
;
277 hlist_for_each_entry(r
, node
, &fib_rules
, hlist
) {
278 if (r
->r_ifindex
== dev
->ifindex
)
284 /* callers should hold rtnl semaphore */
286 static void fib_rules_attach(struct net_device
*dev
)
288 struct hlist_node
*node
;
291 hlist_for_each_entry(r
, node
, &fib_rules
, hlist
) {
292 if (r
->r_ifindex
== -1 && strcmp(dev
->name
, r
->r_ifname
) == 0)
293 r
->r_ifindex
= dev
->ifindex
;
297 int fib_lookup(const struct flowi
*flp
, struct fib_result
*res
)
300 struct fib_rule
*r
, *policy
;
301 struct fib_table
*tb
;
302 struct hlist_node
*node
;
304 u32 daddr
= flp
->fl4_dst
;
305 u32 saddr
= flp
->fl4_src
;
307 FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
308 NIPQUAD(flp
->fl4_dst
), NIPQUAD(flp
->fl4_src
));
312 hlist_for_each_entry_rcu(r
, node
, &fib_rules
, hlist
) {
313 if (((saddr
^r
->r_src
) & r
->r_srcmask
) ||
314 ((daddr
^r
->r_dst
) & r
->r_dstmask
) ||
315 (r
->r_tos
&& r
->r_tos
!= flp
->fl4_tos
) ||
316 #ifdef CONFIG_IP_ROUTE_FWMARK
317 (r
->r_fwmark
&& r
->r_fwmark
!= flp
->fl4_fwmark
) ||
319 (r
->r_ifindex
&& r
->r_ifindex
!= flp
->iif
))
322 FRprintk("tb %d r %d ", r
->r_table
, r
->r_action
);
323 switch (r
->r_action
) {
327 case RTN_UNREACHABLE
:
339 if ((tb
= fib_get_table(r
->r_table
)) == NULL
)
341 err
= tb
->tb_lookup(tb
, flp
, res
);
345 atomic_inc(&policy
->r_clntref
);
349 if (err
< 0 && err
!= -EAGAIN
) {
354 FRprintk("FAILURE\n");
359 void fib_select_default(const struct flowi
*flp
, struct fib_result
*res
)
361 if (res
->r
&& res
->r
->r_action
== RTN_UNICAST
&&
362 FIB_RES_GW(*res
) && FIB_RES_NH(*res
).nh_scope
== RT_SCOPE_LINK
) {
363 struct fib_table
*tb
;
364 if ((tb
= fib_get_table(res
->r
->r_table
)) != NULL
)
365 tb
->tb_select_default(tb
, flp
, res
);
369 static int fib_rules_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
371 struct net_device
*dev
= ptr
;
373 if (event
== NETDEV_UNREGISTER
)
374 fib_rules_detach(dev
);
375 else if (event
== NETDEV_REGISTER
)
376 fib_rules_attach(dev
);
381 static struct notifier_block fib_rules_notifier
= {
382 .notifier_call
=fib_rules_event
,
385 static __inline__
int inet_fill_rule(struct sk_buff
*skb
,
387 u32 pid
, u32 seq
, int event
,
391 struct nlmsghdr
*nlh
;
392 unsigned char *b
= skb
->tail
;
394 nlh
= NLMSG_NEW(skb
, pid
, seq
, event
, sizeof(*rtm
), flags
);
395 rtm
= NLMSG_DATA(nlh
);
396 rtm
->rtm_family
= AF_INET
;
397 rtm
->rtm_dst_len
= r
->r_dst_len
;
398 rtm
->rtm_src_len
= r
->r_src_len
;
399 rtm
->rtm_tos
= r
->r_tos
;
400 #ifdef CONFIG_IP_ROUTE_FWMARK
402 RTA_PUT(skb
, RTA_PROTOINFO
, 4, &r
->r_fwmark
);
404 rtm
->rtm_table
= r
->r_table
;
405 rtm
->rtm_protocol
= 0;
407 rtm
->rtm_type
= r
->r_action
;
408 rtm
->rtm_flags
= r
->r_flags
;
411 RTA_PUT(skb
, RTA_DST
, 4, &r
->r_dst
);
413 RTA_PUT(skb
, RTA_SRC
, 4, &r
->r_src
);
415 RTA_PUT(skb
, RTA_IIF
, IFNAMSIZ
, &r
->r_ifname
);
417 RTA_PUT(skb
, RTA_PRIORITY
, 4, &r
->r_preference
);
419 RTA_PUT(skb
, RTA_GATEWAY
, 4, &r
->r_srcmap
);
420 #ifdef CONFIG_NET_CLS_ROUTE
422 RTA_PUT(skb
, RTA_FLOW
, 4, &r
->r_tclassid
);
424 nlh
->nlmsg_len
= skb
->tail
- b
;
429 skb_trim(skb
, b
- skb
->data
);
433 /* callers should hold rtnl semaphore */
435 static void rtmsg_rule(int event
, struct fib_rule
*r
)
437 int size
= NLMSG_SPACE(sizeof(struct rtmsg
) + 128);
438 struct sk_buff
*skb
= alloc_skb(size
, GFP_KERNEL
);
441 netlink_set_err(rtnl
, 0, RTNLGRP_IPV4_RULE
, ENOBUFS
);
442 else if (inet_fill_rule(skb
, r
, 0, 0, event
, 0) < 0) {
444 netlink_set_err(rtnl
, 0, RTNLGRP_IPV4_RULE
, EINVAL
);
446 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_IPV4_RULE
, GFP_KERNEL
);
450 int inet_dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
)
453 int s_idx
= cb
->args
[0];
455 struct hlist_node
*node
;
458 hlist_for_each_entry(r
, node
, &fib_rules
, hlist
) {
461 if (inet_fill_rule(skb
, r
, NETLINK_CB(cb
->skb
).pid
,
463 RTM_NEWRULE
, NLM_F_MULTI
) < 0)
474 void __init
fib_rules_init(void)
476 INIT_HLIST_HEAD(&fib_rules
);
477 hlist_add_head(&local_rule
.hlist
, &fib_rules
);
478 hlist_add_after(&local_rule
.hlist
, &main_rule
.hlist
);
479 hlist_add_after(&main_rule
.hlist
, &default_rule
.hlist
);
480 register_netdevice_notifier(&fib_rules_notifier
);