blob: 1d351490a150e0c84e6411cf155370b4571576ed [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001#ifndef __UTILS_H__
2#define __UTILS_H__ 1
3
Strake5bd9dd42012-12-23 08:46:04 -05004#include <sys/types.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00005#include <asm/types.h>
6#include <resolv.h>
jamal56b94062007-02-25 12:02:23 -05007#include <stdlib.h>
Vadim Kochane998e112015-01-18 16:10:17 +02008#include <stdbool.h>
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00009
10#include "libnetlink.h"
11#include "ll_map.h"
12#include "rtm_map.h"
13
14extern int preferred_family;
Christian Hesseb68d9832014-10-31 22:33:13 +010015extern int human_readable;
Stephen Hemminger1e264ab2014-11-02 12:49:19 -080016extern int use_iec;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000017extern int show_stats;
18extern int show_details;
19extern int show_raw;
20extern int resolve_hosts;
21extern int oneline;
Andy Gospodarek5d295bb2015-08-28 13:45:41 -040022extern int brief;
shemminger90f93022005-06-07 21:55:55 +000023extern int timestamp;
Vadim Kochan79aa79d2014-12-23 00:13:58 +020024extern int timestamp_short;
Stephen Hemmingerec7aff52015-07-26 21:46:44 -070025extern const char * _SL_;
Ben Greear64c79562010-12-01 11:13:51 -080026extern int max_flush_loops;
Stephen Hemmingera3aa47a2013-07-16 10:04:05 -070027extern int batch_mode;
Vadim Kochanb13ba032015-01-18 16:10:18 +020028extern bool do_all;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000029
30#ifndef IPPROTO_ESP
31#define IPPROTO_ESP 50
32#endif
33#ifndef IPPROTO_AH
34#define IPPROTO_AH 51
35#endif
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000036#ifndef IPPROTO_COMP
37#define IPPROTO_COMP 108
38#endif
org[shemminger]!nakambd641cd2004-09-28 18:38:35 +000039#ifndef IPSEC_PROTO_ANY
40#define IPSEC_PROTO_ANY 255
41#endif
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000042
43#define SPRINT_BSIZE 64
44#define SPRINT_BUF(x) char x[SPRINT_BSIZE]
45
Stephen Hemminger892e2122015-08-13 14:09:58 -070046void incomplete_command(void) __attribute__((noreturn));
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000047
48#define NEXT_ARG() do { argv++; if (--argc <= 0) incomplete_command(); } while(0)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000049#define NEXT_ARG_OK() (argc - 1 > 0)
Daniel Borkmannfaa8a462015-09-25 12:32:41 +020050#define NEXT_ARG_FWD() do { argv++; argc--; } while(0)
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +000051#define PREV_ARG() do { argv--; argc++; } while(0)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000052
53typedef struct
54{
Eric W. Biederman93ae2832015-03-15 14:52:06 -050055 __u16 flags;
56 __u16 bytelen;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000057 __s16 bitlen;
Eric W. Biederman93ae2832015-03-15 14:52:06 -050058 /* These next two fields match rtvia */
59 __u16 family;
Olivier Fourdan7f71c0c2008-11-25 12:36:22 +000060 __u32 data[8];
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000061} inet_prefix;
62
net[shemminger]!shemmingerf082b642005-03-30 18:16:10 +000063#define PREFIXLEN_SPECIFIED 1
64
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000065#define DN_MAXADDL 20
66#ifndef AF_DECnet
67#define AF_DECnet 12
68#endif
69
Stephen Hemmingerae665a52006-12-05 10:10:22 -080070struct dn_naddr
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000071{
72 unsigned short a_len;
73 unsigned char a_addr[DN_MAXADDL];
74};
75
76#define IPX_NODE_LEN 6
77
78struct ipx_addr {
79 u_int32_t ipx_net;
80 u_int8_t ipx_node[IPX_NODE_LEN];
81};
82
Eric W. Biedermandacc5d42015-03-15 14:53:45 -050083#ifndef AF_MPLS
84# define AF_MPLS 28
85#endif
86
87/* Maximum number of labels the mpls helpers support */
88#define MPLS_MAX_LABELS 8
89
Stephen Hemminger892e2122015-08-13 14:09:58 -070090__u32 get_addr32(const char *name);
91int get_addr_1(inet_prefix *dst, const char *arg, int family);
92int get_prefix_1(inet_prefix *dst, char *arg, int family);
93int get_addr(inet_prefix *dst, const char *arg, int family);
94int get_prefix(inet_prefix *dst, char *arg, int family);
95int mask2bits(__u32 netmask);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000096
Stephen Hemminger892e2122015-08-13 14:09:58 -070097int get_integer(int *val, const char *arg, int base);
98int get_unsigned(unsigned *val, const char *arg, int base);
99int get_time_rtt(unsigned *val, const char *arg, int *raw);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100#define get_byte get_u8
101#define get_ushort get_u16
102#define get_short get_s16
Stephen Hemminger892e2122015-08-13 14:09:58 -0700103int get_u64(__u64 *val, const char *arg, int base);
104int get_u32(__u32 *val, const char *arg, int base);
105int get_s32(__s32 *val, const char *arg, int base);
106int get_u16(__u16 *val, const char *arg, int base);
107int get_s16(__s16 *val, const char *arg, int base);
108int get_u8(__u8 *val, const char *arg, int base);
109int get_s8(__s8 *val, const char *arg, int base);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000110
Stephen Hemminger892e2122015-08-13 14:09:58 -0700111char* hexstring_n2a(const __u8 *str, int len, char *buf, int blen);
112__u8* hexstring_a2n(const char *str, __u8 *buf, int blen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000113
Stephen Hemminger892e2122015-08-13 14:09:58 -0700114int af_bit_len(int af);
115int af_byte_len(int af);
Vadim Kochanf3a2ddc2015-02-28 02:50:24 +0200116
Stephen Hemminger892e2122015-08-13 14:09:58 -0700117const char *format_host(int af, int len, const void *addr,
osdl.org!shemminger04457b32004-06-02 20:22:08 +0000118 char *buf, int buflen);
Stephen Hemminger892e2122015-08-13 14:09:58 -0700119const char *rt_addr_n2a(int af, int len, const void *addr,
osdl.org!shemminger04457b32004-06-02 20:22:08 +0000120 char *buf, int buflen);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000121
Stephen Hemminger892e2122015-08-13 14:09:58 -0700122int read_family(const char *name);
123const char *family_name(int family);
Eric W. Biederman45c90d12015-03-15 14:50:03 -0500124
net[shemminger]!shemmingerc7699872004-07-07 17:05:56 +0000125void missarg(const char *) __attribute__((noreturn));
osdl.org!shemminger04457b32004-06-02 20:22:08 +0000126void invarg(const char *, const char *) __attribute__((noreturn));
127void duparg(const char *, const char *) __attribute__((noreturn));
128void duparg2(const char *, const char *) __attribute__((noreturn));
129int matches(const char *arg, const char *pattern);
Stephen Hemminger892e2122015-08-13 14:09:58 -0700130int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000131
132const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
133int dnet_pton(int af, const char *src, void *addr);
134
135const char *ipx_ntop(int af, const void *addr, char *str, size_t len);
136int ipx_pton(int af, const char *src, void *addr);
137
Eric W. Biedermandacc5d42015-03-15 14:53:45 -0500138const char *mpls_ntop(int af, const void *addr, char *str, size_t len);
139int mpls_pton(int af, const char *src, void *addr);
140
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000141extern int __iproute2_hz_internal;
Zhang Shengjue3c27c22015-08-14 01:36:20 +0000142int __get_hz(void);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143
144static __inline__ int get_hz(void)
145{
146 if (__iproute2_hz_internal == 0)
147 __iproute2_hz_internal = __get_hz();
148 return __iproute2_hz_internal;
149}
150
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000151extern int __iproute2_user_hz_internal;
Stephen Hemminger892e2122015-08-13 14:09:58 -0700152int __get_user_hz(void);
net[shemminger]!shemminger5e8bc632005-03-14 18:44:54 +0000153
154static __inline__ int get_user_hz(void)
155{
156 if (__iproute2_user_hz_internal == 0)
157 __iproute2_user_hz_internal = __get_user_hz();
158 return __iproute2_user_hz_internal;
159}
160
jamal8cd09e62006-12-07 20:47:03 -0500161static inline __u32 nl_mgrp(__u32 group)
162{
jamal56b94062007-02-25 12:02:23 -0500163 if (group > 31 ) {
164 fprintf(stderr, "Use setsockopt for this group %d\n", group);
165 exit(-1);
166 }
167 return group ? (1 << (group - 1)) : 0;
jamal8cd09e62006-12-07 20:47:03 -0500168}
169
170
shemminger90f93022005-06-07 21:55:55 +0000171int print_timestamp(FILE *fp);
Vadim Kochanddb11292015-01-13 20:14:24 +0200172void print_nlmsg_timestamp(FILE *fp, const struct nlmsghdr *n);
shemminger90f93022005-06-07 21:55:55 +0000173
shemminger02d2ae52005-06-23 17:31:27 +0000174#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
175
Daniel Borkmann6256f8c2015-04-01 17:57:44 +0200176#define BUILD_BUG_ON(cond) ((void)sizeof(char[1 - 2 * !!(cond)]))
177
178#ifndef offsetof
179# define offsetof(type, member) ((size_t) &((type *)0)->member)
180#endif
181
182#ifndef min
183# define min(x, y) ({ \
184 typeof(x) _min1 = (x); \
185 typeof(y) _min2 = (y); \
186 (void) (&_min1 == &_min2); \
187 _min1 < _min2 ? _min1 : _min2; })
188#endif
189
Daniel Borkmann11c39b52015-03-16 19:37:41 +0100190#ifndef __check_format_string
191# define __check_format_string(pos_str, pos_args) \
192 __attribute__ ((format (printf, (pos_str), (pos_args))))
193#endif
194
Roopa Prabhu1e529302015-10-15 13:13:39 +0200195#define htonll(x) ((1==htonl(1)) ? (x) : ((uint64_t)htonl((x) & 0xFFFFFFFF) << 32) | htonl((x) >> 32))
196#define ntohll(x) ((1==ntohl(1)) ? (x) : ((uint64_t)ntohl((x) & 0xFFFFFFFF) << 32) | ntohl((x) >> 32))
197
shemminger351efcd2005-09-01 19:21:50 +0000198extern int cmdlineno;
Stephen Hemminger892e2122015-08-13 14:09:58 -0700199ssize_t getcmdline(char **line, size_t *len, FILE *in);
200int makeargs(char *line, char *argv[], int maxargs);
201int inet_get_addr(const char *src, __u32 *dst, struct in6_addr *dst6);
shemminger351efcd2005-09-01 19:21:50 +0000202
Pavel Emelyanov909dfe22007-07-19 13:32:31 +0400203struct iplink_req;
204int iplink_parse(int argc, char **argv, struct iplink_req *req,
Vlad Dogarudb026082011-02-02 20:23:41 +0200205 char **name, char **type, char **link, char **dev,
Pavel Emelyanov5e25cf72013-12-26 23:15:20 +0400206 int *group, int *index);
Vadim Kochanddb11292015-01-13 20:14:24 +0200207
Stephen Hemminger892e2122015-08-13 14:09:58 -0700208int do_each_netns(int (*func)(char *nsname, void *arg), void *arg,
Vadim Kochane998e112015-01-18 16:10:17 +0200209 bool show_label);
210
Vadim Kochanb217df12015-02-13 22:14:03 +0200211char *int_to_str(int val, char *buf);
212
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000213#endif /* __UTILS_H__ */