| 1 |
/* |
| 2 |
* |
| 3 |
* Modified for AF_INET6 by Pedro Roque |
| 4 |
* |
| 5 |
* <[email protected]> |
| 6 |
* |
| 7 |
* Original copyright notice included below |
| 8 |
*/ |
| 9 |
|
| 10 |
/* |
| 11 |
* Copyright (c) 1989 The Regents of the University of California. |
| 12 |
* All rights reserved. |
| 13 |
* |
| 14 |
* This code is derived from software contributed to Berkeley by |
| 15 |
* Mike Muuss. |
| 16 |
* |
| 17 |
* Redistribution and use in source and binary forms, with or without |
| 18 |
* modification, are permitted provided that the following conditions |
| 19 |
* are met: |
| 20 |
* 1. Redistributions of source code must retain the above copyright |
| 21 |
* notice, this list of conditions and the following disclaimer. |
| 22 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 23 |
* notice, this list of conditions and the following disclaimer in the |
| 24 |
* documentation and/or other materials provided with the distribution. |
| 25 |
* 3. Neither the name of the University nor the names of its contributors |
| 26 |
* may be used to endorse or promote products derived from this software |
| 27 |
* without specific prior written permission. |
| 28 |
* |
| 29 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 30 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 31 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 32 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 33 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 34 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 35 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 36 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 37 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 38 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 39 |
* SUCH DAMAGE. |
| 40 |
*/ |
| 41 |
/* |
| 42 |
* P I N G . C |
| 43 |
* |
| 44 |
* Using the InterNet Control Message Protocol (ICMP) "ECHO" facility, |
| 45 |
* measure round-trip-delays and packet loss across network paths. |
| 46 |
* |
| 47 |
* Author - |
| 48 |
* Mike Muuss |
| 49 |
* U. S. Army Ballistic Research Laboratory |
| 50 |
* December, 1983 |
| 51 |
* |
| 52 |
* Status - |
| 53 |
* Public Domain. Distribution Unlimited. |
| 54 |
* Bugs - |
| 55 |
* More statistics could always be gathered. |
| 56 |
* If kernel does not support ICMP datagram sockets or |
| 57 |
* if -N option is used, this program has to run SUID to ROOT or |
| 58 |
* with net_cap_raw enabled. |
| 59 |
*/ |
| 60 |
|
| 61 |
#define _GNU_SOURCE |
| 62 |
|
| 63 |
#include <stddef.h> |
| 64 |
|
| 65 |
#include "iputils_common.h" |
| 66 |
#include "iputils_ni.h" |
| 67 |
#include "ipv6.h" |
| 68 |
#include "ping.h" |
| 69 |
|
| 70 |
#ifndef IPV6_FLOWLABEL_MGR |
| 71 |
# define IPV6_FLOWLABEL_MGR 32 |
| 72 |
#endif |
| 73 |
#ifndef IPV6_FLOWINFO_SEND |
| 74 |
# define IPV6_FLOWINFO_SEND 33 |
| 75 |
#endif |
| 76 |
|
| 77 |
ping_func_set_st ping6_func_set = { |
| 78 |
.send_probe = ping6_send_probe, |
| 79 |
.receive_error_msg = ping6_receive_error_msg, |
| 80 |
.parse_reply = ping6_parse_reply, |
| 81 |
.install_filter = ping6_install_filter |
| 82 |
}; |
| 83 |
|
| 84 |
#ifndef SCOPE_DELIMITER |
| 85 |
# define SCOPE_DELIMITER '%' |
| 86 |
#endif |
| 87 |
|
| 88 |
int ntohsp(uint16_t *p) |
| 89 |
{ |
| 90 |
uint16_t v; |
| 91 |
memcpy(&v, p, sizeof(v)); |
| 92 |
return ntohs(v); |
| 93 |
} |
| 94 |
|
| 95 |
unsigned int if_name2index(const char *ifname) |
| 96 |
{ |
| 97 |
unsigned int i = if_nametoindex(ifname); |
| 98 |
if (!i) |
| 99 |
error(2, 0, _("unknown iface: %s"), ifname); |
| 100 |
return i; |
| 101 |
} |
| 102 |
|
| 103 |
/* return >= 0: exit with this code, < 0: go on to next addrinfo result */ |
| 104 |
int ping6_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai, |
| 105 |
struct socket_st *sock) |
| 106 |
{ |
| 107 |
int hold, packlen; |
| 108 |
size_t i; |
| 109 |
unsigned char *packet; |
| 110 |
char *target; |
| 111 |
struct icmp6_filter filter; |
| 112 |
int err, ret; |
| 113 |
static uint32_t scope_id = 0; |
| 114 |
|
| 115 |
if (niquery_is_enabled(&rts->ni)) { |
| 116 |
niquery_init_nonce(&rts->ni); |
| 117 |
|
| 118 |
if (!niquery_is_subject_valid(&rts->ni)) { |
| 119 |
rts->ni.subject = &rts->whereto6.sin6_addr; |
| 120 |
rts->ni.subject_len = sizeof(rts->whereto6.sin6_addr); |
| 121 |
rts->ni.subject_type = IPUTILS_NI_ICMP6_SUBJ_IPV6; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if (argc > 1) { |
| 126 |
usage(); |
| 127 |
} else if (argc == 1) { |
| 128 |
target = *argv; |
| 129 |
} else { |
| 130 |
if (rts->ni.query < 0 && rts->ni.subject_type != IPUTILS_NI_ICMP6_SUBJ_FQDN) |
| 131 |
usage(); |
| 132 |
target = rts->ni.group; |
| 133 |
} |
| 134 |
|
| 135 |
memcpy(&rts->whereto6, ai->ai_addr, sizeof(rts->whereto6)); |
| 136 |
rts->whereto6.sin6_port = htons(IPPROTO_ICMPV6); |
| 137 |
|
| 138 |
if (memchr(target, ':', strlen(target))) |
| 139 |
rts->opt_numeric = 1; |
| 140 |
|
| 141 |
if (IN6_IS_ADDR_UNSPECIFIED(&rts->firsthop.sin6_addr)) { |
| 142 |
memcpy(&rts->firsthop.sin6_addr, &rts->whereto6.sin6_addr, 16); |
| 143 |
rts->firsthop.sin6_scope_id = rts->whereto6.sin6_scope_id; |
| 144 |
/* Verify scope_id is the same as intermediate nodes */ |
| 145 |
if (rts->firsthop.sin6_scope_id && scope_id && rts->firsthop.sin6_scope_id != scope_id) |
| 146 |
error(2, 0, _("scope discrepancy among the nodes")); |
| 147 |
else if (!scope_id) |
| 148 |
scope_id = rts->firsthop.sin6_scope_id; |
| 149 |
} |
| 150 |
|
| 151 |
rts->hostname = target; |
| 152 |
|
| 153 |
if (IN6_IS_ADDR_UNSPECIFIED(&rts->source6.sin6_addr)) { |
| 154 |
socklen_t alen; |
| 155 |
int probe_fd = socket(AF_INET6, SOCK_DGRAM, 0); |
| 156 |
|
| 157 |
if (probe_fd < 0) |
| 158 |
error(2, errno, "socket"); |
| 159 |
if (rts->device) { |
| 160 |
unsigned int iface = if_name2index(rts->device); |
| 161 |
#ifdef IPV6_RECVPKTINFO |
| 162 |
struct in6_pktinfo ipi; |
| 163 |
|
| 164 |
memset(&ipi, 0, sizeof(ipi)); |
| 165 |
ipi.ipi6_ifindex = iface; |
| 166 |
#endif |
| 167 |
|
| 168 |
if (IN6_IS_ADDR_LINKLOCAL(&rts->firsthop.sin6_addr) || |
| 169 |
IN6_IS_ADDR_MC_LINKLOCAL(&rts->firsthop.sin6_addr)) |
| 170 |
rts->firsthop.sin6_scope_id = iface; |
| 171 |
enable_capability_raw(); |
| 172 |
#ifdef IPV6_RECVPKTINFO |
| 173 |
if (setsockopt(probe_fd, IPPROTO_IPV6, IPV6_PKTINFO, &ipi, sizeof ipi) == -1 || |
| 174 |
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO, &ipi, sizeof ipi) == -1) { |
| 175 |
error(2, errno, "setsockopt(IPV6_PKTINFO)"); |
| 176 |
} |
| 177 |
#endif |
| 178 |
if (setsockopt(probe_fd, SOL_SOCKET, SO_BINDTODEVICE, rts->device, strlen(rts->device) + 1) == -1 || |
| 179 |
setsockopt(sock->fd, SOL_SOCKET, SO_BINDTODEVICE, rts->device, strlen(rts->device) + 1) == -1) { |
| 180 |
error(2, errno, "setsockopt(SO_BINDTODEVICE) %s", rts->device); |
| 181 |
} |
| 182 |
disable_capability_raw(); |
| 183 |
} |
| 184 |
|
| 185 |
if (rts->tclass && |
| 186 |
setsockopt(probe_fd, IPPROTO_IPV6, IPV6_TCLASS, &rts->tclass, sizeof (rts->tclass)) <0) |
| 187 |
error(2, errno, "setsockopt(IPV6_TCLASS)"); |
| 188 |
|
| 189 |
if (!IN6_IS_ADDR_LINKLOCAL(&rts->firsthop.sin6_addr) && |
| 190 |
!IN6_IS_ADDR_MC_LINKLOCAL(&rts->firsthop.sin6_addr)) |
| 191 |
rts->firsthop.sin6_family = AF_INET6; |
| 192 |
|
| 193 |
sock_setmark(rts, probe_fd); |
| 194 |
|
| 195 |
rts->firsthop.sin6_port = htons(1025); |
| 196 |
if (connect(probe_fd, (struct sockaddr *)&rts->firsthop, sizeof(rts->firsthop)) == -1) { |
| 197 |
if ((errno == EHOSTUNREACH || errno == ENETUNREACH) && ai->ai_next) { |
| 198 |
close(probe_fd); |
| 199 |
return -1; |
| 200 |
} |
| 201 |
error(2, errno, "connect"); |
| 202 |
} |
| 203 |
alen = sizeof rts->source6; |
| 204 |
if (getsockname(probe_fd, (struct sockaddr *)&rts->source6, &alen) == -1) |
| 205 |
error(2, errno, "getsockname"); |
| 206 |
rts->source6.sin6_port = 0; |
| 207 |
close(probe_fd); |
| 208 |
|
| 209 |
if (rts->device) { |
| 210 |
struct ifaddrs *ifa0, *ifa; |
| 211 |
|
| 212 |
if (getifaddrs(&ifa0)) |
| 213 |
error(2, errno, "getifaddrs"); |
| 214 |
|
| 215 |
for (ifa = ifa0; ifa; ifa = ifa->ifa_next) { |
| 216 |
if (!ifa->ifa_name || !ifa->ifa_addr || |
| 217 |
ifa->ifa_addr->sa_family != AF_INET6) |
| 218 |
continue; |
| 219 |
if (!strcmp(ifa->ifa_name, rts->device) && |
| 220 |
IN6_ARE_ADDR_EQUAL(&((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr, |
| 221 |
&rts->source6.sin6_addr)) |
| 222 |
break; |
| 223 |
} |
| 224 |
if (!ifa) |
| 225 |
error(0, 0, _("Warning: source address might be selected on device other than: %s"), rts->device); |
| 226 |
|
| 227 |
freeifaddrs(ifa0); |
| 228 |
} |
| 229 |
} else if (rts->device && (IN6_IS_ADDR_LINKLOCAL(&rts->source6.sin6_addr) || |
| 230 |
IN6_IS_ADDR_MC_LINKLOCAL(&rts->source6.sin6_addr))) |
| 231 |
rts->source6.sin6_scope_id = if_name2index(rts->device); |
| 232 |
|
| 233 |
if (rts->device) { |
| 234 |
struct cmsghdr *cmsg; |
| 235 |
struct in6_pktinfo *ipi; |
| 236 |
int rc; |
| 237 |
int errno_save; |
| 238 |
|
| 239 |
cmsg = (struct cmsghdr *)(rts->cmsgbuf + rts->cmsglen); |
| 240 |
rts->cmsglen += CMSG_SPACE(sizeof(*ipi)); |
| 241 |
cmsg->cmsg_len = CMSG_LEN(sizeof(*ipi)); |
| 242 |
cmsg->cmsg_level = IPPROTO_IPV6; |
| 243 |
cmsg->cmsg_type = IPV6_PKTINFO; |
| 244 |
|
| 245 |
ipi = (struct in6_pktinfo *)CMSG_DATA(cmsg); |
| 246 |
memset(ipi, 0, sizeof(*ipi)); |
| 247 |
ipi->ipi6_ifindex = if_name2index(rts->device); |
| 248 |
|
| 249 |
if (rts->opt_strictsource) { |
| 250 |
enable_capability_raw(); |
| 251 |
rc = setsockopt(sock->fd, SOL_SOCKET, SO_BINDTODEVICE, |
| 252 |
rts->device, strlen(rts->device) + 1); |
| 253 |
errno_save = errno; |
| 254 |
disable_capability_raw(); |
| 255 |
|
| 256 |
if (rc == -1) |
| 257 |
error(2, errno_save, "SO_BINDTODEVICE %s", rts->device); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if (IN6_IS_ADDR_MULTICAST(&rts->whereto6.sin6_addr)) { |
| 262 |
rts->multicast = 1; |
| 263 |
|
| 264 |
if (rts->uid) { |
| 265 |
if (rts->interval < MIN_MULTICAST_USER_INTERVAL_MS) |
| 266 |
error(2, 0, _("minimal interval for multicast ping for user must be >= %d ms, use -i %s (or higher)"), |
| 267 |
MIN_MULTICAST_USER_INTERVAL_MS, |
| 268 |
str_interval(MIN_MULTICAST_USER_INTERVAL_MS)); |
| 269 |
|
| 270 |
if (rts->pmtudisc >= 0 && rts->pmtudisc != IPV6_PMTUDISC_DO) |
| 271 |
error(2, 0, _("multicast ping does not fragment")); |
| 272 |
} |
| 273 |
|
| 274 |
if (rts->pmtudisc < 0) |
| 275 |
rts->pmtudisc = IPV6_PMTUDISC_DO; |
| 276 |
} |
| 277 |
|
| 278 |
/* detect Subnet-Router anycast at least for the default prefix 64 */ |
| 279 |
rts->subnet_router_anycast = 1; |
| 280 |
for (i = 8; i < sizeof(struct in6_addr); i++) { |
| 281 |
if (rts->whereto6.sin6_addr.s6_addr[i]) { |
| 282 |
rts->subnet_router_anycast = 0; |
| 283 |
break; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
if (rts->pmtudisc >= 0) { |
| 288 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &rts->pmtudisc, |
| 289 |
sizeof rts->pmtudisc) == -1) |
| 290 |
error(2, errno, "IPV6_MTU_DISCOVER"); |
| 291 |
} |
| 292 |
|
| 293 |
int set_ident = rts->ident > 0 && sock->socktype == SOCK_DGRAM; |
| 294 |
if (set_ident) |
| 295 |
rts->source6.sin6_port = rts->ident; |
| 296 |
|
| 297 |
if (rts->opt_strictsource || set_ident) { |
| 298 |
if (bind(sock->fd, (struct sockaddr *)&rts->source6, sizeof rts->source6) == -1) |
| 299 |
error(2, errno, "bind icmp socket"); |
| 300 |
} |
| 301 |
|
| 302 |
if ((ssize_t)rts->datalen >= (ssize_t)sizeof(struct timeval) && (rts->ni.query < 0)) { |
| 303 |
/* can we time transfer */ |
| 304 |
rts->timing = 1; |
| 305 |
} |
| 306 |
packlen = rts->datalen + 8 + 4096 + 40 + 8; /* 4096 for rthdr */ |
| 307 |
if (!(packet = (unsigned char *)malloc((unsigned int)packlen))) |
| 308 |
error(2, errno, _("memory allocation failed")); |
| 309 |
|
| 310 |
hold = 1; |
| 311 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVERR, (const void *)&hold, sizeof hold)) |
| 312 |
error(2, errno, "IPV6_RECVERR"); |
| 313 |
|
| 314 |
/* Estimate memory eaten by single packet. It is rough estimate. |
| 315 |
* Actually, for small datalen's it depends on kernel side a lot. */ |
| 316 |
hold = rts->datalen + 8; |
| 317 |
hold += ((hold + 511) / 512) * (40 + 16 + 64 + 160); |
| 318 |
sock_setbufs(rts, sock, hold); |
| 319 |
|
| 320 |
#ifdef __linux__ |
| 321 |
if (sock->socktype == SOCK_RAW) { |
| 322 |
int csum_offset = 2; |
| 323 |
int sz_opt = sizeof(int); |
| 324 |
|
| 325 |
err = setsockopt(sock->fd, SOL_RAW, IPV6_CHECKSUM, &csum_offset, sz_opt); |
| 326 |
if (err < 0) { |
| 327 |
/* checksum should be enabled by default and setting this |
| 328 |
* option might fail anyway. |
| 329 |
*/ |
| 330 |
error(0, errno, _("setsockopt(RAW_CHECKSUM) failed - try to continue")); |
| 331 |
} |
| 332 |
#else |
| 333 |
{ |
| 334 |
#endif |
| 335 |
|
| 336 |
/* |
| 337 |
* select icmp echo reply as icmp type to receive |
| 338 |
*/ |
| 339 |
|
| 340 |
ICMP6_FILTER_SETBLOCKALL(&filter); |
| 341 |
|
| 342 |
if (niquery_is_enabled(&rts->ni)) |
| 343 |
ICMP6_FILTER_SETPASS(IPUTILS_NI_ICMP6_REPLY, &filter); |
| 344 |
else |
| 345 |
ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filter); |
| 346 |
|
| 347 |
err = setsockopt(sock->fd, IPPROTO_ICMPV6, ICMP6_FILTER, &filter, sizeof filter); |
| 348 |
|
| 349 |
if (err < 0) |
| 350 |
error(2, errno, "setsockopt(ICMP6_FILTER)"); |
| 351 |
} |
| 352 |
|
| 353 |
if (rts->opt_noloop) { |
| 354 |
int loop = 0; |
| 355 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &loop, sizeof loop) == -1) |
| 356 |
error(2, errno, _("can't disable multicast loopback")); |
| 357 |
} |
| 358 |
if (rts->opt_ttl) { |
| 359 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &rts->ttl, |
| 360 |
sizeof rts->ttl) == -1) |
| 361 |
error(2, errno, _("can't set multicast hop limit")); |
| 362 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &rts->ttl, |
| 363 |
sizeof rts->ttl) == -1) |
| 364 |
error(2, errno, _("can't set unicast hop limit")); |
| 365 |
} |
| 366 |
|
| 367 |
const int on = 1; |
| 368 |
if ( |
| 369 |
#ifdef IPV6_RECVHOPLIMIT |
| 370 |
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &on, sizeof on) == -1 && |
| 371 |
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_2292HOPLIMIT, &on, sizeof on) == -1 |
| 372 |
#else |
| 373 |
setsockopt(sock->fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof on) == -1 |
| 374 |
#endif |
| 375 |
) |
| 376 |
error(2, errno, _("can't receive hop limit")); |
| 377 |
|
| 378 |
if (rts->opt_flowinfo) { |
| 379 |
char freq_buf[CMSG_ALIGN(sizeof(struct in6_flowlabel_req)) + rts->cmsglen]; |
| 380 |
struct in6_flowlabel_req *freq = (struct in6_flowlabel_req *)freq_buf; |
| 381 |
int freq_len = sizeof(*freq); |
| 382 |
|
| 383 |
memset(freq, 0, sizeof(*freq)); |
| 384 |
freq->flr_label = htonl(rts->flowlabel & IPV6_FLOWINFO_FLOWLABEL); |
| 385 |
freq->flr_action = IPV6_FL_A_GET; |
| 386 |
freq->flr_flags = IPV6_FL_F_CREATE; |
| 387 |
freq->flr_share = IPV6_FL_S_EXCL; |
| 388 |
memcpy(&freq->flr_dst, &rts->whereto6.sin6_addr, 16); |
| 389 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_FLOWLABEL_MGR, freq, freq_len) == -1) |
| 390 |
error(2, errno, _("can't set flowlabel")); |
| 391 |
rts->flowlabel = freq->flr_label; |
| 392 |
rts->whereto6.sin6_flowinfo = rts->flowlabel; |
| 393 |
if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_FLOWINFO_SEND, &on, sizeof on) == -1) |
| 394 |
error(2, errno, _("can't send flowinfo")); |
| 395 |
} |
| 396 |
|
| 397 |
printf(_("PING %s (%s) "), rts->hostname, pr_raw_addr(rts, &rts->whereto6, sizeof rts->whereto6)); |
| 398 |
if (rts->flowlabel) |
| 399 |
printf(_(", flow 0x%05x, "), (unsigned)ntohl(rts->flowlabel)); |
| 400 |
if (rts->device || rts->opt_strictsource) { |
| 401 |
int saved_opt_numeric = rts->opt_numeric; |
| 402 |
|
| 403 |
rts->opt_numeric = 1; |
| 404 |
printf(_("from %s %s: "), pr_addr(rts, &rts->source6, sizeof rts->source6), rts->device ? rts->device : ""); |
| 405 |
rts->opt_numeric = saved_opt_numeric; |
| 406 |
} |
| 407 |
printf(_("%d data bytes\n"), rts->datalen); |
| 408 |
|
| 409 |
setup(rts, sock); |
| 410 |
|
| 411 |
drop_capabilities(); |
| 412 |
|
| 413 |
ret = main_loop(rts, &ping6_func_set, sock, packet, packlen); |
| 414 |
free(packet); |
| 415 |
|
| 416 |
return ret; |
| 417 |
} |
| 418 |
|
| 419 |
int print_icmp(uint8_t type, uint8_t code, uint32_t info) |
| 420 |
{ |
| 421 |
switch (type) { |
| 422 |
case ICMP6_DST_UNREACH: |
| 423 |
printf(_("Destination unreachable: ")); |
| 424 |
switch (code) { |
| 425 |
case ICMP6_DST_UNREACH_NOROUTE: |
| 426 |
printf(_("No route")); |
| 427 |
break; |
| 428 |
case ICMP6_DST_UNREACH_ADMIN: |
| 429 |
printf(_("Administratively prohibited")); |
| 430 |
break; |
| 431 |
case ICMP6_DST_UNREACH_BEYONDSCOPE: |
| 432 |
printf(_("Beyond scope of source address")); |
| 433 |
break; |
| 434 |
case ICMP6_DST_UNREACH_ADDR: |
| 435 |
printf(_("Address unreachable")); |
| 436 |
break; |
| 437 |
case ICMP6_DST_UNREACH_NOPORT: |
| 438 |
printf(_("Port unreachable")); |
| 439 |
break; |
| 440 |
case ICMP6_DST_UNREACH_POLICYFAIL: |
| 441 |
printf(_("Source address failed ingress/egress policy")); |
| 442 |
break; |
| 443 |
case ICMP6_DST_UNREACH_REJECTROUTE: |
| 444 |
printf(_("Reject route to destination")); |
| 445 |
break; |
| 446 |
default: |
| 447 |
printf(_("Unknown code %d"), code); |
| 448 |
break; |
| 449 |
} |
| 450 |
break; |
| 451 |
case ICMP6_PACKET_TOO_BIG: |
| 452 |
printf(_("Packet too big: mtu=%u"), info); |
| 453 |
if (code) |
| 454 |
printf(_(", code=%d"), code); |
| 455 |
break; |
| 456 |
case ICMP6_TIME_EXCEEDED: |
| 457 |
printf(_("Time exceeded: ")); |
| 458 |
if (code == ICMP6_TIME_EXCEED_TRANSIT) |
| 459 |
printf(_("Hop limit")); |
| 460 |
else if (code == ICMP6_TIME_EXCEED_REASSEMBLY) |
| 461 |
printf(_("Defragmentation failure")); |
| 462 |
else |
| 463 |
printf(_("code %d"), code); |
| 464 |
break; |
| 465 |
case ICMP6_PARAM_PROB: |
| 466 |
printf(_("Parameter problem: ")); |
| 467 |
if (code == ICMP6_PARAMPROB_HEADER) |
| 468 |
printf(_("Wrong header field ")); |
| 469 |
else if (code == ICMP6_PARAMPROB_NEXTHEADER) |
| 470 |
printf(_("Unknown header ")); |
| 471 |
else if (code == ICMP6_PARAMPROB_OPTION) |
| 472 |
printf(_("Unknown option ")); |
| 473 |
else |
| 474 |
printf(_("code %d "), code); |
| 475 |
printf(_("at %u"), info); |
| 476 |
break; |
| 477 |
case ICMP6_ECHO_REQUEST: |
| 478 |
printf(_("Echo request")); |
| 479 |
break; |
| 480 |
case ICMP6_ECHO_REPLY: |
| 481 |
printf(_("Echo reply")); |
| 482 |
break; |
| 483 |
case MLD_LISTENER_QUERY: |
| 484 |
printf(_("MLD Query")); |
| 485 |
break; |
| 486 |
case MLD_LISTENER_REPORT: |
| 487 |
printf(_("MLD Report")); |
| 488 |
break; |
| 489 |
case MLD_LISTENER_REDUCTION: |
| 490 |
printf(_("MLD Reduction")); |
| 491 |
break; |
| 492 |
default: |
| 493 |
printf(_("unknown icmp type: %u"), type); |
| 494 |
|
| 495 |
} |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
int ping6_receive_error_msg(struct ping_rts *rts, socket_st *sock) |
| 500 |
{ |
| 501 |
ssize_t res; |
| 502 |
char cbuf[512]; |
| 503 |
struct iovec iov; |
| 504 |
struct msghdr msg; |
| 505 |
struct cmsghdr *cmsg; |
| 506 |
struct sock_extended_err *e; |
| 507 |
struct icmp6_hdr icmph; |
| 508 |
struct sockaddr_in6 target; |
| 509 |
int net_errors = 0; |
| 510 |
int local_errors = 0; |
| 511 |
int saved_errno = errno; |
| 512 |
|
| 513 |
iov.iov_base = &icmph; |
| 514 |
iov.iov_len = sizeof(icmph); |
| 515 |
msg.msg_name = (void *)⌖ |
| 516 |
msg.msg_namelen = sizeof(target); |
| 517 |
msg.msg_iov = &iov; |
| 518 |
msg.msg_iovlen = 1; |
| 519 |
msg.msg_flags = 0; |
| 520 |
msg.msg_control = cbuf; |
| 521 |
msg.msg_controllen = sizeof(cbuf); |
| 522 |
|
| 523 |
res = recvmsg(sock->fd, &msg, MSG_ERRQUEUE | MSG_DONTWAIT); |
| 524 |
if (res < 0) { |
| 525 |
if (errno == EAGAIN || errno == EINTR) |
| 526 |
local_errors++; |
| 527 |
goto out; |
| 528 |
} |
| 529 |
|
| 530 |
e = NULL; |
| 531 |
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
| 532 |
if (cmsg->cmsg_level == IPPROTO_IPV6) { |
| 533 |
if (cmsg->cmsg_type == IPV6_RECVERR) |
| 534 |
e = (struct sock_extended_err *)CMSG_DATA(cmsg); |
| 535 |
} |
| 536 |
} |
| 537 |
if (e == NULL) |
| 538 |
abort(); |
| 539 |
|
| 540 |
if (e->ee_origin == SO_EE_ORIGIN_LOCAL) { |
| 541 |
local_errors++; |
| 542 |
if (rts->opt_quiet) |
| 543 |
goto out; |
| 544 |
if (rts->opt_flood) |
| 545 |
write_stdout("E", 1); |
| 546 |
else if (e->ee_errno != EMSGSIZE) |
| 547 |
error(0, e->ee_errno, _("local error")); |
| 548 |
else |
| 549 |
error(0, 0, _("local error: message too long, mtu: %u"), e->ee_info); |
| 550 |
rts->nerrors++; |
| 551 |
} else if (e->ee_origin == SO_EE_ORIGIN_ICMP6) { |
| 552 |
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)(e + 1); |
| 553 |
|
| 554 |
if ((size_t)res < sizeof(icmph) || |
| 555 |
memcmp(&target.sin6_addr, &rts->whereto6.sin6_addr, 16) || |
| 556 |
icmph.icmp6_type != ICMP6_ECHO_REQUEST || |
| 557 |
!is_ours(rts, sock, icmph.icmp6_id)) { |
| 558 |
/* Not our error, not an error at all. Clear. */ |
| 559 |
saved_errno = 0; |
| 560 |
goto out; |
| 561 |
} |
| 562 |
|
| 563 |
net_errors++; |
| 564 |
rts->nerrors++; |
| 565 |
if (rts->opt_quiet) |
| 566 |
goto out; |
| 567 |
if (rts->opt_flood) { |
| 568 |
write_stdout("\bE", 2); |
| 569 |
} else { |
| 570 |
print_timestamp(rts); |
| 571 |
printf(_("From %s icmp_seq=%u "), pr_addr(rts, sin6, sizeof *sin6), ntohs(icmph.icmp6_seq)); |
| 572 |
print_icmp(e->ee_type, e->ee_code, e->ee_info); |
| 573 |
putchar('\n'); |
| 574 |
fflush(stdout); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
out: |
| 579 |
errno = saved_errno; |
| 580 |
return net_errors ? net_errors : -local_errors; |
| 581 |
} |
| 582 |
|
| 583 |
/* |
| 584 |
* pinger -- |
| 585 |
* Compose and transmit an ICMP ECHO REQUEST packet. The IP packet |
| 586 |
* will be added on by the kernel. The ID field is our UNIX process ID, |
| 587 |
* and the sequence number is an ascending integer. The first several bytes |
| 588 |
* of the data portion are used to hold a UNIX "timeval" struct in VAX |
| 589 |
* byte-order, to compute the round-trip time. |
| 590 |
*/ |
| 591 |
int build_echo(struct ping_rts *rts, uint8_t *_icmph, |
| 592 |
unsigned packet_size __attribute__((__unused__))) |
| 593 |
{ |
| 594 |
struct icmp6_hdr *icmph; |
| 595 |
int cc; |
| 596 |
|
| 597 |
icmph = (struct icmp6_hdr *)_icmph; |
| 598 |
icmph->icmp6_type = ICMP6_ECHO_REQUEST; |
| 599 |
icmph->icmp6_code = 0; |
| 600 |
icmph->icmp6_cksum = 0; |
| 601 |
icmph->icmp6_seq = htons(rts->ntransmitted + 1); |
| 602 |
icmph->icmp6_id = rts->ident; |
| 603 |
|
| 604 |
if (rts->timing) |
| 605 |
gettimeofday((struct timeval *)&_icmph[8], NULL); |
| 606 |
|
| 607 |
cc = rts->datalen + 8; /* skips ICMP portion */ |
| 608 |
|
| 609 |
return cc; |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
int build_niquery(struct ping_rts *rts, uint8_t *_nih, |
| 614 |
unsigned packet_size __attribute__((__unused__))) |
| 615 |
{ |
| 616 |
struct ni_hdr *nih; |
| 617 |
int cc; |
| 618 |
|
| 619 |
nih = (struct ni_hdr *)_nih; |
| 620 |
nih->ni_cksum = 0; |
| 621 |
|
| 622 |
nih->ni_type = IPUTILS_NI_ICMP6_QUERY; |
| 623 |
cc = sizeof(*nih); |
| 624 |
rts->datalen = 0; |
| 625 |
|
| 626 |
niquery_fill_nonce(&rts->ni, rts->ntransmitted + 1, nih->ni_nonce); |
| 627 |
nih->ni_code = rts->ni.subject_type; |
| 628 |
nih->ni_qtype = htons(rts->ni.query); |
| 629 |
nih->ni_flags = rts->ni.flag; |
| 630 |
memcpy(nih + 1, rts->ni.subject, rts->ni.subject_len); |
| 631 |
cc += rts->ni.subject_len; |
| 632 |
|
| 633 |
return cc; |
| 634 |
} |
| 635 |
|
| 636 |
int ping6_send_probe(struct ping_rts *rts, socket_st *sock, void *packet, unsigned packet_size) |
| 637 |
{ |
| 638 |
int len, cc; |
| 639 |
|
| 640 |
rcvd_clear(rts, rts->ntransmitted + 1); |
| 641 |
|
| 642 |
if (niquery_is_enabled(&rts->ni)) |
| 643 |
len = build_niquery(rts, packet, packet_size); |
| 644 |
else |
| 645 |
len = build_echo(rts, packet, packet_size); |
| 646 |
|
| 647 |
if (rts->cmsglen == 0) { |
| 648 |
cc = sendto(sock->fd, (char *)packet, len, rts->confirm, |
| 649 |
(struct sockaddr *)&rts->whereto6, |
| 650 |
sizeof(struct sockaddr_in6)); |
| 651 |
} else { |
| 652 |
struct msghdr mhdr; |
| 653 |
struct iovec iov; |
| 654 |
|
| 655 |
iov.iov_len = len; |
| 656 |
iov.iov_base = packet; |
| 657 |
|
| 658 |
memset(&mhdr, 0, sizeof(mhdr)); |
| 659 |
mhdr.msg_name = &rts->whereto6; |
| 660 |
mhdr.msg_namelen = sizeof(struct sockaddr_in6); |
| 661 |
mhdr.msg_iov = &iov; |
| 662 |
mhdr.msg_iovlen = 1; |
| 663 |
mhdr.msg_control = rts->cmsgbuf; |
| 664 |
mhdr.msg_controllen = rts->cmsglen; |
| 665 |
|
| 666 |
cc = sendmsg(sock->fd, &mhdr, rts->confirm); |
| 667 |
} |
| 668 |
rts->confirm = 0; |
| 669 |
|
| 670 |
return (cc == len ? 0 : cc); |
| 671 |
} |
| 672 |
|
| 673 |
void pr_echo_reply(uint8_t *_icmph, int cc __attribute__((__unused__))) |
| 674 |
{ |
| 675 |
struct icmp6_hdr *icmph = (struct icmp6_hdr *)_icmph; |
| 676 |
|
| 677 |
printf(_(" icmp_seq=%u"), ntohs(icmph->icmp6_seq)); |
| 678 |
} |
| 679 |
|
| 680 |
static void putchar_safe(char c) |
| 681 |
{ |
| 682 |
if (isprint(c)) |
| 683 |
putchar(c); |
| 684 |
else |
| 685 |
printf("\\%03o", c); |
| 686 |
} |
| 687 |
|
| 688 |
static |
| 689 |
void pr_niquery_reply_name(struct ni_hdr *nih, int len) |
| 690 |
{ |
| 691 |
uint8_t *h = (uint8_t *)(nih + 1); |
| 692 |
uint8_t *p = h + 4; |
| 693 |
uint8_t *end = (uint8_t *)nih + len; |
| 694 |
int continued = 0; |
| 695 |
char buf[1024]; |
| 696 |
int ret; |
| 697 |
|
| 698 |
len -= sizeof(struct ni_hdr) + 4; |
| 699 |
|
| 700 |
if (len < 0) { |
| 701 |
printf(_(" parse error (too short)")); |
| 702 |
return; |
| 703 |
} |
| 704 |
while (p < end) { |
| 705 |
int fqdn = 1; |
| 706 |
size_t i; |
| 707 |
|
| 708 |
memset(buf, 0xff, sizeof(buf)); |
| 709 |
|
| 710 |
if (continued) |
| 711 |
putchar(','); |
| 712 |
|
| 713 |
ret = dn_expand(h, end, p, buf, sizeof(buf)); |
| 714 |
if (ret < 0) { |
| 715 |
printf(_(" parse error (truncated)")); |
| 716 |
break; |
| 717 |
} |
| 718 |
if (p + ret < end && *(p + ret) == '\0') |
| 719 |
fqdn = 0; |
| 720 |
|
| 721 |
putchar(' '); |
| 722 |
for (i = 0; i < strlen(buf); i++) |
| 723 |
putchar_safe(buf[i]); |
| 724 |
if (fqdn) |
| 725 |
putchar('.'); |
| 726 |
|
| 727 |
p += ret + !fqdn; |
| 728 |
|
| 729 |
continued = 1; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
static |
| 734 |
void pr_niquery_reply_addr(struct ni_hdr *nih, int len) |
| 735 |
{ |
| 736 |
uint8_t *h = (uint8_t *)(nih + 1); |
| 737 |
uint8_t *p; |
| 738 |
uint8_t *end = (uint8_t *)nih + len; |
| 739 |
int af; |
| 740 |
int aflen; |
| 741 |
int continued = 0; |
| 742 |
int truncated; |
| 743 |
char buf[1024]; |
| 744 |
|
| 745 |
switch (ntohs(nih->ni_qtype)) { |
| 746 |
case IPUTILS_NI_QTYPE_IPV4ADDR: |
| 747 |
af = AF_INET; |
| 748 |
aflen = sizeof(struct in_addr); |
| 749 |
truncated = nih->ni_flags & IPUTILS_NI_IPV6_FLAG_TRUNCATE; |
| 750 |
break; |
| 751 |
case IPUTILS_NI_QTYPE_IPV6ADDR: |
| 752 |
af = AF_INET6; |
| 753 |
aflen = sizeof(struct in6_addr); |
| 754 |
truncated = nih->ni_flags & IPUTILS_NI_IPV4_FLAG_TRUNCATE; |
| 755 |
break; |
| 756 |
default: |
| 757 |
/* should not happen */ |
| 758 |
af = aflen = truncated = 0; |
| 759 |
} |
| 760 |
p = h; |
| 761 |
if (len < 0) { |
| 762 |
printf(_(" parse error (too short)")); |
| 763 |
return; |
| 764 |
} |
| 765 |
|
| 766 |
while (p < end) { |
| 767 |
if (continued) |
| 768 |
putchar(','); |
| 769 |
|
| 770 |
if (p + sizeof(uint32_t) + aflen > end) { |
| 771 |
printf(_(" parse error (truncated)")); |
| 772 |
break; |
| 773 |
} |
| 774 |
if (!inet_ntop(af, p + sizeof(uint32_t), buf, sizeof(buf))) |
| 775 |
printf(_(" unexpected error in inet_ntop(%s)"), |
| 776 |
strerror(errno)); |
| 777 |
else |
| 778 |
printf(" %s", buf); |
| 779 |
p += sizeof(uint32_t) + aflen; |
| 780 |
|
| 781 |
continued = 1; |
| 782 |
} |
| 783 |
if (truncated) |
| 784 |
printf(_(" (truncated)")); |
| 785 |
} |
| 786 |
|
| 787 |
static |
| 788 |
void pr_niquery_reply(uint8_t *_nih, int len) |
| 789 |
{ |
| 790 |
struct ni_hdr *nih = (struct ni_hdr *)_nih; |
| 791 |
|
| 792 |
switch (nih->ni_code) { |
| 793 |
case IPUTILS_NI_ICMP6_SUCCESS: |
| 794 |
switch (ntohs(nih->ni_qtype)) { |
| 795 |
case IPUTILS_NI_QTYPE_DNSNAME: |
| 796 |
pr_niquery_reply_name(nih, len); |
| 797 |
break; |
| 798 |
case IPUTILS_NI_QTYPE_IPV4ADDR: |
| 799 |
case IPUTILS_NI_QTYPE_IPV6ADDR: |
| 800 |
pr_niquery_reply_addr(nih, len); |
| 801 |
break; |
| 802 |
default: |
| 803 |
printf(_(" unknown qtype(0x%02x)"), ntohs(nih->ni_qtype)); |
| 804 |
} |
| 805 |
break; |
| 806 |
case IPUTILS_NI_ICMP6_REFUSED: |
| 807 |
printf(_(" refused")); |
| 808 |
break; |
| 809 |
case IPUTILS_NI_ICMP6_UNKNOWN: |
| 810 |
printf(_(" unknown")); |
| 811 |
break; |
| 812 |
default: |
| 813 |
printf(_(" unknown code(%02x)"), ntohs(nih->ni_code)); |
| 814 |
} |
| 815 |
printf(_("; seq=%u;"), ntohsp((uint16_t *)nih->ni_nonce)); |
| 816 |
} |
| 817 |
|
| 818 |
/* |
| 819 |
* parse_reply -- |
| 820 |
* Print out the packet, if it came from us. This logic is necessary |
| 821 |
* because ALL readers of the ICMP socket get a copy of ALL ICMP packets |
| 822 |
* which arrive ('tis only fair). This permits multiple copies of this |
| 823 |
* program to be run without having intermingled output (or statistics!). |
| 824 |
*/ |
| 825 |
int ping6_parse_reply(struct ping_rts *rts, socket_st *sock, |
| 826 |
struct msghdr *msg, int cc, void *addr, |
| 827 |
struct timeval *tv) |
| 828 |
{ |
| 829 |
struct sockaddr_in6 *from = addr; |
| 830 |
uint8_t *buf = msg->msg_iov->iov_base; |
| 831 |
struct cmsghdr *c; |
| 832 |
struct icmp6_hdr *icmph; |
| 833 |
int hops = -1; |
| 834 |
int wrong_source = 0; |
| 835 |
|
| 836 |
for (c = CMSG_FIRSTHDR(msg); c; c = CMSG_NXTHDR(msg, c)) { |
| 837 |
if (c->cmsg_level != IPPROTO_IPV6) |
| 838 |
continue; |
| 839 |
switch (c->cmsg_type) { |
| 840 |
case IPV6_HOPLIMIT: |
| 841 |
#ifdef IPV6_2292HOPLIMIT |
| 842 |
case IPV6_2292HOPLIMIT: |
| 843 |
#endif |
| 844 |
if (c->cmsg_len < CMSG_LEN(sizeof(int))) |
| 845 |
continue; |
| 846 |
memcpy(&hops, CMSG_DATA(c), sizeof(hops)); |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
|
| 851 |
/* Now the ICMP part */ |
| 852 |
|
| 853 |
icmph = (struct icmp6_hdr *)buf; |
| 854 |
if (cc < 8) { |
| 855 |
if (rts->opt_verbose) |
| 856 |
error(0, 0, _("packet too short: %d bytes"), cc); |
| 857 |
return 1; |
| 858 |
} |
| 859 |
|
| 860 |
if (icmph->icmp6_type == ICMP6_ECHO_REPLY) { |
| 861 |
if (!is_ours(rts, sock, icmph->icmp6_id)) |
| 862 |
return 1; |
| 863 |
|
| 864 |
if (!rts->multicast && !rts->subnet_router_anycast && |
| 865 |
memcmp(&from->sin6_addr.s6_addr, &rts->whereto6.sin6_addr.s6_addr, 16)) |
| 866 |
wrong_source = 1; |
| 867 |
|
| 868 |
if (gather_statistics(rts, (uint8_t *)icmph, sizeof(*icmph), cc, |
| 869 |
ntohs(icmph->icmp6_seq), |
| 870 |
hops, 0, tv, pr_addr(rts, from, sizeof *from), |
| 871 |
pr_echo_reply, |
| 872 |
rts->multicast, wrong_source)) { |
| 873 |
fflush(stdout); |
| 874 |
return 0; |
| 875 |
} |
| 876 |
} else if (icmph->icmp6_type == IPUTILS_NI_ICMP6_REPLY) { |
| 877 |
struct ni_hdr *nih = (struct ni_hdr *)icmph; |
| 878 |
int seq = niquery_check_nonce(&rts->ni, nih->ni_nonce); |
| 879 |
if (seq < 0) |
| 880 |
return 1; |
| 881 |
if (gather_statistics(rts, (uint8_t *)icmph, sizeof(*icmph), cc, |
| 882 |
seq, |
| 883 |
hops, 0, tv, pr_addr(rts, from, sizeof *from), |
| 884 |
pr_niquery_reply, |
| 885 |
rts->multicast, 0)) |
| 886 |
return 0; |
| 887 |
} else { |
| 888 |
int nexthdr; |
| 889 |
struct ip6_hdr *iph1 = (struct ip6_hdr *)(icmph + 1); |
| 890 |
struct icmp6_hdr *icmph1 = (struct icmp6_hdr *)(iph1 + 1); |
| 891 |
|
| 892 |
/* We must not ever fall here. All the messages but |
| 893 |
* echo reply are blocked by filter and error are |
| 894 |
* received with IPV6_RECVERR. Ugly code is preserved |
| 895 |
* however, just to remember what crap we avoided |
| 896 |
* using RECVRERR. :-) |
| 897 |
*/ |
| 898 |
|
| 899 |
if (cc < (int)(8 + sizeof(struct ip6_hdr) + 8)) |
| 900 |
return 1; |
| 901 |
|
| 902 |
if (memcmp(&iph1->ip6_dst, &rts->whereto6.sin6_addr, 16)) |
| 903 |
return 1; |
| 904 |
|
| 905 |
nexthdr = iph1->ip6_nxt; |
| 906 |
|
| 907 |
if (nexthdr == NEXTHDR_FRAGMENT) { |
| 908 |
nexthdr = *(uint8_t *)icmph1; |
| 909 |
icmph1++; |
| 910 |
} |
| 911 |
if (nexthdr == IPPROTO_ICMPV6) { |
| 912 |
if (icmph1->icmp6_type != ICMP6_ECHO_REQUEST || |
| 913 |
!is_ours(rts, sock, icmph1->icmp6_id)) |
| 914 |
return 1; |
| 915 |
acknowledge(rts, ntohs(icmph1->icmp6_seq)); |
| 916 |
return 0; |
| 917 |
} |
| 918 |
|
| 919 |
/* We've got something other than an ECHOREPLY */ |
| 920 |
if (!rts->opt_verbose || rts->uid) |
| 921 |
return 1; |
| 922 |
print_timestamp(rts); |
| 923 |
printf(_("From %s: "), pr_addr(rts, from, sizeof *from)); |
| 924 |
print_icmp(icmph->icmp6_type, icmph->icmp6_code, ntohl(icmph->icmp6_mtu)); |
| 925 |
} |
| 926 |
|
| 927 |
if (rts->opt_audible) { |
| 928 |
putchar('\a'); |
| 929 |
if (rts->opt_flood) |
| 930 |
fflush(stdout); |
| 931 |
} |
| 932 |
if (!rts->opt_flood) { |
| 933 |
putchar('\n'); |
| 934 |
fflush(stdout); |
| 935 |
} |
| 936 |
return 0; |
| 937 |
} |
| 938 |
|
| 939 |
void ping6_install_filter(struct ping_rts *rts, socket_st *sock) |
| 940 |
{ |
| 941 |
static int once; |
| 942 |
static struct sock_filter insns[] = { |
| 943 |
BPF_STMT(BPF_LD | BPF_H | BPF_ABS, 4), /* Load icmp echo ident */ |
| 944 |
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0xAAAA, 0, 1), /* Ours? */ |
| 945 |
BPF_STMT(BPF_RET | BPF_K, ~0U), /* Yes, it passes. */ |
| 946 |
BPF_STMT(BPF_LD | BPF_B | BPF_ABS, 0), /* Load icmp type */ |
| 947 |
BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, ICMP6_ECHO_REPLY, 1, 0), /* Echo? */ |
| 948 |
BPF_STMT(BPF_RET | BPF_K, ~0U), /* No. It passes. This must not happen. */ |
| 949 |
BPF_STMT(BPF_RET | BPF_K, 0), /* Echo with wrong ident. Reject. */ |
| 950 |
}; |
| 951 |
static struct sock_fprog filter = { |
| 952 |
sizeof insns / sizeof(insns[0]), |
| 953 |
insns |
| 954 |
}; |
| 955 |
|
| 956 |
if (once) |
| 957 |
return; |
| 958 |
once = 1; |
| 959 |
|
| 960 |
/* Patch bpflet for current identifier. */ |
| 961 |
insns[1] = (struct sock_filter)BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, htons(rts->ident), 0, 1); |
| 962 |
|
| 963 |
if (setsockopt(sock->fd, SOL_SOCKET, SO_ATTACH_FILTER, &filter, sizeof(filter))) |
| 964 |
error(0, errno, _("WARNING: failed to install socket filter")); |
| 965 |
} |