git: e23a0f521f68 - stable/13 - ping: if -S srcaddr uses a numeric address, use that protocol

From: Mike Karels <karels_at_FreeBSD.org>
Date: Fri, 27 May 2022 12:16:48 UTC
The branch stable/13 has been updated by karels:

URL: https://cgit.FreeBSD.org/src/commit/?id=e23a0f521f68c8385d5b98897cf0135dc9bac78a

commit e23a0f521f68c8385d5b98897cf0135dc9bac78a
Author:     Mike Karels <karels@FreeBSD.org>
AuthorDate: 2022-05-20 14:16:01 +0000
Commit:     Mike Karels <karels@FreeBSD.org>
CommitDate: 2022-05-27 12:11:46 +0000

    ping: if -S srcaddr uses a numeric address, use that protocol
    
    The command "ping -S dotted.quad hostname" fails on dual-stack hosts
    with the confusing message "ping: invalid source address: Name does
    not resolve" because IPv6 is selected in preference.  If the argument
    to -S is numeric (likely), select the corresponding address family,
    as if -4 or -6 was specified.  Add tests that either IPv4 or IPv6 can
    be forced via a -S parameter.
    
    Reviewed by:    asomers
    Differential Revision: https://reviews.freebsd.org/D35271
    
    (cherry picked from commit 5af718a5927a9e078201ebc7ad1310e5b4918b7a)
---
 sbin/ping/main.c                        | 12 ++++++++++++
 sbin/ping/tests/ping_c1_s56_t1_S127.out |  6 ++++++
 sbin/ping/tests/ping_c1_s8_t1_S1.out    |  6 ++++++
 sbin/ping/tests/ping_test.sh            | 26 ++++++++++++++++++++++++++
 4 files changed, 50 insertions(+)

diff --git a/sbin/ping/main.c b/sbin/ping/main.c
index 1d0b714f1480..11fa17b5ce2d 100644
--- a/sbin/ping/main.c
+++ b/sbin/ping/main.c
@@ -93,6 +93,18 @@ main(int argc, char *argv[])
 		case '6':
 			ipv6 = true;
 			break;
+#endif
+#if defined(INET) && defined(INET6)
+		case 'S':
+			/*
+			 * If -S is given with a numeric parameter,
+			 * force use of the corresponding version.
+			 */
+			if (inet_pton(AF_INET, optarg, &a) == 1)
+				ipv4 = true;
+			else if (inet_pton(AF_INET6, optarg, &a) == 1)
+				ipv6 = true;
+			break;
 #endif
 		default:
 			break;
diff --git a/sbin/ping/tests/ping_c1_s56_t1_S127.out b/sbin/ping/tests/ping_c1_s56_t1_S127.out
new file mode 100644
index 000000000000..07d90918293b
--- /dev/null
+++ b/sbin/ping/tests/ping_c1_s56_t1_S127.out
@@ -0,0 +1,6 @@
+PING localhost from: 56 data bytes
+64 bytes from: icmp_seq=0 ttl= time= ms
+
+--- localhost ping statistics ---
+1 packets transmitted, 1 packets received, 0.0% packet loss
+round-trip min/avg/max/stddev = /// ms
diff --git a/sbin/ping/tests/ping_c1_s8_t1_S1.out b/sbin/ping/tests/ping_c1_s8_t1_S1.out
new file mode 100644
index 000000000000..81c56e6cf586
--- /dev/null
+++ b/sbin/ping/tests/ping_c1_s8_t1_S1.out
@@ -0,0 +1,6 @@
+PING6(56=40+8+8 bytes) ::1 --> ::1
+16 bytes from ::1, icmp_seq=0 hlim= time= ms
+
+--- localhost ping6 statistics ---
+1 packets transmitted, 1 packets received, 0.0% packet loss
+round-trip min/avg/max/std-dev = /// ms
diff --git a/sbin/ping/tests/ping_test.sh b/sbin/ping/tests/ping_test.sh
index 54af89f4a22b..7f11af7e9818 100644
--- a/sbin/ping/tests/ping_test.sh
+++ b/sbin/ping/tests/ping_test.sh
@@ -49,6 +49,18 @@ ping_c1_s56_t1_body() {
     check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1.out
 }
 
+atf_test_case ping_c1_s56_t1_S127
+ping_c1_s56_t1_S127_head() {
+    atf_set "descr" "Check that ping -S 127.0.0.1 localhost succeeds"
+}
+ping_c1_s56_t1_S127_body() {
+    require_ipv4
+    require_ipv6
+    atf_check -s exit:0 -o save:std.out -e empty \
+	      ping -c 1 -s 56 -t 1 -S 127.0.0.1 localhost
+    check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1_S127.out
+}
+
 atf_test_case ping_6_c1_s8_t1
 ping_6_c1_s8_t1_head() {
     atf_set "descr" "Stop after receiving 1 ECHO_RESPONSE packet"
@@ -60,6 +72,18 @@ ping_6_c1_s8_t1_body() {
     check_ping_statistics std.out $(atf_get_srcdir)/ping_6_c1_s8_t1.out
 }
 
+atf_test_case ping_c1_s8_t1_S1
+ping_c1_s8_t1_S1_head() {
+    atf_set "descr" "Check that ping -S ::1 localhost succeeds"
+}
+ping_c1_s8_t1_S1_body() {
+    require_ipv4
+    require_ipv6
+    atf_check -s exit:0 -o save:std.out -e empty \
+	      ping -c 1 -s 8 -t 1 -S ::1 localhost
+    check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s8_t1_S1.out
+}
+
 atf_test_case ping6_c1_s8_t1
 ping6_c1_s8_t1_head() {
     atf_set "descr" "Use IPv6 when invoked as ping6"
@@ -104,7 +128,9 @@ ping6_46_body() {
 
 atf_init_test_cases() {
     atf_add_test_case ping_c1_s56_t1
+    atf_add_test_case ping_c1_s56_t1_S127
     atf_add_test_case ping_6_c1_s8_t1
+    atf_add_test_case ping_c1_s8_t1_S1
     atf_add_test_case ping6_c1_s8_t1
     atf_add_test_case ping_c1t6
     atf_add_test_case ping6_c1t4