blob: d2001f1c28a271accb227e330d87027b755f6001 [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 -070035struct fib4_rule
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070037 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 u8 tos;
Al Viro81f7bf62006-09-27 18:40:00 -070041 __be32 src;
42 __be32 srcmask;
43 __be32 dst;
44 __be32 dstmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070046 u32 tclassid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070050#ifdef CONFIG_NET_CLS_ROUTE
51u32 fib_rules_tclass(struct fib_result *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070053 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070057int fib_lookup(struct flowi *flp, struct fib_result *res)
58{
59 struct fib_lookup_arg arg = {
60 .result = res,
61 };
62 int err;
63
Denis V. Luneve4e49712008-01-10 03:27:51 -080064 err = fib_rules_lookup(init_net.ipv4.rules_ops, flp, 0, &arg);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070065 res->r = arg.rule;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return err;
68}
69
Adrian Bunk8ce11e62006-08-07 21:50:48 -070070static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
71 int flags, struct fib_lookup_arg *arg)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070072{
73 int err = -EAGAIN;
74 struct fib_table *tbl;
75
76 switch (rule->action) {
77 case FR_ACT_TO_TBL:
78 break;
79
80 case FR_ACT_UNREACHABLE:
81 err = -ENETUNREACH;
82 goto errout;
83
84 case FR_ACT_PROHIBIT:
85 err = -EACCES;
86 goto errout;
87
88 case FR_ACT_BLACKHOLE:
89 default:
90 err = -EINVAL;
91 goto errout;
92 }
93
Denis V. Lunev51314a12008-01-20 16:47:09 -080094 if ((tbl = fib_get_table(rule->fr_net, rule->table)) == NULL)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070095 goto errout;
96
97 err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
98 if (err > 0)
99 err = -EAGAIN;
100errout:
101 return err;
102}
103
104
105void fib_select_default(const struct flowi *flp, struct fib_result *res)
106{
107 if (res->r && res->r->action == FR_ACT_TO_TBL &&
108 FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
109 struct fib_table *tb;
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800110 if ((tb = fib_get_table(&init_net, res->r->table)) != NULL)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700111 tb->tb_select_default(tb, flp, res);
112 }
113}
114
115static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
116{
117 struct fib4_rule *r = (struct fib4_rule *) rule;
Al Viro81f7bf62006-09-27 18:40:00 -0700118 __be32 daddr = fl->fl4_dst;
119 __be32 saddr = fl->fl4_src;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700120
121 if (((saddr ^ r->src) & r->srcmask) ||
122 ((daddr ^ r->dst) & r->dstmask))
123 return 0;
124
125 if (r->tos && (r->tos != fl->fl4_tos))
126 return 0;
127
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700128 return 1;
129}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800131static struct fib_table *fib_empty_table(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700133 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 for (id = 1; id <= RT_TABLE_MAX; id++)
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800136 if (fib_get_table(net, id) == NULL)
137 return fib_new_table(net, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return NULL;
139}
140
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700141static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800142 FRA_GENERIC_POLICY,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700143 [FRA_FLOW] = { .type = NLA_U32 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700146static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
147 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
148 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Denis V. Luneve4e49712008-01-10 03:27:51 -0800150 struct net *net = skb->sk->sk_net;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700151 int err = -EINVAL;
152 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Thomas Grafe1701c62007-03-24 12:46:02 -0700154 if (frh->tos & ~IPTOS_TOS_MASK)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700155 goto errout;
156
157 if (rule->table == RT_TABLE_UNSPEC) {
158 if (rule->action == FR_ACT_TO_TBL) {
159 struct fib_table *table;
160
Denis V. Luneve4e49712008-01-10 03:27:51 -0800161 table = fib_empty_table(net);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700162 if (table == NULL) {
163 err = -ENOBUFS;
164 goto errout;
165 }
166
167 rule->table = table->tb_id;
168 }
169 }
170
Thomas Grafe1701c62007-03-24 12:46:02 -0700171 if (frh->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700172 rule4->src = nla_get_be32(tb[FRA_SRC]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700173
Thomas Grafe1701c62007-03-24 12:46:02 -0700174 if (frh->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700175 rule4->dst = nla_get_be32(tb[FRA_DST]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177#ifdef CONFIG_NET_CLS_ROUTE
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700178 if (tb[FRA_FLOW])
179 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700182 rule4->src_len = frh->src_len;
183 rule4->srcmask = inet_make_mask(rule4->src_len);
184 rule4->dst_len = frh->dst_len;
185 rule4->dstmask = inet_make_mask(rule4->dst_len);
186 rule4->tos = frh->tos;
187
188 err = 0;
189errout:
190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700193static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
194 struct nlattr **tb)
Patrick McHardya5cdc032006-03-23 01:16:06 -0800195{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700196 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800197
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700198 if (frh->src_len && (rule4->src_len != frh->src_len))
199 return 0;
200
201 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
202 return 0;
203
204 if (frh->tos && (rule4->tos != frh->tos))
205 return 0;
206
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700207#ifdef CONFIG_NET_CLS_ROUTE
208 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
209 return 0;
210#endif
211
Thomas Grafe1701c62007-03-24 12:46:02 -0700212 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700213 return 0;
214
Thomas Grafe1701c62007-03-24 12:46:02 -0700215 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700216 return 0;
217
218 return 1;
219}
220
221static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
222 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
223{
224 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
225
226 frh->family = AF_INET;
227 frh->dst_len = rule4->dst_len;
228 frh->src_len = rule4->src_len;
229 frh->tos = rule4->tos;
230
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700231 if (rule4->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700232 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700233
234 if (rule4->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700235 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700236
237#ifdef CONFIG_NET_CLS_ROUTE
238 if (rule4->tclassid)
239 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
240#endif
241 return 0;
242
243nla_put_failure:
244 return -ENOBUFS;
245}
246
Denis V. Lunev868d13a2008-01-10 03:18:25 -0800247static u32 fib4_rule_default_pref(struct fib_rules_ops *ops)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700248{
249 struct list_head *pos;
250 struct fib_rule *rule;
251
Denis V. Luneve4e49712008-01-10 03:27:51 -0800252 if (!list_empty(&ops->rules_list)) {
253 pos = ops->rules_list.next;
254 if (pos->next != &ops->rules_list) {
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700255 rule = list_entry(pos->next, struct fib_rule, list);
256 if (rule->pref)
257 return rule->pref - 1;
258 }
Patrick McHardya5cdc032006-03-23 01:16:06 -0800259 }
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700260
261 return 0;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800262}
263
Thomas Graf339bf982006-11-10 14:10:15 -0800264static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
265{
266 return nla_total_size(4) /* dst */
267 + nla_total_size(4) /* src */
268 + nla_total_size(4); /* flow */
269}
270
Thomas Graf73417f62007-03-27 13:56:52 -0700271static void fib4_rule_flush_cache(void)
272{
Thomas Graf4b19ca42007-03-28 14:18:52 -0700273 rt_cache_flush(-1);
Thomas Graf73417f62007-03-27 13:56:52 -0700274}
275
Denis V. Luneve4e49712008-01-10 03:27:51 -0800276static struct fib_rules_ops fib4_rules_ops_template = {
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700277 .family = AF_INET,
278 .rule_size = sizeof(struct fib4_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700279 .addr_size = sizeof(u32),
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700280 .action = fib4_rule_action,
281 .match = fib4_rule_match,
282 .configure = fib4_rule_configure,
283 .compare = fib4_rule_compare,
284 .fill = fib4_rule_fill,
285 .default_pref = fib4_rule_default_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800286 .nlmsg_payload = fib4_rule_nlmsg_payload,
Thomas Graf73417f62007-03-27 13:56:52 -0700287 .flush_cache = fib4_rule_flush_cache,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700288 .nlgroup = RTNLGRP_IPV4_RULE,
289 .policy = fib4_rule_policy,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700290 .owner = THIS_MODULE,
291};
292
Denis V. Luneve4e49712008-01-10 03:27:51 -0800293static int fib_default_rules_init(struct fib_rules_ops *ops)
Denis V. Lunev2994c632007-11-10 22:12:03 -0800294{
295 int err;
296
Denis V. Luneve4e49712008-01-10 03:27:51 -0800297 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, FIB_RULE_PERMANENT);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800298 if (err < 0)
299 return err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800300 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800301 if (err < 0)
302 return err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800303 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800304 if (err < 0)
305 return err;
306 return 0;
307}
308
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800309int __net_init fib4_rules_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800311 int err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800312 struct fib_rules_ops *ops;
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800313
Denis V. Luneve4e49712008-01-10 03:27:51 -0800314 ops = kmemdup(&fib4_rules_ops_template, sizeof(*ops), GFP_KERNEL);
315 if (ops == NULL)
316 return -ENOMEM;
317 INIT_LIST_HEAD(&ops->rules_list);
Denis V. Lunev03592382008-01-20 16:46:01 -0800318 ops->fro_net = net;
319
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800320 fib_rules_register(ops);
Denis V. Luneve4e49712008-01-10 03:27:51 -0800321
322 err = fib_default_rules_init(ops);
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800323 if (err < 0)
324 goto fail;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800325 net->ipv4.rules_ops = ops;
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800326 return 0;
327
328fail:
329 /* also cleans all rules already added */
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800330 fib_rules_unregister(ops);
Denis V. Luneve4e49712008-01-10 03:27:51 -0800331 kfree(ops);
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800334
335void __net_exit fib4_rules_exit(struct net *net)
336{
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800337 fib_rules_unregister(net->ipv4.rules_ops);
Denis V. Luneve4e49712008-01-10 03:27:51 -0800338 kfree(net->ipv4.rules_ops);
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800339}