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