如何正确修改数据包字段,并更新 ip/eth 校验和?
How to properly modify packet fields, and update ip/eth checksums?
我像这样更改我的小 NAT 实现的数据包源端口和 IP 地址(数据包在 struct rte_mbuf *m
中):
eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
ipv4_hdr->dst_addr = f->int_src_ip;
这样修改包合适吗?如果是这样,我该如何更新校验和?
是的,这是通常的方式。您可以使用 checksum API of DPDK.
修改校验和
示例:
ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
udp_hdr = (struct udp_hdr *)((char *)ipv4_hdr + sizeof(struct ipv4_hdr));
udp_hdr->src_port = 42;
ipv4_hdr->hdr_checksum = 0;
udp_hdr->dgram_cksum = 0;
udp_hdr->dgram_cksum = rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
ipv4_hdr>hdr_checksum = rte_ipv4_cksum(ipv4_hdr);
我像这样更改我的小 NAT 实现的数据包源端口和 IP 地址(数据包在 struct rte_mbuf *m
中):
eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
ipv4_hdr->dst_addr = f->int_src_ip;
这样修改包合适吗?如果是这样,我该如何更新校验和?
是的,这是通常的方式。您可以使用 checksum API of DPDK.
修改校验和示例:
ipv4_hdr = (struct ipv4_hdr *)(eth_hdr + 1);
udp_hdr = (struct udp_hdr *)((char *)ipv4_hdr + sizeof(struct ipv4_hdr));
udp_hdr->src_port = 42;
ipv4_hdr->hdr_checksum = 0;
udp_hdr->dgram_cksum = 0;
udp_hdr->dgram_cksum = rte_ipv4_udptcp_cksum(ipv4_hdr, udp_hdr);
ipv4_hdr>hdr_checksum = rte_ipv4_cksum(ipv4_hdr);