blob: 9f2906679d1f0c874245381d4b338ce8b1d3965b [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>
Eric Dumazet6a31d2a2010-10-04 20:00:18 +00009 * 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:
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000017 * Rani Assaf : local_rule cannot be deleted
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040029#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <net/route.h>
32#include <net/tcp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <net/ip_fib.h>
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070034#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000036struct fib4_rule {
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;
Patrick McHardyc7066f72011-01-14 13:36:42 +010045#ifdef CONFIG_IP_ROUTE_CLASSID
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
David S. Millerf4530fa2012-07-05 22:13:13 -070050int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070051{
52 struct fib_lookup_arg arg = {
53 .result = res,
Eric Dumazetebc0ffa2010-10-05 10:41:36 +000054 .flags = FIB_LOOKUP_NOREF,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070055 };
56 int err;
57
David S. Miller22bd5b92011-03-11 19:54:08 -050058 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
David S. Miller85b91b02012-07-13 08:21:29 -070059#ifdef CONFIG_IP_ROUTE_CLASSID
60 if (arg.rule)
61 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
62 else
63 res->tclassid = 0;
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 return err;
66}
David S. Millerf4530fa2012-07-05 22:13:13 -070067EXPORT_SYMBOL_GPL(__fib_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Adrian Bunk8ce11e62006-08-07 21:50:48 -070069static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
70 int flags, struct fib_lookup_arg *arg)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070071{
72 int err = -EAGAIN;
73 struct fib_table *tbl;
74
75 switch (rule->action) {
76 case FR_ACT_TO_TBL:
77 break;
78
79 case FR_ACT_UNREACHABLE:
80 err = -ENETUNREACH;
81 goto errout;
82
83 case FR_ACT_PROHIBIT:
84 err = -EACCES;
85 goto errout;
86
87 case FR_ACT_BLACKHOLE:
88 default:
89 err = -EINVAL;
90 goto errout;
91 }
92
Eric Dumazet6a31d2a2010-10-04 20:00:18 +000093 tbl = fib_get_table(rule->fr_net, rule->table);
94 if (!tbl)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070095 goto errout;
96
David S. Miller22bd5b92011-03-11 19:54:08 -050097 err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -070098 if (err > 0)
99 err = -EAGAIN;
100errout:
101 return err;
102}
103
Stefan Tomanek7764a452013-08-01 02:17:15 +0200104static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
105{
106 /* do not accept result if the route does
107 * not meet the required prefix length
108 */
109 struct fib_result *result = (struct fib_result *) arg->result;
110 if (result->prefixlen < rule->table_prefixlen_min) {
111 if (!(arg->flags & FIB_LOOKUP_NOREF))
112 fib_info_put(result->fi);
113 return true;
114 }
115 return false;
116}
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700117
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700118static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
119{
120 struct fib4_rule *r = (struct fib4_rule *) rule;
David S. Miller9ade2282011-03-12 02:02:42 -0500121 struct flowi4 *fl4 = &fl->u.ip4;
122 __be32 daddr = fl4->daddr;
123 __be32 saddr = fl4->saddr;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700124
125 if (((saddr ^ r->src) & r->srcmask) ||
126 ((daddr ^ r->dst) & r->dstmask))
127 return 0;
128
David S. Miller9ade2282011-03-12 02:02:42 -0500129 if (r->tos && (r->tos != fl4->flowi4_tos))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700130 return 0;
131
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700132 return 1;
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800135static struct fib_table *fib_empty_table(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700137 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 for (id = 1; id <= RT_TABLE_MAX; id++)
Denis V. Lunev8ad49422008-01-10 03:24:11 -0800140 if (fib_get_table(net, id) == NULL)
141 return fib_new_table(net, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return NULL;
143}
144
Patrick McHardyef7c79e2007-06-05 12:38:30 -0700145static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
Thomas Graf1f6c9552006-11-09 15:22:48 -0800146 FRA_GENERIC_POLICY,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700147 [FRA_FLOW] = { .type = NLA_U32 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148};
149
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700150static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen8b3521e2009-05-11 05:52:49 +0000151 struct fib_rule_hdr *frh,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700152 struct nlattr **tb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900154 struct net *net = sock_net(skb->sk);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700155 int err = -EINVAL;
156 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Thomas Grafe1701c62007-03-24 12:46:02 -0700158 if (frh->tos & ~IPTOS_TOS_MASK)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700159 goto errout;
160
161 if (rule->table == RT_TABLE_UNSPEC) {
162 if (rule->action == FR_ACT_TO_TBL) {
163 struct fib_table *table;
164
Denis V. Luneve4e49712008-01-10 03:27:51 -0800165 table = fib_empty_table(net);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700166 if (table == NULL) {
167 err = -ENOBUFS;
168 goto errout;
169 }
170
171 rule->table = table->tb_id;
172 }
173 }
174
Thomas Grafe1701c62007-03-24 12:46:02 -0700175 if (frh->src_len)
Al Viro45d60b92006-09-27 18:40:27 -0700176 rule4->src = nla_get_be32(tb[FRA_SRC]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700177
Thomas Grafe1701c62007-03-24 12:46:02 -0700178 if (frh->dst_len)
Al Viro45d60b92006-09-27 18:40:27 -0700179 rule4->dst = nla_get_be32(tb[FRA_DST]);
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700180
Patrick McHardyc7066f72011-01-14 13:36:42 +0100181#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700182 if (tb[FRA_FLOW]) {
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700183 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700184 if (rule4->tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700185 net->ipv4.fib_num_tclassid_users++;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700189 rule4->src_len = frh->src_len;
190 rule4->srcmask = inet_make_mask(rule4->src_len);
191 rule4->dst_len = frh->dst_len;
192 rule4->dstmask = inet_make_mask(rule4->dst_len);
193 rule4->tos = frh->tos;
194
David S. Millerf4530fa2012-07-05 22:13:13 -0700195 net->ipv4.fib_has_custom_rules = true;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700196 err = 0;
197errout:
198 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700201static void fib4_rule_delete(struct fib_rule *rule)
202{
David S. Millerf4530fa2012-07-05 22:13:13 -0700203 struct net *net = rule->fr_net;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700204#ifdef CONFIG_IP_ROUTE_CLASSID
205 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
206
207 if (rule4->tclassid)
David S. Millerf4530fa2012-07-05 22:13:13 -0700208 net->ipv4.fib_num_tclassid_users--;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700209#endif
David S. Millerf4530fa2012-07-05 22:13:13 -0700210 net->ipv4.fib_has_custom_rules = true;
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700211}
212
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700213static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214 struct nlattr **tb)
Patrick McHardya5cdc032006-03-23 01:16:06 -0800215{
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700216 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
Patrick McHardya5cdc032006-03-23 01:16:06 -0800217
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700218 if (frh->src_len && (rule4->src_len != frh->src_len))
219 return 0;
220
221 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
222 return 0;
223
224 if (frh->tos && (rule4->tos != frh->tos))
225 return 0;
226
Patrick McHardyc7066f72011-01-14 13:36:42 +0100227#ifdef CONFIG_IP_ROUTE_CLASSID
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700228 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
229 return 0;
230#endif
231
Thomas Grafe1701c62007-03-24 12:46:02 -0700232 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700233 return 0;
234
Thomas Grafe1701c62007-03-24 12:46:02 -0700235 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700236 return 0;
237
238 return 1;
239}
240
241static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
Rami Rosen04af8cf2009-05-20 17:26:23 -0700242 struct fib_rule_hdr *frh)
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700243{
244 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
245
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700246 frh->dst_len = rule4->dst_len;
247 frh->src_len = rule4->src_len;
248 frh->tos = rule4->tos;
249
David S. Millerf3756b72012-04-01 20:39:02 -0400250 if ((rule4->dst_len &&
251 nla_put_be32(skb, FRA_DST, rule4->dst)) ||
252 (rule4->src_len &&
253 nla_put_be32(skb, FRA_SRC, rule4->src)))
254 goto nla_put_failure;
Patrick McHardyc7066f72011-01-14 13:36:42 +0100255#ifdef CONFIG_IP_ROUTE_CLASSID
David S. Millerf3756b72012-04-01 20:39:02 -0400256 if (rule4->tclassid &&
257 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
258 goto nla_put_failure;
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700259#endif
260 return 0;
261
262nla_put_failure:
263 return -ENOBUFS;
264}
265
Thomas Graf339bf982006-11-10 14:10:15 -0800266static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
267{
268 return nla_total_size(4) /* dst */
269 + nla_total_size(4) /* src */
270 + nla_total_size(4); /* flow */
271}
272
Denis V. Lunevae299fc2008-07-05 19:01:28 -0700273static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
Thomas Graf73417f62007-03-27 13:56:52 -0700274{
Nicolas Dichtelbafa6d92012-09-07 00:45:29 +0000275 rt_cache_flush(ops->fro_net);
Thomas Graf73417f62007-03-27 13:56:52 -0700276}
277
Andi Kleen04a6f822012-10-04 17:12:11 -0700278static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200279 .family = AF_INET,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700280 .rule_size = sizeof(struct fib4_rule),
Thomas Grafe1701c62007-03-24 12:46:02 -0700281 .addr_size = sizeof(u32),
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700282 .action = fib4_rule_action,
Stefan Tomanek7764a452013-08-01 02:17:15 +0200283 .suppress = fib4_rule_suppress,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700284 .match = fib4_rule_match,
285 .configure = fib4_rule_configure,
David S. Miller7a9bc9b2012-06-29 01:32:45 -0700286 .delete = fib4_rule_delete,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700287 .compare = fib4_rule_compare,
288 .fill = fib4_rule_fill,
Patrick McHardyd8a566b2010-04-13 05:03:15 +0000289 .default_pref = fib_default_rule_pref,
Thomas Graf339bf982006-11-10 14:10:15 -0800290 .nlmsg_payload = fib4_rule_nlmsg_payload,
Thomas Graf73417f62007-03-27 13:56:52 -0700291 .flush_cache = fib4_rule_flush_cache,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700292 .nlgroup = RTNLGRP_IPV4_RULE,
293 .policy = fib4_rule_policy,
Thomas Grafe1ef4bf2006-08-04 03:39:22 -0700294 .owner = THIS_MODULE,
295};
296
Denis V. Luneve4e49712008-01-10 03:27:51 -0800297static int fib_default_rules_init(struct fib_rules_ops *ops)
Denis V. Lunev2994c632007-11-10 22:12:03 -0800298{
299 int err;
300
Patrick McHardy5adef182009-12-03 01:25:57 +0000301 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800302 if (err < 0)
303 return err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800304 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800305 if (err < 0)
306 return err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800307 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
Denis V. Lunev2994c632007-11-10 22:12:03 -0800308 if (err < 0)
309 return err;
310 return 0;
311}
312
Denis V. Lunev7b1a74f2008-01-10 03:22:17 -0800313int __net_init fib4_rules_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800315 int err;
Denis V. Luneve4e49712008-01-10 03:27:51 -0800316 struct fib_rules_ops *ops;
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800317
Eric W. Biedermane9c51582009-12-03 12:22:55 -0800318 ops = fib_rules_register(&fib4_rules_ops_template, net);
319 if (IS_ERR(ops))
320 return PTR_ERR(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;
David S. Millerf4530fa2012-07-05 22:13:13 -0700326 net->ipv4.fib_has_custom_rules = false;
Denis V. Lunevdbb50162008-01-10 03:21:49 -0800327 return 0;
328
329fail:
330 /* also cleans all rules already added */
Denis V. Lunev9e3a5482008-01-20 16:46:41 -0800331 fib_rules_unregister(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. Lunev7b1a74f2008-01-10 03:22:17 -0800338}