blob: de8d5dd7099b44c0a342af87c5288904d27ce701 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -07009 * Thomas Graf <tgraf@suug.ch>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/types.h>
22#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/netlink.h>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070025#include <linux/inetdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
Robert Olsson7b204af2006-03-20 17:18:53 -080027#include <linux/list.h>
28#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/route.h>
31#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <net/ip_fib.h>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070033#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070035static struct fib_rules_ops fib4_rules_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070037struct fib4_rule
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070039 struct fib_rule common;
40 u8 dst_len;
41 u8 src_len;
42 u8 tos;
Al Viro81f7bf62006-09-27 18:40:00 -070043 __be32 src;
44 __be32 srcmask;
45 __be32 dst;
46 __be32 dstmask;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070047 u32 fwmark;
Patrick McHardybbfb39c2006-08-25 16:10:14 -070048 u32 fwmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070050 u32 tclassid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070054static struct fib4_rule default_rule = {
55 .common = {
56 .refcnt = ATOMIC_INIT(2),
57 .pref = 0x7FFF,
58 .table = RT_TABLE_DEFAULT,
59 .action = FR_ACT_TO_TBL,
60 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070063static struct fib4_rule main_rule = {
64 .common = {
65 .refcnt = ATOMIC_INIT(2),
66 .pref = 0x7FFE,
67 .table = RT_TABLE_MAIN,
68 .action = FR_ACT_TO_TBL,
69 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070070};
71
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070072static struct fib4_rule local_rule = {
73 .common = {
74 .refcnt = ATOMIC_INIT(2),
75 .table = RT_TABLE_LOCAL,
76 .action = FR_ACT_TO_TBL,
77 .flags = FIB_RULE_PERMANENT,
78 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070081static LIST_HEAD(fib4_rules);
Robert Olsson7b204af2006-03-20 17:18:53 -080082
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070083#ifdef CONFIG_NET_CLS_ROUTE
84u32 fib_rules_tclass(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070086 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070090int fib_lookup(struct flowi *flp, struct fib_result *res)
91{
92 struct fib_lookup_arg arg = {
93 .result = res,
94 };
95 int err;
96
97 err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
98 res->r = arg.rule;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return err;
101}
102
Adrian Bunk8ce11e62006-08-07 21:50:48 -0700103static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
104 int flags, struct fib_lookup_arg *arg)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700105{
106 int err = -EAGAIN;
107 struct fib_table *tbl;
108
109 switch (rule->action) {
110 case FR_ACT_TO_TBL:
111 break;
112
113 case FR_ACT_UNREACHABLE:
114 err = -ENETUNREACH;
115 goto errout;
116
117 case FR_ACT_PROHIBIT:
118 err = -EACCES;
119 goto errout;
120
121 case FR_ACT_BLACKHOLE:
122 default:
123 err = -EINVAL;
124 goto errout;
125 }
126
127 if ((tbl = fib_get_table(rule->table)) == NULL)
128 goto errout;
129
130 err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
131 if (err > 0)
132 err = -EAGAIN;
133errout:
134 return err;
135}
136
137
138void fib_select_default(const struct flowi *flp, struct fib_result *res)
139{
140 if (res->r && res->r->action == FR_ACT_TO_TBL &&
141 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
142 struct fib_table *tb;
143 if ((tb = fib_get_table(res->r->table)) != NULL)
144 tb->tb_select_default(tb, flp, res);
145 }
146}
147
148static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
149{
150 struct fib4_rule *r = (struct fib4_rule *) rule;
Al Viro81f7bf62006-09-27 18:40:00 -0700151 __be32 daddr = fl->fl4_dst;
152 __be32 saddr = fl->fl4_src;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700153
154 if (((saddr ^ r->src) & r->srcmask) ||
155 ((daddr ^ r->dst) & r->dstmask))
156 return 0;
157
158 if (r->tos && (r->tos != fl->fl4_tos))
159 return 0;
160
Thomas Graf47dcf0c2006-11-09 15:20:38 -0800161 if ((r->fwmark ^ fl->mark) & r->fwmask)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700162 return 0;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700163
164 return 1;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167static struct fib_table *fib_empty_table(void)
168{
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700169 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 for (id = 1; id <= RT_TABLE_MAX; id++)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700172 if (fib_get_table(id) == NULL)
173 return fib_new_table(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return NULL;
175}
176
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700177static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf5176f912006-08-26 20:13:18 -0700178 [FRA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700179 [FRA_PRIORITY] = { .type = NLA_U32 },
180 [FRA_SRC] = { .type = NLA_U32 },
181 [FRA_DST] = { .type = NLA_U32 },
182 [FRA_FWMARK] = { .type = NLA_U32 },
Patrick McHardybbfb39c2006-08-25 16:10:14 -0700183 [FRA_FWMASK] = { .type = NLA_U32 },
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700184 [FRA_FLOW] = { .type = NLA_U32 },
Patrick McHardy9e762a42006-08-10 23:09:48 -0700185 [FRA_TABLE] = { .type = NLA_U32 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};
187
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700188static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
190 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700192 int err = -EINVAL;
193 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700195 if (frh->src_len > 32 || frh->dst_len > 32 ||
196 (frh->tos & ~IPTOS_TOS_MASK))
197 goto errout;
198
199 if (rule->table == RT_TABLE_UNSPEC) {
200 if (rule->action == FR_ACT_TO_TBL) {
201 struct fib_table *table;
202
203 table = fib_empty_table();
204 if (table == NULL) {
205 err = -ENOBUFS;
206 goto errout;
207 }
208
209 rule->table = table->tb_id;
210 }
211 }
212
213 if (tb[FRA_SRC])
Al Viro45d60b92006-09-27 18:40:27 -0700214 rule4->src = nla_get_be32(tb[FRA_SRC]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700215
216 if (tb[FRA_DST])
Al Viro45d60b92006-09-27 18:40:27 -0700217 rule4->dst = nla_get_be32(tb[FRA_DST]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700218
Patrick McHardybbfb39c2006-08-25 16:10:14 -0700219 if (tb[FRA_FWMARK]) {
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700220 rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]);
Patrick McHardybbfb39c2006-08-25 16:10:14 -0700221 if (rule4->fwmark)
222 /* compatibility: if the mark value is non-zero all bits
223 * are compared unless a mask is explicitly specified.
224 */
225 rule4->fwmask = 0xFFFFFFFF;
226 }
227
228 if (tb[FRA_FWMASK])
229 rule4->fwmask = nla_get_u32(tb[FRA_FWMASK]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700232 if (tb[FRA_FLOW])
233 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700236 rule4->src_len = frh->src_len;
237 rule4->srcmask = inet_make_mask(rule4->src_len);
238 rule4->dst_len = frh->dst_len;
239 rule4->dstmask = inet_make_mask(rule4->dst_len);
240 rule4->tos = frh->tos;
241
242 err = 0;
243errout:
244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700247static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
248 struct nlattr **tb)
Patrick McHardya5cdc032006-03-23 01:16:06 -0800249{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700250 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800251
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700252 if (frh->src_len && (rule4->src_len != frh->src_len))
253 return 0;
254
255 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
256 return 0;
257
258 if (frh->tos && (rule4->tos != frh->tos))
259 return 0;
260
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700261 if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK])))
262 return 0;
Patrick McHardybbfb39c2006-08-25 16:10:14 -0700263
264 if (tb[FRA_FWMASK] && (rule4->fwmask != nla_get_u32(tb[FRA_FWMASK])))
265 return 0;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700266
267#ifdef CONFIG_NET_CLS_ROUTE
268 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
269 return 0;
270#endif
271
Al Viro45d60b92006-09-27 18:40:27 -0700272 if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700273 return 0;
274
Al Viro45d60b92006-09-27 18:40:27 -0700275 if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700276 return 0;
277
278 return 1;
279}
280
281static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
282 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
283{
284 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
285
286 frh->family = AF_INET;
287 frh->dst_len = rule4->dst_len;
288 frh->src_len = rule4->src_len;
289 frh->tos = rule4->tos;
290
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700291 if (rule4->fwmark)
292 NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark);
Patrick McHardybbfb39c2006-08-25 16:10:14 -0700293
294 if (rule4->fwmask || rule4->fwmark)
295 NLA_PUT_U32(skb, FRA_FWMASK, rule4->fwmask);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700296
297 if (rule4->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700298 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700299
300 if (rule4->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700301 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700302
303#ifdef CONFIG_NET_CLS_ROUTE
304 if (rule4->tclassid)
305 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
306#endif
307 return 0;
308
309nla_put_failure:
310 return -ENOBUFS;
311}
312
313int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
314{
315 return fib_rules_dump(skb, cb, AF_INET);
316}
317
318static u32 fib4_rule_default_pref(void)
319{
320 struct list_head *pos;
321 struct fib_rule *rule;
322
323 if (!list_empty(&fib4_rules)) {
324 pos = fib4_rules.next;
325 if (pos->next != &fib4_rules) {
326 rule = list_entry(pos->next, struct fib_rule, list);
327 if (rule->pref)
328 return rule->pref - 1;
329 }
Patrick McHardya5cdc032006-03-23 01:16:06 -0800330 }
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700331
332 return 0;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800333}
334
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700335static struct fib_rules_ops fib4_rules_ops = {
336 .family = AF_INET,
337 .rule_size = sizeof(struct fib4_rule),
338 .action = fib4_rule_action,
339 .match = fib4_rule_match,
340 .configure = fib4_rule_configure,
341 .compare = fib4_rule_compare,
342 .fill = fib4_rule_fill,
343 .default_pref = fib4_rule_default_pref,
344 .nlgroup = RTNLGRP_IPV4_RULE,
345 .policy = fib4_rule_policy,
346 .rules_list = &fib4_rules,
347 .owner = THIS_MODULE,
348};
349
350void __init fib4_rules_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700352 list_add_tail(&local_rule.common.list, &fib4_rules);
353 list_add_tail(&main_rule.common.list, &fib4_rules);
354 list_add_tail(&default_rule.common.list, &fib4_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700356 fib_rules_register(&fib4_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}