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 <linux/config.h>
23 #include <asm/uaccess.h>
24 #include <asm/system.h>
25 #include <asm/bitops.h>
26 #include <linux/types.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
30 #include <linux/string.h>
31 #include <linux/socket.h>
32 #include <linux/sockios.h>
33 #include <linux/errno.h>
35 #include <linux/inet.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>
44 #include <net/protocol.h>
45 #include <net/route.h>
48 #include <net/ip_fib.h>
50 #define FRprintk(a...)
54 struct fib_rule
*r_next
;
57 unsigned char r_table
;
58 unsigned char r_action
;
59 unsigned char r_dst_len
;
60 unsigned char r_src_len
;
68 #ifdef CONFIG_IP_ROUTE_FWMARK
72 #ifdef CONFIG_NET_CLS_ROUTE
75 char r_ifname
[IFNAMSIZ
];
79 static struct fib_rule default_rule
= {
80 .r_clntref
= ATOMIC_INIT(2),
81 .r_preference
= 0x7FFF,
82 .r_table
= RT_TABLE_DEFAULT
,
83 .r_action
= RTN_UNICAST
,
86 static struct fib_rule main_rule
= {
87 .r_next
= &default_rule
,
88 .r_clntref
= ATOMIC_INIT(2),
89 .r_preference
= 0x7FFE,
90 .r_table
= RT_TABLE_MAIN
,
91 .r_action
= RTN_UNICAST
,
94 static struct fib_rule local_rule
= {
96 .r_clntref
= ATOMIC_INIT(2),
97 .r_table
= RT_TABLE_LOCAL
,
98 .r_action
= RTN_UNICAST
,
101 static struct fib_rule
*fib_rules
= &local_rule
;
102 static rwlock_t fib_rules_lock
= RW_LOCK_UNLOCKED
;
104 int inet_rtm_delrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
106 struct rtattr
**rta
= arg
;
107 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
108 struct fib_rule
*r
, **rp
;
111 for (rp
=&fib_rules
; (r
=*rp
) != NULL
; rp
=&r
->r_next
) {
112 if ((!rta
[RTA_SRC
-1] || memcmp(RTA_DATA(rta
[RTA_SRC
-1]), &r
->r_src
, 4) == 0) &&
113 rtm
->rtm_src_len
== r
->r_src_len
&&
114 rtm
->rtm_dst_len
== r
->r_dst_len
&&
115 (!rta
[RTA_DST
-1] || memcmp(RTA_DATA(rta
[RTA_DST
-1]), &r
->r_dst
, 4) == 0) &&
116 rtm
->rtm_tos
== r
->r_tos
&&
117 #ifdef CONFIG_IP_ROUTE_FWMARK
118 (!rta
[RTA_PROTOINFO
-1] || memcmp(RTA_DATA(rta
[RTA_PROTOINFO
-1]), &r
->r_fwmark
, 4) == 0) &&
120 (!rtm
->rtm_type
|| rtm
->rtm_type
== r
->r_action
) &&
121 (!rta
[RTA_PRIORITY
-1] || memcmp(RTA_DATA(rta
[RTA_PRIORITY
-1]), &r
->r_preference
, 4) == 0) &&
122 (!rta
[RTA_IIF
-1] || strcmp(RTA_DATA(rta
[RTA_IIF
-1]), r
->r_ifname
) == 0) &&
123 (!rtm
->rtm_table
|| (r
&& rtm
->rtm_table
== r
->r_table
))) {
125 if (r
== &local_rule
)
128 write_lock_bh(&fib_rules_lock
);
131 write_unlock_bh(&fib_rules_lock
);
140 /* Allocate new unique table id */
142 static struct fib_table
*fib_empty_table(void)
146 for (id
= 1; id
<= RT_TABLE_MAX
; id
++)
147 if (fib_tables
[id
] == NULL
)
148 return __fib_new_table(id
);
152 void fib_rule_put(struct fib_rule
*r
)
154 if (atomic_dec_and_test(&r
->r_clntref
)) {
158 printk("Freeing alive rule %p\n", r
);
162 int inet_rtm_newrule(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
164 struct rtattr
**rta
= arg
;
165 struct rtmsg
*rtm
= NLMSG_DATA(nlh
);
166 struct fib_rule
*r
, *new_r
, **rp
;
167 unsigned char table_id
;
169 if (rtm
->rtm_src_len
> 32 || rtm
->rtm_dst_len
> 32 ||
170 (rtm
->rtm_tos
& ~IPTOS_TOS_MASK
))
173 if (rta
[RTA_IIF
-1] && RTA_PAYLOAD(rta
[RTA_IIF
-1]) > IFNAMSIZ
)
176 table_id
= rtm
->rtm_table
;
177 if (table_id
== RT_TABLE_UNSPEC
) {
178 struct fib_table
*table
;
179 if (rtm
->rtm_type
== RTN_UNICAST
) {
180 if ((table
= fib_empty_table()) == NULL
)
182 table_id
= table
->tb_id
;
186 new_r
= kmalloc(sizeof(*new_r
), GFP_KERNEL
);
189 memset(new_r
, 0, sizeof(*new_r
));
191 memcpy(&new_r
->r_src
, RTA_DATA(rta
[RTA_SRC
-1]), 4);
193 memcpy(&new_r
->r_dst
, RTA_DATA(rta
[RTA_DST
-1]), 4);
194 if (rta
[RTA_GATEWAY
-1])
195 memcpy(&new_r
->r_srcmap
, RTA_DATA(rta
[RTA_GATEWAY
-1]), 4);
196 new_r
->r_src_len
= rtm
->rtm_src_len
;
197 new_r
->r_dst_len
= rtm
->rtm_dst_len
;
198 new_r
->r_srcmask
= inet_make_mask(rtm
->rtm_src_len
);
199 new_r
->r_dstmask
= inet_make_mask(rtm
->rtm_dst_len
);
200 new_r
->r_tos
= rtm
->rtm_tos
;
201 #ifdef CONFIG_IP_ROUTE_FWMARK
202 if (rta
[RTA_PROTOINFO
-1])
203 memcpy(&new_r
->r_fwmark
, RTA_DATA(rta
[RTA_PROTOINFO
-1]), 4);
205 new_r
->r_action
= rtm
->rtm_type
;
206 new_r
->r_flags
= rtm
->rtm_flags
;
207 if (rta
[RTA_PRIORITY
-1])
208 memcpy(&new_r
->r_preference
, RTA_DATA(rta
[RTA_PRIORITY
-1]), 4);
209 new_r
->r_table
= table_id
;
210 if (rta
[RTA_IIF
-1]) {
211 struct net_device
*dev
;
212 memcpy(new_r
->r_ifname
, RTA_DATA(rta
[RTA_IIF
-1]), IFNAMSIZ
);
213 new_r
->r_ifname
[IFNAMSIZ
-1] = 0;
214 new_r
->r_ifindex
= -1;
215 dev
= __dev_get_by_name(new_r
->r_ifname
);
217 new_r
->r_ifindex
= dev
->ifindex
;
219 #ifdef CONFIG_NET_CLS_ROUTE
221 memcpy(&new_r
->r_tclassid
, RTA_DATA(rta
[RTA_FLOW
-1]), 4);
225 if (!new_r
->r_preference
) {
227 if (r
&& (r
= r
->r_next
) != NULL
) {
228 rp
= &fib_rules
->r_next
;
230 new_r
->r_preference
= r
->r_preference
- 1;
234 while ( (r
= *rp
) != NULL
) {
235 if (r
->r_preference
> new_r
->r_preference
)
241 atomic_inc(&new_r
->r_clntref
);
242 write_lock_bh(&fib_rules_lock
);
244 write_unlock_bh(&fib_rules_lock
);
248 u32
fib_rules_map_destination(u32 daddr
, struct fib_result
*res
)
250 u32 mask
= inet_make_mask(res
->prefixlen
);
251 return (daddr
&~mask
)|res
->fi
->fib_nh
->nh_gw
;
254 #ifdef CONFIG_NET_CLS_ROUTE
255 u32
fib_rules_tclass(struct fib_result
*res
)
258 return res
->r
->r_tclassid
;
264 static void fib_rules_detach(struct net_device
*dev
)
268 for (r
=fib_rules
; r
; r
=r
->r_next
) {
269 if (r
->r_ifindex
== dev
->ifindex
) {
270 write_lock_bh(&fib_rules_lock
);
272 write_unlock_bh(&fib_rules_lock
);
277 static void fib_rules_attach(struct net_device
*dev
)
281 for (r
=fib_rules
; r
; r
=r
->r_next
) {
282 if (r
->r_ifindex
== -1 && strcmp(dev
->name
, r
->r_ifname
) == 0) {
283 write_lock_bh(&fib_rules_lock
);
284 r
->r_ifindex
= dev
->ifindex
;
285 write_unlock_bh(&fib_rules_lock
);
290 int fib_lookup(const struct flowi
*flp
, struct fib_result
*res
)
293 struct fib_rule
*r
, *policy
;
294 struct fib_table
*tb
;
296 u32 daddr
= flp
->fl4_dst
;
297 u32 saddr
= flp
->fl4_src
;
299 FRprintk("Lookup: %u.%u.%u.%u <- %u.%u.%u.%u ",
300 NIPQUAD(flp
->fl4_dst
), NIPQUAD(flp
->fl4_src
));
301 read_lock(&fib_rules_lock
);
302 for (r
= fib_rules
; r
; r
=r
->r_next
) {
303 if (((saddr
^r
->r_src
) & r
->r_srcmask
) ||
304 ((daddr
^r
->r_dst
) & r
->r_dstmask
) ||
305 (r
->r_tos
&& r
->r_tos
!= flp
->fl4_tos
) ||
306 #ifdef CONFIG_IP_ROUTE_FWMARK
307 (r
->r_fwmark
&& r
->r_fwmark
!= flp
->fl4_fwmark
) ||
309 (r
->r_ifindex
&& r
->r_ifindex
!= flp
->iif
))
312 FRprintk("tb %d r %d ", r
->r_table
, r
->r_action
);
313 switch (r
->r_action
) {
317 case RTN_UNREACHABLE
:
318 read_unlock(&fib_rules_lock
);
322 read_unlock(&fib_rules_lock
);
325 read_unlock(&fib_rules_lock
);
329 if ((tb
= fib_get_table(r
->r_table
)) == NULL
)
331 err
= tb
->tb_lookup(tb
, flp
, res
);
335 atomic_inc(&policy
->r_clntref
);
336 read_unlock(&fib_rules_lock
);
339 if (err
< 0 && err
!= -EAGAIN
) {
340 read_unlock(&fib_rules_lock
);
344 FRprintk("FAILURE\n");
345 read_unlock(&fib_rules_lock
);
349 void fib_select_default(const struct flowi
*flp
, struct fib_result
*res
)
351 if (res
->r
&& res
->r
->r_action
== RTN_UNICAST
&&
352 FIB_RES_GW(*res
) && FIB_RES_NH(*res
).nh_scope
== RT_SCOPE_LINK
) {
353 struct fib_table
*tb
;
354 if ((tb
= fib_get_table(res
->r
->r_table
)) != NULL
)
355 tb
->tb_select_default(tb
, flp
, res
);
359 static int fib_rules_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
361 struct net_device
*dev
= ptr
;
363 if (event
== NETDEV_UNREGISTER
)
364 fib_rules_detach(dev
);
365 else if (event
== NETDEV_REGISTER
)
366 fib_rules_attach(dev
);
371 struct notifier_block fib_rules_notifier
= {
372 .notifier_call
=fib_rules_event
,
375 static __inline__
int inet_fill_rule(struct sk_buff
*skb
,
377 struct netlink_callback
*cb
)
380 struct nlmsghdr
*nlh
;
381 unsigned char *b
= skb
->tail
;
383 nlh
= NLMSG_PUT(skb
, NETLINK_CREDS(cb
->skb
)->pid
, cb
->nlh
->nlmsg_seq
, RTM_NEWRULE
, sizeof(*rtm
));
384 rtm
= NLMSG_DATA(nlh
);
385 rtm
->rtm_family
= AF_INET
;
386 rtm
->rtm_dst_len
= r
->r_dst_len
;
387 rtm
->rtm_src_len
= r
->r_src_len
;
388 rtm
->rtm_tos
= r
->r_tos
;
389 #ifdef CONFIG_IP_ROUTE_FWMARK
391 RTA_PUT(skb
, RTA_PROTOINFO
, 4, &r
->r_fwmark
);
393 rtm
->rtm_table
= r
->r_table
;
394 rtm
->rtm_protocol
= 0;
396 rtm
->rtm_type
= r
->r_action
;
397 rtm
->rtm_flags
= r
->r_flags
;
400 RTA_PUT(skb
, RTA_DST
, 4, &r
->r_dst
);
402 RTA_PUT(skb
, RTA_SRC
, 4, &r
->r_src
);
404 RTA_PUT(skb
, RTA_IIF
, IFNAMSIZ
, &r
->r_ifname
);
406 RTA_PUT(skb
, RTA_PRIORITY
, 4, &r
->r_preference
);
408 RTA_PUT(skb
, RTA_GATEWAY
, 4, &r
->r_srcmap
);
409 #ifdef CONFIG_NET_CLS_ROUTE
411 RTA_PUT(skb
, RTA_FLOW
, 4, &r
->r_tclassid
);
413 nlh
->nlmsg_len
= skb
->tail
- b
;
418 skb_trim(skb
, b
- skb
->data
);
422 int inet_dump_rules(struct sk_buff
*skb
, struct netlink_callback
*cb
)
425 int s_idx
= cb
->args
[0];
428 read_lock(&fib_rules_lock
);
429 for (r
=fib_rules
, idx
=0; r
; r
= r
->r_next
, idx
++) {
432 if (inet_fill_rule(skb
, r
, cb
) < 0)
435 read_unlock(&fib_rules_lock
);
441 void __init
fib_rules_init(void)
443 register_netdevice_notifier(&fib_rules_notifier
);