blob: 7158555369cab469c07788fbd4fe814980f4a50d [file] [log] [blame]
Lorenzo Colitti313379e2013-07-11 01:07:11 +09001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <time.h>
5#include <sys/param.h>
6#include <sys/socket.h>
7#include <linux/sockios.h>
8#include <sys/file.h>
9#include <sys/time.h>
10#include <sys/signal.h>
11#include <sys/ioctl.h>
12#include <net/if.h>
13#include <sys/uio.h>
14#include <sys/poll.h>
15#include <ctype.h>
16#include <errno.h>
17#include <string.h>
18#include <netdb.h>
19#include <setjmp.h>
20
21#ifdef CAPABILITIES
22#include <sys/prctl.h>
23#include <sys/capability.h>
24#endif
25
26#ifdef USE_IDN
27#include <locale.h>
28#include <idna.h>
29#endif
30
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <linux/types.h>
34#include <linux/errqueue.h>
35
Lorenzo Colitti5df1daf2013-07-09 17:28:09 +090036#ifdef ANDROID
37#include <linux/icmp.h>
Lorenzo Colitti924a10d2013-07-09 23:43:41 +090038#include <sys/auxv.h>
Lorenzo Colitti5df1daf2013-07-09 17:28:09 +090039#endif
40
Lorenzo Colitti313379e2013-07-11 01:07:11 +090041#include "SNAPSHOT.h"
42
43#define DEFDATALEN (64 - 8) /* default data length */
44
45#define MAXWAIT 10 /* max seconds to wait for response */
46#define MININTERVAL 10 /* Minimal interpacket gap */
47#define MINUSERINTERVAL 200 /* Minimal allowed interval for non-root */
48
49#define SCHINT(a) (((a) <= MININTERVAL) ? MININTERVAL : (a))
50
51/* various options */
52extern int options;
53#define F_FLOOD 0x001
54#define F_INTERVAL 0x002
55#define F_NUMERIC 0x004
56#define F_PINGFILLED 0x008
57#define F_QUIET 0x010
58#define F_RROUTE 0x020
59#define F_SO_DEBUG 0x040
60#define F_SO_DONTROUTE 0x080
61#define F_VERBOSE 0x100
62#define F_TIMESTAMP 0x200
63#define F_FLOWINFO 0x200
64#define F_SOURCEROUTE 0x400
65#define F_TCLASS 0x400
66#define F_FLOOD_POLL 0x800
67#define F_LATENCY 0x1000
68#define F_AUDIBLE 0x2000
69#define F_ADAPTIVE 0x4000
70#define F_STRICTSOURCE 0x8000
71#define F_NOLOOP 0x10000
72#define F_TTL 0x20000
73#define F_MARK 0x40000
74#define F_PTIMEOFDAY 0x80000
75#define F_OUTSTANDING 0x100000
76
77/*
78 * MAX_DUP_CHK is the number of bits in received table, i.e. the maximum
79 * number of received sequence numbers we can keep track of.
80 */
81#define MAX_DUP_CHK 0x10000
82
83#if defined(__WORDSIZE) && __WORDSIZE == 64
84# define USE_BITMAP64
85#endif
86
87#ifdef USE_BITMAP64
88typedef __u64 bitmap_t;
89# define BITMAP_SHIFT 6
90#else
91typedef __u32 bitmap_t;
92# define BITMAP_SHIFT 5
93#endif
94
95#if ((MAX_DUP_CHK >> (BITMAP_SHIFT + 3)) << (BITMAP_SHIFT + 3)) != MAX_DUP_CHK
96# error Please MAX_DUP_CHK and/or BITMAP_SHIFT
97#endif
98
99struct rcvd_table {
100 bitmap_t bitmap[MAX_DUP_CHK / (sizeof(bitmap_t) * 8)];
101};
102
103extern struct rcvd_table rcvd_tbl;
Lorenzo Colitti7618e812013-07-09 22:54:28 +0900104extern int using_ping_socket;
Lorenzo Colitti313379e2013-07-11 01:07:11 +0900105
106#define A(bit) (rcvd_tbl.bitmap[(bit) >> BITMAP_SHIFT]) /* identify word in array */
107#define B(bit) (((bitmap_t)1) << ((bit) & ((1 << BITMAP_SHIFT) - 1))) /* identify bit in word */
108
109static inline void rcvd_set(__u16 seq)
110{
111 unsigned bit = seq % MAX_DUP_CHK;
112 A(bit) |= B(bit);
113}
114
115static inline void rcvd_clear(__u16 seq)
116{
117 unsigned bit = seq % MAX_DUP_CHK;
118 A(bit) &= ~B(bit);
119}
120
121static inline bitmap_t rcvd_test(__u16 seq)
122{
123 unsigned bit = seq % MAX_DUP_CHK;
124 return A(bit) & B(bit);
125}
126
127extern u_char outpack[];
128extern int maxpacket;
129
130extern int datalen;
131extern char *hostname;
132extern int uid;
133extern int ident; /* process id to identify our packets */
134
135extern int sndbuf;
136extern int ttl;
137
138extern long npackets; /* max packets to transmit */
139extern long nreceived; /* # of packets we got back */
140extern long nrepeats; /* number of duplicates */
141extern long ntransmitted; /* sequence # for outbound packets = #sent */
142extern long nchecksum; /* replies with bad checksum */
143extern long nerrors; /* icmp errors */
144extern int interval; /* interval between packets (msec) */
145extern int preload;
146extern int deadline; /* time to die */
147extern int lingertime;
148extern struct timeval start_time, cur_time;
149extern volatile int exiting;
150extern volatile int status_snapshot;
151extern int confirm;
152extern int confirm_flag;
153extern int working_recverr;
154
155extern volatile int in_pr_addr; /* pr_addr() is executing */
156extern jmp_buf pr_addr_jmp;
157
158#ifndef MSG_CONFIRM
159#define MSG_CONFIRM 0
160#endif
161
162
163/* timing */
164extern int timing; /* flag to do timing */
165extern long tmin; /* minimum round trip time */
166extern long tmax; /* maximum round trip time */
167extern long long tsum; /* sum of all times, for doing average */
168extern long long tsum2;
169extern int rtt;
170extern __u16 acked;
171extern int pipesize;
172
173#define COMMON_OPTIONS \
174case 'a': case 'U': case 'c': case 'd': \
175case 'f': case 'i': case 'w': case 'l': \
176case 'S': case 'n': case 'p': case 'q': \
177case 'r': case 's': case 'v': case 'L': \
178case 't': case 'A': case 'W': case 'B': case 'm': \
179case 'D': case 'O':
180
181#define COMMON_OPTSTR "h?VQ:I:M:aUc:dfi:w:l:S:np:qrs:vLt:AW:Bm:DO"
182
183/*
184 * Write to stdout
185 */
186static inline void write_stdout(const char *str, size_t len)
187{
188 size_t o = 0;
189 ssize_t cc;
190 do {
191 cc = write(STDOUT_FILENO, str + o, len - o);
192 o += cc;
193 } while (len > o || cc < 0);
194}
195
196/*
197 * tvsub --
198 * Subtract 2 timeval structs: out = out - in. Out is assumed to
199 * be >= in.
200 */
201static inline void tvsub(struct timeval *out, struct timeval *in)
202{
203 if ((out->tv_usec -= in->tv_usec) < 0) {
204 --out->tv_sec;
205 out->tv_usec += 1000000;
206 }
207 out->tv_sec -= in->tv_sec;
208}
209
210static inline void set_signal(int signo, void (*handler)(int))
211{
212 struct sigaction sa;
213
214 memset(&sa, 0, sizeof(sa));
215
216 sa.sa_handler = (void (*)(int))handler;
217#ifdef SA_INTERRUPT
218 sa.sa_flags = SA_INTERRUPT;
219#endif
220 sigaction(signo, &sa, NULL);
221}
222
223extern int __schedule_exit(int next);
224
225static inline int schedule_exit(int next)
226{
227 if (npackets && ntransmitted >= npackets && !deadline)
228 next = __schedule_exit(next);
229 return next;
230}
231
232static inline int in_flight(void)
233{
234 __u16 diff = (__u16)ntransmitted - acked;
235 return (diff<=0x7FFF) ? diff : ntransmitted-nreceived-nerrors;
236}
237
238static inline void acknowledge(__u16 seq)
239{
240 __u16 diff = (__u16)ntransmitted - seq;
241 if (diff <= 0x7FFF) {
242 if ((int)diff+1 > pipesize)
243 pipesize = (int)diff+1;
244 if ((__s16)(seq - acked) > 0 ||
245 (__u16)ntransmitted - acked > 0x7FFF)
246 acked = seq;
247 }
248}
249
250static inline void advance_ntransmitted(void)
251{
252 ntransmitted++;
253 /* Invalidate acked, if 16 bit seq overflows. */
254 if ((__u16)ntransmitted - acked > 0x7FFF)
255 acked = (__u16)ntransmitted + 1;
256}
257
258extern void limit_capabilities(void);
259static int enable_capability_raw(void);
260static int disable_capability_raw(void);
261static int enable_capability_admin(void);
262static int disable_capability_admin(void);
263#ifdef CAPABILITIES
264extern int modify_capability(cap_value_t, cap_flag_value_t);
265static inline int enable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_SET); };
266static inline int disable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_CLEAR); };
267static inline int enable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_SET); };
268static inline int disable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_CLEAR); };
269#else
270extern int modify_capability(int);
271static inline int enable_capability_raw(void) { return modify_capability(1); };
272static inline int disable_capability_raw(void) { return modify_capability(0); };
273static inline int enable_capability_admin(void) { return modify_capability(1); };
274static inline int disable_capability_admin(void) { return modify_capability(0); };
275#endif
276extern void drop_capabilities(void);
Lorenzo Colitti924a10d2013-07-09 23:43:41 +0900277extern void android_check_security(void);
Lorenzo Colitti313379e2013-07-11 01:07:11 +0900278
279extern int send_probe(void);
280extern int receive_error_msg(void);
281extern int parse_reply(struct msghdr *msg, int len, void *addr, struct timeval *);
282extern void install_filter(void);
Lorenzo Colittice2d2d02013-07-09 17:58:13 +0900283extern int is_ours(uint16_t id);
Lorenzo Colitti313379e2013-07-11 01:07:11 +0900284
285extern int pinger(void);
286extern void sock_setbufs(int icmp_sock, int alloc);
Lorenzo Colitti3d667cc2014-05-30 23:39:50 +0900287extern void sock_setmark(int icmp_sock);
Lorenzo Colitti313379e2013-07-11 01:07:11 +0900288extern void setup(int icmp_sock);
289extern void main_loop(int icmp_sock, __u8 *buf, int buflen) __attribute__((noreturn));
290extern void finish(void) __attribute__((noreturn));
291extern void status(void);
292extern void common_options(int ch);
293extern int gather_statistics(__u8 *ptr, int icmplen,
294 int cc, __u16 seq, int hops,
295 int csfailed, struct timeval *tv, char *from,
296 void (*pr_reply)(__u8 *ptr, int cc));
297extern void print_timestamp(void);