From 855d587243b84357c52b33e52eed407656f7a9c0 Mon Sep 17 00:00:00 2001 From: logicog Date: Fri, 13 Feb 2026 18:36:18 +0100 Subject: [PATCH] Add dhcpd VLAN parameter to restrict dhcpd to given VLAN --- cmd_parser.c | 5 +++++ dhcp.c | 5 +++++ html/system.html | 3 ++- html/system.js | 7 ++++++- rtl837x_common.h | 2 ++ uip/uip.c | 8 ++++++-- 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/cmd_parser.c b/cmd_parser.c index bb37703..953af4b 100644 --- a/cmd_parser.c +++ b/cmd_parser.c @@ -44,6 +44,7 @@ extern __xdata struct dhcp_state dhcp_state; __xdata uint8_t vlan_names[VLAN_NAMES_SIZE]; __xdata uint16_t vlan_ptr; extern __xdata uint16_t management_vlan; +extern __xdata uint16_t dhcpd_vlan; __xdata uint8_t gpio_last_value[8] = { 0 }; // Temporatly for str to hex convertion value. @@ -1001,6 +1002,10 @@ void cmd_parser(void) __banked } } else if (cmd_compare(0, "dhcpd")) { if (cmd_words_b[1] > 0 && cmd_compare(1, "on")) { + if (cmd_words_b[2] > 0) + atoi_short(&dhcpd_vlan, cmd_words_b[2]); + else + dhcpd_vlan = 0; dhcpd_start(); } else { print_string("DHCP Server disabled\n"); diff --git a/dhcp.c b/dhcp.c index 81a3d9d..6239593 100644 --- a/dhcp.c +++ b/dhcp.c @@ -89,6 +89,7 @@ struct dhcp_pkt { __xdata uint32_t long_value; __xdata struct dhcpd_cstate cstates[DHCPD_MAX_CLIENTS]; __xdata uint8_t client_idx; +__xdata uint16_t dhcpd_vlan; void dhcp_print_ip(uint8_t *a) { @@ -558,6 +559,10 @@ void dhcpd_start(void) __banked print_string("dhcpd_start failed to set up socket\n"); return; } + if (!dhcpd_vlan) + print_string("dhcpd: enabling for all VLANs\n"); + else + print_string("dhcpd: enabling for VLAN "); print_short(dhcpd_vlan); write_char('\n'); dhcp_state.state = DHCP_SERVER; dhcp_state.server[1] = uip_hostaddr[0] >> 8; dhcp_state.server[0] = uip_hostaddr[0] & 0xff; diff --git a/html/system.html b/html/system.html index f950f7b..31b37c6 100644 --- a/html/system.html +++ b/html/system.html @@ -63,7 +63,8 @@ When updating the above settings, remember to point your browser to the new IP afterwards:



-
+

+



Save all current settings to Flash:
diff --git a/html/system.js b/html/system.js index ba1c74a..7f6dd1b 100644 --- a/html/system.js +++ b/html/system.js @@ -42,9 +42,14 @@ async function ipSub() { } async function dhcpdSub() { var dhcpd_cmd = "dhcpd off"; - if (document.getElementById('dhcpd').checked) + if (document.getElementById('dhcpd').checked) { dhcpd_cmd = "dhcpd on"; + var v=document.getElementById('dhcpd_vid').value + if (v && v!= 0) + dhcpd_cmd = dhcpd_cmd + " " + v; + } try { + console.log("Sending: ", dhcpd_cmd); const response = await fetch('/cmd', { method: 'POST', body: dhcpd_cmd diff --git a/rtl837x_common.h b/rtl837x_common.h index c69d303..f827149 100644 --- a/rtl837x_common.h +++ b/rtl837x_common.h @@ -97,6 +97,8 @@ struct flash_region_t { extern __xdata uint8_t uip_buf[UIP_CONF_BUFFER_SIZE+2]; extern __xdata struct uip_eth_addr uip_ethaddr; +extern __xdata uint16_t rx_packet_vlan; +extern __xdata uint16_t dhcpd_vlan; // Headers for calls in the common code area (HOME/BANK0) void print_string(__code char *p); diff --git a/uip/uip.c b/uip/uip.c index 6f184db..55f960a 100644 --- a/uip/uip.c +++ b/uip/uip.c @@ -924,7 +924,7 @@ uip_process(u8_t flag) __banked UIP_STAT(++uip_stat.ip.drop); goto drop; } - } + } #else /* UIP_CONF_IPV6 */ /* For IPv6, packet reception is a little trickier as we need to make sure that we listen to certain multicast addresses (all @@ -938,7 +938,11 @@ uip_process(u8_t flag) __banked } #endif /* UIP_CONF_IPV6 */ } - + // If we serve only the designated DHCPD-VLAN, we drop the packet if not in that VLAN + if(BUF->proto == UIP_PROTO_UDP && dhcpd_vlan && UDPBUF->destport == HTONS(DHCP_SERVER_PORT) && dhcpd_vlan != rx_packet_vlan) { + UIP_STAT(++uip_stat.ip.drop); + goto drop; + } #if !UIP_CONF_IPV6 // if(uip_ipchksum() != 0xffff) { /* Compute and check the IP header // checksum. */