Honor lease owner

This commit is contained in:
Sergio R. Caprile committed 2026-04-01 13:44:12 -03:00
1 parent b1c2ffe1a0
commit 47f3c94465
2 files changed
+26 -10

No files matched your search

+13 -5
View File
@@ -5603,7 +5603,7 @@ static bool dhcp_opt_len_ok(uint8_t len, uint8_t *p, uint8_t *end) {
} }
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) { static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0; uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0, owner = 0;
uint8_t msgtype = 0, state = ifp->state; uint8_t msgtype = 0, state = ifp->state;
// perform size check first, then access fields // perform size check first, then access fields
uint8_t *p = pkt->dhcp->options, uint8_t *p = pkt->dhcp->options,
@@ -5627,6 +5627,8 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
lease = mg_ntohl(lease); lease = mg_ntohl(lease);
} else if (p[0] == 53 && p[1] == 1 && p + 6 < end) { // Msg Type } else if (p[0] == 53 && p[1] == 1 && p + 6 < end) { // Msg Type
msgtype = p[2]; msgtype = p[2];
} else if (p[0] == 54 && p[1] == 4 && p + 6 < end) { // Server id 9.7
memcpy(&owner, p + 2, sizeof(sntp)); // This is the lease owner
} }
p += p[1] + 2; p += p[1] + 2;
} }
@@ -5635,9 +5637,13 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
ifp->state = MG_TCPIP_STATE_UP, ifp->ip = 0; ifp->state = MG_TCPIP_STATE_UP, ifp->ip = 0;
} else if (msgtype == 2 && ifp->state == MG_TCPIP_STATE_UP && ip && gw && } else if (msgtype == 2 && ifp->state == MG_TCPIP_STATE_UP && ip && gw &&
lease) { // DHCPOFFER lease) { // DHCPOFFER
// select IP, (4.4.1) (fallback to IP source addr on foul play) // select a server (2131 4.4.1, 2132 9.7): lease owner takes precedence,
// otherwise use siaddr (fallback to IP source addr on foul play).
// This is broadcast, otherwise siaddr would be the destination.
tx_dhcp_request_sel(ifp, ip, tx_dhcp_request_sel(ifp, ip,
pkt->dhcp->siaddr ? pkt->dhcp->siaddr : pkt->ip->src); owner ? owner
: pkt->dhcp->siaddr ? pkt->dhcp->siaddr
: pkt->ip->src);
ifp->state = MG_TCPIP_STATE_REQ; // REQUESTING state ifp->state = MG_TCPIP_STATE_REQ; // REQUESTING state
} else if (msgtype == 5) { // DHCPACK } else if (msgtype == 5) { // DHCPACK
if (ifp->state == MG_TCPIP_STATE_REQ && ip && gw && lease) { // got an IP if (ifp->state == MG_TCPIP_STATE_REQ && ip && gw && lease) { // got an IP
@@ -6493,7 +6499,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// - check clients (Group 1) and established connections (Group 3) // - check clients (Group 1) and established connections (Group 3)
if (c != NULL && c->is_connecting && pkt->tcp->flags == (TH_SYN | TH_ACK)) { if (c != NULL && c->is_connecting && pkt->tcp->flags == (TH_SYN | TH_ACK)) {
// client got a server connection accept // client got a server connection accept
if (!handle_opt(s, pkt->tcp, pkt->ip6 != NULL)) return; // process options (MSS) if (!handle_opt(s, pkt->tcp, pkt->ip6 != NULL))
return; // process options (MSS)
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1; s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1;
tx_tcp_ctrlresp(ifp, pkt, TH_ACK, pkt->tcp->ack); tx_tcp_ctrlresp(ifp, pkt, TH_ACK, pkt->tcp->ack);
c->is_connecting = 0; // Client connected c->is_connecting = 0; // Client connected
@@ -6528,7 +6535,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
int key; int key;
uint32_t isn; uint32_t isn;
if (pkt->tcp->sport != 0) { if (pkt->tcp->sport != 0) {
if (!handle_opt(&cs, pkt->tcp, pkt->ip6 != NULL)) return; // process options (MSS) if (!handle_opt(&cs, pkt->tcp, pkt->ip6 != NULL))
return; // process options (MSS)
key = backlog_insert(c, pkt->tcp->sport, key = backlog_insert(c, pkt->tcp->sport,
cs.dmss); // backlog options (MSS) cs.dmss); // backlog options (MSS)
if (key < 0) return; // no room in backlog, discard SYN, client retries if (key < 0) return; // no room in backlog, discard SYN, client retries
+13 -5
View File
@@ -643,7 +643,7 @@ static bool dhcp_opt_len_ok(uint8_t len, uint8_t *p, uint8_t *end) {
} }
static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) { static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0; uint32_t ip = 0, gw = 0, mask = 0, lease = 0, dns = 0, sntp = 0, owner = 0;
uint8_t msgtype = 0, state = ifp->state; uint8_t msgtype = 0, state = ifp->state;
// perform size check first, then access fields // perform size check first, then access fields
uint8_t *p = pkt->dhcp->options, uint8_t *p = pkt->dhcp->options,
@@ -667,6 +667,8 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
lease = mg_ntohl(lease); lease = mg_ntohl(lease);
} else if (p[0] == 53 && p[1] == 1 && p + 6 < end) { // Msg Type } else if (p[0] == 53 && p[1] == 1 && p + 6 < end) { // Msg Type
msgtype = p[2]; msgtype = p[2];
} else if (p[0] == 54 && p[1] == 4 && p + 6 < end) { // Server id 9.7
memcpy(&owner, p + 2, sizeof(sntp)); // This is the lease owner
} }
p += p[1] + 2; p += p[1] + 2;
} }
@@ -675,9 +677,13 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
ifp->state = MG_TCPIP_STATE_UP, ifp->ip = 0; ifp->state = MG_TCPIP_STATE_UP, ifp->ip = 0;
} else if (msgtype == 2 && ifp->state == MG_TCPIP_STATE_UP && ip && gw && } else if (msgtype == 2 && ifp->state == MG_TCPIP_STATE_UP && ip && gw &&
lease) { // DHCPOFFER lease) { // DHCPOFFER
// select IP, (4.4.1) (fallback to IP source addr on foul play) // select a server (2131 4.4.1, 2132 9.7): lease owner takes precedence,
// otherwise use siaddr (fallback to IP source addr on foul play).
// This is broadcast, otherwise siaddr would be the destination.
tx_dhcp_request_sel(ifp, ip, tx_dhcp_request_sel(ifp, ip,
pkt->dhcp->siaddr ? pkt->dhcp->siaddr : pkt->ip->src); owner ? owner
: pkt->dhcp->siaddr ? pkt->dhcp->siaddr
: pkt->ip->src);
ifp->state = MG_TCPIP_STATE_REQ; // REQUESTING state ifp->state = MG_TCPIP_STATE_REQ; // REQUESTING state
} else if (msgtype == 5) { // DHCPACK } else if (msgtype == 5) { // DHCPACK
if (ifp->state == MG_TCPIP_STATE_REQ && ip && gw && lease) { // got an IP if (ifp->state == MG_TCPIP_STATE_REQ && ip && gw && lease) { // got an IP
@@ -1533,7 +1539,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
// - check clients (Group 1) and established connections (Group 3) // - check clients (Group 1) and established connections (Group 3)
if (c != NULL && c->is_connecting && pkt->tcp->flags == (TH_SYN | TH_ACK)) { if (c != NULL && c->is_connecting && pkt->tcp->flags == (TH_SYN | TH_ACK)) {
// client got a server connection accept // client got a server connection accept
if (!handle_opt(s, pkt->tcp, pkt->ip6 != NULL)) return; // process options (MSS) if (!handle_opt(s, pkt->tcp, pkt->ip6 != NULL))
return; // process options (MSS)
s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1; s->seq = mg_ntohl(pkt->tcp->ack), s->ack = mg_ntohl(pkt->tcp->seq) + 1;
tx_tcp_ctrlresp(ifp, pkt, TH_ACK, pkt->tcp->ack); tx_tcp_ctrlresp(ifp, pkt, TH_ACK, pkt->tcp->ack);
c->is_connecting = 0; // Client connected c->is_connecting = 0; // Client connected
@@ -1568,7 +1575,8 @@ static void rx_tcp(struct mg_tcpip_if *ifp, struct pkt *pkt) {
int key; int key;
uint32_t isn; uint32_t isn;
if (pkt->tcp->sport != 0) { if (pkt->tcp->sport != 0) {
if (!handle_opt(&cs, pkt->tcp, pkt->ip6 != NULL)) return; // process options (MSS) if (!handle_opt(&cs, pkt->tcp, pkt->ip6 != NULL))
return; // process options (MSS)
key = backlog_insert(c, pkt->tcp->sport, key = backlog_insert(c, pkt->tcp->sport,
cs.dmss); // backlog options (MSS) cs.dmss); // backlog options (MSS)
if (key < 0) return; // no room in backlog, discard SYN, client retries if (key < 0) return; // no room in backlog, discard SYN, client retries