Debian Codegraph @

ping/ping.h

raw
1 #ifndef IPUTILS_PING_H
2 #define IPUTILS_PING_H
3
4 #define _GNU_SOURCE
5
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <signal.h>
11 #include <poll.h>
12 #include <sys/param.h>
13 #include <sys/socket.h>
14 #include <linux/types.h>
15 #include <linux/sockios.h>
16 #include <sys/file.h>
17 #include <sys/time.h>
18 #include <sys/ioctl.h>
19 #include <net/if.h>
20 #include <sys/uio.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <netdb.h>
25 #include <setjmp.h>
26 #include <asm/byteorder.h>
27 #include <sched.h>
28 #include <math.h>
29 #include <netinet/ip.h>
30 #include <netinet/ip6.h>
31 #include <netinet/ip_icmp.h>
32 #include <netinet/icmp6.h>
33 #include <linux/filter.h>
34 #include <resolv.h>
35
36 #ifdef HAVE_LIBCAP
37 # include <sys/prctl.h>
38 # include <sys/capability.h>
39 #endif
40
41 #include "iputils_common.h"
42 #include "iputils_ni.h"
43
44 #ifdef USE_IDN
45 # define getaddrinfo_flags (AI_CANONNAME | AI_IDN | AI_CANONIDN)
46 # define getnameinfo_flags NI_IDN
47 #else
48 # define getaddrinfo_flags (AI_CANONNAME)
49 # define getnameinfo_flags 0
50 #endif
51
52 #include <ifaddrs.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <linux/types.h>
56 #include <linux/errqueue.h>
57 #include <linux/in6.h>
58
59 #ifndef SCOPE_DELIMITER
60 # define SCOPE_DELIMITER '%'
61 #endif
62
63 #define DEFDATALEN (64 - 8) /* default data length */
64
65 #define MAXWAIT 10 /* max seconds to wait for response */
66 #define MIN_INTERVAL_MS 10 /* Minimal interpacket gap */
67 #define MIN_USER_INTERVAL_MS 2 /* Minimal allowed interval for non-root for single host ping */
68 #define MIN_MULTICAST_USER_INTERVAL_MS 1000 /* Minimal allowed interval for non-root for broadcast/multicast ping */
69 #define IDENTIFIER_MAX 0xFFFF /* max unsigned 2-byte value */
70
71 #define SCHINT(a) (((a) <= MIN_INTERVAL_MS) ? MIN_INTERVAL_MS : (a))
72
73
74 #ifndef MSG_CONFIRM
75 #define MSG_CONFIRM 0
76 #endif
77
78 /* RFC 4443 addition not yet available in libc headers */
79 #ifndef ICMP6_DST_UNREACH_POLICYFAIL
80 #define ICMP6_DST_UNREACH_POLICYFAIL 5
81 #endif
82
83 /* RFC 4443 addition not yet available in libc headers */
84 #ifndef ICMP6_DST_UNREACH_REJECTROUTE
85 #define ICMP6_DST_UNREACH_REJECTROUTE 6
86 #endif
87
88 /*
89 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
90 * number of received sequence numbers we can keep track of.
91 */
92 #define MAX_DUP_CHK 0x10000
93
94 #if defined(__WORDSIZE) && __WORDSIZE == 64
95 # define USE_BITMAP64
96 #endif
97
98 #ifdef USE_BITMAP64
99 typedef uint64_t bitmap_t;
100 # define BITMAP_SHIFT 6
101 #else
102 typedef uint32_t bitmap_t;
103 # define BITMAP_SHIFT 5
104 #endif
105
106 #if ((MAX_DUP_CHK >> (BITMAP_SHIFT + 3)) << (BITMAP_SHIFT + 3)) != MAX_DUP_CHK
107 # error Please MAX_DUP_CHK and/or BITMAP_SHIFT
108 #endif
109
110 /* IPv4 packet size / IPv6 payload size */
111 #define MAXPACKET 65535
112
113 struct rcvd_table {
114 bitmap_t bitmap[MAX_DUP_CHK / (sizeof(bitmap_t) * 8)];
115 };
116
117 typedef struct socket_st {
118 int fd;
119 int socktype;
120 } socket_st;
121
122 struct ping_rts;
123
124 int ping4_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai, socket_st *sock);
125 int ping4_send_probe(struct ping_rts *rts, socket_st *, void *packet, unsigned packet_size);
126 int ping4_receive_error_msg(struct ping_rts *, socket_st *);
127 int ping4_parse_reply(struct ping_rts *, socket_st *, struct msghdr *msg, int cc, void *addr, struct timeval *);
128 void ping4_install_filter(struct ping_rts *rts, socket_st *);
129
130 typedef struct ping_func_set_st {
131 int (*send_probe)(struct ping_rts *rts, socket_st *, void *packet, unsigned packet_size);
132 int (*receive_error_msg)(struct ping_rts *rts, socket_st *sock);
133 int (*parse_reply)(struct ping_rts *rts, socket_st *, struct msghdr *msg, int len, void *addr, struct timeval *);
134 void (*install_filter)(struct ping_rts *rts, socket_st *);
135 } ping_func_set_st;
136
137 /* Node Information query */
138 struct ping_ni {
139 int query;
140 int flag;
141 void *subject;
142 int subject_len;
143 int subject_type;
144 char *group;
145 #if PING6_NONCE_MEMORY
146 uint8_t *nonce_ptr;
147 #else
148 struct {
149 struct timeval tv;
150 pid_t pid;
151 } nonce_secret;
152 #endif
153 };
154
155 /*ping runtime state */
156 struct ping_rts {
157 unsigned int mark;
158 unsigned char outpack[MAXPACKET];
159
160 struct rcvd_table rcvd_tbl;
161
162 int datalen;
163 char *hostname;
164 uid_t uid;
165 int ident; /* process id to identify our packets */
166
167 int sndbuf;
168 int ttl;
169
170 long npackets; /* max packets to transmit */
171 long nreceived; /* # of packets we got back */
172 long nrepeats; /* number of duplicates */
173 long ntransmitted; /* sequence # for outbound packets = #sent */
174 long nchecksum; /* replies with bad checksum */
175 long nerrors; /* icmp errors */
176 int interval; /* interval between packets (msec) */
177 int preload;
178 int deadline; /* time to die */
179 uint32_t lingertime;
180 struct timespec start_time, cur_time;
181 volatile int exiting;
182 volatile int status_snapshot;
183 int confirm;
184 int confirm_flag;
185 char *device;
186 int pmtudisc;
187
188 volatile int in_pr_addr; /* pr_addr() is executing */
189 jmp_buf pr_addr_jmp;
190
191 /* timing */
192 int timing; /* flag to do timing */
193 long tmin; /* minimum round trip time */
194 long tmax; /* maximum round trip time */
195 double tsum; /* sum of all times, for doing average */
196 double tsum2;
197 uint64_t rtt; /* Exponential weight moving average calculated in fixed point */
198 int rtt_addend;
199 uint16_t acked;
200 int pipesize;
201
202 ping_func_set_st ping4_func_set;
203 ping_func_set_st ping6_func_set;
204 uint32_t tclass;
205 uint32_t flowlabel;
206 struct sockaddr_in6 source6;
207 struct sockaddr_in6 whereto6;
208 struct sockaddr_in6 firsthop6;
209 int multicast;
210
211 /* Used only in ping.c */
212 int ts_type;
213 int nroute;
214 uint32_t route[10];
215 struct sockaddr_in whereto; /* who to ping */
216 int optlen;
217 int settos; /* Set TOS, Precedence or other QOS options */
218 int broadcast_pings;
219 struct sockaddr_in source;
220
221 /* Used only in ping_common.c */
222 int screen_width;
223 #ifdef HAVE_LIBCAP
224 cap_value_t cap_raw;
225 cap_value_t cap_admin;
226 #endif
227
228 /* Used only in ping6_common.c */
229 int subnet_router_anycast; /* Subnet-Router anycast (RFC 4291) */
230 struct sockaddr_in6 firsthop;
231 unsigned char cmsgbuf[4096];
232 size_t cmsglen;
233 struct ping_ni ni;
234
235 /* boolean option bits */
236 unsigned int
237 opt_adaptive:1,
238 opt_audible:1,
239 opt_connect_sk:1,
240 opt_flood:1,
241 opt_flood_poll:1,
242 opt_flowinfo:1,
243 opt_force_lookup:1,
244 opt_interval:1,
245 opt_latency:1,
246 opt_mark:1,
247 opt_noloop:1,
248 opt_numeric:1,
249 opt_outstanding:1,
250 opt_pingfilled:1,
251 opt_ptimeofday:1,
252 opt_rtt_precision:1,
253 opt_quiet:1,
254 opt_rroute:1,
255 opt_so_debug:1,
256 opt_so_dontroute:1,
257 opt_sourceroute:1,
258 opt_strictsource:1,
259 opt_timestamp:1,
260 opt_ttl:1,
261 opt_verbose:1;
262 };
263 /* FIXME: global_rts will be removed in future */
264 extern struct ping_rts *global_rts;
265
266 #define A(bit) (rts->rcvd_tbl.bitmap[(bit) >> BITMAP_SHIFT]) /* identify word in array */
267 #define B(bit) (((bitmap_t)1) << ((bit) & ((1 << BITMAP_SHIFT) - 1))) /* identify bit in word */
268
269 static inline void rcvd_set(struct ping_rts *rts, uint16_t seq)
270 {
271 unsigned bit = seq % MAX_DUP_CHK;
272 A(bit) |= B(bit);
273 }
274
275 static inline void rcvd_clear(struct ping_rts *rts, uint16_t seq)
276 {
277 unsigned bit = seq % MAX_DUP_CHK;
278 A(bit) &= ~B(bit);
279 }
280
281 static inline bitmap_t rcvd_test(struct ping_rts *rts, uint16_t seq)
282 {
283 unsigned bit = seq % MAX_DUP_CHK;
284 return A(bit) & B(bit);
285 }
286
287 /*
288 * Write to stdout
289 */
290 static inline void write_stdout(const char *str, size_t len)
291 {
292 size_t o = 0;
293 ssize_t cc;
294 do {
295 cc = write(STDOUT_FILENO, str + o, len - o);
296
297 if (cc < 0)
298 break;
299
300 o += (size_t) cc;
301 } while (len > o);
302 }
303
304 /*
305 * tvsub --
306 * Subtract 2 timeval structs: out = out - in. Out is assumed to
307 * be >= in.
308 */
309 static inline void tvsub(struct timeval *out, struct timeval *in)
310 {
311 if ((out->tv_usec -= in->tv_usec) < 0) {
312 --out->tv_sec;
313 out->tv_usec += 1000000;
314 }
315 out->tv_sec -= in->tv_sec;
316 }
317
318 /*
319 * tssub --
320 * Subtract 2 timespec structs: out = out - in. Out is assumed to
321 * be >= in.
322 */
323 static inline void tssub(struct timespec *out, struct timespec *in)
324 {
325 if ((out->tv_nsec -= in->tv_nsec) < 0) {
326 --out->tv_sec;
327 out->tv_nsec += 1000000000;
328 }
329 out->tv_sec -= in->tv_sec;
330 }
331
332 static inline void set_signal(int signo, void (*handler)(int))
333 {
334 struct sigaction sa;
335
336 memset(&sa, 0, sizeof(sa));
337
338 sa.sa_handler = (void (*)(int))handler;
339 sa.sa_flags = SA_RESTART;
340 sigaction(signo, &sa, NULL);
341 }
342
343 extern int __schedule_exit(int next);
344
345 static inline int schedule_exit(struct ping_rts *rts, int next)
346 {
347 if (rts->npackets && rts->ntransmitted >= rts->npackets && !rts->deadline)
348 next = __schedule_exit(next);
349 return next;
350 }
351
352 static inline int in_flight(struct ping_rts *rts)
353 {
354 uint16_t diff = (uint16_t)rts->ntransmitted - rts->acked;
355 return (diff <= 0x7FFF) ? diff : rts->ntransmitted - rts->nreceived - rts->nerrors;
356 }
357
358 static inline void acknowledge(struct ping_rts *rts, uint16_t seq)
359 {
360 uint16_t diff = (uint16_t)rts->ntransmitted - seq;
361 if (diff <= 0x7FFF) {
362 if ((int)diff + 1 > rts->pipesize)
363 rts->pipesize = (int)diff + 1;
364 if ((int16_t)(seq - rts->acked) > 0 ||
365 (uint16_t)rts->ntransmitted - rts->acked > 0x7FFF)
366 rts->acked = seq;
367 }
368 }
369
370 static inline void advance_ntransmitted(struct ping_rts *rts)
371 {
372 rts->ntransmitted++;
373 /* Invalidate acked, if 16 bit seq overflows. */
374 if ((uint16_t)rts->ntransmitted - rts->acked > 0x7FFF)
375 rts->acked = (uint16_t)rts->ntransmitted + 1;
376 }
377
378 extern void usage(void) __attribute__((noreturn));
379 extern void limit_capabilities(struct ping_rts *rts);
380 static int enable_capability_raw(void);
381 static int disable_capability_raw(void);
382 static int enable_capability_admin(void);
383 static int disable_capability_admin(void);
384 #ifdef HAVE_LIBCAP
385 extern int modify_capability(cap_value_t, cap_flag_value_t);
386 static inline int enable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_SET); }
387 static inline int disable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_CLEAR); }
388 static inline int enable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_SET); }
389 static inline int disable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_CLEAR); }
390 #else
391 extern int modify_capability(int);
392 static inline int enable_capability_raw(void) { return modify_capability(1); }
393 static inline int disable_capability_raw(void) { return modify_capability(0); }
394 static inline int enable_capability_admin(void) { return modify_capability(1); }
395 static inline int disable_capability_admin(void) { return modify_capability(0); }
396 #endif
397 extern void drop_capabilities(void);
398
399 char *pr_addr(struct ping_rts *rts, void *sa, socklen_t salen);
400 char *pr_raw_addr(struct ping_rts *rts, void *sa, socklen_t salen);
401 char *str_interval(int interval);
402
403 int is_ours(struct ping_rts *rts, socket_st *sock, uint16_t id);
404 extern int pinger(struct ping_rts *rts, ping_func_set_st *fset, socket_st *sock);
405 extern void sock_setbufs(struct ping_rts *rts, socket_st *, int alloc);
406 extern void sock_setmark(struct ping_rts *rts, int fd);
407 extern void setup(struct ping_rts *rts, socket_st *);
408 extern int main_loop(struct ping_rts *rts, ping_func_set_st *fset, socket_st*,
409 uint8_t *packet, int packlen);
410 extern int finish(struct ping_rts *rts);
411 extern void status(struct ping_rts *rts);
412 extern void common_options(int ch);
413 extern int gather_statistics(struct ping_rts *rts, uint8_t *icmph, int icmplen,
414 int cc, uint16_t seq, int hops,
415 int csfailed, struct timeval *tv, char *from,
416 void (*pr_reply)(uint8_t *ptr, int cc), int multicast,
417 int wrong_source);
418 extern void print_timestamp(struct ping_rts *rts);
419 void fill(struct ping_rts *rts, char *patp, unsigned char *packet, unsigned packet_size);
420
421 /* IPv6 */
422
423 int ping6_run(struct ping_rts *rts, int argc, char **argv, struct addrinfo *ai,
424 socket_st *sock);
425 void ping6_usage(unsigned from_ping);
426
427 int ping6_send_probe(struct ping_rts *rts, socket_st *sockets, void *packet, unsigned packet_size);
428 int ping6_receive_error_msg(struct ping_rts *rts, socket_st *sockets);
429 int ping6_parse_reply(struct ping_rts *rts, socket_st *, struct msghdr *msg, int cc, void *addr, struct timeval *);
430 void ping6_install_filter(struct ping_rts *rts, socket_st *sockets);
431 int ntohsp(uint16_t *p);
432
433 /* IPv6 node information query */
434
435 int niquery_is_enabled(struct ping_ni *ni);
436 void niquery_init_nonce(struct ping_ni *ni);
437 int niquery_option_handler(struct ping_ni *ni, const char *opt_arg);
438 int niquery_is_subject_valid(struct ping_ni *ni);
439 int niquery_check_nonce(struct ping_ni *ni, uint8_t *nonce);
440 void niquery_fill_nonce(struct ping_ni *ni, uint16_t seq, uint8_t *nonce);
441
442 #define NI_NONCE_SIZE 8
443
444 struct ni_hdr {
445 struct icmp6_hdr ni_u;
446 uint8_t ni_nonce[NI_NONCE_SIZE];
447 };
448
449 #define ni_type ni_u.icmp6_type
450 #define ni_code ni_u.icmp6_code
451 #define ni_cksum ni_u.icmp6_cksum
452 #define ni_qtype ni_u.icmp6_data16[0]
453 #define ni_flags ni_u.icmp6_data16[1]
454
455 #endif /* IPUTILS_PING_H */