1 /*
2 * netlink-local.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-2008 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
32 #include <arpa/inet.h>
33 #include <netdb.h>
34
35 #ifndef SOL_NETLINK
36 #define SOL_NETLINK 270
37 #endif
38
39 #include <linux/types.h>
40
41 /* local header copies */
42 #include <linux/if.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_ether.h>
45 #include <linux/pkt_sched.h>
46 #include <linux/pkt_cls.h>
47 #include <linux/gen_stats.h>
48 #include <linux/ip_mp_alg.h>
49
50 #include <netlink/netlink.h>
51 #include <netlink/handlers.h>
52 #include <netlink/cache.h>
53 #include <netlink/route/tc.h>
54 #include <netlink/object-api.h>
55 #include <netlink/cache-api.h>
56 #include <netlink-types.h>
57
58 struct trans_tbl {
59 int i;
60 const char *a;
61 };
62
63 #define __ADD(id, name) { .i = id, .a = #name },
64
65 struct trans_list {
66 int i;
67 char *a;
68 struct nl_list_head list;
69 };
70
71 #define NL_DEBUG 1
72
73 #define NL_DBG(LVL,FMT,ARG...) \
74 do { \
75 if (LVL <= nl_debug) \
76 fprintf(stderr, "DBG<" #LVL ">: " FMT, ##ARG); \
77 } while (0)
78
79 #define BUG() \
80 do { \
81 fprintf(stderr, "BUG: %s:%d\n", \
82 __FILE__, __LINE__); \
83 assert(0); \
84 } while (0)
85
86 extern int __nl_read_num_str_file(const char *path,
87 int (*cb)(long, const char *));
88
89 extern int __trans_list_add(int, const char *, struct nl_list_head *);
90 extern void __trans_list_clear(struct nl_list_head *);
91
92 extern char *__type2str(int, char *, size_t, struct trans_tbl *, size_t);
93 extern int __str2type(const char *, struct trans_tbl *, size_t);
94
95 extern char *__list_type2str(int, char *, size_t, struct nl_list_head *);
96 extern int __list_str2type(const char *, struct nl_list_head *);
97
98 extern char *__flags2str(int, char *, size_t, struct trans_tbl *, size_t);
99 extern int __str2flags(const char *, struct trans_tbl *, size_t);
100
101 extern void dump_from_ops(struct nl_object *, struct nl_dump_params *);
102
dp_cache(struct nl_object * obj)103 static inline struct nl_cache *dp_cache(struct nl_object *obj)
104 {
105 if (obj->ce_cache == NULL)
106 return nl_cache_mngt_require(obj->ce_ops->oo_name);
107
108 return obj->ce_cache;
109 }
110
nl_cb_call(struct nl_cb * cb,int type,struct nl_msg * msg)111 static inline int nl_cb_call(struct nl_cb *cb, int type, struct nl_msg *msg)
112 {
113 return cb->cb_set[type](msg, cb->cb_args[type]);
114 }
115
116 #define ARRAY_SIZE(X) (sizeof(X) / sizeof((X)[0]))
117 #ifndef offsetof
118 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
119 #endif
120
121 #define __init __attribute__ ((constructor))
122 #define __exit __attribute__ ((destructor))
123 #undef __deprecated
124 #define __deprecated __attribute__ ((deprecated))
125
126 #define min(x,y) ({ \
127 typeof(x) _x = (x); \
128 typeof(y) _y = (y); \
129 (void) (&_x == &_y); \
130 _x < _y ? _x : _y; })
131
132 #define max(x,y) ({ \
133 typeof(x) _x = (x); \
134 typeof(y) _y = (y); \
135 (void) (&_x == &_y); \
136 _x > _y ? _x : _y; })
137
138 #define min_t(type,x,y) \
139 ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
140 #define max_t(type,x,y) \
141 ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
142
143 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
144 struct nlmsghdr *, struct nl_parser_param *);
145
146
rtnl_copy_ratespec(struct rtnl_ratespec * dst,struct tc_ratespec * src)147 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
148 struct tc_ratespec *src)
149 {
150 dst->rs_cell_log = src->cell_log;
151 dst->rs_feature = src->feature;
152 dst->rs_addend = src->addend;
153 dst->rs_mpu = src->mpu;
154 dst->rs_rate = src->rate;
155 }
156
rtnl_rcopy_ratespec(struct tc_ratespec * dst,struct rtnl_ratespec * src)157 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
158 struct rtnl_ratespec *src)
159 {
160 dst->cell_log = src->rs_cell_log;
161 dst->feature = src->rs_feature;
162 dst->addend = src->rs_addend;
163 dst->mpu = src->rs_mpu;
164 dst->rate = src->rs_rate;
165 }
166
nl_cache_name(struct nl_cache * cache)167 static inline char *nl_cache_name(struct nl_cache *cache)
168 {
169 return cache->c_ops ? cache->c_ops->co_name : "unknown";
170 }
171
172 #define GENL_FAMILY(id, name) \
173 { \
174 { id, NL_ACT_UNSPEC, name }, \
175 END_OF_MSGTYPES_LIST, \
176 }
177
wait_for_ack(struct nl_sock * sk)178 static inline int wait_for_ack(struct nl_sock *sk)
179 {
180 if (sk->s_flags & NL_NO_AUTO_ACK)
181 return 0;
182 else
183 return nl_wait_for_ack(sk);
184 }
185
186 #endif
187