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