OCT 1. 备份代码,支持在vlan上接收DHCP数据包

This commit is contained in:
huangxin 2023-03-20 15:59:13 +08:00
parent 325c51d940
commit 1190d115b7
1 changed files with 92 additions and 33 deletions

View File

@ -97,13 +97,74 @@ static struct sock_filter g_filterCode[] = {
{0x6, 0, 0, 0x00040000}, {0x6, 0, 0, 0x00040000},
{0x6, 0, 0, 0x00000000}, {0x6, 0, 0, 0x00000000},
#endif #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}, {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}, {0x28, 0, 0, 0x0000000c},
{0x15, 2, 0, 0x00008100}, {0x15, 2, 0, 0x00008100},
{0x15, 1, 0, 0x000088a8}, {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, 0x00040000},
{0x6, 0, 0, 0x00000000}, {0x6, 0, 0, 0x00000000},
}; };
@ -120,6 +181,7 @@ static void on_sock_recv(uv_work_t *req) {
PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase; PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase;
//LOG_MSG_HEX(debug, pkg, pWork->nSize); //LOG_MSG_HEX(debug, pkg, pWork->nSize);
LOG_MSG(info, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid));
#if 0 #if 0
LOG_MOD(info, ZLOG_MOD_OPENDHCPD, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->vlan_hdr.vlan.id)); 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)); 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; PPKG_MSG pMsg = (PPKG_MSG)pInfo->pData;
pMsg->nf -= 1; pMsg->nf -= 1;
if (pMsg->nf <= 0) { if (pMsg->nf == 0) {
LOG_MOD(debug, ZLOG_MOD_OPENDHCPD, "Free all resources: %p\n", pMsg); LOG_MOD(trace, ZLOG_MOD_OPENDHCPD, "---Free resources: %p\n", pMsg);
free(pMsg->pPkgInfo); free(pMsg->pPkgInfo);
free(pMsg); free(pMsg);
} }
@ -160,43 +222,40 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
if (status >= 0) { if (status >= 0) {
struct block_desc *pbd = (struct block_desc *)g_pkgRing.rd[block_num].iov_base; 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; int i;
unsigned int num_pkts = pbd->h1.num_pkts;
struct tpacket3_hdr *ppd; 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) { if (pMsg == NULL) {
unsigned int memSize = sizeof(PKG_PROCESS_INFO) * num_pkts; LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %lu\n", sizeof(PKG_MSG));
PPKG_MSG pMsg = (PPKG_MSG)malloc(sizeof(PKG_MSG)); return;
}
if (pMsg == NULL) { LOG_MOD(trace, ZLOG_MOD_OPENDHCPD, "++++Malloc resources: %p\n", pMsg);
LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %lu\n", sizeof(PKG_MSG));
return;
}
memset(pMsg, 0, sizeof(PKG_MSG)); memset(pMsg, 0, sizeof(PKG_MSG));
pMsg->pPkgInfo = (PPKG_PROCESS_INFO)malloc(memSize); pMsg->pPkgInfo = (PPKG_PROCESS_INFO)malloc(memSize);
if (pMsg->pPkgInfo == NULL) { if (pMsg->pPkgInfo == NULL) {
LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %u\n", memSize); LOG_MOD(error, ZLOG_MOD_OPENDHCPD, "Malloc memory error: %u\n", memSize);
free(pMsg); free(pMsg);
return; return;
} }
memset(pMsg->pPkgInfo, 0, memSize); memset(pMsg->pPkgInfo, 0, memSize);
pMsg->nf = num_pkts; pMsg->nf = pbd->h1.num_pkts;
ppd = (struct tpacket3_hdr *)((uint8_t *)pbd + pbd->h1.offset_to_first_pkt); ppd = (struct tpacket3_hdr *)((uint8_t *)pbd + pbd->h1.offset_to_first_pkt);
for (i = 0; i < num_pkts; i++) { for (i = 0; i < pbd->h1.num_pkts; i++) {
pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *)ppd + ppd->tp_mac); pMsg->pPkgInfo[i].pPkgBase = ((uint8_t *)ppd + ppd->tp_mac);
pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen; pMsg->pPkgInfo[i].nSize = ppd->tp_snaplen;
pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i]; pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i];
pMsg->pPkgInfo[i].pData = pMsg; pMsg->pPkgInfo[i].pData = pMsg;
uv_queue_work(get_task_manager(), &(pMsg->pPkgInfo[i].uvWork), on_sock_recv, after_msg_recv); 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); ppd = (struct tpacket3_hdr *)((uint8_t *)ppd + ppd->tp_next_offset);
}
} }
} }