Bug Summary

File:src/sbin/ping/ping.c
Warning:line 783, column 21
2nd function call argument is an uninitialized value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ping.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/sbin/ping/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/sbin/ping/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/sbin/ping/ping.c
1/* $OpenBSD: ping.c,v 1.245 2021/07/12 15:09:19 beck Exp $ */
2
3/*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 1989, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * Mike Muuss.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 */
63
64/*
65 * Using the InterNet Control Message Protocol (ICMP) "ECHO" facility,
66 * measure round-trip-delays and packet loss across network paths.
67 *
68 * Author -
69 * Mike Muuss
70 * U. S. Army Ballistic Research Laboratory
71 * December, 1983
72 *
73 * Status -
74 * Public Domain. Distribution Unlimited.
75 * Bugs -
76 * More statistics could always be gathered.
77 * This program has to run SUID to ROOT to access the ICMP socket.
78 */
79
80#include <sys/types.h>
81#include <sys/socket.h>
82#include <sys/time.h>
83#include <sys/uio.h>
84
85#include <netinet/in.h>
86#include <netinet/ip.h>
87#include <netinet/ip_icmp.h>
88#include <netinet/ip_var.h>
89#include <netinet/ip6.h>
90#include <netinet/icmp6.h>
91#include <netinet/ip_ah.h>
92#include <arpa/inet.h>
93#include <netdb.h>
94
95#include <ctype.h>
96#include <err.h>
97#include <errno(*__errno()).h>
98#include <limits.h>
99#include <math.h>
100#include <poll.h>
101#include <pwd.h>
102#include <signal.h>
103#include <siphash.h>
104#include <stdint.h>
105#include <stdio.h>
106#include <stdlib.h>
107#include <string.h>
108#include <time.h>
109#include <unistd.h>
110
111struct tv64 {
112 u_int64_t tv64_sec;
113 u_int64_t tv64_nsec;
114};
115
116struct payload {
117 struct tv64 tv64;
118 u_int8_t mac[SIPHASH_DIGEST_LENGTH8];
119};
120
121#define ECHOLEN8 8 /* icmp echo header len excluding time */
122#define ECHOTMLENsizeof(struct payload) sizeof(struct payload)
123#define DEFDATALEN(64 - 8) (64 - ECHOLEN8) /* default data length */
124#define MAXIPLEN60 60
125#define MAXICMPLEN76 76
126#define MAXPAYLOAD(65535 - 60 - 8) (IP_MAXPACKET65535 - MAXIPLEN60 - ECHOLEN8)
127#define IP6LEN40 40
128#define EXTRA256 256 /* for AH and various other headers. weird. */
129#define MAXPAYLOAD665535 - 40 - 8 IPV6_MAXPACKET65535 - IP6LEN40 - ECHOLEN8
130#define MAXWAIT_DEFAULT10 10 /* secs to wait for response */
131#define NROUTES9 9 /* number of record route slots */
132
133#define A(bit)rcvd_tbl[(bit)>>3] rcvd_tbl[(bit)>>3] /* identify byte in array */
134#define B(bit)(1 << ((bit) & 0x07)) (1 << ((bit) & 0x07)) /* identify bit in byte */
135#define SET(bit)(rcvd_tbl[(bit)>>3] |= (1 << ((bit) & 0x07))) (A(bit)rcvd_tbl[(bit)>>3] |= B(bit)(1 << ((bit) & 0x07)))
136#define CLR(bit)(rcvd_tbl[(bit)>>3] &= (~(1 << ((bit) & 0x07
))))
(A(bit)rcvd_tbl[(bit)>>3] &= (~B(bit)(1 << ((bit) & 0x07))))
137#define TST(bit)(rcvd_tbl[(bit)>>3] & (1 << ((bit) & 0x07
)))
(A(bit)rcvd_tbl[(bit)>>3] & B(bit)(1 << ((bit) & 0x07)))
138
139/* various options */
140int options;
141#define F_FLOOD0x0001 0x0001
142#define F_INTERVAL0x0002 0x0002
143#define F_HOSTNAME0x0004 0x0004
144#define F_PINGFILLED0x0008 0x0008
145#define F_QUIET0x0010 0x0010
146#define F_RROUTE0x0020 0x0020
147#define F_SO_DEBUG0x0040 0x0040
148#define F_SHOWCHAR0x0080 0x0080
149#define F_VERBOSE0x0100 0x0100
150/* 0x0200 */
151#define F_HDRINCL0x0400 0x0400
152#define F_TTL0x0800 0x0800
153#define F_TOS0x1000 0x1000
154#define F_AUD_RECV0x2000 0x2000
155#define F_AUD_MISS0x4000 0x4000
156
157/* multicast options */
158int moptions;
159#define MULTICAST_NOLOOP0x001 0x001
160#define MULTICAST_TTL0x002 0x002
161
162#define DUMMY_PORT10101 10101
163#define PING_USER"_ping" "_ping"
164
165/*
166 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
167 * number of received sequence numbers we can keep track of. Change 128
168 * to 8192 for complete accuracy...
169 */
170#define MAX_DUP_CHK(8 * 8192) (8 * 8192)
171int mx_dup_ck = MAX_DUP_CHK(8 * 8192);
172char rcvd_tbl[MAX_DUP_CHK(8 * 8192) / 8];
173
174int datalen = DEFDATALEN(64 - 8);
175int maxpayload = MAXPAYLOAD(65535 - 60 - 8);
176u_char outpackhdr[IP_MAXPACKET65535+sizeof(struct ip)];
177u_char *outpack = outpackhdr+sizeof(struct ip);
178char BSPACE = '\b'; /* characters written for flood */
179char DOT = '.';
180char *hostname;
181int ident; /* random number to identify our packets */
182int v6flag; /* are we ping6? */
183
184/* counters */
185int64_t npackets; /* max packets to transmit */
186int64_t nreceived; /* # of packets we got back */
187int64_t nrepeats; /* number of duplicates */
188int64_t ntransmitted; /* sequence # for outbound packets = #sent */
189int64_t nmissedmax = 1; /* max value of ntransmitted - nreceived - 1 */
190struct timeval interval = {1, 0}; /* interval between packets */
191
192/* timing */
193int timing; /* flag to do timing */
194int timinginfo;
195unsigned int maxwait = MAXWAIT_DEFAULT10; /* max seconds to wait for response */
196double tmin = 999999999.0; /* minimum round trip time */
197double tmax; /* maximum round trip time */
198double tsum; /* sum of all times, for doing average */
199double tsumsq; /* sum of all times squared, for std. dev. */
200
201struct tv64 tv64_offset;
202SIPHASH_KEY mac_key;
203
204struct msghdr smsghdr;
205struct iovec smsgiov;
206
207volatile sig_atomic_t seenalrm;
208volatile sig_atomic_t seenint;
209volatile sig_atomic_t seeninfo;
210
211void fill(char *, char *);
212void summary(void);
213void onsignal(int);
214void retransmit(int);
215int pinger(int);
216const char *pr_addr(struct sockaddr *, socklen_t);
217void pr_pack(u_char *, int, struct msghdr *);
218__dead__attribute__((__noreturn__)) void usage(void);
219
220/* IPv4 specific functions */
221void pr_ipopt(int, u_char *);
222int in_cksum(u_short *, int);
223void pr_icmph(struct icmp *);
224void pr_retip(struct ip *);
225void pr_iph(struct ip *);
226#ifndef SMALL
227int map_tos(char *, int *);
228#endif /* SMALL */
229
230/* IPv6 specific functions */
231int get_hoplim(struct msghdr *);
232int get_pathmtu(struct msghdr *, struct sockaddr_in6 *);
233void pr_icmph6(struct icmp6_hdr *, u_char *);
234void pr_iph6(struct ip6_hdr *);
235void pr_exthdrs(struct msghdr *);
236void pr_ip6opt(void *);
237void pr_rthdr(void *);
238void pr_retip6(struct ip6_hdr *, u_char *);
239
240int
241main(int argc, char *argv[])
242{
243 struct addrinfo hints, *res;
244 struct itimerval itimer;
245 struct sockaddr *from, *dst;
246 struct sockaddr_in from4, dst4;
247 struct sockaddr_in6 from6, dst6;
248 struct cmsghdr *scmsg = NULL((void *)0);
249 struct in6_pktinfo *pktinfo = NULL((void *)0);
250 struct icmp6_filter filt;
251 struct passwd *pw;
252 socklen_t maxsizelen;
253 int64_t preload;
254 int ch, i, optval = 1, packlen, maxsize, error, s, flooddone = 0;
255 int df = 0, tos = 0, bufspace = IP_MAXPACKET65535, hoplimit = -1, mflag = 0;
256 u_char *datap, *packet;
257 u_char ttl = MAXTTL255;
258 char *e, *target, hbuf[NI_MAXHOST256], *source = NULL((void *)0);
259 char rspace[3 + 4 * NROUTES9 + 1]; /* record route space */
260 const char *errstr;
261 double fraction, integral, seconds;
262 uid_t ouid, uid;
263 gid_t gid;
264 u_int rtableid = 0;
265 extern char *__progname;
266
267 /* Cannot pledge due to special setsockopt()s below */
268 if (unveil("/", "r") == -1)
1
Assuming the condition is false
2
Taking false branch
269 err(1, "unveil /");
270 if (unveil(NULL((void *)0), NULL((void *)0)) == -1)
3
Assuming the condition is false
4
Taking false branch
271 err(1, "unveil");
272
273 if (strcmp("ping6", __progname) == 0) {
5
Assuming the condition is false
6
Taking false branch
274 v6flag = 1;
275 maxpayload = MAXPAYLOAD665535 - 40 - 8;
276 if ((s = socket(AF_INET624, SOCK_RAW3, IPPROTO_ICMPV658)) == -1)
277 err(1, "socket");
278 } else {
279 if ((s = socket(AF_INET2, SOCK_RAW3, IPPROTO_ICMP1)) == -1)
7
Assuming the condition is false
8
Taking false branch
280 err(1, "socket");
281 }
282
283 /* revoke privs */
284 ouid = getuid();
285 if (ouid == 0 && (pw = getpwnam(PING_USER"_ping")) != NULL((void *)0)) {
9
Assuming 'ouid' is not equal to 0
286 uid = pw->pw_uid;
287 gid = pw->pw_gid;
288 } else {
289 uid = getuid();
290 gid = getgid();
291 }
292 if (ouid
9.1
'ouid' is not equal to 0
&& (setgroups(1, &gid) ||
10
Assuming the condition is false
13
Taking false branch
293 setresgid(gid, gid, gid) ||
11
Assuming the condition is false
294 setresuid(uid, uid, uid)))
12
Assuming the condition is false
295 err(1, "unable to revoke privs");
296
297 preload = 0;
298 datap = &outpack[ECHOLEN8 + ECHOTMLENsizeof(struct payload)];
299 while ((ch = getopt(argc, argv, v6flag ?
14
Assuming 'v6flag' is 0
15
'?' condition is false
16
Assuming the condition is false
17
Loop condition is false. Execution continues on line 440
300 "c:DdEefgHh:I:i:Ll:mNnp:qS:s:T:V:vw:" :
301 "DEI:LRS:c:defgHi:l:np:qs:T:t:V:vw:")) != -1) {
302 switch(ch) {
303 case 'c':
304 npackets = strtonum(optarg, 0, INT64_MAX0x7fffffffffffffffLL, &errstr);
305 if (errstr)
306 errx(1,
307 "number of packets to transmit is %s: %s",
308 errstr, optarg);
309 break;
310 case 'D':
311 options |= F_HDRINCL0x0400;
312 df = 1;
313 break;
314 case 'd':
315 options |= F_SO_DEBUG0x0040;
316 break;
317 case 'E':
318 options |= F_AUD_MISS0x4000;
319 break;
320 case 'e':
321 options |= F_AUD_RECV0x2000;
322 break;
323 case 'f':
324 if (ouid)
325 errc(1, EPERM1, NULL((void *)0));
326 options |= F_FLOOD0x0001;
327 setvbuf(stdout(&__sF[1]), NULL((void *)0), _IONBF2, 0);
328 break;
329 case 'g':
330 options |= F_SHOWCHAR0x0080;
331 break;
332 case 'H':
333 options |= F_HOSTNAME0x0004;
334 break;
335 case 'h': /* hoplimit */
336 hoplimit = strtonum(optarg, 0, IPV6_MAXHLIM255, &errstr);
337 if (errstr)
338 errx(1, "hoplimit is %s: %s", errstr, optarg);
339 break;
340 case 'I':
341 case 'S': /* deprecated */
342 source = optarg;
343 break;
344 case 'i': /* interval between packets */
345 seconds = strtod(optarg, &e);
346 if (*optarg == '\0' || *e != '\0' || seconds < 0.0)
347 errx(1, "interval is invalid: %s", optarg);
348 fraction = modf(seconds, &integral);
349 if (integral > UINT_MAX(2147483647 *2U +1U))
350 errx(1, "interval is too large: %s", optarg);
351 interval.tv_sec = integral;
352 interval.tv_usec = fraction * 1000000.0;
353 if (!timerisset(&interval)((&interval)->tv_sec || (&interval)->tv_usec))
354 errx(1, "interval is too small: %s", optarg);
355 if (interval.tv_sec < 1 && ouid != 0) {
356 errx(1, "only root may use an interval smaller"
357 " than one second");
358 }
359 options |= F_INTERVAL0x0002;
360 break;
361 case 'L':
362 moptions |= MULTICAST_NOLOOP0x001;
363 break;
364 case 'l':
365 if (ouid)
366 errc(1, EPERM1, NULL((void *)0));
367 preload = strtonum(optarg, 1, INT64_MAX0x7fffffffffffffffLL, &errstr);
368 if (errstr)
369 errx(1, "preload value is %s: %s", errstr,
370 optarg);
371 break;
372 case 'm':
373 mflag++;
374 break;
375 case 'n':
376 options &= ~F_HOSTNAME0x0004;
377 break;
378 case 'p': /* fill buffer with user pattern */
379 options |= F_PINGFILLED0x0008;
380 fill((char *)datap, optarg);
381 break;
382 case 'q':
383 options |= F_QUIET0x0010;
384 break;
385 case 'R':
386 options |= F_RROUTE0x0020;
387 break;
388 case 's': /* size of packet to send */
389 datalen = strtonum(optarg, 0, maxpayload, &errstr);
390 if (errstr)
391 errx(1, "packet size is %s: %s", errstr,
392 optarg);
393 break;
394#ifndef SMALL
395 case 'T':
396 options |= F_HDRINCL0x0400;
397 options |= F_TOS0x1000;
398 errno(*__errno()) = 0;
399 errstr = NULL((void *)0);
400 if (map_tos(optarg, &tos))
401 break;
402 if (strlen(optarg) > 1 && optarg[0] == '0' &&
403 optarg[1] == 'x')
404 tos = (int)strtol(optarg, NULL((void *)0), 16);
405 else
406 tos = strtonum(optarg, 0, 255, &errstr);
407 if (tos < 0 || tos > 255 || errstr || errno(*__errno()))
408 errx(1, "illegal tos value %s", optarg);
409 break;
410#endif /* SMALL */
411 case 't':
412 options |= F_TTL0x0800;
413 ttl = strtonum(optarg, 0, MAXTTL255, &errstr);
414 if (errstr)
415 errx(1, "ttl value is %s: %s", errstr, optarg);
416 break;
417 case 'V':
418 rtableid = strtonum(optarg, 0, RT_TABLEID_MAX255, &errstr);
419 if (errstr)
420 errx(1, "rtable value is %s: %s", errstr,
421 optarg);
422 if (setsockopt(s, SOL_SOCKET0xffff, SO_RTABLE0x1021, &rtableid,
423 sizeof(rtableid)) == -1)
424 err(1, "setsockopt SO_RTABLE");
425 break;
426 case 'v':
427 options |= F_VERBOSE0x0100;
428 break;
429 case 'w':
430 maxwait = strtonum(optarg, 1, INT_MAX2147483647, &errstr);
431 if (errstr)
432 errx(1, "maxwait value is %s: %s",
433 errstr, optarg);
434 break;
435 default:
436 usage();
437 }
438 }
439
440 if (ouid
17.1
'ouid' is not equal to 0
== 0 && (setgroups(1, &gid) ||
441 setresgid(gid, gid, gid) ||
442 setresuid(uid, uid, uid)))
443 err(1, "unable to revoke privs");
444
445 argc -= optind;
446 argv += optind;
447
448 if (argc != 1)
18
Assuming 'argc' is equal to 1
19
Taking false branch
449 usage();
450
451 memset(&dst4, 0, sizeof(dst4));
452 memset(&dst6, 0, sizeof(dst6));
453
454 target = *argv;
455
456 memset(&hints, 0, sizeof(hints));
457 hints.ai_family = v6flag
19.1
'v6flag' is 0
? AF_INET624 : AF_INET2;
20
'?' condition is false
458 hints.ai_socktype = SOCK_RAW3;
459 hints.ai_protocol = 0;
460 hints.ai_flags = AI_CANONNAME2;
461 if ((error = getaddrinfo(target, NULL((void *)0), &hints, &res)))
21
Assuming 'error' is 0
22
Taking false branch
462 errx(1, "%s", gai_strerror(error));
463
464 switch (res->ai_family) {
23
Control jumps to 'case 24:' at line 469
465 case AF_INET2:
466 dst = (struct sockaddr *)&dst4;
467 from = (struct sockaddr *)&from4;
468 break;
469 case AF_INET624:
470 dst = (struct sockaddr *)&dst6;
471 from = (struct sockaddr *)&from6;
472 break;
24
Execution continues on line 478
473 default:
474 errx(1, "unsupported AF: %d", res->ai_family);
475 break;
476 }
477
478 memcpy(dst, res->ai_addr, res->ai_addrlen);
479
480 if (!hostname) {
25
Assuming 'hostname' is non-null
26
Taking false branch
481 hostname = res->ai_canonname ? strdup(res->ai_canonname) :
482 target;
483 if (!hostname)
484 err(1, "malloc");
485 }
486
487 if (res->ai_next) {
27
Assuming field 'ai_next' is null
28
Taking false branch
488 if (getnameinfo(res->ai_addr, res->ai_addrlen, hbuf,
489 sizeof(hbuf), NULL((void *)0), 0, NI_NUMERICHOST1) != 0)
490 strlcpy(hbuf, "?", sizeof(hbuf));
491 warnx("Warning: %s has multiple "
492 "addresses; using %s", hostname, hbuf);
493 }
494 freeaddrinfo(res);
495
496 if (source
28.1
'source' is null
) {
29
Taking false branch
497 memset(&hints, 0, sizeof(hints));
498 hints.ai_family = dst->sa_family;
499 if ((error = getaddrinfo(source, NULL((void *)0), &hints, &res)))
500 errx(1, "%s: %s", source, gai_strerror(error));
501 memcpy(from, res->ai_addr, res->ai_addrlen);
502 freeaddrinfo(res);
503
504 if (!v6flag && IN_MULTICAST(ntohl(dst4.sin_addr.s_addr))(((u_int32_t)((__uint32_t)(__builtin_constant_p(dst4.sin_addr
.s_addr) ? (__uint32_t)(((__uint32_t)(dst4.sin_addr.s_addr) &
0xff) << 24 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff00) << 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff0000) >> 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff000000) >> 24) : __swap32md(dst4.sin_addr.s_addr))
) & ((u_int32_t)(0xf0000000))) == ((u_int32_t)(0xe0000000
)))
) {
505 if (setsockopt(s, IPPROTO_IP0, IP_MULTICAST_IF9,
506 &from4.sin_addr, sizeof(from4.sin_addr)) == -1)
507 err(1, "setsockopt IP_MULTICAST_IF");
508 } else {
509 if (bind(s, from, from->sa_len) == -1)
510 err(1, "bind");
511 }
512 } else if (options & F_VERBOSE0x0100) {
30
Assuming the condition is false
31
Taking false branch
513 /*
514 * get the source address. XXX since we revoked the root
515 * privilege, we cannot use a raw socket for this.
516 */
517 int dummy;
518 socklen_t len = dst->sa_len;
519
520 if ((dummy = socket(dst->sa_family, SOCK_DGRAM2, 0)) == -1)
521 err(1, "UDP socket");
522
523 memcpy(from, dst, dst->sa_len);
524 if (v6flag) {
525 from6.sin6_port = ntohs(DUMMY_PORT)(__uint16_t)(__builtin_constant_p(10101) ? (__uint16_t)(((__uint16_t
)(10101) & 0xffU) << 8 | ((__uint16_t)(10101) &
0xff00U) >> 8) : __swap16md(10101))
;
526 if (pktinfo &&
527 setsockopt(dummy, IPPROTO_IPV641, IPV6_PKTINFO46,
528 pktinfo, sizeof(*pktinfo)))
529 err(1, "UDP setsockopt(IPV6_PKTINFO)");
530
531 if (hoplimit != -1 &&
532 setsockopt(dummy, IPPROTO_IPV641, IPV6_UNICAST_HOPS4,
533 &hoplimit, sizeof(hoplimit)))
534 err(1, "UDP setsockopt(IPV6_UNICAST_HOPS)");
535
536 if (hoplimit != -1 &&
537 setsockopt(dummy, IPPROTO_IPV641, IPV6_MULTICAST_HOPS10,
538 &hoplimit, sizeof(hoplimit)))
539 err(1, "UDP setsockopt(IPV6_MULTICAST_HOPS)");
540 } else {
541 u_char loop = 0;
542
543 from4.sin_port = ntohs(DUMMY_PORT)(__uint16_t)(__builtin_constant_p(10101) ? (__uint16_t)(((__uint16_t
)(10101) & 0xffU) << 8 | ((__uint16_t)(10101) &
0xff00U) >> 8) : __swap16md(10101))
;
544
545 if ((moptions & MULTICAST_NOLOOP0x001) && setsockopt(dummy,
546 IPPROTO_IP0, IP_MULTICAST_LOOP11, &loop,
547 sizeof(loop)) == -1)
548 err(1, "setsockopt IP_MULTICAST_LOOP");
549 if ((moptions & MULTICAST_TTL0x002) && setsockopt(dummy,
550 IPPROTO_IP0, IP_MULTICAST_TTL10, &ttl,
551 sizeof(ttl)) == -1)
552 err(1, "setsockopt IP_MULTICAST_TTL");
553 }
554
555 if (rtableid > 0 &&
556 setsockopt(dummy, SOL_SOCKET0xffff, SO_RTABLE0x1021, &rtableid,
557 sizeof(rtableid)) == -1)
558 err(1, "setsockopt(SO_RTABLE)");
559
560 if (connect(dummy, from, len) == -1)
561 err(1, "UDP connect");
562
563 if (getsockname(dummy, from, &len) == -1)
564 err(1, "getsockname");
565
566 close(dummy);
567 }
568
569 if (options & F_SO_DEBUG0x0040)
32
Assuming the condition is false
33
Taking false branch
570 (void)setsockopt(s, SOL_SOCKET0xffff, SO_DEBUG0x0001, &optval,
571 sizeof(optval));
572
573 if ((options & F_FLOOD0x0001) && (options & F_INTERVAL0x0002))
34
Assuming the condition is false
574 errx(1, "-f and -i options are incompatible");
575
576 if ((options & F_FLOOD0x0001) && (options & (F_AUD_RECV0x2000 | F_AUD_MISS0x4000)))
577 warnx("No audible output for flood pings");
578
579 if (datalen >= sizeof(struct payload)) /* can we time transfer */
35
Assuming the condition is false
36
Taking false branch
580 timing = 1;
581
582 if (v6flag
36.1
'v6flag' is 0
) {
37
Taking false branch
583 /* in F_VERBOSE case, we may get non-echoreply packets*/
584 if ((options & F_VERBOSE0x0100) && datalen < 2048) /* XXX 2048? */
585 packlen = 2048 + IP6LEN40 + ECHOLEN8 + EXTRA256;
586 else
587 packlen = datalen + IP6LEN40 + ECHOLEN8 + EXTRA256;
588 } else
589 packlen = datalen + MAXIPLEN60 + MAXICMPLEN76;
590 if (!(packet = malloc(packlen)))
38
Assuming 'packet' is non-null
39
Taking false branch
591 err(1, "malloc");
592
593 if (!(options & F_PINGFILLED0x0008))
40
Assuming the condition is false
41
Taking false branch
594 for (i = ECHOTMLENsizeof(struct payload); i < datalen; ++i)
595 *datap++ = i;
596
597 ident = arc4random() & 0xFFFF;
598
599 /*
600 * When trying to send large packets, you must increase the
601 * size of both the send and receive buffers...
602 */
603 maxsizelen = sizeof maxsize;
604 if (getsockopt(s, SOL_SOCKET0xffff, SO_SNDBUF0x1001, &maxsize, &maxsizelen) == -1)
42
Assuming the condition is false
43
Taking false branch
605 err(1, "getsockopt");
606 if (maxsize < packlen &&
44
Assuming 'maxsize' is >= 'packlen'
607 setsockopt(s, SOL_SOCKET0xffff, SO_SNDBUF0x1001, &packlen, sizeof(maxsize)) == -1)
608 err(1, "setsockopt");
609
610 /*
611 * When pinging the broadcast address, you can get a lot of answers.
612 * Doing something so evil is useful if you are trying to stress the
613 * ethernet, or just want to fill the arp cache to get some stuff for
614 * /etc/ethers.
615 */
616 while (setsockopt(s, SOL_SOCKET0xffff, SO_RCVBUF0x1002,
45
Assuming the condition is false
46
Loop condition is false. Execution continues on line 621
617 &bufspace, sizeof(bufspace)) == -1) {
618 if ((bufspace -= 1024) <= 0)
619 err(1, "Cannot set the receive buffer size");
620 }
621 if (bufspace
46.1
'bufspace' is >= IP_MAXPACKET
< IP_MAXPACKET65535)
47
Taking false branch
622 warnx("Could only allocate a receive buffer of %d bytes "
623 "(default %d)", bufspace, IP_MAXPACKET65535);
624
625 if (v6flag
47.1
'v6flag' is 0
) {
48
Taking false branch
626 unsigned int loop = 0;
627
628 /*
629 * let the kernel pass extension headers of incoming packets,
630 * for privileged socket options
631 */
632 if (options & F_VERBOSE0x0100) {
633 int opton = 1;
634
635 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVHOPOPTS39,
636 &opton, sizeof(opton)))
637 err(1, "setsockopt(IPV6_RECVHOPOPTS)");
638 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVDSTOPTS40,
639 &opton, sizeof(opton)))
640 err(1, "setsockopt(IPV6_RECVDSTOPTS)");
641 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVRTHDR38,
642 &opton, sizeof(opton)))
643 err(1, "setsockopt(IPV6_RECVRTHDR)");
644 ICMP6_FILTER_SETPASSALL(&filt)memset(&filt, 0xff, sizeof(struct icmp6_filter));
645 } else {
646 ICMP6_FILTER_SETBLOCKALL(&filt)memset(&filt, 0x00, sizeof(struct icmp6_filter));
647 ICMP6_FILTER_SETPASS(ICMP6_ECHO_REPLY, &filt)(((&filt)->icmp6_filt[(129) >> 5]) |= (1 <<
((129) & 31)))
;
648 }
649
650 if ((moptions & MULTICAST_NOLOOP0x001) &&
651 setsockopt(s, IPPROTO_IPV641, IPV6_MULTICAST_LOOP11,
652 &loop, sizeof(loop)) == -1)
653 err(1, "setsockopt IPV6_MULTICAST_LOOP");
654
655 optval = IPV6_DEFHLIM64;
656 if (IN6_IS_ADDR_MULTICAST(&dst6.sin6_addr)((&dst6.sin6_addr)->__u6_addr.__u6_addr8[0] == 0xff)) {
657 if (setsockopt(s, IPPROTO_IPV641, IPV6_MULTICAST_HOPS10,
658 &optval, sizeof(optval)) == -1)
659 err(1, "IPV6_MULTICAST_HOPS");
660 }
661 if (mflag != 1) {
662 optval = mflag > 1 ? 0 : 1;
663
664 if (setsockopt(s, IPPROTO_IPV641, IPV6_USE_MIN_MTU42,
665 &optval, sizeof(optval)) == -1)
666 err(1, "setsockopt(IPV6_USE_MIN_MTU)");
667 } else {
668 optval = 1;
669 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVPATHMTU43,
670 &optval, sizeof(optval)) == -1)
671 err(1, "setsockopt(IPV6_RECVPATHMTU)");
672 }
673
674 if (setsockopt(s, IPPROTO_ICMPV658, ICMP6_FILTER18,
675 &filt, sizeof(filt)) == -1)
676 err(1, "setsockopt(ICMP6_FILTER)");
677
678 if (hoplimit != -1) {
679 /* set IP6 packet options */
680 if ((scmsg = malloc( CMSG_SPACE(sizeof(int))((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (((unsigned long)(sizeof(int))
+ (sizeof(long) - 1)) &~(sizeof(long) - 1)))
)) == NULL((void *)0))
681 err(1, "malloc");
682 smsghdr.msg_control = (caddr_t)scmsg;
683 smsghdr.msg_controllen = CMSG_SPACE(sizeof(int))((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (((unsigned long)(sizeof(int))
+ (sizeof(long) - 1)) &~(sizeof(long) - 1)))
;
684
685 scmsg->cmsg_len = CMSG_LEN(sizeof(int))((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (sizeof(int)))
;
686 scmsg->cmsg_level = IPPROTO_IPV641;
687 scmsg->cmsg_type = IPV6_HOPLIMIT47;
688 *(int *)(CMSG_DATA(scmsg)((unsigned char *)(scmsg) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
) = hoplimit;
689 }
690
691 if (options & F_TOS0x1000) {
692 optval = tos;
693 if (setsockopt(s, IPPROTO_IPV641, IPV6_TCLASS61,
694 &optval, sizeof(optval)) == -1)
695 err(1, "setsockopt(IPV6_TCLASS)");
696 }
697
698 if (df) {
699 optval = 1;
700 if (setsockopt(s, IPPROTO_IPV641, IPV6_DONTFRAG62,
701 &optval, sizeof(optval)) == -1)
702 err(1, "setsockopt(IPV6_DONTFRAG)");
703 }
704
705 optval = 1;
706 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVPKTINFO36,
707 &optval, sizeof(optval)) == -1)
708 err(1, "setsockopt(IPV6_RECVPKTINFO)");
709 if (setsockopt(s, IPPROTO_IPV641, IPV6_RECVHOPLIMIT37,
710 &optval, sizeof(optval)) == -1)
711 err(1, "setsockopt(IPV6_RECVHOPLIMIT)");
712 } else {
713 u_char loop = 0;
714
715 if (options & F_TTL0x0800) {
49
Assuming the condition is true
50
Taking true branch
716 if (IN_MULTICAST(ntohl(dst4.sin_addr.s_addr))(((u_int32_t)((__uint32_t)(__builtin_constant_p(dst4.sin_addr
.s_addr) ? (__uint32_t)(((__uint32_t)(dst4.sin_addr.s_addr) &
0xff) << 24 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff00) << 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff0000) >> 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff000000) >> 24) : __swap32md(dst4.sin_addr.s_addr))
) & ((u_int32_t)(0xf0000000))) == ((u_int32_t)(0xe0000000
)))
)
51
'?' condition is false
52
Assuming the condition is false
53
Taking false branch
717 moptions |= MULTICAST_TTL0x002;
718 else
719 options |= F_HDRINCL0x0400;
720 }
721
722 if ((options & F_RROUTE0x0020) && (options & F_HDRINCL0x0400))
54
Assuming the condition is false
723 errx(1, "-R option and -D or -T, or -t to unicast"
724 " destinations are incompatible");
725
726 if (options & F_HDRINCL0x0400) {
55
Assuming the condition is false
56
Taking false branch
727 struct ip *ip = (struct ip *)outpackhdr;
728
729 if (setsockopt(s, IPPROTO_IP0, IP_HDRINCL2,
730 &optval, sizeof(optval)) == -1)
731 err(1, "setsockopt(IP_HDRINCL)");
732 ip->ip_v = IPVERSION4;
733 ip->ip_hl = sizeof(struct ip) >> 2;
734 ip->ip_tos = tos;
735 ip->ip_id = 0;
736 ip->ip_off = htons(df ? IP_DF : 0)(__uint16_t)(__builtin_constant_p(df ? 0x4000 : 0) ? (__uint16_t
)(((__uint16_t)(df ? 0x4000 : 0) & 0xffU) << 8 | ((
__uint16_t)(df ? 0x4000 : 0) & 0xff00U) >> 8) : __swap16md
(df ? 0x4000 : 0))
;
737 ip->ip_ttl = ttl;
738 ip->ip_p = IPPROTO_ICMP1;
739 if (source)
740 ip->ip_src = from4.sin_addr;
741 else
742 ip->ip_src.s_addr = INADDR_ANY((u_int32_t)(0x00000000));
743 ip->ip_dst = dst4.sin_addr;
744 }
745
746 /* record route option */
747 if (options & F_RROUTE0x0020) {
57
Taking false branch
748 if (IN_MULTICAST(ntohl(dst4.sin_addr.s_addr))(((u_int32_t)((__uint32_t)(__builtin_constant_p(dst4.sin_addr
.s_addr) ? (__uint32_t)(((__uint32_t)(dst4.sin_addr.s_addr) &
0xff) << 24 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff00) << 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff0000) >> 8 | ((__uint32_t)(dst4.sin_addr.s_addr) &
0xff000000) >> 24) : __swap32md(dst4.sin_addr.s_addr))
) & ((u_int32_t)(0xf0000000))) == ((u_int32_t)(0xe0000000
)))
)
749 errx(1, "record route not valid to multicast"
750 " destinations");
751 memset(rspace, 0, sizeof(rspace));
752 rspace[IPOPT_OPTVAL0] = IPOPT_RR7;
753 rspace[IPOPT_OLEN1] = sizeof(rspace)-1;
754 rspace[IPOPT_OFFSET2] = IPOPT_MINOFF4;
755 if (setsockopt(s, IPPROTO_IP0, IP_OPTIONS1,
756 rspace, sizeof(rspace)) == -1)
757 err(1, "record route");
758 }
759
760 if ((moptions & MULTICAST_NOLOOP0x001) &&
58
Assuming the condition is false
761 setsockopt(s, IPPROTO_IP0, IP_MULTICAST_LOOP11,
762 &loop, sizeof(loop)) == -1)
763 err(1, "setsockopt IP_MULTICAST_LOOP");
764 if ((moptions & MULTICAST_TTL0x002) &&
59
Assuming the condition is false
765 setsockopt(s, IPPROTO_IP0, IP_MULTICAST_TTL10,
766 &ttl, sizeof(ttl)) == -1)
767 err(1, "setsockopt IP_MULTICAST_TTL");
768 }
769
770 if (options & F_HOSTNAME0x0004) {
60
Assuming the condition is false
61
Taking false branch
771 if (pledge("stdio inet dns", NULL((void *)0)) == -1)
772 err(1, "pledge");
773 } else {
774 if (pledge("stdio inet", NULL((void *)0)) == -1)
62
Assuming the condition is false
63
Taking false branch
775 err(1, "pledge");
776 }
777
778 arc4random_buf(&tv64_offset, sizeof(tv64_offset));
779 arc4random_buf(&mac_key, sizeof(mac_key));
780
781 printf("PING %s (", hostname);
782 if (options & F_VERBOSE0x0100)
64
Assuming the condition is true
65
Taking true branch
783 printf("%s --> ", pr_addr(from, from->sa_len));
66
2nd function call argument is an uninitialized value
784 printf("%s): %d data bytes\n", pr_addr(dst, dst->sa_len), datalen);
785
786 smsghdr.msg_name = dst;
787 smsghdr.msg_namelen = dst->sa_len;
788 smsgiov.iov_base = (caddr_t)outpack;
789 smsghdr.msg_iov = &smsgiov;
790 smsghdr.msg_iovlen = 1;
791
792 /* Drain our socket. */
793 (void)signal(SIGALRM14, onsignal);
794 memset(&itimer, 0, sizeof(itimer));
795 itimer.it_value.tv_sec = 1; /* make sure we don't get stuck */
796 (void)setitimer(ITIMER_REAL0, &itimer, NULL((void *)0));
797 for (;;) {
798 struct msghdr m;
799 union {
800 struct cmsghdr hdr;
801 u_char buf[CMSG_SPACE(1024)((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (((unsigned long)(1024) + (sizeof
(long) - 1)) &~(sizeof(long) - 1)))
];
802 } cmsgbuf;
803 struct iovec iov[1];
804 struct pollfd pfd;
805 struct sockaddr_in peer4;
806 struct sockaddr_in6 peer6;
807 ssize_t cc;
808
809 if (seenalrm)
810 break;
811
812 pfd.fd = s;
813 pfd.events = POLLIN0x0001;
814
815 if (poll(&pfd, 1, 0) <= 0)
816 break;
817
818 if (v6flag) {
819 m.msg_name = &peer6;
820 m.msg_namelen = sizeof(peer6);
821 } else {
822 m.msg_name = &peer4;
823 m.msg_namelen = sizeof(peer4);
824 }
825 memset(&iov, 0, sizeof(iov));
826 iov[0].iov_base = (caddr_t)packet;
827 iov[0].iov_len = packlen;
828
829 m.msg_iov = iov;
830 m.msg_iovlen = 1;
831 m.msg_control = (caddr_t)&cmsgbuf.buf;
832 m.msg_controllen = sizeof(cmsgbuf.buf);
833
834 cc = recvmsg(s, &m, 0);
835 if (cc == -1 && errno(*__errno()) != EINTR4)
836 break;
837 }
838 memset(&itimer, 0, sizeof(itimer));
839 (void)setitimer(ITIMER_REAL0, &itimer, NULL((void *)0));
840
841 while (preload--) /* Fire off them quickies. */
842 pinger(s);
843
844 (void)signal(SIGINT2, onsignal);
845 (void)signal(SIGINFO29, onsignal);
846
847 if (!(options & F_FLOOD0x0001)) {
848 (void)signal(SIGALRM14, onsignal);
849 itimer.it_interval = interval;
850 itimer.it_value = interval;
851 (void)setitimer(ITIMER_REAL0, &itimer, NULL((void *)0));
852 if (ntransmitted == 0)
853 retransmit(s);
854 }
855
856 seenalrm = seenint = 0;
857 seeninfo = 0;
858
859 for (;;) {
860 struct msghdr m;
861 union {
862 struct cmsghdr hdr;
863 u_char buf[CMSG_SPACE(1024)((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (((unsigned long)(1024) + (sizeof
(long) - 1)) &~(sizeof(long) - 1)))
];
864 } cmsgbuf;
865 struct iovec iov[1];
866 struct pollfd pfd;
867 struct sockaddr_in peer4;
868 struct sockaddr_in6 peer6;
869 ssize_t cc;
870 int timeout;
871
872 /* signal handling */
873 if (seenint)
874 break;
875 if (seenalrm) {
876 if (flooddone)
877 break;
878 retransmit(s);
879 seenalrm = 0;
880 if (ntransmitted - nreceived - 1 > nmissedmax) {
881 nmissedmax = ntransmitted - nreceived - 1;
882 if (!(options & F_FLOOD0x0001) &&
883 (options & F_AUD_MISS0x4000))
884 fputc('\a', stderr(&__sF[2]));
885 if ((options & F_SHOWCHAR0x0080) &&
886 !(options & F_FLOOD0x0001)) {
887 putchar('.')(!__isthreaded ? __sputc('.', (&__sF[1])) : (putc)('.', (
&__sF[1])))
;
888 fflush(stdout(&__sF[1]));
889 }
890 }
891 continue;
892 }
893 if (seeninfo) {
894 summary();
895 seeninfo = 0;
896 continue;
897 }
898
899 if ((options & F_FLOOD0x0001 && !flooddone)) {
900 if (pinger(s) != 0) {
901 (void)signal(SIGALRM14, onsignal);
902 timeout = INFTIM(-1);
903 memset(&itimer, 0, sizeof(itimer));
904 if (nreceived) {
905 itimer.it_value.tv_sec = 2 * tmax /
906 1000;
907 if (itimer.it_value.tv_sec == 0)
908 itimer.it_value.tv_sec = 1;
909 } else
910 itimer.it_value.tv_sec = maxwait;
911 (void)setitimer(ITIMER_REAL0, &itimer, NULL((void *)0));
912
913 /* When the alarm goes off we are done. */
914 flooddone = 1;
915 } else
916 timeout = 10;
917 } else
918 timeout = INFTIM(-1);
919
920 pfd.fd = s;
921 pfd.events = POLLIN0x0001;
922
923 if (poll(&pfd, 1, timeout) <= 0)
924 continue;
925
926 if (v6flag) {
927 m.msg_name = &peer6;
928 m.msg_namelen = sizeof(peer6);
929 } else {
930 m.msg_name = &peer4;
931 m.msg_namelen = sizeof(peer4);
932 }
933 memset(&iov, 0, sizeof(iov));
934 iov[0].iov_base = (caddr_t)packet;
935 iov[0].iov_len = packlen;
936 m.msg_iov = iov;
937 m.msg_iovlen = 1;
938 m.msg_control = (caddr_t)&cmsgbuf.buf;
939 m.msg_controllen = sizeof(cmsgbuf.buf);
940
941 cc = recvmsg(s, &m, 0);
942 if (cc == -1) {
943 if (errno(*__errno()) != EINTR4) {
944 warn("recvmsg");
945 sleep(1);
946 }
947 continue;
948 } else if (cc == 0) {
949 int mtu;
950
951 /*
952 * receive control messages only. Process the
953 * exceptions (currently the only possibility is
954 * a path MTU notification.)
955 */
956 if ((mtu = get_pathmtu(&m, &dst6)) > 0) {
957 if (options & F_VERBOSE0x0100) {
958 printf("new path MTU (%d) is "
959 "notified\n", mtu);
960 }
961 }
962 continue;
963 } else
964 pr_pack(packet, cc, &m);
965
966 if (npackets && nreceived >= npackets)
967 break;
968 }
969 summary();
970 exit(nreceived == 0);
971}
972
973void
974onsignal(int sig)
975{
976 switch (sig) {
977 case SIGALRM14:
978 seenalrm++;
979 break;
980 case SIGINT2:
981 seenint++;
982 break;
983 case SIGINFO29:
984 seeninfo++;
985 break;
986 }
987}
988
989void
990fill(char *bp, char *patp)
991{
992 int ii, jj, kk;
993 int pat[16];
994 char *cp;
995
996 for (cp = patp; *cp; cp++)
997 if (!isxdigit((unsigned char)*cp))
998 errx(1, "patterns must be specified as hex digits");
999 ii = sscanf(patp,
1000 "%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x%2x",
1001 &pat[0], &pat[1], &pat[2], &pat[3], &pat[4], &pat[5], &pat[6],
1002 &pat[7], &pat[8], &pat[9], &pat[10], &pat[11], &pat[12],
1003 &pat[13], &pat[14], &pat[15]);
1004
1005 if (ii > 0)
1006 for (kk = 0;
1007 kk <= maxpayload - (ECHOLEN8 + ECHOTMLENsizeof(struct payload) + ii);
1008 kk += ii)
1009 for (jj = 0; jj < ii; ++jj)
1010 bp[jj + kk] = pat[jj];
1011 if (!(options & F_QUIET0x0010)) {
1012 printf("PATTERN: 0x");
1013 for (jj = 0; jj < ii; ++jj)
1014 printf("%02x", bp[jj] & 0xFF);
1015 printf("\n");
1016 }
1017}
1018
1019void
1020summary(void)
1021{
1022 printf("\n--- %s ping statistics ---\n", hostname);
1023 printf("%lld packets transmitted, ", ntransmitted);
1024 printf("%lld packets received, ", nreceived);
1025
1026 if (nrepeats)
1027 printf("%lld duplicates, ", nrepeats);
1028 if (ntransmitted) {
1029 if (nreceived > ntransmitted)
1030 printf("-- somebody's duplicating packets!");
1031 else
1032 printf("%.1f%% packet loss",
1033 ((((double)ntransmitted - nreceived) * 100) /
1034 ntransmitted));
1035 }
1036 printf("\n");
1037 if (timinginfo) {
1038 /* Only display average to microseconds */
1039 double num = nreceived + nrepeats;
1040 double avg = tsum / num;
1041 double dev = sqrt(fmax(0, tsumsq / num - avg * avg));
1042 printf("round-trip min/avg/max/std-dev = %.3f/%.3f/%.3f/%.3f ms\n",
1043 tmin, avg, tmax, dev);
1044 }
1045}
1046
1047/*
1048 * pr_addr --
1049 * Return address in numeric form or a host name
1050 */
1051const char *
1052pr_addr(struct sockaddr *addr, socklen_t addrlen)
1053{
1054 static char buf[NI_MAXHOST256];
1055 int flag = 0;
1056
1057 if (!(options & F_HOSTNAME0x0004))
1058 flag |= NI_NUMERICHOST1;
1059
1060 if (getnameinfo(addr, addrlen, buf, sizeof(buf), NULL((void *)0), 0, flag) == 0)
1061 return (buf);
1062 else
1063 return "?";
1064}
1065
1066/*
1067 * retransmit --
1068 * This routine transmits another ping.
1069 */
1070void
1071retransmit(int s)
1072{
1073 struct itimerval itimer;
1074 static int last_time;
1075
1076 if (last_time) {
1077 seenint = 1; /* break out of ping event loop */
1078 return;
1079 }
1080
1081 if (pinger(s) == 0)
1082 return;
1083
1084 /*
1085 * If we're not transmitting any more packets, change the timer
1086 * to wait two round-trip times if we've received any packets or
1087 * maxwait seconds if we haven't.
1088 */
1089 memset(&itimer, 0, sizeof(itimer));
1090 if (nreceived) {
1091 itimer.it_value.tv_sec = 2 * tmax / 1000;
1092 if (itimer.it_value.tv_sec == 0)
1093 itimer.it_value.tv_sec = 1;
1094 } else
1095 itimer.it_value.tv_sec = maxwait;
1096 (void)setitimer(ITIMER_REAL0, &itimer, NULL((void *)0));
1097
1098 /* When the alarm goes off we are done. */
1099 last_time = 1;
1100}
1101
1102/*
1103 * pinger --
1104 * Compose and transmit an ICMP ECHO REQUEST packet. The IP packet
1105 * will be added on by the kernel. The ID field is a random number,
1106 * and the sequence number is an ascending integer. The first 8 bytes
1107 * of the data portion are used to hold a UNIX "timeval" struct in VAX
1108 * byte-order, to compute the round-trip time.
1109 */
1110int
1111pinger(int s)
1112{
1113 struct icmp *icp = NULL((void *)0);
1114 struct icmp6_hdr *icp6 = NULL((void *)0);
1115 int cc, i;
1116 u_int16_t seq;
1117
1118 if (npackets && ntransmitted >= npackets)
1119 return(-1); /* no more transmission */
1120
1121 seq = htons(ntransmitted++)(__uint16_t)(__builtin_constant_p(ntransmitted++) ? (__uint16_t
)(((__uint16_t)(ntransmitted++) & 0xffU) << 8 | ((__uint16_t
)(ntransmitted++) & 0xff00U) >> 8) : __swap16md(ntransmitted
++))
;
1122
1123 if (v6flag) {
1124 icp6 = (struct icmp6_hdr *)outpack;
1125 memset(icp6, 0, sizeof(*icp6));
1126 icp6->icmp6_cksum = 0;
1127 icp6->icmp6_type = ICMP6_ECHO_REQUEST128;
1128 icp6->icmp6_code = 0;
1129 icp6->icmp6_idicmp6_dataun.icmp6_un_data16[0] = ident;
1130 icp6->icmp6_seqicmp6_dataun.icmp6_un_data16[1] = seq;
1131 } else {
1132 icp = (struct icmp *)outpack;
1133 icp->icmp_type = ICMP_ECHO8;
1134 icp->icmp_code = 0;
1135 icp->icmp_cksum = 0;
1136 icp->icmp_seqicmp_hun.ih_idseq.icd_seq = seq;
1137 icp->icmp_idicmp_hun.ih_idseq.icd_id = ident;
1138 }
1139 CLR(ntohs(seq) % mx_dup_ck)(rcvd_tbl[((__uint16_t)(__builtin_constant_p(seq) ? (__uint16_t
)(((__uint16_t)(seq) & 0xffU) << 8 | ((__uint16_t)(
seq) & 0xff00U) >> 8) : __swap16md(seq)) % mx_dup_ck
)>>3] &= (~(1 << (((__uint16_t)(__builtin_constant_p
(seq) ? (__uint16_t)(((__uint16_t)(seq) & 0xffU) <<
8 | ((__uint16_t)(seq) & 0xff00U) >> 8) : __swap16md
(seq)) % mx_dup_ck) & 0x07))))
;
1140
1141 if (timing) {
1142 SIPHASH_CTX ctx;
1143 struct timespec ts;
1144 struct payload payload;
1145 struct tv64 *tv64 = &payload.tv64;
1146
1147 if (clock_gettime(CLOCK_MONOTONIC3, &ts) == -1)
1148 err(1, "clock_gettime(CLOCK_MONOTONIC)");
1149 tv64->tv64_sec = htobe64((u_int64_t)ts.tv_sec +(__uint64_t)(__builtin_constant_p((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec) ? (__uint64_t)((((__uint64_t)((u_int64_t)ts.tv_sec
+ tv64_offset.tv64_sec) & 0xff) << 56) | ((__uint64_t
)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec) & 0xff00ULL
) << 40 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec) & 0xff0000ULL) << 24 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff000000ULL) <<
8 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec
) & 0xff00000000ULL) >> 8 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff0000000000ULL) >>
24 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec
) & 0xff000000000000ULL) >> 40 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff00000000000000ULL
) >> 56) : __swap64md((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec))
1150 tv64_offset.tv64_sec)(__uint64_t)(__builtin_constant_p((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec) ? (__uint64_t)((((__uint64_t)((u_int64_t)ts.tv_sec
+ tv64_offset.tv64_sec) & 0xff) << 56) | ((__uint64_t
)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec) & 0xff00ULL
) << 40 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec) & 0xff0000ULL) << 24 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff000000ULL) <<
8 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec
) & 0xff00000000ULL) >> 8 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff0000000000ULL) >>
24 | ((__uint64_t)((u_int64_t)ts.tv_sec + tv64_offset.tv64_sec
) & 0xff000000000000ULL) >> 40 | ((__uint64_t)((u_int64_t
)ts.tv_sec + tv64_offset.tv64_sec) & 0xff00000000000000ULL
) >> 56) : __swap64md((u_int64_t)ts.tv_sec + tv64_offset
.tv64_sec))
;
1151 tv64->tv64_nsec = htobe64((u_int64_t)ts.tv_nsec +(__uint64_t)(__builtin_constant_p((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) ? (__uint64_t)((((__uint64_t)((u_int64_t)ts.tv_nsec
+ tv64_offset.tv64_nsec) & 0xff) << 56) | ((__uint64_t
)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff00ULL
) << 40 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) & 0xff0000ULL) << 24 | ((__uint64_t)((u_int64_t
)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff000000ULL) <<
8 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec
) & 0xff00000000ULL) >> 8 | ((__uint64_t)((u_int64_t
)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff0000000000ULL)
>> 24 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) & 0xff000000000000ULL) >> 40 | ((__uint64_t
)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff00000000000000ULL
) >> 56) : __swap64md((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec))
1152 tv64_offset.tv64_nsec)(__uint64_t)(__builtin_constant_p((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) ? (__uint64_t)((((__uint64_t)((u_int64_t)ts.tv_nsec
+ tv64_offset.tv64_nsec) & 0xff) << 56) | ((__uint64_t
)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff00ULL
) << 40 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) & 0xff0000ULL) << 24 | ((__uint64_t)((u_int64_t
)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff000000ULL) <<
8 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec
) & 0xff00000000ULL) >> 8 | ((__uint64_t)((u_int64_t
)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff0000000000ULL)
>> 24 | ((__uint64_t)((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec) & 0xff000000000000ULL) >> 40 | ((__uint64_t
)((u_int64_t)ts.tv_nsec + tv64_offset.tv64_nsec) & 0xff00000000000000ULL
) >> 56) : __swap64md((u_int64_t)ts.tv_nsec + tv64_offset
.tv64_nsec))
;
1153
1154 SipHash24_Init(&ctx, &mac_key)SipHash_Init((&ctx), (&mac_key));
1155 SipHash24_Update(&ctx, tv64, sizeof(*tv64))SipHash_Update((&ctx), 2, 4, (tv64), (sizeof(*tv64)));
1156 SipHash24_Update(&ctx, &ident, sizeof(ident))SipHash_Update((&ctx), 2, 4, (&ident), (sizeof(ident)
))
;
1157 SipHash24_Update(&ctx, &seq, sizeof(seq))SipHash_Update((&ctx), 2, 4, (&seq), (sizeof(seq)));
1158 SipHash24_Final(&payload.mac, &ctx)SipHash_Final((&payload.mac), (&ctx), 2, 4);
1159
1160 memcpy(&outpack[ECHOLEN8], &payload, sizeof(payload));
1161 }
1162
1163 cc = ECHOLEN8 + datalen;
1164
1165 if (!v6flag) {
1166 /* compute ICMP checksum here */
1167 icp->icmp_cksum = in_cksum((u_short *)icp, cc);
1168
1169 if (options & F_HDRINCL0x0400) {
1170 struct ip *ip = (struct ip *)outpackhdr;
1171
1172 smsgiov.iov_base = (caddr_t)outpackhdr;
1173 cc += sizeof(struct ip);
1174 ip->ip_len = htons(cc)(__uint16_t)(__builtin_constant_p(cc) ? (__uint16_t)(((__uint16_t
)(cc) & 0xffU) << 8 | ((__uint16_t)(cc) & 0xff00U
) >> 8) : __swap16md(cc))
;
1175 ip->ip_sum = in_cksum((u_short *)outpackhdr, cc);
1176 }
1177 }
1178
1179 smsgiov.iov_len = cc;
1180
1181 i = sendmsg(s, &smsghdr, 0);
1182
1183 if (i == -1 || i != cc) {
1184 if (i == -1)
1185 warn("sendmsg");
1186 printf("ping: wrote %s %d chars, ret=%d\n", hostname, cc, i);
1187 }
1188 if (!(options & F_QUIET0x0010) && (options & F_FLOOD0x0001))
1189 write(STDOUT_FILENO1, &DOT, 1);
1190
1191 return (0);
1192}
1193
1194/*
1195 * pr_pack --
1196 * Print out the packet, if it came from us. This logic is necessary
1197 * because ALL readers of the ICMP socket get a copy of ALL ICMP packets
1198 * which arrive ('tis only fair). This permits multiple copies of this
1199 * program to be run without having intermingled output (or statistics!).
1200 */
1201void
1202pr_pack(u_char *buf, int cc, struct msghdr *mhdr)
1203{
1204 struct ip *ip = NULL((void *)0);
1205 struct icmp *icp = NULL((void *)0);
1206 struct icmp6_hdr *icp6 = NULL((void *)0);
1207 struct timespec ts, tp;
1208 struct payload payload;
1209 struct sockaddr *from;
1210 socklen_t fromlen;
1211 double triptime = 0;
1212 int i, dupflag;
1213 int hlen = -1, hoplim = -1, echo_reply = 0;
1214 u_int16_t seq;
1215 u_char *cp, *dp;
1216 char* pkttime;
1217
1218 if (clock_gettime(CLOCK_MONOTONIC3, &ts) == -1)
1219 err(1, "clock_gettime(CLOCK_MONOTONIC)");
1220
1221 if (v6flag) {
1222 if (!mhdr || !mhdr->msg_name ||
1223 mhdr->msg_namelen != sizeof(struct sockaddr_in6) ||
1224 ((struct sockaddr *)mhdr->msg_name)->sa_family !=
1225 AF_INET624) {
1226 if (options & F_VERBOSE0x0100)
1227 warnx("invalid peername");
1228 return;
1229 }
1230 from = (struct sockaddr *)mhdr->msg_name;
1231 fromlen = mhdr->msg_namelen;
1232
1233 if (cc < sizeof(struct icmp6_hdr)) {
1234 if (options & F_VERBOSE0x0100)
1235 warnx("packet too short (%d bytes) from %s", cc,
1236 pr_addr(from, fromlen));
1237 return;
1238 }
1239 icp6 = (struct icmp6_hdr *)buf;
1240
1241 if ((hoplim = get_hoplim(mhdr)) == -1) {
1242 warnx("failed to get receiving hop limit");
1243 return;
1244 }
1245
1246 if (icp6->icmp6_type == ICMP6_ECHO_REPLY129) {
1247 if (icp6->icmp6_idicmp6_dataun.icmp6_un_data16[0] != ident)
1248 return; /* 'Twas not our ECHO */
1249 seq = icp6->icmp6_seqicmp6_dataun.icmp6_un_data16[1];
1250 echo_reply = 1;
1251 pkttime = (char *)(icp6 + 1);
1252 }
1253 } else {
1254 if (!mhdr || !mhdr->msg_name ||
1255 mhdr->msg_namelen != sizeof(struct sockaddr_in) ||
1256 ((struct sockaddr *)mhdr->msg_name)->sa_family != AF_INET2) {
1257 if (options & F_VERBOSE0x0100)
1258 warnx("invalid peername");
1259 return;
1260 }
1261
1262 from = (struct sockaddr *)mhdr->msg_name;
1263 fromlen = mhdr->msg_namelen;
1264
1265 /* Check the IP header */
1266 ip = (struct ip *)buf;
1267 hlen = ip->ip_hl << 2;
1268 if (cc < hlen + ICMP_MINLEN8) {
1269 if (options & F_VERBOSE0x0100)
1270 warnx("packet too short (%d bytes) from %s", cc,
1271 pr_addr(from, fromlen));
1272 return;
1273 }
1274
1275 /* Now the ICMP part */
1276 cc -= hlen;
1277 icp = (struct icmp *)(buf + hlen);
1278 if (icp->icmp_type == ICMP_ECHOREPLY0) {
1279 if (icp->icmp_idicmp_hun.ih_idseq.icd_id != ident)
1280 return; /* 'Twas not our ECHO */
1281 seq = icp->icmp_seqicmp_hun.ih_idseq.icd_seq;
1282 echo_reply = 1;
1283 pkttime = (char *)icp->icmp_dataicmp_dun.id_data;
1284 }
1285 }
1286
1287 if (echo_reply) {
1288 ++nreceived;
1289 if (cc >= ECHOLEN8 + ECHOTMLENsizeof(struct payload)) {
1290 SIPHASH_CTX ctx;
1291 struct tv64 *tv64;
1292 u_int8_t mac[SIPHASH_DIGEST_LENGTH8];
1293
1294 memcpy(&payload, pkttime, sizeof(payload));
1295 tv64 = &payload.tv64;
1296
1297 SipHash24_Init(&ctx, &mac_key)SipHash_Init((&ctx), (&mac_key));
1298 SipHash24_Update(&ctx, tv64, sizeof(*tv64))SipHash_Update((&ctx), 2, 4, (tv64), (sizeof(*tv64)));
1299 SipHash24_Update(&ctx, &ident, sizeof(ident))SipHash_Update((&ctx), 2, 4, (&ident), (sizeof(ident)
))
;
1300 SipHash24_Update(&ctx, &seq, sizeof(seq))SipHash_Update((&ctx), 2, 4, (&seq), (sizeof(seq)));
1301 SipHash24_Final(mac, &ctx)SipHash_Final((mac), (&ctx), 2, 4);
1302
1303 if (timingsafe_memcmp(mac, &payload.mac,
1304 sizeof(mac)) != 0) {
1305 printf("signature mismatch!\n");
1306 return;
1307 }
1308 timinginfo=1;
1309
1310 tp.tv_sec = betoh64(tv64->tv64_sec)(__uint64_t)(__builtin_constant_p(tv64->tv64_sec) ? (__uint64_t
)((((__uint64_t)(tv64->tv64_sec) & 0xff) << 56) |
((__uint64_t)(tv64->tv64_sec) & 0xff00ULL) << 40
| ((__uint64_t)(tv64->tv64_sec) & 0xff0000ULL) <<
24 | ((__uint64_t)(tv64->tv64_sec) & 0xff000000ULL) <<
8 | ((__uint64_t)(tv64->tv64_sec) & 0xff00000000ULL) >>
8 | ((__uint64_t)(tv64->tv64_sec) & 0xff0000000000ULL
) >> 24 | ((__uint64_t)(tv64->tv64_sec) & 0xff000000000000ULL
) >> 40 | ((__uint64_t)(tv64->tv64_sec) & 0xff00000000000000ULL
) >> 56) : __swap64md(tv64->tv64_sec))
-
1311 tv64_offset.tv64_sec;
1312 tp.tv_nsec = betoh64(tv64->tv64_nsec)(__uint64_t)(__builtin_constant_p(tv64->tv64_nsec) ? (__uint64_t
)((((__uint64_t)(tv64->tv64_nsec) & 0xff) << 56)
| ((__uint64_t)(tv64->tv64_nsec) & 0xff00ULL) <<
40 | ((__uint64_t)(tv64->tv64_nsec) & 0xff0000ULL) <<
24 | ((__uint64_t)(tv64->tv64_nsec) & 0xff000000ULL) <<
8 | ((__uint64_t)(tv64->tv64_nsec) & 0xff00000000ULL)
>> 8 | ((__uint64_t)(tv64->tv64_nsec) & 0xff0000000000ULL
) >> 24 | ((__uint64_t)(tv64->tv64_nsec) & 0xff000000000000ULL
) >> 40 | ((__uint64_t)(tv64->tv64_nsec) & 0xff00000000000000ULL
) >> 56) : __swap64md(tv64->tv64_nsec))
-
1313 tv64_offset.tv64_nsec;
1314
1315 timespecsub(&ts, &tp, &ts)do { (&ts)->tv_sec = (&ts)->tv_sec - (&tp)->
tv_sec; (&ts)->tv_nsec = (&ts)->tv_nsec - (&
tp)->tv_nsec; if ((&ts)->tv_nsec < 0) { (&ts
)->tv_sec--; (&ts)->tv_nsec += 1000000000L; } } while
(0)
;
1316 triptime = ((double)ts.tv_sec) * 1000.0 +
1317 ((double)ts.tv_nsec) / 1000000.0;
1318 tsum += triptime;
1319 tsumsq += triptime * triptime;
1320 if (triptime < tmin)
1321 tmin = triptime;
1322 if (triptime > tmax)
1323 tmax = triptime;
1324 }
1325
1326 if (TST(ntohs(seq) % mx_dup_ck)(rcvd_tbl[((__uint16_t)(__builtin_constant_p(seq) ? (__uint16_t
)(((__uint16_t)(seq) & 0xffU) << 8 | ((__uint16_t)(
seq) & 0xff00U) >> 8) : __swap16md(seq)) % mx_dup_ck
)>>3] & (1 << (((__uint16_t)(__builtin_constant_p
(seq) ? (__uint16_t)(((__uint16_t)(seq) & 0xffU) <<
8 | ((__uint16_t)(seq) & 0xff00U) >> 8) : __swap16md
(seq)) % mx_dup_ck) & 0x07)))
) {
1327 ++nrepeats;
1328 --nreceived;
1329 dupflag = 1;
1330 } else {
1331 SET(ntohs(seq) % mx_dup_ck)(rcvd_tbl[((__uint16_t)(__builtin_constant_p(seq) ? (__uint16_t
)(((__uint16_t)(seq) & 0xffU) << 8 | ((__uint16_t)(
seq) & 0xff00U) >> 8) : __swap16md(seq)) % mx_dup_ck
)>>3] |= (1 << (((__uint16_t)(__builtin_constant_p
(seq) ? (__uint16_t)(((__uint16_t)(seq) & 0xffU) <<
8 | ((__uint16_t)(seq) & 0xff00U) >> 8) : __swap16md
(seq)) % mx_dup_ck) & 0x07)))
;
1332 dupflag = 0;
1333 }
1334
1335 if (options & F_QUIET0x0010)
1336 return;
1337
1338 if (options & F_FLOOD0x0001)
1339 write(STDOUT_FILENO1, &BSPACE, 1);
1340 else if (options & F_SHOWCHAR0x0080) {
1341 if (dupflag)
1342 putchar('D')(!__isthreaded ? __sputc('D', (&__sF[1])) : (putc)('D', (
&__sF[1])))
;
1343 else if (cc - ECHOLEN8 < datalen)
1344 putchar('T')(!__isthreaded ? __sputc('T', (&__sF[1])) : (putc)('T', (
&__sF[1])))
;
1345 else
1346 putchar('!')(!__isthreaded ? __sputc('!', (&__sF[1])) : (putc)('!', (
&__sF[1])))
;
1347 } else {
1348 printf("%d bytes from %s: icmp_seq=%u", cc,
1349 pr_addr(from, fromlen), ntohs(seq)(__uint16_t)(__builtin_constant_p(seq) ? (__uint16_t)(((__uint16_t
)(seq) & 0xffU) << 8 | ((__uint16_t)(seq) & 0xff00U
) >> 8) : __swap16md(seq))
);
1350 if (v6flag)
1351 printf(" hlim=%d", hoplim);
1352 else
1353 printf(" ttl=%d", ip->ip_ttl);
1354 if (cc >= ECHOLEN8 + ECHOTMLENsizeof(struct payload))
1355 printf(" time=%.3f ms", triptime);
1356 if (dupflag)
1357 printf(" (DUP!)");
1358 /* check the data */
1359 if (cc - ECHOLEN8 < datalen)
1360 printf(" (TRUNC!)");
1361 if (v6flag)
1362 cp = buf + ECHOLEN8 + ECHOTMLENsizeof(struct payload);
1363 else
1364 cp = (u_char *)&icp->icmp_dataicmp_dun.id_data[ECHOTMLENsizeof(struct payload)];
1365 dp = &outpack[ECHOLEN8 + ECHOTMLENsizeof(struct payload)];
1366 for (i = ECHOLEN8 + ECHOTMLENsizeof(struct payload);
1367 i < cc && i < datalen;
1368 ++i, ++cp, ++dp) {
1369 if (*cp != *dp) {
1370 printf("\nwrong data byte #%d "
1371 "should be 0x%x but was 0x%x",
1372 i - ECHOLEN8, *dp, *cp);
1373 if (v6flag)
1374 cp = buf + ECHOLEN8;
1375 else
1376 cp = (u_char *)
1377 &icp->icmp_dataicmp_dun.id_data[0];
1378 for (i = ECHOLEN8; i < cc && i < datalen;
1379 ++i, ++cp) {
1380 if ((i % 32) == 8)
1381 printf("\n\t");
1382 printf("%x ", *cp);
1383 }
1384 break;
1385 }
1386 }
1387 }
1388 } else {
1389 /* We've got something other than an ECHOREPLY */
1390 if (!(options & F_VERBOSE0x0100))
1391 return;
1392 printf("%d bytes from %s: ", cc, pr_addr(from, fromlen));
1393 if (v6flag)
1394 pr_icmph6(icp6, buf + cc);
1395 else
1396 pr_icmph(icp);
1397 }
1398
1399 /* Display any IP options */
1400 if (!v6flag && hlen > sizeof(struct ip))
1401 pr_ipopt(hlen, buf);
1402
1403 if (!(options & F_FLOOD0x0001)) {
1404 if (!(options & F_SHOWCHAR0x0080)) {
1405 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
1406 if (v6flag && (options & F_VERBOSE0x0100))
1407 pr_exthdrs(mhdr);
1408 }
1409 fflush(stdout(&__sF[1]));
1410 if (options & F_AUD_RECV0x2000)
1411 fputc('\a', stderr(&__sF[2]));
1412 }
1413}
1414
1415void
1416pr_ipopt(int hlen, u_char *buf)
1417{
1418 static int old_rrlen;
1419 static char old_rr[MAX_IPOPTLEN40];
1420 struct sockaddr_in s_in;
1421 in_addr_t l;
1422 u_int i, j;
1423 u_char *cp;
1424
1425 cp = buf + sizeof(struct ip);
1426
1427 s_in.sin_len = sizeof(s_in);
1428 s_in.sin_family = AF_INET2;
1429
1430 for (; hlen > (int)sizeof(struct ip); --hlen, ++cp) {
1431 switch (*cp) {
1432 case IPOPT_EOL0:
1433 hlen = 0;
1434 break;
1435 case IPOPT_LSRR131:
1436 printf("\nLSRR: ");
1437 hlen -= 2;
1438 j = *++cp;
1439 ++cp;
1440 i = 0;
1441 if (j > IPOPT_MINOFF4) {
1442 for (;;) {
1443 l = *++cp;
1444 l = (l<<8) + *++cp;
1445 l = (l<<8) + *++cp;
1446 l = (l<<8) + *++cp;
1447 if (l == 0)
1448 printf("\t0.0.0.0");
1449 else {
1450 s_in.sin_addr.s_addr = ntohl(l)(__uint32_t)(__builtin_constant_p(l) ? (__uint32_t)(((__uint32_t
)(l) & 0xff) << 24 | ((__uint32_t)(l) & 0xff00)
<< 8 | ((__uint32_t)(l) & 0xff0000) >> 8 | (
(__uint32_t)(l) & 0xff000000) >> 24) : __swap32md(l
))
;
1451 printf("\t%s",
1452 pr_addr((struct sockaddr*)
1453 &s_in, sizeof(s_in)));
1454 }
1455 hlen -= 4;
1456 j -= 4;
1457 i += 4;
1458 if (j <= IPOPT_MINOFF4)
1459 break;
1460 if (i >= MAX_IPOPTLEN40) {
1461 printf("\t(truncated route)");
1462 break;
1463 }
1464 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
1465 }
1466 }
1467 break;
1468 case IPOPT_RR7:
1469 j = *++cp; /* get length */
1470 i = *++cp; /* and pointer */
1471 hlen -= 2;
1472 if (i > j)
1473 i = j;
1474 i -= IPOPT_MINOFF4;
1475 if (i <= 0)
1476 continue;
1477 if (i == old_rrlen &&
1478 cp == buf + sizeof(struct ip) + 2 &&
1479 !memcmp(cp, old_rr, i) &&
1480 !(options & F_FLOOD0x0001)) {
1481 printf("\t(same route)");
1482 i = (i + 3) & ~0x3;
1483 hlen -= i;
1484 cp += i;
1485 break;
1486 }
1487 if (i < MAX_IPOPTLEN40) {
1488 old_rrlen = i;
1489 memcpy(old_rr, cp, i);
1490 } else
1491 old_rrlen = 0;
1492
1493 printf("\nRR: ");
1494 j = 0;
1495 for (;;) {
1496 l = *++cp;
1497 l = (l<<8) + *++cp;
1498 l = (l<<8) + *++cp;
1499 l = (l<<8) + *++cp;
1500 if (l == 0)
1501 printf("\t0.0.0.0");
1502 else {
1503 s_in.sin_addr.s_addr = ntohl(l)(__uint32_t)(__builtin_constant_p(l) ? (__uint32_t)(((__uint32_t
)(l) & 0xff) << 24 | ((__uint32_t)(l) & 0xff00)
<< 8 | ((__uint32_t)(l) & 0xff0000) >> 8 | (
(__uint32_t)(l) & 0xff000000) >> 24) : __swap32md(l
))
;
1504 printf("\t%s",
1505 pr_addr((struct sockaddr*)&s_in,
1506 sizeof(s_in)));
1507 }
1508 hlen -= 4;
1509 i -= 4;
1510 j += 4;
1511 if (i <= 0)
1512 break;
1513 if (j >= MAX_IPOPTLEN40) {
1514 printf("\t(truncated route)");
1515 break;
1516 }
1517 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
1518 }
1519 break;
1520 case IPOPT_NOP1:
1521 printf("\nNOP");
1522 break;
1523 default:
1524 printf("\nunknown option %x", *cp);
1525 hlen = hlen - (cp[IPOPT_OLEN1] - 1);
1526 cp = cp + (cp[IPOPT_OLEN1] - 1);
1527 break;
1528 }
1529 }
1530}
1531
1532/*
1533 * in_cksum --
1534 * Checksum routine for Internet Protocol family headers (C Version)
1535 */
1536int
1537in_cksum(u_short *addr, int len)
1538{
1539 int nleft = len;
1540 u_short *w = addr;
1541 int sum = 0;
1542 u_short answer = 0;
1543
1544 /*
1545 * Our algorithm is simple, using a 32 bit accumulator (sum), we add
1546 * sequential 16 bit words to it, and at the end, fold back all the
1547 * carry bits from the top 16 bits into the lower 16 bits.
1548 */
1549 while (nleft > 1) {
1550 sum += *w++;
1551 nleft -= 2;
1552 }
1553
1554 /* mop up an odd byte, if necessary */
1555 if (nleft == 1) {
1556 *(u_char *)(&answer) = *(u_char *)w ;
1557 sum += answer;
1558 }
1559
1560 /* add back carry outs from top 16 bits to low 16 bits */
1561 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
1562 sum += (sum >> 16); /* add carry */
1563 answer = ~sum; /* truncate to 16 bits */
1564 return(answer);
1565}
1566
1567/*
1568 * pr_icmph --
1569 * Print a descriptive string about an ICMP header.
1570 */
1571void
1572pr_icmph(struct icmp *icp)
1573{
1574 switch(icp->icmp_type) {
1575 case ICMP_ECHOREPLY0:
1576 printf("Echo Reply\n");
1577 /* XXX ID + Seq + Data */
1578 break;
1579 case ICMP_UNREACH3:
1580 switch(icp->icmp_code) {
1581 case ICMP_UNREACH_NET0:
1582 printf("Destination Net Unreachable\n");
1583 break;
1584 case ICMP_UNREACH_HOST1:
1585 printf("Destination Host Unreachable\n");
1586 break;
1587 case ICMP_UNREACH_PROTOCOL2:
1588 printf("Destination Protocol Unreachable\n");
1589 break;
1590 case ICMP_UNREACH_PORT3:
1591 printf("Destination Port Unreachable\n");
1592 break;
1593 case ICMP_UNREACH_NEEDFRAG4:
1594 if (icp->icmp_nextmtuicmp_hun.ih_pmtu.ipm_nextmtu != 0)
1595 printf("frag needed and DF set (MTU %d)\n",
1596 ntohs(icp->icmp_nextmtu)(__uint16_t)(__builtin_constant_p(icp->icmp_hun.ih_pmtu.ipm_nextmtu
) ? (__uint16_t)(((__uint16_t)(icp->icmp_hun.ih_pmtu.ipm_nextmtu
) & 0xffU) << 8 | ((__uint16_t)(icp->icmp_hun.ih_pmtu
.ipm_nextmtu) & 0xff00U) >> 8) : __swap16md(icp->
icmp_hun.ih_pmtu.ipm_nextmtu))
);
1597 else
1598 printf("frag needed and DF set\n");
1599 break;
1600 case ICMP_UNREACH_SRCFAIL5:
1601 printf("Source Route Failed\n");
1602 break;
1603 case ICMP_UNREACH_NET_UNKNOWN6:
1604 printf("Network Unknown\n");
1605 break;
1606 case ICMP_UNREACH_HOST_UNKNOWN7:
1607 printf("Host Unknown\n");
1608 break;
1609 case ICMP_UNREACH_ISOLATED8:
1610 printf("Source Isolated\n");
1611 break;
1612 case ICMP_UNREACH_NET_PROHIB9:
1613 printf("Dest. Net Administratively Prohibited\n");
1614 break;
1615 case ICMP_UNREACH_HOST_PROHIB10:
1616 printf("Dest. Host Administratively Prohibited\n");
1617 break;
1618 case ICMP_UNREACH_TOSNET11:
1619 printf("Destination Net Unreachable for TOS\n");
1620 break;
1621 case ICMP_UNREACH_TOSHOST12:
1622 printf("Destination Host Unreachable for TOS\n");
1623 break;
1624 case ICMP_UNREACH_FILTER_PROHIB13:
1625 printf("Route administratively prohibited\n");
1626 break;
1627 case ICMP_UNREACH_HOST_PRECEDENCE14:
1628 printf("Host Precedence Violation\n");
1629 break;
1630 case ICMP_UNREACH_PRECEDENCE_CUTOFF15:
1631 printf("Precedence Cutoff\n");
1632 break;
1633 default:
1634 printf("Dest Unreachable, Unknown Code: %d\n",
1635 icp->icmp_code);
1636 break;
1637 }
1638 /* Print returned IP header information */
1639 pr_retip((struct ip *)icp->icmp_dataicmp_dun.id_data);
1640 break;
1641 case ICMP_SOURCEQUENCH4:
1642 printf("Source Quench\n");
1643 pr_retip((struct ip *)icp->icmp_dataicmp_dun.id_data);
1644 break;
1645 case ICMP_REDIRECT5:
1646 switch(icp->icmp_code) {
1647 case ICMP_REDIRECT_NET0:
1648 printf("Redirect Network");
1649 break;
1650 case ICMP_REDIRECT_HOST1:
1651 printf("Redirect Host");
1652 break;
1653 case ICMP_REDIRECT_TOSNET2:
1654 printf("Redirect Type of Service and Network");
1655 break;
1656 case ICMP_REDIRECT_TOSHOST3:
1657 printf("Redirect Type of Service and Host");
1658 break;
1659 default:
1660 printf("Redirect, Unknown Code: %d", icp->icmp_code);
1661 break;
1662 }
1663 printf("(New addr: %s)\n",
1664 inet_ntoa(icp->icmp_gwaddricmp_hun.ih_gwaddr));
1665 pr_retip((struct ip *)icp->icmp_dataicmp_dun.id_data);
1666 break;
1667 case ICMP_ECHO8:
1668 printf("Echo Request\n");
1669 /* XXX ID + Seq + Data */
1670 break;
1671 case ICMP_ROUTERADVERT9:
1672 /* RFC1256 */
1673 printf("Router Discovery Advertisement\n");
1674 printf("(%d entries, lifetime %d seconds)\n",
1675 icp->icmp_num_addrsicmp_hun.ih_rtradv.irt_num_addrs, ntohs(icp->icmp_lifetime)(__uint16_t)(__builtin_constant_p(icp->icmp_hun.ih_rtradv.
irt_lifetime) ? (__uint16_t)(((__uint16_t)(icp->icmp_hun.ih_rtradv
.irt_lifetime) & 0xffU) << 8 | ((__uint16_t)(icp->
icmp_hun.ih_rtradv.irt_lifetime) & 0xff00U) >> 8) :
__swap16md(icp->icmp_hun.ih_rtradv.irt_lifetime))
);
1676 break;
1677 case ICMP_ROUTERSOLICIT10:
1678 /* RFC1256 */
1679 printf("Router Discovery Solicitation\n");
1680 break;
1681 case ICMP_TIMXCEED11:
1682 switch(icp->icmp_code) {
1683 case ICMP_TIMXCEED_INTRANS0:
1684 printf("Time to live exceeded\n");
1685 break;
1686 case ICMP_TIMXCEED_REASS1:
1687 printf("Frag reassembly time exceeded\n");
1688 break;
1689 default:
1690 printf("Time exceeded, Unknown Code: %d\n",
1691 icp->icmp_code);
1692 break;
1693 }
1694 pr_retip((struct ip *)icp->icmp_dataicmp_dun.id_data);
1695 break;
1696 case ICMP_PARAMPROB12:
1697 switch(icp->icmp_code) {
1698 case ICMP_PARAMPROB_OPTABSENT1:
1699 printf("Parameter problem, required option "
1700 "absent: pointer = 0x%02x\n",
1701 ntohs(icp->icmp_hun.ih_pptr)(__uint16_t)(__builtin_constant_p(icp->icmp_hun.ih_pptr) ?
(__uint16_t)(((__uint16_t)(icp->icmp_hun.ih_pptr) & 0xffU
) << 8 | ((__uint16_t)(icp->icmp_hun.ih_pptr) & 0xff00U
) >> 8) : __swap16md(icp->icmp_hun.ih_pptr))
);
1702 break;
1703 default:
1704 printf("Parameter problem: pointer = 0x%02x\n",
1705 ntohs(icp->icmp_hun.ih_pptr)(__uint16_t)(__builtin_constant_p(icp->icmp_hun.ih_pptr) ?
(__uint16_t)(((__uint16_t)(icp->icmp_hun.ih_pptr) & 0xffU
) << 8 | ((__uint16_t)(icp->icmp_hun.ih_pptr) & 0xff00U
) >> 8) : __swap16md(icp->icmp_hun.ih_pptr))
);
1706 break;
1707 }
1708 pr_retip((struct ip *)icp->icmp_dataicmp_dun.id_data);
1709 break;
1710 case ICMP_TSTAMP13:
1711 printf("Timestamp\n");
1712 /* XXX ID + Seq + 3 timestamps */
1713 break;
1714 case ICMP_TSTAMPREPLY14:
1715 printf("Timestamp Reply\n");
1716 /* XXX ID + Seq + 3 timestamps */
1717 break;
1718 case ICMP_IREQ15:
1719 printf("Information Request\n");
1720 /* XXX ID + Seq */
1721 break;
1722 case ICMP_IREQREPLY16:
1723 printf("Information Reply\n");
1724 /* XXX ID + Seq */
1725 break;
1726 case ICMP_MASKREQ17:
1727 printf("Address Mask Request\n");
1728 break;
1729 case ICMP_MASKREPLY18:
1730 printf("Address Mask Reply (Mask 0x%08x)\n",
1731 ntohl(icp->icmp_mask)(__uint32_t)(__builtin_constant_p(icp->icmp_dun.id_mask) ?
(__uint32_t)(((__uint32_t)(icp->icmp_dun.id_mask) & 0xff
) << 24 | ((__uint32_t)(icp->icmp_dun.id_mask) &
0xff00) << 8 | ((__uint32_t)(icp->icmp_dun.id_mask)
& 0xff0000) >> 8 | ((__uint32_t)(icp->icmp_dun.
id_mask) & 0xff000000) >> 24) : __swap32md(icp->
icmp_dun.id_mask))
);
1732 break;
1733 default:
1734 printf("Unknown ICMP type: %d\n", icp->icmp_type);
1735 }
1736}
1737
1738/*
1739 * pr_iph --
1740 * Print an IP header with options.
1741 */
1742void
1743pr_iph(struct ip *ip)
1744{
1745 int hlen;
1746 u_char *cp;
1747
1748 hlen = ip->ip_hl << 2;
1749 cp = (u_char *)ip + 20; /* point to options */
1750
1751 printf("Vr HL TOS Len ID Flg off TTL Pro cks Src Dst Data\n");
1752 printf(" %1x %1x %02x %04x %04x",
1753 ip->ip_v, ip->ip_hl, ip->ip_tos, ip->ip_len, ip->ip_id);
1754 printf(" %1x %04x", ((ip->ip_off) & 0xe000) >> 13,
1755 (ip->ip_off) & 0x1fff);
1756 printf(" %02x %02x %04x", ip->ip_ttl, ip->ip_p, ip->ip_sum);
1757 printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_src.s_addr));
1758 printf(" %s ", inet_ntoa(*(struct in_addr *)&ip->ip_dst.s_addr));
1759 /* dump and option bytes */
1760 while (hlen-- > 20) {
1761 printf("%02x", *cp++);
1762 }
1763 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
1764}
1765
1766/*
1767 * pr_retip --
1768 * Dump some info on a returned (via ICMP) IP packet.
1769 */
1770void
1771pr_retip(struct ip *ip)
1772{
1773 int hlen;
1774 u_char *cp;
1775
1776 pr_iph(ip);
1777 hlen = ip->ip_hl << 2;
1778 cp = (u_char *)ip + hlen;
1779
1780 if (ip->ip_p == 6)
1781 printf("TCP: from port %u, to port %u (decimal)\n",
1782 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1783 else if (ip->ip_p == 17)
1784 printf("UDP: from port %u, to port %u (decimal)\n",
1785 (*cp * 256 + *(cp + 1)), (*(cp + 2) * 256 + *(cp + 3)));
1786}
1787
1788#ifndef SMALL
1789int
1790map_tos(char *key, int *val)
1791{
1792 /* DiffServ Codepoints and other TOS mappings */
1793 const struct toskeywords {
1794 const char *keyword;
1795 int val;
1796 } *t, toskeywords[] = {
1797 { "af11", IPTOS_DSCP_AF110x28 },
1798 { "af12", IPTOS_DSCP_AF120x30 },
1799 { "af13", IPTOS_DSCP_AF130x38 },
1800 { "af21", IPTOS_DSCP_AF210x48 },
1801 { "af22", IPTOS_DSCP_AF220x50 },
1802 { "af23", IPTOS_DSCP_AF230x58 },
1803 { "af31", IPTOS_DSCP_AF310x68 },
1804 { "af32", IPTOS_DSCP_AF320x70 },
1805 { "af33", IPTOS_DSCP_AF330x78 },
1806 { "af41", IPTOS_DSCP_AF410x88 },
1807 { "af42", IPTOS_DSCP_AF420x90 },
1808 { "af43", IPTOS_DSCP_AF430x98 },
1809 { "critical", IPTOS_PREC_CRITIC_ECP0xa0 },
1810 { "cs0", IPTOS_DSCP_CS00x00 },
1811 { "cs1", IPTOS_DSCP_CS10x20 },
1812 { "cs2", IPTOS_DSCP_CS20x40 },
1813 { "cs3", IPTOS_DSCP_CS30x60 },
1814 { "cs4", IPTOS_DSCP_CS40x80 },
1815 { "cs5", IPTOS_DSCP_CS50xa0 },
1816 { "cs6", IPTOS_DSCP_CS60xc0 },
1817 { "cs7", IPTOS_DSCP_CS70xe0 },
1818 { "ef", IPTOS_DSCP_EF0xb8 },
1819 { "inetcontrol", IPTOS_PREC_INTERNETCONTROL0xc0 },
1820 { "lowdelay", IPTOS_LOWDELAY0x10 },
1821 { "netcontrol", IPTOS_PREC_NETCONTROL0xe0 },
1822 { "reliability", IPTOS_RELIABILITY0x04 },
1823 { "throughput", IPTOS_THROUGHPUT0x08 },
1824 { NULL((void *)0), -1 },
1825 };
1826
1827 for (t = toskeywords; t->keyword != NULL((void *)0); t++) {
1828 if (strcmp(key, t->keyword) == 0) {
1829 *val = t->val;
1830 return (1);
1831 }
1832 }
1833
1834 return (0);
1835}
1836#endif /* SMALL */
1837
1838void
1839pr_exthdrs(struct msghdr *mhdr)
1840{
1841 struct cmsghdr *cm;
1842
1843 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr)((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? (struct
cmsghdr *)(mhdr)->msg_control : (struct cmsghdr *)((void *
)0))
; cm;
1844 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)(((char *)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof
(long) - 1)) &~(sizeof(long) - 1)) + (((unsigned long)(sizeof
(struct cmsghdr)) + (sizeof(long) - 1)) &~(sizeof(long) -
1)) > ((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen
) ? (struct cmsghdr *)((void *)0) : (struct cmsghdr *)((char *
)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof(long) -
1)) &~(sizeof(long) - 1))))
) {
1845 if (cm->cmsg_level != IPPROTO_IPV641)
1846 continue;
1847
1848 switch (cm->cmsg_type) {
1849 case IPV6_HOPOPTS49:
1850 printf(" HbH Options: ");
1851 pr_ip6opt(CMSG_DATA(cm)((unsigned char *)(cm) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
);
1852 break;
1853 case IPV6_DSTOPTS50:
1854 case IPV6_RTHDRDSTOPTS35:
1855 printf(" Dst Options: ");
1856 pr_ip6opt(CMSG_DATA(cm)((unsigned char *)(cm) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
);
1857 break;
1858 case IPV6_RTHDR51:
1859 printf(" Routing: ");
1860 pr_rthdr(CMSG_DATA(cm)((unsigned char *)(cm) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
);
1861 break;
1862 }
1863 }
1864}
1865
1866void
1867pr_ip6opt(void *extbuf)
1868{
1869 struct ip6_hbh *ext;
1870 int currentlen;
1871 u_int8_t type;
1872 size_t extlen;
1873 socklen_t len;
1874 void *databuf;
1875 u_int16_t value2;
1876 u_int32_t value4;
1877
1878 ext = (struct ip6_hbh *)extbuf;
1879 extlen = (ext->ip6h_len + 1) * 8;
1880 printf("nxt %u, len %u (%lu bytes)\n", ext->ip6h_nxt,
1881 (unsigned int)ext->ip6h_len, (unsigned long)extlen);
1882
1883 currentlen = 0;
1884 while (1) {
1885 currentlen = inet6_opt_next(extbuf, extlen, currentlen,
1886 &type, &len, &databuf);
1887 if (currentlen == -1)
1888 break;
1889 switch (type) {
1890 /*
1891 * Note that inet6_opt_next automatically skips any padding
1892 * options.
1893 */
1894 case IP6OPT_JUMBO0xC2:
1895 inet6_opt_get_val(databuf, 0, &value4, sizeof(value4));
1896 printf(" Jumbo Payload Opt: Length %u\n",
1897 (u_int32_t)ntohl(value4)(__uint32_t)(__builtin_constant_p(value4) ? (__uint32_t)(((__uint32_t
)(value4) & 0xff) << 24 | ((__uint32_t)(value4) &
0xff00) << 8 | ((__uint32_t)(value4) & 0xff0000) >>
8 | ((__uint32_t)(value4) & 0xff000000) >> 24) : __swap32md
(value4))
);
1898 break;
1899 case IP6OPT_ROUTER_ALERT0x05:
1900 inet6_opt_get_val(databuf, 0, &value2, sizeof(value2));
1901 printf(" Router Alert Opt: Type %u\n",
1902 ntohs(value2)(__uint16_t)(__builtin_constant_p(value2) ? (__uint16_t)(((__uint16_t
)(value2) & 0xffU) << 8 | ((__uint16_t)(value2) &
0xff00U) >> 8) : __swap16md(value2))
);
1903 break;
1904 default:
1905 printf(" Received Opt %u len %lu\n",
1906 type, (unsigned long)len);
1907 break;
1908 }
1909 }
1910 return;
1911}
1912
1913void
1914pr_rthdr(void *extbuf)
1915{
1916 struct in6_addr *in6;
1917 char ntopbuf[INET6_ADDRSTRLEN46];
1918 struct ip6_rthdr *rh = (struct ip6_rthdr *)extbuf;
1919 int i, segments;
1920
1921 /* print fixed part of the header */
1922 printf("nxt %u, len %u (%d bytes), type %u, ", rh->ip6r_nxt,
1923 rh->ip6r_len, (rh->ip6r_len + 1) << 3, rh->ip6r_type);
1924 if ((segments = inet6_rth_segments(extbuf)) >= 0)
1925 printf("%d segments, ", segments);
1926 else
1927 printf("segments unknown, ");
1928 printf("%d left\n", rh->ip6r_segleft);
1929
1930 for (i = 0; i < segments; i++) {
1931 in6 = inet6_rth_getaddr(extbuf, i);
1932 if (in6 == NULL((void *)0))
1933 printf(" [%d]<NULL>\n", i);
1934 else {
1935 if (!inet_ntop(AF_INET624, in6, ntopbuf,
1936 sizeof(ntopbuf)))
1937 strncpy(ntopbuf, "?", sizeof(ntopbuf));
1938 printf(" [%d]%s\n", i, ntopbuf);
1939 }
1940 }
1941
1942 return;
1943
1944}
1945
1946int
1947get_hoplim(struct msghdr *mhdr)
1948{
1949 struct cmsghdr *cm;
1950
1951 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr)((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? (struct
cmsghdr *)(mhdr)->msg_control : (struct cmsghdr *)((void *
)0))
; cm;
1952 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)(((char *)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof
(long) - 1)) &~(sizeof(long) - 1)) + (((unsigned long)(sizeof
(struct cmsghdr)) + (sizeof(long) - 1)) &~(sizeof(long) -
1)) > ((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen
) ? (struct cmsghdr *)((void *)0) : (struct cmsghdr *)((char *
)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof(long) -
1)) &~(sizeof(long) - 1))))
) {
1953 if (cm->cmsg_len == 0)
1954 return(-1);
1955
1956 if (cm->cmsg_level == IPPROTO_IPV641 &&
1957 cm->cmsg_type == IPV6_HOPLIMIT47 &&
1958 cm->cmsg_len == CMSG_LEN(sizeof(int))((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (sizeof(int)))
)
1959 return(*(int *)CMSG_DATA(cm)((unsigned char *)(cm) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
);
1960 }
1961
1962 return(-1);
1963}
1964
1965int
1966get_pathmtu(struct msghdr *mhdr, struct sockaddr_in6 *dst)
1967{
1968 struct cmsghdr *cm;
1969 struct ip6_mtuinfo *mtuctl = NULL((void *)0);
1970
1971 for (cm = (struct cmsghdr *)CMSG_FIRSTHDR(mhdr)((mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? (struct
cmsghdr *)(mhdr)->msg_control : (struct cmsghdr *)((void *
)0))
; cm;
1972 cm = (struct cmsghdr *)CMSG_NXTHDR(mhdr, cm)(((char *)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof
(long) - 1)) &~(sizeof(long) - 1)) + (((unsigned long)(sizeof
(struct cmsghdr)) + (sizeof(long) - 1)) &~(sizeof(long) -
1)) > ((char *)(mhdr)->msg_control) + (mhdr)->msg_controllen
) ? (struct cmsghdr *)((void *)0) : (struct cmsghdr *)((char *
)(cm) + (((unsigned long)((cm)->cmsg_len) + (sizeof(long) -
1)) &~(sizeof(long) - 1))))
) {
1973 if (cm->cmsg_len == 0)
1974 return(0);
1975
1976 if (cm->cmsg_level == IPPROTO_IPV641 &&
1977 cm->cmsg_type == IPV6_PATHMTU44 &&
1978 cm->cmsg_len == CMSG_LEN(sizeof(struct ip6_mtuinfo))((((unsigned long)(sizeof(struct cmsghdr)) + (sizeof(long) - 1
)) &~(sizeof(long) - 1)) + (sizeof(struct ip6_mtuinfo)))
) {
1979 mtuctl = (struct ip6_mtuinfo *)CMSG_DATA(cm)((unsigned char *)(cm) + (((unsigned long)(sizeof(struct cmsghdr
)) + (sizeof(long) - 1)) &~(sizeof(long) - 1)))
;
1980
1981 /*
1982 * If the notified destination is different from
1983 * the one we are pinging, just ignore the info.
1984 * We check the scope ID only when both notified value
1985 * and our own value have non-0 values, because we may
1986 * have used the default scope zone ID for sending,
1987 * in which case the scope ID value is 0.
1988 */
1989 if (!IN6_ARE_ADDR_EQUAL(&mtuctl->ip6m_addr.sin6_addr,(memcmp(&(&mtuctl->ip6m_addr.sin6_addr)->__u6_addr
.__u6_addr8[0], &(&dst->sin6_addr)->__u6_addr.__u6_addr8
[0], sizeof(struct in6_addr)) == 0)
1990 &dst->sin6_addr)(memcmp(&(&mtuctl->ip6m_addr.sin6_addr)->__u6_addr
.__u6_addr8[0], &(&dst->sin6_addr)->__u6_addr.__u6_addr8
[0], sizeof(struct in6_addr)) == 0)
||
1991 (mtuctl->ip6m_addr.sin6_scope_id &&
1992 dst->sin6_scope_id &&
1993 mtuctl->ip6m_addr.sin6_scope_id !=
1994 dst->sin6_scope_id)) {
1995 if (options & F_VERBOSE0x0100) {
1996 printf("path MTU for %s is notified. "
1997 "(ignored)\n",
1998 pr_addr((struct sockaddr *)
1999 &mtuctl->ip6m_addr,
2000 sizeof(mtuctl->ip6m_addr)));
2001 }
2002 return(0);
2003 }
2004
2005 /*
2006 * Ignore an invalid MTU. XXX: can we just believe
2007 * the kernel check?
2008 */
2009 if (mtuctl->ip6m_mtu < IPV6_MMTU1280)
2010 return(0);
2011
2012 /* notification for our destination. return the MTU. */
2013 return((int)mtuctl->ip6m_mtu);
2014 }
2015 }
2016 return(0);
2017}
2018
2019/*
2020 * pr_icmph6 --
2021 * Print a descriptive string about an ICMP header.
2022 */
2023void
2024pr_icmph6(struct icmp6_hdr *icp, u_char *end)
2025{
2026 char ntop_buf[INET6_ADDRSTRLEN46];
2027 struct nd_redirect *red;
2028
2029 switch (icp->icmp6_type) {
2030 case ICMP6_DST_UNREACH1:
2031 switch (icp->icmp6_code) {
2032 case ICMP6_DST_UNREACH_NOROUTE0:
2033 printf("No Route to Destination\n");
2034 break;
2035 case ICMP6_DST_UNREACH_ADMIN1:
2036 printf("Destination Administratively "
2037 "Unreachable\n");
2038 break;
2039 case ICMP6_DST_UNREACH_BEYONDSCOPE2:
2040 printf("Destination Unreachable Beyond Scope\n");
2041 break;
2042 case ICMP6_DST_UNREACH_ADDR3:
2043 printf("Destination Host Unreachable\n");
2044 break;
2045 case ICMP6_DST_UNREACH_NOPORT4:
2046 printf("Destination Port Unreachable\n");
2047 break;
2048 default:
2049 printf("Destination Unreachable, Bad Code: %d\n",
2050 icp->icmp6_code);
2051 break;
2052 }
2053 /* Print returned IP header information */
2054 pr_retip6((struct ip6_hdr *)(icp + 1), end);
2055 break;
2056 case ICMP6_PACKET_TOO_BIG2:
2057 printf("Packet too big mtu = %d\n",
2058 (int)ntohl(icp->icmp6_mtu)(__uint32_t)(__builtin_constant_p(icp->icmp6_dataun.icmp6_un_data32
[0]) ? (__uint32_t)(((__uint32_t)(icp->icmp6_dataun.icmp6_un_data32
[0]) & 0xff) << 24 | ((__uint32_t)(icp->icmp6_dataun
.icmp6_un_data32[0]) & 0xff00) << 8 | ((__uint32_t)
(icp->icmp6_dataun.icmp6_un_data32[0]) & 0xff0000) >>
8 | ((__uint32_t)(icp->icmp6_dataun.icmp6_un_data32[0]) &
0xff000000) >> 24) : __swap32md(icp->icmp6_dataun.icmp6_un_data32
[0]))
);
2059 pr_retip6((struct ip6_hdr *)(icp + 1), end);
2060 break;
2061 case ICMP6_TIME_EXCEEDED3:
2062 switch (icp->icmp6_code) {
2063 case ICMP6_TIME_EXCEED_TRANSIT0:
2064 printf("Time to live exceeded\n");
2065 break;
2066 case ICMP6_TIME_EXCEED_REASSEMBLY1:
2067 printf("Frag reassembly time exceeded\n");
2068 break;
2069 default:
2070 printf("Time exceeded, Bad Code: %d\n",
2071 icp->icmp6_code);
2072 break;
2073 }
2074 pr_retip6((struct ip6_hdr *)(icp + 1), end);
2075 break;
2076 case ICMP6_PARAM_PROB4:
2077 printf("Parameter problem: ");
2078 switch (icp->icmp6_code) {
2079 case ICMP6_PARAMPROB_HEADER0:
2080 printf("Erroneous Header ");
2081 break;
2082 case ICMP6_PARAMPROB_NEXTHEADER1:
2083 printf("Unknown Nextheader ");
2084 break;
2085 case ICMP6_PARAMPROB_OPTION2:
2086 printf("Unrecognized Option ");
2087 break;
2088 default:
2089 printf("Bad code(%d) ", icp->icmp6_code);
2090 break;
2091 }
2092 printf("pointer = 0x%02x\n",
2093 (u_int32_t)ntohl(icp->icmp6_pptr)(__uint32_t)(__builtin_constant_p(icp->icmp6_dataun.icmp6_un_data32
[0]) ? (__uint32_t)(((__uint32_t)(icp->icmp6_dataun.icmp6_un_data32
[0]) & 0xff) << 24 | ((__uint32_t)(icp->icmp6_dataun
.icmp6_un_data32[0]) & 0xff00) << 8 | ((__uint32_t)
(icp->icmp6_dataun.icmp6_un_data32[0]) & 0xff0000) >>
8 | ((__uint32_t)(icp->icmp6_dataun.icmp6_un_data32[0]) &
0xff000000) >> 24) : __swap32md(icp->icmp6_dataun.icmp6_un_data32
[0]))
);
2094 pr_retip6((struct ip6_hdr *)(icp + 1), end);
2095 break;
2096 case ICMP6_ECHO_REQUEST128:
2097 printf("Echo Request");
2098 /* XXX ID + Seq + Data */
2099 break;
2100 case ICMP6_ECHO_REPLY129:
2101 printf("Echo Reply");
2102 /* XXX ID + Seq + Data */
2103 break;
2104 case ICMP6_MEMBERSHIP_QUERY130:
2105 printf("Listener Query");
2106 break;
2107 case ICMP6_MEMBERSHIP_REPORT131:
2108 printf("Listener Report");
2109 break;
2110 case ICMP6_MEMBERSHIP_REDUCTION132:
2111 printf("Listener Done");
2112 break;
2113 case ND_ROUTER_SOLICIT133:
2114 printf("Router Solicitation");
2115 break;
2116 case ND_ROUTER_ADVERT134:
2117 printf("Router Advertisement");
2118 break;
2119 case ND_NEIGHBOR_SOLICIT135:
2120 printf("Neighbor Solicitation");
2121 break;
2122 case ND_NEIGHBOR_ADVERT136:
2123 printf("Neighbor Advertisement");
2124 break;
2125 case ND_REDIRECT137:
2126 red = (struct nd_redirect *)icp;
2127 printf("Redirect\n");
2128 if (!inet_ntop(AF_INET624, &red->nd_rd_dst, ntop_buf,
2129 sizeof(ntop_buf)))
2130 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2131 printf("Destination: %s", ntop_buf);
2132 if (!inet_ntop(AF_INET624, &red->nd_rd_target, ntop_buf,
2133 sizeof(ntop_buf)))
2134 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2135 printf(" New Target: %s", ntop_buf);
2136 break;
2137 default:
2138 printf("Bad ICMP type: %d", icp->icmp6_type);
2139 }
2140}
2141
2142/*
2143 * pr_iph6 --
2144 * Print an IP6 header.
2145 */
2146void
2147pr_iph6(struct ip6_hdr *ip6)
2148{
2149 u_int32_t flow = ip6->ip6_flowip6_ctlun.ip6_un1.ip6_un1_flow & IPV6_FLOWLABEL_MASK0xffff0f00;
2150 u_int8_t tc;
2151 char ntop_buf[INET6_ADDRSTRLEN46];
2152
2153 tc = *(&ip6->ip6_vfcip6_ctlun.ip6_un2_vfc + 1); /* XXX */
2154 tc = (tc >> 4) & 0x0f;
2155 tc |= (ip6->ip6_vfcip6_ctlun.ip6_un2_vfc << 4);
2156
2157 printf("Vr TC Flow Plen Nxt Hlim\n");
2158 printf(" %1x %02x %05x %04x %02x %02x\n",
2159 (ip6->ip6_vfcip6_ctlun.ip6_un2_vfc & IPV6_VERSION_MASK0xf0) >> 4, tc, (u_int32_t)ntohl(flow)(__uint32_t)(__builtin_constant_p(flow) ? (__uint32_t)(((__uint32_t
)(flow) & 0xff) << 24 | ((__uint32_t)(flow) & 0xff00
) << 8 | ((__uint32_t)(flow) & 0xff0000) >> 8
| ((__uint32_t)(flow) & 0xff000000) >> 24) : __swap32md
(flow))
,
2160 ntohs(ip6->ip6_plen)(__uint16_t)(__builtin_constant_p(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen
) ? (__uint16_t)(((__uint16_t)(ip6->ip6_ctlun.ip6_un1.ip6_un1_plen
) & 0xffU) << 8 | ((__uint16_t)(ip6->ip6_ctlun.ip6_un1
.ip6_un1_plen) & 0xff00U) >> 8) : __swap16md(ip6->
ip6_ctlun.ip6_un1.ip6_un1_plen))
, ip6->ip6_nxtip6_ctlun.ip6_un1.ip6_un1_nxt, ip6->ip6_hlimip6_ctlun.ip6_un1.ip6_un1_hlim);
2161 if (!inet_ntop(AF_INET624, &ip6->ip6_src, ntop_buf, sizeof(ntop_buf)))
2162 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2163 printf("%s->", ntop_buf);
2164 if (!inet_ntop(AF_INET624, &ip6->ip6_dst, ntop_buf, sizeof(ntop_buf)))
2165 strncpy(ntop_buf, "?", sizeof(ntop_buf));
2166 printf("%s\n", ntop_buf);
2167}
2168
2169/*
2170 * pr_retip6 --
2171 * Dump some info on a returned (via ICMPv6) IPv6 packet.
2172 */
2173void
2174pr_retip6(struct ip6_hdr *ip6, u_char *end)
2175{
2176 u_char *cp = (u_char *)ip6, nh;
2177 int hlen;
2178
2179 if (end - (u_char *)ip6 < sizeof(*ip6)) {
2180 printf("IP6");
2181 goto trunc;
2182 }
2183 pr_iph6(ip6);
2184 hlen = sizeof(*ip6);
2185
2186 nh = ip6->ip6_nxtip6_ctlun.ip6_un1.ip6_un1_nxt;
2187 cp += hlen;
2188 while (end - cp >= 8) {
2189 switch (nh) {
2190 case IPPROTO_HOPOPTS0:
2191 printf("HBH ");
2192 hlen = (((struct ip6_hbh *)cp)->ip6h_len+1) << 3;
2193 nh = ((struct ip6_hbh *)cp)->ip6h_nxt;
2194 break;
2195 case IPPROTO_DSTOPTS60:
2196 printf("DSTOPT ");
2197 hlen = (((struct ip6_dest *)cp)->ip6d_len+1) << 3;
2198 nh = ((struct ip6_dest *)cp)->ip6d_nxt;
2199 break;
2200 case IPPROTO_FRAGMENT44:
2201 printf("FRAG ");
2202 hlen = sizeof(struct ip6_frag);
2203 nh = ((struct ip6_frag *)cp)->ip6f_nxt;
2204 break;
2205 case IPPROTO_ROUTING43:
2206 printf("RTHDR ");
2207 hlen = (((struct ip6_rthdr *)cp)->ip6r_len+1) << 3;
2208 nh = ((struct ip6_rthdr *)cp)->ip6r_nxt;
2209 break;
2210 case IPPROTO_AH51:
2211 printf("AH ");
2212 hlen = (((struct ah *)cp)->ah_hl+2) << 2;
2213 nh = ((struct ah *)cp)->ah_nh;
2214 break;
2215 case IPPROTO_ICMPV658:
2216 printf("ICMP6: type = %d, code = %d\n",
2217 *cp, *(cp + 1));
2218 return;
2219 case IPPROTO_ESP50:
2220 printf("ESP\n");
2221 return;
2222 case IPPROTO_TCP6:
2223 printf("TCP: from port %u, to port %u (decimal)\n",
2224 (*cp * 256 + *(cp + 1)),
2225 (*(cp + 2) * 256 + *(cp + 3)));
2226 return;
2227 case IPPROTO_UDP17:
2228 printf("UDP: from port %u, to port %u (decimal)\n",
2229 (*cp * 256 + *(cp + 1)),
2230 (*(cp + 2) * 256 + *(cp + 3)));
2231 return;
2232 default:
2233 printf("Unknown Header(%d)\n", nh);
2234 return;
2235 }
2236
2237 if ((cp += hlen) >= end)
2238 goto trunc;
2239 }
2240 if (end - cp < 8)
2241 goto trunc;
2242
2243 putchar('\n')(!__isthreaded ? __sputc('\n', (&__sF[1])) : (putc)('\n',
(&__sF[1])))
;
2244 return;
2245
2246 trunc:
2247 printf("...\n");
2248 return;
2249}
2250
2251__dead__attribute__((__noreturn__)) void
2252usage(void)
2253{
2254 if (v6flag) {
2255 fprintf(stderr(&__sF[2]),
2256 "usage: ping6 [-DdEefgHLmnqv] [-c count] [-h hoplimit] "
2257 "[-I sourceaddr]\n\t[-i interval] [-l preload] "
2258 "[-p pattern] [-s packetsize] [-T toskeyword]\n"
2259 "\t[-V rtable] [-w maxwait] host\n");
2260 } else {
2261 fprintf(stderr(&__sF[2]),
2262 "usage: ping [-DdEefgHLnqRv] [-c count] [-I sourceaddr] "
2263 "[-i interval]\n\t[-l preload] [-p pattern] [-s packetsize]"
2264#ifndef SMALL
2265 " [-T toskeyword]"
2266#endif /* SMALL */
2267 "\n\t[-t ttl] [-V rtable] [-w maxwait] host\n");
2268 }
2269 exit(1);
2270}