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.
9 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <asm/types.h>
26 #include <linux/pkt_sched.h>
27 #include <linux/param.h>
28 #include <linux/if_arp.h>
29 #include <linux/mpls.h>
30 #include <linux/snmp.h>
35 #include <sys/capability.h>
41 #include "namespace.h"
46 const char *_SL_
= "\n";
48 int read_prop(const char *dev
, char *prop
, long *value
)
50 char fname
[128], buf
[80], *endp
, *nl
;
55 ret
= snprintf(fname
, sizeof(fname
), "/sys/class/net/%s/%s",
58 if (ret
<= 0 || ret
>= sizeof(fname
)) {
59 fprintf(stderr
, "could not build pathname for property\n");
63 fp
= fopen(fname
, "r");
65 fprintf(stderr
, "fopen %s: %s\n", fname
, strerror(errno
));
69 if (!fgets(buf
, sizeof(buf
), fp
)) {
70 fprintf(stderr
, "property \"%s\" in file %s is currently unknown\n", prop
, fname
);
75 nl
= strchr(buf
, '\n');
80 result
= strtol(buf
, &endp
, 0);
82 if (*endp
|| buf
== endp
) {
83 fprintf(stderr
, "value \"%s\" in file %s is not a number\n",
88 if ((result
== LONG_MAX
|| result
== LONG_MIN
) && errno
== ERANGE
) {
89 fprintf(stderr
, "strtol %s: %s", fname
, strerror(errno
));
96 fprintf(stderr
, "Failed to parse %s\n", fname
);
100 /* Parse a percent e.g: '30%'
101 * return: 0 = ok, -1 = error, 1 = out of range
103 int parse_percent(double *val
, const char *str
)
107 *val
= strtod(str
, &p
) / 100.;
108 if (*val
== HUGE_VALF
|| *val
== HUGE_VALL
)
110 if (*p
&& strcmp(p
, "%"))
118 if (c
>= 'A' && c
<= 'F')
120 if (c
>= 'a' && c
<= 'f')
122 if (c
>= '0' && c
<= '9')
128 int get_integer(int *val
, const char *arg
, int base
)
136 res
= strtol(arg
, &ptr
, base
);
138 /* If there were no digits at all, strtol() stores
139 * the original value of nptr in *endptr (and returns 0).
140 * In particular, if *nptr is not '\0' but **endptr is '\0' on return,
141 * the entire string is valid.
143 if (!ptr
|| ptr
== arg
|| *ptr
)
146 /* If an underflow occurs, strtol() returns LONG_MIN.
147 * If an overflow occurs, strtol() returns LONG_MAX.
148 * In both cases, errno is set to ERANGE.
150 if ((res
== LONG_MAX
|| res
== LONG_MIN
) && errno
== ERANGE
)
153 /* Outside range of int */
154 if (res
< INT_MIN
|| res
> INT_MAX
)
161 int mask2bits(__u32 netmask
)
163 unsigned int bits
= 0;
164 __u32 mask
= ntohl(netmask
);
167 /* a valid netmask must be 2^n - 1 */
168 if ((host
& (host
+ 1)) != 0)
171 for (; mask
; mask
<<= 1)
176 static int get_netmask(unsigned int *val
, const char *arg
, int base
)
180 if (!get_unsigned(val
, arg
, base
))
183 /* try coverting dotted quad to CIDR */
184 if (!get_addr_1(&addr
, arg
, AF_INET
) && addr
.family
== AF_INET
) {
185 int b
= mask2bits(addr
.data
[0]);
196 int get_unsigned(unsigned int *val
, const char *arg
, int base
)
204 res
= strtoul(arg
, &ptr
, base
);
206 /* empty string or trailing non-digits */
207 if (!ptr
|| ptr
== arg
|| *ptr
)
211 if (res
== ULONG_MAX
&& errno
== ERANGE
)
214 /* out side range of unsigned */
223 * get_time_rtt is "translated" from a similar routine "get_time" in
224 * tc_util.c. We don't use the exact same routine because tc passes
225 * microseconds to the kernel and the callers of get_time_rtt want to
226 * pass milliseconds (standard unit for rtt values since 2.6.27), and
227 * have a different assumption for the units of a "raw" number.
229 int get_time_rtt(unsigned int *val
, const char *arg
, int *raw
)
235 if (strchr(arg
, '.') != NULL
) {
245 if ((t
== HUGE_VALF
|| t
== HUGE_VALL
) && errno
== ERANGE
)
248 res
= strtoul(arg
, &p
, 0);
255 if (res
== ULONG_MAX
&& errno
== ERANGE
)
267 if (strcasecmp(p
, "s") == 0 ||
268 strcasecmp(p
, "sec") == 0 ||
269 strcasecmp(p
, "secs") == 0)
271 else if (strcasecmp(p
, "ms") == 0 ||
272 strcasecmp(p
, "msec") == 0 ||
273 strcasecmp(p
, "msecs") == 0)
274 t
*= 1.0; /* allow suffix, do nothing */
279 /* emulate ceil() without having to bring-in -lm and always be >= 1 */
288 int get_u64(__u64
*val
, const char *arg
, int base
)
290 unsigned long long res
;
296 res
= strtoull(arg
, &ptr
, base
);
298 /* empty string or trailing non-digits */
299 if (!ptr
|| ptr
== arg
|| *ptr
)
303 if (res
== ULLONG_MAX
&& errno
== ERANGE
)
306 /* in case ULL is 128 bits */
307 if (res
> 0xFFFFFFFFFFFFFFFFULL
)
314 int get_u32(__u32
*val
, const char *arg
, int base
)
321 res
= strtoul(arg
, &ptr
, base
);
323 /* empty string or trailing non-digits */
324 if (!ptr
|| ptr
== arg
|| *ptr
)
328 if (res
== ULONG_MAX
&& errno
== ERANGE
)
331 /* in case UL > 32 bits */
332 if (res
> 0xFFFFFFFFUL
)
339 int get_u16(__u16
*val
, const char *arg
, int base
)
346 res
= strtoul(arg
, &ptr
, base
);
348 /* empty string or trailing non-digits */
349 if (!ptr
|| ptr
== arg
|| *ptr
)
353 if (res
== ULONG_MAX
&& errno
== ERANGE
)
363 int get_u8(__u8
*val
, const char *arg
, int base
)
371 res
= strtoul(arg
, &ptr
, base
);
372 /* empty string or trailing non-digits */
373 if (!ptr
|| ptr
== arg
|| *ptr
)
377 if (res
== ULONG_MAX
&& errno
== ERANGE
)
387 int get_s64(__s64
*val
, const char *arg
, int base
)
396 res
= strtoll(arg
, &ptr
, base
);
397 if (!ptr
|| ptr
== arg
|| *ptr
)
399 if ((res
== LLONG_MIN
|| res
== LLONG_MAX
) && errno
== ERANGE
)
401 if (res
> INT64_MAX
|| res
< INT64_MIN
)
408 int get_s32(__s32
*val
, const char *arg
, int base
)
417 res
= strtol(arg
, &ptr
, base
);
418 if (!ptr
|| ptr
== arg
|| *ptr
)
420 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
422 if (res
> INT32_MAX
|| res
< INT32_MIN
)
429 int get_s16(__s16
*val
, const char *arg
, int base
)
436 res
= strtol(arg
, &ptr
, base
);
437 if (!ptr
|| ptr
== arg
|| *ptr
)
439 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
441 if (res
> 0x7FFF || res
< -0x8000)
448 int get_s8(__s8
*val
, const char *arg
, int base
)
455 res
= strtol(arg
, &ptr
, base
);
456 if (!ptr
|| ptr
== arg
|| *ptr
)
458 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
460 if (res
> 0x7F || res
< -0x80)
466 int get_be64(__be64
*val
, const char *arg
, int base
)
469 int ret
= get_u64(&v
, arg
, base
);
477 int get_be32(__be32
*val
, const char *arg
, int base
)
480 int ret
= get_u32(&v
, arg
, base
);
488 int get_be16(__be16
*val
, const char *arg
, int base
)
491 int ret
= get_u16(&v
, arg
, base
);
499 /* This uses a non-standard parsing (ie not inet_aton, or inet_pton)
500 * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8
502 static int get_addr_ipv4(__u8
*ap
, const char *cp
)
506 for (i
= 0; i
< 4; i
++) {
510 n
= strtoul(cp
, &endp
, 0);
512 return -1; /* bogus network value */
514 if (endp
== cp
) /* no digits */
522 if (i
== 3 || *endp
!= '.')
523 return -1; /* extra characters */
530 int get_addr64(__u64
*ap
, const char *cp
)
539 for (i
= 0; i
< 4; i
++) {
543 n
= strtoul(cp
, &endp
, 16);
545 return -1; /* bogus network value */
547 if (endp
== cp
) /* no digits */
550 val
.v16
[i
] = htons(n
);
555 if (i
== 3 || *endp
!= ':')
556 return -1; /* extra characters */
565 static void set_address_type(inet_prefix
*addr
)
567 switch (addr
->family
) {
570 addr
->flags
|= ADDRTYPE_INET_UNSPEC
;
571 else if (IN_MULTICAST(ntohl(addr
->data
[0])))
572 addr
->flags
|= ADDRTYPE_INET_MULTI
;
574 addr
->flags
|= ADDRTYPE_INET
;
577 if (IN6_IS_ADDR_UNSPECIFIED(addr
->data
))
578 addr
->flags
|= ADDRTYPE_INET_UNSPEC
;
579 else if (IN6_IS_ADDR_MULTICAST(addr
->data
))
580 addr
->flags
|= ADDRTYPE_INET_MULTI
;
582 addr
->flags
|= ADDRTYPE_INET
;
587 static int __get_addr_1(inet_prefix
*addr
, const char *name
, int family
)
589 memset(addr
, 0, sizeof(*addr
));
591 if (strcmp(name
, "default") == 0) {
592 if ((family
== AF_DECnet
) || (family
== AF_MPLS
))
594 addr
->family
= family
;
595 addr
->bytelen
= af_byte_len(addr
->family
);
597 addr
->flags
|= PREFIXLEN_SPECIFIED
;
601 if (strcmp(name
, "all") == 0 ||
602 strcmp(name
, "any") == 0) {
603 if ((family
== AF_DECnet
) || (family
== AF_MPLS
))
605 addr
->family
= family
;
611 if (family
== AF_PACKET
) {
614 len
= ll_addr_a2n((char *) &addr
->data
, sizeof(addr
->data
),
619 addr
->family
= AF_PACKET
;
621 addr
->bitlen
= len
* 8;
625 if (strchr(name
, ':')) {
626 addr
->family
= AF_INET6
;
627 if (family
!= AF_UNSPEC
&& family
!= AF_INET6
)
629 if (inet_pton(AF_INET6
, name
, addr
->data
) <= 0)
636 if (family
== AF_DECnet
) {
639 addr
->family
= AF_DECnet
;
640 if (dnet_pton(AF_DECnet
, name
, &dna
) <= 0)
642 memcpy(addr
->data
, dna
.a_addr
, 2);
648 if (family
== AF_MPLS
) {
649 unsigned int maxlabels
;
652 addr
->family
= AF_MPLS
;
653 if (mpls_pton(AF_MPLS
, name
, addr
->data
,
654 sizeof(addr
->data
)) <= 0)
658 /* How many bytes do I need? */
659 maxlabels
= sizeof(addr
->data
) / sizeof(struct mpls_label
);
660 for (i
= 0; i
< maxlabels
; i
++) {
661 if (ntohl(addr
->data
[i
]) & MPLS_LS_S_MASK
) {
662 addr
->bytelen
= (i
+ 1)*4;
669 addr
->family
= AF_INET
;
670 if (family
!= AF_UNSPEC
&& family
!= AF_INET
)
673 if (get_addr_ipv4((__u8
*)addr
->data
, name
) <= 0)
681 int get_addr_1(inet_prefix
*addr
, const char *name
, int family
)
685 ret
= __get_addr_1(addr
, name
, family
);
689 set_address_type(addr
);
693 int af_bit_len(int af
)
711 int af_byte_len(int af
)
713 return af_bit_len(af
) / 8;
716 int get_prefix_1(inet_prefix
*dst
, char *arg
, int family
)
719 int err
, bitlen
, flags
;
721 slash
= strchr(arg
, '/');
725 err
= get_addr_1(dst
, arg
, family
);
733 bitlen
= af_bit_len(dst
->family
);
739 if (dst
->bitlen
== -2)
741 if (get_netmask(&plen
, slash
+ 1, 0))
746 flags
|= PREFIXLEN_SPECIFIED
;
749 if (dst
->bitlen
== -2)
754 dst
->bitlen
= bitlen
;
759 static const char *family_name_verbose(int family
)
761 if (family
== AF_UNSPEC
)
763 return family_name(family
);
766 int get_addr(inet_prefix
*dst
, const char *arg
, int family
)
768 if (get_addr_1(dst
, arg
, family
)) {
770 "Error: %s address is expected rather than \"%s\".\n",
771 family_name_verbose(family
), arg
);
777 int get_addr_rta(inet_prefix
*dst
, const struct rtattr
*rta
, int family
)
779 const int len
= RTA_PAYLOAD(rta
);
780 const void *data
= RTA_DATA(rta
);
784 dst
->family
= AF_INET
;
786 memcpy(dst
->data
, data
, 4);
789 dst
->family
= AF_INET6
;
791 memcpy(dst
->data
, data
, 16);
794 dst
->family
= AF_DECnet
;
796 memcpy(dst
->data
, data
, 2);
799 dst
->family
= AF_IPX
;
801 memcpy(dst
->data
, data
, 10);
807 if (family
!= AF_UNSPEC
&& family
!= dst
->family
)
813 set_address_type(dst
);
817 int get_prefix(inet_prefix
*dst
, char *arg
, int family
)
819 if (family
== AF_PACKET
) {
821 "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n",
826 if (get_prefix_1(dst
, arg
, family
)) {
828 "Error: %s prefix is expected rather than \"%s\".\n",
829 family_name_verbose(family
), arg
);
835 __u32
get_addr32(const char *name
)
839 if (get_addr_1(&addr
, name
, AF_INET
)) {
841 "Error: an IP address is expected rather than \"%s\"\n",
848 void incomplete_command(void)
850 fprintf(stderr
, "Command line is not complete. Try option \"help\"\n");
854 void missarg(const char *key
)
856 fprintf(stderr
, "Error: argument \"%s\" is required\n", key
);
860 void invarg(const char *msg
, const char *arg
)
862 fprintf(stderr
, "Error: argument \"%s\" is wrong: %s\n", arg
, msg
);
866 void duparg(const char *key
, const char *arg
)
869 "Error: duplicate \"%s\": \"%s\" is the second value.\n",
874 void duparg2(const char *key
, const char *arg
)
877 "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n",
882 int nodev(const char *dev
)
884 fprintf(stderr
, "Cannot find device \"%s\"\n", dev
);
888 int check_ifname(const char *name
)
890 /* These checks mimic kernel checks in dev_valid_name */
893 if (strlen(name
) >= IFNAMSIZ
)
897 if (*name
== '/' || isspace(*name
))
904 /* buf is assumed to be IFNAMSIZ */
905 int get_ifname(char *buf
, const char *name
)
909 ret
= check_ifname(name
);
911 strncpy(buf
, name
, IFNAMSIZ
);
916 const char *get_ifname_rta(int ifindex
, const struct rtattr
*rta
)
921 name
= rta_getattr_str(rta
);
924 "BUG: device with ifindex %d has nil ifname\n",
926 name
= ll_idx_n2a(ifindex
);
929 if (check_ifname(name
))
935 int matches(const char *cmd
, const char *pattern
)
937 int len
= strlen(cmd
);
939 if (len
> strlen(pattern
))
941 return memcmp(pattern
, cmd
, len
);
944 int inet_addr_match(const inet_prefix
*a
, const inet_prefix
*b
, int bits
)
946 const __u32
*a1
= a
->data
;
947 const __u32
*a2
= b
->data
;
948 int words
= bits
>> 0x05;
953 if (memcmp(a1
, a2
, words
<< 2))
963 mask
= htonl((0xffffffff) << (0x20 - bits
));
965 if ((w1
^ w2
) & mask
)
972 int inet_addr_match_rta(const inet_prefix
*m
, const struct rtattr
*rta
)
976 if (!rta
|| m
->family
== AF_UNSPEC
|| m
->bitlen
<= 0)
979 if (get_addr_rta(&dst
, rta
, m
->family
))
982 return inet_addr_match(&dst
, m
, m
->bitlen
);
985 int __iproute2_hz_internal
;
994 return atoi(getenv("HZ")) ? : HZ
;
996 if (getenv("PROC_NET_PSCHED"))
997 snprintf(name
, sizeof(name
)-1,
998 "%s", getenv("PROC_NET_PSCHED"));
999 else if (getenv("PROC_ROOT"))
1000 snprintf(name
, sizeof(name
)-1,
1001 "%s/net/psched", getenv("PROC_ROOT"));
1003 strcpy(name
, "/proc/net/psched");
1005 fp
= fopen(name
, "r");
1008 unsigned int nom
, denom
;
1010 if (fscanf(fp
, "%*08x%*08x%08x%08x", &nom
, &denom
) == 2)
1020 int __iproute2_user_hz_internal
;
1022 int __get_user_hz(void)
1024 return sysconf(_SC_CLK_TCK
);
1027 const char *rt_addr_n2a_r(int af
, int len
,
1028 const void *addr
, char *buf
, int buflen
)
1033 return inet_ntop(af
, addr
, buf
, buflen
);
1035 return mpls_ntop(af
, addr
, buf
, buflen
);
1037 return ipx_ntop(af
, addr
, buf
, buflen
);
1040 struct dn_naddr dna
= { 2, { 0, 0, } };
1042 memcpy(dna
.a_addr
, addr
, 2);
1043 return dnet_ntop(af
, &dna
, buf
, buflen
);
1046 return ll_addr_n2a(addr
, len
, ARPHRD_VOID
, buf
, buflen
);
1051 struct sockaddr_in sin
;
1052 struct sockaddr_in6 sin6
;
1055 switch (sa
->sa
.sa_family
) {
1057 return inet_ntop(AF_INET
, &sa
->sin
.sin_addr
,
1060 return inet_ntop(AF_INET6
, &sa
->sin6
.sin6_addr
,
1071 const char *rt_addr_n2a(int af
, int len
, const void *addr
)
1073 static char buf
[256];
1075 return rt_addr_n2a_r(af
, len
, addr
, buf
, 256);
1078 int read_family(const char *name
)
1080 int family
= AF_UNSPEC
;
1082 if (strcmp(name
, "inet") == 0)
1084 else if (strcmp(name
, "inet6") == 0)
1086 else if (strcmp(name
, "dnet") == 0)
1088 else if (strcmp(name
, "link") == 0)
1090 else if (strcmp(name
, "ipx") == 0)
1092 else if (strcmp(name
, "mpls") == 0)
1094 else if (strcmp(name
, "bridge") == 0)
1099 const char *family_name(int family
)
1101 if (family
== AF_INET
)
1103 if (family
== AF_INET6
)
1105 if (family
== AF_DECnet
)
1107 if (family
== AF_PACKET
)
1109 if (family
== AF_IPX
)
1111 if (family
== AF_MPLS
)
1113 if (family
== AF_BRIDGE
)
1118 #ifdef RESOLVE_HOSTNAMES
1120 struct namerec
*next
;
1126 static struct namerec
*nht
[NHASH
];
1128 static const char *resolve_address(const void *addr
, int len
, int af
)
1131 struct hostent
*h_ent
;
1133 static int notfirst
;
1136 if (af
== AF_INET6
&& ((__u32
*)addr
)[0] == 0 &&
1137 ((__u32
*)addr
)[1] == 0 && ((__u32
*)addr
)[2] == htonl(0xffff)) {
1143 hash
= *(__u32
*)(addr
+ len
- 4) % NHASH
;
1145 for (n
= nht
[hash
]; n
; n
= n
->next
) {
1146 if (n
->addr
.family
== af
&&
1147 n
->addr
.bytelen
== len
&&
1148 memcmp(n
->addr
.data
, addr
, len
) == 0)
1151 n
= malloc(sizeof(*n
));
1154 n
->addr
.family
= af
;
1155 n
->addr
.bytelen
= len
;
1157 memcpy(n
->addr
.data
, addr
, len
);
1158 n
->next
= nht
[hash
];
1160 if (++notfirst
== 1)
1164 h_ent
= gethostbyaddr(addr
, len
, af
);
1166 n
->name
= strdup(h_ent
->h_name
);
1168 /* Even if we fail, "negative" entry is remembered. */
1173 const char *format_host_r(int af
, int len
, const void *addr
,
1174 char *buf
, int buflen
)
1176 #ifdef RESOLVE_HOSTNAMES
1177 if (resolve_hosts
) {
1180 len
= len
<= 0 ? af_byte_len(af
) : len
;
1183 (n
= resolve_address(addr
, len
, af
)) != NULL
)
1187 return rt_addr_n2a_r(af
, len
, addr
, buf
, buflen
);
1190 const char *format_host(int af
, int len
, const void *addr
)
1192 static char buf
[256];
1194 return format_host_r(af
, len
, addr
, buf
, 256);
1198 char *hexstring_n2a(const __u8
*str
, int len
, char *buf
, int blen
)
1203 for (i
= 0; i
< len
; i
++) {
1206 sprintf(ptr
, "%02x", str
[i
]);
1213 __u8
*hexstring_a2n(const char *str
, __u8
*buf
, int blen
, unsigned int *len
)
1215 unsigned int cnt
= 0;
1218 if (strlen(str
) % 2)
1220 while (cnt
< blen
&& strlen(str
) > 1) {
1224 strncpy(tmpstr
, str
, 2);
1227 tmp
= strtoul(tmpstr
, &endptr
, 16);
1228 if (errno
!= 0 || tmp
> 0xFF || *endptr
!= '\0')
1240 int hex2mem(const char *buf
, uint8_t *mem
, int count
)
1245 for (i
= 0, j
= 0; i
< count
; i
++, j
+= 2) {
1246 c
= get_hex(buf
[j
]);
1252 c
= get_hex(buf
[j
+ 1]);
1262 int addr64_n2a(__u64 addr
, char *buff
, size_t len
)
1264 __u16
*words
= (__u16
*)&addr
;
1270 for (i
= 0; i
< 4; i
++) {
1271 v
= ntohs(words
[i
]);
1276 ret
= snprintf(&buff
[written
], len
- written
, "%x%s", v
, sep
);
1286 /* Print buffer and escape bytes that are !isprint or among 'escape' */
1287 void print_escape_buf(const __u8
*buf
, size_t len
, const char *escape
)
1291 for (i
= 0; i
< len
; ++i
) {
1292 if (isprint(buf
[i
]) && buf
[i
] != '\\' &&
1293 !strchr(escape
, buf
[i
]))
1294 printf("%c", buf
[i
]);
1296 printf("\\%03o", buf
[i
]);
1300 int print_timestamp(FILE *fp
)
1305 gettimeofday(&tv
, NULL
);
1306 tm
= localtime(&tv
.tv_sec
);
1308 if (timestamp_short
) {
1311 strftime(tshort
, sizeof(tshort
), "%Y-%m-%dT%H:%M:%S", tm
);
1312 fprintf(fp
, "[%s.%06ld] ", tshort
, tv
.tv_usec
);
1314 char *tstr
= asctime(tm
);
1316 tstr
[strlen(tstr
)-1] = 0;
1317 fprintf(fp
, "Timestamp: %s %ld usec\n",
1324 unsigned int print_name_and_link(const char *fmt
,
1325 const char *name
, struct rtattr
*tb
[])
1327 const char *link
= NULL
;
1328 unsigned int m_flag
= 0;
1331 if (tb
[IFLA_LINK
]) {
1332 int iflink
= rta_getattr_u32(tb
[IFLA_LINK
]);
1335 if (tb
[IFLA_LINK_NETNSID
]) {
1336 if (is_json_context()) {
1337 print_int(PRINT_JSON
,
1338 "link_index", NULL
, iflink
);
1340 link
= ll_idx_n2a(iflink
);
1343 link
= ll_index_to_name(iflink
);
1345 if (is_json_context()) {
1346 print_string(PRINT_JSON
,
1347 "link", NULL
, link
);
1351 m_flag
= ll_index_to_flags(iflink
);
1352 m_flag
= !(m_flag
& IFF_UP
);
1355 if (is_json_context())
1356 print_null(PRINT_JSON
, "link", NULL
, NULL
);
1362 snprintf(b1
, sizeof(b1
), "%s@%s", name
, link
);
1367 print_color_string(PRINT_ANY
, COLOR_IFNAME
, "ifname", fmt
, name
);
1374 /* Like glibc getline but handle continuation lines and comments */
1375 ssize_t
getcmdline(char **linep
, size_t *lenp
, FILE *in
)
1380 cc
= getline(linep
, lenp
, in
);
1382 return cc
; /* eof or error */
1385 cp
= strchr(*linep
, '#');
1389 while ((cp
= strstr(*linep
, "\\\n")) != NULL
) {
1394 cc1
= getline(&line1
, &len1
, in
);
1396 fprintf(stderr
, "Missing continuation line\n");
1403 cp
= strchr(line1
, '#');
1407 *lenp
= strlen(*linep
) + strlen(line1
) + 1;
1408 *linep
= realloc(*linep
, *lenp
);
1410 fprintf(stderr
, "Out of memory\n");
1415 strcat(*linep
, line1
);
1421 /* split command line into argument vector */
1422 int makeargs(char *line
, char *argv
[], int maxargs
)
1424 static const char ws
[] = " \t\r\n";
1429 /* skip leading whitespace */
1430 cp
+= strspn(cp
, ws
);
1435 if (argc
>= (maxargs
- 1)) {
1436 fprintf(stderr
, "Too many arguments to command\n");
1440 /* word begins with quote */
1441 if (*cp
== '\'' || *cp
== '"') {
1445 /* find ending quote */
1446 cp
= strchr(cp
, quote
);
1448 fprintf(stderr
, "Unterminated quoted string\n");
1454 /* find end of word */
1455 cp
+= strcspn(cp
, ws
);
1460 /* separate words */
1468 void print_nlmsg_timestamp(FILE *fp
, const struct nlmsghdr
*n
)
1471 time_t secs
= ((__u32
*)NLMSG_DATA(n
))[0];
1472 long usecs
= ((__u32
*)NLMSG_DATA(n
))[1];
1474 tstr
= asctime(localtime(&secs
));
1475 tstr
[strlen(tstr
)-1] = 0;
1476 fprintf(fp
, "Timestamp: %s %lu us\n", tstr
, usecs
);
1479 static int on_netns(char *nsname
, void *arg
)
1481 struct netns_func
*f
= arg
;
1483 if (netns_switch(nsname
))
1486 return f
->func(nsname
, f
->arg
);
1489 static int on_netns_label(char *nsname
, void *arg
)
1491 printf("\nnetns: %s\n", nsname
);
1492 return on_netns(nsname
, arg
);
1495 int do_each_netns(int (*func
)(char *nsname
, void *arg
), void *arg
,
1498 struct netns_func nsf
= { .func
= func
, .arg
= arg
};
1501 return netns_foreach(on_netns_label
, &nsf
);
1503 return netns_foreach(on_netns
, &nsf
);
1506 char *int_to_str(int val
, char *buf
)
1508 sprintf(buf
, "%d", val
);
1512 int get_guid(__u64
*guid
, const char *arg
)
1514 unsigned long int tmp
;
1518 #define GUID_STR_LEN 23
1519 /* Verify strict format: format string must be
1520 * xx:xx:xx:xx:xx:xx:xx:xx where xx can be an arbitrary
1524 if (strlen(arg
) != GUID_STR_LEN
)
1527 /* make sure columns are in place */
1528 for (i
= 0; i
< 7; i
++)
1529 if (arg
[2 + i
* 3] != ':')
1533 for (i
= 0; i
< 8; i
++) {
1534 tmp
= strtoul(arg
+ i
* 3, &endptr
, 16);
1535 if (endptr
!= arg
+ i
* 3 + 2)
1541 *guid
|= tmp
<< (56 - 8 * i
);
1547 /* This is a necessary workaround for multicast route dumps */
1548 int get_real_family(int rtm_type
, int rtm_family
)
1550 if (rtm_type
!= RTN_MULTICAST
)
1553 if (rtm_family
== RTNL_FAMILY_IPMR
)
1556 if (rtm_family
== RTNL_FAMILY_IP6MR
)
1562 /* Based on copy_rtnl_link_stats() from kernel at net/core/rtnetlink.c */
1563 static void copy_rtnl_link_stats64(struct rtnl_link_stats64
*stats64
,
1564 const struct rtnl_link_stats
*stats
)
1566 __u64
*a
= (__u64
*)stats64
;
1567 const __u32
*b
= (const __u32
*)stats
;
1568 const __u32
*e
= b
+ sizeof(*stats
) / sizeof(*b
);
1574 #define IPSTATS_MIB_MAX_LEN (__IPSTATS_MIB_MAX * sizeof(__u64))
1575 static void get_snmp_counters(struct rtnl_link_stats64
*stats64
,
1578 __u64
*mib
= (__u64
*)RTA_DATA(s
);
1580 memset(stats64
, 0, sizeof(*stats64
));
1582 stats64
->rx_packets
= mib
[IPSTATS_MIB_INPKTS
];
1583 stats64
->rx_bytes
= mib
[IPSTATS_MIB_INOCTETS
];
1584 stats64
->tx_packets
= mib
[IPSTATS_MIB_OUTPKTS
];
1585 stats64
->tx_bytes
= mib
[IPSTATS_MIB_OUTOCTETS
];
1586 stats64
->rx_errors
= mib
[IPSTATS_MIB_INDISCARDS
];
1587 stats64
->tx_errors
= mib
[IPSTATS_MIB_OUTDISCARDS
];
1588 stats64
->multicast
= mib
[IPSTATS_MIB_INMCASTPKTS
];
1589 stats64
->rx_frame_errors
= mib
[IPSTATS_MIB_CSUMERRORS
];
1592 int get_rtnl_link_stats_rta(struct rtnl_link_stats64
*stats64
,
1593 struct rtattr
*tb
[])
1595 struct rtnl_link_stats stats
;
1600 if (tb
[IFLA_STATS64
]) {
1601 rta
= tb
[IFLA_STATS64
];
1602 size
= sizeof(struct rtnl_link_stats64
);
1604 } else if (tb
[IFLA_STATS
]) {
1605 rta
= tb
[IFLA_STATS
];
1606 size
= sizeof(struct rtnl_link_stats
);
1608 } else if (tb
[IFLA_PROTINFO
]) {
1609 struct rtattr
*ptb
[IPSTATS_MIB_MAX_LEN
+ 1];
1611 parse_rtattr_nested(ptb
, IPSTATS_MIB_MAX_LEN
,
1613 if (ptb
[IFLA_INET6_STATS
])
1614 get_snmp_counters(stats64
, ptb
[IFLA_INET6_STATS
]);
1615 return sizeof(*stats64
);
1620 len
= RTA_PAYLOAD(rta
);
1622 memset(s
+ len
, 0, size
- len
);
1626 memcpy(s
, RTA_DATA(rta
), len
);
1629 copy_rtnl_link_stats64(stats64
, s
);
1634 size_t strlcpy(char *dst
, const char *src
, size_t size
)
1636 size_t srclen
= strlen(src
);
1639 size_t minlen
= min(srclen
, size
- 1);
1641 memcpy(dst
, src
, minlen
);
1647 size_t strlcat(char *dst
, const char *src
, size_t size
)
1649 size_t dlen
= strlen(dst
);
1652 return dlen
+ strlen(src
);
1654 return dlen
+ strlcpy(dst
+ dlen
, src
, size
- dlen
);
1661 /* don't harmstring root/sudo */
1662 if (getuid() != 0 && geteuid() != 0) {
1664 cap_value_t net_admin
= CAP_NET_ADMIN
;
1665 cap_flag_t inheritable
= CAP_INHERITABLE
;
1666 cap_flag_value_t is_set
;
1668 capabilities
= cap_get_proc();
1671 if (cap_get_flag(capabilities
, net_admin
, inheritable
,
1674 /* apps with ambient caps can fork and call ip */
1675 if (is_set
== CAP_CLEAR
) {
1676 if (cap_clear(capabilities
) != 0)
1678 if (cap_set_proc(capabilities
) != 0)
1681 cap_free(capabilities
);
1686 int get_time(unsigned int *time
, const char *str
)
1691 t
= strtod(str
, &p
);
1696 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec") == 0 ||
1697 strcasecmp(p
, "secs") == 0)
1698 t
*= TIME_UNITS_PER_SEC
;
1699 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec") == 0 ||
1700 strcasecmp(p
, "msecs") == 0)
1701 t
*= TIME_UNITS_PER_SEC
/1000;
1702 else if (strcasecmp(p
, "us") == 0 || strcasecmp(p
, "usec") == 0 ||
1703 strcasecmp(p
, "usecs") == 0)
1704 t
*= TIME_UNITS_PER_SEC
/1000000;
1714 void print_time(char *buf
, int len
, __u32 time
)
1718 if (tmp
>= TIME_UNITS_PER_SEC
)
1719 snprintf(buf
, len
, "%.1fs", tmp
/TIME_UNITS_PER_SEC
);
1720 else if (tmp
>= TIME_UNITS_PER_SEC
/1000)
1721 snprintf(buf
, len
, "%.1fms", tmp
/(TIME_UNITS_PER_SEC
/1000));
1723 snprintf(buf
, len
, "%uus", time
);
1726 char *sprint_time(__u32 time
, char *buf
)
1728 print_time(buf
, SPRINT_BSIZE
-1, time
);
1732 /* 64 bit times are represented internally in nanoseconds */
1733 int get_time64(__s64
*time
, const char *str
)
1738 nsec
= strtod(str
, &p
);
1743 if (strcasecmp(p
, "s") == 0 ||
1744 strcasecmp(p
, "sec") == 0 ||
1745 strcasecmp(p
, "secs") == 0)
1746 nsec
*= NSEC_PER_SEC
;
1747 else if (strcasecmp(p
, "ms") == 0 ||
1748 strcasecmp(p
, "msec") == 0 ||
1749 strcasecmp(p
, "msecs") == 0)
1750 nsec
*= NSEC_PER_MSEC
;
1751 else if (strcasecmp(p
, "us") == 0 ||
1752 strcasecmp(p
, "usec") == 0 ||
1753 strcasecmp(p
, "usecs") == 0)
1754 nsec
*= NSEC_PER_USEC
;
1755 else if (strcasecmp(p
, "ns") == 0 ||
1756 strcasecmp(p
, "nsec") == 0 ||
1757 strcasecmp(p
, "nsecs") == 0)
1767 void print_time64(char *buf
, int len
, __s64 time
)
1771 if (time
>= NSEC_PER_SEC
)
1772 snprintf(buf
, len
, "%.3fs", nsec
/NSEC_PER_SEC
);
1773 else if (time
>= NSEC_PER_MSEC
)
1774 snprintf(buf
, len
, "%.3fms", nsec
/NSEC_PER_MSEC
);
1775 else if (time
>= NSEC_PER_USEC
)
1776 snprintf(buf
, len
, "%.3fus", nsec
/NSEC_PER_USEC
);
1778 snprintf(buf
, len
, "%lldns", time
);
1781 char *sprint_time64(__s64 time
, char *buf
)
1783 print_time64(buf
, SPRINT_BSIZE
-1, time
);