2019-05-31 08:09:32 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2006-08-04 10:39:02 +00:00
|
|
|
/*
|
|
|
|
* 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>
|
2017-08-03 11:28:18 +00:00
|
|
|
#include <linux/notifier.h>
|
2011-07-15 15:47:34 +00:00
|
|
|
#include <linux/export.h>
|
2020-07-26 22:48:16 +00:00
|
|
|
#include <linux/indirect_call_wrapper.h>
|
2006-08-04 10:39:02 +00:00
|
|
|
|
|
|
|
#include <net/fib_rules.h>
|
2022-02-04 13:58:11 +00:00
|
|
|
#include <net/inet_dscp.h>
|
2006-08-04 10:39:02 +00:00
|
|
|
#include <net/ipv6.h>
|
2007-04-06 18:45:39 +00:00
|
|
|
#include <net/addrconf.h>
|
2006-08-04 10:39:02 +00:00
|
|
|
#include <net/ip6_route.h>
|
|
|
|
#include <net/netlink.h>
|
|
|
|
|
2012-04-01 07:49:06 +00:00
|
|
|
struct fib6_rule {
|
2006-08-04 10:39:02 +00:00
|
|
|
struct fib_rule common;
|
|
|
|
struct rt6key src;
|
|
|
|
struct rt6key dst;
|
2022-02-04 13:58:11 +00:00
|
|
|
dscp_t dscp;
|
2024-09-11 09:37:45 +00:00
|
|
|
u8 dscp_full:1; /* DSCP or TOS selector */
|
2006-08-04 10:39:02 +00:00
|
|
|
};
|
|
|
|
|
2017-08-03 11:28:15 +00:00
|
|
|
static bool fib6_rule_matchall(const struct fib_rule *rule)
|
|
|
|
{
|
|
|
|
struct fib6_rule *r = container_of(rule, struct fib6_rule, common);
|
|
|
|
|
2022-02-04 13:58:11 +00:00
|
|
|
if (r->dst.plen || r->src.plen || r->dscp)
|
2017-08-03 11:28:15 +00:00
|
|
|
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);
|
|
|
|
|
2019-10-03 09:49:30 +00:00
|
|
|
int fib6_rules_dump(struct net *net, struct notifier_block *nb,
|
|
|
|
struct netlink_ext_ack *extack)
|
2017-08-03 11:28:18 +00:00
|
|
|
{
|
2019-10-03 09:49:30 +00:00
|
|
|
return fib_rules_dump(net, nb, AF_INET6, extack);
|
2017-08-03 11:28:18 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 18:44:03 +00:00
|
|
|
unsigned int fib6_rules_seq_read(const struct net *net)
|
2017-08-03 11:28:18 +00:00
|
|
|
{
|
|
|
|
return fib_rules_seq_read(net, AF_INET6);
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:34:23 +00:00
|
|
|
/* called with rcu lock held; no reference taken on fib6_info */
|
2019-04-16 21:36:10 +00:00
|
|
|
int fib6_lookup(struct net *net, int oif, struct flowi6 *fl6,
|
|
|
|
struct fib6_result *res, int flags)
|
2018-05-10 03:34:23 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (net->ipv6.fib6_has_custom_rules) {
|
|
|
|
struct fib_lookup_arg arg = {
|
|
|
|
.lookup_ptr = fib6_table_lookup,
|
|
|
|
.lookup_data = &oif,
|
2019-04-16 21:36:10 +00:00
|
|
|
.result = res,
|
2018-05-10 03:34:23 +00:00
|
|
|
.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 {
|
2019-04-16 21:36:10 +00:00
|
|
|
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);
|
2018-05-10 03:34:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:36:10 +00:00
|
|
|
return err;
|
2018-05-10 03:34:23 +00:00
|
|
|
}
|
|
|
|
|
2011-03-12 21:22:43 +00:00
|
|
|
struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
|
2018-03-02 16:32:17 +00:00
|
|
|
const struct sk_buff *skb,
|
2008-03-04 07:25:27 +00:00
|
|
|
int flags, pol_lookup_t lookup)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
2017-08-08 18:23:49 +00:00
|
|
|
if (net->ipv6.fib6_has_custom_rules) {
|
2019-04-24 01:05:33 +00:00
|
|
|
struct fib6_result res = {};
|
2017-08-08 18:23:49 +00:00
|
|
|
struct fib_lookup_arg arg = {
|
|
|
|
.lookup_ptr = lookup,
|
2018-03-02 16:32:17 +00:00
|
|
|
.lookup_data = skb,
|
2019-04-24 01:05:33 +00:00
|
|
|
.result = &res,
|
2017-08-08 18:23:49 +00:00
|
|
|
.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);
|
|
|
|
|
2019-04-24 01:05:33 +00:00
|
|
|
if (res.rt6)
|
|
|
|
return &res.rt6->dst;
|
2017-08-08 18:23:49 +00:00
|
|
|
} else {
|
|
|
|
struct rt6_info *rt;
|
|
|
|
|
2020-06-23 16:42:32 +00:00
|
|
|
rt = pol_lookup_func(lookup,
|
|
|
|
net, net->ipv6.fib6_local_tbl, fl6, skb, flags);
|
2017-08-08 18:23:49 +00:00
|
|
|
if (rt != net->ipv6.ip6_null_entry && rt->dst.error != -EAGAIN)
|
|
|
|
return &rt->dst;
|
2019-06-21 00:36:39 +00:00
|
|
|
ip6_rt_put_flags(rt, flags);
|
2020-06-23 16:42:32 +00:00
|
|
|
rt = pol_lookup_func(lookup,
|
|
|
|
net, net->ipv6.fib6_main_tbl, fl6, skb, flags);
|
2017-08-08 18:23:49 +00:00
|
|
|
if (rt->dst.error != -EAGAIN)
|
|
|
|
return &rt->dst;
|
2019-06-21 00:36:39 +00:00
|
|
|
ip6_rt_put_flags(rt, flags);
|
2017-08-08 18:23:49 +00:00
|
|
|
}
|
2006-08-08 23:44:17 +00:00
|
|
|
|
2019-06-21 00:36:39 +00:00
|
|
|
if (!(flags & RT6_LOOKUP_F_DST_NOREF))
|
|
|
|
dst_hold(&net->ipv6.ip6_null_entry->dst);
|
2017-06-20 10:29:25 +00:00
|
|
|
return &net->ipv6.ip6_null_entry->dst;
|
2006-08-04 10:39:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 03:34:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:34:23 +00:00
|
|
|
static int fib6_rule_action_alt(struct fib_rule *rule, struct flowi *flp,
|
|
|
|
int flags, struct fib_lookup_arg *arg)
|
|
|
|
{
|
2019-04-16 21:36:10 +00:00
|
|
|
struct fib6_result *res = arg->result;
|
2018-05-10 03:34:23 +00:00
|
|
|
struct flowi6 *flp6 = &flp->u.ip6;
|
|
|
|
struct net *net = rule->fr_net;
|
|
|
|
struct fib6_table *table;
|
2019-04-24 01:06:30 +00:00
|
|
|
int err, *oif;
|
2018-05-10 03:34:23 +00:00
|
|
|
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;
|
2019-04-16 21:36:10 +00:00
|
|
|
err = fib6_table_lookup(net, table, *oif, flp6, res, flags);
|
|
|
|
if (!err && res->f6i != net->ipv6.fib6_null_entry)
|
2018-05-10 03:34:23 +00:00
|
|
|
err = fib6_rule_saddr(net, rule, flags, flp6,
|
2019-04-16 21:36:10 +00:00
|
|
|
res->nh->fib_nh_dev);
|
2019-04-24 01:06:30 +00:00
|
|
|
else
|
|
|
|
err = -EAGAIN;
|
2018-05-10 03:34:23 +00:00
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
|
|
|
|
int flags, struct fib_lookup_arg *arg)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
2019-04-24 01:05:33 +00:00
|
|
|
struct fib6_result *res = arg->result;
|
2011-03-12 21:22:43 +00:00
|
|
|
struct flowi6 *flp6 = &flp->u.ip6;
|
2006-08-04 10:39:02 +00:00
|
|
|
struct rt6_info *rt = NULL;
|
|
|
|
struct fib6_table *table;
|
2008-03-04 21:48:30 +00:00
|
|
|
struct net *net = rule->fr_net;
|
2006-08-04 10:39:02 +00:00
|
|
|
pol_lookup_t lookup = arg->lookup_ptr;
|
2013-08-01 06:54:47 +00:00
|
|
|
int err = 0;
|
2016-06-08 17:55:39 +00:00
|
|
|
u32 tb_id;
|
2006-08-04 10:39:02 +00:00
|
|
|
|
|
|
|
switch (rule->action) {
|
|
|
|
case FR_ACT_TO_TBL:
|
|
|
|
break;
|
|
|
|
case FR_ACT_UNREACHABLE:
|
2013-08-01 06:54:47 +00:00
|
|
|
err = -ENETUNREACH;
|
2008-03-04 21:48:30 +00:00
|
|
|
rt = net->ipv6.ip6_null_entry;
|
2006-08-04 10:39:02 +00:00
|
|
|
goto discard_pkt;
|
|
|
|
default:
|
|
|
|
case FR_ACT_BLACKHOLE:
|
2013-08-01 06:54:47 +00:00
|
|
|
err = -EINVAL;
|
2008-03-04 21:48:30 +00:00
|
|
|
rt = net->ipv6.ip6_blk_hole_entry;
|
2006-08-04 10:39:02 +00:00
|
|
|
goto discard_pkt;
|
|
|
|
case FR_ACT_PROHIBIT:
|
2013-08-01 06:54:47 +00:00
|
|
|
err = -EACCES;
|
2008-03-04 21:48:30 +00:00
|
|
|
rt = net->ipv6.ip6_prohibit_entry;
|
2006-08-04 10:39:02 +00:00
|
|
|
goto discard_pkt;
|
|
|
|
}
|
|
|
|
|
2016-06-08 17:55:39 +00:00
|
|
|
tb_id = fib_rule_get_table(rule, arg);
|
|
|
|
table = fib6_get_table(net, tb_id);
|
2013-08-01 06:54:47 +00:00
|
|
|
if (!table) {
|
|
|
|
err = -EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2020-06-23 16:42:32 +00:00
|
|
|
rt = pol_lookup_func(lookup,
|
|
|
|
net, table, flp6, arg->lookup_data, flags);
|
2008-03-04 21:48:30 +00:00
|
|
|
if (rt != net->ipv6.ip6_null_entry) {
|
2024-05-07 16:31:45 +00:00
|
|
|
struct inet6_dev *idev = ip6_dst_idev(&rt->dst);
|
|
|
|
|
|
|
|
if (!idev)
|
|
|
|
goto again;
|
2018-05-10 03:34:22 +00:00
|
|
|
err = fib6_rule_saddr(net, rule, flags, flp6,
|
2024-05-07 16:31:45 +00:00
|
|
|
idev->dev);
|
2018-05-10 03:34:22 +00:00
|
|
|
|
|
|
|
if (err == -EAGAIN)
|
|
|
|
goto again;
|
|
|
|
|
2015-03-19 15:16:04 +00:00
|
|
|
err = rt->dst.error;
|
2017-06-20 10:29:25 +00:00
|
|
|
if (err != -EAGAIN)
|
|
|
|
goto out;
|
2007-04-06 18:45:39 +00:00
|
|
|
}
|
|
|
|
again:
|
2019-06-21 00:36:39 +00:00
|
|
|
ip6_rt_put_flags(rt, flags);
|
2013-08-01 06:54:47 +00:00
|
|
|
err = -EAGAIN;
|
2006-08-07 05:24:08 +00:00
|
|
|
rt = NULL;
|
|
|
|
goto out;
|
|
|
|
|
2006-08-04 10:39:02 +00:00
|
|
|
discard_pkt:
|
2019-06-21 00:36:39 +00:00
|
|
|
if (!(flags & RT6_LOOKUP_F_DST_NOREF))
|
|
|
|
dst_hold(&rt->dst);
|
2006-08-04 10:39:02 +00:00
|
|
|
out:
|
2019-04-24 01:05:33 +00:00
|
|
|
res->rt6 = rt;
|
2013-08-01 06:54:47 +00:00
|
|
|
return err;
|
2006-08-04 10:39:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-26 22:48:16 +00:00
|
|
|
INDIRECT_CALLABLE_SCOPE int fib6_rule_action(struct fib_rule *rule,
|
|
|
|
struct flowi *flp, int flags,
|
|
|
|
struct fib_lookup_arg *arg)
|
2018-05-10 03:34:23 +00:00
|
|
|
{
|
|
|
|
if (arg->lookup_ptr == fib6_table_lookup)
|
|
|
|
return fib6_rule_action_alt(rule, flp, flags, arg);
|
|
|
|
|
|
|
|
return __fib6_rule_action(rule, flp, flags, arg);
|
|
|
|
}
|
|
|
|
|
2020-07-26 22:48:16 +00:00
|
|
|
INDIRECT_CALLABLE_SCOPE bool fib6_rule_suppress(struct fib_rule *rule,
|
2021-11-23 12:48:32 +00:00
|
|
|
int flags,
|
2020-07-26 22:48:16 +00:00
|
|
|
struct fib_lookup_arg *arg)
|
2013-08-01 00:17:15 +00:00
|
|
|
{
|
2019-04-24 01:05:33 +00:00
|
|
|
struct fib6_result *res = arg->result;
|
|
|
|
struct rt6_info *rt = res->rt6;
|
2013-12-10 22:21:25 +00:00
|
|
|
struct net_device *dev = NULL;
|
|
|
|
|
2019-04-24 01:05:33 +00:00
|
|
|
if (!rt)
|
|
|
|
return false;
|
|
|
|
|
2013-12-10 22:21:25 +00:00
|
|
|
if (rt->rt6i_idev)
|
|
|
|
dev = rt->rt6i_idev->dev;
|
|
|
|
|
2013-08-01 00:17:15 +00:00
|
|
|
/* do not accept result if the route does
|
|
|
|
* not meet the required prefix length
|
|
|
|
*/
|
2013-08-03 12:14:43 +00:00
|
|
|
if (rt->rt6i_dst.plen <= rule->suppress_prefixlen)
|
2013-08-02 15:19:56 +00:00
|
|
|
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:
|
2021-11-23 12:48:32 +00:00
|
|
|
ip6_rt_put_flags(rt, flags);
|
2013-09-08 15:09:43 +00:00
|
|
|
return true;
|
2013-08-01 00:17:15 +00:00
|
|
|
}
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2020-07-26 22:48:16 +00:00
|
|
|
INDIRECT_CALLABLE_SCOPE int fib6_rule_match(struct fib_rule *rule,
|
|
|
|
struct flowi *fl, int flags)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
|
|
|
struct fib6_rule *r = (struct fib6_rule *) rule;
|
2011-03-12 21:22:43 +00:00
|
|
|
struct flowi6 *fl6 = &fl->u.ip6;
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2006-10-13 22:01:03 +00:00
|
|
|
if (r->dst.plen &&
|
2011-03-12 21:22:43 +00:00
|
|
|
!ipv6_prefix_equal(&fl6->daddr, &r->dst.addr, r->dst.plen))
|
2006-08-04 10:39:02 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-04-06 18:45:39 +00:00
|
|
|
/*
|
|
|
|
* If FIB_RULE_FIND_SADDR is set and we do not have a
|
|
|
|
* source address for the traffic, we defer check for
|
|
|
|
* source address.
|
|
|
|
*/
|
2006-10-13 22:01:03 +00:00
|
|
|
if (r->src.plen) {
|
2007-04-06 18:45:39 +00:00
|
|
|
if (flags & RT6_LOOKUP_F_HAS_SADDR) {
|
2011-03-12 21:22:43 +00:00
|
|
|
if (!ipv6_prefix_equal(&fl6->saddr, &r->src.addr,
|
2007-04-06 18:45:39 +00:00
|
|
|
r->src.plen))
|
|
|
|
return 0;
|
|
|
|
} else if (!(r->common.flags & FIB_RULE_FIND_SADDR))
|
2006-10-13 22:01:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2022-02-04 13:58:11 +00:00
|
|
|
if (r->dscp && r->dscp != ip6_dscp(fl6->flowlabel))
|
2006-08-21 10:18:57 +00:00
|
|
|
return 0;
|
|
|
|
|
2018-03-01 03:41:37 +00:00
|
|
|
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;
|
|
|
|
|
2006-08-04 10:39:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-11 09:37:45 +00:00
|
|
|
static int fib6_nl2rule_dscp(const struct nlattr *nla, struct fib6_rule *rule6,
|
|
|
|
struct netlink_ext_ack *extack)
|
|
|
|
{
|
|
|
|
if (rule6->dscp) {
|
|
|
|
NL_SET_ERR_MSG(extack, "Cannot specify both TOS and DSCP");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
rule6->dscp = inet_dsfield_to_dscp(nla_get_u8(nla) << 2);
|
|
|
|
rule6->dscp_full = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-04 10:39:02 +00:00
|
|
|
static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
|
2009-05-11 05:52:49 +00:00
|
|
|
struct fib_rule_hdr *frh,
|
2018-04-21 16:41:31 +00:00
|
|
|
struct nlattr **tb,
|
|
|
|
struct netlink_ext_ack *extack)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
|
|
|
int err = -EINVAL;
|
2008-03-25 17:26:21 +00:00
|
|
|
struct net *net = sock_net(skb->sk);
|
2006-08-04 10:39:02 +00:00
|
|
|
struct fib6_rule *rule6 = (struct fib6_rule *) rule;
|
|
|
|
|
2022-02-04 13:58:11 +00:00
|
|
|
if (!inet_validate_dscp(frh->tos)) {
|
|
|
|
NL_SET_ERR_MSG(extack,
|
|
|
|
"Invalid dsfield (tos): ECN bits must be 0");
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
rule6->dscp = inet_dsfield_to_dscp(frh->tos);
|
|
|
|
|
2024-09-11 09:37:45 +00:00
|
|
|
if (tb[FRA_DSCP] && fib6_nl2rule_dscp(tb[FRA_DSCP], rule6, extack) < 0)
|
|
|
|
goto errout;
|
|
|
|
|
2016-06-08 17:55:39 +00:00
|
|
|
if (rule->action == FR_ACT_TO_TBL && !rule->l3mdev) {
|
2018-04-21 16:41:31 +00:00
|
|
|
if (rule->table == RT6_TABLE_UNSPEC) {
|
|
|
|
NL_SET_ERR_MSG(extack, "Invalid table");
|
2006-08-04 10:39:02 +00:00
|
|
|
goto errout;
|
2018-04-21 16:41:31 +00:00
|
|
|
}
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2008-03-04 07:33:08 +00:00
|
|
|
if (fib6_new_table(net, rule->table) == NULL) {
|
2006-08-04 10:39:02 +00:00
|
|
|
err = -ENOBUFS;
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-24 19:46:02 +00:00
|
|
|
if (frh->src_len)
|
2015-03-29 14:59:26 +00:00
|
|
|
rule6->src.addr = nla_get_in6_addr(tb[FRA_SRC]);
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2007-03-24 19:46:02 +00:00
|
|
|
if (frh->dst_len)
|
2015-03-29 14:59:26 +00:00
|
|
|
rule6->dst.addr = nla_get_in6_addr(tb[FRA_DST]);
|
2006-08-04 10:39:02 +00:00
|
|
|
|
|
|
|
rule6->src.plen = frh->src_len;
|
|
|
|
rule6->dst.plen = frh->dst_len;
|
|
|
|
|
2018-03-01 03:43:22 +00:00
|
|
|
if (fib_rule_requires_fldissect(rule))
|
|
|
|
net->ipv6.fib6_rules_require_fldissect++;
|
|
|
|
|
2017-08-08 18:23:49 +00:00
|
|
|
net->ipv6.fib6_has_custom_rules = true;
|
2006-08-04 10:39:02 +00:00
|
|
|
err = 0;
|
|
|
|
errout:
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2018-03-01 03:43:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-08-04 10:39:02 +00:00
|
|
|
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;
|
|
|
|
|
2024-09-11 09:37:45 +00:00
|
|
|
if (frh->tos &&
|
|
|
|
(rule6->dscp_full ||
|
|
|
|
inet_dscp_to_dsfield(rule6->dscp) != frh->tos))
|
2006-08-04 10:39:02 +00:00
|
|
|
return 0;
|
|
|
|
|
2024-09-11 09:37:45 +00:00
|
|
|
if (tb[FRA_DSCP]) {
|
|
|
|
dscp_t dscp;
|
|
|
|
|
|
|
|
dscp = inet_dsfield_to_dscp(nla_get_u8(tb[FRA_DSCP]) << 2);
|
|
|
|
if (!rule6->dscp_full || rule6->dscp != dscp)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-24 19:46:02 +00:00
|
|
|
if (frh->src_len &&
|
2006-08-04 10:39:02 +00:00
|
|
|
nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
|
|
|
|
return 0;
|
|
|
|
|
2007-03-24 19:46:02 +00:00
|
|
|
if (frh->dst_len &&
|
2006-08-04 10:39:02 +00:00
|
|
|
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,
|
2009-05-21 00:26:23 +00:00
|
|
|
struct fib_rule_hdr *frh)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
|
|
|
struct fib6_rule *rule6 = (struct fib6_rule *) rule;
|
|
|
|
|
|
|
|
frh->dst_len = rule6->dst.plen;
|
|
|
|
frh->src_len = rule6->src.plen;
|
2024-09-11 09:37:45 +00:00
|
|
|
|
|
|
|
if (rule6->dscp_full) {
|
|
|
|
frh->tos = 0;
|
|
|
|
if (nla_put_u8(skb, FRA_DSCP,
|
|
|
|
inet_dscp_to_dsfield(rule6->dscp) >> 2))
|
|
|
|
goto nla_put_failure;
|
|
|
|
} else {
|
|
|
|
frh->tos = inet_dscp_to_dsfield(rule6->dscp);
|
|
|
|
}
|
2006-08-04 10:39:02 +00:00
|
|
|
|
2012-04-02 00:27:33 +00:00
|
|
|
if ((rule6->dst.plen &&
|
2015-03-29 14:59:25 +00:00
|
|
|
nla_put_in6_addr(skb, FRA_DST, &rule6->dst.addr)) ||
|
2012-04-02 00:27:33 +00:00
|
|
|
(rule6->src.plen &&
|
2015-03-29 14:59:25 +00:00
|
|
|
nla_put_in6_addr(skb, FRA_SRC, &rule6->src.addr)))
|
2012-04-02 00:27:33 +00:00
|
|
|
goto nla_put_failure;
|
2006-08-04 10:39:02 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
|
|
|
|
2006-11-10 22:10:15 +00:00
|
|
|
static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
|
|
|
|
{
|
|
|
|
return nla_total_size(16) /* dst */
|
2024-09-11 09:37:45 +00:00
|
|
|
+ nla_total_size(16) /* src */
|
|
|
|
+ nla_total_size(1); /* dscp */
|
2006-11-10 22:10:15 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 10:01:57 +00:00
|
|
|
static void fib6_rule_flush_cache(struct fib_rules_ops *ops)
|
|
|
|
{
|
|
|
|
rt_genid_bump_ipv6(ops->fro_net);
|
|
|
|
}
|
|
|
|
|
2012-10-05 00:12:11 +00:00
|
|
|
static const struct fib_rules_ops __net_initconst fib6_rules_ops_template = {
|
2010-04-26 14:02:05 +00:00
|
|
|
.family = AF_INET6,
|
2006-08-04 10:39:02 +00:00
|
|
|
.rule_size = sizeof(struct fib6_rule),
|
2007-03-24 19:46:02 +00:00
|
|
|
.addr_size = sizeof(struct in6_addr),
|
2006-08-04 10:39:02 +00:00
|
|
|
.action = fib6_rule_action,
|
|
|
|
.match = fib6_rule_match,
|
2013-08-01 00:17:15 +00:00
|
|
|
.suppress = fib6_rule_suppress,
|
2006-08-04 10:39:02 +00:00
|
|
|
.configure = fib6_rule_configure,
|
2018-03-01 03:43:22 +00:00
|
|
|
.delete = fib6_rule_delete,
|
2006-08-04 10:39:02 +00:00
|
|
|
.compare = fib6_rule_compare,
|
|
|
|
.fill = fib6_rule_fill,
|
2006-11-10 22:10:15 +00:00
|
|
|
.nlmsg_payload = fib6_rule_nlmsg_payload,
|
2024-03-07 10:01:57 +00:00
|
|
|
.flush_cache = fib6_rule_flush_cache,
|
2006-08-04 10:39:02 +00:00
|
|
|
.nlgroup = RTNLGRP_IPV6_RULE,
|
|
|
|
.owner = THIS_MODULE,
|
2008-01-21 00:46:01 +00:00
|
|
|
.fro_net = &init_net,
|
2006-08-04 10:39:02 +00:00
|
|
|
};
|
|
|
|
|
2010-01-17 03:35:32 +00:00
|
|
|
static int __net_init fib6_rules_net_init(struct net *net)
|
2006-08-04 10:39:02 +00:00
|
|
|
{
|
2009-12-03 20:22:55 +00:00
|
|
|
struct fib_rules_ops *ops;
|
2021-06-13 13:46:36 +00:00
|
|
|
int err;
|
2007-11-11 06:12:03 +00:00
|
|
|
|
2009-12-03 20:22:55 +00:00
|
|
|
ops = fib_rules_register(&fib6_rules_ops_template, net);
|
|
|
|
if (IS_ERR(ops))
|
|
|
|
return PTR_ERR(ops);
|
2008-03-04 07:32:30 +00:00
|
|
|
|
2024-01-02 07:15:19 +00:00
|
|
|
err = fib_default_rule_add(ops, 0, RT6_TABLE_LOCAL);
|
2008-03-04 07:33:08 +00:00
|
|
|
if (err)
|
|
|
|
goto out_fib6_rules_ops;
|
2007-12-07 08:42:52 +00:00
|
|
|
|
2024-01-02 07:15:19 +00:00
|
|
|
err = fib_default_rule_add(ops, 0x7FFE, RT6_TABLE_MAIN);
|
2008-03-04 07:33:08 +00:00
|
|
|
if (err)
|
2009-12-03 20:22:55 +00:00
|
|
|
goto out_fib6_rules_ops;
|
2007-12-07 08:42:52 +00:00
|
|
|
|
2015-03-25 21:45:02 +00:00
|
|
|
net->ipv6.fib6_rules_ops = ops;
|
2018-03-01 03:43:22 +00:00
|
|
|
net->ipv6.fib6_rules_require_fldissect = 0;
|
2007-12-07 08:42:52 +00:00
|
|
|
out:
|
2008-03-04 07:33:08 +00:00
|
|
|
return err;
|
2007-12-07 08:42:52 +00:00
|
|
|
|
2008-03-04 07:33:08 +00:00
|
|
|
out_fib6_rules_ops:
|
2009-12-03 20:22:55 +00:00
|
|
|
fib_rules_unregister(ops);
|
2007-12-07 08:42:52 +00:00
|
|
|
goto out;
|
2006-08-04 10:39:02 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 04:50:33 +00:00
|
|
|
static void __net_exit fib6_rules_net_exit_batch(struct list_head *net_list)
|
2008-03-04 07:33:08 +00:00
|
|
|
{
|
2022-02-08 04:50:33 +00:00
|
|
|
struct net *net;
|
|
|
|
|
2015-03-31 18:01:46 +00:00
|
|
|
rtnl_lock();
|
2022-02-08 04:50:33 +00:00
|
|
|
list_for_each_entry(net, net_list, exit_list) {
|
|
|
|
fib_rules_unregister(net->ipv6.fib6_rules_ops);
|
|
|
|
cond_resched();
|
|
|
|
}
|
2015-03-31 18:01:46 +00:00
|
|
|
rtnl_unlock();
|
2008-03-04 07:33:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations fib6_rules_net_ops = {
|
|
|
|
.init = fib6_rules_net_init,
|
2022-02-08 04:50:33 +00:00
|
|
|
.exit_batch = fib6_rules_net_exit_batch,
|
2008-03-04 07:33:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
int __init fib6_rules_init(void)
|
|
|
|
{
|
|
|
|
return register_pernet_subsys(&fib6_rules_net_ops);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-08-04 10:39:02 +00:00
|
|
|
void fib6_rules_cleanup(void)
|
|
|
|
{
|
2008-04-10 06:41:28 +00:00
|
|
|
unregister_pernet_subsys(&fib6_rules_net_ops);
|
2006-08-04 10:39:02 +00:00
|
|
|
}
|