blob: a0ada3a8d8dd8c2cfd931cc0c475c73aadf8d62b [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 -070052#ifdef CONFIG_NET_CLS_ROUTE
53u32 fib_rules_tclass(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070055 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070059int fib_lookup(struct flowi *flp, struct fib_result *res)
60{
61 struct fib_lookup_arg arg = {
62 .result = res,
63 };
64 int err;
65
66 err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
67 res->r = arg.rule;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 return err;
70}
71
Adrian Bunk8ce11e62006-08-07 21:50:48 -070072static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
73 int flags, struct fib_lookup_arg *arg)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070074{
75 int err = -EAGAIN;
76 struct fib_table *tbl;
77
78 switch (rule->action) {
79 case FR_ACT_TO_TBL:
80 break;
81
82 case FR_ACT_UNREACHABLE:
83 err = -ENETUNREACH;
84 goto errout;
85
86 case FR_ACT_PROHIBIT:
87 err = -EACCES;
88 goto errout;
89
90 case FR_ACT_BLACKHOLE:
91 default:
92 err = -EINVAL;
93 goto errout;
94 }
95
96 if ((tbl = fib_get_table(rule->table)) == NULL)
97 goto errout;
98
99 err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
100 if (err > 0)
101 err = -EAGAIN;
102errout:
103 return err;
104}
105
106
107void fib_select_default(const struct flowi *flp, struct fib_result *res)
108{
109 if (res->r && res->r->action == FR_ACT_TO_TBL &&
110 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
111 struct fib_table *tb;
112 if ((tb = fib_get_table(res->r->table)) != NULL)
113 tb->tb_select_default(tb, flp, res);
114 }
115}
116
117static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
118{
119 struct fib4_rule *r = (struct fib4_rule *) rule;
Al Viro81f7bf62006-09-27 18:40:00 -0700120 __be32 daddr = fl->fl4_dst;
121 __be32 saddr = fl->fl4_src;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700122
123 if (((saddr ^ r->src) & r->srcmask) ||
124 ((daddr ^ r->dst) & r->dstmask))
125 return 0;
126
127 if (r->tos && (r->tos != fl->fl4_tos))
128 return 0;
129
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700130 return 1;
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133static struct fib_table *fib_empty_table(void)
134{
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700135 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 for (id = 1; id <= RT_TABLE_MAX; id++)
Patrick McHardy1af5a8c2006-08-10 23:10:46 -0700138 if (fib_get_table(id) == NULL)
139 return fib_new_table(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return NULL;
141}
142
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700143static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800144 FRA_GENERIC_POLICY,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700145 [FRA_FLOW] = { .type = NLA_U32 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700148static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
149 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
150 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700152 int err = -EINVAL;
153 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Thomas Grafe1701c62007-03-24 12:46:02 -0700155 if (frh->tos & ~IPTOS_TOS_MASK)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700156 goto errout;
157
158 if (rule->table == RT_TABLE_UNSPEC) {
159 if (rule->action == FR_ACT_TO_TBL) {
160 struct fib_table *table;
161
162 table = fib_empty_table();
163 if (table == NULL) {
164 err = -ENOBUFS;
165 goto errout;
166 }
167
168 rule->table = table->tb_id;
169 }
170 }
171
Thomas Grafe1701c62007-03-24 12:46:02 -0700172 if (frh->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700173 rule4->src = nla_get_be32(tb[FRA_SRC]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700174
Thomas Grafe1701c62007-03-24 12:46:02 -0700175 if (frh->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700176 rule4->dst = nla_get_be32(tb[FRA_DST]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700179 if (tb[FRA_FLOW])
180 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700183 rule4->src_len = frh->src_len;
184 rule4->srcmask = inet_make_mask(rule4->src_len);
185 rule4->dst_len = frh->dst_len;
186 rule4->dstmask = inet_make_mask(rule4->dst_len);
187 rule4->tos = frh->tos;
188
189 err = 0;
190errout:
191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700194static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
195 struct nlattr **tb)
Patrick McHardya5cdc032006-03-23 01:16:06 -0800196{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700197 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800198
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700199 if (frh->src_len && (rule4->src_len != frh->src_len))
200 return 0;
201
202 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
203 return 0;
204
205 if (frh->tos && (rule4->tos != frh->tos))
206 return 0;
207
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700208#ifdef CONFIG_NET_CLS_ROUTE
209 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
210 return 0;
211#endif
212
Thomas Grafe1701c62007-03-24 12:46:02 -0700213 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700214 return 0;
215
Thomas Grafe1701c62007-03-24 12:46:02 -0700216 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700217 return 0;
218
219 return 1;
220}
221
222static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
223 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
224{
225 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
226
227 frh->family = AF_INET;
228 frh->dst_len = rule4->dst_len;
229 frh->src_len = rule4->src_len;
230 frh->tos = rule4->tos;
231
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700232 if (rule4->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700233 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700234
235 if (rule4->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700236 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700237
238#ifdef CONFIG_NET_CLS_ROUTE
239 if (rule4->tclassid)
240 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
241#endif
242 return 0;
243
244nla_put_failure:
245 return -ENOBUFS;
246}
247
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700248static u32 fib4_rule_default_pref(void)
249{
250 struct list_head *pos;
251 struct fib_rule *rule;
252
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700253 if (!list_empty(&fib4_rules_ops.rules_list)) {
254 pos = fib4_rules_ops.rules_list.next;
255 if (pos->next != &fib4_rules_ops.rules_list) {
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700256 rule = list_entry(pos->next, struct fib_rule, list);
257 if (rule->pref)
258 return rule->pref - 1;
259 }
Patrick McHardya5cdc032006-03-23 01:16:06 -0800260 }
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700261
262 return 0;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800263}
264
Thomas Graf339bf982006-11-10 14:10:15 -0800265static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
266{
267 return nla_total_size(4) /* dst */
268 + nla_total_size(4) /* src */
269 + nla_total_size(4); /* flow */
270}
271
Thomas Graf73417f62007-03-27 13:56:52 -0700272static void fib4_rule_flush_cache(void)
273{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700274 rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700275}
276
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700277static struct fib_rules_ops fib4_rules_ops = {
278 .family = AF_INET,
279 .rule_size = sizeof(struct fib4_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700280 .addr_size = sizeof(u32),
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700281 .action = fib4_rule_action,
282 .match = fib4_rule_match,
283 .configure = fib4_rule_configure,
284 .compare = fib4_rule_compare,
285 .fill = fib4_rule_fill,
286 .default_pref = fib4_rule_default_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800287 .nlmsg_payload = fib4_rule_nlmsg_payload,
Thomas Graf73417f62007-03-27 13:56:52 -0700288 .flush_cache = fib4_rule_flush_cache,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700289 .nlgroup = RTNLGRP_IPV4_RULE,
290 .policy = fib4_rule_policy,
Denis V. Lunev76c72d42007-09-16 15:44:27 -0700291 .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list),
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700292 .owner = THIS_MODULE,
293};
294
Denis V. Lunev2994c632007-11-10 22:12:03 -0800295static int __init fib_default_rules_init(void)
296{
297 int err;
298
299 err = fib_default_rule_add(&fib4_rules_ops, 0,
300 RT_TABLE_LOCAL, FIB_RULE_PERMANENT);
301 if (err < 0)
302 return err;
303 err = fib_default_rule_add(&fib4_rules_ops, 0x7FFE,
304 RT_TABLE_MAIN, 0);
305 if (err < 0)
306 return err;
307 err = fib_default_rule_add(&fib4_rules_ops, 0x7FFF,
308 RT_TABLE_DEFAULT, 0);
309 if (err < 0)
310 return err;
311 return 0;
312}
313
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700314void __init fib4_rules_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Denis V. Lunev2994c632007-11-10 22:12:03 -0800316 BUG_ON(fib_default_rules_init());
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700317 fib_rules_register(&fib4_rules_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}