From 5e85626464d1851e70dbb14fd7ac65055d0340f6 Mon Sep 17 00:00:00 2001 From: Yaqiang Zhu Date: Tue, 18 Feb 2025 15:59:10 +0800 Subject: [PATCH 1/2] 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 --- src/dhcp_device.cpp | 13 ++++++++++++- src/dhcp_device.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/dhcp_device.cpp b/src/dhcp_device.cpp index ac46ef6d1..cc956e898 100644 --- a/src/dhcp_device.cpp +++ b/src/dhcp_device.cpp @@ -43,7 +43,11 @@ #define DHCP_OPTIONS_HEADER_SIZE 240 /** Offset of DHCP GIADDR */ #define DHCP_GIADDR_OFFSET 24 +/** Offset of magic cookie */ +#define MAGIC_COOKIE_OFFSET 236 #define CLIENT_IF_PREFIX "Ethernet" +/** 32-bit decimal of 99.130.83.99 (indicate DHCP packets), Refer to RFC 2131 */ +#define DHCP_MAGIC_COOKIE 1669485411 #define OP_LDHA (BPF_LD | BPF_H | BPF_ABS) /** bpf ldh Abs */ #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 ntohs(udp->len) : buffer_sz - UDP_START_OFFSET - sizeof(struct udphdr); int dhcp_option_sz = dhcp_sz - DHCP_OPTIONS_HEADER_SIZE; const u_char *dhcp_option = buffer + dhcp_option_offset; - + uint32_t magic_cookie = dhcphdr[MAGIC_COOKIE_OFFSET] << 24 | dhcphdr[MAGIC_COOKIE_OFFSET + 1] << 16 | + dhcphdr[MAGIC_COOKIE_OFFSET + 2] << 8 | dhcphdr[MAGIC_COOKIE_OFFSET + 3]; + // If magic cookie not equals to DHCP value, its format is not DHCP format, shouldn't count as DHCP packets. + if (magic_cookie != DHCP_MAGIC_COOKIE) { + context->counters[DHCP_COUNTERS_CURRENT][dir][BOOTP_MESSAGE]++; + aggregate_dev.counters[DHCP_COUNTERS_CURRENT][dir][BOOTP_MESSAGE]++; + return; + } int offset = 0; while ((offset < (dhcp_option_sz + 1)) && dhcp_option[offset] != 255) { if (dhcp_option[offset] == OPTION_DHCP_MESSAGE_TYPE) { diff --git a/src/dhcp_device.h b/src/dhcp_device.h index cd8eab1ee..07629f6a4 100644 --- a/src/dhcp_device.h +++ b/src/dhcp_device.h @@ -32,6 +32,7 @@ typedef enum DHCP_MESSAGE_TYPE_NAK = 6, DHCP_MESSAGE_TYPE_RELEASE = 7, DHCP_MESSAGE_TYPE_INFORM = 8, + BOOTP_MESSAGE = 9, DHCP_MESSAGE_TYPE_COUNT } dhcp_message_type_t; From bd768d426232d61634ec7245965a8d3d017c79c5 Mon Sep 17 00:00:00 2001 From: yaqiangz Date: Mon, 24 Mar 2025 03:15:18 +0000 Subject: [PATCH 2/2] Update pipeline --- .azure-pipelines/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.azure-pipelines/build.yml b/.azure-pipelines/build.yml index 7ca40a07d..522a31dfb 100644 --- a/.azure-pipelines/build.yml +++ b/.azure-pipelines/build.yml @@ -52,7 +52,7 @@ jobs: project: build pipeline: Azure.sonic-buildimage.common_libs runVersion: 'latestFromBranch' - runBranch: 'refs/heads/master' + runBranch: 'refs/heads/202311' path: $(Build.ArtifactStagingDirectory)/download ${{ if eq(parameters.arch, 'amd64') }}: artifact: common-lib @@ -77,7 +77,7 @@ jobs: ${{ else }}: artifact: sonic-swss-common.${{ parameters.arch }} runVersion: 'latestFromBranch' - runBranch: 'refs/heads/master' + runBranch: 'refs/heads/202405' displayName: "Download sonic-swss-common" - script: | set -ex