-
Notifications
You must be signed in to change notification settings - Fork 1
/
compat.h
291 lines (220 loc) · 6.89 KB
/
compat.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
*
* Marek Lindner, Simon Wunderlich
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA
*
*
* This file contains macros for maintaining compatibility with older versions
* of the Linux kernel.
*/
#ifndef _NET_BATMAN_ADV_COMPAT_H_
#define _NET_BATMAN_ADV_COMPAT_H_
#include <linux/version.h> /* LINUX_VERSION_CODE */
#include "bat_sysfs.h" /* struct bat_attribute */
#ifndef IPPROTO_UDP
#define IPPROTO_UDP 17
#endif
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
#define skb_set_network_header(_skb, _offset) \
do { (_skb)->nh.raw = (_skb)->data + (_offset); } while (0)
#define skb_reset_mac_header(_skb) \
do { (_skb)->mac.raw = (_skb)->data; } while (0)
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define skb_mac_header(_skb) \
((_skb)->mac.raw)
#include <linux/etherdevice.h>
static inline __be16 bat_eth_type_trans(struct sk_buff *skb,
struct net_device *dev)
{
skb->dev = dev;
return eth_type_trans(skb, dev);
}
#define eth_type_trans(_skb, _dev) \
bat_eth_type_trans(_skb, _dev);
#endif /* < KERNEL_VERSION(2,6,22) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
static inline int skb_cow_head(struct sk_buff *skb, unsigned int headroom)
{
return skb_cow(skb, headroom);
}
#define cancel_delayed_work_sync(wq) cancel_delayed_work(wq)
#endif /* < KERNEL_VERSION(2, 6, 23) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
#ifndef pr_fmt
#define pr_fmt(fmt) fmt
#endif
#define pr_err(fmt, ...) \
printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_warning(fmt, ...) \
printk(KERN_WARNING pr_fmt(fmt), ##__VA_ARGS__)
#if defined(DEBUG)
#define pr_debug(fmt, ...) \
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#else
#define pr_debug(fmt, ...) \
({ if (0) printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__); 0; })
#endif
#define dev_get_by_name(x, y) dev_get_by_name(y)
#endif /* < KERNEL_VERSION(2, 6, 24) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
#define strict_strtoul(cp, base, res) \
({ \
int ret = 0; \
char *endp; \
*res = simple_strtoul(cp, &endp, base); \
if (cp == endp) \
ret = -EINVAL; \
ret; \
})
#define to_battr(a) container_of(a, struct bat_attribute, attr)
static inline ssize_t bat_wrapper_show(struct kobject *kobj,
struct attribute *attr, char *buf)
{
struct bat_attribute *bat_attr = to_battr(attr);
if (bat_attr->show)
return bat_attr->show(kobj, attr, buf);
return -EIO;
}
static inline ssize_t bat_wrapper_store(struct kobject *kobj,
struct attribute *attr,
const char *buf, size_t count)
{
struct bat_attribute *bat_attr = to_battr(attr);
if (bat_attr->store)
return bat_attr->store(kobj, attr, (char *)buf, count);
return -EIO;
}
static struct sysfs_ops bat_wrapper_ops = {
.show = bat_wrapper_show,
.store = bat_wrapper_store,
};
static struct kobj_type ktype_bat_wrapper = {
.sysfs_ops = &bat_wrapper_ops,
};
static inline struct kobject *kobject_create_and_add(const char *name,
struct kobject *parent)
{
struct kobject *kobj;
int err;
kobj = kzalloc(sizeof(*kobj), GFP_KERNEL);
if (!kobj)
return NULL;
kobject_set_name(kobj, name);
kobj->ktype = &ktype_bat_wrapper;
kobj->kset = NULL;
kobj->parent = parent;
err = kobject_register(kobj);
if (err) {
kobject_put(kobj);
return NULL;
}
return kobj;
}
#define kobject_put(kobj) kobject_unregister(kobj)
#endif /* < KERNEL_VERSION(2, 6, 25) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
static const char hex_asc[] = "0123456789abcdef";
#define hex_asc_lo(x) hex_asc[((x) & 0x0f)]
#define hex_asc_hi(x) hex_asc[((x) & 0xf0) >> 4]
static inline char *pack_hex_byte(char *buf, u8 byte)
{
*buf++ = hex_asc_hi(byte);
*buf++ = hex_asc_lo(byte);
return buf;
}
#endif /* < KERNEL_VERSION(2, 6, 26) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 27)
#ifndef dereference_function_descriptor
#define dereference_function_descriptor(p) (p)
#endif
#include <linux/debugfs.h>
static inline void debugfs_remove_recursive(struct dentry *dentry)
{
struct dentry *child;
struct dentry *parent;
if (!dentry)
return;
parent = dentry->d_parent;
if (!parent || !parent->d_inode)
return;
parent = dentry;
while (1) {
/*
* When all dentries under "parent" has been removed,
* walk up the tree until we reach our starting point.
*/
if (list_empty(&parent->d_subdirs)) {
if (parent == dentry)
break;
parent = parent->d_parent;
}
child = list_entry(parent->d_subdirs.next, struct dentry,
d_u.d_child);
next_sibling:
/*
* If "child" isn't empty, walk down the tree and
* remove all its descendants first.
*/
if (!list_empty(&child->d_subdirs)) {
parent = child;
continue;
}
debugfs_remove(child);
if (parent->d_subdirs.next == &child->d_u.d_child) {
/*
* Try the next sibling.
*/
if (child->d_u.d_child.next != &parent->d_subdirs) {
child = list_entry(child->d_u.d_child.next,
struct dentry,
d_u.d_child);
goto next_sibling;
}
break;
}
}
debugfs_remove(dentry);
}
#endif /* < KERNEL_VERSION(2, 6, 27) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 29)
int bat_vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
#define vscnprintf bat_vscnprintf
asmlinkage int bat_printk(const char *fmt, ...);
#define printk bat_printk
int bat_sprintf(char *buf, const char *fmt, ...);
#define sprintf bat_sprintf
int bat_snprintf(char *buf, size_t size, const char *fmt, ...);
#define snprintf bat_snprintf
int bat_seq_printf(struct seq_file *m, const char *f, ...);
#define seq_printf bat_seq_printf
#endif /* < KERNEL_VERSION(2, 6, 29) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
#define __always_unused __attribute__((unused))
#endif /* < KERNEL_VERSION(2, 6, 33) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 34)
#define hlist_first_rcu(head) (*((struct hlist_node **)(&(head)->first)))
#define hlist_next_rcu(node) (*((struct hlist_node **)(&(node)->next)))
#define __hlist_for_each_rcu(pos, head) \
for (pos = rcu_dereference(hlist_first_rcu(head)); \
pos && ({ prefetch(pos->next); 1; }); \
pos = rcu_dereference(hlist_next_rcu(pos)))
#endif /* < KERNEL_VERSION(2, 6, 34) */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
#define __rcu
#endif /* < KERNEL_VERSION(2, 6, 36) */
#endif /* _NET_BATMAN_ADV_COMPAT_H_ */