Skip to content

Commit 7274563

Browse files
dborcaDenys Vlasenko
authored andcommitted
ping: try SOCK_DGRAM if no root privileges
Signed-off-by: Daniel Borca <[email protected]> Signed-off-by: Denys Vlasenko <[email protected]>
1 parent df0d2cd commit 7274563

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

networking/ping.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,22 @@ create_icmp_socket(void)
168168
#endif
169169
sock = socket(AF_INET, SOCK_RAW, 1); /* 1 == ICMP */
170170
if (sock < 0) {
171-
if (errno == EPERM)
172-
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
173-
bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
171+
if (errno != EPERM)
172+
bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
173+
#if defined(__linux__) || defined(__APPLE__)
174+
/* We don't have root privileges. Try SOCK_DGRAM instead.
175+
* Linux needs net.ipv4.ping_group_range for this to work.
176+
* MacOSX allows ICMP_ECHO, ICMP_TSTAMP or ICMP_MASKREQ
177+
*/
178+
#if ENABLE_PING6
179+
if (lsa->u.sa.sa_family == AF_INET6)
180+
sock = socket(AF_INET6, SOCK_DGRAM, IPPROTO_ICMPV6);
181+
else
182+
#endif
183+
sock = socket(AF_INET, SOCK_DGRAM, 1); /* 1 == ICMP */
184+
if (sock < 0)
185+
#endif
186+
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
174187
}
175188

176189
xmove_fd(sock, pingsock);

0 commit comments

Comments
 (0)