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