From: Roy Marples Date: Tue, 19 May 2020 16:09:35 +0000 (+0000) Subject: Fix compile on Linux X-Git-Tag: v9.1.0~53 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=00185f719ddc13a4fdb39af51ae0d61d50fa548f;p=thirdparty%2Fdhcpcd.git Fix compile on Linux --- diff --git a/src/arp.c b/src/arp.c index 7cc4f8ae..b81277c9 100644 --- a/src/arp.c +++ b/src/arp.c @@ -222,7 +222,8 @@ arp_validate(const struct interface *ifp, struct arphdr *arp) } void -arp_packet(struct interface *ifp, uint8_t *data, size_t len, uint8_t bpf_flags) +arp_packet(struct interface *ifp, uint8_t *data, size_t len, + unsigned int bpf_flags) { size_t fl = bpf_frame_header_len(ifp), falen; const struct interface *ifn; diff --git a/src/arp.h b/src/arp.h index 460aeee6..f0728027 100644 --- a/src/arp.h +++ b/src/arp.h @@ -92,7 +92,7 @@ struct iarp_state { ((const struct iarp_state *)(ifp)->if_data[IF_DATA_ARP]) #ifdef ARP -void arp_packet(struct interface *, uint8_t *, size_t, uint8_t); +void arp_packet(struct interface *, uint8_t *, size_t, unsigned int); struct arp_state *arp_new(struct interface *, const struct in_addr *); void arp_probe(struct arp_state *); void arp_announce(struct arp_state *); diff --git a/src/bpf.h b/src/bpf.h index bf38fdff..866ae23c 100644 --- a/src/bpf.h +++ b/src/bpf.h @@ -29,9 +29,9 @@ #ifndef BPF_HEADER #define BPF_HEADER -#define BPF_EOF 0x01 -#define BPF_PARTIALCSUM 0x02 -#define BPF_BCAST 0x04 +#define BPF_EOF 0x01U +#define BPF_PARTIALCSUM 0x02U +#define BPF_BCAST 0x04U /* * Even though we program the BPF filter should we trust it? @@ -57,7 +57,7 @@ struct bpf { const struct interface *bpf_ifp; int bpf_fd; - uint8_t bpf_flags; + unsigned int bpf_flags; void *bpf_buffer; size_t bpf_size; size_t bpf_len; diff --git a/src/dhcp.c b/src/dhcp.c index c44912ca..744d672b 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -1676,7 +1676,7 @@ dhcp_sendudp(struct interface *ifp, struct in_addr *to, void *data, size_t len) struct udphdr udp = { .uh_sport = htons(BOOTPC), .uh_dport = htons(BOOTPS), - .uh_ulen = htons(sizeof(udp) + len), + .uh_ulen = htons((uint16_t)(sizeof(udp) + len)), }; struct iovec iov[] = { { .iov_base = &udp, .iov_len = sizeof(udp), }, @@ -3462,7 +3462,8 @@ dhcp_handlebootp(struct interface *ifp, struct bootp *bootp, size_t len, } void -dhcp_packet(struct interface *ifp, uint8_t *data, size_t len, uint8_t bpf_flags) +dhcp_packet(struct interface *ifp, uint8_t *data, size_t len, + unsigned int bpf_flags) { struct bootp *bootp; struct in_addr from; diff --git a/src/dhcp.h b/src/dhcp.h index dd0a63d0..d15e10be 100644 --- a/src/dhcp.h +++ b/src/dhcp.h @@ -256,7 +256,7 @@ ssize_t print_rfc3361(FILE *, const uint8_t *, size_t); ssize_t print_rfc3442(FILE *, const uint8_t *, size_t); int dhcp_openudp(struct in_addr *); -void dhcp_packet(struct interface *, uint8_t *, size_t, uint8_t); +void dhcp_packet(struct interface *, uint8_t *, size_t, unsigned int); void dhcp_recvmsg(struct dhcpcd_ctx *, struct msghdr *); void dhcp_printoptions(const struct dhcpcd_ctx *, const struct dhcp_opt *, size_t); diff --git a/src/dhcp6.c b/src/dhcp6.c index 777d30a8..e432bfa4 100644 --- a/src/dhcp6.c +++ b/src/dhcp6.c @@ -1214,7 +1214,7 @@ dhcp6_sendmessage(struct interface *ifp, void (*callback)(void *)) struct udphdr udp = { .uh_sport = htons(DHCP6_CLIENT_PORT), .uh_dport = htons(DHCP6_SERVER_PORT), - .uh_ulen = htons(sizeof(udp) + state->send_len), + .uh_ulen = htons((uint16_t)(sizeof(udp) + state->send_len)), }; struct iovec iov[] = { { .iov_base = &udp, .iov_len = sizeof(udp), }, diff --git a/src/if-linux.c b/src/if-linux.c index 62bb2398..32d380c4 100644 --- a/src/if-linux.c +++ b/src/if-linux.c @@ -1587,11 +1587,12 @@ if_initrt(struct dhcpcd_ctx *ctx, rb_tree_t *kroutes, int af) const char *bpf_name = "Packet Socket"; /* Linux is a special snowflake for opening BPF. */ -int -bpf_open(struct interface *ifp, int (*filter)(struct interface *, int)) +struct bpf * +bpf_open(const struct interface *ifp, + int (*filter)(const struct bpf *, const struct in_addr *), + const struct in_addr *ia) { - struct ipv4_state *state; - int s; + struct bpf *bpf; union sockunion { struct sockaddr sa; struct sockaddr_ll sll; @@ -1605,38 +1606,34 @@ bpf_open(struct interface *ifp, int (*filter)(struct interface *, int)) int n; #endif - /* Allocate a suitably large buffer for a single packet. */ - state = ipv4_getstate(ifp); - if (state == NULL) - return -1; - if (state->buffer_size < ETH_DATA_LEN) { - void *nb; - - if ((nb = realloc(state->buffer, ETH_DATA_LEN)) == NULL) - return -1; - state->buffer = nb; - state->buffer_size = ETH_DATA_LEN; - state->buffer_len = state->buffer_pos = 0; - } + bpf = calloc(1, sizeof(*bpf)); + if (bpf == NULL) + return NULL; + bpf->bpf_ifp = ifp; + /* Allocate a suitably large buffer for a single packet. */ + bpf->bpf_size = ETH_DATA_LEN; + bpf->bpf_buffer = malloc(bpf->bpf_size); + if (bpf->bpf_buffer == NULL) + goto eexit; -#define SF SOCK_CLOEXEC | SOCK_NONBLOCK - if ((s = xsocket(PF_PACKET, SOCK_RAW | SF, htons(ETH_P_ALL))) == -1) - return -1; -#undef SF + bpf->bpf_fd = xsocket(PF_PACKET, SOCK_RAW|SOCK_CXNB,htons(ETH_P_ALL)); + if (bpf->bpf_fd == -1) + goto eexit; /* We cannot validate the correct interface, * so we MUST set this first. */ - if (bind(s, &su.sa, sizeof(su.sll)) == -1) + if (bind(bpf->bpf_fd, &su.sa, sizeof(su.sll)) == -1) goto eexit; - if (filter(ifp, s) != 0) + if (filter(bpf, ia) != 0) goto eexit; /* In the ideal world, this would be set before the bind and filter. */ #ifdef PACKET_AUXDATA n = 1; - if (setsockopt(s, SOL_PACKET, PACKET_AUXDATA, &n, sizeof(n)) != 0) { + if (setsockopt(bpf->bpf_fd, SOL_PACKET, PACKET_AUXDATA, + &n, sizeof(n)) != 0) { if (errno != ENOPROTOOPT) goto eexit; } @@ -1651,25 +1648,25 @@ bpf_open(struct interface *ifp, int (*filter)(struct interface *, int)) * Or to put it another way, don't trust the Linux BPF filter. */ - return s; + return bpf; eexit: - close(s); - return -1; + if (bpf->bpf_fd != -1) + close(bpf->bpf_fd); + free(bpf->bpf_buffer); + free(bpf); + return NULL; } /* BPF requires that we read the entire buffer. * So we pass the buffer in the API so we can loop on >1 packet. */ ssize_t -bpf_read(struct interface *ifp, int s, void *data, size_t len, - unsigned int *flags) +bpf_read(struct bpf *bpf, void *data, size_t len) { ssize_t bytes; - struct ipv4_state *state = IPV4_STATE(ifp); - struct iovec iov = { - .iov_base = state->buffer, - .iov_len = state->buffer_size + .iov_base = bpf->bpf_buffer, + .iov_len = bpf->bpf_size, }; struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 }; #ifdef PACKET_AUXDATA @@ -1686,19 +1683,19 @@ bpf_read(struct interface *ifp, int s, void *data, size_t len, msg.msg_controllen = sizeof(cmsgbuf.buf); #endif - bytes = recvmsg(s, &msg, 0); + bytes = recvmsg(bpf->bpf_fd, &msg, 0); if (bytes == -1) return -1; - *flags |= BPF_EOF; /* We only ever read one packet. */ - *flags &= ~BPF_PARTIALCSUM; + bpf->bpf_flags |= BPF_EOF; /* We only ever read one packet. */ + bpf->bpf_flags &= ~BPF_PARTIALCSUM; if (bytes) { - if (bpf_frame_bcast(ifp, state->buffer) == 0) - *flags |= BPF_BCAST; + if (bpf_frame_bcast(bpf->bpf_ifp, bpf->bpf_buffer) == 0) + bpf->bpf_flags |= BPF_BCAST; else - *flags &= ~BPF_BCAST; + bpf->bpf_flags &= ~BPF_BCAST; if ((size_t)bytes > len) bytes = (ssize_t)len; - memcpy(data, state->buffer, (size_t)bytes); + memcpy(data, bpf->bpf_buffer, (size_t)bytes); #ifdef PACKET_AUXDATA for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; @@ -1708,7 +1705,7 @@ bpf_read(struct interface *ifp, int s, void *data, size_t len, cmsg->cmsg_type == PACKET_AUXDATA) { aux = (void *)CMSG_DATA(cmsg); if (aux->tp_status & TP_STATUS_CSUMNOTREADY) - *flags |= BPF_PARTIALCSUM; + bpf->bpf_flags |= BPF_PARTIALCSUM; } } #endif diff --git a/src/privsep-bpf.c b/src/privsep-bpf.c index 4fb4b142..5b52ddb9 100644 --- a/src/privsep-bpf.c +++ b/src/privsep-bpf.c @@ -269,8 +269,9 @@ ps_bpf_dispatch(struct dhcpcd_ctx *ctx, { struct iovec *iov = msg->msg_iov; struct interface *ifp; - uint8_t bpf_flags, *bpf; + uint8_t *bpf; size_t bpf_len; + unsigned int bpf_flags; ifp = if_findindex(ctx->ifaces, psm->ps_id.psi_ifindex); bpf = iov->iov_base; diff --git a/src/privsep.c b/src/privsep.c index 6d71f81d..9ff89ed3 100644 --- a/src/privsep.c +++ b/src/privsep.c @@ -470,7 +470,7 @@ ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, if (msg != NULL) { struct iovec *iovp = &iov[1]; - size_t i; + int i; psm->ps_namelen = msg->msg_namelen; psm->ps_controllen = (socklen_t)msg->msg_controllen; @@ -482,8 +482,8 @@ ps_sendpsmmsg(struct dhcpcd_ctx *ctx, int fd, iovp->iov_len = msg->msg_controllen; iovlen = 3; - for (i = 0; i < (size_t)msg->msg_iovlen; i++) { - if ((size_t)iovlen + i > __arraycount(iov)) { + for (i = 0; i < (int)msg->msg_iovlen; i++) { + if ((size_t)(iovlen + i) > __arraycount(iov)) { errno = ENOBUFS; return -1; }