Skip to content

Commit 83c4425

Browse files
dsaherndavem330
authored andcommitted
ipv6: Create init helper for fib6_nh
Similar to IPv4, consolidate the fib6_nh initialization into a helper. As a new standalone function, add a cleanup path to put lwtstate on error. To avoid modifying fib6_config flags, move the reject check to a helper that is invoked once by fib6_nh_init to reset the device and then again in ip6_route_info_create to set the fib6_flags. Signed-off-by: David Ahern <dsahern@gmail.com> Reviewed-by: Ido Schimmel <idosch@mellanox.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent faa041a commit 83c4425

2 files changed

Lines changed: 145 additions & 108 deletions

File tree

‎include/net/ip6_fib.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ static inline struct net_device *fib6_info_nh_dev(const struct fib6_info *f6i)
444444
return f6i->fib6_nh.nh_dev;
445445
}
446446

447+
int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
448+
struct fib6_config *cfg, gfp_t gfp_flags,
449+
struct netlink_ext_ack *extack);
450+
447451
static inline
448452
struct lwtunnel_state *fib6_info_nh_lwt(const struct fib6_info *f6i)
449453
{

‎net/ipv6/route.c‎

Lines changed: 141 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,17 +2898,142 @@ static int ip6_validate_gw(struct net *net, struct fib6_config *cfg,
28982898
return err;
28992899
}
29002900

2901+
static bool fib6_is_reject(u32 flags, struct net_device *dev, int addr_type)
2902+
{
2903+
if ((flags & RTF_REJECT) ||
2904+
(dev && (dev->flags & IFF_LOOPBACK) &&
2905+
!(addr_type & IPV6_ADDR_LOOPBACK) &&
2906+
!(flags & RTF_LOCAL)))
2907+
return true;
2908+
2909+
return false;
2910+
}
2911+
2912+
int fib6_nh_init(struct net *net, struct fib6_nh *fib6_nh,
2913+
struct fib6_config *cfg, gfp_t gfp_flags,
2914+
struct netlink_ext_ack *extack)
2915+
{
2916+
struct net_device *dev = NULL;
2917+
struct inet6_dev *idev = NULL;
2918+
int addr_type;
2919+
int err;
2920+
2921+
err = -ENODEV;
2922+
if (cfg->fc_ifindex) {
2923+
dev = dev_get_by_index(net, cfg->fc_ifindex);
2924+
if (!dev)
2925+
goto out;
2926+
idev = in6_dev_get(dev);
2927+
if (!idev)
2928+
goto out;
2929+
}
2930+
2931+
if (cfg->fc_flags & RTNH_F_ONLINK) {
2932+
if (!dev) {
2933+
NL_SET_ERR_MSG(extack,
2934+
"Nexthop device required for onlink");
2935+
goto out;
2936+
}
2937+
2938+
if (!(dev->flags & IFF_UP)) {
2939+
NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2940+
err = -ENETDOWN;
2941+
goto out;
2942+
}
2943+
2944+
fib6_nh->nh_flags |= RTNH_F_ONLINK;
2945+
}
2946+
2947+
if (cfg->fc_encap) {
2948+
struct lwtunnel_state *lwtstate;
2949+
2950+
err = lwtunnel_build_state(cfg->fc_encap_type,
2951+
cfg->fc_encap, AF_INET6, cfg,
2952+
&lwtstate, extack);
2953+
if (err)
2954+
goto out;
2955+
2956+
fib6_nh->nh_lwtstate = lwtstate_get(lwtstate);
2957+
}
2958+
2959+
fib6_nh->nh_weight = 1;
2960+
2961+
/* We cannot add true routes via loopback here,
2962+
* they would result in kernel looping; promote them to reject routes
2963+
*/
2964+
addr_type = ipv6_addr_type(&cfg->fc_dst);
2965+
if (fib6_is_reject(cfg->fc_flags, dev, addr_type)) {
2966+
/* hold loopback dev/idev if we haven't done so. */
2967+
if (dev != net->loopback_dev) {
2968+
if (dev) {
2969+
dev_put(dev);
2970+
in6_dev_put(idev);
2971+
}
2972+
dev = net->loopback_dev;
2973+
dev_hold(dev);
2974+
idev = in6_dev_get(dev);
2975+
if (!idev) {
2976+
err = -ENODEV;
2977+
goto out;
2978+
}
2979+
}
2980+
goto set_dev;
2981+
}
2982+
2983+
if (cfg->fc_flags & RTF_GATEWAY) {
2984+
err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
2985+
if (err)
2986+
goto out;
2987+
2988+
fib6_nh->nh_gw = cfg->fc_gateway;
2989+
}
2990+
2991+
err = -ENODEV;
2992+
if (!dev)
2993+
goto out;
2994+
2995+
if (idev->cnf.disable_ipv6) {
2996+
NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
2997+
err = -EACCES;
2998+
goto out;
2999+
}
3000+
3001+
if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3002+
NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3003+
err = -ENETDOWN;
3004+
goto out;
3005+
}
3006+
3007+
if (!(cfg->fc_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3008+
!netif_carrier_ok(dev))
3009+
fib6_nh->nh_flags |= RTNH_F_LINKDOWN;
3010+
3011+
set_dev:
3012+
fib6_nh->nh_dev = dev;
3013+
err = 0;
3014+
out:
3015+
if (idev)
3016+
in6_dev_put(idev);
3017+
3018+
if (err) {
3019+
lwtstate_put(fib6_nh->nh_lwtstate);
3020+
fib6_nh->nh_lwtstate = NULL;
3021+
if (dev)
3022+
dev_put(dev);
3023+
}
3024+
3025+
return err;
3026+
}
3027+
29013028
static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
29023029
gfp_t gfp_flags,
29033030
struct netlink_ext_ack *extack)
29043031
{
29053032
struct net *net = cfg->fc_nlinfo.nl_net;
29063033
struct fib6_info *rt = NULL;
2907-
struct net_device *dev = NULL;
2908-
struct inet6_dev *idev = NULL;
29093034
struct fib6_table *table;
2910-
int addr_type;
29113035
int err = -EINVAL;
3036+
int addr_type;
29123037

29133038
/* RTF_PCPU is an internal flag; can not be set by userspace */
29143039
if (cfg->fc_flags & RTF_PCPU) {
@@ -2942,30 +3067,6 @@ static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
29423067
goto out;
29433068
}
29443069
#endif
2945-
if (cfg->fc_ifindex) {
2946-
err = -ENODEV;
2947-
dev = dev_get_by_index(net, cfg->fc_ifindex);
2948-
if (!dev)
2949-
goto out;
2950-
idev = in6_dev_get(dev);
2951-
if (!idev)
2952-
goto out;
2953-
}
2954-
2955-
if (cfg->fc_flags & RTNH_F_ONLINK) {
2956-
if (!dev) {
2957-
NL_SET_ERR_MSG(extack,
2958-
"Nexthop device required for onlink");
2959-
err = -ENODEV;
2960-
goto out;
2961-
}
2962-
2963-
if (!(dev->flags & IFF_UP)) {
2964-
NL_SET_ERR_MSG(extack, "Nexthop device is not up");
2965-
err = -ENETDOWN;
2966-
goto out;
2967-
}
2968-
}
29693070

29703071
err = -ENOBUFS;
29713072
if (cfg->fc_nlinfo.nlh &&
@@ -3009,18 +3110,10 @@ static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
30093110
cfg->fc_protocol = RTPROT_BOOT;
30103111
rt->fib6_protocol = cfg->fc_protocol;
30113112

3012-
addr_type = ipv6_addr_type(&cfg->fc_dst);
3013-
3014-
if (cfg->fc_encap) {
3015-
struct lwtunnel_state *lwtstate;
3016-
3017-
err = lwtunnel_build_state(cfg->fc_encap_type,
3018-
cfg->fc_encap, AF_INET6, cfg,
3019-
&lwtstate, extack);
3020-
if (err)
3021-
goto out;
3022-
rt->fib6_nh.nh_lwtstate = lwtstate_get(lwtstate);
3023-
}
3113+
rt->fib6_table = table;
3114+
rt->fib6_metric = cfg->fc_metric;
3115+
rt->fib6_type = cfg->fc_type;
3116+
rt->fib6_flags = cfg->fc_flags;
30243117

30253118
ipv6_addr_prefix(&rt->fib6_dst.addr, &cfg->fc_dst, cfg->fc_dst_len);
30263119
rt->fib6_dst.plen = cfg->fc_dst_len;
@@ -3031,62 +3124,20 @@ static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
30313124
ipv6_addr_prefix(&rt->fib6_src.addr, &cfg->fc_src, cfg->fc_src_len);
30323125
rt->fib6_src.plen = cfg->fc_src_len;
30333126
#endif
3034-
3035-
rt->fib6_metric = cfg->fc_metric;
3036-
rt->fib6_nh.nh_weight = 1;
3037-
3038-
rt->fib6_type = cfg->fc_type;
3127+
err = fib6_nh_init(net, &rt->fib6_nh, cfg, gfp_flags, extack);
3128+
if (err)
3129+
goto out;
30393130

30403131
/* We cannot add true routes via loopback here,
3041-
they would result in kernel looping; promote them to reject routes
3132+
* they would result in kernel looping; promote them to reject routes
30423133
*/
3043-
if ((cfg->fc_flags & RTF_REJECT) ||
3044-
(dev && (dev->flags & IFF_LOOPBACK) &&
3045-
!(addr_type & IPV6_ADDR_LOOPBACK) &&
3046-
!(cfg->fc_flags & RTF_LOCAL))) {
3047-
/* hold loopback dev/idev if we haven't done so. */
3048-
if (dev != net->loopback_dev) {
3049-
if (dev) {
3050-
dev_put(dev);
3051-
in6_dev_put(idev);
3052-
}
3053-
dev = net->loopback_dev;
3054-
dev_hold(dev);
3055-
idev = in6_dev_get(dev);
3056-
if (!idev) {
3057-
err = -ENODEV;
3058-
goto out;
3059-
}
3060-
}
3061-
rt->fib6_flags = RTF_REJECT|RTF_NONEXTHOP;
3062-
goto install_route;
3063-
}
3064-
3065-
if (cfg->fc_flags & RTF_GATEWAY) {
3066-
err = ip6_validate_gw(net, cfg, &dev, &idev, extack);
3067-
if (err)
3068-
goto out;
3069-
3070-
rt->fib6_nh.nh_gw = cfg->fc_gateway;
3071-
}
3072-
3073-
err = -ENODEV;
3074-
if (!dev)
3075-
goto out;
3076-
3077-
if (idev->cnf.disable_ipv6) {
3078-
NL_SET_ERR_MSG(extack, "IPv6 is disabled on nexthop device");
3079-
err = -EACCES;
3080-
goto out;
3081-
}
3082-
3083-
if (!(dev->flags & IFF_UP) && !cfg->fc_ignore_dev_down) {
3084-
NL_SET_ERR_MSG(extack, "Nexthop device is not up");
3085-
err = -ENETDOWN;
3086-
goto out;
3087-
}
3134+
addr_type = ipv6_addr_type(&cfg->fc_dst);
3135+
if (fib6_is_reject(cfg->fc_flags, rt->fib6_nh.nh_dev, addr_type))
3136+
rt->fib6_flags = RTF_REJECT | RTF_NONEXTHOP;
30883137

30893138
if (!ipv6_addr_any(&cfg->fc_prefsrc)) {
3139+
struct net_device *dev = fib6_info_nh_dev(rt);
3140+
30903141
if (!ipv6_chk_addr(net, &cfg->fc_prefsrc, dev, 0)) {
30913142
NL_SET_ERR_MSG(extack, "Invalid source address");
30923143
err = -EINVAL;
@@ -3097,26 +3148,8 @@ static struct fib6_info *ip6_route_info_create(struct fib6_config *cfg,
30973148
} else
30983149
rt->fib6_prefsrc.plen = 0;
30993150

3100-
rt->fib6_flags = cfg->fc_flags;
3101-
3102-
install_route:
3103-
if (!(rt->fib6_flags & (RTF_LOCAL | RTF_ANYCAST)) &&
3104-
!netif_carrier_ok(dev))
3105-
rt->fib6_nh.nh_flags |= RTNH_F_LINKDOWN;
3106-
rt->fib6_nh.nh_flags |= (cfg->fc_flags & RTNH_F_ONLINK);
3107-
rt->fib6_nh.nh_dev = dev;
3108-
rt->fib6_table = table;
3109-
3110-
if (idev)
3111-
in6_dev_put(idev);
3112-
31133151
return rt;
31143152
out:
3115-
if (dev)
3116-
dev_put(dev);
3117-
if (idev)
3118-
in6_dev_put(idev);
3119-
31203153
fib6_info_release(rt);
31213154
return ERR_PTR(err);
31223155
}

0 commit comments

Comments
 (0)