xref: /openbmc/linux/net/ipv4/fib_rules.c (revision efe4a1ac)
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 static bool fib4_rule_matchall(const struct fib_rule *rule)
51 {
52 	struct fib4_rule *r = container_of(rule, struct fib4_rule, common);
53 
54 	if (r->dst_len || r->src_len || r->tos)
55 		return false;
56 	return fib_rule_matchall(rule);
57 }
58 
59 bool fib4_rule_default(const struct fib_rule *rule)
60 {
61 	if (!fib4_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
62 	    rule->l3mdev)
63 		return false;
64 	if (rule->table != RT_TABLE_LOCAL && rule->table != RT_TABLE_MAIN &&
65 	    rule->table != RT_TABLE_DEFAULT)
66 		return false;
67 	return true;
68 }
69 EXPORT_SYMBOL_GPL(fib4_rule_default);
70 
71 int __fib_lookup(struct net *net, struct flowi4 *flp,
72 		 struct fib_result *res, unsigned int flags)
73 {
74 	struct fib_lookup_arg arg = {
75 		.result = res,
76 		.flags = flags,
77 	};
78 	int err;
79 
80 	/* update flow if oif or iif point to device enslaved to l3mdev */
81 	l3mdev_update_flow(net, flowi4_to_flowi(flp));
82 
83 	err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
84 #ifdef CONFIG_IP_ROUTE_CLASSID
85 	if (arg.rule)
86 		res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
87 	else
88 		res->tclassid = 0;
89 #endif
90 
91 	if (err == -ESRCH)
92 		err = -ENETUNREACH;
93 
94 	return err;
95 }
96 EXPORT_SYMBOL_GPL(__fib_lookup);
97 
98 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
99 			    int flags, struct fib_lookup_arg *arg)
100 {
101 	int err = -EAGAIN;
102 	struct fib_table *tbl;
103 	u32 tb_id;
104 
105 	switch (rule->action) {
106 	case FR_ACT_TO_TBL:
107 		break;
108 
109 	case FR_ACT_UNREACHABLE:
110 		return -ENETUNREACH;
111 
112 	case FR_ACT_PROHIBIT:
113 		return -EACCES;
114 
115 	case FR_ACT_BLACKHOLE:
116 	default:
117 		return -EINVAL;
118 	}
119 
120 	rcu_read_lock();
121 
122 	tb_id = fib_rule_get_table(rule, arg);
123 	tbl = fib_get_table(rule->fr_net, tb_id);
124 	if (tbl)
125 		err = fib_table_lookup(tbl, &flp->u.ip4,
126 				       (struct fib_result *)arg->result,
127 				       arg->flags);
128 
129 	rcu_read_unlock();
130 	return err;
131 }
132 
133 static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
134 {
135 	struct fib_result *result = (struct fib_result *) arg->result;
136 	struct net_device *dev = NULL;
137 
138 	if (result->fi)
139 		dev = result->fi->fib_dev;
140 
141 	/* do not accept result if the route does
142 	 * not meet the required prefix length
143 	 */
144 	if (result->prefixlen <= rule->suppress_prefixlen)
145 		goto suppress_route;
146 
147 	/* do not accept result if the route uses a device
148 	 * belonging to a forbidden interface group
149 	 */
150 	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
151 		goto suppress_route;
152 
153 	return false;
154 
155 suppress_route:
156 	if (!(arg->flags & FIB_LOOKUP_NOREF))
157 		fib_info_put(result->fi);
158 	return true;
159 }
160 
161 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
162 {
163 	struct fib4_rule *r = (struct fib4_rule *) rule;
164 	struct flowi4 *fl4 = &fl->u.ip4;
165 	__be32 daddr = fl4->daddr;
166 	__be32 saddr = fl4->saddr;
167 
168 	if (((saddr ^ r->src) & r->srcmask) ||
169 	    ((daddr ^ r->dst) & r->dstmask))
170 		return 0;
171 
172 	if (r->tos && (r->tos != fl4->flowi4_tos))
173 		return 0;
174 
175 	return 1;
176 }
177 
178 static struct fib_table *fib_empty_table(struct net *net)
179 {
180 	u32 id;
181 
182 	for (id = 1; id <= RT_TABLE_MAX; id++)
183 		if (!fib_get_table(net, id))
184 			return fib_new_table(net, id);
185 	return NULL;
186 }
187 
188 static int call_fib_rule_notifier(struct notifier_block *nb, struct net *net,
189 				  enum fib_event_type event_type,
190 				  struct fib_rule *rule)
191 {
192 	struct fib_rule_notifier_info info = {
193 		.rule = rule,
194 	};
195 
196 	return call_fib_notifier(nb, net, event_type, &info.info);
197 }
198 
199 static int call_fib_rule_notifiers(struct net *net,
200 				   enum fib_event_type event_type,
201 				   struct fib_rule *rule)
202 {
203 	struct fib_rule_notifier_info info = {
204 		.rule = rule,
205 	};
206 
207 	return call_fib_notifiers(net, event_type, &info.info);
208 }
209 
210 /* Called with rcu_read_lock() */
211 void fib_rules_notify(struct net *net, struct notifier_block *nb)
212 {
213 	struct fib_rules_ops *ops = net->ipv4.rules_ops;
214 	struct fib_rule *rule;
215 
216 	list_for_each_entry_rcu(rule, &ops->rules_list, list)
217 		call_fib_rule_notifier(nb, net, FIB_EVENT_RULE_ADD, rule);
218 }
219 
220 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
221 	FRA_GENERIC_POLICY,
222 	[FRA_FLOW]	= { .type = NLA_U32 },
223 };
224 
225 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
226 			       struct fib_rule_hdr *frh,
227 			       struct nlattr **tb)
228 {
229 	struct net *net = sock_net(skb->sk);
230 	int err = -EINVAL;
231 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
232 
233 	if (frh->tos & ~IPTOS_TOS_MASK)
234 		goto errout;
235 
236 	/* split local/main if they are not already split */
237 	err = fib_unmerge(net);
238 	if (err)
239 		goto errout;
240 
241 	if (rule->table == RT_TABLE_UNSPEC && !rule->l3mdev) {
242 		if (rule->action == FR_ACT_TO_TBL) {
243 			struct fib_table *table;
244 
245 			table = fib_empty_table(net);
246 			if (!table) {
247 				err = -ENOBUFS;
248 				goto errout;
249 			}
250 
251 			rule->table = table->tb_id;
252 		}
253 	}
254 
255 	if (frh->src_len)
256 		rule4->src = nla_get_in_addr(tb[FRA_SRC]);
257 
258 	if (frh->dst_len)
259 		rule4->dst = nla_get_in_addr(tb[FRA_DST]);
260 
261 #ifdef CONFIG_IP_ROUTE_CLASSID
262 	if (tb[FRA_FLOW]) {
263 		rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
264 		if (rule4->tclassid)
265 			net->ipv4.fib_num_tclassid_users++;
266 	}
267 #endif
268 
269 	rule4->src_len = frh->src_len;
270 	rule4->srcmask = inet_make_mask(rule4->src_len);
271 	rule4->dst_len = frh->dst_len;
272 	rule4->dstmask = inet_make_mask(rule4->dst_len);
273 	rule4->tos = frh->tos;
274 
275 	net->ipv4.fib_has_custom_rules = true;
276 	call_fib_rule_notifiers(net, FIB_EVENT_RULE_ADD, rule);
277 
278 	err = 0;
279 errout:
280 	return err;
281 }
282 
283 static int fib4_rule_delete(struct fib_rule *rule)
284 {
285 	struct net *net = rule->fr_net;
286 	int err;
287 
288 	/* split local/main if they are not already split */
289 	err = fib_unmerge(net);
290 	if (err)
291 		goto errout;
292 
293 #ifdef CONFIG_IP_ROUTE_CLASSID
294 	if (((struct fib4_rule *)rule)->tclassid)
295 		net->ipv4.fib_num_tclassid_users--;
296 #endif
297 	net->ipv4.fib_has_custom_rules = true;
298 	call_fib_rule_notifiers(net, FIB_EVENT_RULE_DEL, rule);
299 errout:
300 	return err;
301 }
302 
303 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
304 			     struct nlattr **tb)
305 {
306 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
307 
308 	if (frh->src_len && (rule4->src_len != frh->src_len))
309 		return 0;
310 
311 	if (frh->dst_len && (rule4->dst_len != frh->dst_len))
312 		return 0;
313 
314 	if (frh->tos && (rule4->tos != frh->tos))
315 		return 0;
316 
317 #ifdef CONFIG_IP_ROUTE_CLASSID
318 	if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
319 		return 0;
320 #endif
321 
322 	if (frh->src_len && (rule4->src != nla_get_in_addr(tb[FRA_SRC])))
323 		return 0;
324 
325 	if (frh->dst_len && (rule4->dst != nla_get_in_addr(tb[FRA_DST])))
326 		return 0;
327 
328 	return 1;
329 }
330 
331 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
332 			  struct fib_rule_hdr *frh)
333 {
334 	struct fib4_rule *rule4 = (struct fib4_rule *) rule;
335 
336 	frh->dst_len = rule4->dst_len;
337 	frh->src_len = rule4->src_len;
338 	frh->tos = rule4->tos;
339 
340 	if ((rule4->dst_len &&
341 	     nla_put_in_addr(skb, FRA_DST, rule4->dst)) ||
342 	    (rule4->src_len &&
343 	     nla_put_in_addr(skb, FRA_SRC, rule4->src)))
344 		goto nla_put_failure;
345 #ifdef CONFIG_IP_ROUTE_CLASSID
346 	if (rule4->tclassid &&
347 	    nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
348 		goto nla_put_failure;
349 #endif
350 	return 0;
351 
352 nla_put_failure:
353 	return -ENOBUFS;
354 }
355 
356 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
357 {
358 	return nla_total_size(4) /* dst */
359 	       + nla_total_size(4) /* src */
360 	       + nla_total_size(4); /* flow */
361 }
362 
363 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
364 {
365 	rt_cache_flush(ops->fro_net);
366 }
367 
368 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
369 	.family		= AF_INET,
370 	.rule_size	= sizeof(struct fib4_rule),
371 	.addr_size	= sizeof(u32),
372 	.action		= fib4_rule_action,
373 	.suppress	= fib4_rule_suppress,
374 	.match		= fib4_rule_match,
375 	.configure	= fib4_rule_configure,
376 	.delete		= fib4_rule_delete,
377 	.compare	= fib4_rule_compare,
378 	.fill		= fib4_rule_fill,
379 	.nlmsg_payload	= fib4_rule_nlmsg_payload,
380 	.flush_cache	= fib4_rule_flush_cache,
381 	.nlgroup	= RTNLGRP_IPV4_RULE,
382 	.policy		= fib4_rule_policy,
383 	.owner		= THIS_MODULE,
384 };
385 
386 static int fib_default_rules_init(struct fib_rules_ops *ops)
387 {
388 	int err;
389 
390 	err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
391 	if (err < 0)
392 		return err;
393 	err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
394 	if (err < 0)
395 		return err;
396 	err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
397 	if (err < 0)
398 		return err;
399 	return 0;
400 }
401 
402 int __net_init fib4_rules_init(struct net *net)
403 {
404 	int err;
405 	struct fib_rules_ops *ops;
406 
407 	ops = fib_rules_register(&fib4_rules_ops_template, net);
408 	if (IS_ERR(ops))
409 		return PTR_ERR(ops);
410 
411 	err = fib_default_rules_init(ops);
412 	if (err < 0)
413 		goto fail;
414 	net->ipv4.rules_ops = ops;
415 	net->ipv4.fib_has_custom_rules = false;
416 	return 0;
417 
418 fail:
419 	/* also cleans all rules already added */
420 	fib_rules_unregister(ops);
421 	return err;
422 }
423 
424 void __net_exit fib4_rules_exit(struct net *net)
425 {
426 	fib_rules_unregister(net->ipv4.rules_ops);
427 }
428