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