3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-10-04 02:18:49 +02:00

netconfig: Store rtm_protocol value directly

Instead of using a flag ipv4_static, just store the value of the rtm
protocol directly inside netconfig object.  This allows us to simplify
the logic quite significantly and avoid repeating the conditional
expression needlessly
This commit is contained in:
Denis Kenzior 2019-07-31 04:38:54 -05:00
parent 2eded67c62
commit 1fd0dbb74b

View File

@ -46,7 +46,7 @@ struct netconfig {
enum station_state station_state; enum station_state station_state;
struct l_dhcp_client *dhcp_client; struct l_dhcp_client *dhcp_client;
struct l_queue *ifaddr_list; struct l_queue *ifaddr_list;
bool ipv4_is_static:1; uint8_t rtm_protocol;
}; };
struct netconfig_ifaddr { struct netconfig_ifaddr {
@ -153,8 +153,6 @@ static struct netconfig_ifaddr *netconfig_ipv4_get_ifaddr(
if (!ip) if (!ip)
return NULL; return NULL;
netconfig->ipv4_is_static = true;
ifaddr = l_new(struct netconfig_ifaddr, 1); ifaddr = l_new(struct netconfig_ifaddr, 1);
ifaddr->ip = ip; ifaddr->ip = ip;
@ -204,13 +202,12 @@ static struct netconfig_ifaddr *netconfig_ipv4_get_ifaddr(
return NULL; return NULL;
} }
static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig, static char *netconfig_ipv4_get_gateway(struct netconfig *netconfig)
uint8_t proto)
{ {
const struct l_dhcp_lease *lease; const struct l_dhcp_lease *lease;
const struct l_settings *settings; const struct l_settings *settings;
switch (proto) { switch (netconfig->rtm_protocol) {
case RTPROT_STATIC: case RTPROT_STATIC:
settings = netconfig_get_connected_network_settings(netconfig); settings = netconfig_get_connected_network_settings(netconfig);
if (!settings) if (!settings)
@ -387,7 +384,6 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig,
L_AUTO_FREE_VAR(char *, gateway) = NULL; L_AUTO_FREE_VAR(char *, gateway) = NULL;
struct in_addr in_addr; struct in_addr in_addr;
char *network; char *network;
uint8_t proto;
if (inet_pton(AF_INET, ifaddr->ip, &in_addr) < 1) if (inet_pton(AF_INET, ifaddr->ip, &in_addr) < 1)
return false; return false;
@ -399,11 +395,10 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig,
if (!network) if (!network)
return false; return false;
proto = netconfig->ipv4_is_static ? RTPROT_STATIC : RTPROT_DHCP;
if (!rtnl_route_ipv4_add_connected(rtnl, netconfig->ifindex, if (!rtnl_route_ipv4_add_connected(rtnl, netconfig->ifindex,
ifaddr->prefix_len, network, ifaddr->prefix_len, network,
ifaddr->ip, proto, ifaddr->ip,
netconfig->rtm_protocol,
netconfig_route_cmd_cb, netconfig_route_cmd_cb,
NULL, NULL)) { NULL, NULL)) {
l_error("netconfig: Failed to add subnet route."); l_error("netconfig: Failed to add subnet route.");
@ -411,11 +406,11 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig,
return false; return false;
} }
gateway = netconfig_ipv4_get_gateway(netconfig, proto); gateway = netconfig_ipv4_get_gateway(netconfig);
if (!gateway) { if (!gateway) {
l_error("netconfig: Failed to obtain gateway from %s.", l_error("netconfig: Failed to obtain gateway from %s.",
netconfig->ipv4_is_static ? netconfig->rtm_protocol == RTPROT_STATIC ?
"setting file" : "DHCPv4 lease"); "setting file" : "DHCPv4 lease");
return false; return false;
} }
@ -423,7 +418,8 @@ static bool netconfig_ipv4_routes_install(struct netconfig *netconfig,
if (!rtnl_route_ipv4_add_gateway(rtnl, netconfig->ifindex, gateway, if (!rtnl_route_ipv4_add_gateway(rtnl, netconfig->ifindex, gateway,
ifaddr->ip, ifaddr->ip,
ROUTE_PRIORITY_OFFSET, ROUTE_PRIORITY_OFFSET,
proto, netconfig_route_cmd_cb, netconfig->rtm_protocol,
netconfig_route_cmd_cb,
NULL, NULL)) { NULL, NULL)) {
l_error("netconfig: Failed to add route for: %s gateway.", l_error("netconfig: Failed to add route for: %s gateway.",
gateway); gateway);
@ -448,13 +444,11 @@ static void netconfig_ipv4_ifaddr_add_cmd_cb(int error, uint16_t type,
return; return;
} }
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, ifaddr = netconfig_ipv4_get_ifaddr(netconfig, netconfig->rtm_protocol);
netconfig->ipv4_is_static ?
RTPROT_STATIC : RTPROT_DHCP);
if (!ifaddr) { if (!ifaddr) {
l_error("netconfig: Failed to obtain IP address from %s.", l_error("netconfig: Failed to obtain IP address from %s.",
netconfig->ipv4_is_static ? netconfig->rtm_protocol == RTPROT_STATIC ?
"setting file" : "DHCPv4 lease"); "setting file" : "DHCPv4 lease");
return; return;
} }
@ -464,12 +458,11 @@ static void netconfig_ipv4_ifaddr_add_cmd_cb(int error, uint16_t type,
goto done; goto done;
} }
dns = netconfig_ipv4_get_dns(netconfig, netconfig->ipv4_is_static ? dns = netconfig_ipv4_get_dns(netconfig, netconfig->rtm_protocol);
RTPROT_STATIC : RTPROT_DHCP);
if (!dns) { if (!dns) {
l_error("netconfig: Failed to obtain DNS addresses from %s.", l_error("netconfig: Failed to obtain DNS addresses from %s.",
netconfig->ipv4_is_static ? netconfig->rtm_protocol == RTPROT_STATIC ?
"setting file" : "DHCPv4 lease"); "setting file" : "DHCPv4 lease");
goto done; goto done;
} }
@ -639,12 +632,15 @@ static void netconfig_ipv4_select_and_install(struct netconfig *netconfig)
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_STATIC); ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_STATIC);
if (ifaddr) { if (ifaddr) {
netconfig->rtm_protocol = RTPROT_STATIC;
netconfig_install_address(netconfig, ifaddr); netconfig_install_address(netconfig, ifaddr);
netconfig_ifaddr_destroy(ifaddr); netconfig_ifaddr_destroy(ifaddr);
return; return;
} }
netconfig->rtm_protocol = RTPROT_DHCP;
if (netconfig->station_state == STATION_STATE_ROAMING) { if (netconfig->station_state == STATION_STATE_ROAMING) {
/* /*
* TODO l_dhcp_client to try to request a * TODO l_dhcp_client to try to request a
@ -665,21 +661,12 @@ static void netconfig_ipv4_select_and_uninstall(struct netconfig *netconfig)
{ {
struct netconfig_ifaddr *ifaddr; struct netconfig_ifaddr *ifaddr;
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_STATIC); ifaddr = netconfig_ipv4_get_ifaddr(netconfig, netconfig->rtm_protocol);
if (ifaddr) { if (ifaddr) {
netconfig_uninstall_address(netconfig, ifaddr); netconfig_uninstall_address(netconfig, ifaddr);
netconfig_ifaddr_destroy(ifaddr); netconfig_ifaddr_destroy(ifaddr);
return;
} }
ifaddr = netconfig_ipv4_get_ifaddr(netconfig, RTPROT_DHCP);
if (!ifaddr)
return;
netconfig_uninstall_address(netconfig, ifaddr);
netconfig_ifaddr_destroy(ifaddr);
l_dhcp_client_stop(netconfig->dhcp_client); l_dhcp_client_stop(netconfig->dhcp_client);
} }