IPv6: add extack info for IPv6 address add/delete

Add extack info for IPv6 address add/delete, which would be useful for
users to understand the problem without having to read kernel code.

Suggested-by: Beniamino Galvani <bgalvani@redhat.com>
Reviewed-by: Ido Schimmel <idosch@nvidia.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Reviewed-by: Simon Horman <simon.horman@corigine.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Hangbin Liu 2023-07-26 10:39:05 +08:00 коммит произвёл David S. Miller
Родитель 92af463d68
Коммит 7f6c40391a
4 изменённых файлов: 59 добавлений и 27 удалений

Просмотреть файл

@ -156,7 +156,7 @@ void fib6_force_start_gc(struct net *net);
struct fib6_info *addrconf_f6i_alloc(struct net *net, struct inet6_dev *idev, struct fib6_info *addrconf_f6i_alloc(struct net *net, struct inet6_dev *idev,
const struct in6_addr *addr, bool anycast, const struct in6_addr *addr, bool anycast,
gfp_t gfp_flags); gfp_t gfp_flags, struct netlink_ext_ack *extack);
struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev, struct rt6_info *ip6_dst_alloc(struct net *net, struct net_device *dev,
int flags); int flags);

Просмотреть файл

@ -1063,20 +1063,28 @@ ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
struct fib6_info *f6i = NULL; struct fib6_info *f6i = NULL;
int err = 0; int err = 0;
if (addr_type == IPV6_ADDR_ANY || if (addr_type == IPV6_ADDR_ANY) {
(addr_type & IPV6_ADDR_MULTICAST && NL_SET_ERR_MSG_MOD(extack, "Invalid address");
!(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) ||
(!(idev->dev->flags & IFF_LOOPBACK) &&
!netif_is_l3_master(idev->dev) &&
addr_type & IPV6_ADDR_LOOPBACK))
return ERR_PTR(-EADDRNOTAVAIL); return ERR_PTR(-EADDRNOTAVAIL);
} else if (addr_type & IPV6_ADDR_MULTICAST &&
!(cfg->ifa_flags & IFA_F_MCAUTOJOIN)) {
NL_SET_ERR_MSG_MOD(extack, "Cannot assign multicast address without \"IFA_F_MCAUTOJOIN\" flag");
return ERR_PTR(-EADDRNOTAVAIL);
} else if (!(idev->dev->flags & IFF_LOOPBACK) &&
!netif_is_l3_master(idev->dev) &&
addr_type & IPV6_ADDR_LOOPBACK) {
NL_SET_ERR_MSG_MOD(extack, "Cannot assign loopback address on this device");
return ERR_PTR(-EADDRNOTAVAIL);
}
if (idev->dead) { if (idev->dead) {
err = -ENODEV; /*XXX*/ NL_SET_ERR_MSG_MOD(extack, "device is going away");
err = -ENODEV;
goto out; goto out;
} }
if (idev->cnf.disable_ipv6) { if (idev->cnf.disable_ipv6) {
NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
err = -EACCES; err = -EACCES;
goto out; goto out;
} }
@ -1103,7 +1111,7 @@ ipv6_add_addr(struct inet6_dev *idev, struct ifa6_config *cfg,
goto out; goto out;
} }
f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags); f6i = addrconf_f6i_alloc(net, idev, cfg->pfx, false, gfp_flags, extack);
if (IS_ERR(f6i)) { if (IS_ERR(f6i)) {
err = PTR_ERR(f6i); err = PTR_ERR(f6i);
f6i = NULL; f6i = NULL;
@ -2927,30 +2935,40 @@ static int inet6_addr_add(struct net *net, int ifindex,
ASSERT_RTNL(); ASSERT_RTNL();
if (cfg->plen > 128) if (cfg->plen > 128) {
NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
return -EINVAL; return -EINVAL;
}
/* check the lifetime */ /* check the lifetime */
if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) if (!cfg->valid_lft || cfg->preferred_lft > cfg->valid_lft) {
NL_SET_ERR_MSG_MOD(extack, "address lifetime invalid");
return -EINVAL; return -EINVAL;
}
if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) if (cfg->ifa_flags & IFA_F_MANAGETEMPADDR && cfg->plen != 64) {
NL_SET_ERR_MSG_MOD(extack, "address with \"mngtmpaddr\" flag must have a prefix length of 64");
return -EINVAL; return -EINVAL;
}
dev = __dev_get_by_index(net, ifindex); dev = __dev_get_by_index(net, ifindex);
if (!dev) if (!dev)
return -ENODEV; return -ENODEV;
idev = addrconf_add_dev(dev); idev = addrconf_add_dev(dev);
if (IS_ERR(idev)) if (IS_ERR(idev)) {
NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
return PTR_ERR(idev); return PTR_ERR(idev);
}
if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) { if (cfg->ifa_flags & IFA_F_MCAUTOJOIN) {
int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk, int ret = ipv6_mc_config(net->ipv6.mc_autojoin_sk,
true, cfg->pfx, ifindex); true, cfg->pfx, ifindex);
if (ret < 0) if (ret < 0) {
NL_SET_ERR_MSG_MOD(extack, "Multicast auto join failed");
return ret; return ret;
}
} }
cfg->scope = ipv6_addr_scope(cfg->pfx); cfg->scope = ipv6_addr_scope(cfg->pfx);
@ -3007,22 +3025,29 @@ static int inet6_addr_add(struct net *net, int ifindex,
} }
static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags, static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
const struct in6_addr *pfx, unsigned int plen) const struct in6_addr *pfx, unsigned int plen,
struct netlink_ext_ack *extack)
{ {
struct inet6_ifaddr *ifp; struct inet6_ifaddr *ifp;
struct inet6_dev *idev; struct inet6_dev *idev;
struct net_device *dev; struct net_device *dev;
if (plen > 128) if (plen > 128) {
NL_SET_ERR_MSG_MOD(extack, "Invalid prefix length");
return -EINVAL; return -EINVAL;
}
dev = __dev_get_by_index(net, ifindex); dev = __dev_get_by_index(net, ifindex);
if (!dev) if (!dev) {
NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
return -ENODEV; return -ENODEV;
}
idev = __in6_dev_get(dev); idev = __in6_dev_get(dev);
if (!idev) if (!idev) {
NL_SET_ERR_MSG_MOD(extack, "IPv6 is disabled on this device");
return -ENXIO; return -ENXIO;
}
read_lock_bh(&idev->lock); read_lock_bh(&idev->lock);
list_for_each_entry(ifp, &idev->addr_list, if_list) { list_for_each_entry(ifp, &idev->addr_list, if_list) {
@ -3045,6 +3070,8 @@ static int inet6_addr_del(struct net *net, int ifindex, u32 ifa_flags,
} }
} }
read_unlock_bh(&idev->lock); read_unlock_bh(&idev->lock);
NL_SET_ERR_MSG_MOD(extack, "address not found");
return -EADDRNOTAVAIL; return -EADDRNOTAVAIL;
} }
@ -3087,7 +3114,7 @@ int addrconf_del_ifaddr(struct net *net, void __user *arg)
rtnl_lock(); rtnl_lock();
err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr, err = inet6_addr_del(net, ireq.ifr6_ifindex, 0, &ireq.ifr6_addr,
ireq.ifr6_prefixlen); ireq.ifr6_prefixlen, NULL);
rtnl_unlock(); rtnl_unlock();
return err; return err;
} }
@ -3490,7 +3517,7 @@ static int fixup_permanent_addr(struct net *net,
struct fib6_info *f6i, *prev; struct fib6_info *f6i, *prev;
f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false, f6i = addrconf_f6i_alloc(net, idev, &ifp->addr, false,
GFP_ATOMIC); GFP_ATOMIC, NULL);
if (IS_ERR(f6i)) if (IS_ERR(f6i))
return PTR_ERR(f6i); return PTR_ERR(f6i);
@ -4700,7 +4727,7 @@ inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh,
ifa_flags &= IFA_F_MANAGETEMPADDR; ifa_flags &= IFA_F_MANAGETEMPADDR;
return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx, return inet6_addr_del(net, ifm->ifa_index, ifa_flags, pfx,
ifm->ifa_prefixlen); ifm->ifa_prefixlen, extack);
} }
static int modify_prefix_route(struct inet6_ifaddr *ifp, static int modify_prefix_route(struct inet6_ifaddr *ifp,
@ -4905,8 +4932,10 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
} }
dev = __dev_get_by_index(net, ifm->ifa_index); dev = __dev_get_by_index(net, ifm->ifa_index);
if (!dev) if (!dev) {
NL_SET_ERR_MSG_MOD(extack, "Unable to find the interface");
return -ENODEV; return -ENODEV;
}
if (tb[IFA_FLAGS]) if (tb[IFA_FLAGS])
cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]); cfg.ifa_flags = nla_get_u32(tb[IFA_FLAGS]);
@ -4941,10 +4970,12 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh,
} }
if (nlh->nlmsg_flags & NLM_F_EXCL || if (nlh->nlmsg_flags & NLM_F_EXCL ||
!(nlh->nlmsg_flags & NLM_F_REPLACE)) !(nlh->nlmsg_flags & NLM_F_REPLACE)) {
NL_SET_ERR_MSG_MOD(extack, "address already assigned");
err = -EEXIST; err = -EEXIST;
else } else {
err = inet6_addr_modify(net, ifa, &cfg); err = inet6_addr_modify(net, ifa, &cfg);
}
in6_ifa_put(ifa); in6_ifa_put(ifa);

Просмотреть файл

@ -305,7 +305,7 @@ int __ipv6_dev_ac_inc(struct inet6_dev *idev, const struct in6_addr *addr)
} }
net = dev_net(idev->dev); net = dev_net(idev->dev);
f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC); f6i = addrconf_f6i_alloc(net, idev, addr, true, GFP_ATOMIC, NULL);
if (IS_ERR(f6i)) { if (IS_ERR(f6i)) {
err = PTR_ERR(f6i); err = PTR_ERR(f6i);
goto out; goto out;

Просмотреть файл

@ -4543,7 +4543,8 @@ static int ip6_pkt_prohibit_out(struct net *net, struct sock *sk, struct sk_buff
struct fib6_info *addrconf_f6i_alloc(struct net *net, struct fib6_info *addrconf_f6i_alloc(struct net *net,
struct inet6_dev *idev, struct inet6_dev *idev,
const struct in6_addr *addr, const struct in6_addr *addr,
bool anycast, gfp_t gfp_flags) bool anycast, gfp_t gfp_flags,
struct netlink_ext_ack *extack)
{ {
struct fib6_config cfg = { struct fib6_config cfg = {
.fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL, .fc_table = l3mdev_fib_table(idev->dev) ? : RT6_TABLE_LOCAL,
@ -4565,7 +4566,7 @@ struct fib6_info *addrconf_f6i_alloc(struct net *net,
cfg.fc_flags |= RTF_LOCAL; cfg.fc_flags |= RTF_LOCAL;
} }
f6i = ip6_route_info_create(&cfg, gfp_flags, NULL); f6i = ip6_route_info_create(&cfg, gfp_flags, extack);
if (!IS_ERR(f6i)) { if (!IS_ERR(f6i)) {
f6i->dst_nocount = true; f6i->dst_nocount = true;