2 * netlink-local.h Local Netlink Interface
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
9 * Copyright (c) 2003-2006 Thomas Graf <tgraf@suug.ch>
12 #ifndef NETLINK_LOCAL_H_
13 #define NETLINK_LOCAL_H_
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <arpa/inet.h>
34 #define SOL_NETLINK 270
37 #include <linux/types.h>
39 /* local header copies */
41 #include <linux/if_arp.h>
42 #include <linux/if_ether.h>
43 #include <linux/pkt_sched.h>
44 #include <linux/pkt_cls.h>
45 #include <linux/gen_stats.h>
46 #include <linux/ip_mp_alg.h>
48 #include <netlink/netlink.h>
49 #include <netlink/handlers.h>
50 #include <netlink/cache.h>
51 #include <netlink/route/tc.h>
52 #include <netlink/object-api.h>
53 #include <netlink/cache-api.h>
54 #include <netlink-types.h>
61 #define __ADD(id, name) { .i = id, .a = #name },
66 struct nl_list_head list;
71 #define NL_DBG(LVL,FMT,ARG...) \
73 if (LVL <= nl_debug) \
74 fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
79 fprintf(stderr, "BUG: %s:%d\n", \
80 __FILE__, __LINE__); \
84 #define RET_ERR(R, E) \
90 extern int __nl_error(int, const char *, unsigned int,
91 const char *, const char *, ...);
93 extern int __nl_read_num_str_file(const char *path,
94 int (*cb)(long, const char *));
96 #ifdef NL_ERROR_ASSERT
98 static inline int __assert_error(const char *file, int line, char *func,
102 fprintf(stderr, "%s:%d:%s: ", file, line, func);
104 vfprintf(stderr, fmt, args);
106 fprintf(stderr, "\n");
110 #define nl_error(E, FMT,ARG...) \
111 __assert_error(__FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
114 #define nl_error(E, FMT,ARG...) \
115 __nl_error(E, __FILE__, __LINE__, __FUNCTION__, FMT, ##ARG)
119 #define nl_errno(E) nl_error(E, NULL)
121 /* backwards compat */
122 #define dp_new_line(params, line) nl_new_line(params, line)
123 #define dp_dump(params, fmt, arg...) nl_dump(params, fmt, ##arg)
125 static inline int __trans_list_add(int i, const char *a,
126 struct nl_list_head *head)
128 struct trans_list *tl;
130 tl = calloc(1, sizeof(*tl));
132 return nl_errno(ENOMEM);
137 nl_list_add_tail(&tl->list, head);
142 static inline void __trans_list_clear(struct nl_list_head *head)
144 struct trans_list *tl, *next;
146 nl_list_for_each_entry_safe(tl, next, head, list) {
152 static inline char *__type2str(int type, char *buf, size_t len,
153 struct trans_tbl *tbl, size_t tbl_len)
156 for (i = 0; i < tbl_len; i++) {
157 if (tbl[i].i == type) {
158 snprintf(buf, len, "%s", tbl[i].a);
163 snprintf(buf, len, "0x%x", type);
167 static inline char *__list_type2str(int type, char *buf, size_t len,
168 struct nl_list_head *head)
170 struct trans_list *tl;
172 nl_list_for_each_entry(tl, head, list) {
174 snprintf(buf, len, "%s", tl->a);
179 snprintf(buf, len, "0x%x", type);
183 static inline char *__flags2str(int flags, char *buf, size_t len,
184 struct trans_tbl *tbl, size_t tbl_len)
191 for (i = 0; i < tbl_len; i++) {
192 if (tbl[i].i & tmp) {
194 strncat(buf, tbl[i].a, len - strlen(buf) - 1);
196 strncat(buf, ",", len - strlen(buf) - 1);
203 static inline int __str2type(const char *buf, struct trans_tbl *tbl,
213 for (i = 0; i < tbl_len; i++)
214 if (!strcasecmp(tbl[i].a, buf))
217 l = strtoul(buf, &end, 0);
218 if (l == ULONG_MAX || *end != '\0')
224 static inline int __list_str2type(const char *buf, struct nl_list_head *head)
226 struct trans_list *tl;
233 nl_list_for_each_entry(tl, head, list) {
234 if (!strcasecmp(tl->a, buf))
238 l = strtoul(buf, &end, 0);
239 if (l == ULONG_MAX || *end != '\0')
245 static inline int __str2flags(const char *buf, struct trans_tbl *tbl,
248 int i, flags = 0, len;
249 char *p = (char *) buf, *t;
256 len = t ? t - p : strlen(p);
257 for (i = 0; i < tbl_len; i++)
258 if (!strncasecmp(tbl[i].a, p, len))
270 static inline void __dp_dump(struct nl_dump_params *parms, const char *fmt,
274 vfprintf(parms->dp_fd, fmt, args);
275 else if (parms->dp_buf || parms->dp_cb) {
277 vasprintf(&buf, fmt, args);
279 parms->dp_cb(parms, buf);
281 strncat(parms->dp_buf, buf,
282 parms->dp_buflen - strlen(parms->dp_buf) - 1);
287 static inline void dp_dump_line(struct nl_dump_params *parms, int line,
288 const char *fmt, ...)
292 nl_new_line(parms, line);
295 __dp_dump(parms, fmt, args);
299 static inline void dump_from_ops(struct nl_object *obj,
300 struct nl_dump_params *params)
302 int type = params->dp_type;
304 if (type < 0 || type > NL_DUMP_MAX)
307 if (params->dp_dump_msgtype) {
312 dp_dump_line(params, 0, "%s ",
313 nl_cache_mngt_type2name(obj->ce_ops,
314 obj->ce_ops->co_protocol,
318 params->dp_pre_dump = 1;
320 dp_new_line(params, 0);
322 if (obj->ce_ops->oo_dump[type])
323 obj->ce_ops->oo_dump[type](obj, params);
326 static inline struct nl_cache *dp_cache(struct nl_object *obj)
328 if (obj->ce_cache == NULL)
329 return nl_cache_mngt_require(obj->ce_ops->oo_name);
331 return obj->ce_cache;
334 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
336 return cb->cb_set[type](msg, cb->cb_args[type]);
339 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
340 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
342 #define __init __attribute__ ((constructor))
343 #define __exit __attribute__ ((destructor))
348 #define min(x,y) ({ \
349 typeof(x) _x = (x); \
350 typeof(y) _y = (y); \
351 (void) (&_x == &_y); \
352 _x < _y ? _x : _y; })
354 #define max(x,y) ({ \
355 typeof(x) _x = (x); \
356 typeof(y) _y = (y); \
357 (void) (&_x == &_y); \
358 _x > _y ? _x : _y; })
360 #define min_t(type,x,y) \
361 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
362 #define max_t(type,x,y) \
363 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
365 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
366 struct nlmsghdr *, struct nl_parser_param *);
369 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
370 struct tc_ratespec *src)
372 dst->rs_cell_log = src->cell_log;
373 dst->rs_feature = src->feature;
374 dst->rs_addend = src->addend;
375 dst->rs_mpu = src->mpu;
376 dst->rs_rate = src->rate;
379 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
380 struct rtnl_ratespec *src)
382 dst->cell_log = src->rs_cell_log;
383 dst->feature = src->rs_feature;
384 dst->addend = src->rs_addend;
385 dst->mpu = src->rs_mpu;
386 dst->rate = src->rs_rate;
389 static inline char *nl_cache_name(struct nl_cache *cache)
391 return cache->c_ops ? cache->c_ops->co_name : "unknown";
394 #define GENL_FAMILY(id, name) \
396 { id, NL_ACT_UNSPEC, name }, \
397 END_OF_MSGTYPES_LIST, \