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