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