用户态协议栈03-icmp实现

2024-06-21 11:04
文章标签 实现 协议 icmp 用户 03

本文主要是介绍用户态协议栈03-icmp实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

icmp协议

ICMP(Internet Control Message Protocol)Internet控制报文协议。它是TCP/IP协议簇的一个子协议,用于在IP主机、路由器之间传递控制消息。控制消息是指网络通不通、主机是否可达、路由是否可用等网络本身的消息。这些控制消息虽然并不传输用户数据,但是对于用户数据的传递起着重要的作用。 [1]

ICMP使用IP的基本支持,就像它是一个更高级别的协议,但是,ICMP实际上是IP的一个组成部分,必须由每个IP模块实现。

icmp报文结构

打包icmp

打包其实都是一个原理,直接放代码了:

static void dpdk_encode_icmp_pkt(uint8_t* msg, uint8_t* dst_mac, uint32_t sip, uint32_t dip, uint16_t id, uint16_t seq) {struct rte_ether_hdr* eth = (struct rte_ether_hdr*)msg;rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);rte_memcpy(eth->d_addr.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);struct rte_ipv4_hdr* ip = (struct rte_ipv4_hdr*)(eth + 1);ip->version_ihl = 0x45;ip->type_of_service = 0;ip->total_length = htons(sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr));ip->packet_id = 0;ip->fragment_offset = 0;ip->time_to_live = 64;ip->next_proto_id = IPPROTO_ICMP;ip->dst_addr = dip;ip->src_addr = sip;ip->hdr_checksum = 0;ip->hdr_checksum = rte_ipv4_cksum(ip);struct rte_icmp_hdr* icmp = (struct rte_icmp_hdr*)(ip + 1);icmp->icmp_type = RTE_IP_ICMP_ECHO_REPLY;icmp->icmp_code = 0;icmp->icmp_ident = id;icmp->icmp_seq_nb = seq;icmp->icmp_cksum = 0;icmp->icmp_cksum = rte_icmp_cksum((uint16_t*)icmp, sizeof(struct rte_icmp_hdr));
}static struct rte_mbuf* dpdk_send_icmp(struct rte_mempool* mbuf_pool, uint8_t* dst_mac, uint32_t sip,  uint32_t dip, uint16_t id, uint16_t seq) {struct rte_mbuf* mbuf = rte_pktmbuf_alloc(mbuf_pool);if(!mbuf) {rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");}uint16_t total_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr);mbuf->pkt_len = total_len;mbuf->data_len = total_len;uint8_t* pkt = rte_pktmbuf_mtod(mbuf, uint8_t*);dpdk_encode_icmp_pkt(pkt, dst_mac, sip, dip, id, seq);return mbuf;
}

这里我们依然是做的基础的回复包,效果是使用物理机进行ping操作的时候,可以收到数据返回,所以我们只是打了一个RTE_IP_ICMP_ECHO_REPLY的包。

icmp校验

由于DPDK中好像没有专门的ICMP校验接口,这里我们参考文档自己写一个,文档我会放在最后:

static uint16_t rte_icmp_cksum(uint16_t* addr, int count) {long sum = 0;while (count > 1){sum += *(unsigned short*)addr++;count -= 2;}if(count > 0)sum += *(unsigned char*)addr;while(sum >> 16)sum = (sum & 0xffff) + (sum >> 16);return ~sum;
}

完整代码

#include <rte_eal.h>
#include <rte_ethdev.h>
#include <stdio.h>
#include <arpa/inet.h>#define MBUF_LEN    (4096-1)
#define MBUF_SIZE   64
static const int gDpdkPortId = 0;
#define MAKE_IPV4_ADDR(a, b, c, d)  (a + (b<<8) + (c<<16) + (d<<24))
uint32_t gLocalIp = MAKE_IPV4_ADDR(192, 168, 1, 185);uint32_t gSrcIp;
uint32_t gDstIp;
uint8_t gSrcMac[RTE_ETHER_ADDR_LEN];
uint8_t gDstMac[RTE_ETHER_ADDR_LEN];
uint16_t gSrcPort;
uint16_t gDstPort;struct rte_eth_conf default_port_info = {.rxmode = {.max_rx_pkt_len = RTE_ETHER_MAX_LEN},
};static void dpdk_port_init(struct rte_mempool* mbuf_pool) {uint16_t sys_port_count = rte_eth_dev_count_avail();if(sys_port_count == 0)rte_exit(EXIT_FAILURE, "Could not support port\n");struct rte_eth_dev_info dev_info;rte_eth_dev_info_get(gDpdkPortId, &dev_info);const unsigned nb_rx_queue = 1;const unsigned nb_tx_queue = 1;struct rte_eth_conf port_conf = default_port_info;rte_eth_dev_configure(gDpdkPortId, nb_rx_queue, nb_tx_queue, &port_conf);if(rte_eth_rx_queue_setup(gDpdkPortId, 0, 128, rte_eth_dev_socket_id(gDpdkPortId), NULL, mbuf_pool) < 0)rte_exit(EXIT_FAILURE, "Could not setup RX queue\n");struct rte_eth_txconf txconf = dev_info.default_txconf;txconf.offloads = default_port_info.rxmode.offloads;if(rte_eth_tx_queue_setup(gDpdkPortId, 0, 512, rte_eth_dev_socket_id(gDpdkPortId), &txconf) < 0)rte_exit(EXIT_FAILURE, "Could not setup TX queue\n");if(rte_eth_dev_start(gDpdkPortId) < 0)rte_exit(EXIT_FAILURE, "Could not start\n");
}static void dpdk_encode_udp_pkt(uint8_t* msg, uint8_t* data, uint16_t total_len) {struct rte_ether_hdr* eth = (struct rte_ether_hdr*)msg;rte_memcpy(eth->d_addr.addr_bytes, gDstMac, RTE_ETHER_ADDR_LEN);rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);struct rte_ipv4_hdr* ip = (struct rte_ipv4_hdr*)(eth + 1);ip->version_ihl = 0x45;ip->type_of_service = 0;ip->total_length = htons(total_len - sizeof(struct rte_ether_hdr));ip->packet_id = 0;ip->fragment_offset = 0;ip->time_to_live = 64;ip->next_proto_id = IPPROTO_UDP;ip->dst_addr = gDstIp;ip->src_addr = gSrcIp;ip->hdr_checksum = 0;ip->hdr_checksum = rte_ipv4_cksum(ip);struct rte_udp_hdr* udp = (struct rte_udp_hdr*)(ip + 1);uint16_t length = total_len - sizeof(struct rte_ether_hdr) - sizeof(struct rte_ipv4_hdr);udp->dst_port = gDstPort;udp->src_port = gSrcIp;udp->dgram_len = htons(length);rte_memcpy((uint8_t*)(udp + 1), data, length);udp->dgram_cksum = 0;udp->dgram_cksum = rte_ipv4_udptcp_cksum(ip, udp);
}static  struct rte_mbuf* dpdk_send_udp(struct rte_mempool* mbuf_pool, uint8_t* data, uint16_t length) {struct rte_mbuf* mbuf = rte_pktmbuf_alloc(mbuf_pool);if(!mbuf)rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");uint16_t total_len = length + 42;mbuf->pkt_len = total_len;mbuf->data_len = total_len;uint8_t* pkt = rte_pktmbuf_mtod(mbuf, uint8_t*);dpdk_encode_udp_pkt(pkt, data, length);return mbuf;
}static void dpdk_encode_arp_pkt(uint8_t* msg, uint8_t* dst_mac, uint32_t sip, uint32_t dip) {struct rte_ether_hdr* eth = (struct rte_ether_hdr*)(msg);rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);rte_memcpy(eth->d_addr.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);eth->ether_type = htons(RTE_ETHER_TYPE_ARP);struct rte_arp_hdr* arp = (struct rte_arp_hdr*)(eth + 1);arp->arp_hardware = htons(1);arp->arp_protocol = htons(RTE_ETHER_TYPE_IPV4);arp->arp_plen = sizeof(uint32_t);arp->arp_hlen = RTE_ETHER_ADDR_LEN;arp->arp_opcode = htons(2);arp->arp_data.arp_sip = sip;arp->arp_data.arp_tip = dip;rte_memcpy(arp->arp_data.arp_sha.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);rte_memcpy(arp->arp_data.arp_tha.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);
}static  struct rte_mbuf* dpdk_send_arp(struct rte_mempool* mbuf_pool, uint8_t* dst_mac, uint32_t sip, uint32_t dip) {struct rte_mbuf* mbuf = rte_pktmbuf_alloc(mbuf_pool);if(!mbuf)rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");uint16_t total_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_arp_hdr);mbuf->pkt_len = total_len;mbuf->data_len = total_len;uint8_t* pkt = rte_pktmbuf_mtod(mbuf, uint8_t*);dpdk_encode_arp_pkt(pkt, dst_mac, sip, dip);return mbuf;
}static uint16_t rte_icmp_cksum(uint16_t* addr, int count) {long sum = 0;while (count > 1){sum += *(unsigned short*)addr++;count -= 2;}if(count > 0)sum += *(unsigned char*)addr;while(sum >> 16)sum = (sum & 0xffff) + (sum >> 16);return ~sum;
}static void dpdk_encode_icmp_pkt(uint8_t* msg, uint8_t* dst_mac, uint32_t sip, uint32_t dip, uint16_t id, uint16_t seq) {struct rte_ether_hdr* eth = (struct rte_ether_hdr*)msg;rte_memcpy(eth->s_addr.addr_bytes, gSrcMac, RTE_ETHER_ADDR_LEN);rte_memcpy(eth->d_addr.addr_bytes, dst_mac, RTE_ETHER_ADDR_LEN);eth->ether_type = htons(RTE_ETHER_TYPE_IPV4);struct rte_ipv4_hdr* ip = (struct rte_ipv4_hdr*)(eth + 1);ip->version_ihl = 0x45;ip->type_of_service = 0;ip->total_length = htons(sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr));ip->packet_id = 0;ip->fragment_offset = 0;ip->time_to_live = 64;ip->next_proto_id = IPPROTO_ICMP;ip->dst_addr = dip;ip->src_addr = sip;ip->hdr_checksum = 0;ip->hdr_checksum = rte_ipv4_cksum(ip);struct rte_icmp_hdr* icmp = (struct rte_icmp_hdr*)(ip + 1);icmp->icmp_type = RTE_IP_ICMP_ECHO_REPLY;icmp->icmp_code = 0;icmp->icmp_ident = id;icmp->icmp_seq_nb = seq;icmp->icmp_cksum = 0;icmp->icmp_cksum = rte_icmp_cksum((uint16_t*)icmp, sizeof(struct rte_icmp_hdr));
}static struct rte_mbuf* dpdk_send_icmp(struct rte_mempool* mbuf_pool, uint8_t* dst_mac, uint32_t sip,  uint32_t dip, uint16_t id, uint16_t seq) {struct rte_mbuf* mbuf = rte_pktmbuf_alloc(mbuf_pool);if(!mbuf) {rte_exit(EXIT_FAILURE, "rte_pktmbuf_alloc\n");}uint16_t total_len = sizeof(struct rte_ether_hdr) + sizeof(struct rte_ipv4_hdr) + sizeof(struct rte_icmp_hdr);mbuf->pkt_len = total_len;mbuf->data_len = total_len;uint8_t* pkt = rte_pktmbuf_mtod(mbuf, uint8_t*);dpdk_encode_icmp_pkt(pkt, dst_mac, sip, dip, id, seq);return mbuf;
}int main(int argc, char* argv[]) {if(rte_eal_init(argc, argv) < 0)rte_exit(EXIT_FAILURE, "Error with eal init\n");struct rte_mempool* mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_LEN, 0, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());if(!mbuf_pool)rte_exit(EXIT_FAILURE, "Error with mempool create\n");dpdk_port_init(mbuf_pool);rte_eth_macaddr_get(gDpdkPortId, (struct rte_ether_addr*)gSrcMac);while(1) {struct rte_mbuf* mbufs[MBUF_SIZE];int nb_pkt = rte_eth_rx_burst(gDpdkPortId, 0, mbufs, MBUF_SIZE);if(nb_pkt > MBUF_SIZE)rte_exit(EXIT_FAILURE, "Error withs pkt num\n");int i;for(i = 0; i < nb_pkt; i++) {struct rte_ether_hdr* eth = rte_pktmbuf_mtod(mbufs[i], struct rte_ether_hdr*);if(eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)) {struct rte_arp_hdr* arp = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_arp_hdr*, sizeof(struct rte_ether_hdr));struct in_addr addr;addr.s_addr = arp->arp_data.arp_sip;printf("arp-->src: %s ", inet_ntoa(addr));addr.s_addr = arp->arp_data.arp_tip;printf("dst: %s\n", inet_ntoa(addr));if(arp->arp_data.arp_tip == gLocalIp) {struct rte_mbuf* txbuf = dpdk_send_arp(mbuf_pool, arp->arp_data.arp_sha.addr_bytes, gLocalIp, arp->arp_data.arp_sip);rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);rte_pktmbuf_free(txbuf);rte_pktmbuf_free(mbufs[i]);txbuf = NULL;mbufs[i] = NULL;}continue;}if(eth->ether_type != rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {rte_pktmbuf_free(mbufs[i]);mbufs[i] = NULL;continue;}struct rte_ipv4_hdr* ip = rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr*, sizeof(struct rte_ether_hdr));if(ip->next_proto_id == IPPROTO_UDP) {struct rte_udp_hdr* udp = (struct rte_udp_hdr*)(ip + 1);rte_memcpy(gDstMac, eth->s_addr.addr_bytes, RTE_ETHER_ADDR_LEN);rte_memcpy(&gSrcIp, &ip->dst_addr, sizeof(uint32_t));rte_memcpy(&gDstIp, &ip->src_addr, sizeof(uint32_t));rte_memcpy(&gSrcPort, &udp->dst_port, sizeof(uint16_t));rte_memcpy(&gDstPort, &udp->src_port, sizeof(uint16_t));uint16_t len = ntohs(udp->dgram_len);*((char*)udp + len) = '\0';struct in_addr addr;addr.s_addr = ip->src_addr;printf("udp-->src: %s:%d ", inet_ntoa(addr), ntohs(udp->src_port));addr.s_addr = ip->dst_addr;printf("dst: %s:%d %s\n", inet_ntoa(addr), udp->dst_port, (char*)(udp + 1));struct rte_mbuf* txbuf = dpdk_send_udp(mbuf_pool, (uint8_t*)(udp + 1), len);rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);rte_pktmbuf_free(txbuf);rte_pktmbuf_free(mbufs[i]);txbuf = NULL;mbufs[i] = NULL;}if(ip->next_proto_id == IPPROTO_ICMP) {struct rte_icmp_hdr* icmp = (struct rte_icmp_hdr*)(ip + 1);struct in_addr addr;addr.s_addr = ip->src_addr;printf("icmp-->src: %s ", inet_ntoa(addr));if(icmp->icmp_type == RTE_IP_ICMP_ECHO_REQUEST) {addr.s_addr = ip->dst_addr;printf("local:%s, type: %d\n", inet_ntoa(addr), icmp->icmp_type);struct rte_mbuf* txbuf = dpdk_send_icmp(mbuf_pool, eth->s_addr.addr_bytes, ip->dst_addr, ip->src_addr, icmp->icmp_ident, icmp->icmp_seq_nb);rte_eth_tx_burst(gDpdkPortId, 0, &txbuf, 1);rte_pktmbuf_free(txbuf);rte_pktmbuf_free(mbufs[i]);}}}}return 0;
}

文档

RFC参考文档

在参考文档中找到cksum相关的就可以了。

这篇关于用户态协议栈03-icmp实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1081047

相关文章

python使用watchdog实现文件资源监控

《python使用watchdog实现文件资源监控》watchdog支持跨平台文件资源监控,可以检测指定文件夹下文件及文件夹变动,下面我们来看看Python如何使用watchdog实现文件资源监控吧... python文件监控库watchdogs简介随着Python在各种应用领域中的广泛使用,其生态环境也

el-select下拉选择缓存的实现

《el-select下拉选择缓存的实现》本文主要介绍了在使用el-select实现下拉选择缓存时遇到的问题及解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录项目场景:问题描述解决方案:项目场景:从左侧列表中选取字段填入右侧下拉多选框,用户可以对右侧

Python pyinstaller实现图形化打包工具

《Pythonpyinstaller实现图形化打包工具》:本文主要介绍一个使用PythonPYQT5制作的关于pyinstaller打包工具,代替传统的cmd黑窗口模式打包页面,实现更快捷方便的... 目录1.简介2.运行效果3.相关源码1.简介一个使用python PYQT5制作的关于pyinstall

使用Python实现大文件切片上传及断点续传的方法

《使用Python实现大文件切片上传及断点续传的方法》本文介绍了使用Python实现大文件切片上传及断点续传的方法,包括功能模块划分(获取上传文件接口状态、临时文件夹状态信息、切片上传、切片合并)、整... 目录概要整体架构流程技术细节获取上传文件状态接口获取临时文件夹状态信息接口切片上传功能文件合并功能小

python实现自动登录12306自动抢票功能

《python实现自动登录12306自动抢票功能》随着互联网技术的发展,越来越多的人选择通过网络平台购票,特别是在中国,12306作为官方火车票预订平台,承担了巨大的访问量,对于热门线路或者节假日出行... 目录一、遇到的问题?二、改进三、进阶–展望总结一、遇到的问题?1.url-正确的表头:就是首先ur

C#实现文件读写到SQLite数据库

《C#实现文件读写到SQLite数据库》这篇文章主要为大家详细介绍了使用C#将文件读写到SQLite数据库的几种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以参考一下... 目录1. 使用 BLOB 存储文件2. 存储文件路径3. 分块存储文件《文件读写到SQLite数据库China编程的方法》博客中,介绍了文

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

基于Python实现PDF动画翻页效果的阅读器

《基于Python实现PDF动画翻页效果的阅读器》在这篇博客中,我们将深入分析一个基于wxPython实现的PDF阅读器程序,该程序支持加载PDF文件并显示页面内容,同时支持页面切换动画效果,文中有详... 目录全部代码代码结构初始化 UI 界面加载 PDF 文件显示 PDF 页面页面切换动画运行效果总结主

SpringBoot实现基于URL和IP的访问频率限制

《SpringBoot实现基于URL和IP的访问频率限制》在现代Web应用中,接口被恶意刷新或暴力请求是一种常见的攻击手段,为了保护系统资源,需要对接口的访问频率进行限制,下面我们就来看看如何使用... 目录1. 引言2. 项目依赖3. 配置 Redis4. 创建拦截器5. 注册拦截器6. 创建控制器8.