xref: /linux/net/ipv4/fib_rules.c (revision 5e8c0fb6a95728b852d56c0a9244425d474670c0)
1 /*
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  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *		Thomas Graf <tgraf@suug.ch>
10  *
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 
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/inetdevice.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/rcupdate.h>
29 #include <linux/export.h>
30 #include <net/ip.h>
31 #include <net/route.h>
32 #include <net/tcp.h>
33 #include <net/ip_fib.h>
34 #include <net/fib_rules.h>
35 
36 struct fib4_rule {
37 	struct fib_rule		common;
38 	u8			dst_len;
39 	u8			src_len;
40 	u8			tos;
41 	__be32			src;
42 	__be32			srcmask;
43 	__be32			dst;
44 	__be32			dstmask;
45 #ifdef CONFIG_IP_ROUTE_CLASSID
46 	u32			tclassid;
47 #endif
48 };
49 
50 int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
51 {
52 	struct fib_lookup_arg arg = {
53 		.result = res,
54 		.flags = FIB_LOOKUP_NOREF,
55 	};
56 	int err;
57 
58 	err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
59 #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
65 
66 	if (err == -ESRCH)
67 		err = -ENETUNREACH;
68 
69 	return err;
70 }
71 EXPORT_SYMBOL_GPL(__fib_lookup);
72 
73 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
74 			    int flags, struct fib_lookup_arg *arg)
75 {
76 	int err = -EAGAIN;
77 	struct fib_table *tbl;
78 
79 	switch (rule->action) {
80 	case FR_ACT_TO_TBL:
81 		break;
82 
83 	case FR_ACT_UNREACHABLE:
84 		err = -ENETUNREACH;
85 		goto errout;
86 
87 	case FR_ACT_PROHIBIT:
88 		err = -EACCES;
89 		goto errout;
90 
91 	case FR_ACT_BLACKHOLE:
92 	default:
93 		err = -EINVAL;
94 		goto errout;
95 	}
96 
97 	tbl = fib_get_table(rule->fr_net, rule->table);
98 	if (!tbl)
99 		goto errout;
100 
101 	err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
102 	if (err > 0)
103 		err = -EAGAIN;
104 errout:
105 	return err;
106 }
107 
108 static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
109 {
110 	struct fib_result *result = (struct fib_result *) arg->result;
111 	struct net_device *dev = NULL;
112 
113 	if (result->fi)
114 		dev = result->fi->fib_dev;
115 
116 	/* do not accept result if the route does
117 	 * not meet the required prefix length
118 	 */
119 	if (result->prefixlen <= rule->suppress_prefixlen)
120 		goto suppress_route;
121 
122 	/* do not accept result if the route uses a device
123 	 * belonging to a forbidden interface group
124 	 */
125 	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
126 		goto suppress_route;
127 
128 	return false;
129 
130 suppress_route:
131 	if (!(arg->flags & FIB_LOOKUP_NOREF))
132 		fib_info_put(result->fi);
133 	return true;
134 }
135 
136 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
137 {
138 	struct fib4_rule *r = (struct fib4_rule *) rule;
139 	struct flowi4 *fl4 = &fl->u.ip4;
140 	__be32 daddr = fl4->daddr;
141 	__be32 saddr = fl4->saddr;
142 
143 	if (((saddr ^ r->src) & r->srcmask) ||
144 	    ((daddr ^ r->dst) & r->dstmask))
145 		return 0;
146 
147 	if (r->tos && (r->tos != fl4->flowi4_tos))
148 		return 0;
149 
150 	return 1;
151 }
152 
153 static struct fib_table *fib_empty_table(struct net *net)
154 {
155 	u32 id;
156 
157 	for (id = 1; id <= RT_TABLE_MAX; id++)
158 		if (fib_get_table(net, id) == NULL)
159 			return fib_new_table(net, id);
160 	return NULL;
161 }
162 
163 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
164 	FRA_GENERIC_POLICY,
165 	[FRA_FLOW]	= { .type = NLA_U32 },
166 };
167 
168 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
169 			       struct fib_rule_hdr *frh,
170 			       struct nlattr **tb)
171 {
172 	struct net *net = sock_net(skb->sk);
173 	int err = -EINVAL;
174 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
175 
176 	if (frh->tos & ~IPTOS_TOS_MASK)
177 		goto errout;
178 
179 	if (rule->table == RT_TABLE_UNSPEC) {
180 		if (rule->action == FR_ACT_TO_TBL) {
181 			struct fib_table *table;
182 
183 			table = fib_empty_table(net);
184 			if (table == NULL) {
185 				err = -ENOBUFS;
186 				goto errout;
187 			}
188 
189 			rule->table = table->tb_id;
190 		}
191 	}
192 
193 	if (frh->src_len)
194 		rule4->src = nla_get_be32(tb[FRA_SRC]);
195 
196 	if (frh->dst_len)
197 		rule4->dst = nla_get_be32(tb[FRA_DST]);
198 
199 #ifdef CONFIG_IP_ROUTE_CLASSID
200 	if (tb[FRA_FLOW]) {
201 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
202 		if (rule4->tclassid)
203 			net->ipv4.fib_num_tclassid_users++;
204 	}
205 #endif
206 
207 	rule4->src_len = frh->src_len;
208 	rule4->srcmask = inet_make_mask(rule4->src_len);
209 	rule4->dst_len = frh->dst_len;
210 	rule4->dstmask = inet_make_mask(rule4->dst_len);
211 	rule4->tos = frh->tos;
212 
213 	net->ipv4.fib_has_custom_rules = true;
214 	err = 0;
215 errout:
216 	return err;
217 }
218 
219 static void fib4_rule_delete(struct fib_rule *rule)
220 {
221 	struct net *net = rule->fr_net;
222 #ifdef CONFIG_IP_ROUTE_CLASSID
223 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
224 
225 	if (rule4->tclassid)
226 		net->ipv4.fib_num_tclassid_users--;
227 #endif
228 	net->ipv4.fib_has_custom_rules = true;
229 }
230 
231 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
232 			     struct nlattr **tb)
233 {
234 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
235 
236 	if (frh->src_len && (rule4->src_len != frh->src_len))
237 		return 0;
238 
239 	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
240 		return 0;
241 
242 	if (frh->tos && (rule4->tos != frh->tos))
243 		return 0;
244 
245 #ifdef CONFIG_IP_ROUTE_CLASSID
246 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
247 		return 0;
248 #endif
249 
250 	if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
251 		return 0;
252 
253 	if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
254 		return 0;
255 
256 	return 1;
257 }
258 
259 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
260 			  struct fib_rule_hdr *frh)
261 {
262 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
263 
264 	frh->dst_len = rule4->dst_len;
265 	frh->src_len = rule4->src_len;
266 	frh->tos = rule4->tos;
267 
268 	if ((rule4->dst_len &&
269 	     nla_put_be32(skb, FRA_DST, rule4->dst)) ||
270 	    (rule4->src_len &&
271 	     nla_put_be32(skb, FRA_SRC, rule4->src)))
272 		goto nla_put_failure;
273 #ifdef CONFIG_IP_ROUTE_CLASSID
274 	if (rule4->tclassid &&
275 	    nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
276 		goto nla_put_failure;
277 #endif
278 	return 0;
279 
280 nla_put_failure:
281 	return -ENOBUFS;
282 }
283 
284 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
285 {
286 	return nla_total_size(4) /* dst */
287 	       + nla_total_size(4) /* src */
288 	       + nla_total_size(4); /* flow */
289 }
290 
291 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
292 {
293 	rt_cache_flush(ops->fro_net);
294 }
295 
296 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
297 	.family		= AF_INET,
298 	.rule_size	= sizeof(struct fib4_rule),
299 	.addr_size	= sizeof(u32),
300 	.action		= fib4_rule_action,
301 	.suppress	= fib4_rule_suppress,
302 	.match		= fib4_rule_match,
303 	.configure	= fib4_rule_configure,
304 	.delete		= fib4_rule_delete,
305 	.compare	= fib4_rule_compare,
306 	.fill		= fib4_rule_fill,
307 	.default_pref	= fib_default_rule_pref,
308 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
309 	.flush_cache	= fib4_rule_flush_cache,
310 	.nlgroup	= RTNLGRP_IPV4_RULE,
311 	.policy		= fib4_rule_policy,
312 	.owner		= THIS_MODULE,
313 };
314 
315 static int fib_default_rules_init(struct fib_rules_ops *ops)
316 {
317 	int err;
318 
319 	err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
320 	if (err < 0)
321 		return err;
322 	err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
323 	if (err < 0)
324 		return err;
325 	err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
326 	if (err < 0)
327 		return err;
328 	return 0;
329 }
330 
331 int __net_init fib4_rules_init(struct net *net)
332 {
333 	int err;
334 	struct fib_rules_ops *ops;
335 
336 	ops = fib_rules_register(&fib4_rules_ops_template, net);
337 	if (IS_ERR(ops))
338 		return PTR_ERR(ops);
339 
340 	err = fib_default_rules_init(ops);
341 	if (err < 0)
342 		goto fail;
343 	net->ipv4.rules_ops = ops;
344 	net->ipv4.fib_has_custom_rules = false;
345 	return 0;
346 
347 fail:
348 	/* also cleans all rules already added */
349 	fib_rules_unregister(ops);
350 	return err;
351 }
352 
353 void __net_exit fib4_rules_exit(struct net *net)
354 {
355 	fib_rules_unregister(net->ipv4.rules_ops);
356 }
357