]>
Commit | Line | Data |
---|---|---|
aba5acdf SH |
1 | /* |
2 | * utils.c | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public License | |
6 | * as published by the Free Software Foundation; either version | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> | |
10 | * | |
aba5acdf SH |
11 | */ |
12 | ||
13 | #include <stdio.h> | |
14 | #include <stdlib.h> | |
697ac639 | 15 | #include <math.h> |
aba5acdf | 16 | #include <unistd.h> |
aba5acdf | 17 | #include <fcntl.h> |
5bd9dd49 | 18 | #include <limits.h> |
aba5acdf SH |
19 | #include <sys/socket.h> |
20 | #include <netinet/in.h> | |
21 | #include <string.h> | |
625df645 | 22 | #include <ctype.h> |
aba5acdf SH |
23 | #include <netdb.h> |
24 | #include <arpa/inet.h> | |
5dfe5561 | 25 | #include <asm/types.h> |
aba5acdf | 26 | #include <linux/pkt_sched.h> |
5bd9dd49 | 27 | #include <linux/param.h> |
0b218ab1 | 28 | #include <linux/if_arp.h> |
dacc5d41 | 29 | #include <linux/mpls.h> |
c7a3b229 | 30 | #include <linux/snmp.h> |
90f93024 SH |
31 | #include <time.h> |
32 | #include <sys/time.h> | |
30d10db5 | 33 | #include <errno.h> |
ba2fc55b LB |
34 | #ifdef HAVE_LIBCAP |
35 | #include <sys/capability.h> | |
36 | #endif | |
90f93024 | 37 | |
0b218ab1 | 38 | #include "rt_names.h" |
aba5acdf | 39 | #include "utils.h" |
f5b50a18 | 40 | #include "ll_map.h" |
e998e118 | 41 | #include "namespace.h" |
aba5acdf | 42 | |
66488539 | 43 | int resolve_hosts; |
e1933b92 | 44 | int timestamp_short; |
54336567 | 45 | int pretty; |
b85076cd | 46 | const char *_SL_ = "\n"; |
79aa79d0 | 47 | |
b8795a32 SH |
48 | static int af_byte_len(int af); |
49 | static void print_time(char *buf, int len, __u32 time); | |
50 | static void print_time64(char *buf, int len, __s64 time); | |
51 | ||
927e3cfb ND |
52 | int read_prop(const char *dev, char *prop, long *value) |
53 | { | |
54 | char fname[128], buf[80], *endp, *nl; | |
55 | FILE *fp; | |
56 | long result; | |
57 | int ret; | |
58 | ||
59 | ret = snprintf(fname, sizeof(fname), "/sys/class/net/%s/%s", | |
60 | dev, prop); | |
61 | ||
62 | if (ret <= 0 || ret >= sizeof(fname)) { | |
63 | fprintf(stderr, "could not build pathname for property\n"); | |
64 | return -1; | |
65 | } | |
66 | ||
67 | fp = fopen(fname, "r"); | |
68 | if (fp == NULL) { | |
69 | fprintf(stderr, "fopen %s: %s\n", fname, strerror(errno)); | |
70 | return -1; | |
71 | } | |
72 | ||
73 | if (!fgets(buf, sizeof(buf), fp)) { | |
74 | fprintf(stderr, "property \"%s\" in file %s is currently unknown\n", prop, fname); | |
75 | fclose(fp); | |
76 | goto out; | |
77 | } | |
78 | ||
79 | nl = strchr(buf, '\n'); | |
80 | if (nl) | |
81 | *nl = '\0'; | |
82 | ||
83 | fclose(fp); | |
84 | result = strtol(buf, &endp, 0); | |
85 | ||
86 | if (*endp || buf == endp) { | |
87 | fprintf(stderr, "value \"%s\" in file %s is not a number\n", | |
88 | buf, fname); | |
89 | goto out; | |
90 | } | |
91 | ||
92 | if ((result == LONG_MAX || result == LONG_MIN) && errno == ERANGE) { | |
93 | fprintf(stderr, "strtol %s: %s", fname, strerror(errno)); | |
94 | goto out; | |
95 | } | |
96 | ||
97 | *value = result; | |
98 | return 0; | |
99 | out: | |
100 | fprintf(stderr, "Failed to parse %s\n", fname); | |
101 | return -1; | |
102 | } | |
103 | ||
609640f5 SD |
104 | int get_hex(char c) |
105 | { | |
106 | if (c >= 'A' && c <= 'F') | |
107 | return c - 'A' + 10; | |
108 | if (c >= 'a' && c <= 'f') | |
109 | return c - 'a' + 10; | |
110 | if (c >= '0' && c <= '9') | |
111 | return c - '0'; | |
112 | ||
113 | return -1; | |
114 | } | |
115 | ||
4094db72 | 116 | int get_integer(int *val, const char *arg, int base) |
aba5acdf SH |
117 | { |
118 | long res; | |
119 | char *ptr; | |
120 | ||
121 | if (!arg || !*arg) | |
122 | return -1; | |
697ac639 | 123 | |
aba5acdf | 124 | res = strtol(arg, &ptr, base); |
697ac639 SH |
125 | |
126 | /* If there were no digits at all, strtol() stores | |
e1933b92 | 127 | * the original value of nptr in *endptr (and returns 0). |
697ac639 SH |
128 | * In particular, if *nptr is not '\0' but **endptr is '\0' on return, |
129 | * the entire string is valid. | |
130 | */ | |
131 | if (!ptr || ptr == arg || *ptr) | |
132 | return -1; | |
133 | ||
134 | /* If an underflow occurs, strtol() returns LONG_MIN. | |
135 | * If an overflow occurs, strtol() returns LONG_MAX. | |
136 | * In both cases, errno is set to ERANGE. | |
137 | */ | |
138 | if ((res == LONG_MAX || res == LONG_MIN) && errno == ERANGE) | |
aba5acdf | 139 | return -1; |
697ac639 SH |
140 | |
141 | /* Outside range of int */ | |
142 | if (res < INT_MIN || res > INT_MAX) | |
143 | return -1; | |
144 | ||
aba5acdf SH |
145 | *val = res; |
146 | return 0; | |
147 | } | |
148 | ||
6b1ac654 | 149 | int mask2bits(__u32 netmask) |
4b270b17 | 150 | { |
e1933b92 | 151 | unsigned int bits = 0; |
6b1ac654 SH |
152 | __u32 mask = ntohl(netmask); |
153 | __u32 host = ~mask; | |
4b270b17 | 154 | |
6b1ac654 SH |
155 | /* a valid netmask must be 2^n - 1 */ |
156 | if ((host & (host + 1)) != 0) | |
157 | return -1; | |
4b270b17 | 158 | |
6b1ac654 SH |
159 | for (; mask; mask <<= 1) |
160 | ++bits; | |
4b270b17 SH |
161 | return bits; |
162 | } | |
163 | ||
e1933b92 | 164 | static int get_netmask(unsigned int *val, const char *arg, int base) |
5a0d1cbf AH |
165 | { |
166 | inet_prefix addr; | |
167 | ||
168 | if (!get_unsigned(val, arg, base)) | |
169 | return 0; | |
170 | ||
171 | /* try coverting dotted quad to CIDR */ | |
6b1ac654 SH |
172 | if (!get_addr_1(&addr, arg, AF_INET) && addr.family == AF_INET) { |
173 | int b = mask2bits(addr.data[0]); | |
e9e9365b | 174 | |
6b1ac654 SH |
175 | if (b >= 0) { |
176 | *val = b; | |
5a0d1cbf | 177 | return 0; |
6b1ac654 | 178 | } |
5a0d1cbf AH |
179 | } |
180 | ||
181 | return -1; | |
182 | } | |
183 | ||
e1933b92 | 184 | int get_unsigned(unsigned int *val, const char *arg, int base) |
aba5acdf SH |
185 | { |
186 | unsigned long res; | |
187 | char *ptr; | |
188 | ||
189 | if (!arg || !*arg) | |
190 | return -1; | |
697ac639 | 191 | |
aba5acdf | 192 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
193 | |
194 | /* empty string or trailing non-digits */ | |
195 | if (!ptr || ptr == arg || *ptr) | |
196 | return -1; | |
197 | ||
198 | /* overflow */ | |
199 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 200 | return -1; |
697ac639 SH |
201 | |
202 | /* out side range of unsigned */ | |
203 | if (res > UINT_MAX) | |
204 | return -1; | |
205 | ||
aba5acdf SH |
206 | *val = res; |
207 | return 0; | |
208 | } | |
209 | ||
54e0b2e7 | 210 | /* |
db6b0cfa GR |
211 | * get_time_rtt is "translated" from a similar routine "get_time" in |
212 | * tc_util.c. We don't use the exact same routine because tc passes | |
213 | * microseconds to the kernel and the callers of get_time_rtt want to | |
214 | * pass milliseconds (standard unit for rtt values since 2.6.27), and | |
215 | * have a different assumption for the units of a "raw" number. | |
54e0b2e7 | 216 | */ |
e1933b92 | 217 | int get_time_rtt(unsigned int *val, const char *arg, int *raw) |
54e0b2e7 RJ |
218 | { |
219 | double t; | |
220 | unsigned long res; | |
221 | char *p; | |
222 | ||
697ac639 SH |
223 | if (strchr(arg, '.') != NULL) { |
224 | t = strtod(arg, &p); | |
54e0b2e7 RJ |
225 | if (t < 0.0) |
226 | return -1; | |
697ac639 | 227 | |
4d9a264f FW |
228 | /* no digits? */ |
229 | if (!p || p == arg) | |
697ac639 SH |
230 | return -1; |
231 | ||
232 | /* over/underflow */ | |
233 | if ((t == HUGE_VALF || t == HUGE_VALL) && errno == ERANGE) | |
234 | return -1; | |
235 | } else { | |
897fb84f | 236 | res = strtoul(arg, &p, 0); |
697ac639 | 237 | |
4d9a264f FW |
238 | /* empty string? */ |
239 | if (!p || p == arg) | |
697ac639 SH |
240 | return -1; |
241 | ||
242 | /* overflow */ | |
243 | if (res == ULONG_MAX && errno == ERANGE) | |
54e0b2e7 | 244 | return -1; |
697ac639 | 245 | |
54e0b2e7 RJ |
246 | t = (double)res; |
247 | } | |
697ac639 | 248 | |
54e0b2e7 RJ |
249 | if (p == arg) |
250 | return -1; | |
54e0b2e7 RJ |
251 | *raw = 1; |
252 | ||
253 | if (*p) { | |
254 | *raw = 0; | |
e1933b92 SH |
255 | if (strcasecmp(p, "s") == 0 || |
256 | strcasecmp(p, "sec") == 0 || | |
257 | strcasecmp(p, "secs") == 0) | |
258 | t *= 1000; | |
259 | else if (strcasecmp(p, "ms") == 0 || | |
260 | strcasecmp(p, "msec") == 0 || | |
261 | strcasecmp(p, "msecs") == 0) | |
54e0b2e7 | 262 | t *= 1.0; /* allow suffix, do nothing */ |
e1933b92 SH |
263 | else |
264 | return -1; | |
265 | } | |
54e0b2e7 RJ |
266 | |
267 | /* emulate ceil() without having to bring-in -lm and always be >= 1 */ | |
db6b0cfa GR |
268 | *val = t; |
269 | if (*val < t) | |
270 | *val += 1; | |
e9e9365b | 271 | |
e1933b92 | 272 | return 0; |
54e0b2e7 RJ |
273 | |
274 | } | |
275 | ||
c7699875 | 276 | int get_u64(__u64 *val, const char *arg, int base) |
277 | { | |
278 | unsigned long long res; | |
279 | char *ptr; | |
280 | ||
281 | if (!arg || !*arg) | |
282 | return -1; | |
697ac639 | 283 | |
c7699875 | 284 | res = strtoull(arg, &ptr, base); |
697ac639 SH |
285 | |
286 | /* empty string or trailing non-digits */ | |
287 | if (!ptr || ptr == arg || *ptr) | |
288 | return -1; | |
289 | ||
290 | /* overflow */ | |
291 | if (res == ULLONG_MAX && errno == ERANGE) | |
292 | return -1; | |
293 | ||
294 | /* in case ULL is 128 bits */ | |
295 | if (res > 0xFFFFFFFFFFFFFFFFULL) | |
296 | return -1; | |
297 | ||
e1933b92 SH |
298 | *val = res; |
299 | return 0; | |
c7699875 | 300 | } |
301 | ||
4094db72 | 302 | int get_u32(__u32 *val, const char *arg, int base) |
aba5acdf SH |
303 | { |
304 | unsigned long res; | |
305 | char *ptr; | |
306 | ||
307 | if (!arg || !*arg) | |
308 | return -1; | |
309 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
310 | |
311 | /* empty string or trailing non-digits */ | |
312 | if (!ptr || ptr == arg || *ptr) | |
313 | return -1; | |
314 | ||
315 | /* overflow */ | |
316 | if (res == ULONG_MAX && errno == ERANGE) | |
317 | return -1; | |
318 | ||
319 | /* in case UL > 32 bits */ | |
320 | if (res > 0xFFFFFFFFUL) | |
aba5acdf | 321 | return -1; |
697ac639 | 322 | |
aba5acdf SH |
323 | *val = res; |
324 | return 0; | |
325 | } | |
326 | ||
4094db72 | 327 | int get_u16(__u16 *val, const char *arg, int base) |
aba5acdf SH |
328 | { |
329 | unsigned long res; | |
330 | char *ptr; | |
331 | ||
332 | if (!arg || !*arg) | |
333 | return -1; | |
334 | res = strtoul(arg, &ptr, base); | |
697ac639 SH |
335 | |
336 | /* empty string or trailing non-digits */ | |
337 | if (!ptr || ptr == arg || *ptr) | |
338 | return -1; | |
339 | ||
340 | /* overflow */ | |
341 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 342 | return -1; |
697ac639 SH |
343 | |
344 | if (res > 0xFFFFUL) | |
345 | return -1; | |
346 | ||
aba5acdf SH |
347 | *val = res; |
348 | return 0; | |
349 | } | |
350 | ||
4094db72 | 351 | int get_u8(__u8 *val, const char *arg, int base) |
aba5acdf SH |
352 | { |
353 | unsigned long res; | |
354 | char *ptr; | |
355 | ||
356 | if (!arg || !*arg) | |
357 | return -1; | |
697ac639 | 358 | |
aba5acdf | 359 | res = strtoul(arg, &ptr, base); |
697ac639 SH |
360 | /* empty string or trailing non-digits */ |
361 | if (!ptr || ptr == arg || *ptr) | |
362 | return -1; | |
363 | ||
364 | /* overflow */ | |
365 | if (res == ULONG_MAX && errno == ERANGE) | |
aba5acdf | 366 | return -1; |
697ac639 SH |
367 | |
368 | if (res > 0xFFUL) | |
369 | return -1; | |
370 | ||
aba5acdf SH |
371 | *val = res; |
372 | return 0; | |
373 | } | |
374 | ||
a066bac8 VCG |
375 | int get_s64(__s64 *val, const char *arg, int base) |
376 | { | |
377 | long res; | |
378 | char *ptr; | |
379 | ||
380 | errno = 0; | |
381 | ||
382 | if (!arg || !*arg) | |
383 | return -1; | |
384 | res = strtoll(arg, &ptr, base); | |
385 | if (!ptr || ptr == arg || *ptr) | |
386 | return -1; | |
387 | if ((res == LLONG_MIN || res == LLONG_MAX) && errno == ERANGE) | |
388 | return -1; | |
389 | if (res > INT64_MAX || res < INT64_MIN) | |
390 | return -1; | |
391 | ||
392 | *val = res; | |
393 | return 0; | |
394 | } | |
395 | ||
30d10db5 HPP |
396 | int get_s32(__s32 *val, const char *arg, int base) |
397 | { | |
398 | long res; | |
399 | char *ptr; | |
400 | ||
401 | errno = 0; | |
402 | ||
403 | if (!arg || !*arg) | |
404 | return -1; | |
405 | res = strtol(arg, &ptr, base); | |
697ac639 SH |
406 | if (!ptr || ptr == arg || *ptr) |
407 | return -1; | |
408 | if ((res == LONG_MIN || res == LONG_MAX) && errno == ERANGE) | |
30d10db5 | 409 | return -1; |
697ac639 SH |
410 | if (res > INT32_MAX || res < INT32_MIN) |
411 | return -1; | |
412 | ||
30d10db5 HPP |
413 | *val = res; |
414 | return 0; | |
415 | } | |
416 | ||
9f7401fa SD |
417 | int get_be64(__be64 *val, const char *arg, int base) |
418 | { | |
419 | __u64 v; | |
420 | int ret = get_u64(&v, arg, base); | |
421 | ||
422 | if (!ret) | |
423 | *val = htonll(v); | |
424 | ||
425 | return ret; | |
426 | } | |
427 | ||
428 | int get_be32(__be32 *val, const char *arg, int base) | |
429 | { | |
430 | __u32 v; | |
431 | int ret = get_u32(&v, arg, base); | |
432 | ||
433 | if (!ret) | |
434 | *val = htonl(v); | |
435 | ||
436 | return ret; | |
437 | } | |
438 | ||
439 | int get_be16(__be16 *val, const char *arg, int base) | |
440 | { | |
441 | __u16 v; | |
442 | int ret = get_u16(&v, arg, base); | |
443 | ||
444 | if (!ret) | |
445 | *val = htons(v); | |
446 | ||
447 | return ret; | |
448 | } | |
449 | ||
cafa6c8e SH |
450 | /* This uses a non-standard parsing (ie not inet_aton, or inet_pton) |
451 | * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8 | |
452 | */ | |
453 | static int get_addr_ipv4(__u8 *ap, const char *cp) | |
aba5acdf | 454 | { |
cafa6c8e SH |
455 | int i; |
456 | ||
457 | for (i = 0; i < 4; i++) { | |
458 | unsigned long n; | |
459 | char *endp; | |
e9e9365b | 460 | |
cafa6c8e SH |
461 | n = strtoul(cp, &endp, 0); |
462 | if (n > 255) | |
463 | return -1; /* bogus network value */ | |
464 | ||
465 | if (endp == cp) /* no digits */ | |
466 | return -1; | |
94afda75 | 467 | |
cafa6c8e SH |
468 | ap[i] = n; |
469 | ||
470 | if (*endp == '\0') | |
471 | break; | |
472 | ||
473 | if (i == 3 || *endp != '.') | |
e1933b92 | 474 | return -1; /* extra characters */ |
cafa6c8e SH |
475 | cp = endp + 1; |
476 | } | |
477 | ||
478 | return 1; | |
479 | } | |
480 | ||
5866bddd TH |
481 | int get_addr64(__u64 *ap, const char *cp) |
482 | { | |
483 | int i; | |
484 | ||
485 | union { | |
486 | __u16 v16[4]; | |
487 | __u64 v64; | |
488 | } val; | |
489 | ||
490 | for (i = 0; i < 4; i++) { | |
491 | unsigned long n; | |
492 | char *endp; | |
493 | ||
494 | n = strtoul(cp, &endp, 16); | |
495 | if (n > 0xffff) | |
496 | return -1; /* bogus network value */ | |
497 | ||
498 | if (endp == cp) /* no digits */ | |
499 | return -1; | |
500 | ||
501 | val.v16[i] = htons(n); | |
502 | ||
503 | if (*endp == '\0') | |
504 | break; | |
505 | ||
506 | if (i == 3 || *endp != ':') | |
507 | return -1; /* extra characters */ | |
508 | cp = endp + 1; | |
509 | } | |
510 | ||
511 | *ap = val.v64; | |
512 | ||
513 | return 1; | |
514 | } | |
515 | ||
27c523e2 SP |
516 | static void set_address_type(inet_prefix *addr) |
517 | { | |
518 | switch (addr->family) { | |
519 | case AF_INET: | |
520 | if (!addr->data[0]) | |
521 | addr->flags |= ADDRTYPE_INET_UNSPEC; | |
522 | else if (IN_MULTICAST(ntohl(addr->data[0]))) | |
523 | addr->flags |= ADDRTYPE_INET_MULTI; | |
524 | else | |
525 | addr->flags |= ADDRTYPE_INET; | |
526 | break; | |
527 | case AF_INET6: | |
528 | if (IN6_IS_ADDR_UNSPECIFIED(addr->data)) | |
529 | addr->flags |= ADDRTYPE_INET_UNSPEC; | |
530 | else if (IN6_IS_ADDR_MULTICAST(addr->data)) | |
531 | addr->flags |= ADDRTYPE_INET_MULTI; | |
532 | else | |
533 | addr->flags |= ADDRTYPE_INET; | |
534 | break; | |
535 | } | |
536 | } | |
537 | ||
7bf5e876 | 538 | static int __get_addr_1(inet_prefix *addr, const char *name, int family) |
cafa6c8e | 539 | { |
aba5acdf SH |
540 | memset(addr, 0, sizeof(*addr)); |
541 | ||
7696f109 | 542 | if (strcmp(name, "default") == 0) { |
dacc5d41 | 543 | if ((family == AF_DECnet) || (family == AF_MPLS)) |
aba5acdf | 544 | return -1; |
d42c7891 | 545 | addr->family = family; |
f2522007 | 546 | addr->bytelen = af_byte_len(addr->family); |
93fa1241 | 547 | addr->bitlen = -2; |
7696f109 AZ |
548 | addr->flags |= PREFIXLEN_SPECIFIED; |
549 | return 0; | |
550 | } | |
551 | ||
552 | if (strcmp(name, "all") == 0 || | |
553 | strcmp(name, "any") == 0) { | |
554 | if ((family == AF_DECnet) || (family == AF_MPLS)) | |
555 | return -1; | |
d42c7891 | 556 | addr->family = family; |
7696f109 AZ |
557 | addr->bytelen = 0; |
558 | addr->bitlen = -2; | |
aba5acdf SH |
559 | return 0; |
560 | } | |
561 | ||
0b218ab1 EB |
562 | if (family == AF_PACKET) { |
563 | int len; | |
e1933b92 | 564 | |
8b59612f DA |
565 | len = ll_addr_a2n((char *) &addr->data, sizeof(addr->data), |
566 | name); | |
0b218ab1 EB |
567 | if (len < 0) |
568 | return -1; | |
569 | ||
570 | addr->family = AF_PACKET; | |
571 | addr->bytelen = len; | |
572 | addr->bitlen = len * 8; | |
573 | return 0; | |
574 | } | |
575 | ||
aba5acdf SH |
576 | if (strchr(name, ':')) { |
577 | addr->family = AF_INET6; | |
578 | if (family != AF_UNSPEC && family != AF_INET6) | |
579 | return -1; | |
580 | if (inet_pton(AF_INET6, name, addr->data) <= 0) | |
581 | return -1; | |
582 | addr->bytelen = 16; | |
583 | addr->bitlen = -1; | |
584 | return 0; | |
585 | } | |
586 | ||
dacc5d41 | 587 | if (family == AF_MPLS) { |
4af44716 | 588 | unsigned int maxlabels; |
dacc5d41 | 589 | int i; |
e1933b92 | 590 | |
dacc5d41 | 591 | addr->family = AF_MPLS; |
4af44716 DA |
592 | if (mpls_pton(AF_MPLS, name, addr->data, |
593 | sizeof(addr->data)) <= 0) | |
dacc5d41 EB |
594 | return -1; |
595 | addr->bytelen = 4; | |
596 | addr->bitlen = 20; | |
597 | /* How many bytes do I need? */ | |
4af44716 DA |
598 | maxlabels = sizeof(addr->data) / sizeof(struct mpls_label); |
599 | for (i = 0; i < maxlabels; i++) { | |
dacc5d41 EB |
600 | if (ntohl(addr->data[i]) & MPLS_LS_S_MASK) { |
601 | addr->bytelen = (i + 1)*4; | |
602 | break; | |
603 | } | |
604 | } | |
605 | return 0; | |
606 | } | |
607 | ||
aba5acdf SH |
608 | addr->family = AF_INET; |
609 | if (family != AF_UNSPEC && family != AF_INET) | |
610 | return -1; | |
94afda75 | 611 | |
cafa6c8e | 612 | if (get_addr_ipv4((__u8 *)addr->data, name) <= 0) |
516ffb6b | 613 | return -1; |
94afda75 | 614 | |
aba5acdf SH |
615 | addr->bytelen = 4; |
616 | addr->bitlen = -1; | |
aba5acdf SH |
617 | return 0; |
618 | } | |
619 | ||
7bf5e876 SP |
620 | int get_addr_1(inet_prefix *addr, const char *name, int family) |
621 | { | |
622 | int ret; | |
623 | ||
624 | ret = __get_addr_1(addr, name, family); | |
625 | if (ret) | |
626 | return ret; | |
627 | ||
27c523e2 | 628 | set_address_type(addr); |
7bf5e876 SP |
629 | return 0; |
630 | } | |
631 | ||
f3a2ddc1 VK |
632 | int af_bit_len(int af) |
633 | { | |
634 | switch (af) { | |
635 | case AF_INET6: | |
636 | return 128; | |
637 | case AF_INET: | |
638 | return 32; | |
639 | case AF_DECnet: | |
640 | return 16; | |
641 | case AF_IPX: | |
642 | return 80; | |
dacc5d41 EB |
643 | case AF_MPLS: |
644 | return 20; | |
f3a2ddc1 VK |
645 | } |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
b8795a32 | 650 | static int af_byte_len(int af) |
f3a2ddc1 VK |
651 | { |
652 | return af_bit_len(af) / 8; | |
653 | } | |
654 | ||
fb9b1d0f | 655 | int get_prefix_1(inet_prefix *dst, char *arg, int family) |
aba5acdf | 656 | { |
aba5acdf | 657 | char *slash; |
93fa1241 | 658 | int err, bitlen, flags; |
aba5acdf SH |
659 | |
660 | slash = strchr(arg, '/'); | |
661 | if (slash) | |
662 | *slash = 0; | |
fb9b1d0f | 663 | |
aba5acdf | 664 | err = get_addr_1(dst, arg, family); |
93fa1241 | 665 | |
aba5acdf SH |
666 | if (slash) |
667 | *slash = '/'; | |
93fa1241 SP |
668 | |
669 | if (err) | |
670 | return err; | |
671 | ||
672 | bitlen = af_bit_len(dst->family); | |
673 | ||
7696f109 | 674 | flags = 0; |
93fa1241 SP |
675 | if (slash) { |
676 | unsigned int plen; | |
677 | ||
678 | if (dst->bitlen == -2) | |
679 | return -1; | |
680 | if (get_netmask(&plen, slash + 1, 0)) | |
681 | return -1; | |
682 | if (plen > bitlen) | |
683 | return -1; | |
684 | ||
7696f109 | 685 | flags |= PREFIXLEN_SPECIFIED; |
93fa1241 SP |
686 | bitlen = plen; |
687 | } else { | |
688 | if (dst->bitlen == -2) | |
689 | bitlen = 0; | |
93fa1241 SP |
690 | } |
691 | ||
692 | dst->flags |= flags; | |
693 | dst->bitlen = bitlen; | |
694 | ||
695 | return 0; | |
aba5acdf SH |
696 | } |
697 | ||
34705c80 PS |
698 | static const char *family_name_verbose(int family) |
699 | { | |
700 | if (family == AF_UNSPEC) | |
701 | return "any valid"; | |
702 | return family_name(family); | |
703 | } | |
704 | ||
c7699875 | 705 | int get_addr(inet_prefix *dst, const char *arg, int family) |
aba5acdf | 706 | { |
aba5acdf | 707 | if (get_addr_1(dst, arg, family)) { |
e1933b92 SH |
708 | fprintf(stderr, |
709 | "Error: %s address is expected rather than \"%s\".\n", | |
34705c80 | 710 | family_name_verbose(family), arg); |
aba5acdf SH |
711 | exit(1); |
712 | } | |
713 | return 0; | |
714 | } | |
715 | ||
27c523e2 SP |
716 | int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family) |
717 | { | |
718 | const int len = RTA_PAYLOAD(rta); | |
719 | const void *data = RTA_DATA(rta); | |
720 | ||
721 | switch (len) { | |
722 | case 4: | |
723 | dst->family = AF_INET; | |
724 | dst->bytelen = 4; | |
725 | memcpy(dst->data, data, 4); | |
726 | break; | |
727 | case 16: | |
728 | dst->family = AF_INET6; | |
729 | dst->bytelen = 16; | |
730 | memcpy(dst->data, data, 16); | |
731 | break; | |
732 | case 2: | |
733 | dst->family = AF_DECnet; | |
734 | dst->bytelen = 2; | |
735 | memcpy(dst->data, data, 2); | |
736 | break; | |
737 | case 10: | |
738 | dst->family = AF_IPX; | |
739 | dst->bytelen = 10; | |
740 | memcpy(dst->data, data, 10); | |
741 | break; | |
742 | default: | |
743 | return -1; | |
744 | } | |
745 | ||
746 | if (family != AF_UNSPEC && family != dst->family) | |
747 | return -2; | |
748 | ||
749 | dst->bitlen = -1; | |
750 | dst->flags = 0; | |
751 | ||
752 | set_address_type(dst); | |
753 | return 0; | |
754 | } | |
755 | ||
fb9b1d0f | 756 | int get_prefix(inet_prefix *dst, char *arg, int family) |
aba5acdf SH |
757 | { |
758 | if (family == AF_PACKET) { | |
e1933b92 SH |
759 | fprintf(stderr, |
760 | "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", | |
761 | arg); | |
aba5acdf SH |
762 | exit(1); |
763 | } | |
334af761 | 764 | |
aba5acdf | 765 | if (get_prefix_1(dst, arg, family)) { |
e1933b92 SH |
766 | fprintf(stderr, |
767 | "Error: %s prefix is expected rather than \"%s\".\n", | |
34705c80 | 768 | family_name_verbose(family), arg); |
aba5acdf SH |
769 | exit(1); |
770 | } | |
771 | return 0; | |
772 | } | |
773 | ||
c7699875 | 774 | __u32 get_addr32(const char *name) |
aba5acdf SH |
775 | { |
776 | inet_prefix addr; | |
e1933b92 | 777 | |
aba5acdf | 778 | if (get_addr_1(&addr, name, AF_INET)) { |
e1933b92 SH |
779 | fprintf(stderr, |
780 | "Error: an IP address is expected rather than \"%s\"\n", | |
781 | name); | |
aba5acdf SH |
782 | exit(1); |
783 | } | |
784 | return addr.data[0]; | |
785 | } | |
786 | ||
4094db72 | 787 | void incomplete_command(void) |
aba5acdf SH |
788 | { |
789 | fprintf(stderr, "Command line is not complete. Try option \"help\"\n"); | |
790 | exit(-1); | |
791 | } | |
792 | ||
c7699875 | 793 | void missarg(const char *key) |
794 | { | |
795 | fprintf(stderr, "Error: argument \"%s\" is required\n", key); | |
796 | exit(-1); | |
797 | } | |
798 | ||
4094db72 | 799 | void invarg(const char *msg, const char *arg) |
aba5acdf SH |
800 | { |
801 | fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg); | |
802 | exit(-1); | |
803 | } | |
804 | ||
4094db72 | 805 | void duparg(const char *key, const char *arg) |
aba5acdf | 806 | { |
e1933b92 SH |
807 | fprintf(stderr, |
808 | "Error: duplicate \"%s\": \"%s\" is the second value.\n", | |
809 | key, arg); | |
aba5acdf SH |
810 | exit(-1); |
811 | } | |
812 | ||
4094db72 | 813 | void duparg2(const char *key, const char *arg) |
aba5acdf | 814 | { |
e1933b92 SH |
815 | fprintf(stderr, |
816 | "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", | |
817 | key, arg); | |
aba5acdf SH |
818 | exit(-1); |
819 | } | |
820 | ||
fe99adbc SP |
821 | int nodev(const char *dev) |
822 | { | |
823 | fprintf(stderr, "Cannot find device \"%s\"\n", dev); | |
824 | return -1; | |
825 | } | |
826 | ||
625df645 PS |
827 | int check_ifname(const char *name) |
828 | { | |
829 | /* These checks mimic kernel checks in dev_valid_name */ | |
830 | if (*name == '\0') | |
831 | return -1; | |
832 | if (strlen(name) >= IFNAMSIZ) | |
833 | return -1; | |
834 | ||
835 | while (*name) { | |
836 | if (*name == '/' || isspace(*name)) | |
837 | return -1; | |
838 | ++name; | |
839 | } | |
840 | return 0; | |
841 | } | |
842 | ||
843 | /* buf is assumed to be IFNAMSIZ */ | |
844 | int get_ifname(char *buf, const char *name) | |
845 | { | |
846 | int ret; | |
847 | ||
848 | ret = check_ifname(name); | |
849 | if (ret == 0) | |
850 | strncpy(buf, name, IFNAMSIZ); | |
851 | ||
852 | return ret; | |
853 | } | |
854 | ||
fcac9665 SP |
855 | const char *get_ifname_rta(int ifindex, const struct rtattr *rta) |
856 | { | |
857 | const char *name; | |
858 | ||
859 | if (rta) { | |
860 | name = rta_getattr_str(rta); | |
861 | } else { | |
862 | fprintf(stderr, | |
863 | "BUG: device with ifindex %d has nil ifname\n", | |
864 | ifindex); | |
865 | name = ll_idx_n2a(ifindex); | |
866 | } | |
867 | ||
868 | if (check_ifname(name)) | |
869 | return NULL; | |
870 | ||
871 | return name; | |
872 | } | |
873 | ||
4094db72 | 874 | int matches(const char *cmd, const char *pattern) |
aba5acdf SH |
875 | { |
876 | int len = strlen(cmd); | |
e1933b92 | 877 | |
aba5acdf SH |
878 | if (len > strlen(pattern)) |
879 | return -1; | |
880 | return memcmp(pattern, cmd, len); | |
881 | } | |
882 | ||
4094db72 | 883 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits) |
aba5acdf | 884 | { |
9626dfd5 SH |
885 | const __u32 *a1 = a->data; |
886 | const __u32 *a2 = b->data; | |
aba5acdf SH |
887 | int words = bits >> 0x05; |
888 | ||
889 | bits &= 0x1f; | |
890 | ||
891 | if (words) | |
892 | if (memcmp(a1, a2, words << 2)) | |
893 | return -1; | |
894 | ||
895 | if (bits) { | |
896 | __u32 w1, w2; | |
897 | __u32 mask; | |
898 | ||
899 | w1 = a1[words]; | |
900 | w2 = a2[words]; | |
901 | ||
902 | mask = htonl((0xffffffff) << (0x20 - bits)); | |
903 | ||
904 | if ((w1 ^ w2) & mask) | |
905 | return 1; | |
906 | } | |
907 | ||
908 | return 0; | |
909 | } | |
910 | ||
27c523e2 SP |
911 | int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta) |
912 | { | |
913 | inet_prefix dst; | |
914 | ||
915 | if (!rta || m->family == AF_UNSPEC || m->bitlen <= 0) | |
916 | return 0; | |
917 | ||
918 | if (get_addr_rta(&dst, rta, m->family)) | |
919 | return -1; | |
920 | ||
921 | return inet_addr_match(&dst, m, m->bitlen); | |
922 | } | |
923 | ||
aba5acdf SH |
924 | int __iproute2_hz_internal; |
925 | ||
926 | int __get_hz(void) | |
927 | { | |
928 | char name[1024]; | |
929 | int hz = 0; | |
930 | FILE *fp; | |
931 | ||
932 | if (getenv("HZ")) | |
933 | return atoi(getenv("HZ")) ? : HZ; | |
934 | ||
e1933b92 SH |
935 | if (getenv("PROC_NET_PSCHED")) |
936 | snprintf(name, sizeof(name)-1, | |
937 | "%s", getenv("PROC_NET_PSCHED")); | |
938 | else if (getenv("PROC_ROOT")) | |
939 | snprintf(name, sizeof(name)-1, | |
940 | "%s/net/psched", getenv("PROC_ROOT")); | |
941 | else | |
aba5acdf | 942 | strcpy(name, "/proc/net/psched"); |
e1933b92 | 943 | |
aba5acdf SH |
944 | fp = fopen(name, "r"); |
945 | ||
946 | if (fp) { | |
e1933b92 SH |
947 | unsigned int nom, denom; |
948 | ||
aba5acdf SH |
949 | if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2) |
950 | if (nom == 1000000) | |
951 | hz = denom; | |
952 | fclose(fp); | |
953 | } | |
954 | if (hz) | |
955 | return hz; | |
956 | return HZ; | |
957 | } | |
958 | ||
5e8bc631 | 959 | int __iproute2_user_hz_internal; |
960 | ||
961 | int __get_user_hz(void) | |
962 | { | |
963 | return sysconf(_SC_CLK_TCK); | |
964 | } | |
965 | ||
e1933b92 SH |
966 | const char *rt_addr_n2a_r(int af, int len, |
967 | const void *addr, char *buf, int buflen) | |
aba5acdf SH |
968 | { |
969 | switch (af) { | |
970 | case AF_INET: | |
971 | case AF_INET6: | |
972 | return inet_ntop(af, addr, buf, buflen); | |
dacc5d41 EB |
973 | case AF_MPLS: |
974 | return mpls_ntop(af, addr, buf, buflen); | |
0b218ab1 EB |
975 | case AF_PACKET: |
976 | return ll_addr_n2a(addr, len, ARPHRD_VOID, buf, buflen); | |
728eb8d0 DS |
977 | case AF_BRIDGE: |
978 | { | |
979 | const union { | |
980 | struct sockaddr sa; | |
981 | struct sockaddr_in sin; | |
982 | struct sockaddr_in6 sin6; | |
983 | } *sa = addr; | |
984 | ||
985 | switch (sa->sa.sa_family) { | |
986 | case AF_INET: | |
987 | return inet_ntop(AF_INET, &sa->sin.sin_addr, | |
988 | buf, buflen); | |
989 | case AF_INET6: | |
990 | return inet_ntop(AF_INET6, &sa->sin6.sin6_addr, | |
991 | buf, buflen); | |
992 | } | |
993 | ||
994 | /* fallthrough */ | |
995 | } | |
aba5acdf SH |
996 | default: |
997 | return "???"; | |
998 | } | |
999 | } | |
1000 | ||
2e96d2cc PS |
1001 | const char *rt_addr_n2a(int af, int len, const void *addr) |
1002 | { | |
1003 | static char buf[256]; | |
1004 | ||
1005 | return rt_addr_n2a_r(af, len, addr, buf, 256); | |
1006 | } | |
1007 | ||
45c90d19 EB |
1008 | int read_family(const char *name) |
1009 | { | |
1010 | int family = AF_UNSPEC; | |
e1933b92 | 1011 | |
45c90d19 EB |
1012 | if (strcmp(name, "inet") == 0) |
1013 | family = AF_INET; | |
1014 | else if (strcmp(name, "inet6") == 0) | |
1015 | family = AF_INET6; | |
45c90d19 EB |
1016 | else if (strcmp(name, "link") == 0) |
1017 | family = AF_PACKET; | |
1018 | else if (strcmp(name, "ipx") == 0) | |
1019 | family = AF_IPX; | |
dacc5d41 EB |
1020 | else if (strcmp(name, "mpls") == 0) |
1021 | family = AF_MPLS; | |
45c90d19 EB |
1022 | else if (strcmp(name, "bridge") == 0) |
1023 | family = AF_BRIDGE; | |
1024 | return family; | |
1025 | } | |
1026 | ||
1027 | const char *family_name(int family) | |
1028 | { | |
1029 | if (family == AF_INET) | |
1030 | return "inet"; | |
1031 | if (family == AF_INET6) | |
1032 | return "inet6"; | |
45c90d19 EB |
1033 | if (family == AF_PACKET) |
1034 | return "link"; | |
1035 | if (family == AF_IPX) | |
1036 | return "ipx"; | |
dacc5d41 EB |
1037 | if (family == AF_MPLS) |
1038 | return "mpls"; | |
45c90d19 EB |
1039 | if (family == AF_BRIDGE) |
1040 | return "bridge"; | |
1041 | return "???"; | |
1042 | } | |
1043 | ||
aba5acdf | 1044 | #ifdef RESOLVE_HOSTNAMES |
e1933b92 | 1045 | struct namerec { |
aba5acdf | 1046 | struct namerec *next; |
7dec1bf8 | 1047 | const char *name; |
aba5acdf | 1048 | inet_prefix addr; |
aba5acdf SH |
1049 | }; |
1050 | ||
7dec1bf8 SH |
1051 | #define NHASH 257 |
1052 | static struct namerec *nht[NHASH]; | |
aba5acdf | 1053 | |
7dec1bf8 | 1054 | static const char *resolve_address(const void *addr, int len, int af) |
aba5acdf SH |
1055 | { |
1056 | struct namerec *n; | |
1057 | struct hostent *h_ent; | |
e1933b92 | 1058 | unsigned int hash; |
aba5acdf SH |
1059 | static int notfirst; |
1060 | ||
1061 | ||
e1933b92 SH |
1062 | if (af == AF_INET6 && ((__u32 *)addr)[0] == 0 && |
1063 | ((__u32 *)addr)[1] == 0 && ((__u32 *)addr)[2] == htonl(0xffff)) { | |
aba5acdf SH |
1064 | af = AF_INET; |
1065 | addr += 12; | |
1066 | len = 4; | |
1067 | } | |
1068 | ||
7dec1bf8 | 1069 | hash = *(__u32 *)(addr + len - 4) % NHASH; |
aba5acdf SH |
1070 | |
1071 | for (n = nht[hash]; n; n = n->next) { | |
1072 | if (n->addr.family == af && | |
1073 | n->addr.bytelen == len && | |
1074 | memcmp(n->addr.data, addr, len) == 0) | |
1075 | return n->name; | |
1076 | } | |
e1933b92 SH |
1077 | n = malloc(sizeof(*n)); |
1078 | if (n == NULL) | |
aba5acdf SH |
1079 | return NULL; |
1080 | n->addr.family = af; | |
1081 | n->addr.bytelen = len; | |
1082 | n->name = NULL; | |
1083 | memcpy(n->addr.data, addr, len); | |
1084 | n->next = nht[hash]; | |
1085 | nht[hash] = n; | |
1086 | if (++notfirst == 1) | |
1087 | sethostent(1); | |
1088 | fflush(stdout); | |
1089 | ||
e1933b92 SH |
1090 | h_ent = gethostbyaddr(addr, len, af); |
1091 | if (h_ent != NULL) | |
aba5acdf SH |
1092 | n->name = strdup(h_ent->h_name); |
1093 | ||
1094 | /* Even if we fail, "negative" entry is remembered. */ | |
1095 | return n->name; | |
1096 | } | |
1097 | #endif | |
1098 | ||
a418e451 | 1099 | const char *format_host_r(int af, int len, const void *addr, |
4094db72 | 1100 | char *buf, int buflen) |
aba5acdf SH |
1101 | { |
1102 | #ifdef RESOLVE_HOSTNAMES | |
1103 | if (resolve_hosts) { | |
7dec1bf8 SH |
1104 | const char *n; |
1105 | ||
f3a2ddc1 VK |
1106 | len = len <= 0 ? af_byte_len(af) : len; |
1107 | ||
aba5acdf SH |
1108 | if (len > 0 && |
1109 | (n = resolve_address(addr, len, af)) != NULL) | |
1110 | return n; | |
1111 | } | |
1112 | #endif | |
2e96d2cc | 1113 | return rt_addr_n2a_r(af, len, addr, buf, buflen); |
aba5acdf SH |
1114 | } |
1115 | ||
a418e451 PS |
1116 | const char *format_host(int af, int len, const void *addr) |
1117 | { | |
1118 | static char buf[256]; | |
1119 | ||
1120 | return format_host_r(af, len, addr, buf, 256); | |
1121 | } | |
1122 | ||
aba5acdf | 1123 | |
f332d169 | 1124 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen) |
aba5acdf | 1125 | { |
f332d169 | 1126 | char *ptr = buf; |
aba5acdf SH |
1127 | int i; |
1128 | ||
e1933b92 | 1129 | for (i = 0; i < len; i++) { |
aba5acdf SH |
1130 | if (blen < 3) |
1131 | break; | |
1132 | sprintf(ptr, "%02x", str[i]); | |
1133 | ptr += 2; | |
1134 | blen -= 2; | |
aba5acdf SH |
1135 | } |
1136 | return buf; | |
1137 | } | |
1138 | ||
89ae5020 | 1139 | __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len) |
aba5acdf | 1140 | { |
89ae5020 | 1141 | unsigned int cnt = 0; |
316c2346 | 1142 | char *endptr; |
aba5acdf | 1143 | |
316c2346 JP |
1144 | if (strlen(str) % 2) |
1145 | return NULL; | |
1146 | while (cnt < blen && strlen(str) > 1) { | |
1147 | unsigned int tmp; | |
1148 | char tmpstr[3]; | |
1149 | ||
1150 | strncpy(tmpstr, str, 2); | |
1151 | tmpstr[2] = '\0'; | |
9ba4126d | 1152 | errno = 0; |
316c2346 JP |
1153 | tmp = strtoul(tmpstr, &endptr, 16); |
1154 | if (errno != 0 || tmp > 0xFF || *endptr != '\0') | |
aba5acdf | 1155 | return NULL; |
316c2346 JP |
1156 | buf[cnt++] = tmp; |
1157 | str += 2; | |
aba5acdf | 1158 | } |
89ae5020 SD |
1159 | |
1160 | if (len) | |
1161 | *len = cnt; | |
1162 | ||
aba5acdf SH |
1163 | return buf; |
1164 | } | |
90f93024 | 1165 | |
1c570c50 JHS |
1166 | int hex2mem(const char *buf, uint8_t *mem, int count) |
1167 | { | |
1168 | int i, j; | |
1169 | int c; | |
1170 | ||
1171 | for (i = 0, j = 0; i < count; i++, j += 2) { | |
1172 | c = get_hex(buf[j]); | |
1173 | if (c < 0) | |
2bf1a81a | 1174 | return -1; |
1c570c50 JHS |
1175 | |
1176 | mem[i] = c << 4; | |
1177 | ||
1178 | c = get_hex(buf[j + 1]); | |
1179 | if (c < 0) | |
2bf1a81a | 1180 | return -1; |
1c570c50 JHS |
1181 | |
1182 | mem[i] |= c; | |
1183 | } | |
1184 | ||
1185 | return 0; | |
1c570c50 JHS |
1186 | } |
1187 | ||
5866bddd TH |
1188 | int addr64_n2a(__u64 addr, char *buff, size_t len) |
1189 | { | |
1190 | __u16 *words = (__u16 *)&addr; | |
1191 | __u16 v; | |
1192 | int i, ret; | |
1193 | size_t written = 0; | |
1194 | char *sep = ":"; | |
1195 | ||
1196 | for (i = 0; i < 4; i++) { | |
1197 | v = ntohs(words[i]); | |
1198 | ||
1199 | if (i == 3) | |
1200 | sep = ""; | |
1201 | ||
1202 | ret = snprintf(&buff[written], len - written, "%x%s", v, sep); | |
1203 | if (ret < 0) | |
1204 | return ret; | |
1205 | ||
1206 | written += ret; | |
1207 | } | |
1208 | ||
1209 | return written; | |
1210 | } | |
1211 | ||
7c72df5a ID |
1212 | /* Print buffer and escape bytes that are !isprint or among 'escape' */ |
1213 | void print_escape_buf(const __u8 *buf, size_t len, const char *escape) | |
1214 | { | |
1215 | size_t i; | |
1216 | ||
1217 | for (i = 0; i < len; ++i) { | |
1218 | if (isprint(buf[i]) && buf[i] != '\\' && | |
1219 | !strchr(escape, buf[i])) | |
1220 | printf("%c", buf[i]); | |
1221 | else | |
1222 | printf("\\%03o", buf[i]); | |
1223 | } | |
1224 | } | |
1225 | ||
90f93024 SH |
1226 | int print_timestamp(FILE *fp) |
1227 | { | |
1228 | struct timeval tv; | |
79aa79d0 | 1229 | struct tm *tm; |
90f93024 | 1230 | |
90f93024 | 1231 | gettimeofday(&tv, NULL); |
79aa79d0 VK |
1232 | tm = localtime(&tv.tv_sec); |
1233 | ||
1234 | if (timestamp_short) { | |
1235 | char tshort[40]; | |
1236 | ||
1237 | strftime(tshort, sizeof(tshort), "%Y-%m-%dT%H:%M:%S", tm); | |
1238 | fprintf(fp, "[%s.%06ld] ", tshort, tv.tv_usec); | |
1239 | } else { | |
1240 | char *tstr = asctime(tm); | |
1241 | ||
1242 | tstr[strlen(tstr)-1] = 0; | |
1243 | fprintf(fp, "Timestamp: %s %ld usec\n", | |
1244 | tstr, tv.tv_usec); | |
1245 | } | |
90f93024 | 1246 | |
90f93024 SH |
1247 | return 0; |
1248 | } | |
351efcde | 1249 | |
4328b687 | 1250 | unsigned int print_name_and_link(const char *fmt, |
f5b50a18 SP |
1251 | const char *name, struct rtattr *tb[]) |
1252 | { | |
1253 | const char *link = NULL; | |
1254 | unsigned int m_flag = 0; | |
1255 | SPRINT_BUF(b1); | |
1256 | ||
1257 | if (tb[IFLA_LINK]) { | |
1258 | int iflink = rta_getattr_u32(tb[IFLA_LINK]); | |
1259 | ||
1260 | if (iflink) { | |
1261 | if (tb[IFLA_LINK_NETNSID]) { | |
1262 | if (is_json_context()) { | |
1263 | print_int(PRINT_JSON, | |
1264 | "link_index", NULL, iflink); | |
1265 | } else { | |
1266 | link = ll_idx_n2a(iflink); | |
1267 | } | |
1268 | } else { | |
1269 | link = ll_index_to_name(iflink); | |
1270 | ||
1271 | if (is_json_context()) { | |
1272 | print_string(PRINT_JSON, | |
1273 | "link", NULL, link); | |
1274 | link = NULL; | |
1275 | } | |
1276 | ||
1277 | m_flag = ll_index_to_flags(iflink); | |
1278 | m_flag = !(m_flag & IFF_UP); | |
1279 | } | |
1280 | } else { | |
1281 | if (is_json_context()) | |
1282 | print_null(PRINT_JSON, "link", NULL, NULL); | |
1283 | else | |
1284 | link = "NONE"; | |
1285 | } | |
1286 | ||
1287 | if (link) { | |
1288 | snprintf(b1, sizeof(b1), "%s@%s", name, link); | |
1289 | name = b1; | |
1290 | } | |
1291 | } | |
1292 | ||
4328b687 | 1293 | print_color_string(PRINT_ANY, COLOR_IFNAME, "ifname", fmt, name); |
f5b50a18 SP |
1294 | |
1295 | return m_flag; | |
1296 | } | |
1297 | ||
351efcde SH |
1298 | int cmdlineno; |
1299 | ||
1300 | /* Like glibc getline but handle continuation lines and comments */ | |
d21e8835 | 1301 | ssize_t getcmdline(char **linep, size_t *lenp, FILE *in) |
351efcde | 1302 | { |
d21e8835 | 1303 | ssize_t cc; |
351efcde | 1304 | char *cp; |
ae665a52 | 1305 | |
e1933b92 SH |
1306 | cc = getline(linep, lenp, in); |
1307 | if (cc < 0) | |
351efcde SH |
1308 | return cc; /* eof or error */ |
1309 | ++cmdlineno; | |
1310 | ||
1311 | cp = strchr(*linep, '#'); | |
ae665a52 | 1312 | if (cp) |
351efcde | 1313 | *cp = '\0'; |
ae665a52 | 1314 | |
351efcde SH |
1315 | while ((cp = strstr(*linep, "\\\n")) != NULL) { |
1316 | char *line1 = NULL; | |
1317 | size_t len1 = 0; | |
21a85d3b | 1318 | ssize_t cc1; |
351efcde | 1319 | |
e1933b92 SH |
1320 | cc1 = getline(&line1, &len1, in); |
1321 | if (cc1 < 0) { | |
351efcde SH |
1322 | fprintf(stderr, "Missing continuation line\n"); |
1323 | return cc1; | |
1324 | } | |
1325 | ||
1326 | ++cmdlineno; | |
1327 | *cp = 0; | |
1328 | ||
1329 | cp = strchr(line1, '#'); | |
ae665a52 | 1330 | if (cp) |
351efcde SH |
1331 | *cp = '\0'; |
1332 | ||
d21e8835 AH |
1333 | *lenp = strlen(*linep) + strlen(line1) + 1; |
1334 | *linep = realloc(*linep, *lenp); | |
351efcde SH |
1335 | if (!*linep) { |
1336 | fprintf(stderr, "Out of memory\n"); | |
d21e8835 | 1337 | *lenp = 0; |
351efcde SH |
1338 | return -1; |
1339 | } | |
1340 | cc += cc1 - 2; | |
1341 | strcat(*linep, line1); | |
1342 | free(line1); | |
1343 | } | |
1344 | return cc; | |
1345 | } | |
1346 | ||
1347 | /* split command line into argument vector */ | |
1348 | int makeargs(char *line, char *argv[], int maxargs) | |
1349 | { | |
1350 | static const char ws[] = " \t\r\n"; | |
bd9cea5d | 1351 | char *cp = line; |
351efcde SH |
1352 | int argc = 0; |
1353 | ||
bd9cea5d SH |
1354 | while (*cp) { |
1355 | /* skip leading whitespace */ | |
1356 | cp += strspn(cp, ws); | |
1357 | ||
1358 | if (*cp == '\0') | |
1359 | break; | |
1360 | ||
351efcde SH |
1361 | if (argc >= (maxargs - 1)) { |
1362 | fprintf(stderr, "Too many arguments to command\n"); | |
1363 | exit(1); | |
1364 | } | |
39e3d383 CG |
1365 | |
1366 | /* word begins with quote */ | |
1367 | if (*cp == '\'' || *cp == '"') { | |
1368 | char quote = *cp++; | |
1369 | ||
1370 | argv[argc++] = cp; | |
1371 | /* find ending quote */ | |
1372 | cp = strchr(cp, quote); | |
1373 | if (cp == NULL) { | |
1374 | fprintf(stderr, "Unterminated quoted string\n"); | |
1375 | exit(1); | |
1376 | } | |
bd9cea5d SH |
1377 | } else { |
1378 | argv[argc++] = cp; | |
1379 | ||
1380 | /* find end of word */ | |
1381 | cp += strcspn(cp, ws); | |
1382 | if (*cp == '\0') | |
1383 | break; | |
39e3d383 CG |
1384 | } |
1385 | ||
95debca7 | 1386 | /* separate words */ |
39e3d383 | 1387 | *cp++ = 0; |
351efcde SH |
1388 | } |
1389 | argv[argc] = NULL; | |
1390 | ||
1391 | return argc; | |
1392 | } | |
aa574cd6 | 1393 | |
ddb1129b VK |
1394 | void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n) |
1395 | { | |
1396 | char *tstr; | |
e1933b92 SH |
1397 | time_t secs = ((__u32 *)NLMSG_DATA(n))[0]; |
1398 | long usecs = ((__u32 *)NLMSG_DATA(n))[1]; | |
1399 | ||
ddb1129b VK |
1400 | tstr = asctime(localtime(&secs)); |
1401 | tstr[strlen(tstr)-1] = 0; | |
1402 | fprintf(fp, "Timestamp: %s %lu us\n", tstr, usecs); | |
1403 | } | |
e998e118 | 1404 | |
b217df10 VK |
1405 | char *int_to_str(int val, char *buf) |
1406 | { | |
1407 | sprintf(buf, "%d", val); | |
1408 | return buf; | |
1409 | } | |
d91fb3f4 EC |
1410 | |
1411 | int get_guid(__u64 *guid, const char *arg) | |
1412 | { | |
3a1f602a | 1413 | unsigned long tmp; |
d91fb3f4 EC |
1414 | char *endptr; |
1415 | int i; | |
1416 | ||
1417 | #define GUID_STR_LEN 23 | |
1418 | /* Verify strict format: format string must be | |
1419 | * xx:xx:xx:xx:xx:xx:xx:xx where xx can be an arbitrary | |
1420 | * hex digit | |
1421 | */ | |
1422 | ||
1423 | if (strlen(arg) != GUID_STR_LEN) | |
1424 | return -1; | |
1425 | ||
1426 | /* make sure columns are in place */ | |
1427 | for (i = 0; i < 7; i++) | |
1428 | if (arg[2 + i * 3] != ':') | |
1429 | return -1; | |
1430 | ||
1431 | *guid = 0; | |
1432 | for (i = 0; i < 8; i++) { | |
1433 | tmp = strtoul(arg + i * 3, &endptr, 16); | |
1434 | if (endptr != arg + i * 3 + 2) | |
1435 | return -1; | |
1436 | ||
1437 | if (tmp > 255) | |
1438 | return -1; | |
1439 | ||
1440 | *guid |= tmp << (56 - 8 * i); | |
1441 | } | |
1442 | ||
1443 | return 0; | |
1444 | } | |
56e3eb4c NA |
1445 | |
1446 | /* This is a necessary workaround for multicast route dumps */ | |
1447 | int get_real_family(int rtm_type, int rtm_family) | |
1448 | { | |
1449 | if (rtm_type != RTN_MULTICAST) | |
1450 | return rtm_family; | |
1451 | ||
5ce897a0 HL |
1452 | if (rtm_family == RTNL_FAMILY_IPMR) |
1453 | return AF_INET; | |
1454 | ||
1455 | if (rtm_family == RTNL_FAMILY_IP6MR) | |
1456 | return AF_INET6; | |
1457 | ||
1458 | return rtm_family; | |
56e3eb4c | 1459 | } |
8d15e012 | 1460 | |
9a7bd544 SP |
1461 | /* Based on copy_rtnl_link_stats() from kernel at net/core/rtnetlink.c */ |
1462 | static void copy_rtnl_link_stats64(struct rtnl_link_stats64 *stats64, | |
1463 | const struct rtnl_link_stats *stats) | |
1464 | { | |
1465 | __u64 *a = (__u64 *)stats64; | |
1466 | const __u32 *b = (const __u32 *)stats; | |
1467 | const __u32 *e = b + sizeof(*stats) / sizeof(*b); | |
1468 | ||
1469 | while (b < e) | |
1470 | *a++ = *b++; | |
1471 | } | |
1472 | ||
c7a3b229 LB |
1473 | #define IPSTATS_MIB_MAX_LEN (__IPSTATS_MIB_MAX * sizeof(__u64)) |
1474 | static void get_snmp_counters(struct rtnl_link_stats64 *stats64, | |
1475 | struct rtattr *s) | |
1476 | { | |
1477 | __u64 *mib = (__u64 *)RTA_DATA(s); | |
1478 | ||
1479 | memset(stats64, 0, sizeof(*stats64)); | |
1480 | ||
1481 | stats64->rx_packets = mib[IPSTATS_MIB_INPKTS]; | |
1482 | stats64->rx_bytes = mib[IPSTATS_MIB_INOCTETS]; | |
1483 | stats64->tx_packets = mib[IPSTATS_MIB_OUTPKTS]; | |
1484 | stats64->tx_bytes = mib[IPSTATS_MIB_OUTOCTETS]; | |
1485 | stats64->rx_errors = mib[IPSTATS_MIB_INDISCARDS]; | |
1486 | stats64->tx_errors = mib[IPSTATS_MIB_OUTDISCARDS]; | |
1487 | stats64->multicast = mib[IPSTATS_MIB_INMCASTPKTS]; | |
1488 | stats64->rx_frame_errors = mib[IPSTATS_MIB_CSUMERRORS]; | |
1489 | } | |
1490 | ||
9a7bd544 SP |
1491 | int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64, |
1492 | struct rtattr *tb[]) | |
1493 | { | |
1494 | struct rtnl_link_stats stats; | |
1495 | void *s; | |
1496 | struct rtattr *rta; | |
1497 | int size, len; | |
1498 | ||
1499 | if (tb[IFLA_STATS64]) { | |
1500 | rta = tb[IFLA_STATS64]; | |
1501 | size = sizeof(struct rtnl_link_stats64); | |
1502 | s = stats64; | |
1503 | } else if (tb[IFLA_STATS]) { | |
1504 | rta = tb[IFLA_STATS]; | |
1505 | size = sizeof(struct rtnl_link_stats); | |
1506 | s = &stats; | |
c7a3b229 LB |
1507 | } else if (tb[IFLA_PROTINFO]) { |
1508 | struct rtattr *ptb[IPSTATS_MIB_MAX_LEN + 1]; | |
1509 | ||
1510 | parse_rtattr_nested(ptb, IPSTATS_MIB_MAX_LEN, | |
1511 | tb[IFLA_PROTINFO]); | |
1512 | if (ptb[IFLA_INET6_STATS]) | |
1513 | get_snmp_counters(stats64, ptb[IFLA_INET6_STATS]); | |
1514 | return sizeof(*stats64); | |
9a7bd544 SP |
1515 | } else { |
1516 | return -1; | |
1517 | } | |
1518 | ||
1519 | len = RTA_PAYLOAD(rta); | |
1520 | if (len < size) | |
1521 | memset(s + len, 0, size - len); | |
1522 | else | |
1523 | len = size; | |
1524 | ||
1525 | memcpy(s, RTA_DATA(rta), len); | |
1526 | ||
1527 | if (s != stats64) | |
1528 | copy_rtnl_link_stats64(stats64, s); | |
1529 | return size; | |
1530 | } | |
1531 | ||
4f6b7338 | 1532 | #ifdef NEED_STRLCPY |
8d15e012 PS |
1533 | size_t strlcpy(char *dst, const char *src, size_t size) |
1534 | { | |
50ea3c64 PS |
1535 | size_t srclen = strlen(src); |
1536 | ||
8d15e012 | 1537 | if (size) { |
50ea3c64 PS |
1538 | size_t minlen = min(srclen, size - 1); |
1539 | ||
1540 | memcpy(dst, src, minlen); | |
1541 | dst[minlen] = '\0'; | |
8d15e012 | 1542 | } |
50ea3c64 | 1543 | return srclen; |
8d15e012 PS |
1544 | } |
1545 | ||
1546 | size_t strlcat(char *dst, const char *src, size_t size) | |
1547 | { | |
1548 | size_t dlen = strlen(dst); | |
1549 | ||
50ea3c64 | 1550 | if (dlen >= size) |
8d15e012 PS |
1551 | return dlen + strlen(src); |
1552 | ||
1553 | return dlen + strlcpy(dst + dlen, src, size - dlen); | |
1554 | } | |
4f6b7338 | 1555 | #endif |
ba2fc55b LB |
1556 | |
1557 | void drop_cap(void) | |
1558 | { | |
1559 | #ifdef HAVE_LIBCAP | |
1560 | /* don't harmstring root/sudo */ | |
1561 | if (getuid() != 0 && geteuid() != 0) { | |
1562 | cap_t capabilities; | |
9b13cc98 LB |
1563 | cap_value_t net_admin = CAP_NET_ADMIN; |
1564 | cap_flag_t inheritable = CAP_INHERITABLE; | |
1565 | cap_flag_value_t is_set; | |
ba2fc55b LB |
1566 | |
1567 | capabilities = cap_get_proc(); | |
1568 | if (!capabilities) | |
1569 | exit(EXIT_FAILURE); | |
9b13cc98 LB |
1570 | if (cap_get_flag(capabilities, net_admin, inheritable, |
1571 | &is_set) != 0) | |
ba2fc55b | 1572 | exit(EXIT_FAILURE); |
9b13cc98 LB |
1573 | /* apps with ambient caps can fork and call ip */ |
1574 | if (is_set == CAP_CLEAR) { | |
1575 | if (cap_clear(capabilities) != 0) | |
1576 | exit(EXIT_FAILURE); | |
1577 | if (cap_set_proc(capabilities) != 0) | |
1578 | exit(EXIT_FAILURE); | |
1579 | } | |
ba2fc55b LB |
1580 | cap_free(capabilities); |
1581 | } | |
1582 | #endif | |
1583 | } | |
abf70ef4 DT |
1584 | |
1585 | int get_time(unsigned int *time, const char *str) | |
1586 | { | |
1587 | double t; | |
1588 | char *p; | |
1589 | ||
1590 | t = strtod(str, &p); | |
1591 | if (p == str) | |
1592 | return -1; | |
1593 | ||
1594 | if (*p) { | |
1595 | if (strcasecmp(p, "s") == 0 || strcasecmp(p, "sec") == 0 || | |
1596 | strcasecmp(p, "secs") == 0) | |
1597 | t *= TIME_UNITS_PER_SEC; | |
1598 | else if (strcasecmp(p, "ms") == 0 || strcasecmp(p, "msec") == 0 || | |
1599 | strcasecmp(p, "msecs") == 0) | |
1600 | t *= TIME_UNITS_PER_SEC/1000; | |
1601 | else if (strcasecmp(p, "us") == 0 || strcasecmp(p, "usec") == 0 || | |
1602 | strcasecmp(p, "usecs") == 0) | |
1603 | t *= TIME_UNITS_PER_SEC/1000000; | |
1604 | else | |
1605 | return -1; | |
1606 | } | |
1607 | ||
1608 | *time = t; | |
1609 | return 0; | |
1610 | } | |
1611 | ||
b8795a32 | 1612 | static void print_time(char *buf, int len, __u32 time) |
abf70ef4 DT |
1613 | { |
1614 | double tmp = time; | |
1615 | ||
1616 | if (tmp >= TIME_UNITS_PER_SEC) | |
1617 | snprintf(buf, len, "%.1fs", tmp/TIME_UNITS_PER_SEC); | |
1618 | else if (tmp >= TIME_UNITS_PER_SEC/1000) | |
1619 | snprintf(buf, len, "%.1fms", tmp/(TIME_UNITS_PER_SEC/1000)); | |
1620 | else | |
1621 | snprintf(buf, len, "%uus", time); | |
1622 | } | |
1623 | ||
1624 | char *sprint_time(__u32 time, char *buf) | |
1625 | { | |
1626 | print_time(buf, SPRINT_BSIZE-1, time); | |
1627 | return buf; | |
1628 | } | |
1629 | ||
1630 | /* 64 bit times are represented internally in nanoseconds */ | |
1631 | int get_time64(__s64 *time, const char *str) | |
1632 | { | |
1633 | double nsec; | |
1634 | char *p; | |
1635 | ||
1636 | nsec = strtod(str, &p); | |
1637 | if (p == str) | |
1638 | return -1; | |
1639 | ||
1640 | if (*p) { | |
1641 | if (strcasecmp(p, "s") == 0 || | |
1642 | strcasecmp(p, "sec") == 0 || | |
1643 | strcasecmp(p, "secs") == 0) | |
1644 | nsec *= NSEC_PER_SEC; | |
1645 | else if (strcasecmp(p, "ms") == 0 || | |
1646 | strcasecmp(p, "msec") == 0 || | |
1647 | strcasecmp(p, "msecs") == 0) | |
1648 | nsec *= NSEC_PER_MSEC; | |
1649 | else if (strcasecmp(p, "us") == 0 || | |
1650 | strcasecmp(p, "usec") == 0 || | |
1651 | strcasecmp(p, "usecs") == 0) | |
1652 | nsec *= NSEC_PER_USEC; | |
1653 | else if (strcasecmp(p, "ns") == 0 || | |
1654 | strcasecmp(p, "nsec") == 0 || | |
1655 | strcasecmp(p, "nsecs") == 0) | |
1656 | nsec *= 1; | |
1657 | else | |
1658 | return -1; | |
1659 | } | |
1660 | ||
1661 | *time = nsec; | |
1662 | return 0; | |
1663 | } | |
1664 | ||
b8795a32 | 1665 | static void print_time64(char *buf, int len, __s64 time) |
abf70ef4 DT |
1666 | { |
1667 | double nsec = time; | |
1668 | ||
1669 | if (time >= NSEC_PER_SEC) | |
1670 | snprintf(buf, len, "%.3fs", nsec/NSEC_PER_SEC); | |
1671 | else if (time >= NSEC_PER_MSEC) | |
1672 | snprintf(buf, len, "%.3fms", nsec/NSEC_PER_MSEC); | |
1673 | else if (time >= NSEC_PER_USEC) | |
1674 | snprintf(buf, len, "%.3fus", nsec/NSEC_PER_USEC); | |
1675 | else | |
1676 | snprintf(buf, len, "%lldns", time); | |
1677 | } | |
1678 | ||
1679 | char *sprint_time64(__s64 time, char *buf) | |
1680 | { | |
1681 | print_time64(buf, SPRINT_BSIZE-1, time); | |
1682 | return buf; | |
1683 | } |