diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..6d16c1f7 --- /dev/null +++ b/.clang-format @@ -0,0 +1,203 @@ +# Basic .clang-format taken from FreeBSD +--- +BasedOnStyle: WebKit +AlignAfterOpenBracket: DontAlign +AlignConsecutiveMacros: AcrossEmptyLines +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlines: Left +AlignOperands: false +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: false +AllowAllParametersOfDeclarationOnNextLine: false +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: InlineOnly +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterReturnType: TopLevelDefinitions +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +BinPackArguments: true +BinPackParameters: true +BreakBeforeBinaryOperators: None +BreakBeforeBraces: WebKit +BreakBeforeTernaryOperators: false +# TODO: BreakStringLiterals can cause very strange formatting so turn it off? +BreakStringLiterals: false +# Prefer: +# some_var = function(arg1, +# arg2) +# over: +# some_var = +# function(arg1, arg2) +PenaltyBreakAssignment: 100 +# Prefer: +# some_long_function(arg1, arg2 +# arg3) +# over: +# some_long_function( +# arg1, arg2, arg3) +PenaltyBreakBeforeFirstCallParameter: 100 +CompactNamespaces: true +DerivePointerAlignment: false +DisableFormat: false +ForEachMacros: + - ARB_ARRFOREACH + - ARB_ARRFOREACH_REVWCOND + - ARB_ARRFOREACH_REVERSE + - ARB_FOREACH + - ARB_FOREACH_FROM + - ARB_FOREACH_SAFE + - ARB_FOREACH_REVERSE + - ARB_FOREACH_REVERSE_FROM + - ARB_FOREACH_REVERSE_SAFE + - BIT_FOREACH_ISCLR + - BIT_FOREACH_ISSET + - CPU_FOREACH + - CPU_FOREACH_ISCLR + - CPU_FOREACH_ISSET + - FOREACH_THREAD_IN_PROC + - FOREACH_PROC_IN_SYSTEM + - FOREACH_PRISON_CHILD + - FOREACH_PRISON_DESCENDANT + - FOREACH_PRISON_DESCENDANT_LOCKED + - FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL + - MNT_VNODE_FOREACH_ALL + - MNT_VNODE_FOREACH_ACTIVE + - RB_FOREACH + - RB_FOREACH_FROM + - RB_FOREACH_SAFE + - RB_FOREACH_REVERSE + - RB_FOREACH_REVERSE_FROM + - RB_FOREACH_REVERSE_SAFE + - SLIST_FOREACH + - SLIST_FOREACH_FROM + - SLIST_FOREACH_FROM_SAFE + - SLIST_FOREACH_SAFE + - SLIST_FOREACH_PREVPTR + - SPLAY_FOREACH + - LIST_FOREACH + - LIST_FOREACH_FROM + - LIST_FOREACH_FROM_SAFE + - LIST_FOREACH_SAFE + - STAILQ_FOREACH + - STAILQ_FOREACH_FROM + - STAILQ_FOREACH_FROM_SAFE + - STAILQ_FOREACH_SAFE + - TAILQ_FOREACH + - TAILQ_FOREACH_FROM + - TAILQ_FOREACH_FROM_SAFE + - TAILQ_FOREACH_REVERSE + - TAILQ_FOREACH_REVERSE_FROM + - TAILQ_FOREACH_REVERSE_FROM_SAFE + - TAILQ_FOREACH_REVERSE_SAFE + - TAILQ_FOREACH_SAFE + - VM_MAP_ENTRY_FOREACH + - VM_PAGE_DUMP_FOREACH +SpaceBeforeParens: ControlStatementsExceptForEachMacros +IndentCaseLabels: false +IndentPPDirectives: None +Language: Cpp +NamespaceIndentation: None +PointerAlignment: Right +ContinuationIndentWidth: 4 +IndentWidth: 8 +TabWidth: 8 +ColumnLimit: 80 +UseTab: Always +SpaceAfterCStyleCast: false +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^\"opt_.*\.h\"' + Priority: 1 + SortPriority: 10 + - Regex: '^' + Priority: 2 + SortPriority: 20 + - Regex: '^' + Priority: 2 + SortPriority: 21 + - Regex: '^' + Priority: 2 + SortPriority: 22 + - Regex: '^' + Priority: 2 + SortPriority: 23 + - Regex: '^' + Priority: 3 + SortPriority: 30 + - Regex: '^ before this ... + - Regex: '^' + Priority: 7 + SortPriority: 71 + - Regex: '^ #include - -#include +#include #include #include #include +#include #include -#include #include +#include #include #include -#define ELOOP_QUEUE ELOOP_ARP -#include "config.h" +#define ELOOP_QUEUE ELOOP_ARP #include "arp.h" #include "bpf.h" -#include "ipv4.h" #include "common.h" +#include "config.h" #include "dhcpcd.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" +#include "ipv4.h" #include "ipv4ll.h" #include "logerr.h" #include "privsep.h" #if defined(ARP) -#define ARP_LEN \ - (FRAMEHDRLEN_MAX + \ - sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * HWADDR_LEN)) +#define ARP_LEN \ + (FRAMEHDRLEN_MAX + sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + \ + (2 * HWADDR_LEN)) /* ARP debugging can be quite noisy. Enable this for more noise! */ -//#define ARP_DEBUG +// #define ARP_DEBUG /* Assert the correct structure size for on wire */ __CTASSERT(sizeof(struct arphdr) == 8); static ssize_t -arp_request(const struct arp_state *astate, - const struct in_addr *sip) +arp_request(const struct arp_state *astate, const struct in_addr *sip) { const struct interface *ifp = astate->iface; const struct in_addr *tip = &astate->addr; @@ -86,16 +84,16 @@ arp_request(const struct arp_state *astate, p = arp_buffer; len = 0; -#define CHECK(fun, b, l) \ - do { \ - if (len + (l) > sizeof(arp_buffer)) \ - goto eexit; \ - fun(p, (b), (l)); \ - p += (l); \ - len += (l); \ +#define CHECK(fun, b, l) \ + do { \ + if (len + (l) > sizeof(arp_buffer)) \ + goto eexit; \ + fun(p, (b), (l)); \ + p += (l); \ + len += (l); \ } while (/* CONSTCOND */ 0) -#define APPEND(b, l) CHECK(memcpy, b, l) -#define ZERO(l) CHECK(memset, 0, l) +#define APPEND(b, l) CHECK(memcpy, b, l) +#define ZERO(l) CHECK(memset, 0, l) APPEND(&ar, sizeof(ar)); APPEND(ifp->hwaddr, ifp->hwlen); @@ -127,20 +125,19 @@ arp_report_conflicted(const struct arp_state *astate, char fbuf[HWADDR_LEN * 3]; if (amsg == NULL) { - logerrx("%s: DAD detected %s", - astate->iface->name, inet_ntoa(astate->addr)); + logerrx("%s: DAD detected %s", astate->iface->name, + inet_ntoa(astate->addr)); return; } hwaddr_ntoa(amsg->sha, astate->iface->hwlen, abuf, sizeof(abuf)); if (bpf_frame_header_len(astate->iface) == 0) { - logwarnx("%s: %s claims %s", - astate->iface->name, abuf, inet_ntoa(astate->addr)); + logwarnx("%s: %s claims %s", astate->iface->name, abuf, + inet_ntoa(astate->addr)); return; } - logwarnx("%s: %s(%s) claims %s", - astate->iface->name, abuf, + logwarnx("%s: %s(%s) claims %s", astate->iface->name, abuf, hwaddr_ntoa(amsg->fsha, astate->iface->hwlen, fbuf, sizeof(fbuf)), inet_ntoa(astate->addr)); } @@ -184,18 +181,17 @@ arp_found(struct arp_state *astate, const struct arp_msg *amsg) * ignored. */ clock_gettime(CLOCK_MONOTONIC, &now); if (timespecisset(&astate->defend) && - eloop_timespec_diff(&now, &astate->defend, NULL) < DEFEND_INTERVAL) - { - logwarnx("%s: %d second defence failed for %s", - ifp->name, DEFEND_INTERVAL, inet_ntoa(astate->addr)); + eloop_timespec_diff(&now, &astate->defend, NULL) < + DEFEND_INTERVAL) { + logwarnx("%s: %d second defence failed for %s", ifp->name, + DEFEND_INTERVAL, inet_ntoa(astate->addr)); if (ifp->options->options & DHCPCD_ARP_PERSISTDEFENCE) return; - } - else if (arp_request(astate, &astate->addr) == -1) + } else if (arp_request(astate, &astate->addr) == -1) logerr(__func__); else { - logdebugx("%s: defended address %s", - ifp->name, inet_ntoa(astate->addr)); + logdebugx("%s: defended address %s", ifp->name, + inet_ntoa(astate->addr)); astate->defend = now; return; } @@ -208,7 +204,6 @@ arp_found(struct arp_state *astate, const struct arp_msg *amsg) static bool arp_validate(const struct interface *ifp, struct arphdr *arp) { - /* Address type must match */ if (arp->ar_hrd != htons(ifp->hwtype)) return false; @@ -282,8 +277,7 @@ arp_packet(struct interface *ifp, uint8_t *data, size_t len, return; /* Ignore messages from ourself */ if (ar.ar_hln == ifp->hwlen && - memcmp(hw_s, ifp->hwaddr, ifp->hwlen) == 0) - { + memcmp(hw_s, ifp->hwaddr, ifp->hwlen) == 0) { #ifdef ARP_DEBUG logdebugx("%s: ignoring ARP from self", ifp->name); #endif @@ -296,17 +290,18 @@ arp_packet(struct interface *ifp, uint8_t *data, size_t len, memcpy(&arm.tip.s_addr, hw_t + ar.ar_hln, ar.ar_pln); #ifndef KERNEL_RFC5227 - /* During ARP probe the 'sender hardware address' MUST contain the hardware - * address of the interface sending the packet. RFC5227, 1.1 */ - is_probe = ar.ar_op == htons(ARPOP_REQUEST) && IN_IS_ADDR_UNSPECIFIED(&arm.sip) && - bpf_flags & BPF_BCAST; - if (is_probe && falen > 0 && (falen != ar.ar_hln || - memcmp(&arm.sha, &arm.fsha, ar.ar_hln))) { + /* During ARP probe the 'sender hardware address' MUST contain the + * hardware address of the interface sending the packet. RFC5227, 1.1 */ + is_probe = ar.ar_op == htons(ARPOP_REQUEST) && + IN_IS_ADDR_UNSPECIFIED(&arm.sip) && bpf_flags & BPF_BCAST; + if (is_probe && falen > 0 && + (falen != ar.ar_hln || memcmp(&arm.sha, &arm.fsha, ar.ar_hln))) { char abuf[HWADDR_LEN * 3]; char fbuf[HWADDR_LEN * 3]; hwaddr_ntoa(&arm.sha, ar.ar_hln, abuf, sizeof(abuf)); hwaddr_ntoa(&arm.fsha, falen, fbuf, sizeof(fbuf)); - logwarnx("%s: invalid ARP probe, sender hw address mismatch (%s, %s)", + logwarnx( + "%s: invalid ARP probe, sender hw address mismatch (%s, %s)", ifp->name, abuf, fbuf); return; } @@ -320,8 +315,8 @@ arp_packet(struct interface *ifp, uint8_t *data, size_t len, TAILQ_FOREACH_SAFE(astate, &state->arp_states, next, astaten) { if (IN_ARE_ADDR_EQUAL(&arm.sip, &astate->addr) || (IN_IS_ADDR_UNSPECIFIED(&arm.sip) && - IN_ARE_ADDR_EQUAL(&arm.tip, &astate->addr) && - bpf_flags & BPF_BCAST)) + IN_ARE_ADDR_EQUAL(&arm.tip, &astate->addr) && + bpf_flags & BPF_BCAST)) arp_found(astate, &arm); } } @@ -378,11 +373,13 @@ arp_probe1(void *arg) if (++astate->probes < PROBE_NUM) { delay = (PROBE_MIN * MSEC_PER_SEC) + (arc4random_uniform( - (PROBE_MAX - PROBE_MIN) * MSEC_PER_SEC)); - eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probe1, astate); + (PROBE_MAX - PROBE_MIN) * MSEC_PER_SEC)); + eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probe1, + astate); } else { - delay = ANNOUNCE_WAIT * MSEC_PER_SEC; - eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probed, astate); + delay = ANNOUNCE_WAIT * MSEC_PER_SEC; + eloop_timeout_add_msec(ifp->ctx->eloop, delay, arp_probed, + astate); } logdebugx("%s: ARP probing %s (%d of %d), next in %0.1f seconds", ifp->name, inet_ntoa(astate->addr), @@ -395,13 +392,12 @@ arp_probe1(void *arg) void arp_probe(struct arp_state *astate) { - astate->probes = 0; - logdebugx("%s: probing for %s", - astate->iface->name, inet_ntoa(astate->addr)); + logdebugx("%s: probing for %s", astate->iface->name, + inet_ntoa(astate->addr)); arp_probe1(astate); } -#endif /* ARP */ +#endif /* ARP */ struct arp_state * arp_find(struct interface *ifp, const struct in_addr *addr) @@ -443,13 +439,12 @@ arp_announce1(void *arg) if (++astate->claims < ANNOUNCE_NUM) logdebugx("%s: ARP announcing %s (%d of %d), " - "next in %d.0 seconds", - ifp->name, inet_ntoa(astate->addr), - astate->claims, ANNOUNCE_NUM, ANNOUNCE_WAIT); + "next in %d.0 seconds", + ifp->name, inet_ntoa(astate->addr), astate->claims, + ANNOUNCE_NUM, ANNOUNCE_WAIT); else - logdebugx("%s: ARP announcing %s (%d of %d)", - ifp->name, inet_ntoa(astate->addr), - astate->claims, ANNOUNCE_NUM); + logdebugx("%s: ARP announcing %s (%d of %d)", ifp->name, + inet_ntoa(astate->addr), astate->claims, ANNOUNCE_NUM); /* The kernel will send a Gratuitous ARP for newly added addresses. * So we can avoid sending the same. @@ -493,16 +488,15 @@ arp_announce(struct arp_state *astate) a2->addr.s_addr != astate->addr.s_addr) continue; r = eloop_timeout_delete(a2->iface->ctx->eloop, - a2->claims < ANNOUNCE_NUM - ? arp_announce1 : arp_announced, + a2->claims < ANNOUNCE_NUM ? arp_announce1 : + arp_announced, a2); if (r == -1) logerr(__func__); else if (r != 0) { logdebugx("%s: ARP announcement " - "of %s cancelled", - a2->iface->name, - inet_ntoa(a2->addr)); + "of %s cancelled", + a2->iface->name, inet_ntoa(a2->addr)); arp_announced(a2); } } @@ -574,12 +568,11 @@ arp_new(struct interface *ifp, const struct in_addr *addr) free(astate); return NULL; } - if (eloop_event_add(ifp->ctx->eloop, astate->bpf->bpf_fd, ELE_READ, - arp_read, astate) == -1) + if (eloop_event_add(ifp->ctx->eloop, astate->bpf->bpf_fd, + ELE_READ, arp_read, astate) == -1) logerr("%s: eloop_event_add", __func__); } - state = ARP_STATE(ifp); TAILQ_INSERT_TAIL(&state->arp_states, astate, next); return astate; @@ -599,7 +592,7 @@ arp_free(struct arp_state *astate) ctx = ifp->ctx; eloop_timeout_delete(ctx->eloop, NULL, astate); - state = ARP_STATE(ifp); + state = ARP_STATE(ifp); TAILQ_REMOVE(&state->arp_states, astate, next); if (astate->free_cb) astate->free_cb(astate); diff --git a/src/arp.h b/src/arp.h index d336f0ef..95860be8 100644 --- a/src/arp.h +++ b/src/arp.h @@ -30,16 +30,16 @@ #define ARP_H /* ARP timings from RFC5227 */ -#define PROBE_WAIT 1 -#define PROBE_NUM 3 -#define PROBE_MIN 1 -#define PROBE_MAX 2 -#define ANNOUNCE_WAIT 2 -#define ANNOUNCE_NUM 2 -#define ANNOUNCE_INTERVAL 2 -#define MAX_CONFLICTS 10 -#define RATE_LIMIT_INTERVAL 60 -#define DEFEND_INTERVAL 10 +#define PROBE_WAIT 1 +#define PROBE_NUM 3 +#define PROBE_MIN 1 +#define PROBE_MAX 2 +#define ANNOUNCE_WAIT 2 +#define ANNOUNCE_NUM 2 +#define ANNOUNCE_INTERVAL 2 +#define MAX_CONFLICTS 10 +#define RATE_LIMIT_INTERVAL 60 +#define DEFEND_INTERVAL 10 #include "bpf.h" #include "dhcpcd.h" @@ -87,17 +87,16 @@ struct iarp_state { struct arp_statehead arp_states; }; -#define ARP_STATE(ifp) \ - ((struct iarp_state *)(ifp)->if_data[IF_DATA_ARP]) -#define ARP_CSTATE(ifp) \ - ((const struct iarp_state *)(ifp)->if_data[IF_DATA_ARP]) +#define ARP_STATE(ifp) ((struct iarp_state *)(ifp)->if_data[IF_DATA_ARP]) +#define ARP_CSTATE(ifp) ((const struct iarp_state *)(ifp)->if_data[IF_DATA_ARP]) #ifdef ARP void arp_packet(struct interface *, uint8_t *, size_t, unsigned int); struct arp_state *arp_new(struct interface *, const struct in_addr *); void arp_probe(struct arp_state *); -struct arp_state *arp_ifannounceaddr(struct interface *, const struct in_addr *); -struct arp_state * arp_find(struct interface *, const struct in_addr *); +struct arp_state *arp_ifannounceaddr(struct interface *, + const struct in_addr *); +struct arp_state *arp_find(struct interface *, const struct in_addr *); void arp_free(struct arp_state *); void arp_freeaddr(struct interface *, const struct in_addr *); void arp_drop(struct interface *); diff --git a/src/auth.c b/src/auth.c index 594d6a45..b7310264 100644 --- a/src/auth.c +++ b/src/auth.c @@ -39,8 +39,8 @@ #include #include -#include "config.h" #include "auth.h" +#include "config.h" #include "dhcp.h" #include "dhcp6.h" #include "dhcpcd.h" @@ -57,28 +57,29 @@ #ifndef htonll #if (BYTE_ORDER == LITTLE_ENDIAN) -#define htonll(x) ((uint64_t)htonl((uint32_t)((x) >> 32)) | \ - (uint64_t)htonl((uint32_t)((x) & 0x00000000ffffffffULL)) << 32) -#else /* (BYTE_ORDER == LITTLE_ENDIAN) */ -#define htonll(x) (x) +#define htonll(x) \ + ((uint64_t)htonl((uint32_t)((x) >> 32)) | \ + (uint64_t)htonl((uint32_t)((x) & 0x00000000ffffffffULL)) << 32) +#else /* (BYTE_ORDER == LITTLE_ENDIAN) */ +#define htonll(x) (x) #endif -#endif /* htonll */ +#endif /* htonll */ #ifndef ntohll #if (BYTE_ORDER == LITTLE_ENDIAN) -#define ntohll(x) ((uint64_t)ntohl((uint32_t)((x) >> 32)) | \ - (uint64_t)ntohl((uint32_t)((x) & 0x00000000ffffffffULL)) << 32) -#else /* (BYTE_ORDER == LITTLE_ENDIAN) */ -#define ntohll(x) (x) +#define ntohll(x) \ + ((uint64_t)ntohl((uint32_t)((x) >> 32)) | \ + (uint64_t)ntohl((uint32_t)((x) & 0x00000000ffffffffULL)) << 32) +#else /* (BYTE_ORDER == LITTLE_ENDIAN) */ +#define ntohll(x) (x) #endif -#endif /* ntohll */ +#endif /* ntohll */ -#define HMAC_LENGTH 16 +#define HMAC_LENGTH 16 void dhcp_auth_reset(struct authstate *state) { - state->replay = 0; if (state->token) { free(state->token->key); @@ -102,8 +103,7 @@ dhcp_auth_reset(struct authstate *state) */ const struct token * dhcp_auth_validate(struct authstate *state, const struct auth *auth, - const void *vm, size_t mlen, int mp, int mt, - const void *vdata, size_t dlen) + const void *vm, size_t mlen, int mp, int mt, const void *vdata, size_t dlen) { const uint8_t *m, *data; uint8_t protocol, algorithm, rdm, *mm, type; @@ -143,15 +143,12 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, errno = EINVAL; return NULL; } - } else if (protocol != auth->protocol || - algorithm != auth->algorithm || - rdm != auth->rdm) - { + } else if (protocol != auth->protocol || algorithm != auth->algorithm || + rdm != auth->rdm) { /* As we don't require authentication, we should still * accept a reconfigure key */ if (protocol != AUTH_PROTO_RECONFKEY || - auth->options & DHCPCD_AUTH_REQUIRE) - { + auth->options & DHCPCD_AUTH_REQUIRE) { errno = EPERM; return NULL; } @@ -190,7 +187,7 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, return NULL; } } - d+= sizeof(replay); + d += sizeof(replay); dlen -= sizeof(replay); realm = NULL; @@ -238,11 +235,10 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, switch (type) { case 1: if ((mp == 4 && mt == DHCP_ACK) || - (mp == 6 && mt == DHCP6_REPLY)) - { + (mp == 6 && mt == DHCP6_REPLY)) { if (state->reconf == NULL) { - state->reconf = - malloc(sizeof(*state->reconf)); + state->reconf = malloc( + sizeof(*state->reconf)); if (state->reconf == NULL) return NULL; state->reconf->key = malloc(16); @@ -273,8 +269,7 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, return state->reconf; case 2: if (!((mp == 4 && mt == DHCP_FORCERENEW) || - (mp == 6 && mt == DHCP6_RECONFIGURE))) - { + (mp == 6 && mt == DHCP6_RECONFIGURE))) { errno = EINVAL; return NULL; } @@ -295,10 +290,9 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, /* Find a token for the realm and secret */ TAILQ_FOREACH(t, &auth->tokens, next) { - if (t->secretid == secretid && - t->realm_len == realm_len && + if (t->secretid == secretid && t->realm_len == realm_len && (t->realm_len == 0 || - memcmp(t->realm, realm, t->realm_len) == 0)) + memcmp(t->realm, realm, t->realm_len) == 0)) break; } if (t == NULL) { @@ -318,9 +312,8 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, /* First message from the server */ if (state->token && (state->token->secretid != t->secretid || - state->token->realm_len != t->realm_len || - memcmp(state->token->realm, t->realm, t->realm_len))) - { + state->token->realm_len != t->realm_len || + memcmp(state->token->realm, t->realm, t->realm_len))) { errno = EPERM; return NULL; } @@ -353,8 +346,8 @@ dhcp_auth_validate(struct authstate *state, const struct auth *auth, memset(hmac_code, 0, sizeof(hmac_code)); switch (algorithm) { case AUTH_ALG_HMAC_MD5: - hmac("md5", t->key, t->key_len, mm, mlen, - hmac_code, sizeof(hmac_code)); + hmac("md5", t->key, t->key_len, mm, mlen, hmac_code, + sizeof(hmac_code)); break; default: errno = ENOSYS; @@ -447,10 +440,8 @@ auth_get_rdm_monotonic(uint64_t *rdm) } (*rdm)++; - if (fseek(fp, 0, SEEK_SET) == -1 || - ftruncate(fileno(fp), 0) == -1 || - fprintf(fp, "0x%016" PRIu64 "\n", *rdm) != 19 || - fflush(fp) == EOF) + if (fseek(fp, 0, SEEK_SET) == -1 || ftruncate(fileno(fp), 0) == -1 || + fprintf(fp, "0x%016" PRIu64 "\n", *rdm) != 19 || fflush(fp) == EOF) err = -1; else err = 0; @@ -462,8 +453,8 @@ auth_get_rdm_monotonic(uint64_t *rdm) return err; } -#define NTP_EPOCH 2208988800U /* 1970 - 1900 in seconds */ -#define NTP_SCALE_FRAC 4294967295.0 /* max value of the fractional part */ +#define NTP_EPOCH 2208988800U /* 1970 - 1900 in seconds */ +#define NTP_SCALE_FRAC 4294967295.0 /* max value of the fractional part */ static uint64_t get_next_rdm_monotonic_clock(struct auth *auth) { @@ -493,7 +484,6 @@ get_next_rdm_monotonic(struct dhcpcd_ctx *ctx, struct auth *auth) #ifdef PRIVSEP if (IN_PRIVSEP(ctx)) { - err = ps_root_getauthrdm(ctx, &rdm); } else #endif @@ -519,9 +509,8 @@ get_next_rdm_monotonic(struct dhcpcd_ctx *ctx, struct auth *auth) */ ssize_t dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, - const struct token *t, - void *vm, size_t mlen, int mp, int mt, - void *vdata, size_t dlen) + const struct token *t, void *vm, size_t mlen, int mp, int mt, void *vdata, + size_t dlen) { uint64_t rdm; uint8_t hmac_code[HMAC_LENGTH]; @@ -551,7 +540,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, } } - switch(auth->protocol) { + switch (auth->protocol) { case AUTH_PROTO_TOKEN: case AUTH_PROTO_DELAYED: case AUTH_PROTO_DELAYEDREALM: @@ -562,7 +551,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, return -1; } - switch(auth->algorithm) { + switch (auth->algorithm) { case AUTH_ALG_NONE: case AUTH_ALG_HMAC_MD5: break; @@ -571,7 +560,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, return -1; } - switch(auth->rdm) { + switch (auth->rdm) { case AUTH_RDM_MONOTONIC: break; default: @@ -590,7 +579,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, * We only need to do this for DISCOVER messages */ if (vdata == NULL) { dlen = 1 + 1 + 1 + 8; - switch(auth->protocol) { + switch (auth->protocol) { case AUTH_PROTO_TOKEN: dlen += t->key_len; break; @@ -630,8 +619,8 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, * which is probably a good idea because both states start from zero. */ if (auth_info || - !(auth->protocol & (AUTH_PROTO_DELAYED | AUTH_PROTO_DELAYEDREALM))) - { + !(auth->protocol & + (AUTH_PROTO_DELAYED | AUTH_PROTO_DELAYEDREALM))) { *data++ = auth->rdm; switch (auth->rdm) { case AUTH_RDM_MONOTONIC: @@ -645,8 +634,8 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, rdm = htonll(rdm); memcpy(data, &rdm, 8); } else { - *data++ = 0; /* rdm */ - memset(data, 0, 8); /* replay detection data */ + *data++ = 0; /* rdm */ + memset(data, 0, 8); /* replay detection data */ } data += 8; dlen -= 1 + 1 + 1 + 8; @@ -659,7 +648,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, return -1; } if (dlen < t->key_len) { - errno = ENOBUFS; + errno = ENOBUFS; return -1; } memcpy(data, t->key, t->key_len); @@ -687,8 +676,7 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, /* Write out the SecretID */ if (auth->protocol == AUTH_PROTO_DELAYED || - auth->protocol == AUTH_PROTO_DELAYEDREALM) - { + auth->protocol == AUTH_PROTO_DELAYEDREALM) { if (dlen < sizeof(t->secretid)) { errno = ENOBUFS; return -1; @@ -717,10 +705,10 @@ dhcp_auth_encode(struct dhcpcd_ctx *ctx, struct auth *auth, } /* Create our hash and write it out */ - switch(auth->algorithm) { + switch (auth->algorithm) { case AUTH_ALG_HMAC_MD5: - hmac("md5", t->key, t->key_len, m, mlen, - hmac_code, sizeof(hmac_code)); + hmac("md5", t->key, t->key_len, m, mlen, hmac_code, + sizeof(hmac_code)); memcpy(data, hmac_code, sizeof(hmac_code)); break; } diff --git a/src/auth.h b/src/auth.h index fbffd8ed..750ec713 100644 --- a/src/auth.h +++ b/src/auth.h @@ -35,13 +35,13 @@ #define DHCPCD_AUTH_SEND (1 << 0) #define DHCPCD_AUTH_REQUIRE (1 << 1) -#define DHCPCD_AUTH_RDM_COUNTER (1 << 2) +#define DHCPCD_AUTH_RDM_COUNTER (1 << 2) -#define DHCPCD_AUTH_SENDREQUIRE (DHCPCD_AUTH_SEND | DHCPCD_AUTH_REQUIRE) +#define DHCPCD_AUTH_SENDREQUIRE (DHCPCD_AUTH_SEND | DHCPCD_AUTH_REQUIRE) #define AUTH_PROTO_TOKEN 0 #define AUTH_PROTO_DELAYED 1 -#define AUTH_PROTO_DELAYEDREALM 2 +#define AUTH_PROTO_DELAYEDREALM 2 #define AUTH_PROTO_RECONFKEY 3 #define AUTH_ALG_NONE 0 @@ -83,16 +83,12 @@ struct authstate { void dhcp_auth_reset(struct authstate *); -const struct token * dhcp_auth_validate(struct authstate *, - const struct auth *, - const void *, size_t, int, int, - const void *, size_t); +const struct token *dhcp_auth_validate(struct authstate *, const struct auth *, + const void *, size_t, int, int, const void *, size_t); struct dhcpcd_ctx; ssize_t dhcp_auth_encode(struct dhcpcd_ctx *, struct auth *, - const struct token *, - void *, size_t, int, int, - void *, size_t); + const struct token *, void *, size_t, int, int, void *, size_t); int auth_get_rdm_monotonic(uint64_t *rdm); #endif diff --git a/src/bpf.c b/src/bpf.c index 7c521261..f846e1f8 100644 --- a/src/bpf.c +++ b/src/bpf.c @@ -29,16 +29,16 @@ #include #include -#include - #include #include #include +#include + #ifdef __linux__ /* Special BPF snowflake. */ #include -#define bpf_insn sock_filter +#define bpf_insn sock_filter #else #include #endif @@ -49,39 +49,40 @@ #include #include -#include "common.h" #include "arp.h" #include "bpf.h" +#include "common.h" #include "dhcp.h" #include "if.h" #include "logerr.h" /* BPF helper macros */ #ifdef __linux__ -#define BPF_WHOLEPACKET 0x7fffffff /* work around buggy LPF filters */ +#define BPF_WHOLEPACKET 0x7fffffff /* work around buggy LPF filters */ #else -#define BPF_WHOLEPACKET ~0U +#define BPF_WHOLEPACKET ~0U #endif /* Macros to update the BPF structure */ -#define BPF_SET_STMT(insn, c, v) { \ - (insn)->code = (c); \ - (insn)->jt = 0; \ - (insn)->jf = 0; \ - (insn)->k = (uint32_t)(v); \ -} +#define BPF_SET_STMT(insn, c, v) \ + { \ + (insn)->code = (c); \ + (insn)->jt = 0; \ + (insn)->jf = 0; \ + (insn)->k = (uint32_t)(v); \ + } -#define BPF_SET_JUMP(insn, c, v, t, f) { \ - (insn)->code = (c); \ - (insn)->jt = (t); \ - (insn)->jf = (f); \ - (insn)->k = (uint32_t)(v); \ -} +#define BPF_SET_JUMP(insn, c, v, t, f) \ + { \ + (insn)->code = (c); \ + (insn)->jt = (t); \ + (insn)->jf = (f); \ + (insn)->k = (uint32_t)(v); \ + } size_t bpf_frame_header_len(const struct interface *ifp) { - switch (ifp->hwtype) { case ARPHRD_ETHER: return sizeof(struct ether_header); @@ -101,7 +102,7 @@ bpf_frame_header_src(const struct interface *ifp, void *fh, size_t *len) return f + offsetof(struct ether_header, ether_shost); default: *len = 0; - errno = ENOTSUP; + errno = ENOTSUP; return NULL; } } @@ -117,22 +118,20 @@ bpf_frame_header_dst(const struct interface *ifp, void *fh, size_t *len) return f + offsetof(struct ether_header, ether_dhost); default: *len = 0; - errno = ENOTSUP; + errno = ENOTSUP; return NULL; } } -static const uint8_t etherbcastaddr[] = - { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; +static const uint8_t etherbcastaddr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; int bpf_frame_bcast(const struct interface *ifp, const void *frame) { - switch (ifp->hwtype) { case ARPHRD_ETHER: return memcmp((const char *)frame + - offsetof(struct ether_header, ether_dhost), + offsetof(struct ether_header, ether_dhost), etherbcastaddr, sizeof(etherbcastaddr)); default: return -1; @@ -277,9 +276,9 @@ bpf_read(struct bpf *bpf, void *data, size_t len) else bpf->bpf_flags &= ~BPF_BCAST; memcpy(data, payload, (size_t)bytes); -next: - bpf->bpf_pos += BPF_WORDALIGN(packet.bh_hdrlen + - packet.bh_caplen); + next: + bpf->bpf_pos += BPF_WORDALIGN( + packet.bh_hdrlen + packet.bh_caplen); if (bpf->bpf_pos >= bpf->bpf_len) { bpf->bpf_len = bpf->bpf_pos = 0; bpf->bpf_flags |= BPF_EOF; @@ -315,13 +314,12 @@ bpf_wattach(int fd, void *filter, unsigned int filter_len) #ifndef __sun /* SunOS is special too - sending via BPF goes nowhere. */ ssize_t -bpf_send(const struct bpf *bpf, uint16_t protocol, - const void *data, size_t len) +bpf_send(const struct bpf *bpf, uint16_t protocol, const void *data, size_t len) { struct iovec iov[2]; struct ether_header eh; - switch(bpf->bpf_ifp->hwtype) { + switch (bpf->bpf_ifp->hwtype) { case ARPHRD_ETHER: memset(&eh.ether_dhost, 0xff, sizeof(eh.ether_dhost)); memcpy(&eh.ether_shost, bpf->bpf_ifp->hwaddr, @@ -344,17 +342,16 @@ bpf_send(const struct bpf *bpf, uint16_t protocol, void bpf_close(struct bpf *bpf) { - close(bpf->bpf_fd); free(bpf->bpf_buffer); free(bpf); } #ifdef ARP -#define BPF_CMP_HWADDR_LEN ((((HWADDR_LEN / 4) + 2) * 2) + 1) +#define BPF_CMP_HWADDR_LEN ((((HWADDR_LEN / 4) + 2) * 2) + 1) static unsigned int -bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, - bool equal, const uint8_t *hwaddr, size_t hwaddr_len) +bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, bool equal, + const uint8_t *hwaddr, size_t hwaddr_len) { struct bpf_insn *bp; size_t maclen, nlft, njmps; @@ -377,7 +374,6 @@ bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, nlft = nlft % 2; if (nlft) njmps += 2; - } /* Skip to positive finish. */ @@ -392,8 +388,7 @@ bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, bp = bpf; for (; hwaddr_len > 0; - hwaddr += maclen, hwaddr_len -= maclen, off += maclen) - { + hwaddr += maclen, hwaddr_len -= maclen, off += maclen) { if (bpf_len < 3) { errno = ENOBUFS; return 0; @@ -406,20 +401,20 @@ bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, BPF_SET_STMT(bp, BPF_LD + BPF_W + BPF_IND, off); bp++; BPF_SET_JUMP(bp, BPF_JMP + BPF_JEQ + BPF_K, - htonl(mac32), jt, jf); + htonl(mac32), jt, jf); } else if (hwaddr_len >= 2) { maclen = sizeof(mac16); memcpy(&mac16, hwaddr, maclen); BPF_SET_STMT(bp, BPF_LD + BPF_H + BPF_IND, off); bp++; BPF_SET_JUMP(bp, BPF_JMP + BPF_JEQ + BPF_K, - htons(mac16), jt, jf); + htons(mac16), jt, jf); } else { maclen = sizeof(*hwaddr); BPF_SET_STMT(bp, BPF_LD + BPF_B + BPF_IND, off); bp++; - BPF_SET_JUMP(bp, BPF_JMP + BPF_JEQ + BPF_K, - *hwaddr, jt, jf); + BPF_SET_JUMP(bp, BPF_JMP + BPF_JEQ + BPF_K, *hwaddr, jt, + jf); } if (jt) jt = (uint8_t)(jt - 2); @@ -438,10 +433,10 @@ bpf_cmp_hwaddr(struct bpf_insn *bpf, size_t bpf_len, size_t off, #endif #ifdef ARP -static const struct bpf_insn bpf_arp_ether [] = { +static const struct bpf_insn bpf_arp_ether[] = { /* Check this is an ARP packet. */ BPF_STMT(BPF_LD + BPF_H + BPF_ABS, - offsetof(struct ether_header, ether_type)), + offsetof(struct ether_header, ether_type)), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_ARP, 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), @@ -456,12 +451,12 @@ static const struct bpf_insn bpf_arp_ether [] = { /* Make sure the hardware length matches. */ BPF_STMT(BPF_LD + BPF_B + BPF_IND, offsetof(struct arphdr, ar_hln)), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, - sizeof(((struct ether_arp *)0)->arp_sha), 1, 0), + sizeof(((struct ether_arp *)0)->arp_sha), 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), }; -#define BPF_ARP_ETHER_LEN __arraycount(bpf_arp_ether) +#define BPF_ARP_ETHER_LEN __arraycount(bpf_arp_ether) -static const struct bpf_insn bpf_arp_filter [] = { +static const struct bpf_insn bpf_arp_filter[] = { /* Make sure this is for IP. */ BPF_STMT(BPF_LD + BPF_H + BPF_IND, offsetof(struct arphdr, ar_pro)), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), @@ -477,14 +472,15 @@ static const struct bpf_insn bpf_arp_filter [] = { BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, sizeof(in_addr_t), 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), }; -#define BPF_ARP_FILTER_LEN __arraycount(bpf_arp_filter) +#define BPF_ARP_FILTER_LEN __arraycount(bpf_arp_filter) /* One address is two checks of two statements. */ -#define BPF_NADDRS 1 -#define BPF_ARP_ADDRS_LEN 5 + ((BPF_NADDRS * 2) * 2) +#define BPF_NADDRS 1 +#define BPF_ARP_ADDRS_LEN 5 + ((BPF_NADDRS * 2) * 2) -#define BPF_ARP_LEN BPF_ARP_ETHER_LEN + BPF_ARP_FILTER_LEN + \ - BPF_CMP_HWADDR_LEN + BPF_ARP_ADDRS_LEN +#define BPF_ARP_LEN \ + BPF_ARP_ETHER_LEN + BPF_ARP_FILTER_LEN + BPF_CMP_HWADDR_LEN + \ + BPF_ARP_ADDRS_LEN static int bpf_arp_rw(const struct bpf *bpf, const struct in_addr *ia, bool recv) @@ -496,11 +492,12 @@ bpf_arp_rw(const struct bpf *bpf, const struct in_addr *ia, bool recv) bp = buf; /* Check frame header. */ - switch(ifp->hwtype) { + switch (ifp->hwtype) { case ARPHRD_ETHER: memcpy(bp, bpf_arp_ether, sizeof(bpf_arp_ether)); bp += BPF_ARP_ETHER_LEN; - arp_len = sizeof(struct ether_header)+sizeof(struct ether_arp); + arp_len = sizeof(struct ether_header) + + sizeof(struct ether_arp); break; default: errno = EINVAL; @@ -513,7 +510,7 @@ bpf_arp_rw(const struct bpf *bpf, const struct in_addr *ia, bool recv) /* Ensure it's not from us. */ bp += bpf_cmp_hwaddr(bp, BPF_CMP_HWADDR_LEN, sizeof(struct arphdr), - !recv, ifp->hwaddr, ifp->hwlen); + !recv, ifp->hwaddr, ifp->hwlen); /* Match sender protocol address */ BPF_SET_STMT(bp, BPF_LD + BPF_W + BPF_IND, @@ -532,8 +529,9 @@ bpf_arp_rw(const struct bpf *bpf, const struct in_addr *ia, bool recv) bp++; /* Match target protocol address */ - BPF_SET_STMT(bp, BPF_LD + BPF_W + BPF_IND, (sizeof(struct arphdr) + - (size_t)(ifp->hwlen * 2) + sizeof(in_addr_t))); + BPF_SET_STMT(bp, BPF_LD + BPF_W + BPF_IND, + (sizeof(struct arphdr) + (size_t)(ifp->hwlen * 2) + + sizeof(in_addr_t))); bp++; BPF_SET_JUMP(bp, BPF_JMP + BPF_JEQ + BPF_K, htonl(ia->s_addr), 0, 1); bp++; @@ -555,7 +553,6 @@ bpf_arp_rw(const struct bpf *bpf, const struct in_addr *ia, bool recv) int bpf_arp(const struct bpf *bpf, const struct in_addr *ia) { - #ifdef BIOCSETWF if (bpf_arp_rw(bpf, ia, true) == -1 || bpf_arp_rw(bpf, ia, false) == -1 || @@ -569,22 +566,21 @@ bpf_arp(const struct bpf *bpf, const struct in_addr *ia) #endif #ifdef ARPHRD_NONE -static const struct bpf_insn bpf_bootp_none[] = { -}; -#define BPF_BOOTP_NONE_LEN __arraycount(bpf_bootp_none) +static const struct bpf_insn bpf_bootp_none[] = {}; +#define BPF_BOOTP_NONE_LEN __arraycount(bpf_bootp_none) #endif static const struct bpf_insn bpf_bootp_ether[] = { /* Make sure this is an IP packet. */ BPF_STMT(BPF_LD + BPF_H + BPF_ABS, - offsetof(struct ether_header, ether_type)), + offsetof(struct ether_header, ether_type)), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ETHERTYPE_IP, 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), /* Advance to the IP header. */ BPF_STMT(BPF_LDX + BPF_K, sizeof(struct ether_header)), }; -#define BPF_BOOTP_ETHER_LEN __arraycount(bpf_bootp_ether) +#define BPF_BOOTP_ETHER_LEN __arraycount(bpf_bootp_ether) static const struct bpf_insn bpf_bootp_base[] = { /* Make sure it's an IPv4 packet. */ @@ -610,7 +606,7 @@ static const struct bpf_insn bpf_bootp_base[] = { BPF_STMT(BPF_ALU + BPF_ADD + BPF_X, 0), BPF_STMT(BPF_MISC + BPF_TAX, 0), }; -#define BPF_BOOTP_BASE_LEN __arraycount(bpf_bootp_base) +#define BPF_BOOTP_BASE_LEN __arraycount(bpf_bootp_base) static const struct bpf_insn bpf_bootp_read[] = { /* Make sure it's to the right port. @@ -619,7 +615,7 @@ static const struct bpf_insn bpf_bootp_read[] = { BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, BOOTPC, 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), }; -#define BPF_BOOTP_READ_LEN __arraycount(bpf_bootp_read) +#define BPF_BOOTP_READ_LEN __arraycount(bpf_bootp_read) #ifdef BIOCSETWF static const struct bpf_insn bpf_bootp_write[] = { @@ -630,15 +626,15 @@ static const struct bpf_insn bpf_bootp_write[] = { BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (BOOTPC << 16) + BOOTPS, 1, 0), BPF_STMT(BPF_RET + BPF_K, 0), }; -#define BPF_BOOTP_WRITE_LEN __arraycount(bpf_bootp_write) +#define BPF_BOOTP_WRITE_LEN __arraycount(bpf_bootp_write) #endif -#define BPF_BOOTP_CHADDR_LEN ((BOOTP_CHADDR_LEN / 4) * 3) -#define BPF_BOOTP_XID_LEN 4 /* BOUND check is 4 instructions */ +#define BPF_BOOTP_CHADDR_LEN ((BOOTP_CHADDR_LEN / 4) * 3) +#define BPF_BOOTP_XID_LEN 4 /* BOUND check is 4 instructions */ -#define BPF_BOOTP_LEN BPF_BOOTP_ETHER_LEN + \ - BPF_BOOTP_BASE_LEN + BPF_BOOTP_READ_LEN + \ - BPF_BOOTP_XID_LEN + BPF_BOOTP_CHADDR_LEN + 4 +#define BPF_BOOTP_LEN \ + BPF_BOOTP_ETHER_LEN + BPF_BOOTP_BASE_LEN + BPF_BOOTP_READ_LEN + \ + BPF_BOOTP_XID_LEN + BPF_BOOTP_CHADDR_LEN + 4 static int bpf_bootp_rw(const struct bpf *bpf, bool read) @@ -648,7 +644,7 @@ bpf_bootp_rw(const struct bpf *bpf, bool read) bp = buf; /* Check frame header. */ - switch(bpf->bpf_ifp->hwtype) { + switch (bpf->bpf_ifp->hwtype) { #ifdef ARPHRD_NONE case ARPHRD_NONE: memcpy(bp, bpf_bootp_none, sizeof(bpf_bootp_none)); @@ -696,10 +692,8 @@ bpf_bootp_rw(const struct bpf *bpf, bool read) int bpf_bootp(const struct bpf *bpf, __unused const struct in_addr *ia) { - #ifdef BIOCSETWF - if (bpf_bootp_rw(bpf, true) == -1 || - bpf_bootp_rw(bpf, false) == -1 || + if (bpf_bootp_rw(bpf, true) == -1 || bpf_bootp_rw(bpf, false) == -1 || ioctl(bpf->bpf_fd, BIOCLOCK) == -1) return -1; return 0; diff --git a/src/bpf.h b/src/bpf.h index 1ffcd65a..dc80341f 100644 --- a/src/bpf.h +++ b/src/bpf.h @@ -29,9 +29,9 @@ #ifndef BPF_HEADER #define BPF_HEADER -#define BPF_EOF 0x01U -#define BPF_PARTIALCSUM 0x02U -#define BPF_BCAST 0x04U +#define BPF_EOF 0x01U +#define BPF_PARTIALCSUM 0x02U +#define BPF_BCAST 0x04U /* * Even though we program the BPF filter should we trust it? @@ -50,7 +50,7 @@ * If you want to be notified of any packet failing the BPF filter, * define BPF_DEBUG below. */ -//#define BPF_DEBUG +// #define BPF_DEBUG #include "dhcpcd.h" @@ -69,7 +69,7 @@ size_t bpf_frame_header_len(const struct interface *); void *bpf_frame_header_src(const struct interface *, void *, size_t *); void *bpf_frame_header_dst(const struct interface *, void *, size_t *); int bpf_frame_bcast(const struct interface *, const void *); -struct bpf * bpf_open(const struct interface *, +struct bpf *bpf_open(const struct interface *, int (*)(const struct bpf *, const struct in_addr *), const struct in_addr *); void bpf_close(struct bpf *); diff --git a/src/common.c b/src/common.c index 6a266892..73ea820a 100644 --- a/src/common.c +++ b/src/common.c @@ -65,10 +65,10 @@ hwaddr_ntoa(const void *hwaddr, size_t hwlen, char *buf, size_t buflen) p = buf; while (hp < ep) { if (hp != hwaddr) - *p ++= ':'; + *p++ = ':'; p += snprintf(p, 3, "%.2x", *hp++); } - *p ++= '\0'; + *p++ = '\0'; return buf; } @@ -85,16 +85,15 @@ hwaddr_aton(uint8_t *buffer, const char *addr) /* Skip separators */ c[0] = *p++; switch (c[0]) { - case '\n': /* long duid split on lines */ - case ':': /* typical mac address */ - case '-': /* uuid */ + case '\n': /* long duid split on lines */ + case ':': /* typical mac address */ + case '-': /* uuid */ continue; } c[1] = *p++; /* Ensure that digits are hex */ if (isxdigit((unsigned char)c[0]) == 0 || - isxdigit((unsigned char)c[1]) == 0) - { + isxdigit((unsigned char)c[1]) == 0) { errno = EINVAL; return 0; } @@ -158,7 +157,7 @@ filemtime(const char *file, time_t *time) * We strip leading space and avoid comment lines, making the code that calls * us smaller. */ char * -get_line(char ** __restrict buf, ssize_t * __restrict buflen) +get_line(char **__restrict buf, ssize_t *__restrict buflen) { char *p, *c; bool quoted; @@ -200,7 +199,6 @@ get_line(char ** __restrict buf, ssize_t * __restrict buflen) return p; } - int is_root_local(void) { @@ -217,7 +215,8 @@ is_root_local(void) } uint32_t -lifetime_left(uint32_t lifetime, const struct timespec *acquired, struct timespec *now) +lifetime_left(uint32_t lifetime, const struct timespec *acquired, + struct timespec *now) { uint32_t elapsed; struct timespec n; diff --git a/src/common.h b/src/common.h index a2a7abda..c2fd4b6b 100644 --- a/src/common.h +++ b/src/common.h @@ -29,72 +29,73 @@ #ifndef COMMON_H #define COMMON_H +#include #include #include -#include + #include #include /* Define eloop queues here, as other apps share eloop.h */ -#define ELOOP_DHCPCD 1 /* default queue */ -#define ELOOP_DHCP 2 -#define ELOOP_ARP 3 -#define ELOOP_IPV4LL 4 -#define ELOOP_IPV6 5 -#define ELOOP_IPV6ND 6 -#define ELOOP_IPV6RA_EXPIRE 7 -#define ELOOP_DHCP6 8 -#define ELOOP_IF 9 +#define ELOOP_DHCPCD 1 /* default queue */ +#define ELOOP_DHCP 2 +#define ELOOP_ARP 3 +#define ELOOP_IPV4LL 4 +#define ELOOP_IPV6 5 +#define ELOOP_IPV6ND 6 +#define ELOOP_IPV6RA_EXPIRE 7 +#define ELOOP_DHCP6 8 +#define ELOOP_IF 9 #ifndef HOSTNAME_MAX_LEN -#define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */ +#define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */ #endif #ifndef MIN -#define MIN(a,b) ((/*CONSTCOND*/(a)<(b))?(a):(b)) -#define MAX(a,b) ((/*CONSTCOND*/(a)>(b))?(a):(b)) +#define MIN(a, b) ((/*CONSTCOND*/ (a) < (b)) ? (a) : (b)) +#define MAX(a, b) ((/*CONSTCOND*/ (a) > (b)) ? (a) : (b)) #endif -#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a)) -#define STRINGIFY(a) #a -#define TOSTRING(a) STRINGIFY(a) -#define UNUSED(a) (void)(a) +#define UNCONST(a) ((void *)(uintptr_t)(const void *)(a)) +#define STRINGIFY(a) #a +#define TOSTRING(a) STRINGIFY(a) +#define UNUSED(a) (void)(a) -#define ROUNDUP4(a) (1 + (((a) - 1) | 3)) -#define ROUNDUP8(a) (1 + (((a) - 1) | 7)) +#define ROUNDUP4(a) (1 + (((a) - 1) | 3)) +#define ROUNDUP8(a) (1 + (((a) - 1) | 7)) /* Some systems don't define timespec macros */ #ifndef timespecclear -#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L) -#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec) -#define timespecadd(tsp, usp, vsp) \ - do { \ - (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ - (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ - if ((vsp)->tv_nsec >= 1000000000L) { \ - (vsp)->tv_sec++; \ - (vsp)->tv_nsec -= 1000000000L; \ - } \ +#define timespecclear(tsp) (tsp)->tv_sec = (time_t)((tsp)->tv_nsec = 0L) +#define timespecisset(tsp) ((tsp)->tv_sec || (tsp)->tv_nsec) +#define timespecadd(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec + (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec + (usp)->tv_nsec; \ + if ((vsp)->tv_nsec >= 1000000000L) { \ + (vsp)->tv_sec++; \ + (vsp)->tv_nsec -= 1000000000L; \ + } \ } while (0) -#define timespecsub(tsp, usp, vsp) \ - do { \ - (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ - (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ - if ((vsp)->tv_nsec < 0) { \ - (vsp)->tv_sec--; \ - (vsp)->tv_nsec += 1000000000L; \ - } \ +#define timespecsub(tsp, usp, vsp) \ + do { \ + (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec; \ + (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec; \ + if ((vsp)->tv_nsec < 0) { \ + (vsp)->tv_sec--; \ + (vsp)->tv_nsec += 1000000000L; \ + } \ } while (0) #endif #if __GNUC__ > 2 || defined(__INTEL_COMPILER) -# ifndef __unused -# define __unused __attribute__((__unused__)) -# endif +#ifndef __unused +#define __unused __attribute__((__unused__)) +#endif #else -# ifndef __unused -# define __unused -# endif +#ifndef __unused +#define __unused +#endif #endif /* Needed for rbtree(3) compat */ @@ -102,61 +103,64 @@ #define __RCSID(a) #endif #ifndef __predict_false -# if __GNUC__ > 2 -# define __predict_true(exp) __builtin_expect((exp) != 0, 1) -# define __predict_false(exp) __builtin_expect((exp) != 0, 0) +#if __GNUC__ > 2 +#define __predict_true(exp) __builtin_expect((exp) != 0, 1) +#define __predict_false(exp) __builtin_expect((exp) != 0, 0) #else -# define __predict_true(exp) (exp) -# define __predict_false(exp) (exp) -# endif +#define __predict_true(exp) (exp) +#define __predict_false(exp) (exp) +#endif #endif #ifndef __BEGIN_DECLS -# if defined(__cplusplus) -# define __BEGIN_DECLS extern "C" { -# define __END_DECLS }; -# else /* __BEGIN_DECLS */ -# define __BEGIN_DECLS -# define __END_DECLS -# endif /* __BEGIN_DECLS */ +#if defined(__cplusplus) +#define __BEGIN_DECLS extern "C" { +#define __END_DECLS \ + } \ + ; +#else /* __BEGIN_DECLS */ +#define __BEGIN_DECLS +#define __END_DECLS +#endif /* __BEGIN_DECLS */ #endif /* __BEGIN_DECLS */ #ifndef __fallthrough -# if __GNUC__ >= 7 -# define __fallthrough __attribute__((fallthrough)) -# else -# define __fallthrough -# endif +#if __GNUC__ >= 7 +#define __fallthrough __attribute__((fallthrough)) +#else +#define __fallthrough +#endif #endif /* * Compile Time Assertion. */ #ifndef __CTASSERT -# ifdef __COUNTER__ -# define __CTASSERT(x) __CTASSERT0(x, __ctassert, __COUNTER__) -# else -# define __CTASSERT(x) __CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__) -# define __CTASSERT99(x, a, b) __CTASSERT0(x, __CONCAT(__ctassert,a), \ - __CONCAT(_,b)) -# endif -# define __CTASSERT0(x, y, z) __CTASSERT1(x, y, z) -# define __CTASSERT1(x, y, z) typedef char y ## z[/*CONSTCOND*/(x) ? 1 : -1] __unused +#ifdef __COUNTER__ +#define __CTASSERT(x) __CTASSERT0(x, __ctassert, __COUNTER__) +#else +#define __CTASSERT(x) __CTASSERT99(x, __INCLUDE_LEVEL__, __LINE__) +#define __CTASSERT99(x, a, b) \ + __CTASSERT0(x, __CONCAT(__ctassert, a), __CONCAT(_, b)) +#endif +#define __CTASSERT0(x, y, z) __CTASSERT1(x, y, z) +#define __CTASSERT1(x, y, z) \ + typedef char y##z[/*CONSTCOND*/ (x) ? 1 : -1] __unused #endif #ifndef __arraycount -# define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) +#define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) #endif /* We don't really need this as our supported systems define __restrict * automatically for us, but it is here for completeness. */ #ifndef __restrict -# if defined(__lint__) -# define __restrict -# elif __STDC_VERSION__ >= 199901L -# define __restrict restrict -# elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__)) -# define __restrict -# endif +#if defined(__lint__) +#define __restrict +#elif __STDC_VERSION__ >= 199901L +#define __restrict restrict +#elif !(2 < __GNUC__ || (2 == __GNU_C && 95 <= __GNUC_VERSION__)) +#define __restrict +#endif #endif #define INFINITE_LIFETIME (~0U) @@ -166,7 +170,7 @@ size_t hwaddr_aton(uint8_t *, const char *); ssize_t readfile(const char *, void *, size_t); ssize_t writefile(const char *, mode_t, const void *, size_t); int filemtime(const char *, time_t *); -char *get_line(char ** __restrict, ssize_t * __restrict); +char *get_line(char **__restrict, ssize_t *__restrict); int is_root_local(void); uint32_t lifetime_left(uint32_t, const struct timespec *, struct timespec *); #endif diff --git a/src/control.c b/src/control.c index ec1c633c..efb03089 100644 --- a/src/control.c +++ b/src/control.c @@ -39,10 +39,10 @@ #include #include -#include "config.h" #include "common.h" -#include "dhcpcd.h" +#include "config.h" #include "control.h" +#include "dhcpcd.h" #include "eloop.h" #include "if.h" #include "logerr.h" @@ -50,7 +50,7 @@ #ifndef SUN_LEN #define SUN_LEN(su) \ - (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) + (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path)) #endif static void control_handle_data(void *, unsigned short); @@ -80,7 +80,6 @@ control_queue_free(struct fd_list *fd) void control_free(struct fd_list *fd) { - #ifdef PRIVSEP if (fd->ctx->ps_control_client == fd) fd->ctx->ps_control_client = NULL; @@ -96,7 +95,6 @@ control_free(struct fd_list *fd) static void control_hangup(struct fd_list *fd) { - #ifdef PRIVSEP if (IN_PRIVSEP(fd->ctx)) { if (ps_ctl_sendeof(fd) == -1) @@ -196,7 +194,7 @@ control_handle_write(struct fd_list *fd) /* Done sending data, stop watching write to fd */ if (eloop_event_add(fd->ctx->eloop, fd->fd, ELE_READ, - control_handle_data, fd) == -1) + control_handle_data, fd) == -1) logerr("%s: eloop_event_add", __func__); return 0; } @@ -323,14 +321,14 @@ control_handle1(struct dhcpcd_ctx *ctx, int lfd, unsigned int fd_flags, ; else #endif - fd_flags |= FD_SENDLEN; + fd_flags |= FD_SENDLEN; l = control_new(ctx, fd, fd_flags); if (l == NULL) goto error; - if (eloop_event_add(ctx->eloop, l->fd, ELE_READ, - control_handle_data, l) == -1) + if (eloop_event_add(ctx->eloop, l->fd, ELE_READ, control_handle_data, + l) == -1) logerr("%s: eloop_event_add", __func__); return; @@ -363,7 +361,7 @@ make_path(char *path, size_t len, const char *ifname, sa_family_t family, const char *per; const char *sunpriv; - switch(family) { + switch (family) { case AF_INET: per = "-4"; break; @@ -378,9 +376,8 @@ make_path(char *path, size_t len, const char *ifname, sa_family_t family, sunpriv = ifname ? ".unpriv" : "unpriv."; else sunpriv = ""; - return snprintf(path, len, CONTROLSOCKET, - ifname ? ifname : "", ifname ? per : "", - sunpriv, ifname ? "." : ""); + return snprintf(path, len, CONTROLSOCKET, ifname ? ifname : "", + ifname ? per : "", sunpriv, ifname ? "." : ""); } static int @@ -397,7 +394,7 @@ make_sock(struct sockaddr_un *sa, const char *ifname, sa_family_t family, return fd; } -#define S_PRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) +#define S_PRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) #define S_UNPRIV (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) static int @@ -417,9 +414,8 @@ control_start1(struct dhcpcd_ctx *ctx, const char *ifname, sa_family_t family, if (bind(fd, (struct sockaddr *)&sa, len) == -1 || chmod(sa.sun_path, fmode) == -1 || (ctx->control_group && - chown(sa.sun_path, geteuid(), ctx->control_group) == -1) || - listen(fd, sizeof(ctx->control_fds)) == -1) - { + chown(sa.sun_path, geteuid(), ctx->control_group) == -1) || + listen(fd, sizeof(ctx->control_fds)) == -1) { close(fd); unlink(sa.sun_path); return -1; @@ -449,11 +445,10 @@ control_start(struct dhcpcd_ctx *ctx, const char *ifname, sa_family_t family) #ifdef PRIVSEP if (IN_PRIVSEP_SE(ctx)) { - make_path(ctx->control_sock, sizeof(ctx->control_sock), - ifname, family, false); + make_path(ctx->control_sock, sizeof(ctx->control_sock), ifname, + family, false); make_path(ctx->control_sock_unpriv, - sizeof(ctx->control_sock_unpriv), - ifname, family, true); + sizeof(ctx->control_sock_unpriv), ifname, family, true); return 0; } #endif @@ -462,14 +457,14 @@ control_start(struct dhcpcd_ctx *ctx, const char *ifname, sa_family_t family) return -1; ctx->control_fd = fd; - if (eloop_event_add(ctx->eloop, fd, ELE_READ, - control_handle, ctx) == -1) + if (eloop_event_add(ctx->eloop, fd, ELE_READ, control_handle, ctx) == + -1) logerr("%s: eloop_event_add", __func__); if ((fd = control_start1(ctx, ifname, family, S_UNPRIV)) != -1) { ctx->control_unpriv_fd = fd; if (eloop_event_add(ctx->eloop, fd, ELE_READ, - control_handle_unpriv, ctx) == -1) + control_handle_unpriv, ctx) == -1) logerr("%s: eloop_event_add", __func__); } return ctx->control_fd; @@ -486,7 +481,7 @@ control_unlink(struct dhcpcd_ctx *ctx, const char *file) retval = (int)ps_root_unlink(ctx, file); else #else - UNUSED(ctx); + UNUSED(ctx); #endif retval = unlink(file); @@ -554,7 +549,7 @@ control_open(const char *ifname, sa_family_t family, bool unpriv) } ssize_t -control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv) +control_send(struct dhcpcd_ctx *ctx, int argc, char *const *argv) { char buffer[1024]; int i; diff --git a/src/control.h b/src/control.h index ef592268..7900e5c0 100644 --- a/src/control.h +++ b/src/control.h @@ -29,19 +29,20 @@ #ifndef CONTROL_H #define CONTROL_H +#include + #include -#include "dhcpcd.h" #include "queue.h" #if !defined(CTL_FREE_LIST) -#define CTL_FREE_LIST 1 +#define CTL_FREE_LIST 1 #elif CTL_FREE_LIST == 0 -#undef CTL_FREE_LIST +#undef CTL_FREE_LIST #endif /* Limit queue size per fd */ -#define CONTROL_QUEUE_MAX 100 +#define CONTROL_QUEUE_MAX 100 struct fd_data { TAILQ_ENTRY(fd_data) next; @@ -64,14 +65,14 @@ struct fd_list { }; TAILQ_HEAD(fd_list_head, fd_list); -#define FD_LISTEN 0x01U -#define FD_UNPRIV 0x02U -#define FD_SENDLEN 0x04U +#define FD_LISTEN 0x01U +#define FD_UNPRIV 0x02U +#define FD_SENDLEN 0x04U int control_start(struct dhcpcd_ctx *, const char *, sa_family_t); int control_stop(struct dhcpcd_ctx *); int control_open(const char *, sa_family_t, bool); -ssize_t control_send(struct dhcpcd_ctx *, int, char * const *); +ssize_t control_send(struct dhcpcd_ctx *, int, char *const *); struct fd_list *control_new(struct dhcpcd_ctx *, int, unsigned int); void control_free(struct fd_list *); void control_delete(struct fd_list *); diff --git a/src/defs.h b/src/defs.h index 637e8440..57db7550 100644 --- a/src/defs.h +++ b/src/defs.h @@ -28,51 +28,51 @@ #ifndef DEFS_H #define DEFS_H -#define PACKAGE "dhcpcd" -#define VERSION "10.3.2" +#define PACKAGE "dhcpcd" +#define VERSION "10.3.2" #ifndef PRIVSEP_USER -# define PRIVSEP_USER "_" PACKAGE +#define PRIVSEP_USER "_" PACKAGE #endif #ifndef CONFIG -# define CONFIG SYSCONFDIR "/" PACKAGE ".conf" +#define CONFIG SYSCONFDIR "/" PACKAGE ".conf" #endif #ifndef SCRIPT -# define SCRIPT LIBEXECDIR "/" PACKAGE "-run-hooks" +#define SCRIPT LIBEXECDIR "/" PACKAGE "-run-hooks" #endif #ifndef DEVDIR -# define DEVDIR LIBDIR "/" PACKAGE "/dev" +#define DEVDIR LIBDIR "/" PACKAGE "/dev" #endif #ifndef DUID -# define DUID DBDIR "/duid" +#define DUID DBDIR "/duid" #endif #ifndef SECRET -# define SECRET DBDIR "/secret" +#define SECRET DBDIR "/secret" #endif #ifndef LEASEFILE -# define LEASEFILE DBDIR "/%s%s.lease" +#define LEASEFILE DBDIR "/%s%s.lease" #endif #ifndef LEASEFILE6 -# define LEASEFILE6 LEASEFILE "6" +#define LEASEFILE6 LEASEFILE "6" #endif #ifndef PIDFILE -# define PIDFILE RUNDIR "/%s%s%spid" +#define PIDFILE RUNDIR "/%s%s%spid" #endif #ifndef CONTROLSOCKET -# define CONTROLSOCKET RUNDIR "/%s%s%s%ssock" +#define CONTROLSOCKET RUNDIR "/%s%s%s%ssock" #endif #ifndef RDM_MONOFILE -# define RDM_MONOFILE DBDIR "/rdm_monotonic" +#define RDM_MONOFILE DBDIR "/rdm_monotonic" #endif #ifndef NO_SIGNALS -# define USE_SIGNALS +#define USE_SIGNALS #endif #ifndef USE_SIGNALS -# ifndef THERE_IS_NO_FORK -# define THERE_IS_NO_FORK -# endif +#ifndef THERE_IS_NO_FORK +#define THERE_IS_NO_FORK +#endif #endif #endif diff --git a/src/dev.c b/src/dev.c index c20a3f49..75feef62 100644 --- a/src/dev.c +++ b/src/dev.c @@ -34,14 +34,13 @@ #define _INDEV #include "common.h" #include "dev.h" -#include "eloop.h" #include "dhcpcd.h" +#include "eloop.h" #include "logerr.h" int dev_initialised(struct dhcpcd_ctx *ctx, const char *ifname) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) @@ -56,7 +55,6 @@ dev_initialised(struct dhcpcd_ctx *ctx, const char *ifname) int dev_listening(struct dhcpcd_ctx *ctx) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) @@ -71,7 +69,6 @@ dev_listening(struct dhcpcd_ctx *ctx) static void dev_stop1(struct dhcpcd_ctx *ctx, int stop) { - if (ctx->dev) { if (stop) logdebugx("dev: unloaded %s", ctx->dev->name); @@ -90,7 +87,6 @@ dev_stop1(struct dhcpcd_ctx *ctx, int stop) void dev_stop(struct dhcpcd_ctx *ctx) { - dev_stop1(ctx, !(ctx->options & DHCPCD_FORKED)); } @@ -121,7 +117,7 @@ dev_start2(struct dhcpcd_ctx *ctx, const struct dev_dhcpcd *dev_dhcpcd, return -1; } fptr(ctx->dev, dev_dhcpcd); - if (ctx->dev->start == NULL || (r = ctx->dev->start()) == -1) { + if (ctx->dev->start == NULL || (r = ctx->dev->start()) == -1) { free(ctx->dev); ctx->dev = NULL; dlclose(h); @@ -195,8 +191,7 @@ dev_start(struct dhcpcd_ctx *ctx, int (*handler)(void *, int, const char *)) ctx->dev_fd = dev_start1(ctx, &dev_dhcpcd); if (ctx->dev_fd != -1) { if (eloop_event_add(ctx->eloop, ctx->dev_fd, ELE_READ, - dev_handle_data, ctx) == -1) - { + dev_handle_data, ctx) == -1) { logerr(__func__); dev_stop1(ctx, 1); return -1; diff --git a/src/dev/Makefile b/src/dev/Makefile index 26e2e60d..a1a318f2 100644 --- a/src/dev/Makefile +++ b/src/dev/Makefile @@ -43,3 +43,5 @@ install: proginstall clean: rm -f ${CLEANFILES} + +include ${TOP}/src//Makefile.inc diff --git a/src/dev/udev.c b/src/dev/udev.c index 5cef941c..d380f1a9 100644 --- a/src/dev/udev.c +++ b/src/dev/udev.c @@ -26,12 +26,12 @@ */ #ifdef LIBUDEV_NOINIT -# define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE -# warning This version of udev is too old does not support -# warning per device initialization checks. -# warning As such, dhcpcd will need to depend on the -# warning udev-settle service or similar if starting -# warning in master mode. +#define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE +#warning This version of udev is too old does not support +#warning per device initialization checks. +#warning As such, dhcpcd will need to depend on the +#warning udev-settle service or similar if starting +#warning in master mode. #endif #include @@ -51,7 +51,6 @@ static struct dev_dhcpcd dhcpcd; static int udev_listening(void) { - return monitor ? 1 : 0; } @@ -106,7 +105,6 @@ udev_handle_device(void *ctx) static void udev_stop(void) { - if (monitor) { udev_monitor_unref(monitor); monitor = NULL; @@ -146,9 +144,8 @@ udev_start(void) goto bad; } #ifndef LIBUDEV_NOFILTER - if (udev_monitor_filter_add_match_subsystem_devtype(monitor, - "net", NULL) != 0) - { + if (udev_monitor_filter_add_match_subsystem_devtype(monitor, "net", + NULL) != 0) { logerr("udev_monitor_filter_add_match_subsystem_devtype"); goto bad; } @@ -172,7 +169,6 @@ udev_start(void) int dev_init(struct dev *dev, const struct dev_dhcpcd *dev_dhcpcd) { - dev->name = udev_name; dev->initialised = udev_initialised; dev->listening = udev_listening; diff --git a/src/dhcp-common.c b/src/dhcp-common.c index 14d2d92a..755b6fb3 100644 --- a/src/dhcp-common.c +++ b/src/dhcp-common.c @@ -36,9 +36,8 @@ #include #include -#include "config.h" - #include "common.h" +#include "config.h" #include "dhcp-common.h" #include "dhcp.h" #include "if.h" @@ -49,7 +48,6 @@ const char * dhcp_get_hostname(char *buf, size_t buf_len, const struct if_options *ifo) { - if (ifo->hostname[0] == '\0') { if (gethostname(buf, buf_len) != 0) return NULL; @@ -58,8 +56,7 @@ dhcp_get_hostname(char *buf, size_t buf_len, const struct if_options *ifo) strlcpy(buf, ifo->hostname, buf_len); /* Deny sending of these local hostnames */ - if (buf[0] == '\0' || buf[0] == '.' || - strcmp(buf, "(none)") == 0 || + if (buf[0] == '\0' || buf[0] == '.' || strcmp(buf, "(none)") == 0 || strcmp(buf, "localhost") == 0 || strncmp(buf, "localhost.", strlen("localhost.")) == 0) return NULL; @@ -79,7 +76,6 @@ dhcp_get_hostname(char *buf, size_t buf_len, const struct if_options *ifo) void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols) { - while (cols < 40) { putchar(' '); cols++; @@ -150,13 +146,10 @@ vivso_find(uint32_t iana_en, const void *arg) ifp = arg; for (i = 0, opt = ifp->options->vivso_override; - i < ifp->options->vivso_override_len; - i++, opt++) + i < ifp->options->vivso_override_len; i++, opt++) if (opt->option == iana_en) return opt; - for (i = 0, opt = ifp->ctx->vivso; - i < ifp->ctx->vivso_len; - i++, opt++) + for (i = 0, opt = ifp->ctx->vivso; i < ifp->ctx->vivso_len; i++, opt++) if (opt->option == iana_en) return opt; return NULL; @@ -170,12 +163,10 @@ dhcp_vendor(char *str, size_t len) int l; if (uname(&utn) == -1) - return (ssize_t)snprintf(str, len, "%s-%s", - PACKAGE, VERSION); + return (ssize_t)snprintf(str, len, "%s-%s", PACKAGE, VERSION); p = str; - l = snprintf(p, len, - "%s-%s:%s-%s:%s", PACKAGE, VERSION, - utn.sysname, utn.release, utn.machine); + l = snprintf(p, len, "%s-%s:%s-%s:%s", PACKAGE, VERSION, utn.sysname, + utn.release, utn.machine); if (l == -1 || (size_t)(l + 1) > len) return -1; p += l; @@ -190,8 +181,8 @@ dhcp_vendor(char *str, size_t len) int make_option_mask(const struct dhcp_opt *dopts, size_t dopts_len, - const struct dhcp_opt *odopts, size_t odopts_len, - uint8_t *mask, const char *opts, int add) + const struct dhcp_opt *odopts, size_t odopts_len, uint8_t *mask, + const char *opts, int add) { char *token, *o, *p; const struct dhcp_opt *opt; @@ -216,8 +207,8 @@ make_option_mask(const struct dhcp_opt *dopts, size_t dopts_len, if (strcmp(opt->var, token) == 0) match = 1; else { - n = (unsigned int)strtou(token, NULL, 0, - 0, UINT_MAX, &e); + n = (unsigned int)strtou(token, NULL, 0, 0, + UINT_MAX, &e); if (e == 0 && opt->option == n) match = 1; } @@ -227,7 +218,7 @@ make_option_mask(const struct dhcp_opt *dopts, size_t dopts_len, if (match == 0) { for (i = 0, opt = dopts; i < dopts_len; i++, opt++) { if (strcmp(opt->var, token) == 0) - match = 1; + match = 1; else { n = (unsigned int)strtou(token, NULL, 0, 0, UINT_MAX, &e); @@ -341,8 +332,7 @@ decode_rfc1035(char *out, size_t len, const uint8_t *p, size_t pl) * combinations. */ errno = ENOTSUP; return -1; - } - else if (ltype == 0xc0) { /* pointer */ + } else if (ltype == 0xc0) { /* pointer */ if (q == e) { errno = ERANGE; return -1; @@ -455,10 +445,9 @@ valid_domainname(char *lbl, int type) len = 0; continue; } - if (((c == '-' || c == '_') && - !start && *lbl != ' ' && *lbl != '\0') || - isalnum(c)) - { + if (((c == '-' || c == '_') && !start && *lbl != ' ' && + *lbl != '\0') || + isalnum(c)) { if (++len > NS_MAXLABEL) { errno = ERANGE; errset = true; @@ -502,7 +491,7 @@ print_string(char *dst, size_t len, int type, const uint8_t *data, size_t dl) c = *data++; if (type & OT_BINHEX) { if (dst) { - if (len == 0 || len == 1) { + if (len == 0 || len == 1) { errno = ENOBUFS; return -1; } @@ -518,8 +507,7 @@ print_string(char *dst, size_t len, int type, const uint8_t *data, size_t dl) break; } if (!(type & (OT_ASCII | OT_RAW | OT_ESCSTRING | OT_ESCFILE)) && - (!isascii(c) && !isprint(c))) - { + (!isascii(c) && !isprint(c))) { errno = EINVAL; break; } @@ -528,17 +516,17 @@ print_string(char *dst, size_t len, int type, const uint8_t *data, size_t dl) break; } if ((type & (OT_ESCSTRING | OT_ESCFILE) && - (c == '\\' || !isascii(c) || !isprint(c))) || - (type & OT_ESCFILE && (c == '/' || c == ' '))) - { + (c == '\\' || !isascii(c) || !isprint(c))) || + (type & OT_ESCFILE && (c == '/' || c == ' '))) { errno = EINVAL; if (c == '\\') { if (dst) { - if (len == 0 || len == 1) { + if (len == 0 || len == 1) { errno = ENOBUFS; return -1; } - *dst++ = '\\'; *dst++ = '\\'; + *dst++ = '\\'; + *dst++ = '\\'; len -= 2; } bytes += 2; @@ -550,9 +538,9 @@ print_string(char *dst, size_t len, int type, const uint8_t *data, size_t dl) return -1; } *dst++ = '\\'; - *dst++ = (char)(((c >> 6) & 03) + '0'); - *dst++ = (char)(((c >> 3) & 07) + '0'); - *dst++ = (char)(( c & 07) + '0'); + *dst++ = (char)(((c >> 6) & 03) + '0'); + *dst++ = (char)(((c >> 3) & 07) + '0'); + *dst++ = (char)((c & 07) + '0'); len -= 4; } bytes += 4; @@ -582,13 +570,12 @@ print_string(char *dst, size_t len, int type, const uint8_t *data, size_t dl) *odst = '\0'; return 1; } - } return (ssize_t)bytes; } -#define ADDR6SZ 16 +#define ADDR6SZ 16 static ssize_t dhcp_optlen(const struct dhcp_opt *opt, size_t dl) { @@ -631,8 +618,7 @@ dhcp_optlen(const struct dhcp_opt *opt, size_t dl) static ssize_t print_option(FILE *fp, const char *prefix, const struct dhcp_opt *opt, - int vname, - const uint8_t *data, size_t dl, const char *ifname) + int vname, const uint8_t *data, size_t dl, const char *ifname) { fpos_t fp_pos; const uint8_t *e, *t; @@ -715,8 +701,8 @@ print_option(FILE *fp, const char *prefix, const struct dhcp_opt *opt, goto err; } - if (print_string(buf, sizeof(buf), - opt->type, data, sz) == -1) + if (print_string(buf, sizeof(buf), opt->type, data, + sz) == -1) goto err; if (first) @@ -752,9 +738,7 @@ print_option(FILE *fp, const char *prefix, const struct dhcp_opt *opt, /* bitflags are a string, MSB first, such as ABCDEFGH * where A is 10000000, B is 01000000, etc. */ for (l = 0, sl = sizeof(opt->bitflags) - 1; - l < sizeof(opt->bitflags); - l++, sl--) - { + l < sizeof(opt->bitflags); l++, sl--) { if (opt->bitflags[l] == '1') { if (fprintf(fp, "%d", *data & (1 << sl)) == -1) goto err; @@ -762,9 +746,7 @@ print_option(FILE *fp, const char *prefix, const struct dhcp_opt *opt, } /* Don't print NULL or 0 flags */ if (opt->bitflags[l] != '\0' && - opt->bitflags[l] != '0' && - *data & (1 << sl)) - { + opt->bitflags[l] != '0' && *data & (1 << sl)) { if (fputc(opt->bitflags[l], fp) == EOF) goto err; } @@ -818,18 +800,20 @@ print_option(FILE *fp, const char *prefix, const struct dhcp_opt *opt, data += sizeof(addr.s_addr); } else if (opt->type & OT_ADDRIPV6) { uint8_t databuf[sizeof(struct in6_addr)] = { 0 }; - size_t datalen = e - data >= 16 ? 16 : (size_t)(e - data); + size_t datalen = e - data >= 16 ? 16 : + (size_t)(e - data); char buf[INET6_ADDRSTRLEN]; /* avoid inet_ntop going beyond our option space by * copying out into a temporary buffer. */ memcpy(databuf, data, datalen); - if (inet_ntop(AF_INET6, databuf, buf, sizeof(buf)) == NULL) + if (inet_ntop(AF_INET6, databuf, buf, sizeof(buf)) == + NULL) goto err; if (fprintf(fp, "%s", buf) == -1) goto err; if (data[0] == 0xfe && (data[1] & 0xc0) == 0x80) { - if (fprintf(fp,"%%%s", ifname) == -1) + if (fprintf(fp, "%%%s", ifname) == -1) goto err; } data += 16; @@ -871,22 +855,19 @@ dhcp_set_leasefile(char *leasefile, size_t len, int family, if (ifp->wireless) { ssid[0] = '-'; - print_string(ssid + 1, sizeof(ssid) - 1, - OT_ESCFILE, + print_string(ssid + 1, sizeof(ssid) - 1, OT_ESCFILE, (const uint8_t *)ifp->ssid, ifp->ssid_len); } else ssid[0] = '\0'; return snprintf(leasefile, len, - family == AF_INET ? LEASEFILE : LEASEFILE6, - ifp->name, ssid); + family == AF_INET ? LEASEFILE : LEASEFILE6, ifp->name, ssid); } void dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, const char *ifname, struct dhcp_opt *opt, - const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, - size_t *, unsigned int *, size_t *, - const uint8_t *, size_t, struct dhcp_opt **), + const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, size_t *, unsigned int *, + size_t *, const uint8_t *, size_t, struct dhcp_opt **), const uint8_t *od, size_t ol) { size_t i, eos, eol; @@ -908,8 +889,8 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, /* Create a new prefix based on the option */ if (opt->type & OT_INDEX) { - if (asprintf(&pfx, "%s_%s%d", - prefix, opt->var, ++opt->index) == -1) + if (asprintf(&pfx, "%s_%s%d", prefix, opt->var, ++opt->index) == + -1) pfx = NULL; } else { if (asprintf(&pfx, "%s_%s", prefix, opt->var) == -1) @@ -926,9 +907,8 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, eo = dhcp_optlen(eopt, ol); if (eo == -1) { logerrx("%s: %s %d.%d/%zu: " - "malformed embedded option", - ifname, __func__, opt->option, - eopt->option, i); + "malformed embedded option", + ifname, __func__, opt->option, eopt->option, i); goto out; } if (eo == 0) { @@ -939,9 +919,9 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, * option which is optional. */ if (ol != 0 || !(eopt->type & OT_OPTIONAL)) logerrx("%s: %s %d.%d/%zu: " - "missing embedded option", - ifname, __func__, opt->option, - eopt->option, i); + "missing embedded option", + ifname, __func__, opt->option, eopt->option, + i); goto out; } /* Use the option prefix if the embedded option @@ -950,9 +930,8 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, if (!(eopt->type & OT_RESERVED)) { ov = strcmp(opt->var, eopt->var); if (print_option(fp, pfx, eopt, ov, od, (size_t)eo, - ifname) == -1) - logerr("%s: %s %d.%d/%zu", - ifname, __func__, + ifname) == -1) + logerr("%s: %s %d.%d/%zu", ifname, __func__, opt->option, eopt->option, i); } od += (size_t)eo; @@ -964,10 +943,8 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, /* Zero any option indexes * We assume that referenced encapsulated options are NEVER * recursive as the index order could break. */ - for (i = 0, eopt = opt->encopts; - i < opt->encopts_len; - i++, eopt++) - { + for (i = 0, eopt = opt->encopts; i < opt->encopts_len; + i++, eopt++) { eoc = opt->option; if (eopt->type & OT_OPTION) { dgetopt(ctx, NULL, &eoc, NULL, NULL, 0, &oopt); @@ -977,10 +954,8 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, } while ((eod = dgetopt(ctx, &eos, &eoc, &eol, od, ol, &oopt))) { - for (i = 0, eopt = opt->encopts; - i < opt->encopts_len; - i++, eopt++) - { + for (i = 0, eopt = opt->encopts; i < opt->encopts_len; + i++, eopt++) { if (eopt->option != eoc) continue; if (eopt->type & OT_OPTION) { @@ -989,7 +964,7 @@ dhcp_envoption(struct dhcpcd_ctx *ctx, FILE *fp, const char *prefix, continue; } dhcp_envoption(ctx, fp, pfx, ifname, - eopt->type & OT_OPTION ? oopt:eopt, + eopt->type & OT_OPTION ? oopt : eopt, dgetopt, eod, eol); } od += eos + eol; @@ -1017,7 +992,6 @@ dhcp_zero_index(struct dhcp_opt *opt) ssize_t dhcp_readfile(struct dhcpcd_ctx *ctx, const char *file, void *data, size_t len) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) @@ -1033,7 +1007,6 @@ ssize_t dhcp_writefile(struct dhcpcd_ctx *ctx, const char *file, mode_t mode, const void *data, size_t len) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) @@ -1048,7 +1021,6 @@ dhcp_writefile(struct dhcpcd_ctx *ctx, const char *file, mode_t mode, int dhcp_filemtime(struct dhcpcd_ctx *ctx, const char *file, time_t *time) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) @@ -1063,7 +1035,6 @@ dhcp_filemtime(struct dhcpcd_ctx *ctx, const char *file, time_t *time) int dhcp_unlink(struct dhcpcd_ctx *ctx, const char *file) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP && !(ctx->options & DHCPCD_PRIVSEPROOT)) diff --git a/src/dhcp-common.h b/src/dhcp-common.h index ecee7674..b706b174 100644 --- a/src/dhcp-common.h +++ b/src/dhcp-common.h @@ -27,14 +27,13 @@ */ #ifndef DHCPCOMMON_H -#define DHCPCOMMON_H +#define DHCPCOMMON_H -#include #include -#include - +#include #include /* after normal includes for sunos */ +#include #include "common.h" #include "dhcpcd.h" @@ -42,49 +41,49 @@ /* Support very old arpa/nameser.h as found in OpenBSD */ #ifndef NS_MAXDNAME #define NS_MAXCDNAME MAXCDNAME -#define NS_MAXDNAME MAXDNAME -#define NS_MAXLABEL MAXLABEL +#define NS_MAXDNAME MAXDNAME +#define NS_MAXLABEL MAXLABEL #endif -#define OT_REQUEST (1 << 0) -#define OT_UINT8 (1 << 1) -#define OT_INT8 (1 << 2) -#define OT_UINT16 (1 << 3) -#define OT_INT16 (1 << 4) -#define OT_UINT32 (1 << 5) -#define OT_INT32 (1 << 6) -#define OT_ADDRIPV4 (1 << 7) -#define OT_STRING (1 << 8) -#define OT_ARRAY (1 << 9) -#define OT_RFC3361 (1 << 10) -#define OT_RFC1035 (1 << 11) -#define OT_RFC3442 (1 << 12) -#define OT_OPTIONAL (1 << 13) -#define OT_ADDRIPV6 (1 << 14) -#define OT_BINHEX (1 << 15) -#define OT_FLAG (1 << 16) -#define OT_NOREQ (1 << 17) -#define OT_EMBED (1 << 18) -#define OT_ENCAP (1 << 19) -#define OT_INDEX (1 << 20) -#define OT_OPTION (1 << 21) -#define OT_DOMAIN (1 << 22) -#define OT_ASCII (1 << 23) -#define OT_RAW (1 << 24) -#define OT_ESCSTRING (1 << 25) -#define OT_ESCFILE (1 << 26) -#define OT_BITFLAG (1 << 27) -#define OT_RESERVED (1 << 28) -#define OT_URI (1 << 29) -#define OT_TRUNCATED (1 << 30) - -#define DHC_REQ(r, n, o) \ +#define OT_REQUEST (1 << 0) +#define OT_UINT8 (1 << 1) +#define OT_INT8 (1 << 2) +#define OT_UINT16 (1 << 3) +#define OT_INT16 (1 << 4) +#define OT_UINT32 (1 << 5) +#define OT_INT32 (1 << 6) +#define OT_ADDRIPV4 (1 << 7) +#define OT_STRING (1 << 8) +#define OT_ARRAY (1 << 9) +#define OT_RFC3361 (1 << 10) +#define OT_RFC1035 (1 << 11) +#define OT_RFC3442 (1 << 12) +#define OT_OPTIONAL (1 << 13) +#define OT_ADDRIPV6 (1 << 14) +#define OT_BINHEX (1 << 15) +#define OT_FLAG (1 << 16) +#define OT_NOREQ (1 << 17) +#define OT_EMBED (1 << 18) +#define OT_ENCAP (1 << 19) +#define OT_INDEX (1 << 20) +#define OT_OPTION (1 << 21) +#define OT_DOMAIN (1 << 22) +#define OT_ASCII (1 << 23) +#define OT_RAW (1 << 24) +#define OT_ESCSTRING (1 << 25) +#define OT_ESCFILE (1 << 26) +#define OT_BITFLAG (1 << 27) +#define OT_RESERVED (1 << 28) +#define OT_URI (1 << 29) +#define OT_TRUNCATED (1 << 30) + +#define DHC_REQ(r, n, o) \ (has_option_mask((r), (o)) && !has_option_mask((n), (o))) -#define DHC_REQOPT(o, r, n) \ - (!((o)->type & OT_NOREQ) && \ - ((o)->type & OT_REQUEST || has_option_mask((r), (o)->option)) && \ - !has_option_mask((n), (o)->option)) +#define DHC_REQOPT(o, r, n) \ + (!((o)->type & OT_NOREQ) && \ + ((o)->type & OT_REQUEST || has_option_mask((r), (o)->option)) && \ + !has_option_mask((n), (o)->option)) struct dhcp_opt { uint32_t option; /* Also used for IANA Enterpise Number */ @@ -111,32 +110,30 @@ struct dhcp_opt *vivso_find(uint32_t, const void *); ssize_t dhcp_vendor(char *, size_t); void dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols); -#define add_option_mask(var, val) \ +#define add_option_mask(var, val) \ ((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] | 1 << ((val) & 7))) -#define del_option_mask(var, val) \ +#define del_option_mask(var, val) \ ((var)[(val) >> 3] = (uint8_t)((var)[(val) >> 3] & ~(1 << ((val) & 7)))) -#define has_option_mask(var, val) \ +#define has_option_mask(var, val) \ ((var)[(val) >> 3] & (uint8_t)(1 << ((val) & 7))) -int make_option_mask(const struct dhcp_opt *, size_t, - const struct dhcp_opt *, size_t, - uint8_t *, const char *, int); +int make_option_mask(const struct dhcp_opt *, size_t, const struct dhcp_opt *, + size_t, uint8_t *, const char *, int); size_t encode_rfc1035(const char *src, uint8_t *dst); ssize_t decode_rfc1035(char *, size_t, const uint8_t *, size_t); ssize_t print_string(char *, size_t, int, const uint8_t *, size_t); int dhcp_set_leasefile(char *, size_t, int, const struct interface *); -void dhcp_envoption(struct dhcpcd_ctx *, - FILE *, const char *, const char *, struct dhcp_opt *, - const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, - size_t *, unsigned int *, size_t *, - const uint8_t *, size_t, struct dhcp_opt **), +void dhcp_envoption(struct dhcpcd_ctx *, FILE *, const char *, const char *, + struct dhcp_opt *, + const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, size_t *, unsigned int *, + size_t *, const uint8_t *, size_t, struct dhcp_opt **), const uint8_t *od, size_t ol); void dhcp_zero_index(struct dhcp_opt *); ssize_t dhcp_readfile(struct dhcpcd_ctx *, const char *, void *, size_t); -ssize_t dhcp_writefile(struct dhcpcd_ctx *, const char *, mode_t, - const void *, size_t); +ssize_t dhcp_writefile(struct dhcpcd_ctx *, const char *, mode_t, const void *, + size_t); int dhcp_filemtime(struct dhcpcd_ctx *, const char *, time_t *); int dhcp_unlink(struct dhcpcd_ctx *, const char *); size_t dhcp_read_hwaddr_aton(struct dhcpcd_ctx *, uint8_t **, const char *); diff --git a/src/dhcp.c b/src/dhcp.c index 4c1b35ea..9d68baa9 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -29,19 +29,20 @@ #include #include -#include #include #include -#include -#include #include +#include #include +#include + +#include #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */ #include #undef __FAVOR_BSD #ifdef AF_LINK -# include +#include #endif #include @@ -55,17 +56,17 @@ #include #include #include -#include #include +#include -#define ELOOP_QUEUE ELOOP_DHCP -#include "config.h" +#define ELOOP_QUEUE ELOOP_DHCP #include "arp.h" #include "bpf.h" #include "common.h" +#include "config.h" +#include "dhcp-common.h" #include "dhcp.h" #include "dhcpcd.h" -#include "dhcp-common.h" #include "duid.h" #include "eloop.h" #include "if.h" @@ -76,14 +77,14 @@ #include "sa.h" #include "script.h" -#define DAD "Duplicate address detected" -#define DHCP_MIN_LEASE 20 +#define DAD "Duplicate address detected" +#define DHCP_MIN_LEASE 20 -#define IPV4A ADDRIPV4 | ARRAY -#define IPV4R ADDRIPV4 | REQUEST +#define IPV4A ADDRIPV4 | ARRAY +#define IPV4R ADDRIPV4 | REQUEST /* We should define a maximum for the NAK exponential backoff */ -#define NAKOFF_MAX 60 +#define NAKOFF_MAX 60 #ifndef IPDEFTTL #define IPDEFTTL 64 /* RFC1340 */ @@ -95,38 +96,25 @@ #endif /* Assert the correct structure size for on wire */ -__CTASSERT(sizeof(struct ip) == 20); -__CTASSERT(sizeof(struct udphdr) == 8); -__CTASSERT(sizeof(struct bootp) == 300); -#define IP_UDP_SIZE (sizeof(struct ip) + sizeof(struct udphdr)) -#define BOOTP_MIN_MTU (IP_UDP_SIZE + sizeof(struct bootp)) +__CTASSERT(sizeof(struct ip) == 20); +__CTASSERT(sizeof(struct udphdr) == 8); +__CTASSERT(sizeof(struct bootp) == 300); +#define IP_UDP_SIZE (sizeof(struct ip) + sizeof(struct udphdr)) +#define BOOTP_MIN_MTU (IP_UDP_SIZE + sizeof(struct bootp)) struct dhcp_op { uint8_t value; const char *name; }; -static const struct dhcp_op dhcp_ops[] = { - { DHCP_DISCOVER, "DISCOVER" }, - { DHCP_OFFER, "OFFER" }, - { DHCP_REQUEST, "REQUEST" }, - { DHCP_DECLINE, "DECLINE" }, - { DHCP_ACK, "ACK" }, - { DHCP_NAK, "NAK" }, - { DHCP_RELEASE, "RELEASE" }, - { DHCP_INFORM, "INFORM" }, - { DHCP_FORCERENEW, "FORCERENEW"}, - { 0, NULL } -}; +static const struct dhcp_op dhcp_ops[] = { { DHCP_DISCOVER, "DISCOVER" }, + { DHCP_OFFER, "OFFER" }, { DHCP_REQUEST, "REQUEST" }, + { DHCP_DECLINE, "DECLINE" }, { DHCP_ACK, "ACK" }, { DHCP_NAK, "NAK" }, + { DHCP_RELEASE, "RELEASE" }, { DHCP_INFORM, "INFORM" }, + { DHCP_FORCERENEW, "FORCERENEW" }, { 0, NULL } }; -static const char * const dhcp_params[] = { - "ip_address", - "subnet_cidr", - "network_number", - "filename", - "server_name", - NULL -}; +static const char *const dhcp_params[] = { "ip_address", "subnet_cidr", + "network_number", "filename", "server_name", NULL }; static int dhcp_openbpf(struct interface *); static void dhcp_start1(void *); @@ -139,10 +127,10 @@ static void dhcp_handleifudp(void *, unsigned short); static int dhcp_initstate(struct interface *); void -dhcp_printoptions(const struct dhcpcd_ctx *ctx, - const struct dhcp_opt *opts, size_t opts_len) +dhcp_printoptions(const struct dhcpcd_ctx *ctx, const struct dhcp_opt *opts, + size_t opts_len) { - const char * const *p; + const char *const *p; size_t i, j; const struct dhcp_opt *opt, *opt2; int cols; @@ -166,8 +154,7 @@ dhcp_printoptions(const struct dhcpcd_ctx *ctx, } static const uint8_t * -get_option(struct dhcpcd_ctx *ctx, - const struct bootp *bootp, size_t bootp_len, +get_option(struct dhcpcd_ctx *ctx, const struct bootp *bootp, size_t bootp_len, unsigned int opt, size_t *opt_len) { const uint8_t *p, *e; @@ -245,8 +232,8 @@ get_option(struct dhcpcd_ctx *ctx, uint8_t *nb; if (bp) - pos = (size_t) - (bp - ctx->opt_buffer); + pos = (size_t)(bp - + ctx->opt_buffer); else pos = 0; nb = realloc(ctx->opt_buffer, bl + l); @@ -282,9 +269,8 @@ get_option(struct dhcpcd_ctx *ctx, } static int -get_option_addr(struct dhcpcd_ctx *ctx, - struct in_addr *a, const struct bootp *bootp, size_t bootp_len, - uint8_t option) +get_option_addr(struct dhcpcd_ctx *ctx, struct in_addr *a, + const struct bootp *bootp, size_t bootp_len, uint8_t option) { const uint8_t *p; size_t len; @@ -297,8 +283,8 @@ get_option_addr(struct dhcpcd_ctx *ctx, } static int -get_option_uint32(struct dhcpcd_ctx *ctx, - uint32_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) +get_option_uint32(struct dhcpcd_ctx *ctx, uint32_t *i, + const struct bootp *bootp, size_t bootp_len, uint8_t option) { const uint8_t *p; size_t len; @@ -314,8 +300,8 @@ get_option_uint32(struct dhcpcd_ctx *ctx, } static int -get_option_uint16(struct dhcpcd_ctx *ctx, - uint16_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) +get_option_uint16(struct dhcpcd_ctx *ctx, uint16_t *i, + const struct bootp *bootp, size_t bootp_len, uint8_t option) { const uint8_t *p; size_t len; @@ -331,8 +317,8 @@ get_option_uint16(struct dhcpcd_ctx *ctx, } static int -get_option_uint8(struct dhcpcd_ctx *ctx, - uint8_t *i, const struct bootp *bootp, size_t bootp_len, uint8_t option) +get_option_uint8(struct dhcpcd_ctx *ctx, uint8_t *i, const struct bootp *bootp, + size_t bootp_len, uint8_t option) { const uint8_t *p; size_t len; @@ -397,8 +383,8 @@ print_rfc3442(FILE *fp, const uint8_t *data, size_t data_len) } static int -decode_rfc3442_rt(rb_tree_t *routes, struct interface *ifp, - const uint8_t *data, size_t dl) +decode_rfc3442_rt(rb_tree_t *routes, struct interface *ifp, const uint8_t *data, + size_t dl) { const uint8_t *p = data; const uint8_t *e; @@ -484,10 +470,8 @@ print_rfc3361(FILE *fp, const uint8_t *data, size_t dl) break; } addr.s_addr = INADDR_BROADCAST; - for (; - dl != 0; - data += sizeof(addr.s_addr), dl -= sizeof(addr.s_addr)) - { + for (; dl != 0; + data += sizeof(addr.s_addr), dl -= sizeof(addr.s_addr)) { memcpy(&addr.s_addr, data, sizeof(addr.s_addr)); if (fprintf(fp, "%s", inet_ntoa(addr)) == -1) return -1; @@ -508,8 +492,8 @@ print_rfc3361(FILE *fp, const uint8_t *data, size_t dl) } static char * -get_option_string(struct dhcpcd_ctx *ctx, - const struct bootp *bootp, size_t bootp_len, uint8_t option) +get_option_string(struct dhcpcd_ctx *ctx, const struct bootp *bootp, + size_t bootp_len, uint8_t option) { size_t len; const uint8_t *p; @@ -588,8 +572,7 @@ get_option_routes(rb_tree_t *routes, struct interface *ifp, state = D_CSTATE(ifp); if (!(ifo->options & DHCPCD_CSR_WARNED) && - !(state->added & STATE_FAKE)) - { + !(state->added & STATE_FAKE)) { logdebugx("%s: using %sClassless Static Routes", ifp->name, csr); ifo->options |= DHCPCD_CSR_WARNED; @@ -600,8 +583,8 @@ get_option_routes(rb_tree_t *routes, struct interface *ifp, n = 0; /* OK, get our static routes first. */ if (!has_option_mask(ifo->nomask, DHO_STATICROUTE)) - p = get_option(ifp->ctx, bootp, bootp_len, - DHO_STATICROUTE, &len); + p = get_option(ifp->ctx, bootp, bootp_len, DHO_STATICROUTE, + &len); else p = NULL; /* RFC 2131 Section 5.8 states length MUST be in multiples of 8 */ @@ -622,9 +605,8 @@ get_option_routes(rb_tree_t *routes, struct interface *ifp, /* A on-link host route is normally set by having the * gateway match the destination or assigned address */ if (gateway.s_addr == dest.s_addr || - (gateway.s_addr == bootp->yiaddr || - gateway.s_addr == bootp->ciaddr)) - { + (gateway.s_addr == bootp->yiaddr || + gateway.s_addr == bootp->ciaddr)) { gateway.s_addr = INADDR_ANY; netmask.s_addr = INADDR_BROADCAST; } else @@ -676,8 +658,8 @@ dhcp_get_mtu(const struct interface *ifp) mtu = 0; /* bogus gcc warning */ if ((state = D_CSTATE(ifp)) == NULL || has_option_mask(ifp->options->nomask, DHO_MTU) || - get_option_uint16(ifp->ctx, &mtu, - state->new, state->new_len, DHO_MTU) == -1) + get_option_uint16(ifp->ctx, &mtu, state->new, state->new_len, + DHO_MTU) == -1) return 0; if (mtu < IPV4_MMTU) return IPV4_MMTU; @@ -698,8 +680,7 @@ dhcp_get_routes(rb_tree_t *routes, struct interface *ifp) /* Assumes DHCP options */ static int -dhcp_message_add_addr(struct bootp *bootp, - uint8_t type, struct in_addr addr) +dhcp_message_add_addr(struct bootp *bootp, uint8_t type, struct in_addr addr) { uint8_t *p; size_t len; @@ -781,12 +762,12 @@ rfc3396_write(struct rfc3396_ctx *ctx, void *data, size_t len) static ssize_t rfc3396_write_byte(struct rfc3396_ctx *ctx, uint8_t byte) { - return rfc3396_write(ctx, &byte, sizeof(byte)); } static uint8_t * -rfc3396_zero(struct rfc3396_ctx *ctx) { +rfc3396_zero(struct rfc3396_ctx *ctx) +{ uint8_t *zerop = *ctx->buf, zero = 0; if (rfc3396_write(ctx, &zero, sizeof(zero)) == -1) @@ -822,8 +803,8 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) return -1; } if ((size_t)mtu < BOOTP_MIN_MTU) { - logerr("%s: interface mtu is too small (%d<%zu)", - ifp->name, mtu, BOOTP_MIN_MTU); + logerr("%s: interface mtu is too small (%d<%zu)", ifp->name, + mtu, BOOTP_MIN_MTU); return -1; } @@ -840,10 +821,10 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) if (state->addr != NULL && (type == DHCP_INFORM || type == DHCP_RELEASE || - (type == DHCP_REQUEST && - state->addr->mask.s_addr == lease->mask.s_addr && - (state->new == NULL || IS_DHCP(state->new)) && - !(state->added & (STATE_FAKE | STATE_EXPIRED))))) + (type == DHCP_REQUEST && + state->addr->mask.s_addr == lease->mask.s_addr && + (state->new == NULL || IS_DHCP(state->new)) && + !(state->added & (STATE_FAKE | STATE_EXPIRED))))) bootp->ciaddr = state->addr->addr.s_addr; bootp->op = BOOTREQUEST; @@ -853,10 +834,8 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) memcpy(&bootp->chaddr, &ifp->hwaddr, ifp->hwlen); } - if (ifo->options & DHCPCD_BROADCAST && - bootp->ciaddr == INADDR_ANY && - type != DHCP_DECLINE && - type != DHCP_RELEASE) + if (ifo->options & DHCPCD_BROADCAST && bootp->ciaddr == INADDR_ANY && + type != DHCP_DECLINE && type != DHCP_RELEASE) bootp->flags = htons(BROADCAST_FLAG); if (type != DHCP_DECLINE && type != DHCP_RELEASE) { @@ -877,22 +856,27 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) return sizeof(*bootp); p = bootp->vend; - e = (uint8_t *)bootp + ((size_t)mtu - IP_UDP_SIZE - 1/* DHO_END */); + e = (uint8_t *)bootp + ((size_t)mtu - IP_UDP_SIZE - 1 /* DHO_END */); ul = htonl(MAGIC_COOKIE); memcpy(p, &ul, sizeof(ul)); p += sizeof(ul); -#define AREA_LEFT (size_t)(e - p) -#define AREA_FIT(s) if ((s) > AREA_LEFT) goto toobig -#define AREA_CHECK(s) if ((s) + 2UL > AREA_LEFT) goto toobig -#define PUT_ADDR(o, a) do { \ - AREA_CHECK(4); \ - *p++ = (o); \ - *p++ = 4; \ - memcpy(p, &(a)->s_addr, 4); \ - p += 4; \ -} while (0 /* CONSTCOND */) +#define AREA_LEFT (size_t)(e - p) +#define AREA_FIT(s) \ + if ((s) > AREA_LEFT) \ + goto toobig +#define AREA_CHECK(s) \ + if ((s) + 2UL > AREA_LEFT) \ + goto toobig +#define PUT_ADDR(o, a) \ + do { \ + AREA_CHECK(4); \ + *p++ = (o); \ + *p++ = 4; \ + memcpy(p, &(a)->s_addr, 4); \ + p += 4; \ + } while (0 /* CONSTCOND */) /* * RFC 7844 3.1 says options should be randomised, but if not @@ -913,10 +897,9 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) if (lease->addr.s_addr && lease->cookie == htonl(MAGIC_COOKIE)) { if (type == DHCP_DECLINE || (type == DHCP_REQUEST && - (state->addr == NULL || - state->added & (STATE_FAKE | STATE_EXPIRED) || - lease->addr.s_addr != state->addr->addr.s_addr))) - { + (state->addr == NULL || + state->added & (STATE_FAKE | STATE_EXPIRED) || + lease->addr.s_addr != state->addr->addr.s_addr))) { putip = true; PUT_ADDR(DHO_IPADDRESS, &lease->addr); } @@ -939,8 +922,9 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) } } -#define DHCP_DIR(type) ((type) == DHCP_DISCOVER || (type) == DHCP_INFORM || \ - (type) == DHCP_REQUEST) +#define DHCP_DIR(type) \ + ((type) == DHCP_DISCOVER || (type) == DHCP_INFORM || \ + (type) == DHCP_REQUEST) if (DHCP_DIR(type)) { /* vendor is already encoded correctly, so just add it */ @@ -972,9 +956,7 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) n_params = p; *p++ = 0; for (i = 0, opt = ifp->ctx->dhcp_opts; - i < ifp->ctx->dhcp_opts_len; - i++, opt++) - { + i < ifp->ctx->dhcp_opts_len; i++, opt++) { if (!DHC_REQOPT(opt, ifo->requestmask, ifo->nomask)) continue; if (type == DHCP_INFORM && @@ -985,9 +967,7 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) *p++ = (uint8_t)opt->option; } for (i = 0, opt = ifo->dhcp_override; - i < ifo->dhcp_override_len; - i++, opt++) - { + i < ifo->dhcp_override_len; i++, opt++) { /* Check if added above */ for (lp = n_params + 1; lp < p; lp++) if (*lp == (uint8_t)opt->option) @@ -1006,8 +986,7 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) *n_params = (uint8_t)(p - n_params - 1); if (mtu != -1 && - !(has_option_mask(ifo->nomask, DHO_MAXMESSAGESIZE))) - { + !(has_option_mask(ifo->nomask, DHO_MAXMESSAGESIZE))) { AREA_CHECK(2); *p++ = DHO_MAXMESSAGESIZE; *p++ = 2; @@ -1017,8 +996,7 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) } if (ifo->userclass[0] && - !has_option_mask(ifo->nomask, DHO_USERCLASS)) - { + !has_option_mask(ifo->nomask, DHO_USERCLASS)) { AREA_CHECK(ifo->userclass[0]); *p++ = DHO_USERCLASS; memcpy(p, ifo->userclass, @@ -1036,18 +1014,16 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) if (DHCP_DIR(type) && !has_option_mask(ifo->nomask, DHO_VENDORCLASSID) && - ifo->vendorclassid[0]) - { + ifo->vendorclassid[0]) { AREA_CHECK(ifo->vendorclassid[0]); *p++ = DHO_VENDORCLASSID; - memcpy(p, ifo->vendorclassid, (size_t)ifo->vendorclassid[0]+1); + memcpy(p, ifo->vendorclassid, + (size_t)ifo->vendorclassid[0] + 1); p += ifo->vendorclassid[0] + 1; } - if (type == DHCP_DISCOVER && - !(ifp->ctx->options & DHCPCD_TEST) && - DHC_REQ(ifo->requestmask, ifo->nomask, DHO_RAPIDCOMMIT)) - { + if (type == DHCP_DISCOVER && !(ifp->ctx->options & DHCPCD_TEST) && + DHC_REQ(ifo->requestmask, ifo->nomask, DHO_RAPIDCOMMIT)) { /* RFC 4039 Section 3 */ AREA_CHECK(0); *p++ = DHO_RAPIDCOMMIT; @@ -1104,12 +1080,11 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) } #ifdef AUTH - auth = NULL; /* appease GCC */ + auth = NULL; /* appease GCC */ auth_len = 0; if (ifo->auth.options & DHCPCD_AUTH_SEND) { ssize_t alen = dhcp_auth_encode(ifp->ctx, &ifo->auth, - state->auth.token, - NULL, 0, 4, type, NULL, 0); + state->auth.token, NULL, 0, 4, type, NULL, 0); if (alen != -1 && alen > UINT8_MAX) { errno = ERANGE; alen = -1; @@ -1129,8 +1104,7 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) /* RFC 2563 Auto Configure */ if (type == DHCP_DISCOVER && ifo->options & DHCPCD_IPV4LL && - !(has_option_mask(ifo->nomask, DHO_AUTOCONFIGURE))) - { + !(has_option_mask(ifo->nomask, DHO_AUTOCONFIGURE))) { AREA_CHECK(1); *p++ = DHO_AUTOCONFIGURE; *p++ = 1; @@ -1139,16 +1113,15 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) if (DHCP_DIR(type)) { if (ifo->mudurl[0]) { - AREA_CHECK(ifo->mudurl[0]); - *p++ = DHO_MUDURL; - memcpy(p, ifo->mudurl, (size_t)ifo->mudurl[0] + 1); - p += ifo->mudurl[0] + 1; + AREA_CHECK(ifo->mudurl[0]); + *p++ = DHO_MUDURL; + memcpy(p, ifo->mudurl, (size_t)ifo->mudurl[0] + 1); + p += ifo->mudurl[0] + 1; } #ifndef SMALL if (ifo->vivco_len && - !has_option_mask(ifo->nomask, DHO_VIVCO)) - { + !has_option_mask(ifo->nomask, DHO_VIVCO)) { struct vivco *vivco = ifo->vivco; size_t vlen = ifo->vivco_len; struct rfc3396_ctx rctx = { @@ -1165,18 +1138,16 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) if (lp == NULL) goto toobig; if (rfc3396_write_byte(&rctx, - (uint8_t)vivco->len) == -1) + (uint8_t)vivco->len) == -1) goto toobig; - if (rfc3396_write(&rctx, - vivco->data, vivco->len) == -1) + if (rfc3396_write(&rctx, vivco->data, + vivco->len) == -1) goto toobig; *lp = (uint8_t)(*lp + vivco->len + 1); } } - - if (ifo->vsio_len && - !has_option_mask(ifo->nomask, DHO_VIVSO)) - { + + if (ifo->vsio_len && !has_option_mask(ifo->nomask, DHO_VIVSO)) { struct vsio *vso = ifo->vsio; size_t vlen = ifo->vsio_len; struct vsio_so *so; @@ -1203,13 +1174,13 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) for (; slen > 0; so++, slen--) { if (rfc3396_write_byte(&rctx, - (uint8_t)so->opt) == -1) + (uint8_t)so->opt) == -1) goto toobig; if (rfc3396_write_byte(&rctx, - (uint8_t)so->len) == -1) + (uint8_t)so->len) == -1) goto toobig; - if (rfc3396_write(&rctx, - so->data, so->len) == -1) + if (rfc3396_write(&rctx, so->data, + so->len) == -1) goto toobig; *lp = (uint8_t)(*lp + so->len + 2); } @@ -1219,9 +1190,8 @@ make_message(struct bootp **bootpm, const struct interface *ifp, uint8_t type) #ifdef AUTH if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != - DHCPCD_AUTH_SENDREQUIRE && - !has_option_mask(ifo->nomask, DHO_FORCERENEW_NONCE)) - { + DHCPCD_AUTH_SENDREQUIRE && + !has_option_mask(ifo->nomask, DHO_FORCERENEW_NONCE)) { /* We support HMAC-MD5 */ AREA_CHECK(1); *p++ = DHO_FORCERENEW_NONCE; @@ -1281,10 +1251,9 @@ read_lease(struct interface *ifp, struct bootp **bootp) logdebugx("reading standard input"); sbytes = read(fileno(stdin), buf.buf, sizeof(buf.buf)); } else { - logdebugx("%s: reading lease: %s", - ifp->name, state->leasefile); - sbytes = dhcp_readfile(ifp->ctx, state->leasefile, - buf.buf, sizeof(buf.buf)); + logdebugx("%s: reading lease: %s", ifp->name, state->leasefile); + sbytes = dhcp_readfile(ifp->ctx, state->leasefile, buf.buf, + sizeof(buf.buf)); } if (sbytes == -1) { if (errno != ENOENT) @@ -1308,28 +1277,26 @@ read_lease(struct interface *ifp, struct bootp **bootp) /* We may have found a BOOTP server */ if (get_option_uint8(ifp->ctx, &type, &buf.bootp, bytes, - DHO_MESSAGETYPE) == -1) + DHO_MESSAGETYPE) == -1) type = 0; #ifdef AUTH /* Authenticate the message */ - auth = get_option(ifp->ctx, &buf.bootp, bytes, - DHO_AUTHENTICATION, &auth_len); + auth = get_option(ifp->ctx, &buf.bootp, bytes, DHO_AUTHENTICATION, + &auth_len); if (auth) { if (dhcp_auth_validate(&state->auth, &ifp->options->auth, - &buf.bootp, bytes, 4, type, auth, auth_len) == NULL) - { + &buf.bootp, bytes, 4, type, auth, auth_len) == NULL) { logerr("%s: authentication failed", ifp->name); return 0; } if (state->auth.token) - logdebugx("%s: validated using 0x%08" PRIu32, - ifp->name, state->auth.token->secretid); + logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, + state->auth.token->secretid); else logdebugx("%s: accepted reconfigure key", ifp->name); } else if ((ifp->options->auth.options & DHCPCD_AUTH_SENDREQUIRE) == - DHCPCD_AUTH_SENDREQUIRE) - { + DHCPCD_AUTH_SENDREQUIRE) { logerrx("%s: authentication now required", ifp->name); return 0; } @@ -1351,10 +1318,8 @@ dhcp_getoverride(const struct if_options *ifo, unsigned int o) size_t i; const struct dhcp_opt *opt; - for (i = 0, opt = ifo->dhcp_override; - i < ifo->dhcp_override_len; - i++, opt++) - { + for (i = 0, opt = ifo->dhcp_override; i < ifo->dhcp_override_len; + i++, opt++) { if (opt->option == o) return opt; } @@ -1362,9 +1327,8 @@ dhcp_getoverride(const struct if_options *ifo, unsigned int o) } static const uint8_t * -dhcp_getoption(struct dhcpcd_ctx *ctx, - size_t *os, unsigned int *code, size_t *len, - const uint8_t *od, size_t ol, struct dhcp_opt **oopt) +dhcp_getoption(struct dhcpcd_ctx *ctx, size_t *os, unsigned int *code, + size_t *len, const uint8_t *od, size_t ol, struct dhcp_opt **oopt) { size_t i; struct dhcp_opt *opt; @@ -1411,43 +1375,42 @@ dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp, ifo = ifp->options; if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len, - DHO_OPTSOVERLOADED) == -1) + DHO_OPTSOVERLOADED) == -1) overl = 0; if (bootp->yiaddr || bootp->ciaddr) { /* Set some useful variables that we derive from the DHCP * message but are not necessarily in the options */ addr.s_addr = bootp->yiaddr ? bootp->yiaddr : bootp->ciaddr; - if (efprintf(fenv, "%s_ip_address=%s", - prefix, inet_ntoa(addr)) == -1) + if (efprintf(fenv, "%s_ip_address=%s", prefix, + inet_ntoa(addr)) == -1) return -1; - if (get_option_addr(ifp->ctx, &net, - bootp, bootp_len, DHO_SUBNETMASK) == -1) { + if (get_option_addr(ifp->ctx, &net, bootp, bootp_len, + DHO_SUBNETMASK) == -1) { net.s_addr = ipv4_getnetmask(addr.s_addr); - if (efprintf(fenv, "%s_subnet_mask=%s", - prefix, inet_ntoa(net)) == -1) + if (efprintf(fenv, "%s_subnet_mask=%s", prefix, + inet_ntoa(net)) == -1) return -1; } - if (efprintf(fenv, "%s_subnet_cidr=%d", - prefix, inet_ntocidr(net))== -1) + if (efprintf(fenv, "%s_subnet_cidr=%d", prefix, + inet_ntocidr(net)) == -1) return -1; - if (get_option_addr(ifp->ctx, &brd, - bootp, bootp_len, DHO_BROADCAST) == -1) - { + if (get_option_addr(ifp->ctx, &brd, bootp, bootp_len, + DHO_BROADCAST) == -1) { brd.s_addr = addr.s_addr | ~net.s_addr; - if (efprintf(fenv, "%s_broadcast_address=%s", - prefix, inet_ntoa(brd)) == -1) + if (efprintf(fenv, "%s_broadcast_address=%s", prefix, + inet_ntoa(brd)) == -1) return -1; } addr.s_addr = bootp->yiaddr & net.s_addr; - if (efprintf(fenv, "%s_network_number=%s", - prefix, inet_ntoa(addr)) == -1) + if (efprintf(fenv, "%s_network_number=%s", prefix, + inet_ntoa(addr)) == -1) return -1; } if (*bootp->file && !(overl & 1)) { - print_string(safe, sizeof(safe), OT_STRING, - bootp->file, sizeof(bootp->file)); + print_string(safe, sizeof(safe), OT_STRING, bootp->file, + sizeof(bootp->file)); if (efprintf(fenv, "%s_filename=%s", prefix, safe) == -1) return -1; } @@ -1459,23 +1422,17 @@ dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp, } /* Zero our indexes */ - for (i = 0, opt = ifp->ctx->dhcp_opts; - i < ifp->ctx->dhcp_opts_len; + for (i = 0, opt = ifp->ctx->dhcp_opts; i < ifp->ctx->dhcp_opts_len; i++, opt++) dhcp_zero_index(opt); for (i = 0, opt = ifp->options->dhcp_override; - i < ifp->options->dhcp_override_len; - i++, opt++) + i < ifp->options->dhcp_override_len; i++, opt++) dhcp_zero_index(opt); - for (i = 0, opt = ifp->ctx->vivso; - i < ifp->ctx->vivso_len; - i++, opt++) + for (i = 0, opt = ifp->ctx->vivso; i < ifp->ctx->vivso_len; i++, opt++) dhcp_zero_index(opt); - for (i = 0, opt = ifp->ctx->dhcp_opts; - i < ifp->ctx->dhcp_opts_len; - i++, opt++) - { + for (i = 0, opt = ifp->ctx->dhcp_opts; i < ifp->ctx->dhcp_opts_len; + i++, opt++) { if (has_option_mask(ifo->nomask, opt->option)) continue; if (dhcp_getoverride(ifo, opt->option)) @@ -1483,8 +1440,8 @@ dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp, p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl); if (p == NULL) continue; - dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, - opt, dhcp_getoption, p, pl); + dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, opt, + dhcp_getoption, p, pl); if (opt->option != DHO_VIVSO || pl <= (int)sizeof(uint32_t)) continue; @@ -1496,29 +1453,27 @@ dhcp_env(FILE *fenv, const char *prefix, const struct interface *ifp, /* Skip over en + total size */ p += sizeof(en) + 1; pl -= sizeof(en) + 1; - dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, - vo, dhcp_getoption, p, pl); + dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, vo, + dhcp_getoption, p, pl); } - for (i = 0, opt = ifo->dhcp_override; - i < ifo->dhcp_override_len; - i++, opt++) - { + for (i = 0, opt = ifo->dhcp_override; i < ifo->dhcp_override_len; + i++, opt++) { if (has_option_mask(ifo->nomask, opt->option)) continue; p = get_option(ifp->ctx, bootp, bootp_len, opt->option, &pl); if (p == NULL) continue; - dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, - opt, dhcp_getoption, p, pl); + dhcp_envoption(ifp->ctx, fenv, prefix, ifp->name, opt, + dhcp_getoption, p, pl); } return 1; } static void -get_lease(struct interface *ifp, - struct dhcp_lease *lease, const struct bootp *bootp, size_t len) +get_lease(struct interface *ifp, struct dhcp_lease *lease, + const struct bootp *bootp, size_t len) { struct dhcpcd_ctx *ctx; @@ -1534,17 +1489,17 @@ get_lease(struct interface *ifp, if (ifp->options->req_brd.s_addr != INADDR_ANY) lease->brd = ifp->options->req_brd; else - lease->brd.s_addr = - lease->addr.s_addr | ~lease->mask.s_addr; + lease->brd.s_addr = lease->addr.s_addr | + ~lease->mask.s_addr; } else { const struct ipv4_addr *ia; ia = ipv4_iffindaddr(ifp, &lease->addr, NULL); if (ia == NULL) { - lease->mask.s_addr = - ipv4_getnetmask(lease->addr.s_addr); - lease->brd.s_addr = - lease->addr.s_addr | ~lease->mask.s_addr; + lease->mask.s_addr = ipv4_getnetmask( + lease->addr.s_addr); + lease->brd.s_addr = lease->addr.s_addr | + ~lease->mask.s_addr; } else { lease->mask = ia->mask; lease->brd = ia->brd; @@ -1552,22 +1507,22 @@ get_lease(struct interface *ifp, } } else { if (get_option_addr(ctx, &lease->mask, bootp, len, - DHO_SUBNETMASK) == -1) - lease->mask.s_addr = - ipv4_getnetmask(lease->addr.s_addr); + DHO_SUBNETMASK) == -1) + lease->mask.s_addr = ipv4_getnetmask( + lease->addr.s_addr); if (get_option_addr(ctx, &lease->brd, bootp, len, - DHO_BROADCAST) == -1) - lease->brd.s_addr = - lease->addr.s_addr | ~lease->mask.s_addr; + DHO_BROADCAST) == -1) + lease->brd.s_addr = lease->addr.s_addr | + ~lease->mask.s_addr; } - if (get_option_uint32(ctx, &lease->leasetime, - bootp, len, DHO_LEASETIME) != 0) + if (get_option_uint32(ctx, &lease->leasetime, bootp, len, + DHO_LEASETIME) != 0) lease->leasetime = DHCP_INFINITE_LIFETIME; - if (get_option_uint32(ctx, &lease->renewaltime, - bootp, len, DHO_RENEWALTIME) != 0) + if (get_option_uint32(ctx, &lease->renewaltime, bootp, len, + DHO_RENEWALTIME) != 0) lease->renewaltime = 0; - if (get_option_uint32(ctx, &lease->rebindtime, - bootp, len, DHO_REBINDTIME) != 0) + if (get_option_uint32(ctx, &lease->rebindtime, bootp, len, + DHO_REBINDTIME) != 0) lease->rebindtime = 0; if (get_option_addr(ctx, &lease->server, bootp, len, DHO_SERVERID) != 0) lease->server.s_addr = INADDR_ANY; @@ -1609,7 +1564,7 @@ dhcp_new_xid(struct interface *ifp) (ifp->hwaddr + ifp->hwlen) - sizeof(state->xid), sizeof(state->xid)); else { -again: + again: state->xid = arc4random(); } @@ -1624,11 +1579,10 @@ dhcp_new_xid(struct interface *ifp) } if (ifp1 != NULL) { if (ifp->options->options & DHCPCD_XID_HWADDR && - ifp->hwlen >= sizeof(state->xid)) - { - logerrx("%s: duplicate xid on %s", - ifp->name, ifp1->name); - return; + ifp->hwlen >= sizeof(state->xid)) { + logerrx("%s: duplicate xid on %s", ifp->name, + ifp1->name); + return; } goto again; } @@ -1758,7 +1712,7 @@ in_cksum(const void *data, size_t len, uint32_t *isum) static struct bootp_pkt * dhcp_makeudppacket(size_t *sz, const uint8_t *data, size_t length, - struct in_addr source, struct in_addr dest) + struct in_addr source, struct in_addr dest) { struct bootp_pkt *udpp; struct ip *ip; @@ -1818,13 +1772,19 @@ dhcp_sendudp(struct interface *ifp, struct in_addr *to, void *data, size_t len) #endif }; struct udphdr udp = { - .uh_sport = htons(BOOTPC), - .uh_dport = htons(BOOTPS), - .uh_ulen = htons((uint16_t)(sizeof(udp) + len)), + .uh_sport = htons(BOOTPC), + .uh_dport = htons(BOOTPS), + .uh_ulen = htons((uint16_t)(sizeof(udp) + len)), }; struct iovec iov[] = { - { .iov_base = &udp, .iov_len = sizeof(udp), }, - { .iov_base = data, .iov_len = len, }, + { + .iov_base = &udp, + .iov_len = sizeof(udp), + }, + { + .iov_base = data, + .iov_len = len, + }, }; struct msghdr msg = { .msg_name = (void *)&sin, @@ -1842,8 +1802,7 @@ dhcp_sendudp(struct interface *ifp, struct in_addr *to, void *data, size_t len) } static void -send_message(struct interface *ifp, uint8_t type, - void (*callback)(void *)) +send_message(struct interface *ifp, uint8_t type, void (*callback)(void *)) { struct dhcp_state *state = D_STATE(ifp); struct if_options *ifo = ifp->options; @@ -1858,8 +1817,7 @@ send_message(struct interface *ifp, uint8_t type, /* No carrier? Don't bother sending the packet. */ if (!if_is_link_up(ifp)) return; - logdebugx("%s: sending %s with xid 0x%x", - ifp->name, + logdebugx("%s: sending %s with xid 0x%x", ifp->name, ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type), state->xid); RT = 0; /* bogus gcc warning */ @@ -1880,8 +1838,7 @@ send_message(struct interface *ifp, uint8_t type, logdebugx("%s: sending %s (xid 0x%x), next in %0.1f seconds", ifp->name, ifo->options & DHCPCD_BOOTP ? "BOOTP" : get_dhcp_op(type), - state->xid, - (float)RT / MSEC_PER_SEC); + state->xid, (float)RT / MSEC_PER_SEC); } r = make_message(&bootp, ifp, type); @@ -1943,7 +1900,7 @@ send_message(struct interface *ifp, uint8_t type, * stopping the interface. */ if (r == -1) { logerr("%s: bpf_send", ifp->name); - switch(errno) { + switch (errno) { case ENETDOWN: case ENETRESET: case ENETUNREACH: @@ -1952,8 +1909,7 @@ send_message(struct interface *ifp, uint8_t type, default: if (!(ifp->ctx->options & DHCPCD_TEST)) dhcp_drop(ifp, "FAIL"); - eloop_timeout_delete(ifp->ctx->eloop, - NULL, ifp); + eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); callback = NULL; } } @@ -1971,35 +1927,30 @@ send_message(struct interface *ifp, uint8_t type, static void send_inform(void *arg) { - send_message((struct interface *)arg, DHCP_INFORM, send_inform); } static void send_discover(void *arg) { - send_message((struct interface *)arg, DHCP_DISCOVER, send_discover); } static void send_request(void *arg) { - send_message((struct interface *)arg, DHCP_REQUEST, send_request); } static void send_renew(void *arg) { - send_message((struct interface *)arg, DHCP_REQUEST, send_renew); } static void send_rebind(void *arg) { - send_message((struct interface *)arg, DHCP_REQUEST, send_rebind); } @@ -2019,16 +1970,16 @@ dhcp_discover(void *arg) ifo->fallback_time, dhcp_fallback, ifp); #ifdef IPV4LL else if (ifo->options & DHCPCD_IPV4LL) - eloop_timeout_add_sec(ifp->ctx->eloop, - ifo->ipv4ll_time, ipv4ll_start, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, ifo->ipv4ll_time, + ipv4ll_start, ifp); #endif } if (ifo->options & DHCPCD_REQUEST) loginfox("%s: soliciting a DHCP lease (requesting %s)", ifp->name, inet_ntoa(ifo->req_addr)); else - loginfox("%s: soliciting a %s lease", - ifp->name, ifo->options & DHCPCD_BOOTP ? "BOOTP" : "DHCP"); + loginfox("%s: soliciting a %s lease", ifp->name, + ifo->options & DHCPCD_BOOTP ? "BOOTP" : "DHCP"); send_discover(ifp); } @@ -2103,8 +2054,7 @@ dhcp_startrenew(void *arg) return; /* Only renew in the bound or renew states */ - if (state->state != DHS_BOUND && - state->state != DHS_RENEW) + if (state->state != DHS_BOUND && state->state != DHS_RENEW) return; /* Remove the timeout as the renew may have been forced. */ @@ -2122,7 +2072,6 @@ dhcp_startrenew(void *arg) void dhcp_renew(struct interface *ifp) { - dhcp_startrenew(ifp); } @@ -2134,14 +2083,14 @@ dhcp_rebind(void *arg) struct dhcp_lease *lease = &state->lease; logwarnx("%s: failed to renew DHCP, rebinding", ifp->name); - logdebugx("%s: expire in %"PRIu32" seconds", - ifp->name, lease->leasetime - lease->rebindtime); + logdebugx("%s: expire in %" PRIu32 " seconds", ifp->name, + lease->leasetime - lease->rebindtime); state->state = DHS_REBIND; eloop_timeout_delete(ifp->ctx->eloop, send_renew, ifp); state->lease.server.s_addr = INADDR_ANY; state->interval = 0; - ifp->options->options &= ~(DHCPCD_CSR_WARNED | - DHCPCD_ROUTER_HOST_ROUTE_WARNED); + ifp->options->options &= ~( + DHCPCD_CSR_WARNED | DHCPCD_ROUTER_HOST_ROUTE_WARNED); send_rebind(ifp); } @@ -2177,8 +2126,8 @@ dhcp_finish_dad(struct interface *ifp, struct in_addr *ia) #ifdef IPV4LL /* Stop IPv4LL now we have a working DHCP address */ - if ((!IN_LINKLOCAL(ntohl(ia->s_addr))) - && (ifp->options->options & DHCPCD_IPV4LL)) + if ((!IN_LINKLOCAL(ntohl(ia->s_addr))) && + (ifp->options->options & DHCPCD_IPV4LL)) ipv4ll_drop(ifp); #endif @@ -2221,8 +2170,8 @@ dhcp_addr_duplicated(struct interface *ifp, struct in_addr *ia) eloop_exit(ifp->ctx->eloop, EXIT_FAILURE); return deleted; } - eloop_timeout_add_sec(ifp->ctx->eloop, - DHCP_RAND_MAX, dhcp_discover, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, DHCP_RAND_MAX, dhcp_discover, + ifp); return deleted; } #endif @@ -2233,7 +2182,6 @@ dhcp_addr_duplicated(struct interface *ifp, struct in_addr *ia) static void dhcp_arp_announced(struct arp_state *state) { - arp_free(state); } #endif @@ -2253,7 +2201,7 @@ dhcp_arp_defend_failed(struct arp_state *astate) // Delay restarting to give time for the BPF ARP process to exit // as we may spawn a new one with a different filter fairly quickly delay = MSEC_PER_SEC + - (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC); + (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC); eloop_timeout_add_msec(ifp->ctx->eloop, delay, dhcp_start1, ifp); } #endif @@ -2344,16 +2292,13 @@ dhcp_arp_found(struct arp_state *astate, const struct arp_msg *amsg) ifo = ifp->options; if (state->arping_index != -1 && - state->arping_index < ifo->arping_len && - amsg && - amsg->sip.s_addr == ifo->arping[state->arping_index]) - { + state->arping_index < ifo->arping_len && amsg && + amsg->sip.s_addr == ifo->arping[state->arping_index]) { char buf[HWADDR_LEN * 3]; hwaddr_ntoa(amsg->sha, ifp->hwlen, buf, sizeof(buf)); if (dhcpcd_selectprofile(ifp, buf) == -1 && - dhcpcd_selectprofile(ifp, inet_ntoa(amsg->sip)) == -1) - { + dhcpcd_selectprofile(ifp, inet_ntoa(amsg->sip)) == -1) { /* We didn't find a profile for this * address or hwaddr, so move to the next * arping profile */ @@ -2391,15 +2336,16 @@ dhcp_bound(struct interface *ifp, uint8_t old_state) if (ctx->options & DHCPCD_MANAGER || ifp->options->options & DHCPCD_STATIC || (state->old != NULL && - state->old->yiaddr == state->new->yiaddr && - old_state & STATE_ADDED && !(old_state & STATE_FAKE))) + state->old->yiaddr == + state->new->yiaddr &&old_state &STATE_ADDED && + !(old_state & STATE_FAKE))) return; dhcp_closeinet(ifp); #ifdef PRIVSEP if (IN_PRIVSEP_SE(ctx)) { if (ps_inet_openbootp(state->addr) == -1) - logerr(__func__); + logerr(__func__); return; } #endif @@ -2413,7 +2359,7 @@ dhcp_bound(struct interface *ifp, uint8_t old_state) } if (eloop_event_add(ctx->eloop, state->udp_rfd, ELE_READ, - dhcp_handleifudp, ifp) == -1) + dhcp_handleifudp, ifp) == -1) logerr("%s: eloop_event_add", __func__); } @@ -2440,25 +2386,23 @@ dhcp_bind(struct interface *ifp) } get_lease(ifp, lease, state->new, state->new_len); if (ifo->options & DHCPCD_STATIC) { - loginfox("%s: using static address %s/%d", - ifp->name, inet_ntoa(lease->addr), - inet_ntocidr(lease->mask)); + loginfox("%s: using static address %s/%d", ifp->name, + inet_ntoa(lease->addr), inet_ntocidr(lease->mask)); lease->leasetime = DHCP_INFINITE_LIFETIME; state->reason = "STATIC"; } else if (ifo->options & DHCPCD_INFORM) { - loginfox("%s: received approval for %s", - ifp->name, inet_ntoa(lease->addr)); + loginfox("%s: received approval for %s", ifp->name, + inet_ntoa(lease->addr)); lease->leasetime = DHCP_INFINITE_LIFETIME; state->reason = "INFORM"; } else { if (lease->frominfo) state->reason = "TIMEOUT"; if (lease->leasetime == DHCP_INFINITE_LIFETIME) { - lease->renewaltime = - lease->rebindtime = + lease->renewaltime = lease->rebindtime = lease->leasetime; - loginfox("%s: leased %s for infinity", - ifp->name, inet_ntoa(lease->addr)); + loginfox("%s: leased %s for infinity", ifp->name, + inet_ntoa(lease->addr)); } else { if (lease->leasetime < DHCP_MIN_LEASE) { logwarnx("%s: minimum lease is %d seconds", @@ -2472,7 +2416,7 @@ dhcp_bind(struct interface *ifp) lease->rebindtime = (uint32_t)(lease->leasetime * T2); logwarnx("%s: rebind time greater than lease " - "time, forcing to %"PRIu32" seconds", + "time, forcing to %" PRIu32 " seconds", ifp->name, lease->rebindtime); } if (lease->renewaltime == 0) @@ -2482,17 +2426,20 @@ dhcp_bind(struct interface *ifp) lease->renewaltime = (uint32_t)(lease->leasetime * T1); logwarnx("%s: renewal time greater than " - "rebind time, forcing to %"PRIu32" seconds", + "rebind time, forcing to %" PRIu32 + " seconds", ifp->name, lease->renewaltime); } if (state->state == DHS_RENEW && state->addr && lease->addr.s_addr == state->addr->addr.s_addr && !(state->added & STATE_FAKE)) - logdebugx("%s: leased %s for %"PRIu32" seconds", + logdebugx("%s: leased %s for %" PRIu32 + " seconds", ifp->name, inet_ntoa(lease->addr), lease->leasetime); else - loginfox("%s: leased %s for %"PRIu32" seconds", + loginfox("%s: leased %s for %" PRIu32 + " seconds", ifp->name, inet_ntoa(lease->addr), lease->leasetime); } @@ -2505,11 +2452,9 @@ dhcp_bind(struct interface *ifp) } if (state->reason == NULL) { if (state->old && - !(state->added & (STATE_FAKE | STATE_EXPIRED))) - { + !(state->added & (STATE_FAKE | STATE_EXPIRED))) { if (state->old->yiaddr == state->new->yiaddr && - lease->server.s_addr && - state->state != DHS_REBIND) + lease->server.s_addr && state->state != DHS_REBIND) state->reason = "RENEW"; else state->reason = "REBIND"; @@ -2521,23 +2466,22 @@ dhcp_bind(struct interface *ifp) if (lease->leasetime == DHCP_INFINITE_LIFETIME) lease->renewaltime = lease->rebindtime = lease->leasetime; else { - eloop_timeout_add_sec(ctx->eloop, - lease->renewaltime, dhcp_startrenew, ifp); - eloop_timeout_add_sec(ctx->eloop, - lease->rebindtime, dhcp_rebind, ifp); - eloop_timeout_add_sec(ctx->eloop, - lease->leasetime, dhcp_expire, ifp); - logdebugx("%s: renew in %"PRIu32" seconds, rebind in %"PRIu32 - " seconds", + eloop_timeout_add_sec(ctx->eloop, lease->renewaltime, + dhcp_startrenew, ifp); + eloop_timeout_add_sec(ctx->eloop, lease->rebindtime, + dhcp_rebind, ifp); + eloop_timeout_add_sec(ctx->eloop, lease->leasetime, dhcp_expire, + ifp); + logdebugx("%s: renew in %" PRIu32 " seconds, rebind in %" PRIu32 + " seconds", ifp->name, lease->renewaltime, lease->rebindtime); } state->state = DHS_BOUND; if (!state->lease.frominfo && !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC))) { - logdebugx("%s: writing lease: %s", - ifp->name, state->leasefile); - if (dhcp_writefile(ifp->ctx, state->leasefile, 0640, - state->new, state->new_len) == -1) + logdebugx("%s: writing lease: %s", ifp->name, state->leasefile); + if (dhcp_writefile(ifp->ctx, state->leasefile, 0640, state->new, + state->new_len) == -1) logerr("dhcp_writefile: %s", state->leasefile); } @@ -2573,8 +2517,8 @@ dhcp_bind(struct interface *ifp) } static size_t -dhcp_message_new(struct bootp **bootp, - const struct in_addr *addr, const struct in_addr *mask) +dhcp_message_new(struct bootp **bootp, const struct in_addr *addr, + const struct in_addr *mask) { uint8_t *p; uint32_t cookie; @@ -2593,7 +2537,7 @@ dhcp_message_new(struct bootp **bootp, *p++ = DHO_SUBNETMASK; *p++ = sizeof(mask->s_addr); memcpy(p, &mask->s_addr, sizeof(mask->s_addr)); - p+= sizeof(mask->s_addr); + p += sizeof(mask->s_addr); } *p = DHO_END; @@ -2612,7 +2556,8 @@ dhcp_arp_address(struct interface *ifp) state = D_STATE(ifp); addr.s_addr = state->offer->yiaddr == INADDR_ANY ? - state->offer->ciaddr : state->offer->yiaddr; + state->offer->ciaddr : + state->offer->yiaddr; /* If the interface already has the address configured * then we can't ARP for duplicate detection. */ ia = ipv4_iffindaddr(ifp, &addr, NULL); @@ -2624,19 +2569,17 @@ dhcp_arp_address(struct interface *ifp) get_lease(ifp, &l, state->offer, state->offer_len); /* Add the address now, let the kernel handle DAD. */ - ipv4_addaddr(ifp, &l.addr, &l.mask, &l.brd, - l.leasetime, l.rebindtime); + ipv4_addaddr(ifp, &l.addr, &l.mask, &l.brd, l.leasetime, + l.rebindtime); } else if (ia->addr_flags & IN_IFF_DUPLICATED) dhcp_addr_duplicated(ifp, &ia->addr); else - loginfox("%s: waiting for DAD on %s", - ifp->name, inet_ntoa(addr)); + loginfox("%s: waiting for DAD on %s", ifp->name, + inet_ntoa(addr)); return 0; } #else - if (!(ifp->flags & IFF_NOARP) && - ifp->options->options & DHCPCD_ARP) - { + if (!(ifp->flags & IFF_NOARP) && ifp->options->options & DHCPCD_ARP) { struct arp_state *astate; struct dhcp_lease l; @@ -2648,8 +2591,8 @@ dhcp_arp_address(struct interface *ifp) if (ia == NULL) { state->state = DHS_PROBE; get_lease(ifp, &l, state->offer, state->offer_len); - loginfox("%s: probing address %s/%d", - ifp->name, inet_ntoa(l.addr), inet_ntocidr(l.mask)); + loginfox("%s: probing address %s/%d", ifp->name, + inet_ntoa(l.addr), inet_ntocidr(l.mask)); /* We need to handle DAD. */ arp_probe(astate); return 0; @@ -2663,9 +2606,7 @@ dhcp_arp_address(struct interface *ifp) static void dhcp_arp_bind(struct interface *ifp) { - - if (ifp->ctx->options & DHCPCD_TEST || - dhcp_arp_address(ifp) == 1) + if (ifp->ctx->options & DHCPCD_TEST || dhcp_arp_address(ifp) == 1) dhcp_bind(ifp); } #endif @@ -2702,18 +2643,17 @@ dhcp_static(struct interface *ifp) ia = NULL; if (ifo->req_addr.s_addr == INADDR_ANY && - (ia = ipv4_iffindaddr(ifp, NULL, NULL)) == NULL) - { + (ia = ipv4_iffindaddr(ifp, NULL, NULL)) == NULL) { loginfox("%s: waiting for 3rd party to " - "configure IP address", ifp->name); + "configure IP address", + ifp->name); state->reason = "3RDPARTY"; script_runreason(ifp, state->reason); return; } state->offer_len = dhcp_message_new(&state->offer, - ia ? &ia->addr : &ifo->req_addr, - ia ? &ia->mask : &ifo->req_mask); + ia ? &ia->addr : &ifo->req_addr, ia ? &ia->mask : &ifo->req_mask); if (state->offer_len) #if defined(ARP) || defined(KERNEL_RFC5227) dhcp_arp_bind(ifp); @@ -2740,7 +2680,7 @@ dhcp_inform(struct interface *ifp) ia = ipv4_iffindaddr(ifp, NULL, NULL); if (ia == NULL) { loginfox("%s: waiting for 3rd party to " - "configure IP address", + "configure IP address", ifp->name); if (!(ifp->ctx->options & DHCPCD_TEST)) { state->reason = "3RDPARTY"; @@ -2752,7 +2692,8 @@ dhcp_inform(struct interface *ifp) ia = ipv4_iffindaddr(ifp, &ifo->req_addr, &ifo->req_mask); if (ia == NULL) { if (ifp->ctx->options & DHCPCD_TEST) { - logerrx("%s: cannot add IP address in test mode", + logerrx( + "%s: cannot add IP address in test mode", ifp->name); return; } @@ -2766,16 +2707,16 @@ dhcp_inform(struct interface *ifp) if (dhcp_arp_address(ifp) != 1) return; #endif - ia = ipv4_iffindaddr(ifp, - &ifo->req_addr, &ifo->req_mask); + ia = ipv4_iffindaddr(ifp, &ifo->req_addr, + &ifo->req_mask); assert(ia != NULL); } } state->state = DHS_INFORM; state->addr = ia; - state->offer_len = dhcp_message_new(&state->offer, - &ia->addr, &ia->mask); + state->offer_len = dhcp_message_new(&state->offer, &ia->addr, + &ia->mask); if (state->offer_len) { dhcp_new_xid(ifp); get_lease(ifp, &state->lease, state->offer, state->offer_len); @@ -2794,10 +2735,9 @@ dhcp_reboot_newopts(struct interface *ifp, unsigned long long oldopts) ifo = ifp->options; if ((ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC) && (state->addr == NULL || - state->addr->addr.s_addr != ifo->req_addr.s_addr)) || + state->addr->addr.s_addr != ifo->req_addr.s_addr)) || (oldopts & (DHCPCD_INFORM | DHCPCD_STATIC) && - !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))) - { + !(ifo->options & (DHCPCD_INFORM | DHCPCD_STATIC)))) { dhcp_drop(ifp, "EXPIRE"); } } @@ -2823,8 +2763,8 @@ dhcp_reboot(struct interface *ifp) return; } if (ifo->options & DHCPCD_INFORM) { - loginfox("%s: informing address of %s", - ifp->name, inet_ntoa(state->lease.addr)); + loginfox("%s: informing address of %s", ifp->name, + inet_ntoa(state->lease.addr)); dhcp_inform(ifp); return; } @@ -2835,8 +2775,8 @@ dhcp_reboot(struct interface *ifp) if (!IS_DHCP(state->offer)) return; - loginfox("%s: rebinding lease of %s", - ifp->name, inet_ntoa(state->lease.addr)); + loginfox("%s: rebinding lease of %s", ifp->name, + inet_ntoa(state->lease.addr)); #if defined(ARP) && !defined(KERNEL_RFC5227) /* Create the DHCP ARP state so we can defend it. */ @@ -2850,16 +2790,16 @@ dhcp_reboot(struct interface *ifp) #ifdef IPV4LL /* Need to add this before dhcp_expire and friends. */ if (!ifo->fallback && ifo->options & DHCPCD_IPV4LL) - eloop_timeout_add_sec(ifp->ctx->eloop, - ifo->ipv4ll_time, ipv4ll_start, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, ifo->ipv4ll_time, + ipv4ll_start, ifp); #endif if (ifo->options & DHCPCD_LASTLEASE && state->lease.frominfo) - eloop_timeout_add_sec(ifp->ctx->eloop, - ifo->reboot, dhcp_lastlease, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, ifo->reboot, + dhcp_lastlease, ifp); else if (!(ifo->options & DHCPCD_INFORM)) - eloop_timeout_add_sec(ifp->ctx->eloop, - ifo->reboot, dhcp_expire, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, ifo->reboot, dhcp_expire, + ifp); /* Don't bother ARP checking as the server could NAK us first. * Don't call dhcp_request as that would change the state */ @@ -2943,10 +2883,8 @@ dhcp_drop(struct interface *ifp, const char *reason) state->state = DHS_RELEASE; dhcp_unlink(ifp->ctx, state->leasefile); - if (if_is_link_up(ifp) && - state->new != NULL && - state->lease.server.s_addr != INADDR_ANY) - { + if (if_is_link_up(ifp) && state->new != NULL && + state->lease.server.s_addr != INADDR_ANY) { /* We need to delay removal of the IP address so the * message can be sent. * Unlike DHCPv6, there is no acknowledgement. */ @@ -2954,13 +2892,13 @@ dhcp_drop(struct interface *ifp, const char *reason) .tv_sec = 1, }; - loginfox("%s: releasing lease of %s", - ifp->name, inet_ntoa(state->lease.addr)); + loginfox("%s: releasing lease of %s", ifp->name, + inet_ntoa(state->lease.addr)); dhcp_new_xid(ifp); send_message(ifp, DHCP_RELEASE, NULL); eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); - eloop_timeout_add_tv(ifp->ctx->eloop, - &delay, dhcp_deconfigure, ifp); + eloop_timeout_add_tv(ifp->ctx->eloop, &delay, + dhcp_deconfigure, ifp); return; } } @@ -2992,9 +2930,9 @@ blacklisted_ip(const struct if_options *ifo, in_addr_t addr) return 0; } -#define WHTLST_NONE 0 -#define WHTLST_MATCH 1 -#define WHTLST_NOMATCH 2 +#define WHTLST_NONE 0 +#define WHTLST_MATCH 1 +#define WHTLST_NOMATCH 2 static unsigned int whitelisted_ip(const struct if_options *ifo, in_addr_t addr) { @@ -3009,9 +2947,9 @@ whitelisted_ip(const struct if_options *ifo, in_addr_t addr) } static void -log_dhcp(int loglevel, const char *msg, - const struct interface *ifp, const struct bootp *bootp, size_t bootp_len, - const struct in_addr *from, int ad) +log_dhcp(int loglevel, const char *msg, const struct interface *ifp, + const struct bootp *bootp, size_t bootp_len, const struct in_addr *from, + int ad) { const char *tfrom; char *a, sname[sizeof(bootp->sname) * 4]; @@ -3050,28 +2988,28 @@ log_dhcp(int loglevel, const char *msg, tfrom = "from"; r = get_option_addr(ifp->ctx, &addr, bootp, bootp_len, DHO_SERVERID); if (get_option_uint8(ifp->ctx, &overl, bootp, bootp_len, - DHO_OPTSOVERLOADED) == -1) + DHO_OPTSOVERLOADED) == -1) overl = 0; if (bootp->sname[0] && r == 0 && !(overl & 2)) { print_string(sname, sizeof(sname), OT_STRING | OT_DOMAIN, bootp->sname, sizeof(bootp->sname)); if (a == NULL) - logmessage(loglevel, "%s: %s %s %s %s", - ifp->name, msg, tfrom, inet_ntoa(addr), sname); + logmessage(loglevel, "%s: %s %s %s %s", ifp->name, msg, + tfrom, inet_ntoa(addr), sname); else - logmessage(loglevel, "%s: %s %s %s %s %s", - ifp->name, msg, a, tfrom, inet_ntoa(addr), sname); + logmessage(loglevel, "%s: %s %s %s %s %s", ifp->name, + msg, a, tfrom, inet_ntoa(addr), sname); } else { if (r != 0) { tfrom = "via"; addr = *from; } if (a == NULL) - logmessage(loglevel, "%s: %s %s %s", - ifp->name, msg, tfrom, inet_ntoa(addr)); + logmessage(loglevel, "%s: %s %s %s", ifp->name, msg, + tfrom, inet_ntoa(addr)); else - logmessage(loglevel, "%s: %s %s %s %s", - ifp->name, msg, a, tfrom, inet_ntoa(addr)); + logmessage(loglevel, "%s: %s %s %s %s", ifp->name, msg, + a, tfrom, inet_ntoa(addr)); } free(a); } @@ -3099,8 +3037,8 @@ dhcp_redirect_dhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (ifn->hwlen <= sizeof(bootp->chaddr) && memcmp(bootp->chaddr, ifn->hwaddr, ifn->hwlen)) continue; - logdebugx("%s: redirecting DHCP message to %s", - ifp->name, ifn->name); + logdebugx("%s: redirecting DHCP message to %s", ifp->name, + ifn->name); dhcp_handledhcp(ifn, bootp, bootp_len, from); } } @@ -3130,13 +3068,12 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, struct ipv4_addr *ia; #endif -#define LOGDHCP0(l, m) \ - log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0) -#define LOGDHCP(l, m) \ - log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1) +#define LOGDHCP0(l, m) log_dhcp((l), (m), ifp, bootp, bootp_len, from, 0) +#define LOGDHCP(l, m) log_dhcp((l), (m), ifp, bootp, bootp_len, from, 1) -#define IS_STATE_ACTIVE(s) ((s)-state != DHS_NONE && \ - (s)->state != DHS_INIT && (s)->state != DHS_BOUND) +#define IS_STATE_ACTIVE(s) \ + ((s) - state != DHS_NONE && (s)->state != DHS_INIT && \ + (s)->state != DHS_BOUND) /* Don't do anything if the user hasn't configured it. */ if (ifp->active != IF_ACTIVE_USER || @@ -3146,8 +3083,8 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (bootp->op != BOOTREPLY) { if (IS_STATE_ACTIVE(state)) - logdebugx("%s: op (%d) is not BOOTREPLY", - ifp->name, bootp->op); + logdebugx("%s: op (%d) is not BOOTREPLY", ifp->name, + bootp->op); return; } @@ -3161,15 +3098,14 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, } if (ifp->hwlen <= sizeof(bootp->chaddr) && - memcmp(bootp->chaddr, ifp->hwaddr, ifp->hwlen)) - { + memcmp(bootp->chaddr, ifp->hwaddr, ifp->hwlen)) { if (IS_STATE_ACTIVE(state)) { char buf[sizeof(bootp->chaddr) * 3]; - logdebugx("%s: xid 0x%x is for hwaddr %s", - ifp->name, ntohl(bootp->xid), + logdebugx("%s: xid 0x%x is for hwaddr %s", ifp->name, + ntohl(bootp->xid), hwaddr_ntoa(bootp->chaddr, sizeof(bootp->chaddr), - buf, sizeof(buf))); + buf, sizeof(buf))); } dhcp_redirect_dhcp(ifp, bootp, bootp_len, from); return; @@ -3181,8 +3117,8 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, i = whitelisted_ip(ifp->options, from->s_addr); switch (i) { case WHTLST_NOMATCH: - logwarnx("%s: non whitelisted DHCP packet from %s", - ifp->name, inet_ntoa(*from)); + logwarnx("%s: non whitelisted DHCP packet from %s", ifp->name, + inet_ntoa(*from)); return; case WHTLST_MATCH: break; @@ -3195,8 +3131,8 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, } /* We may have found a BOOTP server */ - if (get_option_uint8(ifp->ctx, &type, - bootp, bootp_len, DHO_MESSAGETYPE) == -1) + if (get_option_uint8(ifp->ctx, &type, bootp, bootp_len, + DHO_MESSAGETYPE) == -1) type = 0; else if (ifo->options & DHCPCD_BOOTP) { logdebugx("%s: ignoring DHCP reply (expecting BOOTP)", @@ -3206,19 +3142,18 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, #ifdef AUTH /* Authenticate the message */ - auth = get_option(ifp->ctx, bootp, bootp_len, - DHO_AUTHENTICATION, &auth_len); + auth = get_option(ifp->ctx, bootp, bootp_len, DHO_AUTHENTICATION, + &auth_len); if (auth) { if (dhcp_auth_validate(&state->auth, &ifo->auth, - (uint8_t *)bootp, bootp_len, 4, type, - auth, auth_len) == NULL) - { + (uint8_t *)bootp, bootp_len, 4, type, auth, + auth_len) == NULL) { LOGDHCP0(LOG_ERR, "authentication failed"); return; } if (state->auth.token) - logdebugx("%s: validated using 0x%08" PRIu32, - ifp->name, state->auth.token->secretid); + logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, + state->auth.token->secretid); else loginfox("%s: accepted reconfigure key", ifp->name); } else if (ifo->auth.options & DHCPCD_AUTH_SEND) { @@ -3233,8 +3168,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, /* RFC 3203 */ if (type == DHCP_FORCERENEW) { if (from->s_addr == INADDR_ANY || - from->s_addr == INADDR_BROADCAST) - { + from->s_addr == INADDR_BROADCAST) { LOGDHCP(LOG_ERR, "discarding Force Renew"); return; } @@ -3254,8 +3188,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (state->state == DHS_BOUND) dhcp_renew(ifp); else { - eloop_timeout_delete(ifp->ctx->eloop, - send_inform, ifp); + eloop_timeout_delete(ifp->ctx->eloop, send_inform, ifp); dhcp_inform(ifp); } #else @@ -3281,8 +3214,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, /* Ensure that no reject options are present */ for (i = 1; i < 255; i++) { if (has_option_mask(ifo->rejectmask, i) && - get_option(ifp->ctx, bootp, bootp_len, (uint8_t)i, NULL)) - { + get_option(ifp->ctx, bootp, bootp_len, (uint8_t)i, NULL)) { LOGDHCP(LOG_WARNING, "reject DHCP"); return; } @@ -3291,18 +3223,16 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (type == DHCP_NAK) { /* For NAK, only check if we require the ServerID */ if (has_option_mask(ifo->requiremask, DHO_SERVERID) && - get_option_addr(ifp->ctx, &addr, - bootp, bootp_len, DHO_SERVERID) == -1) - { + get_option_addr(ifp->ctx, &addr, bootp, bootp_len, + DHO_SERVERID) == -1) { LOGDHCP(LOG_WARNING, "reject NAK"); return; } /* We should restart on a NAK */ LOGDHCP(LOG_WARNING, "NAK:"); - if ((msg = get_option_string(ifp->ctx, - bootp, bootp_len, DHO_MESSAGE))) - { + if ((msg = get_option_string(ifp->ctx, bootp, bootp_len, + DHO_MESSAGE))) { logwarnx("%s: message: %s", ifp->name, msg); free(msg); } @@ -3314,8 +3244,8 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, } /* If we constantly get NAKS then we should slowly back off */ - eloop_timeout_add_sec(ifp->ctx->eloop, - state->nakoff, dhcp_discover, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, state->nakoff, + dhcp_discover, ifp); if (state->nakoff == 0) state->nakoff = 1; else { @@ -3329,8 +3259,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, /* Ensure that all required options are present */ for (i = 1; i < 255; i++) { if (has_option_mask(ifo->requiremask, i) && - !get_option(ifp->ctx, bootp, bootp_len, (uint8_t)i, NULL)) - { + !get_option(ifp->ctx, bootp, bootp_len, (uint8_t)i, NULL)) { /* If we are BOOTP, then ignore the need for serverid. * To ignore BOOTP, require dhcp_message_type. * However, nothing really stops BOOTP from providing @@ -3345,10 +3274,10 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (has_option_mask(ifo->requestmask, DHO_IPV6_PREFERRED_ONLY)) { if (get_option_uint32(ifp->ctx, &v6only_time, bootp, bootp_len, - DHO_IPV6_PREFERRED_ONLY) == 0 && + DHO_IPV6_PREFERRED_ONLY) == 0 && (state->state == DHS_DISCOVER || - state->state == DHS_REBOOT || state->state == DHS_NONE)) - { + state->state == DHS_REBOOT || + state->state == DHS_NONE)) { char v6msg[128]; use_v6only = true; @@ -3364,17 +3293,15 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, /* DHCP Auto-Configure, RFC 2563 */ if (type == DHCP_OFFER && bootp->yiaddr == INADDR_ANY) { LOGDHCP(LOG_WARNING, "no address offered"); - if ((msg = get_option_string(ifp->ctx, - bootp, bootp_len, DHO_MESSAGE))) - { + if ((msg = get_option_string(ifp->ctx, bootp, bootp_len, + DHO_MESSAGE))) { logwarnx("%s: message: %s", ifp->name, msg); free(msg); } #ifdef IPV4LL if (state->state == DHS_DISCOVER && get_option_uint8(ifp->ctx, &tmp, bootp, bootp_len, - DHO_AUTOCONFIGURE) == 0) - { + DHO_AUTOCONFIGURE) == 0) { has_auto_conf = true; switch (tmp) { case 0: @@ -3391,7 +3318,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, break; default: logerrx("%s: unknown auto configuration " - "option %d", + "option %d", ifp->name, tmp); break; } @@ -3406,8 +3333,7 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (use_v6only || has_auto_conf) { eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); eloop_timeout_add_sec(ifp->ctx->eloop, - use_v6only ? v6only_time : DHCP_MAX, - dhcp_discover, ifp); + use_v6only ? v6only_time : DHCP_MAX, dhcp_discover, ifp); return; } @@ -3418,10 +3344,10 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, /* Ensure that the address offered is valid */ if ((type == 0 || type == DHCP_OFFER || type == DHCP_ACK) && - (bootp->ciaddr == INADDR_ANY || bootp->ciaddr == INADDR_BROADCAST) - && - (bootp->yiaddr == INADDR_ANY || bootp->yiaddr == INADDR_BROADCAST)) - { + (bootp->ciaddr == INADDR_ANY || + bootp->ciaddr == INADDR_BROADCAST) && + (bootp->yiaddr == INADDR_ANY || + bootp->yiaddr == INADDR_BROADCAST)) { LOGDHCP(LOG_WARNING, "reject invalid address"); return; } @@ -3434,8 +3360,8 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, dhcp_decline(ifp); ipv4_deladdr(ia, 0); eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); - eloop_timeout_add_sec(ifp->ctx->eloop, - DHCP_RAND_MAX, dhcp_discover, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, DHCP_RAND_MAX, + dhcp_discover, ifp); return; } #endif @@ -3446,16 +3372,15 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, lease->addr.s_addr = bootp->yiaddr; memcpy(&lease->cookie, bootp->vend, sizeof(lease->cookie)); if (type == 0 || - get_option_addr(ifp->ctx, - &lease->server, bootp, bootp_len, DHO_SERVERID) != 0) + get_option_addr(ifp->ctx, &lease->server, bootp, bootp_len, + DHO_SERVERID) != 0) lease->server.s_addr = INADDR_ANY; /* Test for rapid commit in the OFFER */ if (!(ifp->ctx->options & DHCPCD_TEST) && has_option_mask(ifo->requestmask, DHO_RAPIDCOMMIT) && - get_option(ifp->ctx, bootp, bootp_len, - DHO_RAPIDCOMMIT, NULL)) - { + get_option(ifp->ctx, bootp, bootp_len, DHO_RAPIDCOMMIT, + NULL)) { state->state = DHS_REQUEST; goto rapidcommit; } @@ -3516,9 +3441,9 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, if (state->state == DHS_DISCOVER) { /* We only allow ACK of rapid commit DISCOVER. */ if (has_option_mask(ifo->requestmask, - DHO_RAPIDCOMMIT) && + DHO_RAPIDCOMMIT) && get_option(ifp->ctx, bootp, bootp_len, - DHO_RAPIDCOMMIT, NULL)) + DHO_RAPIDCOMMIT, NULL)) state->state = DHS_REQUEST; else { LOGDHCP(LOG_DEBUG, "ignoring ack of"); @@ -3526,11 +3451,11 @@ dhcp_handledhcp(struct interface *ifp, struct bootp *bootp, size_t bootp_len, } } -rapidcommit: + rapidcommit: if (!(ifo->options & DHCPCD_INFORM)) LOGDHCP(LOG_DEBUG, "acknowledged"); else - ifo->options &= ~DHCPCD_STATIC; + ifo->options &= ~DHCPCD_STATIC; } /* No NAK, so reset the backoff @@ -3624,8 +3549,7 @@ struct ip_pseudo { /* Lengths have already been checked. */ static bool -checksums_valid(const void *packet, - struct in_addr *from, unsigned int flags) +checksums_valid(const void *packet, struct in_addr *from, unsigned int flags) { const struct ip *ip = packet; size_t ip_hlen; @@ -3732,8 +3656,8 @@ dhcp_packet(struct interface *ifp, uint8_t *data, size_t len, /* Trim frame header */ if (fl != 0) { if (len < fl) { - logerrx("%s: %s: short frame header %zu", - __func__, ifp->name, len); + logerrx("%s: %s: short frame header %zu", __func__, + ifp->name, len); return; } len -= fl; @@ -3750,8 +3674,8 @@ dhcp_packet(struct interface *ifp, uint8_t *data, size_t len, } if (!checksums_valid(data, &from, bpf_flags)) { - logerrx("%s: checksum failure from %s", - ifp->name, inet_ntoa(from)); + logerrx("%s: checksum failure from %s", ifp->name, + inet_ntoa(from)); return; } @@ -3813,8 +3737,8 @@ dhcp_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg) } if (iov->iov_len < offsetof(struct bootp, vend)) { - logerrx("%s: truncated packet (%zu) from %s", - ifp->name, iov->iov_len, inet_ntoa(from->sin_addr)); + logerrx("%s: truncated packet (%zu) from %s", ifp->name, + iov->iov_len, inet_ntoa(from->sin_addr)); return; } @@ -3844,8 +3768,7 @@ dhcp_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg) } #endif - dhcp_handlebootp(ifp, iov->iov_base, iov->iov_len, - &from->sin_addr); + dhcp_handlebootp(ifp, iov->iov_base, iov->iov_len, &from->sin_addr); } static void @@ -3871,9 +3794,12 @@ dhcp_readudp(struct dhcpcd_ctx *ctx, struct interface *ifp, #endif } cmsgbuf = { .buf = { 0 } }; struct msghdr msg = { - .msg_name = &from, .msg_namelen = sizeof(from), - .msg_iov = &iov, .msg_iovlen = 1, - .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf), + .msg_name = &from, + .msg_namelen = sizeof(from), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = sizeof(cmsgbuf.buf), }; int s; ssize_t bytes; @@ -3946,7 +3872,7 @@ dhcp_openbpf(struct interface *ifp) } if (eloop_event_add(ifp->ctx->eloop, state->bpf->bpf_fd, ELE_READ, - dhcp_readbpf, ifp) == -1) + dhcp_readbpf, ifp) == -1) logerr("%s: eloop_event_add", __func__); return 0; } @@ -4036,8 +3962,8 @@ dhcp_init(struct interface *ifp) state->state = DHS_INIT; state->reason = "PREINIT"; state->nakoff = 0; - dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), - AF_INET, ifp); + dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), AF_INET, + ifp); ifo = ifp->options; /* We need to drop the leasefile so that dhcp_start @@ -4063,21 +3989,20 @@ dhcp_init(struct interface *ifp) state->clientid = malloc(ifp->ctx->duid_len + 6); if (state->clientid == NULL) goto eexit; - state->clientid[0] =(uint8_t)(ifp->ctx->duid_len + 5); + state->clientid[0] = (uint8_t)(ifp->ctx->duid_len + 5); state->clientid[1] = 255; /* RFC 4361 */ memcpy(state->clientid + 2, ifo->iaid, 4); memcpy(state->clientid + 6, ifp->ctx->duid, ifp->ctx->duid_len); } else { -make_clientid: + make_clientid: len = (uint8_t)(ifp->hwlen + 1); state->clientid = malloc((size_t)len + 1); if (state->clientid == NULL) goto eexit; state->clientid[0] = len; state->clientid[1] = (uint8_t)ifp->hwtype; - memcpy(state->clientid + 2, ifp->hwaddr, - ifp->hwlen); + memcpy(state->clientid + 2, ifp->hwaddr, ifp->hwlen); } } @@ -4088,8 +4013,8 @@ dhcp_init(struct interface *ifp) if (ifo->options & DHCPCD_CLIENTID && state->clientid != NULL) logdebugx("%s: using ClientID %s", ifp->name, - hwaddr_ntoa(state->clientid + 1, state->clientid[0], - buf, sizeof(buf))); + hwaddr_ntoa(state->clientid + 1, state->clientid[0], buf, + sizeof(buf))); else if (ifp->hwlen) logdebugx("%s: using hwaddr %s", ifp->name, hwaddr_ntoa(ifp->hwaddr, ifp->hwlen, buf, sizeof(buf))); @@ -4117,20 +4042,21 @@ dhcp_start1(void *arg) * ICMP port unreachable message back to the DHCP server. * Only do this in manager mode so we don't swallow messages * for dhcpcd running on another interface. */ - if ((ctx->options & (DHCPCD_MANAGER|DHCPCD_PRIVSEP)) == DHCPCD_MANAGER - && ctx->udp_rfd == -1) - { + if ((ctx->options & (DHCPCD_MANAGER | DHCPCD_PRIVSEP)) == + DHCPCD_MANAGER && + ctx->udp_rfd == -1) { ctx->udp_rfd = dhcp_openudp(NULL); if (ctx->udp_rfd == -1) { logerr(__func__); return; } if (eloop_event_add(ctx->eloop, ctx->udp_rfd, ELE_READ, - dhcp_handleudp, ctx) == -1) + dhcp_handleudp, ctx) == -1) logerr("%s: eloop_event_add", __func__); } if (!IN_PRIVSEP(ctx) && ctx->udp_wfd == -1) { - ctx->udp_wfd = xsocket(PF_INET, SOCK_RAW|SOCK_CXNB,IPPROTO_UDP); + ctx->udp_wfd = xsocket(PF_INET, SOCK_RAW | SOCK_CXNB, + IPPROTO_UDP); if (ctx->udp_wfd == -1) { logerr(__func__); return; @@ -4181,13 +4107,12 @@ dhcp_start1(void *arg) #endif if ((!IS_DHCP(state->offer) && - !(ifo->options & DHCPCD_BOOTP)) || + !(ifo->options & DHCPCD_BOOTP)) || #ifdef IN_IFF_DUPLICATED (ia && ia->addr_flags & IN_IFF_DUPLICATED) || #endif (IS_DHCP(state->offer) && - ifo->options & DHCPCD_BOOTP)) - { + ifo->options & DHCPCD_BOOTP)) { free(state->offer); state->offer = NULL; state->offer_len = 0; @@ -4201,16 +4126,15 @@ dhcp_start1(void *arg) get_lease(ifp, &state->lease, state->offer, state->offer_len); state->lease.frominfo = 1; if (state->new == NULL && - (ia = ipv4_iffindaddr(ifp, - &state->lease.addr, &state->lease.mask)) != NULL) - { + (ia = ipv4_iffindaddr(ifp, &state->lease.addr, + &state->lease.mask)) != NULL) { /* We still have the IP address from the last lease. * Fake add the address and routes from it so the lease * can be cleaned up. */ state->new = malloc(state->offer_len); if (state->new) { - memcpy(state->new, - state->offer, state->offer_len); + memcpy(state->new, state->offer, + state->offer_len); state->new_len = state->offer_len; state->addr = ia; state->added |= STATE_ADDED | STATE_FAKE; @@ -4224,15 +4148,13 @@ dhcp_start1(void *arg) state->offer_len = 0; } else if (!(ifo->options & DHCPCD_LASTLEASE_EXTEND) && state->lease.leasetime != DHCP_INFINITE_LIFETIME && - dhcp_filemtime(ifp->ctx, state->leasefile, &mtime) == 0) - { + dhcp_filemtime(ifp->ctx, state->leasefile, &mtime) == 0) { time_t now; /* Offset lease times and check expiry */ now = time(NULL); if (now == -1 || - (time_t)state->lease.leasetime < now - mtime) - { + (time_t)state->lease.leasetime < now - mtime) { logdebugx("%s: discarding expired lease", ifp->name); free(state->offer); @@ -4272,8 +4194,7 @@ dhcp_start1(void *arg) } #endif - if (state->offer == NULL || - !IS_DHCP(state->offer) || + if (state->offer == NULL || !IS_DHCP(state->offer) || ifo->options & DHCPCD_ANONYMOUS) dhcp_discover(ifp); else @@ -4295,8 +4216,8 @@ dhcp_start(struct interface *ifp) * set it now. */ if (ifp->options->req_addr.s_addr != INADDR_ANY && ifp->options->req_mask.s_addr == INADDR_ANY) - ifp->options->req_mask.s_addr = - ipv4_getnetmask(ifp->options->req_addr.s_addr); + ifp->options->req_mask.s_addr = ipv4_getnetmask( + ifp->options->req_addr.s_addr); /* If we haven't specified a ClientID and our hardware address * length is greater than BOOTP CHADDR then we enforce a ClientID @@ -4311,7 +4232,7 @@ dhcp_start(struct interface *ifp) /* Firewire and InfiniBand interfaces require ClientID and * the broadcast option being set. */ switch (ifp->hwtype) { - case ARPHRD_IEEE1394: /* FALLTHROUGH */ + case ARPHRD_IEEE1394: /* FALLTHROUGH */ case ARPHRD_INFINIBAND: ifp->options->options |= DHCPCD_CLIENTID | DHCPCD_BROADCAST; break; @@ -4324,8 +4245,7 @@ dhcp_start(struct interface *ifp) /* No point in delaying a static configuration */ if (ifp->options->options & DHCPCD_STATIC || - !(ifp->options->options & DHCPCD_INITIAL_DELAY)) - { + !(ifp->options->options & DHCPCD_INITIAL_DELAY)) { dhcp_start1(ifp); return; } @@ -4339,9 +4259,9 @@ dhcp_start(struct interface *ifp) } #endif delay = MSEC_PER_SEC + - (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC); - logdebugx("%s: delaying IPv4 for %0.1f seconds", - ifp->name, (float)delay / MSEC_PER_SEC); + (arc4random_uniform(MSEC_PER_SEC * 2) - MSEC_PER_SEC); + logdebugx("%s: delaying IPv4 for %0.1f seconds", ifp->name, + (float)delay / MSEC_PER_SEC); eloop_timeout_add_msec(ifp->ctx->eloop, delay, dhcp_start1, ifp); } @@ -4378,8 +4298,8 @@ dhcp_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) if (cmd == RTM_DELADDR) { if (state->addr == ia) { - loginfox("%s: pid %d deleted IP address %s", - ifp->name, (int)pid, ia->saddr); + loginfox("%s: pid %d deleted IP address %s", ifp->name, + (int)pid, ia->saddr); dhcp_close(ifp); state->addr = NULL; /* Don't clear the added state as we need @@ -4403,8 +4323,7 @@ dhcp_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) ifo = ifp->options; if (!(ifp->ctx->options & (DHCPCD_MANAGER | DHCPCD_CONFIGURE)) && - IN_ARE_ADDR_EQUAL(&state->lease.addr, &ia->addr)) - { + IN_ARE_ADDR_EQUAL(&state->lease.addr, &ia->addr)) { uint8_t old_state = state->added; state->addr = ia; @@ -4437,7 +4356,7 @@ dhcp_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) if (ifp->flags & IFF_POINTOPOINT) { for (i = 1; i < 255; i++) - if (i != DHO_ROUTER && has_option_mask(ifo->dstmask,i)) + if (i != DHO_ROUTER && has_option_mask(ifo->dstmask, i)) dhcp_message_add_addr(state->new, i, ia->brd); } diff --git a/src/dhcp.h b/src/dhcp.h index e8c2a1e3..4c97169d 100644 --- a/src/dhcp.h +++ b/src/dhcp.h @@ -29,10 +29,10 @@ #ifndef DHCP_H #define DHCP_H -#include #include - #include + +#include #define __FAVOR_BSD /* Nasty glibc hack so we can use BSD semantics for UDP */ #include #undef __FAVOR_BSD @@ -41,92 +41,92 @@ #include #include "arp.h" -#include "bpf.h" #include "auth.h" +#include "bpf.h" #include "dhcp-common.h" /* UDP port numbers for BOOTP */ -#define BOOTPS 67 -#define BOOTPC 68 +#define BOOTPS 67 +#define BOOTPC 68 -#define MAGIC_COOKIE 0x63825363 -#define BROADCAST_FLAG 0x8000 +#define MAGIC_COOKIE 0x63825363 +#define BROADCAST_FLAG 0x8000 /* BOOTP message OP code */ -#define BOOTREQUEST 1 -#define BOOTREPLY 2 +#define BOOTREQUEST 1 +#define BOOTREPLY 2 /* DHCP message type */ -#define DHCP_DISCOVER 1 -#define DHCP_OFFER 2 -#define DHCP_REQUEST 3 -#define DHCP_DECLINE 4 -#define DHCP_ACK 5 -#define DHCP_NAK 6 -#define DHCP_RELEASE 7 -#define DHCP_INFORM 8 -#define DHCP_FORCERENEW 9 +#define DHCP_DISCOVER 1 +#define DHCP_OFFER 2 +#define DHCP_REQUEST 3 +#define DHCP_DECLINE 4 +#define DHCP_ACK 5 +#define DHCP_NAK 6 +#define DHCP_RELEASE 7 +#define DHCP_INFORM 8 +#define DHCP_FORCERENEW 9 /* Constants taken from RFC 2131. */ -#define T1 0.5 -#define T2 0.875 -#define DHCP_BASE 4 -#define DHCP_MAX 64 -#define DHCP_RAND_MIN -1 -#define DHCP_RAND_MAX 1 +#define T1 0.5 +#define T2 0.875 +#define DHCP_BASE 4 +#define DHCP_MAX 64 +#define DHCP_RAND_MIN -1 +#define DHCP_RAND_MAX 1 #ifdef RFC2131_STRICT /* Be strictly conformant for section 4.1.1 */ -# define DHCP_MIN_DELAY 1 -# define DHCP_MAX_DELAY 10 +#define DHCP_MIN_DELAY 1 +#define DHCP_MAX_DELAY 10 #else /* or mirror the more modern IPv6RS and DHCPv6 delays */ -# define DHCP_MIN_DELAY 0 -# define DHCP_MAX_DELAY 1 +#define DHCP_MIN_DELAY 0 +#define DHCP_MAX_DELAY 1 #endif /* DHCP options */ enum DHO { - DHO_PAD = 0, - DHO_SUBNETMASK = 1, - DHO_ROUTER = 3, - DHO_DNSSERVER = 6, - DHO_HOSTNAME = 12, - DHO_DNSDOMAIN = 15, - DHO_MTU = 26, - DHO_BROADCAST = 28, - DHO_STATICROUTE = 33, - DHO_NISDOMAIN = 40, - DHO_NISSERVER = 41, - DHO_NTPSERVER = 42, - DHO_VENDOR = 43, - DHO_IPADDRESS = 50, - DHO_LEASETIME = 51, - DHO_OPTSOVERLOADED = 52, - DHO_MESSAGETYPE = 53, - DHO_SERVERID = 54, - DHO_PARAMETERREQUESTLIST = 55, - DHO_MESSAGE = 56, - DHO_MAXMESSAGESIZE = 57, - DHO_RENEWALTIME = 58, - DHO_REBINDTIME = 59, - DHO_VENDORCLASSID = 60, - DHO_CLIENTID = 61, - DHO_USERCLASS = 77, /* RFC 3004 */ - DHO_RAPIDCOMMIT = 80, /* RFC 4039 */ - DHO_FQDN = 81, - DHO_AUTHENTICATION = 90, /* RFC 3118 */ - DHO_IPV6_PREFERRED_ONLY = 108, /* RFC 8925 */ - DHO_AUTOCONFIGURE = 116, /* RFC 2563 */ - DHO_DNSSEARCH = 119, /* RFC 3397 */ - DHO_CSR = 121, /* RFC 3442 */ - DHO_VIVCO = 124, /* RFC 3925 */ - DHO_VIVSO = 125, /* RFC 3925 */ - DHO_FORCERENEW_NONCE = 145, /* RFC 6704 */ - DHO_MUDURL = 161, /* draft-ietf-opsawg-mud */ - DHO_SIXRD = 212, /* RFC 5969 */ - DHO_MSCSR = 249, /* MS code for RFC 3442 */ - DHO_END = 255 + DHO_PAD = 0, + DHO_SUBNETMASK = 1, + DHO_ROUTER = 3, + DHO_DNSSERVER = 6, + DHO_HOSTNAME = 12, + DHO_DNSDOMAIN = 15, + DHO_MTU = 26, + DHO_BROADCAST = 28, + DHO_STATICROUTE = 33, + DHO_NISDOMAIN = 40, + DHO_NISSERVER = 41, + DHO_NTPSERVER = 42, + DHO_VENDOR = 43, + DHO_IPADDRESS = 50, + DHO_LEASETIME = 51, + DHO_OPTSOVERLOADED = 52, + DHO_MESSAGETYPE = 53, + DHO_SERVERID = 54, + DHO_PARAMETERREQUESTLIST = 55, + DHO_MESSAGE = 56, + DHO_MAXMESSAGESIZE = 57, + DHO_RENEWALTIME = 58, + DHO_REBINDTIME = 59, + DHO_VENDORCLASSID = 60, + DHO_CLIENTID = 61, + DHO_USERCLASS = 77, /* RFC 3004 */ + DHO_RAPIDCOMMIT = 80, /* RFC 4039 */ + DHO_FQDN = 81, + DHO_AUTHENTICATION = 90, /* RFC 3118 */ + DHO_IPV6_PREFERRED_ONLY = 108, /* RFC 8925 */ + DHO_AUTOCONFIGURE = 116, /* RFC 2563 */ + DHO_DNSSEARCH = 119, /* RFC 3397 */ + DHO_CSR = 121, /* RFC 3442 */ + DHO_VIVCO = 124, /* RFC 3925 */ + DHO_VIVSO = 125, /* RFC 3925 */ + DHO_FORCERENEW_NONCE = 145, /* RFC 6704 */ + DHO_MUDURL = 161, /* draft-ietf-opsawg-mud */ + DHO_SIXRD = 212, /* RFC 5969 */ + DHO_MSCSR = 249, /* MS code for RFC 3442 */ + DHO_END = 255 }; /* FQDN values - lsnybble used in flags @@ -134,44 +134,43 @@ enum DHO { * and to allow 0x00 to mean disable */ enum FQDN { - FQDN_DISABLE = 0x00, - FQDN_NONE = 0x18, - FQDN_PTR = 0x20, - FQDN_BOTH = 0x31 + FQDN_DISABLE = 0x00, + FQDN_NONE = 0x18, + FQDN_PTR = 0x20, + FQDN_BOTH = 0x31 }; -#define MIN_V6ONLY_WAIT 300 /* seconds, RFC 8925 */ +#define MIN_V6ONLY_WAIT 300 /* seconds, RFC 8925 */ /* Sizes for BOOTP options */ -#define BOOTP_CHADDR_LEN 16 -#define BOOTP_SNAME_LEN 64 -#define BOOTP_FILE_LEN 128 -#define BOOTP_VEND_LEN 64 +#define BOOTP_CHADDR_LEN 16 +#define BOOTP_SNAME_LEN 64 +#define BOOTP_FILE_LEN 128 +#define BOOTP_VEND_LEN 64 /* DHCP is basically an extension to BOOTP */ struct bootp { - uint8_t op; /* message type */ - uint8_t htype; /* hardware address type */ - uint8_t hlen; /* hardware address length */ - uint8_t hops; /* should be zero in client message */ - uint32_t xid; /* transaction id */ - uint16_t secs; /* elapsed time in sec. from boot */ - uint16_t flags; /* such as broadcast flag */ - uint32_t ciaddr; /* (previously allocated) client IP */ - uint32_t yiaddr; /* 'your' client IP address */ - uint32_t siaddr; /* should be zero in client's messages */ - uint32_t giaddr; /* should be zero in client's messages */ - uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client's hardware address */ - uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */ - uint8_t file[BOOTP_FILE_LEN]; /* boot file name */ - uint8_t vend[BOOTP_VEND_LEN]; /* vendor specific area */ + uint8_t op; /* message type */ + uint8_t htype; /* hardware address type */ + uint8_t hlen; /* hardware address length */ + uint8_t hops; /* should be zero in client message */ + uint32_t xid; /* transaction id */ + uint16_t secs; /* elapsed time in sec. from boot */ + uint16_t flags; /* such as broadcast flag */ + uint32_t ciaddr; /* (previously allocated) client IP */ + uint32_t yiaddr; /* 'your' client IP address */ + uint32_t siaddr; /* should be zero in client's messages */ + uint32_t giaddr; /* should be zero in client's messages */ + uint8_t chaddr[BOOTP_CHADDR_LEN]; /* client's hardware address */ + uint8_t sname[BOOTP_SNAME_LEN]; /* server host name */ + uint8_t file[BOOTP_FILE_LEN]; /* boot file name */ + uint8_t vend[BOOTP_VEND_LEN]; /* vendor specific area */ /* DHCP allows a variable length vendor area */ }; -#define DHCP_MIN_LEN (offsetof(struct bootp, vend) + 4) +#define DHCP_MIN_LEN (offsetof(struct bootp, vend) + 4) -struct bootp_pkt -{ +struct bootp_pkt { struct ip ip; struct udphdr udp; struct bootp bootp; @@ -190,7 +189,7 @@ struct dhcp_lease { }; #ifndef DHCP_INFINITE_LIFETIME -# define DHCP_INFINITE_LIFETIME (~0U) +#define DHCP_INFINITE_LIFETIME (~0U) #endif enum DHS { @@ -240,17 +239,14 @@ struct dhcp_state { }; #ifdef INET -#define D_STATE(ifp) \ - ((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP]) -#define D_CSTATE(ifp) \ - ((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP]) -#define D_STATE_RUNNING(ifp) \ +#define D_STATE(ifp) ((struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP]) +#define D_CSTATE(ifp) ((const struct dhcp_state *)(ifp)->if_data[IF_DATA_DHCP]) +#define D_STATE_RUNNING(ifp) \ (D_CSTATE((ifp)) && D_CSTATE((ifp))->new && D_CSTATE((ifp))->reason) -#define IS_DHCP(b) ((b)->vend[0] == 0x63 && \ - (b)->vend[1] == 0x82 && \ - (b)->vend[2] == 0x53 && \ - (b)->vend[3] == 0x63) +#define IS_DHCP(b) \ + ((b)->vend[0] == 0x63 && (b)->vend[1] == 0x82 && \ + (b)->vend[2] == 0x53 && (b)->vend[3] == 0x63) #include "dhcpcd.h" #include "if-options.h" @@ -261,8 +257,8 @@ ssize_t print_rfc3442(FILE *, const uint8_t *, size_t); int dhcp_openudp(struct in_addr *); void dhcp_packet(struct interface *, uint8_t *, size_t, unsigned int); void dhcp_recvmsg(struct dhcpcd_ctx *, struct msghdr *); -void dhcp_printoptions(const struct dhcpcd_ctx *, - const struct dhcp_opt *, size_t); +void dhcp_printoptions(const struct dhcpcd_ctx *, const struct dhcp_opt *, + size_t); uint16_t dhcp_get_mtu(const struct interface *); int dhcp_get_routes(rb_tree_t *, struct interface *); ssize_t dhcp_env(FILE *, const char *, const struct interface *, diff --git a/src/dhcp6.c b/src/dhcp6.c index 91341994..68574874 100644 --- a/src/dhcp6.c +++ b/src/dhcp6.c @@ -26,8 +26,8 @@ * SUCH DAMAGE. */ -#include #include +#include #include #include @@ -41,19 +41,18 @@ #include #include #include -#include -#include #include +#include -#define ELOOP_QUEUE ELOOP_DHCP6 -#include "config.h" +#define ELOOP_QUEUE ELOOP_DHCP6 #include "common.h" +#include "config.h" #include "dhcp.h" #include "dhcp6.h" #include "duid.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv6nd.h" #include "logerr.h" #include "privsep.h" @@ -69,7 +68,7 @@ #define DHCPCD_IANA_PEN 40712 /* Unsure if I want this */ -//#define VENDOR_SPLIT +// #define VENDOR_SPLIT /* Support older systems with different defines */ #if !defined(IPV6_RECVPKTINFO) && defined(IPV6_PKTINFO) @@ -124,31 +123,24 @@ struct dhcp6_pd_addr { __CTASSERT(sizeof(struct dhcp6_pd_addr) == 8 + 1 + 16); #endif -#define DHCP6_PD_ADDR_SIZE (8 + 1 + 16) -#define DHCP6_PD_ADDR_PLTIME 0 -#define DHCP6_PD_ADDR_VLTIME 4 -#define DHCP6_PD_ADDR_PLEN 8 -#define DHCP6_PD_ADDR_PREFIX 9 +#define DHCP6_PD_ADDR_SIZE (8 + 1 + 16) +#define DHCP6_PD_ADDR_PLTIME 0 +#define DHCP6_PD_ADDR_VLTIME 4 +#define DHCP6_PD_ADDR_PLEN 8 +#define DHCP6_PD_ADDR_PREFIX 9 struct dhcp6_op { uint16_t type; const char *name; }; -static const struct dhcp6_op dhcp6_ops[] = { - { DHCP6_SOLICIT, "SOLICIT6" }, - { DHCP6_ADVERTISE, "ADVERTISE6" }, - { DHCP6_REQUEST, "REQUEST6" }, - { DHCP6_REPLY, "REPLY6" }, - { DHCP6_RENEW, "RENEW6" }, - { DHCP6_REBIND, "REBIND6" }, - { DHCP6_CONFIRM, "CONFIRM6" }, - { DHCP6_INFORMATION_REQ, "INFORM6" }, - { DHCP6_RELEASE, "RELEASE6" }, - { DHCP6_RECONFIGURE, "RECONFIGURE6" }, - { DHCP6_DECLINE, "DECLINE6" }, - { 0, NULL } -}; +static const struct dhcp6_op dhcp6_ops[] = { { DHCP6_SOLICIT, "SOLICIT6" }, + { DHCP6_ADVERTISE, "ADVERTISE6" }, { DHCP6_REQUEST, "REQUEST6" }, + { DHCP6_REPLY, "REPLY6" }, { DHCP6_RENEW, "RENEW6" }, + { DHCP6_REBIND, "REBIND6" }, { DHCP6_CONFIRM, "CONFIRM6" }, + { DHCP6_INFORMATION_REQ, "INFORM6" }, { DHCP6_RELEASE, "RELEASE6" }, + { DHCP6_RECONFIGURE, "RECONFIGURE6" }, { DHCP6_DECLINE, "DECLINE6" }, + { 0, NULL } }; struct dhcp_compat { uint8_t dhcp_opt; @@ -160,30 +152,20 @@ struct dhcp_compat { * But we can support both as the hook scripts will uniqify the * results if the server returns both options. */ -static const struct dhcp_compat dhcp_compats[] = { - { DHO_DNSSERVER, D6_OPTION_DNS_SERVERS }, - { DHO_HOSTNAME, D6_OPTION_FQDN }, - { DHO_DNSDOMAIN, D6_OPTION_FQDN }, - { DHO_NISSERVER, D6_OPTION_NIS_SERVERS }, - { DHO_NTPSERVER, D6_OPTION_SNTP_SERVERS }, - { DHO_NTPSERVER, D6_OPTION_NTP_SERVER }, - { DHO_RAPIDCOMMIT, D6_OPTION_RAPID_COMMIT }, - { DHO_FQDN, D6_OPTION_FQDN }, - { DHO_VIVCO, D6_OPTION_VENDOR_CLASS }, - { DHO_VIVSO, D6_OPTION_VENDOR_OPTS }, - { DHO_DNSSEARCH, D6_OPTION_DOMAIN_LIST }, - { 0, 0 } -}; - -static const char * const dhcp6_statuses[] = { - "Success", - "Unspecified Failure", - "No Addresses Available", - "No Binding", - "Not On Link", - "Use Multicast", - "No Prefix Available" -}; +static const struct dhcp_compat dhcp_compats[] = { { DHO_DNSSERVER, + D6_OPTION_DNS_SERVERS }, + { DHO_HOSTNAME, D6_OPTION_FQDN }, { DHO_DNSDOMAIN, D6_OPTION_FQDN }, + { DHO_NISSERVER, D6_OPTION_NIS_SERVERS }, + { DHO_NTPSERVER, D6_OPTION_SNTP_SERVERS }, + { DHO_NTPSERVER, D6_OPTION_NTP_SERVER }, + { DHO_RAPIDCOMMIT, D6_OPTION_RAPID_COMMIT }, + { DHO_FQDN, D6_OPTION_FQDN }, { DHO_VIVCO, D6_OPTION_VENDOR_CLASS }, + { DHO_VIVSO, D6_OPTION_VENDOR_OPTS }, + { DHO_DNSSEARCH, D6_OPTION_DOMAIN_LIST }, { 0, 0 } }; + +static const char *const dhcp6_statuses[] = { "Success", "Unspecified Failure", + "No Addresses Available", "No Binding", "Not On Link", "Use Multicast", + "No Prefix Available" }; static void dhcp6_bind(struct interface *, const char *, const char *); static void dhcp6_failinform(void *); @@ -193,33 +175,30 @@ static void dhcp6_startdecline(struct interface *); static void dhcp6_startrequest(struct interface *); #ifdef SMALL -#define dhcp6_hasprefixdelegation(a) (0) +#define dhcp6_hasprefixdelegation(a) (0) #else static int dhcp6_hasprefixdelegation(struct interface *); #endif -#define DECLINE_IA(ia) \ - ((ia)->addr_flags & IN6_IFF_DUPLICATED && \ - (ia)->ia_type != 0 && (ia)->ia_type != D6_OPTION_IA_PD && \ - !((ia)->flags & IPV6_AF_STALE) && \ - (ia)->prefix_vltime != 0) - +#define DECLINE_IA(ia) \ + ((ia)->addr_flags & IN6_IFF_DUPLICATED && (ia)->ia_type != 0 && \ + (ia)->ia_type != D6_OPTION_IA_PD && \ + !((ia)->flags & IPV6_AF_STALE) && (ia)->prefix_vltime != 0) /* Gets a pointer to the length part of the option to fill it * in later. */ #define NEXTLEN(p) ((p) + offsetof(struct dhcp6_option, len)) void -dhcp6_printoptions(const struct dhcpcd_ctx *ctx, - const struct dhcp_opt *opts, size_t opts_len) +dhcp6_printoptions(const struct dhcpcd_ctx *ctx, const struct dhcp_opt *opts, + size_t opts_len) { size_t i, j; const struct dhcp_opt *opt, *opt2; int cols; - for (i = 0, opt = ctx->dhcp6_opts; - i < ctx->dhcp6_opts_len; i++, opt++) - { + for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; + i++, opt++) { for (j = 0, opt2 = opts; j < opts_len; j++, opt2++) if (opt2->option == opt->option) break; @@ -289,10 +268,10 @@ dhcp6_makevendor(void *data, const struct interface *ifp) ifo = ifp->options; if (ifo->vivco_len > 0) { - for (i = 0, vivco = ifo->vivco; - i < ifo->vivco_len; + for (i = 0, vivco = ifo->vivco; i < ifo->vivco_len; i++, vivco++) - len += sizeof(o) + sizeof(uint32_t) + sizeof(uint16_t) + vivco->len; + len += sizeof(o) + sizeof(uint32_t) + sizeof(uint16_t) + + vivco->len; } else if (ifo->vendorclassid[0] != '\0') { /* dhcpcd owns DHCPCD_IANA_PEN. * If you need your own string, get your own IANA PEN. */ @@ -313,10 +292,10 @@ dhcp6_makevendor(void *data, const struct interface *ifp) p = data; if (ifo->vivco_len > 0) { - for (i = 0, vivco = ifo->vivco; - i < ifo->vivco_len; + for (i = 0, vivco = ifo->vivco; i < ifo->vivco_len; i++, vivco++) { - optlen = sizeof(uint32_t) + sizeof(uint16_t) + vivco->len; + optlen = sizeof(uint32_t) + sizeof(uint16_t) + + vivco->len; o.code = htons(D6_OPTION_VENDOR_CLASS); o.len = htons((uint16_t)optlen); memcpy(p, &o, sizeof(o)); @@ -382,9 +361,8 @@ dhcp6_makevendoropts(void *data, const struct interface *ifp) so_endp = vsio->so + vsio->so_len; for (so = vsio->so; so != so_endp; so++) { - if (olen + sizeof(opt) + sizeof(slen) - + so->len > UINT16_MAX) - { + if (olen + sizeof(opt) + sizeof(slen) + so->len > + UINT16_MAX) { logerrx("%s: option too big", __func__); break; } @@ -447,8 +425,7 @@ dhcp6_findoption(void *data, size_t data_len, uint16_t code, uint16_t *len) } static void * -dhcp6_findmoption(void *data, size_t data_len, uint16_t code, - uint16_t *len) +dhcp6_findmoption(void *data, size_t data_len, uint16_t code, uint16_t *len) { uint8_t *d; @@ -463,9 +440,8 @@ dhcp6_findmoption(void *data, size_t data_len, uint16_t code, } static const uint8_t * -dhcp6_getoption(struct dhcpcd_ctx *ctx, - size_t *os, unsigned int *code, size_t *len, - const uint8_t *od, size_t ol, struct dhcp_opt **oopt) +dhcp6_getoption(struct dhcpcd_ctx *ctx, size_t *os, unsigned int *code, + size_t *len, const uint8_t *od, size_t ol, struct dhcp_opt **oopt) { struct dhcp6_option o; size_t i; @@ -487,9 +463,8 @@ dhcp6_getoption(struct dhcpcd_ctx *ctx, } *oopt = NULL; - for (i = 0, opt = ctx->dhcp6_opts; - i < ctx->dhcp6_opts_len; i++, opt++) - { + for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; + i++, opt++) { if (opt->option == *code) { *oopt = opt; break; @@ -536,8 +511,7 @@ dhcp6_updateelapsed(struct interface *ifp, struct dhcp6_message *m, size_t len) if (secs >= (UINT16_MAX / CSEC_PER_SEC) + 1) hsec = UINT16_MAX; else { - hsec = (secs * CSEC_PER_SEC) + - (nsecs / NSEC_PER_CSEC); + hsec = (secs * CSEC_PER_SEC) + (nsecs / NSEC_PER_CSEC); if (hsec > UINT16_MAX) hsec = UINT16_MAX; } @@ -560,7 +534,7 @@ dhcp6_newxid(const struct interface *ifp, struct dhcp6_message *m) memcpy(&xid, (ifp->hwaddr + ifp->hwlen) - sizeof(xid), sizeof(xid)); else { -again: + again: xid = arc4random(); } @@ -574,8 +548,7 @@ dhcp6_newxid(const struct interface *ifp, struct dhcp6_message *m) continue; if ((state1 = D6_CSTATE(ifp1)) == NULL) continue; - if (state1->send != NULL && - state1->send->xid[0] == m->xid[0] && + if (state1->send != NULL && state1->send->xid[0] == m->xid[0] && state1->send->xid[1] == m->xid[1] && state1->send->xid[2] == m->xid[2]) break; @@ -583,11 +556,10 @@ dhcp6_newxid(const struct interface *ifp, struct dhcp6_message *m) if (ifp1 != NULL) { if (ifp->options->options & DHCPCD_XID_HWADDR && - ifp->hwlen >= sizeof(xid)) - { - logerrx("%s: duplicate xid on %s", - ifp->name, ifp1->name); - return; + ifp->hwlen >= sizeof(xid)) { + logerrx("%s: duplicate xid on %s", ifp->name, + ifp1->name); + return; } goto again; } @@ -679,11 +651,11 @@ dhcp6_delegateaddr(struct in6_addr *addr, struct interface *ifp, if (asla.prefix_len <= 64) asla.prefix_len = 64; else - asla.prefix_len = - (uint8_t)ROUNDUP4(asla.prefix_len); + asla.prefix_len = (uint8_t)ROUNDUP4( + asla.prefix_len); } -#define BIT(n) (1UL << (n)) +#define BIT(n) (1UL << (n)) #define BIT_MASK(len) (BIT(len) - 1) if (ia->sla_max == 0) { /* Work out the real sla_max from our bits used */ @@ -695,24 +667,20 @@ dhcp6_delegateaddr(struct in6_addr *addr, struct interface *ifp, } } - if (ipv6_userprefix(&prefix->prefix, prefix->prefix_len, - sla->sla, addr, sla->prefix_len) == -1) - { - sa = inet_ntop(AF_INET6, &prefix->prefix, - sabuf, sizeof(sabuf)); - logerr("%s: invalid prefix %s/%d + %d/%d", - ifp->name, sa, prefix->prefix_len, - sla->sla, sla->prefix_len); + if (ipv6_userprefix(&prefix->prefix, prefix->prefix_len, sla->sla, addr, + sla->prefix_len) == -1) { + sa = inet_ntop(AF_INET6, &prefix->prefix, sabuf, sizeof(sabuf)); + logerr("%s: invalid prefix %s/%d + %d/%d", ifp->name, sa, + prefix->prefix_len, sla->sla, sla->prefix_len); return -1; } if (prefix->prefix_exclude_len && - IN6_ARE_ADDR_EQUAL(addr, &prefix->prefix_exclude)) - { - sa = inet_ntop(AF_INET6, &prefix->prefix_exclude, - sabuf, sizeof(sabuf)); - logerrx("%s: cannot delegate excluded prefix %s/%d", - ifp->name, sa, prefix->prefix_exclude_len); + IN6_ARE_ADDR_EQUAL(addr, &prefix->prefix_exclude)) { + sa = inet_ntop(AF_INET6, &prefix->prefix_exclude, sabuf, + sizeof(sabuf)); + logerrx("%s: cannot delegate excluded prefix %s/%d", ifp->name, + sa, prefix->prefix_exclude_len); return -1; } @@ -752,7 +720,7 @@ dhcp6_makemessage(struct interface *ifp) state->send = NULL; } - switch(state->state) { + switch (state->state) { case DH6S_INIT: /* FALLTHROUGH */ case DH6S_DISCOVER: type = DHCP6_SOLICIT; @@ -785,7 +753,7 @@ dhcp6_makemessage(struct interface *ifp) /* RFC 4704 Section 5 says we can only send FQDN for these * message types. */ - switch(type) { + switch (type) { case DHCP6_SOLICIT: case DHCP6_REQUEST: case DHCP6_RENEW: @@ -815,13 +783,9 @@ dhcp6_makemessage(struct interface *ifp) hl = 0; /* Appease gcc */ if (state->state != DH6S_RELEASE && state->state != DH6S_DECLINE) { for (l = 0, opt = ifp->ctx->dhcp6_opts; - l < ifp->ctx->dhcp6_opts_len; - l++, opt++) - { + l < ifp->ctx->dhcp6_opts_len; l++, opt++) { for (n = 0, opt2 = ifo->dhcp6_override; - n < ifo->dhcp6_override_len; - n++, opt2++) - { + n < ifo->dhcp6_override_len; n++, opt2++) { if (opt->option == opt2->option) break; } @@ -834,9 +798,7 @@ dhcp6_makemessage(struct interface *ifp) } #ifndef SMALL for (l = 0, opt = ifo->dhcp6_override; - l < ifo->dhcp6_override_len; - l++, opt++) - { + l < ifo->dhcp6_override_len; l++, opt++) { if (!DHC_REQOPT(opt, ifo->requestmask6, ifo->nomask6)) continue; n_options++; @@ -861,9 +823,9 @@ dhcp6_makemessage(struct interface *ifp) #ifdef AUTH if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != - DHCPCD_AUTH_SENDREQUIRE && + DHCPCD_AUTH_SENDREQUIRE && DHC_REQ(ifo->requestmask6, ifo->nomask6, - D6_OPTION_RECONF_ACCEPT)) + D6_OPTION_RECONF_ACCEPT)) len += sizeof(o); /* Reconfigure Accept */ #endif } @@ -891,7 +853,7 @@ dhcp6_makemessage(struct interface *ifp) /* IA */ m = NULL; ml = 0; - switch(state->state) { + switch (state->state) { case DH6S_REQUEST: m = state->recv; ml = state->recv_len; @@ -924,7 +886,7 @@ dhcp6_makemessage(struct interface *ifp) continue; if (!(ap->flags & IPV6_AF_REQUEST) && (ap->prefix_vltime == 0 || - state->state == DH6S_DISCOVER)) + state->state == DH6S_DISCOVER)) continue; if (DECLINE_IA(ap) && state->state != DH6S_DECLINE) continue; @@ -934,7 +896,9 @@ dhcp6_makemessage(struct interface *ifp) if (ap->prefix_exclude_len) len += sizeof(o) + 1 + (uint8_t)((ap->prefix_exclude_len - - ap->prefix_len - 1) / NBBY) + 1; + ap->prefix_len - 1) / + NBBY) + + 1; #endif } else len += sizeof(o) + sizeof(struct dhcp6_ia_addr); @@ -963,7 +927,7 @@ dhcp6_makemessage(struct interface *ifp) ml = state->new_len; } - switch(state->state) { + switch (state->state) { case DH6S_REQUEST: /* FALLTHROUGH */ case DH6S_RENEW: /* FALLTHROUGH */ case DH6S_RELEASE: @@ -1020,27 +984,28 @@ dhcp6_makemessage(struct interface *ifp) dhcp6_newxid(ifp, state->send); -#define COPYIN1(_code, _len) { \ - o.code = htons((_code)); \ - o.len = htons((_len)); \ - memcpy(p, &o, sizeof(o)); \ - p += sizeof(o); \ -} -#define COPYIN(_code, _data, _len) do { \ - COPYIN1((_code), (_len)); \ - if ((_len) != 0) { \ - memcpy(p, (_data), (_len)); \ - p += (_len); \ - } \ -} while (0 /* CONSTCOND */) +#define COPYIN1(_code, _len) \ + { \ + o.code = htons((_code)); \ + o.len = htons((_len)); \ + memcpy(p, &o, sizeof(o)); \ + p += sizeof(o); \ + } +#define COPYIN(_code, _data, _len) \ + do { \ + COPYIN1((_code), (_len)); \ + if ((_len) != 0) { \ + memcpy(p, (_data), (_len)); \ + p += (_len); \ + } \ + } while (0 /* CONSTCOND */) /* Options are listed in numerical order as per RFC 7844 Section 4.1 * XXX: They should be randomised. */ p = (uint8_t *)state->send + sizeof(*state->send); if (ifo->options & DHCPCD_ANONYMOUS) - COPYIN(D6_OPTION_CLIENTID, duid, - (uint16_t)duid_len); + COPYIN(D6_OPTION_CLIENTID, duid, (uint16_t)duid_len); else COPYIN(D6_OPTION_CLIENTID, ifp->ctx->duid, (uint16_t)ifp->ctx->duid_len); @@ -1070,7 +1035,7 @@ dhcp6_makemessage(struct interface *ifp) continue; if (!(ap->flags & IPV6_AF_REQUEST) && (ap->prefix_vltime == 0 || - state->state == DH6S_DISCOVER)) + state->state == DH6S_DISCOVER)) continue; if (DECLINE_IA(ap) && state->state != DH6S_DECLINE) continue; @@ -1083,13 +1048,14 @@ dhcp6_makemessage(struct interface *ifp) uint8_t pdp[DHCP6_PD_ADDR_SIZE]; memset(pdp, 0, DHCP6_PD_ADDR_PLEN); - pdp[DHCP6_PD_ADDR_PLEN] = (uint8_t)ap->prefix_len; + pdp[DHCP6_PD_ADDR_PLEN] = (uint8_t) + ap->prefix_len; memcpy(pdp + DHCP6_PD_ADDR_PREFIX, &ap->prefix, DHCP6_PD_ADDR_SIZE - DHCP6_PD_ADDR_PREFIX); COPYIN(D6_OPTION_IAPREFIX, pdp, sizeof(pdp)); - ia_na_len = (uint16_t) - (ia_na_len + sizeof(o) + sizeof(pdp)); + ia_na_len = (uint16_t)(ia_na_len + sizeof(o) + + sizeof(pdp)); /* RFC6603 Section 4.2 */ if (ap->prefix_exclude_len) { @@ -1097,12 +1063,14 @@ dhcp6_makemessage(struct interface *ifp) const uint8_t *pp; n = (size_t)((ap->prefix_exclude_len - - ap->prefix_len - 1) / NBBY) + 1; + ap->prefix_len - 1) / + NBBY) + + 1; ep = exb; *ep++ = (uint8_t)ap->prefix_exclude_len; pp = ap->prefix_exclude.s6_addr; - pp += (size_t) - ((ap->prefix_len - 1) / NBBY) + + pp += (size_t)((ap->prefix_len - 1) / + NBBY) + (n - 1); u8 = ap->prefix_len % NBBY; if (u8) @@ -1116,24 +1084,24 @@ dhcp6_makemessage(struct interface *ifp) } COPYIN(D6_OPTION_PD_EXCLUDE, exb, (uint16_t)n); - ia_na_len = (uint16_t) - (ia_na_len + sizeof(o) + n); + ia_na_len = (uint16_t)(ia_na_len + + sizeof(o) + n); } #endif } else { struct dhcp6_ia_addr ia = { - .addr = ap->addr, - /* - * RFC 8415 21.6 states that the - * valid and preferred lifetimes sent by - * the client SHOULD be zero and MUST - * be ignored by the server. - */ + .addr = ap->addr, + /* + * RFC 8415 21.6 states that the + * valid and preferred lifetimes sent by + * the client SHOULD be zero and MUST + * be ignored by the server. + */ }; COPYIN(D6_OPTION_IA_ADDR, &ia, sizeof(ia)); - ia_na_len = (uint16_t) - (ia_na_len + sizeof(o) + sizeof(ia)); + ia_na_len = (uint16_t)(ia_na_len + sizeof(o) + + sizeof(ia)); } } @@ -1143,26 +1111,20 @@ dhcp6_makemessage(struct interface *ifp) } if (state->send->type != DHCP6_RELEASE && - state->send->type != DHCP6_DECLINE && - n_options) - { + state->send->type != DHCP6_DECLINE && n_options) { o_lenp = NEXTLEN(p); o.len = 0; COPYIN1(D6_OPTION_ORO, 0); for (l = 0, opt = ifp->ctx->dhcp6_opts; - l < ifp->ctx->dhcp6_opts_len; - l++, opt++) - { + l < ifp->ctx->dhcp6_opts_len; l++, opt++) { #ifndef SMALL for (n = 0, opt2 = ifo->dhcp6_override; - n < ifo->dhcp6_override_len; - n++, opt2++) - { + n < ifo->dhcp6_override_len; n++, opt2++) { if (opt->option == opt2->option) break; } if (n < ifo->dhcp6_override_len) - continue; + continue; #endif if (!DHC_REQOPT(opt, ifo->requestmask6, ifo->nomask6)) continue; @@ -1173,9 +1135,7 @@ dhcp6_makemessage(struct interface *ifp) } #ifndef SMALL for (l = 0, opt = ifo->dhcp6_override; - l < ifo->dhcp6_override_len; - l++, opt++) - { + l < ifo->dhcp6_override_len; l++, opt++) { if (!DHC_REQOPT(opt, ifo->requestmask6, ifo->nomask6)) continue; o.code = htons((uint16_t)opt->option); @@ -1213,8 +1173,7 @@ dhcp6_makemessage(struct interface *ifp) #endif if (state->send->type != DHCP6_RELEASE && - state->send->type != DHCP6_DECLINE) - { + state->send->type != DHCP6_DECLINE) { if (fqdn != FQDN_DISABLE) { o_lenp = NEXTLEN(p); COPYIN1(D6_OPTION_FQDN, 0); @@ -1242,17 +1201,16 @@ dhcp6_makemessage(struct interface *ifp) if (!has_option_mask(ifo->nomask6, D6_OPTION_MUDURL) && ifo->mudurl[0]) - COPYIN(D6_OPTION_MUDURL, - ifo->mudurl + 1, ifo->mudurl[0]); + COPYIN(D6_OPTION_MUDURL, ifo->mudurl + 1, + ifo->mudurl[0]); #ifdef AUTH if ((ifo->auth.options & DHCPCD_AUTH_SENDREQUIRE) != - DHCPCD_AUTH_SENDREQUIRE && + DHCPCD_AUTH_SENDREQUIRE && DHC_REQ(ifo->requestmask6, ifo->nomask6, - D6_OPTION_RECONF_ACCEPT)) + D6_OPTION_RECONF_ACCEPT)) COPYIN1(D6_OPTION_RECONF_ACCEPT, 0); #endif - } #ifdef AUTH @@ -1292,7 +1250,8 @@ dhcp6_freedrop_addrs(struct interface *ifp, int drop, unsigned int notflags, } #ifndef SMALL -static void dhcp6_delete_delegates(struct interface *ifp) +static void +dhcp6_delete_delegates(struct interface *ifp) { struct interface *ifp0; @@ -1333,30 +1292,38 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) unsigned int RT; bool multicast = true; struct sockaddr_in6 dst = { - .sin6_family = AF_INET6, - /* Setting the port on Linux gives EINVAL when sending. - * This looks like a kernel bug as the equivalent works - * fine with the DHCP counterpart. */ + .sin6_family = AF_INET6, + /* Setting the port on Linux gives EINVAL when sending. + * This looks like a kernel bug as the equivalent works + * fine with the DHCP counterpart. */ #ifndef __linux__ - .sin6_port = htons(DHCP6_SERVER_PORT), + .sin6_port = htons(DHCP6_SERVER_PORT), #endif }; struct udphdr udp = { - .uh_sport = htons(DHCP6_CLIENT_PORT), - .uh_dport = htons(DHCP6_SERVER_PORT), - .uh_ulen = htons((uint16_t)(sizeof(udp) + state->send_len)), + .uh_sport = htons(DHCP6_CLIENT_PORT), + .uh_dport = htons(DHCP6_SERVER_PORT), + .uh_ulen = htons((uint16_t)(sizeof(udp) + state->send_len)), }; struct iovec iov[] = { - { .iov_base = &udp, .iov_len = sizeof(udp), }, - { .iov_base = state->send, .iov_len = state->send_len, }, + { + .iov_base = &udp, + .iov_len = sizeof(udp), + }, + { + .iov_base = state->send, + .iov_len = state->send_len, + }, }; union { struct cmsghdr hdr; uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; } cmsgbuf = { .buf = { 0 } }; struct msghdr msg = { - .msg_name = &dst, .msg_namelen = sizeof(dst), - .msg_iov = iov, .msg_iovlen = __arraycount(iov), + .msg_name = &dst, + .msg_namelen = sizeof(dst), + .msg_iov = iov, + .msg_iovlen = __arraycount(iov), }; char uaddr[INET6_ADDRSTRLEN]; @@ -1365,8 +1332,8 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) if (!IN6_IS_ADDR_UNSPECIFIED(&state->unicast)) { switch (state->send->type) { - case DHCP6_SOLICIT: /* FALLTHROUGH */ - case DHCP6_CONFIRM: /* FALLTHROUGH */ + case DHCP6_SOLICIT: /* FALLTHROUGH */ + case DHCP6_CONFIRM: /* FALLTHROUGH */ case DHCP6_REBIND: /* Unicasting is denied for these types. */ break; @@ -1380,15 +1347,11 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) dst.sin6_addr = multicast ? alldhcp : state->unicast; if (!callback) { - logdebugx("%s: %s %s with xid 0x%02x%02x%02x%s%s", - ifp->name, + logdebugx("%s: %s %s with xid 0x%02x%02x%02x%s%s", ifp->name, multicast ? "multicasting" : "unicasting", - dhcp6_get_op(state->send->type), - state->send->xid[0], - state->send->xid[1], - state->send->xid[2], - !multicast ? " " : "", - !multicast ? uaddr : ""); + dhcp6_get_op(state->send->type), state->send->xid[0], + state->send->xid[1], state->send->xid[2], + !multicast ? " " : "", !multicast ? uaddr : ""); RT = 0; } else { if (state->IMD && @@ -1413,23 +1376,23 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) } /* Add -.1 to .1 * RT randomness as per RFC8415 section 15 */ - uint32_t lru = arc4random_uniform( - state->RTC == 0 ? DHCP6_RAND_MAX - : DHCP6_RAND_MAX - DHCP6_RAND_MIN); + uint32_t lru = arc4random_uniform(state->RTC == 0 ? + DHCP6_RAND_MAX : + DHCP6_RAND_MAX - DHCP6_RAND_MIN); int lr = (int)lru - (state->RTC == 0 ? 0 : DHCP6_RAND_MAX); - RT = state->RT - + (unsigned int)((float)state->RT - * ((float)lr / DHCP6_RAND_DIV)); + RT = state->RT + + (unsigned int)((float)state->RT * + ((float)lr / DHCP6_RAND_DIV)); if (if_is_link_up(ifp)) logdebugx("%s: %s %s (xid 0x%02x%02x%02x)%s%s," - " next in %0.1f seconds", + " next in %0.1f seconds", ifp->name, state->IMD != 0 ? "delaying" : - multicast ? "multicasting" : "unicasting", + multicast ? "multicasting" : + "unicasting", dhcp6_get_op(state->send->type), - state->send->xid[0], - state->send->xid[1], + state->send->xid[0], state->send->xid[1], state->send->xid[2], state->IMD == 0 && !multicast ? " " : "", state->IMD == 0 && !multicast ? uaddr : "", @@ -1450,8 +1413,7 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) dhcp6_updateelapsed(ifp, state->send, state->send_len); #ifdef AUTH if (ifp->options->auth.options & DHCPCD_AUTH_SEND && - dhcp6_update_auth(ifp, state->send, state->send_len) == -1) - { + dhcp6_update_auth(ifp, state->send, state->send_len) == -1) { logerr("%s: %s: dhcp6_updateauth", __func__, ifp->name); if (errno != ESRCH) return -1; @@ -1500,14 +1462,13 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) if (state->RT < RT) /* Check overflow */ state->RT = RT; if (state->MRC == 0 || state->RTC <= state->MRC) - eloop_timeout_add_msec(ctx->eloop, - RT, callback, ifp); + eloop_timeout_add_msec(ctx->eloop, RT, callback, ifp); else if (state->MRC != 0 && state->MRCcallback) - eloop_timeout_add_msec(ctx->eloop, - RT, state->MRCcallback, ifp); + eloop_timeout_add_msec(ctx->eloop, RT, + state->MRCcallback, ifp); else - logwarnx("%s: sent %d times with no reply", - ifp->name, state->RTC); + logwarnx("%s: sent %d times with no reply", ifp->name, + state->RTC); } return 0; } @@ -1515,14 +1476,12 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) static void dhcp6_sendinform(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendinform); } static void dhcp6_senddiscover2(void *arg) { - dhcp6_sendmessage(arg, dhcp6_senddiscover2); } @@ -1556,42 +1515,36 @@ dhcp6_senddiscover(void *arg) static void dhcp6_sendrequest(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendrequest); } static void dhcp6_sendrebind(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendrebind); } static void dhcp6_sendrenew(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendrenew); } static void dhcp6_sendconfirm(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendconfirm); } static void dhcp6_senddecline(void *arg) { - dhcp6_sendmessage(arg, dhcp6_senddecline); } static void dhcp6_sendrelease(void *arg) { - dhcp6_sendmessage(arg, dhcp6_sendrelease); } @@ -1606,8 +1559,7 @@ dhcp6_startrenew(void *arg) return; /* Only renew in the bound or renew states */ - if (state->state != DH6S_BOUND && - state->state != DH6S_RENEW) + if (state->state != DH6S_BOUND && state->state != DH6S_RENEW) return; /* Remove the timeout as the renew may have been forced. */ @@ -1626,9 +1578,9 @@ dhcp6_startrenew(void *arg) dhcp6_sendrenew(ifp); } -void dhcp6_renew(struct interface *ifp) +void +dhcp6_renew(struct interface *ifp) { - dhcp6_startrenew(ifp); } @@ -1678,8 +1630,7 @@ dhcp6_dadcallback(void *arg) oneduplicated = false; TAILQ_FOREACH(ia2, &state->addrs, next) { if (ia2->flags & IPV6_AF_ADDED && - !(ia2->flags & IPV6_AF_DADCOMPLETED)) - { + !(ia2->flags & IPV6_AF_DADCOMPLETED)) { completed = false; break; } @@ -1700,7 +1651,7 @@ dhcp6_dadcallback(void *arg) #ifndef SMALL ia->delegating_prefix ? "DELEGATED6" : #endif - state->reason); + state->reason); if (valid) dhcpcd_daemonise(ifp->ctx); } @@ -1718,16 +1669,16 @@ dhcp6_addrequestedaddrs(struct interface *ifp) for (i = 0; i < ifp->options->ia_len; i++) { ia = &ifp->options->ia[i]; if (!((ia->ia_type == D6_OPTION_IA_PD && ia->prefix_len) || - !IN6_IS_ADDR_UNSPECIFIED(&ia->addr))) + !IN6_IS_ADDR_UNSPECIFIED(&ia->addr))) continue; a = ipv6_newaddr(ifp, &ia->addr, - /* - * RFC 5942 Section 5 - * We cannot assume any prefix length, nor tie the - * address to an existing one as it could expire - * before the address. - * As such we just give it a 128 prefix. - */ + /* + * RFC 5942 Section 5 + * We cannot assume any prefix length, nor tie the + * address to an existing one as it could expire + * before the address. + * As such we just give it a 128 prefix. + */ ia->ia_type == D6_OPTION_IA_PD ? ia->prefix_len : 128, IPV6_AF_REQUEST); if (a == NULL) @@ -1756,7 +1707,7 @@ dhcp6_startdiscover(void *arg) dhcp6_delete_delegates(ifp); #endif /* Ensure we never request INFO_REFRESH_TIME, - * this only belongs in Information-Request messages */ + * this only belongs in Information-Request messages */ del_option_mask(ifo->requestmask6, D6_OPTION_INFO_REFRESH_TIME); if (state->new == NULL && !state->failed) @@ -1819,8 +1770,8 @@ dhcp6_startinform(void *arg) * This poor wording might explain the lack of similar text for INFORM * in 18.1.5 because there are no addresses in the INFORM message. */ if (!state->failed) - eloop_timeout_add_sec(ifp->ctx->eloop, - INF_MAX_RD, dhcp6_failinform, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, INF_MAX_RD, + dhcp6_failinform, ifp); } static bool @@ -1860,8 +1811,9 @@ dhcp6_fail(struct interface *ifp, bool drop) dhcp_unlink(ifp->ctx, state->leasefile); dhcp6_addrequestedaddrs(ifp); eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); - } else if ((state->state == DH6S_CONFIRM || state->state == DH6S_REBIND) && - ifp->options->options & DHCPCD_LASTLEASE) { + } else if ((state->state == DH6S_CONFIRM || + state->state == DH6S_REBIND) && + ifp->options->options & DHCPCD_LASTLEASE) { dhcp6_bind(ifp, NULL, NULL); state->state = DH6S_REBIND; dhcp6_startrebind(ifp); @@ -1872,7 +1824,8 @@ dhcp6_fail(struct interface *ifp, bool drop) } if (!dhcp6_startdiscoinform(ifp)) { - logwarnx("%s: no advertising IPv6 router wants DHCP",ifp->name); + logwarnx("%s: no advertising IPv6 router wants DHCP", + ifp->name); state->state = DH6S_INIT; } } @@ -1895,7 +1848,8 @@ dhcp6_failconfirm(void *arg) ifp->name); eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendconfirm, ifp); - /* RFC8415 18.2.3 says that prior addresses SHOULD be used on failure. */ + /* RFC8415 18.2.3 says that prior addresses SHOULD be used on failure. + */ dhcp6_fail(ifp, false); } @@ -1930,7 +1884,8 @@ dhcp6_failrebindpd(void *arg) eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendrebind, ifp); /* RFC8415 18.2.3 says that prior addresses SHOULD be used on failure. - * 18.2 says REBIND rather than CONFIRM with PD but use CONFIRM timings. */ + * 18.2 says REBIND rather than CONFIRM with PD but use CONFIRM timings. + */ dhcp6_fail(ifp, false); } @@ -1980,8 +1935,8 @@ dhcp6_startrebind(void *arg) state->IMD = CNF_MAX_DELAY; state->IRT = CNF_TIMEOUT; state->MRT = CNF_MAX_RT; - eloop_timeout_add_sec(ifp->ctx->eloop, - CNF_MAX_RD, dhcp6_failrebindpd, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, CNF_MAX_RD, + dhcp6_failrebindpd, ifp); } #endif } @@ -2046,8 +2001,8 @@ dhcp6_startconfirm(struct interface *ifp) return; } dhcp6_sendconfirm(ifp); - eloop_timeout_add_sec(ifp->ctx->eloop, - CNF_MAX_RD, dhcp6_failconfirm, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, CNF_MAX_RD, dhcp6_failconfirm, + ifp); } static void @@ -2133,14 +2088,14 @@ dhcp6_startrelease(struct interface *ifp) } static int -dhcp6_checkstatusok(const struct interface *ifp, - struct dhcp6_message *m, uint8_t *p, size_t len) +dhcp6_checkstatusok(const struct interface *ifp, struct dhcp6_message *m, + uint8_t *p, size_t len) { struct dhcp6_state *state; uint8_t *opt; uint16_t opt_len, code; size_t mlen; - void * (*f)(void *, size_t, uint16_t, uint16_t *), *farg; + void *(*f)(void *, size_t, uint16_t, uint16_t *), *farg; char buf[32], *sbuf; const char *status; int loglevel; @@ -2152,7 +2107,7 @@ dhcp6_checkstatusok(const struct interface *ifp, else farg = m; if ((opt = f(farg, len, D6_OPTION_STATUS_CODE, &opt_len)) == NULL) { - //logdebugx("%s: no status", ifp->name); + // logdebugx("%s: no status", ifp->name); state->lerror = 0; errno = ESRCH; return 0; @@ -2277,8 +2232,8 @@ dhcp6_findna(struct interface *ifp, uint16_t ot, const uint8_t *iaid, /* RFC 3315 22.6 */ if (ia.pltime > ia.vltime) { errno = EINVAL; - logerr("%s: IA Address pltime %"PRIu32 - " > vltime %"PRIu32, + logerr("%s: IA Address pltime %" PRIu32 + " > vltime %" PRIu32, ifp->name, ia.pltime, ia.vltime); continue; } @@ -2313,9 +2268,9 @@ dhcp6_findna(struct interface *ifp, uint16_t ot, const uint8_t *iaid, a->prefix_vltime = ia.vltime; } if (a->prefix_pltime && a->prefix_pltime < state->lowpl) - state->lowpl = a->prefix_pltime; + state->lowpl = a->prefix_pltime; if (a->prefix_vltime && a->prefix_vltime > state->expire) - state->expire = a->prefix_vltime; + state->expire = a->prefix_vltime; i++; } return i; @@ -2323,8 +2278,8 @@ dhcp6_findna(struct interface *ifp, uint16_t ot, const uint8_t *iaid, #ifndef SMALL static int -dhcp6_findpd(struct interface *ifp, const uint8_t *iaid, - uint8_t *d, size_t l, const struct timespec *acquired) +dhcp6_findpd(struct interface *ifp, const uint8_t *iaid, uint8_t *d, size_t l, + const struct timespec *acquired) { struct dhcp6_state *state; uint8_t *o, *nd; @@ -2361,8 +2316,8 @@ dhcp6_findpd(struct interface *ifp, const uint8_t *iaid, /* RFC 3315 22.6 */ if (pdp_pltime > pdp_vltime) { errno = EINVAL; - logerrx("%s: IA Prefix pltime %"PRIu32 - " > vltime %"PRIu32, + logerrx("%s: IA Prefix pltime %" PRIu32 + " > vltime %" PRIu32, ifp->name, pdp_pltime, pdp_vltime); continue; } @@ -2499,7 +2454,7 @@ dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, l -= sizeof(o) + o.len; o.code = ntohs(o.code); - switch(o.code) { + switch (o.code) { case D6_OPTION_IA_TA: nl = 4; break; @@ -2527,12 +2482,11 @@ dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, break; } if (j == ifo->ia_len && - !(ifo->ia_len == 0 && ifp->ctx->options & DHCPCD_DUMPLEASE)) - { - logdebugx("%s: ignoring unrequested IAID %s", - ifp->name, - hwaddr_ntoa(ia.iaid, sizeof(ia.iaid), - buf, sizeof(buf))); + !(ifo->ia_len == 0 && + ifp->ctx->options & DHCPCD_DUMPLEASE)) { + logdebugx("%s: ignoring unrequested IAID %s", ifp->name, + hwaddr_ntoa(ia.iaid, sizeof(ia.iaid), buf, + sizeof(buf))); continue; } @@ -2544,7 +2498,7 @@ dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, logwarnx("%s: IAID %s T1(%d) > T2(%d) from %s", ifp->name, hwaddr_ntoa(iaid, sizeof(iaid), buf, - sizeof(buf)), + sizeof(buf)), ia.t1, ia.t2, sfrom); continue; } @@ -2558,9 +2512,8 @@ dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, } if (o.code == D6_OPTION_IA_PD) { #ifndef SMALL - if (dhcp6_findpd(ifp, ia.iaid, p, o.len, - acquired) == 0) - { + if (dhcp6_findpd(ifp, ia.iaid, p, o.len, acquired) == + 0) { logwarnx("%s: %s: DHCPv6 REPLY missing Prefix", ifp->name, sfrom); continue; @@ -2568,10 +2521,9 @@ dhcp6_findia(struct interface *ifp, struct dhcp6_message *m, size_t l, #endif } else { if (dhcp6_findna(ifp, o.code, ia.iaid, p, o.len, - acquired) == 0) - { + acquired) == 0) { logwarnx("%s: %s: DHCPv6 REPLY missing " - "IA Address", + "IA Address", ifp->name, sfrom); continue; } @@ -2649,8 +2601,8 @@ dhcp6_deprecateaddrs(struct ipv6_addrhead *addrs) * try and prefer other addresses. */ ia->prefix_pltime = 0; } else if (ia->prefix_vltime == 0) - loginfox("%s: %s: no valid lifetime", - ia->iface->name, ia->saddr); + loginfox("%s: %s: no valid lifetime", ia->iface->name, + ia->saddr); else continue; @@ -2675,8 +2627,7 @@ dhcp6_deprecateaddrs(struct ipv6_addrhead *addrs) } static int -dhcp6_validatelease(struct interface *ifp, - struct dhcp6_message *m, size_t len, +dhcp6_validatelease(struct interface *ifp, struct dhcp6_message *m, size_t len, const char *sfrom, const struct timespec *acquired) { struct dhcp6_state *state; @@ -2703,11 +2654,10 @@ dhcp6_validatelease(struct interface *ifp, state->has_no_binding = false; nia = dhcp6_findia(ifp, m, len, sfrom, acquired); if (nia == 0 && state->state == DH6S_CONFIRM && ok_errno == 0 && - state->new && state->new_len) - { + state->new && state->new_len) { state->has_no_binding = false; - nia = dhcp6_findia(ifp, state->new, state->new_len, - sfrom, acquired); + nia = dhcp6_findia(ifp, state->new, state->new_len, sfrom, + acquired); } if (nia == 0) { logerrx("%s: no useable IA found in lease", ifp->name); @@ -2737,10 +2687,9 @@ dhcp6_readlease(struct interface *ifp, int validate) logdebugx("reading standard input"); bytes = read(fileno(stdin), buf.buf, sizeof(buf.buf)); } else { - logdebugx("%s: reading lease: %s", - ifp->name, state->leasefile); - bytes = dhcp_readfile(ifp->ctx, state->leasefile, - buf.buf, sizeof(buf.buf)); + logdebugx("%s: reading lease: %s", ifp->name, state->leasefile); + bytes = dhcp_readfile(ifp->ctx, state->leasefile, buf.buf, + sizeof(buf.buf)); } if (bytes == -1) goto ex; @@ -2772,8 +2721,7 @@ dhcp6_readlease(struct interface *ifp, int validate) } if (state->expire != ND6_INFINITE_LIFETIME && - (time_t)state->expire < now - mtime) - { + (time_t)state->expire < now - mtime) { logdebugx("%s: discarding expired lease", ifp->name); bytes = 0; goto ex; @@ -2785,20 +2733,19 @@ dhcp6_readlease(struct interface *ifp, int validate) o = dhcp6_findmoption(&buf.dhcp6, (size_t)bytes, D6_OPTION_AUTH, &ol); if (o) { if (dhcp_auth_validate(&state->auth, &ifp->options->auth, - buf.buf, (size_t)bytes, 6, buf.dhcp6.type, o, ol) == NULL) - { + buf.buf, (size_t)bytes, 6, buf.dhcp6.type, o, + ol) == NULL) { logerr("%s: authentication failed", ifp->name); bytes = 0; goto ex; } if (state->auth.token) - logdebugx("%s: validated using 0x%08" PRIu32, - ifp->name, state->auth.token->secretid); + logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, + state->auth.token->secretid); else loginfox("%s: accepted reconfigure key", ifp->name); } else if ((ifp->options->auth.options & DHCPCD_AUTH_SENDREQUIRE) == - DHCPCD_AUTH_SENDREQUIRE) - { + DHCPCD_AUTH_SENDREQUIRE) { logerrx("%s: authentication now required", ifp->name); goto ex; } @@ -2852,17 +2799,13 @@ dhcp6_startinit(struct interface *ifp) } } - if (!(ifp->ctx->options & DHCPCD_TEST) && - !(has_ta && !has_non_ta) && - ifo->reboot != 0) - { + if (!(ifp->ctx->options & DHCPCD_TEST) && !(has_ta && !has_non_ta) && + ifo->reboot != 0) { r = dhcp6_readlease(ifp, 1); if (r == -1) { if (errno != ENOENT && errno != ESRCH) logerr("%s: %s", __func__, state->leasefile); - } else if (r != 0 && - !(ifo->options & DHCPCD_ANONYMOUS)) - { + } else if (r != 0 && !(ifo->options & DHCPCD_ANONYMOUS)) { /* RFC 3633 section 12.1 */ #ifndef SMALL if (state->state == DH6S_MANUALREBIND || @@ -2894,14 +2837,14 @@ dhcp6_ifdelegateaddr(struct interface *ifp, struct ipv6_addr *prefix, /* Don't spam the log automatically */ if (sla != NULL) logwarnx("%s: DHCPv6 server does not support " - "OPTION_PD_EXCLUDE", + "OPTION_PD_EXCLUDE", ifp->name); return NULL; } pfxlen = prefix->prefix_exclude_len; memcpy(&addr, &prefix->prefix_exclude, sizeof(addr)); - } else if ((pfxlen = dhcp6_delegateaddr(&addr, ifp, prefix, - sla, if_ia)) == -1) + } else if ((pfxlen = dhcp6_delegateaddr(&addr, ifp, prefix, sla, + if_ia)) == -1) return NULL; if (sla != NULL && fls64(sla->suffix) > 128 - pfxlen) { @@ -2976,15 +2919,14 @@ dhcp6_script_try_run(struct interface *ifp, int delegated) if (ap->flags & IPV6_AF_ONLINK) { if (!(ap->flags & IPV6_AF_DADCOMPLETED) && ipv6_iffindaddr(ap->iface, &ap->addr, - IN6_IFF_TENTATIVE)) + IN6_IFF_TENTATIVE)) ap->flags |= IPV6_AF_DADCOMPLETED; if ((ap->flags & IPV6_AF_DADCOMPLETED) == 0 #ifndef SMALL && ((delegated && ap->delegating_prefix) || - (!delegated && !ap->delegating_prefix)) + (!delegated && !ap->delegating_prefix)) #endif - ) - { + ) { completed = 0; break; } @@ -3002,7 +2944,6 @@ dhcp6_script_try_run(struct interface *ifp, int delegated) size_t dhcp6_find_delegates(__unused struct interface *ifp) { - return 0; } #else @@ -3055,7 +2996,7 @@ dhcp6_delegate_prefix(struct interface *ifp) if (ia->ia_type != D6_OPTION_IA_PD) continue; if (memcmp(ia->iaid, ap->iaid, - sizeof(ia->iaid))) + sizeof(ia->iaid))) continue; if (ia->sla_len == 0) { /* no SLA configured, so lets @@ -3068,8 +3009,8 @@ dhcp6_delegate_prefix(struct interface *ifp) carrier_warned = true; break; } - if (dhcp6_ifdelegateaddr(ifd, ap, - NULL, ia)) + if (dhcp6_ifdelegateaddr(ifd, ap, NULL, + ia)) k++; } for (j = 0; j < ia->sla_len; j++) { @@ -3084,8 +3025,8 @@ dhcp6_delegate_prefix(struct interface *ifp) carrier_warned = true; break; } - if (dhcp6_ifdelegateaddr(ifd, ap, - sla, ia)) + if (dhcp6_ifdelegateaddr(ifd, ap, sla, + ia)) k++; } if (carrier_warned) @@ -3109,7 +3050,6 @@ dhcp6_delegate_prefix(struct interface *ifp) static void dhcp6_find_delegates1(void *arg) { - dhcp6_find_delegates(arg); } @@ -3124,8 +3064,7 @@ dhcp6_find_delegates(struct interface *ifp) struct if_sla *sla; struct interface *ifd; - if (ifp->options != NULL && - !(ifp->options->options & DHCPCD_CONFIGURE)) + if (ifp->options != NULL && !(ifp->options->options & DHCPCD_CONFIGURE)) return 0; k = 0; @@ -3142,7 +3081,7 @@ dhcp6_find_delegates(struct interface *ifp) if (ia->ia_type != D6_OPTION_IA_PD) continue; if (memcmp(ia->iaid, ap->iaid, - sizeof(ia->iaid))) + sizeof(ia->iaid))) continue; for (j = 0; j < ia->sla_len; j++) { sla = &ia->sla[j]; @@ -3158,9 +3097,9 @@ dhcp6_find_delegates(struct interface *ifp) dhcp6_find_delegates1, ifp); return 1; } - if (dhcp6_ifdelegateaddr(ifp, ap, - sla, ia)) - k++; + if (dhcp6_ifdelegateaddr(ifp, ap, sla, + ia)) + k++; } } } @@ -3201,8 +3140,8 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) state->new_start = false; if (!timedout) { - logmessage(loglevel, "%s: %s received from %s", - ifp->name, op, sfrom); + logmessage(loglevel, "%s: %s received from %s", ifp->name, op, + sfrom); #ifndef SMALL /* If we delegated from an unconfirmed lease we MUST drop * them now. Hopefully we have new delegations. */ @@ -3217,16 +3156,15 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) eloop_timeout_delete(ifp->ctx->eloop, NULL, ifp); clock_gettime(CLOCK_MONOTONIC, &now); - switch(state->state) { - case DH6S_INFORM: - { + switch (state->state) { + case DH6S_INFORM: { struct dhcp6_option *o; uint16_t ol; if (state->reason == NULL) state->reason = "INFORM6"; o = dhcp6_findmoption(state->recv, state->recv_len, - D6_OPTION_INFO_REFRESH_TIME, &ol); + D6_OPTION_INFO_REFRESH_TIME, &ol); if (o == NULL || ol != sizeof(uint32_t)) state->renew = IRT_DEFAULT; else { @@ -3238,8 +3176,7 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) state->rebind = 0; state->expire = ND6_INFINITE_LIFETIME; state->lowpl = ND6_INFINITE_LIFETIME; - } - break; + } break; case DH6S_REQUEST: if (state->reason == NULL) @@ -3262,10 +3199,11 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) TAILQ_FOREACH(ia, &state->addrs, next) { if (ia->flags & IPV6_AF_STALE) continue; - if (!(state->renew == ND6_INFINITE_LIFETIME - && ia->prefix_vltime == ND6_INFINITE_LIFETIME) - && ia->prefix_vltime != 0 - && ia->prefix_vltime <= state->renew) + if (!(state->renew == ND6_INFINITE_LIFETIME && + ia->prefix_vltime == + ND6_INFINITE_LIFETIME) && + ia->prefix_vltime != 0 && + ia->prefix_vltime <= state->renew) logwarnx( "%s: %s will expire before renewal", ifp->name, ia->saddr); @@ -3285,8 +3223,8 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) * and T2 fields for the T1 and T2 parameters, * unless those values in those fields are 0. */ - logwarnx("%s: ignoring T1 %"PRIu32 - " due to address expiry", + logwarnx("%s: ignoring T1 %" PRIu32 + " due to address expiry", ifp->name, state->renew); state->renew = state->rebind = 0; } @@ -3315,8 +3253,8 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) /* Reduce timers based on when we got the lease. */ uint32_t elapsed; - elapsed = (uint32_t)eloop_timespec_diff(&now, - &state->acquired, NULL); + elapsed = (uint32_t)eloop_timespec_diff(&now, &state->acquired, + NULL); if (state->renew && state->renew != ND6_INFINITE_LIFETIME) { if (state->renew > elapsed) state->renew -= elapsed; @@ -3351,17 +3289,17 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) * or REBIND right away if the timers have expired */ if ((state->renew || (state->rebind && confirmed)) && state->renew != ND6_INFINITE_LIFETIME) - eloop_timeout_add_sec(ifp->ctx->eloop, - state->renew, - state->state == DH6S_INFORMED ? - dhcp6_startinform : dhcp6_startrenew, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, state->renew, + state->state == DH6S_INFORMED ? dhcp6_startinform : + dhcp6_startrenew, + ifp); if ((state->rebind || (state->expire && confirmed)) && state->rebind != ND6_INFINITE_LIFETIME) - eloop_timeout_add_sec(ifp->ctx->eloop, - state->rebind, dhcp6_startrebind, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, state->rebind, + dhcp6_startrebind, ifp); if (state->expire != ND6_INFINITE_LIFETIME) - eloop_timeout_add_sec(ifp->ctx->eloop, - state->expire, dhcp6_startexpire, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, state->expire, + dhcp6_startexpire, ifp); if (ifp->options->options & DHCPCD_CONFIGURE) { ipv6_addaddrs(&state->addrs); @@ -3370,29 +3308,32 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) } if (state->state == DH6S_INFORMED) - logmessage(loglevel, "%s: refresh in %"PRIu32" seconds", - ifp->name, state->renew); + logmessage(loglevel, + "%s: refresh in %" PRIu32 " seconds", ifp->name, + state->renew); else if (state->renew == ND6_INFINITE_LIFETIME) logmessage(loglevel, "%s: leased for infinity", ifp->name); else if (state->renew || state->rebind) - logmessage(loglevel, "%s: renew in %"PRIu32", " - "rebind in %"PRIu32", " - "expire in %"PRIu32" seconds", - ifp->name, - state->renew, state->rebind, state->expire); + logmessage(loglevel, + "%s: renew in %" PRIu32 ", " + "rebind in %" PRIu32 ", " + "expire in %" PRIu32 " seconds", + ifp->name, state->renew, state->rebind, + state->expire); else if (state->expire == 0) logmessage(loglevel, "%s: will expire", ifp->name); else - logmessage(loglevel, "%s: expire in %"PRIu32" seconds", - ifp->name, state->expire); + logmessage(loglevel, + "%s: expire in %" PRIu32 " seconds", ifp->name, + state->expire); rt_build(ifp->ctx, AF_INET6); if (!confirmed && !timedout) { - logdebugx("%s: writing lease: %s", - ifp->name, state->leasefile); + logdebugx("%s: writing lease: %s", ifp->name, + state->leasefile); if (dhcp_writefile(ifp->ctx, state->leasefile, 0640, - state->new, state->new_len) == -1) - logerr("dhcp_writefile: %s",state->leasefile); + state->new, state->new_len) == -1) + logerr("dhcp_writefile: %s", state->leasefile); } #ifndef SMALL dhcp6_delegate_prefix(ifp); @@ -3405,8 +3346,7 @@ dhcp6_bind(struct interface *ifp, const char *op, const char *sfrom) } static void -dhcp6_adjust_max_rt(struct interface *ifp, - struct dhcp6_message *r, size_t len) +dhcp6_adjust_max_rt(struct interface *ifp, struct dhcp6_message *r, size_t len) { struct dhcp6_state *state = D6_STATE(ifp); uint8_t *o; @@ -3420,10 +3360,8 @@ dhcp6_adjust_max_rt(struct interface *ifp, memcpy(&max_rt, o, sizeof(max_rt)); max_rt = ntohl(max_rt); if (max_rt >= 60 && max_rt <= 86400) { - logdebugx("%s: SOL_MAX_RT %llu -> %u", - ifp->name, - (unsigned long long)state->sol_max_rt, - max_rt); + logdebugx("%s: SOL_MAX_RT %llu -> %u", ifp->name, + (unsigned long long)state->sol_max_rt, max_rt); state->sol_max_rt = max_rt; } else logerrx("%s: invalid SOL_MAX_RT %u", ifp->name, max_rt); @@ -3436,10 +3374,8 @@ dhcp6_adjust_max_rt(struct interface *ifp, memcpy(&max_rt, o, sizeof(max_rt)); max_rt = ntohl(max_rt); if (max_rt >= 60 && max_rt <= 86400) { - logdebugx("%s: INF_MAX_RT %llu -> %u", - ifp->name, - (unsigned long long)state->inf_max_rt, - max_rt); + logdebugx("%s: INF_MAX_RT %llu -> %u", ifp->name, + (unsigned long long)state->inf_max_rt, max_rt); state->inf_max_rt = max_rt; } else logerrx("%s: invalid INF_MAX_RT %u", ifp->name, max_rt); @@ -3447,8 +3383,8 @@ dhcp6_adjust_max_rt(struct interface *ifp, } static void -dhcp6_recvif(struct interface *ifp, const char *sfrom, - struct dhcp6_message *r, size_t len) +dhcp6_recvif(struct interface *ifp, const char *sfrom, struct dhcp6_message *r, + size_t len) { struct dhcpcd_ctx *ctx; size_t i; @@ -3475,8 +3411,7 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, /* We're already bound and this message is for another machine */ /* XXX DELEGATED? */ if (r->type != DHCP6_RECONFIGURE && - (state->state == DH6S_BOUND || state->state == DH6S_INFORMED)) - { + (state->state == DH6S_BOUND || state->state == DH6S_INFORMED)) { logdebugx("%s: DHCPv6 reply received but already bound", ifp->name); return; @@ -3488,20 +3423,16 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, } ifo = ifp->options; - for (i = 0, opt = ctx->dhcp6_opts; - i < ctx->dhcp6_opts_len; - i++, opt++) - { + for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; + i++, opt++) { if (has_option_mask(ifo->requiremask6, opt->option) && - !dhcp6_findmoption(r, len, (uint16_t)opt->option, NULL)) - { + !dhcp6_findmoption(r, len, (uint16_t)opt->option, NULL)) { logwarnx("%s: reject DHCPv6 (no option %s) from %s", ifp->name, opt->var, sfrom); return; } if (has_option_mask(ifo->rejectmask6, opt->option) && - dhcp6_findmoption(r, len, (uint16_t)opt->option, NULL)) - { + dhcp6_findmoption(r, len, (uint16_t)opt->option, NULL)) { logwarnx("%s: reject DHCPv6 (option %s) from %s", ifp->name, opt->var, sfrom); return; @@ -3512,22 +3443,21 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, /* Authenticate the message */ auth = dhcp6_findmoption(r, len, D6_OPTION_AUTH, &auth_len); if (auth != NULL) { - if (dhcp_auth_validate(&state->auth, &ifo->auth, - (uint8_t *)r, len, 6, r->type, auth, auth_len) == NULL) - { - logerr("%s: authentication failed from %s", - ifp->name, sfrom); + if (dhcp_auth_validate(&state->auth, &ifo->auth, (uint8_t *)r, + len, 6, r->type, auth, auth_len) == NULL) { + logerr("%s: authentication failed from %s", ifp->name, + sfrom); return; } if (state->auth.token) - logdebugx("%s: validated using 0x%08" PRIu32, - ifp->name, state->auth.token->secretid); + logdebugx("%s: validated using 0x%08" PRIu32, ifp->name, + state->auth.token->secretid); else loginfox("%s: accepted reconfigure key", ifp->name); } else if (ifo->auth.options & DHCPCD_AUTH_SEND) { if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) { - logerrx("%s: no authentication from %s", - ifp->name, sfrom); + logerrx("%s: no authentication from %s", ifp->name, + sfrom); return; } logwarnx("%s: no authentication from %s", ifp->name, sfrom); @@ -3536,16 +3466,16 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, op = dhcp6_get_op(r->type); valid_op = op != NULL; - switch(r->type) { + switch (r->type) { case DHCP6_REPLY: - switch(state->state) { + switch (state->state) { case DH6S_INFORM: if (dhcp6_checkstatusok(ifp, r, NULL, len) != 0) return; break; case DH6S_CONFIRM: - if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == -1) - { + if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == + -1) { dhcp6_startdiscoinform(ifp); return; } @@ -3554,21 +3484,20 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, /* Only accept REPLY in DISCOVER for RAPID_COMMIT. * Normally we get an ADVERTISE for a DISCOVER. */ if (!has_option_mask(ifo->requestmask6, - D6_OPTION_RAPID_COMMIT) || + D6_OPTION_RAPID_COMMIT) || !dhcp6_findmoption(r, len, D6_OPTION_RAPID_COMMIT, - NULL)) - { + NULL)) { valid_op = false; break; } /* Validate lease before setting state to REQUEST. */ /* FALLTHROUGH */ case DH6S_REQUEST: /* FALLTHROUGH */ - case DH6S_RENEW: /* FALLTHROUGH */ + case DH6S_RENEW: /* FALLTHROUGH */ case DH6S_REBIND: dhcp6_adjust_max_rt(ifp, r, len); - if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == -1) - { + if (dhcp6_validatelease(ifp, r, len, sfrom, NULL) == + -1) { /* * If we can't use the lease, fallback to * DISCOVER and try and get a new one. @@ -3591,9 +3520,8 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, } /* RFC8415 18.2.10.1 */ if ((state->state == DH6S_RENEW || - state->state == DH6S_REBIND) && - state->has_no_binding) - { + state->state == DH6S_REBIND) && + state->has_no_binding) { dhcp6_startrequest(ifp); return; } @@ -3603,13 +3531,13 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, case DH6S_DECLINE: /* This isnt really a failure, but an * acknowledgement of one. */ - loginfox("%s: %s acknowledged DECLINE6", - ifp->name, sfrom); + loginfox("%s: %s acknowledged DECLINE6", ifp->name, + sfrom); dhcp6_fail(ifp, true); return; case DH6S_RELEASE: - loginfox("%s: %s acknowledged RELEASE6", - ifp->name, sfrom); + loginfox("%s: %s acknowledged RELEASE6", ifp->name, + sfrom); dhcp6_finishrelease(ifp); return; default: @@ -3648,8 +3576,8 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, #ifdef AUTH if (auth == NULL) { #endif - logerrx("%s: unauthenticated %s from %s", - ifp->name, op, sfrom); + logerrx("%s: unauthenticated %s from %s", ifp->name, op, + sfrom); if (ifo->auth.options & DHCPCD_AUTH_REQUIRE) return; #ifdef AUTH @@ -3666,11 +3594,11 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, ifp->name); return; } - switch(*o) { + switch (*o) { case DHCP6_RENEW: if (state->state != DH6S_BOUND) { - logerrx("%s: not bound, ignoring %s", - ifp->name, op); + logerrx("%s: not bound, ignoring %s", ifp->name, + op); return; } dhcp6_startrenew(ifp); @@ -3681,13 +3609,12 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, ifp->name, op); return; } - eloop_timeout_delete(ifp->ctx->eloop, - dhcp6_sendinform, ifp); + eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendinform, + ifp); dhcp6_startinform(ifp); break; default: - logerr("%s: unsupported %s type %d", - ifp->name, op, *o); + logerr("%s: unsupported %s type %d", ifp->name, op, *o); break; } return; @@ -3695,13 +3622,13 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, break; #endif default: - logerrx("%s: invalid DHCP6 type %s (%d)", - ifp->name, op, r->type); + logerrx("%s: invalid DHCP6 type %s (%d)", ifp->name, op, + r->type); return; } if (!valid_op) { - logwarnx("%s: invalid state for DHCP6 type %s (%d)", - ifp->name, op, r->type); + logwarnx("%s: invalid state for DHCP6 type %s (%d)", ifp->name, + op, r->type); return; } @@ -3726,11 +3653,11 @@ dhcp6_recvif(struct interface *ifp, const char *sfrom, if (ia == NULL) ia = TAILQ_FIRST(&state->addrs); if (ia == NULL) - loginfox("%s: ADV (no address) from %s (%u)", - ifp->name, sfrom, preference); + loginfox("%s: ADV (no address) from %s (%u)", ifp->name, + sfrom, preference); else - loginfox("%s: ADV %s from %s (%u)", - ifp->name, ia->saddr, sfrom, preference); + loginfox("%s: ADV %s from %s (%u)", ifp->name, + ia->saddr, sfrom, preference); /* * RFC 8415 18.2.1 says we must collect until ADVERTISEMENTs @@ -3786,14 +3713,12 @@ dhcp6_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, struct ipv6_addr *ia) dp = ctx->duid; } if (o == NULL || ol != duid_len || memcmp(o, dp, ol) != 0) { - logdebugx("%s: incorrect client ID from %s", - ifp->name, sfrom); + logdebugx("%s: incorrect client ID from %s", ifp->name, sfrom); return; } if (dhcp6_findmoption(r, len, D6_OPTION_SERVERID, NULL) == NULL) { - logdebugx("%s: no DHCPv6 server ID from %s", - ifp->name, sfrom); + logdebugx("%s: no DHCPv6 server ID from %s", ifp->name, sfrom); return; } @@ -3807,11 +3732,9 @@ dhcp6_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, struct ipv6_addr *ia) } state = D6_CSTATE(ifp); - if (state == NULL || - r->xid[0] != state->send->xid[0] || + if (state == NULL || r->xid[0] != state->send->xid[0] || r->xid[1] != state->send->xid[1] || - r->xid[2] != state->send->xid[2]) - { + r->xid[2] != state->send->xid[2]) { struct interface *ifp1; const struct dhcp6_state *state1; @@ -3829,17 +3752,14 @@ dhcp6_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, struct ipv6_addr *ia) if (ifp1 == NULL) { if (state != NULL) logdebugx("%s: wrong xid 0x%02x%02x%02x" - " (expecting 0x%02x%02x%02x) from %s", - ifp->name, - r->xid[0], r->xid[1], r->xid[2], - state->send->xid[0], - state->send->xid[1], - state->send->xid[2], - sfrom); + " (expecting 0x%02x%02x%02x) from %s", + ifp->name, r->xid[0], r->xid[1], r->xid[2], + state->send->xid[0], state->send->xid[1], + state->send->xid[2], sfrom); return; } - logdebugx("%s: redirecting DHCP6 message to %s", - ifp->name, ifp1->name); + logdebugx("%s: redirecting DHCP6 message to %s", ifp->name, + ifp1->name); ifp = ifp1; } @@ -3892,16 +3812,20 @@ dhcp6_recv(struct dhcpcd_ctx *ctx, struct ipv6_addr *ia, unsigned short events) uint8_t buf[UDPLEN_MAX]; /* Maximum UDP message size */ } iovbuf; struct iovec iov = { - .iov_base = iovbuf.buf, .iov_len = sizeof(iovbuf.buf), + .iov_base = iovbuf.buf, + .iov_len = sizeof(iovbuf.buf), }; union { struct cmsghdr hdr; uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; } cmsgbuf = { .buf = { 0 } }; struct msghdr msg = { - .msg_name = &from, .msg_namelen = sizeof(from), - .msg_iov = &iov, .msg_iovlen = 1, - .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf), + .msg_name = &from, + .msg_namelen = sizeof(from), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = sizeof(cmsgbuf.buf), }; int s; ssize_t bytes; @@ -4048,16 +3972,16 @@ dhcp6_start1(void *arg) size_t i; const struct dhcp_compat *dhc; - if ((ctx->options & (DHCPCD_MANAGER|DHCPCD_PRIVSEP)) == DHCPCD_MANAGER && - ctx->dhcp6_rfd == -1) - { + if ((ctx->options & (DHCPCD_MANAGER | DHCPCD_PRIVSEP)) == + DHCPCD_MANAGER && + ctx->dhcp6_rfd == -1) { ctx->dhcp6_rfd = dhcp6_openudp(0, NULL); if (ctx->dhcp6_rfd == -1) { logerr(__func__); return; } if (eloop_event_add(ctx->eloop, ctx->dhcp6_rfd, ELE_READ, - dhcp6_recvctx, ctx) == -1) + dhcp6_recvctx, ctx) == -1) logerr("%s: eloop_event_add", __func__); } @@ -4078,7 +4002,8 @@ dhcp6_start1(void *arg) } if (i == sizeof(ifo->requestmask6)) { for (dhc = dhcp_compats; dhc->dhcp_opt; dhc++) { - if (DHC_REQ(ifo->requestmask, ifo->nomask, dhc->dhcp_opt)) + if (DHC_REQ(ifo->requestmask, ifo->nomask, + dhc->dhcp_opt)) add_option_mask(ifo->requestmask6, dhc->dhcp6_opt); } @@ -4120,17 +4045,16 @@ dhcp6_start(struct interface *ifp, enum DH6S init_state) * pl/vl time of the RA changing. */ if (state->state == DH6S_INIT || (state->state == DH6S_DISCOVER && - !(ifp->options->options & DHCPCD_IA_FORCED) && - !ipv6nd_hasradhcp(ifp, true))) + !(ifp->options->options & DHCPCD_IA_FORCED) && + !ipv6nd_hasradhcp(ifp, true))) dhcp6_startinform(ifp); break; case DH6S_REQUEST: if (ifp->options->options & DHCPCD_DHCP6 && (state->state == DH6S_INIT || - state->state == DH6S_INFORM || - state->state == DH6S_INFORMED || - state->state == DH6S_DELEGATED)) - { + state->state == DH6S_INFORM || + state->state == DH6S_INFORMED || + state->state == DH6S_DELEGATED)) { /* Change from stateless to stateful */ init_state = DH6S_INIT; goto gogogo; @@ -4185,8 +4109,8 @@ dhcp6_start(struct interface *ifp, enum DH6S init_state) state->new_start = true; state->lerror = 0; state->failed = false; - dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), - AF_INET6, ifp); + dhcp_set_leasefile(state->leasefile, sizeof(state->leasefile), AF_INET6, + ifp); if (ipv6_linklocal(ifp) == NULL) { logdebugx("%s: delaying DHCPv6 for LL address", ifp->name); ipv6_addlinklocalcallback(ifp, dhcp6_start1, ifp); @@ -4252,12 +4176,10 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason) } if (drop && options & DHCPCD_RELEASE && - state->state != DH6S_DELEGATED) - { + state->state != DH6S_DELEGATED) { if (if_is_link_up(ifp) && state->state != DH6S_RELEASED && - state->state != DH6S_INFORMED) - { + state->state != DH6S_INFORMED) { dhcp6_startrelease(ifp); return; } @@ -4283,8 +4205,7 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason) state->new = NULL; state->new_len = 0; if (drop && state->old && - (options & DHCPCD_NODROP) != DHCPCD_NODROP) - { + (options & DHCPCD_NODROP) != DHCPCD_NODROP) { if (reason == NULL) reason = "STOP6"; script_runreason(ifp, reason); @@ -4316,14 +4237,12 @@ dhcp6_freedrop(struct interface *ifp, int drop, const char *reason) void dhcp6_drop(struct interface *ifp, const char *reason) { - dhcp6_freedrop(ifp, 1, reason); } void dhcp6_free(struct interface *ifp) { - dhcp6_freedrop(ifp, 0, NULL); } @@ -4343,8 +4262,8 @@ dhcp6_abort(struct interface *ifp) eloop_timeout_delete(ifp->ctx->eloop, dhcp6_sendinform, ifp); switch (state->state) { - case DH6S_DISCOVER: /* FALLTHROUGH */ - case DH6S_REQUEST: /* FALLTHROUGH */ + case DH6S_DISCOVER: /* FALLTHROUGH */ + case DH6S_REQUEST: /* FALLTHROUGH */ case DH6S_INFORM: state->state = DH6S_INIT; break; @@ -4360,12 +4279,10 @@ dhcp6_handleifa(int cmd, struct ipv6_addr *ia, pid_t pid) struct interface *ifp = ia->iface; /* If not running in manager mode, listen to this address */ - if (cmd == RTM_NEWADDR && - !(ia->addr_flags & IN6_IFF_NOTUSEABLE) && + if (cmd == RTM_NEWADDR && !(ia->addr_flags & IN6_IFF_NOTUSEABLE) && ifp->active == IF_ACTIVE_USER && !(ifp->ctx->options & DHCPCD_MANAGER) && - ifp->options->options & DHCPCD_DHCP6) - { + ifp->options->options & DHCPCD_DHCP6) { #ifdef PRIVSEP if (IN_PRIVSEP_SE(ifp->ctx)) { if (ps_inet_opendhcp6(ia) == -1) @@ -4377,8 +4294,8 @@ dhcp6_handleifa(int cmd, struct ipv6_addr *ia, pid_t pid) ia->dhcp6_fd = dhcp6_openudp(ia->iface->index, &ia->addr); if (ia->dhcp6_fd != -1 && - eloop_event_add(ia->iface->ctx->eloop, - ia->dhcp6_fd, ELE_READ, dhcp6_recvaddr, ia) == -1) + eloop_event_add(ia->iface->ctx->eloop, ia->dhcp6_fd, + ELE_READ, dhcp6_recvaddr, ia) == -1) logerr("%s: eloop_event_add", __func__); } } @@ -4419,17 +4336,12 @@ dhcp6_env(FILE *fp, const char *prefix, const struct interface *ifp, ctx = ifp->ctx; /* Zero our indexes */ - for (i = 0, opt = ctx->dhcp6_opts; - i < ctx->dhcp6_opts_len; - i++, opt++) + for (i = 0, opt = ctx->dhcp6_opts; i < ctx->dhcp6_opts_len; i++, opt++) dhcp_zero_index(opt); for (i = 0, opt = ifp->options->dhcp6_override; - i < ifp->options->dhcp6_override_len; - i++, opt++) + i < ifp->options->dhcp6_override_len; i++, opt++) dhcp_zero_index(opt); - for (i = 0, opt = ctx->vivso; - i < ctx->vivso_len; - i++, opt++) + for (i = 0, opt = ctx->vivso; i < ctx->vivso_len; i++, opt++) dhcp_zero_index(opt); if (asprintf(&pfx, "%s_dhcp6", prefix) == -1) return -1; @@ -4448,21 +4360,18 @@ dhcp6_env(FILE *fp, const char *prefix, const struct interface *ifp, len -= sizeof(o); o.len = ntohs(o.len); if (len < o.len) { - errno = EINVAL; + errno = EINVAL; break; } o.code = ntohs(o.code); if (has_option_mask(ifo->nomask6, o.code)) continue; for (i = 0, opt = ifo->dhcp6_override; - i < ifo->dhcp6_override_len; - i++, opt++) + i < ifo->dhcp6_override_len; i++, opt++) if (opt->option == o.code) break; if (i == ifo->dhcp6_override_len && - o.code == D6_OPTION_VENDOR_OPTS && - o.len > sizeof(en)) - { + o.code == D6_OPTION_VENDOR_OPTS && o.len > sizeof(en)) { memcpy(&en, p, sizeof(en)); en = ntohl(en); vo = vivso_find(en, ifp); @@ -4470,23 +4379,19 @@ dhcp6_env(FILE *fp, const char *prefix, const struct interface *ifp, vo = NULL; if (i == ifo->dhcp6_override_len) { for (i = 0, opt = ctx->dhcp6_opts; - i < ctx->dhcp6_opts_len; - i++, opt++) + i < ctx->dhcp6_opts_len; i++, opt++) if (opt->option == o.code) break; if (i == ctx->dhcp6_opts_len) opt = NULL; } if (opt) { - dhcp_envoption(ifp->ctx, - fp, pfx, ifp->name, - opt, dhcp6_getoption, p, o.len); + dhcp_envoption(ifp->ctx, fp, pfx, ifp->name, opt, + dhcp6_getoption, p, o.len); } if (vo) { - dhcp_envoption(ifp->ctx, - fp, pfx, ifp->name, - vo, dhcp6_getoption, - p + sizeof(en), + dhcp_envoption(ifp->ctx, fp, pfx, ifp->name, vo, + dhcp6_getoption, p + sizeof(en), o.len - sizeof(en)); } } diff --git a/src/dhcp6.h b/src/dhcp6.h index 008981c7..45449a54 100644 --- a/src/dhcp6.h +++ b/src/dhcp6.h @@ -31,13 +31,15 @@ #include "dhcpcd.h" -#define IN6ADDR_LINKLOCAL_ALLDHCP_INIT \ - {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02 }}} +#define IN6ADDR_LINKLOCAL_ALLDHCP_INIT \ + { \ + {{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02 }} \ + } /* UDP port numbers for DHCP */ -#define DHCP6_CLIENT_PORT 546 -#define DHCP6_SERVER_PORT 547 +#define DHCP6_CLIENT_PORT 546 +#define DHCP6_SERVER_PORT 547 /* DHCP message type */ #define DHCP6_SOLICIT 1 @@ -54,108 +56,108 @@ #define DHCP6_RELAY_FLOW 12 #define DHCP6_RELAY_REPL 13 #define DHCP6_RECONFIGURE_REQ 18 -#define DHCP6_RECONFIGURE_REPLY 19 +#define DHCP6_RECONFIGURE_REPLY 19 #ifdef DHCP6 -#define D6_OPTION_CLIENTID 1 -#define D6_OPTION_SERVERID 2 -#define D6_OPTION_IA_NA 3 -#define D6_OPTION_IA_TA 4 -#define D6_OPTION_ORO 6 -#define D6_OPTION_IA_ADDR 5 -#define D6_OPTION_PREFERENCE 7 -#define D6_OPTION_ELAPSED 8 -#define D6_OPTION_AUTH 11 -#define D6_OPTION_UNICAST 12 -#define D6_OPTION_STATUS_CODE 13 -#define D6_OPTION_RAPID_COMMIT 14 -#define D6_OPTION_USER_CLASS 15 -#define D6_OPTION_VENDOR_CLASS 16 -#define D6_OPTION_VENDOR_OPTS 17 -#define D6_OPTION_INTERFACE_ID 18 -#define D6_OPTION_RECONF_MSG 19 -#define D6_OPTION_RECONF_ACCEPT 20 -#define D6_OPTION_SIP_SERVERS_NAME 21 -#define D6_OPTION_SIP_SERVERS_ADDRESS 22 -#define D6_OPTION_DNS_SERVERS 23 -#define D6_OPTION_DOMAIN_LIST 24 -#define D6_OPTION_IA_PD 25 -#define D6_OPTION_IAPREFIX 26 -#define D6_OPTION_NIS_SERVERS 27 -#define D6_OPTION_NISP_SERVERS 28 -#define D6_OPTION_NIS_DOMAIN_NAME 29 -#define D6_OPTION_NISP_DOMAIN_NAME 30 -#define D6_OPTION_SNTP_SERVERS 31 -#define D6_OPTION_INFO_REFRESH_TIME 32 -#define D6_OPTION_BCMS_SERVER_D 33 -#define D6_OPTION_BCMS_SERVER_A 34 -#define D6_OPTION_FQDN 39 -#define D6_OPTION_POSIX_TIMEZONE 41 -#define D6_OPTION_TZDB_TIMEZONE 42 -#define D6_OPTION_NTP_SERVER 56 -#define D6_OPTION_PD_EXCLUDE 67 -#define D6_OPTION_SOL_MAX_RT 82 -#define D6_OPTION_INF_MAX_RT 83 -#define D6_OPTION_MUDURL 112 - -#define D6_FQDN_PTR 0x00 -#define D6_FQDN_BOTH 0x01 -#define D6_FQDN_NONE 0x04 +#define D6_OPTION_CLIENTID 1 +#define D6_OPTION_SERVERID 2 +#define D6_OPTION_IA_NA 3 +#define D6_OPTION_IA_TA 4 +#define D6_OPTION_ORO 6 +#define D6_OPTION_IA_ADDR 5 +#define D6_OPTION_PREFERENCE 7 +#define D6_OPTION_ELAPSED 8 +#define D6_OPTION_AUTH 11 +#define D6_OPTION_UNICAST 12 +#define D6_OPTION_STATUS_CODE 13 +#define D6_OPTION_RAPID_COMMIT 14 +#define D6_OPTION_USER_CLASS 15 +#define D6_OPTION_VENDOR_CLASS 16 +#define D6_OPTION_VENDOR_OPTS 17 +#define D6_OPTION_INTERFACE_ID 18 +#define D6_OPTION_RECONF_MSG 19 +#define D6_OPTION_RECONF_ACCEPT 20 +#define D6_OPTION_SIP_SERVERS_NAME 21 +#define D6_OPTION_SIP_SERVERS_ADDRESS 22 +#define D6_OPTION_DNS_SERVERS 23 +#define D6_OPTION_DOMAIN_LIST 24 +#define D6_OPTION_IA_PD 25 +#define D6_OPTION_IAPREFIX 26 +#define D6_OPTION_NIS_SERVERS 27 +#define D6_OPTION_NISP_SERVERS 28 +#define D6_OPTION_NIS_DOMAIN_NAME 29 +#define D6_OPTION_NISP_DOMAIN_NAME 30 +#define D6_OPTION_SNTP_SERVERS 31 +#define D6_OPTION_INFO_REFRESH_TIME 32 +#define D6_OPTION_BCMS_SERVER_D 33 +#define D6_OPTION_BCMS_SERVER_A 34 +#define D6_OPTION_FQDN 39 +#define D6_OPTION_POSIX_TIMEZONE 41 +#define D6_OPTION_TZDB_TIMEZONE 42 +#define D6_OPTION_NTP_SERVER 56 +#define D6_OPTION_PD_EXCLUDE 67 +#define D6_OPTION_SOL_MAX_RT 82 +#define D6_OPTION_INF_MAX_RT 83 +#define D6_OPTION_MUDURL 112 + +#define D6_FQDN_PTR 0x00 +#define D6_FQDN_BOTH 0x01 +#define D6_FQDN_NONE 0x04 #include "dhcp.h" #include "ipv6.h" -#define D6_STATUS_OK 0 -#define D6_STATUS_FAIL 1 -#define D6_STATUS_NOADDR 2 -#define D6_STATUS_NOBINDING 3 -#define D6_STATUS_NOTONLINK 4 -#define D6_STATUS_USEMULTICAST 5 - -#define SOL_MAX_DELAY 1 -#define SOL_TIMEOUT 1 -#define SOL_MAX_RT 3600 /* RFC 8415 */ -#define SOL_MAX_RC 0 -#define REQ_MAX_DELAY 0 -#define REQ_TIMEOUT 1 -#define REQ_MAX_RT 30 -#define REQ_MAX_RC 10 -#define CNF_MAX_DELAY 1 -#define CNF_TIMEOUT 1 -#define CNF_MAX_RT 4 -#define CNF_MAX_RC 0 -#define CNF_MAX_RD 10 -#define REN_MAX_DELAY 0 -#define REN_TIMEOUT 10 -#define REN_MAX_RT 600 -#define REB_MAX_DELAY 0 -#define REB_TIMEOUT 10 -#define REB_MAX_RT 600 -#define INF_MAX_DELAY 1 -#define INF_TIMEOUT 1 -#define INF_MAX_RD CNF_MAX_RD /* NOT RFC defined */ -#define INF_MAX_RT 3600 /* RFC 8415*/ -#define REL_MAX_DELAY 0 -#define REL_TIMEOUT 1 -#define REL_MAX_RT 0 -#define REL_MAX_RC 4 /* RFC 8415 */ -#define DEC_MAX_DELAY 0 -#define DEC_TIMEOUT 1 -#define DEC_MAX_RC 4 /* RFC 8415 */ -#define REC_MAX_DELAY 0 -#define REC_TIMEOUT 2 -#define REC_MAX_RC 8 -#define HOP_COUNT_LIMIT 32 +#define D6_STATUS_OK 0 +#define D6_STATUS_FAIL 1 +#define D6_STATUS_NOADDR 2 +#define D6_STATUS_NOBINDING 3 +#define D6_STATUS_NOTONLINK 4 +#define D6_STATUS_USEMULTICAST 5 + +#define SOL_MAX_DELAY 1 +#define SOL_TIMEOUT 1 +#define SOL_MAX_RT 3600 /* RFC 8415 */ +#define SOL_MAX_RC 0 +#define REQ_MAX_DELAY 0 +#define REQ_TIMEOUT 1 +#define REQ_MAX_RT 30 +#define REQ_MAX_RC 10 +#define CNF_MAX_DELAY 1 +#define CNF_TIMEOUT 1 +#define CNF_MAX_RT 4 +#define CNF_MAX_RC 0 +#define CNF_MAX_RD 10 +#define REN_MAX_DELAY 0 +#define REN_TIMEOUT 10 +#define REN_MAX_RT 600 +#define REB_MAX_DELAY 0 +#define REB_TIMEOUT 10 +#define REB_MAX_RT 600 +#define INF_MAX_DELAY 1 +#define INF_TIMEOUT 1 +#define INF_MAX_RD CNF_MAX_RD /* NOT RFC defined */ +#define INF_MAX_RT 3600 /* RFC 8415*/ +#define REL_MAX_DELAY 0 +#define REL_TIMEOUT 1 +#define REL_MAX_RT 0 +#define REL_MAX_RC 4 /* RFC 8415 */ +#define DEC_MAX_DELAY 0 +#define DEC_TIMEOUT 1 +#define DEC_MAX_RC 4 /* RFC 8415 */ +#define REC_MAX_DELAY 0 +#define REC_TIMEOUT 2 +#define REC_MAX_RC 8 +#define HOP_COUNT_LIMIT 32 /* RFC4242 3.1 */ -#define IRT_DEFAULT 86400 -#define IRT_MINIMUM 600 +#define IRT_DEFAULT 86400 +#define IRT_MINIMUM 600 /* These should give -.1 to .1 randomness */ -#define DHCP6_RAND_MIN -100 -#define DHCP6_RAND_MAX 100 -#define DHCP6_RAND_DIV 1000.0f +#define DHCP6_RAND_MIN -100 +#define DHCP6_RAND_MAX 100 +#define DHCP6_RAND_DIV 1000.0f enum DH6S { DH6S_INIT, @@ -189,9 +191,9 @@ struct dhcp6_state { void (*MRCcallback)(void *); unsigned int sol_max_rt; unsigned int inf_max_rt; - unsigned int RT; /* retransmission timer in milliseconds - * maximal RT is 1 day + RAND, - * so should be enough */ + unsigned int RT; /* retransmission timer in milliseconds + * maximal RT is 1 day + RAND, + * so should be enough */ struct dhcp6_message *send; size_t send_len; @@ -210,30 +212,29 @@ struct dhcp6_state { struct ipv6_addrhead addrs; uint32_t lowpl; /* The +3 is for the possible .pd extension for prefix delegation */ - char leasefile[sizeof(LEASEFILE6) + IF_NAMESIZE + (IF_SSIDLEN * 4) +3]; + char leasefile[sizeof(LEASEFILE6) + IF_NAMESIZE + (IF_SSIDLEN * 4) + 3]; const char *reason; uint16_t lerror; /* Last error received from DHCPv6 reply. */ bool has_no_binding; - bool failed; /* Entered the failed state - used to rate limit log. */ + bool failed; /* Entered the failed state - used to rate limit log. */ bool new_start; /* New external start, to determine log type. */ #ifdef AUTH struct authstate auth; #endif }; -#define D6_STATE(ifp) \ - ((struct dhcp6_state *)(ifp)->if_data[IF_DATA_DHCP6]) -#define D6_CSTATE(ifp) \ +#define D6_STATE(ifp) ((struct dhcp6_state *)(ifp)->if_data[IF_DATA_DHCP6]) +#define D6_CSTATE(ifp) \ ((const struct dhcp6_state *)(ifp)->if_data[IF_DATA_DHCP6]) -#define D6_STATE_RUNNING(ifp) \ - (D6_CSTATE((ifp)) && \ - D6_CSTATE((ifp))->reason && dhcp6_dadcompleted((ifp))) +#define D6_STATE_RUNNING(ifp) \ + (D6_CSTATE((ifp)) && D6_CSTATE((ifp))->reason && \ + dhcp6_dadcompleted((ifp))) int dhcp6_openraw(void); int dhcp6_openudp(unsigned int, struct in6_addr *); void dhcp6_recvmsg(struct dhcpcd_ctx *, struct msghdr *, struct ipv6_addr *); -void dhcp6_printoptions(const struct dhcpcd_ctx *, - const struct dhcp_opt *, size_t); +void dhcp6_printoptions(const struct dhcpcd_ctx *, const struct dhcp_opt *, + size_t); const struct ipv6_addr *dhcp6_iffindaddr(const struct interface *ifp, const struct in6_addr *addr, unsigned int flags); struct ipv6_addr *dhcp6_findaddr(struct dhcpcd_ctx *, const struct in6_addr *, diff --git a/src/dhcpcd.c b/src/dhcpcd.c index 6299c67c..aaaa34e6 100644 --- a/src/dhcpcd.c +++ b/src/dhcpcd.c @@ -28,11 +28,11 @@ static const char dhcpcd_copyright[] = "Copyright (c) 2006-2025 Roy Marples"; +#include #include #include #include #include -#include #include #include @@ -47,22 +47,22 @@ static const char dhcpcd_copyright[] = "Copyright (c) 2006-2025 Roy Marples"; #include #include #include -#include #include +#include -#include "config.h" #include "arp.h" #include "common.h" +#include "config.h" #include "control.h" #include "dev.h" #include "dhcp-common.h" -#include "dhcpcd.h" #include "dhcp.h" #include "dhcp6.h" +#include "dhcpcd.h" #include "duid.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "ipv6.h" @@ -104,27 +104,27 @@ const char *dhcpcd_default_script = SCRIPT; static void usage(void) { - -printf("usage: "PACKAGE"\t[-146ABbDdEGgHJKLMNPpqTV]\n" - "\t\t[-C, --nohook hook] [-c, --script script]\n" - "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n" - "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n" - "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n" - "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n" - "\t\t[-O, --nooption option] [-o, --option option]\n" - "\t\t[-Q, --require option] [-r, --request address]\n" - "\t\t[-S, --static value]\n" - "\t\t[-s, --inform address[/cidr[/broadcast_address]]] [--inform6]\n" - "\t\t[-t, --timeout seconds] [-u, --userclass class]\n" - "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n" - "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n" - "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n" - "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n" - " "PACKAGE"\t-n, --rebind [interface]\n" - " "PACKAGE"\t-k, --release [interface]\n" - " "PACKAGE"\t-U, --dumplease interface\n" - " "PACKAGE"\t--version\n" - " "PACKAGE"\t-x, --exit [interface]\n"); + printf( + "usage: " PACKAGE "\t[-146ABbDdEGgHJKLMNPpqTV]\n" + "\t\t[-C, --nohook hook] [-c, --script script]\n" + "\t\t[-e, --env value] [-F, --fqdn FQDN] [-f, --config file]\n" + "\t\t[-h, --hostname hostname] [-I, --clientid clientid]\n" + "\t\t[-i, --vendorclassid vendorclassid] [-j, --logfile logfile]\n" + "\t\t[-l, --leasetime seconds] [-m, --metric metric]\n" + "\t\t[-O, --nooption option] [-o, --option option]\n" + "\t\t[-Q, --require option] [-r, --request address]\n" + "\t\t[-S, --static value]\n" + "\t\t[-s, --inform address[/cidr[/broadcast_address]]] [--inform6]\n" + "\t\t[-t, --timeout seconds] [-u, --userclass class]\n" + "\t\t[-v, --vendor code, value] [-W, --whitelist address[/cidr]] [-w]\n" + "\t\t[--waitip [4 | 6]] [-y, --reboot seconds]\n" + "\t\t[-X, --blacklist address[/cidr]] [-Z, --denyinterfaces pattern]\n" + "\t\t[-z, --allowinterfaces pattern] [--inactive] [interface] [...]\n" + " " PACKAGE "\t-n, --rebind [interface]\n" + " " PACKAGE "\t-k, --release [interface]\n" + " " PACKAGE "\t-U, --dumplease interface\n" + " " PACKAGE "\t--version\n" + " " PACKAGE "\t-x, --exit [interface]\n"); } static void @@ -153,8 +153,7 @@ free_globals(struct dhcpcd_ctx *ctx) #ifdef INET if (ctx->dhcp_opts) { - for (opt = ctx->dhcp_opts; - ctx->dhcp_opts_len > 0; + for (opt = ctx->dhcp_opts; ctx->dhcp_opts_len > 0; opt++, ctx->dhcp_opts_len--) free_dhcp_opt_embenc(opt); free(ctx->dhcp_opts); @@ -163,8 +162,7 @@ free_globals(struct dhcpcd_ctx *ctx) #endif #ifdef INET6 if (ctx->nd_opts) { - for (opt = ctx->nd_opts; - ctx->nd_opts_len > 0; + for (opt = ctx->nd_opts; ctx->nd_opts_len > 0; opt++, ctx->nd_opts_len--) free_dhcp_opt_embenc(opt); free(ctx->nd_opts); @@ -172,8 +170,7 @@ free_globals(struct dhcpcd_ctx *ctx) } #ifdef DHCP6 if (ctx->dhcp6_opts) { - for (opt = ctx->dhcp6_opts; - ctx->dhcp6_opts_len > 0; + for (opt = ctx->dhcp6_opts; ctx->dhcp6_opts_len > 0; opt++, ctx->dhcp6_opts_len--) free_dhcp_opt_embenc(opt); free(ctx->dhcp6_opts); @@ -182,8 +179,7 @@ free_globals(struct dhcpcd_ctx *ctx) #endif #endif if (ctx->vivso) { - for (opt = ctx->vivso; - ctx->vivso_len > 0; + for (opt = ctx->vivso; ctx->vivso_len > 0; opt++, ctx->vivso_len--) free_dhcp_opt_embenc(opt); free(ctx->vivso); @@ -216,7 +212,6 @@ handle_exit_timeout(void *arg) static const char * dhcpcd_af(int af) { - switch (af) { case AF_UNSPEC: return "IP"; @@ -238,12 +233,11 @@ dhcpcd_ifafwaiting(const struct interface *ifp) if (ifp->active != IF_ACTIVE_USER) return AF_MAX; -#define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6) +#define DHCPCD_WAITALL (DHCPCD_WAITIP4 | DHCPCD_WAITIP6) opts = ifp->options->options; #ifdef INET if (opts & DHCPCD_WAITIP4 || - (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) - { + (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) { bool foundaddr = ipv4_hasaddr(ifp); if (opts & DHCPCD_WAITIP4 && !foundaddr) @@ -254,8 +248,7 @@ dhcpcd_ifafwaiting(const struct interface *ifp) #endif #ifdef INET6 if (opts & DHCPCD_WAITIP6 || - (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) - { + (opts & DHCPCD_WAITIP && !(opts & DHCPCD_WAITALL))) { bool foundaddr = ipv6_hasaddr(ifp); if (opts & DHCPCD_WAITIP6 && !foundaddr) @@ -314,15 +307,14 @@ dhcpcd_ipwaited(struct dhcpcd_ctx *ctx) TAILQ_FOREACH(ifp, ctx->ifaces, next) { if ((af = dhcpcd_ifafwaiting(ifp)) != AF_MAX) { - logdebugx("%s: waiting for an %s address", - ifp->name, dhcpcd_af(af)); + logdebugx("%s: waiting for an %s address", ifp->name, + dhcpcd_af(af)); return 0; } } if ((af = dhcpcd_afwaiting(ctx)) != AF_MAX) { - logdebugx("waiting for an %s address", - dhcpcd_af(af)); + logdebugx("waiting for an %s address", dhcpcd_af(af)); return 0; } @@ -367,8 +359,7 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx) int exit_code; if (ctx->options & DHCPCD_DAEMONISE && - !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_BACKGROUND))) - { + !(ctx->options & (DHCPCD_DAEMONISED | DHCPCD_BACKGROUND))) { if (!dhcpcd_ipwaited(ctx)) return; } @@ -403,7 +394,6 @@ dhcpcd_daemonise(struct dhcpcd_ctx *ctx) static void dhcpcd_drop_af(struct interface *ifp, int stop, int af) { - if (af == AF_UNSPEC || af == AF_INET6) { #ifdef DHCP6 dhcp6_drop(ifp, stop ? NULL : "EXPIRE6"); @@ -434,14 +424,12 @@ dhcpcd_drop_af(struct interface *ifp, int stop, int af) static void dhcpcd_drop(struct interface *ifp, int stop) { - dhcpcd_drop_af(ifp, stop, AF_UNSPEC); } static bool dhcpcd_ifrunning(struct interface *ifp) { - #ifdef INET if (D_CSTATE(ifp) != NULL) return true; @@ -463,8 +451,7 @@ dhcpcd_dropped(struct interface *ifp) struct dhcpcd_ctx *ctx = ifp->ctx; if (ifp->options == NULL || - !(ifp->options->options & DHCPCD_STOPPING) || - dhcpcd_ifrunning(ifp)) + !(ifp->options->options & DHCPCD_STOPPING) || dhcpcd_ifrunning(ifp)) return; /* De-activate the interface */ @@ -490,7 +477,6 @@ dhcpcd_dropped(struct interface *ifp) static void stop_interface(struct interface *ifp) { - loginfox("%s: removing interface", ifp->name); ifp->options->options |= DHCPCD_STOPPING; dhcpcd_drop(ifp, 1); @@ -522,10 +508,8 @@ configure_interface1(struct interface *ifp) #ifdef INET6 /* We want to setup INET6 on the interface as soon as possible. */ - if (ifp->active == IF_ACTIVE_USER && - ifo->options & DHCPCD_IPV6 && - !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) - { + if (ifp->active == IF_ACTIVE_USER && ifo->options & DHCPCD_IPV6 && + !(ifp->ctx->options & (DHCPCD_DUMPLEASE | DHCPCD_TEST))) { /* If not doing any DHCP, disable the RDNSS requirement. */ if (!(ifo->options & (DHCPCD_DHCP | DHCPCD_DHCP6))) ifo->options &= ~DHCPCD_IPV6RA_REQRDNSS; @@ -606,8 +590,7 @@ configure_interface1(struct interface *ifp) #ifdef DHCP6 if (ifo->ia_len == 0 && ifo->options & DHCPCD_IPV6 && - ifp->name[0] != '\0') - { + ifp->name[0] != '\0') { ifo->ia = malloc(sizeof(*ifo->ia)); if (ifo->ia == NULL) logerr(__func__); @@ -650,8 +633,8 @@ dhcpcd_selectprofile(struct interface *ifp, const char *profile) if (ifp->ssid_len) { ssize_t r; - r = print_string(pssid, sizeof(pssid), OT_ESCSTRING, - ifp->ssid, ifp->ssid_len); + r = print_string(pssid, sizeof(pssid), OT_ESCSTRING, ifp->ssid, + ifp->ssid_len); if (r == -1) { logerr(__func__); pssid[0] = '\0'; @@ -672,8 +655,8 @@ dhcpcd_selectprofile(struct interface *ifp, const char *profile) free_options(ifp->ctx, ifp->options); ifp->options = ifo; if (profile) { - add_options(ifp->ctx, ifp->name, ifp->options, - ifp->ctx->argc, ifp->ctx->argv); + add_options(ifp->ctx, ifp->name, ifp->options, ifp->ctx->argc, + ifp->ctx->argv); configure_interface1(ifp); } return 1; @@ -698,8 +681,7 @@ configure_interface(struct interface *ifp, int argc, char **argv, /* If the mtime has changed drop any old lease */ if (old != 0 && ifp->options->mtime != old) { - logwarnx("%s: config file changed, expiring leases", - ifp->name); + logwarnx("%s: config file changed, expiring leases", ifp->name); dhcpcd_drop(ifp, 0); } } @@ -728,11 +710,8 @@ dhcpcd_initstate1(struct interface *ifp, int argc, char **argv, static void dhcpcd_initstate(struct interface *ifp, unsigned long long options) { - - dhcpcd_initstate1(ifp, - ifp->argv ? ifp->argc : ifp->ctx->argc, - ifp->argv ? ifp->argv : ifp->ctx->argv, - options); + dhcpcd_initstate1(ifp, ifp->argv ? ifp->argc : ifp->ctx->argc, + ifp->argv ? ifp->argv : ifp->ctx->argv, options); } static void @@ -740,9 +719,8 @@ dhcpcd_reportssid(struct interface *ifp) { char pssid[IF_SSIDLEN * 4]; - if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, - ifp->ssid, ifp->ssid_len) == -1) - { + if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, ifp->ssid, + ifp->ssid_len) == -1) { logerr(__func__); return; } @@ -753,7 +731,6 @@ dhcpcd_reportssid(struct interface *ifp) static void dhcpcd_nocarrier_roaming(struct interface *ifp) { - loginfox("%s: carrier lost - roaming", ifp->name); #ifdef ARP @@ -860,8 +837,8 @@ dhcpcd_handlecarrier(struct interface *ifp, int carrier, unsigned int flags) /* If we changed SSID network, drop leases */ if ((ifp->ssid_len != olen || - memcmp(ifp->ssid, ossid, ifp->ssid_len)) && ifp->active) - { + memcmp(ifp->ssid, ossid, ifp->ssid_len)) && + ifp->active) { dhcpcd_reportssid(ifp); dhcpcd_drop(ifp, 0); #ifdef IPV4LL @@ -905,7 +882,7 @@ warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid) continue; if (ia_type == 0 && memcmp(ifn->options->iaid, iaid, - sizeof(ifn->options->iaid)) == 0) + sizeof(ifn->options->iaid)) == 0) break; #ifdef INET6 for (i = 0; i < ifn->options->ia_len; i++) { @@ -919,8 +896,8 @@ warn_iaid_conflict(struct interface *ifp, uint16_t ia_type, uint8_t *iaid) /* This is only a problem if the interfaces are on the same network. */ if (ifn) - logerrx("%s: IAID conflicts with one assigned to %s", - ifp->name, ifn->name); + logerrx("%s: IAID conflicts with one assigned to %s", ifp->name, + ifn->name); } static void @@ -955,8 +932,7 @@ dhcpcd_startinterface(void *arg) } if (ifo->options & (DHCPCD_DUID | DHCPCD_IPV6) && - !(ifo->options & DHCPCD_ANONYMOUS)) - { + !(ifo->options & DHCPCD_ANONYMOUS)) { char buf[sizeof(ifo->iaid) * 3]; #ifdef INET6 size_t i; @@ -968,18 +944,18 @@ dhcpcd_startinterface(void *arg) /* Report IAIDs */ loginfox("%s: IAID %s", ifp->name, - hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid), - buf, sizeof(buf))); + hwaddr_ntoa(ifo->iaid, sizeof(ifo->iaid), buf, + sizeof(buf))); warn_iaid_conflict(ifp, 0, ifo->iaid); #ifdef INET6 for (i = 0; i < ifo->ia_len; i++) { ia = &ifo->ia[i]; if (memcmp(ifo->iaid, ia->iaid, sizeof(ifo->iaid))) { - loginfox("%s: IA type %u IAID %s", - ifp->name, ia->ia_type, - hwaddr_ntoa(ia->iaid, sizeof(ia->iaid), - buf, sizeof(buf))); + loginfox("%s: IA type %u IAID %s", ifp->name, + ia->ia_type, + hwaddr_ntoa(ia->iaid, sizeof(ia->iaid), buf, + sizeof(buf))); warn_iaid_conflict(ifp, ia->ia_type, ia->iaid); } } @@ -1041,19 +1017,16 @@ dhcpcd_prestartinterface(void *arg) struct dhcpcd_ctx *ctx = ifp->ctx; bool randmac_down; - if (ifp->carrier <= LINK_DOWN && - ifp->options->randomise_hwaddr && - ifp->flags & IFF_UP) - { + if (ifp->carrier <= LINK_DOWN && ifp->options->randomise_hwaddr && + ifp->flags & IFF_UP) { if_down(ifp); randmac_down = true; } else randmac_down = false; if ((!(ctx->options & DHCPCD_MANAGER) || - ifp->options->options & DHCPCD_IF_UP || randmac_down) && - !(ifp->flags & IFF_UP)) - { + ifp->options->options & DHCPCD_IF_UP || randmac_down) && + !(ifp->flags & IFF_UP)) { if (ifp->options->randomise_hwaddr && if_randomisemac(ifp) == -1) logerr(__func__); @@ -1067,7 +1040,6 @@ dhcpcd_prestartinterface(void *arg) static void run_preinit(struct interface *ifp) { - if (ifp->ctx->options & DHCPCD_TEST) return; @@ -1082,7 +1054,6 @@ run_preinit(struct interface *ifp) void dhcpcd_activateinterface(struct interface *ifp, unsigned long long options) { - if (ifp->active) return; @@ -1108,7 +1079,7 @@ dhcpcd_handleinterface(void *arg, int action, const char *ifname) struct ifaddrs *ifaddrs; struct if_head *ifs; struct interface *ifp, *iff; - const char * const argv[] = { ifname }; + const char *const argv[] = { ifname }; int e; if (action == -1) { @@ -1227,8 +1198,8 @@ dhcpcd_setlinkrcvbuf(struct dhcpcd_ctx *ctx) ctx->link_rcvbuf); socklen = sizeof(ctx->link_rcvbuf); - if (setsockopt(ctx->link_fd, SOL_SOCKET, - SO_RCVBUF, &ctx->link_rcvbuf, socklen) == -1) + if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RCVBUF, &ctx->link_rcvbuf, + socklen) == -1) logerr(__func__); } #endif @@ -1255,8 +1226,8 @@ dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) struct interface *ifp, *ifn, *ifp1; socklen = sizeof(rcvbuflen); - if (getsockopt(ctx->link_fd, SOL_SOCKET, - SO_RCVBUF, &rcvbuflen, &socklen) == -1) { + if (getsockopt(ctx->link_fd, SOL_SOCKET, SO_RCVBUF, &rcvbuflen, + &socklen) == -1) { logerr("%s: getsockopt", __func__); rcvbuflen = 0; } @@ -1266,7 +1237,8 @@ dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) #endif logerrx("route socket overflowed (rcvbuflen %d)" - " - learning interface state", rcvbuflen); + " - learning interface state", + rcvbuflen); /* Drain the socket. * We cannot open a new one due to privsep. */ @@ -1294,7 +1266,7 @@ dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) } /* Add new interfaces */ - while ((ifp = TAILQ_FIRST(ifaces)) != NULL ) { + while ((ifp = TAILQ_FIRST(ifaces)) != NULL) { TAILQ_REMOVE(ifaces, ifp, next); ifp1 = if_find(ctx->ifaces, ifp->name); if (ifp1 != NULL) { @@ -1322,8 +1294,8 @@ dhcpcd_linkoverflow(struct dhcpcd_ctx *ctx) } void -dhcpcd_handlehwaddr(struct interface *ifp, - uint16_t hwtype, const void *hwaddr, uint8_t hwlen) +dhcpcd_handlehwaddr(struct interface *ifp, uint16_t hwtype, const void *hwaddr, + uint8_t hwlen) { char buf[sizeof(ifp->hwaddr) * 3]; @@ -1339,7 +1311,8 @@ dhcpcd_handlehwaddr(struct interface *ifp, if (ifp->hwtype != hwtype) { if (ifp->active) loginfox("%s: hardware address type changed" - " from %d to %d", ifp->name, ifp->hwtype, hwtype); + " from %d to %d", + ifp->name, ifp->hwtype, hwtype); ifp->hwtype = hwtype; } @@ -1402,8 +1375,8 @@ reload_config(struct dhcpcd_ctx *ctx) } static void -reconf_reboot(struct dhcpcd_ctx *ctx, const bool reboot, - const int argc, char **argv, const int oi) +reconf_reboot(struct dhcpcd_ctx *ctx, const bool reboot, const int argc, + char **argv, const int oi) { int i; struct interface *ifp; @@ -1439,7 +1412,7 @@ reconf_reboot(struct dhcpcd_ctx *ctx, const bool reboot, } ifp->argc = argc; } else { -alloc_args_err: + alloc_args_err: ifp->argv = NULL; ifp->argc = 0; } @@ -1480,7 +1453,6 @@ stop_all_interfaces(struct dhcpcd_ctx *ctx, unsigned long long opts) static void dhcpcd_ifrenew(struct interface *ifp) { - if (!ifp->active) return; @@ -1524,8 +1496,7 @@ dhcpcd_signal_cb(int sig, void *arg) return; } - if (sig != SIGCHLD && ctx->options & DHCPCD_FORKED) - { + if (sig != SIGCHLD && ctx->options & DHCPCD_FORKED) { if (ctx->fork_fd != -1 && sig != SIGHUP && send(ctx->fork_fd, &sig, sizeof(sig), MSG_EOR) == -1) logerr("%s: send", __func__); @@ -1595,9 +1566,7 @@ dhcpcd_signal_cb(int sig, void *arg) return; ctx->options |= DHCPCD_EXITING; - if (!(ctx->options & DHCPCD_TEST) && - stop_all_interfaces(ctx, opts)) - { + if (!(ctx->options & DHCPCD_TEST) && stop_all_interfaces(ctx, opts)) { /* We stopped something, we will exit once that is done. */ return; } @@ -1607,8 +1576,8 @@ dhcpcd_signal_cb(int sig, void *arg) #endif int -dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, - int argc, char **argv) +dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, int argc, + char **argv) { struct interface *ifp; struct if_options *ifo; @@ -1624,8 +1593,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, * write callback on the fd */ /* Make any change here in privsep-control.c as well. */ if (strcmp(*argv, "--version") == 0) { - return control_queue(fd, UNCONST(VERSION), - strlen(VERSION) + 1); + return control_queue(fd, UNCONST(VERSION), strlen(VERSION) + 1); } else if (strcmp(*argv, "--getconfigfile") == 0) { return control_queue(fd, UNCONST(fd->ctx->cffile), strlen(fd->ctx->cffile) + 1); @@ -1660,8 +1628,8 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, oi = 0; opts = 0; do_reboot = do_renew = false; - while ((opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) - { + while ( + (opt = getopt_long(argc, argv, IF_OPTS, cf_options, &oi)) != -1) { switch (opt) { case 'g': /* Assumed if below not set */ @@ -1698,7 +1666,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, if (opts & DHCPCD_DUMPLEASE) { ctx->options |= DHCPCD_DUMPLEASE; -dumplease: + dumplease: nifaces = 0; TAILQ_FOREACH(ifp, ctx->ifaces, next) { if (!ifp->active) @@ -1730,7 +1698,7 @@ dhcpcd_handleargs(struct dhcpcd_ctx *ctx, struct fd_list *fd, } ctx->options &= ~DHCPCD_DUMPLEASE; return 0; -dumperr: + dumperr: ctx->options &= ~DHCPCD_DUMPLEASE; return -1; } @@ -1832,7 +1800,7 @@ dhcpcd_readdump2(void *arg, unsigned short events) if (--ctx->ctl_extra != 0) { putchar('\n'); if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, - dhcpcd_readdump1, ctx) == -1) + dhcpcd_readdump1, ctx) == -1) logerr("%s: eloop_event_add", __func__); return; } @@ -1870,7 +1838,7 @@ dhcpcd_readdump1(void *arg, unsigned short events) ctx->ctl_bufpos = 0; if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, - dhcpcd_readdump2, ctx) == -1) + dhcpcd_readdump2, ctx) == -1) logerr("%s: eloop_event_add", __func__); return; @@ -1903,7 +1871,7 @@ dhcpcd_readdump0(void *arg, unsigned short events) } if (eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, - dhcpcd_readdump1, ctx) == -1) + dhcpcd_readdump1, ctx) == -1) logerr("%s: eloop_event_add", __func__); } @@ -1919,10 +1887,9 @@ dhcpcd_readdumptimeout(void *arg) static int dhcpcd_readdump(struct dhcpcd_ctx *ctx) { - - ctx->options |= DHCPCD_FORKED; - if (eloop_timeout_add_sec(ctx->eloop, 5, - dhcpcd_readdumptimeout, ctx) == -1) + ctx->options |= DHCPCD_FORKED; + if (eloop_timeout_add_sec(ctx->eloop, 5, dhcpcd_readdumptimeout, ctx) == + -1) return -1; return eloop_event_add(ctx->eloop, ctx->control_fd, ELE_READ, dhcpcd_readdump0, ctx); @@ -1957,8 +1924,8 @@ dhcpcd_fork_cb(void *arg, unsigned short events) return; } if ((size_t)len < sizeof(exit_code)) { - logerrx("%s: truncated read %zd (expected %zu)", - __func__, len, sizeof(exit_code)); + logerrx("%s: truncated read %zd (expected %zu)", __func__, len, + sizeof(exit_code)); eloop_exit(ctx->eloop, EXIT_FAILURE); return; } @@ -1981,8 +1948,8 @@ dhcpcd_pidfile_timeout(void *arg) if (pid == -1) eloop_exit(ctx->eloop, EXIT_SUCCESS); else - eloop_timeout_add_msec(ctx->eloop, 100, - dhcpcd_pidfile_timeout, ctx); + eloop_timeout_add_msec(ctx->eloop, 100, dhcpcd_pidfile_timeout, + ctx); } static void @@ -2000,7 +1967,8 @@ dhcpcd_exit_timeout(void *arg) } } -static int dup_null(int fd) +static int +dup_null(int fd) { int fd_null = open(_PATH_DEVNULL, O_WRONLY); int err; @@ -2049,33 +2017,34 @@ main(int argc, char **argv, char **envp) usage(); return EXIT_SUCCESS; } else if (strcmp(argv[1], "--version") == 0) { - printf(""PACKAGE" "VERSION"\n%s\n", dhcpcd_copyright); + printf("" PACKAGE " " VERSION "\n%s\n", + dhcpcd_copyright); printf("Compiled in features:" #ifdef INET - " INET" + " INET" #endif #ifdef ARP - " ARP" + " ARP" #endif #ifdef ARPING - " ARPing" + " ARPing" #endif #ifdef IPV4LL - " IPv4LL" + " IPv4LL" #endif #ifdef INET6 - " INET6" + " INET6" #endif #ifdef DHCP6 - " DHCPv6" + " DHCPv6" #endif #ifdef AUTH - " AUTH" + " AUTH" #endif #ifdef PRIVSEP - " PRIVSEP" + " PRIVSEP" #endif - "\n"); + "\n"); return EXIT_SUCCESS; } } @@ -2119,21 +2088,21 @@ main(int argc, char **argv, char **envp) /* Ensure we have stdin, stdout and stderr file descriptors. * This is important as we do run scripts which expect these. */ - if (fcntl(STDIN_FILENO, F_GETFD) == -1) + if (fcntl(STDIN_FILENO, F_GETFD) == -1) dup_null(STDIN_FILENO); - if (fcntl(STDOUT_FILENO, F_GETFD) == -1) + if (fcntl(STDOUT_FILENO, F_GETFD) == -1) dup_null(STDOUT_FILENO); - if (fcntl(STDERR_FILENO, F_GETFD) == -1) + if (fcntl(STDERR_FILENO, F_GETFD) == -1) dup_null(STDERR_FILENO); else logopts |= LOGERR_ERR; i = 0; - while ((opt = getopt_long(argc, argv, - ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, - cf_options, &oi)) != -1) - { + while ( + (opt = getopt_long(argc, argv, + ctx.options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, + cf_options, &oi)) != -1) { switch (opt) { case '4': family = AF_INET; @@ -2239,19 +2208,19 @@ main(int argc, char **argv, char **envp) #ifdef INET if (family == 0 || family == AF_INET) { printf("\nDHCPv4 options:\n"); - dhcp_printoptions(&ctx, - ifo->dhcp_override, ifo->dhcp_override_len); + dhcp_printoptions(&ctx, ifo->dhcp_override, + ifo->dhcp_override_len); } #endif #ifdef INET6 if (family == 0 || family == AF_INET6) { printf("\nND options:\n"); - ipv6nd_printoptions(&ctx, - ifo->nd_override, ifo->nd_override_len); + ipv6nd_printoptions(&ctx, ifo->nd_override, + ifo->nd_override_len); #ifdef DHCP6 printf("\nDHCPv6 options:\n"); - dhcp6_printoptions(&ctx, - ifo->dhcp6_override, ifo->dhcp6_override_len); + dhcp6_printoptions(&ctx, ifo->dhcp6_override, + ifo->dhcp6_override_len); #endif } #endif @@ -2273,7 +2242,7 @@ main(int argc, char **argv, char **envp) #endif if (!(ctx.options & (DHCPCD_TEST | DHCPCD_DUMPLEASE))) { -printpidfile: + printpidfile: /* If we have any other args, we should run as a single dhcpcd * instance for that interface. */ if (optind == argc - 1 && !(ctx.options & DHCPCD_MANAGER)) { @@ -2287,7 +2256,7 @@ main(int argc, char **argv, char **envp) goto exit_failure; } /* Allow a dhcpcd interface per address family */ - switch(family) { + switch (family) { case AF_INET: per = "-4"; break; @@ -2297,11 +2266,11 @@ main(int argc, char **argv, char **envp) default: per = ""; } - snprintf(ctx.pidfile, sizeof(ctx.pidfile), - PIDFILE, ifname, per, "."); + snprintf(ctx.pidfile, sizeof(ctx.pidfile), PIDFILE, + ifname, per, "."); } else { - snprintf(ctx.pidfile, sizeof(ctx.pidfile), - PIDFILE, "", "", ""); + snprintf(ctx.pidfile, sizeof(ctx.pidfile), PIDFILE, "", + "", ""); ctx.options |= DHCPCD_MANAGER; /* @@ -2333,10 +2302,8 @@ main(int argc, char **argv, char **envp) signal(dhcpcd_signals_ignore[si], SIG_IGN); /* Save signal mask, block and redirect signals to our handler */ - if (eloop_signal_set_cb(ctx.eloop, - dhcpcd_signals, dhcpcd_signals_len, - dhcpcd_signal_cb, &ctx) == -1) - { + if (eloop_signal_set_cb(ctx.eloop, dhcpcd_signals, dhcpcd_signals_len, + dhcpcd_signal_cb, &ctx) == -1) { logerr("%s: eloop_signal_set_cb", __func__); goto exit_failure; } @@ -2364,7 +2331,7 @@ main(int argc, char **argv, char **envp) loginfox("waiting for pid %d to exit", (int)pid); dhcpcd_pidfile_timeout(&ctx); eloop_timeout_add_sec(ctx.eloop, 50, - dhcpcd_exit_timeout, &ctx); + dhcpcd_exit_timeout, &ctx); goto run_loop; } } @@ -2372,7 +2339,8 @@ main(int argc, char **argv, char **envp) #ifdef HAVE_OPENSSL OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | - OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, NULL); + OPENSSL_INIT_ADD_ALL_DIGESTS | OPENSSL_INIT_LOAD_CONFIG, + NULL); #endif #ifdef PRIVSEP @@ -2380,9 +2348,8 @@ main(int argc, char **argv, char **envp) #endif #ifndef SMALL - if (ctx.options & DHCPCD_DUMPLEASE && - ctx.ifc == 1 && ctx.ifv[0][0] == '-' && ctx.ifv[0][1] == '\0') - { + if (ctx.options & DHCPCD_DUMPLEASE && ctx.ifc == 1 && + ctx.ifv[0][0] == '-' && ctx.ifv[0][1] == '\0') { ctx.options |= DHCPCD_FORKED; /* pretend child process */ #ifdef PRIVSEP if (IN_PRIVSEP(&ctx) && ps_managersandbox(&ctx, NULL) == -1) @@ -2463,11 +2430,10 @@ main(int argc, char **argv, char **envp) /* If asking dhcpcd to exit and we failed to * send a signal or a message then we * don't proceed past here. */ - if (ctx.options & DHCPCD_DUMPLEASE || - sig == SIGTERM || sig == SIGALRM) - { + if (ctx.options & DHCPCD_DUMPLEASE || sig == SIGTERM || + sig == SIGALRM) { if (errno == ENOENT) - logerrx(PACKAGE" is not running"); + logerrx(PACKAGE " is not running"); goto exit_failure; } if (errno == EPERM || errno == EACCES) @@ -2484,8 +2450,8 @@ main(int argc, char **argv, char **envp) logerr("%s: mkdir: %s", __func__, RUNDIR); if ((pid = pidfile_lock(ctx.pidfile)) != 0) { if (pid == -1) - logerr("%s: pidfile_lock: %s", - __func__, ctx.pidfile); + logerr("%s: pidfile_lock: %s", __func__, + ctx.pidfile); else logerrx(PACKAGE " already running on pid %d (%s)", @@ -2503,7 +2469,8 @@ main(int argc, char **argv, char **envp) if (!(ctx.options & DHCPCD_DAEMONISE)) goto start_manager; - if (xsocketpair(AF_UNIX, SOCK_SEQPACKET|SOCK_CXNB, 0, fork_fd) == -1) { + if (xsocketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CXNB, 0, fork_fd) == + -1) { logerr("socketpair"); goto exit_failure; } @@ -2542,8 +2509,7 @@ main(int argc, char **argv, char **envp) goto exit_failure; } if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ, - dhcpcd_fork_cb, &ctx) == -1) - { + dhcpcd_fork_cb, &ctx) == -1) { logerr("%s: eloop_event_add", __func__); goto exit_failure; } @@ -2565,7 +2531,7 @@ main(int argc, char **argv, char **envp) } #endif if (eloop_event_add(ctx.eloop, ctx.fork_fd, ELE_READ, - dhcpcd_fork_cb, &ctx) == -1) + dhcpcd_fork_cb, &ctx) == -1) logerr("%s: eloop_event_add", __func__); #ifdef PRIVSEP @@ -2614,9 +2580,8 @@ main(int argc, char **argv, char **envp) if (!(ctx.options & DHCPCD_TEST)) { if (control_start(&ctx, - ctx.options & DHCPCD_MANAGER ? - NULL : argv[optind], family) == -1) - { + ctx.options & DHCPCD_MANAGER ? NULL : argv[optind], + family) == -1) { logerr("%s: control_start", __func__); goto exit_failure; } @@ -2627,7 +2592,7 @@ main(int argc, char **argv, char **envp) * change the interface name provided by the kernel */ if (!IN_PRIVSEP(&ctx) && (ctx.options & (DHCPCD_MANAGER | DHCPCD_DEV)) == - (DHCPCD_MANAGER | DHCPCD_DEV)) + (DHCPCD_MANAGER | DHCPCD_DEV)) dev_start(&ctx, dhcpcd_handleinterface); #endif @@ -2653,8 +2618,8 @@ main(int argc, char **argv, char **envp) /* Start handling kernel messages for interfaces, addresses and * routes. */ - if (eloop_event_add(ctx.eloop, ctx.link_fd, ELE_READ, - dhcpcd_handlelink, &ctx) == -1) + if (eloop_event_add(ctx.eloop, ctx.link_fd, ELE_READ, dhcpcd_handlelink, + &ctx) == -1) logerr("%s: eloop_event_add", __func__); #ifdef PRIVSEP @@ -2674,8 +2639,7 @@ main(int argc, char **argv, char **envp) } for (i = 0; i < ctx.ifc; i++) { if ((ifp = if_find(ctx.ifaces, ctx.ifv[i])) == NULL) - logerrx("%s: interface not found", - ctx.ifv[i]); + logerrx("%s: interface not found", ctx.ifv[i]); else if (!ifp->active) logerrx("%s: interface has an invalid configuration", ctx.ifv[i]); @@ -2689,8 +2653,8 @@ main(int argc, char **argv, char **envp) if (ctx.ifc == 0) { int loglevel; - loglevel = ctx.options & DHCPCD_INACTIVE ? - LOG_DEBUG : LOG_ERR; + loglevel = ctx.options & DHCPCD_INACTIVE ? LOG_DEBUG : + LOG_ERR; logmessage(loglevel, "no valid interfaces found"); dhcpcd_daemonise(&ctx); } else @@ -2733,22 +2697,19 @@ main(int argc, char **argv, char **envp) } } } - if (opt == 0 && - ctx.options & DHCPCD_LINK && - !(ctx.options & DHCPCD_WAITIP)) - { + if (opt == 0 && ctx.options & DHCPCD_LINK && + !(ctx.options & DHCPCD_WAITIP)) { int loglevel; - loglevel = ctx.options & DHCPCD_INACTIVE ? - LOG_DEBUG : LOG_WARNING; + loglevel = ctx.options & DHCPCD_INACTIVE ? LOG_DEBUG : + LOG_WARNING; logmessage(loglevel, "no interfaces have a carrier"); dhcpcd_daemonise(&ctx); } else if (t > 0 && /* Test mode removes the daemonise bit, so check for both */ - ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST)) - { - eloop_timeout_add_sec(ctx.eloop, t, - handle_exit_timeout, &ctx); + ctx.options & (DHCPCD_DAEMONISE | DHCPCD_TEST)) { + eloop_timeout_add_sec(ctx.eloop, t, handle_exit_timeout, + &ctx); } } free_options(&ctx, ifo); diff --git a/src/dhcpcd.h b/src/dhcpcd.h index 9464a902..57152b45 100644 --- a/src/dhcpcd.h +++ b/src/dhcpcd.h @@ -30,37 +30,38 @@ #define DHCPCD_H #include + #include #include #include "config.h" -#include "defs.h" #include "control.h" +#include "defs.h" #include "if-options.h" #include "queue.h" -#define HWADDR_LEN 20 -#define IF_SSIDLEN 32 -#define PROFILE_LEN 64 -#define SECRET_LEN 64 +#define HWADDR_LEN 20 +#define IF_SSIDLEN 32 +#define PROFILE_LEN 64 +#define SECRET_LEN 64 -#define IF_INACTIVE 0 -#define IF_ACTIVE 1 -#define IF_ACTIVE_USER 2 +#define IF_INACTIVE 0 +#define IF_ACTIVE 1 +#define IF_ACTIVE_USER 2 -#define LINK_UP 1 -#define LINK_UNKNOWN 0 -#define LINK_DOWN -1 +#define LINK_UP 1 +#define LINK_UNKNOWN 0 +#define LINK_DOWN -1 -#define IF_DATA_IPV4 0 -#define IF_DATA_ARP 1 -#define IF_DATA_IPV4LL 2 -#define IF_DATA_DHCP 3 -#define IF_DATA_IPV6 4 -#define IF_DATA_IPV6ND 5 -#define IF_DATA_DHCP6 6 -#define IF_DATA_MAX 7 +#define IF_DATA_IPV4 0 +#define IF_DATA_ARP 1 +#define IF_DATA_IPV4LL 2 +#define IF_DATA_DHCP 3 +#define IF_DATA_IPV6 4 +#define IF_DATA_IPV6ND 5 +#define IF_DATA_DHCP6 6 +#define IF_DATA_MAX 7 #ifdef __QNX__ /* QNX carries defines for, but does not actually support PF_LINK */ @@ -98,7 +99,7 @@ TAILQ_HEAD(if_head, interface); /* dhcpcd requires CMSG_SPACE to evaluate to a compile time constant. */ #if defined(__QNX) || \ - (defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000) + (defined(__NetBSD_Version__) && __NetBSD_Version__ < 600000000) #undef CMSG_SPACE #endif @@ -106,10 +107,10 @@ TAILQ_HEAD(if_head, interface); #define ALIGNBYTES (sizeof(int) - 1) #endif #ifndef ALIGN -#define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) +#define ALIGN(p) (((unsigned int)(p) + ALIGNBYTES) & ~ALIGNBYTES) #endif #ifndef CMSG_SPACE -#define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) +#define CMSG_SPACE(len) (ALIGN(sizeof(struct cmsghdr)) + ALIGN(len)) #endif struct passwd; @@ -117,20 +118,20 @@ struct passwd; struct dhcpcd_ctx { char pidfile[sizeof(PIDFILE) + IF_NAMESIZE + 1]; char vendor[256]; - int fork_fd; /* FD for the fork init signal pipe */ + int fork_fd; /* FD for the fork init signal pipe */ const char *cffile; unsigned long long options; char *logfile; int argc; char **argv; - int ifac; /* allowed interfaces */ - char **ifav; /* allowed interfaces */ - int ifdc; /* denied interfaces */ - char **ifdv; /* denied interfaces */ - int ifc; /* listed interfaces */ - char **ifv; /* listed interfaces */ - int ifcc; /* configured interfaces */ - char **ifcv; /* configured interfaces */ + int ifac; /* allowed interfaces */ + char **ifav; /* allowed interfaces */ + int ifdc; /* denied interfaces */ + char **ifdv; /* denied interfaces */ + int ifc; /* listed interfaces */ + char **ifv; /* listed interfaces */ + int ifcc; /* configured interfaces */ + char **ifcv; /* configured interfaces */ uint8_t duid_type; unsigned char *duid; size_t duid_len; @@ -141,11 +142,11 @@ struct dhcpcd_ctx { size_t ctl_bufpos; size_t ctl_extra; - rb_tree_t routes; /* our routes */ + rb_tree_t routes; /* our routes */ #ifdef RT_FREE_ROUTE_TABLE - rb_tree_t froutes; /* free routes for re-use */ + rb_tree_t froutes; /* free routes for re-use */ #endif - size_t rt_order; /* route order storage */ + size_t rt_order; /* route order storage */ int pf_inet_fd; #ifdef PF_LINK @@ -156,8 +157,8 @@ struct dhcpcd_ctx { #ifndef SMALL int link_rcvbuf; #endif - int seq; /* route message sequence no */ - int sseq; /* successful seq no sent */ + int seq; /* route message sequence no */ + int sseq; /* successful seq no sent */ struct eloop *eloop; @@ -191,16 +192,16 @@ struct dhcpcd_ctx { #endif #ifdef PRIVSEP - struct passwd *ps_user; /* struct passwd for privsep user */ - struct ps_process_head ps_processes; /* List of spawned processes */ + struct passwd *ps_user; /* struct passwd for privsep user */ + struct ps_process_head ps_processes; /* List of spawned processes */ struct ps_process *ps_root; struct ps_process *ps_inet; struct ps_process *ps_ctl; - int ps_data_fd; /* data returned from processes */ - int ps_log_fd; /* chroot logging */ - int ps_log_root_fd; /* outside chroot log reader */ - struct fd_list *ps_control; /* Queue for the above */ - struct fd_list *ps_control_client; /* Queue for the above */ + int ps_data_fd; /* data returned from processes */ + int ps_log_fd; /* chroot logging */ + int ps_log_root_fd; /* outside chroot log reader */ + struct fd_list *ps_control; /* Queue for the above */ + struct fd_list *ps_control_client; /* Queue for the above */ #endif #ifdef INET diff --git a/src/duid.c b/src/duid.c index 13c339c9..5d80cd2b 100644 --- a/src/duid.c +++ b/src/duid.c @@ -26,21 +26,20 @@ * SUCH DAMAGE. */ -#define UUID_LEN 36 -#define DUID_TIME_EPOCH 946684800 +#define UUID_LEN 36 +#define DUID_TIME_EPOCH 946684800 +#include #include #include -#include #ifdef BSD -# include +#include #endif -#include - #include #include +#include #include #include #include @@ -68,11 +67,11 @@ duid_machineuuid(char *uuid, size_t uuid_len) #if defined(HW_UUID) /* OpenBSD */ int mib[] = { CTL_HW, HW_UUID }; - r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0); + r = sysctl(mib, sizeof(mib) / sizeof(mib[0]), uuid, &len, NULL, 0); #elif defined(KERN_HOSTUUID) /* FreeBSD */ int mib[] = { CTL_KERN, KERN_HOSTUUID }; - r = sysctl(mib, sizeof(mib)/sizeof(mib[0]), uuid, &len, NULL, 0); + r = sysctl(mib, sizeof(mib) / sizeof(mib[0]), uuid, &len, NULL, 0); #elif defined(__NetBSD__) r = sysctlbyname("machdep.dmi.system-uuid", uuid, &len, NULL, 0); #elif defined(__linux__) @@ -216,7 +215,7 @@ duid_get(struct dhcpcd_ctx *ctx, const struct interface *ifp) } else { if (ctx->duid_type != DUID_LL) logwarnx("no interfaces have a fixed hardware " - "address"); + "address"); return duid_make(data, ifp, DUID_LL); } } @@ -240,7 +239,6 @@ duid_get(struct dhcpcd_ctx *ctx, const struct interface *ifp) size_t duid_init(struct dhcpcd_ctx *ctx, const struct interface *ifp) { - if (ctx->duid == NULL) ctx->duid_len = duid_get(ctx, ifp); return ctx->duid_len; diff --git a/src/duid.h b/src/duid.h index da6e864f..35873d76 100644 --- a/src/duid.h +++ b/src/duid.h @@ -29,11 +29,11 @@ #ifndef DUID_H #define DUID_H -#define DUID_LEN 128 + 2 -#define DUID_DEFAULT 0 -#define DUID_LLT 1 -#define DUID_LL 3 -#define DUID_UUID 4 +#define DUID_LEN 128 + 2 +#define DUID_DEFAULT 0 +#define DUID_LLT 1 +#define DUID_LL 3 +#define DUID_UUID 4 size_t duid_make(void *, const struct interface *, uint16_t); size_t duid_init(struct dhcpcd_ctx *, const struct interface *); diff --git a/src/if-bsd.c b/src/if-bsd.c index 02347e3a..d9b4213a 100644 --- a/src/if-bsd.c +++ b/src/if-bsd.c @@ -26,29 +26,30 @@ * SUCH DAMAGE. */ -#include +#include #include +#include #include #include #include #include -#include #include #include -#include "config.h" - -#include #include #include #include #include #include -#include #include #include #include #include +#include + +#include + +#include "config.h" #ifdef __NetBSD__ #include /* Needs netinet/if_ether.h */ #elif defined(__DragonFly__) @@ -57,10 +58,10 @@ #include #endif #ifdef __DragonFly__ -# include +#include #else -# include -# include +#include +#include #endif #include @@ -83,8 +84,8 @@ #include "common.h" #include "dhcp.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "ipv6.h" @@ -95,26 +96,21 @@ #include "sa.h" #ifndef RT_ROUNDUP -#define RT_ROUNDUP(a) \ +#define RT_ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) #define RT_ADVANCE(x, n) (x += RT_ROUNDUP((n)->sa_len)) #endif /* Ignore these interface names which look like ethernet but are virtual or * just won't work without explicit configuration. */ -static const char * const ifnames_ignore[] = { - "bridge", - "epair", /* Virtual patch cable */ - "fwe", /* Firewire */ - "fwip", /* Firewire */ - "tap", - "vether", - "xvif", /* XEN DOM0 -> guest interface */ - NULL -}; - -struct rtm -{ +static const char *const ifnames_ignore[] = { "bridge", + "epair", /* Virtual patch cable */ + "fwe", /* Firewire */ + "fwip", /* Firewire */ + "tap", "vether", "xvif", /* XEN DOM0 -> guest interface */ + NULL }; + +struct rtm { struct rt_msghdr hdr; char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX]; }; @@ -145,20 +141,18 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) struct priv *priv; int n; #if defined(RO_MSGFILTER) || defined(ROUTE_MSGFILTER) - unsigned char msgfilter[] = { - RTM_IFINFO, + unsigned char msgfilter[] = { RTM_IFINFO, #ifdef RTM_IFANNOUNCE - RTM_IFANNOUNCE, + RTM_IFANNOUNCE, #endif - RTM_ADD, RTM_CHANGE, RTM_DELETE, RTM_MISS, + RTM_ADD, RTM_CHANGE, RTM_DELETE, RTM_MISS, #ifdef RTM_CHGADDR - RTM_CHGADDR, + RTM_CHGADDR, #endif #ifdef RTM_DESYNC - RTM_DESYNC, + RTM_DESYNC, #endif - RTM_NEWADDR, RTM_DELADDR - }; + RTM_NEWADDR, RTM_DELADDR }; #ifdef ROUTE_MSGFILTER unsigned int i, msgfilter_mask; #endif @@ -185,7 +179,8 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) #ifdef SO_RERROR n = 1; - if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RERROR, &n,sizeof(n)) == -1) + if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RERROR, &n, sizeof(n)) == + -1) logerr("%s: SO_RERROR", __func__); #endif @@ -193,8 +188,8 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) * Sadly there is no way of doing this for route(4) messages * generated from addresses we add/delete. */ n = 0; - if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK, - &n, sizeof(n)) == -1) + if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK, &n, + sizeof(n)) == -1) logerr("%s: SO_USELOOPBACK", __func__); #ifdef PRIVSEP @@ -203,23 +198,23 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) * a small as possible buffer size. */ socklen_t smallbuf = 1; - if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RCVBUF, - &smallbuf, (socklen_t)sizeof(smallbuf)) == -1) + if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_RCVBUF, &smallbuf, + (socklen_t)sizeof(smallbuf)) == -1) logerr("%s: setsockopt(SO_RCVBUF)", __func__); } #endif #if defined(RO_MSGFILTER) - if (setsockopt(ctx->link_fd, PF_ROUTE, RO_MSGFILTER, - &msgfilter, sizeof(msgfilter)) == -1) + if (setsockopt(ctx->link_fd, PF_ROUTE, RO_MSGFILTER, &msgfilter, + sizeof(msgfilter)) == -1) logerr(__func__); #elif defined(ROUTE_MSGFILTER) /* Convert the array into a bitmask. */ msgfilter_mask = 0; for (i = 0; i < __arraycount(msgfilter); i++) msgfilter_mask |= ROUTE_FILTER(msgfilter[i]); - if (setsockopt(ctx->link_fd, PF_ROUTE, ROUTE_MSGFILTER, - &msgfilter_mask, sizeof(msgfilter_mask)) == -1) + if (setsockopt(ctx->link_fd, PF_ROUTE, ROUTE_MSGFILTER, &msgfilter_mask, + sizeof(msgfilter_mask)) == -1) logerr(__func__); #else #warning kernel does not support route message filtering @@ -284,7 +279,6 @@ if_ioctllink(struct dhcpcd_ctx *ctx, unsigned long req, void *data, size_t len) int if_setmac(struct interface *ifp, void *mac, uint8_t maclen) { - if (ifp->hwlen != maclen) { errno = EINVAL; return -1; @@ -322,7 +316,7 @@ if_setmac(struct interface *ifp, void *mac, uint8_t maclen) static bool if_ignore1(const char *drvname) { - const char * const *p; + const char *const *p; for (p = ifnames_ignore; *p; p++) { if (strcmp(*p, drvname) == 0) @@ -346,16 +340,13 @@ if_ignoregroup(int s, const char *ifname) strlcpy(ifgr.ifgr_name, ifname, sizeof(ifgr.ifgr_name)); if (ioctl(s, SIOCGIFGROUP, &ifgr) == -1 || (ifgr.ifgr_groups = malloc(ifgr.ifgr_len)) == NULL || - ioctl(s, SIOCGIFGROUP, &ifgr) == -1) - { + ioctl(s, SIOCGIFGROUP, &ifgr) == -1) { logerr(__func__); return -1; } for (ifg = ifgr.ifgr_groups, ifg_len = ifgr.ifgr_len; - ifg && ifg_len >= sizeof(*ifg); - ifg++, ifg_len -= sizeof(*ifg)) - { + ifg && ifg_len >= sizeof(*ifg); ifg++, ifg_len -= sizeof(*ifg)) { if (if_ignore1(ifg->ifgrq_group)) return 1; } @@ -380,16 +371,17 @@ if_ignore(struct dhcpcd_ctx *ctx, const char *ifname) return ps_root_ifignoregroup(ctx, ifname) == 1 ? true : false; #endif else - return if_ignoregroup(ctx->pf_inet_fd, ifname) == 1 ? - true : false; + return if_ignoregroup(ctx->pf_inet_fd, ifname) == 1 ? true : + false; #else UNUSED(ctx); return false; #endif } -static int if_indirect_ioctl(struct dhcpcd_ctx *ctx, - const char *ifname, unsigned long cmd, void *data, size_t len) +static int +if_indirect_ioctl(struct dhcpcd_ctx *ctx, const char *ifname, unsigned long cmd, + void *data, size_t len) { struct ifreq ifr = { .ifr_flags = 0 }; @@ -434,7 +426,6 @@ if_carrier(struct interface *ifp, const void *ifadata) bool if_roaming(struct interface *ifp) { - /* Check for NetBSD as a safety measure. * If other BSD's gain IN_IFF_TENTATIVE check they re-do DAD * when the carrier comes up again. */ @@ -449,7 +440,6 @@ if_roaming(struct interface *ifp) static void if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp) { - memset(sdl, 0, sizeof(*sdl)); sdl->sdl_family = AF_LINK; sdl->sdl_len = sizeof(*sdl); @@ -470,9 +460,8 @@ if_getssid1(struct dhcpcd_ctx *ctx, const char *ifname, void *ssid) #if defined(SIOCG80211NWID) /* NetBSD */ memset(&nwid, 0, sizeof(nwid)); - if (if_indirect_ioctl(ctx, ifname, SIOCG80211NWID, - &nwid, sizeof(nwid)) == 0) - { + if (if_indirect_ioctl(ctx, ifname, SIOCG80211NWID, &nwid, + sizeof(nwid)) == 0) { if (ssid == NULL) retval = nwid.i_len; else if (nwid.i_len > IF_SSIDLEN) @@ -494,7 +483,7 @@ if_getssid1(struct dhcpcd_ctx *ctx, const char *ifname, void *ssid) retval = ireq.i_len; else if (ireq.i_len > IF_SSIDLEN) errno = ENOBUFS; - else { + else { retval = ireq.i_len; memcpy(ssid, nwid, ireq.i_len); } @@ -538,8 +527,7 @@ if_vimaster(struct dhcpcd_ctx *ctx, const char *ifname) if (r == -1) return -1; if (ifmr.ifm_status & IFM_AVALID && - IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211) - { + IFM_TYPE(ifmr.ifm_active) == IFM_IEEE80211) { if (if_getssid1(ctx, ifname, NULL) == -1) return 1; } @@ -552,8 +540,8 @@ if_vlanid(const struct interface *ifp) #ifdef SIOCGETVLAN struct vlanreq vlr = { .vlr_tag = 0 }; - if (if_indirect_ioctl(ifp->ctx, ifp->name, SIOCGETVLAN, - &vlr, sizeof(vlr)) != 0) + if (if_indirect_ioctl(ifp->ctx, ifp->name, SIOCGETVLAN, &vlr, + sizeof(vlr)) != 0) return 0; /* 0 means no VLANID */ return vlr.vlr_tag; #elif defined(SIOCGVNETID) @@ -596,7 +584,6 @@ get_addrs(int type, const void *data, size_t data_len, static struct interface * if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl) { - if (sdl->sdl_index) return if_findindex(ctx->ifaces, sdl->sdl_index); @@ -612,8 +599,8 @@ if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl) TAILQ_FOREACH(ifp, ctx->ifaces, next) { if (ifp->hwlen == sdl->sdl_alen && - memcmp(ifp->hwaddr, - sdl->sdl_data, sdl->sdl_alen) == 0) + memcmp(ifp->hwaddr, sdl->sdl_data, sdl->sdl_alen) == + 0) return ifp; } } @@ -631,16 +618,14 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) } switch (sa->sa_family) { - case AF_LINK: - { + case AF_LINK: { const struct sockaddr_dl *sdl; sdl = (const void *)sa; return if_findsdl(ctx, sdl); } #ifdef INET - case AF_INET: - { + case AF_INET: { const struct sockaddr_in *sin; struct ipv4_addr *ia; @@ -653,8 +638,7 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) } #endif #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { const struct sockaddr_in6 *sin; unsigned int scope; struct ipv6_addr *ia; @@ -682,7 +666,6 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) static void if_copysa(struct sockaddr *dst, const struct sockaddr *src) { - assert(dst != NULL); assert(src != NULL); @@ -713,10 +696,11 @@ if_route(unsigned char cmd, const struct rt *rt) assert(rt->rt_ifp->ctx != NULL); ctx = rt->rt_ifp->ctx; -#define ADDSA(sa) do { \ - memcpy(bp, (sa), (sa)->sa_len); \ - bp += RT_ROUNDUP((sa)->sa_len); \ - } while (0 /* CONSTCOND */) +#define ADDSA(sa) \ + do { \ + memcpy(bp, (sa), (sa)->sa_len); \ + bp += RT_ROUNDUP((sa)->sa_len); \ + } while (0 /* CONSTCOND */) memset(&rtmsg, 0, sizeof(rtmsg)); rtm->rtm_version = RTM_VERSION; @@ -740,8 +724,7 @@ if_route(unsigned char cmd, const struct rt *rt) rtm->rtm_flags |= RTF_UP; rtm->rtm_addrs |= RTA_GATEWAY; if (!(rtm->rtm_flags & RTF_REJECT) && - !sa_is_loopback(&rt->rt_gateway)) - { + !sa_is_loopback(&rt->rt_gateway)) { rtm->rtm_index = (unsigned short)rt->rt_ifp->index; /* * OpenBSD rejects this for on-link routes when there is no default route @@ -751,9 +734,10 @@ if_route(unsigned char cmd, const struct rt *rt) */ #ifdef __OpenBSD__ #warning kernel does not allow IPv6 address sharing - if (!gateway_unspec || rt->rt_dest.sa_family!=AF_INET6) + if (!gateway_unspec || + rt->rt_dest.sa_family != AF_INET6) #endif - rtm->rtm_addrs |= RTA_IFP; + rtm->rtm_addrs |= RTA_IFP; if (!sa_is_unspecified(&rt->rt_ifa)) rtm->rtm_addrs |= RTA_IFA; } @@ -848,7 +832,6 @@ if_route(unsigned char cmd, const struct rt *rt) static bool if_realroute(const struct rt_msghdr *rtm) { - #ifdef RTF_CLONED if (rtm->rtm_flags & RTF_CLONED) return false; @@ -883,7 +866,7 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) } if (get_addrs(rtm->rtm_addrs, (const char *)rtm + sizeof(*rtm), - rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1) + rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1) return -1; memset(rt, 0, sizeof(*rt)); @@ -913,8 +896,8 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) if (rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) { const struct sockaddr_dl *sdl; - sdl = (const struct sockaddr_dl*) - (const void *)rti_info[RTAX_GATEWAY]; + sdl = (const struct sockaddr_dl *)(const void *) + rti_info[RTAX_GATEWAY]; if (sdl->sdl_alen != 0) rt->rt_dflags |= RTDF_GATELINK; } else if (rtm->rtm_flags & RTF_GATEWAY) @@ -946,14 +929,13 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) } static int -if_sysctl(struct dhcpcd_ctx *ctx, - const int *name, u_int namelen, - void *oldp, size_t *oldlenp, void *newp, size_t newlen) +if_sysctl(struct dhcpcd_ctx *ctx, const int *name, u_int namelen, void *oldp, + size_t *oldlenp, void *newp, size_t newlen) { #if defined(PRIVSEP) && defined(HAVE_CAPSICUM) if (IN_PRIVSEP(ctx)) - return (int)ps_root_sysctl(ctx, name, namelen, - oldp, oldlenp, newp, newlen); + return (int)ps_root_sysctl(ctx, name, namelen, oldp, oldlenp, + newp, newlen); #else UNUSED(ctx); #endif @@ -980,8 +962,7 @@ if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) if ((p = realloc(buf, bufl)) == NULL) goto err; buf = p; - if (if_sysctl(ctx, mib, __arraycount(mib), buf, &bufl, NULL, 0) == -1) - { + if (if_sysctl(ctx, mib, __arraycount(mib), buf, &bufl, NULL, 0) == -1) { if (errno == ENOMEM) goto again; goto err; @@ -1025,19 +1006,20 @@ if_address(unsigned char cmd, const struct ipv4_addr *ia) memset(&ifra, 0, sizeof(ifra)); strlcpy(ifra.ifra_name, ia->iface->name, sizeof(ifra.ifra_name)); -#define ADDADDR(var, addr) do { \ - (var)->sin_family = AF_INET; \ - (var)->sin_len = sizeof(*(var)); \ - (var)->sin_addr = *(addr); \ - } while (/*CONSTCOND*/0) +#define ADDADDR(var, addr) \ + do { \ + (var)->sin_family = AF_INET; \ + (var)->sin_len = sizeof(*(var)); \ + (var)->sin_addr = *(addr); \ + } while (/*CONSTCOND*/ 0) ADDADDR(&ifra.ifra_addr, &ia->addr); ADDADDR(&ifra.ifra_mask, &ia->mask); if (cmd == RTM_NEWADDR && ia->brd.s_addr != INADDR_ANY) ADDADDR(&ifra.ifra_broadaddr, &ia->brd); #undef ADDADDR - r = if_ioctl(ctx, - cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra,sizeof(ifra)); + r = if_ioctl(ctx, cmd == RTM_DELADDR ? SIOCDIFADDR : SIOCAIFADDR, &ifra, + sizeof(ifra)); return r; } @@ -1107,10 +1089,11 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) ifa.ifra_flags |= IN6_IFF_TEMPORARY; #endif -#define ADDADDR(v, addr) { \ - (v)->sin6_family = AF_INET6; \ - (v)->sin6_len = sizeof(*v); \ - (v)->sin6_addr = *(addr); \ +#define ADDADDR(v, addr) \ + { \ + (v)->sin6_family = AF_INET6; \ + (v)->sin6_len = sizeof(*v); \ + (v)->sin6_addr = *(addr); \ } ADDADDR(&ifa.ifra_addr, &ia->addr); @@ -1150,7 +1133,7 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) */ #if !((defined(__NetBSD_Version__) && __NetBSD_Version__ >= 799003600) || \ - (defined(__OpenBSD__) && OpenBSD >= 201605)) + (defined(__OpenBSD__) && OpenBSD >= 201605)) if (cmd == RTM_NEWADDR && !(ia->flags & IPV6_AF_ADDED)) { ifa.ifra_lifetime.ia6t_vltime = ND6_INFINITE_LIFETIME; ifa.ifra_lifetime.ia6t_pltime = ND6_INFINITE_LIFETIME; @@ -1176,8 +1159,8 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) #endif return if_ioctl6(ctx, - cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6, - &ifa, sizeof(ifa)); + cmd == RTM_DELADDR ? SIOCDIFADDR_IN6 : SIOCAIFADDR_IN6, &ifa, + sizeof(ifa)); } int @@ -1245,13 +1228,12 @@ if_getlifetime6(struct ipv6_addr *ia) static int if_announce(struct dhcpcd_ctx *ctx, const struct if_announcemsghdr *ifan) { - if (ifan->ifan_msglen < sizeof(*ifan)) { errno = EINVAL; return -1; } - switch(ifan->ifan_what) { + switch (ifan->ifan_what) { case IFAN_ARRIVAL: return dhcpcd_handleinterface(ctx, 1, ifan->ifan_name); case IFAN_DEPARTURE: @@ -1315,12 +1297,11 @@ if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) */ if (rt.rt_dest.sa_family == AF_INET6 && (rt.rt_flags & RTF_HOST || rtm->rtm_type == RTM_MISS) && - !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK))) - { + !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK))) { bool reachable; reachable = (rtm->rtm_type == RTM_ADD || - rtm->rtm_type == RTM_CHANGE) && + rtm->rtm_type == RTM_CHANGE) && rt.rt_dflags & RTDF_GATELINK; ipv6nd_neighbour(ctx, &rt.rt_ss_dest.sin6.sin6_addr, reachable); } @@ -1371,7 +1352,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) return 0; if (get_addrs(ifam->ifam_addrs, (const char *)ifam + sizeof(*ifam), - ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1) + ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1) return -1; /* All BSD's set IFF_UP on the interface when adding an address. @@ -1390,8 +1371,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) } switch (rti_info[RTAX_IFA]->sa_family) { - case AF_LINK: - { + case AF_LINK: { struct sockaddr_dl sdl; #ifdef RTM_CHGADDR @@ -1402,8 +1382,8 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) break; #endif memcpy(&sdl, rti_info[RTAX_IFA], rti_info[RTAX_IFA]->sa_len); - dhcpcd_handlehwaddr(ifp, ifp->hwtype, - CLLADDR(&sdl), sdl.sdl_alen); + dhcpcd_handlehwaddr(ifp, ifp->hwtype, CLLADDR(&sdl), + sdl.sdl_alen); break; } #ifdef INET @@ -1415,13 +1395,16 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) sin = (const void *)rti_info[RTAX_IFA]; addr.s_addr = sin != NULL && sin->sin_family == AF_INET ? - sin->sin_addr.s_addr : INADDR_ANY; + sin->sin_addr.s_addr : + INADDR_ANY; sin = (const void *)rti_info[RTAX_NETMASK]; mask.s_addr = sin != NULL && sin->sin_family == AF_INET ? - sin->sin_addr.s_addr : INADDR_ANY; + sin->sin_addr.s_addr : + INADDR_ANY; sin = (const void *)rti_info[RTAX_BRD]; bcast.s_addr = sin != NULL && sin->sin_family == AF_INET ? - sin->sin_addr.s_addr : INADDR_ANY; + sin->sin_addr.s_addr : + INADDR_ANY; /* * NetBSD-7 and older send an invalid broadcast address. @@ -1469,7 +1452,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) } } else #endif - if (getifaddrs(&ifaddrs) == -1) { + if (getifaddrs(&ifaddrs) == -1) { logerr("getifaddrs"); break; } @@ -1485,7 +1468,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) free(ifaddrs); else #endif - freeifaddrs(ifaddrs); + freeifaddrs(ifaddrs); if (ifam->ifam_type == RTM_DELADDR) { if (ifa != NULL) break; @@ -1501,14 +1484,13 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) flags = 0; #endif - ipv4_handleifa(ctx, ifam->ifam_type, NULL, ifp->name, - &addr, &mask, &bcast, flags, pid); + ipv4_handleifa(ctx, ifam->ifam_type, NULL, ifp->name, &addr, + &mask, &bcast, flags, pid); break; } #endif #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { struct in6_addr addr6, mask6; const struct in6_addr *dstaddr6; const struct sockaddr_in6 *sin6; @@ -1543,9 +1525,8 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) addr6.s6_addr[2] = addr6.s6_addr[3] = '\0'; #endif - ipv6_handleifa(ctx, ifam->ifam_type, NULL, - ifp->name, &addr6, ipv6_prefixlen(&mask6), - dstaddr6, flags, pid); + ipv6_handleifa(ctx, ifam->ifam_type, NULL, ifp->name, &addr6, + ipv6_prefixlen(&mask6), dstaddr6, flags, pid); break; } #endif @@ -1557,26 +1538,25 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) static int if_dispatch(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) { - if (rtm->rtm_version != RTM_VERSION) return 0; - switch(rtm->rtm_type) { + switch (rtm->rtm_type) { #ifdef RTM_IFANNOUNCE case RTM_IFANNOUNCE: return if_announce(ctx, (const void *)rtm); #endif case RTM_IFINFO: return if_ifinfo(ctx, (const void *)rtm); - case RTM_ADD: /* FALLTHROUGH */ - case RTM_CHANGE: /* FALLTHROUGH */ - case RTM_DELETE: /* FALLTHROUGH */ + case RTM_ADD: /* FALLTHROUGH */ + case RTM_CHANGE: /* FALLTHROUGH */ + case RTM_DELETE: /* FALLTHROUGH */ case RTM_MISS: return if_rtm(ctx, (const void *)rtm); #ifdef RTM_CHGADDR - case RTM_CHGADDR: /* FALLTHROUGH */ + case RTM_CHGADDR: /* FALLTHROUGH */ #endif - case RTM_DELADDR: /* FALLTHROUGH */ + case RTM_DELADDR: /* FALLTHROUGH */ case RTM_NEWADDR: return if_ifa(ctx, (const void *)rtm); #ifdef RTM_DESYNC @@ -1631,7 +1611,6 @@ if_missfilter0(struct dhcpcd_ctx *ctx, struct interface *ifp, int if_missfilter(struct interface *ifp, struct sockaddr *sa) { - return if_missfilter0(ifp->ctx, ifp, sa); } @@ -1641,8 +1620,8 @@ if_missfilter_apply(struct dhcpcd_ctx *ctx) #ifdef RO_MISSFILTER if (ctx->rt_missfilterlen == 0) { struct sockaddr sa = { - .sa_family = AF_UNSPEC, - .sa_len = sizeof(sa), + .sa_family = AF_UNSPEC, + .sa_len = sizeof(sa), }; if (if_missfilter0(ctx, NULL, &sa) == -1) @@ -1672,8 +1651,7 @@ if_handlelink(struct dhcpcd_ctx *ctx) if (len == 0) return 0; if ((size_t)len < sizeof(rtm.hdr.rtm_msglen) || - len != rtm.hdr.rtm_msglen) - { + len != rtm.hdr.rtm_msglen) { errno = EINVAL; return -1; } @@ -1689,13 +1667,13 @@ if_handlelink(struct dhcpcd_ctx *ctx) return if_dispatch(ctx, &rtm.hdr); } -#ifndef SYS_NMLN /* OSX */ -# define SYS_NMLN __SYS_NAMELEN +#ifndef SYS_NMLN /* OSX */ +#define SYS_NMLN __SYS_NAMELEN #endif #ifndef HW_MACHINE_ARCH -# ifdef HW_MODEL /* OpenBSD */ -# define HW_MACHINE_ARCH HW_MODEL -# endif +#ifdef HW_MODEL /* OpenBSD */ +#define HW_MACHINE_ARCH HW_MODEL +#endif #endif int if_machinearch(char *str, size_t len) @@ -1707,7 +1685,7 @@ if_machinearch(char *str, size_t len) #ifdef INET6 #if (defined(IPV6CTL_ACCEPT_RTADV) && !defined(ND6_IFF_ACCEPT_RTADV)) -#define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0) +#define get_inet6_sysctl(code) inet6_sysctl(code, 0, 0) #define set_inet6_sysctl(code, val) inet6_sysctl(code, val, 1) static int inet6_sysctl(int code, int val, int action) @@ -1816,9 +1794,9 @@ if_set_ifxflags(const struct interface *ifp) * dhcpcd is instructed not to do auto configuration of addresses. */ #if defined(ND6_IFF_ACCEPT_RTADV) -#define BSD_AUTOCONF DHCPCD_IPV6RS +#define BSD_AUTOCONF DHCPCD_IPV6RS #else -#define BSD_AUTOCONF DHCPCD_IPV6RA_AUTOCONF +#define BSD_AUTOCONF DHCPCD_IPV6RA_AUTOCONF #endif if (ifp->options->options & BSD_AUTOCONF) flags &= ~IFXF_AUTOCONF6; @@ -1831,12 +1809,10 @@ if_set_ifxflags(const struct interface *ifp) /* OpenBSD removed ND6 flags entirely, so we need to check for their * existance. */ -#if defined(ND6_IFF_AUTO_LINKLOCAL) || \ - defined(ND6_IFF_PERFORMNUD) || \ - defined(ND6_IFF_ACCEPT_RTADV) || \ - defined(ND6_IFF_OVERRIDE_RTADV) || \ +#if defined(ND6_IFF_AUTO_LINKLOCAL) || defined(ND6_IFF_PERFORMNUD) || \ + defined(ND6_IFF_ACCEPT_RTADV) || defined(ND6_IFF_OVERRIDE_RTADV) || \ defined(ND6_IFF_IFDISABLED) -#define ND6_NDI_FLAGS +#define ND6_NDI_FLAGS #endif void @@ -1848,9 +1824,9 @@ if_disable_rtadv(void) if (ra == -1) { if (errno != ENOENT) logerr("IPV6CTL_ACCEPT_RTADV"); - else if (ra != 0) - if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1) - logerr("IPV6CTL_ACCEPT_RTADV"); + else if (ra != 0) + if (set_inet6_sysctl(IPV6CTL_ACCEPT_RTADV, 0) == -1) + logerr("IPV6CTL_ACCEPT_RTADV"); } #endif } @@ -1903,8 +1879,8 @@ if_setup_inet6(const struct interface *ifp) if (nd.ndi.flags != (uint32_t)flags) { nd.ndi.flags = (uint32_t)flags; - if (if_ioctl6(ifp->ctx, SIOCSIFINFO_FLAGS, - &nd, sizeof(nd)) == -1) + if (if_ioctl6(ifp->ctx, SIOCSIFINFO_FLAGS, &nd, sizeof(nd)) == + -1) logerr("%s: SIOCSIFINFO_FLAGS", ifp->name); } #endif /* ND6_NDI_FLAGS */ @@ -1931,13 +1907,13 @@ if_setup_inet6(const struct interface *ifp) memset(&ifr, 0, sizeof(ifr)); strlcpy(ifr.ifr_name, ifp->name, sizeof(ifr.ifr_name)); - if (if_ioctl6(ifp->ctx, SIOCSRTRFLUSH_IN6, - &ifr, sizeof(ifr)) == -1 && + if (if_ioctl6(ifp->ctx, SIOCSRTRFLUSH_IN6, &ifr, sizeof(ifr)) == + -1 && errno != ENOTSUP && errno != ENOTTY) logwarn("SIOCSRTRFLUSH_IN6 %d", errno); #ifdef SIOCSPFXFLUSH_IN6 - if (if_ioctl6(ifp->ctx, SIOCSPFXFLUSH_IN6, - &ifr, sizeof(ifr)) == -1 && + if (if_ioctl6(ifp->ctx, SIOCSPFXFLUSH_IN6, &ifr, sizeof(ifr)) == + -1 && errno != ENOTSUP && errno != ENOTTY) logwarn("SIOCSPFXFLUSH_IN6"); #endif diff --git a/src/if-linux-wext.c b/src/if-linux-wext.c index f395fc89..25ad2d30 100644 --- a/src/if-linux-wext.c +++ b/src/if-linux-wext.c @@ -42,14 +42,14 @@ #include #include -#include #include +#include /* Support older kernels */ #ifdef IFLA_WIRELESS -# include -# include +#include +#include #else -# define IFLA_WIRELESS (IFLA_MASTER + 1) +#define IFLA_WIRELESS (IFLA_MASTER + 1) #endif #include diff --git a/src/if-linux.c b/src/if-linux.c index 7e0e3c72..276f34d1 100644 --- a/src/if-linux.c +++ b/src/if-linux.c @@ -26,30 +26,29 @@ * SUCH DAMAGE. */ -#include /* Needed for 2.4 kernels */ - #include +#include #include #include -#include #include +#include +#include +#include +#include + +#include +#include /* Needed for 2.4 kernels */ +#include #include #include #include #include #include #include -#include #include -#include #include - -#include -#include -#include -#include -#include +#include /* musl has its own definition of struct ethhdr, so only include * netinet/if_ether.h on systems with GLIBC. For the ARPHRD constants, @@ -60,10 +59,10 @@ #include #endif +#include #include #include #include -#include #include #include #include @@ -72,9 +71,9 @@ #include #include -#include "config.h" #include "bpf.h" #include "common.h" +#include "config.h" #include "dev.h" #include "dhcp.h" #include "if.h" @@ -101,7 +100,7 @@ int if_getssid_wext(const char *ifname, uint8_t *ssid); /* Buggy CentOS and RedHat */ #ifndef SOL_NETLINK -#define SOL_NETLINK 270 +#define SOL_NETLINK 270 #endif /* @@ -112,10 +111,10 @@ int if_getssid_wext(const char *ifname, uint8_t *ssid); */ #ifdef IFLA_LINKINFO #ifndef IFF_LOWER_UP -#define IFF_LOWER_UP 0x10000 +#define IFF_LOWER_UP 0x10000 #endif #ifndef IFF_DORMANT -#define IFF_DORMANT 0x20000 +#define IFF_DORMANT 0x20000 #endif #endif @@ -134,26 +133,30 @@ int if_getssid_wext(const char *ifname, uint8_t *ssid); #undef NLA_ALIGNTO #undef NLA_ALIGN #undef NLA_HDRLEN -#define NLA_ALIGNTO 4U -#define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) -#define NLA_HDRLEN ((int) NLA_ALIGN(sizeof(struct nlattr))) +#define NLA_ALIGNTO 4U +#define NLA_ALIGN(len) (((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1)) +#define NLA_HDRLEN ((int)NLA_ALIGN(sizeof(struct nlattr))) #undef IFA_RTA -#define IFA_RTA(r) ((struct rtattr *)(void *)(((char *)(r)) \ - + NLMSG_ALIGN(sizeof(struct ifaddrmsg)))) +#define IFA_RTA(r) \ + ((struct rtattr *)(void *)(((char *)(r)) + \ + NLMSG_ALIGN(sizeof(struct ifaddrmsg)))) #undef IFLA_RTA -#define IFLA_RTA(r) ((struct rtattr *)(void *)(((char *)(r)) \ - + NLMSG_ALIGN(sizeof(struct ifinfomsg)))) +#define IFLA_RTA(r) \ + ((struct rtattr *)(void *)(((char *)(r)) + \ + NLMSG_ALIGN(sizeof(struct ifinfomsg)))) #undef NLMSG_NEXT -#define NLMSG_NEXT(nlh, len) ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \ - (struct nlmsghdr *)(void *)(((char *)(nlh)) \ - + NLMSG_ALIGN((nlh)->nlmsg_len))) +#define NLMSG_NEXT(nlh, len) \ + ((len) -= NLMSG_ALIGN((nlh)->nlmsg_len), \ + (struct nlmsghdr *)(void *)(((char *)(nlh)) + \ + NLMSG_ALIGN((nlh)->nlmsg_len))) #undef RTM_RTA #define RTM_RTA(r) (void *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtmsg))) #undef RTA_NEXT -#define RTA_NEXT(rta, attrlen) ((attrlen) -= RTA_ALIGN((rta)->rta_len), \ - (struct rtattr *)(void *)(((char *)(rta)) \ - + RTA_ALIGN((rta)->rta_len))) +#define RTA_NEXT(rta, attrlen) \ + ((attrlen) -= RTA_ALIGN((rta)->rta_len), \ + (struct rtattr *)(void *)(((char *)(rta)) + \ + RTA_ALIGN((rta)->rta_len))) /* We need this to send a broadcast for InfiniBand. * Our old code used sendto, but our new code writes to a raw BPF socket. @@ -169,59 +172,58 @@ static const uint8_t ipv4_bcast_addr[] = { static int if_addressexists(struct interface *, struct in_addr *); -#define PROC_PROMOTE "/proc/sys/net/ipv4/conf/%s/promote_secondaries" -#define SYS_BRIDGE "/sys/class/net/%s/bridge/bridge_id" -#define SYS_LAYER2 "/sys/class/net/%s/device/layer2" -#define SYS_TUNTAP "/sys/class/net/%s/tun_flags" +#define PROC_PROMOTE "/proc/sys/net/ipv4/conf/%s/promote_secondaries" +#define SYS_BRIDGE "/sys/class/net/%s/bridge/bridge_id" +#define SYS_LAYER2 "/sys/class/net/%s/device/layer2" +#define SYS_TUNTAP "/sys/class/net/%s/tun_flags" #if defined(__aarch64__) static const char *mproc = "AArch64"; int if_machinearch(char *str, size_t len) { - return snprintf(str, len, "%s", mproc); } #else static const char *mproc = #if defined(__alpha__) - "system type" + "system type" #elif defined(__arm__) - "Hardware" + "Hardware" #elif defined(__avr32__) - "cpu family" + "cpu family" #elif defined(__bfin__) - "BOARD Name" + "BOARD Name" #elif defined(__cris__) - "cpu model" + "cpu model" #elif defined(__frv__) - "System" + "System" #elif defined(__hppa__) - "model" + "model" #elif defined(__i386__) || defined(__x86_64__) - "vendor_id" + "vendor_id" #elif defined(__ia64__) - "vendor" + "vendor" #elif defined(__m68k__) - "MMU" + "MMU" #elif defined(__mips__) - "system type" + "system type" #elif defined(__powerpc__) || defined(__powerpc64__) - "machine" + "machine" #elif defined(__riscv) - "uarch" + "uarch" #elif defined(__s390__) || defined(__s390x__) - "Manufacturer" + "Manufacturer" #elif defined(__sh__) - "machine" + "machine" #elif defined(sparc) || defined(__sparc__) - "cpu" + "cpu" #elif defined(__vax__) - "cpu" + "cpu" #else - NULL + NULL #endif - ; + ; int if_machinearch(char *str, size_t len) @@ -240,8 +242,7 @@ if_machinearch(char *str, size_t len) while (fscanf(fp, "%255s : ", buf) != EOF) { if (strncmp(buf, mproc, strlen(mproc)) == 0 && - fscanf(fp, "%255s", buf) == 1) - { + fscanf(fp, "%255s", buf) == 1) { fclose(fp); return snprintf(str, len, "%s", buf); } @@ -360,7 +361,6 @@ if_tap(struct dhcpcd_ctx *ctx, const char *ifname) bool if_ignore(struct dhcpcd_ctx *ctx, const char *ifname) { - if (if_tap(ctx, ifname) || if_bridge(ctx, ifname)) return true; return false; @@ -370,7 +370,6 @@ if_ignore(struct dhcpcd_ctx *ctx, const char *ifname) int if_vimaster(__unused struct dhcpcd_ctx *ctx, __unused const char *ifname) { - return 0; } @@ -479,8 +478,8 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) if (ctx->link_fd == -1) return -1; #ifdef NETLINK_BROADCAST_ERROR - if (setsockopt(ctx->link_fd, SOL_NETLINK, NETLINK_BROADCAST_ERROR, - &on, sizeof(on)) == -1) + if (setsockopt(ctx->link_fd, SOL_NETLINK, NETLINK_BROADCAST_ERROR, &on, + sizeof(on)) == -1) logerr("%s: NETLINK_BROADCAST_ERROR", __func__); #endif @@ -546,12 +545,12 @@ if_setmac(struct interface *ifp, void *mac, uint8_t maclen) return if_ioctl(ifp->ctx, SIOCSIFHWADDR, &ifr, sizeof(ifr)); } -static int if_carrier_from_flags(unsigned int flags) +static int +if_carrier_from_flags(unsigned int flags) { - #ifdef IFF_LOWER_UP return ((flags & (IFF_LOWER_UP | IFF_RUNNING)) == - (IFF_LOWER_UP | IFF_RUNNING)) + (IFF_LOWER_UP | IFF_RUNNING)) #ifdef IFF_DORMANT /* * IFF_DORMANT means L1 is up but waiting for an external @@ -561,7 +560,9 @@ static int if_carrier_from_flags(unsigned int flags) */ && !(flags & IFF_DORMANT) #endif - ? LINK_UP : LINK_DOWN; + ? + LINK_UP : + LINK_DOWN; #else return flags & IFF_RUNNING ? LINK_UP : LINK_DOWN; #endif @@ -573,25 +574,23 @@ if_carrier(struct interface *ifp, __unused const void *ifadata) return if_carrier_from_flags(ifp->flags); } - bool if_roaming(struct interface *ifp) { - return #ifdef IFF_DORMANT - ifp->flags & IFF_DORMANT || + ifp->flags & IFF_DORMANT || #endif #ifdef IFF_LOWER_UP - /* - * IFF_DORMANT only occurs for supplicant initiated roaming. - * For firmware initiated roaming we don't get IFF_DORMANT. - * Seems weird that the driver can't set it though. - * We can check that IFF_RUNNING is not set but UP and L1 are - * to get the same effect. - */ - (ifp->flags & (IFF_UP | IFF_LOWER_UP | IFF_RUNNING)) == - (IFF_UP | IFF_LOWER_UP) || + /* + * IFF_DORMANT only occurs for supplicant initiated roaming. + * For firmware initiated roaming we don't get IFF_DORMANT. + * Seems weird that the driver can't set it though. + * We can check that IFF_RUNNING is not set but UP and L1 are + * to get the same effect. + */ + (ifp->flags & (IFF_UP | IFF_LOWER_UP | IFF_RUNNING)) == + (IFF_UP | IFF_LOWER_UP) || #endif false; } @@ -602,8 +601,10 @@ if_getnetlink(struct dhcpcd_ctx *ctx, struct iovec *iov, int fd, int flags, { struct sockaddr_nl nladdr = { .nl_pid = 0 }; struct msghdr msg = { - .msg_name = &nladdr, .msg_namelen = sizeof(nladdr), - .msg_iov = iov, .msg_iovlen = 1, + .msg_name = &nladdr, + .msg_namelen = sizeof(nladdr), + .msg_iov = iov, + .msg_iovlen = 1, }; size_t len; struct nlmsghdr *nlm; @@ -628,10 +629,8 @@ if_getnetlink(struct dhcpcd_ctx *ctx, struct iovec *iov, int fd, int flags, again = 0; terminated = false; - for (nlm = iov->iov_base; - nlm && NLMSG_OK(nlm, len); - nlm = NLMSG_NEXT(nlm, len)) - { + for (nlm = iov->iov_base; nlm && NLMSG_OK(nlm, len); + nlm = NLMSG_NEXT(nlm, len)) { again = (nlm->nlmsg_flags & NLM_F_MULTI); if (nlm->nlmsg_type == NLMSG_NOOP) continue; @@ -702,21 +701,20 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm) case RTA_DST: sa = &rt->rt_dest; break; - case RTA_SRC: - { + case RTA_SRC: { union sa_ss ssa; struct sockaddr *psa = (struct sockaddr *)&ssa; socklen_t salen; psa->sa_family = rtm->rtm_family; salen = sa_addrlen(psa); - memcpy((char *)psa + sa_addroffset(psa), - RTA_DATA(rta), MIN(salen, RTA_PAYLOAD(rta))); + memcpy((char *)psa + sa_addroffset(psa), RTA_DATA(rta), + MIN(salen, RTA_PAYLOAD(rta))); /* if ip-route "from" address is not unspecified, - route is source-based, eg: - from via ... dev ... - ignore the route as may otherwise appear to overlap - with routes set/removed by dhcpcd */ + route is source-based, eg: + from via ... dev ... + ignore the route as may otherwise appear to overlap + with routes set/removed by dhcpcd */ if (!sa_is_unspecified(psa)) { errno = ENOTSUP; return -1; @@ -736,8 +734,7 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm) case RTA_PRIORITY: rt->rt_metric = *(unsigned int *)RTA_DATA(rta); break; - case RTA_METRICS: - { + case RTA_METRICS: { struct rtattr *r2; size_t l2; @@ -746,15 +743,15 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm) for (; RTA_OK(r2, l2); r2 = RTA_NEXT(r2, l2)) { switch (r2->rta_type) { case RTAX_MTU: - rt->rt_mtu = *(unsigned int *)RTA_DATA(r2); + rt->rt_mtu = *(unsigned int *)RTA_DATA( + r2); break; } } break; } #ifdef HAVE_ROUTE_LIFETIME - case RTA_CACHEINFO: - { + case RTA_CACHEINFO: { struct rta_cacheinfo ci; static long hz; @@ -803,7 +800,7 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm) if (sa_is_allones(&rt->rt_netmask)) rt->rt_flags |= RTF_HOST; - #if 0 +#if 0 if (rt->rtp_ifp == NULL && rt->src.s_addr != INADDR_ANY) { struct ipv4_addr *ap; @@ -813,7 +810,7 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, struct nlmsghdr *nlm) if ((ap = ipv4_findaddr(ctx, &rt->src))) rt->iface = ap->iface; } - #endif +#endif if (rt->rt_ifp == NULL) { errno = ESRCH; @@ -897,7 +894,7 @@ link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm) nlm->nlmsg_pid == (uint32_t)ctx->ps_root->psp_pid) return 0; #endif - priv = (struct priv*)ctx->priv; + priv = (struct priv *)ctx->priv; if (nlm->nlmsg_pid == priv->route_pid) return 0; } @@ -948,14 +945,14 @@ link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm) break; } - ipv4_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name, - &addr, &net, &brd, ifa->ifa_flags, (pid_t)nlm->nlmsg_pid); + ipv4_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name, &addr, + &net, &brd, ifa->ifa_flags, (pid_t)nlm->nlmsg_pid); break; #endif #ifdef INET6 case AF_INET6: for (; RTA_OK(rta, len); rta = RTA_NEXT(rta, len)) { - switch (rta->rta_type) { + switch (rta->rta_type) { case IFA_ADDRESS: addr6 = (struct in6_addr *)RTA_DATA(rta); break; @@ -983,9 +980,9 @@ link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm) break; } - ipv6_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name, - addr6, ifa->ifa_prefixlen, dstaddr6, - ifa->ifa_flags, (pid_t)nlm->nlmsg_pid); + ipv6_handleifa(ctx, nlm->nlmsg_type, NULL, ifp->name, addr6, + ifa->ifa_prefixlen, dstaddr6, ifa->ifa_flags, + (pid_t)nlm->nlmsg_pid); break; #endif } @@ -995,9 +992,8 @@ link_addr(struct dhcpcd_ctx *ctx, struct interface *ifp, struct nlmsghdr *nlm) static uint8_t l2addr_len(unsigned short if_type) { - switch (if_type) { - case ARPHRD_ETHER: /* FALLTHROUGH */ + case ARPHRD_ETHER: /* FALLTHROUGH */ case ARPHRD_IEEE802: /*FALLTHROUGH */ case ARPHRD_IEEE80211: return 6; @@ -1039,7 +1035,7 @@ link_neigh(struct dhcpcd_ctx *ctx, __unused struct interface *ifp, switch (rta->rta_type) { case NDA_DST: memcpy(&addr6.s6_addr, RTA_DATA(rta), - sizeof(addr6.s6_addr)); + sizeof(addr6.s6_addr)); break; } } @@ -1158,8 +1154,7 @@ link_netlink(struct dhcpcd_ctx *ctx, void *arg, struct nlmsghdr *nlm) dhcpcd_handlehwaddr(ifp, ifi->ifi_type, hwa, hwl); } - dhcpcd_handlecarrier(ifp, - if_carrier_from_flags(ifi->ifi_flags), + dhcpcd_handlecarrier(ifp, if_carrier_from_flags(ifi->ifi_flags), ifi->ifi_flags); return 0; } @@ -1181,15 +1176,14 @@ if_handlelink(struct dhcpcd_ctx *ctx) static bool if_netlinkpriv(int protocol, struct nlmsghdr *nlm) { - if (protocol != NETLINK_ROUTE) return false; - switch(nlm->nlmsg_type) { - case RTM_NEWADDR: /* FALLTHROUGH */ - case RTM_DELADDR: /* FALLTHROUGH */ - case RTM_NEWROUTE: /* FALLTHROUGH */ - case RTM_DELROUTE: /* FALLTHROUGH */ + switch (nlm->nlmsg_type) { + case RTM_NEWADDR: /* FALLTHROUGH */ + case RTM_DELADDR: /* FALLTHROUGH */ + case RTM_NEWROUTE: /* FALLTHROUGH */ + case RTM_DELROUTE: /* FALLTHROUGH */ case RTM_NEWLINK: return true; default: @@ -1205,10 +1199,10 @@ if_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct nlmsghdr *hdr, int s; struct sockaddr_nl snl = { .nl_family = AF_NETLINK }; struct iovec iov = { .iov_base = hdr, .iov_len = hdr->nlmsg_len }; - struct msghdr msg = { - .msg_name = &snl, .msg_namelen = sizeof(snl), - .msg_iov = &iov, .msg_iovlen = 1 - }; + struct msghdr msg = { .msg_name = &snl, + .msg_namelen = sizeof(snl), + .msg_iov = &iov, + .msg_iovlen = 1 }; struct priv *priv = (struct priv *)ctx->priv; unsigned char buf[16 * 1024]; struct iovec riov = { @@ -1256,7 +1250,7 @@ if_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct nlmsghdr *hdr, return if_getnetlink(ctx, &riov, s, 0, cb, cbarg); } -#define NLMSG_TAIL(nmsg) \ +#define NLMSG_TAIL(nmsg) \ ((struct rtattr *)(void *)(((char *)(nmsg)) + \ NLMSG_ALIGN((nmsg)->nlmsg_len))) @@ -1287,7 +1281,6 @@ static int add_attr_8(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, uint8_t data) { - return add_attr_l(n, maxlen, type, &data, sizeof(data)); } #endif @@ -1314,8 +1307,8 @@ add_attr_32(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, } static int -rta_add_attr_32(struct rtattr *rta, unsigned short maxlen, - unsigned short type, uint32_t data) +rta_add_attr_32(struct rtattr *rta, unsigned short maxlen, unsigned short type, + uint32_t data) { unsigned short len = RTA_LENGTH(sizeof(data)); struct rtattr *subrta; @@ -1325,7 +1318,7 @@ rta_add_attr_32(struct rtattr *rta, unsigned short maxlen, return -1; } - subrta = (void *)((char*)rta + RTA_ALIGN(rta->rta_len)); + subrta = (void *)((char *)rta + RTA_ALIGN(rta->rta_len)); subrta->rta_type = type; subrta->rta_len = len; memcpy(RTA_DATA(subrta), &data, sizeof(data)); @@ -1337,33 +1330,29 @@ rta_add_attr_32(struct rtattr *rta, unsigned short maxlen, static struct nlattr * nla_next(struct nlattr *nla, size_t *rem) { - *rem -= (size_t)NLA_ALIGN(nla->nla_len); return (void *)((char *)nla + NLA_ALIGN(nla->nla_len)); } #define NLA_TYPE(nla) ((nla)->nla_type & NLA_TYPE_MASK) -#define NLA_LEN(nla) (unsigned int)((nla)->nla_len - NLA_HDRLEN) -#define NLA_OK(nla, rem) \ +#define NLA_LEN(nla) (unsigned int)((nla)->nla_len - NLA_HDRLEN) +#define NLA_OK(nla, rem) \ ((rem) >= sizeof(struct nlattr) && \ - (nla)->nla_len >= sizeof(struct nlattr) && \ - (nla)->nla_len <= rem) + (nla)->nla_len >= sizeof(struct nlattr) && (nla)->nla_len <= rem) #define NLA_DATA(nla) (void *)((char *)(nla) + NLA_HDRLEN) -#define NLA_FOR_EACH_ATTR(pos, head, len, rem) \ - for (pos = head, rem = len; \ - NLA_OK(pos, rem); \ - pos = nla_next(pos, &(rem))) +#define NLA_FOR_EACH_ATTR(pos, head, len, rem) \ + for (pos = head, rem = len; NLA_OK(pos, rem); \ + pos = nla_next(pos, &(rem))) -struct nlmg -{ +struct nlmg { struct nlmsghdr hdr; struct genlmsghdr ghdr; char buffer[64]; }; static int -nla_put_32(struct nlmsghdr *n, unsigned short maxlen, - unsigned short type, uint32_t data) +nla_put_32(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, + uint32_t data) { unsigned short len; struct nlattr *nla; @@ -1384,8 +1373,8 @@ nla_put_32(struct nlmsghdr *n, unsigned short maxlen, } static int -nla_put_string(struct nlmsghdr *n, unsigned short maxlen, - unsigned short type, const char *data) +nla_put_string(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, + const char *data) { struct nlattr *nla; size_t len, sl; @@ -1413,7 +1402,8 @@ nla_parse(struct nlattr *tb[], struct nlattr *head, size_t len, int maxtype) int type; memset(tb, 0, sizeof(*tb) * ((unsigned int)maxtype + 1)); - NLA_FOR_EACH_ATTR(nla, head, len, rem) { + NLA_FOR_EACH_ATTR(nla, head, len, rem) + { type = NLA_TYPE(nla); if (type > maxtype) continue; @@ -1463,11 +1453,11 @@ gnl_getfamily(struct dhcpcd_ctx *ctx, const char *name) nlm.hdr.nlmsg_flags = NLM_F_REQUEST; nlm.ghdr.cmd = CTRL_CMD_GETFAMILY; nlm.ghdr.version = 1; - if (nla_put_string(&nlm.hdr, sizeof(nlm), - CTRL_ATTR_FAMILY_NAME, name) == -1) + if (nla_put_string(&nlm.hdr, sizeof(nlm), CTRL_ATTR_FAMILY_NAME, + name) == -1) return -1; - return if_sendnetlink(ctx, NETLINK_GENERIC, &nlm.hdr, - &_gnl_getfamily, NULL); + return if_sendnetlink(ctx, NETLINK_GENERIC, &nlm.hdr, &_gnl_getfamily, + NULL); } static int @@ -1487,10 +1477,8 @@ _if_getssid_nl80211(__unused struct dhcpcd_ctx *ctx, void *arg, if (tb[NL80211_ATTR_BSS] == NULL) return 0; - if (nla_parse(bss, - NLA_DATA(tb[NL80211_ATTR_BSS]), - NLA_LEN(tb[NL80211_ATTR_BSS]), - NL80211_BSS_STATUS) == -1) + if (nla_parse(bss, NLA_DATA(tb[NL80211_ATTR_BSS]), + NLA_LEN(tb[NL80211_ATTR_BSS]), NL80211_BSS_STATUS) == -1) return 0; if (bss[NL80211_BSS_BSSID] == NULL || bss[NL80211_BSS_STATUS] == NULL) @@ -1542,8 +1530,8 @@ if_getssid_nl80211(struct interface *ifp) nlm.hdr.nlmsg_flags = NLM_F_REQUEST; nlm.ghdr.cmd = NL80211_CMD_GET_WIPHY; nla_put_32(&nlm.hdr, sizeof(nlm), NL80211_ATTR_IFINDEX, ifp->index); - if (if_sendnetlink(ifp->ctx, NETLINK_GENERIC, &nlm.hdr, - NULL, NULL) == -1) + if (if_sendnetlink(ifp->ctx, NETLINK_GENERIC, &nlm.hdr, NULL, NULL) == + -1) return -1; /* We need to parse out the list of scan results and find the one @@ -1579,24 +1567,22 @@ if_getssid(struct interface *ifp) return r; } -struct nlma -{ +struct nlma { struct nlmsghdr hdr; struct ifaddrmsg ifa; char buffer[64]; }; #ifdef INET -struct ifiaddr -{ +struct ifiaddr { unsigned int ifa_ifindex; struct in_addr ifa_addr; bool ifa_found; }; static int -_if_addressexists(__unused struct dhcpcd_ctx *ctx, - void *arg, struct nlmsghdr *nlm) +_if_addressexists(__unused struct dhcpcd_ctx *ctx, void *arg, + struct nlmsghdr *nlm) { struct ifiaddr *ia = arg; in_addr_t this_addr; @@ -1633,11 +1619,11 @@ if_addressexists(struct interface *ifp, struct in_addr *addr) .ifa_found = false, }; struct nlma nlm = { - .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), - .hdr.nlmsg_type = RTM_GETADDR, - .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, - .ifa.ifa_family = AF_INET, - .ifa.ifa_index = ifp->index, + .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), + .hdr.nlmsg_type = RTM_GETADDR, + .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, + .ifa.ifa_family = AF_INET, + .ifa.ifa_index = ifp->index, }; int error = if_sendnetlink(ifp->ctx, NETLINK_ROUTE, &nlm.hdr, @@ -1648,8 +1634,7 @@ if_addressexists(struct interface *ifp, struct in_addr *addr) } #endif -struct nlmr -{ +struct nlmr { struct nlmsghdr hdr; struct rtmsg rt; char buffer[256]; @@ -1693,11 +1678,11 @@ if_route(unsigned char cmd, const struct rt *rt) else #endif #ifdef RTPROT_DHCP - if (rt->rt_dflags & RTDF_DHCP) + if (rt->rt_dflags & RTDF_DHCP) nlm.rt.rtm_protocol = RTPROT_DHCP; else #endif - if (rt->rt_dflags & RTDF_IFA_ROUTE) + if (rt->rt_dflags & RTDF_IFA_ROUTE) nlm.rt.rtm_protocol = RTPROT_KERNEL; else nlm.rt.rtm_protocol = RTPROT_BOOT; @@ -1713,9 +1698,9 @@ if_route(unsigned char cmd, const struct rt *rt) nlm.rt.rtm_type = RTN_UNICAST; } -#define ADDSA(type, sa) \ - add_attr_l(&nlm.hdr, sizeof(nlm), (type), \ - (const char *)(sa) + sa_addroffset((sa)), \ +#define ADDSA(type, sa) \ + add_attr_l(&nlm.hdr, sizeof(nlm), (type), \ + (const char *)(sa) + sa_addroffset((sa)), \ (unsigned short)sa_addrlen((sa))); nlm.rt.rtm_dst_len = (unsigned char)sa_toprefix(&rt->rt_netmask); /* rt->rt_dest and rt->gateway are unions where sockaddr_in6 @@ -1739,8 +1724,8 @@ if_route(unsigned char cmd, const struct rt *rt) metrics->rta_type = RTA_METRICS; metrics->rta_len = RTA_LENGTH(0); - rta_add_attr_32(metrics, sizeof(metricsbuf), - RTAX_MTU, rt->rt_mtu); + rta_add_attr_32(metrics, sizeof(metricsbuf), RTAX_MTU, + rt->rt_mtu); add_attr_l(&nlm.hdr, sizeof(nlm), RTA_METRICS, RTA_DATA(metrics), (unsigned short)RTA_PAYLOAD(metrics)); @@ -1750,7 +1735,7 @@ if_route(unsigned char cmd, const struct rt *rt) if (rt->rt_dflags & RTDF_RA) { uint8_t pref; - switch(rt->rt_pref) { + switch (rt->rt_pref) { case RTPREF_LOW: pref = ICMPV6_ROUTER_PREF_LOW; break; @@ -1782,16 +1767,14 @@ if_route(unsigned char cmd, const struct rt *rt) #endif if (rt->rt_metric != 0) - add_attr_32(&nlm.hdr, sizeof(nlm), RTA_PRIORITY, - rt->rt_metric); + add_attr_32(&nlm.hdr, sizeof(nlm), RTA_PRIORITY, rt->rt_metric); - return if_sendnetlink(rt->rt_ifp->ctx, NETLINK_ROUTE, &nlm.hdr, - NULL, NULL); + return if_sendnetlink(rt->rt_ifp->ctx, NETLINK_ROUTE, &nlm.hdr, NULL, + NULL); } static int -_if_initrt(struct dhcpcd_ctx *ctx, void *arg, - struct nlmsghdr *nlm) +_if_initrt(struct dhcpcd_ctx *ctx, void *arg, struct nlmsghdr *nlm) { struct rt rt, *rtn; rb_tree_t *kroutes = arg; @@ -1812,15 +1795,15 @@ int if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) { struct nlmr nlm = { - .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)), - .hdr.nlmsg_type = RTM_GETROUTE, - .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, - .rt.rtm_table = RT_TABLE_MAIN, - .rt.rtm_family = (unsigned char)af, + .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)), + .hdr.nlmsg_type = RTM_GETROUTE, + .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, + .rt.rtm_table = RT_TABLE_MAIN, + .rt.rtm_family = (unsigned char)af, }; - return if_sendnetlink(ctx, NETLINK_ROUTE, &nlm.hdr, - &_if_initrt, kroutes); + return if_sendnetlink(ctx, NETLINK_ROUTE, &nlm.hdr, &_if_initrt, + kroutes); } #ifdef INET @@ -1838,13 +1821,11 @@ bpf_open(const struct interface *ifp, struct sockaddr sa; struct sockaddr_ll sll; struct sockaddr_storage ss; - } su = { - .sll = { - .sll_family = PF_PACKET, - .sll_protocol = htons(ETH_P_ALL), - .sll_ifindex = (int)ifp->index, - } - }; + } su = { .sll = { + .sll_family = PF_PACKET, + .sll_protocol = htons(ETH_P_ALL), + .sll_ifindex = (int)ifp->index, + } }; #ifdef PACKET_AUXDATA int n; #endif @@ -1861,7 +1842,8 @@ bpf_open(const struct interface *ifp, if (bpf->bpf_buffer == NULL) goto eexit; - bpf->bpf_fd = xsocket(PF_PACKET, SOCK_RAW|SOCK_CXNB,htons(ETH_P_ALL)); + bpf->bpf_fd = xsocket(PF_PACKET, SOCK_RAW | SOCK_CXNB, + htons(ETH_P_ALL)); if (bpf->bpf_fd == -1) goto eexit; @@ -1876,8 +1858,8 @@ bpf_open(const struct interface *ifp, /* In the ideal world, this would be set before the bind and filter. */ #ifdef PACKET_AUXDATA n = 1; - if (setsockopt(bpf->bpf_fd, SOL_PACKET, PACKET_AUXDATA, - &n, sizeof(n)) != 0) { + if (setsockopt(bpf->bpf_fd, SOL_PACKET, PACKET_AUXDATA, &n, + sizeof(n)) != 0) { if (errno != ENOPROTOOPT) goto eexit; } @@ -1890,7 +1872,7 @@ bpf_open(const struct interface *ifp, * There is no way of flushing them from userland. * As such, consumers need to inspect each packet to ensure it's valid. * Or to put it another way, don't trust the Linux BPF filter. - */ + */ return bpf; @@ -1941,10 +1923,8 @@ bpf_read(struct bpf *bpf, void *data, size_t len) bytes = (ssize_t)len; memcpy(data, bpf->bpf_buffer, (size_t)bytes); #ifdef PACKET_AUXDATA - for (cmsg = CMSG_FIRSTHDR(&msg); - cmsg; - cmsg = CMSG_NXTHDR(&msg, cmsg)) - { + for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; + cmsg = CMSG_NXTHDR(&msg, cmsg)) { if (cmsg->cmsg_level == SOL_PACKET && cmsg->cmsg_type == PACKET_AUXDATA) { aux = (void *)CMSG_DATA(cmsg); @@ -2007,8 +1987,8 @@ if_address(unsigned char cmd, const struct ipv4_addr *ia) (unsigned short)(strlen(ia->iface->alias) + 1)); #endif - add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, - &ia->addr.s_addr, sizeof(ia->addr.s_addr)); + add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, &ia->addr.s_addr, + sizeof(ia->addr.s_addr)); if (cmd == RTM_NEWADDR) { #ifdef IFA_F_NOPREFIXROUTE @@ -2023,21 +2003,20 @@ if_address(unsigned char cmd, const struct ipv4_addr *ia) memset(&cinfo, 0, sizeof(cinfo)); cinfo.ifa_prefered = ia->pltime; cinfo.ifa_valid = ia->vltime; - add_attr_l(&nlm.hdr, sizeof(nlm), IFA_CACHEINFO, - &cinfo, sizeof(cinfo)); + add_attr_l(&nlm.hdr, sizeof(nlm), IFA_CACHEINFO, &cinfo, + sizeof(cinfo)); } - if (if_sendnetlink(ia->iface->ctx, NETLINK_ROUTE, &nlm.hdr, - NULL, NULL) == -1) + if (if_sendnetlink(ia->iface->ctx, NETLINK_ROUTE, &nlm.hdr, NULL, + NULL) == -1) retval = -1; return retval; } int if_addrflags(__unused const struct interface *ifp, -__unused const struct in_addr *addr, __unused const char *alias) + __unused const struct in_addr *addr, __unused const char *alias) { - /* Linux has no support for IPv4 address flags */ return 0; } @@ -2070,8 +2049,8 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LABEL, ia->iface->alias, (unsigned short)(strlen(ia->iface->alias) + 1)); #endif - add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, - &ia->addr.s6_addr, sizeof(ia->addr.s6_addr)); + add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, &ia->addr.s6_addr, + sizeof(ia->addr.s6_addr)); if (cmd == RTM_NEWADDR) { #ifdef IPV6_MANAGETEMPADDR @@ -2098,16 +2077,15 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) memset(&cinfo, 0, sizeof(cinfo)); cinfo.ifa_prefered = ia->prefix_pltime; cinfo.ifa_valid = ia->prefix_vltime; - add_attr_l(&nlm.hdr, sizeof(nlm), IFA_CACHEINFO, - &cinfo, sizeof(cinfo)); + add_attr_l(&nlm.hdr, sizeof(nlm), IFA_CACHEINFO, &cinfo, + sizeof(cinfo)); } - return if_sendnetlink(ia->iface->ctx, NETLINK_ROUTE, &nlm.hdr, - NULL, NULL); + return if_sendnetlink(ia->iface->ctx, NETLINK_ROUTE, &nlm.hdr, NULL, + NULL); } -struct ifiaddr6 -{ +struct ifiaddr6 { unsigned int ifa_ifindex; struct in6_addr ifa_addr; uint32_t ifa_flags; @@ -2115,8 +2093,7 @@ struct ifiaddr6 }; static int -_if_addrflags6(__unused struct dhcpcd_ctx *ctx, - void *arg, struct nlmsghdr *nlm) +_if_addrflags6(__unused struct dhcpcd_ctx *ctx, void *arg, struct nlmsghdr *nlm) { struct ifiaddr6 *ia = arg; size_t len; @@ -2151,10 +2128,10 @@ _if_addrflags6(__unused struct dhcpcd_ctx *ctx, } if (local) { - if (IN6_ARE_ADDR_EQUAL(&ia->ifa_addr, local)) + if (IN6_ARE_ADDR_EQUAL(&ia->ifa_addr, local)) ia->ifa_found = true; } else if (address) { - if (IN6_ARE_ADDR_EQUAL(&ia->ifa_addr, address)) + if (IN6_ARE_ADDR_EQUAL(&ia->ifa_addr, address)) ia->ifa_found = true; } if (ia->ifa_found) @@ -2172,11 +2149,11 @@ if_addrflags6(const struct interface *ifp, const struct in6_addr *addr, .ifa_found = false, }; struct nlma nlm = { - .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), - .hdr.nlmsg_type = RTM_GETADDR, - .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, - .ifa.ifa_family = AF_INET6, - .ifa.ifa_index = ifp->index, + .hdr.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)), + .hdr.nlmsg_type = RTM_GETADDR, + .hdr.nlmsg_flags = NLM_F_REQUEST | NLM_F_MATCH, + .ifa.ifa_family = AF_INET6, + .ifa.ifa_index = ifp->index, }; int error = if_sendnetlink(ifp->ctx, NETLINK_ROUTE, &nlm.hdr, @@ -2193,14 +2170,12 @@ if_addrflags6(const struct interface *ifp, const struct in6_addr *addr, int if_getlifetime6(__unused struct ipv6_addr *ia) { - /* God knows how to work out address lifetimes on Linux */ errno = ENOTSUP; return -1; } -struct nlml -{ +struct nlml { struct nlmsghdr hdr; struct ifinfomsg i; char buffer[32]; @@ -2220,7 +2195,6 @@ add_attr_nest(struct nlmsghdr *n, unsigned short maxlen, unsigned short type) static void add_attr_nest_end(struct nlmsghdr *n, struct rtattr *nest) { - nest->rta_len = (unsigned short)((char *)NLMSG_TAIL(n) - (char *)nest); } #endif @@ -2317,8 +2291,8 @@ if_applyra(const struct ra *rap) } if (rap->retrans != 0) { - snprintf(path, sizeof(path), "%s/%s/retrans_time_ms", - p_neigh, ifname); + snprintf(path, sizeof(path), "%s/%s/retrans_time_ms", p_neigh, + ifname); if (if_writepathuint(ctx, path, rap->retrans) == -1) error = -1; } diff --git a/src/if-options.c b/src/if-options.c index c4b1141f..58a18efc 100644 --- a/src/if-options.c +++ b/src/if-options.c @@ -26,11 +26,11 @@ * SUCH DAMAGE. */ -#include #include +#include #include - +#include #include #include #include @@ -42,143 +42,140 @@ #include #include #include -#include #include -#include +#include -#include "config.h" #include "common.h" +#include "config.h" #include "dhcp.h" #include "dhcp6.h" #include "dhcpcd-embedded.h" #include "duid.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "logerr.h" #include "sa.h" -#define IN_CONFIG_BLOCK(ifo) ((ifo)->options & DHCPCD_FORKED) -#define SET_CONFIG_BLOCK(ifo) ((ifo)->options |= DHCPCD_FORKED) -#define CLEAR_CONFIG_BLOCK(ifo) ((ifo)->options &= ~DHCPCD_FORKED) +#define IN_CONFIG_BLOCK(ifo) ((ifo)->options & DHCPCD_FORKED) +#define SET_CONFIG_BLOCK(ifo) ((ifo)->options |= DHCPCD_FORKED) +#define CLEAR_CONFIG_BLOCK(ifo) ((ifo)->options &= ~DHCPCD_FORKED) static unsigned long long default_options; -const struct option cf_options[] = { - {"background", no_argument, NULL, 'b'}, - {"script", required_argument, NULL, 'c'}, - {"debug", no_argument, NULL, 'd'}, - {"env", required_argument, NULL, 'e'}, - {"config", required_argument, NULL, 'f'}, - {"reconfigure", no_argument, NULL, 'g'}, - {"hostname", optional_argument, NULL, 'h'}, - {"vendorclassid", optional_argument, NULL, 'i'}, - {"logfile", required_argument, NULL, 'j'}, - {"release", no_argument, NULL, 'k'}, - {"leasetime", required_argument, NULL, 'l'}, - {"metric", required_argument, NULL, 'm'}, - {"rebind", no_argument, NULL, 'n'}, - {"option", required_argument, NULL, 'o'}, - {"persistent", no_argument, NULL, 'p'}, - {"quiet", no_argument, NULL, 'q'}, - {"request", optional_argument, NULL, 'r'}, - {"inform", optional_argument, NULL, 's'}, - {"inform6", optional_argument, NULL, O_INFORM6}, - {"timeout", required_argument, NULL, 't'}, - {"userclass", required_argument, NULL, 'u'}, +const struct option cf_options[] = { { "background", no_argument, NULL, 'b' }, + { "script", required_argument, NULL, 'c' }, + { "debug", no_argument, NULL, 'd' }, + { "env", required_argument, NULL, 'e' }, + { "config", required_argument, NULL, 'f' }, + { "reconfigure", no_argument, NULL, 'g' }, + { "hostname", optional_argument, NULL, 'h' }, + { "vendorclassid", optional_argument, NULL, 'i' }, + { "logfile", required_argument, NULL, 'j' }, + { "release", no_argument, NULL, 'k' }, + { "leasetime", required_argument, NULL, 'l' }, + { "metric", required_argument, NULL, 'm' }, + { "rebind", no_argument, NULL, 'n' }, + { "option", required_argument, NULL, 'o' }, + { "persistent", no_argument, NULL, 'p' }, + { "quiet", no_argument, NULL, 'q' }, + { "request", optional_argument, NULL, 'r' }, + { "inform", optional_argument, NULL, 's' }, + { "inform6", optional_argument, NULL, O_INFORM6 }, + { "timeout", required_argument, NULL, 't' }, + { "userclass", required_argument, NULL, 'u' }, #ifndef SMALL - {"msuserclass", required_argument, NULL, O_MSUSERCLASS}, + { "msuserclass", required_argument, NULL, O_MSUSERCLASS }, #endif - {"vsio", required_argument, NULL, O_VSIO}, - {"vsio6", required_argument, NULL, O_VSIO6}, - {"vendor", required_argument, NULL, 'v'}, - {"waitip", optional_argument, NULL, 'w'}, - {"exit", no_argument, NULL, 'x'}, - {"allowinterfaces", required_argument, NULL, 'z'}, - {"reboot", required_argument, NULL, 'y'}, - {"noarp", no_argument, NULL, 'A'}, - {"nobackground", no_argument, NULL, 'B'}, - {"nohook", required_argument, NULL, 'C'}, - {"duid", optional_argument, NULL, 'D'}, - {"lastlease", no_argument, NULL, 'E'}, - {"fqdn", optional_argument, NULL, 'F'}, - {"nogateway", no_argument, NULL, 'G'}, - {"xidhwaddr", no_argument, NULL, 'H'}, - {"clientid", optional_argument, NULL, 'I'}, - {"broadcast", no_argument, NULL, 'J'}, - {"nolink", no_argument, NULL, 'K'}, - {"noipv4ll", no_argument, NULL, 'L'}, - {"manager", no_argument, NULL, 'M'}, - {"renew", no_argument, NULL, 'N'}, - {"nooption", required_argument, NULL, 'O'}, - {"printpidfile", no_argument, NULL, 'P'}, - {"require", required_argument, NULL, 'Q'}, - {"static", required_argument, NULL, 'S'}, - {"test", no_argument, NULL, 'T'}, - {"dumplease", no_argument, NULL, 'U'}, - {"variables", no_argument, NULL, 'V'}, - {"whitelist", required_argument, NULL, 'W'}, - {"blacklist", required_argument, NULL, 'X'}, - {"denyinterfaces", required_argument, NULL, 'Z'}, - {"oneshot", no_argument, NULL, '1'}, - {"ipv4only", no_argument, NULL, '4'}, - {"ipv6only", no_argument, NULL, '6'}, - {"anonymous", no_argument, NULL, O_ANONYMOUS}, - {"randomise_hwaddr",no_argument, NULL, O_RANDOMISE_HWADDR}, - {"arping", required_argument, NULL, O_ARPING}, - {"destination", required_argument, NULL, O_DESTINATION}, - {"fallback", required_argument, NULL, O_FALLBACK}, - {"ipv6rs", no_argument, NULL, O_IPV6RS}, - {"noipv6rs", no_argument, NULL, O_NOIPV6RS}, - {"ipv6ra_autoconf", no_argument, NULL, O_IPV6RA_AUTOCONF}, - {"ipv6ra_noautoconf", no_argument, NULL, O_IPV6RA_NOAUTOCONF}, - {"ipv6ra_fork", no_argument, NULL, O_IPV6RA_FORK}, - {"ipv4", no_argument, NULL, O_IPV4}, - {"noipv4", no_argument, NULL, O_NOIPV4}, - {"ipv6", no_argument, NULL, O_IPV6}, - {"noipv6", no_argument, NULL, O_NOIPV6}, - {"noalias", no_argument, NULL, O_NOALIAS}, - {"iaid", required_argument, NULL, O_IAID}, - {"ia_na", optional_argument, NULL, O_IA_NA}, - {"ia_ta", optional_argument, NULL, O_IA_TA}, - {"ia_pd", optional_argument, NULL, O_IA_PD}, - {"hostname_short", no_argument, NULL, O_HOSTNAME_SHORT}, - {"dev", required_argument, NULL, O_DEV}, - {"nodev", no_argument, NULL, O_NODEV}, - {"define", required_argument, NULL, O_DEFINE}, - {"definend", required_argument, NULL, O_DEFINEND}, - {"define6", required_argument, NULL, O_DEFINE6}, - {"embed", required_argument, NULL, O_EMBED}, - {"encap", required_argument, NULL, O_ENCAP}, - {"vendopt", required_argument, NULL, O_VENDOPT}, - {"vendclass", required_argument, NULL, O_VENDCLASS}, - {"authprotocol", required_argument, NULL, O_AUTHPROTOCOL}, - {"authtoken", required_argument, NULL, O_AUTHTOKEN}, - {"noauthrequired", no_argument, NULL, O_AUTHNOTREQUIRED}, - {"dhcp", no_argument, NULL, O_DHCP}, - {"nodhcp", no_argument, NULL, O_NODHCP}, - {"dhcp6", no_argument, NULL, O_DHCP6}, - {"nodhcp6", no_argument, NULL, O_NODHCP6}, - {"controlgroup", required_argument, NULL, O_CONTROLGRP}, - {"slaac", required_argument, NULL, O_SLAAC}, - {"gateway", no_argument, NULL, O_GATEWAY}, - {"reject", required_argument, NULL, O_REJECT}, - {"bootp", no_argument, NULL, O_BOOTP}, - {"nodelay", no_argument, NULL, O_NODELAY}, - {"noup", no_argument, NULL, O_NOUP}, - {"lastleaseextend", no_argument, NULL, O_LASTLEASE_EXTEND}, - {"inactive", no_argument, NULL, O_INACTIVE}, - {"mudurl", required_argument, NULL, O_MUDURL}, - {"link_rcvbuf", required_argument, NULL, O_LINK_RCVBUF}, - {"configure", no_argument, NULL, O_CONFIGURE}, - {"noconfigure", no_argument, NULL, O_NOCONFIGURE}, - {"arp_persistdefence", no_argument, NULL, O_ARP_PERSISTDEFENCE}, - {"request_time", required_argument, NULL, O_REQUEST_TIME}, - {"fallback_time", required_argument, NULL, O_FALLBACK_TIME}, - {"ipv4ll_time", required_argument, NULL, O_IPV4LL_TIME}, - {"nosyslog", no_argument, NULL, O_NOSYSLOG}, - {NULL, 0, NULL, '\0'} -}; + { "vsio", required_argument, NULL, O_VSIO }, + { "vsio6", required_argument, NULL, O_VSIO6 }, + { "vendor", required_argument, NULL, 'v' }, + { "waitip", optional_argument, NULL, 'w' }, + { "exit", no_argument, NULL, 'x' }, + { "allowinterfaces", required_argument, NULL, 'z' }, + { "reboot", required_argument, NULL, 'y' }, + { "noarp", no_argument, NULL, 'A' }, + { "nobackground", no_argument, NULL, 'B' }, + { "nohook", required_argument, NULL, 'C' }, + { "duid", optional_argument, NULL, 'D' }, + { "lastlease", no_argument, NULL, 'E' }, + { "fqdn", optional_argument, NULL, 'F' }, + { "nogateway", no_argument, NULL, 'G' }, + { "xidhwaddr", no_argument, NULL, 'H' }, + { "clientid", optional_argument, NULL, 'I' }, + { "broadcast", no_argument, NULL, 'J' }, + { "nolink", no_argument, NULL, 'K' }, + { "noipv4ll", no_argument, NULL, 'L' }, + { "manager", no_argument, NULL, 'M' }, + { "renew", no_argument, NULL, 'N' }, + { "nooption", required_argument, NULL, 'O' }, + { "printpidfile", no_argument, NULL, 'P' }, + { "require", required_argument, NULL, 'Q' }, + { "static", required_argument, NULL, 'S' }, + { "test", no_argument, NULL, 'T' }, + { "dumplease", no_argument, NULL, 'U' }, + { "variables", no_argument, NULL, 'V' }, + { "whitelist", required_argument, NULL, 'W' }, + { "blacklist", required_argument, NULL, 'X' }, + { "denyinterfaces", required_argument, NULL, 'Z' }, + { "oneshot", no_argument, NULL, '1' }, + { "ipv4only", no_argument, NULL, '4' }, + { "ipv6only", no_argument, NULL, '6' }, + { "anonymous", no_argument, NULL, O_ANONYMOUS }, + { "randomise_hwaddr", no_argument, NULL, O_RANDOMISE_HWADDR }, + { "arping", required_argument, NULL, O_ARPING }, + { "destination", required_argument, NULL, O_DESTINATION }, + { "fallback", required_argument, NULL, O_FALLBACK }, + { "ipv6rs", no_argument, NULL, O_IPV6RS }, + { "noipv6rs", no_argument, NULL, O_NOIPV6RS }, + { "ipv6ra_autoconf", no_argument, NULL, O_IPV6RA_AUTOCONF }, + { "ipv6ra_noautoconf", no_argument, NULL, O_IPV6RA_NOAUTOCONF }, + { "ipv6ra_fork", no_argument, NULL, O_IPV6RA_FORK }, + { "ipv4", no_argument, NULL, O_IPV4 }, + { "noipv4", no_argument, NULL, O_NOIPV4 }, + { "ipv6", no_argument, NULL, O_IPV6 }, + { "noipv6", no_argument, NULL, O_NOIPV6 }, + { "noalias", no_argument, NULL, O_NOALIAS }, + { "iaid", required_argument, NULL, O_IAID }, + { "ia_na", optional_argument, NULL, O_IA_NA }, + { "ia_ta", optional_argument, NULL, O_IA_TA }, + { "ia_pd", optional_argument, NULL, O_IA_PD }, + { "hostname_short", no_argument, NULL, O_HOSTNAME_SHORT }, + { "dev", required_argument, NULL, O_DEV }, + { "nodev", no_argument, NULL, O_NODEV }, + { "define", required_argument, NULL, O_DEFINE }, + { "definend", required_argument, NULL, O_DEFINEND }, + { "define6", required_argument, NULL, O_DEFINE6 }, + { "embed", required_argument, NULL, O_EMBED }, + { "encap", required_argument, NULL, O_ENCAP }, + { "vendopt", required_argument, NULL, O_VENDOPT }, + { "vendclass", required_argument, NULL, O_VENDCLASS }, + { "authprotocol", required_argument, NULL, O_AUTHPROTOCOL }, + { "authtoken", required_argument, NULL, O_AUTHTOKEN }, + { "noauthrequired", no_argument, NULL, O_AUTHNOTREQUIRED }, + { "dhcp", no_argument, NULL, O_DHCP }, + { "nodhcp", no_argument, NULL, O_NODHCP }, + { "dhcp6", no_argument, NULL, O_DHCP6 }, + { "nodhcp6", no_argument, NULL, O_NODHCP6 }, + { "controlgroup", required_argument, NULL, O_CONTROLGRP }, + { "slaac", required_argument, NULL, O_SLAAC }, + { "gateway", no_argument, NULL, O_GATEWAY }, + { "reject", required_argument, NULL, O_REJECT }, + { "bootp", no_argument, NULL, O_BOOTP }, + { "nodelay", no_argument, NULL, O_NODELAY }, + { "noup", no_argument, NULL, O_NOUP }, + { "lastleaseextend", no_argument, NULL, O_LASTLEASE_EXTEND }, + { "inactive", no_argument, NULL, O_INACTIVE }, + { "mudurl", required_argument, NULL, O_MUDURL }, + { "link_rcvbuf", required_argument, NULL, O_LINK_RCVBUF }, + { "configure", no_argument, NULL, O_CONFIGURE }, + { "noconfigure", no_argument, NULL, O_NOCONFIGURE }, + { "arp_persistdefence", no_argument, NULL, O_ARP_PERSISTDEFENCE }, + { "request_time", required_argument, NULL, O_REQUEST_TIME }, + { "fallback_time", required_argument, NULL, O_FALLBACK_TIME }, + { "ipv4ll_time", required_argument, NULL, O_IPV4LL_TIME }, + { "nosyslog", no_argument, NULL, O_NOSYSLOG }, + { NULL, 0, NULL, '\0' } }; static char * add_environ(char ***array, const char *value, int uniq) @@ -254,12 +251,12 @@ add_environ(char ***array, const char *value, int uniq) return newlist[i]; } -#define PARSE_STRING 0 -#define PARSE_STRING_NULL 1 -#define PARSE_HWADDR 2 -#define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING) +#define PARSE_STRING 0 +#define PARSE_STRING_NULL 1 +#define PARSE_HWADDR 2 +#define parse_string(a, b, c) parse_str((a), (b), (c), PARSE_STRING) #define parse_nstring(a, b, c) parse_str((a), (b), (c), PARSE_STRING_NULL) -#define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR) +#define parse_hwaddr(a, b, c) parse_str((a), (b), (c), PARSE_HWADDR) static ssize_t parse_str(char *sbuf, size_t slen, const char *str, int flags) { @@ -311,7 +308,7 @@ parse_str(char *sbuf, size_t slen, const char *str, int flags) } if (*str == '\\') { str++; - switch((cmd = *str++)) { + switch ((cmd = *str++)) { case '\0': str--; break; @@ -342,7 +339,8 @@ parse_str(char *sbuf, size_t slen, const char *str, int flags) if (c[1] != '\0') { c[2] = '\0'; if (sbuf) - *sbuf++ = (char)strtol(c, NULL, 16); + *sbuf++ = (char)strtol(c, NULL, + 16); } else l--; break; @@ -416,7 +414,6 @@ parse_iaid1(uint8_t *iaid, const char *arg, size_t len, int n) static int parse_iaid(uint8_t *iaid, const char *arg, size_t len) { - return parse_iaid1(iaid, arg, len, 1); } @@ -424,7 +421,6 @@ parse_iaid(uint8_t *iaid, const char *arg, size_t len) static int parse_uint32(uint32_t *i, const char *arg) { - return parse_iaid1((uint8_t *)i, arg, sizeof(uint32_t), 0); } #endif @@ -481,8 +477,7 @@ parse_addr(struct in_addr *addr, struct in_addr *net, const char *arg) *p++ = '\0'; i = strtoi(p, NULL, 10, 0, 32, &e); if (e != 0 || - (net != NULL && inet_cidrtoaddr((int)i, net) != 0)) - { + (net != NULL && inet_cidrtoaddr((int)i, net) != 0)) { logerrx("invalid CIDR: %s", p); return -1; } @@ -503,21 +498,17 @@ static int parse_addr(__unused struct in_addr *addr, __unused struct in_addr *net, __unused const char *arg) { - logerrx("No IPv4 support"); return -1; } #endif static void -set_option_space(struct dhcpcd_ctx *ctx, - const char *arg, - const struct dhcp_opt **d, size_t *dl, - const struct dhcp_opt **od, size_t *odl, - struct if_options *ifo, - uint8_t *request[], uint8_t *require[], uint8_t *no[], uint8_t *reject[]) +set_option_space(struct dhcpcd_ctx *ctx, const char *arg, + const struct dhcp_opt **d, size_t *dl, const struct dhcp_opt **od, + size_t *odl, struct if_options *ifo, uint8_t *request[], uint8_t *require[], + uint8_t *no[], uint8_t *reject[]) { - #if !defined(INET) && !defined(INET6) UNUSED(ctx); #endif @@ -593,7 +584,6 @@ free_dhcp_opt_embenc(struct dhcp_opt *opt) static char * strwhite(const char *s) { - if (s == NULL) return NULL; while (*s != ' ' && *s != '\t') { @@ -607,7 +597,6 @@ strwhite(const char *s) static char * strskipwhite(const char *s) { - if (s == NULL || *s == '\0') return NULL; while (*s == ' ' || *s == '\t') { @@ -623,14 +612,13 @@ strskipwhite(const char *s) static char * strend(const char *s) { - s = strskipwhite(s); if (s == NULL) return NULL; if (*s != '"') return strchr(s, ' '); s++; - for (; *s != '"' ; s++) { + for (; *s != '"'; s++) { if (*s == '\0') return NULL; if (*s == '\\') { @@ -695,9 +683,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, /* Add a guard for static analysers. * This should not be needed really because of the argument_required option * in the options declaration above. */ -#define ARG_REQUIRED if (arg == NULL) goto arg_required +#define ARG_REQUIRED \ + if (arg == NULL) \ + goto arg_required - switch(opt) { + switch (opt) { case 'f': /* FALLTHROUGH */ case 'g': /* FALLTHROUGH */ case 'n': /* FALLTHROUGH */ @@ -716,7 +706,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (IN_CONFIG_BLOCK(ifo)) { logerrx("%s: per interface scripts" - " are no longer supported", + " are no longer supported", ifname); return -1; } @@ -734,10 +724,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; } s = parse_nstring(ctx->script, dl, arg); - if (s == -1 || - ctx->script[0] == '\0' || - strcmp(ctx->script, "/dev/null") == 0) - { + if (s == -1 || ctx->script[0] == '\0' || + strcmp(ctx->script, "/dev/null") == 0) { free(ctx->script); ctx->script = NULL; } @@ -799,8 +787,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ifo->leasetime = DHCP_INFINITE_LIFETIME; break; } - ifo->leasetime = (uint32_t)strtou(arg, NULL, - 0, 0, UINT32_MAX, &e); + ifo->leasetime = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, + &e); if (e) { logerrx("failed to convert leasetime %s", arg); return -1; @@ -818,12 +806,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ctx->options & DHCPCD_PRINT_PIDFILE) break; - set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, - &request, &require, &no, &reject); + set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, &request, + &require, &no, &reject); if (make_option_mask(d, dl, od, odl, request, arg, 1) != 0 || make_option_mask(d, dl, od, odl, no, arg, -1) != 0 || - make_option_mask(d, dl, od, odl, reject, arg, -1) != 0) - { + make_option_mask(d, dl, od, odl, reject, arg, -1) != 0) { logerrx("unknown option: %s", arg); return -1; } @@ -832,12 +819,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ctx->options & DHCPCD_PRINT_PIDFILE) break; - set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, - &request, &require, &no, &reject); + set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, &request, + &require, &no, &reject); if (make_option_mask(d, dl, od, odl, reject, arg, 1) != 0 || make_option_mask(d, dl, od, odl, request, arg, -1) != 0 || - make_option_mask(d, dl, od, odl, require, arg, -1) != 0) - { + make_option_mask(d, dl, od, odl, require, arg, -1) != 0) { logerrx("unknown option: %s", arg); return -1; } @@ -881,7 +867,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, break; case 't': ARG_REQUIRED; - ifo->timeout = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e); + ifo->timeout = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, + &e); if (e) { logerrx("failed to convert timeout %s", arg); return -1; @@ -889,15 +876,16 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, break; case 'u': dl = sizeof(ifo->userclass) - ifo->userclass[0] - 1; - s = parse_string((char *)ifo->userclass + - ifo->userclass[0] + 2, dl, arg); + s = parse_string((char *)ifo->userclass + ifo->userclass[0] + 2, + dl, arg); if (s == -1) { logerr("userclass"); return -1; } if (s != 0) { ifo->userclass[ifo->userclass[0] + 1] = (uint8_t)s; - ifo->userclass[0] = (uint8_t)(ifo->userclass[0] + s +1); + ifo->userclass[0] = (uint8_t)(ifo->userclass[0] + s + + 1); } break; #ifndef SMALL @@ -962,7 +950,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *p = ','; if (e) { logerrx("vendor option should be between" - " 1 and %zu inclusive", opt_max); + " 1 and %zu inclusive", + opt_max); return -1; } @@ -1025,8 +1014,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, vsio->so_len = 0; } - for (sl = 0, vsio_so = vsio->so; - sl < vsio->so_len; + for (sl = 0, vsio_so = vsio->so; sl < vsio->so_len; sl++, vsio_so++) opt_max -= opt_header + vsio_so->len; if (opt_header + dl > opt_max) { @@ -1084,7 +1072,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *p = ','; if (e) { logerrx("vendor option should be between" - " 1 and 254 inclusive"); + " 1 and 254 inclusive"); return -1; } @@ -1100,8 +1088,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, s = sizeof(addr.s_addr); } } else { - s = parse_string((char *)ifo->vendor + - ifo->vendor[0] + 3, (size_t)s, arg); + s = parse_string((char *)ifo->vendor + ifo->vendor[0] + + 3, + (size_t)s, arg); } if (s == -1) { logerr("vendor"); @@ -1249,12 +1238,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ctx->options & DHCPCD_PRINT_PIDFILE) break; - set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, - &request, &require, &no, &reject); + set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, &request, + &require, &no, &reject); if (make_option_mask(d, dl, od, odl, request, arg, -1) != 0 || make_option_mask(d, dl, od, odl, require, arg, -1) != 0 || - make_option_mask(d, dl, od, odl, no, arg, 1) != 0) - { + make_option_mask(d, dl, od, odl, no, arg, 1) != 0) { logerrx("unknown option: %s", arg); return -1; } @@ -1263,13 +1251,12 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ctx->options & DHCPCD_PRINT_PIDFILE) break; - set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, - &request, &require, &no, &reject); + set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, &request, + &require, &no, &reject); if (make_option_mask(d, dl, od, odl, require, arg, 1) != 0 || make_option_mask(d, dl, od, odl, request, arg, 1) != 0 || make_option_mask(d, dl, od, odl, no, arg, -1) != 0 || - make_option_mask(d, dl, od, odl, reject, arg, -1) != 0) - { + make_option_mask(d, dl, od, odl, reject, arg, -1) != 0) { logerrx("unknown option: %s", arg); return -1; } @@ -1289,15 +1276,15 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, break; } if (parse_addr(&ifo->req_addr, - ifo->req_mask.s_addr == 0 ? &ifo->req_mask : NULL, - p) != 0) + ifo->req_mask.s_addr == 0 ? &ifo->req_mask : + NULL, + p) != 0) return -1; ifo->options |= DHCPCD_STATIC; ifo->options &= ~DHCPCD_INFORM; - } else if (strncmp(arg, "subnet_mask=", - strlen("subnet_mask=")) == 0) - { + } else if (strncmp(arg, + "subnet_mask=", strlen("subnet_mask=")) == 0) { if (p == NULL) { ifo->req_mask.s_addr = INADDR_ANY; break; @@ -1305,8 +1292,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, if (parse_addr(&ifo->req_mask, NULL, p) != 0) return -1; } else if (strncmp(arg, "broadcast_address=", - strlen("broadcast_address=")) == 0) - { + strlen("broadcast_address=")) == 0) { if (p == NULL) { ifo->req_brd.s_addr = INADDR_ANY; break; @@ -1314,13 +1300,12 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, if (parse_addr(&ifo->req_brd, NULL, p) != 0) return -1; } else if (strncmp(arg, "routes=", strlen("routes=")) == 0 || - strncmp(arg, "static_routes=", - strlen("static_routes=")) == 0 || + strncmp(arg, "static_routes=", strlen("static_routes=")) == + 0 || strncmp(arg, "classless_static_routes=", - strlen("classless_static_routes=")) == 0 || + strlen("classless_static_routes=")) == 0 || strncmp(arg, "ms_classless_static_routes=", - strlen("ms_classless_static_routes=")) == 0) - { + strlen("ms_classless_static_routes=")) == 0) { #ifdef INET struct in_addr addr3; @@ -1338,8 +1323,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *np++ = '\0'; np = strskipwhite(np); if (parse_addr(&addr, &addr2, p) == -1 || - parse_addr(&addr3, NULL, np) == -1) - { + parse_addr(&addr3, NULL, np) == -1) { *fp = ' '; return -1; } @@ -1377,18 +1361,18 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; #endif } else if (strncmp(arg, "interface_mtu=", - strlen("interface_mtu=")) == 0 || - strncmp(arg, "mtu=", strlen("mtu=")) == 0) - { + strlen("interface_mtu=")) == 0 || + strncmp(arg, "mtu=", strlen("mtu=")) == 0) { if (p == NULL) break; - ifo->mtu = (unsigned int)strtou(p, NULL, 0, - IPV4_MMTU, UINT_MAX, &e); + ifo->mtu = (unsigned int)strtou(p, NULL, 0, IPV4_MMTU, + UINT_MAX, &e); if (e) { logerrx("invalid MTU %s", p); return -1; } - } else if (strncmp(arg, "ip6_address=", strlen("ip6_address=")) == 0) { + } else if (strncmp(arg, + "ip6_address=", strlen("ip6_address=")) == 0) { #ifdef INET6 if (p == NULL) { memset(&ifo->req_addr6, 0, @@ -1399,13 +1383,14 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, np = strchr(p, '/'); if (np) *np++ = '\0'; - if ((i = inet_pton(AF_INET6, p, &ifo->req_addr6)) == 1) { + if ((i = inet_pton(AF_INET6, p, &ifo->req_addr6)) == + 1) { if (np) { - ifo->req_prefix_len = (uint8_t)strtou(np, - NULL, 0, 0, 128, &e); + ifo->req_prefix_len = (uint8_t) + strtou(np, NULL, 0, 0, 128, &e); if (e) { logerrx("%s: failed to " - "convert prefix len", + "convert prefix len", ifname); return -1; } @@ -1433,8 +1418,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; if (strchr(arg, '/') == NULL) addr2.s_addr = INADDR_BROADCAST; - naddr = reallocarray(ifo->whitelist, - ifo->whitelist_len + 2, sizeof(in_addr_t)); + naddr = reallocarray(ifo->whitelist, ifo->whitelist_len + 2, + sizeof(in_addr_t)); if (naddr == NULL) { logerr(__func__); return -1; @@ -1448,8 +1433,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; if (strchr(arg, '/') == NULL) addr2.s_addr = INADDR_BROADCAST; - naddr = reallocarray(ifo->blacklist, - ifo->blacklist_len + 2, sizeof(in_addr_t)); + naddr = reallocarray(ifo->blacklist, ifo->blacklist_len + 2, + sizeof(in_addr_t)); if (naddr == NULL) { logerr(__func__); return -1; @@ -1554,14 +1539,14 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ARG_REQUIRED; if (ctx->options & DHCPCD_PRINT_PIDFILE) break; - set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, - &request, &require, &no, &reject); - if (make_option_mask(d, dl, od, odl, - ifo->dstmask, arg, 2) != 0) - { + set_option_space(ctx, arg, &d, &dl, &od, &odl, ifo, &request, + &require, &no, &reject); + if (make_option_mask(d, dl, od, odl, ifo->dstmask, arg, 2) != + 0) { if (errno == EINVAL) logerrx("option does not take" - " an IPv4 address: %s", arg); + " an IPv4 address: %s", + arg); else logerrx("unknown option: %s", arg); return -1; @@ -1622,20 +1607,18 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; #else if (ctx->options & DHCPCD_MANAGER && - !IN_CONFIG_BLOCK(ifo)) - { + !IN_CONFIG_BLOCK(ifo)) { logerrx("IA PD must belong in an " - "interface block"); + "interface block"); return -1; } i = D6_OPTION_IA_PD; #endif } - if (ctx->options & DHCPCD_MANAGER && - !IN_CONFIG_BLOCK(ifo) && arg) - { + if (ctx->options & DHCPCD_MANAGER && !IN_CONFIG_BLOCK(ifo) && + arg) { logerrx("IA with IAID must belong in an " - "interface block"); + "interface block"); return -1; } ifo->options |= DHCPCD_IA_FORCED; @@ -1657,19 +1640,18 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, for (sl = 0; sl < ifo->ia_len; sl++) { if ((arg == NULL && !ifo->ia[sl].iaid_set) || (arg != NULL && ifo->ia[sl].iaid_set && - ifo->ia[sl].ia_type == (uint16_t)i && - ifo->ia[sl].iaid[0] == iaid[0] && - ifo->ia[sl].iaid[1] == iaid[1] && - ifo->ia[sl].iaid[2] == iaid[2] && - ifo->ia[sl].iaid[3] == iaid[3])) - { - ia = &ifo->ia[sl]; + ifo->ia[sl].ia_type == (uint16_t)i && + ifo->ia[sl].iaid[0] == iaid[0] && + ifo->ia[sl].iaid[1] == iaid[1] && + ifo->ia[sl].iaid[2] == iaid[2] && + ifo->ia[sl].iaid[3] == iaid[3])) { + ia = &ifo->ia[sl]; break; } } if (ia == NULL) { - ia = reallocarray(ifo->ia, - ifo->ia_len + 1, sizeof(*ifo->ia)); + ia = reallocarray(ifo->ia, ifo->ia_len + 1, + sizeof(*ifo->ia)); if (ia == NULL) { logerr(__func__); return -1; @@ -1685,10 +1667,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ia->iaid_set = 1; } else ia->iaid_set = 0; - if (!ia->iaid_set || - p == NULL || - ia->ia_type == D6_OPTION_IA_TA) - { + if (!ia->iaid_set || p == NULL || + ia->ia_type == D6_OPTION_IA_TA) { memset(&ia->addr, 0, sizeof(ia->addr)); ia->prefix_len = 0; } else { @@ -1705,7 +1685,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, NULL, 0, 8, 120, &e); if (e) { logerrx("%s: failed to convert" - " prefix len", + " prefix len", p); ia->prefix_len = 0; } @@ -1730,8 +1710,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *fp++ = '\0'; fp = strskipwhite(fp); } - sla = reallocarray(ia->sla, - ia->sla_len + 1, sizeof(*ia->sla)); + sla = reallocarray(ia->sla, ia->sla_len + 1, + sizeof(*ia->sla)); if (sla == NULL) { logerr(__func__); return -1; @@ -1741,9 +1721,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, np = strchr(p, '/'); if (np) *np++ = '\0'; - if (strlcpy(sla->ifname, p, - sizeof(sla->ifname)) >= sizeof(sla->ifname)) - { + if (strlcpy(sla->ifname, p, sizeof(sla->ifname)) >= + sizeof(sla->ifname)) { logerrx("%s: interface name too long", arg); goto err_sla; } @@ -1756,12 +1735,12 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, if (np) *np++ = '\0'; if (*p != '\0') { - sla->sla = (uint32_t)strtou(p, NULL, - 0, 0, UINT32_MAX, &e); + sla->sla = (uint32_t)strtou(p, NULL, 0, + 0, UINT32_MAX, &e); sla->sla_set = true; if (e) { logerrx("%s: failed to convert " - "sla", + "sla", ifname); goto err_sla; } @@ -1774,10 +1753,10 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *np++ = '\0'; if (*p != '\0') { sla->prefix_len = (uint8_t)strtou(p, - NULL, 0, 0, 120, &e); + NULL, 0, 0, 120, &e); if (e) { logerrx("%s: failed to " - "convert prefix len", + "convert prefix len", ifname); goto err_sla; } @@ -1793,7 +1772,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, 0, 0, UINT64_MAX, &e); if (e) { logerrx("%s: failed to " - "convert suffix", + "convert suffix", ifname); goto err_sla; } @@ -1804,34 +1783,31 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, slap = &ia->sla[sl]; if (slap->sla_set != sla->sla_set) { logerrx("%s: cannot mix automatic " - "and fixed SLA", + "and fixed SLA", sla->ifname); goto err_sla; } if (ia->prefix_len && (sla->prefix_len == ia->prefix_len || - slap->prefix_len == ia->prefix_len)) - { + slap->prefix_len == ia->prefix_len)) { logerrx("%s: cannot delegte the same" - "prefix length more than once", + "prefix length more than once", sla->ifname); goto err_sla; } if (!sla->sla_set && - strcmp(slap->ifname, sla->ifname) == 0) - { + strcmp(slap->ifname, sla->ifname) == 0) { logwarnx("%s: cannot specify the " - "same interface twice with " - "an automatic SLA", + "same interface twice with " + "an automatic SLA", sla->ifname); goto err_sla; } if (slap->sla_set && sla->sla_set && - slap->sla == sla->sla) - { + slap->sla == sla->sla) { logerrx("%s: cannot" - " assign the same SLA %u" - " more than once", + " assign the same SLA %u" + " more than once", sla->ifname, sla->sla); goto err_sla; } @@ -1840,7 +1816,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, ia->sla_max = sla->sla; } break; -err_sla: + err_sla: ia->sla_len--; return -1; #endif @@ -1892,7 +1868,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, dop_len = &(*ldop)->embopts_len; } else { logerrx("embed must be after a define " - "or encap"); + "or encap"); return -1; } } @@ -2050,9 +2026,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, t |= OT_EMBED; else if (strcasecmp(arg, "encap") == 0) t |= OT_ENCAP; - else if (strcasecmp(arg, "rfc3361") ==0) + else if (strcasecmp(arg, "rfc3361") == 0) t |= OT_STRING | OT_RFC3361; - else if (strcasecmp(arg, "rfc3442") ==0) + else if (strcasecmp(arg, "rfc3442") == 0) t |= OT_STRING | OT_RFC3442; else if (strcasecmp(arg, "option") == 0) t |= OT_OPTION; @@ -2065,8 +2041,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, l = 0; } if (t & OT_ARRAY && t & (OT_STRING | OT_BINHEX) && - !(t & (OT_RFC1035 | OT_DOMAIN))) - { + !(t & (OT_RFC1035 | OT_DOMAIN))) { logwarnx("ignoring array for strings"); t &= ~OT_ARRAY; } @@ -2077,7 +2052,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, /* variable */ if (!fp) { if (!(t & OT_OPTION)) { - logerrx("type %s requires a variable name", + logerrx("type %s requires a variable name", arg); return -1; } @@ -2103,8 +2078,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, return -1; } if (opt != O_EMBED) { - for (dl = 0, ndop = *dop; dl < *dop_len; dl++, ndop++) - { + for (dl = 0, ndop = *dop; dl < *dop_len; dl++, ndop++) { /* type 0 seems freshly malloced struct * for us to use */ if (ndop->option == u || ndop->type == 0) @@ -2160,8 +2134,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, case O_VENDCLASS: ARG_REQUIRED; #ifdef SMALL - logwarnx("%s: vendor options not compiled in", ifname); - return -1; + logwarnx("%s: vendor options not compiled in", ifname); + return -1; #else fp = strwhite(arg); if (fp) @@ -2173,7 +2147,9 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, } for (vivco = ifo->vivco; vivco != vivco_endp; vivco++) { if (vivco->en == (uint32_t)u) { - logerrx("vendor class option for enterprise number %u already defined", vivco->en); + logerrx( + "vendor class option for enterprise number %u already defined", + vivco->en); return -1; } } @@ -2199,10 +2175,10 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, dl = 0; np = NULL; } - vivco = reallocarray(ifo->vivco, - ifo->vivco_len + 1, sizeof(*ifo->vivco)); + vivco = reallocarray(ifo->vivco, ifo->vivco_len + 1, + sizeof(*ifo->vivco)); if (vivco == NULL) { - logerr( __func__); + logerr(__func__); free(np); return -1; } @@ -2250,15 +2226,15 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, else np = NULL; } - if (parse_uint32(&ifo->auth.token_snd_secretid, - arg) == -1) + if (parse_uint32(&ifo->auth.token_snd_secretid, arg) == + -1) logerrx("%s: not a number", arg); else ifo->auth.token_rcv_secretid = ifo->auth.token_snd_secretid; if (np && - parse_uint32(&ifo->auth.token_rcv_secretid, - np) == -1) + parse_uint32(&ifo->auth.token_rcv_secretid, np) == + -1) logerrx("%s: not a number", arg); } else { if (strcasecmp(arg, "hmacmd5") == 0 || @@ -2278,7 +2254,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, if (strcasecmp(arg, "monocounter") == 0) { ifo->auth.rdm = AUTH_RDM_MONOTONIC; ifo->auth.options |= DHCPCD_AUTH_RDM_COUNTER; - } else if (strcasecmp(arg, "monotonic") ==0 || + } else if (strcasecmp(arg, "monotonic") == 0 || strcasecmp(arg, "monotime") == 0) ifo->auth.rdm = AUTH_RDM_MONOTONIC; else { @@ -2345,7 +2321,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, *np = '\0'; } if (strcmp(arg, "0") == 0 || strcasecmp(arg, "forever") == 0) - token->expire =0; + token->expire = 0; else { struct tm tm; @@ -2378,7 +2354,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, TAILQ_INSERT_TAIL(&ifo->auth.tokens, token, next); break; -invalid_token: + invalid_token: free(token->realm); free(token); #else @@ -2419,9 +2395,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, logerr(__func__); return -1; } - while ((i = getgrnam_r(arg, &grpbuf, p, dl, &grp)) == - ERANGE) - { + while ((i = getgrnam_r(arg, &grpbuf, p, dl, &grp)) == ERANGE) { size_t nl = dl * 2; if (nl < dl) { logerrx("control_group: out of buffer"); @@ -2545,8 +2519,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, break; case O_REQUEST_TIME: ARG_REQUIRED; - ifo->request_time = - (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e); + ifo->request_time = (uint32_t)strtou(arg, NULL, 0, 0, + UINT32_MAX, &e); if (e) { logerrx("invalid request time: %s", arg); return -1; @@ -2555,8 +2529,8 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, #ifdef INET case O_FALLBACK_TIME: ARG_REQUIRED; - ifo->fallback_time = - (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e); + ifo->fallback_time = (uint32_t)strtou(arg, NULL, 0, 0, + UINT32_MAX, &e); if (e) { logerrx("invalid fallback time: %s", arg); return -1; @@ -2564,22 +2538,20 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, break; case O_IPV4LL_TIME: ARG_REQUIRED; - ifo->ipv4ll_time = - (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, &e); + ifo->ipv4ll_time = (uint32_t)strtou(arg, NULL, 0, 0, UINT32_MAX, + &e); if (e) { logerrx("invalid ipv4ll time: %s", arg); return -1; } break; #endif - case O_NOSYSLOG: - { - unsigned int logopts = loggetopts(); + case O_NOSYSLOG: { + unsigned int logopts = loggetopts(); - logopts &= ~LOGERR_LOG; - logsetopts(logopts); - } - break; + logopts &= ~LOGERR_LOG; + logsetopts(logopts); + } break; default: return 0; } @@ -2596,14 +2568,13 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, static int parse_config_line(struct dhcpcd_ctx *ctx, const char *ifname, - struct if_options *ifo, const char *opt, char *line, - struct dhcp_opt **ldop, struct dhcp_opt **edop) + struct if_options *ifo, const char *opt, char *line, struct dhcp_opt **ldop, + struct dhcp_opt **edop) { unsigned int i; for (i = 0; i < sizeof(cf_options) / sizeof(cf_options[0]); i++) { - if (!cf_options[i].name || - strcmp(cf_options[i].name, opt) != 0) + if (!cf_options[i].name || strcmp(cf_options[i].name, opt) != 0) continue; if (cf_options[i].has_arg == required_argument && !line) { @@ -2623,7 +2594,6 @@ parse_config_line(struct dhcpcd_ctx *ctx, const char *ifname, static void finish_config(struct if_options *ifo) { - /* Terminate the encapsulated options */ if (ifo->vendor[0] && !(ifo->options & DHCPCD_VENDORRAW)) { ifo->vendor[0]++; @@ -2642,12 +2612,12 @@ finish_config(struct if_options *ifo) ifo->options &= ~(DHCPCD_DHCP | DHCPCD_IPV4LL | DHCPCD_WAITIP4); if (!(ifo->options & DHCPCD_IPV6)) - ifo->options &= - ~(DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6); + ifo->options &= ~( + DHCPCD_IPV6RS | DHCPCD_DHCP6 | DHCPCD_WAITIP6); if (!(ifo->options & DHCPCD_IPV6RS)) - ifo->options &= - ~(DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS); + ifo->options &= ~( + DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS); } static struct if_options * @@ -2687,8 +2657,8 @@ default_config(struct dhcpcd_ctx *ctx) } struct if_options * -read_config(struct dhcpcd_ctx *ctx, - const char *ifname, const char *ssid, const char *profile) +read_config(struct dhcpcd_ctx *ctx, const char *ifname, const char *ssid, + const char *profile) { struct if_options *ifo; char buf[UDPLEN_MAX], *bp; /* 64k max config file size */ @@ -2744,8 +2714,8 @@ read_config(struct dhcpcd_ctx *ctx, if (ifname == NULL && !(ctx->options & DHCPCD_PRINT_PIDFILE)) { /* Space for initial estimates */ #if defined(INET) && defined(INITDEFINES) - ifo->dhcp_override = - calloc(INITDEFINES, sizeof(*ifo->dhcp_override)); + ifo->dhcp_override = calloc(INITDEFINES, + sizeof(*ifo->dhcp_override)); if (ifo->dhcp_override == NULL) logerr(__func__); else @@ -2753,16 +2723,16 @@ read_config(struct dhcpcd_ctx *ctx, #endif #if defined(INET6) && defined(INITDEFINENDS) - ifo->nd_override = - calloc(INITDEFINENDS, sizeof(*ifo->nd_override)); + ifo->nd_override = calloc(INITDEFINENDS, + sizeof(*ifo->nd_override)); if (ifo->nd_override == NULL) logerr(__func__); else ifo->nd_override_len = INITDEFINENDS; #endif #if defined(INET6) && defined(INITDEFINE6S) - ifo->dhcp6_override = - calloc(INITDEFINE6S, sizeof(*ifo->dhcp6_override)); + ifo->dhcp6_override = calloc(INITDEFINE6S, + sizeof(*ifo->dhcp6_override)); if (ifo->dhcp6_override == NULL) logerr(__func__); else @@ -2798,13 +2768,12 @@ read_config(struct dhcpcd_ctx *ctx, /* Trim trailing whitespace */ if (line) { p = line + strlen(line) - 1; - while (p != line && - (*p == ' ' || *p == '\t') && + while (p != line && (*p == ' ' || *p == '\t') && *(p - 1) != '\\') *p-- = '\0'; } - parse_config_line(ctx, NULL, ifo, option, line, - &ldop, &edop); + parse_config_line(ctx, NULL, ifo, option, line, &ldop, + &edop); } #ifdef INET @@ -2812,8 +2781,7 @@ read_config(struct dhcpcd_ctx *ctx, ctx->dhcp_opts_len = ifo->dhcp_override_len; #else for (i = 0, opt = ifo->dhcp_override; - i < ifo->dhcp_override_len; - i++, opt++) + i < ifo->dhcp_override_len; i++, opt++) free_dhcp_opt_embenc(opt); free(ifo->dhcp_override); #endif @@ -2828,14 +2796,12 @@ read_config(struct dhcpcd_ctx *ctx, ctx->dhcp6_opts_len = ifo->dhcp6_override_len; #endif #else - for (i = 0, opt = ifo->nd_override; - i < ifo->nd_override_len; + for (i = 0, opt = ifo->nd_override; i < ifo->nd_override_len; i++, opt++) free_dhcp_opt_embenc(opt); free(ifo->nd_override); for (i = 0, opt = ifo->dhcp6_override; - i < ifo->dhcp6_override_len; - i++, opt++) + i < ifo->dhcp6_override_len; i++, opt++) free_dhcp_opt_embenc(opt); free(ifo->dhcp6_override); #endif @@ -2876,8 +2842,7 @@ read_config(struct dhcpcd_ctx *ctx, /* Trim trailing whitespace */ if (line) { p = line + strlen(line) - 1; - while (p != line && - (*p == ' ' || *p == '\t') && + while (p != line && (*p == ' ' || *p == '\t') && *(p - 1) != '\\') *p-- = '\0'; } @@ -2905,8 +2870,8 @@ read_config(struct dhcpcd_ctx *ctx, if (ifname) continue; - n = reallocarray(ctx->ifcv, - (size_t)ctx->ifcc + 1, sizeof(char *)); + n = reallocarray(ctx->ifcv, (size_t)ctx->ifcc + 1, + sizeof(char *)); if (n == NULL) { logerr(__func__); continue; @@ -2963,8 +2928,8 @@ read_config(struct dhcpcd_ctx *ctx, } int -add_options(struct dhcpcd_ctx *ctx, const char *ifname, - struct if_options *ifo, int argc, char **argv) +add_options(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, + int argc, char **argv) { int oi, opt, r; unsigned long long wait_opts; @@ -2978,10 +2943,10 @@ add_options(struct dhcpcd_ctx *ctx, const char *ifname, * only use the dhcpcd.conf entry for that. */ if (ifname != NULL) wait_opts = ifo->options & DHCPCD_WAITOPTS; - while ((opt = getopt_long(argc, argv, - ctx->options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, - cf_options, &oi)) != -1) - { + while ( + (opt = getopt_long(argc, argv, + ctx->options & DHCPCD_PRINT_PIDFILE ? NOERR_IF_OPTS : IF_OPTS, + cf_options, &oi)) != -1) { r = parse_option(ctx, ifname, ifo, opt, optarg, NULL, NULL); if (r != 1) break; @@ -2995,13 +2960,13 @@ add_options(struct dhcpcd_ctx *ctx, const char *ifname, return r; } -char -**alloc_args(int argc, char **argv) +char ** +alloc_args(int argc, char **argv) { int i; size_t strslen = 0, len; size_t nptrs = (size_t)argc; - size_t ptrslen = nptrs * sizeof(char *); + size_t ptrslen = nptrs * sizeof(char *); void *buf; char **ptrs, *strsp; @@ -3077,7 +3042,8 @@ free_options(struct dhcpcd_ctx *ctx, struct if_options *ifo) * free list. */ ifp = ctx->ifaces != NULL ? TAILQ_FIRST(ctx->ifaces) : NULL; if (ifp != NULL) { - RB_TREE_FOREACH(rt, &ifo->routes) { + RB_TREE_FOREACH(rt, &ifo->routes) + { if (rt->rt_ifp == NULL) rt->rt_ifp = ifp; } @@ -3089,52 +3055,38 @@ free_options(struct dhcpcd_ctx *ctx, struct if_options *ifo) free(ifo->blacklist); free(ifo->fallback); - for (opt = ifo->dhcp_override; - ifo->dhcp_override_len > 0; + for (opt = ifo->dhcp_override; ifo->dhcp_override_len > 0; opt++, ifo->dhcp_override_len--) free_dhcp_opt_embenc(opt); free(ifo->dhcp_override); - for (opt = ifo->nd_override; - ifo->nd_override_len > 0; + for (opt = ifo->nd_override; ifo->nd_override_len > 0; opt++, ifo->nd_override_len--) free_dhcp_opt_embenc(opt); free(ifo->nd_override); - for (opt = ifo->dhcp6_override; - ifo->dhcp6_override_len > 0; + for (opt = ifo->dhcp6_override; ifo->dhcp6_override_len > 0; opt++, ifo->dhcp6_override_len--) free_dhcp_opt_embenc(opt); free(ifo->dhcp6_override); #ifndef SMALL - for (vo = ifo->vivco; - ifo->vivco_len > 0; - vo++, ifo->vivco_len--) + for (vo = ifo->vivco; ifo->vivco_len > 0; vo++, ifo->vivco_len--) free(vo->data); free(ifo->vivco); - for (vsio = ifo->vsio; - ifo->vsio_len > 0; - vsio++, ifo->vsio_len--) - { - for (vsio_so = vsio->so; - vsio->so_len > 0; + for (vsio = ifo->vsio; ifo->vsio_len > 0; vsio++, ifo->vsio_len--) { + for (vsio_so = vsio->so; vsio->so_len > 0; vsio_so++, vsio->so_len--) free(vsio_so->data); free(vsio->so); } free(ifo->vsio); - for (vsio = ifo->vsio6; - ifo->vsio6_len > 0; - vsio++, ifo->vsio6_len--) - { - for (vsio_so = vsio->so; - vsio->so_len > 0; + for (vsio = ifo->vsio6; ifo->vsio6_len > 0; vsio++, ifo->vsio6_len--) { + for (vsio_so = vsio->so; vsio->so_len > 0; vsio_so++, vsio->so_len--) free(vsio_so->data); free(vsio->so); } free(ifo->vsio6); #endif - for (opt = ifo->vivso_override; - ifo->vivso_override_len > 0; + for (opt = ifo->vivso_override; ifo->vivso_override_len > 0; opt++, ifo->vivso_override_len--) free_dhcp_opt_embenc(opt); free(ifo->vivso_override); diff --git a/src/if-options.h b/src/if-options.h index 51d282dc..f96edbc8 100644 --- a/src/if-options.h +++ b/src/if-options.h @@ -31,6 +31,7 @@ #include #include + #include #include @@ -43,44 +44,45 @@ /* Don't set any optional arguments here so we retain POSIX * compatibility with getopt */ -#define IF_OPTS "146bc:de:f:gh:i:j:kl:m:no:pqr:s:t:u:v:wxy:z:" \ - "ABC:DEF:GHI:JKLMNO:PQ:S:TUVW:X:Z:" -#define NOERR_IF_OPTS ":" IF_OPTS +#define IF_OPTS \ + "146bc:de:f:gh:i:j:kl:m:no:pqr:s:t:u:v:wxy:z:" \ + "ABC:DEF:GHI:JKLMNO:PQ:S:TUVW:X:Z:" +#define NOERR_IF_OPTS ":" IF_OPTS -#define DEFAULT_TIMEOUT 30 -#define DEFAULT_REBOOT 5 -#define DEFAULT_REQUEST 180 /* secs to request, mirror DHCP6 */ -#define DEFAULT_FALLBACK 5 /* secs until fallback */ -#define DEFAULT_IPV4LL 5 /* secs until ipv4ll */ +#define DEFAULT_TIMEOUT 30 +#define DEFAULT_REBOOT 5 +#define DEFAULT_REQUEST 180 /* secs to request, mirror DHCP6 */ +#define DEFAULT_FALLBACK 5 /* secs until fallback */ +#define DEFAULT_IPV4LL 5 /* secs until ipv4ll */ #ifndef HOSTNAME_MAX_LEN -#define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */ +#define HOSTNAME_MAX_LEN 250 /* 255 - 3 (FQDN) - 2 (DNS enc) */ #endif -#define DHCP_OPTION_MAX_LEN 255 +#define DHCP_OPTION_MAX_LEN 255 -#define DHCPCD_ARP (1ULL << 0) -#define DHCPCD_RELEASE (1ULL << 1) -#define DHCPCD_RTBUILD (1ULL << 2) -#define DHCPCD_GATEWAY (1ULL << 3) -#define DHCPCD_STATIC (1ULL << 4) +#define DHCPCD_ARP (1ULL << 0) +#define DHCPCD_RELEASE (1ULL << 1) +#define DHCPCD_RTBUILD (1ULL << 2) +#define DHCPCD_GATEWAY (1ULL << 3) +#define DHCPCD_STATIC (1ULL << 4) // unused (1ULL << 5) -#define DHCPCD_ARP_PERSISTDEFENCE (1ULL << 6) -#define DHCPCD_LASTLEASE (1ULL << 7) -#define DHCPCD_INFORM (1ULL << 8) -#define DHCPCD_REQUEST (1ULL << 9) -#define DHCPCD_IPV4LL (1ULL << 10) -#define DHCPCD_DUID (1ULL << 11) -#define DHCPCD_PERSISTENT (1ULL << 12) -#define DHCPCD_DAEMONISE (1ULL << 14) -#define DHCPCD_DAEMONISED (1ULL << 15) -#define DHCPCD_TEST (1ULL << 16) -#define DHCPCD_MANAGER (1ULL << 17) -#define DHCPCD_HOSTNAME (1ULL << 18) -#define DHCPCD_CLIENTID (1ULL << 19) -#define DHCPCD_LINK (1ULL << 20) -#define DHCPCD_ANONYMOUS (1ULL << 21) -#define DHCPCD_BACKGROUND (1ULL << 22) -#define DHCPCD_VENDORRAW (1ULL << 23) +#define DHCPCD_ARP_PERSISTDEFENCE (1ULL << 6) +#define DHCPCD_LASTLEASE (1ULL << 7) +#define DHCPCD_INFORM (1ULL << 8) +#define DHCPCD_REQUEST (1ULL << 9) +#define DHCPCD_IPV4LL (1ULL << 10) +#define DHCPCD_DUID (1ULL << 11) +#define DHCPCD_PERSISTENT (1ULL << 12) +#define DHCPCD_DAEMONISE (1ULL << 14) +#define DHCPCD_DAEMONISED (1ULL << 15) +#define DHCPCD_TEST (1ULL << 16) +#define DHCPCD_MANAGER (1ULL << 17) +#define DHCPCD_HOSTNAME (1ULL << 18) +#define DHCPCD_CLIENTID (1ULL << 19) +#define DHCPCD_LINK (1ULL << 20) +#define DHCPCD_ANONYMOUS (1ULL << 21) +#define DHCPCD_BACKGROUND (1ULL << 22) +#define DHCPCD_VENDORRAW (1ULL << 23) // unused (1ULL << 24) #define DHCPCD_WAITIP (1ULL << 25) #define DHCPCD_SLAACPRIVATE (1ULL << 26) @@ -112,84 +114,83 @@ #define DHCPCD_INFORM6 (1ULL << 52) #define DHCPCD_WANTDHCP (1ULL << 53) #define DHCPCD_IPV6RA_AUTOCONF (1ULL << 54) -#define DHCPCD_ROUTER_HOST_ROUTE_WARNED (1ULL << 55) +#define DHCPCD_ROUTER_HOST_ROUTE_WARNED (1ULL << 55) #define DHCPCD_LASTLEASE_EXTEND (1ULL << 56) #define DHCPCD_BOOTP (1ULL << 57) #define DHCPCD_INITIAL_DELAY (1ULL << 58) #define DHCPCD_PRINT_PIDFILE (1ULL << 59) #define DHCPCD_ONESHOT (1ULL << 60) #define DHCPCD_INACTIVE (1ULL << 61) -#define DHCPCD_SLAACTEMP (1ULL << 62) +#define DHCPCD_SLAACTEMP (1ULL << 62) #define DHCPCD_PRIVSEPROOT (1ULL << 63) -#define DHCPCD_NODROP (DHCPCD_EXITING | DHCPCD_PERSISTENT) +#define DHCPCD_NODROP (DHCPCD_EXITING | DHCPCD_PERSISTENT) -#define DHCPCD_WAITOPTS (DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6) +#define DHCPCD_WAITOPTS (DHCPCD_WAITIP | DHCPCD_WAITIP4 | DHCPCD_WAITIP6) -#define DHCPCD_WARNINGS (DHCPCD_CSR_WARNED | \ - DHCPCD_ROUTER_HOST_ROUTE_WARNED) +#define DHCPCD_WARNINGS (DHCPCD_CSR_WARNED | DHCPCD_ROUTER_HOST_ROUTE_WARNED) /* These options only make sense in the config file, so don't use any valid short options for them */ -#define O_BASE MAX('z', 'Z') + 1 -#define O_ARPING O_BASE + 1 -#define O_FALLBACK O_BASE + 2 -#define O_DESTINATION O_BASE + 3 -#define O_IPV6RS O_BASE + 4 -#define O_NOIPV6RS O_BASE + 5 -#define O_IPV6RA_FORK O_BASE + 6 -#define O_LINK_RCVBUF O_BASE + 7 -#define O_ANONYMOUS O_BASE + 8 -#define O_NOALIAS O_BASE + 9 -#define O_IA_NA O_BASE + 10 -#define O_IA_TA O_BASE + 11 -#define O_IA_PD O_BASE + 12 -#define O_HOSTNAME_SHORT O_BASE + 13 -#define O_DEV O_BASE + 14 -#define O_NODEV O_BASE + 15 -#define O_NOIPV4 O_BASE + 16 -#define O_NOIPV6 O_BASE + 17 -#define O_IAID O_BASE + 18 -#define O_DEFINE O_BASE + 19 -#define O_DEFINE6 O_BASE + 20 -#define O_EMBED O_BASE + 21 -#define O_ENCAP O_BASE + 22 -#define O_VENDOPT O_BASE + 23 -#define O_VENDCLASS O_BASE + 24 -#define O_AUTHPROTOCOL O_BASE + 25 -#define O_AUTHTOKEN O_BASE + 26 -#define O_AUTHNOTREQUIRED O_BASE + 27 -#define O_NODHCP O_BASE + 28 -#define O_NODHCP6 O_BASE + 29 -#define O_DHCP O_BASE + 30 -#define O_DHCP6 O_BASE + 31 -#define O_IPV4 O_BASE + 32 -#define O_IPV6 O_BASE + 33 -#define O_CONTROLGRP O_BASE + 34 -#define O_SLAAC O_BASE + 35 -#define O_GATEWAY O_BASE + 36 -#define O_NOUP O_BASE + 37 -#define O_IPV6RA_AUTOCONF O_BASE + 38 -#define O_IPV6RA_NOAUTOCONF O_BASE + 39 -#define O_REJECT O_BASE + 40 -#define O_BOOTP O_BASE + 42 -#define O_DEFINEND O_BASE + 43 -#define O_NODELAY O_BASE + 44 -#define O_INFORM6 O_BASE + 45 -#define O_LASTLEASE_EXTEND O_BASE + 46 -#define O_INACTIVE O_BASE + 47 -#define O_MUDURL O_BASE + 48 -#define O_MSUSERCLASS O_BASE + 49 -#define O_CONFIGURE O_BASE + 50 -#define O_NOCONFIGURE O_BASE + 51 -#define O_RANDOMISE_HWADDR O_BASE + 52 -#define O_ARP_PERSISTDEFENCE O_BASE + 53 -#define O_REQUEST_TIME O_BASE + 54 -#define O_FALLBACK_TIME O_BASE + 55 -#define O_IPV4LL_TIME O_BASE + 56 -#define O_VSIO O_BASE + 57 -#define O_VSIO6 O_BASE + 58 -#define O_NOSYSLOG O_BASE + 59 +#define O_BASE MAX('z', 'Z') + 1 +#define O_ARPING O_BASE + 1 +#define O_FALLBACK O_BASE + 2 +#define O_DESTINATION O_BASE + 3 +#define O_IPV6RS O_BASE + 4 +#define O_NOIPV6RS O_BASE + 5 +#define O_IPV6RA_FORK O_BASE + 6 +#define O_LINK_RCVBUF O_BASE + 7 +#define O_ANONYMOUS O_BASE + 8 +#define O_NOALIAS O_BASE + 9 +#define O_IA_NA O_BASE + 10 +#define O_IA_TA O_BASE + 11 +#define O_IA_PD O_BASE + 12 +#define O_HOSTNAME_SHORT O_BASE + 13 +#define O_DEV O_BASE + 14 +#define O_NODEV O_BASE + 15 +#define O_NOIPV4 O_BASE + 16 +#define O_NOIPV6 O_BASE + 17 +#define O_IAID O_BASE + 18 +#define O_DEFINE O_BASE + 19 +#define O_DEFINE6 O_BASE + 20 +#define O_EMBED O_BASE + 21 +#define O_ENCAP O_BASE + 22 +#define O_VENDOPT O_BASE + 23 +#define O_VENDCLASS O_BASE + 24 +#define O_AUTHPROTOCOL O_BASE + 25 +#define O_AUTHTOKEN O_BASE + 26 +#define O_AUTHNOTREQUIRED O_BASE + 27 +#define O_NODHCP O_BASE + 28 +#define O_NODHCP6 O_BASE + 29 +#define O_DHCP O_BASE + 30 +#define O_DHCP6 O_BASE + 31 +#define O_IPV4 O_BASE + 32 +#define O_IPV6 O_BASE + 33 +#define O_CONTROLGRP O_BASE + 34 +#define O_SLAAC O_BASE + 35 +#define O_GATEWAY O_BASE + 36 +#define O_NOUP O_BASE + 37 +#define O_IPV6RA_AUTOCONF O_BASE + 38 +#define O_IPV6RA_NOAUTOCONF O_BASE + 39 +#define O_REJECT O_BASE + 40 +#define O_BOOTP O_BASE + 42 +#define O_DEFINEND O_BASE + 43 +#define O_NODELAY O_BASE + 44 +#define O_INFORM6 O_BASE + 45 +#define O_LASTLEASE_EXTEND O_BASE + 46 +#define O_INACTIVE O_BASE + 47 +#define O_MUDURL O_BASE + 48 +#define O_MSUSERCLASS O_BASE + 49 +#define O_CONFIGURE O_BASE + 50 +#define O_NOCONFIGURE O_BASE + 51 +#define O_RANDOMISE_HWADDR O_BASE + 52 +#define O_ARP_PERSISTDEFENCE O_BASE + 53 +#define O_REQUEST_TIME O_BASE + 54 +#define O_FALLBACK_TIME O_BASE + 55 +#define O_IPV4LL_TIME O_BASE + 56 +#define O_VSIO O_BASE + 57 +#define O_VSIO6 O_BASE + 58 +#define O_NOSYSLOG O_BASE + 59 extern const struct option cf_options[]; @@ -315,10 +316,10 @@ struct if_options { struct auth auth; }; -struct if_options *read_config(struct dhcpcd_ctx *, - const char *, const char *, const char *); -int add_options(struct dhcpcd_ctx *, const char *, - struct if_options *, int, char **); +struct if_options *read_config(struct dhcpcd_ctx *, const char *, const char *, + const char *); +int add_options(struct dhcpcd_ctx *, const char *, struct if_options *, int, + char **); void free_dhcp_opt_embenc(struct dhcp_opt *); void free_options(struct dhcpcd_ctx *, struct if_options *); diff --git a/src/if-sun.c b/src/if-sun.c index 24ef5b58..43f6575d 100644 --- a/src/if-sun.c +++ b/src/if-sun.c @@ -26,43 +26,41 @@ * SUCH DAMAGE. */ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + #include #include #include #include -#include +#include #include +#include #include #include -#include #include +#include #include -#include - -#include -#include - -#include -#include - -#include -#include -#include -#include -#include - /* Private libsocket interface we can hook into to get * a better getifaddrs(3). * From libsocket_priv.h, which is not always distributed so is here. */ extern int getallifaddrs(sa_family_t, struct ifaddrs **, int64_t); -#include "config.h" #include "bpf.h" #include "common.h" +#include "config.h" #include "dhcp.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv6.h" #include "ipv6nd.h" @@ -71,31 +69,34 @@ extern int getallifaddrs(sa_family_t, struct ifaddrs **, int64_t); #include "sa.h" #ifndef ARP_MOD_NAME -# define ARP_MOD_NAME "arp" +#define ARP_MOD_NAME "arp" #endif #ifndef RT_ROUNDUP -#define RT_ROUNDUP(a) \ - ((a) > 0 ? (1 + (((a) - 1) | (sizeof(int32_t) - 1))) : sizeof(int32_t)) +#define RT_ROUNDUP(a) \ + ((a) > 0 ? (1 + (((a) - 1) | (sizeof(int32_t) - 1))) : sizeof(int32_t)) #define RT_ADVANCE(x, n) ((x) += RT_ROUNDUP(sa_len((n)))) #endif -#define COPYOUT(sin, sa) do { \ - if ((sa) && ((sa)->sa_family == AF_INET)) \ - (sin) = ((const struct sockaddr_in *)(const void *) \ - (sa))->sin_addr; \ +#define COPYOUT(sin, sa) \ + do { \ + if ((sa) && ((sa)->sa_family == AF_INET)) \ + (sin) = \ + ((const struct sockaddr_in *)(const void *)(sa)) \ + ->sin_addr; \ } while (0) -#define COPYOUT6(sin, sa) do { \ - if ((sa) && ((sa)->sa_family == AF_INET6)) \ - (sin) = ((const struct sockaddr_in6 *)(const void *) \ - (sa))->sin6_addr; \ +#define COPYOUT6(sin, sa) \ + do { \ + if ((sa) && ((sa)->sa_family == AF_INET6)) \ + (sin) = \ + ((const struct sockaddr_in6 *)(const void *)(sa)) \ + ->sin6_addr; \ } while (0) #define COPYSA(dst, src) memcpy((dst), (src), sa_len((src))) -struct rtm -{ +struct rtm { struct rt_msghdr hdr; char buffer[sizeof(struct sockaddr_storage) * RTAX_MAX]; }; @@ -111,7 +112,6 @@ os_init(void) int if_init(struct interface *ifp) { - #ifdef INET if (if_plumb(RTM_NEWADDR, ifp->ctx, AF_INET, ifp->name) == -1 && errno != EEXIST) @@ -133,15 +133,14 @@ if_init(struct interface *ifp) int if_conf(__unused struct interface *ifp) { - return 0; } int if_opensockets_os(struct dhcpcd_ctx *ctx) { - struct priv *priv; - int n; + struct priv *priv; + int n; if ((priv = malloc(sizeof(*priv))) == NULL) return -1; @@ -166,8 +165,8 @@ if_opensockets_os(struct dhcpcd_ctx *ctx) * Sadly there is no way of doing this for route(4) messages * generated from addresses we add/delete. */ n = 0; - if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK, - &n, sizeof(n)) == -1) + if (setsockopt(ctx->link_fd, SOL_SOCKET, SO_USELOOPBACK, &n, + sizeof(n)) == -1) logerr("%s: SO_USELOOPBACK", __func__); return 0; @@ -177,7 +176,7 @@ void if_closesockets_os(struct dhcpcd_ctx *ctx) { #ifdef INET6 - struct priv *priv; + struct priv *priv; priv = (struct priv *)ctx->priv; if (priv && priv->pf_inet6_fd != -1) @@ -192,7 +191,6 @@ if_closesockets_os(struct dhcpcd_ctx *ctx) int if_setmac(struct interface *ifp, void *mac, uint8_t maclen) { - errno = ENOTSUP; return -1; } @@ -200,10 +198,10 @@ if_setmac(struct interface *ifp, void *mac, uint8_t maclen) int if_carrier(struct interface *ifp, __unused const void *ifadata) { - kstat_ctl_t *kcp; - kstat_t *ksp; - kstat_named_t *knp; - link_state_t linkstate; + kstat_ctl_t *kcp; + kstat_t *ksp; + kstat_named_t *knp; + link_state_t linkstate; kcp = kstat_open(); if (kcp == NULL) @@ -240,16 +238,15 @@ if_carrier(struct interface *ifp, __unused const void *ifadata) bool if_roaming(__unused struct interface *ifp) { - return false; } int if_mtu_os(const struct interface *ifp) { - dlpi_handle_t dh; - dlpi_info_t dlinfo; - int mtu; + dlpi_handle_t dh; + dlpi_info_t dlinfo; + int mtu; if (dlpi_open(ifp->name, &dh, 0) != DLPI_SUCCESS) return -1; @@ -264,7 +261,6 @@ if_mtu_os(const struct interface *ifp) int if_getssid(__unused struct interface *ifp) { - errno = ENOTSUP; return -1; } @@ -273,49 +269,45 @@ if_getssid(__unused struct interface *ifp) bool if_ignore(__unused struct dhcpcd_ctx *ctx, __unused const char *ifname) { - return false; } unsigned short if_vlanid(__unused const struct interface *ifp) { - return 0; } int if_vimaster(__unused struct dhcpcd_ctx *ctx, __unused const char *ifname) { - return 0; } int if_machinearch(__unused char *str, __unused size_t len) { - /* There is no extra data really. * isainfo -v does return amd64, but also i386. */ return 0; } struct linkwalk { - struct ifaddrs *lw_ifa; - int lw_error; + struct ifaddrs *lw_ifa; + int lw_error; }; static boolean_t if_newaddr(const char *ifname, void *arg) { - struct linkwalk *lw = arg; + struct linkwalk *lw = arg; int error; - struct ifaddrs *ifa; - dlpi_handle_t dh; - dlpi_info_t dlinfo; - uint8_t pa[DLPI_PHYSADDR_MAX]; - size_t pa_len; - struct sockaddr_dl *sdl; + struct ifaddrs *ifa; + dlpi_handle_t dh; + dlpi_info_t dlinfo; + uint8_t pa[DLPI_PHYSADDR_MAX]; + size_t pa_len; + struct sockaddr_dl *sdl; ifa = NULL; error = dlpi_open(ifname, &dh, 0); @@ -330,8 +322,8 @@ if_newaddr(const char *ifname, void *arg) * physical address, it's all zero's. * So cal dlpi_get_physaddr. */ pa_len = DLPI_PHYSADDR_MAX; - if (dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, - pa, &pa_len) != DLPI_SUCCESS) + if (dlpi_get_physaddr(dh, DL_CURR_PHYS_ADDR, pa, &pa_len) != + DLPI_SUCCESS) goto failed; if ((ifa = calloc(1, sizeof(*ifa))) == NULL) @@ -380,8 +372,8 @@ if_newaddr(const char *ifname, void *arg) static struct ifaddrs * if_ifa_lo0(void) { - struct ifaddrs *ifa; - struct sockaddr_dl *sdl; + struct ifaddrs *ifa; + struct sockaddr_dl *sdl; if ((ifa = calloc(1, sizeof(*ifa))) == NULL) return NULL; @@ -409,8 +401,8 @@ if_ifa_lo0(void) int if_getifaddrs(struct ifaddrs **ifap) { - struct linkwalk lw; - struct ifaddrs *ifa; + struct linkwalk lw; + struct ifaddrs *ifa; /* Private libc function which we should not have to call * to get non UP addresses. */ @@ -440,7 +432,6 @@ if_getifaddrs(struct ifaddrs **ifap) static void if_linkaddr(struct sockaddr_dl *sdl, const struct interface *ifp) { - memset(sdl, 0, sizeof(*sdl)); sdl->sdl_family = AF_LINK; sdl->sdl_nlen = sdl->sdl_alen = sdl->sdl_slen = 0; @@ -474,7 +465,6 @@ get_addrs(int type, const void *data, size_t data_len, static struct interface * if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl) { - if (sdl->sdl_index) return if_findindex(ctx->ifaces, sdl->sdl_index); @@ -490,8 +480,8 @@ if_findsdl(struct dhcpcd_ctx *ctx, const struct sockaddr_dl *sdl) TAILQ_FOREACH(ifp, ctx->ifaces, next) { if (ifp->hwlen == sdl->sdl_alen && - memcmp(ifp->hwaddr, - sdl->sdl_data, sdl->sdl_alen) == 0) + memcmp(ifp->hwaddr, sdl->sdl_data, sdl->sdl_alen) == + 0) return ifp; } } @@ -509,16 +499,14 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) } switch (sa->sa_family) { - case AF_LINK: - { + case AF_LINK: { const struct sockaddr_dl *sdl; sdl = (const void *)sa; return if_findsdl(ctx, sdl); } #ifdef INET - case AF_INET: - { + case AF_INET: { const struct sockaddr_in *sin; struct ipv4_addr *ia; @@ -531,8 +519,7 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) } #endif #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { const struct sockaddr_in6 *sin; struct ipv6_addr *ia; @@ -554,8 +541,8 @@ if_findsa(struct dhcpcd_ctx *ctx, const struct sockaddr *sa) } static void -if_route0(struct dhcpcd_ctx *ctx, struct rtm *rtmsg, - unsigned char cmd, const struct rt *rt) +if_route0(struct dhcpcd_ctx *ctx, struct rtm *rtmsg, unsigned char cmd, + const struct rt *rt) { struct rt_msghdr *rtm; char *bp = rtmsg->buffer; @@ -565,10 +552,11 @@ if_route0(struct dhcpcd_ctx *ctx, struct rtm *rtmsg, /* WARNING: Solaris will not allow you to delete RTF_KERNEL routes. * This includes subnet/prefix routes. */ -#define ADDSA(sa) do { \ - sl = sa_len((sa)); \ - memcpy(bp, (sa), sl); \ - bp += RT_ROUNDUP(sl); \ +#define ADDSA(sa) \ + do { \ + sl = sa_len((sa)); \ + memcpy(bp, (sa), sl); \ + bp += RT_ROUNDUP(sl); \ } while (/* CONSTCOND */ 0) memset(rtmsg, 0, sizeof(*rtmsg)); @@ -586,8 +574,7 @@ if_route0(struct dhcpcd_ctx *ctx, struct rtm *rtmsg, rtm->rtm_flags |= RTF_UP; if (!(rtm->rtm_flags & RTF_REJECT) && - !sa_is_loopback(&rt->rt_gateway)) - { + !sa_is_loopback(&rt->rt_gateway)) { rtm->rtm_addrs |= RTA_IFP; /* RTA_IFA is currently ignored by the kernel. * RTA_SRC and RTF_SETSRC look like what we want, @@ -667,7 +654,7 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) } if (get_addrs(rtm->rtm_addrs, (const char *)rtm + sizeof(*rtm), - rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1) + rtm->rtm_msglen - sizeof(*rtm), rti_info) == -1) return -1; memset(rt, 0, sizeof(*rt)); @@ -683,8 +670,8 @@ if_copyrt(struct dhcpcd_ctx *ctx, struct rt *rt, const struct rt_msghdr *rtm) if (rti_info[RTAX_GATEWAY]->sa_family == AF_LINK) { const struct sockaddr_dl *sdl; - sdl = (const struct sockaddr_dl*) - (const void *)rti_info[RTAX_GATEWAY]; + sdl = (const struct sockaddr_dl *)(const void *) + rti_info[RTAX_GATEWAY]; if (sdl->sdl_alen != 0) rt->rt_dflags |= RTDF_GATELINK; } else if (rtm->rtm_flags & RTF_GATEWAY) @@ -757,12 +744,10 @@ if_finishrt(struct dhcpcd_ctx *ctx, struct rt *rt) * dhcpcd has a blank gateway here to indicate a * subnet route. */ if (!sa_is_unspecified(&rt->rt_dest) && - !sa_is_unspecified(&rt->rt_gateway)) - { - switch(rt->rt_gateway.sa_family) { + !sa_is_unspecified(&rt->rt_gateway)) { + switch (rt->rt_gateway.sa_family) { #ifdef INET - case AF_INET: - { + case AF_INET: { struct in_addr *in; in = &satosin(&rt->rt_gateway)->sin_addr; @@ -772,8 +757,7 @@ if_finishrt(struct dhcpcd_ctx *ctx, struct rt *rt) } #endif #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { struct in6_addr *in6; in6 = &satosin6(&rt->rt_gateway)->sin6_addr; @@ -792,7 +776,7 @@ if_finishrt(struct dhcpcd_ctx *ctx, struct rt *rt) if (if_route_get(ctx, rt) == NULL) { rt->rt_ifp = if_loopback(ctx); if (rt->rt_ifp == NULL) - return - 1; + return -1; } } @@ -812,7 +796,7 @@ if_finishrt(struct dhcpcd_ctx *ctx, struct rt *rt) static int if_addrflags0(int fd, int af, const char *ifname) { - struct lifreq lifr; + struct lifreq lifr; int flags; memset(&lifr, 0, sizeof(lifr)); @@ -822,9 +806,10 @@ if_addrflags0(int fd, int af, const char *ifname) flags = 0; if (lifr.lifr_flags & IFF_DUPLICATE) - flags |= af == AF_INET6 ? IN6_IFF_DUPLICATED:IN_IFF_DUPLICATED; + flags |= af == AF_INET6 ? IN6_IFF_DUPLICATED : + IN_IFF_DUPLICATED; else if (!(lifr.lifr_flags & IFF_UP)) - flags |= af == AF_INET6 ? IN6_IFF_TENTATIVE:IN_IFF_TENTATIVE; + flags |= af == AF_INET6 ? IN6_IFF_TENTATIVE : IN_IFF_TENTATIVE; return flags; } @@ -851,12 +836,11 @@ if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) */ if (rt.rt_dest.sa_family == AF_INET6 && (rt.rt_flags & RTF_HOST || rtm->rtm_type == RTM_MISS) && - !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK))) - { + !(rtm->rtm_type == RTM_ADD && !(rt.rt_dflags & RTDF_GATELINK))) { bool reachable; reachable = (rtm->rtm_type == RTM_ADD || - rtm->rtm_type == RTM_CHANGE) && + rtm->rtm_type == RTM_CHANGE) && rt.rt_dflags & RTDF_GATELINK; ipv6nd_neighbour(ctx, &rt.rt_ss_dest.sin6.sin6_addr, reachable); } @@ -871,9 +855,9 @@ if_rtm(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) static bool if_getalias(struct interface *ifp, const struct sockaddr *sa, char *alias) { - struct ifaddrs *ifaddrs, *ifa; - struct interface *ifpx; - bool found; + struct ifaddrs *ifaddrs, *ifa; + struct interface *ifpx; + bool found; ifaddrs = NULL; if (getallifaddrs(sa->sa_family, &ifaddrs, 0) == -1) @@ -914,10 +898,10 @@ if_getbrdaddr(struct dhcpcd_ctx *ctx, const char *ifname, struct in_addr *brd) static int if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) { - struct interface *ifp; - const struct sockaddr *sa, *rti_info[RTAX_MAX]; - int flags; - char ifalias[IF_NAMESIZE]; + struct interface *ifp; + const struct sockaddr *sa, *rti_info[RTAX_MAX]; + int flags; + char ifalias[IF_NAMESIZE]; if (ifam->ifam_msglen < sizeof(*ifam)) { errno = EINVAL; @@ -927,7 +911,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) return 0; if (get_addrs(ifam->ifam_addrs, (const char *)ifam + sizeof(*ifam), - ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1) + ifam->ifam_msglen - sizeof(*ifam), rti_info) == -1) return -1; sa = rti_info[RTAX_IFA]; @@ -954,21 +938,19 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) return 0; switch (sa->sa_family) { - case AF_LINK: - { + case AF_LINK: { struct sockaddr_dl sdl; if (ifam->ifam_type != RTM_CHGADDR && ifam->ifam_type != RTM_NEWADDR) break; memcpy(&sdl, rti_info[RTAX_IFA], sizeof(sdl)); - dhcpcd_handlehwaddr(ifp, ifp->hwtype, - CLLADDR(&sdl), sdl.sdl_alen); + dhcpcd_handlehwaddr(ifp, ifp->hwtype, CLLADDR(&sdl), + sdl.sdl_alen); break; } #ifdef INET - case AF_INET: - { + case AF_INET: { struct in_addr addr, mask, bcast; COPYOUT(addr, rti_info[RTAX_IFA]); @@ -996,18 +978,17 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) return 0; ipv4_handleifa(ctx, - ifam->ifam_type == RTM_CHGADDR ? - RTM_NEWADDR : ifam->ifam_type, + ifam->ifam_type == RTM_CHGADDR ? RTM_NEWADDR : + ifam->ifam_type, NULL, ifalias, &addr, &mask, &bcast, flags, 0); break; } #endif #ifdef INET6 - case AF_INET6: - { - struct in6_addr addr6, mask6; - const struct in6_addr *dstaddr6; - const struct sockaddr_in6 *sin6; + case AF_INET6: { + struct in6_addr addr6, mask6; + const struct in6_addr *dstaddr6; + const struct sockaddr_in6 *sin6; sin6 = (const void *)rti_info[RTAX_IFA]; addr6 = sin6->sin6_addr; @@ -1017,7 +998,7 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) dstaddr6 = sin6 ? &sin6->sin6_addr : NULL; if (ifam->ifam_type == RTM_DELADDR) { - struct ipv6_addr *ia; + struct ipv6_addr *ia; ia = ipv6_iffindaddr(ifp, &addr6, 0); if (ia == NULL) @@ -1032,10 +1013,10 @@ if_ifa(struct dhcpcd_ctx *ctx, const struct ifa_msghdr *ifam) return 0; ipv6_handleifa(ctx, - ifam->ifam_type == RTM_CHGADDR ? - RTM_NEWADDR : ifam->ifam_type, - NULL, ifalias, &addr6, ipv6_prefixlen(&mask6), - dstaddr6, flags, 0); + ifam->ifam_type == RTM_CHGADDR ? RTM_NEWADDR : + ifam->ifam_type, + NULL, ifalias, &addr6, ipv6_prefixlen(&mask6), dstaddr6, + flags, 0); break; } #endif @@ -1073,20 +1054,19 @@ if_ifinfo(struct dhcpcd_ctx *ctx, const struct if_msghdr *ifm) static int if_dispatch(struct dhcpcd_ctx *ctx, const struct rt_msghdr *rtm) { - if (rtm->rtm_version != RTM_VERSION) return 0; - switch(rtm->rtm_type) { + switch (rtm->rtm_type) { case RTM_IFINFO: return if_ifinfo(ctx, (const void *)rtm); - case RTM_ADD: /* FALLTHROUGH */ - case RTM_CHANGE: /* FALLTHROUGH */ - case RTM_DELETE: /* FALLTHROUGH */ + case RTM_ADD: /* FALLTHROUGH */ + case RTM_CHANGE: /* FALLTHROUGH */ + case RTM_DELETE: /* FALLTHROUGH */ case RTM_MISS: return if_rtm(ctx, (const void *)rtm); - case RTM_CHGADDR: /* FALLTHROUGH */ - case RTM_DELADDR: /* FALLTHROUGH */ + case RTM_CHGADDR: /* FALLTHROUGH */ + case RTM_DELADDR: /* FALLTHROUGH */ case RTM_NEWADDR: return if_ifa(ctx, (const void *)rtm); } @@ -1106,8 +1086,7 @@ if_handlelink(struct dhcpcd_ctx *ctx) if (len == 0) return 0; if ((size_t)len < sizeof(rtm.hdr.rtm_msglen) || - len != rtm.hdr.rtm_msglen) - { + len != rtm.hdr.rtm_msglen) { errno = EINVAL; return -1; } @@ -1126,8 +1105,8 @@ if_handlelink(struct dhcpcd_ctx *ctx) static void if_octetstr(char *buf, const Octet_t *o, ssize_t len) { - int i; - char *p; + int i; + char *p; p = buf; for (i = 0; i < o->o_length; i++) { @@ -1142,7 +1121,7 @@ if_octetstr(char *buf, const Octet_t *o, ssize_t len) static int if_setflags(int fd, const char *ifname, uint64_t flags) { - struct lifreq lifr = { .lifr_addrlen = 0 }; + struct lifreq lifr = { .lifr_addrlen = 0 }; strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); if (ioctl(fd, SIOCGLIFFLAGS, &lifr) == -1) @@ -1156,11 +1135,10 @@ if_setflags(int fd, const char *ifname, uint64_t flags) } static int -if_addaddr(int fd, const char *ifname, - struct sockaddr_storage *addr, struct sockaddr_storage *mask, - struct sockaddr_storage *brd, uint8_t plen) +if_addaddr(int fd, const char *ifname, struct sockaddr_storage *addr, + struct sockaddr_storage *mask, struct sockaddr_storage *brd, uint8_t plen) { - struct lifreq lifr = { .lifr_addrlen = plen }; + struct lifreq lifr = { .lifr_addrlen = plen }; strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); @@ -1195,11 +1173,10 @@ if_addaddr(int fd, const char *ifname, static int if_getaf_fd(const struct dhcpcd_ctx *ctx, int af) { - if (af == AF_INET) return ctx->pf_inet_fd; if (af == AF_INET6) { - struct priv *priv; + struct priv *priv; priv = (struct priv *)ctx->priv; return priv->pf_inet6_fd; @@ -1210,45 +1187,48 @@ if_getaf_fd(const struct dhcpcd_ctx *ctx, int af) } int -if_getsubnet(struct dhcpcd_ctx *ctx, const char *ifname, int af, - void *subnet, size_t subnet_len) +if_getsubnet(struct dhcpcd_ctx *ctx, const char *ifname, int af, void *subnet, + size_t subnet_len) { - struct lifreq lifr = { .lifr_addrlen = 0 }; + struct lifreq lifr = { .lifr_addrlen = 0 }; int fd; fd = if_getaf_fd(ctx, af); strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); if (ioctl(fd, SIOCGLIFSUBNET, &lifr) == -1) return -1; - memcpy(subnet, &lifr.lifr_addr, MIN(subnet_len,sizeof(lifr.lifr_addr))); + memcpy(subnet, &lifr.lifr_addr, + MIN(subnet_len, sizeof(lifr.lifr_addr))); return 0; } static int if_plumblif(int cmd, const struct dhcpcd_ctx *ctx, int af, const char *ifname) { - struct lifreq lifr; - int s; + struct lifreq lifr; + int s; memset(&lifr, 0, sizeof(lifr)); strlcpy(lifr.lifr_name, ifname, sizeof(lifr.lifr_name)); lifr.lifr_addr.ss_family = af; s = if_getaf_fd(ctx, af); - return ioctl(s, - cmd == RTM_NEWADDR ? SIOCLIFADDIF : SIOCLIFREMOVEIF, - &lifr) == -1 && errno != EEXIST ? -1 : 0; + return ioctl(s, cmd == RTM_NEWADDR ? SIOCLIFADDIF : SIOCLIFREMOVEIF, + &lifr) == -1 && + errno != EEXIST ? + -1 : + 0; } static int if_plumbif(const struct dhcpcd_ctx *ctx, int af, const char *ifname) { - dlpi_handle_t dh, dh_arp = NULL; - int fd, af_fd, mux_fd, arp_fd = -1, mux_id, retval; - uint64_t flags; - struct lifreq lifr; - const char *udp_dev; - struct strioctl ioc; - struct if_spec spec; + dlpi_handle_t dh, dh_arp = NULL; + int fd, af_fd, mux_fd, arp_fd = -1, mux_id, retval; + uint64_t flags; + struct lifreq lifr; + const char *udp_dev; + struct strioctl ioc; + struct if_spec spec; if (if_nametospec(ifname, &spec) == -1) return -1; @@ -1302,7 +1282,7 @@ if_plumbif(const struct dhcpcd_ctx *ctx, int af, const char *ifname) ; if (errno != EINVAL) goto out; - if(ioctl(mux_fd, I_PUSH, ARP_MOD_NAME) == -1) + if (ioctl(mux_fd, I_PUSH, ARP_MOD_NAME) == -1) goto out; if (flags & (IFF_NOARP | IFF_IPV6)) { @@ -1353,8 +1333,8 @@ if_plumbif(const struct dhcpcd_ctx *ctx, int af, const char *ifname) static int if_unplumbif(const struct dhcpcd_ctx *ctx, int af, const char *ifname) { - struct sockaddr_storage addr = { .ss_family = af }; - int fd; + struct sockaddr_storage addr = { .ss_family = af }; + int fd; /* For the time being, don't unplumb the interface, just * set the address to zero. */ @@ -1366,7 +1346,7 @@ if_unplumbif(const struct dhcpcd_ctx *ctx, int af, const char *ifname) static int if_plumb(int cmd, const struct dhcpcd_ctx *ctx, int af, const char *ifname) { - struct if_spec spec; + struct if_spec spec; if (if_nametospec(ifname, &spec) == -1) return -1; @@ -1492,17 +1472,17 @@ if_walkrt6(struct dhcpcd_ctx *ctx, rb_tree_t *routes, char *data, size_t len) #endif static int -if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes, - unsigned int level, unsigned int name, +if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes, unsigned int level, + unsigned int name, int (*walkrt)(struct dhcpcd_ctx *, rb_tree_t *, char *, size_t)) { - int s, retval, code, flags; - uintptr_t buf[512 / sizeof(uintptr_t)]; - struct strbuf ctlbuf, databuf; - struct T_optmgmt_req *tor = (struct T_optmgmt_req *)buf; - struct T_optmgmt_ack *toa = (struct T_optmgmt_ack *)buf; - struct T_error_ack *tea = (struct T_error_ack *)buf; - struct opthdr *req; + int s, retval, code, flags; + uintptr_t buf[512 / sizeof(uintptr_t)]; + struct strbuf ctlbuf, databuf; + struct T_optmgmt_req *tor = (struct T_optmgmt_req *)buf; + struct T_optmgmt_ack *toa = (struct T_optmgmt_ack *)buf; + struct T_error_ack *tea = (struct T_error_ack *)buf; + struct opthdr *req; if ((s = open("/dev/arp", O_RDWR)) == -1) return -1; @@ -1511,8 +1491,8 @@ if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes, retval = -1; tor->PRIM_type = T_SVR4_OPTMGMT_REQ; - tor->OPT_offset = sizeof (struct T_optmgmt_req); - tor->OPT_length = sizeof (struct opthdr); + tor->OPT_offset = sizeof(struct T_optmgmt_req); + tor->OPT_length = sizeof(struct opthdr); tor->MGMT_flags = T_CURRENT; req = (struct opthdr *)&tor[1]; @@ -1537,24 +1517,20 @@ if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes, flags = 0; if ((code = getmsg(s, &ctlbuf, 0, &flags)) == -1) break; - if (code == 0 && - toa->PRIM_type == T_OPTMGMT_ACK && + if (code == 0 && toa->PRIM_type == T_OPTMGMT_ACK && toa->MGMT_flags == T_SUCCESS && - (size_t)ctlbuf.len >= sizeof(struct T_optmgmt_ack)) - { + (size_t)ctlbuf.len >= sizeof(struct T_optmgmt_ack)) { /* End of messages, so return success! */ retval = 0; break; } if (tea->PRIM_type == T_ERROR_ACK) { - errno = tea->TLI_error == TSYSERR ? - tea->UNIX_error : EPROTO; + errno = tea->TLI_error == TSYSERR ? tea->UNIX_error : + EPROTO; break; } - if (code != MOREDATA || - toa->PRIM_type != T_OPTMGMT_ACK || - toa->MGMT_flags != T_SUCCESS) - { + if (code != MOREDATA || toa->PRIM_type != T_OPTMGMT_ACK || + toa->MGMT_flags != T_SUCCESS) { errno = ENOMSG; break; } @@ -1590,14 +1566,12 @@ if_parsert(struct dhcpcd_ctx *ctx, rb_tree_t *routes, return retval; } - int if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *routes, int af) { - #ifdef INET if ((af == AF_UNSPEC || af == AF_INET) && - if_parsert(ctx, routes, MIB2_IP,MIB2_IP_ROUTE, if_walkrt) == -1) + if_parsert(ctx, routes, MIB2_IP, MIB2_IP_ROUTE, if_walkrt) == -1) return -1; #endif #ifdef INET6 @@ -1608,16 +1582,15 @@ if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *routes, int af) return 0; } - #ifdef INET /* XXX We should fix this to write via the BPF interface. */ ssize_t bpf_send(const struct bpf *bpf, uint16_t protocol, const void *data, size_t len) { const struct interface *ifp = bpf->bpf_ifp; - dlpi_handle_t dh; - dlpi_info_t di; - int r; + dlpi_handle_t dh; + dlpi_info_t di; + int r; if (dlpi_open(ifp->name, &dh, 0) != DLPI_SUCCESS) return -1; @@ -1632,7 +1605,7 @@ int if_address(unsigned char cmd, const struct ipv4_addr *ia) { union { - struct sockaddr sa; + struct sockaddr sa; struct sockaddr_storage ss; } addr, mask, brd; int fd = ia->iface->ctx->pf_inet_fd; @@ -1660,10 +1633,9 @@ if_address(unsigned char cmd, const struct ipv4_addr *ia) } int -if_addrflags(const struct interface *ifp, __unused const struct in_addr * ia, +if_addrflags(const struct interface *ifp, __unused const struct in_addr *ia, const char *alias) { - return if_addrflags0(ifp->ctx->pf_inet_fd, AF_INET, alias); } @@ -1674,11 +1646,11 @@ int if_address6(unsigned char cmd, const struct ipv6_addr *ia) { union { - struct sockaddr sa; - struct sockaddr_in6 sin6; + struct sockaddr sa; + struct sockaddr_in6 sin6; struct sockaddr_storage ss; } addr, mask; - int fd, r; + int fd, r; /* Either remove the alias or ensure it exists. */ if (if_plumb(cmd, ia->iface->ctx, AF_INET6, ia->alias) == -1 && @@ -1696,17 +1668,17 @@ if_address6(unsigned char cmd, const struct ipv6_addr *ia) fd = if_getaf_fd(ia->iface->ctx, AF_INET6); if (!(ia->flags & IPV6_AF_AUTOCONF) && ia->flags & IPV6_AF_RAPFX) { - if (if_setflags(fd, ia->alias, IFF_NOLOCAL) ==-1) + if (if_setflags(fd, ia->alias, IFF_NOLOCAL) == -1) return -1; sa_in6_init(&mask.sa, &ia->prefix); - r = if_addaddr(fd, ia->alias, - NULL, &mask.ss, NULL, ia->prefix_len); + r = if_addaddr(fd, ia->alias, NULL, &mask.ss, NULL, + ia->prefix_len); } else { sa_in6_init(&addr.sa, &ia->addr); mask.sin6.sin6_family = AF_INET6; ipv6_mask(&mask.sin6.sin6_addr, ia->prefix_len); - r = if_addaddr(fd, ia->alias, - &addr.ss, &mask.ss, NULL, ia->prefix_len); + r = if_addaddr(fd, ia->alias, &addr.ss, &mask.ss, NULL, + ia->prefix_len); } if (r == -1 && errno == EEXIST) return 0; @@ -1717,7 +1689,7 @@ int if_addrflags6(const struct interface *ifp, __unused const struct in6_addr *ia, const char *alias) { - int fd; + int fd; fd = if_getaf_fd(ifp->ctx, AF_INET6); return if_addrflags0(fd, AF_INET6, alias); @@ -1726,7 +1698,6 @@ if_addrflags6(const struct interface *ifp, __unused const struct in6_addr *ia, int if_getlifetime6(struct ipv6_addr *addr) { - UNUSED(addr); errno = ENOTSUP; return -1; @@ -1735,7 +1706,7 @@ if_getlifetime6(struct ipv6_addr *addr) int if_applyra(const struct ra *rap) { - struct lifreq lifr = { + struct lifreq lifr = { .lifr_ifinfo.lir_maxhops = rap->hoplimit, .lifr_ifinfo.lir_reachtime = rap->reachable, .lifr_ifinfo.lir_reachretrans = rap->retrans, @@ -1748,6 +1719,5 @@ if_applyra(const struct ra *rap) void if_setup_inet6(__unused const struct interface *ifp) { - } #endif diff --git a/src/if.c b/src/if.c index 0283f060..ea4e676d 100644 --- a/src/if.c +++ b/src/if.c @@ -26,37 +26,37 @@ * SUCH DAMAGE. */ -#include #include +#include #include #include -#include /* Needs to be here for old Linux */ - -#include "config.h" - #include #include #include + +#include /* Needs to be here for old Linux */ + +#include "config.h" #ifdef AF_LINK -# include -# include -# include -# undef AF_PACKET /* Newer Illumos defines this */ +#include +#include +#include +#undef AF_PACKET /* Newer Illumos defines this */ #endif #ifdef AF_PACKET -# include +#include #endif #ifdef SIOCGIFMEDIA -# include +#include #endif #include #include #include +#include #include #include -#include #include #include #include @@ -64,14 +64,14 @@ #include #include -#define ELOOP_QUEUE ELOOP_IF +#define ELOOP_QUEUE ELOOP_IF #include "common.h" -#include "eloop.h" #include "dev.h" #include "dhcp.h" #include "dhcp6.h" -#include "if.h" +#include "eloop.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "ipv6nd.h" @@ -81,7 +81,6 @@ void if_free(struct interface *ifp) { - if (ifp == NULL) return; #ifdef IPV4LL @@ -107,7 +106,6 @@ if_free(struct interface *ifp) int if_opensockets(struct dhcpcd_ctx *ctx) { - if (if_opensockets_os(ctx) == -1) return -1; @@ -132,7 +130,6 @@ if_opensockets(struct dhcpcd_ctx *ctx) void if_closesockets(struct dhcpcd_ctx *ctx) { - if (ctx->link_fd != -1) { eloop_event_delete(ctx->eloop, ctx->link_fd); close(ctx->link_fd); @@ -150,7 +147,6 @@ if_closesockets(struct dhcpcd_ctx *ctx) int if_ioctl(struct dhcpcd_ctx *ctx, ioctl_request_t req, void *data, size_t len) { - #ifdef PRIVSEP if (ctx->options & DHCPCD_PRIVSEP) return (int)ps_root_ioctl(ctx, req, data, len); @@ -186,10 +182,10 @@ if_setflag(struct interface *ifp, short setflag, short unsetflag) bool if_is_link_up(const struct interface *ifp) { - return ifp->flags & IFF_UP && (ifp->carrier != LINK_DOWN || - (ifp->options != NULL && !(ifp->options->options & DHCPCD_LINK))); + (ifp->options != NULL && + !(ifp->options->options & DHCPCD_LINK))); } int @@ -224,8 +220,7 @@ if_randomisemac(struct interface *ifp) buf[0] &= 0xFC; buf[0] |= 0x02; - logdebugx("%s: hardware address randomised to %s", - ifp->name, + logdebugx("%s: hardware address randomised to %s", ifp->name, hwaddr_ntoa(buf, ifp->hwlen, sbuf, sizeof(sbuf))); retval = if_setmac(ifp, buf, ifp->hwlen); if (retval == 0) @@ -282,7 +277,7 @@ if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs, #ifdef HAVE_IFADDRS_ADDRFLAGS addrflags = (int)ifa->ifa_addrflags; #endif - switch(ifa->ifa_addr->sa_family) { + switch (ifa->ifa_addr->sa_family) { #ifdef INET case AF_INET: addr = (void *)ifa->ifa_addr; @@ -299,8 +294,9 @@ if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs, char dbuf[INET_ADDRSTRLEN]; const char *dbp; - dbp = inet_ntop(AF_INET, &addr->sin_addr, - dbuf, sizeof(dbuf)); + dbp = inet_ntop(AF_INET, + &addr->sin_addr, dbuf, + sizeof(dbuf)); logerr("%s: if_addrflags: %s%%%s", __func__, dbp, ifp->name); } @@ -308,8 +304,8 @@ if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs, } #endif ipv4_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name, - &addr->sin_addr, &net->sin_addr, - brd ? &brd->sin_addr : NULL, addrflags, 0); + &addr->sin_addr, &net->sin_addr, + brd ? &brd->sin_addr : NULL, addrflags, 0); break; #endif #ifdef INET6 @@ -332,26 +328,27 @@ if_learnaddrs(struct dhcpcd_ctx *ctx, struct if_head *ifs, char dbuf[INET6_ADDRSTRLEN]; const char *dbp; - dbp = inet_ntop(AF_INET6, &addr6->sin6_addr, - dbuf, sizeof(dbuf)); + dbp = inet_ntop(AF_INET6, + &addr6->sin6_addr, dbuf, + sizeof(dbuf)); logerr("%s: if_addrflags6: %s%%%s", __func__, dbp, ifp->name); } continue; } #endif - ipv6_handleifa(ctx, RTM_NEWADDR, ifs, - ifa->ifa_name, &addr6->sin6_addr, - ipv6_prefixlen(&net6->sin6_addr), - dstaddr6 ? &dstaddr6->sin6_addr : NULL, - addrflags, 0); + ipv6_handleifa(ctx, RTM_NEWADDR, ifs, ifa->ifa_name, + &addr6->sin6_addr, ipv6_prefixlen(&net6->sin6_addr), + dstaddr6 ? &dstaddr6->sin6_addr : NULL, addrflags, + 0); break; #endif } } } -void if_freeifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs) +void +if_freeifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs) { #ifndef PRIVSEP_GETIFADDRS UNUSED(ctx); @@ -405,18 +402,17 @@ if_valid_hwaddr(const uint8_t *hwaddr, size_t hwlen) static unsigned int if_check_arphrd(struct interface *ifp, unsigned int active, bool if_noconf) { - - switch(ifp->hwtype) { + switch (ifp->hwtype) { case ARPHRD_ETHER: /* FALLTHROUGH */ case ARPHRD_IEEE1394: /* FALLTHROUGH */ - case ARPHRD_INFINIBAND: /* FALLTHROUGH */ + case ARPHRD_INFINIBAND: /* FALLTHROUGH */ case ARPHRD_NONE: /* FALLTHROUGH */ break; case ARPHRD_LOOPBACK: case ARPHRD_PPP: if (if_noconf && active) { logdebugx("%s: ignoring due to interface type and" - " no config", + " no config", ifp->name); active = IF_INACTIVE; } @@ -428,7 +424,8 @@ if_check_arphrd(struct interface *ifp, unsigned int active, bool if_noconf) if (if_noconf) active = IF_INACTIVE; i = active ? LOG_WARNING : LOG_DEBUG; - logmessage(i, "%s: unsupported" + logmessage(i, + "%s: unsupported" " interface type 0x%.2x", ifp->name, ifp->hwtype); } @@ -440,8 +437,8 @@ if_check_arphrd(struct interface *ifp, unsigned int active, bool if_noconf) #endif struct if_head * -if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, - int argc, char * const *argv) +if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, int argc, + char *const *argv) { struct ifaddrs *ifa; int i; @@ -477,7 +474,7 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, } } else #endif - if (getifaddrs(ifaddrs) == -1) { + if (getifaddrs(ifaddrs) == -1) { logerr("getifaddrs"); free(ifs); return NULL; @@ -518,7 +515,8 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, if (argc == -1 && strcmp(argv[0], spec.devname) != 0) continue; active = ctx->options & DHCPCD_INACTIVE ? - IF_INACTIVE: IF_ACTIVE_USER; + IF_INACTIVE : + IF_ACTIVE_USER; } for (i = 0; i < ctx->ifdc; i++) @@ -560,7 +558,8 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, /* Don't allow some reserved interface names unless explicit. */ if (if_noconf && if_ignore(ctx, spec.devname)) { logdebugx("%s: ignoring due to interface type and" - " no config", spec.devname); + " no config", + spec.devname); active = IF_INACTIVE; } @@ -586,15 +585,14 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, iflr.flags = IFLR_PREFIX; iflr.prefixlen = (unsigned int)sdl->sdl_alen * NBBY; if (ioctl(ctx->pf_link_fd, SIOCGLIFADDR, &iflr) == -1 || - !(iflr.flags & IFLR_ACTIVE)) - { + !(iflr.flags & IFLR_ACTIVE)) { if_free(ifp); continue; } #endif ifp->index = sdl->sdl_index; - switch(sdl->sdl_type) { + switch (sdl->sdl_type) { #ifdef IFT_BRIDGE case IFT_BRIDGE: /* FALLTHROUGH */ #endif @@ -609,13 +607,13 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, /* Don't allow unless explicit */ if (if_noconf && active) { logdebugx("%s: ignoring due to" - " interface type and" - " no config", + " interface type and" + " no config", ifp->name); active = IF_INACTIVE; } __fallthrough; /* appease gcc */ - /* FALLTHROUGH */ + /* FALLTHROUGH */ #ifdef IFT_L2VLAN case IFT_L2VLAN: /* FALLTHROUGH */ #endif @@ -641,7 +639,8 @@ if_discover(struct dhcpcd_ctx *ctx, struct ifaddrs **ifaddrs, if (if_noconf) active = IF_INACTIVE; i = active ? LOG_WARNING : LOG_DEBUG; - logmessage(i, "%s: unsupported" + logmessage(i, + "%s: unsupported" " interface type 0x%.2x", ifp->name, sdl->sdl_type); } @@ -732,10 +731,9 @@ if_nametospec(const char *ifname, struct if_spec *spec) if (ifname == NULL || *ifname == '\0' || strlcpy(spec->ifname, ifname, sizeof(spec->ifname)) >= - sizeof(spec->ifname) || + sizeof(spec->ifname) || strlcpy(spec->drvname, ifname, sizeof(spec->drvname)) >= - sizeof(spec->drvname)) - { + sizeof(spec->drvname)) { errno = EINVAL; return -1; } @@ -799,7 +797,6 @@ if_nametospec(const char *ifname, struct if_spec *spec) static struct interface * if_findindexname(struct if_head *ifaces, unsigned int idx, const char *name) { - if (ifaces != NULL) { struct if_spec spec; struct interface *ifp; @@ -821,14 +818,12 @@ if_findindexname(struct if_head *ifaces, unsigned int idx, const char *name) struct interface * if_find(struct if_head *ifaces, const char *name) { - return if_findindexname(ifaces, 0, name); } struct interface * if_findindex(struct if_head *ifaces, unsigned int idx) { - return if_findindexname(ifaces, idx, NULL); } @@ -863,7 +858,6 @@ if_getmtu(const struct interface *ifp) int if_makealias(char *alias, size_t alias_len, const char *ifname, int lun) { - if (lun == 0) return strlcpy(alias, ifname, alias_len); return snprintf(alias, alias_len, "%s:%u", ifname, lun); @@ -889,18 +883,16 @@ if_findifpfromcmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, int *hoplimit) UNUSED(hoplimit); #endif - for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(msg); - cm; - cm = (struct cmsghdr *)CMSG_NXTHDR(msg, cm)) - { + for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(msg); cm; + cm = (struct cmsghdr *)CMSG_NXTHDR(msg, cm)) { #ifdef INET if (cm->cmsg_level == IPPROTO_IP) { - switch(cm->cmsg_type) { + switch (cm->cmsg_type) { #ifdef IP_RECVIF case IP_RECVIF: if (cm->cmsg_len < offsetof(struct sockaddr_dl, sdl_index) + - sizeof(sdl.sdl_index)) + sizeof(sdl.sdl_index)) continue; memcpy(&sdl, CMSG_DATA(cm), MIN(sizeof(sdl), cm->cmsg_len)); @@ -919,7 +911,7 @@ if_findifpfromcmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg, int *hoplimit) #endif #ifdef INET6 if (cm->cmsg_level == IPPROTO_IPV6) { - switch(cm->cmsg_type) { + switch (cm->cmsg_type) { case IPV6_PKTINFO: if (cm->cmsg_len != CMSG_LEN(sizeof(ipi6))) continue; @@ -968,18 +960,20 @@ xsocket(int domain, int type, int protocol) if ((s = socket(domain, type, protocol)) == -1) return -1; #ifdef DEBUG_FD - logerrx("pid %d fd=%d domain=%d type=%d protocol=%d", - getpid(), s, domain, type, protocol); + logerrx("pid %d fd=%d domain=%d type=%d protocol=%d", getpid(), s, + domain, type, protocol); #endif #ifndef HAVE_SOCK_CLOEXEC - if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(s, F_GETFD)) == -1 || - fcntl(s, F_SETFD, xflags | FD_CLOEXEC) == -1)) + if ((xtype & SOCK_CLOEXEC) && + ((xflags = fcntl(s, F_GETFD)) == -1 || + fcntl(s, F_SETFD, xflags | FD_CLOEXEC) == -1)) goto out; #endif #ifndef HAVE_SOCK_NONBLOCK - if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(s, F_GETFL)) == -1 || - fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1)) + if ((xtype & SOCK_NONBLOCK) && + ((xflags = fcntl(s, F_GETFL)) == -1 || + fcntl(s, F_SETFL, xflags | O_NONBLOCK) == -1)) goto out; #endif @@ -1017,19 +1011,23 @@ xsocketpair(int domain, int type, int protocol, int fd[2]) #endif #ifndef HAVE_SOCK_CLOEXEC - if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(fd[0], F_GETFD)) == -1 || - fcntl(fd[0], F_SETFD, xflags | FD_CLOEXEC) == -1)) + if ((xtype & SOCK_CLOEXEC) && + ((xflags = fcntl(fd[0], F_GETFD)) == -1 || + fcntl(fd[0], F_SETFD, xflags | FD_CLOEXEC) == -1)) goto out; - if ((xtype & SOCK_CLOEXEC) && ((xflags = fcntl(fd[1], F_GETFD)) == -1 || - fcntl(fd[1], F_SETFD, xflags | FD_CLOEXEC) == -1)) + if ((xtype & SOCK_CLOEXEC) && + ((xflags = fcntl(fd[1], F_GETFD)) == -1 || + fcntl(fd[1], F_SETFD, xflags | FD_CLOEXEC) == -1)) goto out; #endif #ifndef HAVE_SOCK_NONBLOCK - if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(fd[0], F_GETFL)) == -1 || - fcntl(fd[0], F_SETFL, xflags | O_NONBLOCK) == -1)) + if ((xtype & SOCK_NONBLOCK) && + ((xflags = fcntl(fd[0], F_GETFL)) == -1 || + fcntl(fd[0], F_SETFL, xflags | O_NONBLOCK) == -1)) goto out; - if ((xtype & SOCK_NONBLOCK) && ((xflags = fcntl(fd[1], F_GETFL)) == -1 || - fcntl(fd[1], F_SETFL, xflags | O_NONBLOCK) == -1)) + if ((xtype & SOCK_NONBLOCK) && + ((xflags = fcntl(fd[1], F_GETFL)) == -1 || + fcntl(fd[1], F_SETFL, xflags | O_NONBLOCK) == -1)) goto out; #endif diff --git a/src/if.h b/src/if.h index 8d1aeccf..7854a0c4 100644 --- a/src/if.h +++ b/src/if.h @@ -30,17 +30,17 @@ #define INTERFACE_H #include -#include /* for RTM_ADD et all */ +#include /* for RTM_ADD et all */ #include #ifdef BSD -#include /* for IN_IFF_TENTATIVE et all */ +#include /* for IN_IFF_TENTATIVE et all */ #endif #include /* If the interface does not support carrier status (ie PPP), * dhcpcd can poll it for the relevant flags periodically */ -#define IF_POLL_UP 100 /* milliseconds */ +#define IF_POLL_UP 100 /* milliseconds */ /* * Systems which handle 1 address per alias. @@ -52,9 +52,9 @@ * restriction. */ #ifndef ALIAS_ADDR -# ifdef __sun -# define ALIAS_ADDR -# endif +#ifdef __sun +#define ALIAS_ADDR +#endif #endif #include "config.h" @@ -63,9 +63,9 @@ * Everyone else use an unsigned long, which happens to be the bigger one * so we use that in our on wire API. */ #ifdef IOCTL_REQUEST_TYPE -typedef IOCTL_REQUEST_TYPE ioctl_request_t; +typedef IOCTL_REQUEST_TYPE ioctl_request_t; #else -typedef unsigned long ioctl_request_t; +typedef unsigned long ioctl_request_t; #endif #include "dhcpcd.h" @@ -73,25 +73,25 @@ typedef unsigned long ioctl_request_t; #include "ipv6.h" #include "route.h" -#define EUI64_ADDR_LEN 8 -#define INFINIBAND_ADDR_LEN 20 +#define EUI64_ADDR_LEN 8 +#define INFINIBAND_ADDR_LEN 20 /* Linux 2.4 doesn't define this */ #ifndef ARPHRD_IEEE1394 -# define ARPHRD_IEEE1394 24 +#define ARPHRD_IEEE1394 24 #endif /* The BSD's don't define this yet */ #ifndef ARPHRD_INFINIBAND -# define ARPHRD_INFINIBAND 32 +#define ARPHRD_INFINIBAND 32 #endif /* Maximum frame length. * Support jumbo frames and some extra. */ -#define FRAMEHDRLEN_MAX 14 /* only ethernet support */ -#define FRAMELEN_MAX (FRAMEHDRLEN_MAX + 9216) +#define FRAMEHDRLEN_MAX 14 /* only ethernet support */ +#define FRAMELEN_MAX (FRAMEHDRLEN_MAX + 9216) -#define UDPLEN_MAX 64 * 1024 +#define UDPLEN_MAX 64 * 1024 /* Work out if we have a private address or not * 10/8 @@ -99,14 +99,15 @@ typedef unsigned long ioctl_request_t; * 192.168/16 */ #ifndef IN_PRIVATE -# define IN_PRIVATE(addr) (((addr & IN_CLASSA_NET) == 0x0a000000) || \ - ((addr & 0xfff00000) == 0xac100000) || \ +#define IN_PRIVATE(addr) \ + (((addr & IN_CLASSA_NET) == 0x0a000000) || \ + ((addr & 0xfff00000) == 0xac100000) || \ ((addr & IN_CLASSB_NET) == 0xc0a80000)) #endif #ifndef CLLADDR #ifdef AF_LINK -# define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen) +#define CLLADDR(sdl) (const void *)((sdl)->sdl_data + (sdl)->sdl_nlen) #endif #endif @@ -152,24 +153,25 @@ struct priv { * See if-sun.c for details why. */ struct ifaddrs; int if_getifaddrs(struct ifaddrs **); -#define getifaddrs if_getifaddrs +#define getifaddrs if_getifaddrs int if_getsubnet(struct dhcpcd_ctx *, const char *, int, void *, size_t); #endif int if_ioctl(struct dhcpcd_ctx *, ioctl_request_t, void *, size_t); #ifdef HAVE_PLEDGE -#define pioctl(ctx, req, data, len) if_ioctl((ctx), (req), (data), (len)) +#define pioctl(ctx, req, data, len) if_ioctl((ctx), (req), (data), (len)) #else -#define pioctl(ctx, req, data, len) ioctl((ctx)->pf_inet_fd, (req),(data),(len)) +#define pioctl(ctx, req, data, len) \ + ioctl((ctx)->pf_inet_fd, (req), (data), (len)) #endif int if_setflag(struct interface *, short, short); int if_getmtu(const struct interface *); -#define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING), 0) +#define if_up(ifp) if_setflag((ifp), (IFF_UP | IFF_RUNNING), 0) #define if_down(ifp) if_setflag((ifp), 0, IFF_UP); bool if_is_link_up(const struct interface *); bool if_valid_hwaddr(const uint8_t *, size_t); -struct if_head *if_discover(struct dhcpcd_ctx *, struct ifaddrs **, - int, char * const *); +struct if_head *if_discover(struct dhcpcd_ctx *, struct ifaddrs **, int, + char *const *); void if_freeifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **); void if_markaddrsstale(struct if_head *); void if_learnaddrs(struct dhcpcd_ctx *, struct if_head *, struct ifaddrs **); @@ -211,7 +213,7 @@ int if_ignoregroup(int, const char *); bool if_ignore(struct dhcpcd_ctx *, const char *); int if_vimaster(struct dhcpcd_ctx *ctx, const char *); unsigned short if_vlanid(const struct interface *); -char * if_getnetworknamespace(char *, size_t); /* used by udev */ +char *if_getnetworknamespace(char *, size_t); /* used by udev */ int if_opensockets(struct dhcpcd_ctx *); int if_opensockets_os(struct dhcpcd_ctx *); void if_closesockets(struct dhcpcd_ctx *); @@ -224,26 +226,26 @@ int if_setmac(struct interface *ifp, void *, uint8_t); * If the platform doesn't use these flags, * map them in the platform interace file. */ #ifndef RTM_ADD -#define RTM_ADD 0x1 /* Add Route */ -#define RTM_DELETE 0x2 /* Delete Route */ -#define RTM_CHANGE 0x3 /* Change Metrics or flags */ -#define RTM_GET 0x4 /* Report Metrics */ +#define RTM_ADD 0x1 /* Add Route */ +#define RTM_DELETE 0x2 /* Delete Route */ +#define RTM_CHANGE 0x3 /* Change Metrics or flags */ +#define RTM_GET 0x4 /* Report Metrics */ #endif /* Define SOCK_CLOEXEC and SOCK_NONBLOCK for systems that lack it. * xsocket() in if.c will map them to fctnl FD_CLOEXEC and O_NONBLOCK. */ #ifdef SOCK_CLOEXEC -# define HAVE_SOCK_CLOEXEC +#define HAVE_SOCK_CLOEXEC #else -# define SOCK_CLOEXEC 0x10000000 +#define SOCK_CLOEXEC 0x10000000 #endif #ifdef SOCK_NONBLOCK -# define HAVE_SOCK_NONBLOCK +#define HAVE_SOCK_NONBLOCK #else -# define SOCK_NONBLOCK 0x20000000 +#define SOCK_NONBLOCK 0x20000000 #endif #ifndef SOCK_CXNB -#define SOCK_CXNB SOCK_CLOEXEC | SOCK_NONBLOCK +#define SOCK_CXNB SOCK_CLOEXEC | SOCK_NONBLOCK #endif int xsocket(int, int, int); int xsocketpair(int, int, int, int[2]); @@ -280,8 +282,8 @@ int if_getlifetime6(struct ipv6_addr *); #endif int if_machinearch(char *, size_t); -struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *, - struct msghdr *, int *); +struct interface *if_findifpfromcmsg(struct dhcpcd_ctx *, struct msghdr *, + int *); #ifdef __linux__ int if_linksocket(struct sockaddr_nl *, int, int); diff --git a/src/ipv4.c b/src/ipv4.c index 4137e326..311d0982 100644 --- a/src/ipv4.c +++ b/src/ipv4.c @@ -26,15 +26,15 @@ * SUCH DAMAGE. */ -#include #include +#include -#include #include #include -#include #include +#include +#include #include #include #include @@ -44,20 +44,20 @@ #include #include -#include "config.h" #include "arp.h" #include "common.h" -#include "dhcpcd.h" +#include "config.h" #include "dhcp.h" +#include "dhcpcd.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "logerr.h" #include "route.h" -#include "script.h" #include "sa.h" +#include "script.h" #define IPV4_LOOPBACK_ROUTE #if defined(__linux__) || defined(__sun) || (defined(BSD) && defined(RTF_LOCAL)) @@ -120,8 +120,8 @@ ipv4_getnetmask(uint32_t addr) } struct ipv4_addr * -ipv4_iffindaddr(struct interface *ifp, - const struct in_addr *addr, const struct in_addr *mask) +ipv4_iffindaddr(struct interface *ifp, const struct in_addr *addr, + const struct in_addr *mask) { struct ipv4_state *state; struct ipv4_addr *ap; @@ -161,7 +161,7 @@ ipv4_iffindmaskaddr(struct interface *ifp, const struct in_addr *addr) state = IPV4_STATE(ifp); if (state) { - TAILQ_FOREACH (ap, &state->addrs, next) { + TAILQ_FOREACH(ap, &state->addrs, next) { if ((ap->addr.s_addr & ap->mask.s_addr) == (addr->s_addr & ap->mask.s_addr)) return ap; @@ -178,7 +178,7 @@ ipv4_iffindmaskbrd(struct interface *ifp, const struct in_addr *addr) state = IPV4_STATE(ifp); if (state) { - TAILQ_FOREACH (ap, &state->addrs, next) { + TAILQ_FOREACH(ap, &state->addrs, next) { if ((ap->brd.s_addr & ap->mask.s_addr) == (addr->s_addr & ap->mask.s_addr)) return ap; @@ -240,9 +240,7 @@ ipv4_hasaddr(const struct interface *ifp) #endif dstate = D_CSTATE(ifp); - return (dstate && - dstate->added == STATE_ADDED && - dstate->addr != NULL); + return (dstate && dstate->added == STATE_ADDED && dstate->addr != NULL); } /* Interface comparer for working out ordering. */ @@ -313,15 +311,16 @@ inet_dhcproutes(rb_tree_t *routes, struct interface *ifp, bool *have_default) sa_in_init(&rt->rt_dest, &in); in.s_addr = state->addr->mask.s_addr; sa_in_init(&rt->rt_netmask, &in); - //in.s_addr = INADDR_ANY; - //sa_in_init(&rt->rt_gateway, &in); + // in.s_addr = INADDR_ANY; + // sa_in_init(&rt->rt_gateway, &in); rt->rt_gateway.sa_family = AF_UNSPEC; rt_proto_add(&nroutes, rt); } /* If any set routes, grab them, otherwise DHCP routes. */ if (RB_TREE_MIN(&ifp->options->routes)) { - RB_TREE_FOREACH(r, &ifp->options->routes) { + RB_TREE_FOREACH(r, &ifp->options->routes) + { if (sa_is_unspecified(&r->rt_gateway)) break; if ((rt = rt_new0(ifp->ctx)) == NULL) @@ -339,8 +338,7 @@ inet_dhcproutes(rb_tree_t *routes, struct interface *ifp, bool *have_default) /* If configured, install a gateway to the desintion * for P2P interfaces. */ if (ifp->flags & IFF_POINTOPOINT && - has_option_mask(ifp->options->dstmask, DHO_ROUTER)) - { + has_option_mask(ifp->options->dstmask, DHO_ROUTER)) { if ((rt = rt_new(ifp)) == NULL) return -1; in.s_addr = INADDR_ANY; @@ -393,7 +391,8 @@ inet_routerhostroute(rb_tree_t *routes, struct interface *ifp) rb_tree_init(&troutes, &rt_compare_proto_ops); - RB_TREE_FOREACH(rt, routes) { + RB_TREE_FOREACH(rt, routes) + { if (rt->rt_dest.sa_family != AF_INET) continue; if (!sa_is_unspecified(&rt->rt_dest) || @@ -401,7 +400,8 @@ inet_routerhostroute(rb_tree_t *routes, struct interface *ifp) continue; gateway = satosin(&rt->rt_gateway); /* Scan for a route to match */ - RB_TREE_FOREACH(rth, routes) { + RB_TREE_FOREACH(rth, routes) + { if (rth == rt) break; /* match host */ @@ -429,23 +429,21 @@ inet_routerhostroute(rb_tree_t *routes, struct interface *ifp) ifo = ifp->options; if (ifp->flags & IFF_NOARP) { if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) && - !(state->added & STATE_FAKE)) - { + !(state->added & STATE_FAKE)) { char buf[INET_MAX_ADDRSTRLEN]; ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED; logwarnx("%s: forcing router %s through " - "interface", + "interface", ifp->name, - sa_addrtop(&rt->rt_gateway, - buf, sizeof(buf))); + sa_addrtop(&rt->rt_gateway, buf, + sizeof(buf))); } gateway->sin_addr.s_addr = INADDR_ANY; continue; } if (!(ifo->options & DHCPCD_ROUTER_HOST_ROUTE_WARNED) && - !(state->added & STATE_FAKE)) - { + !(state->added & STATE_FAKE)) { char buf[INET_MAX_ADDRSTRLEN]; ifo->options |= DHCPCD_ROUTER_HOST_ROUTE_WARNED; @@ -510,8 +508,7 @@ inet_getroutes(struct dhcpcd_ctx *ctx, rb_tree_t *routes) return true; TAILQ_FOREACH(ifp, ctx->ifaces, next) { - if (ifp->active && - ipv4ll_defaultroute(routes, ifp) == 1) + if (ifp->active && ipv4ll_defaultroute(routes, ifp) == 1) break; } #endif @@ -527,12 +524,10 @@ ipv4_deladdr(struct ipv4_addr *addr, int keeparp) struct ipv4_addr *ap; assert(addr != NULL); - logdebugx("%s: deleting IP address %s", - addr->iface->name, addr->saddr); + logdebugx("%s: deleting IP address %s", addr->iface->name, addr->saddr); r = if_address(RTM_DELADDR, addr); - if (r == -1 && - errno != EADDRNOTAVAIL && errno != ESRCH && + if (r == -1 && errno != EADDRNOTAVAIL && errno != ESRCH && errno != ENXIO && errno != ENODEV) logerr("%s: %s", addr->iface->name, __func__); @@ -570,7 +565,7 @@ ipv4_getstate(struct interface *ifp) state = IPV4_STATE(ifp); if (state == NULL) { - ifp->if_data[IF_DATA_IPV4] = malloc(sizeof(*state)); + ifp->if_data[IF_DATA_IPV4] = malloc(sizeof(*state)); state = IPV4_STATE(ifp); if (state == NULL) { logerr(__func__); @@ -598,8 +593,7 @@ ipv4_aliasaddr(struct ipv4_addr *ia, struct ipv4_addr **repl) state = IPV4_STATE(ia->iface); find_lun: if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >= - IF_NAMESIZE) - { + IF_NAMESIZE) { errno = ENOMEM; return -1; } @@ -631,8 +625,8 @@ ipv4_aliasaddr(struct ipv4_addr *ia, struct ipv4_addr **repl) struct ipv4_addr * ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, - const struct in_addr *mask, const struct in_addr *bcast, - uint32_t vltime, uint32_t pltime) + const struct in_addr *mask, const struct in_addr *bcast, uint32_t vltime, + uint32_t pltime) { struct ipv4_state *state; struct ipv4_addr *ia; @@ -684,8 +678,8 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, UNUSED(vltime); UNUSED(pltime); #endif - snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", - inet_ntoa(*addr), inet_ntocidr(*mask)); + snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", inet_ntoa(*addr), + inet_ntocidr(*mask)); #ifdef ALIAS_ADDR blank = (ia->alias[0] == '\0'); @@ -698,14 +692,12 @@ ipv4_addaddr(struct interface *ifp, const struct in_addr *addr, logdebugx("%s: aliased %s", ia->alias, ia->saddr); #endif - logdebugx("%s: adding IP address %s %s %s", - ifp->name, ia->saddr, + logdebugx("%s: adding IP address %s %s %s", ifp->name, ia->saddr, ifp->flags & IFF_POINTOPOINT ? "destination" : "broadcast", inet_ntoa(*bcast)); if (if_address(RTM_NEWADDR, ia) == -1) { if (errno != EEXIST) - logerr("%s: if_addaddress", - __func__); + logerr("%s: if_addaddress", __func__); if (ia->flags & IPV4_AF_NEW) free(ia); return NULL; @@ -760,8 +752,7 @@ ipv4_applyaddr(void *arg) lease = &state->lease; if (state->new == NULL) { if ((ifo->options & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != - (DHCPCD_EXITING | DHCPCD_PERSISTENT)) - { + (DHCPCD_EXITING | DHCPCD_PERSISTENT)) { if (state->added) { /* Someone might have deleted our address */ if (state->addr != NULL) @@ -782,13 +773,11 @@ ipv4_applyaddr(void *arg) * If IP addresses do not have lifetimes, there is a very real chance * that re-adding them will scrub the subnet route temporarily * which is a bad thing, so avoid it. */ - if (ia != NULL && - ia->mask.s_addr == lease->mask.s_addr && - ia->brd.s_addr == lease->brd.s_addr) - { + if (ia != NULL && ia->mask.s_addr == lease->mask.s_addr && + ia->brd.s_addr == lease->brd.s_addr) { #ifndef IP_LIFETIME - logdebugx("%s: IP address %s already exists", - ifp->name, ia->saddr); + logdebugx("%s: IP address %s already exists", ifp->name, + ia->saddr); #endif } else { #ifdef __linux__ @@ -862,16 +851,14 @@ ipv4_deletestaleaddrs(struct interface *ifp) TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) { if (!(ia->flags & IPV4_AF_STALE)) continue; - ipv4_handleifa(ifp->ctx, RTM_DELADDR, - ifp->ctx->ifaces, ifp->name, - &ia->addr, &ia->mask, &ia->brd, 0, getpid()); + ipv4_handleifa(ifp->ctx, RTM_DELADDR, ifp->ctx->ifaces, + ifp->name, &ia->addr, &ia->mask, &ia->brd, 0, getpid()); } } void -ipv4_handleifa(struct dhcpcd_ctx *ctx, - int cmd, struct if_head *ifs, const char *ifname, - const struct in_addr *addr, const struct in_addr *mask, +ipv4_handleifa(struct dhcpcd_ctx *ctx, int cmd, struct if_head *ifs, + const char *ifname, const struct in_addr *addr, const struct in_addr *mask, const struct in_addr *brd, int addrflags, pid_t pid) { struct interface *ifp; @@ -928,8 +915,7 @@ ipv4_handleifa(struct dhcpcd_ctx *ctx, /* Mask could have changed */ if (ia_is_new || (mask->s_addr != INADDR_ANY && - mask->s_addr != ia->mask.s_addr)) - { + mask->s_addr != ia->mask.s_addr)) { ia->mask = *mask; snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", inet_ntoa(*addr), inet_ntocidr(*mask)); diff --git a/src/ipv4.h b/src/ipv4.h index b94b1c56..fb9d504c 100644 --- a/src/ipv4.h +++ b/src/ipv4.h @@ -37,12 +37,12 @@ #endif #ifndef BYTE_ORDER -#define BIG_ENDIAN 1234 -#define LITTLE_ENDIAN 4321 +#define BIG_ENDIAN 1234 +#define LITTLE_ENDIAN 4321 #if defined(_BIG_ENDIAN) -#define BYTE_ORDER BIG_ENDIAN +#define BYTE_ORDER BIG_ENDIAN #elif defined(_LITTLE_ENDIAN) -#define BYTE_ORDER LITTLE_ENDIAN +#define BYTE_ORDER LITTLE_ENDIAN #else #error Endian unknown #endif @@ -51,11 +51,11 @@ #if BYTE_ORDER == BIG_ENDIAN #define HTONL(A) (A) #elif BYTE_ORDER == LITTLE_ENDIAN -#define HTONL(A) \ - ((((uint32_t)(A) & 0xff000000) >> 24) | \ - (((uint32_t)(A) & 0x00ff0000) >> 8) | \ - (((uint32_t)(A) & 0x0000ff00) << 8) | \ - (((uint32_t)(A) & 0x000000ff) << 24)) +#define HTONL(A) \ + ((((uint32_t)(A) & 0xff000000) >> 24) | \ + (((uint32_t)(A) & 0x00ff0000) >> 8) | \ + (((uint32_t)(A) & 0x0000ff00) << 8) | \ + (((uint32_t)(A) & 0x000000ff) << 24)) #endif /* BYTE_ORDER */ #ifndef IPV4_MMTU @@ -63,22 +63,22 @@ #endif #ifdef __sun - /* Solaris lacks these defines. - * While it supports DaD, to seems to only expose IFF_DUPLICATE - * so we have no way of knowing if it's tentative or not. - * I don't even know if Solaris has any special treatment for tentative. */ -# define IN_IFF_TENTATIVE 0x01 -# define IN_IFF_DUPLICATED 0x02 -# define IN_IFF_DETACHED 0x00 +/* Solaris lacks these defines. + * While it supports DaD, to seems to only expose IFF_DUPLICATE + * so we have no way of knowing if it's tentative or not. + * I don't even know if Solaris has any special treatment for tentative. */ +#define IN_IFF_TENTATIVE 0x01 +#define IN_IFF_DUPLICATED 0x02 +#define IN_IFF_DETACHED 0x00 #endif #ifdef IN_IFF_TENTATIVE #define IN_IFF_NOTUSEABLE \ - (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED | IN_IFF_DETACHED) + (IN_IFF_TENTATIVE | IN_IFF_DUPLICATED | IN_IFF_DETACHED) #endif -#define IN_ARE_ADDR_EQUAL(a, b) ((a)->s_addr == (b)->s_addr) -#define IN_IS_ADDR_UNSPECIFIED(a) ((a)->s_addr == INADDR_ANY) +#define IN_ARE_ADDR_EQUAL(a, b) ((a)->s_addr == (b)->s_addr) +#define IN_IS_ADDR_UNSPECIFIED(a) ((a)->s_addr == INADDR_ANY) #ifdef __linux__ #define IP_LIFETIME @@ -103,22 +103,21 @@ struct ipv4_addr { }; TAILQ_HEAD(ipv4_addrhead, ipv4_addr); -#define IPV4_AF_STALE (1U << 0) -#define IPV4_AF_NEW (1U << 1) +#define IPV4_AF_STALE (1U << 0) +#define IPV4_AF_NEW (1U << 1) -#define IPV4_ADDR_EQ(a1, a2) ((a1) && (a1)->addr.s_addr == (a2)->addr.s_addr) -#define IPV4_MASK1_EQ(a1, a2) ((a1) && (a1)->mask.s_addr == (a2)->mask.s_addr) -#define IPV4_MASK_EQ(a1, a2) (IPV4_ADDR_EQ(a1, a2) && IPV4_MASK1_EQ(a1, a2)) -#define IPV4_BRD1_EQ(a1, a2) ((a1) && (a1)->brd.s_addr == (a2)->brd.s_addr) -#define IPV4_BRD_EQ(a1, a2) (IPV4_MASK_EQ(a1, a2) && IPV4_BRD1_EQ(a1, a2)) +#define IPV4_ADDR_EQ(a1, a2) ((a1) && (a1)->addr.s_addr == (a2)->addr.s_addr) +#define IPV4_MASK1_EQ(a1, a2) ((a1) && (a1)->mask.s_addr == (a2)->mask.s_addr) +#define IPV4_MASK_EQ(a1, a2) (IPV4_ADDR_EQ(a1, a2) && IPV4_MASK1_EQ(a1, a2)) +#define IPV4_BRD1_EQ(a1, a2) ((a1) && (a1)->brd.s_addr == (a2)->brd.s_addr) +#define IPV4_BRD_EQ(a1, a2) (IPV4_MASK_EQ(a1, a2) && IPV4_BRD1_EQ(a1, a2)) struct ipv4_state { struct ipv4_addrhead addrs; }; -#define IPV4_STATE(ifp) \ - ((struct ipv4_state *)(ifp)->if_data[IF_DATA_IPV4]) -#define IPV4_CSTATE(ifp) \ +#define IPV4_STATE(ifp) ((struct ipv4_state *)(ifp)->if_data[IF_DATA_IPV4]) +#define IPV4_CSTATE(ifp) \ ((const struct ipv4_state *)(ifp)->if_data[IF_DATA_IPV4]) #ifdef INET @@ -131,29 +130,27 @@ int ipv4_hasaddr(const struct interface *); bool inet_getroutes(struct dhcpcd_ctx *, rb_tree_t *); -#define STATE_ADDED 0x01 -#define STATE_FAKE 0x02 -#define STATE_EXPIRED 0x04 +#define STATE_ADDED 0x01 +#define STATE_FAKE 0x02 +#define STATE_EXPIRED 0x04 int ipv4_deladdr(struct ipv4_addr *, int); -struct ipv4_addr *ipv4_addaddr(struct interface *, - const struct in_addr *, const struct in_addr *, const struct in_addr *, - uint32_t, uint32_t); +struct ipv4_addr *ipv4_addaddr(struct interface *, const struct in_addr *, + const struct in_addr *, const struct in_addr *, uint32_t, uint32_t); struct ipv4_addr *ipv4_applyaddr(void *); -struct ipv4_addr *ipv4_iffindaddr(struct interface *, - const struct in_addr *, const struct in_addr *); +struct ipv4_addr *ipv4_iffindaddr(struct interface *, const struct in_addr *, + const struct in_addr *); struct ipv4_addr *ipv4_iffindlladdr(struct interface *); struct ipv4_addr *ipv4_findaddr(struct dhcpcd_ctx *, const struct in_addr *); struct ipv4_addr *ipv4_findmaskaddr(struct dhcpcd_ctx *, const struct in_addr *); -struct ipv4_addr *ipv4_findmaskbrd(struct dhcpcd_ctx *, - const struct in_addr *); +struct ipv4_addr *ipv4_findmaskbrd(struct dhcpcd_ctx *, const struct in_addr *); void ipv4_markaddrsstale(struct interface *); void ipv4_deletestaleaddrs(struct interface *); void ipv4_handleifa(struct dhcpcd_ctx *, int, struct if_head *, const char *, - const struct in_addr *, const struct in_addr *, const struct in_addr *, - int, pid_t); + const struct in_addr *, const struct in_addr *, const struct in_addr *, int, + pid_t); void ipv4_free(struct interface *); #endif /* INET */ diff --git a/src/ipv4ll.c b/src/ipv4ll.c index c16a1dca..c94f3cf8 100644 --- a/src/ipv4ll.c +++ b/src/ipv4ll.c @@ -27,7 +27,6 @@ */ #include - #include #include #include @@ -36,14 +35,14 @@ #include #include -#define ELOOP_QUEUE IPV4LL -#include "config.h" +#define ELOOP_QUEUE IPV4LL #include "arp.h" #include "common.h" +#include "config.h" #include "dhcp.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "logerr.h" @@ -52,12 +51,9 @@ static void ipv4ll_start_arp(void *arg); -static const struct in_addr inaddr_llmask = { - .s_addr = HTONL(LINKLOCAL_MASK) -}; -static const struct in_addr inaddr_llbcast = { - .s_addr = HTONL(LINKLOCAL_BCAST) -}; +static const struct in_addr inaddr_llmask = { .s_addr = HTONL(LINKLOCAL_MASK) }; +static const struct in_addr inaddr_llbcast = { .s_addr = HTONL( + LINKLOCAL_BCAST) }; static void ipv4ll_pickaddr(struct interface *ifp) @@ -71,14 +67,14 @@ ipv4ll_pickaddr(struct interface *ifp) do { long r; -again: + again: /* RFC 3927 Section 2.1 states that the first 256 and * last 256 addresses are reserved for future use. * See ipv4ll_start for why we don't use arc4random. */ /* coverity[dont_call] */ r = random(); - addr.s_addr = ntohl(LINKLOCAL_ADDR | - ((uint32_t)(r % 0xFD00) + 0x0100)); + addr.s_addr = ntohl( + LINKLOCAL_ADDR | ((uint32_t)(r % 0xFD00) + 0x0100)); /* No point using a failed address */ if (IN_ARE_ADDR_EQUAL(&addr, &state->pickedaddr)) @@ -99,8 +95,7 @@ ipv4ll_subnetroute(rb_tree_t *routes, struct interface *ifp) struct in_addr in; assert(ifp != NULL); - if ((state = IPV4LL_STATE(ifp)) == NULL || - state->addr == NULL) + if ((state = IPV4LL_STATE(ifp)) == NULL || state->addr == NULL) return 0; if ((rt = rt_new(ifp)) == NULL) @@ -125,8 +120,7 @@ ipv4ll_defaultroute(rb_tree_t *routes, struct interface *ifp) struct in_addr in; assert(ifp != NULL); - if ((state = IPV4LL_STATE(ifp)) == NULL || - state->addr == NULL) + if ((state = IPV4LL_STATE(ifp)) == NULL || state->addr == NULL) return 0; if ((rt = rt_new(ifp)) == NULL) @@ -156,21 +150,21 @@ ipv4ll_env(FILE *fp, const char *prefix, const struct interface *ifp) return 0; /* Emulate a DHCP environment */ - if (efprintf(fp, "%s%sip_address=%s", - prefix, pf, inet_ntoa(state->addr->addr)) == -1) + if (efprintf(fp, "%s%sip_address=%s", prefix, pf, + inet_ntoa(state->addr->addr)) == -1) return -1; - if (efprintf(fp, "%s%ssubnet_mask=%s", - prefix, pf, inet_ntoa(state->addr->mask)) == -1) + if (efprintf(fp, "%s%ssubnet_mask=%s", prefix, pf, + inet_ntoa(state->addr->mask)) == -1) return -1; - if (efprintf(fp, "%s%ssubnet_cidr=%d", - prefix, pf, inet_ntocidr(state->addr->mask)) == -1) + if (efprintf(fp, "%s%ssubnet_cidr=%d", prefix, pf, + inet_ntocidr(state->addr->mask)) == -1) return -1; - if (efprintf(fp, "%s%sbroadcast_address=%s", - prefix, pf, inet_ntoa(state->addr->brd)) == -1) + if (efprintf(fp, "%s%sbroadcast_address=%s", prefix, pf, + inet_ntoa(state->addr->brd)) == -1) return -1; netnum.s_addr = state->addr->addr.s_addr & state->addr->mask.s_addr; - if (efprintf(fp, "%s%snetwork_number=%s", - prefix, pf, inet_ntoa(netnum)) == -1) + if (efprintf(fp, "%s%snetwork_number=%s", prefix, pf, + inet_ntoa(netnum)) == -1) return -1; return 5; } @@ -210,7 +204,7 @@ ipv4ll_freearp(struct interface *ifp) state->arp = NULL; } #else -#define ipv4ll_freearp(ifp) +#define ipv4ll_freearp(ifp) #endif static void @@ -224,8 +218,8 @@ ipv4ll_not_found(struct interface *ifp) #ifdef IN_IFF_NOTREADY if (ia == NULL || ia->addr_flags & IN_IFF_NOTREADY) #endif - loginfox("%s: using IPv4LL address %s", - ifp->name, inet_ntoa(state->pickedaddr)); + loginfox("%s: using IPv4LL address %s", ifp->name, + inet_ntoa(state->pickedaddr)); if (ia == NULL) { if (ifp->ctx->options & DHCPCD_TEST) { ia = malloc(sizeof(*ia)); @@ -240,9 +234,9 @@ ipv4ll_not_found(struct interface *ifp) ifp->name, inet_ntoa(state->pickedaddr)); return; } - ia = ipv4_addaddr(ifp, &state->pickedaddr, - &inaddr_llmask, &inaddr_llbcast, - DHCP_INFINITE_LIFETIME, DHCP_INFINITE_LIFETIME); + ia = ipv4_addaddr(ifp, &state->pickedaddr, &inaddr_llmask, + &inaddr_llbcast, DHCP_INFINITE_LIFETIME, + DHCP_INFINITE_LIFETIME); } if (ia == NULL) return; @@ -272,12 +266,11 @@ ipv4ll_found(struct interface *ifp) ipv4ll_freearp(ifp); if (++state->conflicts == MAX_CONFLICTS) - logerrx("%s: failed to acquire an IPv4LL address", - ifp->name); + logerrx("%s: failed to acquire an IPv4LL address", ifp->name); ipv4ll_pickaddr(ifp); eloop_timeout_add_sec(ifp->ctx->eloop, - state->conflicts >= MAX_CONFLICTS ? - RATE_LIMIT_INTERVAL : PROBE_WAIT, + state->conflicts >= MAX_CONFLICTS ? RATE_LIMIT_INTERVAL : + PROBE_WAIT, ipv4ll_start_arp, ifp); } @@ -300,21 +293,18 @@ ipv4ll_defend_failed(struct interface *ifp) static void ipv4ll_not_found_arp(struct arp_state *astate) { - ipv4ll_not_found(astate->iface); } static void ipv4ll_found_arp(struct arp_state *astate, __unused const struct arp_msg *amsg) { - ipv4ll_found(astate->iface); } static void ipv4ll_defend_failed_arp(struct arp_state *astate) { - ipv4ll_defend_failed(astate->iface); } #endif @@ -354,8 +344,8 @@ ipv4ll_start(void *arg) memcpy(&seed, ifp->hwaddr + ifp->hwlen - sizeof(seed), sizeof(seed)); /* coverity[dont_call] */ - orig = initstate(seed, - state->randomstate, sizeof(state->randomstate)); + orig = initstate(seed, state->randomstate, + sizeof(state->randomstate)); /* Save the original state. */ if (ifp->ctx->randomstate == NULL) @@ -503,7 +493,6 @@ ipv4ll_reset(struct interface *ifp) void ipv4ll_free(struct interface *ifp) { - assert(ifp != NULL); ipv4ll_freearp(ifp); @@ -548,12 +537,10 @@ ipv4ll_handleifa(int cmd, struct ipv4_addr *ia, pid_t pid) if (state == NULL) return ia; - if (cmd == RTM_DELADDR && - state->addr != NULL && - IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr)) - { - loginfox("%s: pid %d deleted IP address %s", - ifp->name, (int)pid, ia->saddr); + if (cmd == RTM_DELADDR && state->addr != NULL && + IN_ARE_ADDR_EQUAL(&state->addr->addr, &ia->addr)) { + loginfox("%s: pid %d deleted IP address %s", ifp->name, + (int)pid, ia->saddr); ipv4ll_defend_failed(ifp); return ia; } diff --git a/src/ipv4ll.h b/src/ipv4ll.h index 9d8ec447..b483d61f 100644 --- a/src/ipv4ll.h +++ b/src/ipv4ll.h @@ -29,12 +29,12 @@ #ifndef IPV4LL_H #define IPV4LL_H -#define LINKLOCAL_ADDR 0xa9fe0000 -#define LINKLOCAL_MASK IN_CLASSB_NET -#define LINKLOCAL_BCAST (LINKLOCAL_ADDR | ~LINKLOCAL_MASK) +#define LINKLOCAL_ADDR 0xa9fe0000 +#define LINKLOCAL_MASK IN_CLASSB_NET +#define LINKLOCAL_BCAST (LINKLOCAL_ADDR | ~LINKLOCAL_MASK) #ifndef IN_LINKLOCAL -# define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR) +#define IN_LINKLOCAL(addr) ((addr & IN_CLASSB_NET) == LINKLOCAL_ADDR) #endif #ifdef IPV4LL @@ -53,16 +53,16 @@ struct ipv4ll_state { #endif }; -#define IPV4LL_STATE(ifp) \ +#define IPV4LL_STATE(ifp) \ ((struct ipv4ll_state *)(ifp)->if_data[IF_DATA_IPV4LL]) -#define IPV4LL_CSTATE(ifp) \ +#define IPV4LL_CSTATE(ifp) \ ((const struct ipv4ll_state *)(ifp)->if_data[IF_DATA_IPV4LL]) -#define IPV4LL_STATE_RUNNING(ifp) \ - (IPV4LL_CSTATE((ifp)) && !IPV4LL_CSTATE((ifp))->down && \ - (IPV4LL_CSTATE((ifp))->addr != NULL)) +#define IPV4LL_STATE_RUNNING(ifp) \ + (IPV4LL_CSTATE((ifp)) && !IPV4LL_CSTATE((ifp))->down && \ + (IPV4LL_CSTATE((ifp))->addr != NULL)) int ipv4ll_subnetroute(rb_tree_t *, struct interface *); -int ipv4ll_defaultroute(rb_tree_t *,struct interface *); +int ipv4ll_defaultroute(rb_tree_t *, struct interface *); ssize_t ipv4ll_env(FILE *, const char *, const struct interface *); void ipv4ll_start(void *); void ipv4ll_claimed(void *); diff --git a/src/ipv6.c b/src/ipv6.c index 81249a6a..e51168fa 100644 --- a/src/ipv6.c +++ b/src/ipv6.c @@ -26,17 +26,18 @@ * SUCH DAMAGE. */ -#include #include +#include #include #include -#include #include #include #include #include +#include + #include "config.h" #ifdef HAVE_SYS_BITOPS_H @@ -48,8 +49,8 @@ #ifdef BSD /* Purely for the ND6_IFF_AUTO_LINKLOCAL #define which is solely used * to generate our CAN_ADD_LLADDR #define. */ -# include -# include +#include +#include #endif #include @@ -60,12 +61,12 @@ #include #include -#define ELOOP_QUEUE ELOOP_IPV6 +#define ELOOP_QUEUE ELOOP_IPV6 #include "common.h" -#include "if.h" -#include "dhcpcd.h" #include "dhcp6.h" +#include "dhcpcd.h" #include "eloop.h" +#include "if.h" #include "ipv6.h" #include "ipv6nd.h" #include "logerr.h" @@ -74,50 +75,50 @@ #include "script.h" #ifdef HAVE_MD5_H -# ifndef DEPGEN -# include -# endif +#ifndef DEPGEN +#include +#endif #endif #ifdef SHA2_H -# include SHA2_H +#include SHA2_H #endif #ifndef SHA256_DIGEST_LENGTH -# define SHA256_DIGEST_LENGTH 32 +#define SHA256_DIGEST_LENGTH 32 #endif #ifdef IPV6_POLLADDRFLAG -# warning kernel does not report IPv6 address flag changes +#warning kernel does not report IPv6 address flag changes #endif /* Hackery at it's finest. */ #ifndef s6_addr32 -# ifdef __sun -# define s6_addr32 _S6_un._S6_u32 -# else -# define s6_addr32 __u6_addr.__u6_addr32 -# endif +#ifdef __sun +#define s6_addr32 _S6_un._S6_u32 +#else +#define s6_addr32 __u6_addr.__u6_addr32 +#endif #endif #if defined(HAVE_IN6_ADDR_GEN_MODE_NONE) || defined(ND6_IFF_AUTO_LINKLOCAL) || \ defined(IFF_NOLINKLOCAL) /* Only add the LL address if we have a carrier, so DaD works. */ -#define CAN_ADD_LLADDR(ifp) \ - (!((ifp)->options->options & DHCPCD_LINK) || if_is_link_up((ifp))) +#define CAN_ADD_LLADDR(ifp) \ + (!((ifp)->options->options & DHCPCD_LINK) || if_is_link_up((ifp))) #ifdef __sun /* Although we can add our own LL address, we cannot drop it * without unplumbing the if which is a lot of code. * So just keep it for the time being. */ -#define CAN_DROP_LLADDR(ifp) (0) +#define CAN_DROP_LLADDR(ifp) (0) #else -#define CAN_DROP_LLADDR(ifp) (1) +#define CAN_DROP_LLADDR(ifp) (1) #endif #else /* We have no control over the OS adding the LLADDR, so just let it do it * as we cannot force our own view on it. */ -#define CAN_ADD_LLADDR(ifp) (0) -#define CAN_DROP_LLADDR(ifp) (0) +#define CAN_ADD_LLADDR(ifp) (0) +#define CAN_DROP_LLADDR(ifp) (0) #endif #ifdef IPV6_MANAGETEMPADDR @@ -127,7 +128,6 @@ static void ipv6_regentempaddr(void *); int ipv6_init(struct dhcpcd_ctx *ctx) { - if (ctx->ra_routers != NULL) return 0; @@ -201,10 +201,10 @@ static const struct reslowhigh { } reslowhigh[] = { /* RFC4291 + RFC6543 */ { { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0x00, 0x00, 0x00 }, - { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0xff, 0xff, 0xff } }, + { 0x02, 0x00, 0x5e, 0xff, 0xfe, 0xff, 0xff, 0xff } }, /* RFC2526 */ { { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80 }, - { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } + { 0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff } } }; static bool @@ -228,10 +228,9 @@ ipv6_reserved(const struct in6_addr *addr) } static int -ipv6_makehwaddr(struct in6_addr *addr, - const struct in6_addr *prefix, int prefix_len, const struct interface *ifp) +ipv6_makehwaddr(struct in6_addr *addr, const struct in6_addr *prefix, + int prefix_len, const struct interface *ifp) { - if (prefix_len > 64) { errno = EINVAL; return -1; @@ -241,8 +240,8 @@ ipv6_makehwaddr(struct in6_addr *addr, switch (ifp->hwtype) { case ARPHRD_ETHER: if (ifp->hwlen == 6) { - addr->s6_addr[ 8] = ifp->hwaddr[0]; - addr->s6_addr[ 9] = ifp->hwaddr[1]; + addr->s6_addr[8] = ifp->hwaddr[0]; + addr->s6_addr[9] = ifp->hwaddr[1]; addr->s6_addr[10] = ifp->hwaddr[2]; addr->s6_addr[11] = 0xff; addr->s6_addr[12] = 0xfe; @@ -272,11 +271,10 @@ ipv6_makehwaddr(struct in6_addr *addr, /* RFC7217 */ static int -ipv6_makestableprivate1(struct dhcpcd_ctx *ctx, - struct in6_addr *addr, const struct in6_addr *prefix, int prefix_len, +ipv6_makestableprivate1(struct dhcpcd_ctx *ctx, struct in6_addr *addr, + const struct in6_addr *prefix, int prefix_len, const unsigned char *netiface, size_t netiface_len, - const unsigned char *netid, size_t netid_len, - unsigned short vlanid, + const unsigned char *netid, size_t netid_len, unsigned short vlanid, uint32_t *dad_counter) { unsigned char buf[2048], *p, digest[SHA256_DIGEST_LENGTH]; @@ -350,10 +348,8 @@ ipv6_makestableprivate1(struct dhcpcd_ctx *ctx, } int -ipv6_makestableprivate(struct in6_addr *addr, - const struct in6_addr *prefix, int prefix_len, - const struct interface *ifp, - int *dad_counter) +ipv6_makestableprivate(struct in6_addr *addr, const struct in6_addr *prefix, + int prefix_len, const struct interface *ifp, int *dad_counter) { uint32_t dad; int r; @@ -363,9 +359,8 @@ ipv6_makestableprivate(struct in6_addr *addr, /* For our implementation, we shall set the hardware address * as the interface identifier */ r = ipv6_makestableprivate1(ifp->ctx, addr, prefix, prefix_len, - ifp->hwaddr, ifp->hwlen, - ifp->ssid, ifp->ssid_len, - ifp->vlanid, &dad); + ifp->hwaddr, ifp->hwlen, ifp->ssid, ifp->ssid_len, ifp->vlanid, + &dad); if (r == 0) *dad_counter = (int)dad; @@ -374,9 +369,8 @@ ipv6_makestableprivate(struct in6_addr *addr, #ifdef IPV6_AF_TEMPORARY static int -ipv6_maketemporaryaddress(struct in6_addr *addr, - const struct in6_addr *prefix, int prefix_len, - const struct interface *ifp) +ipv6_maketemporaryaddress(struct in6_addr *addr, const struct in6_addr *prefix, + int prefix_len, const struct interface *ifp) { struct in6_addr mask; struct interface *ifpn; @@ -418,8 +412,8 @@ ipv6_makeprefix(struct in6_addr *prefix, const struct in6_addr *addr, int len) int ipv6_mask(struct in6_addr *mask, int len) { - static const unsigned char masks[NBBY] = - { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff }; + static const unsigned char masks[NBBY] = { 0x80, 0xc0, 0xe0, 0xf0, 0xf8, + 0xfc, 0xfe, 0xff }; int bytes, bits, i; if (len < 0 || len > 128) { @@ -497,8 +491,8 @@ ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp, if (ifo->options & DHCPCD_SLAACPRIVATE) { dad = 0; - if (ipv6_makestableprivate(addr, - prefix, prefix_len, ifp, &dad) == -1) + if (ipv6_makestableprivate(addr, prefix, prefix_len, ifp, + &dad) == -1) return -1; return dad; } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifo->token)) { @@ -528,8 +522,8 @@ ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp, * address to base it on. */ if (ifp->hwlen == 0) { if ((ap = ipv6_linklocal(ifp)) == NULL) { - /* We delay a few functions until we get a local-link address - * so this should never be hit. */ + /* We delay a few functions until we get a local-link + * address so this should never be hit. */ errno = ENOENT; return -1; } @@ -547,7 +541,6 @@ ipv6_makeaddr(struct in6_addr *addr, struct interface *ifp, static void in6_to_h64(uint64_t *vhigh, uint64_t *vlow, const struct in6_addr *addr) { - *vhigh = be64dec(addr->s6_addr); *vlow = be64dec(addr->s6_addr + 8); } @@ -555,34 +548,30 @@ in6_to_h64(uint64_t *vhigh, uint64_t *vlow, const struct in6_addr *addr) static void h64_to_in6(struct in6_addr *addr, uint64_t vhigh, uint64_t vlow) { - be64enc(addr->s6_addr, vhigh); be64enc(addr->s6_addr + 8, vlow); } int -ipv6_userprefix( - const struct in6_addr *prefix, // prefix from router - short prefix_len, // length of prefix received - uint64_t user_number, // "random" number from user - struct in6_addr *result, // resultant prefix - short result_len) // desired prefix length +ipv6_userprefix(const struct in6_addr *prefix, // prefix from router + short prefix_len, // length of prefix received + uint64_t user_number, // "random" number from user + struct in6_addr *result, // resultant prefix + short result_len) // desired prefix length { uint64_t vh, vl, user_low, user_high; - if (prefix_len < 1 || prefix_len > 128 || - result_len < 1 || result_len > 128) - { + if (prefix_len < 1 || prefix_len > 128 || result_len < 1 || + result_len > 128) { errno = EINVAL; return -1; } /* Check that the user_number fits inside result_len less prefix_len */ if (result_len < prefix_len || - fls64(user_number) > result_len - prefix_len) - { - errno = ERANGE; - return -1; + fls64(user_number) > result_len - prefix_len) { + errno = ERANGE; + return -1; } /* If user_number is zero, just copy the prefix into the result. */ @@ -646,12 +635,12 @@ ipv6_checkaddrflags(void *arg) if (!(flags & IN6_IFF_TENTATIVE)) { /* Simulate the kernel announcing the new address. */ ipv6_handleifa(ia->iface->ctx, RTM_NEWADDR, - ia->iface->ctx->ifaces, ia->iface->name, - &ia->addr, ia->prefix_len, &ia->dstaddr, flags, 0); + ia->iface->ctx->ifaces, ia->iface->name, &ia->addr, + ia->prefix_len, &ia->dstaddr, flags, 0); } else { /* Still tentative? Check again in a bit. */ - eloop_timeout_add_msec(ia->iface->ctx->eloop, - RETRANS_TIMER / 2, ipv6_checkaddrflags, ia); + eloop_timeout_add_msec(ia->iface->ctx->eloop, RETRANS_TIMER / 2, + ipv6_checkaddrflags, ia); } } #endif @@ -659,7 +648,6 @@ ipv6_checkaddrflags(void *arg) static void ipv6_deletedaddr(struct ipv6_addr *ia) { - #ifdef DHCP6 #ifdef PRIVSEP if (!(ia->iface->ctx->options & DHCPCD_MANAGER)) @@ -687,9 +675,8 @@ ipv6_deleteaddr(struct ipv6_addr *ia) struct ipv6_addr *ap; loginfox("%s: deleting address %s", ia->iface->name, ia->saddr); - if (if_address6(RTM_DELADDR, ia) == -1 && - errno != EADDRNOTAVAIL && errno != ESRCH && - errno != ENXIO && errno != ENODEV) + if (if_address6(RTM_DELADDR, ia) == -1 && errno != EADDRNOTAVAIL && + errno != ESRCH && errno != ENXIO && errno != ENODEV) logerr(__func__); ipv6_deletedaddr(ia); @@ -711,7 +698,7 @@ ipv6_getstate(struct interface *ifp) state = IPV6_STATE(ifp); if (state == NULL) { - ifp->if_data[IF_DATA_IPV6] = calloc(1, sizeof(*state)); + ifp->if_data[IF_DATA_IPV6] = calloc(1, sizeof(*state)); state = IPV6_STATE(ifp); if (state == NULL) { logerr(__func__); @@ -736,8 +723,8 @@ ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now) * route will be scrubbed and re-added. Something might * be using it, so let's avoid it. */ if (ia->flags & IPV6_AF_DADCOMPLETED) { - logdebugx("%s: IP address %s already exists", - ia->iface->name, ia->saddr); + logdebugx("%s: IP address %s already exists", ia->iface->name, + ia->saddr); return 0; } #endif @@ -776,17 +763,16 @@ ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now) ia->saddr); if (ia->prefix_pltime == ND6_INFINITE_LIFETIME && ia->prefix_vltime == ND6_INFINITE_LIFETIME) - logdebugx("%s: pltime infinity, vltime infinity", - ifp->name); + logdebugx("%s: pltime infinity, vltime infinity", ifp->name); else if (ia->prefix_pltime == ND6_INFINITE_LIFETIME) - logdebugx("%s: pltime infinity, vltime %"PRIu32" seconds", + logdebugx("%s: pltime infinity, vltime %" PRIu32 " seconds", ifp->name, ia->prefix_vltime); else if (ia->prefix_vltime == ND6_INFINITE_LIFETIME) - logdebugx("%s: pltime %"PRIu32"seconds, vltime infinity", + logdebugx("%s: pltime %" PRIu32 "seconds, vltime infinity", ifp->name, ia->prefix_pltime); else - logdebugx("%s: pltime %"PRIu32" seconds, vltime %"PRIu32 - " seconds", + logdebugx("%s: pltime %" PRIu32 " seconds, vltime %" PRIu32 + " seconds", ifp->name, ia->prefix_pltime, ia->prefix_vltime); if (if_address6(RTM_NEWADDR, ia) == -1) { @@ -799,13 +785,10 @@ ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now) #ifdef IPV6_MANAGETEMPADDR /* RFC4941 Section 3.4 */ - if (ia->flags & IPV6_AF_TEMPORARY && - ia->prefix_pltime && - ia->prefix_vltime && - ifp->options->options & DHCPCD_SLAACTEMP) + if (ia->flags & IPV6_AF_TEMPORARY && ia->prefix_pltime && + ia->prefix_vltime && ifp->options->options & DHCPCD_SLAACTEMP) eloop_timeout_add_sec(ifp->ctx->eloop, - ia->prefix_pltime - REGEN_ADVANCE, - ipv6_regentempaddr, ia); + ia->prefix_pltime - REGEN_ADVANCE, ipv6_regentempaddr, ia); #endif /* Restore real pltime and vltime */ @@ -820,11 +803,10 @@ ipv6_addaddr1(struct ipv6_addr *ia, struct timespec *now) #endif #ifdef IPV6_POLLADDRFLAG - eloop_timeout_delete(ifp->ctx->eloop, - ipv6_checkaddrflags, ia); + eloop_timeout_delete(ifp->ctx->eloop, ipv6_checkaddrflags, ia); if (!(ia->flags & IPV6_AF_DADCOMPLETED)) { - eloop_timeout_add_msec(ifp->ctx->eloop, - RETRANS_TIMER / 2, ipv6_checkaddrflags, ia); + eloop_timeout_add_msec(ifp->ctx->eloop, RETRANS_TIMER / 2, + ipv6_checkaddrflags, ia); } #endif @@ -864,8 +846,7 @@ ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl) * maintain their own lists of addresses. */ TAILQ_FOREACH(iap, &state->addrs, next) { if (iap->alias[0] != '\0' && - IN6_ARE_ADDR_EQUAL(&iap->addr, &ia->addr)) - { + IN6_ARE_ADDR_EQUAL(&iap->addr, &ia->addr)) { strlcpy(ia->alias, iap->alias, sizeof(ia->alias)); return 0; } @@ -874,8 +855,7 @@ ipv6_aliasaddr(struct ipv6_addr *ia, struct ipv6_addr **repl) lun = 0; find_unit: if (if_makealias(alias, IF_NAMESIZE, ia->iface->name, lun) >= - IF_NAMESIZE) - { + IF_NAMESIZE) { errno = ENOMEM; return -1; } @@ -940,10 +920,8 @@ int ipv6_findaddrmatch(const struct ipv6_addr *addr, const struct in6_addr *match, unsigned int flags) { - if (match == NULL) { - if ((addr->flags & - (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)) == + if ((addr->flags & (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)) == (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED)) return 1; } else if (addr->prefix_vltime && @@ -955,7 +933,8 @@ ipv6_findaddrmatch(const struct ipv6_addr *addr, const struct in6_addr *match, } struct ipv6_addr * -ipv6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, unsigned int flags) +ipv6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, + unsigned int flags) { struct ipv6_addr *nap; #ifdef DHCP6 @@ -982,7 +961,6 @@ ipv6_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, unsigned int int ipv6_doaddr(struct ipv6_addr *ia, struct timespec *now) { - /* A delegated prefix is not an address. */ if (ia->flags & IPV6_AF_PFXDELEGATION) return 0; @@ -990,8 +968,8 @@ ipv6_doaddr(struct ipv6_addr *ia, struct timespec *now) if (ia->prefix_vltime == 0) { if (ia->flags & IPV6_AF_ADDED) ipv6_deleteaddr(ia); - eloop_q_timeout_delete(ia->iface->ctx->eloop, - ELOOP_QUEUE_ALL, NULL, ia); + eloop_q_timeout_delete(ia->iface->ctx->eloop, ELOOP_QUEUE_ALL, + NULL, ia); if (ia->flags & IPV6_AF_REQUEST) { ia->flags &= ~IPV6_AF_ADDED; return 0; @@ -999,8 +977,7 @@ ipv6_doaddr(struct ipv6_addr *ia, struct timespec *now) return -1; } - if (ia->flags & IPV6_AF_STALE || - IN6_IS_ADDR_UNSPECIFIED(&ia->addr)) + if (ia->flags & IPV6_AF_STALE || IN6_IS_ADDR_UNSPECIFIED(&ia->addr)) return 0; ipv6_addaddr(ia, now); @@ -1072,30 +1049,28 @@ ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop, #ifndef SMALL if (ifd != NULL && (ap->delegating_prefix == NULL || - ap->delegating_prefix->iface != ifd)) + ap->delegating_prefix->iface != ifd)) continue; #endif if (drop != 2) TAILQ_REMOVE(addrs, ap, next); if (drop && ap->flags & IPV6_AF_ADDED && (ap->iface->options->options & - (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != - (DHCPCD_EXITING | DHCPCD_PERSISTENT)) - { + (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != + (DHCPCD_EXITING | DHCPCD_PERSISTENT)) { /* Don't drop link-local addresses. */ if (!IN6_IS_ADDR_LINKLOCAL(&ap->addr) || - CAN_DROP_LLADDR(ap->iface)) - { + CAN_DROP_LLADDR(ap->iface)) { if (drop == 2) TAILQ_REMOVE(addrs, ap, next); /* Find the same address somewhere else */ apf = ipv6_findaddr(ap->iface->ctx, &ap->addr, 0); - if ((apf == NULL || - (apf->iface != ap->iface))) + if ((apf == NULL || (apf->iface != ap->iface))) ipv6_deleteaddr(ap); if (!(ap->iface->options->options & - DHCPCD_EXITING) && apf) + DHCPCD_EXITING) && + apf) ipv6_addaddr(apf, &now); if (drop == 2) ipv6_freeaddr(ap); @@ -1156,9 +1131,8 @@ ipv6_anyglobal(struct interface *sifp) } void -ipv6_handleifa(struct dhcpcd_ctx *ctx, - int cmd, struct if_head *ifs, const char *ifname, - const struct in6_addr *addr, uint8_t prefix_len, +ipv6_handleifa(struct dhcpcd_ctx *ctx, int cmd, struct if_head *ifs, + const char *ifname, const struct in6_addr *addr, uint8_t prefix_len, const struct in6_addr *dstaddr, int addrflags, pid_t pid) { struct interface *ifp; @@ -1172,9 +1146,8 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, /* Solaris on-link route is an unspecified address! */ if (IN6_IS_ADDR_UNSPECIFIED(addr)) { - if (if_getsubnet(ctx, ifname, AF_INET6, - &subnet, sizeof(subnet)) == -1) - { + if (if_getsubnet(ctx, ifname, AF_INET6, &subnet, + sizeof(subnet)) == -1) { logerr(__func__); return; } @@ -1235,8 +1208,7 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, * As we can't extend infinite, we'll * create a new temporary address. */ ia->prefix_pltime = 0; - ia->prefix_vltime = - ND6_INFINITE_LIFETIME; + ia->prefix_vltime = ND6_INFINITE_LIFETIME; } /* This is a minor regression against RFC 4941 * because the kernel only knows when the @@ -1261,10 +1233,8 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, #ifdef IPV6_POLLADDRFLAG if ((IN6_IS_ADDR_LINKLOCAL(&ia->addr) || ia->dadcallback) && - ia->addr_flags & IN6_IFF_TENTATIVE) - { - eloop_timeout_add_msec( - ia->iface->ctx->eloop, + ia->addr_flags & IN6_IFF_TENTATIVE) { + eloop_timeout_add_msec(ia->iface->ctx->eloop, RETRANS_TIMER / 2, ipv6_checkaddrflags, ia); } #endif @@ -1278,18 +1248,16 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, if (ia == NULL) return; - if (ia->dadcallback && ((ia->addr_flags & - (IN6_IFF_DETACHED | IN6_IFF_TENTATIVE)) == 0 || - ia->addr_flags & IN6_IFF_DUPLICATED)) + if (ia->dadcallback && + ((ia->addr_flags & (IN6_IFF_DETACHED | IN6_IFF_TENTATIVE)) == 0 || + ia->addr_flags & IN6_IFF_DUPLICATED)) ia->dadcallback(ia); if (IN6_IS_ADDR_LINKLOCAL(&ia->addr) && - !(ia->addr_flags & IN6_IFF_NOTUSEABLE)) - { + !(ia->addr_flags & IN6_IFF_NOTUSEABLE)) { /* Now run any callbacks. * Typically IPv6RS or DHCPv6 */ - while ((cb = TAILQ_FIRST(&state->ll_callbacks))) - { + while ((cb = TAILQ_FIRST(&state->ll_callbacks))) { TAILQ_REMOVE(&state->ll_callbacks, cb, next); cb->callback(cb->arg); free(cb); @@ -1314,7 +1282,7 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, * call rt_build to add/remove the default route. */ if (ifp->active && ((ifp->options != NULL && ifp->options->options & DHCPCD_IPV6) || - (ifp->options == NULL && ctx->options & DHCPCD_IPV6)) && + (ifp->options == NULL && ctx->options & DHCPCD_IPV6)) && !(ctx->options & DHCPCD_RTBUILD) && (ipv6_anyglobal(ifp) != NULL) != anyglobal) rt_build(ctx, AF_INET6); @@ -1323,7 +1291,6 @@ ipv6_handleifa(struct dhcpcd_ctx *ctx, int ipv6_hasaddr(const struct interface *ifp) { - if (ipv6nd_iffindaddr(ifp, NULL, 0) != NULL) return 1; #ifdef DHCP6 @@ -1392,7 +1359,6 @@ ipv6_findmaskaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr) return NULL; } - static struct ipv6_addr * ipv6_iffinddstaddr(const struct interface *ifp, const struct in6_addr *addr) { @@ -1424,8 +1390,8 @@ ipv6_finddstaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr) } int -ipv6_addlinklocalcallback(struct interface *ifp, - void (*callback)(void *), void *arg) +ipv6_addlinklocalcallback(struct interface *ifp, void (*callback)(void *), + void *arg) { struct ipv6_state *state; struct ll_callback *cb; @@ -1464,8 +1430,8 @@ ipv6_newlinklocal(struct interface *ifp) } static const uint8_t allzero[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; -static const uint8_t allone[8] = - { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; +static const uint8_t allone[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff }; static int ipv6_addlinklocal(struct interface *ifp) @@ -1487,8 +1453,7 @@ ipv6_addlinklocal(struct interface *ifp) return -1; } if (memcmp(ifp->hwaddr, allzero, ifp->hwlen) == 0 || - memcmp(ifp->hwaddr, allone, ifp->hwlen) == 0) - { + memcmp(ifp->hwaddr, allone, ifp->hwlen) == 0) { errno = EINVAL; return -1; } @@ -1509,16 +1474,15 @@ ipv6_addlinklocal(struct interface *ifp) dadcounter = 0; if (ifp->options->options & DHCPCD_SLAACPRIVATE) { -nextslaacprivate: - if (ipv6_makestableprivate(&ap->addr, - &ap->prefix, ap->prefix_len, ifp, &dadcounter) == -1) - { + nextslaacprivate: + if (ipv6_makestableprivate(&ap->addr, &ap->prefix, + ap->prefix_len, ifp, &dadcounter) == -1) { free(ap); return -1; } ap->dadcounter = dadcounter; - } else if (ipv6_makehwaddr(&ap->addr, - &ap->prefix, ap->prefix_len, ifp) == -1) { + } else if (ipv6_makehwaddr(&ap->addr, &ap->prefix, ap->prefix_len, + ifp) == -1) { free(ap); return -1; } @@ -1528,8 +1492,7 @@ ipv6_addlinklocal(struct interface *ifp) if (IN6_ARE_ADDR_EQUAL(&ap->addr, &ap2->addr)) { if (ap2->addr_flags & IN6_IFF_DUPLICATED) { if (ifp->options->options & - DHCPCD_SLAACPRIVATE) - { + DHCPCD_SLAACPRIVATE) { dadcounter++; goto nextslaacprivate; } @@ -1541,7 +1504,7 @@ ipv6_addlinklocal(struct interface *ifp) logwarnx("%s: waiting for %s to complete", ap2->iface->name, ap2->saddr); free(ap); - errno = EEXIST; + errno = EEXIST; return 0; } } @@ -1566,8 +1529,7 @@ ipv6_tryaddlinklocal(struct interface *ifp) if (ia != NULL) { #ifdef IPV6_POLLADDRFLAG if (ia->addr_flags & IN6_IFF_TENTATIVE) { - eloop_timeout_add_msec( - ia->iface->ctx->eloop, + eloop_timeout_add_msec(ia->iface->ctx->eloop, RETRANS_TIMER / 2, ipv6_checkaddrflags, ia); } #endif @@ -1582,14 +1544,12 @@ ipv6_tryaddlinklocal(struct interface *ifp) void ipv6_setscope(struct sockaddr_in6 *sin, unsigned int ifindex) { - #ifdef __KAME__ /* KAME based systems want to store the scope inside the sin6_addr * for link local addresses */ if (IN6_IS_ADDR_LINKLOCAL(&sin->sin6_addr)) { uint16_t scope = htons((uint16_t)ifindex); - memcpy(&sin->sin6_addr.s6_addr[2], &scope, - sizeof(scope)); + memcpy(&sin->sin6_addr.s6_addr[2], &scope, sizeof(scope)); } sin->sin6_scope_id = 0; #else @@ -1647,9 +1607,7 @@ ipv6_newaddr(struct interface *ifp, const struct in6_addr *addr, memcpy(&ia->addr, &iaf->addr, sizeof(ia->addr)); else { ia->dadcounter = ipv6_makeaddr(&ia->addr, ifp, - &ia->prefix, - ia->prefix_len, - ia->flags); + &ia->prefix, ia->prefix_len, ia->flags); if (ia->dadcounter == -1) goto err; } @@ -1667,10 +1625,10 @@ ipv6_newaddr(struct interface *ifp, const struct in6_addr *addr, cbp = inet_ntop(AF_INET6, &ia->prefix, buf, sizeof(buf)); goto paddr; } else { -makepfx: + makepfx: ia->addr = *addr; - if (ipv6_makeprefix(&ia->prefix, - &ia->addr, ia->prefix_len) == -1) + if (ipv6_makeprefix(&ia->prefix, &ia->addr, ia->prefix_len) == + -1) goto err; } @@ -1712,11 +1670,9 @@ ipv6_staticdadcallback(void *arg) wascompleted = (ia->flags & IPV6_AF_DADCOMPLETED); ia->flags |= IPV6_AF_DADCOMPLETED; if (ia->addr_flags & IN6_IFF_DUPLICATED) - logwarnx("%s: DAD detected %s", ia->iface->name, - ia->saddr); + logwarnx("%s: DAD detected %s", ia->iface->name, ia->saddr); else if (!wascompleted) { - logdebugx("%s: IPv6 static DAD completed", - ia->iface->name); + logdebugx("%s: IPv6 static DAD completed", ia->iface->name); } #define FINISHED (IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED) @@ -1728,8 +1684,7 @@ ipv6_staticdadcallback(void *arg) state = IPV6_STATE(ifp); TAILQ_FOREACH(ia, &state->addrs, next) { if (ia->flags & IPV6_AF_STATIC && - (ia->flags & FINISHED) != FINISHED) - { + (ia->flags & FINISHED) != FINISHED) { wascompleted = 1; break; } @@ -1785,8 +1740,7 @@ ipv6_startstatic(struct interface *ifp) ia = ipv6_iffindaddr(ifp, &ifp->options->req_addr6, 0); if (ia != NULL && (ia->prefix_len != ifp->options->req_prefix_len || - ia->addr_flags & IN6_IFF_NOTUSEABLE)) - { + ia->addr_flags & IN6_IFF_NOTUSEABLE)) { ipv6_deleteaddr(ia); ia = NULL; } @@ -1879,14 +1833,13 @@ ipv6_freedrop(struct interface *ifp, int drop) void ipv6_ctxfree(struct dhcpcd_ctx *ctx) { - free(ctx->ra_routers); free(ctx->secret); } int -ipv6_handleifa_addrs(int cmd, - struct ipv6_addrhead *addrs, const struct ipv6_addr *addr, pid_t pid) +ipv6_handleifa_addrs(int cmd, struct ipv6_addrhead *addrs, + const struct ipv6_addr *addr, pid_t pid) { struct ipv6_addr *ia, *ian; int found = 0, alldadcompleted = 1; @@ -1913,10 +1866,9 @@ ipv6_handleifa_addrs(int cmd, /* Check DAD. * On Linux we can get IN6_IFF_DUPLICATED via RTM_DELADDR. */ if (((ia->addr_flags & - (IN6_IFF_DETACHED | IN6_IFF_TENTATIVE)) == 0 || - ia->addr_flags & IN6_IFF_DUPLICATED) && - (ia->flags & IPV6_AF_DADCOMPLETED) == 0) - { + (IN6_IFF_DETACHED | IN6_IFF_TENTATIVE)) == 0 || + ia->addr_flags & IN6_IFF_DUPLICATED) && + (ia->flags & IPV6_AF_DADCOMPLETED) == 0) { found++; if (ia->dadcallback) ia->dadcallback(ia); @@ -1955,8 +1907,8 @@ ipv6_regen_desync(struct interface *ifp, bool force) if (state->desync_factor && !force && state->desync_factor < max) return; if (state->desync_factor == 0) - state->desync_factor = - arc4random_uniform(MIN(MAX_DESYNC_FACTOR, max)); + state->desync_factor = arc4random_uniform( + MIN(MAX_DESYNC_FACTOR, max)); max = TEMP_PREFERRED_LIFETIME - state->desync_factor - REGEN_ADVANCE; eloop_timeout_add_sec(ifp->ctx->eloop, max, ipv6_regentempaddrs, ifp); } @@ -2011,9 +1963,8 @@ ipv6_createtempaddr(struct ipv6_addr *ia0, const struct timespec *now) TEMP_PREFERRED_LIFETIME - state->desync_factor); ia->prefix_vltime = MIN(ia0->prefix_vltime, TEMP_VALID_LIFETIME); if (ia->prefix_pltime <= REGEN_ADVANCE || - ia->prefix_pltime > ia0->prefix_vltime) - { - errno = EINVAL; + ia->prefix_pltime > ia0->prefix_vltime) { + errno = EINVAL; free(ia); return NULL; } @@ -2031,17 +1982,15 @@ ipv6_settemptime(struct ipv6_addr *ia, int flags) state = IPV6_STATE(ia->iface); first = NULL; TAILQ_FOREACH_REVERSE(ap, &state->addrs, ipv6_addrhead, next) { - if (ap->flags & IPV6_AF_TEMPORARY && - ap->prefix_pltime && - IN6_ARE_ADDR_EQUAL(&ia->prefix, &ap->prefix)) - { + if (ap->flags & IPV6_AF_TEMPORARY && ap->prefix_pltime && + IN6_ARE_ADDR_EQUAL(&ia->prefix, &ap->prefix)) { unsigned int max, ext; if (flags == 0) { if (ap->prefix_pltime - - (uint32_t)(ia->acquired.tv_sec - - ap->acquired.tv_sec) - < REGEN_ADVANCE) + (uint32_t)(ia->acquired.tv_sec - + ap->acquired.tv_sec) < + REGEN_ADVANCE) continue; return ap; @@ -2064,18 +2013,17 @@ ipv6_settemptime(struct ipv6_addr *ia, int flags) /* RFC4941 Section 3.3.2 * Extend temporary times, but ensure that they * never last beyond the system limit. */ - ext = (unsigned int)ia->acquired.tv_sec - + ia->prefix_pltime; + ext = (unsigned int)ia->acquired.tv_sec + + ia->prefix_pltime; max = (unsigned int)(ap->created.tv_sec + - TEMP_PREFERRED_LIFETIME - - state->desync_factor); + TEMP_PREFERRED_LIFETIME - state->desync_factor); if (ext < max) ap->prefix_pltime = ia->prefix_pltime; else - ap->prefix_pltime = - (uint32_t)(max - ia->acquired.tv_sec); + ap->prefix_pltime = (uint32_t)(max - + ia->acquired.tv_sec); -valid: + valid: ext = (unsigned int)ia->acquired.tv_sec + ia->prefix_vltime; max = (unsigned int)(ap->created.tv_sec + @@ -2083,8 +2031,8 @@ ipv6_settemptime(struct ipv6_addr *ia, int flags) if (ext < max) ap->prefix_vltime = ia->prefix_vltime; else - ap->prefix_vltime = - (uint32_t)(max - ia->acquired.tv_sec); + ap->prefix_vltime = (uint32_t)(max - + ia->acquired.tv_sec); /* Just extend the latest matching prefix */ ap->acquired = ia->acquired; @@ -2157,8 +2105,7 @@ ipv6_regentempaddrs(void *arg) /* Mark addresses for regen so we don't infinite loop. */ TAILQ_FOREACH(ia, &state->addrs, next) { if (ia->flags & IPV6_AF_TEMPORARY && - ia->flags & IPV6_AF_ADDED && - !(ia->flags & IPV6_AF_STALE)) + ia->flags & IPV6_AF_ADDED && !(ia->flags & IPV6_AF_STALE)) ia->flags |= IPV6_AF_REGEN; else ia->flags &= ~IPV6_AF_REGEN; @@ -2202,14 +2149,12 @@ ipv6_deletestaleaddrs(struct interface *ifp) TAILQ_FOREACH_SAFE(ia, &state->addrs, next, ia1) { if (ia->flags & IPV6_AF_STALE) - ipv6_handleifa(ifp->ctx, RTM_DELADDR, - ifp->ctx->ifaces, ifp->name, - &ia->addr, ia->prefix_len, - &ia->dstaddr, 0, getpid()); + ipv6_handleifa(ifp->ctx, RTM_DELADDR, ifp->ctx->ifaces, + ifp->name, &ia->addr, ia->prefix_len, &ia->dstaddr, + 0, getpid()); } } - static struct rt * inet6_makeroute(struct interface *ifp, const struct ra *rap) { @@ -2258,7 +2203,7 @@ inet6_makeprefix(struct interface *ifp, const struct ra *rap, } if (lo0 == NULL) logwarnx("cannot find a loopback interface " - "to reject via"); + "to reject via"); else ifp = lo0; } @@ -2296,8 +2241,8 @@ inet6_makerouter(struct ra *rap) return rt; } -#define RT_IS_DEFAULT(rtp) \ - (IN6_ARE_ADDR_EQUAL(&((rtp)->dest), &in6addr_any) && \ +#define RT_IS_DEFAULT(rtp) \ + (IN6_ARE_ADDR_EQUAL(&((rtp)->dest), &in6addr_any) && \ IN6_ARE_ADDR_EQUAL(&((rtp)->mask), &in6addr_any)) static int @@ -2313,8 +2258,7 @@ inet6_staticroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) continue; TAILQ_FOREACH(ia, &state->addrs, next) { if ((ia->flags & (IPV6_AF_ADDED | IPV6_AF_STATIC)) == - (IPV6_AF_ADDED | IPV6_AF_STATIC)) - { + (IPV6_AF_ADDED | IPV6_AF_STATIC)) { rt = inet6_makeprefix(ifp, NULL, ia); if (rt) rt_proto_add(routes, rt); @@ -2341,8 +2285,8 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) continue; /* add rfc4191 route information routes */ - TAILQ_FOREACH (rinfo, &rap->rinfos, next) { - if(rinfo->lifetime == 0) + TAILQ_FOREACH(rinfo, &rap->rinfos, next) { + if (rinfo->lifetime == 0) continue; if ((rt = inet6_makeroute(rap->iface, rap)) == NULL) continue; @@ -2421,8 +2365,7 @@ inet6_raroutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx) #ifdef DHCP6 static int -inet6_dhcproutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx, - enum DH6S dstate) +inet6_dhcproutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx, enum DH6S dstate) { struct interface *ifp; const struct dhcp6_state *d6_state; @@ -2463,7 +2406,6 @@ inet6_dhcproutes(rb_tree_t *routes, struct dhcpcd_ctx *ctx, bool inet6_getroutes(struct dhcpcd_ctx *ctx, rb_tree_t *routes) { - /* Should static take priority? */ if (inet6_staticroutes(routes, ctx) == -1) return false; diff --git a/src/ipv6.h b/src/ipv6.h index fd9f4f23..2e731fb4 100644 --- a/src/ipv6.h +++ b/src/ipv6.h @@ -30,63 +30,74 @@ #define IPV6_H #include + #include #include "config.h" #include "if.h" #ifndef __linux__ -# if !defined(__QNX__) && !defined(__sun) -# include -# endif -# include -# ifndef __sun -# include -# endif +#if !defined(__QNX__) && !defined(__sun) +#include +#endif +#include +#ifndef __sun +#include +#endif #endif -#define EUI64_GBIT 0x01 -#define EUI64_UBIT 0x02 -#define EUI64_TO_IFID(in6) do {(in6)->s6_addr[8] ^= EUI64_UBIT; } while (0) -#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) +#define EUI64_GBIT 0x01 +#define EUI64_UBIT 0x02 +#define EUI64_TO_IFID(in6) \ + do { \ + (in6)->s6_addr[8] ^= EUI64_UBIT; \ + } while (0) +#define EUI64_GROUP(in6) ((in6)->s6_addr[8] & EUI64_GBIT) #ifndef ND6_INFINITE_LIFETIME -# define ND6_INFINITE_LIFETIME ((uint32_t)~0) +#define ND6_INFINITE_LIFETIME ((uint32_t)~0) #endif /* RFC4941 constants */ -#define TEMP_VALID_LIFETIME 604800 /* 1 week */ -#define TEMP_PREFERRED_LIFETIME 86400 /* 1 day */ -#define REGEN_ADVANCE 5 /* seconds */ -#define MAX_DESYNC_FACTOR 600 /* 10 minutes */ +#define TEMP_VALID_LIFETIME 604800 /* 1 week */ +#define TEMP_PREFERRED_LIFETIME 86400 /* 1 day */ +#define REGEN_ADVANCE 5 /* seconds */ +#define MAX_DESYNC_FACTOR 600 /* 10 minutes */ #define TEMP_IDGEN_RETRIES 3 /* RFC7217 constants */ -#define IDGEN_RETRIES 3 -#define IDGEN_DELAY 1 /* second */ +#define IDGEN_RETRIES 3 +#define IDGEN_DELAY 1 /* second */ /* Interface identifier length. Prefix + this == 128 for autoconf */ -#define ipv6_ifidlen(ifp) 64 -#define IA6_CANAUTOCONF(ia) \ +#define ipv6_ifidlen(ifp) 64 +#define IA6_CANAUTOCONF(ia) \ ((ia)->prefix_len + ipv6_ifidlen((ia)->iface) == 128) #ifndef IN6_ARE_MASKED_ADDR_EQUAL -#define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \ - (((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \ - (((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == 0 && \ - (((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == 0 && \ - (((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == 0 ) +#define IN6_ARE_MASKED_ADDR_EQUAL(d, a, m) \ + ((((d)->s6_addr32[0] ^ (a)->s6_addr32[0]) & (m)->s6_addr32[0]) == 0 && \ + (((d)->s6_addr32[1] ^ (a)->s6_addr32[1]) & (m)->s6_addr32[1]) == \ + 0 && \ + (((d)->s6_addr32[2] ^ (a)->s6_addr32[2]) & (m)->s6_addr32[2]) == \ + 0 && \ + (((d)->s6_addr32[3] ^ (a)->s6_addr32[3]) & (m)->s6_addr32[3]) == \ + 0) #endif #ifndef IN6ADDR_LINKLOCAL_ALLNODES_INIT -#define IN6ADDR_LINKLOCAL_ALLNODES_INIT \ - {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }}} +#define IN6ADDR_LINKLOCAL_ALLNODES_INIT \ + { \ + {{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 }} \ + } #endif #ifndef IN6ADDR_LINKLOCAL_ALLROUTERS_INIT -#define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \ - {{{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }}} +#define IN6ADDR_LINKLOCAL_ALLROUTERS_INIT \ + { \ + {{ 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02 }} \ + } #endif /* @@ -96,31 +107,31 @@ */ #ifndef __linux__ /* We guard here to avoid breaking a compile on linux ppc-64 headers */ -# include +#include #endif #ifdef BSD -# define IPV6_POLLADDRFLAG +#define IPV6_POLLADDRFLAG #endif /* This was fixed in NetBSD */ #if (defined(__DragonFly_version) && __DragonFly_version >= 500704) || \ (defined(__NetBSD_Version__) && __NetBSD_Version__ >= 699002000) -# undef IPV6_POLLADDRFLAG +#undef IPV6_POLLADDRFLAG #endif /* Of course OpenBSD has their own special name. */ #if !defined(IN6_IFF_TEMPORARY) && defined(IN6_IFF_PRIVACY) -#define IN6_IFF_TEMPORARY IN6_IFF_PRIVACY +#define IN6_IFF_TEMPORARY IN6_IFF_PRIVACY #endif #ifdef __sun - /* Solaris lacks these defines. - * While it supports DaD, to seems to only expose IFF_DUPLICATE - * so we have no way of knowing if it's tentative or not. - * I don't even know if Solaris has any special treatment for tentative. */ -# define IN6_IFF_TENTATIVE 0x02 -# define IN6_IFF_DUPLICATED 0x04 -# define IN6_IFF_DETACHED 0x00 +/* Solaris lacks these defines. + * While it supports DaD, to seems to only expose IFF_DUPLICATE + * so we have no way of knowing if it's tentative or not. + * I don't even know if Solaris has any special treatment for tentative. */ +#define IN6_IFF_TENTATIVE 0x02 +#define IN6_IFF_DUPLICATED 0x04 +#define IN6_IFF_DETACHED 0x00 #endif #define IN6_IFF_NOTUSEABLE \ @@ -137,25 +148,25 @@ * Linux-3.18 allows the marking of addresses from which to manage temp addrs. */ #if defined(IN6_IFF_TEMPORARY) && !defined(__linux__) -#define IPV6_MANAGETEMPADDR +#define IPV6_MANAGETEMPADDR #endif #ifdef __linux__ - /* Match Linux defines to BSD */ -# ifdef IFA_F_TEMPORARY -# define IN6_IFF_TEMPORARY IFA_F_TEMPORARY -# endif -# ifdef IFA_F_OPTIMISTIC -# define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC) -# else -# define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | 0x04) -# endif -# ifdef IF_F_DADFAILED -# define IN6_IFF_DUPLICATED IFA_F_DADFAILED -# else -# define IN6_IFF_DUPLICATED 0x08 -# endif -# define IN6_IFF_DETACHED 0 +/* Match Linux defines to BSD */ +#ifdef IFA_F_TEMPORARY +#define IN6_IFF_TEMPORARY IFA_F_TEMPORARY +#endif +#ifdef IFA_F_OPTIMISTIC +#define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | IFA_F_OPTIMISTIC) +#else +#define IN6_IFF_TENTATIVE (IFA_F_TENTATIVE | 0x04) +#endif +#ifdef IF_F_DADFAILED +#define IN6_IFF_DUPLICATED IFA_F_DADFAILED +#else +#define IN6_IFF_DUPLICATED 0x08 +#endif +#define IN6_IFF_DETACHED 0 #endif #ifdef INET6 @@ -199,25 +210,25 @@ struct ipv6_addr { #endif }; -#define IPV6_AF_ONLINK (1U << 0) -#define IPV6_AF_NEW (1U << 1) -#define IPV6_AF_STALE (1U << 2) -#define IPV6_AF_ADDED (1U << 3) -#define IPV6_AF_AUTOCONF (1U << 4) -#define IPV6_AF_DADCOMPLETED (1U << 5) -#define IPV6_AF_PFXDELEGATION (1U << 6) -#define IPV6_AF_DELEGATED (1U << 7) -#define IPV6_AF_NOREJECT (1U << 8) -#define IPV6_AF_REQUEST (1U << 9) -#define IPV6_AF_STATIC (1U << 10) -#define IPV6_AF_DELEGATEDLOG (1U << 11) -#define IPV6_AF_RAPFX (1U << 12) -#define IPV6_AF_EXTENDED (1U << 13) -#define IPV6_AF_REGEN (1U << 14) -#define IPV6_AF_ROUTER (1U << 15) -#define IPV6_AF_ADVERTISED (1U << 16) +#define IPV6_AF_ONLINK (1U << 0) +#define IPV6_AF_NEW (1U << 1) +#define IPV6_AF_STALE (1U << 2) +#define IPV6_AF_ADDED (1U << 3) +#define IPV6_AF_AUTOCONF (1U << 4) +#define IPV6_AF_DADCOMPLETED (1U << 5) +#define IPV6_AF_PFXDELEGATION (1U << 6) +#define IPV6_AF_DELEGATED (1U << 7) +#define IPV6_AF_NOREJECT (1U << 8) +#define IPV6_AF_REQUEST (1U << 9) +#define IPV6_AF_STATIC (1U << 10) +#define IPV6_AF_DELEGATEDLOG (1U << 11) +#define IPV6_AF_RAPFX (1U << 12) +#define IPV6_AF_EXTENDED (1U << 13) +#define IPV6_AF_REGEN (1U << 14) +#define IPV6_AF_ROUTER (1U << 15) +#define IPV6_AF_ADVERTISED (1U << 16) #ifdef IPV6_MANAGETEMPADDR -#define IPV6_AF_TEMPORARY (1U << 17) +#define IPV6_AF_TEMPORARY (1U << 17) #endif struct ll_callback { @@ -236,21 +247,19 @@ struct ipv6_state { #endif }; -#define IPV6_STATE(ifp) \ - ((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) -#define IPV6_CSTATE(ifp) \ +#define IPV6_STATE(ifp) ((struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) +#define IPV6_CSTATE(ifp) \ ((const struct ipv6_state *)(ifp)->if_data[IF_DATA_IPV6]) #define IPV6_STATE_RUNNING(ifp) ipv6_staticdadcompleted((ifp)) - int ipv6_init(struct dhcpcd_ctx *); -int ipv6_makestableprivate(struct in6_addr *, - const struct in6_addr *, int, const struct interface *, int *); +int ipv6_makestableprivate(struct in6_addr *, const struct in6_addr *, int, + const struct interface *, int *); int ipv6_makeaddr(struct in6_addr *, struct interface *, const struct in6_addr *, int, unsigned int); int ipv6_mask(struct in6_addr *, int); uint8_t ipv6_prefixlen(const struct in6_addr *); -int ipv6_userprefix( const struct in6_addr *, short prefix_len, +int ipv6_userprefix(const struct in6_addr *, short prefix_len, uint64_t user_number, struct in6_addr *result, short result_len); void ipv6_checkaddrflags(void *); void ipv6_markaddrsstale(struct interface *, unsigned int); @@ -261,19 +270,18 @@ ssize_t ipv6_addaddrs(struct ipv6_addrhead *addrs); void ipv6_deleteaddr(struct ipv6_addr *); void ipv6_freedrop_addrs(struct ipv6_addrhead *, int, unsigned int, const struct interface *); -void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *, - const char *, const struct in6_addr *, uint8_t, - const struct in6_addr *, int, pid_t); +void ipv6_handleifa(struct dhcpcd_ctx *ctx, int, struct if_head *, const char *, + const struct in6_addr *, uint8_t, const struct in6_addr *, int, pid_t); int ipv6_handleifa_addrs(int, struct ipv6_addrhead *, const struct ipv6_addr *, pid_t); -struct ipv6_addr *ipv6_iffindaddr(struct interface *, - const struct in6_addr *, int); +struct ipv6_addr *ipv6_iffindaddr(struct interface *, const struct in6_addr *, + int); int ipv6_hasaddr(const struct interface *); struct ipv6_addr *ipv6_anyglobal(struct interface *); int ipv6_findaddrmatch(const struct ipv6_addr *, const struct in6_addr *, unsigned int); -struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *, - const struct in6_addr *, unsigned int); +struct ipv6_addr *ipv6_findaddr(struct dhcpcd_ctx *, const struct in6_addr *, + unsigned int); struct ipv6_addr *ipv6_findmaskaddr(struct dhcpcd_ctx *, const struct in6_addr *); struct ipv6_addr *ipv6_finddstaddr(struct dhcpcd_ctx *, diff --git a/src/ipv6nd.c b/src/ipv6nd.c index ccf71241..0be7fdb2 100644 --- a/src/ipv6nd.c +++ b/src/ipv6nd.c @@ -26,14 +26,15 @@ * SUCH DAMAGE. */ -#include #include +#include #include + #include #include +#include #include #include -#include #include #include @@ -44,11 +45,11 @@ #include #include -#define ELOOP_QUEUE ELOOP_IPV6ND +#define ELOOP_QUEUE ELOOP_IPV6ND #include "common.h" -#include "dhcpcd.h" #include "dhcp-common.h" #include "dhcp6.h" +#include "dhcpcd.h" #include "eloop.h" #include "if.h" #include "ipv6.h" @@ -59,25 +60,25 @@ #include "script.h" /* Debugging Router Solicitations is a lot of spam, so disable it */ -//#define DEBUG_RS +// #define DEBUG_RS #ifndef ND_RA_FLAG_HOME_AGENT -#define ND_RA_FLAG_HOME_AGENT 0x20 /* Home Agent flag in RA */ +#define ND_RA_FLAG_HOME_AGENT 0x20 /* Home Agent flag in RA */ #endif #ifndef ND_RA_FLAG_PROXY -#define ND_RA_FLAG_PROXY 0x04 /* Proxy */ +#define ND_RA_FLAG_PROXY 0x04 /* Proxy */ #endif #ifndef ND_OPT_PI_FLAG_ROUTER -#define ND_OPT_PI_FLAG_ROUTER 0x20 /* Router flag in PI */ +#define ND_OPT_PI_FLAG_ROUTER 0x20 /* Router flag in PI */ #endif #ifndef ND_OPT_RI -#define ND_OPT_RI 24 -struct nd_opt_ri { /* Route Information option RFC4191 */ - uint8_t nd_opt_ri_type; - uint8_t nd_opt_ri_len; - uint8_t nd_opt_ri_prefixlen; - uint8_t nd_opt_ri_flags_reserved; +#define ND_OPT_RI 24 +struct nd_opt_ri { /* Route Information option RFC4191 */ + uint8_t nd_opt_ri_type; + uint8_t nd_opt_ri_len; + uint8_t nd_opt_ri_prefixlen; + uint8_t nd_opt_ri_flags_reserved; uint32_t nd_opt_ri_lifetime; struct in6_addr nd_opt_ri_prefix; }; @@ -86,31 +87,31 @@ __CTASSERT(sizeof(struct nd_opt_ri) == 24); #endif #ifndef ND_OPT_RDNSS -#define ND_OPT_RDNSS 25 -struct nd_opt_rdnss { /* RDNSS option RFC 6106 */ - uint8_t nd_opt_rdnss_type; - uint8_t nd_opt_rdnss_len; - uint16_t nd_opt_rdnss_reserved; - uint32_t nd_opt_rdnss_lifetime; +#define ND_OPT_RDNSS 25 +struct nd_opt_rdnss { /* RDNSS option RFC 6106 */ + uint8_t nd_opt_rdnss_type; + uint8_t nd_opt_rdnss_len; + uint16_t nd_opt_rdnss_reserved; + uint32_t nd_opt_rdnss_lifetime; /* followed by list of IP prefixes */ }; __CTASSERT(sizeof(struct nd_opt_rdnss) == 8); #endif #ifndef ND_OPT_DNSSL -#define ND_OPT_DNSSL 31 -struct nd_opt_dnssl { /* DNSSL option RFC 6106 */ - uint8_t nd_opt_dnssl_type; - uint8_t nd_opt_dnssl_len; - uint16_t nd_opt_dnssl_reserved; - uint32_t nd_opt_dnssl_lifetime; +#define ND_OPT_DNSSL 31 +struct nd_opt_dnssl { /* DNSSL option RFC 6106 */ + uint8_t nd_opt_dnssl_type; + uint8_t nd_opt_dnssl_len; + uint16_t nd_opt_dnssl_reserved; + uint32_t nd_opt_dnssl_lifetime; /* followed by list of DNS servers */ }; __CTASSERT(sizeof(struct nd_opt_dnssl) == 8); #endif /* Impossible options, so we can easily add extras */ -#define _ND_OPT_PREFIX_ADDR 255 + 1 +#define _ND_OPT_PREFIX_ADDR 255 + 1 /* Minimal IPv6 MTU */ #ifndef IPV6_MMTU @@ -118,35 +119,37 @@ __CTASSERT(sizeof(struct nd_opt_dnssl) == 8); #endif #ifndef ND_RA_FLAG_RTPREF_HIGH -#define ND_RA_FLAG_RTPREF_MASK 0x18 -#define ND_RA_FLAG_RTPREF_HIGH 0x08 -#define ND_RA_FLAG_RTPREF_MEDIUM 0x00 -#define ND_RA_FLAG_RTPREF_LOW 0x18 -#define ND_RA_FLAG_RTPREF_RSV 0x10 +#define ND_RA_FLAG_RTPREF_MASK 0x18 +#define ND_RA_FLAG_RTPREF_HIGH 0x08 +#define ND_RA_FLAG_RTPREF_MEDIUM 0x00 +#define ND_RA_FLAG_RTPREF_LOW 0x18 +#define ND_RA_FLAG_RTPREF_RSV 0x10 #endif -#define EXPIRED_MAX 5 /* Remember 5 expired routers to avoid - logspam. */ +#define EXPIRED_MAX \ + 5 /* Remember 5 expired routers to avoid \ + logspam. */ -#define MIN_RANDOM_FACTOR 500 /* millisecs */ -#define MAX_RANDOM_FACTOR 1500 /* millisecs */ -#define MIN_RANDOM_FACTOR_U MIN_RANDOM_FACTOR * 1000 /* usecs */ -#define MAX_RANDOM_FACTOR_U MAX_RANDOM_FACTOR * 1000 /* usecs */ +#define MIN_RANDOM_FACTOR 500 /* millisecs */ +#define MAX_RANDOM_FACTOR 1500 /* millisecs */ +#define MIN_RANDOM_FACTOR_U MIN_RANDOM_FACTOR * 1000 /* usecs */ +#define MAX_RANDOM_FACTOR_U MAX_RANDOM_FACTOR * 1000 /* usecs */ #if BYTE_ORDER == BIG_ENDIAN -#define IPV6_ADDR_INT32_ONE 1 -#define IPV6_ADDR_INT16_MLL 0xff02 +#define IPV6_ADDR_INT32_ONE 1 +#define IPV6_ADDR_INT16_MLL 0xff02 #elif BYTE_ORDER == LITTLE_ENDIAN -#define IPV6_ADDR_INT32_ONE 0x01000000 -#define IPV6_ADDR_INT16_MLL 0x02ff +#define IPV6_ADDR_INT32_ONE 0x01000000 +#define IPV6_ADDR_INT16_MLL 0x02ff #endif /* Debugging Neighbor Solicitations is a lot of spam, so disable it */ -//#define DEBUG_NS +// #define DEBUG_NS // static void ipv6nd_handledata(void *, unsigned short); -static struct routeinfo *routeinfo_findalloc(struct ra *, const struct in6_addr *, uint8_t); +static struct routeinfo *routeinfo_findalloc(struct ra *, + const struct in6_addr *, uint8_t); static void routeinfohead_free(struct routeinfohead *); /* @@ -170,7 +173,7 @@ static void routeinfohead_free(struct routeinfohead *); #define ICMP6_FILTER_SETPASS(type, filterp) \ ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31)))) #define ICMP6_FILTER_SETBLOCK(type, filterp) \ - ((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31)))) + ((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31)))) #define ICMP6_FILTER_SETPASSALL(filterp) \ memset(filterp, 0, sizeof(struct icmp6_filter)); #define ICMP6_FILTER_SETBLOCKALL(filterp) \ @@ -186,8 +189,8 @@ static void routeinfohead_free(struct routeinfohead *); #endif /* Handy defines */ -#define ipv6nd_free_ra(ra) ipv6nd_freedrop_ra((ra), 0) -#define ipv6nd_drop_ra(ra) ipv6nd_freedrop_ra((ra), 1) +#define ipv6nd_free_ra(ra) ipv6nd_freedrop_ra((ra), 0) +#define ipv6nd_drop_ra(ra) ipv6nd_freedrop_ra((ra), 1) /* Clear these addrflags on receipt of a new RA before adding the new flags * dervived from the RA. */ @@ -195,16 +198,14 @@ static void routeinfohead_free(struct routeinfohead *); (IPV6_AF_ONLINK | IPV6_AF_AUTOCONF | IPV6_AF_ROUTER | IPV6_AF_STALE) void -ipv6nd_printoptions(const struct dhcpcd_ctx *ctx, - const struct dhcp_opt *opts, size_t opts_len) +ipv6nd_printoptions(const struct dhcpcd_ctx *ctx, const struct dhcp_opt *opts, + size_t opts_len) { size_t i, j; const struct dhcp_opt *opt, *opt2; int cols; - for (i = 0, opt = ctx->nd_opts; - i < ctx->nd_opts_len; i++, opt++) - { + for (i = 0, opt = ctx->nd_opts; i < ctx->nd_opts_len; i++, opt++) { for (j = 0, opt2 = opts; j < opts_len; j++, opt2++) if (opt2->option == opt->option) break; @@ -233,33 +234,33 @@ ipv6nd_open(bool recv) /* RFC4861 4.1 */ on = 255; - if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, - &on, sizeof(on)) == -1) + if (setsockopt(fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &on, + sizeof(on)) == -1) goto eexit; if (recv) { on = 1; - if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, - &on, sizeof(on)) == -1) + if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &on, + sizeof(on)) == -1) goto eexit; on = 1; - if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, - &on, sizeof(on)) == -1) + if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, + sizeof(on)) == -1) goto eexit; ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &filt); #ifdef SO_RERROR on = 1; - if (setsockopt(fd, SOL_SOCKET, SO_RERROR, - &on, sizeof(on)) == -1) + if (setsockopt(fd, SOL_SOCKET, SO_RERROR, &on, sizeof(on)) == + -1) goto eexit; #endif } - if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, - &filt, sizeof(filt)) == -1) + if (setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, sizeof(filt)) == + -1) goto eexit; return fd; @@ -275,8 +276,8 @@ ipv6nd_openif(struct interface *ifp) { int fd; struct ipv6_mreq mreq = { - .ipv6mr_multiaddr = IN6ADDR_LINKLOCAL_ALLNODES_INIT, - .ipv6mr_interface = ifp->index + .ipv6mr_multiaddr = IN6ADDR_LINKLOCAL_ALLNODES_INIT, + .ipv6mr_interface = ifp->index }; struct rs_state *state = RS_STATE(ifp); uint_t ifindex = ifp->index; @@ -288,23 +289,20 @@ ipv6nd_openif(struct interface *ifp) if (fd == -1) return -1; - if (setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, - &ifindex, sizeof(ifindex)) == -1) - { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_BOUND_IF, &ifindex, + sizeof(ifindex)) == -1) { close(fd); return -1; } - if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, - &mreq, sizeof(mreq)) == -1) - { + if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &mreq, + sizeof(mreq)) == -1) { close(fd); return -1; } - if (eloop_event_add(ifp->ctx->eloop, fd, ELE_READ, - ipv6nd_handledata, ifp) == -1) - { + if (eloop_event_add(ifp->ctx->eloop, fd, ELE_READ, ipv6nd_handledata, + ifp) == -1) { close(fd); return -1; } @@ -330,9 +328,9 @@ ipv6nd_makersprobe(struct interface *ifp) return -1; rs = state->rs; rs->nd_rs_type = ND_ROUTER_SOLICIT; - //rs->nd_rs_code = 0; - //rs->nd_rs_cksum = 0; - //rs->nd_rs_reserved = 0; + // rs->nd_rs_code = 0; + // rs->nd_rs_cksum = 0; + // rs->nd_rs_reserved = 0; if (ifp->hwlen != 0) { struct nd_opt_hdr *nd; @@ -361,9 +359,12 @@ ipv6nd_sendrsprobe(void *arg) uint8_t buf[CMSG_SPACE(sizeof(struct in6_pktinfo))]; } cmsgbuf = { .buf = { 0 } }; struct msghdr msg = { - .msg_name = &dst, .msg_namelen = sizeof(dst), - .msg_iov = &iov, .msg_iovlen = 1, - .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf), + .msg_name = &dst, + .msg_namelen = sizeof(dst), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = sizeof(cmsgbuf.buf), }; struct cmsghdr *cm; struct in6_pktinfo pi = { .ipi6_ifindex = ifp->index }; @@ -416,7 +417,7 @@ ipv6nd_sendrsprobe(void *arg) return; } if (eloop_event_add(ctx->eloop, ctx->nd_fd, ELE_READ, - ipv6nd_handledata, ctx) == -1) + ipv6nd_handledata, ctx) == -1) logerr("%s: eloop_event_add", __func__); } s = ifp->ctx->nd_fd; @@ -474,7 +475,6 @@ ipv6nd_startexpire(struct interface *ifp) int ipv6nd_rtpref(uint8_t flags) { - switch (flags & ND_RA_FLAG_RTPREF_MASK) { case ND_RA_FLAG_RTPREF_HIGH: return RTPREF_HIGH; @@ -509,7 +509,8 @@ ipv6nd_sortrouters(struct dhcpcd_ctx *ctx) continue; if (!ra1->isreachable && ra2->isreachable) continue; - if (ipv6nd_rtpref(ra1->flags) <= ipv6nd_rtpref(ra2->flags)) + if (ipv6nd_rtpref(ra1->flags) <= + ipv6nd_rtpref(ra2->flags)) continue; /* All things being equal, prefer older routers. */ /* We don't need to check time, becase newer @@ -532,7 +533,7 @@ ipv6nd_applyra(struct interface *ifp) struct rs_state *state = RS_STATE(ifp); struct ra defra = { .iface = ifp, - .hoplimit = IPV6_DEFHLIM , + .hoplimit = IPV6_DEFHLIM, .reachable = REACHABLE_TIME, .retrans = RETRANS_TIMER, }; @@ -645,8 +646,7 @@ ipv6nd_findaddr(struct dhcpcd_ctx *ctx, const struct in6_addr *addr, } static struct ipv6_addr * -ipv6nd_rapfindprefix(struct ra *rap, - const struct in6_addr *pfx, uint8_t pfxlen) +ipv6nd_rapfindprefix(struct ra *rap, const struct in6_addr *pfx, uint8_t pfxlen) { struct ipv6_addr *ia; @@ -661,8 +661,8 @@ ipv6nd_rapfindprefix(struct ra *rap, } struct ipv6_addr * -ipv6nd_iffindprefix(struct interface *ifp, - const struct in6_addr *pfx, uint8_t pfxlen) +ipv6nd_iffindprefix(struct interface *ifp, const struct in6_addr *pfx, + uint8_t pfxlen) { struct ra *rap; struct ipv6_addr *ia; @@ -698,7 +698,6 @@ ipv6nd_removefreedrop_ra(struct ra *rap, int remove_ra, int drop_ra) static void ipv6nd_freedrop_ra(struct ra *rap, int drop) { - ipv6nd_removefreedrop_ra(rap, 1, drop); } @@ -759,16 +758,15 @@ ipv6nd_scriptrun(struct ra *rap) /* If all addresses have completed DAD run the script */ TAILQ_FOREACH(ap, &rap->addrs, next) { if ((ap->flags & (IPV6_AF_AUTOCONF | IPV6_AF_ADDED)) == - (IPV6_AF_AUTOCONF | IPV6_AF_ADDED)) - { + (IPV6_AF_AUTOCONF | IPV6_AF_ADDED)) { hasaddress = 1; if (!(ap->flags & IPV6_AF_DADCOMPLETED) && ipv6_iffindaddr(ap->iface, &ap->addr, - IN6_IFF_TENTATIVE)) + IN6_IFF_TENTATIVE)) ap->flags |= IPV6_AF_DADCOMPLETED; if ((ap->flags & IPV6_AF_DADCOMPLETED) == 0) { logdebugx("%s: waiting for Router Advertisement" - " DAD to complete", + " DAD to complete", rap->iface->name); return; } @@ -783,8 +781,9 @@ ipv6nd_scriptrun(struct ra *rap) } script_runreason(rap->iface, "ROUTERADVERT"); - if (hasdns && (hasaddress || - !(rap->flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER)))) + if (hasdns && + (hasaddress || + !(rap->flags & (ND_RA_FLAG_MANAGED | ND_RA_FLAG_OTHER)))) dhcpcd_daemonise(rap->iface->ctx); #if 0 else if (options & DHCPCD_DAEMONISE && @@ -845,26 +844,23 @@ ipv6nd_dadcallback(void *arg) * a different address is generated. */ /* XXX Cache DAD counter per prefix/id/ssid? */ if (ifp->options->options & DHCPCD_SLAACPRIVATE && - IA6_CANAUTOCONF(ia)) - { + IA6_CANAUTOCONF(ia)) { unsigned int delay; if (ia->dadcounter >= IDGEN_RETRIES) { logerrx("%s: unable to obtain a" - " stable private address", + " stable private address", ifp->name); goto try_script; } - loginfox("%s: deleting address %s", - ifp->name, ia->saddr); + loginfox("%s: deleting address %s", ifp->name, + ia->saddr); if (if_address6(RTM_DELADDR, ia) == -1 && errno != EADDRNOTAVAIL && errno != ENXIO) logerr(__func__); dadcounter = ia->dadcounter; - if (ipv6_makestableprivate(&ia->addr, - &ia->prefix, ia->prefix_len, - ifp, &dadcounter) == -1) - { + if (ipv6_makestableprivate(&ia->addr, &ia->prefix, + ia->prefix_len, ifp, &dadcounter) == -1) { logerr("ipv6_makestableprivate"); return; } @@ -873,9 +869,7 @@ ipv6nd_dadcallback(void *arg) ia->flags |= IPV6_AF_NEW; p = inet_ntop(AF_INET6, &ia->addr, buf, sizeof(buf)); if (p) - snprintf(ia->saddr, - sizeof(ia->saddr), - "%s/%d", + snprintf(ia->saddr, sizeof(ia->saddr), "%s/%d", p, ia->prefix_len); else ia->saddr[0] = '\0'; @@ -896,8 +890,8 @@ ipv6nd_dadcallback(void *arg) TAILQ_FOREACH(rapap, &rap->addrs, next) { if (rapap->flags & IPV6_AF_AUTOCONF && rapap->flags & IPV6_AF_ADDED && - (rapap->flags & IPV6_AF_DADCOMPLETED) == 0) - { + (rapap->flags & IPV6_AF_DADCOMPLETED) == + 0) { wascompleted = 0; break; } @@ -907,7 +901,7 @@ ipv6nd_dadcallback(void *arg) if (wascompleted && found) { logdebugx("%s: Router Advertisement DAD " - "completed", + "completed", rap->iface->name); ipv6nd_scriptrun(rap); } @@ -923,9 +917,7 @@ ipv6nd_findmarkstale(struct ra *rap, struct ipv6_addr *ia, bool mark) struct ipv6_addr *ia2; TAILQ_FOREACH(rap2, ctx->ra_routers, next) { - if (rap2 == rap || - rap2->iface != rap->iface || - rap2->expired) + if (rap2 == rap || rap2->iface != rap->iface || rap2->expired) continue; TAILQ_FOREACH(ia2, &rap2->addrs, next) { if (!IN6_ARE_ADDR_EQUAL(&ia->prefix, &ia2->prefix)) @@ -949,16 +941,15 @@ enum DH6S { static int dhcp6_start(__unused struct interface *ifp, __unused enum DH6S init_state) { - errno = ENOTSUP; return -1; } #endif static void -ipv6nd_handlera(struct dhcpcd_ctx *ctx, - const struct sockaddr_in6 *from, const char *sfrom, - struct interface *ifp, struct icmp6_hdr *icp, size_t len, int hoplimit) +ipv6nd_handlera(struct dhcpcd_ctx *ctx, const struct sockaddr_in6 *from, + const char *sfrom, struct interface *ifp, struct icmp6_hdr *icp, size_t len, + int hoplimit) { size_t i, olen; struct nd_router_advert *nd_ra; @@ -982,11 +973,11 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, bool new_ia; #endif -#define FREE_RAP(rap) \ - if (new_rap) \ - ipv6nd_removefreedrop_ra(rap, 0, 0); \ - else \ - ipv6nd_free_ra(rap); \ +#define FREE_RAP(rap) \ + if (new_rap) \ + ipv6nd_removefreedrop_ra(rap, 0, 0); \ + else \ + ipv6nd_free_ra(rap); if (ifp == NULL || RS_STATE(ifp) == NULL) { #ifdef DEBUG_RS @@ -1020,15 +1011,14 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, /* We could receive a RA before we sent a RS*/ if (ipv6_linklocal(ifp) == NULL) { #ifdef DEBUG_RS - logdebugx("%s: received RA from %s (no link-local)", - ifp->name, sfrom); + logdebugx("%s: received RA from %s (no link-local)", ifp->name, + sfrom); #endif return; } if (ipv6_iffindaddr(ifp, &from->sin6_addr, IN6_IFF_TENTATIVE)) { - logdebugx("%s: ignoring RA from ourself %s", - ifp->name, sfrom); + logdebugx("%s: ignoring RA from ourself %s", ifp->name, sfrom); return; } @@ -1055,9 +1045,9 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, /* We don't want to spam the log with the fact we got an RA every * 30 seconds or so, so only spam the log if it's different. */ - if (rap == NULL || (rap->data_len != len || - memcmp(rap->data, (unsigned char *)icp, rap->data_len) != 0)) - { + if (rap == NULL || + (rap->data_len != len || + memcmp(rap->data, (unsigned char *)icp, rap->data_len) != 0)) { if (rap) { free(rap->data); rap->data_len = 0; @@ -1098,10 +1088,10 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, * much needless log spam. */ if (rap->willexpire) new_data = true; - loglevel = new_rap || rap->willexpire || !rap->isreachable ? - LOG_INFO : LOG_DEBUG; - logmessage(loglevel, "%s: Router Advertisement from %s", - ifp->name, rap->sfrom); + loglevel = new_rap || rap->willexpire || !rap->isreachable ? LOG_INFO : + LOG_DEBUG; + logmessage(loglevel, "%s: Router Advertisement from %s", ifp->name, + rap->sfrom); clock_gettime(CLOCK_MONOTONIC, &rap->acquired); rap->flags = nd_ra->nd_ra_flags_reserved; @@ -1150,18 +1140,14 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, return; } if (olen > len) { - logerrx("%s: option length exceeds message", - ifp->name); + logerrx("%s: option length exceeds message", ifp->name); break; } if (has_option_mask(ifp->options->rejectmasknd, - ndo.nd_opt_type)) - { - for (i = 0, dho = ctx->nd_opts; - i < ctx->nd_opts_len; - i++, dho++) - { + ndo.nd_opt_type)) { + for (i = 0, dho = ctx->nd_opts; i < ctx->nd_opts_len; + i++, dho++) { if (dho->option == ndo.nd_opt_type) break; } @@ -1179,8 +1165,7 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, continue; switch (ndo.nd_opt_type) { - case ND_OPT_PREFIX_INFORMATION: - { + case ND_OPT_PREFIX_INFORMATION: { uint32_t vltime, pltime; loglevel = new_data ? LOG_ERR : LOG_DEBUG; @@ -1200,8 +1185,7 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, memcpy(&pi_prefix, &pi.nd_opt_pi_prefix, sizeof(pi_prefix)); if (IN6_IS_ADDR_MULTICAST(&pi_prefix) || - IN6_IS_ADDR_LINKLOCAL(&pi_prefix)) - { + IN6_IS_ADDR_LINKLOCAL(&pi_prefix)) { logmessage(loglevel, "%s: invalid prefix in RA", ifp->name); continue; @@ -1222,16 +1206,16 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, flags |= IPV6_AF_ONLINK; if (pi.nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_AUTO && rap->iface->options->options & - DHCPCD_IPV6RA_AUTOCONF) + DHCPCD_IPV6RA_AUTOCONF) flags |= IPV6_AF_AUTOCONF; if (pi.nd_opt_pi_flags_reserved & ND_OPT_PI_FLAG_ROUTER) flags |= IPV6_AF_ROUTER; - ia = ipv6nd_rapfindprefix(rap, - &pi_prefix, pi.nd_opt_pi_prefix_len); + ia = ipv6nd_rapfindprefix(rap, &pi_prefix, + pi.nd_opt_pi_prefix_len); if (ia == NULL) { - ia = ipv6_newaddr(rap->iface, - &pi_prefix, pi.nd_opt_pi_prefix_len, flags); + ia = ipv6_newaddr(rap->iface, &pi_prefix, + pi.nd_opt_pi_prefix_len, flags); if (ia == NULL) break; @@ -1253,7 +1237,7 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, * create a new one */ if (flags & IPV6_AF_AUTOCONF && ipv6_iffindaddr(ifp, &ia->addr, - IN6_IFF_NOTUSEABLE) && + IN6_IFF_NOTUSEABLE) && ipv6_settemptime(ia, 0)) new_ia = false; else @@ -1278,9 +1262,9 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, if (ia->prefix_vltime) { uint32_t elapsed; - elapsed = (uint32_t)eloop_timespec_diff( - &rap->acquired, &ia->acquired, - NULL); + elapsed = (uint32_t) + eloop_timespec_diff(&rap->acquired, + &ia->acquired, NULL); rmtime = ia->prefix_vltime - elapsed; if (rmtime > ia->prefix_vltime) rmtime = 0; @@ -1319,15 +1303,14 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, /* RFC4941 Section 3.3.3 */ if (ia->flags & IPV6_AF_AUTOCONF && ia->iface->options->options & DHCPCD_SLAACTEMP && - IA6_CANAUTOCONF(ia)) - { + IA6_CANAUTOCONF(ia)) { if (!new_ia) { if (ipv6_settemptime(ia, 1) == NULL) new_ia = true; } if (new_ia && ia->prefix_pltime) { if (ipv6_createtempaddr(ia, - &ia->acquired) == NULL) + &ia->acquired) == NULL) logerr("ipv6_createtempaddr"); } } @@ -1337,7 +1320,8 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, case ND_OPT_MTU: if (len < sizeof(mtu)) { - logmessage(loglevel, "%s: short MTU option", ifp->name); + logmessage(loglevel, "%s: short MTU option", + ifp->name); break; } memcpy(&mtu, p, sizeof(mtu)); @@ -1351,7 +1335,8 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, if (ifmtu == -1) logerr("if_getmtu"); else if (mtu.nd_opt_mtu_mtu > (uint32_t)ifmtu) { - logmessage(loglevel, "%s: advertised MTU %d" + logmessage(loglevel, + "%s: advertised MTU %d" " is greater than link MTU %d", ifp->name, mtu.nd_opt_mtu_mtu, ifmtu); rap->mtu = (uint32_t)ifmtu; @@ -1360,7 +1345,8 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, break; case ND_OPT_RDNSS: if (len < sizeof(rdnss)) { - logmessage(loglevel, "%s: short RDNSS option", ifp->name); + logmessage(loglevel, "%s: short RDNSS option", + ifp->name); break; } memcpy(&rdnss, p, sizeof(rdnss)); @@ -1370,34 +1356,43 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, break; case ND_OPT_RI: if (ndo.nd_opt_len > 3) { - logmessage(loglevel, "%s: invalid route info option", - ifp->name); + logmessage(loglevel, + "%s: invalid route info option", ifp->name); break; } memset(&ri, 0, sizeof(ri)); - memcpy(&ri, p, olen); /* may be smaller than sizeof(ri), pad with zero */ - if(ri.nd_opt_ri_prefixlen > 128) { - logmessage(loglevel, "%s: invalid route info prefix length", + memcpy(&ri, p, olen); /* may be smaller than sizeof(ri), + pad with zero */ + if (ri.nd_opt_ri_prefixlen > 128) { + logmessage(loglevel, + "%s: invalid route info prefix length", ifp->name); break; } /* rfc4191 3.1 - RI for ::/0 applies to default route */ - if(ri.nd_opt_ri_prefixlen == 0) { + if (ri.nd_opt_ri_prefixlen == 0) { rap->lifetime = ntohl(ri.nd_opt_ri_lifetime); - /* Update preference leaving other flags intact */ - rap->flags = ((rap->flags & (~ (unsigned int)ND_RA_FLAG_RTPREF_MASK)) - | ri.nd_opt_ri_flags_reserved) & 0xff; + /* Update preference leaving other flags intact + */ + rap->flags = + ((rap->flags & + (~(unsigned int) + ND_RA_FLAG_RTPREF_MASK)) | + ri.nd_opt_ri_flags_reserved) & + 0xff; break; } - /* Update existing route info instead of rebuilding all routes so that - previously announced but now absent routes can stay alive. To kill a - route early, an RI with lifetime=0 needs to be received (rfc4191 3.1)*/ - rinfo = routeinfo_findalloc(rap, &ri.nd_opt_ri_prefix, ri.nd_opt_ri_prefixlen); - if(rinfo == NULL) { + /* Update existing route info instead of rebuilding all + routes so that previously announced but now absent + routes can stay alive. To kill a route early, an RI + with lifetime=0 needs to be received (rfc4191 3.1)*/ + rinfo = routeinfo_findalloc(rap, &ri.nd_opt_ri_prefix, + ri.nd_opt_ri_prefixlen); + if (rinfo == NULL) { logerr(__func__); break; } @@ -1413,13 +1408,8 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, } } - for (i = 0, dho = ctx->nd_opts; - i < ctx->nd_opts_len; - i++, dho++) - { - if (has_option_mask(ifp->options->requiremasknd, - dho->option)) - { + for (i = 0, dho = ctx->nd_opts; i < ctx->nd_opts_len; i++, dho++) { + if (has_option_mask(ifp->options->requiremasknd, dho->option)) { logwarnx("%s: reject RA (no option %s) from %s", ifp->name, dho->var, rap->sfrom); FREE_RAP(rap); @@ -1480,9 +1470,9 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, goto nodhcp6; /* Only log a DHCPv6 start error if compiled in or debugging is enabled. */ #ifdef DHCP6 -#define LOG_DHCP6 logerr +#define LOG_DHCP6 logerr #else -#define LOG_DHCP6 logdebug +#define LOG_DHCP6 logdebug #endif if (rap->flags & ND_RA_FLAG_MANAGED) { if (new_data && dhcp6_start(ifp, DH6S_REQUEST) == -1) @@ -1495,7 +1485,7 @@ ipv6nd_handlera(struct dhcpcd_ctx *ctx, if (new_data) logdebugx("%s: No DHCPv6 instruction in RA", ifp->name); #endif -nodhcp6: + nodhcp6: if (ifp->ctx->options & DHCPCD_TEST) { eloop_exit(ifp->ctx->eloop, EXIT_SUCCESS); return; @@ -1514,9 +1504,8 @@ ipv6nd_hasralifetime(const struct interface *ifp, bool lifetime) if (ifp->ctx->ra_routers) { TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) - if (rap->iface == ifp && - !rap->expired && - (!lifetime ||rap->lifetime)) + if (rap->iface == ifp && !rap->expired && + (!lifetime || rap->lifetime)) return true; } return false; @@ -1529,10 +1518,10 @@ ipv6nd_hasradhcp(const struct interface *ifp, bool managed) if (ifp->ctx->ra_routers) { TAILQ_FOREACH(rap, ifp->ctx->ra_routers, next) { - if (rap->iface == ifp && - !rap->expired && !rap->willexpire && + if (rap->iface == ifp && !rap->expired && + !rap->willexpire && ((managed && rap->flags & ND_RA_FLAG_MANAGED) || - (!managed && rap->flags & ND_RA_FLAG_OTHER))) + (!managed && rap->flags & ND_RA_FLAG_OTHER))) return true; } } @@ -1540,9 +1529,8 @@ ipv6nd_hasradhcp(const struct interface *ifp, bool managed) } static const uint8_t * -ipv6nd_getoption(struct dhcpcd_ctx *ctx, - size_t *os, unsigned int *code, size_t *len, - const uint8_t *od, size_t ol, struct dhcp_opt **oopt) +ipv6nd_getoption(struct dhcpcd_ctx *ctx, size_t *os, unsigned int *code, + size_t *len, const uint8_t *od, size_t ol, struct dhcp_opt **oopt) { struct nd_opt_hdr ndo; size_t i; @@ -1564,9 +1552,7 @@ ipv6nd_getoption(struct dhcpcd_ctx *ctx, *code = ndo.nd_opt_type; } - for (i = 0, opt = ctx->nd_opts; - i < ctx->nd_opts_len; i++, opt++) - { + for (i = 0, opt = ctx->nd_opts; i < ctx->nd_opts_len; i++, opt++) { if (opt->option == *code) { *oopt = opt; break; @@ -1601,32 +1587,34 @@ ipv6nd_env(FILE *fp, const struct interface *ifp) if (efprintf(fp, "%s_from=%s", ndprefix, rap->sfrom) == -1) return -1; if (efprintf(fp, "%s_acquired=%lld", ndprefix, - (long long)rap->acquired.tv_sec) == -1) + (long long)rap->acquired.tv_sec) == -1) return -1; if (efprintf(fp, "%s_now=%lld", ndprefix, - (long long)now.tv_sec) == -1) + (long long)now.tv_sec) == -1) return -1; - if (efprintf(fp, "%s_hoplimit=%u", ndprefix, rap->hoplimit) == -1) + if (efprintf(fp, "%s_hoplimit=%u", ndprefix, rap->hoplimit) == + -1) return -1; pref = ipv6nd_rtpref(rap->flags); if (efprintf(fp, "%s_flags=%s%s%s%s%s", ndprefix, - rap->flags & ND_RA_FLAG_MANAGED ? "M" : "", - rap->flags & ND_RA_FLAG_OTHER ? "O" : "", - rap->flags & ND_RA_FLAG_HOME_AGENT ? "H" : "", - pref == RTPREF_HIGH ? "h" : pref == RTPREF_LOW ? "l" : "", - rap->flags & ND_RA_FLAG_PROXY ? "P" : "") == -1) + rap->flags & ND_RA_FLAG_MANAGED ? "M" : "", + rap->flags & ND_RA_FLAG_OTHER ? "O" : "", + rap->flags & ND_RA_FLAG_HOME_AGENT ? "H" : "", + pref == RTPREF_HIGH ? "h" : + pref == RTPREF_LOW ? "l" : + "", + rap->flags & ND_RA_FLAG_PROXY ? "P" : "") == -1) return -1; - if (efprintf(fp, "%s_lifetime=%u", ndprefix, rap->lifetime) == -1) + if (efprintf(fp, "%s_lifetime=%u", ndprefix, rap->lifetime) == + -1) return -1; /* Zero our indexes */ for (j = 0, opt = rap->iface->ctx->nd_opts; - j < rap->iface->ctx->nd_opts_len; - j++, opt++) + j < rap->iface->ctx->nd_opts_len; j++, opt++) dhcp_zero_index(opt); for (j = 0, opt = rap->iface->options->nd_override; - j < rap->iface->options->nd_override_len; - j++, opt++) + j < rap->iface->options->nd_override_len; j++, opt++) dhcp_zero_index(opt); /* Unlike DHCP, ND6 options *may* occur more than once. @@ -1634,17 +1622,15 @@ ipv6nd_env(FILE *fp, const struct interface *ifp) * unlike DHCP. */ len = rap->data_len - sizeof(struct nd_router_advert); for (p = rap->data + sizeof(struct nd_router_advert); - len >= sizeof(ndo); - p += olen, len -= olen) - { + len >= sizeof(ndo); p += olen, len -= olen) { memcpy(&ndo, p, sizeof(ndo)); olen = (size_t)(ndo.nd_opt_len * 8); if (olen > len) { - errno = EINVAL; + errno = EINVAL; break; } if (has_option_mask(rap->iface->options->nomasknd, - ndo.nd_opt_type)) + ndo.nd_opt_type)) continue; for (j = 0, opt = rap->iface->options->nd_override; j < rap->iface->options->nd_override_len; @@ -1662,9 +1648,8 @@ ipv6nd_env(FILE *fp, const struct interface *ifp) } if (opt == NULL) continue; - dhcp_envoption(rap->iface->ctx, fp, - ndprefix, rap->iface->name, - opt, ipv6nd_getoption, + dhcp_envoption(rap->iface->ctx, fp, ndprefix, + rap->iface->name, opt, ipv6nd_getoption, p + sizeof(ndo), olen - sizeof(ndo)); } @@ -1679,8 +1664,8 @@ ipv6nd_env(FILE *fp, const struct interface *ifp) !(ia->flags & IPV6_AF_ADDED) || ia->prefix_vltime == 0) continue; - if (efprintf(fp, "%s_addr%zu=%s", - ndprefix, ++j, ia->saddr) == -1) + if (efprintf(fp, "%s_addr%zu=%s", ndprefix, ++j, + ia->saddr) == -1) return -1; } } @@ -1732,10 +1717,11 @@ ipv6nd_expirera(void *arg) if (rap->iface != ifp || rap->expired) continue; valid = false; - /* lifetime may be set to infinite by rfc4191 route information */ + /* lifetime may be set to infinite by rfc4191 route information + */ if (rap->lifetime) { - ltime = lifetime_left(rap->lifetime, - &rap->acquired, &now); + ltime = lifetime_left(rap->lifetime, &rap->acquired, + &now); if (ltime == 0 || rap->doexpire) { if (!rap->expired) { logwarnx("%s: %s: router expired", @@ -1756,23 +1742,25 @@ ipv6nd_expirera(void *arg) TAILQ_FOREACH(ia, &rap->addrs, next) { if (ia->prefix_vltime == 0) continue; - ltime = lifetime_left(ia->prefix_vltime, - &ia->acquired, &now); + ltime = lifetime_left(ia->prefix_vltime, &ia->acquired, + &now); if (ltime == 0 || rap->doexpire) { if (ia->flags & IPV6_AF_ADDED) { logwarnx("%s: expired %s %s", ia->iface->name, ia->flags & IPV6_AF_AUTOCONF ? - "address" : "prefix", + "address" : + "prefix", ia->saddr); - if (if_address6(RTM_DELADDR, ia)== -1 && + if (if_address6(RTM_DELADDR, ia) == + -1 && errno != EADDRNOTAVAIL && errno != ENXIO) logerr(__func__); } ia->prefix_vltime = ia->prefix_pltime = 0; - ia->flags &= - ~(IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED); + ia->flags &= ~( + IPV6_AF_ADDED | IPV6_AF_DADCOMPLETED); expired = true; } else { valid = true; @@ -1783,8 +1771,8 @@ ipv6nd_expirera(void *arg) /* Expire route information */ TAILQ_FOREACH_SAFE(rinfo, &rap->rinfos, next, rinfob) { - ltime = lifetime_left(rinfo->lifetime, - &rinfo->acquired, &now); + ltime = lifetime_left(rinfo->lifetime, &rinfo->acquired, + &now); if (ltime == 0 || rap->doexpire) { logwarnx("%s: expired route %s", rap->iface->name, rinfo->sprefix); @@ -1793,26 +1781,25 @@ ipv6nd_expirera(void *arg) } /* Work out expiry for ND options */ - elapsed = (uint32_t)eloop_timespec_diff(&now, - &rap->acquired, NULL); + elapsed = (uint32_t)eloop_timespec_diff(&now, &rap->acquired, + NULL); len = rap->data_len - sizeof(struct nd_router_advert); for (p = rap->data + sizeof(struct nd_router_advert); - len >= sizeof(ndo); - p += olen, len -= olen) - { + len >= sizeof(ndo); p += olen, len -= olen) { memcpy(&ndo, p, sizeof(ndo)); olen = (size_t)(ndo.nd_opt_len * 8); if (olen > len) { - errno = EINVAL; + errno = EINVAL; break; } if (has_option_mask(rap->iface->options->nomasknd, - ndo.nd_opt_type)) + ndo.nd_opt_type)) continue; switch (ndo.nd_opt_type) { - /* Prefix info is already checked in the above loop. */ + /* Prefix info is already checked in the above + * loop. */ #if 0 case ND_OPT_PREFIX_INFORMATION: if (len < sizeof(pi)) @@ -1870,8 +1857,8 @@ ipv6nd_expirera(void *arg) } if (next != 0) - eloop_timeout_add_sec(ifp->ctx->eloop, - next, ipv6nd_expirera, ifp); + eloop_timeout_add_sec(ifp->ctx->eloop, next, ipv6nd_expirera, + ifp); if (expired) { logwarnx("%s: part of a Router Advertisement expired", ifp->name); @@ -1936,16 +1923,16 @@ ipv6nd_recvmsg(struct dhcpcd_ctx *ctx, struct msghdr *msg) icp = (struct icmp6_hdr *)msg->msg_iov[0].iov_base; if (icp->icmp6_code == 0) { - switch(icp->icmp6_type) { - case ND_ROUTER_ADVERT: - ipv6nd_handlera(ctx, from, sfrom, - ifp, icp, (size_t)len, hoplimit); - return; + switch (icp->icmp6_type) { + case ND_ROUTER_ADVERT: + ipv6nd_handlera(ctx, from, sfrom, ifp, icp, (size_t)len, + hoplimit); + return; } } - logerrx("invalid IPv6 type %d or code %d from %s", - icp->icmp6_type, icp->icmp6_code, sfrom); + logerrx("invalid IPv6 type %d or code %d from %s", icp->icmp6_type, + icp->icmp6_code, sfrom); } static void @@ -1959,7 +1946,8 @@ ipv6nd_handledata(void *arg, unsigned short events) uint8_t buf[64 * 1024]; /* Maximum ICMPv6 size */ } iovbuf; struct iovec iov = { - .iov_base = iovbuf.buf, .iov_len = sizeof(iovbuf.buf), + .iov_base = iovbuf.buf, + .iov_len = sizeof(iovbuf.buf), }; union { struct cmsghdr hdr; @@ -1967,9 +1955,12 @@ ipv6nd_handledata(void *arg, unsigned short events) CMSG_SPACE(sizeof(int))]; } cmsgbuf = { .buf = { 0 } }; struct msghdr msg = { - .msg_name = &from, .msg_namelen = sizeof(from), - .msg_iov = &iov, .msg_iovlen = 1, - .msg_control = cmsgbuf.buf, .msg_controllen = sizeof(cmsgbuf.buf), + .msg_name = &from, + .msg_namelen = sizeof(from), + .msg_iov = &iov, + .msg_iovlen = 1, + .msg_control = cmsgbuf.buf, + .msg_controllen = sizeof(cmsgbuf.buf), }; ssize_t len; @@ -2052,10 +2043,9 @@ ipv6nd_startrs1(void *arg) void ipv6nd_startrs(struct interface *ifp) { - if (ipv6_linklocal(ifp) == NULL) { logdebugx("%s: " - "delaying IPv6 Router Solicitation for LL address", + "delaying IPv6 Router Solicitation for LL address", ifp->name); ipv6_addlinklocalcallback(ifp, ipv6nd_startrs1, ifp); } else @@ -2065,13 +2055,14 @@ ipv6nd_startrs(struct interface *ifp) void ipv6nd_abort(struct interface *ifp) { - eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_startrs1, ifp); eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_startrs2, ifp); eloop_timeout_delete(ifp->ctx->eloop, ipv6nd_sendrsprobe, ifp); } -static struct routeinfo *routeinfo_findalloc(struct ra *rap, const struct in6_addr *prefix, uint8_t prefix_len) +static struct routeinfo * +routeinfo_findalloc(struct ra *rap, const struct in6_addr *prefix, + uint8_t prefix_len) { struct routeinfo *ri; char buf[INET6_ADDRSTRLEN]; @@ -2091,17 +2082,16 @@ static struct routeinfo *routeinfo_findalloc(struct ra *rap, const struct in6_ad ri->prefix_len = prefix_len; p = inet_ntop(AF_INET6, prefix, buf, sizeof(buf)); if (p) - snprintf(ri->sprefix, - sizeof(ri->sprefix), - "%s/%d", - p, prefix_len); + snprintf(ri->sprefix, sizeof(ri->sprefix), "%s/%d", p, + prefix_len); else ri->sprefix[0] = '\0'; TAILQ_INSERT_TAIL(&rap->rinfos, ri, next); return ri; } -static void routeinfohead_free(struct routeinfohead *head) +static void +routeinfohead_free(struct routeinfohead *head) { struct routeinfo *ri; diff --git a/src/ipv6nd.h b/src/ipv6nd.h index acf3a691..691ddeef 100644 --- a/src/ipv6nd.h +++ b/src/ipv6nd.h @@ -50,7 +50,6 @@ struct routeinfo { TAILQ_HEAD(routeinfohead, routeinfo); - struct ra { TAILQ_ENTRY(ra) next; struct interface *iface; @@ -86,34 +85,33 @@ struct rs_state { #endif }; -#define RS_STATE(a) ((struct rs_state *)(ifp)->if_data[IF_DATA_IPV6ND]) -#define RS_CSTATE(a) ((const struct rs_state *)(ifp)->if_data[IF_DATA_IPV6ND]) -#define RS_STATE_RUNNING(a) (ipv6nd_hasra((a)) && ipv6nd_dadcompleted((a))) +#define RS_STATE(a) ((struct rs_state *)(ifp)->if_data[IF_DATA_IPV6ND]) +#define RS_CSTATE(a) ((const struct rs_state *)(ifp)->if_data[IF_DATA_IPV6ND]) +#define RS_STATE_RUNNING(a) (ipv6nd_hasra((a)) && ipv6nd_dadcompleted((a))) #ifndef MAX_RTR_SOLICITATION_DELAY -#define MAX_RTR_SOLICITATION_DELAY 1 /* seconds */ -#define MAX_UNICAST_SOLICIT 3 /* 3 transmissions */ -#define RTR_SOLICITATION_INTERVAL 4 /* seconds */ -#define MAX_RTR_SOLICITATIONS 3 /* times */ -#define MAX_NEIGHBOR_ADVERTISEMENT 3 /* 3 transmissions */ +#define MAX_RTR_SOLICITATION_DELAY 1 /* seconds */ +#define MAX_UNICAST_SOLICIT 3 /* 3 transmissions */ +#define RTR_SOLICITATION_INTERVAL 4 /* seconds */ +#define MAX_RTR_SOLICITATIONS 3 /* times */ +#define MAX_NEIGHBOR_ADVERTISEMENT 3 /* 3 transmissions */ #ifndef IPV6_DEFHLIM -#define IPV6_DEFHLIM 64 +#define IPV6_DEFHLIM 64 #endif #endif /* On carrier up, expire known routers after RTR_CARRIER_EXPIRE seconds. */ -#define RTR_CARRIER_EXPIRE \ - (MAX_RTR_SOLICITATION_DELAY + \ - (MAX_RTR_SOLICITATIONS + 1) * \ - RTR_SOLICITATION_INTERVAL) +#define RTR_CARRIER_EXPIRE \ + (MAX_RTR_SOLICITATION_DELAY + \ + (MAX_RTR_SOLICITATIONS + 1) * RTR_SOLICITATION_INTERVAL) -#define MAX_REACHABLE_TIME 3600000 /* milliseconds */ -#define REACHABLE_TIME 30000 /* milliseconds */ -#define RETRANS_TIMER 1000 /* milliseconds */ -#define DELAY_FIRST_PROBE_TIME 5 /* seconds */ +#define MAX_REACHABLE_TIME 3600000 /* milliseconds */ +#define REACHABLE_TIME 30000 /* milliseconds */ +#define RETRANS_TIMER 1000 /* milliseconds */ +#define DELAY_FIRST_PROBE_TIME 5 /* seconds */ -#define MIN_EXTENDED_VLTIME 7200 /* seconds */ +#define MIN_EXTENDED_VLTIME 7200 /* seconds */ int ipv6nd_open(bool); #ifdef __sun @@ -121,20 +119,20 @@ int ipv6nd_openif(struct interface *); #endif void ipv6nd_recvmsg(struct dhcpcd_ctx *, struct msghdr *); int ipv6nd_rtpref(uint8_t); -void ipv6nd_printoptions(const struct dhcpcd_ctx *, - const struct dhcp_opt *, size_t); +void ipv6nd_printoptions(const struct dhcpcd_ctx *, const struct dhcp_opt *, + size_t); void ipv6nd_startrs(struct interface *); ssize_t ipv6nd_env(FILE *, const struct interface *); const struct ipv6_addr *ipv6nd_iffindaddr(const struct interface *ifp, const struct in6_addr *addr, unsigned int flags); -struct ipv6_addr *ipv6nd_findaddr(struct dhcpcd_ctx *, - const struct in6_addr *, unsigned int); +struct ipv6_addr *ipv6nd_findaddr(struct dhcpcd_ctx *, const struct in6_addr *, + unsigned int); struct ipv6_addr *ipv6nd_iffindprefix(struct interface *, const struct in6_addr *, uint8_t); ssize_t ipv6nd_free(struct interface *); void ipv6nd_expirera(void *arg); bool ipv6nd_hasralifetime(const struct interface *, bool); -#define ipv6nd_hasra(i) ipv6nd_hasralifetime((i), false) +#define ipv6nd_hasra(i) ipv6nd_hasralifetime((i), false) bool ipv6nd_hasradhcp(const struct interface *, bool); void ipv6nd_handleifa(int, struct ipv6_addr *, pid_t); int ipv6nd_dadcompleted(const struct interface *); diff --git a/src/logerr.c b/src/logerr.c index e04493d3..0b04d78f 100644 --- a/src/logerr.c +++ b/src/logerr.c @@ -30,8 +30,8 @@ #include #include -#include #include +#include #include #include #include @@ -41,8 +41,8 @@ #include "logerr.h" -#ifndef LOGERR_SYSLOG_FACILITY -#define LOGERR_SYSLOG_FACILITY LOG_DAEMON +#ifndef LOGERR_SYSLOG_FACILITY +#define LOGERR_SYSLOG_FACILITY LOG_DAEMON #endif #ifdef SMALL @@ -50,19 +50,19 @@ #endif /* syslog protocol is 1k message max, RFC 3164 section 4.1 */ -#define LOGERR_SYSLOGBUF 1024 + sizeof(int) + sizeof(pid_t) +#define LOGERR_SYSLOGBUF 1024 + sizeof(int) + sizeof(pid_t) -#define UNUSED(a) (void)(a) +#define UNUSED(a) (void)(a) struct logctx { - char log_buf[BUFSIZ]; - unsigned int log_opts; - int log_fd; - pid_t log_pid; + char log_buf[BUFSIZ]; + unsigned int log_opts; + int log_fd; + pid_t log_pid; #ifndef SMALL - FILE *log_file; + FILE *log_file; #ifdef LOGERR_TAG - const char *log_tag; + const char *log_tag; #endif #endif }; @@ -125,8 +125,8 @@ logprintdate(FILE *stream) } #endif -__printflike(3, 0) static int -vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) +__printflike(3, 0) static int vlogprintf_r(struct logctx *ctx, FILE *stream, + const char *fmt, va_list args) { int len = 0, e; va_list a; @@ -137,8 +137,7 @@ vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) #endif if ((stream == stderr && ctx->log_opts & LOGERR_ERR_DATE) || - (stream != stderr && ctx->log_opts & LOGERR_LOG_DATE)) - { + (stream != stderr && ctx->log_opts & LOGERR_LOG_DATE)) { if ((e = logprintdate(stream)) == -1) return -1; len += e; @@ -210,8 +209,8 @@ vlogprintf_r(struct logctx *ctx, FILE *stream, const char *fmt, va_list args) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wmissing-format-attribute" #endif -__printflike(2, 0) static int -vlogmessage(int pri, const char *fmt, va_list args) +__printflike(2, 0) static int vlogmessage(int pri, const char *fmt, + va_list args) { struct logctx *ctx = &_logctx; int len = 0; @@ -241,8 +240,8 @@ vlogmessage(int pri, const char *fmt, va_list args) if (ctx->log_opts & LOGERR_ERR && (pri <= LOG_ERR || - (!(ctx->log_opts & LOGERR_QUIET) && pri <= LOG_INFO) || - (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG))) + (!(ctx->log_opts & LOGERR_QUIET) && pri <= LOG_INFO) || + (ctx->log_opts & LOGERR_DEBUG && pri <= LOG_DEBUG))) len = vlogprintf_r(ctx, stderr, fmt, args); #ifndef SMALL @@ -260,8 +259,7 @@ vlogmessage(int pri, const char *fmt, va_list args) #pragma GCC diagnostic pop #endif -__printflike(2, 3) void -logmessage(int pri, const char *fmt, ...) +__printflike(2, 3) void logmessage(int pri, const char *fmt, ...) { va_list args; @@ -270,8 +268,8 @@ logmessage(int pri, const char *fmt, ...) va_end(args); } -__printflike(2, 0) static void -vlogerrmessage(int pri, const char *fmt, va_list args) +__printflike(2, 0) static void vlogerrmessage(int pri, const char *fmt, + va_list args) { int _errno = errno; char buf[1024]; @@ -281,8 +279,7 @@ vlogerrmessage(int pri, const char *fmt, va_list args) errno = _errno; } -__printflike(2, 3) void -logerrmessage(int pri, const char *fmt, ...) +__printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...) { va_list args; @@ -405,12 +402,10 @@ logreadfd(int fd) struct iovec iov[] = { { .iov_base = &pri, .iov_len = sizeof(pri) }, { .iov_base = &pid, .iov_len = sizeof(pid) }, - { .iov_base = buf, .iov_len = sizeof(buf) }, - }; - struct msghdr msg = { - .msg_iov = iov, - .msg_iovlen = sizeof(iov) / sizeof(iov[0]) + { .iov_base = buf, .iov_len = sizeof(buf) }, }; + struct msghdr msg = { .msg_iov = iov, + .msg_iovlen = sizeof(iov) / sizeof(iov[0]) }; ssize_t len; len = recvmsg(fd, &msg, MSG_WAITALL); diff --git a/src/logerr.h b/src/logerr.h index b6d708ca..2fe52c5e 100644 --- a/src/logerr.h +++ b/src/logerr.h @@ -33,9 +33,9 @@ #ifndef __printflike #if __GNUC__ > 2 || defined(__INTEL_COMPILER) -#define __printflike(a, b) __attribute__((format(printf, a, b))) +#define __printflike(a, b) __attribute__((format(printf, a, b))) #else -#define __printflike(a, b) +#define __printflike(a, b) #endif #endif /* !__printflike */ @@ -48,7 +48,7 @@ __printflike(1, 2) void log_warn(const char *, ...); __printflike(1, 2) void log_warnx(const char *, ...); __printflike(1, 2) void log_err(const char *, ...); __printflike(1, 2) void log_errx(const char *, ...); -#define LOGERROR logerr("%s: %d", __FILE__, __LINE__) +#define LOGERROR logerr("%s: %d", __FILE__, __LINE__) __printflike(2, 3) void logmessage(int pri, const char *fmt, ...); __printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...); @@ -67,14 +67,14 @@ __printflike(2, 3) void logerrmessage(int pri, const char *fmt, ...); * The solution is to put fmt into __VA_ARGS__. * It's not pretty but it's 100% portable. */ -#define logdebug(...) log_debug(__VA_ARGS__) -#define logdebugx(...) log_debugx(__VA_ARGS__) -#define loginfo(...) log_info(__VA_ARGS__) -#define loginfox(...) log_infox(__VA_ARGS__) -#define logwarn(...) log_warn(__VA_ARGS__) -#define logwarnx(...) log_warnx(__VA_ARGS__) -#define logerr(...) log_err(__VA_ARGS__) -#define logerrx(...) log_errx(__VA_ARGS__) +#define logdebug(...) log_debug(__VA_ARGS__) +#define logdebugx(...) log_debugx(__VA_ARGS__) +#define loginfo(...) log_info(__VA_ARGS__) +#define loginfox(...) log_infox(__VA_ARGS__) +#define logwarn(...) log_warn(__VA_ARGS__) +#define logwarnx(...) log_warnx(__VA_ARGS__) +#define logerr(...) log_err(__VA_ARGS__) +#define logerrx(...) log_errx(__VA_ARGS__) /* For logging in a chroot using SOCK_SEQPACKET */ int loggetfd(void); @@ -83,21 +83,21 @@ ssize_t logreadfd(int); unsigned int loggetopts(void); void logsetopts(unsigned int); -#define LOGERR_DEBUG (1U << 6) -#define LOGERR_QUIET (1U << 7) -#define LOGERR_LOG (1U << 11) -#define LOGERR_LOG_DATE (1U << 12) -#define LOGERR_LOG_HOST (1U << 13) -#define LOGERR_LOG_TAG (1U << 14) -#define LOGERR_LOG_PID (1U << 15) -#define LOGERR_ERR (1U << 21) -#define LOGERR_ERR_DATE (1U << 22) -#define LOGERR_ERR_HOST (1U << 23) -#define LOGERR_ERR_TAG (1U << 24) -#define LOGERR_ERR_PID (1U << 25) +#define LOGERR_DEBUG (1U << 6) +#define LOGERR_QUIET (1U << 7) +#define LOGERR_LOG (1U << 11) +#define LOGERR_LOG_DATE (1U << 12) +#define LOGERR_LOG_HOST (1U << 13) +#define LOGERR_LOG_TAG (1U << 14) +#define LOGERR_LOG_PID (1U << 15) +#define LOGERR_ERR (1U << 21) +#define LOGERR_ERR_DATE (1U << 22) +#define LOGERR_ERR_HOST (1U << 23) +#define LOGERR_ERR_TAG (1U << 24) +#define LOGERR_ERR_PID (1U << 25) /* To build tag support or not. */ -//#define LOGERR_TAG +// #define LOGERR_TAG #if defined(LOGERR_TAG) void logsettag(const char *); #endif diff --git a/src/privsep-bpf.c b/src/privsep-bpf.c index e2b170bd..cb227def 100644 --- a/src/privsep-bpf.c +++ b/src/privsep-bpf.c @@ -26,8 +26,8 @@ * SUCH DAMAGE. */ -#include #include +#include /* Need these headers just for if_ether on some OS. */ #ifndef __NetBSD__ @@ -38,8 +38,8 @@ #include #include -#include #include +#include #include #include #include @@ -112,9 +112,9 @@ ps_bpf_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) logerrx("%s: IN cmd %x, psp %p", __func__, psm->ps_cmd, psp); #endif - switch(psm->ps_cmd) { + switch (psm->ps_cmd) { #ifdef ARP - case PS_BPF_ARP: /* FALLTHROUGH */ + case PS_BPF_ARP: /* FALLTHROUGH */ #endif case PS_BPF_BOOTP: break; @@ -131,8 +131,8 @@ ps_bpf_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) return -1; } - return bpf_send(psp->psp_bpf, psp->psp_proto, - iov->iov_base, iov->iov_len); + return bpf_send(psp->psp_bpf, psp->psp_proto, iov->iov_base, + iov->iov_len); } static void @@ -140,8 +140,8 @@ ps_bpf_recvmsg(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, - ps_bpf_recvmsgcb, arg) == -1) + if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, ps_bpf_recvmsgcb, + arg) == -1) logerr(__func__); } @@ -166,13 +166,13 @@ ps_bpf_start_bpf(struct ps_process *psp) logdebugx("pid %d bpf_fd=%d", getpid(), psp->psp_bpf->bpf_fd); #endif if (psp->psp_bpf == NULL) - logerr("%s: bpf_open",__func__); + logerr("%s: bpf_open", __func__); #ifdef PRIVSEP_RIGHTS else if (ps_rights_limit_fd(psp->psp_bpf->bpf_fd) == -1) logerr("%s: ps_rights_limit_fd", __func__); #endif else if (eloop_event_add(ctx->eloop, psp->psp_bpf->bpf_fd, ELE_READ, - ps_bpf_recvbpf, psp) == -1) + ps_bpf_recvbpf, psp) == -1) logerr("%s: eloop_event_add", __func__); else { psp->psp_work_fd = psp->psp_bpf->bpf_fd; @@ -203,7 +203,7 @@ ps_bpf_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) switch (cmd) { #ifdef ARP - case PS_BPF_ARP: /* FALLTHROUGH */ + case PS_BPF_ARP: /* FALLTHROUGH */ #endif case PS_BPF_BOOTP: break; @@ -255,11 +255,11 @@ ps_bpf_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) else addr = inet_ntoa(*ia); snprintf(psp->psp_name, sizeof(psp->psp_name), "BPF %s%s%s", - psp->psp_protostr, - addr != NULL ? " " : "", addr != NULL ? addr : ""); + psp->psp_protostr, addr != NULL ? " " : "", + addr != NULL ? addr : ""); - start = ps_startprocess(psp, ps_bpf_recvmsg, NULL, - ps_bpf_start_bpf, PSF_DROPPRIVS); + start = ps_startprocess(psp, ps_bpf_recvmsg, NULL, ps_bpf_start_bpf, + PSF_DROPPRIVS); switch (start) { case -1: @@ -269,16 +269,16 @@ ps_bpf_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) ps_entersandbox("stdio", NULL); break; default: - logdebugx("%s: spawned %s on PID %d", - psp->psp_ifname, psp->psp_name, psp->psp_pid); + logdebugx("%s: spawned %s on PID %d", psp->psp_ifname, + psp->psp_name, psp->psp_pid); break; } return start; } ssize_t -ps_bpf_dispatch(struct dhcpcd_ctx *ctx, - struct ps_msghdr *psm, struct msghdr *msg) +ps_bpf_dispatch(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, + struct msghdr *msg) { struct iovec *iov = msg->msg_iov; struct interface *ifp; @@ -319,8 +319,8 @@ ps_bpf_dispatch(struct dhcpcd_ctx *ctx, } static ssize_t -ps_bpf_send(const struct interface *ifp, const struct in_addr *ia, - uint16_t cmd, const void *data, size_t len) +ps_bpf_send(const struct interface *ifp, const struct in_addr *ia, uint16_t cmd, + const void *data, size_t len) { struct dhcpcd_ctx *ctx = ifp->ctx; struct ps_msghdr psm = { @@ -341,16 +341,13 @@ ps_bpf_send(const struct interface *ifp, const struct in_addr *ia, ssize_t ps_bpf_openarp(const struct interface *ifp, const struct in_addr *ia) { - assert(ia != NULL); - return ps_bpf_send(ifp, ia, PS_BPF_ARP | PS_START, - ifp, sizeof(*ifp)); + return ps_bpf_send(ifp, ia, PS_BPF_ARP | PS_START, ifp, sizeof(*ifp)); } ssize_t ps_bpf_closearp(const struct interface *ifp, const struct in_addr *ia) { - return ps_bpf_send(ifp, ia, PS_BPF_ARP | PS_STOP, NULL, 0); } @@ -358,7 +355,6 @@ ssize_t ps_bpf_sendarp(const struct interface *ifp, const struct in_addr *ia, const void *data, size_t len) { - assert(ia != NULL); return ps_bpf_send(ifp, ia, PS_BPF_ARP, data, len); } @@ -367,21 +363,18 @@ ps_bpf_sendarp(const struct interface *ifp, const struct in_addr *ia, ssize_t ps_bpf_openbootp(const struct interface *ifp) { - - return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP | PS_START, - ifp, sizeof(*ifp)); + return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP | PS_START, ifp, + sizeof(*ifp)); } ssize_t ps_bpf_closebootp(const struct interface *ifp) { - return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP | PS_STOP, NULL, 0); } ssize_t ps_bpf_sendbootp(const struct interface *ifp, const void *data, size_t len) { - return ps_bpf_send(ifp, NULL, PS_BPF_BOOTP, data, len); } diff --git a/src/privsep-bpf.h b/src/privsep-bpf.h index 1539c3ae..45ad9afd 100644 --- a/src/privsep-bpf.h +++ b/src/privsep-bpf.h @@ -29,10 +29,9 @@ #ifndef PRIVSEP_BPF_H #define PRIVSEP_BPF_H -ssize_t ps_bpf_cmd(struct dhcpcd_ctx *, - struct ps_msghdr *, struct msghdr *); -ssize_t ps_bpf_dispatch(struct dhcpcd_ctx *, - struct ps_msghdr *, struct msghdr *); +ssize_t ps_bpf_cmd(struct dhcpcd_ctx *, struct ps_msghdr *, struct msghdr *); +ssize_t ps_bpf_dispatch(struct dhcpcd_ctx *, struct ps_msghdr *, + struct msghdr *); #ifdef ARP ssize_t ps_bpf_openarp(const struct interface *, const struct in_addr *); diff --git a/src/privsep-bsd.c b/src/privsep-bsd.c index ce649b7e..0b42021f 100644 --- a/src/privsep-bsd.c +++ b/src/privsep-bsd.c @@ -26,30 +26,31 @@ * SUCH DAMAGE. */ -#include #include +#include #include /* Need these for filtering the ioctls */ -#include #include -#include #include #include #include -#ifdef __NetBSD__ #include + +#include +#ifdef __NetBSD__ #include /* Needs netinet/if_ether.h */ +#include #elif defined(__DragonFly__) #include #else #include #endif #ifdef __DragonFly__ -# include +#include #else -# include -# include +#include +#include #endif #include @@ -63,14 +64,15 @@ #include "privsep.h" static ssize_t -ps_root_doioctldom(struct dhcpcd_ctx *ctx, int domain, unsigned long req, void *data, size_t len) +ps_root_doioctldom(struct dhcpcd_ctx *ctx, int domain, unsigned long req, + void *data, size_t len) { #if defined(INET6) || (defined(SIOCALIFADDR) && defined(IFLR_ACTIVE)) struct priv *priv = (struct priv *)ctx->priv; #endif int s; - switch(domain) { + switch (domain) { #ifdef INET case PF_INET: s = ctx->pf_inet_fd; @@ -92,39 +94,39 @@ ps_root_doioctldom(struct dhcpcd_ctx *ctx, int domain, unsigned long req, void * } /* Only allow these ioctls */ - switch(req) { + switch (req) { #ifdef SIOCGIFDATA - case SIOCGIFDATA: /* FALLTHROUGH */ + case SIOCGIFDATA: /* FALLTHROUGH */ #endif #ifdef SIOCG80211NWID - case SIOCG80211NWID: /* FALLTHROUGH */ + case SIOCG80211NWID: /* FALLTHROUGH */ #endif #ifdef SIOCGETVLAN - case SIOCGETVLAN: /* FALLTHROUGH */ + case SIOCGETVLAN: /* FALLTHROUGH */ #endif #ifdef SIOCIFAFATTACH - case SIOCIFAFATTACH: /* FALLTHROUGH */ + case SIOCIFAFATTACH: /* FALLTHROUGH */ #endif #ifdef SIOCSIFXFLAGS - case SIOCSIFXFLAGS: /* FALLTHROUGH */ + case SIOCSIFXFLAGS: /* FALLTHROUGH */ #endif #ifdef SIOCSIFINFO_FLAGS - case SIOCSIFINFO_FLAGS: /* FALLTHROUGH */ + case SIOCSIFINFO_FLAGS: /* FALLTHROUGH */ #endif #ifdef SIOCSRTRFLUSH_IN6 - case SIOCSRTRFLUSH_IN6: /* FALLTHROUGH */ + case SIOCSRTRFLUSH_IN6: /* FALLTHROUGH */ case SIOCSPFXFLUSH_IN6: /* FALLTHROUGH */ #endif #if defined(SIOCALIFADDR) && defined(IFLR_ACTIVE) - case SIOCALIFADDR: /* FALLTHROUGH */ - case SIOCDLIFADDR: /* FALLTHROUGH */ + case SIOCALIFADDR: /* FALLTHROUGH */ + case SIOCDLIFADDR: /* FALLTHROUGH */ #else - case SIOCSIFLLADDR: /* FALLTHROUGH */ + case SIOCSIFLLADDR: /* FALLTHROUGH */ #endif #ifdef SIOCSIFINFO_IN6 - case SIOCSIFINFO_IN6: /* FALLTHROUGH */ + case SIOCSIFINFO_IN6: /* FALLTHROUGH */ #endif - case SIOCAIFADDR_IN6: /* FALLTHROUGH */ + case SIOCAIFADDR_IN6: /* FALLTHROUGH */ case SIOCDIFADDR_IN6: break; default: @@ -138,14 +140,13 @@ ps_root_doioctldom(struct dhcpcd_ctx *ctx, int domain, unsigned long req, void * static ssize_t ps_root_doroute(struct dhcpcd_ctx *ctx, void *data, size_t len) { - return write(ctx->link_fd, data, len); } #if defined(HAVE_CAPSICUM) || defined(HAVE_PLEDGE) static ssize_t -ps_root_doindirectioctl(struct dhcpcd_ctx *ctx, - unsigned long req, void *data, size_t len) +ps_root_doindirectioctl(struct dhcpcd_ctx *ctx, unsigned long req, void *data, + size_t len) { char *p = data; struct ifreq ifr = { .ifr_flags = 0 }; @@ -182,7 +183,6 @@ ps_root_doindirectioctl(struct dhcpcd_ctx *ctx, static ssize_t ps_root_doifignoregroup(struct dhcpcd_ctx *ctx, void *data, size_t len) { - if (len == 0 || ((const char *)data)[len - 1] != '\0') { errno = EINVAL; return -1; @@ -194,8 +194,8 @@ ps_root_doifignoregroup(struct dhcpcd_ctx *ctx, void *data, size_t len) #ifdef HAVE_CAPSICUM static ssize_t -ps_root_dosysctl(unsigned long flags, - void *data, size_t len, void **rdata, size_t *rlen) +ps_root_dosysctl(unsigned long flags, void *data, size_t len, void **rdata, + size_t *rlen) { char *p = data, *e = p + len; int name[10]; @@ -273,10 +273,12 @@ ps_root_os(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg, switch (psm->ps_cmd) { case PS_IOCTLLINK: - err = ps_root_doioctldom(ctx, PF_LINK, psm->ps_flags, data, len); + err = ps_root_doioctldom(ctx, PF_LINK, psm->ps_flags, data, + len); break; case PS_IOCTL6: - err = ps_root_doioctldom(ctx, PF_INET6, psm->ps_flags, data, len); + err = ps_root_doioctldom(ctx, PF_INET6, psm->ps_flags, data, + len); break; case PS_ROUTE: return ps_root_doroute(ctx, data, len); @@ -294,7 +296,7 @@ ps_root_os(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg, *free_rdata = true; return ps_root_dosysctl(psm->ps_flags, data, len, rdata, rlen); #else - UNUSED(free_rdata); + UNUSED(free_rdata); #endif default: errno = ENOTSUP; @@ -312,33 +314,28 @@ static ssize_t ps_root_ioctldom(struct dhcpcd_ctx *ctx, uint16_t domain, unsigned long request, void *data, size_t len) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), domain, - request, data, len) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), domain, request, data, len) == -1) return -1; return ps_root_readerror(ctx, data, len); } ssize_t -ps_root_ioctllink(struct dhcpcd_ctx *ctx, unsigned long request, - void *data, size_t len) +ps_root_ioctllink(struct dhcpcd_ctx *ctx, unsigned long request, void *data, + size_t len) { - return ps_root_ioctldom(ctx, PS_IOCTLLINK, request, data, len); } ssize_t -ps_root_ioctl6(struct dhcpcd_ctx *ctx, unsigned long request, - void *data, size_t len) +ps_root_ioctl6(struct dhcpcd_ctx *ctx, unsigned long request, void *data, + size_t len) { - return ps_root_ioctldom(ctx, PS_IOCTL6, request, data, len); } ssize_t ps_root_route(struct dhcpcd_ctx *ctx, void *data, size_t len) { - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_ROUTE, 0, data, len) == -1) return -1; return ps_root_readerror(ctx, data, len); @@ -351,27 +348,25 @@ ps_root_indirectioctl(struct dhcpcd_ctx *ctx, unsigned long request, { size_t ifnamelen = strlen(ifname + 1); - struct iovec iov[] = { - { - .iov_base = &ifnamelen, - .iov_len = sizeof(ifnamelen), - }, + struct iovec iov[] = { { + .iov_base = &ifnamelen, + .iov_len = sizeof(ifnamelen), + }, { - .iov_base = UNCONST(ifname), - .iov_len = ifnamelen, + .iov_base = UNCONST(ifname), + .iov_len = ifnamelen, }, { - .iov_base = data, - .iov_len = len, - } - }; + .iov_base = data, + .iov_len = len, + } }; struct msghdr msg = { .msg_iov = iov, .msg_iovlen = __arraycount(iov), }; - if (ps_sendcmdmsg(ctx, PS_ROOT_FD(ctx), PS_IOCTLINDIRECT, - request, &msg) == -1) + if (ps_sendcmdmsg(ctx, PS_ROOT_FD(ctx), PS_IOCTLINDIRECT, request, + &msg) == -1) return -1; return ps_root_readerror(ctx, data, len); } @@ -381,9 +376,8 @@ ps_root_indirectioctl(struct dhcpcd_ctx *ctx, unsigned long request, ssize_t ps_root_ifignoregroup(struct dhcpcd_ctx *ctx, const char *ifname) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_IFIGNOREGRP, 0, - ifname, strlen(ifname) + 1) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_IFIGNOREGRP, 0, ifname, + strlen(ifname) + 1) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -391,18 +385,16 @@ ps_root_ifignoregroup(struct dhcpcd_ctx *ctx, const char *ifname) #ifdef HAVE_CAPSICUM ssize_t -ps_root_sysctl(struct dhcpcd_ctx *ctx, - const int *name, unsigned int namelen, +ps_root_sysctl(struct dhcpcd_ctx *ctx, const int *name, unsigned int namelen, void *oldp, size_t *oldlenp, const void *newp, size_t newlen) { char buf[PS_BUFLEN], *p = buf; unsigned long flags = 0; size_t olen = (oldp && oldlenp) ? *oldlenp : 0, nolen; - if (sizeof(namelen) + (sizeof(*name) * namelen) + - sizeof(oldlenp) + - sizeof(newlen) + newlen > sizeof(buf)) - { + if (sizeof(namelen) + (sizeof(*name) * namelen) + sizeof(oldlenp) + + sizeof(newlen) + newlen > + sizeof(buf)) { errno = ENOBUFS; return -1; } @@ -424,8 +416,8 @@ ps_root_sysctl(struct dhcpcd_ctx *ctx, p += newlen; } - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_SYSCTL, - flags, buf, (size_t)(p - buf)) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_SYSCTL, flags, buf, + (size_t)(p - buf)) == -1) return -1; if (ps_root_readerror(ctx, buf, sizeof(buf)) == -1) diff --git a/src/privsep-control.c b/src/privsep-control.c index b9ea7f9c..a92bebe6 100644 --- a/src/privsep-control.c +++ b/src/privsep-control.c @@ -30,8 +30,8 @@ #include #include -#include "dhcpcd.h" #include "control.h" +#include "dhcpcd.h" #include "eloop.h" #include "logerr.h" #include "privsep.h" @@ -48,15 +48,13 @@ ps_ctl_startcb(struct ps_process *psp) setproctitle("[control proxy]"); af = AF_UNSPEC; } else { - setproctitle("[control proxy] %s%s%s", - ctx->ifv[0], + setproctitle("[control proxy] %s%s%s", ctx->ifv[0], ctx->options & DHCPCD_IPV4 ? " [ip4]" : "", ctx->options & DHCPCD_IPV6 ? " [ip6]" : ""); - if ((ctx->options & - (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV4) + if ((ctx->options & (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV4) af = AF_INET; - else if ((ctx->options & - (DHCPCD_IPV4 | DHCPCD_IPV6)) == DHCPCD_IPV6) + else if ((ctx->options & (DHCPCD_IPV4 | DHCPCD_IPV6)) == + DHCPCD_IPV6) af = AF_INET6; else af = AF_UNSPEC; @@ -78,25 +76,21 @@ ps_ctl_recvmsg(void *arg, unsigned short events) ssize_t ps_ctl_handleargs(struct fd_list *fd, char *data, size_t len) { - /* Make any change here in dhcpcd.c as well. */ - if (strncmp(data, "--version", - MIN(strlen("--version"), len)) == 0) { - return control_queue(fd, UNCONST(VERSION), - strlen(VERSION) + 1); + if (strncmp(data, "--version", MIN(strlen("--version"), len)) == 0) { + return control_queue(fd, UNCONST(VERSION), strlen(VERSION) + 1); } else if (strncmp(data, "--getconfigfile", - MIN(strlen("--getconfigfile"), len)) == 0) { + MIN(strlen("--getconfigfile"), len)) == 0) { return control_queue(fd, UNCONST(fd->ctx->cffile), strlen(fd->ctx->cffile) + 1); - } else if (strncmp(data, "--listen", - MIN(strlen("--listen"), len)) == 0) { + } else if (strncmp(data, "--listen", MIN(strlen("--listen"), len)) == + 0) { fd->flags |= FD_LISTEN; return 0; } if (fd->ctx->ps_control_client != NULL && - fd->ctx->ps_control_client != fd) - { + fd->ctx->ps_control_client != fd) { logerrx("%s: cannot handle another client", __func__); return 0; } @@ -150,8 +144,8 @@ ps_ctl_dodispatch(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, - ps_ctl_dispatch, psp->psp_ctx) == -1) + if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, ps_ctl_dispatch, + psp->psp_ctx) == -1) logerr(__func__); } @@ -177,8 +171,8 @@ ps_ctl_recv(void *arg, unsigned short events) __func__, ctx->ps_ctl->psp_work_fd, len); else { errno = 0; - if (control_queue(ctx->ps_control_client, - buf, (size_t)len) == -1) + if (control_queue(ctx->ps_control_client, buf, + (size_t)len) == -1) logerr("%s: control_queue", __func__); } } @@ -206,7 +200,7 @@ ps_ctl_listen(void *arg, unsigned short events) TAILQ_FOREACH(fd, &ctx->control_fds, next) { if (!(fd->flags & FD_LISTEN)) continue; - if (control_queue(fd, buf, (size_t)len)== -1) + if (control_queue(fd, buf, (size_t)len) == -1) logerr("%s: control_queue", __func__); } } @@ -244,8 +238,8 @@ ps_ctl_start(struct dhcpcd_ctx *ctx) psp->psp_work_fd = work_fd[0]; close(work_fd[1]); close(listen_fd[1]); - ctx->ps_control = control_new(ctx, - listen_fd[0], FD_SENDLEN | FD_LISTEN); + ctx->ps_control = control_new(ctx, listen_fd[0], + FD_SENDLEN | FD_LISTEN); if (ctx->ps_control == NULL) return -1; return pid; @@ -255,15 +249,15 @@ ps_ctl_start(struct dhcpcd_ctx *ctx) close(listen_fd[0]); psp->psp_work_fd = work_fd[1]; - if (eloop_event_add(ctx->eloop, psp->psp_work_fd, ELE_READ, - ps_ctl_recv, ctx) == -1) + if (eloop_event_add(ctx->eloop, psp->psp_work_fd, ELE_READ, ps_ctl_recv, + ctx) == -1) return -1; ctx->ps_control = control_new(ctx, listen_fd[1], 0); if (ctx->ps_control == NULL) return -1; if (eloop_event_add(ctx->eloop, ctx->ps_control->fd, ELE_READ, - ps_ctl_listen, ctx) == -1) + ps_ctl_listen, ctx) == -1) return -1; ps_entersandbox("stdio inet", NULL); @@ -273,7 +267,6 @@ ps_ctl_start(struct dhcpcd_ctx *ctx) int ps_ctl_stop(struct dhcpcd_ctx *ctx) { - return ps_stopprocess(ctx->ps_ctl); } @@ -286,8 +279,7 @@ ps_ctl_sendargs(struct fd_list *fd, void *data, size_t len) logerrx("%s: cannot deal with another client", __func__); ctx->ps_control_client = fd; return ps_sendcmd(ctx, ctx->ps_ctl->psp_fd, PS_CTL, - fd->flags & FD_UNPRIV ? PS_CTL_UNPRIV : PS_CTL_PRIV, - data, len); + fd->flags & FD_UNPRIV ? PS_CTL_UNPRIV : PS_CTL_PRIV, data, len); } ssize_t diff --git a/src/privsep-control.h b/src/privsep-control.h index f1a2bff1..19724805 100644 --- a/src/privsep-control.h +++ b/src/privsep-control.h @@ -30,7 +30,7 @@ #define PRIVSEP_CTL_H #define IN_PRIVSEP_CONTROLLER(ctx) \ - (IN_PRIVSEP((ctx)) && (ctx)->ps_control_pid == getpid()) + (IN_PRIVSEP((ctx)) && (ctx)->ps_control_pid == getpid()) pid_t ps_ctl_start(struct dhcpcd_ctx *); int ps_ctl_stop(struct dhcpcd_ctx *); diff --git a/src/privsep-inet.c b/src/privsep-inet.c index 5ca356a1..0d11be14 100644 --- a/src/privsep-inet.c +++ b/src/privsep-inet.c @@ -26,11 +26,11 @@ * SUCH DAMAGE. */ -#include #include +#include -#include #include +#include #include #include @@ -55,8 +55,8 @@ ps_inet_recvbootp(void *arg, unsigned short events) { struct dhcpcd_ctx *ctx = arg; - if (ps_recvmsg(ctx->udp_rfd, events, - PS_BOOTP, ctx->ps_inet->psp_fd) == -1) + if (ps_recvmsg(ctx->udp_rfd, events, PS_BOOTP, ctx->ps_inet->psp_fd) == + -1) logerr(__func__); } #endif @@ -70,14 +70,12 @@ ps_inet_recvra(void *arg, unsigned short events) struct rs_state *state = RS_STATE(ifp); struct dhcpcd_ctx *ctx = ifp->ctx; - if (ps_recvmsg(state->nd_fd, events, - PS_ND, ctx->ps_inet->psp_fd) == -1) + if (ps_recvmsg(state->nd_fd, events, PS_ND, ctx->ps_inet->psp_fd) == -1) logerr(__func__); #else struct dhcpcd_ctx *ctx = arg; - if (ps_recvmsg(ctx->nd_fd, events, - PS_ND, ctx->ps_inet->psp_fd) == -1) + if (ps_recvmsg(ctx->nd_fd, events, PS_ND, ctx->ps_inet->psp_fd) == -1) logerr(__func__); #endif } @@ -89,8 +87,8 @@ ps_inet_recvdhcp6(void *arg, unsigned short events) { struct dhcpcd_ctx *ctx = arg; - if (ps_recvmsg(ctx->dhcp6_rfd, events, - PS_DHCP6, ctx->ps_inet->psp_fd) == -1) + if (ps_recvmsg(ctx->dhcp6_rfd, events, PS_DHCP6, + ctx->ps_inet->psp_fd) == -1) logerr(__func__); } #endif @@ -98,7 +96,6 @@ ps_inet_recvdhcp6(void *arg, unsigned short events) bool ps_inet_canstart(const struct dhcpcd_ctx *ctx) { - #ifdef INET if ((ctx->options & (DHCPCD_IPV4 | DHCPCD_MANAGER)) == (DHCPCD_IPV4 | DHCPCD_MANAGER)) @@ -139,8 +136,7 @@ ps_inet_startcb(struct ps_process *psp) #ifdef INET if ((ctx->options & (DHCPCD_IPV4 | DHCPCD_MANAGER)) == - (DHCPCD_IPV4 | DHCPCD_MANAGER)) - { + (DHCPCD_IPV4 | DHCPCD_MANAGER)) { ctx->udp_rfd = dhcp_openudp(NULL); if (ctx->udp_rfd == -1) logerr("%s: dhcp_open", __func__); @@ -152,8 +148,7 @@ ps_inet_startcb(struct ps_process *psp) } #endif else if (eloop_event_add(ctx->eloop, ctx->udp_rfd, ELE_READ, - ps_inet_recvbootp, ctx) == -1) - { + ps_inet_recvbootp, ctx) == -1) { logerr("%s: eloop_event_add DHCP", __func__); close(ctx->udp_rfd); ctx->udp_rfd = -1; @@ -174,8 +169,7 @@ ps_inet_startcb(struct ps_process *psp) } #endif else if (eloop_event_add(ctx->eloop, ctx->nd_fd, ELE_READ, - ps_inet_recvra, ctx) == -1) - { + ps_inet_recvra, ctx) == -1) { logerr("%s: eloop_event_add RA", __func__); close(ctx->nd_fd); ctx->nd_fd = -1; @@ -185,8 +179,7 @@ ps_inet_startcb(struct ps_process *psp) #endif #ifdef DHCP6 if ((ctx->options & (DHCPCD_IPV6 | DHCPCD_MANAGER)) == - (DHCPCD_IPV6 | DHCPCD_MANAGER)) - { + (DHCPCD_IPV6 | DHCPCD_MANAGER)) { ctx->dhcp6_rfd = dhcp6_openudp(0, NULL); if (ctx->dhcp6_rfd == -1) logerr("%s: dhcp6_open", __func__); @@ -198,8 +191,7 @@ ps_inet_startcb(struct ps_process *psp) } #endif else if (eloop_event_add(ctx->eloop, ctx->dhcp6_rfd, ELE_READ, - ps_inet_recvdhcp6, ctx) == -1) - { + ps_inet_recvdhcp6, ctx) == -1) { logerr("%s: eloop_event_add DHCP6", __func__); close(ctx->dhcp6_rfd); ctx->dhcp6_rfd = -1; @@ -249,7 +241,7 @@ ps_inet_validnd(struct msghdr *msg) } memcpy(&icmp6, iov->iov_base, sizeof(icmp6)); - switch(icmp6.icmp6_type) { + switch (icmp6.icmp6_type) { case ND_ROUTER_SOLICIT: case ND_NEIGHBOR_ADVERT: break; @@ -263,8 +255,8 @@ ps_inet_validnd(struct msghdr *msg) #endif static ssize_t -ps_inet_sendmsg(struct dhcpcd_ctx *ctx, - struct ps_msghdr *psm, struct msghdr *msg) +ps_inet_sendmsg(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, + struct msghdr *msg) { struct ps_process *psp; int s; @@ -292,7 +284,8 @@ ps_inet_sendmsg(struct dhcpcd_ctx *ctx, #endif #ifdef DHCP6 case PS_DHCP6: - if (!ps_inet_validudp(msg, DHCP6_CLIENT_PORT,DHCP6_SERVER_PORT)) + if (!ps_inet_validudp(msg, DHCP6_CLIENT_PORT, + DHCP6_SERVER_PORT)) return -1; s = ctx->dhcp6_wfd; break; @@ -349,8 +342,8 @@ ps_inet_dodispatch(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, - ps_inet_dispatch, psp->psp_ctx) == -1) + if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, ps_inet_dispatch, + psp->psp_ctx) == -1) logerr(__func__); } @@ -381,7 +374,6 @@ ps_inet_start(struct dhcpcd_ctx *ctx) int ps_inet_stop(struct dhcpcd_ctx *ctx) { - return ps_stopprocess(ctx->ps_inet); } @@ -391,8 +383,8 @@ ps_inet_recvinbootp(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvmsg(psp->psp_work_fd, events, - PS_BOOTP, psp->psp_ctx->ps_data_fd) == -1) + if (ps_recvmsg(psp->psp_work_fd, events, PS_BOOTP, + psp->psp_ctx->ps_data_fd) == -1) logerr(__func__); } @@ -419,8 +411,7 @@ ps_inet_listenin(struct ps_process *psp) #endif if (eloop_event_add(psp->psp_ctx->eloop, psp->psp_work_fd, ELE_READ, - ps_inet_recvinbootp, psp) == -1) - { + ps_inet_recvinbootp, psp) == -1) { logerr("%s: eloop_event_add DHCP", __func__); return -1; } @@ -434,15 +425,13 @@ ps_inet_recvin6nd(void *arg) { struct ps_process *psp = arg; - if (ps_recvmsg(psp->psp_work_fd, - PS_ND, psp->psp_ctx->ps_data_fd) == -1) + if (ps_recvmsg(psp->psp_work_fd, PS_ND, psp->psp_ctx->ps_data_fd) == -1) logerr(__func__); } static int ps_inet_listennd(struct ps_process *psp) { - setproctitle("[ND network proxy]"); psp->psp_work_fd = ipv6nd_open(&psp->psp_ifp); @@ -459,8 +448,7 @@ ps_inet_listennd(struct ps_process *psp) #endif if (eloop_event_add(psp->psp_ctx->eloop, psp->psp_work_fd, - ps_inet_recvin6nd, psp) == -1) - { + ps_inet_recvin6nd, psp) == -1) { logerr(__func__); return -1; } @@ -474,8 +462,8 @@ ps_inet_recvin6dhcp6(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvmsg(psp->psp_work_fd, events, - PS_DHCP6, psp->psp_ctx->ps_data_fd) == -1) + if (ps_recvmsg(psp->psp_work_fd, events, PS_DHCP6, + psp->psp_ctx->ps_data_fd) == -1) logerr(__func__); } @@ -502,8 +490,7 @@ ps_inet_listenin6(struct ps_process *psp) #endif if (eloop_event_add(psp->psp_ctx->eloop, psp->psp_work_fd, ELE_READ, - ps_inet_recvin6dhcp6, psp) == -1) - { + ps_inet_recvin6dhcp6, psp) == -1) { logerr("%s: eloop_event_add DHCP", __func__); return -1; } @@ -559,7 +546,6 @@ ps_inet_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) if (psp == NULL) return -1; - switch (cmd) { #ifdef INET case PS_BOOTP: @@ -590,11 +576,11 @@ ps_inet_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) return -1; } - snprintf(psp->psp_name, sizeof(psp->psp_name), - "%s proxy %s", psp->psp_protostr, + snprintf(psp->psp_name, sizeof(psp->psp_name), "%s proxy %s", + psp->psp_protostr, inet_ntop(psa->psa_family, ia, buf, sizeof(buf))); - start = ps_startprocess(psp, ps_inet_recvmsgpsp, NULL, - start_func, PSF_DROPPRIVS); + start = ps_startprocess(psp, ps_inet_recvmsgpsp, NULL, start_func, + PSF_DROPPRIVS); switch (start) { case -1: ps_freeprocess(psp); @@ -603,8 +589,8 @@ ps_inet_cmd(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) ps_entersandbox("stdio", NULL); break; default: - logdebugx("%s: spawned %s on PID %d", - psp->psp_ifname, psp->psp_name, psp->psp_pid); + logdebugx("%s: spawned %s on PID %d", psp->psp_ifname, + psp->psp_name, psp->psp_pid); break; } return start; @@ -632,14 +618,12 @@ ps_inet_in_docmd(struct ipv4_addr *ia, uint16_t cmd, const struct msghdr *msg) ssize_t ps_inet_openbootp(struct ipv4_addr *ia) { - return ps_inet_in_docmd(ia, PS_START | PS_BOOTP, NULL); } ssize_t ps_inet_closebootp(struct ipv4_addr *ia) { - return ps_inet_in_docmd(ia, PS_STOP | PS_BOOTP, NULL); } @@ -673,21 +657,18 @@ ps_inet_ifp_docmd(struct interface *ifp, uint16_t cmd, const struct msghdr *msg) ssize_t ps_inet_opennd(struct interface *ifp) { - return ps_inet_ifp_docmd(ifp, PS_ND | PS_START, NULL); } ssize_t ps_inet_closend(struct interface *ifp) { - return ps_inet_ifp_docmd(ifp, PS_ND | PS_STOP, NULL); } ssize_t ps_inet_sendnd(struct interface *ifp, const struct msghdr *msg) { - return ps_inet_ifp_docmd(ifp, PS_ND, msg); } #else @@ -721,14 +702,12 @@ ps_inet_in6_docmd(struct ipv6_addr *ia, uint16_t cmd, const struct msghdr *msg) ssize_t ps_inet_opendhcp6(struct ipv6_addr *ia) { - return ps_inet_in6_docmd(ia, PS_DHCP6 | PS_START, NULL); } ssize_t ps_inet_closedhcp6(struct ipv6_addr *ia) { - return ps_inet_in6_docmd(ia, PS_DHCP6 | PS_STOP, NULL); } diff --git a/src/privsep-linux.c b/src/privsep-linux.c index 43470a2d..7e4cb86f 100644 --- a/src/privsep-linux.c +++ b/src/privsep-linux.c @@ -31,20 +31,19 @@ #include #include +#include +#include #include #include #include #include #include - -#include -#include #include #include #include #include #include -#include /* For TCGETS */ +#include /* For TCGETS */ #include #include "common.h" @@ -58,7 +57,7 @@ * the failing syscall into the __NR_name define we need to use below. * DO NOT ENABLE THIS FOR PRODUCTION BUILDS! */ -//#define SECCOMP_FILTER_DEBUG +// #define SECCOMP_FILTER_DEBUG static ssize_t ps_root_dosendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct msghdr *msg) @@ -71,7 +70,7 @@ ps_root_dosendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct msghdr *msg) .iov_len = sizeof(buf), }; - switch(protocol) { + switch (protocol) { case NETLINK_GENERIC: s = priv->generic_fd; break; @@ -93,7 +92,6 @@ ssize_t ps_root_os(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg, __unused void **rdata, __unused size_t *rlen, __unused bool *free_data) { - switch (psm->ps_cmd) { case PS_ROUTE: return ps_root_dosendnetlink(ctx, (int)psm->ps_flags, msg); @@ -106,9 +104,8 @@ ps_root_os(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg, ssize_t ps_root_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct msghdr *msg) { - - if (ps_sendmsg(ctx, PS_ROOT_FD(ctx), PS_ROUTE, - (unsigned long)protocol, msg) == -1) + if (ps_sendmsg(ctx, PS_ROOT_FD(ctx), PS_ROUTE, (unsigned long)protocol, + msg) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -118,177 +115,174 @@ ps_root_sendnetlink(struct dhcpcd_ctx *ctx, int protocol, struct msghdr *msg) #else #if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_ARG_LO 0 -# define SECCOMP_ARG_HI sizeof(uint32_t) +#define SECCOMP_ARG_LO 0 +#define SECCOMP_ARG_HI sizeof(uint32_t) #elif (BYTE_ORDER == BIG_ENDIAN) -# define SECCOMP_ARG_LO sizeof(uint32_t) -# define SECCOMP_ARG_HI 0 +#define SECCOMP_ARG_LO sizeof(uint32_t) +#define SECCOMP_ARG_HI 0 #else -# error "Uknown endian" -#endif - -#define SECCOMP_ALLOW(_nr) \ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 1), \ - BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW) - -#define SECCOMP_ALLOW_ARG(_nr, _arg, _val) \ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6), \ - BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ - offsetof(struct seccomp_data, args[(_arg)]) + SECCOMP_ARG_LO), \ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \ - ((_val) & 0xffffffff), 0, 3), \ - BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ - offsetof(struct seccomp_data, args[(_arg)]) + SECCOMP_ARG_HI), \ - BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \ - (((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1), \ - BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), \ - BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ +#error "Uknown endian" +#endif + +#define SECCOMP_ALLOW(_nr) \ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 1), \ + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW) + +#define SECCOMP_ALLOW_ARG(_nr, _arg, _val) \ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, (_nr), 0, 6), \ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ + offsetof(struct seccomp_data, args[(_arg)]) + SECCOMP_ARG_LO), \ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, ((_val) & 0xffffffff), 0, 3), \ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ + offsetof(struct seccomp_data, args[(_arg)]) + SECCOMP_ARG_HI), \ + BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, \ + (((uint32_t)((uint64_t)(_val) >> 32)) & 0xffffffff), 0, 1), \ + BPF_STMT(BPF_RET + BPF_K, SECCOMP_RET_ALLOW), \ + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, \ offsetof(struct seccomp_data, nr)) #ifdef SECCOMP_FILTER_DEBUG -#define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP +#define SECCOMP_FILTER_FAIL SECCOMP_RET_TRAP #else -#define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL +#define SECCOMP_FILTER_FAIL SECCOMP_RET_KILL #endif /* I personally find this quite nutty. * Why can a system header not define a default for this? */ #if defined(__i386__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386 #elif defined(__x86_64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64 #elif defined(__arc__) -# if defined(__A7__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE -# endif -# elif defined(__HS__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE -# endif -# else -# error "Platform does not support seccomp filter yet" -# endif +#if defined(__A7__) +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACT +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCOMPACTBE +#endif +#elif defined(__HS__) +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV2BE +#endif +#else +#error "Platform does not support seccomp filter yet" +#endif #elif defined(__ARCV3__) -# if defined(__ARC64__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV3 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV3BE -# endif -# else -# error "Platform does not support seccomp filter yet" -# endif +#if defined(__ARC64__) +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV3 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARCV3BE +#endif +#else +#error "Platform does not support seccomp filter yet" +#endif #elif defined(__arm__) -# ifndef EM_ARM -# define EM_ARM 40 -# endif -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB -# endif +#ifndef EM_ARM +#define EM_ARM 40 +#endif +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARMEB +#endif #elif defined(__aarch64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_AARCH64 #elif defined(__alpha__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ALPHA #elif defined(__hppa__) -# if defined(__LP64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC -# endif +#if defined(__LP64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PARISC +#endif #elif defined(__ia64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_IA64 #elif defined(__loongarch__) -# if defined(__LP64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_LOONGARCH64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_LOONGARCH32 -# endif +#if defined(__LP64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_LOONGARCH64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_LOONGARCH32 +#endif #elif defined(__microblaze__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MICROBLAZE #elif defined(__m68k__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_M68K #elif defined(__mips__) -# if defined(__MIPSEL__) -# if defined(__LP64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL -# endif -# elif defined(__LP64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS -# endif +#if defined(__MIPSEL__) +#if defined(__LP64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPSEL +#endif +#elif defined(__LP64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_MIPS +#endif #elif defined(__nds32__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32 +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32 #else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NDS32BE #endif #elif defined(__nios2__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_NIOS2 #elif defined(__or1k__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_OPENRISC #elif defined(__powerpc64__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64 -# endif +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64LE +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC64 +#endif #elif defined(__powerpc__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_PPC #elif defined(__riscv) -# if defined(__LP64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32 -# endif +#if defined(__LP64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_RISCV32 +#endif #elif defined(__s390x__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390X #elif defined(__s390__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390 +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_S390 #elif defined(__sh__) -# if defined(__LP64__) -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64 -# endif -# else -# if (BYTE_ORDER == LITTLE_ENDIAN) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH -# endif -# endif +#if defined(__LP64__) +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH64 +#endif +#else +#if (BYTE_ORDER == LITTLE_ENDIAN) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SHEL +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SH +#endif +#endif #elif defined(__sparc__) -# if defined(__arch64__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64 -# else -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC -# endif +#if defined(__arch64__) +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC64 +#else +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_SPARC +#endif #elif defined(__xtensa__) -# define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA +#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_XTENSA #else -# error "Platform does not support seccomp filter yet" +#error "Platform does not support seccomp filter yet" #endif static struct sock_filter ps_seccomp_filter[] = { /* Check syscall arch */ - BPF_STMT(BPF_LD + BPF_W + BPF_ABS, - offsetof(struct seccomp_data, arch)), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, arch)), BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, SECCOMP_AUDIT_ARCH, 1, 0), BPF_STMT(BPF_RET + BPF_K, SECCOMP_FILTER_FAIL), /* Allow syscalls */ - BPF_STMT(BPF_LD + BPF_W + BPF_ABS, - offsetof(struct seccomp_data, nr)), + BPF_STMT(BPF_LD + BPF_W + BPF_ABS, offsetof(struct seccomp_data, nr)), #ifdef __NR_accept SECCOMP_ALLOW(__NR_accept), #endif @@ -367,8 +361,8 @@ static struct sock_filter ps_seccomp_filter[] = { SECCOMP_ALLOW_ARG(__NR_ioctl, 1, TCGETS), /* dumping leases on musl requires this */ SECCOMP_ALLOW_ARG(__NR_ioctl, 1, TIOCGWINSZ), - /* SECCOMP BPF is newer than nl80211 so we don't need SIOCGIWESSID - * which lives in the impossible to include linux/wireless.h header */ +/* SECCOMP BPF is newer than nl80211 so we don't need SIOCGIWESSID + * which lives in the impossible to include linux/wireless.h header */ #endif #ifdef __NR_madvise /* needed for musl */ SECCOMP_ALLOW(__NR_madvise), @@ -435,7 +429,7 @@ static struct sock_filter ps_seccomp_filter[] = { SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_ACCEPT), SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_ACCEPT4), SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_LISTEN), - SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_GETSOCKOPT), /* overflow */ + SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_GETSOCKOPT), /* overflow */ SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_RECV), SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_RECVFROM), SECCOMP_ALLOW_ARG(__NR_socketcall, 0, SYS_RECVMSG), @@ -533,9 +527,8 @@ static struct sock_fprog ps_seccomp_prog = { static void ps_seccomp_violation(__unused int signum, siginfo_t *si, __unused void *context) { - - logerrx("%s: unexpected syscall %d (arch=0x%x)", - __func__, si->si_syscall, si->si_arch); + logerrx("%s: unexpected syscall %d (arch=0x%x)", __func__, + si->si_syscall, si->si_arch); _exit(EXIT_FAILURE); } @@ -568,8 +561,8 @@ ps_seccomp_enter(void) #endif if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1 || - prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &ps_seccomp_prog) == -1) - { + prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &ps_seccomp_prog) == + -1) { if (errno == EINVAL) errno = ENOSYS; return -1; diff --git a/src/privsep-root.c b/src/privsep-root.c index f4e65b0e..e9b64b3e 100644 --- a/src/privsep-root.c +++ b/src/privsep-root.c @@ -26,11 +26,11 @@ * SUCH DAMAGE. */ +#include #include #include #include #include -#include #include #include @@ -46,8 +46,8 @@ #include "auth.h" #include "common.h" #include "dev.h" -#include "dhcpcd.h" #include "dhcp6.h" +#include "dhcpcd.h" #include "eloop.h" #include "if.h" #include "ipv6nd.h" @@ -58,8 +58,7 @@ __CTASSERT(sizeof(ioctl_request_t) <= sizeof(unsigned long)); -struct psr_error -{ +struct psr_error { ssize_t psr_result; int psr_errno; char psr_pad[sizeof(ssize_t) - sizeof(int)]; @@ -87,10 +86,10 @@ ps_root_readerrorcb(struct psr_ctx *pc) struct msghdr msg = { .msg_iov = iov, .msg_iovlen = __arraycount(iov) }; ssize_t len; -#define PSR_ERROR(e) \ - do { \ - psr_error->psr_errno = (e); \ - goto error; \ +#define PSR_ERROR(e) \ + do { \ + psr_error->psr_errno = (e); \ + goto error; \ } while (0 /* CONSTCOND */) if (eloop_waitfd(fd) == -1) @@ -155,12 +154,10 @@ ps_root_readerrorcb(struct psr_ctx *pc) ssize_t ps_root_readerror(struct dhcpcd_ctx *ctx, void *data, size_t len) { - struct psr_ctx pc = { - .psr_ctx = ctx, + struct psr_ctx pc = { .psr_ctx = ctx, .psr_data = data, .psr_datalen = len, - .psr_mallocdata = false - }; + .psr_mallocdata = false }; ps_root_readerrorcb(&pc); @@ -171,12 +168,10 @@ ps_root_readerror(struct dhcpcd_ctx *ctx, void *data, size_t len) ssize_t ps_root_mreaderror(struct dhcpcd_ctx *ctx, void **data, size_t *len) { - struct psr_ctx pc = { - .psr_ctx = ctx, + struct psr_ctx pc = { .psr_ctx = ctx, .psr_data = NULL, .psr_datalen = 0, - .psr_mallocdata = true - }; + .psr_mallocdata = true }; ps_root_readerrorcb(&pc); @@ -187,8 +182,8 @@ ps_root_mreaderror(struct dhcpcd_ctx *ctx, void **data, size_t *len) } static ssize_t -ps_root_writeerror(struct dhcpcd_ctx *ctx, ssize_t result, - void *data, size_t len) +ps_root_writeerror(struct dhcpcd_ctx *ctx, ssize_t result, void *data, + size_t len) { struct psr_error psr = { .psr_result = result, @@ -213,8 +208,8 @@ ps_root_writeerror(struct dhcpcd_ctx *ctx, ssize_t result, /* Error sending the message? Try sending the error of sending. */ if (err == -1 && errno != EPIPE) { - logerr("%s: result=%zd, data=%p, len=%zu", - __func__, result, data, len); + logerr("%s: result=%zd, data=%p, len=%zu", __func__, result, + data, len); psr.psr_result = err; psr.psr_errno = errno; psr.psr_datalen = 0; @@ -231,18 +226,18 @@ ps_root_doioctl(unsigned long req, void *data, size_t len) int s, err; /* Only allow these ioctls */ - switch(req) { + switch (req) { #ifdef SIOCAIFADDR - case SIOCAIFADDR: /* FALLTHROUGH */ - case SIOCDIFADDR: /* FALLTHROUGH */ + case SIOCAIFADDR: /* FALLTHROUGH */ + case SIOCDIFADDR: /* FALLTHROUGH */ #endif #ifdef SIOCSIFHWADDR - case SIOCSIFHWADDR: /* FALLTHROUGH */ + case SIOCSIFHWADDR: /* FALLTHROUGH */ #endif #ifdef SIOCGIFPRIORITY - case SIOCGIFPRIORITY: /* FALLTHROUGH */ + case SIOCGIFPRIORITY: /* FALLTHROUGH */ #endif - case SIOCSIFFLAGS: /* FALLTHROUGH */ + case SIOCSIFFLAGS: /* FALLTHROUGH */ case SIOCGIFMTU: break; default: @@ -273,7 +268,7 @@ static ssize_t ps_root_run_script(struct dhcpcd_ctx *ctx, const void *data, size_t len) { const char *envbuf = data; - char * const argv[] = { ctx->script, NULL }; + char *const argv[] = { ctx->script, NULL }; pid_t pid; int status; @@ -301,7 +296,6 @@ ps_root_run_script(struct dhcpcd_ctx *ctx, const void *data, size_t len) static bool ps_root_validpath(const struct dhcpcd_ctx *ctx, uint16_t cmd, const char *path) { - /* Avoid a previous directory attack to avoid /proc/../ * dhcpcd should never use a path with double dots. */ if (strstr(path, "..") != NULL) @@ -332,8 +326,8 @@ ps_root_validpath(const struct dhcpcd_ctx *ctx, uint16_t cmd, const char *path) } static ssize_t -ps_root_dowritefile(const struct dhcpcd_ctx *ctx, - mode_t mode, void *data, size_t len) +ps_root_dowritefile(const struct dhcpcd_ctx *ctx, mode_t mode, void *data, + size_t len) { char *file = data, *nc; @@ -353,7 +347,6 @@ ps_root_dowritefile(const struct dhcpcd_ctx *ctx, static ssize_t ps_root_monordm(uint64_t *rdm, size_t len) { - if (len != sizeof(*rdm)) { errno = EINVAL; return -1; @@ -363,7 +356,7 @@ ps_root_monordm(uint64_t *rdm, size_t len) #endif #ifdef PRIVSEP_GETIFADDRS -#define IFA_NADDRS 4 +#define IFA_NADDRS 4 static ssize_t ps_root_dogetifaddrs(void **rdata, size_t *rlen) { @@ -427,18 +420,18 @@ ps_root_dogetifaddrs(void **rdata, size_t *rlen) sap = buf; buf += ALIGN(sizeof(salen) * IFA_NADDRS); -#define COPYINSA(addr) \ - do { \ - if ((addr) != NULL) \ - salen = sa_len((addr)); \ - else \ - salen = 0; \ - if (salen != 0) { \ - memcpy(sap, &salen, sizeof(salen)); \ - memcpy(buf, (addr), salen); \ - buf += ALIGN(salen); \ - } \ - sap += sizeof(salen); \ +#define COPYINSA(addr) \ + do { \ + if ((addr) != NULL) \ + salen = sa_len((addr)); \ + else \ + salen = 0; \ + if (salen != 0) { \ + memcpy(sap, &salen, sizeof(salen)); \ + memcpy(buf, (addr), salen); \ + buf += ALIGN(salen); \ + } \ + sap += sizeof(salen); \ } while (0 /*CONSTCOND */) COPYINSA(ifa->ifa_addr); @@ -447,8 +440,7 @@ ps_root_dogetifaddrs(void **rdata, size_t *rlen) #ifdef BSD if (ifa->ifa_addr != NULL && - ifa->ifa_addr->sa_family == AF_LINK) - { + ifa->ifa_addr->sa_family == AF_LINK) { salen = (socklen_t)sizeof(struct if_data); memcpy(buf, ifa->ifa_data, salen); buf += ALIGN(salen); @@ -498,8 +490,8 @@ ps_root_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) err = ps_sendpsmmsg(ctx, psp->psp_fd, psm, msg); if (err == -1) { - logerr("%s: failed to send message to pid %d", - __func__, psp->psp_pid); + logerr("%s: failed to send message to pid %d", __func__, + psp->psp_pid); ps_freeprocess(psp); } return 0; @@ -511,7 +503,7 @@ ps_root_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) switch (cmd) { #ifdef INET #ifdef ARP - case PS_BPF_ARP: /* FALLTHROUGH */ + case PS_BPF_ARP: /* FALLTHROUGH */ #endif case PS_BPF_BOOTP: return ps_bpf_cmd(ctx, psm, msg); @@ -522,7 +514,7 @@ ps_root_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) #endif #ifdef INET6 #ifdef DHCP6 - case PS_DHCP6: /* FALLTHROUGH */ + case PS_DHCP6: /* FALLTHROUGH */ #endif case PS_ND: return ps_inet_cmd(ctx, psm, msg); @@ -574,8 +566,8 @@ ps_root_recvmsgcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) } break; case PS_WRITEFILE: - err = ps_root_dowritefile(ctx, (mode_t)psm->ps_flags, - data, len); + err = ps_root_dowritefile(ctx, (mode_t)psm->ps_flags, data, + len); break; case PS_FILEMTIME: err = filemtime(data, &mtime); @@ -627,8 +619,8 @@ ps_root_recvmsg(void *arg, unsigned short events) { struct ps_process *psp = arg; - if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, - ps_root_recvmsgcb, psp->psp_ctx) == -1) + if (ps_recvpsmsg(psp->psp_ctx, psp->psp_fd, events, ps_root_recvmsgcb, + psp->psp_ctx) == -1) logerr(__func__); } @@ -650,8 +642,8 @@ ps_root_handleinterface(void *arg, int action, const char *ifname) return -1; } - return (int)ps_sendcmd(ctx, ctx->ps_data_fd, PS_DEV_IFCMD, - flag, ifname, strlen(ifname) + 1); + return (int)ps_sendcmd(ctx, ctx->ps_data_fd, PS_DEV_IFCMD, flag, ifname, + strlen(ifname) + 1); } #endif @@ -663,8 +655,7 @@ ps_root_startcb(struct ps_process *psp) if (ctx->options & DHCPCD_MANAGER) setproctitle("[privileged proxy]"); else - setproctitle("[privileged proxy] %s%s%s", - ctx->ifv[0], + setproctitle("[privileged proxy] %s%s%s", ctx->ifv[0], ctx->options & DHCPCD_IPV4 ? " [ip4]" : "", ctx->options & DHCPCD_IPV6 ? " [ip6]" : ""); ctx->options |= DHCPCD_PRIVSEPROOT; @@ -684,12 +675,12 @@ ps_root_startcb(struct ps_process *psp) if (ctx->options & DHCPCD_IPV4) { int buflen = 1; - ctx->udp_wfd = xsocket(PF_INET, - SOCK_RAW | SOCK_CXNB, IPPROTO_UDP); + ctx->udp_wfd = xsocket(PF_INET, SOCK_RAW | SOCK_CXNB, + IPPROTO_UDP); if (ctx->udp_wfd == -1) logerr("%s: dhcp_openraw", __func__); else if (setsockopt(ctx->udp_wfd, SOL_SOCKET, SO_RCVBUF, - &buflen, sizeof(buflen)) == -1) + &buflen, sizeof(buflen)) == -1) logerr("%s: setsockopt SO_RCVBUF DHCP", __func__); } #endif @@ -700,8 +691,8 @@ ps_root_startcb(struct ps_process *psp) ctx->nd_fd = ipv6nd_open(false); if (ctx->nd_fd == -1) logerr("%s: ipv6nd_open", __func__); - else if (setsockopt(ctx->nd_fd, SOL_SOCKET, SO_RCVBUF, - &buflen, sizeof(buflen)) == -1) + else if (setsockopt(ctx->nd_fd, SOL_SOCKET, SO_RCVBUF, &buflen, + sizeof(buflen)) == -1) logerr("%s: setsockopt SO_RCVBUF ND", __func__); } #endif @@ -713,7 +704,7 @@ ps_root_startcb(struct ps_process *psp) if (ctx->dhcp6_wfd == -1) logerr("%s: dhcp6_openraw", __func__); else if (setsockopt(ctx->dhcp6_wfd, SOL_SOCKET, SO_RCVBUF, - &buflen, sizeof(buflen)) == -1) + &buflen, sizeof(buflen)) == -1) logerr("%s: setsockopt SO_RCVBUF DHCP6", __func__); } #endif @@ -756,18 +747,17 @@ ps_root_signalcb(int sig, void *arg) if (WIFEXITED(status) && WEXITSTATUS(status) != 0) logerrx("%s%s%s exited unexpectedly from PID %d," - " code=%d", - ifname, ifname[0] != '\0' ? ": " : "", - name, pid, WEXITSTATUS(status)); + " code=%d", + ifname, ifname[0] != '\0' ? ": " : "", name, pid, + WEXITSTATUS(status)); else if (WIFSIGNALED(status)) logerrx("%s%s%s exited unexpectedly from PID %d," - " signal=%s", - ifname, ifname[0] != '\0' ? ": " : "", - name, pid, strsignal(WTERMSIG(status))); + " signal=%s", + ifname, ifname[0] != '\0' ? ": " : "", name, pid, + strsignal(WTERMSIG(status))); else - logdebugx("%s%s%s exited from PID %d", - ifname, ifname[0] != '\0' ? ": " : "", - name, pid); + logdebugx("%s%s%s exited from PID %d", ifname, + ifname[0] != '\0' ? ": " : "", name, pid); if (psp != NULL) ps_freeprocess(psp); @@ -793,7 +783,7 @@ ps_root_devcb(struct dhcpcd_ctx *ctx, struct ps_msghdr *psm, struct msghdr *msg) return -1; } - switch(psm->ps_flags) { + switch (psm->ps_flags) { case PS_DEV_IFADDED: action = 1; break; @@ -818,7 +808,7 @@ ps_root_dispatchcb(void *arg, struct ps_msghdr *psm, struct msghdr *msg) struct dhcpcd_ctx *ctx = arg; ssize_t err; - switch(psm->ps_cmd) { + switch (psm->ps_cmd) { #ifdef PLUGIN_DEV case PS_DEV_IFCMD: err = ps_root_devcb(ctx, psm, msg); @@ -839,8 +829,8 @@ ps_root_dispatch(void *arg, unsigned short events) { struct dhcpcd_ctx *ctx = arg; - if (ps_recvpsmsg(ctx, ctx->ps_data_fd, events, - ps_root_dispatchcb, ctx) == -1) + if (ps_recvpsmsg(ctx, ctx->ps_data_fd, events, ps_root_dispatchcb, + ctx) == -1) logerr(__func__); } @@ -864,7 +854,7 @@ ps_root_start(struct dhcpcd_ctx *ctx) .psi_cmd = PS_ROOT, }; struct ps_process *psp; - int logfd[2] = { -1, -1}, datafd[2] = { -1, -1}; + int logfd[2] = { -1, -1 }, datafd[2] = { -1, -1 }; pid_t pid; if (xsocketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CXNB, 0, logfd) == -1) @@ -889,8 +879,8 @@ ps_root_start(struct dhcpcd_ctx *ctx) return -1; strlcpy(psp->psp_name, "privileged proxy", sizeof(psp->psp_name)); - pid = ps_startprocess(psp, ps_root_recvmsg, NULL, - ps_root_startcb, PSF_ELOOP); + pid = ps_startprocess(psp, ps_root_recvmsg, NULL, ps_root_startcb, + PSF_ELOOP); if (pid == -1) return -1; @@ -898,7 +888,7 @@ ps_root_start(struct dhcpcd_ctx *ctx) ctx->ps_log_fd = logfd[0]; /* Keep open to pass to processes */ ctx->ps_log_root_fd = logfd[1]; if (eloop_event_add(ctx->eloop, ctx->ps_log_root_fd, ELE_READ, - ps_root_log, ctx) == -1) + ps_root_log, ctx) == -1) return -1; ctx->ps_data_fd = datafd[1]; close(datafd[0]); @@ -912,7 +902,7 @@ ps_root_start(struct dhcpcd_ctx *ctx) ctx->ps_data_fd = datafd[0]; close(datafd[1]); if (eloop_event_add(ctx->eloop, ctx->ps_data_fd, ELE_READ, - ps_root_dispatch, ctx) == -1) + ps_root_dispatch, ctx) == -1) return -1; return pid; @@ -921,7 +911,6 @@ ps_root_start(struct dhcpcd_ctx *ctx) void ps_root_close(struct dhcpcd_ctx *ctx) { - if_closesockets(ctx); #ifdef INET @@ -990,10 +979,9 @@ ps_root_stop(struct dhcpcd_ctx *ctx) * So we just log the intent to exit. * Even sending this will be a race to exit. */ if (psp) { - logdebugx("%s%s%s will exit from PID %d", - psp->psp_ifname, - psp->psp_ifname[0] != '\0' ? ": " : "", - psp->psp_name, psp->psp_pid); + logdebugx("%s%s%s will exit from PID %d", psp->psp_ifname, + psp->psp_ifname[0] != '\0' ? ": " : "", psp->psp_name, + psp->psp_pid); if (ps_stopprocess(psp) == -1) return -1; @@ -1008,12 +996,10 @@ ps_root_stop(struct dhcpcd_ctx *ctx) ssize_t ps_root_stopprocesses(struct dhcpcd_ctx *ctx) { - if (!(IN_PRIVSEP_SE(ctx))) return 0; - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_STOPPROCS, 0, - NULL, 0) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_STOPPROCS, 0, NULL, 0) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -1021,9 +1007,7 @@ ps_root_stopprocesses(struct dhcpcd_ctx *ctx) ssize_t ps_root_script(struct dhcpcd_ctx *ctx, const void *data, size_t len) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_SCRIPT, - 0, data, len) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_SCRIPT, 0, data, len) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -1049,19 +1033,18 @@ ps_root_ioctl(struct dhcpcd_ctx *ctx, ioctl_request_t req, void *data, ssize_t ps_root_unlink(struct dhcpcd_ctx *ctx, const char *file) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_UNLINK, 0, - file, strlen(file) + 1) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_UNLINK, 0, file, + strlen(file) + 1) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } ssize_t -ps_root_readfile(struct dhcpcd_ctx *ctx, const char *file, - void *data, size_t len) +ps_root_readfile(struct dhcpcd_ctx *ctx, const char *file, void *data, + size_t len) { - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_READFILE, 0, - file, strlen(file) + 1) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_READFILE, 0, file, + strlen(file) + 1) == -1) return -1; return ps_root_readerror(ctx, data, len); } @@ -1081,8 +1064,8 @@ ps_root_writefile(struct dhcpcd_ctx *ctx, const char *file, mode_t mode, } memcpy(buf + flen, data, len); - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_WRITEFILE, mode, - buf, flen + len) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_WRITEFILE, mode, buf, + flen + len) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -1090,9 +1073,8 @@ ps_root_writefile(struct dhcpcd_ctx *ctx, const char *file, mode_t mode, ssize_t ps_root_filemtime(struct dhcpcd_ctx *ctx, const char *file, time_t *time) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_FILEMTIME, 0, - file, strlen(file) + 1) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_FILEMTIME, 0, file, + strlen(file) + 1) == -1) return -1; return ps_root_readerror(ctx, time, sizeof(*time)); } @@ -1100,9 +1082,7 @@ ps_root_filemtime(struct dhcpcd_ctx *ctx, const char *file, time_t *time) ssize_t ps_root_logreopen(struct dhcpcd_ctx *ctx) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_LOGREOPEN, 0, - NULL, 0) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_LOGREOPEN, 0, NULL, 0) == -1) return -1; return ps_root_readerror(ctx, NULL, 0); } @@ -1118,8 +1098,7 @@ ps_root_getifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifahead) size_t len; ssize_t err; - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), - PS_GETIFADDRS, 0, NULL, 0) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_GETIFADDRS, 0, NULL, 0) == -1) return -1; err = ps_root_mreaderror(ctx, &buf, &len); @@ -1135,28 +1114,28 @@ ps_root_getifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifahead) bp = buf; *ifahead = (struct ifaddrs *)(void *)bp; for (ifa = *ifahead; ifa != NULL; ifa = ifa->ifa_next) { - if (len < ALIGN(sizeof(*ifa)) + - ALIGN(IFNAMSIZ) + ALIGN(sizeof(salen) * IFA_NADDRS)) + if (len < ALIGN(sizeof(*ifa)) + ALIGN(IFNAMSIZ) + + ALIGN(sizeof(salen) * IFA_NADDRS)) goto err; bp += ALIGN(sizeof(*ifa)); ifa->ifa_name = bp; bp += ALIGN(IFNAMSIZ); sap = bp; bp += ALIGN(sizeof(salen) * IFA_NADDRS); - len -= ALIGN(sizeof(*ifa)) + - ALIGN(IFNAMSIZ) + ALIGN(sizeof(salen) * IFA_NADDRS); - -#define COPYOUTSA(addr) \ - do { \ - memcpy(&salen, sap, sizeof(salen)); \ - if (len < salen) \ - goto err; \ - if (salen != 0) { \ - (addr) = (struct sockaddr *)(void *)bp; \ - bp += ALIGN(salen); \ - len -= ALIGN(salen); \ - } \ - sap += sizeof(salen); \ + len -= ALIGN(sizeof(*ifa)) + ALIGN(IFNAMSIZ) + + ALIGN(sizeof(salen) * IFA_NADDRS); + +#define COPYOUTSA(addr) \ + do { \ + memcpy(&salen, sap, sizeof(salen)); \ + if (len < salen) \ + goto err; \ + if (salen != 0) { \ + (addr) = (struct sockaddr *)(void *)bp; \ + bp += ALIGN(salen); \ + len -= ALIGN(salen); \ + } \ + sap += sizeof(salen); \ } while (0 /* CONSTCOND */) COPYOUTSA(ifa->ifa_addr); @@ -1192,9 +1171,8 @@ ps_root_getifaddrs(struct dhcpcd_ctx *ctx, struct ifaddrs **ifahead) int ps_root_getauthrdm(struct dhcpcd_ctx *ctx, uint64_t *rdm) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_AUTH_MONORDM, 0, - rdm, sizeof(*rdm))== -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_AUTH_MONORDM, 0, rdm, + sizeof(*rdm)) == -1) return -1; return (int)ps_root_readerror(ctx, rdm, sizeof(*rdm)); } @@ -1204,19 +1182,17 @@ ps_root_getauthrdm(struct dhcpcd_ctx *ctx, uint64_t *rdm) int ps_root_dev_initialised(struct dhcpcd_ctx *ctx, const char *ifname) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_DEV_INITTED, 0, - ifname, strlen(ifname) + 1)== -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_DEV_INITTED, 0, ifname, + strlen(ifname) + 1) == -1) return -1; return (int)ps_root_readerror(ctx, NULL, 0); } int -ps_root_dev_listening(struct dhcpcd_ctx * ctx) +ps_root_dev_listening(struct dhcpcd_ctx *ctx) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_DEV_LISTENING, - 0, NULL, 0) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_DEV_LISTENING, 0, NULL, 0) == + -1) return -1; return (int)ps_root_readerror(ctx, NULL, 0); } diff --git a/src/privsep-root.h b/src/privsep-root.h index fa5a6e13..789d5798 100644 --- a/src/privsep-root.h +++ b/src/privsep-root.h @@ -65,8 +65,8 @@ ssize_t ps_root_ioctl6(struct dhcpcd_ctx *, unsigned long, void *, size_t); ssize_t ps_root_indirectioctl(struct dhcpcd_ctx *, unsigned long, const char *, void *, size_t); ssize_t ps_root_ifignoregroup(struct dhcpcd_ctx *, const char *); -ssize_t ps_root_sysctl(struct dhcpcd_ctx *, const int *, unsigned int, - void *, size_t *, const void *, size_t); +ssize_t ps_root_sysctl(struct dhcpcd_ctx *, const int *, unsigned int, void *, + size_t *, const void *, size_t); #endif #ifdef __linux__ ssize_t ps_root_sendnetlink(struct dhcpcd_ctx *, int, struct msghdr *); diff --git a/src/privsep-sun.c b/src/privsep-sun.c index 0da788a5..0920525e 100644 --- a/src/privsep-sun.c +++ b/src/privsep-sun.c @@ -76,8 +76,8 @@ ps_root_doroute(void *data, size_t len) } ssize_t -ps_root_os(struct ps_msghdr *psm, struct msghdr *msg, - void **rdata, size_t *rlen, __unused bool *free_rdata) +ps_root_os(struct ps_msghdr *psm, struct msghdr *msg, void **rdata, + size_t *rlen, __unused bool *free_rdata) { struct iovec *iov = msg->msg_iov; void *data = iov->iov_base; @@ -102,11 +102,11 @@ ps_root_os(struct ps_msghdr *psm, struct msghdr *msg, } ssize_t -ps_root_ioctl6(struct dhcpcd_ctx *ctx, unsigned long request, void *data, size_t len) +ps_root_ioctl6(struct dhcpcd_ctx *ctx, unsigned long request, void *data, + size_t len) { - - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_IOCTL6, - request, data, len) == -1) + if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_IOCTL6, request, data, len) == + -1) return -1; return ps_root_readerror(ctx, data, len); } @@ -114,7 +114,6 @@ ps_root_ioctl6(struct dhcpcd_ctx *ctx, unsigned long request, void *data, size_t ssize_t ps_root_route(struct dhcpcd_ctx *ctx, void *data, size_t len) { - if (ps_sendcmd(ctx, PS_ROOT_FD(ctx), PS_ROUTE, 0, data, len) == -1) return -1; return ps_root_readerror(ctx, data, len); diff --git a/src/privsep.c b/src/privsep.c index 17507a5c..b0279a08 100644 --- a/src/privsep.c +++ b/src/privsep.c @@ -40,10 +40,10 @@ * this in a script or something. */ +#include #include #include #include -#include #include #ifdef AF_LINK @@ -56,8 +56,8 @@ #include #include #include -#include /* For offsetof, struct padding debug */ #include +#include /* For offsetof, struct padding debug */ #include #include #include @@ -76,6 +76,7 @@ #ifdef HAVE_CAPSICUM #include #include + #include #endif #ifdef HAVE_UTIL_H @@ -84,12 +85,12 @@ /* CMSG_ALIGN is a Linux extension */ #ifndef CMSG_ALIGN -#define CMSG_ALIGN(n) (CMSG_SPACE((n)) - CMSG_SPACE(0)) +#define CMSG_ALIGN(n) (CMSG_SPACE((n)) - CMSG_SPACE(0)) #endif /* Calculate number of padding bytes to achieve 'struct cmsghdr' alignment */ #define CALC_CMSG_PADLEN(has_cmsg, pos) \ - ((has_cmsg) ? (socklen_t)(CMSG_ALIGN((pos)) - (pos)) : 0) + ((has_cmsg) ? (socklen_t)(CMSG_ALIGN((pos)) - (pos)) : 0) int ps_init(struct dhcpcd_ctx *ctx) @@ -111,8 +112,7 @@ ps_init(struct dhcpcd_ctx *ctx) if (stat(pw->pw_dir, &st) == -1 || !S_ISDIR(st.st_mode)) { ctx->options &= ~DHCPCD_PRIVSEP; - logerrx("refusing chroot: %s: %s", - PRIVSEP_USER, pw->pw_dir); + logerrx("refusing chroot: %s: %s", PRIVSEP_USER, pw->pw_dir); errno = 0; return -1; } @@ -125,8 +125,8 @@ static int ps_dropprivs(struct dhcpcd_ctx *ctx) { struct passwd *pw = ctx->ps_user; - int fd_out = ctx->options & DHCPCD_DUMPLEASE ? - STDOUT_FILENO : STDERR_FILENO; + int fd_out = ctx->options & DHCPCD_DUMPLEASE ? STDOUT_FILENO : + STDERR_FILENO; if (ctx->options & DHCPCD_LAUNCHER) #ifdef ASAN @@ -142,11 +142,9 @@ ps_dropprivs(struct dhcpcd_ctx *ctx) if (chdir("/") == -1) logerr("%s: chdir: /", __func__); - if ((setgroups(1, &pw->pw_gid) == -1 || - setgid(pw->pw_gid) == -1 || - setuid(pw->pw_uid) == -1) && - (errno != EPERM || ctx->options & DHCPCD_FORKED)) - { + if ((setgroups(1, &pw->pw_gid) == -1 || setgid(pw->pw_gid) == -1 || + setuid(pw->pw_uid) == -1) && + (errno != EPERM || ctx->options & DHCPCD_FORKED)) { logerr("failed to drop privileges"); return -1; } @@ -166,8 +164,8 @@ ps_dropprivs(struct dhcpcd_ctx *ctx) * Linux:seccomp, FreeBSD:capsicum, OpenBSD:pledge * Solaris users are sadly out of luck on both counts. */ -#if defined(__NetBSD__) || defined(__DragonFly__) || \ - defined(HAVE_KQUEUE) || defined(HAVE_EPOLL) +#if defined(__NetBSD__) || defined(__DragonFly__) || defined(HAVE_KQUEUE) || \ + defined(HAVE_EPOLL) /* The control proxy *does* need to create new fd's via accept(2). */ if (ctx->ps_ctl == NULL || ctx->ps_ctl->psp_pid != getpid()) { if (setrlimit(RLIMIT_NOFILE, &rzero) == -1) @@ -175,13 +173,12 @@ ps_dropprivs(struct dhcpcd_ctx *ctx) } #endif -#define DHC_NOCHKIO (DHCPCD_STARTED | DHCPCD_DAEMONISE) +#define DHC_NOCHKIO (DHCPCD_STARTED | DHCPCD_DAEMONISE) /* Prohibit writing to files. * Obviously this won't work if we are using a logfile * or redirecting stderr to a file. */ if ((ctx->options & DHC_NOCHKIO) == DHC_NOCHKIO || - (ctx->logfile == NULL && isatty(fd_out) == 1)) - { + (ctx->logfile == NULL && isatty(fd_out) == 1)) { if (setrlimit(RLIMIT_FSIZE, &rzero) == -1) logerr("setrlimit RLIMIT_FSIZE"); } @@ -222,8 +219,7 @@ ps_setbuf(int fd) int minlen = (int)sizeof(struct ps_msg); if (ps_setbuf0(fd, SO_RCVBUF, minlen) == -1 || - ps_setbuf0(fd, SO_SNDBUF, minlen * 2) == -1) - { + ps_setbuf0(fd, SO_SNDBUF, minlen * 2) == -1) { logerr(__func__); return -1; } @@ -233,7 +229,6 @@ ps_setbuf(int fd) int ps_setbuf_fdpair(int fd[]) { - if (ps_setbuf(fd[0]) == -1 || ps_setbuf(fd[1]) == -1) return -1; return 0; @@ -256,8 +251,8 @@ ps_rights_limit_fd_fctnl(int fd) { cap_rights_t rights; - cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_EVENT, - CAP_ACCEPT, CAP_FCNTL); + cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_EVENT, CAP_ACCEPT, + CAP_FCNTL); if (cap_rights_limit(fd, &rights) == -1 && errno != ENOSYS) return -1; return 0; @@ -279,8 +274,8 @@ ps_rights_limit_fd_sockopt(int fd) { cap_rights_t rights; - cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_EVENT, - CAP_GETSOCKOPT, CAP_SETSOCKOPT); + cap_rights_init(&rights, CAP_READ, CAP_WRITE, CAP_EVENT, CAP_GETSOCKOPT, + CAP_SETSOCKOPT); if (cap_rights_limit(fd, &rights) == -1 && errno != ENOSYS) return -1; return 0; @@ -300,7 +295,6 @@ ps_rights_limit_fd_rdonly(int fd) int ps_rights_limit_fdpair(int fd[]) { - if (ps_rights_limit_fd(fd[0]) == -1 || ps_rights_limit_fd(fd[1]) == -1) return -1; return 0; @@ -333,9 +327,9 @@ ps_processhangup(void *arg, unsigned short events) if (!(events & ELE_HANGUP)) logerrx("%s: unexpected event 0x%04x", __func__, events); - logdebugx("%s%s%s exited from PID %d", - psp->psp_ifname, psp->psp_ifname[0] != '\0' ? ": " : "", - psp->psp_name, psp->psp_pid); + logdebugx("%s%s%s exited from PID %d", psp->psp_ifname, + psp->psp_ifname[0] != '\0' ? ": " : "", psp->psp_name, + psp->psp_pid); ps_freeprocess(psp); @@ -350,8 +344,7 @@ pid_t ps_startprocess(struct ps_process *psp, void (*recv_msg)(void *, unsigned short), void (*recv_unpriv_msg)(void *, unsigned short), - int (*callback)(struct ps_process *), - unsigned int flags) + int (*callback)(struct ps_process *), unsigned int flags) { struct dhcpcd_ctx *ctx = psp->psp_ctx; int fd[2]; @@ -397,18 +390,16 @@ ps_startprocess(struct ps_process *psp, if (recv_unpriv_msg == NULL) ; else if (eloop_event_add(ctx->eloop, psp->psp_fd, ELE_READ, - recv_unpriv_msg, psp) == -1) - { - logerr("%s: eloop_event_add fd %d", - __func__, psp->psp_fd); + recv_unpriv_msg, psp) == -1) { + logerr("%s: eloop_event_add fd %d", __func__, + psp->psp_fd); return -1; } #ifdef HAVE_CAPSICUM if (eloop_event_add(ctx->eloop, psp->psp_pfd, ELE_HANGUP, - ps_processhangup, psp) == -1) - { - logerr("%s: eloop_event_add pfd %d", - __func__, psp->psp_pfd); + ps_processhangup, psp) == -1) { + logerr("%s: eloop_event_add pfd %d", __func__, + psp->psp_pfd); return -1; } #endif @@ -429,8 +420,8 @@ ps_startprocess(struct ps_process *psp, logsetfd(ctx->ps_log_fd); #ifdef DEBUG_FD - logerrx("pid %d log_fd=%d data_fd=%d psp_fd=%d", - getpid(), ctx->ps_log_fd, ctx->ps_data_fd, psp->psp_fd); + logerrx("pid %d log_fd=%d data_fd=%d psp_fd=%d", getpid(), + ctx->ps_log_fd, ctx->ps_data_fd, psp->psp_fd); #endif if (ctx->fork_fd != -1) { @@ -462,9 +453,8 @@ ps_startprocess(struct ps_process *psp, #endif } - if (eloop_event_add(ctx->eloop, psp->psp_fd, ELE_READ, - recv_msg, psp) == -1) - { + if (eloop_event_add(ctx->eloop, psp->psp_fd, ELE_READ, recv_msg, psp) == + -1) { logerr("%s: eloop_event_add", __func__); goto errexit; } @@ -507,8 +497,8 @@ ps_stopprocess(struct ps_process *psp) psp->psp_started = false; #ifdef PRIVSEP_DEBUG - logdebugx("%s: me=%d pid=%d fd=%d %s", __func__, - getpid(), psp->psp_pid, psp->psp_fd, psp->psp_name); + logdebugx("%s: me=%d pid=%d fd=%d %s", __func__, getpid(), psp->psp_pid, + psp->psp_fd, psp->psp_name); #endif if (psp->psp_fd != -1) { @@ -590,7 +580,6 @@ ps_start(struct dhcpcd_ctx *ctx) int ps_entersandbox(const char *_pledge, const char **sandbox) { - #if !defined(HAVE_PLEDGE) UNUSED(_pledge); #endif @@ -649,9 +638,8 @@ ps_managersandbox(struct dhcpcd_ctx *ctx, const char *_pledge) #ifdef PRIVSEP_RIGHTS if ((ctx->pf_inet_fd != -1 && - ps_rights_limit_ioctl(ctx->pf_inet_fd) == -1) || - ps_rights_limit_stdio() == -1) - { + ps_rights_limit_ioctl(ctx->pf_inet_fd) == -1) || + ps_rights_limit_stdio() == -1) { logerr("%s: cap_rights_limit", __func__); return -1; } @@ -668,8 +656,8 @@ ps_managersandbox(struct dhcpcd_ctx *ctx, const char *_pledge) logerr("%s: %s", __func__, sandbox); return -1; } else if (ctx->options & DHCPCD_LAUNCHER || - ((!(ctx->options & DHCPCD_DAEMONISE)) && - ctx->options & DHCPCD_MANAGER)) + ((!(ctx->options & DHCPCD_DAEMONISE)) && + ctx->options & DHCPCD_MANAGER)) logdebugx("sandbox: %s", sandbox); return 0; } @@ -679,8 +667,7 @@ ps_stop(struct dhcpcd_ctx *ctx) { int r, ret = 0; - if (!(ctx->options & DHCPCD_PRIVSEP) || - ctx->options & DHCPCD_FORKED || + if (!(ctx->options & DHCPCD_PRIVSEP) || ctx->options & DHCPCD_FORKED || ctx->eloop == NULL) return 0; @@ -729,7 +716,7 @@ ps_stopwait(struct dhcpcd_ctx *ctx) ctx->options |= DHCPCD_EXITING; if (eloop_timeout_add_sec(ctx->eloop, PS_PROCESS_TIMEOUT, - ps_process_timeout, ctx) == -1) + ps_process_timeout, ctx) == -1) logerr("%s: eloop_timeout_add_sec", __func__); #ifdef HAVE_CAPSICUM @@ -738,10 +725,10 @@ ps_stopwait(struct dhcpcd_ctx *ctx) TAILQ_FOREACH(psp, &ctx->ps_processes, next) { if (psp->psp_pfd == -1) continue; - if (eloop_event_add(ctx->eloop, psp->psp_pfd, - ELE_HANGUP, ps_processhangup, psp) == -1) - logerr("%s: eloop_event_add pfd %d", - __func__, psp->psp_pfd); + if (eloop_event_add(ctx->eloop, psp->psp_pfd, ELE_HANGUP, + ps_processhangup, psp) == -1) + logerr("%s: eloop_event_add pfd %d", __func__, + psp->psp_pfd); } #endif @@ -813,12 +800,12 @@ ps_free(struct dhcpcd_ctx *ctx) } int -ps_unrollmsg(struct msghdr *msg, struct ps_msghdr *psm, - const void *data, size_t len) +ps_unrollmsg(struct msghdr *msg, struct ps_msghdr *psm, const void *data, + size_t len) { uint8_t *datap, *namep, *controlp; - socklen_t cmsg_padlen = - CALC_CMSG_PADLEN(psm->ps_controllen, psm->ps_namelen); + socklen_t cmsg_padlen = CALC_CMSG_PADLEN(psm->ps_controllen, + psm->ps_namelen); namep = UNCONST(data); controlp = namep + psm->ps_namelen + cmsg_padlen; @@ -859,18 +846,30 @@ ps_unrollmsg(struct msghdr *msg, struct ps_msghdr *psm, } ssize_t -ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, - struct ps_msghdr *psm, const struct msghdr *msg) +ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, struct ps_msghdr *psm, + const struct msghdr *msg) { long padding[1] = { 0 }; struct iovec iov[] = { { .iov_base = UNCONST(psm), .iov_len = sizeof(*psm) }, - { .iov_base = NULL, }, /* name */ - { .iov_base = NULL, }, /* control padding */ - { .iov_base = NULL, }, /* control */ - { .iov_base = NULL, }, /* payload 1 */ - { .iov_base = NULL, }, /* payload 2 */ - { .iov_base = NULL, }, /* payload 3 */ + { + .iov_base = NULL, + }, /* name */ + { + .iov_base = NULL, + }, /* control padding */ + { + .iov_base = NULL, + }, /* control */ + { + .iov_base = NULL, + }, /* payload 1 */ + { + .iov_base = NULL, + }, /* payload 2 */ + { + .iov_base = NULL, + }, /* payload 3 */ }; struct msghdr m = { .msg_iov = iov, .msg_iovlen = 1 }; ssize_t len; @@ -888,8 +887,8 @@ ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, iovp++; m.msg_iovlen++; - cmsg_padlen = - CALC_CMSG_PADLEN(msg->msg_controllen, msg->msg_namelen); + cmsg_padlen = CALC_CMSG_PADLEN(msg->msg_controllen, + msg->msg_namelen); assert(cmsg_padlen <= sizeof(padding)); iovp->iov_len = cmsg_padlen; iovp->iov_base = cmsg_padlen != 0 ? padding : NULL; @@ -903,7 +902,7 @@ ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, for (i = 0; i < (int)msg->msg_iovlen; i++) { if ((size_t)(m.msg_iovlen++) > __arraycount(iov)) { - errno = ENOBUFS; + errno = ENOBUFS; return -1; } iovp->iov_base = msg->msg_iov[i].iov_base; @@ -923,20 +922,20 @@ ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, } ssize_t -ps_sendpsmdata(struct dhcpcd_ctx *ctx, int fd, - struct ps_msghdr *psm, const void *data, size_t len) +ps_sendpsmdata(struct dhcpcd_ctx *ctx, int fd, struct ps_msghdr *psm, + const void *data, size_t len) { struct iovec iov[] = { { .iov_base = UNCONST(data), .iov_len = len }, }; struct msghdr msg = { - .msg_iov = iov, .msg_iovlen = 1, + .msg_iov = iov, + .msg_iovlen = 1, }; return ps_sendpsmmsg(ctx, fd, psm, &msg); } - ssize_t ps_sendmsg(struct dhcpcd_ctx *ctx, int fd, uint16_t cmd, unsigned long flags, const struct msghdr *msg) @@ -952,7 +951,7 @@ ps_sendmsg(struct dhcpcd_ctx *ctx, int fd, uint16_t cmd, unsigned long flags, for (i = 0; i < (size_t)msg->msg_iovlen; i++) psm.ps_datalen += msg->msg_iov[i].iov_len; -#if 0 /* For debugging structure padding. */ +#if 0 /* For debugging structure padding. */ logerrx("psa.family %lu %zu", offsetof(struct ps_addr, psa_family), sizeof(psm.ps_id.psi_addr.psa_family)); logerrx("psa.pad %lu %zu", offsetof(struct ps_addr, psa_pad), sizeof(psm.ps_id.psi_addr.psa_pad)); logerrx("psa.psa_u %lu %zu", offsetof(struct ps_addr, psa_u), sizeof(psm.ps_id.psi_addr.psa_u)); @@ -988,19 +987,18 @@ ps_sendcmd(struct dhcpcd_ctx *ctx, int fd, uint16_t cmd, unsigned long flags, .ps_cmd = cmd, .ps_flags = flags, }; - struct iovec iov[] = { - { .iov_base = UNCONST(data), .iov_len = len } - }; + struct iovec iov[] = { { .iov_base = UNCONST(data), .iov_len = len } }; struct msghdr msg = { - .msg_iov = iov, .msg_iovlen = 1, + .msg_iov = iov, + .msg_iovlen = 1, }; return ps_sendpsmmsg(ctx, fd, &psm, &msg); } ssize_t -ps_sendcmdmsg(struct dhcpcd_ctx *ctx, int fd, uint16_t cmd, - unsigned long flags, const struct msghdr *msg) +ps_sendcmdmsg(struct dhcpcd_ctx *ctx, int fd, uint16_t cmd, unsigned long flags, + const struct msghdr *msg) { struct ps_msghdr psm = { .ps_cmd = cmd, .ps_flags = flags }; @@ -1013,13 +1011,15 @@ ps_recvmsg(int rfd, unsigned short events, uint16_t cmd, int wfd) struct sockaddr_storage ss = { .ss_family = AF_UNSPEC }; uint8_t controlbuf[sizeof(struct sockaddr_storage)] = { 0 }; uint8_t databuf[64 * 1024]; - struct iovec iov[] = { - { .iov_base = databuf, .iov_len = sizeof(databuf) } - }; + struct iovec iov[] = { { .iov_base = databuf, + .iov_len = sizeof(databuf) } }; struct msghdr msg = { - .msg_name = &ss, .msg_namelen = sizeof(ss), - .msg_control = controlbuf, .msg_controllen = sizeof(controlbuf), - .msg_iov = iov, .msg_iovlen = 1, + .msg_name = &ss, + .msg_namelen = sizeof(ss), + .msg_control = controlbuf, + .msg_controllen = sizeof(controlbuf), + .msg_iov = iov, + .msg_iovlen = 1, }; ssize_t len; @@ -1051,8 +1051,8 @@ ps_daemonised(struct dhcpcd_ctx *ctx) TAILQ_FOREACH(psp, &ctx->ps_processes, next) { if (psp->psp_pid == getpid()) continue; - if (ps_sendcmd(psp->psp_ctx, psp->psp_fd, PS_DAEMONISED, - 0, NULL, 0) == -1) + if (ps_sendcmd(psp->psp_ctx, psp->psp_fd, PS_DAEMONISED, 0, + NULL, 0) == -1) err = -1; } @@ -1102,7 +1102,7 @@ ps_recvpsmsg(struct dhcpcd_ctx *ctx, int fd, unsigned short events, } if (stop) { -stop: + stop: ctx->options |= DHCPCD_EXITING; #ifdef PRIVSEP_DEBUG logdebugx("process %d stopping", getpid()); diff --git a/src/privsep.h b/src/privsep.h index 3ab67db7..e55176e8 100644 --- a/src/privsep.h +++ b/src/privsep.h @@ -29,109 +29,106 @@ #ifndef PRIVSEP_H #define PRIVSEP_H -//#define PRIVSEP_DEBUG +// #define PRIVSEP_DEBUG /* Start flags */ -#define PSF_DROPPRIVS 0x01 -#define PSF_ELOOP 0x02 +#define PSF_DROPPRIVS 0x01 +#define PSF_ELOOP 0x02 /* Protocols */ -#define PS_BOOTP 0x0001 -#define PS_ND 0x0002 -#define PS_DHCP6 0x0003 -#define PS_BPF_BOOTP 0x0004 -#define PS_BPF_ARP 0x0005 +#define PS_BOOTP 0x0001 +#define PS_ND 0x0002 +#define PS_DHCP6 0x0003 +#define PS_BPF_BOOTP 0x0004 +#define PS_BPF_ARP 0x0005 /* Generic commands */ -#define PS_IOCTL 0x0010 -#define PS_ROUTE 0x0011 /* Also used for NETLINK */ -#define PS_SCRIPT 0x0012 -#define PS_UNLINK 0x0013 -#define PS_READFILE 0x0014 -#define PS_WRITEFILE 0x0015 -#define PS_FILEMTIME 0x0016 -#define PS_AUTH_MONORDM 0x0017 -#define PS_CTL 0x0018 -#define PS_CTL_EOF 0x0019 -#define PS_LOGREOPEN 0x0020 -#define PS_STOPPROCS 0x0021 -#define PS_DAEMONISED 0x0022 +#define PS_IOCTL 0x0010 +#define PS_ROUTE 0x0011 /* Also used for NETLINK */ +#define PS_SCRIPT 0x0012 +#define PS_UNLINK 0x0013 +#define PS_READFILE 0x0014 +#define PS_WRITEFILE 0x0015 +#define PS_FILEMTIME 0x0016 +#define PS_AUTH_MONORDM 0x0017 +#define PS_CTL 0x0018 +#define PS_CTL_EOF 0x0019 +#define PS_LOGREOPEN 0x0020 +#define PS_STOPPROCS 0x0021 +#define PS_DAEMONISED 0x0022 /* Domains */ -#define PS_ROOT 0x0101 -#define PS_INET 0x0102 -#define PS_CONTROL 0x0103 +#define PS_ROOT 0x0101 +#define PS_INET 0x0102 +#define PS_CONTROL 0x0103 /* BSD Commands */ -#define PS_IOCTLLINK 0x0201 -#define PS_IOCTL6 0x0202 -#define PS_IOCTLINDIRECT 0x0203 -#define PS_IP6FORWARDING 0x0204 -#define PS_GETIFADDRS 0x0205 -#define PS_IFIGNOREGRP 0x0206 -#define PS_SYSCTL 0x0207 +#define PS_IOCTLLINK 0x0201 +#define PS_IOCTL6 0x0202 +#define PS_IOCTLINDIRECT 0x0203 +#define PS_IP6FORWARDING 0x0204 +#define PS_GETIFADDRS 0x0205 +#define PS_IFIGNOREGRP 0x0206 +#define PS_SYSCTL 0x0207 /* Dev Commands */ -#define PS_DEV_LISTENING 0x1001 -#define PS_DEV_INITTED 0x1002 -#define PS_DEV_IFCMD 0x1003 +#define PS_DEV_LISTENING 0x1001 +#define PS_DEV_INITTED 0x1002 +#define PS_DEV_IFCMD 0x1003 /* Dev Interface Commands (via flags) */ -#define PS_DEV_IFADDED 0x0001 -#define PS_DEV_IFREMOVED 0x0002 -#define PS_DEV_IFUPDATED 0x0003 +#define PS_DEV_IFADDED 0x0001 +#define PS_DEV_IFREMOVED 0x0002 +#define PS_DEV_IFUPDATED 0x0003 /* Control Type (via flags) */ -#define PS_CTL_PRIV 0x0004 -#define PS_CTL_UNPRIV 0x0005 +#define PS_CTL_PRIV 0x0004 +#define PS_CTL_UNPRIV 0x0005 /* Sysctl Needs (via flags) */ -#define PS_SYSCTL_OLEN 0x0001 -#define PS_SYSCTL_ODATA 0x0002 +#define PS_SYSCTL_OLEN 0x0001 +#define PS_SYSCTL_ODATA 0x0002 /* Process commands */ -#define PS_START 0x4000 -#define PS_STOP 0x8000 +#define PS_START 0x4000 +#define PS_STOP 0x8000 /* Max INET message size + meta data for IPC */ -#define PS_BUFLEN ((64 * 1024) + \ - sizeof(struct ps_msghdr) + \ - sizeof(struct msghdr) + \ - CMSG_SPACE(sizeof(struct in6_pktinfo) + \ - sizeof(int))) +#define PS_BUFLEN \ + ((64 * 1024) + sizeof(struct ps_msghdr) + sizeof(struct msghdr) + \ + CMSG_SPACE(sizeof(struct in6_pktinfo) + sizeof(int))) -#define PSP_NAMESIZE 16 + INET_MAX_ADDRSTRLEN +#define PSP_NAMESIZE 16 + INET_MAX_ADDRSTRLEN /* Handy macro to work out if in the privsep engine or not. */ -#define IN_PRIVSEP(ctx) \ - ((ctx)->options & DHCPCD_PRIVSEP) -#define IN_PRIVSEP_SE(ctx) \ +#define IN_PRIVSEP(ctx) ((ctx)->options & DHCPCD_PRIVSEP) +#define IN_PRIVSEP_SE(ctx) \ (((ctx)->options & (DHCPCD_PRIVSEP | DHCPCD_FORKED)) == DHCPCD_PRIVSEP) -#define PS_PROCESS_TIMEOUT 5 /* seconds to stop all processes */ +#define PS_PROCESS_TIMEOUT 5 /* seconds to stop all processes */ #ifdef PRIVSEP -# ifdef HAVE_CAPSICUM -# define PRIVSEP_RIGHTS -# endif +#ifdef HAVE_CAPSICUM +#define PRIVSEP_RIGHTS +#endif /* Pledge and Capsicum deny nearly all sysctls. * Linux needs directory access to sysctls. */ -# if defined(HAVE_CAPSICUM) ||defined(HAVE_PLEDGE) || defined(__linux__) -# define PRIVSEP_SYSCTL -# endif +#if defined(HAVE_CAPSICUM) || defined(HAVE_PLEDGE) || defined(__linux__) +#define PRIVSEP_SYSCTL +#endif #endif #define PS_ROOT_FD(ctx) ((ctx)->ps_root ? (ctx)->ps_root->psp_fd : -1) #if !defined(DISABLE_SECCOMP) && defined(__linux__) -# include -# if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) -# define HAVE_SECCOMP -# endif +#include +#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0) +#define HAVE_SECCOMP +#endif #endif -#include "config.h" #include "arp.h" +#include "config.h" #include "dhcp.h" #include "dhcpcd.h" @@ -142,8 +139,8 @@ struct ps_addr { struct in_addr psau_in_addr; struct in6_addr psau_in6_addr; } psa_u; -#define psa_in_addr psa_u.psau_in_addr -#define psa_in6_addr psa_u.psau_in6_addr +#define psa_in_addr psa_u.psau_in_addr +#define psa_in6_addr psa_u.psau_in6_addr }; /* Uniquely identify a process */ @@ -214,10 +211,10 @@ int ps_managersandbox(struct dhcpcd_ctx *, const char *); ssize_t ps_daemonised(struct dhcpcd_ctx *); int ps_unrollmsg(struct msghdr *, struct ps_msghdr *, const void *, size_t); -ssize_t ps_sendpsmmsg(struct dhcpcd_ctx *, int, - struct ps_msghdr *, const struct msghdr *); -ssize_t ps_sendpsmdata(struct dhcpcd_ctx *, int, - struct ps_msghdr *, const void *, size_t); +ssize_t ps_sendpsmmsg(struct dhcpcd_ctx *, int, struct ps_msghdr *, + const struct msghdr *); +ssize_t ps_sendpsmdata(struct dhcpcd_ctx *, int, struct ps_msghdr *, + const void *, size_t); ssize_t ps_sendmsg(struct dhcpcd_ctx *, int, uint16_t, unsigned long, const struct msghdr *); ssize_t ps_sendcmd(struct dhcpcd_ctx *, int, uint16_t, unsigned long, @@ -229,7 +226,7 @@ ssize_t ps_recvpsmsg(struct dhcpcd_ctx *, int, unsigned short, ssize_t (*callback)(void *, struct ps_msghdr *, struct msghdr *), void *); /* Internal privsep functions. */ -int ps_setbuf_fdpair(int []); +int ps_setbuf_fdpair(int[]); #ifdef PRIVSEP_RIGHTS int ps_rights_limit_ioctl(int); @@ -237,7 +234,7 @@ int ps_rights_limit_fd_fctnl(int); int ps_rights_limit_fd_rdonly(int); int ps_rights_limit_fd_sockopt(int); int ps_rights_limit_fd(int); -int ps_rights_limit_fdpair(int []); +int ps_rights_limit_fdpair(int[]); #endif #ifdef HAVE_SECCOMP @@ -247,8 +244,7 @@ int ps_seccomp_enter(void); pid_t ps_startprocess(struct ps_process *, void (*recv_msg)(void *, unsigned short), void (*recv_unpriv_msg)(void *, unsigned short), - int (*callback)(struct ps_process *), - unsigned int); + int (*callback)(struct ps_process *), unsigned int); int ps_stopprocess(struct ps_process *); struct ps_process *ps_findprocess(struct dhcpcd_ctx *, struct ps_id *); struct ps_process *ps_findprocesspid(struct dhcpcd_ctx *, pid_t); diff --git a/src/queue.h b/src/queue.h index d83fc62c..8ae181bf 100644 --- a/src/queue.h +++ b/src/queue.h @@ -9,7 +9,7 @@ #include /* Dragonfly BSD needs this :( */ #if !defined(TAILQ_FOREACH_SAFE) && defined(TAILQ_FOREACH_MUTABLE) -#define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE +#define TAILQ_FOREACH_SAFE TAILQ_FOREACH_MUTABLE #endif #else #include "../vendor/queue.h" diff --git a/src/route.c b/src/route.c index 35c26110..3a85efc9 100644 --- a/src/route.c +++ b/src/route.c @@ -36,11 +36,11 @@ #include #include -#include "config.h" #include "common.h" +#include "config.h" #include "dhcpcd.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4.h" #include "ipv4ll.h" #include "ipv6.h" @@ -54,14 +54,12 @@ #define RB_TREE_NEXT(T, N) rb_tree_iterate((T), (N), RB_DIR_RIGHT) #define RB_TREE_PREV(T, N) rb_tree_iterate((T), (N), RB_DIR_LEFT) #endif -#define RB_TREE_FOREACH_SAFE(N, T, S) \ - for ((N) = RB_TREE_MIN(T); \ - (N) && ((S) = RB_TREE_NEXT((T), (N)), 1); \ - (N) = (S)) -#define RB_TREE_FOREACH_REVERSE_SAFE(N, T, S) \ - for ((N) = RB_TREE_MAX(T); \ - (N) && ((S) = RB_TREE_PREV((T), (N)), 1); \ - (N) = (S)) +#define RB_TREE_FOREACH_SAFE(N, T, S) \ + for ((N) = RB_TREE_MIN(T); (N) && ((S) = RB_TREE_NEXT((T), (N)), 1); \ + (N) = (S)) +#define RB_TREE_FOREACH_REVERSE_SAFE(N, T, S) \ + for ((N) = RB_TREE_MAX(T); (N) && ((S) = RB_TREE_PREV((T), (N)), 1); \ + (N) = (S)) #endif /* @@ -69,7 +67,7 @@ * If we change the route, we want to flush anything dynamically created. */ #if defined(BSD) && !defined(RTF_CLONING) && defined(RTF_CONNECTED) -#define RTF_CLONING RTF_CONNECTED +#define RTF_CLONING RTF_CONNECTED #endif #ifdef RT_FREE_ROUTE_TABLE_STATS @@ -80,8 +78,8 @@ static size_t mroutes; #endif static void -rt_maskedaddr(struct sockaddr *dst, - const struct sockaddr *addr, const struct sockaddr *netmask) +rt_maskedaddr(struct sockaddr *dst, const struct sockaddr *addr, + const struct sockaddr *netmask) { const char *addrp = addr->sa_data, *netmaskp = netmask->sa_data; char *dstp = dst->sa_data; @@ -114,7 +112,6 @@ rt_maskedaddr(struct sockaddr *dst, static int rt_cmp_netmask(const struct rt *rt1, const struct rt *rt2) { - if (rt1->rt_flags & RTF_HOST && rt2->rt_flags & RTF_HOST) return 0; return sa_cmp(&rt1->rt_netmask, &rt2->rt_netmask); @@ -195,7 +192,8 @@ rt_compare_proto(void *context, const void *node1, const void *node2) /* IPv4LL routes always come last */ if (rt1->rt_dflags & RTDF_IPV4LL && !(rt2->rt_dflags & RTDF_IPV4LL)) return -1; - else if (!(rt1->rt_dflags & RTDF_IPV4LL) && rt2->rt_dflags & RTDF_IPV4LL) + else if (!(rt1->rt_dflags & RTDF_IPV4LL) && + rt2->rt_dflags & RTDF_IPV4LL) return 1; #endif @@ -233,7 +231,6 @@ const rb_tree_ops_t rt_compare_proto_ops = { static int rt_compare_free(__unused void *context, const void *node1, const void *node2) { - return node1 == node2 ? 0 : node1 < node2 ? -1 : 1; } @@ -248,7 +245,6 @@ static const rb_tree_ops_t rt_compare_free_ops = { void rt_init(struct dhcpcd_ctx *ctx) { - rb_tree_init(&ctx->routes, &rt_compare_os_ops); #ifdef RT_FREE_ROUTE_TABLE rb_tree_init(&ctx->froutes, &rt_compare_free_ops); @@ -258,7 +254,6 @@ rt_init(struct dhcpcd_ctx *ctx) bool rt_is_default(const struct rt *rt) { - return sa_is_unspecified(&rt->rt_dest) && sa_is_unspecified(&rt->rt_netmask); } @@ -282,28 +277,25 @@ rt_desc(int loglevel, const char *cmd, const struct rt *rt) if (rt->rt_flags & RTF_HOST) { if (gateway_unspec) - logmessage(loglevel, "%s: %s host route to %s", - ifname, cmd, dest); + logmessage(loglevel, "%s: %s host route to %s", ifname, + cmd, dest); else logmessage(loglevel, "%s: %s host route to %s via %s", ifname, cmd, dest, gateway); } else if (rt_is_default(rt)) { if (gateway_unspec) - logmessage(loglevel, "%s: %s default route", - ifname, cmd); + logmessage(loglevel, "%s: %s default route", ifname, + cmd); else logmessage(loglevel, "%s: %s default route via %s", ifname, cmd, gateway); } else if (gateway_unspec) - logmessage(loglevel, "%s: %s%s route to %s/%d", - ifname, cmd, - rt->rt_flags & RTF_REJECT ? " reject" : "", - dest, prefix); + logmessage(loglevel, "%s: %s%s route to %s/%d", ifname, cmd, + rt->rt_flags & RTF_REJECT ? " reject" : "", dest, prefix); else - logmessage(loglevel, "%s: %s%s route to %s/%d via %s", - ifname, cmd, - rt->rt_flags & RTF_REJECT ? " reject" : "", - dest, prefix, gateway); + logmessage(loglevel, "%s: %s%s route to %s/%d via %s", ifname, + cmd, rt->rt_flags & RTF_REJECT ? " reject" : "", dest, + prefix, gateway); } void @@ -318,9 +310,9 @@ rt_headclear0(struct dhcpcd_ctx *ctx, rb_tree_t *rts, int af) assert(&ctx->froutes != rts); #endif - RB_TREE_FOREACH_SAFE(rt, rts, rtn) { - if (af != AF_UNSPEC && - rt->rt_dest.sa_family != af && + RB_TREE_FOREACH_SAFE(rt, rts, rtn) + { + if (af != AF_UNSPEC && rt->rt_dest.sa_family != af && rt->rt_gateway.sa_family != af) continue; rb_tree_remove_node(rts, rt); @@ -352,7 +344,6 @@ rt_headfree(rb_tree_t *rts) void rt_dispose(struct dhcpcd_ctx *ctx) { - assert(ctx != NULL); rt_headfree(&ctx->routes); #ifdef RT_FREE_ROUTE_TABLE @@ -380,7 +371,7 @@ rt_new0(struct dhcpcd_ctx *ctx) #endif } else #endif - if ((rt = malloc(sizeof(*rt))) == NULL) { + if ((rt = malloc(sizeof(*rt))) == NULL) { logerr(__func__); return NULL; } @@ -391,7 +382,6 @@ rt_new0(struct dhcpcd_ctx *ctx) void rt_setif(struct rt *rt, struct interface *ifp) { - assert(rt != NULL); assert(ifp != NULL); rt->rt_ifp = ifp; @@ -417,7 +407,6 @@ rt_new(struct interface *ifp) struct rt * rt_proto_add_ctx(rb_tree_t *tree, struct rt *rt, struct dhcpcd_ctx *ctx) { - rt->rt_order = ctx->rt_order++; if (rb_tree_insert_node(tree, rt) == rt) return rt; @@ -430,8 +419,7 @@ rt_proto_add_ctx(rb_tree_t *tree, struct rt *rt, struct dhcpcd_ctx *ctx) struct rt * rt_proto_add(rb_tree_t *tree, struct rt *rt) { - - assert (rt->rt_ifp != NULL); + assert(rt->rt_ifp != NULL); return rt_proto_add_ctx(tree, rt, rt->rt_ifp->ctx); } @@ -469,7 +457,8 @@ rt_freeif(struct interface *ifp) if (ifp == NULL) return; ctx = ifp->ctx; - RB_TREE_FOREACH_SAFE(rt, &ctx->routes, rtn) { + RB_TREE_FOREACH_SAFE(rt, &ctx->routes, rtn) + { if (rt->rt_ifp == ifp) { rb_tree_remove_node(&ctx->routes, rt); rt_free(rt); @@ -491,7 +480,7 @@ rt_recvrt(int cmd, const struct rt *rt, pid_t pid) ctx = rt->rt_ifp->ctx; - switch(cmd) { + switch (cmd) { case RTM_DELETE: f = rb_tree_find_node(&ctx->routes, rt); if (f != NULL) { @@ -580,15 +569,14 @@ rt_add(rb_tree_t *kroutes, struct rt *nrt, struct rt *ort) * and get the VPN to inject the default route into dhcpcd somehow. */ if (((nrt->rt_ifp->active && - !(nrt->rt_ifp->options->options & DHCPCD_GATEWAY)) || - (!nrt->rt_ifp->active && !(ctx->options & DHCPCD_GATEWAY))) && + !(nrt->rt_ifp->options->options & DHCPCD_GATEWAY)) || + (!nrt->rt_ifp->active && !(ctx->options & DHCPCD_GATEWAY))) && sa_is_unspecified(&nrt->rt_dest) && sa_is_unspecified(&nrt->rt_netmask)) return false; krt = rb_tree_find_node(kroutes, nrt); - if (krt != NULL && - krt->rt_ifp == nrt->rt_ifp && + if (krt != NULL && krt->rt_ifp == nrt->rt_ifp && /* Only test flags dhcpcd controls */ (krt->rt_flags & (RTF_HOST | RTF_REJECT)) == nrt->rt_flags && #ifdef HAVE_ROUTE_METRIC @@ -597,8 +585,7 @@ rt_add(rb_tree_t *kroutes, struct rt *nrt, struct rt *ort) sa_cmp(&krt->rt_dest, &nrt->rt_dest) == 0 && rt_cmp_netmask(krt, nrt) == 0 && sa_cmp(&krt->rt_gateway, &nrt->rt_gateway) == 0 && - (nrt->rt_ifp->flags & IFF_LOOPBACK || rt_cmp_mtu(krt, nrt) == 0)) - { + (nrt->rt_ifp->flags & IFF_LOOPBACK || rt_cmp_mtu(krt, nrt) == 0)) { #ifdef HAVE_ROUTE_LIFETIME if (rt_cmp_lifetime(krt, nrt) == 0) { rt_desc(LOG_DEBUG, "keeping", krt); @@ -624,7 +611,8 @@ rt_add(rb_tree_t *kroutes, struct rt *nrt, struct rt *ort) #endif /* Reject routes have a gateway, non reject routes don't. * BSD kernels at least preserve RTF_GATEWAY so we need to punt it. */ - if (change && krt->rt_flags & RTF_REJECT && !(nrt->rt_flags & RTF_REJECT)) + if (change && krt->rt_flags & RTF_REJECT && + !(nrt->rt_flags & RTF_REJECT)) change = false; if (change) { @@ -710,7 +698,6 @@ rt_delete(struct rt *rt) static int rt_cmp(const struct rt *r1, const struct rt *r2) { - if (r1->rt_ifp == r2->rt_ifp && #ifdef HAVE_ROUTE_METRIC r1->rt_metric == r2->rt_metric && @@ -724,7 +711,7 @@ static bool rt_doroute(rb_tree_t *kroutes, struct rt *rt) { struct dhcpcd_ctx *ctx; - struct rt *or; + struct rt * or ; ctx = rt->rt_ifp->ctx; /* Do we already manage it? */ @@ -732,15 +719,13 @@ rt_doroute(rb_tree_t *kroutes, struct rt *rt) if (or != NULL) { if (rt->rt_dflags & RTDF_FAKE) return true; - if (or->rt_dflags & RTDF_FAKE || - rt_cmp(rt, or) != 0 || + if (or->rt_dflags & RTDF_FAKE || rt_cmp(rt, or) != 0 || (rt->rt_ifa.sa_family != AF_UNSPEC && - sa_cmp(&or->rt_ifa, &rt->rt_ifa) != 0) || + sa_cmp(&or->rt_ifa, &rt->rt_ifa) != 0) || #ifdef HAVE_ROUTE_LIFETIME rt_cmp_lifetime(rt, or) != 0 || #endif - rt_cmp_mtu(rt, or) != 0) - { + rt_cmp_mtu(rt, or) != 0) { if (!rt_add(kroutes, rt, or)) return false; } @@ -797,7 +782,8 @@ rt_build(struct dhcpcd_ctx *ctx, int af) ctx->rt_missfilterlen = 0; #endif - RB_TREE_FOREACH_SAFE(rt, &routes, rtn) { + RB_TREE_FOREACH_SAFE(rt, &routes, rtn) + { if (rt->rt_ifp->active) { if (!(rt->rt_ifp->options->options & DHCPCD_CONFIGURE)) continue; @@ -809,9 +795,9 @@ rt_build(struct dhcpcd_ctx *ctx, int af) logerr("if_missfilter"); #endif if ((rt->rt_dest.sa_family != af && - rt->rt_dest.sa_family != AF_UNSPEC) || + rt->rt_dest.sa_family != AF_UNSPEC) || (rt->rt_gateway.sa_family != af && - rt->rt_gateway.sa_family != AF_UNSPEC)) + rt->rt_gateway.sa_family != AF_UNSPEC)) continue; /* Is this route already in our table? */ if (rb_tree_find_node(&added, rt) != NULL) @@ -833,20 +819,19 @@ rt_build(struct dhcpcd_ctx *ctx, int af) #endif /* Remove old routes we used to manage. */ - RB_TREE_FOREACH_REVERSE_SAFE(rt, &ctx->routes, rtn) { + RB_TREE_FOREACH_REVERSE_SAFE(rt, &ctx->routes, rtn) + { if ((rt->rt_dest.sa_family != af && - rt->rt_dest.sa_family != AF_UNSPEC) || + rt->rt_dest.sa_family != AF_UNSPEC) || (rt->rt_gateway.sa_family != af && - rt->rt_gateway.sa_family != AF_UNSPEC)) + rt->rt_gateway.sa_family != AF_UNSPEC)) continue; rb_tree_remove_node(&ctx->routes, rt); if (rb_tree_find_node(&added, rt) == NULL) { - o = rt->rt_ifp->options ? - rt->rt_ifp->options->options : - ctx->options; - if ((o & - (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != - (DHCPCD_EXITING | DHCPCD_PERSISTENT)) + o = rt->rt_ifp->options ? rt->rt_ifp->options->options : + ctx->options; + if ((o & (DHCPCD_EXITING | DHCPCD_PERSISTENT)) != + (DHCPCD_EXITING | DHCPCD_PERSISTENT)) rt_delete(rt); } rt_free(rt); diff --git a/src/route.h b/src/route.h index 6aae9bcb..d31461c0 100644 --- a/src/route.h +++ b/src/route.h @@ -36,6 +36,7 @@ #endif #include + #include #include @@ -49,31 +50,31 @@ * when dealing with millions of routes. */ #if !defined(RT_FREE_ROUTE_TABLE) -#define RT_FREE_ROUTE_TABLE 1 +#define RT_FREE_ROUTE_TABLE 1 #elif RT_FREE_ROUTE_TABLE == 0 -#undef RT_FREE_ROUTE_TABLE +#undef RT_FREE_ROUTE_TABLE #endif /* Some systems have route metrics. * OpenBSD route priority is not this. */ #ifndef HAVE_ROUTE_METRIC -# if defined(__linux__) -# define HAVE_ROUTE_METRIC 1 -# endif +#if defined(__linux__) +#define HAVE_ROUTE_METRIC 1 +#endif #endif #ifdef __linux__ -# include /* RTA_PREF is only an enum.... */ -# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) -# define HAVE_ROUTE_PREF -# endif -# if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) -# define HAVE_ROUTE_LIFETIME /* For IPv6 routes only */ -# endif +#include /* RTA_PREF is only an enum.... */ +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0) +#define HAVE_ROUTE_PREF +#endif +#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) +#define HAVE_ROUTE_LIFETIME /* For IPv6 routes only */ +#endif #endif -#if defined(__OpenBSD__) || defined (__sun) -# define ROUTE_PER_GATEWAY +#if defined(__OpenBSD__) || defined(__sun) +#define ROUTE_PER_GATEWAY /* XXX dhcpcd doesn't really support this yet. * But that's generally OK if only dhcpcd is managing routes. */ #endif @@ -84,51 +85,51 @@ #endif struct rt { - union sa_ss rt_ss_dest; -#define rt_dest rt_ss_dest.sa - union sa_ss rt_ss_netmask; -#define rt_netmask rt_ss_netmask.sa - union sa_ss rt_ss_gateway; -#define rt_gateway rt_ss_gateway.sa - struct interface *rt_ifp; - union sa_ss rt_ss_ifa; -#define rt_ifa rt_ss_ifa.sa - unsigned int rt_flags; - unsigned int rt_mtu; + union sa_ss rt_ss_dest; +#define rt_dest rt_ss_dest.sa + union sa_ss rt_ss_netmask; +#define rt_netmask rt_ss_netmask.sa + union sa_ss rt_ss_gateway; +#define rt_gateway rt_ss_gateway.sa + struct interface *rt_ifp; + union sa_ss rt_ss_ifa; +#define rt_ifa rt_ss_ifa.sa + unsigned int rt_flags; + unsigned int rt_mtu; #ifdef HAVE_ROUTE_METRIC - unsigned int rt_metric; + unsigned int rt_metric; #endif /* Maximum interface index is generally USHORT_MAX or 65535. * Add some padding for other stuff and we get offsets for the * below that should work automatically. * This is only an issue if the user defines higher metrics in * their configuration, but then they might wish to override also. */ -#define RTMETRIC_BASE 1000U -#define RTMETRIC_WIRELESS 2000U -#define RTMETRIC_IPV4LL 1000000U -#define RTMETRIC_ROAM 2000000U +#define RTMETRIC_BASE 1000U +#define RTMETRIC_WIRELESS 2000U +#define RTMETRIC_IPV4LL 1000000U +#define RTMETRIC_ROAM 2000000U #ifdef HAVE_ROUTE_PREF - int rt_pref; + int rt_pref; #endif #define RTPREF_HIGH 1 -#define RTPREF_MEDIUM 0 /* has to be zero */ +#define RTPREF_MEDIUM 0 /* has to be zero */ #define RTPREF_LOW (-1) -#define RTPREF_RESERVED (-2) -#define RTPREF_INVALID (-3) /* internal */ - unsigned int rt_dflags; -#define RTDF_IFA_ROUTE 0x01 /* Address generated route */ -#define RTDF_FAKE 0x02 /* Maybe us on lease reboot */ -#define RTDF_IPV4LL 0x04 /* IPv4LL route */ -#define RTDF_RA 0x08 /* Router Advertisement */ -#define RTDF_DHCP 0x10 /* DHCP route */ -#define RTDF_STATIC 0x20 /* Configured in dhcpcd */ -#define RTDF_GATELINK 0x40 /* Gateway is on link */ - size_t rt_order; - rb_node_t rt_tree; +#define RTPREF_RESERVED (-2) +#define RTPREF_INVALID (-3) /* internal */ + unsigned int rt_dflags; +#define RTDF_IFA_ROUTE 0x01 /* Address generated route */ +#define RTDF_FAKE 0x02 /* Maybe us on lease reboot */ +#define RTDF_IPV4LL 0x04 /* IPv4LL route */ +#define RTDF_RA 0x08 /* Router Advertisement */ +#define RTDF_DHCP 0x10 /* DHCP route */ +#define RTDF_STATIC 0x20 /* Configured in dhcpcd */ +#define RTDF_GATELINK 0x40 /* Gateway is on link */ + size_t rt_order; + rb_node_t rt_tree; #ifdef HAVE_ROUTE_LIFETIME - struct timespec rt_aquired; /* timestamp of aquisition */ - uint32_t rt_lifetime; /* current lifetime of route */ -#define RTLIFETIME_DEV_MAX 2 /* max deviation for cmp */ + struct timespec rt_aquired; /* timestamp of aquisition */ + uint32_t rt_lifetime; /* current lifetime of route */ +#define RTLIFETIME_DEV_MAX 2 /* max deviation for cmp */ #endif }; @@ -143,11 +144,11 @@ bool rt_is_default(const struct rt *); void rt_headclear0(struct dhcpcd_ctx *, rb_tree_t *, int); void rt_headclear(rb_tree_t *, int); void rt_headfreeif(rb_tree_t *); -struct rt * rt_new0(struct dhcpcd_ctx *); +struct rt *rt_new0(struct dhcpcd_ctx *); void rt_setif(struct rt *, struct interface *); -struct rt * rt_new(struct interface *); -struct rt * rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *); -struct rt * rt_proto_add(rb_tree_t *, struct rt *); +struct rt *rt_new(struct interface *); +struct rt *rt_proto_add_ctx(rb_tree_t *, struct rt *, struct dhcpcd_ctx *); +struct rt *rt_proto_add(rb_tree_t *, struct rt *); int rt_cmp_dest(const struct rt *, const struct rt *); void rt_recvrt(int, const struct rt *, pid_t); void rt_build(struct dhcpcd_ctx *, int); diff --git a/src/sa.c b/src/sa.c index 3d184f47..a796d8fa 100644 --- a/src/sa.c +++ b/src/sa.c @@ -26,8 +26,8 @@ * SUCH DAMAGE. */ -#include #include +#include #include #ifdef AF_LINK @@ -40,12 +40,12 @@ #include #include #include -#include #include +#include #include -#include "config.h" #include "common.h" +#include "config.h" #include "sa.h" #ifndef NDEBUG @@ -55,18 +55,17 @@ static bool sa_inprefix; socklen_t sa_addroffset(const struct sockaddr *sa) { - assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { #ifdef INET case AF_INET: return offsetof(struct sockaddr_in, sin_addr) + - offsetof(struct in_addr, s_addr); + offsetof(struct in_addr, s_addr); #endif /* INET */ #ifdef INET6 case AF_INET6: return offsetof(struct sockaddr_in6, sin6_addr) + - offsetof(struct in6_addr, s6_addr); + offsetof(struct in6_addr, s6_addr); #endif /* INET6 */ default: errno = EAFNOSUPPORT; @@ -79,7 +78,7 @@ sa_addrlen(const struct sockaddr *sa) { #define membersize(type, member) sizeof(((type *)0)->member) assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { #ifdef INET case AF_INET: return membersize(struct in_addr, s_addr); @@ -98,7 +97,6 @@ sa_addrlen(const struct sockaddr *sa) socklen_t sa_len(const struct sockaddr *sa) { - switch (sa->sa_family) { #ifdef AF_LINK case AF_LINK: @@ -121,9 +119,8 @@ sa_len(const struct sockaddr *sa) bool sa_is_unspecified(const struct sockaddr *sa) { - assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { case AF_UNSPEC: return true; #ifdef INET @@ -142,8 +139,11 @@ sa_is_unspecified(const struct sockaddr *sa) #ifdef INET6 #ifndef IN6MASK128 -#define IN6MASK128 {{{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }}} +#define IN6MASK128 \ + { \ + {{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, \ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }} \ + } #endif static const struct in6_addr in6allones = IN6MASK128; #endif @@ -151,14 +151,12 @@ static const struct in6_addr in6allones = IN6MASK128; bool sa_is_allones(const struct sockaddr *sa) { - assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { case AF_UNSPEC: return false; #ifdef INET - case AF_INET: - { + case AF_INET: { const struct sockaddr_in *sin; sin = satocsin(sa); @@ -166,8 +164,7 @@ sa_is_allones(const struct sockaddr *sa) } #endif /* INET */ #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { const struct sockaddr_in6 *sin6; sin6 = satocsin6(sa); @@ -183,14 +180,12 @@ sa_is_allones(const struct sockaddr *sa) bool sa_is_loopback(const struct sockaddr *sa) { - assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { case AF_UNSPEC: return false; #ifdef INET - case AF_INET: - { + case AF_INET: { const struct sockaddr_in *sin; sin = satocsin(sa); @@ -198,8 +193,7 @@ sa_is_loopback(const struct sockaddr *sa) } #endif /* INET */ #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { const struct sockaddr_in6 *sin6; sin6 = satocsin6(sa); @@ -218,10 +212,9 @@ sa_toprefix(const struct sockaddr *sa) int prefix; assert(sa != NULL); - switch(sa->sa_family) { + switch (sa->sa_family) { #ifdef INET - case AF_INET: - { + case AF_INET: { const struct sockaddr_in *sin; uint32_t mask; @@ -231,20 +224,20 @@ sa_toprefix(const struct sockaddr *sa) break; } mask = ntohl(sin->sin_addr.s_addr); - prefix = 33 - ffs((int)mask); /* 33 - (1 .. 32) -> 32 .. 1 */ - if (prefix < 32) { /* more than 1 bit in mask */ + prefix = 33 - ffs((int)mask); /* 33 - (1 .. 32) -> 32 .. 1 */ + if (prefix < 32) { /* more than 1 bit in mask */ /* check for non-contig netmask */ - if ((mask^(((1U << prefix)-1) << (32 - prefix))) != 0) { + if ((mask ^ (((1U << prefix) - 1) << (32 - prefix))) != + 0) { errno = EINVAL; - return -1; /* noncontig, no pfxlen */ + return -1; /* noncontig, no pfxlen */ } } break; } #endif #ifdef INET6 - case AF_INET6: - { + case AF_INET6: { const struct sockaddr_in6 *sin6; int x, y; const uint8_t *lim, *p; @@ -314,7 +307,7 @@ ipbytes_fromprefix(uint8_t *ap, int prefix, int max_prefix) uint8_t a; a = 0xff; - a = (uint8_t)(a << (8 - bits)); + a = (uint8_t)(a << (8 - bits)); *ap++ = a; } bytes = (max_prefix - prefix) / NBBY; @@ -457,8 +450,7 @@ sa_cmp(const struct sockaddr *sa1, const struct sockaddr *sa2) break; } - return memcmp((const char *)sa1 + offset, - (const char *)sa2 + offset, + return memcmp((const char *)sa1 + offset, (const char *)sa2 + offset, len); } diff --git a/src/sa.h b/src/sa.h index d303b994..f4082c27 100644 --- a/src/sa.h +++ b/src/sa.h @@ -30,12 +30,13 @@ #define SA_H #include + #include union sa_ss { - struct sockaddr sa; - struct sockaddr_in sin; - struct sockaddr_in6 sin6; + struct sockaddr sa; + struct sockaddr_in sin; + struct sockaddr_in6 sin6; }; #ifdef BSD @@ -43,21 +44,21 @@ union sa_ss { #endif /* Allow for a sockaddr_dl being printed too. */ -#define INET_MAX_ADDRSTRLEN (20 * 3) +#define INET_MAX_ADDRSTRLEN (20 * 3) #ifdef INET -#define satosin(sa) ((struct sockaddr_in *)(void *)(sa)) +#define satosin(sa) ((struct sockaddr_in *)(void *)(sa)) #define satocsin(sa) ((const struct sockaddr_in *)(const void *)(sa)) #endif #ifdef INET6 -#define satosin6(sa) ((struct sockaddr_in6 *)(void *)(sa)) +#define satosin6(sa) ((struct sockaddr_in6 *)(void *)(sa)) #define satocsin6(sa) ((const struct sockaddr_in6 *)(const void *)(sa)) #endif socklen_t sa_addroffset(const struct sockaddr *sa); socklen_t sa_addrlen(const struct sockaddr *sa); #ifdef HAVE_SA_LEN -#define sa_len(sa) ((sa)->sa_len) +#define sa_len(sa) ((sa)->sa_len) #else socklen_t sa_len(const struct sockaddr *sa); #endif diff --git a/src/script.c b/src/script.c index 51e7ccf2..ea69dab9 100644 --- a/src/script.c +++ b/src/script.c @@ -31,8 +31,8 @@ #include #include -#include +#include #include #include #include @@ -44,43 +44,32 @@ #include #include -#include "config.h" #include "common.h" +#include "config.h" #include "dhcp.h" #include "dhcp6.h" #include "eloop.h" -#include "if.h" #include "if-options.h" +#include "if.h" #include "ipv4ll.h" #include "ipv6nd.h" #include "logerr.h" #include "privsep.h" #include "script.h" -#define DEFAULT_PATH "/usr/bin:/usr/sbin:/bin:/sbin" - -static const char * const if_params[] = { - "interface", - "protocol", - "reason", - "pid", - "ifcarrier", - "ifmetric", - "ifwireless", - "ifflags", - "ssid", - "profile", - "interface_order", - NULL -}; - -static const char * true_str = "true"; -static const char * false_str = "false"; +#define DEFAULT_PATH "/usr/bin:/usr/sbin:/bin:/sbin" + +static const char *const if_params[] = { "interface", "protocol", "reason", + "pid", "ifcarrier", "ifmetric", "ifwireless", "ifflags", "ssid", + "profile", "interface_order", NULL }; + +static const char *true_str = "true"; +static const char *false_str = "false"; void if_printoptions(void) { - const char * const *p; + const char *const *p; for (p = if_params; *p; p++) printf(" - %s\n", *p); @@ -144,20 +133,14 @@ append_config(FILE *fp, const char *prefix, const char *const *config) #endif -#define PROTO_LINK 0 -#define PROTO_DHCP 1 -#define PROTO_IPV4LL 2 -#define PROTO_RA 3 -#define PROTO_DHCP6 4 -#define PROTO_STATIC6 5 -static const char *protocols[] = { - "link", - "dhcp", - "ipv4ll", - "ra", - "dhcp6", - "static6" -}; +#define PROTO_LINK 0 +#define PROTO_DHCP 1 +#define PROTO_IPV4LL 2 +#define PROTO_RA 3 +#define PROTO_DHCP6 4 +#define PROTO_STATIC6 5 +static const char *protocols[] = { "link", "dhcp", "ipv4ll", "ra", "dhcp6", + "static6" }; int efprintf(FILE *fp, const char *fmt, ...) @@ -277,7 +260,7 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, /* Needed for scripts */ path = getenv("PATH"); if (efprintf(fp, "PATH=%s", - path == NULL ? DEFAULT_PATH : path) == -1) + path == NULL ? DEFAULT_PATH : path) == -1) goto eexit; if (efprintf(fp, "pid=%d", (int)getpid()) == -1) goto eexit; @@ -334,8 +317,7 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, strcmp(reason, "CARRIER") == 0 || strcmp(reason, "NOCARRIER") == 0 || strcmp(reason, "NOCARRIER_ROAMING") == 0 || - strcmp(reason, "UNKNOWN") == 0 || - strcmp(reason, "DEPARTED") == 0 || + strcmp(reason, "UNKNOWN") == 0 || strcmp(reason, "DEPARTED") == 0 || strcmp(reason, "STOPPED") == 0) protocol = PROTO_LINK; #ifdef INET @@ -351,19 +333,20 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, if (efprintf(fp, "interface=%s", ifp->name) == -1) goto eexit; if (protocols[protocol] != NULL) { - if (efprintf(fp, "protocol=%s", - protocols[protocol]) == -1) + if (efprintf(fp, "protocol=%s", protocols[protocol]) == + -1) goto eexit; } } if (ifp->ctx->options & DHCPCD_DUMPLEASE && protocol != PROTO_LINK) goto dumplease; if (efprintf(fp, "if_configured=%s", - ifo->options & DHCPCD_CONFIGURE ? "true" : "false") == -1) + ifo->options & DHCPCD_CONFIGURE ? "true" : "false") == -1) goto eexit; if (efprintf(fp, "ifcarrier=%s", - ifp->carrier == LINK_UNKNOWN ? "unknown" : - ifp->carrier == LINK_UP ? "up" : "down") == -1) + ifp->carrier == LINK_UNKNOWN ? "unknown" : + ifp->carrier == LINK_UP ? "up" : + "down") == -1) goto eexit; if (efprintf(fp, "ifmetric=%d", ifp->metric) == -1) goto eexit; @@ -376,9 +359,8 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, if (ifp->wireless) { char pssid[IF_SSIDLEN * 4]; - if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, - ifp->ssid, ifp->ssid_len) != -1) - { + if (print_string(pssid, sizeof(pssid), OT_ESCSTRING, ifp->ssid, + ifp->ssid_len) != -1) { if (efprintf(fp, "ifssid=%s", pssid) == -1) goto eexit; } @@ -403,9 +385,9 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, } if (fprintf(fp, "interface_order=") == -1) goto eexit; - RB_TREE_FOREACH(rt, &ifaces) { - if (rt != RB_TREE_MIN(&ifaces) && - fprintf(fp, "%s", " ") == -1) + RB_TREE_FOREACH(rt, &ifaces) + { + if (rt != RB_TREE_MIN(&ifaces) && fprintf(fp, "%s", " ") == -1) goto eexit; if (fprintf(fp, "%s", rt->rt_ifp->name) == -1) goto eexit; @@ -418,11 +400,8 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, if_up = false_str; if_down = ifo->options & DHCPCD_RELEASE ? true_str : false_str; } else if (strcmp(reason, "TEST") == 0 || - strcmp(reason, "PREINIT") == 0 || - strcmp(reason, "CARRIER") == 0 || - strcmp(reason, "STOP") == 0 || - strcmp(reason, "UNKNOWN") == 0) - { + strcmp(reason, "PREINIT") == 0 || strcmp(reason, "CARRIER") == 0 || + strcmp(reason, "STOP") == 0 || strcmp(reason, "UNKNOWN") == 0) { if_up = false_str; if_down = false_str; } else if (strcmp(reason, "NOCARRIER") == 0) { @@ -445,8 +424,7 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, #endif || (protocol == PROTO_RA && ipv6nd_hasra(ifp)) #endif - ) - { + ) { if_up = true_str; if_down = false_str; } else { @@ -478,18 +456,17 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, } #ifdef INET if (protocol == PROTO_DHCP && state && state->old) { - if (dhcp_env(fp, "old", ifp, - state->old, state->old_len) == -1) + if (dhcp_env(fp, "old", ifp, state->old, state->old_len) == -1) goto eexit; if (append_config(fp, "old", - (const char *const *)ifo->config) == -1) + (const char *const *)ifo->config) == -1) goto eexit; } #endif #ifdef DHCP6 if (protocol == PROTO_DHCP6 && d6_state && d6_state->old) { - if (dhcp6_env(fp, "old", ifp, - d6_state->old, d6_state->old_len) == -1) + if (dhcp6_env(fp, "old", ifp, d6_state->old, + d6_state->old_len) == -1) goto eexit; } #endif @@ -503,11 +480,10 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, } #endif if (protocol == PROTO_DHCP && state && state->new) { - if (dhcp_env(fp, "new", ifp, - state->new, state->new_len) == -1) + if (dhcp_env(fp, "new", ifp, state->new, state->new_len) == -1) goto eexit; if (append_config(fp, "new", - (const char *const *)ifo->config) == -1) + (const char *const *)ifo->config) == -1) goto eexit; } #endif @@ -518,8 +494,8 @@ make_env(struct dhcpcd_ctx *ctx, const struct interface *ifp, } #ifdef DHCP6 if (protocol == PROTO_DHCP6 && D6_STATE_RUNNING(ifp)) { - if (dhcp6_env(fp, "new", ifp, - d6_state->new, d6_state->new_len) == -1) + if (dhcp6_env(fp, "new", ifp, d6_state->new, + d6_state->new_len) == -1) goto eexit; } #endif @@ -603,7 +579,7 @@ send_interface(struct fd_list *fd, const struct interface *ifp, int af) #endif #ifndef AF_LINK -#define AF_LINK AF_PACKET +#define AF_LINK AF_PACKET #endif if (af == AF_UNSPEC || af == AF_LINK) { @@ -660,8 +636,8 @@ send_interface(struct fd_list *fd, const struct interface *ifp, int af) } if (RS_STATE_RUNNING(ifp)) { if (fd != NULL) { - if (send_interface1(fd, ifp, - "ROUTERADVERT") == -1) + if (send_interface1(fd, ifp, "ROUTERADVERT") == + -1) retval = -1; } else retval++; @@ -685,14 +661,13 @@ send_interface(struct fd_list *fd, const struct interface *ifp, int af) static int script_status(const char *script, int status) { - if (WIFEXITED(status)) { if (WEXITSTATUS(status)) - logerrx("%s: %s: WEXITSTATUS %d", - __func__, script, WEXITSTATUS(status)); + logerrx("%s: %s: WEXITSTATUS %d", __func__, script, + WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) - logerrx("%s: %s: %s", - __func__, script, strsignal(WTERMSIG(status))); + logerrx("%s: %s: %s", __func__, script, + strsignal(WTERMSIG(status))); return WEXITSTATUS(status); } @@ -752,8 +727,7 @@ script_runreason(const struct interface *ifp, const char *reason) struct fd_list *fd; long buflen; - if (ctx->script == NULL && - TAILQ_FIRST(&ifp->ctx->control_fds) == NULL) + if (ctx->script == NULL && TAILQ_FIRST(&ifp->ctx->control_fds) == NULL) return 0; /* Make our env */ @@ -793,7 +767,8 @@ script_runreason(const struct interface *ifp, const char *reason) TAILQ_FOREACH(fd, &ctx->control_fds, next) { if (!(fd->flags & FD_LISTEN)) continue; - if (control_queue(fd, ctx->script_buf, ctx->script_buflen)== -1) + if (control_queue(fd, ctx->script_buf, ctx->script_buflen) == + -1) logerr("%s: control_queue", __func__); else status = 1; diff --git a/src/script.h b/src/script.h index e5e1d7a4..81bbadf1 100644 --- a/src/script.h +++ b/src/script.h @@ -33,7 +33,7 @@ __printflike(2, 3) int efprintf(FILE *, const char *, ...); void if_printoptions(void); -char ** script_buftoenv(struct dhcpcd_ctx *, char *, size_t); +char **script_buftoenv(struct dhcpcd_ctx *, char *, size_t); pid_t script_exec(char *const *, char *const *); int send_interface(struct fd_list *, const struct interface *, int); int script_dump(const char *, size_t);