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 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Thomas Graf <tgraf@suug.ch>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version
14 * 2 of the License, or (at your option) any later version.
17 * Rani Assaf : local_rule cannot be deleted
18 * Marc Boucher : routing by fwmark
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/inetdevice.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/rcupdate.h>
30 #include <net/route.h>
32 #include <net/ip_fib.h>
33 #include <net/fib_rules.h>
35 static struct fib_rules_ops fib4_rules_ops;
39 struct fib_rule common;
47 #ifdef CONFIG_NET_CLS_ROUTE
52 static struct fib4_rule default_rule = {
54 .refcnt = ATOMIC_INIT(2),
56 .table = RT_TABLE_DEFAULT,
57 .action = FR_ACT_TO_TBL,
61 static struct fib4_rule main_rule = {
63 .refcnt = ATOMIC_INIT(2),
65 .table = RT_TABLE_MAIN,
66 .action = FR_ACT_TO_TBL,
70 static struct fib4_rule local_rule = {
72 .refcnt = ATOMIC_INIT(2),
73 .table = RT_TABLE_LOCAL,
74 .action = FR_ACT_TO_TBL,
75 .flags = FIB_RULE_PERMANENT,
79 #ifdef CONFIG_NET_CLS_ROUTE
80 u32 fib_rules_tclass(struct fib_result *res)
82 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
86 int fib_lookup(struct flowi *flp, struct fib_result *res)
88 struct fib_lookup_arg arg = {
93 err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
99 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
100 int flags, struct fib_lookup_arg *arg)
103 struct fib_table *tbl;
105 switch (rule->action) {
109 case FR_ACT_UNREACHABLE:
113 case FR_ACT_PROHIBIT:
117 case FR_ACT_BLACKHOLE:
123 if ((tbl = fib_get_table(rule->table)) == NULL)
126 err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
134 void fib_select_default(const struct flowi *flp, struct fib_result *res)
136 if (res->r && res->r->action == FR_ACT_TO_TBL &&
137 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
138 struct fib_table *tb;
139 if ((tb = fib_get_table(res->r->table)) != NULL)
140 tb->tb_select_default(tb, flp, res);
144 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
146 struct fib4_rule *r = (struct fib4_rule *) rule;
147 __be32 daddr = fl->fl4_dst;
148 __be32 saddr = fl->fl4_src;
150 if (((saddr ^ r->src) & r->srcmask) ||
151 ((daddr ^ r->dst) & r->dstmask))
154 if (r->tos && (r->tos != fl->fl4_tos))
160 static struct fib_table *fib_empty_table(void)
164 for (id = 1; id <= RT_TABLE_MAX; id++)
165 if (fib_get_table(id) == NULL)
166 return fib_new_table(id);
170 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
172 [FRA_FLOW] = { .type = NLA_U32 },
175 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
176 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
180 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
182 if (frh->tos & ~IPTOS_TOS_MASK)
185 if (rule->table == RT_TABLE_UNSPEC) {
186 if (rule->action == FR_ACT_TO_TBL) {
187 struct fib_table *table;
189 table = fib_empty_table();
195 rule->table = table->tb_id;
200 rule4->src = nla_get_be32(tb[FRA_SRC]);
203 rule4->dst = nla_get_be32(tb[FRA_DST]);
205 #ifdef CONFIG_NET_CLS_ROUTE
207 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
210 rule4->src_len = frh->src_len;
211 rule4->srcmask = inet_make_mask(rule4->src_len);
212 rule4->dst_len = frh->dst_len;
213 rule4->dstmask = inet_make_mask(rule4->dst_len);
214 rule4->tos = frh->tos;
221 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
224 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
226 if (frh->src_len && (rule4->src_len != frh->src_len))
229 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
232 if (frh->tos && (rule4->tos != frh->tos))
235 #ifdef CONFIG_NET_CLS_ROUTE
236 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
240 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
243 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
249 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
250 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
252 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
254 frh->family = AF_INET;
255 frh->dst_len = rule4->dst_len;
256 frh->src_len = rule4->src_len;
257 frh->tos = rule4->tos;
260 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
263 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
265 #ifdef CONFIG_NET_CLS_ROUTE
267 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
275 static u32 fib4_rule_default_pref(void)
277 struct list_head *pos;
278 struct fib_rule *rule;
280 if (!list_empty(&fib4_rules_ops.rules_list)) {
281 pos = fib4_rules_ops.rules_list.next;
282 if (pos->next != &fib4_rules_ops.rules_list) {
283 rule = list_entry(pos->next, struct fib_rule, list);
285 return rule->pref - 1;
292 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
294 return nla_total_size(4) /* dst */
295 + nla_total_size(4) /* src */
296 + nla_total_size(4); /* flow */
299 static void fib4_rule_flush_cache(void)
304 static struct fib_rules_ops fib4_rules_ops = {
306 .rule_size = sizeof(struct fib4_rule),
307 .addr_size = sizeof(u32),
308 .action = fib4_rule_action,
309 .match = fib4_rule_match,
310 .configure = fib4_rule_configure,
311 .compare = fib4_rule_compare,
312 .fill = fib4_rule_fill,
313 .default_pref = fib4_rule_default_pref,
314 .nlmsg_payload = fib4_rule_nlmsg_payload,
315 .flush_cache = fib4_rule_flush_cache,
316 .nlgroup = RTNLGRP_IPV4_RULE,
317 .policy = fib4_rule_policy,
318 .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list),
319 .owner = THIS_MODULE,
322 void __init fib4_rules_init(void)
324 list_add_tail(&local_rule.common.list, &fib4_rules_ops.rules_list);
325 list_add_tail(&main_rule.common.list, &fib4_rules_ops.rules_list);
326 list_add_tail(&default_rule.common.list, &fib4_rules_ops.rules_list);
328 fib_rules_register(&fib4_rules_ops);