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