allow no options in DHCP msgs

This commit is contained in:
Sergio R. Caprile
2025-12-01 10:46:03 -03:00
parent 9c148fd47e
commit a4b7025396
2 changed files with 8 additions and 8 deletions
+4 -4
View File
@@ -4940,7 +4940,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// perform size check first, then access fields
uint8_t *p = pkt->dhcp->options,
*end = (uint8_t *) &pkt->pay.buf[pkt->pay.len];
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
if (end < p) return; // options are optional, check min header length
if (memcmp(&pkt->dhcp->xid, ifp->mac + 2, sizeof(pkt->dhcp->xid))) return;
while (p + 1 < end && p[0] != 255) { // Parse options RFC-1533 #9
if (p[0] == 1 && p[1] == sizeof(ifp->mask) && p + 6 < end) { // Mask
@@ -5002,7 +5002,7 @@ static void rx_dhcp_server(struct mg_tcpip_if *ifp, struct pkt *pkt) {
*end = (uint8_t *) &pkt->pay.buf[pkt->pay.len];
// struct dhcp *req = pkt->dhcp;
struct dhcp res = {2, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, {0}};
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
if (end < p) return; // options are optional, check min header length
res.yiaddr = ifp->ip;
((uint8_t *) (&res.yiaddr))[3]++; // Offer our IP + 1
while (p + 1 < end && p[0] != 255) { // Parse options
@@ -5833,11 +5833,11 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
mg_ntohs(pkt->udp->dport), (int) pkt->pay.len));
if (ifp->enable_dhcp_client && pkt->udp->dport == mg_htons(68)) {
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
mkpay(pkt, pkt->dhcp + 1);
mkpay(pkt, &pkt->dhcp->options);
rx_dhcp_client(ifp, pkt);
} else if (ifp->enable_dhcp_server && pkt->udp->dport == mg_htons(67)) {
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
mkpay(pkt, pkt->dhcp + 1);
mkpay(pkt, &pkt->dhcp->options);
rx_dhcp_server(ifp, pkt);
} else if (!rx_udp(ifp, pkt)) {
// Should send ICMP Destination Unreachable for unicasts, but keep
+4 -4
View File
@@ -557,7 +557,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// perform size check first, then access fields
uint8_t *p = pkt->dhcp->options,
*end = (uint8_t *) &pkt->pay.buf[pkt->pay.len];
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
if (end < p) return; // options are optional, check min header length
if (memcmp(&pkt->dhcp->xid, ifp->mac + 2, sizeof(pkt->dhcp->xid))) return;
while (p + 1 < end && p[0] != 255) { // Parse options RFC-1533 #9
if (p[0] == 1 && p[1] == sizeof(ifp->mask) && p + 6 < end) { // Mask
@@ -619,7 +619,7 @@ static void rx_dhcp_server(struct mg_tcpip_if *ifp, struct pkt *pkt) {
*end = (uint8_t *) &pkt->pay.buf[pkt->pay.len];
// struct dhcp *req = pkt->dhcp;
struct dhcp res = {2, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, {0}, 0, {0}};
if (end < (uint8_t *) (pkt->dhcp + 1)) return;
if (end < p) return; // options are optional, check min header length
res.yiaddr = ifp->ip;
((uint8_t *) (&res.yiaddr))[3]++; // Offer our IP + 1
while (p + 1 < end && p[0] != 255) { // Parse options
@@ -1450,11 +1450,11 @@ static void rx_ip(struct mg_tcpip_if *ifp, struct pkt *pkt) {
mg_ntohs(pkt->udp->dport), (int) pkt->pay.len));
if (ifp->enable_dhcp_client && pkt->udp->dport == mg_htons(68)) {
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
mkpay(pkt, pkt->dhcp + 1);
mkpay(pkt, &pkt->dhcp->options);
rx_dhcp_client(ifp, pkt);
} else if (ifp->enable_dhcp_server && pkt->udp->dport == mg_htons(67)) {
pkt->dhcp = (struct dhcp *) (pkt->udp + 1);
mkpay(pkt, pkt->dhcp + 1);
mkpay(pkt, &pkt->dhcp->options);
rx_dhcp_server(ifp, pkt);
} else if (!rx_udp(ifp, pkt)) {
// Should send ICMP Destination Unreachable for unicasts, but keep