xref: /openbmc/linux/net/ipv6/fib6_rules.c (revision 95298d63)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
4  *
5  * Copyright (C)2003-2006 Helsinki University of Technology
6  * Copyright (C)2003-2006 USAGI/WIDE Project
7  *
8  * Authors
9  *	Thomas Graf		<tgraf@suug.ch>
10  *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
11  */
12 
13 #include <linux/netdevice.h>
14 #include <linux/notifier.h>
15 #include <linux/export.h>
16 
17 #include <net/fib_rules.h>
18 #include <net/ipv6.h>
19 #include <net/addrconf.h>
20 #include <net/ip6_route.h>
21 #include <net/netlink.h>
22 
23 struct fib6_rule {
24 	struct fib_rule		common;
25 	struct rt6key		src;
26 	struct rt6key		dst;
27 	u8			tclass;
28 };
29 
30 static bool fib6_rule_matchall(const struct fib_rule *rule)
31 {
32 	struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
33 
34 	if (r->dst.plen || r->src.plen || r->tclass)
35 		return false;
36 	return fib_rule_matchall(rule);
37 }
38 
39 bool fib6_rule_default(const struct fib_rule *rule)
40 {
41 	if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
42 	    rule->l3mdev)
43 		return false;
44 	if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
45 		return false;
46 	return true;
47 }
48 EXPORT_SYMBOL_GPL(fib6_rule_default);
49 
50 int fib6_rules_dump(struct net *net, struct notifier_block *nb,
51 		    struct netlink_ext_ack *extack)
52 {
53 	return fib_rules_dump(net, nb, AF_INET6, extack);
54 }
55 
56 unsigned int fib6_rules_seq_read(struct net *net)
57 {
58 	return fib_rules_seq_read(net, AF_INET6);
59 }
60 
61 /* called with rcu lock held; no reference taken on fib6_info */
62 int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
63 		struct fib6_result *res, int flags)
64 {
65 	int err;
66 
67 	if (net->ipv6.fib6_has_custom_rules) {
68 		struct fib_lookup_arg arg = {
69 			.lookup_ptr = fib6_table_lookup,
70 			.lookup_data = &oif,
71 			.result = res,
72 			.flags = FIB_LOOKUP_NOREF,
73 		};
74 
75 		l3mdev_update_flow(net, flowi6_to_flowi(fl6));
76 
77 		err = fib_rules_lookup(net->ipv6.fib6_rules_ops,
78 				       flowi6_to_flowi(fl6), flags, &arg);
79 	} else {
80 		err = fib6_table_lookup(net, net->ipv6.fib6_local_tbl, oif,
81 					fl6, res, flags);
82 		if (err || res->f6i == net->ipv6.fib6_null_entry)
83 			err = fib6_table_lookup(net, net->ipv6.fib6_main_tbl,
84 						oif, fl6, res, flags);
85 	}
86 
87 	return err;
88 }
89 
90 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
91 				   const struct sk_buff *skb,
92 				   int flags, pol_lookup_t lookup)
93 {
94 	if (net->ipv6.fib6_has_custom_rules) {
95 		struct fib6_result res = {};
96 		struct fib_lookup_arg arg = {
97 			.lookup_ptr = lookup,
98 			.lookup_data = skb,
99 			.result = &res,
100 			.flags = FIB_LOOKUP_NOREF,
101 		};
102 
103 		/* update flow if oif or iif point to device enslaved to l3mdev */
104 		l3mdev_update_flow(net, flowi6_to_flowi(fl6));
105 
106 		fib_rules_lookup(net->ipv6.fib6_rules_ops,
107 				 flowi6_to_flowi(fl6), flags, &arg);
108 
109 		if (res.rt6)
110 			return &res.rt6->dst;
111 	} else {
112 		struct rt6_info *rt;
113 
114 		rt = pol_lookup_func(lookup,
115 			     net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
116 		if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
117 			return &rt->dst;
118 		ip6_rt_put_flags(rt, flags);
119 		rt = pol_lookup_func(lookup,
120 			     net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
121 		if (rt->dst.error != -EAGAIN)
122 			return &rt->dst;
123 		ip6_rt_put_flags(rt, flags);
124 	}
125 
126 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
127 		dst_hold(&net->ipv6.ip6_null_entry->dst);
128 	return &net->ipv6.ip6_null_entry->dst;
129 }
130 
131 static int fib6_rule_saddr(struct net *net, struct fib_rule *rule, int flags,
132 			   struct flowi6 *flp6, const struct net_device *dev)
133 {
134 	struct fib6_rule *r = (struct fib6_rule *)rule;
135 
136 	/* If we need to find a source address for this traffic,
137 	 * we check the result if it meets requirement of the rule.
138 	 */
139 	if ((rule->flags & FIB_RULE_FIND_SADDR) &&
140 	    r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
141 		struct in6_addr saddr;
142 
143 		if (ipv6_dev_get_saddr(net, dev, &flp6->daddr,
144 				       rt6_flags2srcprefs(flags), &saddr))
145 			return -EAGAIN;
146 
147 		if (!ipv6_prefix_equal(&saddr, &r->src.addr, r->src.plen))
148 			return -EAGAIN;
149 
150 		flp6->saddr = saddr;
151 	}
152 
153 	return 0;
154 }
155 
156 static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp,
157 				int flags, struct fib_lookup_arg *arg)
158 {
159 	struct fib6_result *res = arg->result;
160 	struct flowi6 *flp6 = &flp->u.ip6;
161 	struct net *net = rule->fr_net;
162 	struct fib6_table *table;
163 	int err, *oif;
164 	u32 tb_id;
165 
166 	switch (rule->action) {
167 	case FR_ACT_TO_TBL:
168 		break;
169 	case FR_ACT_UNREACHABLE:
170 		return -ENETUNREACH;
171 	case FR_ACT_PROHIBIT:
172 		return -EACCES;
173 	case FR_ACT_BLACKHOLE:
174 	default:
175 		return -EINVAL;
176 	}
177 
178 	tb_id = fib_rule_get_table(rule, arg);
179 	table = fib6_get_table(net, tb_id);
180 	if (!table)
181 		return -EAGAIN;
182 
183 	oif = (int *)arg->lookup_data;
184 	err = fib6_table_lookup(net, table, *oif, flp6, res, flags);
185 	if (!err && res->f6i != net->ipv6.fib6_null_entry)
186 		err = fib6_rule_saddr(net, rule, flags, flp6,
187 				      res->nh->fib_nh_dev);
188 	else
189 		err = -EAGAIN;
190 
191 	return err;
192 }
193 
194 static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
195 			      int flags, struct fib_lookup_arg *arg)
196 {
197 	struct fib6_result *res = arg->result;
198 	struct flowi6 *flp6 = &flp->u.ip6;
199 	struct rt6_info *rt = NULL;
200 	struct fib6_table *table;
201 	struct net *net = rule->fr_net;
202 	pol_lookup_t lookup = arg->lookup_ptr;
203 	int err = 0;
204 	u32 tb_id;
205 
206 	switch (rule->action) {
207 	case FR_ACT_TO_TBL:
208 		break;
209 	case FR_ACT_UNREACHABLE:
210 		err = -ENETUNREACH;
211 		rt = net->ipv6.ip6_null_entry;
212 		goto discard_pkt;
213 	default:
214 	case FR_ACT_BLACKHOLE:
215 		err = -EINVAL;
216 		rt = net->ipv6.ip6_blk_hole_entry;
217 		goto discard_pkt;
218 	case FR_ACT_PROHIBIT:
219 		err = -EACCES;
220 		rt = net->ipv6.ip6_prohibit_entry;
221 		goto discard_pkt;
222 	}
223 
224 	tb_id = fib_rule_get_table(rule, arg);
225 	table = fib6_get_table(net, tb_id);
226 	if (!table) {
227 		err = -EAGAIN;
228 		goto out;
229 	}
230 
231 	rt = pol_lookup_func(lookup,
232 			     net, table, flp6, arg->lookup_data, flags);
233 	if (rt != net->ipv6.ip6_null_entry) {
234 		err = fib6_rule_saddr(net, rule, flags, flp6,
235 				      ip6_dst_idev(&rt->dst)->dev);
236 
237 		if (err == -EAGAIN)
238 			goto again;
239 
240 		err = rt->dst.error;
241 		if (err != -EAGAIN)
242 			goto out;
243 	}
244 again:
245 	ip6_rt_put_flags(rt, flags);
246 	err = -EAGAIN;
247 	rt = NULL;
248 	goto out;
249 
250 discard_pkt:
251 	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
252 		dst_hold(&rt->dst);
253 out:
254 	res->rt6 = rt;
255 	return err;
256 }
257 
258 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
259 			    int flags, struct fib_lookup_arg *arg)
260 {
261 	if (arg->lookup_ptr == fib6_table_lookup)
262 		return fib6_rule_action_alt(rule, flp, flags, arg);
263 
264 	return __fib6_rule_action(rule, flp, flags, arg);
265 }
266 
267 static bool fib6_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
268 {
269 	struct fib6_result *res = arg->result;
270 	struct rt6_info *rt = res->rt6;
271 	struct net_device *dev = NULL;
272 
273 	if (!rt)
274 		return false;
275 
276 	if (rt->rt6i_idev)
277 		dev = rt->rt6i_idev->dev;
278 
279 	/* do not accept result if the route does
280 	 * not meet the required prefix length
281 	 */
282 	if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
283 		goto suppress_route;
284 
285 	/* do not accept result if the route uses a device
286 	 * belonging to a forbidden interface group
287 	 */
288 	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
289 		goto suppress_route;
290 
291 	return false;
292 
293 suppress_route:
294 	if (!(arg->flags & FIB_LOOKUP_NOREF))
295 		ip6_rt_put(rt);
296 	return true;
297 }
298 
299 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
300 {
301 	struct fib6_rule *r = (struct fib6_rule *) rule;
302 	struct flowi6 *fl6 = &fl->u.ip6;
303 
304 	if (r->dst.plen &&
305 	    !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
306 		return 0;
307 
308 	/*
309 	 * If FIB_RULE_FIND_SADDR is set and we do not have a
310 	 * source address for the traffic, we defer check for
311 	 * source address.
312 	 */
313 	if (r->src.plen) {
314 		if (flags & RT6_LOOKUP_F_HAS_SADDR) {
315 			if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
316 					       r->src.plen))
317 				return 0;
318 		} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
319 			return 0;
320 	}
321 
322 	if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
323 		return 0;
324 
325 	if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto))
326 		return 0;
327 
328 	if (fib_rule_port_range_set(&rule->sport_range) &&
329 	    !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport))
330 		return 0;
331 
332 	if (fib_rule_port_range_set(&rule->dport_range) &&
333 	    !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport))
334 		return 0;
335 
336 	return 1;
337 }
338 
339 static const struct nla_policy fib6_rule_policy[FRA_MAX+1] = {
340 	FRA_GENERIC_POLICY,
341 };
342 
343 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
344 			       struct fib_rule_hdr *frh,
345 			       struct nlattr **tb,
346 			       struct netlink_ext_ack *extack)
347 {
348 	int err = -EINVAL;
349 	struct net *net = sock_net(skb->sk);
350 	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
351 
352 	if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
353 		if (rule->table == RT6_TABLE_UNSPEC) {
354 			NL_SET_ERR_MSG(extack, "Invalid table");
355 			goto errout;
356 		}
357 
358 		if (fib6_new_table(net, rule->table) == NULL) {
359 			err = -ENOBUFS;
360 			goto errout;
361 		}
362 	}
363 
364 	if (frh->src_len)
365 		rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
366 
367 	if (frh->dst_len)
368 		rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
369 
370 	rule6->src.plen = frh->src_len;
371 	rule6->dst.plen = frh->dst_len;
372 	rule6->tclass = frh->tos;
373 
374 	if (fib_rule_requires_fldissect(rule))
375 		net->ipv6.fib6_rules_require_fldissect++;
376 
377 	net->ipv6.fib6_has_custom_rules = true;
378 	err = 0;
379 errout:
380 	return err;
381 }
382 
383 static int fib6_rule_delete(struct fib_rule *rule)
384 {
385 	struct net *net = rule->fr_net;
386 
387 	if (net->ipv6.fib6_rules_require_fldissect &&
388 	    fib_rule_requires_fldissect(rule))
389 		net->ipv6.fib6_rules_require_fldissect--;
390 
391 	return 0;
392 }
393 
394 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
395 			     struct nlattr **tb)
396 {
397 	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
398 
399 	if (frh->src_len && (rule6->src.plen != frh->src_len))
400 		return 0;
401 
402 	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
403 		return 0;
404 
405 	if (frh->tos && (rule6->tclass != frh->tos))
406 		return 0;
407 
408 	if (frh->src_len &&
409 	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
410 		return 0;
411 
412 	if (frh->dst_len &&
413 	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
414 		return 0;
415 
416 	return 1;
417 }
418 
419 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
420 			  struct fib_rule_hdr *frh)
421 {
422 	struct fib6_rule *rule6 = (struct fib6_rule *) rule;
423 
424 	frh->dst_len = rule6->dst.plen;
425 	frh->src_len = rule6->src.plen;
426 	frh->tos = rule6->tclass;
427 
428 	if ((rule6->dst.plen &&
429 	     nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
430 	    (rule6->src.plen &&
431 	     nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
432 		goto nla_put_failure;
433 	return 0;
434 
435 nla_put_failure:
436 	return -ENOBUFS;
437 }
438 
439 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
440 {
441 	return nla_total_size(16) /* dst */
442 	       + nla_total_size(16); /* src */
443 }
444 
445 static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
446 	.family			= AF_INET6,
447 	.rule_size		= sizeof(struct fib6_rule),
448 	.addr_size		= sizeof(struct in6_addr),
449 	.action			= fib6_rule_action,
450 	.match			= fib6_rule_match,
451 	.suppress		= fib6_rule_suppress,
452 	.configure		= fib6_rule_configure,
453 	.delete			= fib6_rule_delete,
454 	.compare		= fib6_rule_compare,
455 	.fill			= fib6_rule_fill,
456 	.nlmsg_payload		= fib6_rule_nlmsg_payload,
457 	.nlgroup		= RTNLGRP_IPV6_RULE,
458 	.policy			= fib6_rule_policy,
459 	.owner			= THIS_MODULE,
460 	.fro_net		= &init_net,
461 };
462 
463 static int __net_init fib6_rules_net_init(struct net *net)
464 {
465 	struct fib_rules_ops *ops;
466 	int err = -ENOMEM;
467 
468 	ops = fib_rules_register(&fib6_rules_ops_template, net);
469 	if (IS_ERR(ops))
470 		return PTR_ERR(ops);
471 
472 	err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
473 	if (err)
474 		goto out_fib6_rules_ops;
475 
476 	err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
477 	if (err)
478 		goto out_fib6_rules_ops;
479 
480 	net->ipv6.fib6_rules_ops = ops;
481 	net->ipv6.fib6_rules_require_fldissect = 0;
482 out:
483 	return err;
484 
485 out_fib6_rules_ops:
486 	fib_rules_unregister(ops);
487 	goto out;
488 }
489 
490 static void __net_exit fib6_rules_net_exit(struct net *net)
491 {
492 	rtnl_lock();
493 	fib_rules_unregister(net->ipv6.fib6_rules_ops);
494 	rtnl_unlock();
495 }
496 
497 static struct pernet_operations fib6_rules_net_ops = {
498 	.init = fib6_rules_net_init,
499 	.exit = fib6_rules_net_exit,
500 };
501 
502 int __init fib6_rules_init(void)
503 {
504 	return register_pernet_subsys(&fib6_rules_net_ops);
505 }
506 
507 
508 void fib6_rules_cleanup(void)
509 {
510 	unregister_pernet_subsys(&fib6_rules_net_ops);
511 }
512