fib6_rules.c 12.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
T
Thomas Graf 已提交
2 3 4 5 6 7 8 9 10 11 12 13
/*
 * net/ipv6/fib6_rules.c	IPv6 Routing Policy Rules
 *
 * Copyright (C)2003-2006 Helsinki University of Technology
 * Copyright (C)2003-2006 USAGI/WIDE Project
 *
 * Authors
 *	Thomas Graf		<tgraf@suug.ch>
 *	Ville Nuorvala		<vnuorval@tcs.hut.fi>
 */

#include <linux/netdevice.h>
14
#include <linux/notifier.h>
15
#include <linux/export.h>
16
#include <linux/indirect_call_wrapper.h>
T
Thomas Graf 已提交
17 18 19

#include <net/fib_rules.h>
#include <net/ipv6.h>
20
#include <net/addrconf.h>
T
Thomas Graf 已提交
21 22 23
#include <net/ip6_route.h>
#include <net/netlink.h>

24
struct fib6_rule {
T
Thomas Graf 已提交
25 26 27 28 29 30
	struct fib_rule		common;
	struct rt6key		src;
	struct rt6key		dst;
	u8			tclass;
};

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static bool fib6_rule_matchall(const struct fib_rule *rule)
{
	struct fib6_rule *r = container_of(rule, struct fib6_rule, common);

	if (r->dst.plen || r->src.plen || r->tclass)
		return false;
	return fib_rule_matchall(rule);
}

bool fib6_rule_default(const struct fib_rule *rule)
{
	if (!fib6_rule_matchall(rule) || rule->action != FR_ACT_TO_TBL ||
	    rule->l3mdev)
		return false;
	if (rule->table != RT6_TABLE_LOCAL && rule->table != RT6_TABLE_MAIN)
		return false;
	return true;
}
EXPORT_SYMBOL_GPL(fib6_rule_default);

51 52
int fib6_rules_dump(struct net *net, struct notifier_block *nb,
		    struct netlink_ext_ack *extack)
53
{
54
	return fib_rules_dump(net, nb, AF_INET6, extack);
55 56 57 58 59 60 61
}

unsigned int fib6_rules_seq_read(struct net *net)
{
	return fib_rules_seq_read(net, AF_INET6);
}

D
David Ahern 已提交
62
/* called with rcu lock held; no reference taken on fib6_info */
63 64
int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
		struct fib6_result *res, int flags)
D
David Ahern 已提交
65 66 67 68 69 70 71
{
	int err;

	if (net->ipv6.fib6_has_custom_rules) {
		struct fib_lookup_arg arg = {
			.lookup_ptr = fib6_table_lookup,
			.lookup_data = &oif,
72
			.result = res,
D
David Ahern 已提交
73 74 75 76 77 78 79 80
			.flags = FIB_LOOKUP_NOREF,
		};

		l3mdev_update_flow(net, flowi6_to_flowi(fl6));

		err = fib_rules_lookup(net->ipv6.fib6_rules_ops,
				       flowi6_to_flowi(fl6), flags, &arg);
	} else {
81 82 83 84 85
		err = fib6_table_lookup(net, net->ipv6.fib6_local_tbl, oif,
					fl6, res, flags);
		if (err || res->f6i == net->ipv6.fib6_null_entry)
			err = fib6_table_lookup(net, net->ipv6.fib6_main_tbl,
						oif, fl6, res, flags);
D
David Ahern 已提交
86 87
	}

88
	return err;
D
David Ahern 已提交
89 90
}

91
struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
D
David Ahern 已提交
92
				   const struct sk_buff *skb,
93
				   int flags, pol_lookup_t lookup)
T
Thomas Graf 已提交
94
{
95
	if (net->ipv6.fib6_has_custom_rules) {
96
		struct fib6_result res = {};
97 98
		struct fib_lookup_arg arg = {
			.lookup_ptr = lookup,
D
David Ahern 已提交
99
			.lookup_data = skb,
100
			.result = &res,
101 102 103 104 105 106 107 108 109
			.flags = FIB_LOOKUP_NOREF,
		};

		/* update flow if oif or iif point to device enslaved to l3mdev */
		l3mdev_update_flow(net, flowi6_to_flowi(fl6));

		fib_rules_lookup(net->ipv6.fib6_rules_ops,
				 flowi6_to_flowi(fl6), flags, &arg);

110 111
		if (res.rt6)
			return &res.rt6->dst;
112 113 114
	} else {
		struct rt6_info *rt;

115 116
		rt = pol_lookup_func(lookup,
			     net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
117 118
		if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
			return &rt->dst;
119
		ip6_rt_put_flags(rt, flags);
120 121
		rt = pol_lookup_func(lookup,
			     net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
122 123
		if (rt->dst.error != -EAGAIN)
			return &rt->dst;
124
		ip6_rt_put_flags(rt, flags);
125
	}
126

127 128
	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
		dst_hold(&net->ipv6.ip6_null_entry->dst);
129
	return &net->ipv6.ip6_null_entry->dst;
T
Thomas Graf 已提交
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static int fib6_rule_saddr(struct net *net, struct fib_rule *rule, int flags,
			   struct flowi6 *flp6, const struct net_device *dev)
{
	struct fib6_rule *r = (struct fib6_rule *)rule;

	/* If we need to find a source address for this traffic,
	 * we check the result if it meets requirement of the rule.
	 */
	if ((rule->flags & FIB_RULE_FIND_SADDR) &&
	    r->src.plen && !(flags & RT6_LOOKUP_F_HAS_SADDR)) {
		struct in6_addr saddr;

		if (ipv6_dev_get_saddr(net, dev, &flp6->daddr,
				       rt6_flags2srcprefs(flags), &saddr))
			return -EAGAIN;

		if (!ipv6_prefix_equal(&saddr, &r->src.addr, r->src.plen))
			return -EAGAIN;

		flp6->saddr = saddr;
	}

	return 0;
}

D
David Ahern 已提交
157 158 159
static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp,
				int flags, struct fib_lookup_arg *arg)
{
160
	struct fib6_result *res = arg->result;
D
David Ahern 已提交
161 162 163
	struct flowi6 *flp6 = &flp->u.ip6;
	struct net *net = rule->fr_net;
	struct fib6_table *table;
164
	int err, *oif;
D
David Ahern 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	u32 tb_id;

	switch (rule->action) {
	case FR_ACT_TO_TBL:
		break;
	case FR_ACT_UNREACHABLE:
		return -ENETUNREACH;
	case FR_ACT_PROHIBIT:
		return -EACCES;
	case FR_ACT_BLACKHOLE:
	default:
		return -EINVAL;
	}

	tb_id = fib_rule_get_table(rule, arg);
	table = fib6_get_table(net, tb_id);
	if (!table)
		return -EAGAIN;

	oif = (int *)arg->lookup_data;
185 186
	err = fib6_table_lookup(net, table, *oif, flp6, res, flags);
	if (!err && res->f6i != net->ipv6.fib6_null_entry)
D
David Ahern 已提交
187
		err = fib6_rule_saddr(net, rule, flags, flp6,
188
				      res->nh->fib_nh_dev);
189 190
	else
		err = -EAGAIN;
D
David Ahern 已提交
191 192 193 194 195 196

	return err;
}

static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
			      int flags, struct fib_lookup_arg *arg)
T
Thomas Graf 已提交
197
{
198
	struct fib6_result *res = arg->result;
199
	struct flowi6 *flp6 = &flp->u.ip6;
T
Thomas Graf 已提交
200 201
	struct rt6_info *rt = NULL;
	struct fib6_table *table;
202
	struct net *net = rule->fr_net;
T
Thomas Graf 已提交
203
	pol_lookup_t lookup = arg->lookup_ptr;
204
	int err = 0;
D
David Ahern 已提交
205
	u32 tb_id;
T
Thomas Graf 已提交
206 207 208 209 210

	switch (rule->action) {
	case FR_ACT_TO_TBL:
		break;
	case FR_ACT_UNREACHABLE:
211
		err = -ENETUNREACH;
212
		rt = net->ipv6.ip6_null_entry;
T
Thomas Graf 已提交
213 214 215
		goto discard_pkt;
	default:
	case FR_ACT_BLACKHOLE:
216
		err = -EINVAL;
217
		rt = net->ipv6.ip6_blk_hole_entry;
T
Thomas Graf 已提交
218 219
		goto discard_pkt;
	case FR_ACT_PROHIBIT:
220
		err = -EACCES;
221
		rt = net->ipv6.ip6_prohibit_entry;
T
Thomas Graf 已提交
222 223 224
		goto discard_pkt;
	}

D
David Ahern 已提交
225 226
	tb_id = fib_rule_get_table(rule, arg);
	table = fib6_get_table(net, tb_id);
227 228 229 230
	if (!table) {
		err = -EAGAIN;
		goto out;
	}
T
Thomas Graf 已提交
231

232 233
	rt = pol_lookup_func(lookup,
			     net, table, flp6, arg->lookup_data, flags);
234
	if (rt != net->ipv6.ip6_null_entry) {
235 236 237 238 239 240
		err = fib6_rule_saddr(net, rule, flags, flp6,
				      ip6_dst_idev(&rt->dst)->dev);

		if (err == -EAGAIN)
			goto again;

241
		err = rt->dst.error;
242 243
		if (err != -EAGAIN)
			goto out;
244 245
	}
again:
246
	ip6_rt_put_flags(rt, flags);
247
	err = -EAGAIN;
248 249 250
	rt = NULL;
	goto out;

T
Thomas Graf 已提交
251
discard_pkt:
252 253
	if (!(flags & RT6_LOOKUP_F_DST_NOREF))
		dst_hold(&rt->dst);
T
Thomas Graf 已提交
254
out:
255
	res->rt6 = rt;
256
	return err;
T
Thomas Graf 已提交
257 258
}

259 260 261
INDIRECT_CALLABLE_SCOPE int fib6_rule_action(struct fib_rule *rule,
					     struct flowi *flp, int flags,
					     struct fib_lookup_arg *arg)
D
David Ahern 已提交
262 263 264 265 266 267 268
{
	if (arg->lookup_ptr == fib6_table_lookup)
		return fib6_rule_action_alt(rule, flp, flags, arg);

	return __fib6_rule_action(rule, flp, flags, arg);
}

269
INDIRECT_CALLABLE_SCOPE bool fib6_rule_suppress(struct fib_rule *rule,
270
						int flags,
271
						struct fib_lookup_arg *arg)
272
{
273 274
	struct fib6_result *res = arg->result;
	struct rt6_info *rt = res->rt6;
275 276
	struct net_device *dev = NULL;

277 278 279
	if (!rt)
		return false;

280 281 282
	if (rt->rt6i_idev)
		dev = rt->rt6i_idev->dev;

283 284 285
	/* do not accept result if the route does
	 * not meet the required prefix length
	 */
286
	if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
287 288 289 290 291 292 293 294 295 296 297
		goto suppress_route;

	/* do not accept result if the route uses a device
	 * belonging to a forbidden interface group
	 */
	if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
		goto suppress_route;

	return false;

suppress_route:
298
	ip6_rt_put_flags(rt, flags);
S
Stefan Tomanek 已提交
299
	return true;
300
}
T
Thomas Graf 已提交
301

302 303
INDIRECT_CALLABLE_SCOPE int fib6_rule_match(struct fib_rule *rule,
					    struct flowi *fl, int flags)
T
Thomas Graf 已提交
304 305
{
	struct fib6_rule *r = (struct fib6_rule *) rule;
306
	struct flowi6 *fl6 = &fl->u.ip6;
T
Thomas Graf 已提交
307

308
	if (r->dst.plen &&
309
	    !ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
T
Thomas Graf 已提交
310 311
		return 0;

312 313 314 315 316
	/*
	 * If FIB_RULE_FIND_SADDR is set and we do not have a
	 * source address for the traffic, we defer check for
	 * source address.
	 */
317
	if (r->src.plen) {
318
		if (flags & RT6_LOOKUP_F_HAS_SADDR) {
319
			if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
320 321 322
					       r->src.plen))
				return 0;
		} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
323 324
			return 0;
	}
T
Thomas Graf 已提交
325

326
	if (r->tclass && r->tclass != ip6_tclass(fl6->flowlabel))
327 328
		return 0;

329 330 331 332 333 334 335 336 337 338 339
	if (rule->ip_proto && (rule->ip_proto != fl6->flowi6_proto))
		return 0;

	if (fib_rule_port_range_set(&rule->sport_range) &&
	    !fib_rule_port_inrange(&rule->sport_range, fl6->fl6_sport))
		return 0;

	if (fib_rule_port_range_set(&rule->dport_range) &&
	    !fib_rule_port_inrange(&rule->dport_range, fl6->fl6_dport))
		return 0;

T
Thomas Graf 已提交
340 341 342 343
	return 1;
}

static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
344
			       struct fib_rule_hdr *frh,
345 346
			       struct nlattr **tb,
			       struct netlink_ext_ack *extack)
T
Thomas Graf 已提交
347 348
{
	int err = -EINVAL;
349
	struct net *net = sock_net(skb->sk);
T
Thomas Graf 已提交
350 351
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

D
David Ahern 已提交
352
	if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
353 354
		if (rule->table == RT6_TABLE_UNSPEC) {
			NL_SET_ERR_MSG(extack, "Invalid table");
T
Thomas Graf 已提交
355
			goto errout;
356
		}
T
Thomas Graf 已提交
357

358
		if (fib6_new_table(net, rule->table) == NULL) {
T
Thomas Graf 已提交
359 360 361 362 363
			err = -ENOBUFS;
			goto errout;
		}
	}

364
	if (frh->src_len)
365
		rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
T
Thomas Graf 已提交
366

367
	if (frh->dst_len)
368
		rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
T
Thomas Graf 已提交
369 370 371 372 373

	rule6->src.plen = frh->src_len;
	rule6->dst.plen = frh->dst_len;
	rule6->tclass = frh->tos;

374 375 376
	if (fib_rule_requires_fldissect(rule))
		net->ipv6.fib6_rules_require_fldissect++;

377
	net->ipv6.fib6_has_custom_rules = true;
T
Thomas Graf 已提交
378 379 380 381 382
	err = 0;
errout:
	return err;
}

383 384 385 386 387 388 389 390 391 392 393
static int fib6_rule_delete(struct fib_rule *rule)
{
	struct net *net = rule->fr_net;

	if (net->ipv6.fib6_rules_require_fldissect &&
	    fib_rule_requires_fldissect(rule))
		net->ipv6.fib6_rules_require_fldissect--;

	return 0;
}

T
Thomas Graf 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407
static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
			     struct nlattr **tb)
{
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

	if (frh->src_len && (rule6->src.plen != frh->src_len))
		return 0;

	if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
		return 0;

	if (frh->tos && (rule6->tclass != frh->tos))
		return 0;

408
	if (frh->src_len &&
T
Thomas Graf 已提交
409 410 411
	    nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
		return 0;

412
	if (frh->dst_len &&
T
Thomas Graf 已提交
413 414 415 416 417 418 419
	    nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
		return 0;

	return 1;
}

static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
420
			  struct fib_rule_hdr *frh)
T
Thomas Graf 已提交
421 422 423 424 425 426 427
{
	struct fib6_rule *rule6 = (struct fib6_rule *) rule;

	frh->dst_len = rule6->dst.plen;
	frh->src_len = rule6->src.plen;
	frh->tos = rule6->tclass;

D
David S. Miller 已提交
428
	if ((rule6->dst.plen &&
429
	     nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
D
David S. Miller 已提交
430
	    (rule6->src.plen &&
431
	     nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
D
David S. Miller 已提交
432
		goto nla_put_failure;
T
Thomas Graf 已提交
433 434 435 436 437 438
	return 0;

nla_put_failure:
	return -ENOBUFS;
}

439 440 441 442 443 444
static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
{
	return nla_total_size(16) /* dst */
	       + nla_total_size(16); /* src */
}

445
static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
446
	.family			= AF_INET6,
T
Thomas Graf 已提交
447
	.rule_size		= sizeof(struct fib6_rule),
448
	.addr_size		= sizeof(struct in6_addr),
T
Thomas Graf 已提交
449 450
	.action			= fib6_rule_action,
	.match			= fib6_rule_match,
451
	.suppress		= fib6_rule_suppress,
T
Thomas Graf 已提交
452
	.configure		= fib6_rule_configure,
453
	.delete			= fib6_rule_delete,
T
Thomas Graf 已提交
454 455
	.compare		= fib6_rule_compare,
	.fill			= fib6_rule_fill,
456
	.nlmsg_payload		= fib6_rule_nlmsg_payload,
T
Thomas Graf 已提交
457 458
	.nlgroup		= RTNLGRP_IPV6_RULE,
	.owner			= THIS_MODULE,
459
	.fro_net		= &init_net,
T
Thomas Graf 已提交
460 461
};

462
static int __net_init fib6_rules_net_init(struct net *net)
T
Thomas Graf 已提交
463
{
464
	struct fib_rules_ops *ops;
465
	int err;
466

467 468 469
	ops = fib_rules_register(&fib6_rules_ops_template, net);
	if (IS_ERR(ops))
		return PTR_ERR(ops);
470

471
	err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL, 0);
472 473
	if (err)
		goto out_fib6_rules_ops;
474

475
	err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN, 0);
476
	if (err)
477
		goto out_fib6_rules_ops;
478

479
	net->ipv6.fib6_rules_ops = ops;
480
	net->ipv6.fib6_rules_require_fldissect = 0;
481
out:
482
	return err;
483

484
out_fib6_rules_ops:
485
	fib_rules_unregister(ops);
486
	goto out;
T
Thomas Graf 已提交
487 488
}

489
static void __net_exit fib6_rules_net_exit(struct net *net)
490
{
491
	rtnl_lock();
492
	fib_rules_unregister(net->ipv6.fib6_rules_ops);
493
	rtnl_unlock();
494 495 496 497 498 499 500 501 502 503 504 505 506
}

static struct pernet_operations fib6_rules_net_ops = {
	.init = fib6_rules_net_init,
	.exit = fib6_rules_net_exit,
};

int __init fib6_rules_init(void)
{
	return register_pernet_subsys(&fib6_rules_net_ops);
}


T
Thomas Graf 已提交
507 508
void fib6_rules_cleanup(void)
{
509
	unregister_pernet_subsys(&fib6_rules_net_ops);
T
Thomas Graf 已提交
510
}