OCT 1. 增加编译开关,支持不带VLAN时 DHCP 服务

This commit is contained in:
黄昕 2023-05-05 09:43:58 +08:00
parent be64836de8
commit 9c13c71e96
8 changed files with 141 additions and 103 deletions

View File

@ -100,7 +100,7 @@ application:
# MAC地址黑名单
# mac_filter = ["00:01:02:03:04:07", "00:01:02:03:04:01"];
# 数据包过滤器
net_filter = "vlan and udp and dst port 67";
net_filter = "udp and dst port 67";
# IP地址池配置
range_set: (
{ dhcp_range = "192.168.101.2-192.168.101.4";

View File

@ -187,48 +187,50 @@ static void create_dhcp_tree_mode(PDHCP_PACKAGE p, GtkWidget *treeView) {
// 添加 Ethernet II 头相关内容
s = sdsempty();
MAC_TO_STR(p->vlan_hdr.eth.h_dest, s);
MAC_TO_STR(p->hdr.eth.h_dest, s);
ADD_SUB_STRING("Destination", s);
sdsfree(s);
s = sdsempty();
MAC_TO_STR(p->vlan_hdr.eth.h_source, s);
MAC_TO_STR(p->hdr.eth.h_source, s);
ADD_SUB_STRING("Source", s);
sdsfree(s);
#if VLAN_SUPPORT
// 添加 VLan 头相关内容
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[1], 1, "", -1);
ADD_SUB_INT("ID", "%u", VLAN_VNI_ID(p->vlan_hdr.vlan.id));
ADD_SUB_INT("Type", "0x%04X", ntohs(p->vlan_hdr.vlan.type));
ADD_SUB_INT("ID", "%u", VLAN_VNI_ID(p->hdr.vlan.id));
ADD_SUB_INT("Type", "0x%04X", ntohs(p->hdr.vlan.type));
#endif
// 添加 IP 头
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[2], 1, "", -1);
ADD_SUB_INT("Version", "%u", p->vlan_hdr.ip.version);
ADD_SUB_INT("Version", "%u", p->hdr.ip.version);
s = sdsempty();
sprintf(s, "%u %s (%u)", p->vlan_hdr.ip.ihl*4, "bytes", p->vlan_hdr.ip.ihl);
sprintf(s, "%u %s (%u)", p->hdr.ip.ihl*4, "bytes", p->hdr.ip.ihl);
ADD_SUB_STRING("Header Length", s);
sdsfree(s);
ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->vlan_hdr.ip.tos);
ADD_SUB_INT("Total Length", "%u", ntohs(p->vlan_hdr.ip.tot_len));
ADD_SUB_INT("Identification", "0x%04X", ntohs(p->vlan_hdr.ip.id));
ADD_SUB_INT("Flags", "0x%04X", ntohs(p->vlan_hdr.ip.frag_off));
ADD_SUB_INT("Time to live", "%u", p->vlan_hdr.ip.ttl);
ADD_SUB_INT("Differentiated Services Field", "0x%02X", p->hdr.ip.tos);
ADD_SUB_INT("Total Length", "%u", ntohs(p->hdr.ip.tot_len));
ADD_SUB_INT("Identification", "0x%04X", ntohs(p->hdr.ip.id));
ADD_SUB_INT("Flags", "0x%04X", ntohs(p->hdr.ip.frag_off));
ADD_SUB_INT("Time to live", "%u", p->hdr.ip.ttl);
ADD_SUB_STRING("Protocol", "UDP");
ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->vlan_hdr.ip.check));
ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.saddr));
ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->vlan_hdr.ip.daddr));
ADD_SUB_INT("Header Checksum", "0x%04X", ntohs(p->hdr.ip.check));
ADD_SUB_STRING("Source", inet_ntoa(*(struct in_addr*)&p->hdr.ip.saddr));
ADD_SUB_STRING("Destination", inet_ntoa(*(struct in_addr*)&p->hdr.ip.daddr));
// 添加 UDP 头
gtk_tree_store_append(store, &iter, NULL);
gtk_tree_store_set(store, &iter, 0, itemTitle[3], 1, "", -1);
ADD_SUB_INT("Source Port", "%u", ntohs(p->vlan_hdr.udp.source));
ADD_SUB_INT("Destination Port", "%u", ntohs(p->vlan_hdr.udp.dest));
ADD_SUB_INT("Length", "%u", ntohs(p->vlan_hdr.udp.len));
ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->vlan_hdr.udp.check));
ADD_SUB_INT("Source Port", "%u", ntohs(p->hdr.udp.source));
ADD_SUB_INT("Destination Port", "%u", ntohs(p->hdr.udp.dest));
ADD_SUB_INT("Length", "%u", ntohs(p->hdr.udp.len));
ADD_SUB_INT("Checksum", "0x%04X", ntohs(p->hdr.udp.check));
// 添加 DHCP 内容
gtk_tree_store_append(store, &iter, NULL);

View File

@ -82,4 +82,7 @@ int cacheDhcpOfferBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size);
int cacheDhcpAckBuffer(PDHCP_INFO pInfo, U8 *pBuf, int size);
void details_wnd_create(GtkBuilder *builder);
void details_wnd_show(PDHCP_INFO pInfo);
void hex_wnd_create(GtkBuilder *builder);
void hex_wnd_show(PDHCP_INFO pInfo);
GtkBuilder *get_main_builder();
#endif //VCPE_MAIN_H

View File

@ -19,6 +19,10 @@ static PDHCP_INFO g_pDhcpInfo = NULL;
static GThread *g_pEvLoopThread = NULL;
static int g_runTask = FALSE;
GtkBuilder *get_main_builder() {
return g_mainBuilder;
}
U32 rand_number() {
GRand *pRand = g_rand_new_with_seed(time(NULL));
@ -67,7 +71,9 @@ static gboolean calc_step_progress(PDHCP_INFO pInfo, DHCP_STEP step) {
return FALSE;
}
void view_popup_menu_onStatistics(GtkWidget *UNUSED(menuitem), gpointer UNUSED(userdata)) {
void view_popup_menu_onStatistics(GtkWidget *UNUSED(menuitem), gpointer userdata) {
//PDHCP_INFO pInfo = (PDHCP_INFO)userdata;
//hex_wnd_show(pInfo);
}
void view_popup_menu_onBinaryPkg(GtkWidget *UNUSED(menuitem), gpointer userdata) {
@ -616,6 +622,7 @@ int main(int args, char **argv) {
//GtkWidget *view = GTK_WIDGET(gtk_builder_get_object(g_mainBuilder, "treeResult"));
details_wnd_create(g_mainBuilder);
//hex_wnd_create(g_mainBuilder);
#if 0
gtk_list_store_new(NUM_COLS,
G_TYPE_INT,

View File

@ -16,34 +16,40 @@ static char *g_pNicName = NULL;
static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) {
// 目的地 MAC 地址
memset(p->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN);
memset(p->hdr.eth.h_dest, 0xFF, ETH_ALEN);
// 源 MAC 地址
memcpy(p->vlan_hdr.eth.h_source, pInfo->mac, ETH_ALEN);
memcpy(p->hdr.eth.h_source, pInfo->mac, ETH_ALEN);
#if VLAN_SUPPORT
// 协议 VLAN
p->vlan_hdr.eth.h_proto = htons(ETH_P_8021Q);
p->hdr.eth.h_proto = htons(ETH_P_8021Q);
// VLAN 隧道信息
p->vlan_hdr.vlan.id = htons(pInfo->vni);
p->vlan_hdr.vlan.type = htons(ETH_P_IP);
p->hdr.vlan.id = htons(pInfo->vni);
p->hdr.vlan.type = htons(ETH_P_IP);
#else
// 协议 VLAN
p->hdr.eth.h_proto = htons(ETH_P_IP);
#endif
// IP 头
p->vlan_hdr.ip.version = IPVERSION;
p->vlan_hdr.ip.ihl = 5;
p->vlan_hdr.ip.tos = 0x10;
p->vlan_hdr.ip.tot_len = 0;
p->vlan_hdr.ip.id = 0;
p->vlan_hdr.ip.frag_off = 0;
p->vlan_hdr.ip.ttl = 128;
p->vlan_hdr.ip.protocol = IPPROTO_UDP;
p->vlan_hdr.ip.check = 0;
p->vlan_hdr.ip.saddr = INADDR_ANY;
p->vlan_hdr.ip.daddr = INADDR_BROADCAST;
p->hdr.ip.version = IPVERSION;
p->hdr.ip.ihl = 5;
p->hdr.ip.tos = 0;
p->hdr.ip.tot_len = 0;
p->hdr.ip.id = 0;
p->hdr.ip.frag_off = 0;
p->hdr.ip.ttl = 128;
p->hdr.ip.protocol = IPPROTO_UDP;
p->hdr.ip.check = 0;
p->hdr.ip.saddr = INADDR_ANY;
p->hdr.ip.daddr = INADDR_BROADCAST;
// UDP 头
p->vlan_hdr.udp.source = htons(DHCP_CLI_PORT);
p->vlan_hdr.udp.dest = htons(DHCP_SVR_PORT);
p->vlan_hdr.udp.len = 0;
p->vlan_hdr.udp.check = 0;
p->hdr.udp.source = htons(DHCP_CLI_PORT);
p->hdr.udp.dest = htons(DHCP_SVR_PORT);
p->hdr.udp.len = 0;
p->hdr.udp.check = 0;
// DHCP 协议内容
p->dhcp.op = BOOTP_REQUEST;
@ -63,18 +69,19 @@ static void pkg_init_head(PDHCP_PACKAGE p, PDHCP_INFO pInfo) {
static void cacl_package_checksum(PDHCP_PACKAGE p, int tolSize) {
U16 csum;
// 计算 IP 数据长度
p->vlan_hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr));
p->hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr));
// 计算 UDP 数据长度
p->vlan_hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr));
p->hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr));
// 计算 IP 校验和
csum = htons(ip_checksum((unsigned char *)&p->vlan_hdr.ip));
p->vlan_hdr.ip.check = htons(csum);
csum = htons(ip_checksum((unsigned char *)&p->hdr.ip));
p->hdr.ip.check = htons(csum);
// 计算 UDP 校验和
csum = htons(udp_checksum(p->vlan_hdr.ip.saddr, p->vlan_hdr.ip.daddr, (unsigned char *)&p->vlan_hdr.udp));
p->vlan_hdr.udp.check = htons(csum);
csum = htons(udp_checksum(p->hdr.ip.saddr, p->hdr.ip.daddr, (unsigned char *)&p->hdr.udp));
p->hdr.udp.check = htons(csum);
}
U8 *dhcp_create_request_req(PDHCP_INFO pInfo, int *pOutSize) {
@ -257,7 +264,11 @@ int dhcp_tools_init_network(const char *pNicName) {
return ERR_SUCCESS;
}
#if VLAN_SUPPORT
init_filter("vlan and udp and dst port 68");
#else
init_filter("udp and dst port 68");
#endif
ret = create_udp_raw_socket(g_pNicName);
if (ret != ERR_SUCCESS) {

View File

@ -13,19 +13,25 @@ extern "C" {
#include <linux/if_ether.h>
#include "common.h"
#define VLAN_SUPPORT (0)
#pragma pack(push)
#pragma pack(1)
#define VXLAN_VIN_ID_PACK(x) ((ntohs(x)) & 0xFFF)
typedef struct vlan_hdr {
#if VLAN_SUPPORT
U16 id;
U16 type;
#endif
} VLAN_HDR, *PVLAN_HDR;
typedef struct {
struct ethhdr eth;
#if VLAN_SUPPORT
struct vlan_hdr vlan;
#endif
struct iphdr ip;
struct udphdr udp;
} VLAN_PKG_HDR, *PVLAN_PKG_HDR;

View File

@ -187,40 +187,41 @@ static void fill_package(PDHCP_PACKAGE pRsp, PDHCP_PACKAGE pReq) {
// 二层头
// 目的IP地址
if (pReq->vlan_hdr.ip.saddr != 0) {
memcpy(pRsp->vlan_hdr.eth.h_dest, pRsp->vlan_hdr.eth.h_source, ETH_ALEN);
if (pReq->hdr.ip.saddr != 0) {
memcpy(pRsp->hdr.eth.h_dest, pRsp->hdr.eth.h_source, ETH_ALEN);
} else {
memset(pRsp->vlan_hdr.eth.h_dest, 0xFF, ETH_ALEN);
memset(pRsp->hdr.eth.h_dest, 0xFF, ETH_ALEN);
}
// 源 IP 地址
memcpy(pRsp->vlan_hdr.eth.h_source, g_nicInfo.macAddr, ETH_ALEN);
memcpy(pRsp->hdr.eth.h_source, g_nicInfo.macAddr, ETH_ALEN);
// protol
pRsp->vlan_hdr.eth.h_proto = pReq->vlan_hdr.eth.h_proto;
pRsp->hdr.eth.h_proto = pReq->hdr.eth.h_proto;
#if VLAN_SUPPORT
// QinQ 隧道
pRsp->vlan_hdr.vlan.id = pReq->vlan_hdr.vlan.id;
pRsp->vlan_hdr.vlan.type = pReq->vlan_hdr.vlan.type;
pRsp->hdr.vlan.id = pReq->hdr.vlan.id;
pRsp->hdr.vlan.type = pReq->hdr.vlan.type;
// TODO 可能的二层QinQ隧道
#endif
// IP 头
memcpy(&pRsp->vlan_hdr.ip, &pReq->vlan_hdr.ip, sizeof(struct iphdr));
memcpy(&pRsp->hdr.ip, &pReq->hdr.ip, sizeof(struct iphdr));
// TOS
pRsp->vlan_hdr.ip.tos = 0;
pRsp->hdr.ip.tos = 0;
// 更新源IP
pRsp->vlan_hdr.ip.saddr = g_nicInfo.ipAddr;
pRsp->hdr.ip.saddr = g_nicInfo.ipAddr;
// 更新目的IP地址广播255.255.255.255
if (pReq->vlan_hdr.ip.saddr == 0) {
pRsp->vlan_hdr.ip.daddr = 0xFFFFFFFF;
if (pReq->hdr.ip.saddr == 0) {
pRsp->hdr.ip.daddr = 0xFFFFFFFF;
} else {
pRsp->vlan_hdr.ip.daddr = pReq->vlan_hdr.ip.saddr;
pRsp->hdr.ip.daddr = pReq->hdr.ip.saddr;
}
// UDP 头
// 目的端口
pRsp->vlan_hdr.udp.dest = pReq->vlan_hdr.udp.source;
pRsp->hdr.udp.dest = pReq->hdr.udp.source;
// 源端口
pRsp->vlan_hdr.udp.source = pReq->vlan_hdr.udp.dest;
pRsp->hdr.udp.source = pReq->hdr.udp.dest;
//DHCP 协议
// 返回类型
@ -255,17 +256,17 @@ static int dhcp_prepare_tx(U8 *pOpt, PDHCP_PACKAGE pRsp) {
tolSize = (int)((pOpt - pRsp->dhcp.options) + 1 + sizeof(DHCP_PACKAGE));
// 计算 IP 数据长度
pRsp->vlan_hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr));
pRsp->hdr.ip.tot_len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr));
// 计算 UDP 数据长度
pRsp->vlan_hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr));
pRsp->hdr.udp.len = htons(tolSize - sizeof(struct ethhdr) - sizeof(struct vlan_hdr) - sizeof(struct iphdr));
// 计算 IP 校验和
csum = htons(ip_checksum((unsigned char *)&pRsp->vlan_hdr.ip));
pRsp->vlan_hdr.ip.check = htons(csum);
csum = htons(ip_checksum((unsigned char *)&pRsp->hdr.ip));
pRsp->hdr.ip.check = htons(csum);
// 计算 UDP 校验和
csum = htons(udp_checksum(pRsp->vlan_hdr.ip.saddr, pRsp->vlan_hdr.ip.daddr, (unsigned char *)&pRsp->vlan_hdr.udp));
pRsp->vlan_hdr.udp.check = htons(csum);
csum = htons(udp_checksum(pRsp->hdr.ip.saddr, pRsp->hdr.ip.daddr, (unsigned char *)&pRsp->hdr.udp));
pRsp->hdr.udp.check = htons(csum);
LOG_MOD(trace, ZM_DHCP_NET, "OPTIONS size: %ld\n", (intptr_t)(pOpt - pRsp->dhcp.options) + 1);
LOG_MOD(trace, ZM_DHCP_NET, "Total size: %d\n", tolSize);
@ -406,7 +407,7 @@ static void on_sock_recv(uv_work_t *req) {
PDHCP_PACKAGE pkg = (PDHCP_PACKAGE)pWork->pPkgBase;
U32 optSize = pWork->nSize - sizeof(DHCP_PACKAGE);
sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", macStr[0], macStr[1], macStr[2], macStr[3], macStr[4], macStr[5]);
//sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", macStr[0], macStr[1], macStr[2], macStr[3], macStr[4], macStr[5]);
// Check op flag
if (pkg->dhcp.op != BOOTP_REQUEST) {
@ -424,7 +425,11 @@ static void on_sock_recv(uv_work_t *req) {
memset(&reqDhcp, 0, sizeof(DHCP_REQ));
reqDhcp.unicast = pkg->dhcp.flags != 0 ? TRUE : FALSE;
reqDhcp.xid = DHCP_XID(pkg->dhcp.xid);
reqDhcp.uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id);
#if VLAN_SUPPORT
reqDhcp.uid = VLAN_VNI_ID(pkg->hdr.vlan.id);
#else
reqDhcp.uid = 0;
#endif
reqDhcp.serverAddr = g_nicInfo.ipAddr;
reqDhcp.cliAddr = ntohl(pkg->dhcp.ciaddr);
memcpy(reqDhcp.cliMac, pkg->dhcp.chaddr, ETH_ALEN);
@ -482,8 +487,8 @@ static void on_sock_recv(uv_work_t *req) {
reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
} else {
// Request 服务器IP可以取目的 IP 地址
if (pkg->vlan_hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.daddr);
if (pkg->hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->hdr.ip.daddr);
}
}
@ -537,8 +542,8 @@ static void on_sock_recv(uv_work_t *req) {
reqDhcp.reqServer = ntohl(*((U32 *)opt.pValue));
} else {
// Request 服务器IP可以取目的 IP 地址
if (pkg->vlan_hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->vlan_hdr.ip.saddr);
if (pkg->hdr.ip.saddr != 0) {
reqDhcp.reqServer = ntohl(pkg->hdr.ip.saddr);
}
}
@ -579,20 +584,20 @@ static void on_sock_recv(uv_work_t *req) {
//dhcp_option_prase(optMsg, pkg->dhcp.options, pWork->nSize - sizeof(DHCP_PACKAGE));
//LOG_MSG_HEX(trace, pkg, pWork->nSize);
// LOG_MSG(info, "vni: %d, xid: 0x%08X\n", VLAN_VNI_ID(pkg->vlan_hdr.vlan.id), DHCP_XID(pkg->dhcp.xid));
LOG_MSG_HEX(trace, pkg, pWork->nSize);
//LOG_MSG(info, "vni: %d, xid: 0x%08X\n", VLAN_VNI_ID(pkg->hdr.vlan.id), DHCP_XID(pkg->dhcp.xid));
#if 0
LOG_MOD(info, ZM_DHCP_NET, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->vlan_hdr.vlan.id));
LOG_MOD(info, ZM_DHCP_NET, "vlan = %u\n", VXLAN_VIN_ID_PACK(pkg->hdr.vlan.id));
LOG_MSG(info, "xid: 0x%08X\n", ntohl(pkg->dhcp.xid));
LOG_MSG(info,
"dest mac: %02X:%02X:%02X:%02X:%02X:%02X\n",
pkg->vlan_hdr.eth.h_dest[0],
pkg->vlan_hdr.eth.h_dest[1],
pkg->vlan_hdr.eth.h_dest[2],
pkg->vlan_hdr.eth.h_dest[3],
pkg->vlan_hdr.eth.h_dest[4],
pkg->vlan_hdr.eth.h_dest[5]);
pkg->hdr.eth.h_dest[0],
pkg->hdr.eth.h_dest[1],
pkg->hdr.eth.h_dest[2],
pkg->hdr.eth.h_dest[3],
pkg->hdr.eth.h_dest[4],
pkg->hdr.eth.h_dest[5]);
LOG_MSG(info,
"client mac: %02X:%02X:%02X:%02X:%02X:%02X\n",
pkg->dhcp.chaddr[0],
@ -655,8 +660,12 @@ void raw_sock_recv_cb(uv_poll_t *handle, int status, int events) {
pMsg->pPkgInfo[i].uvWork.data = &pMsg->pPkgInfo[i];
pMsg->pPkgInfo[i].pData = pMsg;
if (g_dhcpMode == MODE_DHCP_SERVER) {
#if VLAN_SUPPORT
pkg = (PDHCP_PACKAGE)pMsg->pPkgInfo[i].pPkgBase;
uid = VLAN_VNI_ID(pkg->vlan_hdr.vlan.id);
uid = VLAN_VNI_ID(pkg->hdr.vlan.id);
#else
uid = 0;
#endif
pMsg->pPkgInfo[i].pUser = dhcp_user_create(uid);
// LOG_MOD(trace, ZM_DHCP_NET, ">>> User %u xid %08X addr %p\n", uid, DHCP_XID(pkg->dhcp.xid),

View File

@ -53,7 +53,7 @@ typedef struct {
#pragma pack(1)
typedef struct {
VLAN_PKG_HDR vlan_hdr;
VLAN_PKG_HDR hdr;
DHCP_PROTO dhcp;
} DHCP_PACKAGE, *PDHCP_PACKAGE;