[PATCH] dhcp.c: avoid assigning option routers IPs (gateway) to dhcp clients

Jie Hu (胡杰) hujie5036 at aliyun.com
Fri Dec 4 09:46:03 UTC 2015


In general, gateway should have a static IP address and set up on a fixed
routers. So, we should not assign option routers written in dhcpd.conf to
dhcp clients. However, in my test, isc-dhcp-server do assign them to dhcp
clients. In that case, all dhcp clients have a dynamic gateway (a dynamic
IP address and a dynamic position of gateway). So, we should avoid
assigning option routers IPs to dhcp clients.

Signed-off-by: Jie Hu (胡杰) <hujie5036 at aliyun.com>
---
 server/dhcp.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/server/dhcp.c b/server/dhcp.c
index 5969895..9042c9a 100644
--- a/server/dhcp.c
+++ b/server/dhcp.c
@@ -3531,6 +3531,38 @@ relinquish_ackqueue(void)
 
 #endif /* defined(DELAYED_ACK) */
 
+int abandon_option_routers (struct lease *lease, struct dhcp_packet *raw)
+{
+	int i = 0;
+	int j = 0;
+	int option_pos = 4;
+	int routers_num = 0;
+	unsigned long allocated_addr = 0;
+
+	while (0x03 != raw->options[option_pos] && 0xff != raw->options[option_pos]
+	&& DHCP_MAX_OPTION_LEN > option_pos) {
+		option_pos += (raw->options[option_pos+1]+2);
+	}
+
+	if (0x03 == raw->options[option_pos++]) {
+		routers_num = (raw->options[option_pos++])/4;
+		allocated_addr = raw->yiaddr.s_addr;
+		for (i = 0; i < routers_num; ++i) {
+			for (j = 0; j < 4; ++j) {
+				if (raw->options[option_pos++] != *((unsigned char *)(&allocated_addr)+j)) {
+					break;
+				}
+			}
+			if (4 == j) {
+				abandon_lease (lease, "abandon option routers IP");
+				return 0;
+			}
+		}
+
+	}
+	return -1;
+}
+
 void dhcp_reply (lease)
 	struct lease *lease;
 {
@@ -3640,6 +3672,12 @@ void dhcp_reply (lease)
 	} else
 		s = (char *)0;
 
+	if (0 == abandon_option_routers (lease, &raw)) {
+		free_lease_state (state, MDL);
+		lease -> state = (struct lease_state *)0;
+		return;
+	}
+
 	/* Say what we're doing... */
 	log_info ("%s on %s to %s %s%s%svia %s",
 		  (state -> offer
-- 
2.5.1.windows.1



More information about the dhcp-workers mailing list