1 /* SPDX-License-Identifier: LGPL-2.1-only */
2 /*
3  * Copyright (c) 2003-2013 Thomas Graf <tgraf@suug.ch>
4  */
5 
6 #ifndef NETLINK_LOCAL_H_
7 #define NETLINK_LOCAL_H_
8 
9 #include <stdio.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <time.h>
16 #include <stdarg.h>
17 #include <ctype.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/socket.h>
21 #include <inttypes.h>
22 #include <assert.h>
23 #include <limits.h>
24 #include <search.h>
25 
26 #include <arpa/inet.h>
27 #include <netdb.h>
28 
29 #include <defs.h>
30 
31 #ifndef SOL_NETLINK
32 #define SOL_NETLINK 270
33 #endif
34 
35 #include <linux/types.h>
36 
37 /* local header copies */
38 #include <linux/if.h>
39 #include <linux/if_arp.h>
40 #include <linux/if_ether.h>
41 #include <linux/ethtool.h>
42 #include <linux/pkt_sched.h>
43 #include <linux/pkt_cls.h>
44 #include <linux/gen_stats.h>
45 #include <linux/atm.h>
46 #include <linux/ip.h>
47 #include <linux/ipv6.h>
48 #include <linux/snmp.h>
49 #include <linux/xfrm.h>
50 
51 #ifndef DISABLE_PTHREADS
52 #include <pthread.h>
53 #endif
54 
55 #include <netlink/netlink.h>
56 #include <netlink/handlers.h>
57 #include <netlink/cache.h>
58 #include <netlink/route/tc.h>
59 #include <netlink-private/object-api.h>
60 #include <netlink-private/cache-api.h>
61 #include <netlink-private/types.h>
62 #include <netlink-private/utils.h>
63 
64 #define NSEC_PER_SEC	1000000000L
65 
66 struct trans_tbl {
67 	uint64_t i;
68 	const char *a;
69 };
70 
71 #define __ADD(id, name) { .i = id, .a = #name }
72 
73 struct trans_list {
74 	int i;
75 	char *a;
76 	struct nl_list_head list;
77 };
78 
79 #ifdef NL_DEBUG
80 #define NL_DBG(LVL,FMT,ARG...)						\
81 	do {								\
82 		if (LVL <= nl_debug) {					\
83 			int _errsv = errno;				\
84 			fprintf(stderr,					\
85 				"DBG<" #LVL ">%20s:%-4u %s: " FMT,	\
86 				__FILE__, __LINE__,			\
87 				__func__, ##ARG);			\
88 			errno = _errsv;					\
89 		}							\
90 	} while (0)
91 #else /* NL_DEBUG */
92 #define NL_DBG(LVL,FMT,ARG...) do { } while(0)
93 #endif /* NL_DEBUG */
94 
95 #define BUG()                            				\
96 	do {                                 				\
97 		fprintf(stderr, "BUG at file position %s:%d:%s\n",  	\
98 			__FILE__, __LINE__, __func__); 			\
99 		assert(0);						\
100 	} while (0)
101 
102 #define BUG_ON(condition)						\
103 	do {								\
104 		if (condition)						\
105 			BUG();						\
106 	} while (0)
107 
108 
109 #define APPBUG(msg)							\
110 	do {								\
111 		fprintf(stderr, "APPLICATION BUG: %s:%d:%s: %s\n",	\
112 			__FILE__, __LINE__, __func__, msg);		\
113 		assert(0);						\
114 	} while(0)
115 
116 extern int __nl_read_num_str_file(const char *path,
117 				  int (*cb)(long, const char *));
118 
119 extern int __trans_list_add(int, const char *, struct nl_list_head *);
120 extern void __trans_list_clear(struct nl_list_head *);
121 
122 extern char *__type2str(int, char *, size_t, const struct trans_tbl *, size_t);
123 extern int __str2type(const char *, const struct trans_tbl *, size_t);
124 
125 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
126 extern int __list_str2type(const char *, struct nl_list_head *);
127 
128 extern char *__flags2str(int, char *, size_t, const struct trans_tbl *, size_t);
129 extern int __str2flags(const char *, const struct trans_tbl *, size_t);
130 
131 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
132 extern struct rtnl_link *link_lookup(struct nl_cache *cache, int ifindex);
133 
nl_cb_call(struct nl_cb * cb,enum nl_cb_type type,struct nl_msg * msg)134 static inline int nl_cb_call(struct nl_cb *cb, enum nl_cb_type type, struct nl_msg *msg)
135 {
136 	int ret;
137 
138 	cb->cb_active = type;
139 	ret = cb->cb_set[type](msg, cb->cb_args[type]);
140 	cb->cb_active = __NL_CB_TYPE_MAX;
141 	return ret;
142 }
143 
144 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
145 
146 /* This is also defined in stddef.h */
147 #ifndef offsetof
148 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
149 #endif
150 
151 #define __init __attribute__ ((constructor))
152 #define __exit __attribute__ ((destructor))
153 #undef __deprecated
154 #define __deprecated __attribute__ ((deprecated))
155 
156 #define min(x,y) ({ \
157 	__typeof__(x) _x = (x);	\
158 	__typeof__(y) _y = (y);	\
159 	(void) (&_x == &_y);		\
160 	_x < _y ? _x : _y; })
161 
162 #define max(x,y) ({ \
163 	__typeof__(x) _x = (x);	\
164 	__typeof__(y) _y = (y);	\
165 	(void) (&_x == &_y);		\
166 	_x > _y ? _x : _y; })
167 
168 #define min_t(type,x,y) \
169 	({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
170 #define max_t(type,x,y) \
171 	({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
172 
173 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
174 			  struct nlmsghdr *, struct nl_parser_param *);
175 
176 
rtnl_copy_ratespec(struct rtnl_ratespec * dst,struct tc_ratespec * src)177 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
178 				      struct tc_ratespec *src)
179 {
180 	dst->rs_cell_log = src->cell_log;
181 	dst->rs_overhead = src->overhead;
182 	dst->rs_cell_align = src->cell_align;
183 	dst->rs_mpu = src->mpu;
184 	dst->rs_rate64 = src->rate;
185 }
186 
rtnl_rcopy_ratespec(struct tc_ratespec * dst,struct rtnl_ratespec * src)187 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
188 				       struct rtnl_ratespec *src)
189 {
190 	dst->cell_log = src->rs_cell_log;
191 	dst->overhead = src->rs_overhead;
192 	dst->cell_align = src->rs_cell_align;
193 	dst->mpu = src->rs_mpu;
194 	dst->rate = src->rs_rate64 > 0xFFFFFFFFull ? 0xFFFFFFFFull : (uint32_t) src->rs_rate64;
195 }
196 
nl_cache_name(struct nl_cache * cache)197 static inline const char *nl_cache_name(struct nl_cache *cache)
198 {
199 	return cache->c_ops ? cache->c_ops->co_name : "unknown";
200 }
201 
202 #define GENL_FAMILY(id, name) \
203 	{ \
204 		{ id, NL_ACT_UNSPEC, name }, \
205 		END_OF_MSGTYPES_LIST, \
206 	}
207 
wait_for_ack(struct nl_sock * sk)208 static inline int wait_for_ack(struct nl_sock *sk)
209 {
210 	if (sk->s_flags & NL_NO_AUTO_ACK)
211 		return 0;
212 	else
213 		return nl_wait_for_ack(sk);
214 }
215 
build_sysconf_path(char ** strp,const char * filename)216 static inline int build_sysconf_path(char **strp, const char *filename)
217 {
218 	char *sysconfdir;
219 
220 	sysconfdir = getenv("NLSYSCONFDIR");
221 
222 	if (!sysconfdir)
223 		sysconfdir = SYSCONFDIR;
224 
225 	return asprintf(strp, "%s/%s", sysconfdir, filename);
226 }
227 
228 #ifndef DISABLE_PTHREADS
229 #define NL_LOCK(NAME) pthread_mutex_t (NAME) = PTHREAD_MUTEX_INITIALIZER
230 #define NL_RW_LOCK(NAME) pthread_rwlock_t (NAME) = PTHREAD_RWLOCK_INITIALIZER
231 
nl_lock(pthread_mutex_t * lock)232 static inline void nl_lock(pthread_mutex_t *lock)
233 {
234 	pthread_mutex_lock(lock);
235 }
236 
nl_unlock(pthread_mutex_t * lock)237 static inline void nl_unlock(pthread_mutex_t *lock)
238 {
239 	pthread_mutex_unlock(lock);
240 }
241 
nl_read_lock(pthread_rwlock_t * lock)242 static inline void nl_read_lock(pthread_rwlock_t *lock)
243 {
244 	pthread_rwlock_rdlock(lock);
245 }
246 
nl_read_unlock(pthread_rwlock_t * lock)247 static inline void nl_read_unlock(pthread_rwlock_t *lock)
248 {
249 	pthread_rwlock_unlock(lock);
250 }
251 
nl_write_lock(pthread_rwlock_t * lock)252 static inline void nl_write_lock(pthread_rwlock_t *lock)
253 {
254 	pthread_rwlock_wrlock(lock);
255 }
256 
nl_write_unlock(pthread_rwlock_t * lock)257 static inline void nl_write_unlock(pthread_rwlock_t *lock)
258 {
259 	pthread_rwlock_unlock(lock);
260 }
261 
262 #else
263 #define NL_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
264 #define NL_RW_LOCK(NAME) int __unused_lock_ ##NAME __attribute__((unused))
265 
266 #define nl_lock(LOCK) do { } while(0)
267 #define nl_unlock(LOCK) do { } while(0)
268 #define nl_read_lock(LOCK) do { } while(0)
269 #define nl_read_unlock(LOCK) do { } while(0)
270 #define nl_write_lock(LOCK) do { } while(0)
271 #define nl_write_unlock(LOCK) do { } while(0)
272 #endif
273 
rtnl_tc_calc_txtime64(int bufsize,uint64_t rate)274 static inline int rtnl_tc_calc_txtime64(int bufsize, uint64_t rate)
275 {
276 	return ((double) bufsize / (double) rate) * 1000000.0;
277 }
278 
rtnl_tc_calc_bufsize64(int txtime,uint64_t rate)279 static inline int rtnl_tc_calc_bufsize64(int txtime, uint64_t rate)
280 {
281 	return ((double) txtime * (double) rate) / 1000000.0;
282 }
283 
284 #endif
285