From a06b9b2fcdea0ee36545019c6984baad2236e51d Mon Sep 17 00:00:00 2001 From: Frank Lichtenheld Date: Fri, 26 Sep 2025 13:17:26 +0200 Subject: [PATCH] proto: Clean up conversion warnings related to checksum macros These should not change any behavior, they mostly clarify the used types and silence warnings, since these casts are deliberate. Change-Id: Ica721a51b00d5314125bcaf5a586e718c5982aef Signed-off-by: Frank Lichtenheld Acked-by: MaxF Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1164 Message-Id: <20250926111726.153603-1-frank@lichtenheld.com> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg33218.html Signed-off-by: Gert Doering --- src/openvpn/clinat.c | 11 ++++----- src/openvpn/mss.c | 58 +++++++++++++++++--------------------------- src/openvpn/proto.h | 18 +++++++------- 3 files changed, 36 insertions(+), 51 deletions(-) diff --git a/src/openvpn/clinat.c b/src/openvpn/clinat.c index b49d4bb3e..7751a474c 100644 --- a/src/openvpn/clinat.c +++ b/src/openvpn/clinat.c @@ -185,11 +185,7 @@ client_nat_transform(const struct client_nat_option_list *list, struct buffer *i const int direction) { struct ip_tcp_udp_hdr *h = (struct ip_tcp_udp_hdr *)BPTR(ipbuf); - int i; - uint32_t addr, *addr_ptr; - const uint32_t *from, *to; - int accumulate = 0; - unsigned int amask; + int32_t accumulate = 0; unsigned int alog = 0; if (check_debug_level(D_CLIENT_NAT)) @@ -197,8 +193,11 @@ client_nat_transform(const struct client_nat_option_list *list, struct buffer *i print_pkt(&h->ip, "BEFORE", direction, D_CLIENT_NAT); } - for (i = 0; i < list->n; ++i) + for (int i = 0; i < list->n; ++i) { + uint32_t addr, *addr_ptr; + const uint32_t *from, *to; + unsigned int amask; const struct client_nat_entry *e = &list->entries[i]; /* current NAT rule */ if (e->type ^ direction) { diff --git a/src/openvpn/mss.c b/src/openvpn/mss.c index e7111a8bb..10d61d144 100644 --- a/src/openvpn/mss.c +++ b/src/openvpn/mss.c @@ -130,11 +130,6 @@ mss_fixup_ipv6(struct buffer *buf, uint16_t maxmss) } } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wconversion" -#endif - /* * change TCP MSS option in SYN/SYN-ACK packets, if present * this is generic for IPv4 and IPv6, as the TCP header is the same @@ -143,11 +138,8 @@ mss_fixup_ipv6(struct buffer *buf, uint16_t maxmss) void mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) { - int hlen, olen, optlen; + int olen, optlen; uint8_t *opt; - uint16_t mssval; - int accumulate; - struct openvpn_tcphdr *tc; if (BLEN(buf) < (int)sizeof(struct openvpn_tcphdr)) { @@ -155,8 +147,8 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) } verify_align_4(buf); - tc = (struct openvpn_tcphdr *)BPTR(buf); - hlen = OPENVPN_TCPH_GET_DOFF(tc->doff_res); + struct openvpn_tcphdr *tc = (struct openvpn_tcphdr *)BPTR(buf); + int hlen = OPENVPN_TCPH_GET_DOFF(tc->doff_res); /* Invalid header length or header without options. */ if (hlen <= (int)sizeof(struct openvpn_tcphdr) || hlen > BLEN(buf)) @@ -171,43 +163,37 @@ mss_fixup_dowork(struct buffer *buf, uint16_t maxmss) { break; } - else if (*opt == OPENVPN_TCPOPT_NOP) + if (*opt == OPENVPN_TCPOPT_NOP) { optlen = 1; + continue; + } + + optlen = *(opt + 1); + if (optlen <= 0 || optlen > olen) + { + break; } - else + if (*opt == OPENVPN_TCPOPT_MAXSEG) { - optlen = *(opt + 1); - if (optlen <= 0 || optlen > olen) + if (optlen != OPENVPN_TCPOLEN_MAXSEG) { - break; + continue; } - if (*opt == OPENVPN_TCPOPT_MAXSEG) + uint16_t mssval = (uint16_t)(opt[2] << 8) + opt[3]; + if (mssval > maxmss) { - if (optlen != OPENVPN_TCPOLEN_MAXSEG) - { - continue; - } - mssval = opt[2] << 8; - mssval += opt[3]; - if (mssval > maxmss) - { - dmsg(D_MSS, "MSS: %" PRIu16 " -> %" PRIu16, mssval, maxmss); - accumulate = htons(mssval); - opt[2] = (uint8_t)((maxmss >> 8) & 0xff); - opt[3] = (uint8_t)(maxmss & 0xff); - accumulate -= htons(maxmss); - ADJUST_CHECKSUM(accumulate, tc->check); - } + dmsg(D_MSS, "MSS: %" PRIu16 " -> %" PRIu16, mssval, maxmss); + opt[2] = (uint8_t)((maxmss >> 8) & 0xff); + opt[3] = (uint8_t)(maxmss & 0xff); + int32_t accumulate = htons(mssval); + accumulate -= htons(maxmss); + ADJUST_CHECKSUM(accumulate, tc->check); } } } } -#if defined(__GNUC__) || defined(__clang__) -#pragma GCC diagnostic pop -#endif - static inline size_t adjust_payload_max_cbc(const struct key_type *kt, size_t target) { diff --git a/src/openvpn/proto.h b/src/openvpn/proto.h index 62157fa62..94952bd4c 100644 --- a/src/openvpn/proto.h +++ b/src/openvpn/proto.h @@ -214,7 +214,7 @@ struct ip_tcp_udp_hdr */ #define ADJUST_CHECKSUM(acc, cksum) \ { \ - int _acc = acc; \ + int32_t _acc = acc; \ _acc += (cksum); \ if (_acc < 0) \ { \ @@ -231,16 +231,16 @@ struct ip_tcp_udp_hdr } \ } -#define ADD_CHECKSUM_32(acc, u32) \ - { \ - acc += (u32) & 0xffff; \ - acc += (u32) >> 16; \ +#define ADD_CHECKSUM_32(acc, u32) \ + { \ + acc += (int32_t)((u32) & 0xffff); \ + acc += (int32_t)((u32) >> 16); \ } -#define SUB_CHECKSUM_32(acc, u32) \ - { \ - acc -= (u32) & 0xffff; \ - acc -= (u32) >> 16; \ +#define SUB_CHECKSUM_32(acc, u32) \ + { \ + acc -= (int32_t)((u32) & 0xffff); \ + acc -= (int32_t)((u32) >> 16); \ } /* -- 2.47.3