]> git.proxmox.com Git - mirror_iproute2.git/blame - include/utils.h
Drop capabilities if not running ip exec vrf with libcap
[mirror_iproute2.git] / include / utils.h
CommitLineData
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
SH
11
12#include "libnetlink.h"
13#include "ll_map.h"
14#include "rtm_map.h"
15
16extern int preferred_family;
b68d9837 17extern int human_readable;
1e264abc 18extern int use_iec;
aba5acdf
SH
19extern int show_stats;
20extern int show_details;
21extern int show_raw;
22extern int resolve_hosts;
23extern int oneline;
5d295bb8 24extern int brief;
5df60772 25extern int json;
90f93024 26extern int timestamp;
79aa79d0 27extern int timestamp_short;
ec7aff5c 28extern const char * _SL_;
64c79560 29extern int max_flush_loops;
a3aa47a5 30extern int batch_mode;
b13ba03f 31extern bool do_all;
aba5acdf 32
f6793eec
DB
33#ifndef CONFDIR
34#define CONFDIR "/etc/iproute2"
35#endif
36
aba5acdf
SH
37#define SPRINT_BSIZE 64
38#define SPRINT_BUF(x) char x[SPRINT_BSIZE]
39
892e2124 40void incomplete_command(void) __attribute__((noreturn));
aba5acdf
SH
41
42#define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
c7699875 43#define NEXT_ARG_OK() (argc - 1 > 0)
faa8a463 44#define NEXT_ARG_FWD() do { argv++; argc--; } while(0)
c7699875 45#define PREV_ARG() do { argv--; argc++; } while(0)
aba5acdf
SH
46
47typedef struct
48{
93ae2835
EB
49 __u16 flags;
50 __u16 bytelen;
aba5acdf 51 __s16 bitlen;
93ae2835
EB
52 /* These next two fields match rtvia */
53 __u16 family;
4af44716 54 __u32 data[64];
aba5acdf
SH
55} inet_prefix;
56
7bf5e876
SP
57enum {
58 PREFIXLEN_SPECIFIED = (1 << 0),
59 ADDRTYPE_INET = (1 << 1),
60 ADDRTYPE_UNSPEC = (1 << 2),
61 ADDRTYPE_MULTI = (1 << 3),
62
63 ADDRTYPE_INET_UNSPEC = ADDRTYPE_INET | ADDRTYPE_UNSPEC,
64 ADDRTYPE_INET_MULTI = ADDRTYPE_INET | ADDRTYPE_MULTI
65};
66
67static inline bool is_addrtype_inet(const inet_prefix *p)
68{
69 return p->flags & ADDRTYPE_INET;
70}
71
72static inline bool is_addrtype_inet_unspec(const inet_prefix *p)
73{
74 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET_UNSPEC;
75}
76
77static inline bool is_addrtype_inet_multi(const inet_prefix *p)
78{
79 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET_MULTI;
80}
81
82static inline bool is_addrtype_inet_not_unspec(const inet_prefix *p)
83{
84 return (p->flags & ADDRTYPE_INET_UNSPEC) == ADDRTYPE_INET;
85}
86
87static inline bool is_addrtype_inet_not_multi(const inet_prefix *p)
88{
89 return (p->flags & ADDRTYPE_INET_MULTI) == ADDRTYPE_INET;
90}
f082b64f 91
aba5acdf
SH
92#define DN_MAXADDL 20
93#ifndef AF_DECnet
94#define AF_DECnet 12
95#endif
96
ae665a52 97struct dn_naddr
aba5acdf
SH
98{
99 unsigned short a_len;
100 unsigned char a_addr[DN_MAXADDL];
101};
102
103#define IPX_NODE_LEN 6
104
105struct ipx_addr {
106 u_int32_t ipx_net;
107 u_int8_t ipx_node[IPX_NODE_LEN];
108};
109
dacc5d41
EB
110#ifndef AF_MPLS
111# define AF_MPLS 28
112#endif
f005b700
KJ
113#ifndef IPPROTO_MPLS
114#define IPPROTO_MPLS 137
115#endif
dacc5d41 116
892e2124
SH
117__u32 get_addr32(const char *name);
118int get_addr_1(inet_prefix *dst, const char *arg, int family);
119int get_prefix_1(inet_prefix *dst, char *arg, int family);
120int get_addr(inet_prefix *dst, const char *arg, int family);
121int get_prefix(inet_prefix *dst, char *arg, int family);
122int mask2bits(__u32 netmask);
27c523e2 123int get_addr_rta(inet_prefix *dst, const struct rtattr *rta, int family);
5866bddd 124int get_addr_ila(__u64 *val, const char *arg);
aba5acdf 125
927e3cfb
ND
126int read_prop(const char *dev, char *prop, long *value);
127int parse_percent(double *val, const char *str);
609640f5 128int get_hex(char c);
892e2124
SH
129int get_integer(int *val, const char *arg, int base);
130int get_unsigned(unsigned *val, const char *arg, int base);
131int get_time_rtt(unsigned *val, const char *arg, int *raw);
aba5acdf
SH
132#define get_byte get_u8
133#define get_ushort get_u16
134#define get_short get_s16
892e2124
SH
135int get_u64(__u64 *val, const char *arg, int base);
136int get_u32(__u32 *val, const char *arg, int base);
137int get_s32(__s32 *val, const char *arg, int base);
138int get_u16(__u16 *val, const char *arg, int base);
139int get_s16(__s16 *val, const char *arg, int base);
140int get_u8(__u8 *val, const char *arg, int base);
141int get_s8(__s8 *val, const char *arg, int base);
9f7401fa
SD
142int get_be64(__be64 *val, const char *arg, int base);
143int get_be32(__be32 *val, const char *arg, int base);
144int get_be16(__be16 *val, const char *arg, int base);
5866bddd 145int get_addr64(__u64 *ap, const char *cp);
aba5acdf 146
1c570c50 147int hex2mem(const char *buf, uint8_t *mem, int count);
89ae5020
SD
148char *hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
149__u8 *hexstring_a2n(const char *str, __u8 *buf, int blen, unsigned int *len);
5866bddd
TH
150#define ADDR64_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
151int addr64_n2a(__u64 addr, char *buff, size_t len);
aba5acdf 152
892e2124
SH
153int af_bit_len(int af);
154int af_byte_len(int af);
f3a2ddc1 155
a418e451 156const char *format_host_r(int af, int len, const void *addr,
04457b3d 157 char *buf, int buflen);
a418e451 158const char *format_host(int af, int lne, const void *addr);
d49f934c
PS
159#define format_host_rta(af, rta) \
160 format_host(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
2e96d2cc 161const char *rt_addr_n2a_r(int af, int len, const void *addr,
04457b3d 162 char *buf, int buflen);
2e96d2cc 163const char *rt_addr_n2a(int af, int len, const void *addr);
7faf1588
PS
164#define rt_addr_n2a_rta(af, rta) \
165 rt_addr_n2a(af, RTA_PAYLOAD(rta), RTA_DATA(rta))
aba5acdf 166
892e2124
SH
167int read_family(const char *name);
168const char *family_name(int family);
45c90d19 169
c7699875 170void missarg(const char *) __attribute__((noreturn));
04457b3d
SH
171void invarg(const char *, const char *) __attribute__((noreturn));
172void duparg(const char *, const char *) __attribute__((noreturn));
173void duparg2(const char *, const char *) __attribute__((noreturn));
625df645
PS
174int check_ifname(const char *);
175int get_ifname(char *, const char *);
04457b3d 176int matches(const char *arg, const char *pattern);
892e2124 177int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
27c523e2 178int inet_addr_match_rta(const inet_prefix *m, const struct rtattr *rta);
aba5acdf
SH
179
180const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
181int dnet_pton(int af, const char *src, void *addr);
182
183const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
184int ipx_pton(int af, const char *src, void *addr);
185
dacc5d41 186const char *mpls_ntop(int af, const void *addr, char *str, size_t len);
4af44716 187int mpls_pton(int af, const char *src, void *addr, size_t alen);
dacc5d41 188
aba5acdf 189extern int __iproute2_hz_internal;
e3c27c2d 190int __get_hz(void);
aba5acdf
SH
191
192static __inline__ int get_hz(void)
193{
194 if (__iproute2_hz_internal == 0)
195 __iproute2_hz_internal = __get_hz();
196 return __iproute2_hz_internal;
197}
198
5e8bc631 199extern int __iproute2_user_hz_internal;
892e2124 200int __get_user_hz(void);
5e8bc631 201
202static __inline__ int get_user_hz(void)
203{
204 if (__iproute2_user_hz_internal == 0)
205 __iproute2_user_hz_internal = __get_user_hz();
206 return __iproute2_user_hz_internal;
207}
208
8cd09e61 209static inline __u32 nl_mgrp(__u32 group)
210{
56b94061 211 if (group > 31 ) {
212 fprintf(stderr, "Use setsockopt for this group %d\n", group);
213 exit(-1);
214 }
215 return group ? (1 << (group - 1)) : 0;
8cd09e61 216}
217
8c0f7a16
NA
218/* courtesy of bridge-utils */
219static inline unsigned long __tv_to_jiffies(const struct timeval *tv)
220{
221 unsigned long long jif;
222
223 jif = 1000000ULL * tv->tv_sec + tv->tv_usec;
224
225 return jif/10000;
226}
227
228static inline void __jiffies_to_tv(struct timeval *tv, unsigned long jiffies)
229{
230 unsigned long long tvusec;
231
232 tvusec = 10000ULL*jiffies;
233 tv->tv_sec = tvusec/1000000;
234 tv->tv_usec = tvusec - 1000000 * tv->tv_sec;
235}
8cd09e61 236
7c72df5a
ID
237void print_escape_buf(const __u8 *buf, size_t len, const char *escape);
238
90f93024 239int print_timestamp(FILE *fp);
ddb1129b 240void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
90f93024 241
afdc1194
LR
242#define BIT(nr) (1UL << (nr))
243
02d2ae55
SH
244#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
245
6256f8c9
DB
246#define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
247
248#ifndef offsetof
249# define offsetof(type, member) ((size_t) &((type *)0)->member)
250#endif
251
252#ifndef min
253# define min(x, y) ({ \
254 typeof(x) _min1 = (x); \
255 typeof(y) _min2 = (y); \
256 (void) (&_min1 == &_min2); \
257 _min1 < _min2 ? _min1 : _min2; })
258#endif
259
11c39b5e
DB
260#ifndef __check_format_string
261# define __check_format_string(pos_str, pos_args) \
262 __attribute__ ((format (printf, (pos_str), (pos_args))))
263#endif
264
32e93fb7
DB
265#define _textify(x) #x
266#define textify(x) _textify(x)
267
1e529305
RP
268#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
269#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
270
351efcde 271extern int cmdlineno;
892e2124
SH
272ssize_t getcmdline(char **line, size_t *len, FILE *in);
273int makeargs(char *line, char *argv[], int maxargs);
351efcde 274
c7272ca7
DB
275struct iplink_req {
276 struct nlmsghdr n;
277 struct ifinfomsg i;
278 char buf[1024];
279};
280
909dfe2c 281int iplink_parse(int argc, char **argv, struct iplink_req *req,
db02608b 282 char **name, char **type, char **link, char **dev,
5e25cf77 283 int *group, int *index);
ddb1129b 284
892e2124 285int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
e998e118
VK
286 bool show_label);
287
b217df10 288char *int_to_str(int val, char *buf);
d91fb3f4 289int get_guid(__u64 *guid, const char *arg);
56e3eb4c 290int get_real_family(int rtm_type, int rtm_family);
b217df10 291
08bd33d7 292int cmd_exec(const char *cmd, char **argv, bool do_fork);
1dafceb1
DA
293int make_path(const char *path, mode_t mode);
294char *find_cgroup2_mount(void);
f443565f 295int get_command_name(const char *pid, char *comm, size_t len);
08bd33d7 296
05b3b344 297#ifdef NEED_STRLCPY
8d15e012
PS
298size_t strlcpy(char *dst, const char *src, size_t size);
299size_t strlcat(char *dst, const char *src, size_t size);
05b3b344 300#endif
8d15e012 301
ba2fc55b
LB
302void drop_cap(void);
303
aba5acdf 304#endif /* __UTILS_H__ */