Skip to content

Commit f6dfbe9

Browse files
authored
Fix counting bootp packets by mistake (#31)
Why I did it In previous, if switch receives bootp packets with vendor specific options rather than DHCP body, dhcpmon would treat them as DHCP packets and count them. Since the packets body is not DHCP format, it's not expected and would cause incorrect counting data generated image How I did it Per RFC 2131, add check for DHCP magic cookie to make sure dhcpmon would only count DHCP packets How I verify it Install new dhcpmon and send bootp packets manully and we can see bootp packets wouldn't be counted as DHCP packets Install new dhcpmon and run dhcp_relay related tests in sonic-mgmt, all passed Signed-off-by: Yaqiang Zhu <yaqiangzhu@microsoft.com>
1 parent e003522 commit f6dfbe9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/dhcp_device.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@
4343
#define DHCP_OPTIONS_HEADER_SIZE 240
4444
/** Offset of DHCP GIADDR */
4545
#define DHCP_GIADDR_OFFSET 24
46+
/** Offset of magic cookie */
47+
#define MAGIC_COOKIE_OFFSET 236
4648
#define CLIENT_IF_PREFIX "Ethernet"
49+
/** 32-bit decimal of 99.130.83.99 (indicate DHCP packets), Refer to RFC 2131 */
50+
#define DHCP_MAGIC_COOKIE 1669485411
4751

4852
#define OP_LDHA (BPF_LD | BPF_H | BPF_ABS) /** bpf ldh Abs */
4953
#define OP_LDHI (BPF_LD | BPF_H | BPF_IND) /** bpf ldh Ind */
@@ -288,7 +292,14 @@ static void client_packet_handler(dhcp_device_context_t *context, uint8_t *buffe
288292
ntohs(udp->len) : buffer_sz - UDP_START_OFFSET - sizeof(struct udphdr);
289293
int dhcp_option_sz = dhcp_sz - DHCP_OPTIONS_HEADER_SIZE;
290294
const u_char *dhcp_option = buffer + dhcp_option_offset;
291-
295+
uint32_t magic_cookie = dhcphdr[MAGIC_COOKIE_OFFSET] << 24 | dhcphdr[MAGIC_COOKIE_OFFSET + 1] << 16 |
296+
dhcphdr[MAGIC_COOKIE_OFFSET + 2] << 8 | dhcphdr[MAGIC_COOKIE_OFFSET + 3];
297+
// If magic cookie not equals to DHCP value, its format is not DHCP format, shouldn't count as DHCP packets.
298+
if (magic_cookie != DHCP_MAGIC_COOKIE) {
299+
context->counters[DHCP_COUNTERS_CURRENT][dir][BOOTP_MESSAGE]++;
300+
aggregate_dev.counters[DHCP_COUNTERS_CURRENT][dir][BOOTP_MESSAGE]++;
301+
return;
302+
}
292303
int offset = 0;
293304
while ((offset < (dhcp_option_sz + 1)) && dhcp_option[offset] != 255) {
294305
if (dhcp_option[offset] == OPTION_DHCP_MESSAGE_TYPE) {

src/dhcp_device.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ typedef enum
3232
DHCP_MESSAGE_TYPE_NAK = 6,
3333
DHCP_MESSAGE_TYPE_RELEASE = 7,
3434
DHCP_MESSAGE_TYPE_INFORM = 8,
35+
BOOTP_MESSAGE = 9,
3536

3637
DHCP_MESSAGE_TYPE_COUNT
3738
} dhcp_message_type_t;

0 commit comments

Comments
 (0)