]>
Commit | Line | Data |
---|---|---|
6054c1eb | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
aba5acdf SH |
2 | #ifndef __UTILS_H__ |
3 | #define __UTILS_H__ 1 | |
4 | ||
5bd9dd49 | 5 | #include <sys/types.h> |
aba5acdf SH |
6 | #include <asm/types.h> |
7 | #include <resolv.h> | |
56b94061 | 8 | #include <stdlib.h> |
e998e118 | 9 | #include <stdbool.h> |
05b3b344 | 10 | #include <time.h> |
aba5acdf | 11 | |
6d2fd4a5 LB |
12 | #ifdef HAVE_LIBBSD |
13 | #include <bsd/string.h> | |
14 | #endif | |
15 | ||
aba5acdf SH |
16 | #include "libnetlink.h" |
17 | #include "ll_map.h" | |
18 | #include "rtm_map.h" | |
f5b50a18 | 19 | #include "json_print.h" |
aba5acdf SH |
20 | |
21 | extern int preferred_family; | |
b68d9837 | 22 | extern int human_readable; |
aba5acdf SH |
23 | extern int show_stats; |
24 | extern int show_details; | |
25 | extern int show_raw; | |
26 | extern int resolve_hosts; | |
27 | extern int oneline; | |
5d295bb8 | 28 | extern int brief; |
5df60772 | 29 | extern int json; |
663c3cb2 | 30 | extern int pretty; |
90f93024 | 31 | extern int timestamp; |
79aa79d0 | 32 | extern int timestamp_short; |
ec7aff5c | 33 | extern const char * _SL_; |
64c79560 | 34 | extern int max_flush_loops; |
a3aa47a5 | 35 | extern int batch_mode; |
ca697cee | 36 | extern int numeric; |
b13ba03f | 37 | extern bool do_all; |
aba5acdf | 38 | |
f6793eec DB |
39 | #ifndef CONFDIR |
40 | #define CONFDIR "/etc/iproute2" | |
41 | #endif | |
42 | ||
aba5acdf SH |
43 | #define SPRINT_BSIZE 64 |
44 | #define SPRINT_BUF(x) char x[SPRINT_BSIZE] | |
45 | ||
892e2124 | 46 | void incomplete_command(void) __attribute__((noreturn)); |
aba5acdf SH |
47 | |
48 | #define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0) | |
c7699875 | 49 | #define NEXT_ARG_OK() (argc - 1 > 0) |
faa8a463 | 50 | #define NEXT_ARG_FWD() do { argv++; argc--; } while(0) |
c7699875 | 51 | #define PREV_ARG() do { argv--; argc++; } while(0) |
aba5acdf | 52 | |
abf70ef4 DT |
53 | #define TIME_UNITS_PER_SEC 1000000 |
54 | #define NSEC_PER_USEC 1000 | |
55 | #define NSEC_PER_MSEC 1000000 | |
56 | #define NSEC_PER_SEC 1000000000LL | |
57 | ||
aba5acdf SH |
58 | typedef struct |
59 | { | |
93ae2835 EB |
60 | __u16 flags; |
61 | __u16 bytelen; | |
aba5acdf | 62 | __s16 bitlen; |
93ae2835 EB |
63 | /* These next two fields match rtvia */ |
64 | __u16 family; | |
4af44716 | 65 | __u32 data[64]; |
aba5acdf SH |
66 | } inet_prefix; |
67 | ||
7bf5e876 SP |
68 | enum { |
69 | PREFIXLEN_SPECIFIED = (1 << 0), | |
70 | ADDRTYPE_INET = (1 << 1), | |
71 | ADDRTYPE_UNSPEC = (1 << 2), | |
72 | ADDRTYPE_MULTI = (1 << 3), | |
73 | ||
74 | ADDRTYPE_INET_UNSPEC = ADDRTYPE_INET | ADDRTYPE_UNSPEC, | |
75 | ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI | |
76 | }; | |
77 | ||
9cc173d4 SP |
78 | static inline void inet_prefix_reset(inet_prefix *p) |
79 | { | |
80 | p->flags = 0; | |
81 | } | |
82 | ||
7bf5e876 SP |
83 | static inline bool is_addrtype_inet(const inet_prefix *p) |
84 | { | |
85 | return p->flags & ADDRTYPE_INET; | |
86 | } | |
87 | ||
88 | static inline bool is_addrtype_inet_unspec(const inet_prefix *p) | |
89 | { | |
90 | return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET_UNSPEC; | |
91 | } | |
92 | ||
93 | static inline bool is_addrtype_inet_multi(const inet_prefix *p) | |
94 | { | |
95 | return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET_MULTI; | |
96 | } | |
97 | ||
98 | static inline bool is_addrtype_inet_not_unspec(const inet_prefix *p) | |
99 | { | |
100 | return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET; | |
101 | } | |
102 | ||
103 | static inline bool is_addrtype_inet_not_multi(const inet_prefix *p) | |
104 | { | |
105 | return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET; | |
106 | } | |
f082b64f | 107 | |
aba5acdf SH |
108 | #define DN_MAXADDL 20 |
109 | #ifndef AF_DECnet | |
110 | #define AF_DECnet 12 | |
111 | #endif | |
112 | ||
ae665a52 | 113 | struct dn_naddr |
aba5acdf SH |
114 | { |
115 | unsigned short a_len; | |
116 | unsigned char a_addr[DN_MAXADDL]; | |
117 | }; | |
118 | ||
dacc5d41 EB |
119 | #ifndef AF_MPLS |
120 | # define AF_MPLS 28 | |
121 | #endif | |
f005b700 KJ |
122 | #ifndef IPPROTO_MPLS |
123 | #define IPPROTO_MPLS 137 | |
124 | #endif | |
dacc5d41 | 125 | |
f900a216 PK |
126 | #ifndef CLOCK_TAI |
127 | # define CLOCK_TAI 11 | |
128 | #endif | |
129 | ||
2abc3d76 BT |
130 | #ifndef AF_XDP |
131 | # define AF_XDP 44 | |
132 | # if AF_MAX < 45 | |
133 | # undef AF_MAX | |
134 | # define AF_MAX 45 | |
135 | # endif | |
136 | #endif | |
137 | ||
892e2124 SH |
138 | __u32 get_addr32(const char *name); |
139 | int get_addr_1(inet_prefix *dst, const char *arg, int family); | |
140 | int get_prefix_1(inet_prefix *dst, char *arg, int family); | |
141 | int get_addr(inet_prefix *dst, const char *arg, int family); | |
142 | int get_prefix(inet_prefix *dst, char *arg, int family); | |
143 | int mask2bits(__u32 netmask); | |
27c523e2 | 144 | int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family); |
5866bddd | 145 | int get_addr_ila(__u64 *val, const char *arg); |
aba5acdf | 146 | |
927e3cfb | 147 | int read_prop(const char *dev, char *prop, long *value); |
609640f5 | 148 | int get_hex(char c); |
892e2124 SH |
149 | int get_integer(int *val, const char *arg, int base); |
150 | int get_unsigned(unsigned *val, const char *arg, int base); | |
151 | int get_time_rtt(unsigned *val, const char *arg, int *raw); | |
aba5acdf SH |
152 | #define get_byte get_u8 |
153 | #define get_ushort get_u16 | |
154 | #define get_short get_s16 | |
a066bac8 | 155 | int get_s64(__s64 *val, const char *arg, int base); |
892e2124 SH |
156 | int get_u64(__u64 *val, const char *arg, int base); |
157 | int get_u32(__u32 *val, const char *arg, int base); | |
158 | int get_s32(__s32 *val, const char *arg, int base); | |
159 | int get_u16(__u16 *val, const char *arg, int base); | |
892e2124 | 160 | int get_u8(__u8 *val, const char *arg, int base); |
9f7401fa SD |
161 | int get_be64(__be64 *val, const char *arg, int base); |
162 | int get_be32(__be32 *val, const char *arg, int base); | |
163 | int get_be16(__be16 *val, const char *arg, int base); | |
5866bddd | 164 | int get_addr64(__u64 *ap, const char *cp); |
f3be0e63 PM |
165 | int get_rate(unsigned int *rate, const char *str); |
166 | int get_rate64(__u64 *rate, const char *str); | |
44396bdf | 167 | int get_size(unsigned int *size, const char *str); |
aba5acdf | 168 | |
1c570c50 | 169 | int hex2mem(const char *buf, uint8_t *mem, int count); |
89ae5020 SD |
170 | char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen); |
171 | __u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len); | |
5866bddd TH |
172 | #define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx") |
173 | int addr64_n2a(__u64 addr, char *buff, size_t len); | |
aba5acdf | 174 | |
892e2124 | 175 | int af_bit_len(int af); |
f3a2ddc1 | 176 | |
a418e451 | 177 | const char *format_host_r(int af, int len, const void *addr, |
04457b3d | 178 | char *buf, int buflen); |
663c3cb2 SH |
179 | #define format_host_rta_r(af, rta, buf, buflen) \ |
180 | format_host_r(af, RTA_PAYLOAD(rta), RTA_DATA(rta), \ | |
181 | buf, buflen) | |
182 | ||
a418e451 | 183 | const char *format_host(int af, int lne, const void *addr); |
d49f934c PS |
184 | #define format_host_rta(af, rta) \ |
185 | format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta)) | |
2e96d2cc | 186 | const char *rt_addr_n2a_r(int af, int len, const void *addr, |
04457b3d | 187 | char *buf, int buflen); |
2e96d2cc | 188 | const char *rt_addr_n2a(int af, int len, const void *addr); |
7faf1588 PS |
189 | #define rt_addr_n2a_rta(af, rta) \ |
190 | rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta)) | |
aba5acdf | 191 | |
892e2124 SH |
192 | int read_family(const char *name); |
193 | const char *family_name(int family); | |
45c90d19 | 194 | |
c7699875 | 195 | void missarg(const char *) __attribute__((noreturn)); |
04457b3d SH |
196 | void invarg(const char *, const char *) __attribute__((noreturn)); |
197 | void duparg(const char *, const char *) __attribute__((noreturn)); | |
198 | void duparg2(const char *, const char *) __attribute__((noreturn)); | |
fe99adbc | 199 | int nodev(const char *dev); |
625df645 | 200 | int check_ifname(const char *); |
3aa0e51b | 201 | int check_altifname(const char *name); |
625df645 | 202 | int get_ifname(char *, const char *); |
fcac9665 | 203 | const char *get_ifname_rta(int ifindex, const struct rtattr *rta); |
1f420318 | 204 | bool matches(const char *prefix, const char *string); |
892e2124 | 205 | int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits); |
27c523e2 | 206 | int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta); |
aba5acdf | 207 | |
dacc5d41 | 208 | const char *mpls_ntop(int af, const void *addr, char *str, size_t len); |
4af44716 | 209 | int mpls_pton(int af, const char *src, void *addr, size_t alen); |
dacc5d41 | 210 | |
aba5acdf | 211 | extern int __iproute2_hz_internal; |
e3c27c2d | 212 | int __get_hz(void); |
aba5acdf SH |
213 | |
214 | static __inline__ int get_hz(void) | |
215 | { | |
216 | if (__iproute2_hz_internal == 0) | |
217 | __iproute2_hz_internal = __get_hz(); | |
218 | return __iproute2_hz_internal; | |
219 | } | |
220 | ||
5e8bc631 | 221 | extern int __iproute2_user_hz_internal; |
892e2124 | 222 | int __get_user_hz(void); |
5e8bc631 | 223 | |
224 | static __inline__ int get_user_hz(void) | |
225 | { | |
226 | if (__iproute2_user_hz_internal == 0) | |
227 | __iproute2_user_hz_internal = __get_user_hz(); | |
228 | return __iproute2_user_hz_internal; | |
229 | } | |
230 | ||
8cd09e61 | 231 | static inline __u32 nl_mgrp(__u32 group) |
232 | { | |
56b94061 | 233 | if (group > 31 ) { |
234 | fprintf(stderr, "Use setsockopt for this group %d\n", group); | |
235 | exit(-1); | |
236 | } | |
237 | return group ? (1 << (group - 1)) : 0; | |
8cd09e61 | 238 | } |
239 | ||
8c0f7a16 NA |
240 | /* courtesy of bridge-utils */ |
241 | static inline unsigned long __tv_to_jiffies(const struct timeval *tv) | |
242 | { | |
243 | unsigned long long jif; | |
244 | ||
245 | jif = 1000000ULL * tv->tv_sec + tv->tv_usec; | |
246 | ||
247 | return jif/10000; | |
248 | } | |
249 | ||
250 | static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies) | |
251 | { | |
252 | unsigned long long tvusec; | |
253 | ||
254 | tvusec = 10000ULL*jiffies; | |
255 | tv->tv_sec = tvusec/1000000; | |
256 | tv->tv_usec = tvusec - 1000000 * tv->tv_sec; | |
257 | } | |
8cd09e61 | 258 | |
7c72df5a ID |
259 | void print_escape_buf(const __u8 *buf, size_t len, const char *escape); |
260 | ||
90f93024 | 261 | int print_timestamp(FILE *fp); |
ddb1129b | 262 | void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n); |
90f93024 | 263 | |
4328b687 | 264 | unsigned int print_name_and_link(const char *fmt, |
f5b50a18 SP |
265 | const char *name, struct rtattr *tb[]); |
266 | ||
afdc1194 LR |
267 | #define BIT(nr) (1UL << (nr)) |
268 | ||
02d2ae55 SH |
269 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
270 | ||
6256f8c9 DB |
271 | #define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)])) |
272 | ||
273 | #ifndef offsetof | |
274 | # define offsetof(type, member) ((size_t) &((type *)0)->member) | |
275 | #endif | |
276 | ||
277 | #ifndef min | |
278 | # define min(x, y) ({ \ | |
279 | typeof(x) _min1 = (x); \ | |
280 | typeof(y) _min2 = (y); \ | |
281 | (void) (&_min1 == &_min2); \ | |
282 | _min1 < _min2 ? _min1 : _min2; }) | |
283 | #endif | |
284 | ||
11c39b5e DB |
285 | #ifndef __check_format_string |
286 | # define __check_format_string(pos_str, pos_args) \ | |
287 | __attribute__ ((format (printf, (pos_str), (pos_args)))) | |
288 | #endif | |
289 | ||
32e93fb7 DB |
290 | #define _textify(x) #x |
291 | #define textify(x) _textify(x) | |
292 | ||
1e529305 RP |
293 | #define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32)) |
294 | #define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32)) | |
295 | ||
351efcde | 296 | extern int cmdlineno; |
892e2124 SH |
297 | ssize_t getcmdline(char **line, size_t *len, FILE *in); |
298 | int makeargs(char *line, char *argv[], int maxargs); | |
351efcde | 299 | |
b217df10 | 300 | char *int_to_str(int val, char *buf); |
d91fb3f4 | 301 | int get_guid(__u64 *guid, const char *arg); |
56e3eb4c | 302 | int get_real_family(int rtm_type, int rtm_family); |
b217df10 | 303 | |
903818fb MC |
304 | int cmd_exec(const char *cmd, char **argv, bool do_fork, |
305 | int (*setup)(void *), void *arg); | |
1dafceb1 | 306 | int make_path(const char *path, mode_t mode); |
d5e6ee0d DY |
307 | char *find_cgroup2_mount(bool do_mount); |
308 | __u64 get_cgroup2_id(const char *path); | |
309 | char *get_cgroup2_path(__u64 id, bool full); | |
f443565f | 310 | int get_command_name(const char *pid, char *comm, size_t len); |
08bd33d7 | 311 | |
9a7bd544 SP |
312 | int get_rtnl_link_stats_rta(struct rtnl_link_stats64 *stats64, |
313 | struct rtattr *tb[]); | |
314 | ||
05b3b344 | 315 | #ifdef NEED_STRLCPY |
8d15e012 PS |
316 | size_t strlcpy(char *dst, const char *src, size_t size); |
317 | size_t strlcat(char *dst, const char *src, size_t size); | |
05b3b344 | 318 | #endif |
8d15e012 | 319 | |
ba2fc55b LB |
320 | void drop_cap(void); |
321 | ||
abf70ef4 DT |
322 | int get_time(unsigned int *time, const char *str); |
323 | int get_time64(__s64 *time, const char *str); | |
abf70ef4 DT |
324 | char *sprint_time(__u32 time, char *buf); |
325 | char *sprint_time64(__s64 time, char *buf); | |
326 | ||
1d9a81b8 PM |
327 | int do_batch(const char *name, bool force, |
328 | int (*cmd)(int argc, char *argv[], void *user), void *user); | |
329 | ||
82604d28 PM |
330 | int parse_one_of(const char *msg, const char *realval, const char * const *list, |
331 | size_t len, int *p_err); | |
332 | bool parse_on_off(const char *msg, const char *realval, int *p_err); | |
333 | ||
c13216f7 PM |
334 | int parse_mapping_num_all(__u32 *keyp, const char *key); |
335 | int parse_mapping_gen(int *argcp, char ***argvp, | |
336 | int (*key_cb)(__u32 *keyp, const char *key), | |
337 | int (*mapping_cb)(__u32 key, char *value, void *data), | |
338 | void *mapping_cb_data); | |
66a2d714 | 339 | int parse_mapping(int *argcp, char ***argvp, bool allow_all, |
28e663ee PM |
340 | int (*mapping_cb)(__u32 key, char *value, void *data), |
341 | void *mapping_cb_data); | |
342 | ||
a9642c5f PP |
343 | struct str_num_map { |
344 | const char *str; | |
6c769949 | 345 | unsigned int num; |
a9642c5f PP |
346 | }; |
347 | ||
348 | int str_map_lookup_str(const struct str_num_map *map, const char *needle); | |
6c769949 PP |
349 | const char *str_map_lookup_uint(const struct str_num_map *map, |
350 | unsigned int val); | |
a9642c5f | 351 | const char *str_map_lookup_u16(const struct str_num_map *map, uint16_t val); |
249465d3 | 352 | const char *str_map_lookup_u8(const struct str_num_map *map, uint8_t val); |
a9642c5f | 353 | |
bd3709c3 PP |
354 | unsigned int get_str_char_count(const char *str, int match); |
355 | int str_split_by_char(char *str, char **before, char **after, int match); | |
356 | ||
357 | #define INDENT_STR_MAXLEN 32 | |
358 | ||
359 | struct indent_mem { | |
360 | int indent_level; | |
361 | char indent_str[INDENT_STR_MAXLEN + 1]; | |
362 | }; | |
363 | ||
364 | struct indent_mem *alloc_indent_mem(void); | |
365 | void free_indent_mem(struct indent_mem *mem); | |
366 | void inc_indent(struct indent_mem *mem); | |
367 | void dec_indent(struct indent_mem *mem); | |
368 | void print_indent(struct indent_mem *mem); | |
369 | ||
aba5acdf | 370 | #endif /* __UTILS_H__ */ |