From: Roy Marples Date: Thu, 27 Mar 2014 22:14:52 +0000 (+0000) Subject: Add -Wconversion to debug CFLAGS and fix fallout. X-Git-Tag: v6.4.0~124 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34457fe661b5d33b5b5ccc0ca2b4b6dd517ef98e;p=thirdparty%2Fdhcpcd.git Add -Wconversion to debug CFLAGS and fix fallout. --- diff --git a/arp.c b/arp.c index d7292edc..656dbbea 100644 --- a/arp.c +++ b/arp.c @@ -46,7 +46,7 @@ #define ARP_LEN \ (sizeof(struct arphdr) + (2 * sizeof(uint32_t)) + (2 * HWADDR_LEN)) -static int +static ssize_t arp_send(const struct interface *ifp, int op, in_addr_t sip, in_addr_t tip) { uint8_t arp_buffer[ARP_LEN]; @@ -61,15 +61,15 @@ arp_send(const struct interface *ifp, int op, in_addr_t sip, in_addr_t tip) ar.ar_op = htons(op); p = arp_buffer; - len = sizeof(arp_buffer); + len = 0; #define CHECK(fun, b, l) \ do { \ - if (len < (l)) \ + if (len + (l) > sizeof(arp_buffer)) \ goto eexit; \ fun(p, (b), (l)); \ p += (l); \ - len -= (l); \ + len += (l); \ } while (/* CONSTCOND */ 0) #define APPEND(b, l) CHECK(memcpy, b, l) #define ZERO(l) CHECK(memset, 0, l) @@ -79,7 +79,6 @@ arp_send(const struct interface *ifp, int op, in_addr_t sip, in_addr_t tip) APPEND(&sip, sizeof(sip)); ZERO(ifp->hwlen); APPEND(&tip, sizeof(tip)); - len = p - arp_buffer; return ipv4_sendrawpacket(ifp, ETHERTYPE_ARP, arp_buffer, len); eexit: diff --git a/auth.c b/auth.c index 9aeff931..c33e565f 100644 --- a/auth.c +++ b/auth.c @@ -51,7 +51,7 @@ htonll(uint64_t x) { return (uint64_t)htonl((uint32_t)(x >> 32)) | - (int64_t)htonl((uint32_t)(x & 0xffffffff)) << 32; + (uint64_t)htonl((uint32_t)(x & 0xffffffff)) << 32; } #else /* (BYTE_ORDER == LITTLE_ENDIAN) */ #define htonll(x) (x) @@ -65,7 +65,7 @@ ntohll(uint64_t x) { return (uint64_t)ntohl((uint32_t)(x >> 32)) | - (int64_t)ntohl((uint32_t)(x & 0xffffffff)) << 32; + (uint64_t)ntohl((uint32_t)(x & 0xffffffff)) << 32; } #else /* (BYTE_ORDER == LITTLE_ENDIAN) */ #define ntohll(x) (x) @@ -101,14 +101,14 @@ dhcp_auth_reset(struct authstate *state) */ const struct token * dhcp_auth_validate(struct authstate *state, const struct auth *auth, - const uint8_t *m, unsigned int mlen, int mp, int mt, - const uint8_t *data, unsigned int dlen) + const uint8_t *m, size_t mlen, int mp, int mt, + const uint8_t *data, size_t dlen) { uint8_t protocol, algorithm, rdm, *mm, type; uint64_t replay; uint32_t secretid; const uint8_t *d, *realm; - unsigned int realm_len; + size_t realm_len; const struct token *t; time_t now; uint8_t hmac[HMAC_LENGTH]; @@ -422,7 +422,7 @@ get_next_rdm_monotonic_counter(struct auth *auth) return rdm; } -#define JAN_1970 2208988800UL /* 1970 - 1900 in seconds */ +#define JAN_1970 2208988800U /* 1970 - 1900 in seconds */ static uint64_t get_next_rdm_monotonic_clock(struct auth *auth) { @@ -434,7 +434,7 @@ get_next_rdm_monotonic_clock(struct auth *auth) if (clock_gettime(CLOCK_REALTIME, &ts) != 0) return ++auth->last_replay; /* report error? */ pack[0] = htonl((uint32_t)ts.tv_sec + JAN_1970); - frac = (ts.tv_nsec / 1e9 * 0x100000000ULL); + frac = ((double)ts.tv_nsec / 1e9 * 0x100000000ULL); pack[1] = htonl((uint32_t)frac); memcpy(&rdm, &pack, sizeof(rdm)); @@ -460,10 +460,10 @@ get_next_rdm_monotonic(struct auth *auth) * mt is the DHCP message type. * data and dlen refer to the authentication option within the message. */ -int +ssize_t dhcp_auth_encode(struct auth *auth, const struct token *t, - uint8_t *m, unsigned int mlen, int mp, int mt, - uint8_t *data, unsigned int dlen) + uint8_t *m, size_t mlen, int mp, int mt, + uint8_t *data, size_t dlen) { uint64_t rdm; uint8_t hmac[HMAC_LENGTH]; @@ -542,7 +542,7 @@ dhcp_auth_encode(struct auth *auth, const struct token *t, dlen += sizeof(t->secretid) + sizeof(hmac); break; } - return dlen; + return (ssize_t)dlen; } if (dlen < 1 + 1 + 1 + 8) { @@ -586,12 +586,12 @@ dhcp_auth_encode(struct auth *auth, const struct token *t, return -1; } memcpy(data, t->key, t->key_len); - return dlen - t->key_len; + return (ssize_t)(dlen - t->key_len); } /* DISCOVER or INFORM messages don't write auth info */ if (!info) - return dlen; + return (ssize_t)dlen; /* Loading a saved lease without an authentication option */ if (t == NULL) @@ -656,5 +656,5 @@ dhcp_auth_encode(struct auth *auth, const struct token *t, } /* Done! */ - return dlen - sizeof(hmac); /* should be zero */ + return (int)(dlen - sizeof(hmac)); /* should be zero */ } diff --git a/auth.h b/auth.h index 1ad7e9c8..848569f6 100644 --- a/auth.h +++ b/auth.h @@ -48,9 +48,9 @@ struct token { TAILQ_ENTRY(token) next; uint32_t secretid; - unsigned int realm_len; + size_t realm_len; unsigned char *realm; - unsigned int key_len; + size_t key_len; unsigned char *key; time_t expire; }; @@ -77,10 +77,10 @@ void dhcp_auth_reset(struct authstate *); const struct token * dhcp_auth_validate(struct authstate *, const struct auth *, - const uint8_t *, unsigned int, int, int, - const uint8_t *, unsigned int); + const uint8_t *, size_t, int, int, + const uint8_t *, size_t); -int dhcp_auth_encode(struct auth *, const struct token *, - uint8_t *, unsigned int, int, int, - uint8_t *, unsigned int); +ssize_t dhcp_auth_encode(struct auth *, const struct token *, + uint8_t *, size_t, int, int, + uint8_t *, size_t); #endif diff --git a/bpf.c b/bpf.c index 8ec6dfe7..e7d4a662 100644 --- a/bpf.c +++ b/bpf.c @@ -55,7 +55,8 @@ ipv4_opensocket(struct interface *ifp, int protocol) struct dhcp_state *state; int fd = -1; struct ifreq ifr; - int buf_len = 0; + int ibuf_len = 0; + size_t buf_len; struct bpf_version pv; struct bpf_program pf; #ifdef BIOCIMMEDIATE @@ -92,9 +93,10 @@ ipv4_opensocket(struct interface *ifp, int protocol) goto eexit; /* Get the required BPF buffer length from the kernel. */ - if (ioctl(fd, BIOCGBLEN, &buf_len) == -1) + if (ioctl(fd, BIOCGBLEN, &ibuf_len) == -1) goto eexit; - if (state->buffer_size != (size_t)buf_len) { + buf_len = (size_t)ibuf_len; + if (state->buffer_size != buf_len) { free(state->buffer); state->buffer = malloc(buf_len); if (state->buffer == NULL) @@ -138,7 +140,7 @@ eexit: ssize_t ipv4_sendrawpacket(const struct interface *ifp, int protocol, - const void *data, ssize_t len) + const void *data, size_t len) { struct iovec iov[2]; struct ether_header hw; @@ -164,7 +166,7 @@ ipv4_sendrawpacket(const struct interface *ifp, int protocol, * So we pass the buffer in the API so we can loop on >1 packet. */ ssize_t ipv4_getrawpacket(struct interface *ifp, int protocol, - void *data, ssize_t len, int *partialcsum) + void *data, size_t len, int *partialcsum) { int fd = -1; struct bpf_hdr packet; @@ -188,7 +190,7 @@ ipv4_getrawpacket(struct interface *ifp, int protocol, return errno == EAGAIN ? 0 : -1; else if ((size_t)bytes < sizeof(packet)) return -1; - state->buffer_len = bytes; + state->buffer_len = (size_t)bytes; state->buffer_pos = 0; } bytes = -1; @@ -201,10 +203,10 @@ ipv4_getrawpacket(struct interface *ifp, int protocol, goto next; /* Packet beyond buffer, drop. */ payload = state->buffer + state->buffer_pos + packet.bh_hdrlen + ETHER_HDR_LEN; - bytes = packet.bh_caplen - ETHER_HDR_LEN; - if (bytes > len) - bytes = len; - memcpy(data, payload, bytes); + bytes = (ssize_t)packet.bh_caplen - ETHER_HDR_LEN; + if ((size_t)bytes > len) + bytes = (ssize_t)len; + memcpy(data, payload, (size_t)bytes); next: state->buffer_pos += BPF_WORDALIGN(packet.bh_hdrlen + packet.bh_caplen); diff --git a/common.c b/common.c index 95d1e70f..e8548dd8 100644 --- a/common.c +++ b/common.c @@ -157,15 +157,15 @@ setvar(char ***e, const char *prefix, const char *var, const char *value) else snprintf(**e, len, "%s=%s", var, value); (*e)++; - return len; + return (ssize_t)len; } ssize_t -setvard(char ***e, const char *prefix, const char *var, int value) +setvard(char ***e, const char *prefix, const char *var, size_t value) { char buffer[32]; - snprintf(buffer, sizeof(buffer), "%d", value); + snprintf(buffer, sizeof(buffer), "%zu", value); return setvar(e, prefix, var, buffer); } diff --git a/common.h b/common.h index 43c02803..823a1124 100644 --- a/common.h +++ b/common.h @@ -42,7 +42,8 @@ #define STRINGIFY(a) #a #define TOSTRING(a) STRINGIFY(a) -#define timeval_to_double(tv) ((tv)->tv_sec * 1.0 + (tv)->tv_usec * 1.0e-6) +#define timeval_to_double(tv) \ + ((double)(tv)->tv_sec + (double)((tv)->tv_usec) * 1.0e-6) #define timernorm(tv) do { \ while ((tv)->tv_usec >= 1000000) { \ (tv)->tv_sec++; \ @@ -53,9 +54,9 @@ ms = (tv)->tv_sec * 1000; \ ms += (tv)->tv_usec / 1000; \ } while (0 /* CONSTCOND */); -#define ms_to_tv(tv, ms) do { \ - (tv)->tv_sec = ms / 1000; \ - (tv)->tv_usec = (ms - ((tv)->tv_sec * 1000)) * 1000; \ +#define ms_to_tv(tv, ms) do { \ + (tv)->tv_sec = ms / 1000; \ + (tv)->tv_usec = (suseconds_t)(ms - ((tv)->tv_sec * 1000)) * 1000; \ } while (0 /* CONSTCOND */); #ifndef TIMEVAL_TO_TIMESPEC @@ -104,7 +105,7 @@ const char *get_hostname(char *, size_t, int); extern int clock_monotonic; int get_monotonic(struct timeval *); ssize_t setvar(char ***, const char *, const char *, const char *); -ssize_t setvard(char ***, const char *, const char *, int); +ssize_t setvard(char ***, const char *, const char *, size_t); time_t uptime(void); #endif diff --git a/compat/closefrom.c b/compat/closefrom.c index 6e810577..ab57c77b 100644 --- a/compat/closefrom.c +++ b/compat/closefrom.c @@ -32,7 +32,8 @@ int closefrom(int fd) { - int i, max, r; + long max; + int i, r; #ifdef _SC_OPEN_MAX max = sysconf(_SC_OPEN_MAX); @@ -40,7 +41,9 @@ closefrom(int fd) max = getdtablesize(); #endif r = 0; - for (i = fd; i < max; i++) - r += close(i); + for (i = fd; i < max; i++) { + if (close(i) == -1) + r = -1; + } return r; } diff --git a/compat/strlcpy.c b/compat/strlcpy.c index c2e324e8..47776a05 100644 --- a/compat/strlcpy.c +++ b/compat/strlcpy.c @@ -47,5 +47,6 @@ strlcpy(char *dst, const char *src, size_t size) while (*src++); } - return src - s - 1; + + return (size_t)(src - s - 1); } diff --git a/configure b/configure index ceb291df..7e5bc37a 100755 --- a/configure +++ b/configure @@ -334,9 +334,10 @@ CFLAGS+= -Wnested-externs CFLAGS+= -Winline -Wwrite-strings -Wcast-align -Wcast-qual CFLAGS+= -Wpointer-arith -Wstrict-overflow CFLAGS+= -Wdeclaration-after-statement +CFLAGS+= -Wconversion EOF case "$OS" in - openbsd*) ;; # OpenBSD has many redundant decs in system headers + mirbsd*|openbsd*);; # OpenBSD has many redundant decs in system headers *) echo "CFLAGS+= -Wredundant-decls" >>$CONFIG_MK;; esac fi diff --git a/control.c b/control.c index a39176f9..76c2c6b1 100644 --- a/control.c +++ b/control.c @@ -119,32 +119,32 @@ control_handle(void *arg) close(fd); } -static int +static socklen_t make_sock(struct dhcpcd_ctx *ctx, struct sockaddr_un *sun, const char *ifname) { #ifdef SOCK_CLOEXEC if ((ctx->control_fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) == -1) - return -1; + return 0; #else int flags; if ((ctx->control_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - return -1; + return 0; if ((flags = fcntl(ctx->control_fd, F_GETFD, 0)) == -1 || fcntl(ctx->control_fd, F_SETFD, flags | FD_CLOEXEC) == -1) { close(ctx->control_fd); ctx->control_fd = -1; - return -1; + return 0; } if ((flags = fcntl(ctx->control_fd, F_GETFL, 0)) == -1 || fcntl(ctx->control_fd, F_SETFL, flags | O_NONBLOCK) == -1) { close(ctx->control_fd); ctx->control_fd = -1; - return -1; + return 0; } #endif memset(sun, 0, sizeof(*sun)); @@ -159,9 +159,9 @@ int control_start(struct dhcpcd_ctx *ctx, const char *ifname) { struct sockaddr_un sun; - int len; + socklen_t len; - if ((len = make_sock(ctx, &sun, ifname)) == -1) + if ((len = make_sock(ctx, &sun, ifname)) == 0) return -1; unlink(ctx->control_sock); if (bind(ctx->control_fd, (struct sockaddr *)&sun, len) == -1 || @@ -209,9 +209,9 @@ int control_open(struct dhcpcd_ctx *ctx, const char *ifname) { struct sockaddr_un sun; - int len; + socklen_t len; - if ((len = make_sock(ctx, &sun, ifname)) == -1) + if ((len = make_sock(ctx, &sun, ifname)) == 0) return -1; if (connect(ctx->control_fd, (struct sockaddr *)&sun, len) == -1) { close(ctx->control_fd); @@ -221,28 +221,28 @@ control_open(struct dhcpcd_ctx *ctx, const char *ifname) return 0; } -int +ssize_t control_send(struct dhcpcd_ctx *ctx, int argc, char * const *argv) { - char buffer[1024], *p; + char buffer[1024]; int i; - size_t len; + size_t len, l; if (argc > 255) { errno = ENOBUFS; return -1; } - p = buffer; + len = 0; for (i = 0; i < argc; i++) { - len = strlen(argv[i]) + 1; - if ((p - buffer) + len > sizeof(buffer)) { + l = strlen(argv[i]) + 1; + if (len + l > sizeof(buffer)) { errno = ENOBUFS; return -1; } - memcpy(p, argv[i], len); - p += len; + memcpy(buffer + len, argv[i], l); + len += l; } - return write(ctx->control_fd, buffer, p - buffer); + return write(ctx->control_fd, buffer, len); } void diff --git a/control.h b/control.h index 7a4bb255..3204b7ac 100644 --- a/control.h +++ b/control.h @@ -40,7 +40,7 @@ struct fd_list { int control_start(struct dhcpcd_ctx *, const char *); int control_stop(struct dhcpcd_ctx *); int control_open(struct dhcpcd_ctx *, const char *); -int control_send(struct dhcpcd_ctx *, int, char * const *); +ssize_t control_send(struct dhcpcd_ctx *, int, char * const *); void control_close(struct dhcpcd_ctx *ctx); #endif diff --git a/crypt/crypt.h b/crypt/crypt.h index 0a3932e6..5a127c80 100644 --- a/crypt/crypt.h +++ b/crypt/crypt.h @@ -28,6 +28,6 @@ #ifndef CRYPT_H #define CRYPT_H -void hmac_md5(const uint8_t *, int, const uint8_t *, int, uint8_t *); +void hmac_md5(const uint8_t *, size_t, const uint8_t *, size_t, uint8_t *); #endif diff --git a/crypt/hmac_md5.c b/crypt/hmac_md5.c index 06ea465a..6b780671 100644 --- a/crypt/hmac_md5.c +++ b/crypt/hmac_md5.c @@ -42,8 +42,8 @@ /* hmac_md5 as per RFC3118 */ void -hmac_md5(const uint8_t *text, int text_len, - const uint8_t *key, int key_len, +hmac_md5(const uint8_t *text, size_t text_len, + const uint8_t *key, size_t key_len, uint8_t *digest) { uint8_t k_ipad[HMAC_PAD_LEN], k_opad[HMAC_PAD_LEN]; diff --git a/dhcp-common.c b/dhcp-common.c index 4a043602..25c984af 100644 --- a/dhcp-common.c +++ b/dhcp-common.c @@ -42,7 +42,7 @@ #include "platform.h" struct dhcp_opt * -vivso_find(uint16_t iana_en, const void *arg) +vivso_find(uint32_t iana_en, const void *arg) { const struct interface *ifp; size_t i; @@ -63,19 +63,23 @@ vivso_find(uint16_t iana_en, const void *arg) } size_t -dhcp_vendor(char *str, size_t str_len) +dhcp_vendor(char *str, size_t len) { struct utsname utn; char *p; + int l; if (uname(&utn) != 0) - return snprintf(str, str_len, "%s-%s", PACKAGE, VERSION); + return (size_t)snprintf(str, len, "%s-%s", + PACKAGE, VERSION); p = str; - p += snprintf(str, str_len, + l = snprintf(p, len, "%s-%s:%s-%s:%s", PACKAGE, VERSION, utn.sysname, utn.release, utn.machine); - p += hardware_platform(p, str_len - (p - str)); - return p - str; + p += l; + len -= (size_t)l; + l = hardware_platform(p, len); + return (size_t)(p - str); } int @@ -100,7 +104,7 @@ make_option_mask(const struct dhcp_opt *dopts, size_t dopts_len, match = 1; else { errno = 0; - n = strtol(token, &t, 0); + n = (unsigned int)strtol(token, &t, 0); if (errno == 0 && !*t) if (opt->option == n) match = 1; @@ -160,7 +164,7 @@ encode_rfc1035(const char *src, uint8_t *dst) break; has_dot = 1; if (dst) { - *lp = p - lp - 1; + *lp = (uint8_t)(p - lp - 1); if (*lp == '\0') return len; lp = p++; @@ -171,7 +175,7 @@ encode_rfc1035(const char *src, uint8_t *dst) } if (dst) { - *lp = p - lp - 1; + *lp = (uint8_t)(p - lp - 1); if (has_dot) *p++ = '\0'; } @@ -187,25 +191,27 @@ encode_rfc1035(const char *src, uint8_t *dst) * terminating zero) or zero on error. out may be NULL * to just determine output length. */ ssize_t -decode_rfc3397(char *out, ssize_t len, int pl, const uint8_t *p) +decode_rfc3397(char *out, size_t len, const uint8_t *p, size_t pl) { const char *start; - ssize_t start_len; - const uint8_t *r, *q = p; - int count = 0, l, hops; + size_t start_len, l; + const uint8_t *r, *q = p, *e; + int count = 0, hops; uint8_t ltype; start = out; start_len = len; - while (q - p < pl) { + q = p; + e = p + pl; + while (q < e) { r = NULL; hops = 0; /* Check we are inside our length again in-case * the name isn't fully qualified (ie, not terminated) */ - while (q - p < pl && (l = *q++)) { + while (q < e && (l = (size_t)*q++)) { ltype = l & 0xc0; if (ltype == 0x80 || ltype == 0x40) - return 0; + return -1; else if (ltype == 0xc0) { /* pointer */ l = (l & 0x3f) << 8; l |= *q++; @@ -213,16 +219,20 @@ decode_rfc3397(char *out, ssize_t len, int pl, const uint8_t *p) if (!r) r = q; hops++; - if (hops > 255) - return 0; + if (hops > 255) { + errno = ERANGE; + return -1; + } q = p + l; - if (q - p >= pl) - return 0; + if (q >= e) { + errno = ERANGE; + return -1; + } } else { /* straightforward name segment, add with '.' */ count += l + 1; if (out) { - if ((ssize_t)l + 1 > len) { + if (l + 1 > len) { errno = ENOBUFS; return -1; } @@ -254,7 +264,7 @@ decode_rfc3397(char *out, ssize_t len, int pl, const uint8_t *p) } ssize_t -print_string(char *s, ssize_t len, int dl, const uint8_t *data) +print_string(char *s, size_t len, const uint8_t *data, size_t dl) { uint8_t c; const uint8_t *e, *p; @@ -279,7 +289,7 @@ print_string(char *s, ssize_t len, int dl, const uint8_t *data) return -1; } r = snprintf(s, len, "\\%03o", c); - len -= r; + len -= (size_t)r; bytes += r; s += r; } else @@ -306,7 +316,7 @@ print_string(char *s, ssize_t len, int dl, const uint8_t *data) break; } if (s) { - *s++ = c; + *s++ = (char)c; len--; } bytes++; @@ -335,7 +345,7 @@ dhcp_optlen(const struct dhcp_opt *opt, size_t dl) if (opt->len) { if ((size_t)opt->len > dl) return 0; - return opt->len; + return (size_t)opt->len; } return dl; } @@ -373,7 +383,7 @@ dhcp_optlen(const struct dhcp_opt *opt, size_t dl) #endif ssize_t -print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, +print_option(char *s, size_t len, int type, const uint8_t *data, size_t dl, PO_IFNAME const char *ifname) { const uint8_t *e, *t; @@ -382,45 +392,46 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, uint32_t u32; int32_t s32; struct in_addr addr; - ssize_t bytes = 0; - ssize_t l; + ssize_t bytes = 0, sl; + size_t l; char *tmp; if (type & RFC3397) { - l = decode_rfc3397(NULL, 0, dl, data); - if (l < 1) - return l; + sl = decode_rfc3397(NULL, 0, data, dl); + if (sl == 0 || sl == -1) + return sl; + l = (size_t)sl; tmp = malloc(l); if (tmp == NULL) return -1; - decode_rfc3397(tmp, l, dl, data); - l = print_string(s, len, l - 1, (uint8_t *)tmp); + decode_rfc3397(tmp, l, data, dl); + sl = print_string(s, len, (uint8_t *)tmp, l - 1); free(tmp); - return l; + return sl; } #ifdef INET if (type & RFC3361) { - if ((tmp = decode_rfc3361(dl, data)) == NULL) + if ((tmp = decode_rfc3361(data, dl)) == NULL) return -1; l = strlen(tmp); - l = print_string(s, len, l, (uint8_t *)tmp); + sl = print_string(s, len, (uint8_t *)tmp, l); free(tmp); - return l; + return sl; } if (type & RFC3442) - return decode_rfc3442(s, len, dl, data); + return decode_rfc3442(s, len, data, dl); if (type & RFC5969) - return decode_rfc5969(s, len, dl, data); + return decode_rfc5969(s, len, data, dl); #endif if (type & STRING) { /* Some DHCP servers return NULL strings */ if (*data == '\0') return 0; - return print_string(s, len, dl, data); + return print_string(s, len, data, dl); } if (type & FLAG) { @@ -457,12 +468,12 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, while (data < e) { if (l) l++; /* space */ - dl = ipv6_printaddr(NULL, 0, data, ifname); - if (dl != -1) - l += dl; + sl = ipv6_printaddr(NULL, 0, data, ifname); + if (sl != -1) + l += (size_t)sl; data += 16; } - return l + 1; + return (ssize_t)(l + 1); } #endif else if (type & BINHEX) { @@ -471,7 +482,7 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, errno = EINVAL; return -1; } - return (l + 1) * dl; + return (ssize_t)((l + 1) * dl); } t = data; @@ -483,51 +494,53 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, len--; } if (type & UINT8) { - l = snprintf(s, len, "%u", *data); + sl = snprintf(s, len, "%u", *data); data++; } else if (type & UINT16) { memcpy(&u16, data, sizeof(u16)); u16 = ntohs(u16); - l = snprintf(s, len, "%u", u16); + sl = snprintf(s, len, "%u", u16); data += sizeof(u16); } else if (type & SINT16) { - memcpy(&s16, data, sizeof(s16)); - s16 = ntohs(s16); - l = snprintf(s, len, "%d", s16); - data += sizeof(s16); + memcpy(&u16, data, sizeof(u16)); + s16 = (int16_t)ntohs(u16); + sl = snprintf(s, len, "%d", s16); + data += sizeof(u16); } else if (type & UINT32) { memcpy(&u32, data, sizeof(u32)); u32 = ntohl(u32); - l = snprintf(s, len, "%u", u32); + sl = snprintf(s, len, "%u", u32); data += sizeof(u32); } else if (type & SINT32) { - memcpy(&s32, data, sizeof(s32)); - s32 = ntohl(s32); - l = snprintf(s, len, "%d", s32); - data += sizeof(s32); + memcpy(&u32, data, sizeof(u32)); + s32 = (int32_t)ntohl(u32); + sl = snprintf(s, len, "%d", s32); + data += sizeof(u32); } else if (type & ADDRIPV4) { memcpy(&addr.s_addr, data, sizeof(addr.s_addr)); - l = snprintf(s, len, "%s", inet_ntoa(addr)); + sl = snprintf(s, len, "%s", inet_ntoa(addr)); data += sizeof(addr.s_addr); } #ifdef INET6 else if (type & ADDRIPV6) { - dl = ipv6_printaddr(s, len, data, ifname); - if (dl != -1) - l = dl; + ssize_t r; + + r = ipv6_printaddr(s, len, data, ifname); + if (r != -1) + sl = r; else - l = 0; + sl = 0; data += 16; } #endif else if (type & BINHEX) { - l = snprintf(s, len, "%.2x", data[0]); + sl = snprintf(s, len, "%.2x", data[0]); data++; } else - l = 0; - len -= l; - bytes += l; - s += l; + sl = 0; + len -= (size_t)sl; + bytes += sl; + s += sl; } return bytes; @@ -535,7 +548,7 @@ print_option(char *s, ssize_t len, int type, int dl, const uint8_t *data, static size_t dhcp_envoption1(char **env, const char *prefix, - const struct dhcp_opt *opt, int vname, const uint8_t *od, int ol, + const struct dhcp_opt *opt, int vname, const uint8_t *od, size_t ol, const char *ifname) { ssize_t len; @@ -544,7 +557,7 @@ dhcp_envoption1(char **env, const char *prefix, if (opt->len && opt->len < ol) ol = opt->len; - len = print_option(NULL, 0, opt->type, ol, od, ifname); + len = print_option(NULL, 0, opt->type, od, ol, ifname); if (len < 0) return 0; if (vname) @@ -553,7 +566,7 @@ dhcp_envoption1(char **env, const char *prefix, e = 0; if (prefix) e += strlen(prefix); - e += len + 4; + e += (size_t)len + 4; if (env == NULL) return e; v = val = *env = malloc(e); @@ -566,20 +579,19 @@ dhcp_envoption1(char **env, const char *prefix, else v += snprintf(val, e, "%s=", prefix); if (len != 0) - print_option(v, len, opt->type, ol, od, ifname); + print_option(v, (size_t)len, opt->type, od, ol, ifname); return e; } -ssize_t +size_t dhcp_envoption(struct dhcpcd_ctx *ctx, char **env, const char *prefix, const char *ifname, struct dhcp_opt *opt, const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, unsigned int *, unsigned int *, unsigned int *, const uint8_t *, unsigned int, struct dhcp_opt **), - const uint8_t *od, int ol) + const uint8_t *od, size_t ol) { - ssize_t e, n; - size_t i; + size_t e, i, n; unsigned int eoc, eos, eol; const uint8_t *eod; int ov; diff --git a/dhcp-common.h b/dhcp-common.h index 6fa19da9..deef34a0 100644 --- a/dhcp-common.h +++ b/dhcp-common.h @@ -65,7 +65,7 @@ struct dhcp_opt { uint32_t option; /* Also used for IANA Enterpise Number */ int type; - int len; + size_t len; char *var; int index; /* Index counter for many instances of the same option */ @@ -80,7 +80,7 @@ struct dhcp_opt { size_t encopts_len; }; -struct dhcp_opt *vivso_find(uint16_t, const void *); +struct dhcp_opt *vivso_find(uint32_t, const void *); size_t dhcp_vendor(char *, size_t); @@ -91,16 +91,17 @@ int make_option_mask(const struct dhcp_opt *, size_t, uint8_t *, const char *, int); size_t encode_rfc1035(const char *src, uint8_t *dst); -ssize_t decode_rfc3397(char *, ssize_t, int, const uint8_t *); -ssize_t print_string(char *, ssize_t, int, const uint8_t *); -ssize_t print_option(char *, ssize_t, int, int, const uint8_t *, const char *); +ssize_t decode_rfc3397(char *, size_t, const uint8_t *, size_t); +ssize_t print_string(char *, size_t, const uint8_t *, size_t); +ssize_t print_option(char *, size_t, int, const uint8_t *, size_t, + const char *); -ssize_t dhcp_envoption(struct dhcpcd_ctx *, +size_t dhcp_envoption(struct dhcpcd_ctx *, char **, const char *, const char *, struct dhcp_opt *, const uint8_t *(*dgetopt)(struct dhcpcd_ctx *, - unsigned int *, unsigned int *, unsigned int *, - const uint8_t *, unsigned int, struct dhcp_opt **), - const uint8_t *od, int ol); + 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 *); #endif diff --git a/dhcp.c b/dhcp.c index de56dfde..a565629f 100644 --- a/dhcp.c +++ b/dhcp.c @@ -135,7 +135,7 @@ dhcp_printoptions(const struct dhcpcd_ctx *ctx) #define get_option_raw(ctx, dhcp, opt) get_option(ctx, dhcp, opt, NULL) static const uint8_t * get_option(struct dhcpcd_ctx *ctx, - const struct dhcp_message *dhcp, uint8_t opt, int *len) + const struct dhcp_message *dhcp, unsigned int opt, size_t *len) { const uint8_t *p = dhcp->options; const uint8_t *e = p + sizeof(dhcp->options); @@ -144,7 +144,7 @@ get_option(struct dhcpcd_ctx *ctx, uint8_t overl = 0; uint8_t *bp = NULL; const uint8_t *op = NULL; - int bl = 0; + size_t bl = 0; while (p < e) { o = *p++; @@ -216,7 +216,7 @@ get_option_addr(struct dhcpcd_ctx *ctx, uint8_t option) { const uint8_t *p; - int len; + size_t len; p = get_option(ctx, dhcp, option, &len); if (!p || len < (ssize_t)sizeof(a->s_addr)) @@ -230,7 +230,7 @@ get_option_uint32(struct dhcpcd_ctx *ctx, uint32_t *i, const struct dhcp_message *dhcp, uint8_t option) { const uint8_t *p; - int len; + size_t len; uint32_t d; p = get_option(ctx, dhcp, option, &len); @@ -247,7 +247,7 @@ get_option_uint8(struct dhcpcd_ctx *ctx, uint8_t *i, const struct dhcp_message *dhcp, uint8_t option) { const uint8_t *p; - int len; + size_t len; p = get_option(ctx, dhcp, option, &len); if (!p || len < (ssize_t)sizeof(*p)) @@ -258,10 +258,11 @@ get_option_uint8(struct dhcpcd_ctx *ctx, } ssize_t -decode_rfc3442(char *out, ssize_t len, int pl, const uint8_t *p) +decode_rfc3442(char *out, size_t len, const uint8_t *p, size_t pl) { const uint8_t *e; - ssize_t b, bytes = 0, ocets; + size_t bytes = 0, ocets; + int b; uint8_t cidr; struct in_addr addr; char *o = out; @@ -302,23 +303,23 @@ decode_rfc3442(char *out, ssize_t len, int pl, const uint8_t *p) } else b = snprintf(o, len, "0.0.0.0/0"); o += b; - len -= b; + len -= (size_t)b; /* Finally, snag the router */ memcpy(&addr.s_addr, p, 4); p += 4; b = snprintf(o, len, " %s", inet_ntoa(addr)); o += b; - len -= b; + len -= (size_t)b; } if (out) return o - out; - return bytes; + return (ssize_t)bytes; } static struct rt_head * -decode_rfc3442_rt(int dl, const uint8_t *data) +decode_rfc3442_rt(const uint8_t *data, size_t dl) { const uint8_t *p = data; const uint8_t *e; @@ -366,10 +367,11 @@ decode_rfc3442_rt(int dl, const uint8_t *data) } char * -decode_rfc3361(int dl, const uint8_t *data) +decode_rfc3361(const uint8_t *data, size_t dl) { uint8_t enc; - unsigned int l; + size_t l; + ssize_t r; char *sip = NULL; struct in_addr addr; char *p; @@ -383,11 +385,12 @@ decode_rfc3361(int dl, const uint8_t *data) dl--; switch (enc) { case 0: - if ((l = decode_rfc3397(NULL, 0, dl, data)) > 0) { + if ((r = decode_rfc3397(NULL, 0, data, dl)) > 0) { + l = (size_t)r; sip = malloc(l); if (sip == NULL) return 0; - decode_rfc3397(sip, l, dl, data); + decode_rfc3397(sip, l, data, dl); } break; case 1: @@ -403,7 +406,8 @@ decode_rfc3361(int dl, const uint8_t *data) while (dl != 0) { memcpy(&addr.s_addr, data, sizeof(addr.s_addr)); data += sizeof(addr.s_addr); - p += snprintf(p, l - (p - sip), "%s ", inet_ntoa(addr)); + p += snprintf(p, l - (size_t)(p - sip), + "%s ", inet_ntoa(addr)); dl -= sizeof(addr.s_addr); } *--p = '\0'; @@ -420,7 +424,7 @@ decode_rfc3361(int dl, const uint8_t *data) * separated string. Returns length of string (including * terminating zero) or zero on error. */ ssize_t -decode_rfc5969(char *out, ssize_t len, int pl, const uint8_t *p) +decode_rfc5969(char *out, size_t len, const uint8_t *p, size_t pl) { uint8_t ipv4masklen, ipv6prefixlen; uint8_t ipv6prefix[16]; @@ -456,7 +460,7 @@ decode_rfc5969(char *out, ssize_t len, int pl, const uint8_t *p) ipv6prefix[12],ipv6prefix[13],ipv6prefix[14], ipv6prefix[15] ); - len -= b; + len -= (size_t)b; out += b; bytes += b; } else { @@ -473,7 +477,7 @@ decode_rfc5969(char *out, ssize_t len, int pl, const uint8_t *p) if (out) { b= snprintf(out, len, " %d.%d.%d.%d", br[0], br[1], br[2], br[3]); - len -= b; + len -= (size_t)b; out += b; bytes += b; } else { @@ -488,7 +492,7 @@ static char * get_option_string(struct dhcpcd_ctx *ctx, const struct dhcp_message *dhcp, uint8_t option) { - int len; + size_t len; const uint8_t *p; char *s; @@ -544,7 +548,7 @@ get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp) const uint8_t *e; struct rt_head *routes = NULL; struct rt *route = NULL; - int len; + size_t len; const char *csr = ""; /* If we have CSR's then we MUST use these only */ @@ -559,7 +563,7 @@ get_option_routes(struct interface *ifp, const struct dhcp_message *dhcp) csr = "MS "; } if (p) { - routes = decode_rfc3442_rt(len, p); + routes = decode_rfc3442_rt(p, len); if (routes) { if (!(ifo->options & DHCPCD_CSR_WARNED)) { syslog(LOG_DEBUG, @@ -644,7 +648,7 @@ dhcp_message_add_addr(struct dhcp_message *dhcp, p += *p + 1; } - len = p - (uint8_t *)dhcp; + len = (size_t)(p - (uint8_t *)dhcp); if (len + 6 > sizeof(*dhcp)) { errno = ENOMEM; return -1; @@ -665,8 +669,7 @@ make_message(struct dhcp_message **message, uint8_t *n_params = NULL; uint32_t ul; uint16_t sz; - size_t len, i; - int auth_len; + size_t len, i, auth_len; const struct dhcp_opt *opt; struct if_options *ifo = iface->options; const struct dhcp_state *state = D_CSTATE(iface); @@ -748,7 +751,7 @@ make_message(struct dhcp_message **message, if (type == DHCP_DECLINE) { *p++ = DHO_MESSAGE; len = strlen(DAD); - *p++ = len; + *p++ = (uint8_t)len; memcpy(p, DAD, len); p += len; } @@ -776,19 +779,21 @@ make_message(struct dhcp_message **message, type == DHCP_INFORM || type == DHCP_REQUEST) { + int mtu; + *p++ = DHO_MAXMESSAGESIZE; *p++ = 2; - sz = get_mtu(iface->name); - if (sz < MTU_MIN) { + mtu = get_mtu(iface->name); + if (mtu < MTU_MIN) { if (set_mtu(iface->name, MTU_MIN) == 0) sz = MTU_MIN; - } else if (sz > MTU_MAX) { + } else if (mtu > MTU_MAX) { /* Even though our MTU could be greater than * MTU_MAX (1500) dhcpcd does not presently * handle DHCP packets any bigger. */ - sz = MTU_MAX; + mtu = MTU_MAX; } - sz = htons(sz); + sz = htons(mtu); memcpy(p, &sz, 2); p += 2; @@ -850,7 +855,7 @@ make_message(struct dhcp_message **message, } else if (ifo->options & DHCPCD_HOSTNAME && hostname) { *p++ = DHO_HOSTNAME; len = strlen(hostname); - *p++ = len; + *p++ = (uint8_t)len; memcpy(p, hostname, len); p += len; } @@ -882,7 +887,7 @@ make_message(struct dhcp_message **message, i < ifo->vivco_len; i++, vivco++) { - len = (p - m) + vivco->len + 1; + len = (size_t)(p - m) + vivco->len + 1; if (len > sizeof(*dhcp)) goto toobig; if (vivco->len + 2 + *lp > 255) { @@ -899,7 +904,7 @@ make_message(struct dhcp_message **message, } } - len = (p - m) + 3; + len = (size_t)((p - m) + 3); if (len > sizeof(*dhcp)) goto toobig; *p++ = DHO_PARAMETERREQUESTLIST; @@ -918,12 +923,12 @@ make_message(struct dhcp_message **message, (opt->option == DHO_RENEWALTIME || opt->option == DHO_REBINDTIME)) continue; - len = (p - m) + 2; + len = (size_t)((p - m) + 2); if (len > sizeof(*dhcp)) goto toobig; - *p++ = opt->option; + *p++ = (uint8_t)opt->option; } - *n_params = p - n_params - 1; + *n_params = (uint8_t)(p - n_params - 1); } /* silence GCC */ @@ -931,17 +936,18 @@ make_message(struct dhcp_message **message, auth = NULL; if (ifo->auth.options & DHCPCD_AUTH_SEND) { - auth_len = dhcp_auth_encode(&ifo->auth, state->auth.token, + auth_len = (size_t)dhcp_auth_encode(&ifo->auth, + state->auth.token, NULL, 0, 4, type, NULL, 0); if (auth_len > 0) { - len = (p + auth_len) - m; + len = (size_t)((p + auth_len) - m); if (auth_len > 255 || len > sizeof(*dhcp)) goto toobig; *p++ = DHO_AUTHENTICATION; *p++ = (uint8_t)auth_len; auth = p; p += auth_len; - } else if (auth_len == -1) + } else if ((ssize_t)auth_len == -1) syslog(LOG_ERR, "%s: dhcp_auth_encode: %m", iface->name); } @@ -956,13 +962,13 @@ make_message(struct dhcp_message **message, *p++ = DHO_PAD; #endif - len = p - m; + len = (size_t)(p - m); if (ifo->auth.options & DHCPCD_AUTH_SEND && auth_len > 0) dhcp_auth_encode(&ifo->auth, state->auth.token, m, len, 4, type, auth, auth_len); *message = dhcp; - return len; + return (ssize_t)len; toobig: syslog(LOG_ERR, "%s: DHCP messge too big", iface->name); @@ -974,6 +980,7 @@ ssize_t write_lease(const struct interface *ifp, const struct dhcp_message *dhcp) { int fd; + size_t len; ssize_t bytes; const uint8_t *e, *p; uint8_t l; @@ -996,11 +1003,11 @@ write_lease(const struct interface *ifp, const struct dhcp_message *dhcp) /* Only write as much as we need */ p = dhcp->options; e = p + sizeof(dhcp->options); - bytes = sizeof(*dhcp); + len = sizeof(*dhcp); while (p < e) { o = *p; if (o == DHO_END) { - bytes = p - (const uint8_t *)dhcp; + len = (size_t)(p - (const uint8_t *)dhcp); break; } p++; @@ -1009,7 +1016,7 @@ write_lease(const struct interface *ifp, const struct dhcp_message *dhcp) p += l; } } - bytes = write(fd, dhcp, bytes); + bytes = write(fd, dhcp, len); close(fd); return bytes; } @@ -1023,7 +1030,7 @@ read_lease(struct interface *ifp) ssize_t bytes; const uint8_t *auth; uint8_t type; - int auth_len; + size_t auth_len; fd = open(state->leasefile, O_RDONLY); if (fd == -1) { @@ -1073,7 +1080,7 @@ read_lease(struct interface *ifp) } static const struct dhcp_opt * -dhcp_getoverride(const struct if_options *ifo, uint16_t o) +dhcp_getoverride(const struct if_options *ifo, unsigned int o) { size_t i; const struct dhcp_opt *opt; @@ -1090,8 +1097,8 @@ dhcp_getoverride(const struct if_options *ifo, uint16_t o) static const uint8_t * dhcp_getoption(struct dhcpcd_ctx *ctx, - unsigned int *os, unsigned int *code, unsigned int *len, - const uint8_t *od, unsigned int ol, struct dhcp_opt **oopt) + 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; @@ -1102,8 +1109,8 @@ dhcp_getoption(struct dhcpcd_ctx *ctx, return NULL; } *os = 2; /* code + len */ - *code = (int)*od++; - *len = (int)*od++; + *code = (unsigned int)*od++; + *len = (size_t)*od++; if (*len > ol) { errno = EINVAL; return NULL; @@ -1126,18 +1133,17 @@ dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp, { const struct if_options *ifo; const uint8_t *p; - int pl; struct in_addr addr; struct in_addr net; struct in_addr brd; struct dhcp_opt *opt, *vo; - ssize_t e = 0; + size_t e, i, pl; char **ep; char cidr[4]; uint8_t overl = 0; - size_t i; uint32_t en; + e = 0; ifo = ifp->options; get_option_uint8(ifp->ctx, &overl, dhcp, DHO_OPTIONSOVERLOADED); @@ -1174,7 +1180,7 @@ dhcp_env(char **env, const char *prefix, const struct dhcp_message *dhcp, e += dhcp_envoption(ifp->ctx, NULL, NULL, ifp->name, opt, dhcp_getoption, p, pl); } - return e; + return (ssize_t)e; } ep = env; @@ -1422,7 +1428,7 @@ eexit: static ssize_t dhcp_sendpacket(const struct interface *iface, struct in_addr to, - const uint8_t *data, ssize_t len) + const uint8_t *data, size_t len) { struct sockaddr_in sin; @@ -1439,6 +1445,7 @@ checksum(const void *data, uint16_t len) { const uint8_t *addr = data; uint32_t sum = 0; + uint16_t res; while (len > 1) { sum += addr[0] * 256 + addr[1]; @@ -1452,13 +1459,13 @@ checksum(const void *data, uint16_t len) sum = (sum >> 16) + (sum & 0xffff); sum += (sum >> 16); - sum = htons(sum); + res = htons(sum); - return ~sum; + return ~res; } static struct udp_dhcp_packet * -dhcp_makeudppacket(ssize_t *sz, const uint8_t *data, size_t length, +dhcp_makeudppacket(size_t *sz, const uint8_t *data, size_t length, struct in_addr source, struct in_addr dest) { struct udp_dhcp_packet *udpp; @@ -1507,14 +1514,15 @@ dhcp_makeudppacket(ssize_t *sz, const uint8_t *data, size_t length, } static void -send_message(struct interface *iface, int type, +send_message(struct interface *iface, uint8_t type, void (*callback)(void *)) { struct dhcp_state *state = D_STATE(iface); struct if_options *ifo = iface->options; struct dhcp_message *dhcp; struct udp_dhcp_packet *udp; - ssize_t len, r; + size_t len; + ssize_t r; struct in_addr from, to; in_addr_t a = 0; struct timeval tv; @@ -1557,7 +1565,10 @@ send_message(struct interface *iface, int type, a = state->addr.s_addr; state->addr.s_addr = 0; } - len = make_message(&dhcp, iface, type); + r = make_message(&dhcp, iface, type); + if (r == -1) + goto fail; + len = (size_t)r; if (a) state->addr.s_addr = a; from.s_addr = dhcp->ciaddr; @@ -1572,13 +1583,15 @@ send_message(struct interface *iface, int type, dhcp_close(iface); } } else { + size_t ulen; + r = 0; - udp = dhcp_makeudppacket(&r, (uint8_t *)dhcp, len, from, to); + udp = dhcp_makeudppacket(&ulen, (uint8_t *)dhcp, len, from, to); if (udp == NULL) { syslog(LOG_ERR, "dhcp_makeudppacket: %m"); } else { r = ipv4_sendrawpacket(iface, ETHERTYPE_IP, - (uint8_t *)udp, r); + (uint8_t *)udp, ulen); free(udp); } /* If we failed to send a raw packet this normally means @@ -1598,6 +1611,7 @@ send_message(struct interface *iface, int type, } free(dhcp); +fail: /* Even if we fail to send a packet we should continue as we are * as our failure timeouts will change out codepath when needed. */ if (callback) @@ -1645,7 +1659,7 @@ dhcp_discover(void *arg) struct interface *iface = arg; struct dhcp_state *state = D_STATE(iface); struct if_options *ifo = iface->options; - int timeout = ifo->timeout; + time_t timeout = ifo->timeout; /* If we're rebooting and we're not daemonised then we need * to shorten the normal timeout to ensure we try correctly @@ -1653,9 +1667,10 @@ dhcp_discover(void *arg) if (state->state == DHS_REBOOT && !(iface->ctx->options & DHCPCD_DAEMONISED)) { - timeout -= ifo->reboot; - if (timeout <= 0) + if (ifo->reboot >= timeout) timeout = 2; + else + timeout -= ifo->reboot; } state->state = DHS_DISCOVER; @@ -1805,18 +1820,22 @@ dhcp_bind(void *arg) lease->leasetime = DHCP_MIN_LEASE; } if (lease->rebindtime == 0) - lease->rebindtime = lease->leasetime * T2; + lease->rebindtime = + (uint32_t)(lease->leasetime * T2); else if (lease->rebindtime >= lease->leasetime) { - lease->rebindtime = lease->leasetime * T2; + lease->rebindtime = + (uint32_t)(lease->leasetime * T2); syslog(LOG_WARNING, "%s: rebind time greater than lease " "time, forcing to %"PRIu32" seconds", iface->name, lease->rebindtime); } if (lease->renewaltime == 0) - lease->renewaltime = lease->leasetime * T1; + lease->renewaltime = + (uint32_t)(lease->leasetime * T1); else if (lease->renewaltime > lease->rebindtime) { - lease->renewaltime = lease->leasetime * T1; + lease->renewaltime = + (uint32_t)(lease->leasetime * T1); syslog(LOG_WARNING, "%s: renewal time greater than rebind " "time, forcing to %"PRIu32" seconds", @@ -1971,7 +1990,7 @@ dhcp_inform(struct interface *ifp) } void -dhcp_reboot_newopts(struct interface *ifp, int oldopts) +dhcp_reboot_newopts(struct interface *ifp, unsigned long long oldopts) { struct if_options *ifo; struct dhcp_state *state = D_STATE(ifp); @@ -2180,8 +2199,8 @@ dhcp_handledhcp(struct interface *iface, struct dhcp_message **dhcpp, uint8_t type, tmp; const uint8_t *auth; struct in_addr addr; - size_t i; - int auth_len; + unsigned int i; + size_t auth_len; char *msg; /* We may have found a BOOTP server */ @@ -2327,7 +2346,8 @@ dhcp_handledhcp(struct interface *iface, struct dhcp_message **dhcpp, case 1: log_dhcp(LOG_WARNING, "IPv4LL enabled from", iface, dhcp, from); - eloop_timeout_delete(iface->ctx->eloop, NULL, iface); + eloop_timeout_delete(iface->ctx->eloop, + NULL, iface); if (IN_LINKLOCAL(htonl(state->addr.s_addr))) eloop_timeout_add_sec(iface->ctx->eloop, DHCP_MAX, dhcp_discover, iface); @@ -2347,7 +2367,7 @@ dhcp_handledhcp(struct interface *iface, struct dhcp_message **dhcpp, /* Ensure that all required options are present */ for (i = 1; i < 255; i++) { if (has_option_mask(ifo->requiremask, i) && - get_option_uint8(iface->ctx, &tmp, dhcp, i) != 0) + get_option_uint8(iface->ctx, &tmp, dhcp, (uint8_t)i) != 0) { /* If we are bootp, then ignore the need for serverid. * To ignore bootp, require dhcp_message_type. */ @@ -2462,7 +2482,7 @@ dhcp_handledhcp(struct interface *iface, struct dhcp_message **dhcpp, dhcp_bind(iface); } -static ssize_t +static size_t get_udp_data(const uint8_t **data, const uint8_t *udp) { struct udp_dhcp_packet p; @@ -2529,7 +2549,7 @@ dhcp_handlepacket(void *arg) struct interface *iface = arg; struct dhcp_message *dhcp = NULL; const uint8_t *pp; - ssize_t bytes; + size_t bytes; struct in_addr from; int i, partialcsum = 0; const struct dhcp_state *state = D_CSTATE(iface); @@ -2538,9 +2558,9 @@ dhcp_handlepacket(void *arg) * The benefit is that if we get >1 DHCP packet in our buffer and * the first one fails for any reason, we can use the next. */ for(;;) { - bytes = ipv4_getrawpacket(iface, ETHERTYPE_IP, + bytes = (size_t)ipv4_getrawpacket(iface, ETHERTYPE_IP, iface->ctx->packet, udp_dhcp_len, &partialcsum); - if (bytes == 0 || bytes == -1) + if (bytes == 0 || (ssize_t)bytes == -1) break; if (valid_udp_packet(iface->ctx->packet, bytes, &from, partialcsum) == -1) @@ -2571,7 +2591,7 @@ dhcp_handlepacket(void *arg) iface->name, inet_ntoa(from)); } bytes = get_udp_data(&pp, iface->ctx->packet); - if ((size_t)bytes > sizeof(*dhcp)) { + if (bytes > sizeof(*dhcp)) { syslog(LOG_ERR, "%s: packet greater than DHCP size from %s", iface->name, inet_ntoa(from)); @@ -2766,7 +2786,7 @@ dhcp_init(struct interface *ifp) { struct dhcp_state *state; const struct if_options *ifo; - size_t len; + uint8_t len; char buf[(sizeof(ifo->clientid) - 1) * 3]; state = D_STATE(ifp); @@ -2795,27 +2815,28 @@ dhcp_init(struct interface *ifp) state->clientid = NULL; if (*ifo->clientid) { - state->clientid = malloc(ifo->clientid[0] + 1); + state->clientid = malloc((size_t)(ifo->clientid[0] + 1)); if (state->clientid == NULL) goto eexit; - memcpy(state->clientid, ifo->clientid, ifo->clientid[0] + 1); + memcpy(state->clientid, ifo->clientid, + (size_t)(ifo->clientid[0]) + 1); } else if (ifo->options & DHCPCD_CLIENTID) { if (ifo->options & DHCPCD_DUID) { state->clientid = malloc(ifp->ctx->duid_len + 6); if (state->clientid == NULL) goto eexit; - state->clientid[0] = 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 { - len = ifp->hwlen + 1; - state->clientid = malloc(len + 1); + 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] = ifp->family; + state->clientid[1] = (uint8_t)ifp->family; memcpy(state->clientid + 2, ifp->hwaddr, ifp->hwlen); } @@ -2923,7 +2944,7 @@ dhcp_start(struct interface *ifp) state->offer = NULL; state->lease.addr.s_addr = 0; } else { - l = now.tv_sec - st.st_mtime; + l = (uint32_t)(now.tv_sec - st.st_mtime); state->lease.leasetime -= l; state->lease.renewaltime -= l; state->lease.rebindtime -= l; @@ -2958,7 +2979,7 @@ dhcp_handleifa(int type, struct interface *ifp, { struct dhcp_state *state; struct if_options *ifo; - int i; + uint8_t i; state = D_STATE(ifp); if (state == NULL) diff --git a/dhcp.h b/dhcp.h index d9d925e2..0458097f 100644 --- a/dhcp.h +++ b/dhcp.h @@ -246,9 +246,9 @@ struct dhcp_state { #include "net.h" #ifdef INET -char *decode_rfc3361(int dl, const uint8_t *data); -ssize_t decode_rfc3442(char *out, ssize_t len, int pl, const uint8_t *p); -ssize_t decode_rfc5969(char *out, ssize_t len, int pl, const uint8_t *p); +char *decode_rfc3361(const uint8_t *, size_t); +ssize_t decode_rfc3442(char *, size_t, const uint8_t *p, size_t); +ssize_t decode_rfc5969(char *, size_t, const uint8_t *p, size_t); void dhcp_printoptions(const struct dhcpcd_ctx *); int get_option_addr(struct dhcpcd_ctx *,struct in_addr *, @@ -282,7 +282,7 @@ void dhcp_decline(struct interface *); void dhcp_discover(void *); void dhcp_inform(struct interface *); void dhcp_bind(void *); -void dhcp_reboot_newopts(struct interface *, int); +void dhcp_reboot_newopts(struct interface *, unsigned long long); void dhcp_close(struct interface *); void dhcp_free(struct interface *); int dhcp_dump(struct dhcpcd_ctx *, const char *); diff --git a/dhcp6.c b/dhcp6.c index 62489b37..94529629 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -185,22 +185,24 @@ dhcp6_makevendor(struct dhcp6_option *o, const struct interface *ifp) } static const struct dhcp6_option * -dhcp6_findoption(int code, const uint8_t *d, ssize_t len) +dhcp6_findoption(unsigned int code, const uint8_t *d, size_t len) { const struct dhcp6_option *o; + size_t ol; code = htons(code); for (o = (const struct dhcp6_option *)d; len > (ssize_t)sizeof(*o); o = D6_CNEXT_OPTION(o)) { - len -= sizeof(*o) + ntohs(o->len); - if (len < 0) { + ol = sizeof(*o) + ntohs(o->len); + if (ol > len) { errno = EINVAL; return NULL; } if (o->code == code) return o; + len -= ol; } errno = ESRCH; @@ -209,8 +211,8 @@ dhcp6_findoption(int code, const uint8_t *d, ssize_t len) static const uint8_t * dhcp6_getoption(struct dhcpcd_ctx *ctx, - unsigned int *os, unsigned int *code, unsigned int *len, - const uint8_t *od, unsigned int ol, struct dhcp_opt **oopt) + size_t *os, unsigned int *code, size_t *len, + const uint8_t *od, size_t ol, struct dhcp_opt **oopt) { const struct dhcp6_option *o; size_t i; @@ -247,7 +249,7 @@ dhcp6_getoption(struct dhcpcd_ctx *ctx, } static const struct dhcp6_option * -dhcp6_getmoption(int code, const struct dhcp6_message *m, ssize_t len) +dhcp6_getmoption(unsigned int code, const struct dhcp6_message *m, size_t len) { len -= sizeof(*m); @@ -256,7 +258,7 @@ dhcp6_getmoption(int code, const struct dhcp6_message *m, ssize_t len) } static int -dhcp6_updateelapsed(struct interface *ifp, struct dhcp6_message *m, ssize_t len) +dhcp6_updateelapsed(struct interface *ifp, struct dhcp6_message *m, size_t len) { struct dhcp6_state *state; const struct dhcp6_option *co; @@ -303,11 +305,9 @@ dhcp6_makemessage(struct interface *ifp) struct dhcp6_message *m; struct dhcp6_option *o, *so; const struct dhcp6_option *si, *unicast; - ssize_t len, ml; - size_t l; - int auth_len; - uint8_t u8; - uint16_t *u16, n_options, type; + size_t l, len, ml, auth_len; + uint8_t u8, type; + uint16_t *u16, n_options; struct if_options *ifo; const struct dhcp_opt *opt; uint8_t IA, *p; @@ -468,9 +468,11 @@ dhcp6_makemessage(struct interface *ifp) } if (ifo->auth.options & DHCPCD_AUTH_SEND) { - auth_len = dhcp_auth_encode(&ifo->auth, state->auth.token, - NULL, 0, 6, type, NULL, 0); - if (auth_len > 0) + auth_len = (size_t)dhcp_auth_encode(&ifo->auth, + state->auth.token, NULL, 0, 6, type, NULL, 0); + if ((ssize_t)auth_len == -1) + auth_len = 0; + else if (auth_len> 0) len += sizeof(*o) + auth_len; } else auth_len = 0; /* appease GCC */ @@ -680,7 +682,7 @@ static void dhcp6_delete_delegates(struct interface *ifp) static int -dhcp6_update_auth(struct interface *ifp, struct dhcp6_message *m, ssize_t len) +dhcp6_update_auth(struct interface *ifp, struct dhcp6_message *m, size_t len) { struct dhcp6_state *state; const struct dhcp6_option *co; @@ -709,7 +711,7 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) struct in6_pktinfo pi; struct timeval RTprev; double rnd; - suseconds_t ms; + time_t ms; uint8_t neg; const char *broad_uni; const struct in6_addr alldhcp = IN6ADDR_LINKLOCAL_ALLDHCP_INIT; @@ -754,8 +756,8 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) state->RT.tv_sec = 1; else state->RT.tv_sec = 0; - state->RT.tv_usec = arc4random() % - (state->IMD * 1000000); + state->RT.tv_usec = (suseconds_t)(arc4random() % + (state->IMD * 1000000)); timernorm(&state->RT); broad_uni = "delaying"; goto logsend; @@ -1301,12 +1303,14 @@ dhcp6_findna(struct interface *ifp, const uint8_t *iaid, const char *ia; int i; uint32_t u32; + size_t off; i = 0; state = D6_STATE(ifp); while ((o = dhcp6_findoption(D6_OPTION_IA_ADDR, d, l))) { - l -= ((const uint8_t *)o - d); - d += ((const uint8_t *)o - d); + off = (size_t)((const uint8_t *)o - d); + l -= off; + d += off; u32 = ntohs(o->len); l -= sizeof(*o) + u32; d += sizeof(*o) + u32; @@ -1387,12 +1391,14 @@ dhcp6_findpd(struct interface *ifp, const uint8_t *iaid, uint8_t u8, len; uint32_t u32, pltime, vltime; struct in6_addr prefix; + size_t off; i = 0; state = D6_STATE(ifp); while ((o = dhcp6_findoption(D6_OPTION_IAPREFIX, d, l))) { - l -= ((const uint8_t *)o - d); - d += ((const uint8_t *)o - d); + off = (size_t)((const uint8_t *)o - d); + l -= off; + d += off; u32 = ntohs(o->len); l -= sizeof(*o) + u32; d += sizeof(*o) + u32; @@ -1474,8 +1480,9 @@ dhcp6_findia(struct interface *ifp, const uint8_t *d, size_t l, ap->flags |= IPV6_AF_STALE; } while ((o = dhcp6_findoption(ifo->ia_type, d, l))) { - l -= ((const uint8_t *)o - d); - d += ((const uint8_t *)o - d); + ol = (size_t)((const uint8_t *)o - d); + l -= ol; + d += ol; ol = ntohs(o->len); l -= sizeof(*o) + ol; d += sizeof(*o) + ol; @@ -1575,7 +1582,7 @@ dhcp6_validatelease(struct interface *ifp, state->renew = state->rebind = state->expire = 0; state->lowpl = ND6_INFINITE_LIFETIME; - len -= (const char *)o - (const char *)m; + len -= (size_t)((const char *)o - (const char *)m); return dhcp6_findia(ifp, (const uint8_t *)o, len, sfrom); } @@ -1618,10 +1625,14 @@ dhcp6_readlease(struct interface *ifp) } syslog(LOG_DEBUG, "%s: reading lease `%s'", ifp->name, state->leasefile); - state->new = malloc(st.st_size); + if (st.st_size > SIZE_MAX) { + syslog(LOG_ERR, "%s: file too big", ifp->name); + return -1; + } + state->new = malloc((size_t)st.st_size); if (state->new == NULL) return -1; - state->new_len = st.st_size; + state->new_len = (size_t)st.st_size; fd = open(state->leasefile, O_RDONLY); if (fd == -1) return -1; @@ -1744,6 +1755,7 @@ dhcp6_delegate_addr(struct interface *ifp, struct ipv6_addr *prefix, if (sla == NULL) { struct interface *ifi; unsigned int idx; + int bits; asla.sla = ifp->index; /* Work out our largest index delegating to. */ @@ -1752,14 +1764,19 @@ dhcp6_delegate_addr(struct interface *ifp, struct ipv6_addr *prefix, if (ifi != ifp && ifi->index > idx) idx = ifi->index; } - asla.prefix_len = prefix->prefix_len + ffs((int)idx); + bits = ffs((int)idx); + if (prefix->prefix_len + bits > UINT8_MAX) + asla.prefix_len = UINT8_MAX; + else { + asla.prefix_len = prefix->prefix_len + (uint8_t)bits; - /* Make a 64 prefix by default, as this maks SLAAC - * possible. Otherwise round up to the nearest octet. */ - if (asla.prefix_len <= 64) - asla.prefix_len = 64; - else - asla.prefix_len = ROUNDUP8(asla.prefix_len); + /* Make a 64 prefix by default, as this maks SLAAC + * possible. Otherwise round up to the nearest octet. */ + if (asla.prefix_len <= 64) + asla.prefix_len = 64; + else + asla.prefix_len = ROUNDUP8(asla.prefix_len); + } sla = &asla; } @@ -1922,7 +1939,7 @@ dhcp6_find_delegates1(void *arg) dhcp6_find_delegates(arg); } -int +size_t dhcp6_find_delegates(struct interface *ifp) { struct if_options *ifo; @@ -1985,8 +2002,8 @@ dhcp6_handledata(void *arg) { struct dhcpcd_ctx *dhcpcd_ctx; struct ipv6_ctx *ctx; - ssize_t len; - size_t i; + size_t i, len; + ssize_t bytes; struct cmsghdr *cm; struct in6_pktinfo pkt; struct interface *ifp; @@ -2004,14 +2021,15 @@ dhcp6_handledata(void *arg) dhcpcd_ctx = arg; ctx = dhcpcd_ctx->ipv6; ctx->rcvhdr.msg_controllen = CMSG_SPACE(sizeof(struct in6_pktinfo)); - len = recvmsg(ctx->dhcp_fd, &ctx->rcvhdr, 0); - if (len == -1) { + bytes = recvmsg(ctx->dhcp_fd, &ctx->rcvhdr, 0); + if (bytes == -1) { syslog(LOG_ERR, "recvmsg: %m"); return; } + len = (size_t)bytes; ctx->sfrom = inet_ntop(AF_INET6, &ctx->from.sin6_addr, ctx->ntopbuf, sizeof(ctx->ntopbuf)); - if ((size_t)len < sizeof(struct dhcp6_message)) { + if (len < sizeof(struct dhcp6_message)) { syslog(LOG_ERR, "DHCPv6 RA packet too short from %s", ctx->sfrom); return; @@ -2352,13 +2370,13 @@ recv: if (state->expire == ND6_INFINITE_LIFETIME) state->renew = ND6_INFINITE_LIFETIME; else - state->renew = state->lowpl * 0.5; + state->renew = (uint32_t)(state->lowpl * 0.5); } if (state->rebind == 0) { if (state->expire == ND6_INFINITE_LIFETIME) state->rebind = ND6_INFINITE_LIFETIME; else - state->rebind = state->lowpl * 0.8; + state->rebind = (uint32_t)(state->lowpl * 0.8); } break; default: @@ -2742,7 +2760,7 @@ dhcp6_handleifa(struct dhcpcd_ctx *ctx, int cmd, const char *ifname, ssize_t dhcp6_env(char **env, const char *prefix, const struct interface *ifp, - const struct dhcp6_message *m, ssize_t len) + const struct dhcp6_message *m, size_t len) { const struct dhcp6_state *state; const struct if_options *ifo; @@ -2778,7 +2796,7 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, pfx = malloc(i); if (pfx == NULL) { syslog(LOG_ERR, "%s: %m", __func__); - return 0; + return -1; } snprintf(pfx, i, "%s_dhcp6", prefix); } else @@ -2791,11 +2809,11 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, o = D6_CNEXT_OPTION(o)) { ol = ntohs(o->len); - len -= sizeof(*o) + ol; - if (len < 0) { - errno = EINVAL; + if (sizeof(*o) + ol > len) { + errno = EINVAL; break; } + len -= sizeof(*o) + ol; oc = ntohs(o->code); if (has_option_mask(ifo->nomask6, oc)) continue; @@ -2857,7 +2875,7 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, syslog(LOG_ERR, "%s: %m", __func__); return -1; } - i = snprintf(v, l, "%s_dhcp6_prefix=", + i = (size_t)snprintf(v, l, "%s_dhcp6_prefix=", prefix); v += i; l -= i; @@ -2880,7 +2898,8 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, syslog(LOG_ERR, "%s: %m", __func__); return -1; } - i = snprintf(v, l, "%s_dhcp6_ip_address=", + i = (size_t)snprintf(v, l, + "%s_dhcp6_ip_address=", prefix); v += i; l -= i; @@ -2897,5 +2916,5 @@ dhcp6_env(char **env, const char *prefix, const struct interface *ifp, n++; } - return n; + return (ssize_t)n; } diff --git a/dhcp6.h b/dhcp6.h index e5194975..9e148b76 100644 --- a/dhcp6.h +++ b/dhcp6.h @@ -172,14 +172,14 @@ struct dhcp6_state { /* Message retransmission timings */ struct timeval RT; - int IMD; - int RTC; - int IRT; - int MRC; - int MRT; + unsigned int IMD; + unsigned int RTC; + unsigned int IRT; + unsigned int MRC; + unsigned int MRT; void (*MRCcallback)(void *); - int sol_max_rt; - int inf_max_rt; + unsigned int sol_max_rt; + unsigned int inf_max_rt; struct dhcp6_message *send; size_t send_len; @@ -227,11 +227,11 @@ struct dhcp6_state { #ifdef INET6 void dhcp6_printoptions(const struct dhcpcd_ctx *); int dhcp6_addrexists(struct dhcpcd_ctx *, const struct ipv6_addr *); -int dhcp6_find_delegates(struct interface *); +size_t dhcp6_find_delegates(struct interface *); int dhcp6_start(struct interface *, enum DH6S); void dhcp6_reboot(struct interface *); ssize_t dhcp6_env(char **, const char *, const struct interface *, - const struct dhcp6_message *, ssize_t); + const struct dhcp6_message *, size_t); void dhcp6_free(struct interface *); void dhcp6_handleifa(struct dhcpcd_ctx *, int, const char *, const struct in6_addr *addr, int); diff --git a/dhcpcd.c b/dhcpcd.c index 64758e58..399b5744 100644 --- a/dhcpcd.c +++ b/dhcpcd.c @@ -353,7 +353,7 @@ configure_interface1(struct interface *ifp) ifo->options &= ~DHCPCD_LINK; if (ifo->metric != -1) - ifp->metric = ifo->metric; + ifp->metric = (unsigned int)ifo->metric; if (!(ifo->options & DHCPCD_IPV6)) ifo->options &= ~DHCPCD_IPV6RS; @@ -492,7 +492,7 @@ configure_interface(struct interface *ifp, int argc, char **argv) } void -handle_carrier(struct dhcpcd_ctx *ctx, int carrier, int flags, +handle_carrier(struct dhcpcd_ctx *ctx, int carrier, unsigned int flags, const char *ifname) { struct interface *ifp; @@ -574,7 +574,6 @@ start_interface(void *arg) { struct interface *ifp = arg; struct if_options *ifo = ifp->options; - int nolease; size_t i; char buf[DUID_LEN * 3]; @@ -619,10 +618,13 @@ start_interface(void *arg) ipv6nd_startrs(ifp); if (!(ifo->options & DHCPCD_IPV6RS)) { + ssize_t nolease; + if (ifo->options & DHCPCD_IA_FORCED) nolease = dhcp6_start(ifp, DH6S_INIT); else { - nolease = dhcp6_find_delegates(ifp); + dhcp6_find_delegates(ifp); + nolease = 0; /* Enabling the below doesn't really make * sense as there is currently no standard * to push routes via DHCPv6. @@ -760,7 +762,7 @@ handle_interface(void *arg, int action, const char *ifname) void handle_hwaddr(struct dhcpcd_ctx *ctx, const char *ifname, - const uint8_t *hwaddr, size_t hwlen) + const uint8_t *hwaddr, uint8_t hwlen) { struct interface *ifp; char buf[sizeof(ifp->hwaddr) * 3]; @@ -787,7 +789,7 @@ handle_hwaddr(struct dhcpcd_ctx *ctx, const char *ifname, static void if_reboot(struct interface *ifp, int argc, char **argv) { - int oldopts; + unsigned long long oldopts; oldopts = ifp->options->options; script_runreason(ifp, "RECONFIGURE"); @@ -959,8 +961,7 @@ handle_args(struct dhcpcd_ctx *ctx, struct fd_list *fd, int argc, char **argv) struct interface *ifp; int do_exit = 0, do_release = 0, do_reboot = 0; int opt, oi = 0; - ssize_t len; - size_t l; + size_t len, l; struct iovec iov[2]; char *tmp, *p; @@ -998,8 +999,8 @@ handle_args(struct dhcpcd_ctx *ctx, struct fd_list *fd, int argc, char **argv) if (ipv6nd_has_ra(ifp)) len++; } - len = write(fd->fd, &len, sizeof(len)); - if (len != sizeof(len)) + if (write(fd->fd, &len, sizeof(len) != + sizeof(len))) return -1; TAILQ_FOREACH(ifp, ctx->ifaces, next) { send_interface(fd->fd, ifp); @@ -1018,8 +1019,7 @@ handle_args(struct dhcpcd_ctx *ctx, struct fd_list *fd, int argc, char **argv) } } } - len = write(fd->fd, &len, sizeof(len)); - if (len != sizeof(len)) + if (write(fd->fd, &len, sizeof(len)) != sizeof(len)) return -1; opt = 0; while (argv[++opt] != NULL) { @@ -1108,7 +1108,8 @@ main(int argc, char **argv) struct interface *ifp; uint16_t family = 0; int opt, oi = 0, i; - size_t len; + time_t t; + ssize_t len; #if defined(USE_SIGNALS) || !defined(THERE_IS_NO_FORK) pid_t pid; #endif @@ -1488,11 +1489,11 @@ main(int argc, char **argv) } } if (ctx.options & DHCPCD_MASTER) - i = ifo->timeout; + t = ifo->timeout; else if ((ifp = TAILQ_FIRST(ctx.ifaces))) - i = ifp->options->timeout; + t = ifp->options->timeout; else - i = 0; + t = 0; if (opt == 0 && ctx.options & DHCPCD_LINK && !(ctx.options & DHCPCD_WAITIP)) @@ -1500,10 +1501,10 @@ main(int argc, char **argv) syslog(LOG_WARNING, "no interfaces have a carrier"); if (daemonise(&ctx)) goto exit_success; - } else if (i > 0) { + } else if (t > 0) { if (ctx.options & DHCPCD_IPV4LL) ctx.options |= DHCPCD_TIMEOUT_IPV4LL; - eloop_timeout_add_sec(ctx.eloop, i, + eloop_timeout_add_sec(ctx.eloop, t, handle_exit_timeout, &ctx); } } diff --git a/dhcpcd.h b/dhcpcd.h index a580da89..d1fc19a3 100644 --- a/dhcpcd.h +++ b/dhcpcd.h @@ -56,11 +56,11 @@ struct interface { TAILQ_ENTRY(interface) next; char name[IF_NAMESIZE]; unsigned int index; - int flags; + unsigned int flags; sa_family_t family; unsigned char hwaddr[HWADDR_LEN]; - size_t hwlen; - int metric; + uint8_t hwlen; + unsigned int metric; int carrier; int wireless; char ssid[IF_SSIDSIZE]; @@ -119,7 +119,7 @@ struct dhcpcd_ctx { size_t dhcp6_opts_len; struct ipv6_ctx *ipv6; char **ra_restore; - ssize_t ra_restore_len; + size_t ra_restore_len; #ifndef __linux__ int ra_global; int ra_kernel_set; @@ -142,10 +142,10 @@ pid_t daemonise(struct dhcpcd_ctx *); struct interface *find_interface(struct dhcpcd_ctx *, const char *); int handle_args(struct dhcpcd_ctx *, struct fd_list *, int, char **); -void handle_carrier(struct dhcpcd_ctx *, int, int, const char *); +void handle_carrier(struct dhcpcd_ctx *, int, unsigned int, const char *); void handle_interface(void *, int, const char *); void handle_hwaddr(struct dhcpcd_ctx *, const char *, - const unsigned char *, size_t); + const unsigned char *, uint8_t); void drop_interface(struct interface *, const char *); int select_profile(struct interface *, const char *); diff --git a/duid.c b/duid.c index 7f5d3d6c..8f99617d 100644 --- a/duid.c +++ b/duid.c @@ -70,14 +70,14 @@ duid_make(unsigned char *d, const struct interface *ifp, uint16_t type) /* time returns seconds from jan 1 1970, but DUID-LLT is * seconds from jan 1 2000 modulo 2^32 */ t = time(NULL) - DUID_TIME_EPOCH; - u32 = htonl(t & 0xffffffff); + u32 = htonl((uint32_t)t & 0xffffffff); memcpy(p, &u32, 4); p += 4; } /* Finally, add the MAC address of the interface */ memcpy(p, ifp->hwaddr, ifp->hwlen); p += ifp->hwlen; - return p - d; + return (size_t)(p - d); } #define DUID_STRLEN DUID_LEN * 3 diff --git a/if-bsd.c b/if-bsd.c index 650d1fcd..b880ed87 100644 --- a/if-bsd.c +++ b/if-bsd.c @@ -347,9 +347,9 @@ if_route(const struct rt *rt, int action) #undef ADDADDR #undef ADDSU - rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; + rtm.hdr.rtm_msglen = (unsigned short)(bp - (char *)&rtm); - retval = write(s, &rtm, l) == -1 ? -1 : 0; + retval = write(s, &rtm, rtm.hdr.rtm_msglen) == -1 ? -1 : 0; close(s); return retval; } @@ -461,7 +461,7 @@ if_route6(const struct rt6 *rt, int action) rtm.hdr.rtm_type = RTM_ADD; else rtm.hdr.rtm_type = RTM_DELETE; - rtm.hdr.rtm_flags = RTF_UP | rt->flags; + rtm.hdr.rtm_flags = RTF_UP | (int)rt->flags; rtm.hdr.rtm_addrs = RTA_DST | RTA_NETMASK; #ifdef SIOCGIFPRIORITY rtm.hdr.rtm_priority = rt->metric; @@ -524,9 +524,9 @@ if_route6(const struct rt6 *rt, int action) rtm.hdr.rtm_rmx.rmx_mtu = rt->mtu; } - rtm.hdr.rtm_msglen = l = bp - (char *)&rtm; + rtm.hdr.rtm_msglen = (unsigned short)(bp - (char *)&rtm); - retval = write(s, &rtm, l) == -1 ? -1 : 0; + retval = write(s, &rtm, rtm.hdr.rtm_msglen) == -1 ? -1 : 0; close(s); return retval; } @@ -650,7 +650,7 @@ manage_link(struct dhcpcd_ctx *ctx) break; } handle_carrier(ctx, len, - ifm->ifm_flags, ifname); + (unsigned int)ifm->ifm_flags, ifname); break; case RTM_DELETE: if (~rtm->rtm_addrs & diff --git a/if-linux.c b/if-linux.c index 5a61845f..6e564e60 100644 --- a/if-linux.c +++ b/if-linux.c @@ -155,12 +155,15 @@ get_netlink(struct dhcpcd_ctx *ctx, int fd, int flags, int (*callback)(struct dhcpcd_ctx *, struct nlmsghdr *)) { char *buf = NULL, *nbuf; - ssize_t buflen = 0, bytes; + ssize_t bytes; + size_t buflen; struct nlmsghdr *nlm; struct sockaddr_nl nladdr; socklen_t nladdr_len = sizeof(nladdr); - int r = -1; + int r; + buflen = 0; + r = -1; for (;;) { bytes = recv(fd, NULL, 0, flags | MSG_PEEK | MSG_DONTWAIT | MSG_TRUNC); @@ -172,16 +175,16 @@ get_netlink(struct dhcpcd_ctx *ctx, int fd, int flags, if (errno == EINTR) continue; goto eexit; - } else if (bytes == buflen) { + } else if ((size_t)bytes == buflen) { /* Support kernels older than 2.6.22 */ if (bytes == 0) bytes = 512; else bytes *= 2; } - if (buflen < bytes) { + if (buflen < (size_t)bytes) { /* Alloc 1 more so we work with older kernels */ - buflen = bytes + 1; + buflen = (size_t)bytes + 1; nbuf = realloc(buf, buflen); if (nbuf == NULL) goto eexit; @@ -227,18 +230,18 @@ static int err_netlink(__unused struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) { struct nlmsgerr *err; - int l; + size_t len; if (nlm->nlmsg_type != NLMSG_ERROR) return 0; - l = nlm->nlmsg_len - sizeof(*nlm); - if ((size_t)l < sizeof(*err)) { + len = nlm->nlmsg_len - sizeof(*nlm); + if (len < sizeof(*err)) { errno = EBADMSG; return -1; } err = (struct nlmsgerr *)NLMSG_DATA(nlm); if (err->error == 0) - return l; + return (int)len; errno = -err->error; return -1; } @@ -258,7 +261,8 @@ get_max_pid_t() static int link_route(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) { - int len, idx, metric; + size_t len; + unsigned int idx, metric; struct rtattr *rta; struct rtmsg *rtm; struct rt rt; @@ -268,7 +272,7 @@ link_route(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) return 0; len = nlm->nlmsg_len - sizeof(*nlm); - if ((size_t)len < sizeof(*rtm)) { + if (len < sizeof(*rtm)) { errno = EBADMSG; return -1; } @@ -302,12 +306,12 @@ link_route(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) sizeof(rt.gate.s_addr)); break; case RTA_OIF: - idx = *(int *)RTA_DATA(rta); + idx = *(unsigned int *)RTA_DATA(rta); if (if_indextoname(idx, ifn)) rt.iface = find_interface(ctx, ifn); break; case RTA_PRIORITY: - metric = *(int *)RTA_DATA(rta); + metric = *(unsigned int *)RTA_DATA(rta); break; } rta = RTA_NEXT(rta, len); @@ -326,7 +330,7 @@ link_route(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) static int link_addr(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) { - int len; + size_t len; struct rtattr *rta; struct ifaddrmsg *ifa; char ifn[IF_NAMESIZE + 1]; @@ -342,7 +346,7 @@ link_addr(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) return 0; len = nlm->nlmsg_len - sizeof(*nlm); - if ((size_t)len < sizeof(*ifa)) { + if (len < sizeof(*ifa)) { errno = EBADMSG; return -1; } @@ -436,18 +440,19 @@ handle_rename(struct dhcpcd_ctx *ctx, unsigned int ifindex, const char *ifname) static int link_netlink(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) { - int len; + int r; + size_t len; struct rtattr *rta, *hwaddr; struct ifinfomsg *ifi; char ifn[IF_NAMESIZE + 1]; struct interface *ifp; - len = link_route(ctx, nlm); - if (len != 0) - return len; - len = link_addr(ctx, nlm); - if (len != 0) - return len; + r = link_route(ctx, nlm); + if (r != 0) + return r; + r = link_addr(ctx, nlm); + if (r != 0) + return r; if (nlm->nlmsg_type != RTM_NEWLINK && nlm->nlmsg_type != RTM_DELLINK) return 0; @@ -497,7 +502,7 @@ link_netlink(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) } /* Check for interface name change */ - if (handle_rename(ctx, ifi->ifi_index, ifn)) + if (handle_rename(ctx, (unsigned int)ifi->ifi_index, ifn)) return 1; /* Check for a new interface */ @@ -512,9 +517,11 @@ link_netlink(struct dhcpcd_ctx *ctx, struct nlmsghdr *nlm) /* Re-read hardware address and friends */ if (!(ifi->ifi_flags & IFF_UP) && hwaddr) { - len = l2addr_len(ifi->ifi_type); - if (hwaddr->rta_len == RTA_LENGTH(len)) - handle_hwaddr(ctx, ifn, RTA_DATA(hwaddr), len); + short l; + + l = l2addr_len(ifi->ifi_type); + if (hwaddr->rta_len == RTA_LENGTH(l)) + handle_hwaddr(ctx, ifn, RTA_DATA(hwaddr), l); } handle_carrier(ctx, ifi->ifi_flags & IFF_RUNNING ? LINK_UP : LINK_DOWN, @@ -565,10 +572,10 @@ send_netlink(struct dhcpcd_ctx *ctx, struct nlmsghdr *hdr) ((struct rtattr *)(((ptrdiff_t)(nmsg))+NLMSG_ALIGN((nmsg)->nlmsg_len))) static int -add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, - const void *data, int alen) +add_attr_l(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, + const void *data, unsigned short alen) { - int len = RTA_LENGTH(alen); + unsigned short len = RTA_LENGTH(alen); struct rtattr *rta; if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) { @@ -586,9 +593,10 @@ add_attr_l(struct nlmsghdr *n, unsigned int maxlen, int type, } static int -add_attr_32(struct nlmsghdr *n, unsigned int maxlen, int type, uint32_t data) +add_attr_32(struct nlmsghdr *n, unsigned short maxlen, unsigned short type, + uint32_t data) { - int len = RTA_LENGTH(sizeof(data)); + unsigned short len = RTA_LENGTH(sizeof(data)); struct rtattr *rta; if (NLMSG_ALIGN(n->nlmsg_len) + len > maxlen) { @@ -641,7 +649,7 @@ if_address(const struct interface *iface, nlm.ifa.ifa_prefixlen = inet_ntocidr(*netmask); /* This creates the aliased interface */ add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LABEL, - iface->name, strlen(iface->name) + 1); + iface->name, (unsigned short)(strlen(iface->name) + 1)); add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, &address->s_addr, sizeof(address->s_addr)); if (action >= 0 && broadcast) @@ -738,7 +746,7 @@ if_address6(const struct ipv6_addr *ap, int action) nlm.ifa.ifa_prefixlen = ap->prefix_len; /* This creates the aliased interface */ add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LABEL, - ap->iface->name, strlen(ap->iface->name) + 1); + ap->iface->name, (unsigned short)(strlen(ap->iface->name) + 1)); add_attr_l(&nlm.hdr, sizeof(nlm), IFA_LOCAL, &ap->addr.s6_addr, sizeof(ap->addr.s6_addr)); @@ -756,10 +764,10 @@ if_address6(const struct ipv6_addr *ap, int action) } static int -rta_add_attr_32(struct rtattr *rta, unsigned int maxlen, - int type, uint32_t data) +rta_add_attr_32(struct rtattr *rta, unsigned short maxlen, + unsigned short type, uint32_t data) { - unsigned int len = RTA_LENGTH(sizeof(data)); + unsigned short len = RTA_LENGTH(sizeof(data)); struct rtattr *subrta; if (RTA_ALIGN(rta->rta_len) + len > maxlen) { @@ -771,7 +779,7 @@ rta_add_attr_32(struct rtattr *rta, unsigned int maxlen, subrta->rta_type = type; subrta->rta_len = len; memcpy(RTA_DATA(subrta), &data, sizeof(data)); - rta->rta_len = NLMSG_ALIGN(rta->rta_len) + len; + rta->rta_len = (unsigned short)(NLMSG_ALIGN(rta->rta_len) + len); return 0; } diff --git a/if-options.c b/if-options.c index 51743eae..759d1e8c 100644 --- a/if-options.c +++ b/if-options.c @@ -262,9 +262,9 @@ add_environ(struct if_options *ifo, const char *value, int uniq) #define parse_string(buf, len, arg) parse_string_hwaddr(buf, len, arg, 0) static ssize_t -parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) +parse_string_hwaddr(char *sbuf, size_t slen, const char *str, int clid) { - ssize_t l; + size_t l; const char *p; int i, punt_last = 0; char c[4]; @@ -277,14 +277,14 @@ parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) if (*p == '"') punt_last = 1; } else { - l = hwaddr_aton(NULL, str); - if (l > 1) { + l = (size_t)hwaddr_aton(NULL, str); + if ((ssize_t) l != -1 && l > 1) { if (l > slen) { errno = ENOBUFS; return -1; } hwaddr_aton((uint8_t *)sbuf, str); - return l; + return (ssize_t)l; } } @@ -338,7 +338,7 @@ parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) } if (c[1] != '\0' && sbuf) { c[2] = '\0'; - *sbuf++ = strtol(c, NULL, 16); + *sbuf++ = (char)strtol(c, NULL, 16); } else l--; break; @@ -351,10 +351,10 @@ parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) c[i] = *str++; } if (c[2] != '\0' && sbuf) { - i = strtol(c, NULL, 8); + i = (int)strtol(c, NULL, 8); if (i > 255) i = 255; - *sbuf ++= i; + *sbuf ++= (char)i; } else l--; break; @@ -375,14 +375,14 @@ parse_string_hwaddr(char *sbuf, ssize_t slen, const char *str, int clid) *--sbuf = '\0'; l--; } - return l; + return (ssize_t)l; } static int parse_iaid1(uint8_t *iaid, const char *arg, size_t len, int n) { unsigned long l; - size_t s; + ssize_t s; uint32_t u32; char *np; @@ -442,7 +442,7 @@ splitv(int *argc, char **argv, const char *arg) free(o); return v; } - n = realloc(v, sizeof(char *) * ((*argc) + 1)); + n = realloc(v, sizeof(char *) * ((size_t)(*argc) + 1)); if (n == NULL) { syslog(LOG_ERR, "%s: %m", __func__); free(o); @@ -760,15 +760,14 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, case 'u': s = USERCLASS_MAX_LEN - ifo->userclass[0] - 1; s = parse_string((char *)ifo->userclass + - ifo->userclass[0] + 2, - s, arg); + ifo->userclass[0] + 2, (size_t)s, arg); if (s == -1) { syslog(LOG_ERR, "userclass: %m"); return -1; } if (s != 0) { - ifo->userclass[ifo->userclass[0] + 1] = s; - ifo->userclass[0] += s + 1; + ifo->userclass[ifo->userclass[0] + 1] = (uint8_t)s; + ifo->userclass[0] += (uint8_t)s + 1; } break; case 'v': @@ -819,16 +818,16 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, } } else { s = parse_string((char *)ifo->vendor + - ifo->vendor[0] + 3, s, arg); + ifo->vendor[0] + 3, (size_t)s, arg); } if (s == -1) { syslog(LOG_ERR, "vendor: %m"); return -1; } if (s != 0) { - ifo->vendor[ifo->vendor[0] + 1] = i; - ifo->vendor[ifo->vendor[0] + 2] = s; - ifo->vendor[0] += s + 2; + ifo->vendor[ifo->vendor[0] + 1] = (uint8_t)i; + ifo->vendor[ifo->vendor[0] + 2] = (uint8_t)s; + ifo->vendor[0] += (uint8_t)s + 2; } break; case 'w': @@ -863,13 +862,13 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, /* Commas to spaces for shell */ while ((p = strchr(arg, ','))) *p = ' '; - s = strlen("skip_hooks=") + strlen(arg) + 1; - p = malloc(sizeof(char) * s); + dl = strlen("skip_hooks=") + strlen(arg) + 1; + p = malloc(sizeof(char) * dl); if (p == NULL) { syslog(LOG_ERR, "%s: %m", __func__); return -1; } - snprintf(p, s, "skip_hooks=%s", arg); + snprintf(p, dl, "skip_hooks=%s", arg); add_environ(ifo, p, 0); free(p); break; @@ -1026,11 +1025,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, } TAILQ_INSERT_TAIL(ifo->routes, rt, next); } else { - s = 0; + dl = 0; if (ifo->config != NULL) { - while (ifo->config[s] != NULL) { - if (strncmp(ifo->config[s], arg, - p - arg) == 0) + while (ifo->config[dl] != NULL) { + if (strncmp(ifo->config[dl], arg, + (size_t)(p - arg)) == 0) { p = strdup(arg); if (p == NULL) { @@ -1038,11 +1037,11 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, "%s: %m", __func__); return -1; } - free(ifo->config[s]); - ifo->config[s] = p; + free(ifo->config[dl]); + ifo->config[dl] = p; return 1; } - s++; + dl++; } } p = strdup(arg); @@ -1050,14 +1049,14 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, syslog(LOG_ERR, "%s: %m", __func__); return -1; } - nconf = realloc(ifo->config, sizeof(char *) * (s + 2)); + nconf = realloc(ifo->config, sizeof(char *) * (dl + 2)); if (nconf == NULL) { syslog(LOG_ERR, "%s: %m", __func__); return -1; } ifo->config = nconf; - ifo->config[s] = p; - ifo->config[s + 1] = NULL; + ifo->config[dl] = p; + ifo->config[dl + 1] = NULL; } break; case 'W': @@ -1202,7 +1201,7 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, syslog(LOG_ERR, "cannot specify a different IA type"); return -1; } - ifo->ia_type = i; + ifo->ia_type = (uint16_t)i; if (arg == NULL) break; fp = strwhite(arg); @@ -1278,7 +1277,10 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, sla->sla_set = 0; else { errno = 0; - sla->sla = atoint(p); + i = atoint(p); + if (i == -1) + goto err_sla; + sla->sla = (uint32_t)i; if (sla->sla == 0 && ia->sla_len > 1) { syslog(LOG_ERR, "%s: cannot" " assign multiple prefixes" @@ -1291,10 +1293,10 @@ parse_option(struct dhcpcd_ctx *ctx, const char *ifname, struct if_options *ifo, goto err_sla; } if (np) { - sla->prefix_len = atoint(np); - if (sla->prefix_len < 0 || - sla->prefix_len > 128) + i = atoint(np); + if (i < 0 || i > 128) goto err_sla; + sla->prefix_len = (uint8_t)i; } else sla->prefix_len = 64; } else { @@ -1550,7 +1552,7 @@ err_sla: free_dhcp_opt_embenc(ndop); ndop->option = u; /* could have been 0 */ ndop->type = t; - ndop->len = l; + ndop->len = (size_t)l; ndop->var = np; /* Save the define for embed and encap options */ if (opt == O_DEFINE || opt == O_DEFINE6 || opt == O_VENDOPT) @@ -1574,18 +1576,19 @@ err_sla: syslog(LOG_ERR, "%s: %m", __func__); return -1; } - if (s + (sizeof(uint16_t) * 2) > UINT16_MAX) { + dl = (size_t)s; + if (dl + (sizeof(uint16_t) * 2) > UINT16_MAX) { syslog(LOG_ERR, "vendor class is too big"); return -1; } - np = malloc(s); + np = malloc(dl); if (np == NULL) { syslog(LOG_ERR, "%s: %m", __func__); return -1; } - parse_string(np, s, fp); + parse_string(np, dl, fp); } else { - s = 0; + dl = 0; np = NULL; } vivco = realloc(ifo->vivco, sizeof(*ifo->vivco) * @@ -1597,7 +1600,7 @@ err_sla: ifo->vivco = vivco; ifo->vivco_en = u; vivco = &ifo->vivco[ifo->vivco_len++]; - vivco->len = s; + vivco->len = dl; vivco->data = (uint8_t *)np; break; case O_AUTHPROTOCOL: @@ -1674,8 +1677,13 @@ err_sla: return -1; } *fp++ = '\0'; - token->realm_len = parse_string(NULL, 0, arg); - if (token->realm_len) { + s = parse_string(NULL, 0, arg); + if (s == -1) { + syslog(LOG_ERR, "realm_len: %m"); + return -1; + } + if (s) { + token->realm_len = (size_t)s; token->realm = malloc(token->realm_len); if (token->realm == NULL) { free(token); @@ -1684,6 +1692,9 @@ err_sla: } parse_string((char *)token->realm, token->realm_len, arg); + } else { + token->realm_len = 0; + token->realm = NULL; } arg = fp; fp = strend(arg); @@ -1720,13 +1731,15 @@ err_sla: } } arg = fp; - token->key_len = parse_string(NULL, 0, arg); - if (token->key_len == 0) { - syslog(LOG_ERR, "authtoken needs a key"); + s = parse_string(NULL, 0, arg); + if (s == -1 || s == 0) { + syslog(LOG_ERR, s == -1 ? "token_len: %m" : + "authtoken needs a key"); free(token->realm); free(token); return -1; } + token->key_len = (size_t)s; token->key = malloc(token->key_len); parse_string((char *)token->key, token->key_len, arg); TAILQ_INSERT_TAIL(&ifo->auth.tokens, token, next); @@ -1856,7 +1869,8 @@ read_config(struct dhcpcd_ctx *ctx, ifo->auth.options |= DHCPCD_AUTH_REQUIRE; TAILQ_INIT(&ifo->auth.tokens); - ifo->vendorclassid[0] = dhcp_vendor((char *)ifo->vendorclassid + 1, + ifo->vendorclassid[0] = + (uint8_t)dhcp_vendor((char *)ifo->vendorclassid + 1, sizeof(ifo->vendorclassid) - 1); buf = NULL; diff --git a/if-options.h b/if-options.h index a7cfa5e6..9624fd2d 100644 --- a/if-options.h +++ b/if-options.h @@ -108,7 +108,7 @@ extern const struct option cf_options[]; struct if_sla { char ifname[IF_NAMESIZE]; uint32_t sla; - short prefix_len; + uint8_t prefix_len; int8_t sla_set; }; @@ -121,7 +121,7 @@ struct if_ia { }; struct vivco { - uint16_t len; + size_t len; uint8_t *data; }; @@ -151,7 +151,7 @@ struct if_options { char hostname[HOSTNAME_MAX_LEN + 1]; /* We don't store the length */ int fqdn; uint8_t vendorclassid[VENDORCLASSID_MAX_LEN + 2]; - char clientid[CLIENTID_MAX_LEN + 2]; + uint8_t clientid[CLIENTID_MAX_LEN + 2]; uint8_t userclass[USERCLASS_MAX_LEN + 2]; uint8_t vendor[VENDOR_MAX_LEN + 2]; diff --git a/ipv4.c b/ipv4.c index 4f1c5403..317cfff9 100644 --- a/ipv4.c +++ b/ipv4.c @@ -61,10 +61,10 @@ #undef IPV4_LOOPBACK_ROUTE #endif -int +uint8_t inet_ntocidr(struct in_addr address) { - int cidr = 0; + uint8_t cidr = 0; uint32_t mask = htonl(address.s_addr); while (mask) { diff --git a/ipv4.h b/ipv4.h index 7ac93448..c048cb6d 100644 --- a/ipv4.h +++ b/ipv4.h @@ -36,7 +36,7 @@ struct rt { struct in_addr net; struct in_addr gate; const struct interface *iface; - int metric; + unsigned int metric; struct in_addr src; }; TAILQ_HEAD(rt_head, rt); @@ -60,7 +60,7 @@ struct ipv4_state { #ifdef INET int ipv4_init(struct dhcpcd_ctx *); -int inet_ntocidr(struct in_addr); +uint8_t inet_ntocidr(struct in_addr); int inet_cidrtoaddr(int, struct in_addr *); uint32_t ipv4_getnetmask(uint32_t); int ipv4_addrexists(struct dhcpcd_ctx *, const struct in_addr *); @@ -93,8 +93,8 @@ void ipv4_freeroutes(struct rt_head *); int ipv4_opensocket(struct interface *, int); ssize_t ipv4_sendrawpacket(const struct interface *, - int, const void *, ssize_t); -ssize_t ipv4_getrawpacket(struct interface *, int, void *, ssize_t, int *); + int, const void *, size_t); +ssize_t ipv4_getrawpacket(struct interface *, int, void *, size_t, int *); void ipv4_free(struct interface *); void ipv4_ctxfree(struct dhcpcd_ctx *); #else diff --git a/ipv6.c b/ipv6.c index b20bcd2a..3d502b8a 100644 --- a/ipv6.c +++ b/ipv6.c @@ -135,11 +135,11 @@ ipv6_init(struct dhcpcd_ctx *dhcpcd_ctx) } ssize_t -ipv6_printaddr(char *s, ssize_t sl, const uint8_t *d, const char *ifname) +ipv6_printaddr(char *s, size_t sl, const uint8_t *d, const char *ifname) { char buf[INET6_ADDRSTRLEN]; const char *p; - ssize_t l; + size_t l; p = inet_ntop(AF_INET6, d, buf, sizeof(buf)); if (p == NULL) @@ -150,7 +150,7 @@ ipv6_printaddr(char *s, ssize_t sl, const uint8_t *d, const char *ifname) l += 1 + strlen(ifname); if (s == NULL) - return l; + return (ssize_t)l; if (sl < l) { errno = ENOMEM; @@ -163,7 +163,7 @@ ipv6_printaddr(char *s, ssize_t sl, const uint8_t *d, const char *ifname) s += strlcpy(s, ifname, sl); } *s = '\0'; - return l; + return (ssize_t)l; } int @@ -202,11 +202,11 @@ ipv6_makeprefix(struct in6_addr *prefix, const struct in6_addr *addr, int len) bytelen = len / NBBY; bitlen = len % NBBY; - memcpy(&prefix->s6_addr, &addr->s6_addr, bytelen); + memcpy(&prefix->s6_addr, &addr->s6_addr, (size_t)bytelen); if (bitlen != 0) prefix->s6_addr[bytelen] >>= NBBY - bitlen; memset((char *)prefix->s6_addr + bytelen, 0, - sizeof(prefix->s6_addr) - bytelen); + sizeof(prefix->s6_addr) - (size_t)bytelen); return 0; } @@ -232,7 +232,7 @@ ipv6_mask(struct in6_addr *mask, int len) return 0; } -int +uint8_t ipv6_prefixlen(const struct in6_addr *mask) { int x = 0, y; @@ -257,13 +257,13 @@ ipv6_prefixlen(const struct in6_addr *mask) */ if (p < lim) { if (y != 0 && (*p & (0x00ff >> y)) != 0) - return -1; + return 0; for (p = p + 1; p < lim; p++) if (*p != 0) - return -1; + return 0; } - return x * NBBY + y; + return (uint8_t)(x * NBBY + y); } static void @@ -299,23 +299,23 @@ h64_to_in6(uint64_t vhigh, uint64_t vlow, struct in6_addr *add) { uint8_t *p = (uint8_t *)&add->s6_addr; - p[0] = vhigh >> 56; - p[1] = vhigh >> 48; - p[2] = vhigh >> 40; - p[3] = vhigh >> 32; - p[4] = vhigh >> 24; - p[5] = vhigh >> 16; - p[6] = vhigh >> 8; - p[7] = vhigh; + p[0] = (uint8_t)(vhigh >> 56); + p[1] = (uint8_t)(vhigh >> 48); + p[2] = (uint8_t)(vhigh >> 40); + p[3] = (uint8_t)(vhigh >> 32); + p[4] = (uint8_t)(vhigh >> 24); + p[5] = (uint8_t)(vhigh >> 16); + p[6] = (uint8_t)(vhigh >> 8); + p[7] = (uint8_t)vhigh; p += 8; - p[0] = vlow >> 56; - p[1] = vlow >> 48; - p[2] = vlow >> 40; - p[3] = vlow >> 32; - p[4] = vlow >> 24; - p[5] = vlow >> 16; - p[6] = vlow >> 8; - p[7] = vlow; + p[0] = (uint8_t)(vlow >> 56); + p[1] = (uint8_t)(vlow >> 48); + p[2] = (uint8_t)(vlow >> 40); + p[3] = (uint8_t)(vlow >> 32); + p[4] = (uint8_t)(vlow >> 24); + p[5] = (uint8_t)(vlow >> 16); + p[6] = (uint8_t)(vlow >> 8); + p[7] = (uint8_t)vlow; } int diff --git a/ipv6.h b/ipv6.h index c5d29e0b..e2d4183b 100644 --- a/ipv6.h +++ b/ipv6.h @@ -73,7 +73,7 @@ struct ipv6_addr { TAILQ_ENTRY(ipv6_addr) next; struct interface *iface; struct in6_addr prefix; - int prefix_len; + uint8_t prefix_len; uint32_t prefix_vltime; uint32_t prefix_pltime; struct in6_addr addr; @@ -108,7 +108,7 @@ struct rt6 { struct in6_addr gate; const struct interface *iface; unsigned int flags; - int metric; + unsigned int metric; unsigned int mtu; }; TAILQ_HEAD(rt6_head, rt6); @@ -159,12 +159,12 @@ struct ipv6_ctx { #ifdef INET6 struct ipv6_ctx *ipv6_init(struct dhcpcd_ctx *); -ssize_t ipv6_printaddr(char *, ssize_t, const uint8_t *, const char *); +ssize_t ipv6_printaddr(char *, size_t, const uint8_t *, const char *); int ipv6_makeaddr(struct in6_addr *, const struct interface *, const struct in6_addr *, int); int ipv6_makeprefix(struct in6_addr *, const struct in6_addr *, int); int ipv6_mask(struct in6_addr *, int); -int ipv6_prefixlen(const struct in6_addr *); +uint8_t ipv6_prefixlen(const struct in6_addr *); 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 *); diff --git a/ipv6nd.c b/ipv6nd.c index d67e88da..74b0b71e 100644 --- a/ipv6nd.c +++ b/ipv6nd.c @@ -535,9 +535,10 @@ ipv6nd_dadcallback(void *arg) static void ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, - struct icmp6_hdr *icp, ssize_t len) + struct icmp6_hdr *icp, size_t len) { - ssize_t l, m, n, olen; + size_t olen, l, m, n; + ssize_t r; struct nd_router_advert *nd_ra; struct nd_opt_prefix_info *pi; struct nd_opt_mtu *mtu; @@ -556,7 +557,7 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, struct timeval expire; uint8_t new_rap, new_data; - if ((size_t)len < sizeof(struct nd_router_advert)) { + if (len < sizeof(struct nd_router_advert)) { syslog(LOG_ERR, "IPv6 RA packet too short from %s", ctx->sfrom); return; } @@ -665,7 +666,7 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, p = ((uint8_t *)icp) + sizeof(struct nd_router_advert); lifetime = ~0U; for (; len > 0; p += olen, len -= olen) { - if ((size_t)len < sizeof(struct nd_opt_hdr)) { + if (len < sizeof(struct nd_opt_hdr)) { syslog(LOG_ERR, "%s: short option", ifp->name); break; } @@ -803,9 +804,9 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, for (n = ndo->nd_opt_len - 1; n > 1; n -= 2, op += sizeof(addr.s6_addr)) { - m = ipv6_printaddr(NULL, 0, op, ifp->name); - if (m != -1) - l += m + 1; + r = ipv6_printaddr(NULL, 0, op, ifp->name); + if (r != -1) + l += (size_t)r + 1; } op = (uint8_t *)ndo; op += offsetof(struct nd_opt_rdnss, @@ -816,11 +817,11 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, for (n = ndo->nd_opt_len - 1; n > 1; n -= 2, op += sizeof(addr.s6_addr)) { - m = ipv6_printaddr(tmp, l, op, + r = ipv6_printaddr(tmp, l, op, ifp->name); - if (m != -1) { - l -= (m + 1); - tmp += m; + if (r != -1) { + l -= ((size_t)r + 1); + tmp += (size_t)r; *tmp++ = ' '; } } @@ -838,21 +839,22 @@ ipv6nd_handlera(struct ipv6_ctx *ctx, struct interface *ifp, nd_opt_dnssl_lifetime); op += sizeof(dnssl->nd_opt_dnssl_lifetime); n = (dnssl->nd_opt_dnssl_len - 1) * 8; - l = decode_rfc3397(NULL, 0, n, op); - if (l < 1) { + r = decode_rfc3397(NULL, 0, op, n); + if (r < 1) { syslog(LOG_ERR, "%s: invalid DNSSL option", ifp->name); } else { + l = (size_t)r; tmp = malloc(l); if (tmp) { - decode_rfc3397(tmp, l, n, op); - n = print_string(NULL, 0, - l - 1, (const uint8_t *)tmp); + decode_rfc3397(tmp, l, op, n); + l -= 1; + n = (size_t)print_string(NULL, 0, + (const uint8_t *)tmp, l); opt = malloc(n); if (opt) print_string(opt, n, - l - 1, - (const uint8_t *)tmp); + (const uint8_t *)tmp, l); free(tmp); } } @@ -962,17 +964,14 @@ ipv6nd_has_ra(const struct interface *ifp) ssize_t ipv6nd_env(char **env, const char *prefix, const struct interface *ifp) { - ssize_t l; - size_t len; + size_t i, len, l; const struct ra *rap; const struct ra_opt *rao; - int i; char buffer[32]; const char *optn; char **pref, **mtu, **rdnss, **dnssl, ***var, *new; - i = 0; - l = 0; + i = l = 0; TAILQ_FOREACH(rap, ifp->ctx->ipv6->ra_routers, next) { i++; if (rap->iface != ifp) @@ -980,8 +979,7 @@ ipv6nd_env(char **env, const char *prefix, const struct interface *ifp) if (env) { snprintf(buffer, sizeof(buffer), "ra%d_from", i); - if (setvar(&env, prefix, buffer, rap->sfrom) == -1) - return -1; + setvar(&env, prefix, buffer, rap->sfrom); } l++; @@ -1023,7 +1021,7 @@ ipv6nd_env(char **env, const char *prefix, const struct interface *ifp) continue; } else new++; - len = (new - **var) + + len = (size_t)(new - **var) + strlen(rao->option) + 1; if (len > strlen(**var)) new = realloc(**var, len); @@ -1033,7 +1031,9 @@ ipv6nd_env(char **env, const char *prefix, const struct interface *ifp) **var = new; new = strchr(**var, '='); if (new) { - len -= (new - **var); + len -= + (size_t) + (new - **var); strlcpy(new + 1, rao->option, len - 1); @@ -1045,30 +1045,27 @@ ipv6nd_env(char **env, const char *prefix, const struct interface *ifp) } len = strlen(rao->option) + 1; new = realloc(**var, strlen(**var) + 1 + len); - if (new == NULL) - return -1; - **var = new; - new += strlen(new); - *new++ = ' '; - strlcpy(new, rao->option, len); + if (new) { + **var = new; + new += strlen(new); + *new++ = ' '; + strlcpy(new, rao->option, len); + } else + syslog(LOG_ERR, "%s: %m", __func__); continue; } if (env) { snprintf(buffer, sizeof(buffer), "ra%d_%s", i, optn); - if (setvar(&env, prefix, buffer, rao->option) - == -1) - return -1; + setvar(&env, prefix, buffer, rao->option); } } } - if (env) { - if (setvard(&env, prefix, "ra_count", i) == -1) - return -1; - } + if (env) + setvard(&env, prefix, "ra_count", i); l++; - return l; + return (ssize_t)l; } void @@ -1330,7 +1327,7 @@ ipv6nd_cancelproberouter(struct ra *rap) static void ipv6nd_handlena(struct ipv6_ctx *ctx, struct interface *ifp, - struct icmp6_hdr *icp, ssize_t len) + struct icmp6_hdr *icp, size_t len) { struct nd_neighbor_advert *nd_na; struct ra *rap; @@ -1476,10 +1473,10 @@ ipv6nd_handledata(void *arg) if (icp->icmp6_code == 0) { switch(icp->icmp6_type) { case ND_NEIGHBOR_ADVERT: - ipv6nd_handlena(ctx, ifp, icp, len); + ipv6nd_handlena(ctx, ifp, icp, (size_t)len); return; case ND_ROUTER_ADVERT: - ipv6nd_handlera(ctx, ifp, icp, len); + ipv6nd_handlera(ctx, ifp, icp, (size_t)len); return; } } diff --git a/ipv6nd.h b/ipv6nd.h index 30691c8b..3688a989 100644 --- a/ipv6nd.h +++ b/ipv6nd.h @@ -48,7 +48,7 @@ struct ra { struct in6_addr from; char sfrom[INET6_ADDRSTRLEN]; unsigned char *data; - ssize_t data_len; + size_t data_len; struct timeval received; unsigned char flags; uint32_t lifetime; diff --git a/lpf.c b/lpf.c index 4f4a27e7..2501ed0b 100644 --- a/lpf.c +++ b/lpf.c @@ -87,15 +87,15 @@ ipv4_opensocket(struct interface *ifp, int protocol) memset(&su, 0, sizeof(su)); su.sll.sll_family = PF_PACKET; su.sll.sll_protocol = htons(protocol); - su.sll.sll_ifindex = ifp->index; + su.sll.sll_ifindex = (int)ifp->index; /* Install the DHCP filter */ memset(&pf, 0, sizeof(pf)); if (protocol == ETHERTYPE_ARP) { pf.filter = UNCONST(arp_bpf_filter); - pf.len = arp_bpf_filter_len; + pf.len = (unsigned int)arp_bpf_filter_len; } else { pf.filter = UNCONST(dhcp_bpf_filter); - pf.len = dhcp_bpf_filter_len; + pf.len = (unsigned int)dhcp_bpf_filter_len; } if (setsockopt(s, SOL_SOCKET, SO_ATTACH_FILTER, &pf, sizeof(pf)) != 0) goto eexit; @@ -117,7 +117,7 @@ eexit: ssize_t ipv4_sendrawpacket(const struct interface *ifp, int protocol, - const void *data, ssize_t len) + const void *data, size_t len) { const struct dhcp_state *state; union sockunion { @@ -130,9 +130,9 @@ ipv4_sendrawpacket(const struct interface *ifp, int protocol, memset(&su, 0, sizeof(su)); su.sll.sll_family = AF_PACKET; su.sll.sll_protocol = htons(protocol); - su.sll.sll_ifindex = ifp->index; + su.sll.sll_ifindex = (int)ifp->index; su.sll.sll_hatype = htons(ifp->family); - su.sll.sll_halen = ifp->hwlen; + su.sll.sll_halen = (unsigned char)ifp->hwlen; if (ifp->family == ARPHRD_INFINIBAND) memcpy(&su.sll.sll_addr, &ipv4_bcast_addr, sizeof(ipv4_bcast_addr)); @@ -149,7 +149,7 @@ ipv4_sendrawpacket(const struct interface *ifp, int protocol, ssize_t ipv4_getrawpacket(struct interface *ifp, int protocol, - void *data, ssize_t len, int *partialcsum) + void *data, size_t len, int *partialcsum) { struct iovec iov = { .iov_base = data, diff --git a/net.c b/net.c index 6e269a0e..96f1b0bd 100644 --- a/net.c +++ b/net.c @@ -179,7 +179,7 @@ carrier_status(struct interface *iface) close(s); return LINK_UNKNOWN; } - iface->flags = ifr.ifr_flags; + iface->flags = (unsigned int)ifr.ifr_flags; r = LINK_UNKNOWN; #ifdef SIOCGIFMEDIA @@ -222,7 +222,7 @@ up_interface(struct interface *iface) if (ioctl(s, SIOCSIFFLAGS, &ifr) == 0) r = 0; } - iface->flags = ifr.ifr_flags; + iface->flags = (unsigned int)ifr.ifr_flags; } close(s); return r; @@ -233,7 +233,8 @@ discover_interfaces(struct dhcpcd_ctx *ctx, int argc, char * const *argv) { struct ifaddrs *ifaddrs, *ifa; char *p; - int i, sdl_type; + int i; + sa_family_t sdl_type; struct if_head *ifs; struct interface *ifp; #ifdef __linux__ @@ -425,7 +426,7 @@ discover_interfaces(struct dhcpcd_ctx *ctx, int argc, char * const *argv) memcpy(ifp->hwaddr, CLLADDR(sdl), ifp->hwlen); #elif AF_PACKET sll = (const struct sockaddr_ll *)(void *)ifa->ifa_addr; - ifp->index = sll->sll_ifindex; + ifp->index = (unsigned int)sll->sll_ifindex; ifp->family = sdl_type = sll->sll_hatype; ifp->hwlen = sll->sll_halen; if (ifp->hwlen != 0) diff --git a/platform-bsd.c b/platform-bsd.c index ac19386e..417729cb 100644 --- a/platform-bsd.c +++ b/platform-bsd.c @@ -185,7 +185,7 @@ check_ipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own) if (ifname) { #ifdef ND6_IFF_ACCEPT_RTADV - int i; + size_t i; char *p, **nrest; #endif diff --git a/platform-linux.c b/platform-linux.c index af66c242..0be32065 100644 --- a/platform-linux.c +++ b/platform-linux.c @@ -157,7 +157,8 @@ restore_kernel_ra(struct dhcpcd_ctx *ctx) int check_ipv6(struct dhcpcd_ctx *ctx, const char *ifname, int own) { - int ra, i; + int ra; + size_t i; char path[256], *p, **nrest; if (ifname == NULL) diff --git a/script.c b/script.c index 3bdf82a6..3ee71d21 100644 --- a/script.c +++ b/script.c @@ -135,10 +135,10 @@ make_var(const char *prefix, const char *var) static int -append_config(char ***env, ssize_t *len, +append_config(char ***env, size_t *len, const char *prefix, const char *const *config) { - ssize_t i, j, e1; + size_t i, j, e1; char **ne, *eq, **nep, *p; int ret; @@ -149,7 +149,7 @@ append_config(char ***env, ssize_t *len, ret = 0; for (i = 0; config[i] != NULL; i++) { eq = strchr(config[i], '='); - e1 = eq - config[i] + 1; + e1 = (size_t)(eq - config[i] + 1); for (j = 0; j < *len; j++) { if (strncmp(ne[j] + strlen(prefix) + 1, config[i], e1) == 0) @@ -188,7 +188,7 @@ append_config(char ***env, ssize_t *len, } #endif -static size_t +static ssize_t arraytostr(const char *const *argv, char **s) { const char *const *ap; @@ -211,14 +211,15 @@ arraytostr(const char *const *argv, char **s) p += l; ap++; } - return len; + return (ssize_t)len; } static ssize_t make_env(const struct interface *ifp, const char *reason, char ***argv) { char **env, **nenv, *p; - ssize_t e, elen, l; + size_t e, elen, l; + ssize_t n; const struct if_options *ifo = ifp->options; const struct interface *ifp2; #ifdef INET @@ -269,7 +270,7 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) else elen = 11; -#define EMALLOC(i, l) if ((env[(i)] = malloc(l)) == NULL) goto eexit; +#define EMALLOC(i, l) if ((env[(i)] = malloc((l))) == NULL) goto eexit; /* Make our env */ env = calloc(1, sizeof(char *) * (elen + 1)); if (env == NULL) @@ -360,16 +361,19 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) } #ifdef INET if (dhcp && state && state->old) { - e = dhcp_env(NULL, NULL, state->old, ifp); - if (e > 0) { - nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + n = dhcp_env(NULL, NULL, state->old, ifp); + if (n == -1) + goto eexit; + if (n > 0) { + nenv = realloc(env, sizeof(char *) * + (elen + (size_t)n + 1)); if (nenv == NULL) goto eexit; env = nenv; - l = dhcp_env(env + elen, "old", state->old, ifp); - if (l == -1) + n = dhcp_env(env + elen, "old", state->old, ifp); + if (n == -1) goto eexit; - elen += l; + elen += (size_t)n; } if (append_config(&env, &elen, "old", (const char *const *)ifo->config) == -1) @@ -378,18 +382,19 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) #endif #ifdef INET6 if (dhcp6 && d6_state && d6_state->old) { - e = dhcp6_env(NULL, NULL, ifp, + n = dhcp6_env(NULL, NULL, ifp, d6_state->old, d6_state->old_len); - if (e > 0) { - nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (n > 0) { + nenv = realloc(env, sizeof(char *) * + (elen + (size_t)n + 1)); if (nenv == NULL) goto eexit; env = nenv; - l = dhcp6_env(env + elen, "old", ifp, + n = dhcp6_env(env + elen, "old", ifp, d6_state->old, d6_state->old_len); - if (l == -1) + if (n == -1) goto eexit; - elen += l; + elen += (size_t)n; } } #endif @@ -397,17 +402,18 @@ make_env(const struct interface *ifp, const char *reason, char ***argv) dumplease: #ifdef INET if (dhcp && state && state->new) { - e = dhcp_env(NULL, NULL, state->new, ifp); + n = dhcp_env(NULL, NULL, state->new, ifp); if (e > 0) { - nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + nenv = realloc(env, sizeof(char *) * + (elen + (size_t)n + 1)); if (nenv == NULL) goto eexit; env = nenv; - l = dhcp_env(env + elen, "new", + n = dhcp_env(env + elen, "new", state->new, ifp); - if (l == -1) + if (n == -1) goto eexit; - elen += l; + elen += (size_t)n; } if (append_config(&env, &elen, "new", (const char *const *)ifo->config) == -1) @@ -416,31 +422,33 @@ dumplease: #endif #ifdef INET6 if (dhcp6 && d6_state && d6_state->new) { - e = dhcp6_env(NULL, NULL, ifp, + n = dhcp6_env(NULL, NULL, ifp, d6_state->new, d6_state->new_len); - if (e > 0) { - nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + if (n > 0) { + nenv = realloc(env, sizeof(char *) * + (elen + (size_t)n + 1)); if (nenv == NULL) goto eexit; env = nenv; - l = dhcp6_env(env + elen, "new", ifp, + n = dhcp6_env(env + elen, "new", ifp, d6_state->new, d6_state->new_len); - if (l == -1) + if (n == -1) goto eexit; - elen += l; + elen += (size_t)n; } } if (ra) { - e = ipv6nd_env(NULL, NULL, ifp); - if (e > 0) { - nenv = realloc(env, sizeof(char *) * (elen + e + 1)); + n = ipv6nd_env(NULL, NULL, ifp); + if (n > 0) { + nenv = realloc(env, sizeof(char *) * + (elen + (size_t)n + 1)); if (nenv == NULL) goto eexit; env = nenv; - l = ipv6nd_env(env + elen, NULL, ifp); - if (l == -1) + n = ipv6nd_env(env + elen, NULL, ifp); + if (n == -1) goto eexit; - elen += l; + elen += (size_t)n; } } #endif @@ -466,7 +474,7 @@ dumplease: env[elen] = NULL; *argv = env; - return elen; + return (ssize_t)elen; eexit: syslog(LOG_ERR, "%s: %m", __func__); @@ -479,18 +487,18 @@ eexit: return -1; } -static int +static ssize_t send_interface1(int fd, const struct interface *iface, const char *reason) { char **env, **ep, *s; - ssize_t elen; + size_t elen; struct iovec iov[2]; - int retval; + ssize_t retval; if (make_env(iface, reason, &env) == -1) return -1; - elen = arraytostr((const char *const *)env, &s); - if (elen == -1) + elen = (size_t)arraytostr((const char *const *)env, &s); + if ((ssize_t)elen == -1) return -1; iov[0].iov_base = &elen; iov[0].iov_len = sizeof(ssize_t); @@ -558,7 +566,7 @@ script_runreason(const struct interface *ifp, const char *reason) char *argv[2]; char **env = NULL, **ep; char *path, *bigenv; - ssize_t e, elen = 0; + size_t e, elen = 0; pid_t pid; int status = 0; const struct fd_list *fd; @@ -575,10 +583,14 @@ script_runreason(const struct interface *ifp, const char *reason) ifp->name, argv[0], reason); /* Make our env */ - elen = make_env(ifp, reason, &env); + elen = (size_t)make_env(ifp, reason, &env); + if (elen == (size_t)-1) { + syslog(LOG_ERR, "%s: make_env: %m", ifp->name); + return -1; + } ep = realloc(env, sizeof(char *) * (elen + 2)); if (ep == NULL) { - elen = -1; + elen = 0; goto out; } env = ep; @@ -588,14 +600,14 @@ script_runreason(const struct interface *ifp, const char *reason) e = strlen("PATH") + strlen(path) + 2; env[elen] = malloc(e); if (env[elen] == NULL) { - elen = -1; + elen = 0; goto out; } snprintf(env[elen], e, "PATH=%s", path); } else { env[elen] = strdup(DEFAULT_PATH); if (env[elen] == NULL) { - elen = -1; + elen = 0; goto out; } } @@ -628,10 +640,15 @@ script_runreason(const struct interface *ifp, const char *reason) for (fd = ifp->ctx->control_fds; fd != NULL; fd = fd->next) { if (fd->listener) { if (bigenv == NULL) { - elen = arraytostr((const char *const *)env, + elen = (size_t)arraytostr((const char *const *)env, &bigenv); + if ((ssize_t)elen == -1) { + syslog(LOG_ERR, "%s: arraytostr: %m", + ifp->name); + continue; + } iov[0].iov_base = &elen; - iov[0].iov_len = sizeof(ssize_t); + iov[0].iov_len = sizeof(size_t); iov[1].iov_base = bigenv; iov[1].iov_len = elen; } @@ -647,7 +664,7 @@ out: while (*ep) free(*ep++); free(env); - if (elen == -1) + if (elen == 0) return -1; return WEXITSTATUS(status); }