From: Roy Marples Date: Sun, 3 Feb 2013 10:35:59 +0000 (+0000) Subject: Move options away from ipv4 state to the interface. X-Git-Tag: v5.99.6~82 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=875979f614a4d6d1bd843d52b9d75bca2aa38944;p=thirdparty%2Fdhcpcd.git Move options away from ipv4 state to the interface. --- diff --git a/arp.c b/arp.c index 652a2a7f..06fb9722 100644 --- a/arp.c +++ b/arp.c @@ -113,7 +113,7 @@ arp_packet(void *arg) uint8_t *hw_s, *hw_t; ssize_t bytes; struct if_state *state = ifp->state; - struct if_options *opts = state->options; + struct if_options *opts = ifp->options; const char *hwaddr; struct in_addr ina; @@ -254,8 +254,8 @@ arp_probe(void *arg) struct timeval tv; int arping = 0; - if (state->arping_index < state->options->arping_len) { - addr.s_addr = state->options->arping[state->arping_index]; + if (state->arping_index < ifp->options->arping_len) { + addr.s_addr = ifp->options->arping[state->arping_index]; arping = 1; } else if (state->offer) { if (state->offer->yiaddr) @@ -287,7 +287,7 @@ arp_probe(void *arg) tv.tv_usec = 0; if (arping) { state->probes = 0; - if (++state->arping_index < state->options->arping_len) + if (++state->arping_index < ifp->options->arping_len) eloop_timeout_add_tv(&tv, arp_probe, ifp); else eloop_timeout_add_tv(&tv, start_interface, ifp); diff --git a/dhcp.c b/dhcp.c index bd78caf2..6cfbaf11 100644 --- a/dhcp.c +++ b/dhcp.c @@ -681,7 +681,7 @@ route_netmask(uint32_t ip_in) struct rt * get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp) { - struct if_options *ifo = ifp->state->options; + struct if_options *ifo = ifp->options; const uint8_t *p; const uint8_t *e; struct rt *routes = NULL; @@ -825,7 +825,7 @@ make_message(struct dhcp_message **message, size_t len; const char *hp; const struct dhcp_opt *opt; - const struct if_options *ifo = iface->state->options; + const struct if_options *ifo = iface->options; const struct dhcp_lease *lease = &iface->state->lease; dhcp = xzalloc(sizeof (*dhcp)); @@ -1109,7 +1109,7 @@ configure_env(char **env, const char *prefix, const struct dhcp_message *dhcp, char cidr[4]; uint8_t overl = 0; - ifo = ifp->state->options; + ifo = ifp->options; get_option_uint8(&overl, dhcp, DHO_OPTIONSOVERLOADED); if (!env) { @@ -1229,7 +1229,7 @@ dhcp_fallback(void *arg) struct interface *iface; iface = (struct interface *)arg; - select_profile(iface, iface->state->options->fallback); + select_profile(iface, iface->options->fallback); start_interface(iface); } @@ -1238,7 +1238,7 @@ dhcp_xid(const struct interface *ifp) { uint32_t xid; - if (ifp->state->options->options & DHCPCD_XID_HWADDR && + if (ifp->options->options & DHCPCD_XID_HWADDR && ifp->hwlen >= sizeof(xid)) /* The lower bits are probably more unique on the network */ memcpy(&xid, (ifp->hwaddr + ifp->hwlen) - sizeof(xid), @@ -1275,7 +1275,7 @@ send_message(struct interface *iface, int type, void (*callback)(void *)) { struct if_state *state = iface->state; - struct if_options *ifo = state->options; + struct if_options *ifo = iface->options; struct dhcp_message *dhcp; uint8_t *udp; ssize_t len, r; @@ -1399,7 +1399,7 @@ void dhcp_discover(void *arg) { struct interface *iface = arg; - struct if_options *ifo = iface->state->options; + struct if_options *ifo = iface->options; int timeout = ifo->timeout; /* If we're rebooting and we're not daemonised then we need @@ -1530,7 +1530,7 @@ dhcp_bind(void *arg) { struct interface *iface = arg; struct if_state *state = iface->state; - struct if_options *ifo = state->options; + struct if_options *ifo = iface->options; struct dhcp_lease *lease = &state->lease; struct timeval tv; @@ -1679,7 +1679,7 @@ handle_3rdparty(struct interface *iface) struct if_options *ifo; struct in_addr addr, net, dst; - ifo = iface->state->options; + ifo = iface->options; if (ifo->req_addr.s_addr != INADDR_ANY) return 0; @@ -1702,7 +1702,7 @@ dhcp_static(struct interface *iface) if (handle_3rdparty(iface)) return; - ifo = iface->state->options; + ifo = iface->options; iface->state->offer = dhcp_message_new(&ifo->req_addr, &ifo->req_mask); eloop_timeout_delete(NULL, iface); @@ -1717,10 +1717,10 @@ dhcp_inform(struct interface *iface) return; if (options & DHCPCD_TEST) { - iface->addr.s_addr = iface->state->options->req_addr.s_addr; - iface->net.s_addr = iface->state->options->req_mask.s_addr; + iface->addr.s_addr = iface->options->req_addr.s_addr; + iface->net.s_addr = iface->options->req_mask.s_addr; } else { - iface->state->options->options |= DHCPCD_STATIC; + iface->options->options |= DHCPCD_STATIC; dhcp_static(iface); } @@ -1732,7 +1732,7 @@ dhcp_inform(struct interface *iface) static void dhcp_reboot(struct interface *iface) { - struct if_options *ifo = iface->state->options; + struct if_options *ifo = iface->options; if (ifo->options & DHCPCD_LINK && iface->carrier == LINK_DOWN) { syslog(LOG_INFO, "%s: waiting for carrier", iface->name); @@ -1862,7 +1862,7 @@ dhcp_handle(struct interface *iface, struct dhcp_message **dhcpp, const struct in_addr *from) { struct if_state *state = iface->state; - struct if_options *ifo = state->options; + struct if_options *ifo = iface->options; struct dhcp_message *dhcp = *dhcpp; struct dhcp_lease *lease = &state->lease; uint8_t type, tmp; @@ -2044,14 +2044,14 @@ dhcp_handlepacket(void *arg) iface->name, inet_ntoa(from)); continue; } - i = whitelisted_ip(iface->state->options, from.s_addr); + i = whitelisted_ip(iface->options, from.s_addr); if (i == 0) { syslog(LOG_WARNING, "%s: non whitelisted DHCP packet from %s", iface->name, inet_ntoa(from)); continue; } else if (i != 1 && - blacklisted_ip(iface->state->options, from.s_addr) == 1) + blacklisted_ip(iface->options, from.s_addr) == 1) { syslog(LOG_WARNING, "%s: blacklisted DHCP packet from %s", @@ -2121,7 +2121,7 @@ dhcp_open(struct interface *ifp) ifp->addr.s_addr != 0 && ifp->state->new != NULL && (ifp->state->new->cookie == htonl(MAGIC_COOKIE) || - ifp->state->options->options & DHCPCD_INFORM)) + ifp->options->options & DHCPCD_INFORM)) { if (open_udp_socket(ifp) == -1 && errno != EADDRINUSE) { syslog(LOG_ERR, "%s: open_udp_socket: %m", ifp->name); @@ -2134,7 +2134,7 @@ dhcp_open(struct interface *ifp) void dhcp_start(struct interface *ifp) { - struct if_options *ifo = ifp->state->options; + struct if_options *ifo = ifp->options; struct stat st; struct timeval now; uint32_t l; @@ -2202,7 +2202,7 @@ dhcp_start(struct interface *ifp) if (ifp->state->offer == NULL) dhcp_discover(ifp); else if (ifp->state->offer->cookie == 0 && - ifp->state->options->options & DHCPCD_IPV4LL) + ifp->options->options & DHCPCD_IPV4LL) ipv4ll_start(ifp); else dhcp_reboot(ifp); diff --git a/dhcp6.c b/dhcp6.c index 26760550..d05dc281 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -360,7 +360,7 @@ dhcp6_makemessage(struct interface *ifp) } /* Work out option size first */ - ifo = ifp->state->options; + ifo = ifp->options; len = 0; si = NULL; for (opt = dhcp6_opts; opt->option; opt++) { @@ -1234,7 +1234,7 @@ dhcp6_handledata(_unused void *arg) return; } - ifo = ifp->state->options; + ifo = ifp->options; for (opt = dhcp6_opts; opt->option; opt++) { if (has_option_mask(ifo->requiremask6, opt->option) && dhcp6_getoption(opt->option, r, len) == NULL) @@ -1392,7 +1392,7 @@ recv: script_runreason(ifp, options & DHCPCD_TEST ? "TEST" : reason); if (options & DHCPCD_TEST || - (ifp->state->options->options & DHCPCD_INFORM && + (ifp->options->options & DHCPCD_INFORM && !(options & DHCPCD_MASTER))) { #ifdef DEBUG_MEMORY @@ -1580,7 +1580,7 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, state = D6_CSTATE(ifp); e = 0; ep = env; - ifo = ifp->state->options; + ifo = ifp->options; for (opt = dhcp6_opts; opt->option; opt++) { if (!opt->var) continue; diff --git a/dhcpcd.c b/dhcpcd.c index ebbb3007..c2d7579e 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -297,183 +297,183 @@ stop_interface(struct interface *iface) } static void -configure_interface1(struct interface *iface) +configure_interface1(struct interface *ifp) { - struct if_state *ifs = iface->state; - struct if_options *ifo = ifs->options; + struct if_options *ifo = ifp->options; uint8_t *duid; size_t len, ifl; /* Do any platform specific configuration */ - if_conf(iface); + if_conf(ifp); - if (iface->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM)) + if (ifp->flags & IFF_POINTOPOINT && !(ifo->options & DHCPCD_INFORM)) ifo->options |= DHCPCD_STATIC; - if (iface->flags & IFF_NOARP || + if (ifp->flags & IFF_NOARP || ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) ifo->options &= ~(DHCPCD_ARP | DHCPCD_IPV4LL); - if (!(iface->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST))) + if (!(ifp->flags & (IFF_POINTOPOINT | IFF_LOOPBACK | IFF_MULTICAST))) ifo->options &= ~DHCPCD_IPV6RS; - if (ifo->options & DHCPCD_LINK && carrier_status(iface) == -1) + if (ifo->options & DHCPCD_LINK && carrier_status(ifp) == -1) ifo->options &= ~DHCPCD_LINK; if (ifo->metric != -1) - iface->metric = ifo->metric; + ifp->metric = ifo->metric; /* We want to disable kernel interface RA as early as possible. */ if (options & DHCPCD_IPV6RS && ifo->options & DHCPCD_IPV6RS) { - if (check_ipv6(iface->name) != 1) + if (check_ipv6(ifp->name) != 1) ifo->options &= ~DHCPCD_IPV6RS; } /* If we haven't specified a ClientID and our hardware address * length is greater than DHCP_CHADDR_LEN then we enforce a ClientID * of the hardware address family and the hardware address. */ - if (iface->hwlen > DHCP_CHADDR_LEN) + if (ifp->hwlen > DHCP_CHADDR_LEN) ifo->options |= DHCPCD_CLIENTID; /* Firewire and InfiniBand interfaces require ClientID and * the broadcast option being set. */ - switch (iface->family) { + switch (ifp->family) { case ARPHRD_IEEE1394: /* FALLTHROUGH */ case ARPHRD_INFINIBAND: ifo->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST; break; } - free(iface->clientid); - iface->clientid = NULL; + free(ifp->clientid); + ifp->clientid = NULL; if (!(ifo->options & DHCPCD_IPV4)) return; if (*ifo->clientid) { - iface->clientid = xmalloc(ifo->clientid[0] + 1); - memcpy(iface->clientid, ifo->clientid, ifo->clientid[0] + 1); + ifp->clientid = xmalloc(ifo->clientid[0] + 1); + memcpy(ifp->clientid, ifo->clientid, ifo->clientid[0] + 1); } else if (ifo->options & DHCPCD_CLIENTID) { len = 0; if (ifo->options & DHCPCD_DUID) { duid = xmalloc(DUID_LEN); - if ((len = get_duid(duid, iface)) == 0) + if ((len = get_duid(duid, ifp)) == 0) syslog(LOG_ERR, "get_duid: %m"); } else duid = NULL; if (len > 0) { - iface->clientid = xmalloc(len + 6); - iface->clientid[0] = len + 5; - iface->clientid[1] = 255; /* RFC 4361 */ - ifl = strlen(iface->name); + ifp->clientid = xmalloc(len + 6); + ifp->clientid[0] = len + 5; + ifp->clientid[1] = 255; /* RFC 4361 */ + ifl = strlen(ifp->name); if (ifl < 5) { - memcpy(iface->clientid + 2, iface->name, ifl); + memcpy(ifp->clientid + 2, ifp->name, ifl); if (ifl < 4) - memset(iface->clientid + 2 + ifl, + memset(ifp->clientid + 2 + ifl, 0, 4 - ifl); } else { - ifl = htonl(iface->index); - memcpy(iface->clientid + 2, &ifl, 4); + ifl = htonl(ifp->index); + memcpy(ifp->clientid + 2, &ifl, 4); } - memcpy(iface->clientid + 6, duid, len); + memcpy(ifp->clientid + 6, duid, len); } else if (len == 0) { - len = iface->hwlen + 1; - iface->clientid = xmalloc(len + 1); - iface->clientid[0] = len; - iface->clientid[1] = iface->family; - memcpy(iface->clientid + 2, iface->hwaddr, - iface->hwlen); + len = ifp->hwlen + 1; + ifp->clientid = xmalloc(len + 1); + ifp->clientid[0] = len; + ifp->clientid[1] = ifp->family; + memcpy(ifp->clientid + 2, ifp->hwaddr, + ifp->hwlen); } free(duid); } if (ifo->options & DHCPCD_CLIENTID) - syslog(LOG_DEBUG, "%s: using ClientID %s", iface->name, - hwaddr_ntoa(iface->clientid + 1, *iface->clientid)); - else if (iface->hwlen) - syslog(LOG_DEBUG, "%s: using hwaddr %s", iface->name, - hwaddr_ntoa(iface->hwaddr, iface->hwlen)); + syslog(LOG_DEBUG, "%s: using ClientID %s", ifp->name, + hwaddr_ntoa(ifp->clientid + 1, *ifp->clientid)); + else if (ifp->hwlen) + syslog(LOG_DEBUG, "%s: using hwaddr %s", ifp->name, + hwaddr_ntoa(ifp->hwaddr, ifp->hwlen)); } int -select_profile(struct interface *iface, const char *profile) +select_profile(struct interface *ifp, const char *profile) { struct if_options *ifo; int ret; ret = 0; - ifo = read_config(cffile, iface->name, iface->ssid, profile); + ifo = read_config(cffile, ifp->name, ifp->ssid, profile); if (ifo == NULL) { - syslog(LOG_DEBUG, "%s: no profile %s", iface->name, profile); + syslog(LOG_DEBUG, "%s: no profile %s", ifp->name, profile); ret = -1; goto exit; } if (profile != NULL) { - strlcpy(iface->state->profile, profile, - sizeof(iface->state->profile)); + strlcpy(ifp->state->profile, profile, + sizeof(ifp->state->profile)); syslog(LOG_INFO, "%s: selected profile %s", - iface->name, profile); + ifp->name, profile); } else - *iface->state->profile = '\0'; - free_options(iface->state->options); - iface->state->options = ifo; + *ifp->state->profile = '\0'; + free_options(ifp->options); + ifp->options = ifo; exit: if (profile) - configure_interface1(iface); + configure_interface1(ifp); return ret; } static void -configure_interface(struct interface *iface, int argc, char **argv) +configure_interface(struct interface *ifp, int argc, char **argv) { - select_profile(iface, NULL); - add_options(iface->state->options, argc, argv); - configure_interface1(iface); + + select_profile(ifp, NULL); + add_options(ifp->options, argc, argv); + configure_interface1(ifp); } void handle_carrier(int action, int flags, const char *ifname) { - struct interface *iface; + struct interface *ifp; int carrier; if (!(options & DHCPCD_LINK)) return; - for (iface = ifaces; iface; iface = iface->next) - if (strcmp(iface->name, ifname) == 0) + for (ifp = ifaces; ifp; ifp = ifp->next) + if (strcmp(ifp->name, ifname) == 0) break; - if (!iface) { + if (!ifp) { if (options & DHCPCD_LINK) handle_interface(1, ifname); return; } - if (!(iface->state->options->options & DHCPCD_LINK)) + if (!(ifp->options->options & DHCPCD_LINK)) return; if (action) { carrier = action == 1 ? 1 : 0; - iface->flags = flags; + ifp->flags = flags; } else - carrier = carrier_status(iface); + carrier = carrier_status(ifp); if (carrier == -1) syslog(LOG_ERR, "%s: carrier_status: %m", ifname); - else if (carrier == 0 || ~iface->flags & IFF_UP) { - if (iface->carrier != LINK_DOWN) { - iface->carrier = LINK_DOWN; - syslog(LOG_INFO, "%s: carrier lost", iface->name); - dhcp_close(iface); - dhcp6_drop(iface, "EXPIRE6"); - ipv6rs_drop(iface); - dhcp_drop(iface, "NOCARRIER"); + else if (carrier == 0 || ~ifp->flags & IFF_UP) { + if (ifp->carrier != LINK_DOWN) { + ifp->carrier = LINK_DOWN; + syslog(LOG_INFO, "%s: carrier lost", ifp->name); + dhcp_close(ifp); + dhcp6_drop(ifp, "EXPIRE6"); + ipv6rs_drop(ifp); + dhcp_drop(ifp, "NOCARRIER"); } - } else if (carrier == 1 && !(~iface->flags & IFF_UP)) { - if (iface->carrier != LINK_UP) { - iface->carrier = LINK_UP; - syslog(LOG_INFO, "%s: carrier acquired", iface->name); - if (iface->wireless) - getifssid(iface->name, iface->ssid); - configure_interface(iface, margc, margv); - iface->state->interval = 0; - iface->state->reason = "CARRIER"; - script_run(iface); - start_interface(iface); + } else if (carrier == 1 && !(~ifp->flags & IFF_UP)) { + if (ifp->carrier != LINK_UP) { + ifp->carrier = LINK_UP; + syslog(LOG_INFO, "%s: carrier acquired", ifp->name); + if (ifp->wireless) + getifssid(ifp->name, ifp->ssid); + configure_interface(ifp, margc, margv); + ifp->state->interval = 0; + ifp->state->reason = "CARRIER"; + script_run(ifp); + start_interface(ifp); } } } @@ -481,83 +481,83 @@ handle_carrier(int action, int flags, const char *ifname) void start_interface(void *arg) { - struct interface *iface = arg; - struct if_options *ifo = iface->state->options; + struct interface *ifp = arg; + struct if_options *ifo = ifp->options; int nolease; - handle_carrier(0, 0, iface->name); - if (iface->carrier == LINK_DOWN) { - syslog(LOG_INFO, "%s: waiting for carrier", iface->name); + handle_carrier(0, 0, ifp->name); + if (ifp->carrier == LINK_DOWN) { + syslog(LOG_INFO, "%s: waiting for carrier", ifp->name); return; } - iface->start_uptime = uptime(); - free(iface->state->offer); - iface->state->offer = NULL; + ifp->start_uptime = uptime(); + free(ifp->state->offer); + ifp->state->offer = NULL; if (options & DHCPCD_IPV6RS && ifo->options & DHCPCD_IPV6RS && !(ifo->options & DHCPCD_INFORM)) - ipv6rs_start(iface); + ipv6rs_start(ifp); - if (iface->state->arping_index < ifo->arping_len) { - arp_start(iface); + if (ifp->state->arping_index < ifo->arping_len) { + arp_start(ifp); return; } if (ifo->options & DHCPCD_IPV6) { if (ifo->options & DHCPCD_INFORM) - nolease = dhcp6_start(iface, 0); + nolease = dhcp6_start(ifp, 0); else if (!(ifo->options & DHCPCD_IPV6RS)) - nolease = dhcp6_start(iface, 1); + nolease = dhcp6_start(ifp, 1); else nolease = 0; if (nolease == -1) - syslog(LOG_ERR, "%s: dhcp6_start: %m", iface->name); + syslog(LOG_ERR, "%s: dhcp6_start: %m", ifp->name); } if (ifo->options & DHCPCD_IPV4) - dhcp_start(iface); + dhcp_start(ifp); } static void -init_state(struct interface *iface, int argc, char **argv) +init_state(struct interface *ifp, int argc, char **argv) { struct if_state *ifs; - if (iface->state) - ifs = iface->state; + if (ifp->state) + ifs = ifp->state; else - ifs = iface->state = xzalloc(sizeof(*ifs)); + ifs = ifp->state = xzalloc(sizeof(*ifs)); ifs->state = DHS_INIT; ifs->reason = "PREINIT"; ifs->nakoff = 0; - configure_interface(iface, argc, argv); + configure_interface(ifp, argc, argv); if (!(options & DHCPCD_TEST)) - script_run(iface); + script_run(ifp); /* We need to drop the leasefile so that start_interface * doesn't load it. */ - if (ifs->options->options & DHCPCD_REQUEST) - unlink(iface->leasefile); + if (ifp->options->options & DHCPCD_REQUEST) + unlink(ifp->leasefile); - if (ifs->options->options & DHCPCD_LINK) { - switch (carrier_status(iface)) { + if (ifp->options->options & DHCPCD_LINK) { + switch (carrier_status(ifp)) { case 0: - iface->carrier = LINK_DOWN; + ifp->carrier = LINK_DOWN; ifs->reason = "NOCARRIER"; break; case 1: - iface->carrier = LINK_UP; + ifp->carrier = LINK_UP; ifs->reason = "CARRIER"; break; default: - iface->carrier = LINK_UNKNOWN; + ifp->carrier = LINK_UNKNOWN; return; } if (!(options & DHCPCD_TEST)) - script_run(iface); + script_run(ifp); } else - iface->carrier = LINK_UNKNOWN; + ifp->carrier = LINK_UNKNOWN; } void @@ -657,27 +657,27 @@ handle_link(_unused void *arg) } static void -if_reboot(struct interface *iface, int argc, char **argv) +if_reboot(struct interface *ifp, int argc, char **argv) { const struct if_options *ifo; int opt; - ifo = iface->state->options; + ifo = ifp->options; opt = ifo->options; - configure_interface(iface, argc, argv); - ifo = iface->state->options; - iface->state->interval = 0; + configure_interface(ifp, argc, argv); + ifo = ifp->options; + ifp->state->interval = 0; if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) && - iface->addr.s_addr != ifo->req_addr.s_addr) || + ifp->addr.s_addr != ifo->req_addr.s_addr) || (opt & (DHCPCD_INFORM | DHCPCD_STATIC) && !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))) { - dhcp_drop(iface, "EXPIRE"); + dhcp_drop(ifp, "EXPIRE"); } else { - free(iface->state->offer); - iface->state->offer = NULL; + free(ifp->state->offer); + ifp->state->offer = NULL; } - start_interface(iface); + start_interface(ifp); } static void @@ -788,7 +788,7 @@ handle_signal(int sig) break; if (ifp->carrier != LINK_DOWN && (do_release || - ifp->state->options->options & DHCPCD_RELEASE)) + ifp->options->options & DHCPCD_RELEASE)) dhcp_release(ifp); stop_interface(ifp); } @@ -922,8 +922,8 @@ handle_args(struct fd_list *fd, int argc, char **argv) if (!ifp) continue; if (do_release) - ifp->state->options->options |= DHCPCD_RELEASE; - if (ifp->state->options->options & DHCPCD_RELEASE && + ifp->options->options |= DHCPCD_RELEASE; + if (ifp->options->options & DHCPCD_RELEASE && ifp->carrier != LINK_DOWN) dhcp_release(ifp); stop_interface(ifp); @@ -1076,9 +1076,9 @@ main(int argc, char **argv) snprintf(iface->leasefile, sizeof(iface->leasefile), LEASEFILE, iface->name); iface->state = xzalloc(sizeof(*iface->state)); - iface->state->options = xzalloc(sizeof(*iface->state->options)); - strlcpy(iface->state->options->script, if_options->script, - sizeof(iface->state->options->script)); + iface->options = xzalloc(sizeof(*iface->options)); + strlcpy(iface->options->script, if_options->script, + sizeof(iface->options->script)); iface->state->new = read_lease(iface); if (iface->state->new == NULL && errno == ENOENT) { strlcpy(iface->leasefile, argv[optind], @@ -1311,8 +1311,10 @@ main(int argc, char **argv) } if (options & DHCPCD_MASTER) i = if_options->timeout; + else if (ifaces) + i = ifaces->options->timeout; else - i = ifaces->state->options->timeout; + i = 0; if (opt == 0 && options & DHCPCD_LINK && !(options & DHCPCD_WAITIP)) diff --git a/dhcpcd.h b/dhcpcd.h index 2e389946..1db532ec 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -67,7 +67,6 @@ enum DHS { struct if_state { enum DHS state; char profile[PROFILE_LEN]; - struct if_options *options; struct dhcp_message *sent; struct dhcp_message *offer; struct dhcp_message *new; @@ -114,6 +113,7 @@ struct interface { char leasefile[PATH_MAX]; time_t start_uptime; + struct if_options *options; unsigned char *clientid; struct interface *next; diff --git a/if-linux.c b/if-linux.c index 4b21ff34..36d47352 100644 --- a/if-linux.c +++ b/if-linux.c @@ -106,7 +106,7 @@ if_conf(struct interface *iface) if (fp == NULL) return errno == ENOENT ? 0 : -1; if (fgets(buf, sizeof(buf), fp) != NULL && buf[0] == '0') - iface->state->options->options |= DHCPCD_BROADCAST; + iface->options->options |= DHCPCD_BROADCAST; fclose(fp); return 0; } diff --git a/ipv4.c b/ipv4.c index 45b4e5d7..f9dddb86 100644 --- a/ipv4.c +++ b/ipv4.c @@ -121,7 +121,7 @@ n_route(struct rt *rt) /* Don't set default routes if not asked to */ if (rt->dest.s_addr == 0 && rt->net.s_addr == 0 && - !(rt->iface->state->options->options & DHCPCD_GATEWAY)) + !(rt->iface->options->options & DHCPCD_GATEWAY)) return -1; desc_route("adding", rt); @@ -147,7 +147,7 @@ c_route(struct rt *ort, struct rt *nrt) /* Don't set default routes if not asked to */ if (nrt->dest.s_addr == 0 && nrt->net.s_addr == 0 && - !(nrt->iface->state->options->options & DHCPCD_GATEWAY)) + !(nrt->iface->options->options & DHCPCD_GATEWAY)) return -1; desc_route("changing", nrt); @@ -202,9 +202,9 @@ add_subnet_route(struct rt *rt, const struct interface *iface) if (iface->net.s_addr == INADDR_BROADCAST || iface->net.s_addr == INADDR_ANY || - (iface->state->options->options & + (iface->options->options & (DHCPCD_INFORM | DHCPCD_STATIC) && - iface->state->options->req_addr.s_addr == INADDR_ANY)) + iface->options->req_addr.s_addr == INADDR_ANY)) return rt; r = xmalloc(sizeof(*r)); @@ -220,8 +220,8 @@ get_routes(struct interface *ifp) { struct rt *rt, *nrt = NULL, *r = NULL; - if (ifp->state->options->routes != NULL) { - for (rt = ifp->state->options->routes; + if (ifp->options->routes != NULL) { + for (rt = ifp->options->routes; rt != NULL; rt = rt->next) { @@ -263,7 +263,7 @@ add_destination_route(struct rt *rt, const struct interface *iface) struct rt *r; if (!(iface->flags & IFF_POINTOPOINT) || - !has_option_mask(iface->state->options->dstmask, DHO_ROUTER)) + !has_option_mask(iface->options->dstmask, DHO_ROUTER)) return rt; r = xmalloc(sizeof(*r)); r->dest.s_addr = INADDR_ANY; @@ -337,7 +337,7 @@ ipv4_buildroutes(void) dnr = get_routes(ifp); dnr = massage_host_routes(dnr, ifp); dnr = add_subnet_route(dnr, ifp); - if (ifp->state->options->options & DHCPCD_GATEWAY) { + if (ifp->options->options & DHCPCD_GATEWAY) { dnr = add_router_host_route(dnr, ifp); dnr = add_destination_route(dnr, ifp); } @@ -394,7 +394,7 @@ delete_address(struct interface *iface) int retval; struct if_options *ifo; - ifo = iface->state->options; + ifo = iface->options; if (ifo->options & DHCPCD_INFORM || (ifo->options & DHCPCD_STATIC && ifo->req_addr.s_addr == 0)) return 0; @@ -416,7 +416,7 @@ ipv4_applyaddr(void *arg) struct interface *iface = arg; struct dhcp_message *dhcp = iface->state->new; struct dhcp_lease *lease = &iface->state->lease; - struct if_options *ifo = iface->state->options; + struct if_options *ifo = iface->options; struct rt *rt; /* As we are now adjusting an interface, we need to ensure @@ -504,7 +504,7 @@ ipv4_handleifa(int type, const char *ifname, if (type != RTM_NEWADDR) return; - ifo = ifp->state->options; + ifo = ifp->options; if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)) == 0 || ifo->req_addr.s_addr != INADDR_ANY) return; diff --git a/ipv6.c b/ipv6.c index ae6c5df3..03a8dc16 100644 --- a/ipv6.c +++ b/ipv6.c @@ -316,7 +316,7 @@ n_route(struct rt6 *rt) /* Don't set default routes if not asked to */ if (IN6_IS_ADDR_UNSPECIFIED(&rt->dest) && IN6_IS_ADDR_UNSPECIFIED(&rt->net) && - !(rt->iface->state->options->options & DHCPCD_GATEWAY)) + !(rt->iface->options->options & DHCPCD_GATEWAY)) return -1; /* Delete the route first as it could exist prior to dhcpcd running @@ -337,7 +337,7 @@ c_route(struct rt6 *ort, struct rt6 *nrt) /* Don't set default routes if not asked to */ if (IN6_IS_ADDR_UNSPECIFIED(&nrt->dest) && IN6_IS_ADDR_UNSPECIFIED(&nrt->net) && - !(nrt->iface->state->options->options & DHCPCD_GATEWAY)) + !(nrt->iface->options->options & DHCPCD_GATEWAY)) return -1; desc_route("changing", nrt); diff --git a/ipv6rs.c b/ipv6rs.c index 2d64ba98..cbf93b6c 100644 --- a/ipv6rs.c +++ b/ipv6rs.c @@ -764,7 +764,7 @@ ipv6rs_handledata(_unused void *arg) script_runreason(ifp, "ROUTERADVERT"); /* If we don't require RDNSS then set has_dns = 1 so we fork */ - if (!(ifp->state->options->options & DHCPCD_IPV6RA_REQRDNSS)) + if (!(ifp->options->options & DHCPCD_IPV6RA_REQRDNSS)) has_dns = 1; eloop_timeout_delete(NULL, ifp); diff --git a/net.c b/net.c index 3ae3f010..8f9d1edf 100644 --- a/net.c +++ b/net.c @@ -184,22 +184,23 @@ hwaddr_aton(unsigned char *buffer, const char *addr) } void -free_interface(struct interface *iface) +free_interface(struct interface *ifp) { - if (!iface) + + if (ifp == NULL) return; - dhcp6_free(iface); - ipv6rs_free(iface); - if (iface->state) { - free_options(iface->state->options); - free(iface->state->old); - free(iface->state->new); - free(iface->state->offer); - free(iface->state); + dhcp6_free(ifp); + ipv6rs_free(ifp); + free_options(ifp->options); + if (ifp->state) { + free(ifp->state->old); + free(ifp->state->new); + free(ifp->state->offer); + free(ifp->state); } - free(iface->buffer); - free(iface->clientid); - free(iface); + free(ifp->buffer); + free(ifp->clientid); + free(ifp); } int diff --git a/script.c b/script.c index 870d9410..cf286e1d 100644 --- a/script.c +++ b/script.c @@ -175,21 +175,21 @@ arraytostr(const char *const *argv, char **s) } static ssize_t -make_env(const struct interface *iface, const char *reason, char ***argv) +make_env(const struct interface *ifp, const char *reason, char ***argv) { char **env, *p; ssize_t e, elen, l; - const struct if_options *ifo = iface->state->options; - const struct interface *ifp; + const struct if_options *ifo = ifp->options; + const struct interface *ifp2; int dhcp, dhcp6, ra; const struct dhcp6_state *d6_state; dhcp = dhcp6 = ra = 0; - d6_state = D6_STATE(iface); + d6_state = D6_STATE(ifp); if (strcmp(reason, "TEST") == 0) { if (d6_state && d6_state->new) dhcp6 = 1; - else if (ipv6rs_has_ra(iface)) + else if (ipv6rs_has_ra(ifp)) ra = 1; else dhcp = 1; @@ -209,9 +209,9 @@ make_env(const struct interface *iface, const char *reason, char ***argv) /* Make our env */ env = xmalloc(sizeof(char *) * (elen + 1)); - e = strlen("interface") + strlen(iface->name) + 2; + e = strlen("interface") + strlen(ifp->name) + 2; env[0] = xmalloc(e); - snprintf(env[0], e, "interface=%s", iface->name); + snprintf(env[0], e, "interface=%s", ifp->name); e = strlen("reason") + strlen(reason) + 2; env[1] = xmalloc(e); snprintf(env[1], e, "reason=%s", reason); @@ -222,22 +222,22 @@ make_env(const struct interface *iface, const char *reason, char ***argv) env[2] = xmalloc(e); snprintf(env[2], e, "pid=%d", getpid()); env[3] = xmalloc(e); - snprintf(env[3], e, "ifmetric=%d", iface->metric); + snprintf(env[3], e, "ifmetric=%d", ifp->metric); env[4] = xmalloc(e); - snprintf(env[4], e, "ifwireless=%d", iface->wireless); + snprintf(env[4], e, "ifwireless=%d", ifp->wireless); env[5] = xmalloc(e); - snprintf(env[5], e, "ifflags=%u", iface->flags); + snprintf(env[5], e, "ifflags=%u", ifp->flags); env[6] = xmalloc(e); - snprintf(env[6], e, "ifmtu=%d", get_mtu(iface->name)); + snprintf(env[6], e, "ifmtu=%d", get_mtu(ifp->name)); l = e = strlen("interface_order="); - for (ifp = ifaces; ifp; ifp = ifp->next) - e += strlen(ifp->name) + 1; + for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next) + e += strlen(ifp2->name) + 1; p = env[7] = xmalloc(e); strlcpy(p, "interface_order=", e); e -= l; p += l; - for (ifp = ifaces; ifp; ifp = ifp->next) { - l = strlcpy(p, ifp->name, e); + for (ifp2 = ifaces; ifp2; ifp2 = ifp2->next) { + l = strlcpy(p, ifp2->name, e); p += l; e -= l; *p++ = ' '; @@ -247,9 +247,9 @@ make_env(const struct interface *iface, const char *reason, char ***argv) if (strcmp(reason, "TEST") == 0) { env[8] = strdup("if_up=false"); env[9] = strdup("if_down=false"); - } else if ((dhcp && iface->state->new) || + } else if ((dhcp && ifp->state->new) || (dhcp6 && d6_state->new) || - (ra && ipv6rs_has_ra(iface))) + (ra && ipv6rs_has_ra(ifp))) { env[8] = strdup("if_up=true"); env[9] = strdup("if_down=false"); @@ -257,73 +257,73 @@ make_env(const struct interface *iface, const char *reason, char ***argv) env[8] = strdup("if_up=false"); env[9] = strdup("if_down=true"); } - if (*iface->state->profile) { - e = strlen("profile=") + strlen(iface->state->profile) + 2; + if (*ifp->state->profile) { + e = strlen("profile=") + strlen(ifp->state->profile) + 2; env[elen] = xmalloc(e); - snprintf(env[elen++], e, "profile=%s", iface->state->profile); + snprintf(env[elen++], e, "profile=%s", ifp->state->profile); } - if (iface->wireless) { - e = strlen("new_ssid=") + strlen(iface->ssid) + 2; - if (iface->state->new != NULL || - strcmp(iface->state->reason, "CARRIER") == 0) + if (ifp->wireless) { + e = strlen("new_ssid=") + strlen(ifp->ssid) + 2; + if (ifp->state->new != NULL || + strcmp(ifp->state->reason, "CARRIER") == 0) { env = xrealloc(env, sizeof(char *) * (elen + 2)); env[elen] = xmalloc(e); - snprintf(env[elen++], e, "new_ssid=%s", iface->ssid); + snprintf(env[elen++], e, "new_ssid=%s", ifp->ssid); } - if (iface->state->old != NULL || - strcmp(iface->state->reason, "NOCARRIER") == 0) + if (ifp->state->old != NULL || + strcmp(ifp->state->reason, "NOCARRIER") == 0) { env = xrealloc(env, sizeof(char *) * (elen + 2)); env[elen] = xmalloc(e); - snprintf(env[elen++], e, "old_ssid=%s", iface->ssid); + snprintf(env[elen++], e, "old_ssid=%s", ifp->ssid); } } - if (dhcp && iface->state->old) { - e = configure_env(NULL, NULL, iface->state->old, iface); + if (dhcp && ifp->state->old) { + e = configure_env(NULL, NULL, ifp->state->old, ifp); if (e > 0) { env = xrealloc(env, sizeof(char *) * (elen + e + 1)); elen += configure_env(env + elen, "old", - iface->state->old, iface); + ifp->state->old, ifp); } append_config(&env, &elen, "old", (const char *const *)ifo->config); } if (dhcp6 && d6_state->old) { - e = dhcp6_env(NULL, NULL, iface, + e = dhcp6_env(NULL, NULL, ifp, d6_state->old, d6_state->old_len); if (e > 0) { env = xrealloc(env, sizeof(char *) * (elen + e + 1)); - elen += dhcp6_env(env + elen, "old", iface, + elen += dhcp6_env(env + elen, "old", ifp, d6_state->old, d6_state->old_len); } } dumplease: - if (dhcp && iface->state->new) { - e = configure_env(NULL, NULL, iface->state->new, iface); + if (dhcp && ifp->state->new) { + e = configure_env(NULL, NULL, ifp->state->new, ifp); if (e > 0) { env = xrealloc(env, sizeof(char *) * (elen + e + 1)); elen += configure_env(env + elen, "new", - iface->state->new, iface); + ifp->state->new, ifp); } append_config(&env, &elen, "new", (const char *const *)ifo->config); } if (dhcp6 && d6_state->new) { - e = dhcp6_env(NULL, NULL, iface, + e = dhcp6_env(NULL, NULL, ifp, d6_state->new, d6_state->new_len); if (e > 0) { env = xrealloc(env, sizeof(char *) * (elen + e + 1)); - elen += dhcp6_env(env + elen, "new", iface, + elen += dhcp6_env(env + elen, "new", ifp, d6_state->new, d6_state->new_len); } } if (ra) { - e = ipv6rs_env(NULL, NULL, iface); + e = ipv6rs_env(NULL, NULL, ifp); if (e > 0) { env = xrealloc(env, sizeof(char *) * (elen + e + 1)); - elen += ipv6rs_env(env + elen, NULL, iface); + elen += ipv6rs_env(env + elen, NULL, ifp); } } @@ -387,9 +387,9 @@ send_interface(int fd, const struct interface *iface) } int -script_runreason(const struct interface *iface, const char *reason) +script_runreason(const struct interface *ifp, const char *reason) { - char *const argv[2] = { UNCONST(iface->state->options->script), NULL }; + char *const argv[2] = { UNCONST(ifp->options->script), NULL }; char **env = NULL, **ep; char *path, *bigenv; ssize_t e, elen = 0; @@ -398,18 +398,18 @@ script_runreason(const struct interface *iface, const char *reason) const struct fd_list *fd; struct iovec iov[2]; - if (iface->state->options->script == NULL || - iface->state->options->script[0] == '\0' || - strcmp(iface->state->options->script, "/dev/null") == 0) + if (ifp->options->script == NULL || + ifp->options->script[0] == '\0' || + strcmp(ifp->options->script, "/dev/null") == 0) return 0; if (reason == NULL) - reason = iface->state->reason; + reason = ifp->state->reason; syslog(LOG_DEBUG, "%s: executing `%s', reason %s", - iface->name, argv[0], reason); + ifp->name, argv[0], reason); /* Make our env */ - elen = make_env(iface, reason, &env); + elen = make_env(ifp, reason, &env); env = xrealloc(env, sizeof(char *) * (elen + 2)); /* Add path to it */ path = getenv("PATH");