blob: b837c33e0404fc09a8e54ea7798d8e8b698657af [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070048 u32 tclassid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070050};
51
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070052static 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070061static 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070070static 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 },
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070079static LIST_HEAD(fib4_rules);
Robert Olsson7b204af2006-03-20 17:18:53 -080080
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070081#ifdef CONFIG_NET_CLS_ROUTE
82u32 fib_rules_tclass(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070084 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
85}
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070088int fib_lookup(struct flowi *flp, struct fib_result *res)
89{
90 struct fib_lookup_arg arg = {
91 .result = res,
92 };
93 int err;
94
95 err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
96 res->r = arg.rule;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return err;
99}
100
Adrian Bunk8ce11e62006-08-07 21:50:48 -0700101static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
102 int flags, struct fib_lookup_arg *arg)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700103{
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;
123 }
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:
132 return err;
133}
134
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;
Al Viro81f7bf62006-09-27 18:40:00 -0700149 __be32 daddr = fl->fl4_dst;
150 __be32 saddr = fl->fl4_src;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700151
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
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700159 return 1;
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162static struct fib_table *fib_empty_table(void)
163{
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700164 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 for (id = 1; id <= RT_TABLE_MAX; id++)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700167 if (fib_get_table(id) == NULL)
168 return fib_new_table(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return NULL;
170}
171
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700172static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800173 FRA_GENERIC_POLICY,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700174 [FRA_SRC] = { .type = NLA_U32 },
175 [FRA_DST] = { .type = NLA_U32 },
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700176 [FRA_FLOW] = { .type = NLA_U32 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177};
178
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700179static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
180 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
181 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700183 int err = -EINVAL;
184 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700186 if (frh->src_len > 32 || frh->dst_len > 32 ||
187 (frh->tos & ~IPTOS_TOS_MASK))
188 goto errout;
189
190 if (rule->table == RT_TABLE_UNSPEC) {
191 if (rule->action == FR_ACT_TO_TBL) {
192 struct fib_table *table;
193
194 table = fib_empty_table();
195 if (table == NULL) {
196 err = -ENOBUFS;
197 goto errout;
198 }
199
200 rule->table = table->tb_id;
201 }
202 }
203
204 if (tb[FRA_SRC])
Al Viro45d60b92006-09-27 18:40:27 -0700205 rule4->src = nla_get_be32(tb[FRA_SRC]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700206
207 if (tb[FRA_DST])
Al Viro45d60b92006-09-27 18:40:27 -0700208 rule4->dst = nla_get_be32(tb[FRA_DST]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700211 if (tb[FRA_FLOW])
212 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700215 rule4->src_len = frh->src_len;
216 rule4->srcmask = inet_make_mask(rule4->src_len);
217 rule4->dst_len = frh->dst_len;
218 rule4->dstmask = inet_make_mask(rule4->dst_len);
219 rule4->tos = frh->tos;
220
221 err = 0;
222errout:
223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700226static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
227 struct nlattr **tb)
Patrick McHardya5cdc032006-03-23 01:16:06 -0800228{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700229 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800230
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700231 if (frh->src_len && (rule4->src_len != frh->src_len))
232 return 0;
233
234 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
235 return 0;
236
237 if (frh->tos && (rule4->tos != frh->tos))
238 return 0;
239
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700240#ifdef CONFIG_NET_CLS_ROUTE
241 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
242 return 0;
243#endif
244
Al Viro45d60b92006-09-27 18:40:27 -0700245 if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700246 return 0;
247
Al Viro45d60b92006-09-27 18:40:27 -0700248 if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700249 return 0;
250
251 return 1;
252}
253
254static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
255 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
256{
257 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
258
259 frh->family = AF_INET;
260 frh->dst_len = rule4->dst_len;
261 frh->src_len = rule4->src_len;
262 frh->tos = rule4->tos;
263
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700264 if (rule4->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700265 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700266
267 if (rule4->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700268 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700269
270#ifdef CONFIG_NET_CLS_ROUTE
271 if (rule4->tclassid)
272 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
273#endif
274 return 0;
275
276nla_put_failure:
277 return -ENOBUFS;
278}
279
280int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
281{
282 return fib_rules_dump(skb, cb, AF_INET);
283}
284
285static u32 fib4_rule_default_pref(void)
286{
287 struct list_head *pos;
288 struct fib_rule *rule;
289
290 if (!list_empty(&fib4_rules)) {
291 pos = fib4_rules.next;
292 if (pos->next != &fib4_rules) {
293 rule = list_entry(pos->next, struct fib_rule, list);
294 if (rule->pref)
295 return rule->pref - 1;
296 }
Patrick McHardya5cdc032006-03-23 01:16:06 -0800297 }
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700298
299 return 0;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800300}
301
Thomas Graf339bf982006-11-10 14:10:15 -0800302static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
303{
304 return nla_total_size(4) /* dst */
305 + nla_total_size(4) /* src */
306 + nla_total_size(4); /* flow */
307}
308
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700309static struct fib_rules_ops fib4_rules_ops = {
310 .family = AF_INET,
311 .rule_size = sizeof(struct fib4_rule),
312 .action = fib4_rule_action,
313 .match = fib4_rule_match,
314 .configure = fib4_rule_configure,
315 .compare = fib4_rule_compare,
316 .fill = fib4_rule_fill,
317 .default_pref = fib4_rule_default_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800318 .nlmsg_payload = fib4_rule_nlmsg_payload,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700319 .nlgroup = RTNLGRP_IPV4_RULE,
320 .policy = fib4_rule_policy,
321 .rules_list = &fib4_rules,
322 .owner = THIS_MODULE,
323};
324
325void __init fib4_rules_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700327 list_add_tail(&local_rule.common.list, &fib4_rules);
328 list_add_tail(&main_rule.common.list, &fib4_rules);
329 list_add_tail(&default_rule.common.list, &fib4_rules);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700331 fib_rules_register(&fib4_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}