Lines Matching +full:cm +full:- +full:poll +full:- +full:mode
1 // SPDX-License-Identifier: GPL-2.0
8 * CMD=$((./csum -[46] -[tu] -S $SADDR -D $DADDR -[RT] -r 1 $EXTRA_ARGS))
15 * good packet: $CMD [-t]
16 * bad packet: $CMD [-t] -E
19 * option for TCP packets ('-t'). Optionally insert an iptables filter
39 * good packet: $CMD -U
40 * zero csum: $CMD -U -Z
46 * good packet: $CMD -s $smac -d $dmac -p [-t]
48 * Argument '-z' sends UDP packets with a 0x000 checksum disabled field,
51 * Argument '-e' adds a transport mode encapsulation header between
56 * Argument '-r $SEED' optionally randomizes header, payload and length
80 #include <poll.h>
128 /* Trivial demo encap. Stand-in for transport layer protocols like ESP or PSP */
154 sum += ((unsigned char *)data)[len - 1]; in checksum_nofold()
180 /* With CHECKSUM_PARTIAL kernel expects non-inverted pseudo csum */ in checksum()
193 iph->version = 4; in build_packet_ipv4()
194 iph->ihl = 5; in build_packet_ipv4()
195 iph->ttl = 8; in build_packet_ipv4()
196 iph->protocol = proto; in build_packet_ipv4()
197 iph->saddr = cfg_saddr4.sin_addr.s_addr; in build_packet_ipv4()
198 iph->daddr = cfg_daddr4.sin_addr.s_addr; in build_packet_ipv4()
199 iph->tot_len = htons(sizeof(*iph) + len); in build_packet_ipv4()
200 iph->check = checksum_fold(iph, sizeof(*iph), 0); in build_packet_ipv4()
202 iph_addr_p = &iph->saddr; in build_packet_ipv4()
213 ip6h->version = 6; in build_packet_ipv6()
214 ip6h->payload_len = htons(len); in build_packet_ipv6()
215 ip6h->nexthdr = proto; in build_packet_ipv6()
216 ip6h->hop_limit = 64; in build_packet_ipv6()
217 ip6h->saddr = cfg_saddr6.sin6_addr; in build_packet_ipv6()
218 ip6h->daddr = cfg_daddr6.sin6_addr; in build_packet_ipv6()
220 iph_addr_p = &ip6h->saddr; in build_packet_ipv6()
229 uh->source = htons(cfg_port_src); in build_packet_udp()
230 uh->dest = htons(cfg_port_dst); in build_packet_udp()
231 uh->len = htons(sizeof(*uh) + cfg_payload_len); in build_packet_udp()
232 uh->check = 0; in build_packet_udp()
234 /* choose source port so that uh->check adds up to zero */ in build_packet_udp()
236 uh->source = 0; in build_packet_udp()
237 uh->source = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len); in build_packet_udp()
239 fprintf(stderr, "tx: changing sport: %hu -> %hu\n", in build_packet_udp()
240 cfg_port_src, ntohs(uh->source)); in build_packet_udp()
241 cfg_port_src = ntohs(uh->source); in build_packet_udp()
245 uh->check = 0; in build_packet_udp()
247 uh->check = checksum(uh, IPPROTO_UDP, sizeof(*uh) + cfg_payload_len); in build_packet_udp()
250 uh->check = ~uh->check; in build_packet_udp()
252 fprintf(stderr, "tx: sending checksum: 0x%x\n", uh->check); in build_packet_udp()
260 th->source = htons(cfg_port_src); in build_packet_tcp()
261 th->dest = htons(cfg_port_dst); in build_packet_tcp()
262 th->doff = 5; in build_packet_tcp()
263 th->check = 0; in build_packet_tcp()
265 th->check = checksum(th, IPPROTO_TCP, sizeof(*th) + cfg_payload_len); in build_packet_tcp()
268 th->check = ~th->check; in build_packet_tcp()
270 fprintf(stderr, "tx: sending checksum: 0x%x\n", th->check); in build_packet_tcp()
282 uh->dest = htons(cfg_port_dst); in build_packet_udp_encap()
283 uh->source = htons(cfg_port_src_encap); in build_packet_udp_encap()
284 uh->check = 0; in build_packet_udp_encap()
285 uh->len = htons(sizeof(*uh) + in build_packet_udp_encap()
290 eh->nexthdr = IPPROTO_TCP; in build_packet_udp_encap()
341 *len = off - buf + cfg_payload_len; in build_packet()
350 if (fd == -1) in open_inet()
379 if (fd == -1) in open_packet()
393 if (ret == -1) in send_inet()
461 if (ret == -1) in send_packet()
472 if (fd == -1) in recv_prepare_udp()
551 if (fd == -1) in recv_prepare_packet()
578 if (ret == -1 && errno == EAGAIN) in recv_udp()
580 if (ret == -1) in recv_udp()
620 if (len < sizeof(*tcph) || tcph->dest != htons(cfg_port_dst)) in recv_verify_packet_tcp()
621 return -1; in recv_verify_packet_tcp()
623 return recv_verify_csum(th, len, ntohs(tcph->source), tcph->check); in recv_verify_packet_tcp()
630 if (len < sizeof(*eh) || eh->nexthdr != IPPROTO_TCP) in recv_verify_packet_udp_encap()
631 return -1; in recv_verify_packet_udp_encap()
633 return recv_verify_packet_tcp(eh + 1, len - sizeof(*eh)); in recv_verify_packet_udp_encap()
641 return -1; in recv_verify_packet_udp()
643 if (udph->dest != htons(cfg_port_dst)) in recv_verify_packet_udp()
644 return -1; in recv_verify_packet_udp()
646 if (udph->source == htons(cfg_port_src_encap)) in recv_verify_packet_udp()
648 len - sizeof(*udph)); in recv_verify_packet_udp()
650 return recv_verify_csum(th, len, ntohs(udph->source), udph->check); in recv_verify_packet_udp()
659 if (len < sizeof(*iph) || iph->protocol != proto) in recv_verify_packet_ipv4()
660 return -1; in recv_verify_packet_ipv4()
662 ip_len = ntohs(iph->tot_len); in recv_verify_packet_ipv4()
664 return -1; in recv_verify_packet_ipv4()
667 iph_addr_p = &iph->saddr; in recv_verify_packet_ipv4()
669 return recv_verify_packet_tcp(iph + 1, len - sizeof(*iph)); in recv_verify_packet_ipv4()
671 return recv_verify_packet_udp(iph + 1, len - sizeof(*iph)); in recv_verify_packet_ipv4()
680 if (len < sizeof(*ip6h) || ip6h->nexthdr != proto) in recv_verify_packet_ipv6()
681 return -1; in recv_verify_packet_ipv6()
683 ip_len = ntohs(ip6h->payload_len); in recv_verify_packet_ipv6()
684 if (ip_len > len - sizeof(*ip6h)) in recv_verify_packet_ipv6()
685 return -1; in recv_verify_packet_ipv6()
688 iph_addr_p = &ip6h->saddr; in recv_verify_packet_ipv6()
700 struct cmsghdr *cm; in recv_get_packet_csum_status() local
702 if (msg->msg_flags & MSG_CTRUNC) in recv_get_packet_csum_status()
705 for (cm = CMSG_FIRSTHDR(msg); cm; cm = CMSG_NXTHDR(msg, cm)) { in recv_get_packet_csum_status()
706 if (cm->cmsg_level != SOL_PACKET || in recv_get_packet_csum_status()
707 cm->cmsg_type != PACKET_AUXDATA) in recv_get_packet_csum_status()
709 cm->cmsg_level, cm->cmsg_type); in recv_get_packet_csum_status()
711 if (cm->cmsg_len != CMSG_LEN(sizeof(struct tpacket_auxdata))) in recv_get_packet_csum_status()
713 cm->cmsg_len, CMSG_LEN(sizeof(struct tpacket_auxdata))); in recv_get_packet_csum_status()
715 aux = (void *)CMSG_DATA(cm); in recv_get_packet_csum_status()
721 return aux->tp_status; in recv_get_packet_csum_status()
748 if (len == -1 && errno == EAGAIN) in recv_packet()
750 if (len == -1) in recv_packet()
769 if (ret == -1 /* skip: non-matching */) in recv_packet()
798 while ((c = getopt(argc, argv, "46d:D:eEi:l:L:n:r:PRs:S:tTuUzZ")) != -1) { in parse_args()
874 error(1, 0, "Must pass -D <daddr> and -S <saddr>"); in parse_args()
886 error(1, 0, "Zero checksum conversion requires -U for tx csum offload"); in parse_args()
897 error(1, errno, "Cannot parse ipv6 -D"); in parse_args()
899 error(1, errno, "Cannot parse ipv6 -S"); in parse_args()
905 error(1, errno, "Cannot parse ipv4 -D"); in parse_args()
907 error(1, errno, "Cannot parse ipv4 -S"); in parse_args()
911 /* special case: time-based seed */ in parse_args()
963 if (poll(&pfd, 1, tleft) == -1) in do_rx()
964 error(1, errno, "poll"); in do_rx()
972 tleft = tstop - gettimeofday_ms(); in do_rx()
994 int fdp = -1, fdr = -1; /* -1 to silence -Wmaybe-uninitialized */ in main()