forked from mirrors/linux
		
	selftests/net: expand cmsg_ipv6.sh with ipv4
Expand IPV6_TCLASS to also cover IP_TOS. Expand IPV6_HOPLIMIT to also cover IP_TTL. Expand csmg_sender.c to allow setting IPv4 setsockopts. Also rename struct v6 to cmsg to match its expanded scope. Don't bother updating all occurrences of tclass and hoplimit. Rename cmsg_ipv6.sh to cmsg_ip.sh to match the expanded scope. Be careful around the subtle API difference between TCLASS and TOS. IP_TOS includes ECN bits. Add a test to verify that these are masked when making routing decisions. Diff is more concise with --word-diff Signed-off-by: Willem de Bruijn <willemb@google.com> Link: https://patch.msgid.link/20250225022431.2083926-3-willemdebruijn.kernel@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
		
							parent
							
								
									c1d6d629ab
								
							
						
					
					
						commit
						2e5584e0f9
					
				
					 3 changed files with 96 additions and 55 deletions
				
			
		|  | @ -34,7 +34,7 @@ TEST_PROGS += gre_gso.sh | |||
| TEST_PROGS += cmsg_so_mark.sh | ||||
| TEST_PROGS += cmsg_so_priority.sh | ||||
| TEST_PROGS += test_so_rcv.sh | ||||
| TEST_PROGS += cmsg_time.sh cmsg_ipv6.sh | ||||
| TEST_PROGS += cmsg_time.sh cmsg_ip.sh | ||||
| TEST_PROGS += netns-name.sh | ||||
| TEST_PROGS += link_netns.py | ||||
| TEST_PROGS += nl_netdev.py | ||||
|  |  | |||
|  | @ -3,6 +3,8 @@ | |||
| 
 | ||||
| source lib.sh | ||||
| 
 | ||||
| IP4=172.16.0.1/24 | ||||
| TGT4=172.16.0.2 | ||||
| IP6=2001:db8:1::1/64 | ||||
| TGT6=2001:db8:1::2 | ||||
| TMPF=$(mktemp --suffix ".pcap") | ||||
|  | @ -30,6 +32,7 @@ $NSEXE sysctl -w net.ipv4.ping_group_range='0 2147483647' > /dev/null | |||
| # Connectivity | ||||
| ip -netns $NS link add type dummy | ||||
| ip -netns $NS link set dev dummy0 up | ||||
| ip -netns $NS addr add $IP4 dev dummy0 | ||||
| ip -netns $NS addr add $IP6 dev dummy0 | ||||
| 
 | ||||
| # Test | ||||
|  | @ -63,14 +66,19 @@ for ovr in setsock cmsg both diff; do | |||
|     done | ||||
| done | ||||
| 
 | ||||
| # IPV6_TCLASS | ||||
| # IP_TOS + IPV6_TCLASS | ||||
| 
 | ||||
| test_dscp() { | ||||
|     local -r IPVER=$1 | ||||
|     local -r TGT=$2 | ||||
|     local -r MATCH=$3 | ||||
| 
 | ||||
|     local -r TOS=0x10 | ||||
|     local -r TOS2=0x20 | ||||
|     local -r ECN=0x3 | ||||
| 
 | ||||
|     ip -6 -netns $NS rule add tos $TOS lookup 300 | ||||
|     ip -6 -netns $NS route add table 300 prohibit any | ||||
|     ip $IPVER -netns $NS rule add tos $TOS lookup 300 | ||||
|     ip $IPVER -netns $NS route add table 300 prohibit any | ||||
| 
 | ||||
|     for ovr in setsock cmsg both diff; do | ||||
| 	for p in u i r; do | ||||
|  | @ -87,30 +95,42 @@ test_dscp() { | |||
| 	    BG=$! | ||||
| 	    sleep 0.05 | ||||
| 
 | ||||
| 	    $NSEXE ./cmsg_sender -6 -p $p $m $((TOS2)) $TGT6 1234 | ||||
| 	    check_result $? 0 "TCLASS $prot $ovr - pass" | ||||
| 	    $NSEXE ./cmsg_sender $IPVER -p $p $m $((TOS2)) $TGT 1234 | ||||
| 	    check_result $? 0 "$MATCH $prot $ovr - pass" | ||||
| 
 | ||||
| 	    while [ -d /proc/$BG ]; do | ||||
| 	        $NSEXE ./cmsg_sender -6 -p $p $m $((TOS2)) $TGT6 1234 | ||||
| 	        $NSEXE ./cmsg_sender $IPVER -p $p $m $((TOS2)) $TGT 1234 | ||||
| 	    done | ||||
| 
 | ||||
| 	    tcpdump -r $TMPF -v 2>&1 | grep "class $TOS2" >> /dev/null | ||||
| 	    check_result $? 0 "TCLASS $prot $ovr - packet data" | ||||
| 	    tcpdump -r $TMPF -v 2>&1 | grep "$MATCH $TOS2" >> /dev/null | ||||
| 	    check_result $? 0 "$MATCH $prot $ovr - packet data" | ||||
| 	    rm $TMPF | ||||
| 
 | ||||
| 	    [ $ovr == "both" ]    && m="-C $((TOS )) -c" | ||||
| 	    [ $ovr == "diff" ]    && m="-C $((TOS2)) -c" | ||||
| 
 | ||||
| 	    $NSEXE ./cmsg_sender -6 -p $p $m $((TOS)) -s $TGT6 1234 | ||||
| 	    check_result $? 1 "TCLASS $prot $ovr - rejection" | ||||
| 	    # Match prohibit rule: expect failure | ||||
| 	    $NSEXE ./cmsg_sender $IPVER -p $p $m $((TOS)) -s $TGT 1234 | ||||
| 	    check_result $? 1 "$MATCH $prot $ovr - rejection" | ||||
| 
 | ||||
| 	    # Match prohibit rule: IPv4 masks ECN: expect failure | ||||
| 	    if [[ "$IPVER" == "-4" ]]; then | ||||
| 		$NSEXE ./cmsg_sender $IPVER -p $p $m "$((TOS | ECN))" -s $TGT 1234 | ||||
| 		check_result $? 1 "$MATCH $prot $ovr - rejection (ECN)" | ||||
| 	    fi | ||||
| 	done | ||||
|     done | ||||
| } | ||||
| 
 | ||||
| test_dscp | ||||
| test_dscp -4 $TGT4 tos | ||||
| test_dscp -6 $TGT6 class | ||||
| 
 | ||||
| # IP_TTL + IPV6_HOPLIMIT | ||||
| test_ttl_hoplimit() { | ||||
|     local -r IPVER=$1 | ||||
|     local -r TGT=$2 | ||||
|     local -r MATCH=$3 | ||||
| 
 | ||||
| # IPV6_HOPLIMIT | ||||
| test_hoplimit() { | ||||
|     local -r LIM=4 | ||||
| 
 | ||||
|     for ovr in setsock cmsg both diff; do | ||||
|  | @ -128,21 +148,22 @@ test_hoplimit() { | |||
| 	    BG=$! | ||||
| 	    sleep 0.05 | ||||
| 
 | ||||
| 	    $NSEXE ./cmsg_sender -6 -p $p $m $LIM $TGT6 1234 | ||||
| 	    check_result $? 0 "HOPLIMIT $prot $ovr - pass" | ||||
| 	    $NSEXE ./cmsg_sender $IPVER -p $p $m $LIM $TGT 1234 | ||||
| 	    check_result $? 0 "$MATCH $prot $ovr - pass" | ||||
| 
 | ||||
| 	    while [ -d /proc/$BG ]; do | ||||
| 	        $NSEXE ./cmsg_sender -6 -p $p $m $LIM $TGT6 1234 | ||||
| 		$NSEXE ./cmsg_sender $IPVER -p $p $m $LIM $TGT 1234 | ||||
| 	    done | ||||
| 
 | ||||
| 	    tcpdump -r $TMPF -v 2>&1 | grep "hlim $LIM[^0-9]" >> /dev/null | ||||
| 	    check_result $? 0 "HOPLIMIT $prot $ovr - packet data" | ||||
| 	    tcpdump -r $TMPF -v 2>&1 | grep "$MATCH $LIM[^0-9]" >> /dev/null | ||||
| 	    check_result $? 0 "$MATCH $prot $ovr - packet data" | ||||
| 	    rm $TMPF | ||||
| 	done | ||||
|     done | ||||
| } | ||||
| 
 | ||||
| test_hoplimit | ||||
| test_ttl_hoplimit -4 $TGT4 ttl | ||||
| test_ttl_hoplimit -6 $TGT6 hlim | ||||
| 
 | ||||
| # IPV6 exthdr | ||||
| for p in u i r; do | ||||
|  | @ -72,7 +72,7 @@ struct options { | |||
| 		struct option_cmsg_u32 tclass; | ||||
| 		struct option_cmsg_u32 hlimit; | ||||
| 		struct option_cmsg_u32 exthdr; | ||||
| 	} v6; | ||||
| 	} cmsg; | ||||
| } opt = { | ||||
| 	.size = 13, | ||||
| 	.num_pkt = 1, | ||||
|  | @ -104,10 +104,10 @@ static void __attribute__((noreturn)) cs_usage(const char *bin) | |||
| 	       "\t\t-t      Enable time stamp reporting\n" | ||||
| 	       "\t\t-f val  Set don't fragment via cmsg\n" | ||||
| 	       "\t\t-F val  Set don't fragment via setsockopt\n" | ||||
| 	       "\t\t-c val  Set TCLASS via cmsg\n" | ||||
| 	       "\t\t-C val  Set TCLASS via setsockopt\n" | ||||
| 	       "\t\t-l val  Set HOPLIMIT via cmsg\n" | ||||
| 	       "\t\t-L val  Set HOPLIMIT via setsockopt\n" | ||||
| 	       "\t\t-c val  Set TOS/TCLASS via cmsg\n" | ||||
| 	       "\t\t-C val  Set TOS/TCLASS via setsockopt\n" | ||||
| 	       "\t\t-l val  Set TTL/HOPLIMIT via cmsg\n" | ||||
| 	       "\t\t-L val  Set TTL/HOPLIMIT via setsockopt\n" | ||||
| 	       "\t\t-H type Add an IPv6 header option\n" | ||||
| 	       "\t\t        (h = HOP; d = DST; r = RTDST)" | ||||
| 	       ""); | ||||
|  | @ -169,37 +169,37 @@ static void cs_parse_args(int argc, char *argv[]) | |||
| 			opt.ts.ena = true; | ||||
| 			break; | ||||
| 		case 'f': | ||||
| 			opt.v6.dontfrag.ena = true; | ||||
| 			opt.v6.dontfrag.val = atoi(optarg); | ||||
| 			opt.cmsg.dontfrag.ena = true; | ||||
| 			opt.cmsg.dontfrag.val = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'F': | ||||
| 			opt.sockopt.dontfrag = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'c': | ||||
| 			opt.v6.tclass.ena = true; | ||||
| 			opt.v6.tclass.val = atoi(optarg); | ||||
| 			opt.cmsg.tclass.ena = true; | ||||
| 			opt.cmsg.tclass.val = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'C': | ||||
| 			opt.sockopt.tclass = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'l': | ||||
| 			opt.v6.hlimit.ena = true; | ||||
| 			opt.v6.hlimit.val = atoi(optarg); | ||||
| 			opt.cmsg.hlimit.ena = true; | ||||
| 			opt.cmsg.hlimit.val = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'L': | ||||
| 			opt.sockopt.hlimit = atoi(optarg); | ||||
| 			break; | ||||
| 		case 'H': | ||||
| 			opt.v6.exthdr.ena = true; | ||||
| 			opt.cmsg.exthdr.ena = true; | ||||
| 			switch (optarg[0]) { | ||||
| 			case 'h': | ||||
| 				opt.v6.exthdr.val = IPV6_HOPOPTS; | ||||
| 				opt.cmsg.exthdr.val = IPV6_HOPOPTS; | ||||
| 				break; | ||||
| 			case 'd': | ||||
| 				opt.v6.exthdr.val = IPV6_DSTOPTS; | ||||
| 				opt.cmsg.exthdr.val = IPV6_DSTOPTS; | ||||
| 				break; | ||||
| 			case 'r': | ||||
| 				opt.v6.exthdr.val = IPV6_RTHDRDSTOPTS; | ||||
| 				opt.cmsg.exthdr.val = IPV6_RTHDRDSTOPTS; | ||||
| 				break; | ||||
| 			default: | ||||
| 				printf("Error: hdr type: %s\n", optarg); | ||||
|  | @ -261,12 +261,20 @@ cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz) | |||
| 			  SOL_SOCKET, SO_MARK, &opt.mark); | ||||
| 	ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 			  SOL_SOCKET, SO_PRIORITY, &opt.priority); | ||||
| 
 | ||||
| 	if (opt.sock.family == AF_INET) { | ||||
| 		ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 			  SOL_IPV6, IPV6_DONTFRAG, &opt.v6.dontfrag); | ||||
| 				  SOL_IP, IP_TOS, &opt.cmsg.tclass); | ||||
| 		ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 			  SOL_IPV6, IPV6_TCLASS, &opt.v6.tclass); | ||||
| 				  SOL_IP, IP_TTL, &opt.cmsg.hlimit); | ||||
| 	} else { | ||||
| 		ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 			  SOL_IPV6, IPV6_HOPLIMIT, &opt.v6.hlimit); | ||||
| 				  SOL_IPV6, IPV6_DONTFRAG, &opt.cmsg.dontfrag); | ||||
| 		ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 				  SOL_IPV6, IPV6_TCLASS, &opt.cmsg.tclass); | ||||
| 		ca_write_cmsg_u32(cbuf, cbuf_sz, &cmsg_len, | ||||
| 				  SOL_IPV6, IPV6_HOPLIMIT, &opt.cmsg.hlimit); | ||||
| 	} | ||||
| 
 | ||||
| 	if (opt.txtime.ena) { | ||||
| 		__u64 txtime; | ||||
|  | @ -297,14 +305,14 @@ cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz) | |||
| 		*(__u32 *)CMSG_DATA(cmsg) = SOF_TIMESTAMPING_TX_SCHED | | ||||
| 					    SOF_TIMESTAMPING_TX_SOFTWARE; | ||||
| 	} | ||||
| 	if (opt.v6.exthdr.ena) { | ||||
| 	if (opt.cmsg.exthdr.ena) { | ||||
| 		cmsg = (struct cmsghdr *)(cbuf + cmsg_len); | ||||
| 		cmsg_len += CMSG_SPACE(8); | ||||
| 		if (cbuf_sz < cmsg_len) | ||||
| 			error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small"); | ||||
| 
 | ||||
| 		cmsg->cmsg_level = SOL_IPV6; | ||||
| 		cmsg->cmsg_type = opt.v6.exthdr.val; | ||||
| 		cmsg->cmsg_type = opt.cmsg.exthdr.val; | ||||
| 		cmsg->cmsg_len = CMSG_LEN(8); | ||||
| 		*(__u64 *)CMSG_DATA(cmsg) = 0; | ||||
| 	} | ||||
|  | @ -405,6 +413,21 @@ static void ca_set_sockopts(int fd) | |||
| 	    setsockopt(fd, SOL_SOCKET, SO_MARK, | ||||
| 		       &opt.sockopt.mark, sizeof(opt.sockopt.mark))) | ||||
| 		error(ERN_SOCKOPT, errno, "setsockopt SO_MARK"); | ||||
| 	if (opt.sockopt.priority && | ||||
| 	    setsockopt(fd, SOL_SOCKET, SO_PRIORITY, | ||||
| 		       &opt.sockopt.priority, sizeof(opt.sockopt.priority))) | ||||
| 		error(ERN_SOCKOPT, errno, "setsockopt SO_PRIORITY"); | ||||
| 
 | ||||
| 	if (opt.sock.family == AF_INET) { | ||||
| 		if (opt.sockopt.tclass && | ||||
| 		    setsockopt(fd, SOL_IP, IP_TOS, | ||||
| 			       &opt.sockopt.tclass, sizeof(opt.sockopt.tclass))) | ||||
| 			error(ERN_SOCKOPT, errno, "setsockopt IP_TOS"); | ||||
| 		if (opt.sockopt.hlimit && | ||||
| 		    setsockopt(fd, SOL_IP, IP_TTL, | ||||
| 			       &opt.sockopt.hlimit, sizeof(opt.sockopt.hlimit))) | ||||
| 			error(ERN_SOCKOPT, errno, "setsockopt IP_TTL"); | ||||
| 	} else { | ||||
| 		if (opt.sockopt.dontfrag && | ||||
| 		    setsockopt(fd, SOL_IPV6, IPV6_DONTFRAG, | ||||
| 			       &opt.sockopt.dontfrag, sizeof(opt.sockopt.dontfrag))) | ||||
|  | @ -417,10 +440,7 @@ static void ca_set_sockopts(int fd) | |||
| 		    setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, | ||||
| 			       &opt.sockopt.hlimit, sizeof(opt.sockopt.hlimit))) | ||||
| 			error(ERN_SOCKOPT, errno, "setsockopt IPV6_HOPLIMIT"); | ||||
| 	if (opt.sockopt.priority && | ||||
| 	    setsockopt(fd, SOL_SOCKET, SO_PRIORITY, | ||||
| 		       &opt.sockopt.priority, sizeof(opt.sockopt.priority))) | ||||
| 		error(ERN_SOCKOPT, errno, "setsockopt SO_PRIORITY"); | ||||
| 	} | ||||
| 
 | ||||
| 	if (opt.txtime.ena) { | ||||
| 		struct sock_txtime so_txtime = { | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue
	
	 Willem de Bruijn
						Willem de Bruijn