Skip to content

Commit 571e722

Browse files
roopa-prabhudavem330
authored andcommitted
ipv4: support for fib route lwtunnel encap attributes
This patch adds support in ipv4 fib functions to parse user provided encap attributes and attach encap state data to fib_nh and rtable. Signed-off-by: Roopa Prabhu <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 499a242 commit 571e722

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

include/net/ip_fib.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ struct fib_config {
4444
u32 fc_flow;
4545
u32 fc_nlflags;
4646
struct nl_info fc_nlinfo;
47-
};
47+
struct nlattr *fc_encap;
48+
u16 fc_encap_type;
49+
};
4850

4951
struct fib_info;
5052
struct rtable;
@@ -89,6 +91,7 @@ struct fib_nh {
8991
struct rtable __rcu * __percpu *nh_pcpu_rth_output;
9092
struct rtable __rcu *nh_rth_input;
9193
struct fnhe_hash_bucket __rcu *nh_exceptions;
94+
struct lwtunnel_state *nh_lwtstate;
9295
};
9396

9497
/*

include/net/route.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ struct rtable {
6666

6767
struct list_head rt_uncached;
6868
struct uncached_list *rt_uncached_list;
69+
struct lwtunnel_state *rt_lwtstate;
6970
};
7071

7172
static inline bool rt_is_input_route(const struct rtable *rt)

net/ipv4/fib_frontend.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,8 @@ const struct nla_policy rtm_ipv4_policy[RTA_MAX + 1] = {
591591
[RTA_METRICS] = { .type = NLA_NESTED },
592592
[RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
593593
[RTA_FLOW] = { .type = NLA_U32 },
594+
[RTA_ENCAP_TYPE] = { .type = NLA_U16 },
595+
[RTA_ENCAP] = { .type = NLA_NESTED },
594596
};
595597

596598
static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
@@ -656,6 +658,12 @@ static int rtm_to_fib_config(struct net *net, struct sk_buff *skb,
656658
case RTA_TABLE:
657659
cfg->fc_table = nla_get_u32(attr);
658660
break;
661+
case RTA_ENCAP:
662+
cfg->fc_encap = attr;
663+
break;
664+
case RTA_ENCAP_TYPE:
665+
cfg->fc_encap_type = nla_get_u16(attr);
666+
break;
659667
}
660668
}
661669

net/ipv4/fib_semantics.c

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <net/ip_fib.h>
4343
#include <net/netlink.h>
4444
#include <net/nexthop.h>
45+
#include <net/lwtunnel.h>
4546

4647
#include "fib_lookup.h"
4748

@@ -208,6 +209,7 @@ static void free_fib_info_rcu(struct rcu_head *head)
208209
change_nexthops(fi) {
209210
if (nexthop_nh->nh_dev)
210211
dev_put(nexthop_nh->nh_dev);
212+
lwtunnel_state_put(nexthop_nh->nh_lwtstate);
211213
free_nh_exceptions(nexthop_nh);
212214
rt_fibinfo_free_cpus(nexthop_nh->nh_pcpu_rth_output);
213215
rt_fibinfo_free(&nexthop_nh->nh_rth_input);
@@ -266,6 +268,7 @@ static inline int nh_comp(const struct fib_info *fi, const struct fib_info *ofi)
266268
#ifdef CONFIG_IP_ROUTE_CLASSID
267269
nh->nh_tclassid != onh->nh_tclassid ||
268270
#endif
271+
lwtunnel_cmp_encap(nh->nh_lwtstate, onh->nh_lwtstate) ||
269272
((nh->nh_flags ^ onh->nh_flags) & ~RTNH_COMPARE_MASK))
270273
return -1;
271274
onh++;
@@ -366,6 +369,7 @@ static inline size_t fib_nlmsg_size(struct fib_info *fi)
366369
payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
367370

368371
if (fi->fib_nhs) {
372+
size_t nh_encapsize = 0;
369373
/* Also handles the special case fib_nhs == 1 */
370374

371375
/* each nexthop is packed in an attribute */
@@ -374,8 +378,21 @@ static inline size_t fib_nlmsg_size(struct fib_info *fi)
374378
/* may contain flow and gateway attribute */
375379
nhsize += 2 * nla_total_size(4);
376380

381+
/* grab encap info */
382+
for_nexthops(fi) {
383+
if (nh->nh_lwtstate) {
384+
/* RTA_ENCAP_TYPE */
385+
nh_encapsize += lwtunnel_get_encap_size(
386+
nh->nh_lwtstate);
387+
/* RTA_ENCAP */
388+
nh_encapsize += nla_total_size(2);
389+
}
390+
} endfor_nexthops(fi);
391+
377392
/* all nexthops are packed in a nested attribute */
378-
payload += nla_total_size(fi->fib_nhs * nhsize);
393+
payload += nla_total_size((fi->fib_nhs * nhsize) +
394+
nh_encapsize);
395+
379396
}
380397

381398
return payload;
@@ -452,6 +469,9 @@ static int fib_count_nexthops(struct rtnexthop *rtnh, int remaining)
452469
static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
453470
int remaining, struct fib_config *cfg)
454471
{
472+
struct net *net = cfg->fc_nlinfo.nl_net;
473+
int ret;
474+
455475
change_nexthops(fi) {
456476
int attrlen;
457477

@@ -475,18 +495,66 @@ static int fib_get_nhs(struct fib_info *fi, struct rtnexthop *rtnh,
475495
if (nexthop_nh->nh_tclassid)
476496
fi->fib_net->ipv4.fib_num_tclassid_users++;
477497
#endif
498+
nla = nla_find(attrs, attrlen, RTA_ENCAP);
499+
if (nla) {
500+
struct lwtunnel_state *lwtstate;
501+
struct net_device *dev = NULL;
502+
struct nlattr *nla_entype;
503+
504+
nla_entype = nla_find(attrs, attrlen,
505+
RTA_ENCAP_TYPE);
506+
if (!nla_entype)
507+
goto err_inval;
508+
if (cfg->fc_oif)
509+
dev = __dev_get_by_index(net, cfg->fc_oif);
510+
ret = lwtunnel_build_state(dev, nla_get_u16(
511+
nla_entype),
512+
nla, &lwtstate);
513+
if (ret)
514+
goto errout;
515+
lwtunnel_state_get(lwtstate);
516+
nexthop_nh->nh_lwtstate = lwtstate;
517+
}
478518
}
479519

480520
rtnh = rtnh_next(rtnh, &remaining);
481521
} endfor_nexthops(fi);
482522

483523
return 0;
524+
525+
err_inval:
526+
ret = -EINVAL;
527+
528+
errout:
529+
return ret;
484530
}
485531

486532
#endif
487533

534+
int fib_encap_match(struct net *net, u16 encap_type,
535+
struct nlattr *encap,
536+
int oif, const struct fib_nh *nh)
537+
{
538+
struct lwtunnel_state *lwtstate;
539+
struct net_device *dev = NULL;
540+
int ret;
541+
542+
if (encap_type == LWTUNNEL_ENCAP_NONE)
543+
return 0;
544+
545+
if (oif)
546+
dev = __dev_get_by_index(net, oif);
547+
ret = lwtunnel_build_state(dev, encap_type,
548+
encap, &lwtstate);
549+
if (!ret)
550+
return lwtunnel_cmp_encap(lwtstate, nh->nh_lwtstate);
551+
552+
return 0;
553+
}
554+
488555
int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
489556
{
557+
struct net *net = cfg->fc_nlinfo.nl_net;
490558
#ifdef CONFIG_IP_ROUTE_MULTIPATH
491559
struct rtnexthop *rtnh;
492560
int remaining;
@@ -496,6 +564,12 @@ int fib_nh_match(struct fib_config *cfg, struct fib_info *fi)
496564
return 1;
497565

498566
if (cfg->fc_oif || cfg->fc_gw) {
567+
if (cfg->fc_encap) {
568+
if (fib_encap_match(net, cfg->fc_encap_type,
569+
cfg->fc_encap, cfg->fc_oif,
570+
fi->fib_nh))
571+
return 1;
572+
}
499573
if ((!cfg->fc_oif || cfg->fc_oif == fi->fib_nh->nh_oif) &&
500574
(!cfg->fc_gw || cfg->fc_gw == fi->fib_nh->nh_gw))
501575
return 0;
@@ -882,6 +956,22 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
882956
} else {
883957
struct fib_nh *nh = fi->fib_nh;
884958

959+
if (cfg->fc_encap) {
960+
struct lwtunnel_state *lwtstate;
961+
struct net_device *dev = NULL;
962+
963+
if (cfg->fc_encap_type == LWTUNNEL_ENCAP_NONE)
964+
goto err_inval;
965+
if (cfg->fc_oif)
966+
dev = __dev_get_by_index(net, cfg->fc_oif);
967+
err = lwtunnel_build_state(dev, cfg->fc_encap_type,
968+
cfg->fc_encap, &lwtstate);
969+
if (err)
970+
goto failure;
971+
972+
lwtunnel_state_get(lwtstate);
973+
nh->nh_lwtstate = lwtstate;
974+
}
885975
nh->nh_oif = cfg->fc_oif;
886976
nh->nh_gw = cfg->fc_gw;
887977
nh->nh_flags = cfg->fc_flags;
@@ -1055,6 +1145,8 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
10551145
nla_put_u32(skb, RTA_FLOW, fi->fib_nh[0].nh_tclassid))
10561146
goto nla_put_failure;
10571147
#endif
1148+
if (fi->fib_nh->nh_lwtstate)
1149+
lwtunnel_fill_encap(skb, fi->fib_nh->nh_lwtstate);
10581150
}
10591151
#ifdef CONFIG_IP_ROUTE_MULTIPATH
10601152
if (fi->fib_nhs > 1) {
@@ -1090,6 +1182,8 @@ int fib_dump_info(struct sk_buff *skb, u32 portid, u32 seq, int event,
10901182
nla_put_u32(skb, RTA_FLOW, nh->nh_tclassid))
10911183
goto nla_put_failure;
10921184
#endif
1185+
if (nh->nh_lwtstate)
1186+
lwtunnel_fill_encap(skb, nh->nh_lwtstate);
10931187
/* length of rtnetlink header + attributes */
10941188
rtnh->rtnh_len = nlmsg_get_pos(skb) - (void *) rtnh;
10951189
} endfor_nexthops(fi);

net/ipv4/route.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
#include <net/tcp.h>
103103
#include <net/icmp.h>
104104
#include <net/xfrm.h>
105+
#include <net/lwtunnel.h>
105106
#include <net/netevent.h>
106107
#include <net/rtnetlink.h>
107108
#ifdef CONFIG_SYSCTL
@@ -1355,6 +1356,7 @@ static void ipv4_dst_destroy(struct dst_entry *dst)
13551356
list_del(&rt->rt_uncached);
13561357
spin_unlock_bh(&ul->lock);
13571358
}
1359+
lwtunnel_state_put(rt->rt_lwtstate);
13581360
}
13591361

13601362
void rt_flush_dev(struct net_device *dev)
@@ -1403,6 +1405,12 @@ static void rt_set_nexthop(struct rtable *rt, __be32 daddr,
14031405
#ifdef CONFIG_IP_ROUTE_CLASSID
14041406
rt->dst.tclassid = nh->nh_tclassid;
14051407
#endif
1408+
if (nh->nh_lwtstate) {
1409+
lwtunnel_state_get(nh->nh_lwtstate);
1410+
rt->rt_lwtstate = nh->nh_lwtstate;
1411+
} else {
1412+
rt->rt_lwtstate = NULL;
1413+
}
14061414
if (unlikely(fnhe))
14071415
cached = rt_bind_exception(rt, fnhe, daddr);
14081416
else if (!(rt->dst.flags & DST_NOCACHE))
@@ -1488,6 +1496,7 @@ static int ip_route_input_mc(struct sk_buff *skb, __be32 daddr, __be32 saddr,
14881496
rth->rt_gateway = 0;
14891497
rth->rt_uses_gateway = 0;
14901498
INIT_LIST_HEAD(&rth->rt_uncached);
1499+
rth->rt_lwtstate = NULL;
14911500
if (our) {
14921501
rth->dst.input= ip_local_deliver;
14931502
rth->rt_flags |= RTCF_LOCAL;
@@ -1617,6 +1626,7 @@ static int __mkroute_input(struct sk_buff *skb,
16171626
rth->rt_gateway = 0;
16181627
rth->rt_uses_gateway = 0;
16191628
INIT_LIST_HEAD(&rth->rt_uncached);
1629+
rth->rt_lwtstate = NULL;
16201630
RT_CACHE_STAT_INC(in_slow_tot);
16211631

16221632
rth->dst.input = ip_forward;
@@ -1791,6 +1801,8 @@ out: return err;
17911801
rth->rt_gateway = 0;
17921802
rth->rt_uses_gateway = 0;
17931803
INIT_LIST_HEAD(&rth->rt_uncached);
1804+
rth->rt_lwtstate = NULL;
1805+
17941806
RT_CACHE_STAT_INC(in_slow_tot);
17951807
if (res.type == RTN_UNREACHABLE) {
17961808
rth->dst.input= ip_error;
@@ -1980,7 +1992,7 @@ static struct rtable *__mkroute_output(const struct fib_result *res,
19801992
rth->rt_gateway = 0;
19811993
rth->rt_uses_gateway = 0;
19821994
INIT_LIST_HEAD(&rth->rt_uncached);
1983-
1995+
rth->rt_lwtstate = NULL;
19841996
RT_CACHE_STAT_INC(out_slow_tot);
19851997

19861998
if (flags & RTCF_LOCAL)
@@ -2260,7 +2272,7 @@ struct dst_entry *ipv4_blackhole_route(struct net *net, struct dst_entry *dst_or
22602272
rt->rt_uses_gateway = ort->rt_uses_gateway;
22612273

22622274
INIT_LIST_HEAD(&rt->rt_uncached);
2263-
2275+
rt->rt_lwtstate = NULL;
22642276
dst_free(new);
22652277
}
22662278

0 commit comments

Comments
 (0)