From fb72849089f496d4fbbfaead90943e181bce0ecd Mon Sep 17 00:00:00 2001 From: Zhe Weng Date: Wed, 6 Dec 2023 16:00:01 +0800 Subject: [PATCH] nshlib/ifconfig: Set network mask before setting gateway Switch the order of setting network mask and gateway, re-order only, no logic change. In IPv6 cases, we may set `addr6` and prefix length together to an interface, but the gateway logic may change the value in `addr6`, and result in wrong address set to the interface. The order doesn't change behavior in network stack, so we just need to make sure the value is correct. Signed-off-by: Zhe Weng --- nshlib/nsh_netcmds.c | 96 ++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/nshlib/nsh_netcmds.c b/nshlib/nsh_netcmds.c index ddd2b7ea8..cb69bcf1c 100644 --- a/nshlib/nsh_netcmds.c +++ b/nshlib/nsh_netcmds.c @@ -855,54 +855,6 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } #endif /* CONFIG_NET_IPv4 */ - /* Set gateway */ - -#ifdef CONFIG_NET_IPv6 -#ifdef CONFIG_NET_IPv4 - if (inet6) -#endif - { - /* Only set the gateway address if it was explicitly provided. */ - - if (gwip != NULL) - { - ninfo("Gateway: %s\n", gwip); - inet_pton(AF_INET6, gwip, &addr6); - - netlib_set_dripv6addr(ifname, &addr6); - gip6 = addr6; - } - } -#endif /* CONFIG_NET_IPv6 */ - -#ifdef CONFIG_NET_IPv4 -#ifdef CONFIG_NET_IPv6 - else -#endif - { - if (gwip != NULL) - { - ninfo("Gateway: %s\n", gwip); - gip = addr.s_addr = inet_addr(gwip); - } - else - { - if (gip != 0) - { - ninfo("Gateway: default\n"); - gip = NTOHL(gip); - gip &= ~0x000000ff; - gip |= 0x00000001; - gip = HTONL(gip); - } - - addr.s_addr = gip; - } - - netlib_set_dripv4addr(ifname, &addr); - } -#endif /* CONFIG_NET_IPv4 */ - /* Set network mask */ #ifdef CONFIG_NET_IPv6 @@ -975,6 +927,54 @@ int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) } #endif /* CONFIG_NET_IPv4 */ + /* Set gateway */ + +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + if (inet6) +#endif + { + /* Only set the gateway address if it was explicitly provided. */ + + if (gwip != NULL) + { + ninfo("Gateway: %s\n", gwip); + inet_pton(AF_INET6, gwip, &addr6); + + netlib_set_dripv6addr(ifname, &addr6); + gip6 = addr6; + } + } +#endif /* CONFIG_NET_IPv6 */ + +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + else +#endif + { + if (gwip != NULL) + { + ninfo("Gateway: %s\n", gwip); + gip = addr.s_addr = inet_addr(gwip); + } + else + { + if (gip != 0) + { + ninfo("Gateway: default\n"); + gip = NTOHL(gip); + gip &= ~0x000000ff; + gip |= 0x00000001; + gip = HTONL(gip); + } + + addr.s_addr = gip; + } + + netlib_set_dripv4addr(ifname, &addr); + } +#endif /* CONFIG_NET_IPv4 */ + UNUSED(ifname); /* Not used in all configurations */ #if defined(CONFIG_NETINIT_DHCPC) || defined(CONFIG_NETINIT_DNS)