Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 5 | * create raw socket for icmp (IPv6 version) protocol |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 6 | * and drop root privileges if running setuid |
Denis Vlasenko | db12d1d | 2008-12-07 00:52:58 +0000 | [diff] [blame^] | 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
| 12 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 13 | #if ENABLE_FEATURE_IPV6 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 14 | int FAST_FUNC create_icmp6_socket(void) |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 15 | { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 16 | int sock; |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 17 | #if 0 |
| 18 | struct protoent *proto; |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 19 | proto = getprotobyname("ipv6-icmp"); |
| 20 | /* if getprotobyname failed, just silently force |
| 21 | * proto->p_proto to have the correct value for "ipv6-icmp" */ |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 22 | sock = socket(AF_INET6, SOCK_RAW, |
| 23 | (proto ? proto->p_proto : IPPROTO_ICMPV6)); |
Denis Vlasenko | 4e6d511 | 2008-03-12 22:14:34 +0000 | [diff] [blame] | 24 | #else |
| 25 | sock = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6); |
| 26 | #endif |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 27 | if (sock < 0) { |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 28 | if (errno == EPERM) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
Denis Vlasenko | 19c238b | 2007-03-03 00:36:35 +0000 | [diff] [blame] | 30 | bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | /* drop root privs if running setuid */ |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 34 | xsetuid(getuid()); |
Eric Andersen | 51b8bd6 | 2002-07-03 11:46:38 +0000 | [diff] [blame] | 35 | |
| 36 | return sock; |
| 37 | } |
| 38 | #endif |