netlink: provide an ability to set default extack message
authorLeon Romanovsky <leonro@nvidia.com>
Tue, 31 Jan 2023 13:31:57 +0000 (15:31 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 2 Feb 2023 05:04:09 +0000 (21:04 -0800)
In netdev common pattern, extack pointer is forwarded to the drivers
to be filled with error message. However, the caller can easily
overwrite the filled message.

Instead of adding multiple "if (!extack->_msg)" checks before any
NL_SET_ERR_MSG() call, which appears after call to the driver, let's
add new macro to common code.

[1] https://lore.kernel.org/all/Y9Irgrgf3uxOjwUm@unreal
Reviewed-by: Simon Horman <simon.horman@corigine.com>
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: Leon Romanovsky <leonro@nvidia.com>
Link: https://lore.kernel.org/r/6993fac557a40a1973dfa0095107c3d03d40bec1.1675171790.git.leon@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
include/linux/netlink.h
net/bridge/br_switchdev.c
net/dsa/master.c
net/dsa/slave.c
net/xfrm/xfrm_device.c

index fa4d86d..c43ac76 100644 (file)
@@ -130,6 +130,16 @@ struct netlink_ext_ack {
 #define NL_SET_ERR_MSG_FMT_MOD(extack, fmt, args...)   \
        NL_SET_ERR_MSG_FMT((extack), KBUILD_MODNAME ": " fmt, ##args)
 
+#define NL_SET_ERR_MSG_WEAK(extack, msg) do {          \
+       if ((extack) && !(extack)->_msg)                \
+               NL_SET_ERR_MSG((extack), msg);          \
+} while (0)
+
+#define NL_SET_ERR_MSG_WEAK_MOD(extack, msg) do {      \
+       if ((extack) && !(extack)->_msg)                \
+               NL_SET_ERR_MSG_MOD((extack), msg);      \
+} while (0)
+
 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do { \
        if ((extack)) {                                 \
                (extack)->bad_attr = (attr);            \
index 7eb6fd5..de18e9c 100644 (file)
@@ -104,9 +104,8 @@ int br_switchdev_set_port_flag(struct net_bridge_port *p,
                return 0;
 
        if (err) {
-               if (extack && !extack->_msg)
-                       NL_SET_ERR_MSG_MOD(extack,
-                                          "bridge flag offload is not supported");
+               NL_SET_ERR_MSG_WEAK_MOD(extack,
+                                       "bridge flag offload is not supported");
                return -EOPNOTSUPP;
        }
 
@@ -115,9 +114,8 @@ int br_switchdev_set_port_flag(struct net_bridge_port *p,
 
        err = switchdev_port_attr_set(p->dev, &attr, extack);
        if (err) {
-               if (extack && !extack->_msg)
-                       NL_SET_ERR_MSG_MOD(extack,
-                                          "error setting offload flag on port");
+               NL_SET_ERR_MSG_WEAK_MOD(extack,
+                                       "error setting offload flag on port");
                return err;
        }
 
index 26d9014..1507b8c 100644 (file)
@@ -464,9 +464,7 @@ int dsa_master_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
 
        err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack);
        if (err) {
-               if (extack && !extack->_msg)
-                       NL_SET_ERR_MSG_MOD(extack,
-                                          "CPU port failed to join LAG");
+               NL_SET_ERR_MSG_WEAK_MOD(extack, "CPU port failed to join LAG");
                goto out_master_teardown;
        }
 
index 6014ac3..26c458f 100644 (file)
@@ -2692,9 +2692,7 @@ static int dsa_slave_changeupper(struct net_device *dev,
                        if (!err)
                                dsa_bridge_mtu_normalization(dp);
                        if (err == -EOPNOTSUPP) {
-                               if (extack && !extack->_msg)
-                                       NL_SET_ERR_MSG_MOD(extack,
-                                                          "Offloading not supported");
+                               NL_SET_ERR_MSG_WEAK_MOD(extack, "Offloading not supported");
                                err = 0;
                        }
                        err = notifier_from_errno(err);
index 562b9d9..95f1436 100644 (file)
@@ -325,8 +325,10 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
                 * authors to do not return -EOPNOTSUPP in packet offload mode.
                 */
                WARN_ON(err == -EOPNOTSUPP && is_packet_offload);
-               if (err != -EOPNOTSUPP || is_packet_offload)
+               if (err != -EOPNOTSUPP || is_packet_offload) {
+                       NL_SET_ERR_MSG_WEAK(extack, "Device failed to offload this state");
                        return err;
+               }
        }
 
        return 0;
@@ -388,6 +390,7 @@ int xfrm_dev_policy_add(struct net *net, struct xfrm_policy *xp,
                xdo->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
                xdo->dir = 0;
                netdev_put(dev, &xdo->dev_tracker);
+               NL_SET_ERR_MSG_WEAK(extack, "Device failed to offload this policy");
                return err;
        }