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>
20 #include <sys/socket.h>
21 #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>
34 #include "namespace.h"
36 int timestamp_short
= 0;
38 int get_integer(int *val
, const char *arg
, int base
)
46 res
= strtol(arg
, &ptr
, base
);
48 /* If there were no digits at all, strtol() stores
49 * the original value of nptr in *endptr (and returns 0).
50 * In particular, if *nptr is not '\0' but **endptr is '\0' on return,
51 * the entire string is valid.
53 if (!ptr
|| ptr
== arg
|| *ptr
)
56 /* If an underflow occurs, strtol() returns LONG_MIN.
57 * If an overflow occurs, strtol() returns LONG_MAX.
58 * In both cases, errno is set to ERANGE.
60 if ((res
== LONG_MAX
|| res
== LONG_MIN
) && errno
== ERANGE
)
63 /* Outside range of int */
64 if (res
< INT_MIN
|| res
> INT_MAX
)
71 int mask2bits(__u32 netmask
)
74 __u32 mask
= ntohl(netmask
);
77 /* a valid netmask must be 2^n - 1 */
78 if ((host
& (host
+ 1)) != 0)
81 for (; mask
; mask
<<= 1)
86 static int get_netmask(unsigned *val
, const char *arg
, int base
)
90 if (!get_unsigned(val
, arg
, base
))
93 /* try coverting dotted quad to CIDR */
94 if (!get_addr_1(&addr
, arg
, AF_INET
) && addr
.family
== AF_INET
) {
95 int b
= mask2bits(addr
.data
[0]);
106 int get_unsigned(unsigned *val
, const char *arg
, int base
)
114 res
= strtoul(arg
, &ptr
, base
);
116 /* empty string or trailing non-digits */
117 if (!ptr
|| ptr
== arg
|| *ptr
)
121 if (res
== ULONG_MAX
&& errno
== ERANGE
)
124 /* out side range of unsigned */
133 * get_time_rtt is "translated" from a similar routine "get_time" in
134 * tc_util.c. We don't use the exact same routine because tc passes
135 * microseconds to the kernel and the callers of get_time_rtt want to
136 * pass milliseconds (standard unit for rtt values since 2.6.27), and
137 * have a different assumption for the units of a "raw" number.
139 int get_time_rtt(unsigned *val
, const char *arg
, int *raw
)
145 if (strchr(arg
, '.') != NULL
) {
155 if ((t
== HUGE_VALF
|| t
== HUGE_VALL
) && errno
== ERANGE
)
158 res
= strtoul(arg
, &p
, 0);
165 if (res
== ULONG_MAX
&& errno
== ERANGE
)
177 if (strcasecmp(p
, "s") == 0 || strcasecmp(p
, "sec")==0 ||
178 strcasecmp(p
, "secs")==0)
180 else if (strcasecmp(p
, "ms") == 0 || strcasecmp(p
, "msec")==0 ||
181 strcasecmp(p
, "msecs") == 0)
182 t
*= 1.0; /* allow suffix, do nothing */
187 /* emulate ceil() without having to bring-in -lm and always be >= 1 */
197 int get_u64(__u64
*val
, const char *arg
, int base
)
199 unsigned long long res
;
205 res
= strtoull(arg
, &ptr
, base
);
207 /* empty string or trailing non-digits */
208 if (!ptr
|| ptr
== arg
|| *ptr
)
212 if (res
== ULLONG_MAX
&& errno
== ERANGE
)
215 /* in case ULL is 128 bits */
216 if (res
> 0xFFFFFFFFFFFFFFFFULL
)
223 int get_u32(__u32
*val
, const char *arg
, int base
)
230 res
= strtoul(arg
, &ptr
, base
);
232 /* empty string or trailing non-digits */
233 if (!ptr
|| ptr
== arg
|| *ptr
)
237 if (res
== ULONG_MAX
&& errno
== ERANGE
)
240 /* in case UL > 32 bits */
241 if (res
> 0xFFFFFFFFUL
)
248 int get_u16(__u16
*val
, const char *arg
, int base
)
255 res
= strtoul(arg
, &ptr
, base
);
257 /* empty string or trailing non-digits */
258 if (!ptr
|| ptr
== arg
|| *ptr
)
262 if (res
== ULONG_MAX
&& errno
== ERANGE
)
272 int get_u8(__u8
*val
, const char *arg
, int base
)
280 res
= strtoul(arg
, &ptr
, base
);
281 /* empty string or trailing non-digits */
282 if (!ptr
|| ptr
== arg
|| *ptr
)
286 if (res
== ULONG_MAX
&& errno
== ERANGE
)
296 int get_s32(__s32
*val
, const char *arg
, int base
)
305 res
= strtol(arg
, &ptr
, base
);
306 if (!ptr
|| ptr
== arg
|| *ptr
)
308 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
310 if (res
> INT32_MAX
|| res
< INT32_MIN
)
317 int get_s16(__s16
*val
, const char *arg
, int base
)
324 res
= strtol(arg
, &ptr
, base
);
325 if (!ptr
|| ptr
== arg
|| *ptr
)
327 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
329 if (res
> 0x7FFF || res
< -0x8000)
336 int get_s8(__s8
*val
, const char *arg
, int base
)
343 res
= strtol(arg
, &ptr
, base
);
344 if (!ptr
|| ptr
== arg
|| *ptr
)
346 if ((res
== LONG_MIN
|| res
== LONG_MAX
) && errno
== ERANGE
)
348 if (res
> 0x7F || res
< -0x80)
354 /* This uses a non-standard parsing (ie not inet_aton, or inet_pton)
355 * because of legacy choice to parse 10.8 as 10.8.0.0 not 10.0.0.8
357 static int get_addr_ipv4(__u8
*ap
, const char *cp
)
361 for (i
= 0; i
< 4; i
++) {
365 n
= strtoul(cp
, &endp
, 0);
367 return -1; /* bogus network value */
369 if (endp
== cp
) /* no digits */
377 if (i
== 3 || *endp
!= '.')
378 return -1; /* extra characters */
385 int get_addr_1(inet_prefix
*addr
, const char *name
, int family
)
387 memset(addr
, 0, sizeof(*addr
));
389 if (strcmp(name
, "default") == 0 ||
390 strcmp(name
, "all") == 0 ||
391 strcmp(name
, "any") == 0) {
392 if (family
== AF_DECnet
)
394 addr
->family
= family
;
395 addr
->bytelen
= (family
== AF_INET6
? 16 : 4);
400 if (strchr(name
, ':')) {
401 addr
->family
= AF_INET6
;
402 if (family
!= AF_UNSPEC
&& family
!= AF_INET6
)
404 if (inet_pton(AF_INET6
, name
, addr
->data
) <= 0)
411 if (family
== AF_DECnet
) {
413 addr
->family
= AF_DECnet
;
414 if (dnet_pton(AF_DECnet
, name
, &dna
) <= 0)
416 memcpy(addr
->data
, dna
.a_addr
, 2);
422 addr
->family
= AF_INET
;
423 if (family
!= AF_UNSPEC
&& family
!= AF_INET
)
426 if (get_addr_ipv4((__u8
*)addr
->data
, name
) <= 0)
434 int af_bit_len(int af
)
450 int af_byte_len(int af
)
452 return af_bit_len(af
) / 8;
455 int get_prefix_1(inet_prefix
*dst
, char *arg
, int family
)
461 memset(dst
, 0, sizeof(*dst
));
463 if (strcmp(arg
, "default") == 0 ||
464 strcmp(arg
, "any") == 0 ||
465 strcmp(arg
, "all") == 0) {
466 if (family
== AF_DECnet
)
468 dst
->family
= family
;
474 slash
= strchr(arg
, '/');
478 err
= get_addr_1(dst
, arg
, family
);
480 dst
->bitlen
= af_bit_len(dst
->family
);
483 if (get_netmask(&plen
, slash
+1, 0)
484 || plen
> dst
->bitlen
) {
488 dst
->flags
|= PREFIXLEN_SPECIFIED
;
498 int get_addr(inet_prefix
*dst
, const char *arg
, int family
)
500 if (family
== AF_PACKET
) {
501 fprintf(stderr
, "Error: \"%s\" may be inet address, but it is not allowed in this context.\n", arg
);
504 if (get_addr_1(dst
, arg
, family
)) {
505 fprintf(stderr
, "Error: an inet address is expected rather than \"%s\".\n", arg
);
511 int get_prefix(inet_prefix
*dst
, char *arg
, int family
)
513 if (family
== AF_PACKET
) {
514 fprintf(stderr
, "Error: \"%s\" may be inet prefix, but it is not allowed in this context.\n", arg
);
517 if (get_prefix_1(dst
, arg
, family
)) {
518 fprintf(stderr
, "Error: an inet prefix is expected rather than \"%s\".\n", arg
);
524 __u32
get_addr32(const char *name
)
527 if (get_addr_1(&addr
, name
, AF_INET
)) {
528 fprintf(stderr
, "Error: an IP address is expected rather than \"%s\"\n", name
);
534 void incomplete_command(void)
536 fprintf(stderr
, "Command line is not complete. Try option \"help\"\n");
540 void missarg(const char *key
)
542 fprintf(stderr
, "Error: argument \"%s\" is required\n", key
);
546 void invarg(const char *msg
, const char *arg
)
548 fprintf(stderr
, "Error: argument \"%s\" is wrong: %s\n", arg
, msg
);
552 void duparg(const char *key
, const char *arg
)
554 fprintf(stderr
, "Error: duplicate \"%s\": \"%s\" is the second value.\n", key
, arg
);
558 void duparg2(const char *key
, const char *arg
)
560 fprintf(stderr
, "Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n", key
, arg
);
564 int matches(const char *cmd
, const char *pattern
)
566 int len
= strlen(cmd
);
567 if (len
> strlen(pattern
))
569 return memcmp(pattern
, cmd
, len
);
572 int inet_addr_match(const inet_prefix
*a
, const inet_prefix
*b
, int bits
)
574 const __u32
*a1
= a
->data
;
575 const __u32
*a2
= b
->data
;
576 int words
= bits
>> 0x05;
581 if (memcmp(a1
, a2
, words
<< 2))
591 mask
= htonl((0xffffffff) << (0x20 - bits
));
593 if ((w1
^ w2
) & mask
)
600 int __iproute2_hz_internal
;
609 return atoi(getenv("HZ")) ? : HZ
;
611 if (getenv("PROC_NET_PSCHED")) {
612 snprintf(name
, sizeof(name
)-1, "%s", getenv("PROC_NET_PSCHED"));
613 } else if (getenv("PROC_ROOT")) {
614 snprintf(name
, sizeof(name
)-1, "%s/net/psched", getenv("PROC_ROOT"));
616 strcpy(name
, "/proc/net/psched");
618 fp
= fopen(name
, "r");
622 if (fscanf(fp
, "%*08x%*08x%08x%08x", &nom
, &denom
) == 2)
632 int __iproute2_user_hz_internal
;
634 int __get_user_hz(void)
636 return sysconf(_SC_CLK_TCK
);
639 const char *rt_addr_n2a(int af
, int len
, const void *addr
, char *buf
, int buflen
)
644 return inet_ntop(af
, addr
, buf
, buflen
);
646 return ipx_ntop(af
, addr
, buf
, buflen
);
649 struct dn_naddr dna
= { 2, { 0, 0, }};
650 memcpy(dna
.a_addr
, addr
, 2);
651 return dnet_ntop(af
, &dna
, buf
, buflen
);
658 #ifdef RESOLVE_HOSTNAMES
661 struct namerec
*next
;
667 static struct namerec
*nht
[NHASH
];
669 static const char *resolve_address(const void *addr
, int len
, int af
)
672 struct hostent
*h_ent
;
677 if (af
== AF_INET6
&& ((__u32
*)addr
)[0] == 0 &&
678 ((__u32
*)addr
)[1] == 0 && ((__u32
*)addr
)[2] == htonl(0xffff)) {
684 hash
= *(__u32
*)(addr
+ len
- 4) % NHASH
;
686 for (n
= nht
[hash
]; n
; n
= n
->next
) {
687 if (n
->addr
.family
== af
&&
688 n
->addr
.bytelen
== len
&&
689 memcmp(n
->addr
.data
, addr
, len
) == 0)
692 if ((n
= malloc(sizeof(*n
))) == NULL
)
695 n
->addr
.bytelen
= len
;
697 memcpy(n
->addr
.data
, addr
, len
);
704 if ((h_ent
= gethostbyaddr(addr
, len
, af
)) != NULL
)
705 n
->name
= strdup(h_ent
->h_name
);
707 /* Even if we fail, "negative" entry is remembered. */
712 const char *format_host(int af
, int len
, const void *addr
,
713 char *buf
, int buflen
)
715 #ifdef RESOLVE_HOSTNAMES
719 len
= len
<= 0 ? af_byte_len(af
) : len
;
722 (n
= resolve_address(addr
, len
, af
)) != NULL
)
726 return rt_addr_n2a(af
, len
, addr
, buf
, buflen
);
730 char *hexstring_n2a(const __u8
*str
, int len
, char *buf
, int blen
)
735 for (i
=0; i
<len
; i
++) {
738 sprintf(ptr
, "%02x", str
[i
]);
745 __u8
* hexstring_a2n(const char *str
, __u8
*buf
, int blen
)
752 while (cnt
< blen
&& strlen(str
) > 1) {
756 strncpy(tmpstr
, str
, 2);
758 tmp
= strtoul(tmpstr
, &endptr
, 16);
759 if (errno
!= 0 || tmp
> 0xFF || *endptr
!= '\0')
767 int print_timestamp(FILE *fp
)
772 gettimeofday(&tv
, NULL
);
773 tm
= localtime(&tv
.tv_sec
);
775 if (timestamp_short
) {
778 strftime(tshort
, sizeof(tshort
), "%Y-%m-%dT%H:%M:%S", tm
);
779 fprintf(fp
, "[%s.%06ld] ", tshort
, tv
.tv_usec
);
781 char *tstr
= asctime(tm
);
783 tstr
[strlen(tstr
)-1] = 0;
784 fprintf(fp
, "Timestamp: %s %ld usec\n",
793 /* Like glibc getline but handle continuation lines and comments */
794 ssize_t
getcmdline(char **linep
, size_t *lenp
, FILE *in
)
799 if ((cc
= getline(linep
, lenp
, in
)) < 0)
800 return cc
; /* eof or error */
803 cp
= strchr(*linep
, '#');
807 while ((cp
= strstr(*linep
, "\\\n")) != NULL
) {
812 if ((cc1
= getline(&line1
, &len1
, in
)) < 0) {
813 fprintf(stderr
, "Missing continuation line\n");
820 cp
= strchr(line1
, '#');
824 *lenp
= strlen(*linep
) + strlen(line1
) + 1;
825 *linep
= realloc(*linep
, *lenp
);
827 fprintf(stderr
, "Out of memory\n");
832 strcat(*linep
, line1
);
838 /* split command line into argument vector */
839 int makeargs(char *line
, char *argv
[], int maxargs
)
841 static const char ws
[] = " \t\r\n";
845 for (cp
= strtok(line
, ws
); cp
; cp
= strtok(NULL
, ws
)) {
846 if (argc
>= (maxargs
- 1)) {
847 fprintf(stderr
, "Too many arguments to command\n");
857 int inet_get_addr(const char *src
, __u32
*dst
, struct in6_addr
*dst6
)
859 if (strchr(src
, ':'))
860 return inet_pton(AF_INET6
, src
, dst6
);
862 return inet_pton(AF_INET
, src
, dst
);
865 void print_nlmsg_timestamp(FILE *fp
, const struct nlmsghdr
*n
)
868 time_t secs
= ((__u32
*)NLMSG_DATA(n
))[0];
869 long usecs
= ((__u32
*)NLMSG_DATA(n
))[1];
870 tstr
= asctime(localtime(&secs
));
871 tstr
[strlen(tstr
)-1] = 0;
872 fprintf(fp
, "Timestamp: %s %lu us\n", tstr
, usecs
);
875 static int on_netns(char *nsname
, void *arg
)
877 struct netns_func
*f
= arg
;
879 if (netns_switch(nsname
))
882 return f
->func(nsname
, f
->arg
);
885 static int on_netns_label(char *nsname
, void *arg
)
887 printf("\nnetns: %s\n", nsname
);
888 return on_netns(nsname
, arg
);
891 int do_each_netns(int (*func
)(char *nsname
, void *arg
), void *arg
,
894 struct netns_func nsf
= { .func
= func
, .arg
= arg
};
897 return netns_foreach(on_netns_label
, &nsf
);
899 return netns_foreach(on_netns
, &nsf
);
902 char *int_to_str(int val
, char *buf
)
904 sprintf(buf
, "%d", val
);