On 04.07.2025 08:00, Kuniyuki Iwashima wrote:
From: Gabriel Goller <[email protected]>
Date: Thu,  3 Jul 2025 18:01:53 +0200
diff --git a/Documentation/networking/ip-sysctl.rst 
b/Documentation/networking/ip-sysctl.rst
index 0f1251cce314..ec7fa1e890f1 100644
--- a/Documentation/networking/ip-sysctl.rst
+++ b/Documentation/networking/ip-sysctl.rst
@@ -2292,6 +2292,11 @@ conf/all/forwarding - BOOLEAN
 proxy_ndp - BOOLEAN
        Do proxy ndp.

+force_forwarding - BOOLEAN
+       Enable forwarding on this interface only -- regardless of the setting on
+       ``conf/all/forwarding``. When setting ``conf.all.forwarding`` to 0,
+       the ``force_forwarding`` flag will be reset on all interfaces.

Please update conf/all/forwarding too as it will be stale after
this patch.

I'll change the conf/all/forwarding section from:

        IPv4 and IPv6 work differently here; e.g. netfilter must be used
        to control which interfaces may forward packets and which not.

to:

        IPv4 and IPv6 work differently here; the ``force_forwarding`` flag must
        be used to control which interfaces may forward packets.

I hope that's all right.

+
 fwmark_reflect - BOOLEAN
        Controls the fwmark of kernel-generated IPv6 reply packets that are not
        associated with a socket for example, TCP RSTs or ICMPv6 echo replies).
diff --git a/include/linux/ipv6.h b/include/linux/ipv6.h
index 5aeeed22f35b..5380107e466c 100644
--- a/include/linux/ipv6.h
+++ b/include/linux/ipv6.h
@@ -19,6 +19,7 @@ struct ipv6_devconf {
        __s32           forwarding;
        __s32           disable_policy;
        __s32           proxy_ndp;
+       __s32           force_forwarding;

nit: place force_forwarding just after forwarding.

Agree.

        __cacheline_group_end(ipv6_devconf_read_txrx);

        __s32           accept_ra;
@@ -857,6 +859,15 @@ static void addrconf_forward_change(struct net *net, __s32 
newf)
                idev = __in6_dev_get_rtnl_net(dev);
                if (idev) {
                        int changed = (!idev->cnf.forwarding) ^ (!newf);
+                       /*
+                        * With the introduction of force_forwarding, we need 
to be backwards
+                        * compatible, so that means we need to set the 
force_forwarding flag

Strictly backward compatibility is not related I think because
the per iface conf is disabled by default, and this is a new
behaviour.  Maybe simply say

/* Disabling all.forwarding sets 0 to force_forwarding for all interfaces */

Agree.

+                        * on every interface to 0 if 
net.ipv6.conf.all.forwarding is set to 0.
+                        * This allows the global forwarding flag to disable 
forwarding for
+                        * all interfaces.
+                        */
+                       if (newf == 0)
+                               WRITE_ONCE(idev->cnf.force_forwarding, newf);

                        WRITE_ONCE(idev->cnf.forwarding, newf);
                        if (changed)
@@ -5719,6 +5730,7 @@ static void ipv6_store_devconf(const struct ipv6_devconf 
*cnf,
        array[DEVCONF_ACCEPT_UNTRACKED_NA] =
                READ_ONCE(cnf->accept_untracked_na);
        array[DEVCONF_ACCEPT_RA_MIN_LFT] = READ_ONCE(cnf->accept_ra_min_lft);
+       array[DEVCONF_FORCE_FORWARDING] = READ_ONCE(cnf->force_forwarding);
 }

[...]
 static inline size_t inet6_ifla6_size(void)
@@ -6747,6 +6759,78 @@ static int addrconf_sysctl_disable_policy(const struct 
ctl_table *ctl, int write
        return ret;
 }

+static void addrconf_force_forward_change(struct net *net, __s32 newf)
+{
+       ASSERT_RTNL();

__in6_dev_get_rtnl_net() has the same check so this is not needed.

Agree.

+       struct net_device *dev;
+       struct inet6_dev *idev;
+
+       for_each_netdev(net, dev) {
+               idev = __in6_dev_get_rtnl_net(dev);
+               if (idev) {
+                       int changed = (!idev->cnf.force_forwarding) ^ (!newf);
+
+                       WRITE_ONCE(idev->cnf.force_forwarding, newf);
+                       if (changed) {
+                               inet6_netconf_notify_devconf(dev_net(dev), 
RTM_NEWNETCONF,
+                                                            
NETCONFA_FORCE_FORWARDING,
+                                                            dev->ifindex, 
&idev->cnf);
+                       }
+               }
+       }
+}
+
+static int addrconf_sysctl_force_forwarding(const struct ctl_table *ctl, int 
write,
+                                           void *buffer, size_t *lenp, loff_t 
*ppos)
+{
+       struct inet6_dev *idev = ctl->extra1;
+       struct net *net = ctl->extra2;
+       int *valp = ctl->data;
+       loff_t pos = *ppos;

nit: Please keep the reverse xmas tree order.
https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html#local-variable-ordering-reverse-xmas-tree-rcs

+       int new_val = *valp;
+       int old_val = *valp;
+       int ret;
+
+       struct ctl_table tmp_ctl = *ctl;

same here.

Will do; moved this up.

+
+       tmp_ctl.extra1 = SYSCTL_ZERO;
+       tmp_ctl.extra2 = SYSCTL_ONE;

As you are copying *ctl, please specify this in addrconf_sysctl[].

Umm how would I do that? Do you want me to add a comment explaining it?
I need extra1 and extra2 to be the network device so that I can set
NETCONFA_FORCE_FORWARDING but I also want to use proc_douintvec_minmax.

+       tmp_ctl.data = &new_val;
+
+       ret = proc_douintvec_minmax(&tmp_ctl, write, buffer, lenp, ppos);
+
+       if (write && old_val != new_val) {
+               if (!rtnl_net_trylock(net))
+                       return restart_syscall();
+
+               if (valp == &net->ipv6.devconf_dflt->force_forwarding) {
+                       inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
+                                                    NETCONFA_FORCE_FORWARDING,
+                                                    NETCONFA_IFINDEX_DEFAULT,
+                                                    net->ipv6.devconf_dflt);
+               } else if (valp == &net->ipv6.devconf_all->force_forwarding) {
+                       inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
+                                                    NETCONFA_FORCE_FORWARDING,
+                                                    NETCONFA_IFINDEX_ALL,
+                                                    net->ipv6.devconf_all);
+
+                       addrconf_force_forward_change(net, new_val);
+               } else {
+                       inet6_netconf_notify_devconf(net, RTM_NEWNETCONF,
+                                                    NETCONFA_FORCE_FORWARDING,
+                                                    idev->dev->ifindex,
+                                                    &idev->cnf);
+               }
+               rtnl_net_unlock(net);
+       }
+
+       if (write)
+               WRITE_ONCE(*valp, new_val);
+       if (ret)
+               *ppos = pos;
+       return ret;
+}
+
 static int minus_one = -1;
 static const int two_five_five = 255;
 static u32 ioam6_if_id_max = U16_MAX;
@@ -7217,6 +7301,13 @@ static const struct ctl_table addrconf_sysctl[] = {
                .extra1         = SYSCTL_ZERO,
                .extra2         = SYSCTL_TWO,
        },
+       {
+               .procname       = "force_forwarding",
+               .data           = &ipv6_devconf.force_forwarding,
+               .maxlen         = sizeof(int),
+               .mode           = 0644,
+               .proc_handler   = addrconf_sysctl_force_forwarding,

Here for extra{1,2}.

See above.

+       },
 };
 static int __addrconf_sysctl_register(struct net *net, char *dev_name,
diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c
index 7bd29a9ff0db..440b9efced72 100644
--- a/net/ipv6/ip6_output.c
+++ b/net/ipv6/ip6_output.c
@@ -509,7 +509,8 @@ int ip6_forward(struct sk_buff *skb)
        u32 mtu;

        idev = __in6_dev_get_safely(dev_get_by_index_rcu(net, IP6CB(skb)->iif));
-       if (READ_ONCE(net->ipv6.devconf_all->forwarding) == 0)
+       if (idev && !READ_ONCE(idev->cnf.force_forwarding) &&
+           !READ_ONCE(net->ipv6.devconf_all->forwarding))

Now this ignores devconf_all when !idev whose dev was not
found or has not had a valid mtu.

if (!READ_ONCE(net->ipv6.devconf_all->forwarding) &&
   (!idev || !READ_ONCE(idev->cnf.force_forwarding)))

Oof yeah, you're right this is my mistake. I'll use your solution.

Thanks for the review!


Reply via email to