Skip to content

Commit 429ee63

Browse files
committed
Fix IP fragmentation checks for big-endian CPUs
1 parent c00962e commit 429ee63

File tree

6 files changed

+57
-51
lines changed

6 files changed

+57
-51
lines changed

mongoose.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,8 +5145,8 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *mac_dst,
51455145
memcpy(eth->src, ifp->mac, sizeof(eth->src)); // Use our MAC
51465146
eth->type = mg_htons(0x800);
51475147
memset(ip, 0, sizeof(*ip));
5148-
ip->ver = 0x45; // Version 4, header length 5 words
5149-
ip->frag = 0x40; // Don't fragment
5148+
ip->ver = 0x45; // Version 4, header length 5 words
5149+
ip->frag = mg_htons(0x40); // Don't fragment
51505150
ip->len = mg_htons((uint16_t) (sizeof(*ip) + plen));
51515151
ip->ttl = 64;
51525152
ip->proto = proto;
@@ -5706,7 +5706,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
57065706
}
57075707

57085708
static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
5709-
if (pkt->ip->frag & IP_MORE_FRAGS_MSK || pkt->ip->frag & IP_FRAG_OFFSET_MSK) {
5709+
uint16_t frag = mg_ntohs(pkt->ip->frag);
5710+
if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) {
57105711
if (pkt->ip->proto == 17) pkt->udp = (struct udp *) (pkt->ip + 1);
57115712
if (pkt->ip->proto == 6) pkt->tcp = (struct tcp *) (pkt->ip + 1);
57125713
struct mg_connection *c = getpeer(ifp->mgr, pkt, false);
@@ -5870,7 +5871,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
58705871

58715872
// Process timeouts
58725873
for (c = ifp->mgr->conns; c != NULL; c = c->next) {
5873-
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving) continue;
5874+
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving)
5875+
continue;
58745876
struct connstate *s = (struct connstate *) (c + 1);
58755877
uint32_t rem_ip;
58765878
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));
@@ -9472,6 +9474,7 @@ int mg_aes_gcm_decrypt(unsigned char *output, const unsigned char *input,
94729474

94739475

94749476

9477+
94759478
#if MG_TLS == MG_TLS_BUILTIN
94769479

94779480
#define CHACHA20 1
@@ -9554,15 +9557,6 @@ struct tls_data {
95549557
struct tls_enc enc;
95559558
};
95569559

9557-
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
9558-
#define MG_LOAD_BE24(p) \
9559-
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
9560-
#define MG_STORE_BE16(p, n) \
9561-
do { \
9562-
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
9563-
MG_U8P(p)[1] = (n) &255; \
9564-
} while (0)
9565-
95669560
#define TLS_RECHDR_SIZE 5 // 1 byte type, 2 bytes version, 2 bytes length
95679561
#define TLS_MSGHDR_SIZE 4 // 1 byte type, 3 bytes length
95689562

mongoose.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ typedef enum { false = 0, true = 1 } bool;
472472
#pragma comment(lib, "advapi32.lib")
473473
#else
474474
#include <bcrypt.h>
475-
#if defined(_MSC_VER)
475+
#if defined(_MSC_VER)
476476
#pragma comment(lib, "bcrypt.lib")
477477
#endif
478478
#endif
@@ -1112,6 +1112,15 @@ bool mg_path_is_sane(const struct mg_str path);
11121112
#define MG_IPADDR_PARTS(ADDR) \
11131113
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
11141114

1115+
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
1116+
#define MG_LOAD_BE24(p) \
1117+
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
1118+
#define MG_STORE_BE16(p, n) \
1119+
do { \
1120+
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
1121+
MG_U8P(p)[1] = (n) &255; \
1122+
} while (0)
1123+
11151124
#define MG_REG(x) ((volatile uint32_t *) (x))[0]
11161125
#define MG_BIT(x) (((uint32_t) 1U) << (x))
11171126
#define MG_SET_BITS(R, CLRMASK, SETMASK) (R) = ((R) & ~(CLRMASK)) | (SETMASK)

src/arch_rtthread.h

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
#pragma once
2-
3-
#if MG_ARCH == MG_ARCH_RTTHREAD
4-
5-
#include <rtthread.h>
6-
#include <ctype.h>
7-
#include <errno.h>
8-
#include <fcntl.h>
9-
#include <sys/socket.h>
10-
#include <sys/select.h>
11-
#include <stdarg.h>
12-
#include <stdbool.h>
13-
#include <stdint.h>
14-
#include <stdio.h>
15-
#include <stdlib.h>
16-
#include <string.h>
17-
#include <sys/types.h>
18-
#include <time.h>
19-
20-
#ifndef MG_IO_SIZE
21-
#define MG_IO_SIZE 1460
22-
#endif
23-
24-
#endif // MG_ARCH == MG_ARCH_RTTHREAD
1+
#pragma once
2+
3+
#if MG_ARCH == MG_ARCH_RTTHREAD
4+
5+
#include <rtthread.h>
6+
#include <ctype.h>
7+
#include <errno.h>
8+
#include <fcntl.h>
9+
#include <sys/socket.h>
10+
#include <sys/select.h>
11+
#include <stdarg.h>
12+
#include <stdbool.h>
13+
#include <stdint.h>
14+
#include <stdio.h>
15+
#include <stdlib.h>
16+
#include <string.h>
17+
#include <sys/types.h>
18+
#include <time.h>
19+
20+
#ifndef MG_IO_SIZE
21+
#define MG_IO_SIZE 1460
22+
#endif
23+
24+
#endif // MG_ARCH == MG_ARCH_RTTHREAD

src/net_builtin.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,8 @@ static struct ip *tx_ip(struct mg_tcpip_if *ifp, uint8_t *mac_dst,
223223
memcpy(eth->src, ifp->mac, sizeof(eth->src)); // Use our MAC
224224
eth->type = mg_htons(0x800);
225225
memset(ip, 0, sizeof(*ip));
226-
ip->ver = 0x45; // Version 4, header length 5 words
227-
ip->frag = 0x40; // Don't fragment
226+
ip->ver = 0x45; // Version 4, header length 5 words
227+
ip->frag = mg_htons(0x40); // Don't fragment
228228
ip->len = mg_htons((uint16_t) (sizeof(*ip) + plen));
229229
ip->ttl = 64;
230230
ip->proto = proto;
@@ -784,7 +784,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
784784
}
785785

786786
static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
787-
if (pkt->ip->frag & IP_MORE_FRAGS_MSK || pkt->ip->frag & IP_FRAG_OFFSET_MSK) {
787+
uint16_t frag = mg_ntohs(pkt->ip->frag);
788+
if (frag & IP_MORE_FRAGS_MSK || frag & IP_FRAG_OFFSET_MSK) {
788789
if (pkt->ip->proto == 17) pkt->udp = (struct udp *) (pkt->ip + 1);
789790
if (pkt->ip->proto == 6) pkt->tcp = (struct tcp *) (pkt->ip + 1);
790791
struct mg_connection *c = getpeer(ifp->mgr, pkt, false);
@@ -948,7 +949,8 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
948949

949950
// Process timeouts
950951
for (c = ifp->mgr->conns; c != NULL; c = c->next) {
951-
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving) continue;
952+
if ((c->is_udp && !c->is_arplooking) || c->is_listening || c->is_resolving)
953+
continue;
952954
struct connstate *s = (struct connstate *) (c + 1);
953955
uint32_t rem_ip;
954956
memcpy(&rem_ip, c->rem.ip, sizeof(uint32_t));

src/tls_builtin.c

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "tls_chacha20.h"
88
#include "tls_uecc.h"
99
#include "tls_x25519.h"
10+
#include "util.h"
1011

1112
#if MG_TLS == MG_TLS_BUILTIN
1213

@@ -90,15 +91,6 @@ struct tls_data {
9091
struct tls_enc enc;
9192
};
9293

93-
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
94-
#define MG_LOAD_BE24(p) \
95-
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
96-
#define MG_STORE_BE16(p, n) \
97-
do { \
98-
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
99-
MG_U8P(p)[1] = (n) &255; \
100-
} while (0)
101-
10294
#define TLS_RECHDR_SIZE 5 // 1 byte type, 2 bytes version, 2 bytes length
10395
#define TLS_MSGHDR_SIZE 4 // 1 byte type, 3 bytes length
10496

src/util.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ bool mg_path_is_sane(const struct mg_str path);
3434
#define MG_IPADDR_PARTS(ADDR) \
3535
MG_U8P(ADDR)[0], MG_U8P(ADDR)[1], MG_U8P(ADDR)[2], MG_U8P(ADDR)[3]
3636

37+
#define MG_LOAD_BE16(p) ((uint16_t) ((MG_U8P(p)[0] << 8U) | MG_U8P(p)[1]))
38+
#define MG_LOAD_BE24(p) \
39+
((uint32_t) ((MG_U8P(p)[0] << 16U) | (MG_U8P(p)[1] << 8U) | MG_U8P(p)[2]))
40+
#define MG_STORE_BE16(p, n) \
41+
do { \
42+
MG_U8P(p)[0] = ((n) >> 8U) & 255; \
43+
MG_U8P(p)[1] = (n) &255; \
44+
} while (0)
45+
3746
#define MG_REG(x) ((volatile uint32_t *) (x))[0]
3847
#define MG_BIT(x) (((uint32_t) 1U) << (x))
3948
#define MG_SET_BITS(R, CLRMASK, SETMASK) (R) = ((R) & ~(CLRMASK)) | (SETMASK)

0 commit comments

Comments
 (0)