1 #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
36 #ifdef ANDROID
37 #include <linux/icmp.h>
38 #include <sys/auxv.h>
39 #endif
40
41 #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 */
52 extern 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
88 typedef __u64 bitmap_t;
89 # define BITMAP_SHIFT 6
90 #else
91 typedef __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
99 struct rcvd_table {
100 bitmap_t bitmap[MAX_DUP_CHK / (sizeof(bitmap_t) * 8)];
101 };
102
103 extern struct rcvd_table rcvd_tbl;
104 extern int using_ping_socket;
105
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
rcvd_set(__u16 seq)109 static inline void rcvd_set(__u16 seq)
110 {
111 unsigned bit = seq % MAX_DUP_CHK;
112 A(bit) |= B(bit);
113 }
114
rcvd_clear(__u16 seq)115 static inline void rcvd_clear(__u16 seq)
116 {
117 unsigned bit = seq % MAX_DUP_CHK;
118 A(bit) &= ~B(bit);
119 }
120
rcvd_test(__u16 seq)121 static inline bitmap_t rcvd_test(__u16 seq)
122 {
123 unsigned bit = seq % MAX_DUP_CHK;
124 return A(bit) & B(bit);
125 }
126
127 extern u_char outpack[];
128 extern int maxpacket;
129
130 extern int datalen;
131 extern char *hostname;
132 extern int uid;
133 extern int ident; /* process id to identify our packets */
134
135 extern int sndbuf;
136 extern int ttl;
137
138 extern long npackets; /* max packets to transmit */
139 extern long nreceived; /* # of packets we got back */
140 extern long nrepeats; /* number of duplicates */
141 extern long ntransmitted; /* sequence # for outbound packets = #sent */
142 extern long nchecksum; /* replies with bad checksum */
143 extern long nerrors; /* icmp errors */
144 extern int interval; /* interval between packets (msec) */
145 extern int preload;
146 extern int deadline; /* time to die */
147 extern int lingertime;
148 extern struct timeval start_time, cur_time;
149 extern volatile int exiting;
150 extern volatile int status_snapshot;
151 extern int confirm;
152 extern int confirm_flag;
153 extern int working_recverr;
154
155 extern volatile int in_pr_addr; /* pr_addr() is executing */
156 extern jmp_buf pr_addr_jmp;
157
158 #ifndef MSG_CONFIRM
159 #define MSG_CONFIRM 0
160 #endif
161
162
163 /* timing */
164 extern int timing; /* flag to do timing */
165 extern long tmin; /* minimum round trip time */
166 extern long tmax; /* maximum round trip time */
167 extern long long tsum; /* sum of all times, for doing average */
168 extern long long tsum2;
169 extern int rtt;
170 extern __u16 acked;
171 extern int pipesize;
172
173 #define COMMON_OPTIONS \
174 case 'a': case 'U': case 'c': case 'd': \
175 case 'f': case 'i': case 'w': case 'l': \
176 case 'S': case 'n': case 'p': case 'q': \
177 case 'r': case 's': case 'v': case 'L': \
178 case 't': case 'A': case 'W': case 'B': case 'm': \
179 case '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 */
write_stdout(const char * str,size_t len)186 static 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 */
tvsub(struct timeval * out,struct timeval * in)201 static 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
set_signal(int signo,void (* handler)(int))210 static 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
223 extern int __schedule_exit(int next);
224
schedule_exit(int next)225 static inline int schedule_exit(int next)
226 {
227 if (npackets && ntransmitted >= npackets && !deadline)
228 next = __schedule_exit(next);
229 return next;
230 }
231
in_flight(void)232 static inline int in_flight(void)
233 {
234 __u16 diff = (__u16)ntransmitted - acked;
235 return (diff<=0x7FFF) ? diff : ntransmitted-nreceived-nerrors;
236 }
237
acknowledge(__u16 seq)238 static 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
advance_ntransmitted(void)250 static 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
258 extern void limit_capabilities(void);
259 static int enable_capability_raw(void);
260 static int disable_capability_raw(void);
261 static int enable_capability_admin(void);
262 static int disable_capability_admin(void);
263 #ifdef CAPABILITIES
264 extern int modify_capability(cap_value_t, cap_flag_value_t);
enable_capability_raw(void)265 static inline int enable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_SET); };
disable_capability_raw(void)266 static inline int disable_capability_raw(void) { return modify_capability(CAP_NET_RAW, CAP_CLEAR); };
enable_capability_admin(void)267 static inline int enable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_SET); };
disable_capability_admin(void)268 static inline int disable_capability_admin(void) { return modify_capability(CAP_NET_ADMIN, CAP_CLEAR); };
269 #else
270 extern int modify_capability(int);
enable_capability_raw(void)271 static inline int enable_capability_raw(void) { return modify_capability(1); };
disable_capability_raw(void)272 static inline int disable_capability_raw(void) { return modify_capability(0); };
enable_capability_admin(void)273 static inline int enable_capability_admin(void) { return modify_capability(1); };
disable_capability_admin(void)274 static inline int disable_capability_admin(void) { return modify_capability(0); };
275 #endif
276 extern void drop_capabilities(void);
277 extern void android_check_security(void);
278
279 extern int send_probe(void);
280 extern int receive_error_msg(void);
281 extern int parse_reply(struct msghdr *msg, int len, void *addr, struct timeval *);
282 extern void install_filter(void);
283 extern int is_ours(uint16_t id);
284
285 extern int pinger(void);
286 extern void sock_setbufs(int icmp_sock, int alloc);
287 extern void sock_setmark(int icmp_sock);
288 extern void setup(int icmp_sock);
289 extern void main_loop(int icmp_sock, __u8 *buf, int buflen) __attribute__((noreturn));
290 extern void finish(void) __attribute__((noreturn));
291 extern void status(void);
292 extern void common_options(int ch);
293 extern 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));
297 extern void print_timestamp(void);
298