Skip to content

Commit bae9a78

Browse files
dsaherndavem330
authored andcommitted
net/mpls: Plumb support for filtering route dumps
Implement kernel side filtering of routes by egress device index and protocol. MPLS uses only a single table and route type. Signed-off-by: David Ahern <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 13e3890 commit bae9a78

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

net/mpls/af_mpls.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2067,12 +2067,35 @@ static int mpls_valid_fib_dump_req(struct net *net, const struct nlmsghdr *nlh,
20672067
}
20682068
#endif
20692069

2070+
static bool mpls_rt_uses_dev(struct mpls_route *rt,
2071+
const struct net_device *dev)
2072+
{
2073+
struct net_device *nh_dev;
2074+
2075+
if (rt->rt_nhn == 1) {
2076+
struct mpls_nh *nh = rt->rt_nh;
2077+
2078+
nh_dev = rtnl_dereference(nh->nh_dev);
2079+
if (dev == nh_dev)
2080+
return true;
2081+
} else {
2082+
for_nexthops(rt) {
2083+
nh_dev = rtnl_dereference(nh->nh_dev);
2084+
if (nh_dev == dev)
2085+
return true;
2086+
} endfor_nexthops(rt);
2087+
}
2088+
2089+
return false;
2090+
}
2091+
20702092
static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
20712093
{
20722094
const struct nlmsghdr *nlh = cb->nlh;
20732095
struct net *net = sock_net(skb->sk);
20742096
struct mpls_route __rcu **platform_label;
20752097
struct fib_dump_filter filter = {};
2098+
unsigned int flags = NLM_F_MULTI;
20762099
size_t platform_labels;
20772100
unsigned int index;
20782101

@@ -2084,6 +2107,14 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
20842107
err = mpls_valid_fib_dump_req(net, nlh, &filter, cb->extack);
20852108
if (err < 0)
20862109
return err;
2110+
2111+
/* for MPLS, there is only 1 table with fixed type and flags.
2112+
* If either are set in the filter then return nothing.
2113+
*/
2114+
if ((filter.table_id && filter.table_id != RT_TABLE_MAIN) ||
2115+
(filter.rt_type && filter.rt_type != RTN_UNICAST) ||
2116+
filter.flags)
2117+
return skb->len;
20872118
}
20882119

20892120
index = cb->args[0];
@@ -2092,15 +2123,24 @@ static int mpls_dump_routes(struct sk_buff *skb, struct netlink_callback *cb)
20922123

20932124
platform_label = rtnl_dereference(net->mpls.platform_label);
20942125
platform_labels = net->mpls.platform_labels;
2126+
2127+
if (filter.filter_set)
2128+
flags |= NLM_F_DUMP_FILTERED;
2129+
20952130
for (; index < platform_labels; index++) {
20962131
struct mpls_route *rt;
2132+
20972133
rt = rtnl_dereference(platform_label[index]);
20982134
if (!rt)
20992135
continue;
21002136

2137+
if ((filter.dev && !mpls_rt_uses_dev(rt, filter.dev)) ||
2138+
(filter.protocol && rt->rt_protocol != filter.protocol))
2139+
continue;
2140+
21012141
if (mpls_dump_route(skb, NETLINK_CB(cb->skb).portid,
21022142
cb->nlh->nlmsg_seq, RTM_NEWROUTE,
2103-
index, rt, NLM_F_MULTI) < 0)
2143+
index, rt, flags) < 0)
21042144
break;
21052145
}
21062146
cb->args[0] = index;

0 commit comments

Comments
 (0)