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 
118 #define __init __attribute__ ((constructor))
119 #define __exit __attribute__ ((destructor))
120 #undef __deprecated
121 #define __deprecated __attribute__ ((deprecated))
122 
123 #define min(x,y) ({ \
124 	typeof(x) _x = (x);	\
125 	typeof(y) _y = (y);	\
126 	(void) (&_x == &_y);		\
127 	_x < _y ? _x : _y; })
128 
129 #define max(x,y) ({ \
130 	typeof(x) _x = (x);	\
131 	typeof(y) _y = (y);	\
132 	(void) (&_x == &_y);		\
133 	_x > _y ? _x : _y; })
134 
135 #define min_t(type,x,y) \
136 	({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
137 #define max_t(type,x,y) \
138 	({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
139 
140 extern int nl_cache_parse(struct nl_cache_ops *, struct sockaddr_nl *,
141 			  struct nlmsghdr *, struct nl_parser_param *);
142 
143 
rtnl_copy_ratespec(struct rtnl_ratespec * dst,struct tc_ratespec * src)144 static inline void rtnl_copy_ratespec(struct rtnl_ratespec *dst,
145 				      struct tc_ratespec *src)
146 {
147 	dst->rs_cell_log = src->cell_log;
148 	dst->rs_feature = src->feature;
149 	dst->rs_addend = src->addend;
150 	dst->rs_mpu = src->mpu;
151 	dst->rs_rate = src->rate;
152 }
153 
rtnl_rcopy_ratespec(struct tc_ratespec * dst,struct rtnl_ratespec * src)154 static inline void rtnl_rcopy_ratespec(struct tc_ratespec *dst,
155 				       struct rtnl_ratespec *src)
156 {
157 	dst->cell_log = src->rs_cell_log;
158 	dst->feature = src->rs_feature;
159 	dst->addend = src->rs_addend;
160 	dst->mpu = src->rs_mpu;
161 	dst->rate = src->rs_rate;
162 }
163 
nl_cache_name(struct nl_cache * cache)164 static inline char *nl_cache_name(struct nl_cache *cache)
165 {
166 	return cache->c_ops ? cache->c_ops->co_name : "unknown";
167 }
168 
169 #define GENL_FAMILY(id, name) \
170 	{ \
171 		{ id, NL_ACT_UNSPEC, name }, \
172 		END_OF_MSGTYPES_LIST, \
173 	}
174 
wait_for_ack(struct nl_sock * sk)175 static inline int wait_for_ack(struct nl_sock *sk)
176 {
177 	if (sk->s_flags & NL_NO_AUTO_ACK)
178 		return 0;
179 	else
180 		return nl_wait_for_ack(sk);
181 }
182 
183 #endif
184