From 1190d115b7e4a4c9e7e455b9071e7217bc3342db Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 20 Mar 2023 15:59:13 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E5=A4=87=E4=BB=BD=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E5=9C=A8vlan=E4=B8=8A=E6=8E=A5?= =?UTF-8?q?=E6=94=B6DHCP=E6=95=B0=E6=8D=AE=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/service/dhcpd/dhcpd_network.c | 125 +++++++++++++++++++++-------- 1 file changed, 92 insertions(+), 33 deletions(-) diff --git a/srcs/service/dhcpd/dhcpd_network.c b/srcs/service/dhcpd/dhcpd_network.c index 85814d0..226f319 100644 --- a/srcs/service/dhcpd/dhcpd_network.c +++ b/srcs/service/dhcpd/dhcpd_network.c @@ -97,13 +97,74 @@ static struct sock_filter g_filterCode[] = { {0x6, 0, 0, 0x00040000}, {0x6, 0, 0, 0x00000000}, #endif - // create by: tcpdump "vlan" -dd + // create by: tcpdump "vlan and udp and port 67 and port 68" -dd + {0x0, 0, 0, 0x00000000}, + {0x2, 0, 0, 0x00000000}, + {0x2, 0, 0, 0x00000001}, {0x30, 0, 0, 0xfffff030}, - {0x15, 4, 0, 0x00000001}, + {0x15, 7, 0, 0x00000001}, + {0x0, 0, 0, 0x00000004}, + {0x2, 0, 0, 0x00000000}, + {0x2, 0, 0, 0x00000001}, {0x28, 0, 0, 0x0000000c}, {0x15, 2, 0, 0x00008100}, {0x15, 1, 0, 0x000088a8}, - {0x15, 0, 1, 0x00009100}, + {0x15, 0, 56, 0x00009100}, + {0x61, 0, 0, 0x00000001}, + {0x48, 0, 0, 0x0000000c}, + {0x15, 0, 13, 0x000086dd}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x00000014}, + {0x15, 0, 50, 0x00000011}, + {0x61, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000036}, + {0x15, 0, 3, 0x00000043}, + {0x61, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000038}, + {0x15, 43, 44, 0x00000044}, + {0x15, 0, 43, 0x00000044}, + {0x61, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000038}, + {0x15, 39, 40, 0x00000043}, + {0x15, 0, 39, 0x00000800}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x00000017}, + {0x15, 0, 36, 0x00000011}, + {0x61, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000014}, + {0x45, 33, 0, 0x00001fff}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x0000000e}, + {0x54, 0, 0, 0x0000000f}, + {0x64, 0, 0, 0x00000002}, + {0xc, 0, 0, 0x00000000}, + {0x7, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x0000000e}, + {0x15, 0, 8, 0x00000043}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x0000000e}, + {0x54, 0, 0, 0x0000000f}, + {0x64, 0, 0, 0x00000002}, + {0xc, 0, 0, 0x00000000}, + {0x7, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000010}, + {0x15, 16, 17, 0x00000044}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x0000000e}, + {0x54, 0, 0, 0x0000000f}, + {0x64, 0, 0, 0x00000002}, + {0xc, 0, 0, 0x00000000}, + {0x7, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x0000000e}, + {0x15, 0, 9, 0x00000044}, + {0x61, 0, 0, 0x00000000}, + {0x50, 0, 0, 0x0000000e}, + {0x54, 0, 0, 0x0000000f}, + {0x64, 0, 0, 0x00000002}, + {0xc, 0, 0, 0x00000000}, + {0x7, 0, 0, 0x00000000}, + {0x48, 0, 0, 0x00000010}, + {0x15, 0, 1, 0x00000043}, {0x6, 0, 0, 0x00040000}, {0x6, 0, 0, 0x00000000}, }; @@ -120,6 +181,7 @@ static void on_sock_recv(uv_work_t *req) { PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase; //LOG_MSG_HEX(debug, pkg, pWork->nSize); + LOG_MSG(info, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid)); #if 0 LOG_MOD(info, ZLOG_MOD_OPENDHCPD, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->vlan_hdr.vlan.id)); LOG_MSG(info, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid)); @@ -147,8 +209,8 @@ static void after_msg_recv(uv_work_t *req, int status) { PPKG_MSG pMsg = (PPKG_MSG)pInfo->pData; pMsg->nf -= 1; - if (pMsg->nf <= 0) { - LOG_MOD(debug, ZLOG_MOD_OPENDHCPD, "Free all resources: %p\n", pMsg); + if (pMsg->nf == 0) { + LOG_MOD(trace, ZLOG_MOD_OPENDHCPD, "---Free resources: %p\n", pMsg); free(pMsg->pPkgInfo); free(pMsg); } @@ -160,43 +222,40 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) { if (status >= 0) { struct block_desc *pbd = (struct block_desc *)g_pkgRing.rd[block_num].iov_base; - if ((pbd->h1.block_status & TP_STATUS_USER) != 0) { + if ((pbd->h1.block_status & TP_STATUS_USER) && (pbd->h1.num_pkts > 0)) { int i; - unsigned int num_pkts = pbd->h1.num_pkts; struct tpacket3_hdr *ppd; - uv_loop_t *pLoop = get_task_manager(); + unsigned int memSize = sizeof(PKG_PROCESS_INFO) * pbd->h1.num_pkts; + PPKG_MSG pMsg = (PPKG_MSG)malloc(sizeof(PKG_MSG)); - if (num_pkts > 0) { - unsigned int memSize = sizeof(PKG_PROCESS_INFO) * num_pkts; - PPKG_MSG pMsg = (PPKG_MSG)malloc(sizeof(PKG_MSG)); + if (pMsg == NULL) { + LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %lu\n", sizeof(PKG_MSG)); + return; + } - if (pMsg == NULL) { - LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %lu\n", sizeof(PKG_MSG)); - return; - } + LOG_MOD(trace, ZLOG_MOD_OPENDHCPD, "++++Malloc resources: %p\n", pMsg); - memset(pMsg, 0, sizeof(PKG_MSG)); - pMsg->pPkgInfo = (PPKG_PROCESS_INFO)malloc(memSize); + memset(pMsg, 0, sizeof(PKG_MSG)); + pMsg->pPkgInfo = (PPKG_PROCESS_INFO)malloc(memSize); - if (pMsg->pPkgInfo == NULL) { - LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %u\n", memSize); - free(pMsg); - return; - } + if (pMsg->pPkgInfo == NULL) { + LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %u\n", memSize); + free(pMsg); + return; + } - memset(pMsg->pPkgInfo, 0, memSize); + memset(pMsg->pPkgInfo, 0, memSize); - pMsg->nf = num_pkts; - ppd = (struct tpacket3_hdr *)((uint8_t *)pbd + pbd->h1.offset_to_first_pkt); - for (i = 0; i < num_pkts; i++) { - pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *)ppd + ppd->tp_mac); - pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen; - pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i]; - pMsg->pPkgInfo[i].pData = pMsg; + pMsg->nf = pbd->h1.num_pkts; + ppd = (struct tpacket3_hdr *)((uint8_t *)pbd + pbd->h1.offset_to_first_pkt); + for (i = 0; i < pbd->h1.num_pkts; i++) { + pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *)ppd + ppd->tp_mac); + pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen; + pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i]; + pMsg->pPkgInfo[i].pData = pMsg; - uv_queue_work(get_task_manager(), &(pMsg->pPkgInfo[i].uvWork), on_sock_recv, after_msg_recv); - ppd = (struct tpacket3_hdr *)((uint8_t *)ppd + ppd->tp_next_offset); - } + uv_queue_work(get_task_manager(), &(pMsg->pPkgInfo[i].uvWork), on_sock_recv, after_msg_recv); + ppd = (struct tpacket3_hdr *)((uint8_t *)ppd + ppd->tp_next_offset); } }