]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/fib_rules.c
[NET] fib_rules: Flush route cache after rule modifications
[net-next-2.6.git] / net / ipv4 / fib_rules.c
CommitLineData
1da177e4
LT
1/*
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.
5 *
6 * IPv4 Forwarding Information Base: policy rules.
7 *
1da177e4 8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
e1ef4bf2 9 * Thomas Graf <tgraf@suug.ch>
1da177e4
LT
10 *
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.
15 *
16 * Fixes:
17 * Rani Assaf : local_rule cannot be deleted
18 * Marc Boucher : routing by fwmark
19 */
20
1da177e4
LT
21#include <linux/types.h>
22#include <linux/kernel.h>
1da177e4 23#include <linux/netdevice.h>
1da177e4 24#include <linux/netlink.h>
e1ef4bf2 25#include <linux/inetdevice.h>
1da177e4 26#include <linux/init.h>
7b204afd
RO
27#include <linux/list.h>
28#include <linux/rcupdate.h>
1da177e4 29#include <net/ip.h>
1da177e4
LT
30#include <net/route.h>
31#include <net/tcp.h>
1da177e4 32#include <net/ip_fib.h>
e1ef4bf2 33#include <net/fib_rules.h>
1da177e4 34
e1ef4bf2 35static struct fib_rules_ops fib4_rules_ops;
1da177e4 36
e1ef4bf2 37struct fib4_rule
1da177e4 38{
e1ef4bf2
TG
39 struct fib_rule common;
40 u8 dst_len;
41 u8 src_len;
42 u8 tos;
81f7bf6c
AV
43 __be32 src;
44 __be32 srcmask;
45 __be32 dst;
46 __be32 dstmask;
1da177e4 47#ifdef CONFIG_NET_CLS_ROUTE
e1ef4bf2 48 u32 tclassid;
1da177e4 49#endif
1da177e4
LT
50};
51
e1ef4bf2
TG
52static struct fib4_rule default_rule = {
53 .common = {
54 .refcnt = ATOMIC_INIT(2),
55 .pref = 0x7FFF,
56 .table = RT_TABLE_DEFAULT,
57 .action = FR_ACT_TO_TBL,
58 },
1da177e4
LT
59};
60
e1ef4bf2
TG
61static struct fib4_rule main_rule = {
62 .common = {
63 .refcnt = ATOMIC_INIT(2),
64 .pref = 0x7FFE,
65 .table = RT_TABLE_MAIN,
66 .action = FR_ACT_TO_TBL,
67 },
1da177e4
LT
68};
69
e1ef4bf2
TG
70static struct fib4_rule local_rule = {
71 .common = {
72 .refcnt = ATOMIC_INIT(2),
73 .table = RT_TABLE_LOCAL,
74 .action = FR_ACT_TO_TBL,
75 .flags = FIB_RULE_PERMANENT,
76 },
1da177e4
LT
77};
78
e1ef4bf2
TG
79static LIST_HEAD(fib4_rules);
80
81#ifdef CONFIG_NET_CLS_ROUTE
82u32 fib_rules_tclass(struct fib_result *res)
83{
84 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
85}
86#endif
7b204afd 87
e1ef4bf2
TG
88int fib_lookup(struct flowi *flp, struct fib_result *res)
89{
90 struct fib_lookup_arg arg = {
91 .result = res,
92 };
93 int err;
1da177e4 94
e1ef4bf2
TG
95 err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
96 res->r = arg.rule;
a5cdc030 97
e1ef4bf2
TG
98 return err;
99}
100
8ce11e6a
AB
101static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
102 int flags, struct fib_lookup_arg *arg)
1da177e4 103{
e1ef4bf2
TG
104 int err = -EAGAIN;
105 struct fib_table *tbl;
106
107 switch (rule->action) {
108 case FR_ACT_TO_TBL:
109 break;
110
111 case FR_ACT_UNREACHABLE:
112 err = -ENETUNREACH;
113 goto errout;
114
115 case FR_ACT_PROHIBIT:
116 err = -EACCES;
117 goto errout;
118
119 case FR_ACT_BLACKHOLE:
120 default:
121 err = -EINVAL;
122 goto errout;
1da177e4 123 }
e1ef4bf2
TG
124
125 if ((tbl = fib_get_table(rule->table)) == NULL)
126 goto errout;
127
128 err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
129 if (err > 0)
130 err = -EAGAIN;
131errout:
1da177e4
LT
132 return err;
133}
134
e1ef4bf2
TG
135
136void fib_select_default(const struct flowi *flp, struct fib_result *res)
137{
138 if (res->r && res->r->action == FR_ACT_TO_TBL &&
139 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
140 struct fib_table *tb;
141 if ((tb = fib_get_table(res->r->table)) != NULL)
142 tb->tb_select_default(tb, flp, res);
143 }
144}
145
146static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
147{
148 struct fib4_rule *r = (struct fib4_rule *) rule;
81f7bf6c
AV
149 __be32 daddr = fl->fl4_dst;
150 __be32 saddr = fl->fl4_src;
e1ef4bf2
TG
151
152 if (((saddr ^ r->src) & r->srcmask) ||
153 ((daddr ^ r->dst) & r->dstmask))
154 return 0;
155
156 if (r->tos && (r->tos != fl->fl4_tos))
157 return 0;
158
e1ef4bf2
TG
159 return 1;
160}
1da177e4
LT
161
162static struct fib_table *fib_empty_table(void)
163{
2dfe55b4 164 u32 id;
1da177e4
LT
165
166 for (id = 1; id <= RT_TABLE_MAX; id++)
1af5a8c4
PM
167 if (fib_get_table(id) == NULL)
168 return fib_new_table(id);
1da177e4
LT
169 return NULL;
170}
171
e1ef4bf2 172static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
1f6c9557 173 FRA_GENERIC_POLICY,
e1ef4bf2
TG
174 [FRA_FLOW] = { .type = NLA_U32 },
175};
7b204afd 176
e1ef4bf2
TG
177static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
178 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
179 struct nlattr **tb)
1da177e4 180{
e1ef4bf2
TG
181 int err = -EINVAL;
182 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 183
e1701c68 184 if (frh->tos & ~IPTOS_TOS_MASK)
e1ef4bf2 185 goto errout;
7b204afd 186
e1ef4bf2
TG
187 if (rule->table == RT_TABLE_UNSPEC) {
188 if (rule->action == FR_ACT_TO_TBL) {
189 struct fib_table *table;
1da177e4 190
e1ef4bf2
TG
191 table = fib_empty_table();
192 if (table == NULL) {
193 err = -ENOBUFS;
194 goto errout;
195 }
1da177e4 196
e1ef4bf2 197 rule->table = table->tb_id;
1da177e4
LT
198 }
199 }
200
e1701c68 201 if (frh->src_len)
45d60b9e 202 rule4->src = nla_get_be32(tb[FRA_SRC]);
7b204afd 203
e1701c68 204 if (frh->dst_len)
45d60b9e 205 rule4->dst = nla_get_be32(tb[FRA_DST]);
7b204afd 206
1da177e4 207#ifdef CONFIG_NET_CLS_ROUTE
e1ef4bf2
TG
208 if (tb[FRA_FLOW])
209 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
1da177e4
LT
210#endif
211
e1ef4bf2
TG
212 rule4->src_len = frh->src_len;
213 rule4->srcmask = inet_make_mask(rule4->src_len);
214 rule4->dst_len = frh->dst_len;
215 rule4->dstmask = inet_make_mask(rule4->dst_len);
216 rule4->tos = frh->tos;
7b204afd 217
e1ef4bf2
TG
218 err = 0;
219errout:
220 return err;
1da177e4
LT
221}
222
e1ef4bf2
TG
223static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
224 struct nlattr **tb)
1da177e4 225{
e1ef4bf2 226 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 227
e1ef4bf2
TG
228 if (frh->src_len && (rule4->src_len != frh->src_len))
229 return 0;
1da177e4 230
e1ef4bf2
TG
231 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
232 return 0;
7b204afd 233
e1ef4bf2
TG
234 if (frh->tos && (rule4->tos != frh->tos))
235 return 0;
7b204afd 236
e1ef4bf2
TG
237#ifdef CONFIG_NET_CLS_ROUTE
238 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
239 return 0;
240#endif
1da177e4 241
e1701c68 242 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
e1ef4bf2 243 return 0;
1da177e4 244
e1701c68 245 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
e1ef4bf2 246 return 0;
1da177e4 247
e1ef4bf2 248 return 1;
1da177e4
LT
249}
250
e1ef4bf2
TG
251static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
252 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
253{
254 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 255
e1ef4bf2
TG
256 frh->family = AF_INET;
257 frh->dst_len = rule4->dst_len;
258 frh->src_len = rule4->src_len;
259 frh->tos = rule4->tos;
1da177e4 260
e1ef4bf2 261 if (rule4->dst_len)
45d60b9e 262 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
e1ef4bf2
TG
263
264 if (rule4->src_len)
45d60b9e 265 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
e1ef4bf2 266
1da177e4 267#ifdef CONFIG_NET_CLS_ROUTE
e1ef4bf2
TG
268 if (rule4->tclassid)
269 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
1da177e4 270#endif
e1ef4bf2 271 return 0;
1da177e4 272
e1ef4bf2
TG
273nla_put_failure:
274 return -ENOBUFS;
1da177e4
LT
275}
276
e1ef4bf2 277static u32 fib4_rule_default_pref(void)
a5cdc030 278{
e1ef4bf2
TG
279 struct list_head *pos;
280 struct fib_rule *rule;
281
282 if (!list_empty(&fib4_rules)) {
283 pos = fib4_rules.next;
284 if (pos->next != &fib4_rules) {
285 rule = list_entry(pos->next, struct fib_rule, list);
286 if (rule->pref)
287 return rule->pref - 1;
288 }
a5cdc030 289 }
e1ef4bf2
TG
290
291 return 0;
a5cdc030
PM
292}
293
339bf98f
TG
294static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
295{
296 return nla_total_size(4) /* dst */
297 + nla_total_size(4) /* src */
298 + nla_total_size(4); /* flow */
299}
300
73417f61
TG
301static void fib4_rule_flush_cache(void)
302{
303 rt_cache_flush(0);
304}
305
e1ef4bf2
TG
306static struct fib_rules_ops fib4_rules_ops = {
307 .family = AF_INET,
308 .rule_size = sizeof(struct fib4_rule),
e1701c68 309 .addr_size = sizeof(u32),
e1ef4bf2
TG
310 .action = fib4_rule_action,
311 .match = fib4_rule_match,
312 .configure = fib4_rule_configure,
313 .compare = fib4_rule_compare,
314 .fill = fib4_rule_fill,
315 .default_pref = fib4_rule_default_pref,
339bf98f 316 .nlmsg_payload = fib4_rule_nlmsg_payload,
73417f61 317 .flush_cache = fib4_rule_flush_cache,
e1ef4bf2
TG
318 .nlgroup = RTNLGRP_IPV4_RULE,
319 .policy = fib4_rule_policy,
320 .rules_list = &fib4_rules,
321 .owner = THIS_MODULE,
322};
323
324void __init fib4_rules_init(void)
1da177e4 325{
e1ef4bf2
TG
326 list_add_tail(&local_rule.common.list, &fib4_rules);
327 list_add_tail(&main_rule.common.list, &fib4_rules);
328 list_add_tail(&default_rule.common.list, &fib4_rules);
1da177e4 329
e1ef4bf2 330 fib_rules_register(&fib4_rules_ops);
1da177e4 331}