]>
Commit | Line | Data |
---|---|---|
e2d28598 JR |
1 | net: add Intermediate Queueing Device (imq) |
2 | ||
3 | From: Jussi Kivilinna <jussi.kivilinna@iki.fi> | |
4 | ||
5 | See: http://linuximq.net/ | |
6 | ||
7 | Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi> | |
8 | --- | |
9 | drivers/net/Kconfig | 119 ++++ | |
10 | drivers/net/Makefile | 1 | |
11 | drivers/net/imq.c | 1001 +++++++++++++++++++++++++++++++ | |
12 | include/linux/imq.h | 13 | |
13 | include/linux/netfilter/xt_IMQ.h | 9 | |
14 | include/linux/netfilter_ipv4/ipt_IMQ.h | 10 | |
15 | include/linux/netfilter_ipv6/ip6t_IMQ.h | 10 | |
16 | include/linux/skbuff.h | 23 + | |
17 | include/net/netfilter/nf_queue.h | 6 | |
18 | include/uapi/linux/netfilter.h | 3 | |
19 | net/core/dev.c | 8 | |
20 | net/core/skbuff.c | 112 +++ | |
21 | net/ipv6/ip6_output.c | 10 | |
22 | net/netfilter/Kconfig | 12 | |
23 | net/netfilter/Makefile | 1 | |
24 | net/netfilter/core.c | 6 | |
25 | net/netfilter/nf_internals.h | 2 | |
26 | net/netfilter/nf_queue.c | 36 + | |
27 | net/netfilter/xt_IMQ.c | 72 ++ | |
28 | 19 files changed, 1444 insertions(+), 10 deletions(-) | |
29 | create mode 100644 drivers/net/imq.c | |
30 | create mode 100644 include/linux/imq.h | |
31 | create mode 100644 include/linux/netfilter/xt_IMQ.h | |
32 | create mode 100644 include/linux/netfilter_ipv4/ipt_IMQ.h | |
33 | create mode 100644 include/linux/netfilter_ipv6/ip6t_IMQ.h | |
34 | create mode 100644 net/netfilter/xt_IMQ.c | |
35 | ||
36 | diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig | |
37 | index 3835321..77550a9 100644 | |
38 | --- a/drivers/net/Kconfig | |
39 | +++ b/drivers/net/Kconfig | |
40 | @@ -207,6 +207,125 @@ config RIONET_RX_SIZE | |
41 | depends on RIONET | |
42 | default "128" | |
43 | ||
44 | +config IMQ | |
45 | + tristate "IMQ (intermediate queueing device) support" | |
46 | + depends on NETDEVICES && NETFILTER | |
47 | + ---help--- | |
48 | + The IMQ device(s) is used as placeholder for QoS queueing | |
49 | + disciplines. Every packet entering/leaving the IP stack can be | |
50 | + directed through the IMQ device where it's enqueued/dequeued to the | |
51 | + attached qdisc. This allows you to treat network devices as classes | |
52 | + and distribute bandwidth among them. Iptables is used to specify | |
53 | + through which IMQ device, if any, packets travel. | |
54 | + | |
55 | + More information at: http://www.linuximq.net/ | |
56 | + | |
57 | + To compile this driver as a module, choose M here: the module | |
58 | + will be called imq. If unsure, say N. | |
59 | + | |
60 | +choice | |
61 | + prompt "IMQ behavior (PRE/POSTROUTING)" | |
62 | + depends on IMQ | |
63 | + default IMQ_BEHAVIOR_AB | |
64 | + help | |
65 | + This setting defines how IMQ behaves in respect to its | |
66 | + hooking in PREROUTING and POSTROUTING. | |
67 | + | |
68 | + IMQ can work in any of the following ways: | |
69 | + | |
70 | + PREROUTING | POSTROUTING | |
71 | + -----------------|------------------- | |
72 | + #1 After NAT | After NAT | |
73 | + #2 After NAT | Before NAT | |
74 | + #3 Before NAT | After NAT | |
75 | + #4 Before NAT | Before NAT | |
76 | + | |
77 | + The default behavior is to hook before NAT on PREROUTING | |
78 | + and after NAT on POSTROUTING (#3). | |
79 | + | |
80 | + This settings are specially usefull when trying to use IMQ | |
81 | + to shape NATed clients. | |
82 | + | |
83 | + More information can be found at: www.linuximq.net | |
84 | + | |
85 | + If not sure leave the default settings alone. | |
86 | + | |
87 | +config IMQ_BEHAVIOR_AA | |
88 | + bool "IMQ AA" | |
89 | + help | |
90 | + This setting defines how IMQ behaves in respect to its | |
91 | + hooking in PREROUTING and POSTROUTING. | |
92 | + | |
93 | + Choosing this option will make IMQ hook like this: | |
94 | + | |
95 | + PREROUTING: After NAT | |
96 | + POSTROUTING: After NAT | |
97 | + | |
98 | + More information can be found at: www.linuximq.net | |
99 | + | |
100 | + If not sure leave the default settings alone. | |
101 | + | |
102 | +config IMQ_BEHAVIOR_AB | |
103 | + bool "IMQ AB" | |
104 | + help | |
105 | + This setting defines how IMQ behaves in respect to its | |
106 | + hooking in PREROUTING and POSTROUTING. | |
107 | + | |
108 | + Choosing this option will make IMQ hook like this: | |
109 | + | |
110 | + PREROUTING: After NAT | |
111 | + POSTROUTING: Before NAT | |
112 | + | |
113 | + More information can be found at: www.linuximq.net | |
114 | + | |
115 | + If not sure leave the default settings alone. | |
116 | + | |
117 | +config IMQ_BEHAVIOR_BA | |
118 | + bool "IMQ BA" | |
119 | + help | |
120 | + This setting defines how IMQ behaves in respect to its | |
121 | + hooking in PREROUTING and POSTROUTING. | |
122 | + | |
123 | + Choosing this option will make IMQ hook like this: | |
124 | + | |
125 | + PREROUTING: Before NAT | |
126 | + POSTROUTING: After NAT | |
127 | + | |
128 | + More information can be found at: www.linuximq.net | |
129 | + | |
130 | + If not sure leave the default settings alone. | |
131 | + | |
132 | +config IMQ_BEHAVIOR_BB | |
133 | + bool "IMQ BB" | |
134 | + help | |
135 | + This setting defines how IMQ behaves in respect to its | |
136 | + hooking in PREROUTING and POSTROUTING. | |
137 | + | |
138 | + Choosing this option will make IMQ hook like this: | |
139 | + | |
140 | + PREROUTING: Before NAT | |
141 | + POSTROUTING: Before NAT | |
142 | + | |
143 | + More information can be found at: www.linuximq.net | |
144 | + | |
145 | + If not sure leave the default settings alone. | |
146 | + | |
147 | +endchoice | |
148 | + | |
149 | +config IMQ_NUM_DEVS | |
150 | + int "Number of IMQ devices" | |
151 | + range 2 16 | |
152 | + depends on IMQ | |
153 | + default "16" | |
154 | + help | |
155 | + This setting defines how many IMQ devices will be created. | |
156 | + | |
157 | + The default value is 16. | |
158 | + | |
159 | + More information can be found at: www.linuximq.net | |
160 | + | |
161 | + If not sure leave the default settings alone. | |
162 | + | |
163 | config TUN | |
164 | tristate "Universal TUN/TAP device driver support" | |
165 | select CRC32 | |
166 | diff --git a/drivers/net/Makefile b/drivers/net/Makefile | |
167 | index ef3d090..b474ff4 100644 | |
168 | --- a/drivers/net/Makefile | |
169 | +++ b/drivers/net/Makefile | |
170 | @@ -9,6 +9,7 @@ obj-$(CONFIG_BONDING) += bonding/ | |
171 | obj-$(CONFIG_DUMMY) += dummy.o | |
172 | obj-$(CONFIG_EQUALIZER) += eql.o | |
173 | obj-$(CONFIG_IFB) += ifb.o | |
174 | +obj-$(CONFIG_IMQ) += imq.o | |
175 | obj-$(CONFIG_MACVLAN) += macvlan.o | |
176 | obj-$(CONFIG_MACVTAP) += macvtap.o | |
177 | obj-$(CONFIG_MII) += mii.o | |
178 | diff --git a/drivers/net/imq.c b/drivers/net/imq.c | |
179 | new file mode 100644 | |
180 | index 0000000..fd3e6c5 | |
181 | --- /dev/null | |
182 | +++ b/drivers/net/imq.c | |
183 | @@ -0,0 +1,1001 @@ | |
2380c486 JR |
184 | +/* |
185 | + * Pseudo-driver for the intermediate queue device. | |
186 | + * | |
187 | + * This program is free software; you can redistribute it and/or | |
188 | + * modify it under the terms of the GNU General Public License | |
189 | + * as published by the Free Software Foundation; either version | |
190 | + * 2 of the License, or (at your option) any later version. | |
191 | + * | |
192 | + * Authors: Patrick McHardy, <kaber@trash.net> | |
193 | + * | |
194 | + * The first version was written by Martin Devera, <devik@cdi.cz> | |
195 | + * | |
196 | + * Credits: Jan Rafaj <imq2t@cedric.vabo.cz> | |
197 | + * - Update patch to 2.4.21 | |
198 | + * Sebastian Strollo <sstrollo@nortelnetworks.com> | |
199 | + * - Fix "Dead-loop on netdevice imq"-issue | |
200 | + * Marcel Sebek <sebek64@post.cz> | |
201 | + * - Update to 2.6.2-rc1 | |
202 | + * | |
203 | + * After some time of inactivity there is a group taking care | |
204 | + * of IMQ again: http://www.linuximq.net | |
205 | + * | |
206 | + * | |
207 | + * 2004/06/30 - New version of IMQ patch to kernels <=2.6.7 | |
208 | + * including the following changes: | |
209 | + * | |
210 | + * - Correction of ipv6 support "+"s issue (Hasso Tepper) | |
211 | + * - Correction of imq_init_devs() issue that resulted in | |
212 | + * kernel OOPS unloading IMQ as module (Norbert Buchmuller) | |
213 | + * - Addition of functionality to choose number of IMQ devices | |
214 | + * during kernel config (Andre Correa) | |
215 | + * - Addition of functionality to choose how IMQ hooks on | |
216 | + * PRE and POSTROUTING (after or before NAT) (Andre Correa) | |
217 | + * - Cosmetic corrections (Norbert Buchmuller) (Andre Correa) | |
218 | + * | |
219 | + * | |
220 | + * 2005/12/16 - IMQ versions between 2.6.7 and 2.6.13 were | |
221 | + * released with almost no problems. 2.6.14-x was released | |
222 | + * with some important changes: nfcache was removed; After | |
223 | + * some weeks of trouble we figured out that some IMQ fields | |
224 | + * in skb were missing in skbuff.c - skb_clone and copy_skb_header. | |
225 | + * These functions are correctly patched by this new patch version. | |
226 | + * | |
227 | + * Thanks for all who helped to figure out all the problems with | |
228 | + * 2.6.14.x: Patrick McHardy, Rune Kock, VeNoMouS, Max CtRiX, | |
229 | + * Kevin Shanahan, Richard Lucassen, Valery Dachev (hopefully | |
230 | + * I didn't forget anybody). I apologize again for my lack of time. | |
231 | + * | |
232 | + * | |
f6396b7e | 233 | + * 2008/06/17 - 2.6.25 - Changed imq.c to use qdisc_run() instead |
7f07242b | 234 | + * of qdisc_restart() and moved qdisc_run() to tasklet to avoid |
2380c486 JR |
235 | + * recursive locking. New initialization routines to fix 'rmmod' not |
236 | + * working anymore. Used code from ifb.c. (Jussi Kivilinna) | |
7f07242b | 237 | + * |
238 | + * 2008/08/06 - 2.6.26 - (JK) | |
239 | + * - Replaced tasklet with 'netif_schedule()'. | |
240 | + * - Cleaned up and added comments for imq_nf_queue(). | |
241 | + * | |
242 | + * 2009/04/12 | |
243 | + * - Add skb_save_cb/skb_restore_cb helper functions for backuping | |
244 | + * control buffer. This is needed because qdisc-layer on kernels | |
245 | + * 2.6.27 and newer overwrite control buffer. (Jussi Kivilinna) | |
246 | + * - Add better locking for IMQ device. Hopefully this will solve | |
247 | + * SMP issues. (Jussi Kivilinna) | |
248 | + * - Port to 2.6.27 | |
249 | + * - Port to 2.6.28 | |
250 | + * - Port to 2.6.29 + fix rmmod not working | |
251 | + * | |
252 | + * 2009/04/20 - (Jussi Kivilinna) | |
253 | + * - Use netdevice feature flags to avoid extra packet handling | |
254 | + * by core networking layer and possibly increase performance. | |
255 | + * | |
14f08cd0 | 256 | + * 2009/09/26 - (Jussi Kivilinna) |
257 | + * - Add imq_nf_reinject_lockless to fix deadlock with | |
258 | + * imq_nf_queue/imq_nf_reinject. | |
259 | + * | |
260 | + * 2009/12/08 - (Jussi Kivilinna) | |
261 | + * - Port to 2.6.32 | |
262 | + * - Add check for skb->nf_queue_entry==NULL in imq_dev_xmit() | |
263 | + * - Also add better error checking for skb->nf_queue_entry usage | |
264 | + * | |
76514441 AM |
265 | + * 2010/02/25 - (Jussi Kivilinna) |
266 | + * - Port to 2.6.33 | |
267 | + * | |
f6396b7e AM |
268 | + * 2010/08/15 - (Jussi Kivilinna) |
269 | + * - Port to 2.6.35 | |
270 | + * - Simplify hook registration by using nf_register_hooks. | |
271 | + * - nf_reinject doesn't need spinlock around it, therefore remove | |
272 | + * imq_nf_reinject function. Other nf_reinject users protect | |
273 | + * their own data with spinlock. With IMQ however all data is | |
274 | + * needed is stored per skbuff, so no locking is needed. | |
275 | + * - Changed IMQ to use 'separate' NF_IMQ_QUEUE instead of | |
276 | + * NF_QUEUE, this allows working coexistance of IMQ and other | |
277 | + * NF_QUEUE users. | |
278 | + * - Make IMQ multi-queue. Number of IMQ device queues can be | |
279 | + * increased with 'numqueues' module parameters. Default number | |
280 | + * of queues is 1, in other words by default IMQ works as | |
7af23471 | 281 | + * single-queue device. Multi-queue selection is based on |
f6396b7e AM |
282 | + * IFB multi-queue patch by Changli Gao <xiaosuo@gmail.com>. |
283 | + * | |
7af23471 JR |
284 | + * 2011/03/18 - (Jussi Kivilinna) |
285 | + * - Port to 2.6.38 | |
286 | + * | |
a168f21d AM |
287 | + * 2011/07/12 - (syoder89@gmail.com) |
288 | + * - Crash fix that happens when the receiving interface has more | |
289 | + * than one queue (add missing skb_set_queue_mapping in | |
290 | + * imq_select_queue). | |
291 | + * | |
292 | + * 2011/07/26 - (Jussi Kivilinna) | |
293 | + * - Add queue mapping checks for packets exiting IMQ. | |
294 | + * - Port to 3.0 | |
295 | + * | |
3b94b3c4 AM |
296 | + * 2011/08/16 - (Jussi Kivilinna) |
297 | + * - Clear IFF_TX_SKB_SHARING flag that was added for linux 3.0.2 | |
298 | + * | |
299 | + * 2011/11/03 - Germano Michel <germanomichel@gmail.com> | |
300 | + * - Fix IMQ for net namespaces | |
301 | + * | |
302 | + * 2011/11/04 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi> | |
303 | + * - Port to 3.1 | |
304 | + * - Clean-up, move 'get imq device pointer by imqX name' to | |
305 | + * separate function from imq_nf_queue(). | |
306 | + * | |
4bf69007 AM |
307 | + * 2012/01/05 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi> |
308 | + * - Port to 3.2 | |
309 | + * | |
310 | + * 2012/03/19 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi> | |
311 | + * - Port to 3.3 | |
312 | + * | |
514e5dae AM |
313 | + * 2012/12/12 - Jussi Kivilinna <jussi.kivilinna@mbnet.fi> |
314 | + * - Port to 3.7 | |
315 | + * - Fix checkpatch.pl warnings | |
316 | + * | |
e2d28598 JR |
317 | + * 2013/09/10 - Jussi Kivilinna <jussi.kivilinna@iki.fi> |
318 | + * - Fixed GSO handling for 3.10, see imq_nf_queue() for comments. | |
319 | + * - Don't copy skb->cb_next when copying or cloning skbuffs. | |
320 | + * | |
2380c486 JR |
321 | + * Also, many thanks to pablo Sebastian Greco for making the initial |
322 | + * patch and to those who helped the testing. | |
323 | + * | |
324 | + * More info at: http://www.linuximq.net/ (Andre Correa) | |
325 | + */ | |
326 | + | |
327 | +#include <linux/module.h> | |
328 | +#include <linux/kernel.h> | |
329 | +#include <linux/moduleparam.h> | |
7f07242b | 330 | +#include <linux/list.h> |
2380c486 JR |
331 | +#include <linux/skbuff.h> |
332 | +#include <linux/netdevice.h> | |
7f07242b | 333 | +#include <linux/etherdevice.h> |
2380c486 JR |
334 | +#include <linux/rtnetlink.h> |
335 | +#include <linux/if_arp.h> | |
336 | +#include <linux/netfilter.h> | |
337 | +#include <linux/netfilter_ipv4.h> | |
338 | +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
339 | + #include <linux/netfilter_ipv6.h> | |
340 | +#endif | |
341 | +#include <linux/imq.h> | |
342 | +#include <net/pkt_sched.h> | |
343 | +#include <net/netfilter/nf_queue.h> | |
f6396b7e AM |
344 | +#include <net/sock.h> |
345 | +#include <linux/ip.h> | |
346 | +#include <linux/ipv6.h> | |
347 | +#include <linux/if_vlan.h> | |
348 | +#include <linux/if_pppox.h> | |
349 | +#include <net/ip.h> | |
350 | +#include <net/ipv6.h> | |
351 | + | |
e2d28598 | 352 | +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num); |
2380c486 | 353 | + |
2380c486 JR |
354 | +static nf_hookfn imq_nf_hook; |
355 | + | |
f6396b7e AM |
356 | +static struct nf_hook_ops imq_ops[] = { |
357 | + { | |
358 | + /* imq_ingress_ipv4 */ | |
359 | + .hook = imq_nf_hook, | |
360 | + .owner = THIS_MODULE, | |
361 | + .pf = PF_INET, | |
362 | + .hooknum = NF_INET_PRE_ROUTING, | |
2380c486 | 363 | +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB) |
f6396b7e | 364 | + .priority = NF_IP_PRI_MANGLE + 1, |
2380c486 | 365 | +#else |
f6396b7e | 366 | + .priority = NF_IP_PRI_NAT_DST + 1, |
2380c486 | 367 | +#endif |
f6396b7e AM |
368 | + }, |
369 | + { | |
370 | + /* imq_egress_ipv4 */ | |
371 | + .hook = imq_nf_hook, | |
372 | + .owner = THIS_MODULE, | |
373 | + .pf = PF_INET, | |
374 | + .hooknum = NF_INET_POST_ROUTING, | |
2380c486 | 375 | +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA) |
f6396b7e | 376 | + .priority = NF_IP_PRI_LAST, |
2380c486 | 377 | +#else |
f6396b7e | 378 | + .priority = NF_IP_PRI_NAT_SRC - 1, |
2380c486 | 379 | +#endif |
f6396b7e | 380 | + }, |
2380c486 | 381 | +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
f6396b7e AM |
382 | + { |
383 | + /* imq_ingress_ipv6 */ | |
384 | + .hook = imq_nf_hook, | |
385 | + .owner = THIS_MODULE, | |
386 | + .pf = PF_INET6, | |
387 | + .hooknum = NF_INET_PRE_ROUTING, | |
2380c486 | 388 | +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB) |
f6396b7e | 389 | + .priority = NF_IP6_PRI_MANGLE + 1, |
2380c486 | 390 | +#else |
f6396b7e | 391 | + .priority = NF_IP6_PRI_NAT_DST + 1, |
2380c486 | 392 | +#endif |
f6396b7e AM |
393 | + }, |
394 | + { | |
395 | + /* imq_egress_ipv6 */ | |
396 | + .hook = imq_nf_hook, | |
397 | + .owner = THIS_MODULE, | |
398 | + .pf = PF_INET6, | |
399 | + .hooknum = NF_INET_POST_ROUTING, | |
2380c486 | 400 | +#if defined(CONFIG_IMQ_BEHAVIOR_AA) || defined(CONFIG_IMQ_BEHAVIOR_BA) |
f6396b7e | 401 | + .priority = NF_IP6_PRI_LAST, |
2380c486 | 402 | +#else |
f6396b7e | 403 | + .priority = NF_IP6_PRI_NAT_SRC - 1, |
2380c486 | 404 | +#endif |
f6396b7e | 405 | + }, |
2380c486 | 406 | +#endif |
f6396b7e | 407 | +}; |
2380c486 JR |
408 | + |
409 | +#if defined(CONFIG_IMQ_NUM_DEVS) | |
f6396b7e | 410 | +static int numdevs = CONFIG_IMQ_NUM_DEVS; |
2380c486 | 411 | +#else |
f6396b7e | 412 | +static int numdevs = IMQ_MAX_DEVS; |
2380c486 JR |
413 | +#endif |
414 | + | |
415 | +static struct net_device *imq_devs_cache[IMQ_MAX_DEVS]; | |
416 | + | |
a168f21d AM |
417 | +#define IMQ_MAX_QUEUES 32 |
418 | +static int numqueues = 1; | |
f6396b7e AM |
419 | +static u32 imq_hashrnd; |
420 | + | |
421 | +static inline __be16 pppoe_proto(const struct sk_buff *skb) | |
422 | +{ | |
423 | + return *((__be16 *)(skb_mac_header(skb) + ETH_HLEN + | |
424 | + sizeof(struct pppoe_hdr))); | |
425 | +} | |
426 | + | |
427 | +static u16 imq_hash(struct net_device *dev, struct sk_buff *skb) | |
428 | +{ | |
429 | + unsigned int pull_len; | |
430 | + u16 protocol = skb->protocol; | |
431 | + u32 addr1, addr2; | |
432 | + u32 hash, ihl = 0; | |
433 | + union { | |
434 | + u16 in16[2]; | |
435 | + u32 in32; | |
436 | + } ports; | |
437 | + u8 ip_proto; | |
438 | + | |
439 | + pull_len = 0; | |
440 | + | |
441 | +recheck: | |
442 | + switch (protocol) { | |
443 | + case htons(ETH_P_8021Q): { | |
444 | + if (unlikely(skb_pull(skb, VLAN_HLEN) == NULL)) | |
445 | + goto other; | |
446 | + | |
447 | + pull_len += VLAN_HLEN; | |
448 | + skb->network_header += VLAN_HLEN; | |
449 | + | |
450 | + protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto; | |
451 | + goto recheck; | |
452 | + } | |
453 | + | |
454 | + case htons(ETH_P_PPP_SES): { | |
455 | + if (unlikely(skb_pull(skb, PPPOE_SES_HLEN) == NULL)) | |
456 | + goto other; | |
457 | + | |
458 | + pull_len += PPPOE_SES_HLEN; | |
459 | + skb->network_header += PPPOE_SES_HLEN; | |
460 | + | |
461 | + protocol = pppoe_proto(skb); | |
462 | + goto recheck; | |
463 | + } | |
464 | + | |
465 | + case htons(ETH_P_IP): { | |
466 | + const struct iphdr *iph = ip_hdr(skb); | |
467 | + | |
468 | + if (unlikely(!pskb_may_pull(skb, sizeof(struct iphdr)))) | |
469 | + goto other; | |
470 | + | |
471 | + addr1 = iph->daddr; | |
472 | + addr2 = iph->saddr; | |
473 | + | |
474 | + ip_proto = !(ip_hdr(skb)->frag_off & htons(IP_MF | IP_OFFSET)) ? | |
475 | + iph->protocol : 0; | |
476 | + ihl = ip_hdrlen(skb); | |
477 | + | |
478 | + break; | |
479 | + } | |
480 | +#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) | |
481 | + case htons(ETH_P_IPV6): { | |
482 | + const struct ipv6hdr *iph = ipv6_hdr(skb); | |
4bf69007 AM |
483 | + __be16 fo = 0; |
484 | + | |
f6396b7e AM |
485 | + if (unlikely(!pskb_may_pull(skb, sizeof(struct ipv6hdr)))) |
486 | + goto other; | |
487 | + | |
488 | + addr1 = iph->daddr.s6_addr32[3]; | |
489 | + addr2 = iph->saddr.s6_addr32[3]; | |
514e5dae AM |
490 | + ihl = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &ip_proto, |
491 | + &fo); | |
f6396b7e AM |
492 | + if (unlikely(ihl < 0)) |
493 | + goto other; | |
494 | + | |
495 | + break; | |
496 | + } | |
497 | +#endif | |
498 | + default: | |
499 | +other: | |
500 | + if (pull_len != 0) { | |
501 | + skb_push(skb, pull_len); | |
502 | + skb->network_header -= pull_len; | |
503 | + } | |
504 | + | |
505 | + return (u16)(ntohs(protocol) % dev->real_num_tx_queues); | |
506 | + } | |
507 | + | |
508 | + if (addr1 > addr2) | |
509 | + swap(addr1, addr2); | |
510 | + | |
511 | + switch (ip_proto) { | |
512 | + case IPPROTO_TCP: | |
513 | + case IPPROTO_UDP: | |
514 | + case IPPROTO_DCCP: | |
515 | + case IPPROTO_ESP: | |
516 | + case IPPROTO_AH: | |
517 | + case IPPROTO_SCTP: | |
518 | + case IPPROTO_UDPLITE: { | |
519 | + if (likely(skb_copy_bits(skb, ihl, &ports.in32, 4) >= 0)) { | |
520 | + if (ports.in16[0] > ports.in16[1]) | |
521 | + swap(ports.in16[0], ports.in16[1]); | |
522 | + break; | |
523 | + } | |
524 | + /* fall-through */ | |
525 | + } | |
526 | + default: | |
527 | + ports.in32 = 0; | |
528 | + break; | |
529 | + } | |
530 | + | |
531 | + if (pull_len != 0) { | |
532 | + skb_push(skb, pull_len); | |
533 | + skb->network_header -= pull_len; | |
534 | + } | |
535 | + | |
536 | + hash = jhash_3words(addr1, addr2, ports.in32, imq_hashrnd ^ ip_proto); | |
537 | + | |
538 | + return (u16)(((u64)hash * dev->real_num_tx_queues) >> 32); | |
539 | +} | |
540 | + | |
541 | +static inline bool sk_tx_queue_recorded(struct sock *sk) | |
542 | +{ | |
543 | + return (sk_tx_queue_get(sk) >= 0); | |
544 | +} | |
545 | + | |
546 | +static struct netdev_queue *imq_select_queue(struct net_device *dev, | |
547 | + struct sk_buff *skb) | |
548 | +{ | |
549 | + u16 queue_index = 0; | |
550 | + u32 hash; | |
551 | + | |
552 | + if (likely(dev->real_num_tx_queues == 1)) | |
553 | + goto out; | |
554 | + | |
555 | + /* IMQ can be receiving ingress or engress packets. */ | |
556 | + | |
557 | + /* Check first for if rx_queue is set */ | |
558 | + if (skb_rx_queue_recorded(skb)) { | |
559 | + queue_index = skb_get_rx_queue(skb); | |
560 | + goto out; | |
561 | + } | |
562 | + | |
563 | + /* Check if socket has tx_queue set */ | |
564 | + if (sk_tx_queue_recorded(skb->sk)) { | |
565 | + queue_index = sk_tx_queue_get(skb->sk); | |
566 | + goto out; | |
567 | + } | |
568 | + | |
569 | + /* Try use socket hash */ | |
570 | + if (skb->sk && skb->sk->sk_hash) { | |
571 | + hash = skb->sk->sk_hash; | |
572 | + queue_index = | |
573 | + (u16)(((u64)hash * dev->real_num_tx_queues) >> 32); | |
574 | + goto out; | |
575 | + } | |
576 | + | |
577 | + /* Generate hash from packet data */ | |
578 | + queue_index = imq_hash(dev, skb); | |
579 | + | |
580 | +out: | |
581 | + if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
582 | + queue_index = (u16)((u32)queue_index % dev->real_num_tx_queues); | |
583 | + | |
a168f21d | 584 | + skb_set_queue_mapping(skb, queue_index); |
f6396b7e AM |
585 | + return netdev_get_tx_queue(dev, queue_index); |
586 | +} | |
587 | + | |
a168f21d AM |
588 | +static struct net_device_stats *imq_get_stats(struct net_device *dev) |
589 | +{ | |
590 | + return &dev->stats; | |
591 | +} | |
592 | + | |
593 | +/* called for packets kfree'd in qdiscs at places other than enqueue */ | |
594 | +static void imq_skb_destructor(struct sk_buff *skb) | |
595 | +{ | |
596 | + struct nf_queue_entry *entry = skb->nf_queue_entry; | |
597 | + | |
598 | + skb->nf_queue_entry = NULL; | |
599 | + | |
600 | + if (entry) { | |
601 | + nf_queue_entry_release_refs(entry); | |
602 | + kfree(entry); | |
603 | + } | |
604 | + | |
605 | + skb_restore_cb(skb); /* kfree backup */ | |
606 | +} | |
607 | + | |
608 | +static void imq_done_check_queue_mapping(struct sk_buff *skb, | |
609 | + struct net_device *dev) | |
610 | +{ | |
611 | + unsigned int queue_index; | |
612 | + | |
613 | + /* Don't let queue_mapping be left too large after exiting IMQ */ | |
614 | + if (likely(skb->dev != dev && skb->dev != NULL)) { | |
615 | + queue_index = skb_get_queue_mapping(skb); | |
616 | + if (unlikely(queue_index >= skb->dev->real_num_tx_queues)) { | |
617 | + queue_index = (u16)((u32)queue_index % | |
618 | + skb->dev->real_num_tx_queues); | |
619 | + skb_set_queue_mapping(skb, queue_index); | |
620 | + } | |
621 | + } else { | |
622 | + /* skb->dev was IMQ device itself or NULL, be on safe side and | |
623 | + * just clear queue mapping. | |
624 | + */ | |
625 | + skb_set_queue_mapping(skb, 0); | |
626 | + } | |
627 | +} | |
628 | + | |
629 | +static netdev_tx_t imq_dev_xmit(struct sk_buff *skb, struct net_device *dev) | |
630 | +{ | |
631 | + struct nf_queue_entry *entry = skb->nf_queue_entry; | |
632 | + | |
633 | + skb->nf_queue_entry = NULL; | |
634 | + dev->trans_start = jiffies; | |
635 | + | |
636 | + dev->stats.tx_bytes += skb->len; | |
637 | + dev->stats.tx_packets++; | |
638 | + | |
639 | + if (unlikely(entry == NULL)) { | |
640 | + /* We don't know what is going on here.. packet is queued for | |
641 | + * imq device, but (probably) not by us. | |
642 | + * | |
643 | + * If this packet was not send here by imq_nf_queue(), then | |
644 | + * skb_save_cb() was not used and skb_free() should not show: | |
645 | + * WARNING: IMQ: kfree_skb: skb->cb_next:.. | |
646 | + * and/or | |
647 | + * WARNING: IMQ: kfree_skb: skb->nf_queue_entry... | |
648 | + * | |
649 | + * However if this message is shown, then IMQ is somehow broken | |
650 | + * and you should report this to linuximq.net. | |
651 | + */ | |
652 | + | |
653 | + /* imq_dev_xmit is black hole that eats all packets, report that | |
654 | + * we eat this packet happily and increase dropped counters. | |
655 | + */ | |
656 | + | |
657 | + dev->stats.tx_dropped++; | |
658 | + dev_kfree_skb(skb); | |
659 | + | |
660 | + return NETDEV_TX_OK; | |
661 | + } | |
662 | + | |
663 | + skb_restore_cb(skb); /* restore skb->cb */ | |
664 | + | |
665 | + skb->imq_flags = 0; | |
666 | + skb->destructor = NULL; | |
667 | + | |
668 | + imq_done_check_queue_mapping(skb, dev); | |
669 | + | |
670 | + nf_reinject(entry, NF_ACCEPT); | |
671 | + | |
672 | + return NETDEV_TX_OK; | |
673 | +} | |
674 | + | |
3b94b3c4 AM |
675 | +static struct net_device *get_imq_device_by_index(int index) |
676 | +{ | |
677 | + struct net_device *dev = NULL; | |
678 | + struct net *net; | |
679 | + char buf[8]; | |
680 | + | |
681 | + /* get device by name and cache result */ | |
682 | + snprintf(buf, sizeof(buf), "imq%d", index); | |
683 | + | |
684 | + /* Search device from all namespaces. */ | |
685 | + for_each_net(net) { | |
686 | + dev = dev_get_by_name(net, buf); | |
687 | + if (dev) | |
688 | + break; | |
689 | + } | |
690 | + | |
691 | + if (WARN_ON_ONCE(dev == NULL)) { | |
692 | + /* IMQ device not found. Exotic config? */ | |
693 | + return ERR_PTR(-ENODEV); | |
694 | + } | |
695 | + | |
696 | + imq_devs_cache[index] = dev; | |
697 | + dev_put(dev); | |
698 | + | |
699 | + return dev; | |
700 | +} | |
701 | + | |
e2d28598 | 702 | +static struct nf_queue_entry *nf_queue_entry_dup(struct nf_queue_entry *e) |
2380c486 | 703 | +{ |
e2d28598 JR |
704 | + struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC); |
705 | + if (entry) { | |
706 | + if (nf_queue_entry_get_refs(entry)) | |
707 | + return entry; | |
708 | + kfree(entry); | |
709 | + } | |
710 | + return NULL; | |
711 | +} | |
712 | + | |
713 | +#ifdef CONFIG_BRIDGE_NETFILTER | |
714 | +/* When called from bridge netfilter, skb->data must point to MAC header | |
715 | + * before calling skb_gso_segment(). Else, original MAC header is lost | |
716 | + * and segmented skbs will be sent to wrong destination. | |
717 | + */ | |
718 | +static void nf_bridge_adjust_skb_data(struct sk_buff *skb) | |
719 | +{ | |
720 | + if (skb->nf_bridge) | |
721 | + __skb_push(skb, skb->network_header - skb->mac_header); | |
722 | +} | |
723 | + | |
724 | +static void nf_bridge_adjust_segmented_data(struct sk_buff *skb) | |
725 | +{ | |
726 | + if (skb->nf_bridge) | |
727 | + __skb_pull(skb, skb->network_header - skb->mac_header); | |
728 | +} | |
729 | +#else | |
730 | +#define nf_bridge_adjust_skb_data(s) do {} while (0) | |
731 | +#define nf_bridge_adjust_segmented_data(s) do {} while (0) | |
732 | +#endif | |
733 | + | |
734 | +static void free_entry(struct nf_queue_entry *entry) | |
735 | +{ | |
736 | + nf_queue_entry_release_refs(entry); | |
737 | + kfree(entry); | |
738 | +} | |
739 | + | |
740 | +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev); | |
741 | + | |
742 | +static int __imq_nf_queue_gso(struct nf_queue_entry *entry, | |
743 | + struct net_device *dev, struct sk_buff *skb) | |
744 | +{ | |
745 | + int ret = -ENOMEM; | |
746 | + struct nf_queue_entry *entry_seg; | |
747 | + | |
748 | + nf_bridge_adjust_segmented_data(skb); | |
749 | + | |
750 | + if (skb->next == NULL) { /* last packet, no need to copy entry */ | |
751 | + struct sk_buff *gso_skb = entry->skb; | |
752 | + entry->skb = skb; | |
753 | + ret = __imq_nf_queue(entry, dev); | |
754 | + if (ret) | |
755 | + entry->skb = gso_skb; | |
756 | + return ret; | |
757 | + } | |
758 | + | |
759 | + skb->next = NULL; | |
760 | + | |
761 | + entry_seg = nf_queue_entry_dup(entry); | |
762 | + if (entry_seg) { | |
763 | + entry_seg->skb = skb; | |
764 | + ret = __imq_nf_queue(entry_seg, dev); | |
765 | + if (ret) | |
766 | + free_entry(entry_seg); | |
767 | + } | |
768 | + return ret; | |
769 | +} | |
770 | + | |
771 | +static int imq_nf_queue(struct nf_queue_entry *entry, unsigned queue_num) | |
772 | +{ | |
773 | + struct sk_buff *skb, *segs; | |
2380c486 | 774 | + struct net_device *dev; |
e2d28598 JR |
775 | + unsigned int queued; |
776 | + int index, retval, err; | |
7f07242b | 777 | + |
778 | + index = entry->skb->imq_flags & IMQ_F_IFMASK; | |
779 | + if (unlikely(index > numdevs - 1)) { | |
780 | + if (net_ratelimit()) | |
514e5dae AM |
781 | + pr_warn("IMQ: invalid device specified, highest is %u\n", |
782 | + numdevs - 1); | |
7f07242b | 783 | + retval = -EINVAL; |
e2d28598 | 784 | + goto out_no_dev; |
7f07242b | 785 | + } |
2380c486 JR |
786 | + |
787 | + /* check for imq device by index from cache */ | |
788 | + dev = imq_devs_cache[index]; | |
7f07242b | 789 | + if (unlikely(!dev)) { |
3b94b3c4 AM |
790 | + dev = get_imq_device_by_index(index); |
791 | + if (IS_ERR(dev)) { | |
792 | + retval = PTR_ERR(dev); | |
e2d28598 | 793 | + goto out_no_dev; |
2380c486 | 794 | + } |
2380c486 JR |
795 | + } |
796 | + | |
7f07242b | 797 | + if (unlikely(!(dev->flags & IFF_UP))) { |
2380c486 | 798 | + entry->skb->imq_flags = 0; |
e2d28598 JR |
799 | + retval = -ECANCELED; |
800 | + goto out_no_dev; | |
2380c486 | 801 | + } |
e2d28598 JR |
802 | + |
803 | + if (!skb_is_gso(entry->skb)) | |
804 | + return __imq_nf_queue(entry, dev); | |
805 | + | |
806 | + /* Since 3.10.x, GSO handling moved here as result of upstream commit | |
807 | + * a5fedd43d5f6c94c71053a66e4c3d2e35f1731a2 (netfilter: move | |
808 | + * skb_gso_segment into nfnetlink_queue module). | |
809 | + * | |
810 | + * Following code replicates the gso handling from | |
811 | + * 'net/netfilter/nfnetlink_queue_core.c':nfqnl_enqueue_packet(). | |
812 | + */ | |
813 | + | |
814 | + skb = entry->skb; | |
815 | + | |
816 | + switch (entry->pf) { | |
817 | + case NFPROTO_IPV4: | |
818 | + skb->protocol = htons(ETH_P_IP); | |
819 | + break; | |
820 | + case NFPROTO_IPV6: | |
821 | + skb->protocol = htons(ETH_P_IPV6); | |
822 | + break; | |
823 | + } | |
824 | + | |
825 | + nf_bridge_adjust_skb_data(skb); | |
826 | + segs = skb_gso_segment(skb, 0); | |
827 | + /* Does not use PTR_ERR to limit the number of error codes that can be | |
828 | + * returned by nf_queue. For instance, callers rely on -ECANCELED to | |
829 | + * mean 'ignore this hook'. | |
830 | + */ | |
831 | + err = -ENOBUFS; | |
832 | + if (IS_ERR(segs)) | |
833 | + goto out_err; | |
834 | + queued = 0; | |
835 | + err = 0; | |
836 | + do { | |
837 | + struct sk_buff *nskb = segs->next; | |
838 | + if (nskb && nskb->next) | |
839 | + nskb->cb_next = NULL; | |
840 | + if (err == 0) | |
841 | + err = __imq_nf_queue_gso(entry, dev, segs); | |
842 | + if (err == 0) | |
843 | + queued++; | |
844 | + else | |
845 | + kfree_skb(segs); | |
846 | + segs = nskb; | |
847 | + } while (segs); | |
848 | + | |
849 | + if (queued) { | |
850 | + if (err) /* some segments are already queued */ | |
851 | + free_entry(entry); | |
852 | + kfree_skb(skb); | |
853 | + return 0; | |
854 | + } | |
855 | + | |
856 | +out_err: | |
857 | + nf_bridge_adjust_segmented_data(skb); | |
858 | + retval = err; | |
859 | +out_no_dev: | |
860 | + return retval; | |
861 | +} | |
862 | + | |
863 | +static int __imq_nf_queue(struct nf_queue_entry *entry, struct net_device *dev) | |
864 | +{ | |
865 | + struct sk_buff *skb_orig, *skb, *skb_shared; | |
866 | + struct Qdisc *q; | |
867 | + struct netdev_queue *txq; | |
868 | + spinlock_t *root_lock; | |
869 | + int users; | |
870 | + int retval = -EINVAL; | |
871 | + unsigned int orig_queue_index; | |
872 | + | |
2380c486 JR |
873 | + dev->last_rx = jiffies; |
874 | + | |
7f07242b | 875 | + skb = entry->skb; |
876 | + skb_orig = NULL; | |
877 | + | |
878 | + /* skb has owner? => make clone */ | |
879 | + if (unlikely(skb->destructor)) { | |
880 | + skb_orig = skb; | |
881 | + skb = skb_clone(skb, GFP_ATOMIC); | |
f6396b7e | 882 | + if (unlikely(!skb)) { |
7f07242b | 883 | + retval = -ENOMEM; |
884 | + goto out; | |
885 | + } | |
e2d28598 | 886 | + skb->cb_next = NULL; |
7f07242b | 887 | + entry->skb = skb; |
2380c486 | 888 | + } |
2380c486 | 889 | + |
7f07242b | 890 | + skb->nf_queue_entry = entry; |
891 | + | |
892 | + dev->stats.rx_bytes += skb->len; | |
2380c486 JR |
893 | + dev->stats.rx_packets++; |
894 | + | |
a168f21d AM |
895 | + if (!skb->dev) { |
896 | + /* skb->dev == NULL causes problems, try the find cause. */ | |
897 | + if (net_ratelimit()) { | |
898 | + dev_warn(&dev->dev, | |
899 | + "received packet with skb->dev == NULL\n"); | |
900 | + dump_stack(); | |
901 | + } | |
902 | + | |
903 | + skb->dev = dev; | |
904 | + } | |
905 | + | |
f6396b7e AM |
906 | + /* Disables softirqs for lock below */ |
907 | + rcu_read_lock_bh(); | |
908 | + | |
909 | + /* Multi-queue selection */ | |
a168f21d | 910 | + orig_queue_index = skb_get_queue_mapping(skb); |
f6396b7e | 911 | + txq = imq_select_queue(dev, skb); |
2380c486 | 912 | + |
7f07242b | 913 | + q = rcu_dereference(txq->qdisc); |
914 | + if (unlikely(!q->enqueue)) | |
915 | + goto packet_not_eaten_by_imq_dev; | |
2380c486 | 916 | + |
f6396b7e AM |
917 | + root_lock = qdisc_lock(q); |
918 | + spin_lock(root_lock); | |
7f07242b | 919 | + |
920 | + users = atomic_read(&skb->users); | |
921 | + | |
922 | + skb_shared = skb_get(skb); /* increase reference count by one */ | |
514e5dae AM |
923 | + |
924 | + /* backup skb->cb, as qdisc layer will overwrite it */ | |
925 | + skb_save_cb(skb_shared); | |
7f07242b | 926 | + qdisc_enqueue_root(skb_shared, q); /* might kfree_skb */ |
927 | + | |
928 | + if (likely(atomic_read(&skb_shared->users) == users + 1)) { | |
929 | + kfree_skb(skb_shared); /* decrease reference count by one */ | |
930 | + | |
931 | + skb->destructor = &imq_skb_destructor; | |
932 | + | |
933 | + /* cloned? */ | |
f6396b7e | 934 | + if (unlikely(skb_orig)) |
7f07242b | 935 | + kfree_skb(skb_orig); /* free original */ |
936 | + | |
f6396b7e AM |
937 | + spin_unlock(root_lock); |
938 | + rcu_read_unlock_bh(); | |
7f07242b | 939 | + |
940 | + /* schedule qdisc dequeue */ | |
941 | + __netif_schedule(q); | |
942 | + | |
943 | + retval = 0; | |
944 | + goto out; | |
945 | + } else { | |
946 | + skb_restore_cb(skb_shared); /* restore skb->cb */ | |
14f08cd0 | 947 | + skb->nf_queue_entry = NULL; |
514e5dae AM |
948 | + /* |
949 | + * qdisc dropped packet and decreased skb reference count of | |
7f07242b | 950 | + * skb, so we don't really want to and try refree as that would |
514e5dae AM |
951 | + * actually destroy the skb. |
952 | + */ | |
f6396b7e | 953 | + spin_unlock(root_lock); |
7f07242b | 954 | + goto packet_not_eaten_by_imq_dev; |
955 | + } | |
956 | + | |
957 | +packet_not_eaten_by_imq_dev: | |
a168f21d | 958 | + skb_set_queue_mapping(skb, orig_queue_index); |
f6396b7e AM |
959 | + rcu_read_unlock_bh(); |
960 | + | |
7f07242b | 961 | + /* cloned? restore original */ |
f6396b7e | 962 | + if (unlikely(skb_orig)) { |
7f07242b | 963 | + kfree_skb(skb); |
964 | + entry->skb = skb_orig; | |
965 | + } | |
966 | + retval = -1; | |
967 | +out: | |
968 | + return retval; | |
2380c486 JR |
969 | +} |
970 | + | |
2380c486 JR |
971 | +static unsigned int imq_nf_hook(unsigned int hook, struct sk_buff *pskb, |
972 | + const struct net_device *indev, | |
973 | + const struct net_device *outdev, | |
974 | + int (*okfn)(struct sk_buff *)) | |
975 | +{ | |
f6396b7e | 976 | + return (pskb->imq_flags & IMQ_F_ENQUEUE) ? NF_IMQ_QUEUE : NF_ACCEPT; |
2380c486 JR |
977 | +} |
978 | + | |
979 | +static int imq_close(struct net_device *dev) | |
980 | +{ | |
2380c486 | 981 | + netif_stop_queue(dev); |
2380c486 JR |
982 | + return 0; |
983 | +} | |
984 | + | |
985 | +static int imq_open(struct net_device *dev) | |
986 | +{ | |
2380c486 | 987 | + netif_start_queue(dev); |
2380c486 JR |
988 | + return 0; |
989 | +} | |
990 | + | |
7f07242b | 991 | +static const struct net_device_ops imq_netdev_ops = { |
992 | + .ndo_open = imq_open, | |
993 | + .ndo_stop = imq_close, | |
994 | + .ndo_start_xmit = imq_dev_xmit, | |
995 | + .ndo_get_stats = imq_get_stats, | |
996 | +}; | |
997 | + | |
2380c486 JR |
998 | +static void imq_setup(struct net_device *dev) |
999 | +{ | |
7f07242b | 1000 | + dev->netdev_ops = &imq_netdev_ops; |
3b94b3c4 AM |
1001 | + dev->type = ARPHRD_VOID; |
1002 | + dev->mtu = 16000; /* too small? */ | |
1003 | + dev->tx_queue_len = 11000; /* too big? */ | |
1004 | + dev->flags = IFF_NOARP; | |
1005 | + dev->features = NETIF_F_SG | NETIF_F_FRAGLIST | | |
7f07242b | 1006 | + NETIF_F_GSO | NETIF_F_HW_CSUM | |
1007 | + NETIF_F_HIGHDMA; | |
3b94b3c4 AM |
1008 | + dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | |
1009 | + IFF_TX_SKB_SHARING); | |
7f07242b | 1010 | +} |
1011 | + | |
1012 | +static int imq_validate(struct nlattr *tb[], struct nlattr *data[]) | |
1013 | +{ | |
1014 | + int ret = 0; | |
1015 | + | |
1016 | + if (tb[IFLA_ADDRESS]) { | |
1017 | + if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { | |
1018 | + ret = -EINVAL; | |
1019 | + goto end; | |
1020 | + } | |
1021 | + if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { | |
1022 | + ret = -EADDRNOTAVAIL; | |
1023 | + goto end; | |
1024 | + } | |
1025 | + } | |
1026 | + return 0; | |
1027 | +end: | |
514e5dae | 1028 | + pr_warn("IMQ: imq_validate failed (%d)\n", ret); |
7f07242b | 1029 | + return ret; |
2380c486 JR |
1030 | +} |
1031 | + | |
1032 | +static struct rtnl_link_ops imq_link_ops __read_mostly = { | |
1033 | + .kind = "imq", | |
7f07242b | 1034 | + .priv_size = 0, |
2380c486 | 1035 | + .setup = imq_setup, |
7f07242b | 1036 | + .validate = imq_validate, |
2380c486 JR |
1037 | +}; |
1038 | + | |
f6396b7e | 1039 | +static const struct nf_queue_handler imq_nfqh = { |
f6396b7e AM |
1040 | + .outfn = imq_nf_queue, |
1041 | +}; | |
1042 | + | |
2380c486 JR |
1043 | +static int __init imq_init_hooks(void) |
1044 | +{ | |
f6396b7e | 1045 | + int ret; |
2380c486 | 1046 | + |
f6396b7e | 1047 | + nf_register_queue_imq_handler(&imq_nfqh); |
2380c486 | 1048 | + |
f6396b7e AM |
1049 | + ret = nf_register_hooks(imq_ops, ARRAY_SIZE(imq_ops)); |
1050 | + if (ret < 0) | |
1051 | + nf_unregister_queue_imq_handler(); | |
2380c486 | 1052 | + |
f6396b7e | 1053 | + return ret; |
2380c486 JR |
1054 | +} |
1055 | + | |
1056 | +static int __init imq_init_one(int index) | |
1057 | +{ | |
1058 | + struct net_device *dev; | |
1059 | + int ret; | |
1060 | + | |
f6396b7e | 1061 | + dev = alloc_netdev_mq(0, "imq%d", imq_setup, numqueues); |
2380c486 JR |
1062 | + if (!dev) |
1063 | + return -ENOMEM; | |
1064 | + | |
1065 | + ret = dev_alloc_name(dev, dev->name); | |
1066 | + if (ret < 0) | |
1067 | + goto fail; | |
1068 | + | |
1069 | + dev->rtnl_link_ops = &imq_link_ops; | |
1070 | + ret = register_netdevice(dev); | |
1071 | + if (ret < 0) | |
1072 | + goto fail; | |
1073 | + | |
1074 | + return 0; | |
1075 | +fail: | |
1076 | + free_netdev(dev); | |
1077 | + return ret; | |
1078 | +} | |
1079 | + | |
1080 | +static int __init imq_init_devs(void) | |
1081 | +{ | |
1082 | + int err, i; | |
1083 | + | |
7f07242b | 1084 | + if (numdevs < 1 || numdevs > IMQ_MAX_DEVS) { |
514e5dae | 1085 | + pr_err("IMQ: numdevs has to be betweed 1 and %u\n", |
2380c486 JR |
1086 | + IMQ_MAX_DEVS); |
1087 | + return -EINVAL; | |
1088 | + } | |
1089 | + | |
f6396b7e | 1090 | + if (numqueues < 1 || numqueues > IMQ_MAX_QUEUES) { |
514e5dae | 1091 | + pr_err("IMQ: numqueues has to be betweed 1 and %u\n", |
f6396b7e AM |
1092 | + IMQ_MAX_QUEUES); |
1093 | + return -EINVAL; | |
1094 | + } | |
1095 | + | |
1096 | + get_random_bytes(&imq_hashrnd, sizeof(imq_hashrnd)); | |
1097 | + | |
2380c486 JR |
1098 | + rtnl_lock(); |
1099 | + err = __rtnl_link_register(&imq_link_ops); | |
1100 | + | |
1101 | + for (i = 0; i < numdevs && !err; i++) | |
1102 | + err = imq_init_one(i); | |
1103 | + | |
1104 | + if (err) { | |
1105 | + __rtnl_link_unregister(&imq_link_ops); | |
1106 | + memset(imq_devs_cache, 0, sizeof(imq_devs_cache)); | |
1107 | + } | |
1108 | + rtnl_unlock(); | |
1109 | + | |
1110 | + return err; | |
1111 | +} | |
1112 | + | |
1113 | +static int __init imq_init_module(void) | |
1114 | +{ | |
1115 | + int err; | |
1116 | + | |
7f07242b | 1117 | +#if defined(CONFIG_IMQ_NUM_DEVS) |
1118 | + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS > 16); | |
1119 | + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS < 2); | |
1120 | + BUILD_BUG_ON(CONFIG_IMQ_NUM_DEVS - 1 > IMQ_F_IFMASK); | |
1121 | +#endif | |
1122 | + | |
2380c486 JR |
1123 | + err = imq_init_devs(); |
1124 | + if (err) { | |
514e5dae | 1125 | + pr_err("IMQ: Error trying imq_init_devs(net)\n"); |
2380c486 JR |
1126 | + return err; |
1127 | + } | |
1128 | + | |
1129 | + err = imq_init_hooks(); | |
1130 | + if (err) { | |
514e5dae | 1131 | + pr_err(KERN_ERR "IMQ: Error trying imq_init_hooks()\n"); |
2380c486 JR |
1132 | + rtnl_link_unregister(&imq_link_ops); |
1133 | + memset(imq_devs_cache, 0, sizeof(imq_devs_cache)); | |
1134 | + return err; | |
1135 | + } | |
1136 | + | |
514e5dae AM |
1137 | + pr_info("IMQ driver loaded successfully. (numdevs = %d, numqueues = %d)\n", |
1138 | + numdevs, numqueues); | |
2380c486 JR |
1139 | + |
1140 | +#if defined(CONFIG_IMQ_BEHAVIOR_BA) || defined(CONFIG_IMQ_BEHAVIOR_BB) | |
514e5dae | 1141 | + pr_info("\tHooking IMQ before NAT on PREROUTING.\n"); |
2380c486 | 1142 | +#else |
514e5dae | 1143 | + pr_info("\tHooking IMQ after NAT on PREROUTING.\n"); |
2380c486 JR |
1144 | +#endif |
1145 | +#if defined(CONFIG_IMQ_BEHAVIOR_AB) || defined(CONFIG_IMQ_BEHAVIOR_BB) | |
514e5dae | 1146 | + pr_info("\tHooking IMQ before NAT on POSTROUTING.\n"); |
2380c486 | 1147 | +#else |
514e5dae | 1148 | + pr_info("\tHooking IMQ after NAT on POSTROUTING.\n"); |
2380c486 JR |
1149 | +#endif |
1150 | + | |
1151 | + return 0; | |
1152 | +} | |
1153 | + | |
1154 | +static void __exit imq_unhook(void) | |
1155 | +{ | |
f6396b7e | 1156 | + nf_unregister_hooks(imq_ops, ARRAY_SIZE(imq_ops)); |
7f07242b | 1157 | + nf_unregister_queue_imq_handler(); |
2380c486 JR |
1158 | +} |
1159 | + | |
1160 | +static void __exit imq_cleanup_devs(void) | |
1161 | +{ | |
1162 | + rtnl_link_unregister(&imq_link_ops); | |
1163 | + memset(imq_devs_cache, 0, sizeof(imq_devs_cache)); | |
1164 | +} | |
1165 | + | |
1166 | +static void __exit imq_exit_module(void) | |
1167 | +{ | |
1168 | + imq_unhook(); | |
1169 | + imq_cleanup_devs(); | |
514e5dae | 1170 | + pr_info("IMQ driver unloaded successfully.\n"); |
2380c486 JR |
1171 | +} |
1172 | + | |
1173 | +module_init(imq_init_module); | |
1174 | +module_exit(imq_exit_module); | |
1175 | + | |
1176 | +module_param(numdevs, int, 0); | |
f6396b7e | 1177 | +module_param(numqueues, int, 0); |
514e5dae | 1178 | +MODULE_PARM_DESC(numdevs, "number of IMQ devices (how many imq* devices will be created)"); |
f6396b7e | 1179 | +MODULE_PARM_DESC(numqueues, "number of queues per IMQ device"); |
2380c486 | 1180 | +MODULE_AUTHOR("http://www.linuximq.net"); |
514e5dae | 1181 | +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information."); |
2380c486 JR |
1182 | +MODULE_LICENSE("GPL"); |
1183 | +MODULE_ALIAS_RTNL_LINK("imq"); | |
1184 | + | |
e2d28598 JR |
1185 | diff --git a/include/linux/imq.h b/include/linux/imq.h |
1186 | new file mode 100644 | |
1187 | index 0000000..1babb09 | |
1188 | --- /dev/null | |
1189 | +++ b/include/linux/imq.h | |
7f07242b | 1190 | @@ -0,0 +1,13 @@ |
2380c486 JR |
1191 | +#ifndef _IMQ_H |
1192 | +#define _IMQ_H | |
1193 | + | |
7f07242b | 1194 | +/* IFMASK (16 device indexes, 0 to 15) and flag(s) fit in 5 bits */ |
1195 | +#define IMQ_F_BITS 5 | |
1196 | + | |
1197 | +#define IMQ_F_IFMASK 0x0f | |
1198 | +#define IMQ_F_ENQUEUE 0x10 | |
2380c486 | 1199 | + |
7f07242b | 1200 | +#define IMQ_MAX_DEVS (IMQ_F_IFMASK + 1) |
2380c486 JR |
1201 | + |
1202 | +#endif /* _IMQ_H */ | |
2380c486 | 1203 | + |
e2d28598 JR |
1204 | diff --git a/include/linux/netfilter/xt_IMQ.h b/include/linux/netfilter/xt_IMQ.h |
1205 | new file mode 100644 | |
1206 | index 0000000..9b07230 | |
1207 | --- /dev/null | |
1208 | +++ b/include/linux/netfilter/xt_IMQ.h | |
7f07242b | 1209 | @@ -0,0 +1,9 @@ |
1210 | +#ifndef _XT_IMQ_H | |
1211 | +#define _XT_IMQ_H | |
1212 | + | |
1213 | +struct xt_imq_info { | |
2380c486 JR |
1214 | + unsigned int todev; /* target imq device */ |
1215 | +}; | |
1216 | + | |
7f07242b | 1217 | +#endif /* _XT_IMQ_H */ |
1218 | + | |
e2d28598 JR |
1219 | diff --git a/include/linux/netfilter_ipv4/ipt_IMQ.h b/include/linux/netfilter_ipv4/ipt_IMQ.h |
1220 | new file mode 100644 | |
1221 | index 0000000..7af320f | |
1222 | --- /dev/null | |
1223 | +++ b/include/linux/netfilter_ipv4/ipt_IMQ.h | |
7f07242b | 1224 | @@ -0,0 +1,10 @@ |
1225 | +#ifndef _IPT_IMQ_H | |
1226 | +#define _IPT_IMQ_H | |
1227 | + | |
1228 | +/* Backwards compatibility for old userspace */ | |
1229 | +#include <linux/netfilter/xt_IMQ.h> | |
1230 | + | |
1231 | +#define ipt_imq_info xt_imq_info | |
1232 | + | |
2380c486 | 1233 | +#endif /* _IPT_IMQ_H */ |
7f07242b | 1234 | + |
e2d28598 JR |
1235 | diff --git a/include/linux/netfilter_ipv6/ip6t_IMQ.h b/include/linux/netfilter_ipv6/ip6t_IMQ.h |
1236 | new file mode 100644 | |
1237 | index 0000000..198ac01 | |
1238 | --- /dev/null | |
1239 | +++ b/include/linux/netfilter_ipv6/ip6t_IMQ.h | |
7f07242b | 1240 | @@ -0,0 +1,10 @@ |
2380c486 JR |
1241 | +#ifndef _IP6T_IMQ_H |
1242 | +#define _IP6T_IMQ_H | |
1243 | + | |
7f07242b | 1244 | +/* Backwards compatibility for old userspace */ |
1245 | +#include <linux/netfilter/xt_IMQ.h> | |
1246 | + | |
1247 | +#define ip6t_imq_info xt_imq_info | |
2380c486 JR |
1248 | + |
1249 | +#endif /* _IP6T_IMQ_H */ | |
7f07242b | 1250 | + |
e2d28598 JR |
1251 | diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h |
1252 | index dec1748..1f17280 100644 | |
1253 | --- a/include/linux/skbuff.h | |
1254 | +++ b/include/linux/skbuff.h | |
1255 | @@ -33,6 +33,9 @@ | |
3b94b3c4 | 1256 | #include <linux/dma-mapping.h> |
0acdeb19 | 1257 | #include <linux/netdev_features.h> |
7770d33f | 1258 | #include <net/flow_keys.h> |
7f07242b | 1259 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
1260 | +#include <linux/imq.h> | |
1261 | +#endif | |
1262 | ||
14f08cd0 | 1263 | /* Don't change this without changing skb_csum_unnecessary! */ |
1264 | #define CHECKSUM_NONE 0 | |
e2d28598 | 1265 | @@ -420,6 +423,9 @@ struct sk_buff { |
7f07242b | 1266 | * first. This is owned by whoever has the skb queued ATM. |
1267 | */ | |
ca0faea1 | 1268 | char cb[48] __aligned(8); |
7f07242b | 1269 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
1270 | + void *cb_next; | |
1271 | +#endif | |
1272 | ||
f6396b7e | 1273 | unsigned long _skb_refdst; |
ca0faea1 | 1274 | #ifdef CONFIG_XFRM |
e2d28598 | 1275 | @@ -458,6 +464,9 @@ struct sk_buff { |
7af23471 | 1276 | #ifdef NET_SKBUFF_NF_DEFRAG_NEEDED |
2380c486 JR |
1277 | struct sk_buff *nfct_reasm; |
1278 | #endif | |
1279 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
2380c486 JR |
1280 | + struct nf_queue_entry *nf_queue_entry; |
1281 | +#endif | |
1282 | #ifdef CONFIG_BRIDGE_NETFILTER | |
1283 | struct nf_bridge_info *nf_bridge; | |
1284 | #endif | |
5f467116 AM |
1285 | @@ -510,6 +510,10 @@ struct sk_buff { |
1286 | /* 6/8 bit hole (depending on ndisc_nodetype presence) */ | |
4bf69007 | 1287 | kmemcheck_bitfield_end(flags2); |
14f08cd0 | 1288 | |
7f07242b | 1289 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
1290 | + __u8 imq_flags:IMQ_F_BITS; | |
1291 | +#endif | |
14f08cd0 | 1292 | + |
5f467116 AM |
1293 | #if defined CONFIG_NET_DMA || defined CONFIG_NET_RX_BUSY_POLL |
1294 | union { | |
1295 | unsigned int napi_id; | |
e2d28598 | 1296 | @@ -626,6 +639,12 @@ static inline struct rtable *skb_rtable(const struct sk_buff *skb) |
14f08cd0 | 1297 | return (struct rtable *)skb_dst(skb); |
1298 | } | |
7f07242b | 1299 | |
1300 | + | |
1301 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1302 | +extern int skb_save_cb(struct sk_buff *skb); | |
1303 | +extern int skb_restore_cb(struct sk_buff *skb); | |
1304 | +#endif | |
1305 | + | |
1306 | extern void kfree_skb(struct sk_buff *skb); | |
e2d28598 | 1307 | extern void kfree_skb_list(struct sk_buff *segs); |
514e5dae | 1308 | extern void skb_tx_error(struct sk_buff *skb); |
e2d28598 | 1309 | @@ -2754,6 +2773,10 @@ static inline void __nf_copy(struct sk_buff *dst, const struct sk_buff *src) |
2380c486 JR |
1310 | dst->nfct_reasm = src->nfct_reasm; |
1311 | nf_conntrack_get_reasm(src->nfct_reasm); | |
1312 | #endif | |
1313 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1314 | + dst->imq_flags = src->imq_flags; | |
1315 | + dst->nf_queue_entry = src->nf_queue_entry; | |
1316 | +#endif | |
1317 | #ifdef CONFIG_BRIDGE_NETFILTER | |
1318 | dst->nf_bridge = src->nf_bridge; | |
1319 | nf_bridge_get(src->nf_bridge); | |
e2d28598 JR |
1320 | diff --git a/include/net/netfilter/nf_queue.h b/include/net/netfilter/nf_queue.h |
1321 | index aaba4bb..f6e92a4 100644 | |
1322 | --- a/include/net/netfilter/nf_queue.h | |
1323 | +++ b/include/net/netfilter/nf_queue.h | |
1324 | @@ -29,6 +29,12 @@ struct nf_queue_handler { | |
514e5dae AM |
1325 | void nf_register_queue_handler(const struct nf_queue_handler *qh); |
1326 | void nf_unregister_queue_handler(void); | |
7f07242b | 1327 | extern void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict); |
1328 | +extern void nf_queue_entry_release_refs(struct nf_queue_entry *entry); | |
1329 | + | |
1330 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1331 | +extern void nf_register_queue_imq_handler(const struct nf_queue_handler *qh); | |
1332 | +extern void nf_unregister_queue_imq_handler(void); | |
1333 | +#endif | |
1334 | ||
e2d28598 JR |
1335 | bool nf_queue_entry_get_refs(struct nf_queue_entry *entry); |
1336 | void nf_queue_entry_release_refs(struct nf_queue_entry *entry); | |
1337 | diff --git a/include/uapi/linux/netfilter.h b/include/uapi/linux/netfilter.h | |
1338 | index f7dc0eb..58c46a9 100644 | |
1339 | --- a/include/uapi/linux/netfilter.h | |
1340 | +++ b/include/uapi/linux/netfilter.h | |
514e5dae AM |
1341 | @@ -13,7 +13,8 @@ |
1342 | #define NF_QUEUE 3 | |
1343 | #define NF_REPEAT 4 | |
1344 | #define NF_STOP 5 | |
1345 | -#define NF_MAX_VERDICT NF_STOP | |
1346 | +#define NF_IMQ_QUEUE 6 | |
1347 | +#define NF_MAX_VERDICT NF_IMQ_QUEUE | |
1348 | ||
1349 | /* we overload the higher bits for encoding auxiliary data such as the queue | |
1350 | * number or errno values. Not nice, but better than additional function | |
e2d28598 JR |
1351 | diff --git a/net/core/dev.c b/net/core/dev.c |
1352 | index 7ddbb31..125b7b9 100644 | |
1353 | --- a/net/core/dev.c | |
1354 | +++ b/net/core/dev.c | |
514e5dae | 1355 | @@ -129,6 +129,9 @@ |
514e5dae | 1356 | #include <linux/static_key.h> |
5f467116 AM |
1357 | #include <linux/hashtable.h> |
1358 | #include <linux/vmalloc.h> | |
2380c486 JR |
1359 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
1360 | +#include <linux/imq.h> | |
1361 | +#endif | |
514e5dae AM |
1362 | |
1363 | #include "net-sysfs.h" | |
1364 | ||
e2d28598 | 1365 | @@ -2573,7 +2576,12 @@ int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, |
514e5dae AM |
1366 | } |
1367 | } | |
7f07242b | 1368 | |
2380c486 | 1369 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
7af23471 JR |
1370 | + if (!list_empty(&ptype_all) && |
1371 | + !(skb->imq_flags & IMQ_F_ENQUEUE)) | |
1372 | +#else | |
1373 | if (!list_empty(&ptype_all)) | |
2380c486 | 1374 | +#endif |
2380c486 JR |
1375 | dev_queue_xmit_nit(skb, dev); |
1376 | ||
514e5dae | 1377 | skb_len = skb->len; |
e2d28598 JR |
1378 | diff --git a/net/core/skbuff.c b/net/core/skbuff.c |
1379 | index 1c1738c..5d1b75b 100644 | |
1380 | --- a/net/core/skbuff.c | |
1381 | +++ b/net/core/skbuff.c | |
a168f21d | 1382 | @@ -73,6 +73,9 @@ |
7f07242b | 1383 | |
4bf69007 | 1384 | struct kmem_cache *skbuff_head_cache __read_mostly; |
7f07242b | 1385 | static struct kmem_cache *skbuff_fclone_cache __read_mostly; |
1386 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1387 | +static struct kmem_cache *skbuff_cb_store_cache __read_mostly; | |
1388 | +#endif | |
1389 | ||
1390 | static void sock_pipe_buf_release(struct pipe_inode_info *pipe, | |
1391 | struct pipe_buffer *buf) | |
e2d28598 | 1392 | @@ -92,6 +95,82 @@ static int sock_pipe_buf_steal(struct pipe_inode_info *pipe, |
7f07242b | 1393 | return 1; |
1394 | } | |
1395 | ||
1396 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1397 | +/* Control buffer save/restore for IMQ devices */ | |
1398 | +struct skb_cb_table { | |
f6396b7e | 1399 | + char cb[48] __aligned(8); |
7f07242b | 1400 | + void *cb_next; |
1401 | + atomic_t refcnt; | |
7f07242b | 1402 | +}; |
2380c486 | 1403 | + |
7f07242b | 1404 | +static DEFINE_SPINLOCK(skb_cb_store_lock); |
1405 | + | |
1406 | +int skb_save_cb(struct sk_buff *skb) | |
2380c486 | 1407 | +{ |
7f07242b | 1408 | + struct skb_cb_table *next; |
1409 | + | |
1410 | + next = kmem_cache_alloc(skbuff_cb_store_cache, GFP_ATOMIC); | |
1411 | + if (!next) | |
1412 | + return -ENOMEM; | |
2380c486 | 1413 | + |
7f07242b | 1414 | + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb)); |
2380c486 | 1415 | + |
7f07242b | 1416 | + memcpy(next->cb, skb->cb, sizeof(skb->cb)); |
1417 | + next->cb_next = skb->cb_next; | |
1418 | + | |
1419 | + atomic_set(&next->refcnt, 1); | |
1420 | + | |
1421 | + skb->cb_next = next; | |
1422 | + return 0; | |
2380c486 | 1423 | +} |
7f07242b | 1424 | +EXPORT_SYMBOL(skb_save_cb); |
2380c486 | 1425 | + |
7f07242b | 1426 | +int skb_restore_cb(struct sk_buff *skb) |
2380c486 | 1427 | +{ |
7f07242b | 1428 | + struct skb_cb_table *next; |
2380c486 | 1429 | + |
7f07242b | 1430 | + if (!skb->cb_next) |
2380c486 | 1431 | + return 0; |
7f07242b | 1432 | + |
1433 | + next = skb->cb_next; | |
1434 | + | |
1435 | + BUILD_BUG_ON(sizeof(skb->cb) != sizeof(next->cb)); | |
1436 | + | |
1437 | + memcpy(skb->cb, next->cb, sizeof(skb->cb)); | |
1438 | + skb->cb_next = next->cb_next; | |
1439 | + | |
1440 | + spin_lock(&skb_cb_store_lock); | |
1441 | + | |
f6396b7e | 1442 | + if (atomic_dec_and_test(&next->refcnt)) |
7f07242b | 1443 | + kmem_cache_free(skbuff_cb_store_cache, next); |
2380c486 | 1444 | + |
7f07242b | 1445 | + spin_unlock(&skb_cb_store_lock); |
1446 | + | |
1447 | + return 0; | |
2380c486 | 1448 | +} |
7f07242b | 1449 | +EXPORT_SYMBOL(skb_restore_cb); |
2380c486 | 1450 | + |
14f08cd0 | 1451 | +static void skb_copy_stored_cb(struct sk_buff *new, const struct sk_buff *__old) |
7f07242b | 1452 | +{ |
1453 | + struct skb_cb_table *next; | |
14f08cd0 | 1454 | + struct sk_buff *old; |
7f07242b | 1455 | + |
14f08cd0 | 1456 | + if (!__old->cb_next) { |
1457 | + new->cb_next = NULL; | |
7f07242b | 1458 | + return; |
1459 | + } | |
1460 | + | |
1461 | + spin_lock(&skb_cb_store_lock); | |
1462 | + | |
14f08cd0 | 1463 | + old = (struct sk_buff *)__old; |
1464 | + | |
7f07242b | 1465 | + next = old->cb_next; |
1466 | + atomic_inc(&next->refcnt); | |
1467 | + new->cb_next = next; | |
1468 | + | |
1469 | + spin_unlock(&skb_cb_store_lock); | |
1470 | +} | |
1471 | +#endif | |
1472 | ||
1473 | /* Pipe buffer operations for a socket. */ | |
5379d87d | 1474 | static const struct pipe_buf_operations sock_pipe_buf_ops = { |
e2d28598 | 1475 | @@ -582,6 +661,28 @@ static void skb_release_head_state(struct sk_buff *skb) |
7f07242b | 1476 | WARN_ON(in_irq()); |
1477 | skb->destructor(skb); | |
1478 | } | |
1479 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
4bf69007 AM |
1480 | + /* |
1481 | + * This should not happen. When it does, avoid memleak by restoring | |
1482 | + * the chain of cb-backups. | |
1483 | + */ | |
f6396b7e | 1484 | + while (skb->cb_next != NULL) { |
14f08cd0 | 1485 | + if (net_ratelimit()) |
514e5dae AM |
1486 | + pr_warn("IMQ: kfree_skb: skb->cb_next: %08x\n", |
1487 | + (unsigned int)skb->cb_next); | |
14f08cd0 | 1488 | + |
7f07242b | 1489 | + skb_restore_cb(skb); |
1490 | + } | |
4bf69007 AM |
1491 | + /* |
1492 | + * This should not happen either, nf_queue_entry is nullified in | |
14f08cd0 | 1493 | + * imq_dev_xmit(). If we have non-NULL nf_queue_entry then we are |
1494 | + * leaking entry pointers, maybe memory. We don't know if this is | |
1495 | + * pointer to already freed memory, or should this be freed. | |
1496 | + * If this happens we need to add refcounting, etc for nf_queue_entry. | |
1497 | + */ | |
1498 | + if (skb->nf_queue_entry && net_ratelimit()) | |
514e5dae | 1499 | + pr_warn("%s\n", "IMQ: kfree_skb: skb->nf_queue_entry != NULL"); |
7f07242b | 1500 | +#endif |
0acdeb19 | 1501 | #if IS_ENABLED(CONFIG_NF_CONNTRACK) |
7f07242b | 1502 | nf_conntrack_put(skb->nfct); |
7af23471 | 1503 | #endif |
e2d28598 | 1504 | @@ -716,6 +817,10 @@ static void __copy_skb_header(struct sk_buff *new, const struct sk_buff *old) |
7f07242b | 1505 | new->sp = secpath_get(old->sp); |
1506 | #endif | |
1507 | memcpy(new->cb, old->cb, sizeof(old->cb)); | |
1508 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
e2d28598 JR |
1509 | + new->cb_next = NULL; |
1510 | + /*skb_copy_stored_cb(new, old);*/ | |
7f07242b | 1511 | +#endif |
13e5c3b1 | 1512 | new->csum = old->csum; |
7f07242b | 1513 | new->local_df = old->local_df; |
13e5c3b1 | 1514 | new->pkt_type = old->pkt_type; |
e2d28598 | 1515 | @@ -3095,6 +3200,13 @@ void __init skb_init(void) |
7f07242b | 1516 | 0, |
1517 | SLAB_HWCACHE_ALIGN|SLAB_PANIC, | |
1518 | NULL); | |
1519 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
1520 | + skbuff_cb_store_cache = kmem_cache_create("skbuff_cb_store_cache", | |
1521 | + sizeof(struct skb_cb_table), | |
1522 | + 0, | |
1523 | + SLAB_HWCACHE_ALIGN|SLAB_PANIC, | |
1524 | + NULL); | |
1525 | +#endif | |
1526 | } | |
1527 | ||
1528 | /** | |
e2d28598 JR |
1529 | diff --git a/net/ipv6/ip6_output.c b/net/ipv6/ip6_output.c |
1530 | index 6e3ddf8..ddae387 100644 | |
1531 | --- a/net/ipv6/ip6_output.c | |
1532 | +++ b/net/ipv6/ip6_output.c | |
1533 | @@ -89,9 +89,6 @@ static int ip6_finish_output2(struct sk_buff *skb) | |
514e5dae AM |
1534 | struct in6_addr *nexthop; |
1535 | int ret; | |
a168f21d AM |
1536 | |
1537 | - skb->protocol = htons(ETH_P_IPV6); | |
1538 | - skb->dev = dev; | |
1539 | - | |
1540 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) { | |
1541 | struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb)); | |
1542 | ||
e2d28598 | 1543 | @@ -167,6 +164,13 @@ int ip6_output(struct sk_buff *skb) |
a168f21d | 1544 | return 0; |
f6396b7e | 1545 | } |
a168f21d | 1546 | |
514e5dae AM |
1547 | + /* |
1548 | + * IMQ-patch: moved setting skb->dev and skb->protocol from | |
1549 | + * ip6_finish_output2 to fix crashing at netif_skb_features(). | |
1550 | + */ | |
a168f21d AM |
1551 | + skb->protocol = htons(ETH_P_IPV6); |
1552 | + skb->dev = dev; | |
1553 | + | |
1554 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev, | |
1555 | ip6_finish_output, | |
1556 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); | |
e2d28598 JR |
1557 | diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig |
1558 | index 56d22ca..58f40f8 100644 | |
1559 | --- a/net/netfilter/Kconfig | |
1560 | +++ b/net/netfilter/Kconfig | |
1561 | @@ -641,6 +641,18 @@ config NETFILTER_XT_TARGET_LOG | |
4bf69007 AM |
1562 | |
1563 | To compile it as a module, choose M here. If unsure, say N. | |
7f07242b | 1564 | |
1565 | +config NETFILTER_XT_TARGET_IMQ | |
1566 | + tristate '"IMQ" target support' | |
1567 | + depends on NETFILTER_XTABLES | |
1568 | + depends on IP_NF_MANGLE || IP6_NF_MANGLE | |
1569 | + select IMQ | |
1570 | + default m if NETFILTER_ADVANCED=n | |
1571 | + help | |
1572 | + This option adds a `IMQ' target which is used to specify if and | |
1573 | + to which imq device packets should get enqueued/dequeued. | |
2380c486 | 1574 | + |
7f07242b | 1575 | + To compile it as a module, choose M here. If unsure, say N. |
1576 | + | |
1577 | config NETFILTER_XT_TARGET_MARK | |
1578 | tristate '"MARK" target support' | |
f6396b7e | 1579 | depends on NETFILTER_ADVANCED |
e2d28598 JR |
1580 | diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile |
1581 | index a1abf87..2c0cbf4 100644 | |
1582 | --- a/net/netfilter/Makefile | |
1583 | +++ b/net/netfilter/Makefile | |
1584 | @@ -82,6 +82,7 @@ obj-$(CONFIG_NETFILTER_XT_TARGET_CT) += xt_CT.o | |
7f07242b | 1585 | obj-$(CONFIG_NETFILTER_XT_TARGET_DSCP) += xt_DSCP.o |
1586 | obj-$(CONFIG_NETFILTER_XT_TARGET_HL) += xt_HL.o | |
42d62c0a | 1587 | obj-$(CONFIG_NETFILTER_XT_TARGET_HMARK) += xt_HMARK.o |
4bf69007 | 1588 | +obj-$(CONFIG_NETFILTER_XT_TARGET_IMQ) += xt_IMQ.o |
14f08cd0 | 1589 | obj-$(CONFIG_NETFILTER_XT_TARGET_LED) += xt_LED.o |
42d62c0a | 1590 | obj-$(CONFIG_NETFILTER_XT_TARGET_LOG) += xt_LOG.o |
514e5dae | 1591 | obj-$(CONFIG_NETFILTER_XT_TARGET_NETMAP) += xt_NETMAP.o |
e2d28598 JR |
1592 | diff --git a/net/netfilter/core.c b/net/netfilter/core.c |
1593 | index 857ca9f..93fe11b 100644 | |
1594 | --- a/net/netfilter/core.c | |
1595 | +++ b/net/netfilter/core.c | |
1596 | @@ -191,9 +191,11 @@ next_hook: | |
1597 | ret = NF_DROP_GETERR(verdict); | |
1598 | if (ret == 0) | |
1599 | ret = -EPERM; | |
1600 | - } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE) { | |
1601 | + } else if ((verdict & NF_VERDICT_MASK) == NF_QUEUE || | |
1602 | + (verdict & NF_VERDICT_MASK) == NF_IMQ_QUEUE) { | |
1603 | int err = nf_queue(skb, elem, pf, hook, indev, outdev, okfn, | |
1604 | - verdict >> NF_VERDICT_QBITS); | |
1605 | + verdict >> NF_VERDICT_QBITS, | |
1606 | + verdict & NF_VERDICT_MASK); | |
1607 | if (err < 0) { | |
1608 | if (err == -ECANCELED) | |
1609 | goto next_hook; | |
1610 | diff --git a/net/netfilter/nf_internals.h b/net/netfilter/nf_internals.h | |
1611 | index 3deec99..c1a1397 100644 | |
1612 | --- a/net/netfilter/nf_internals.h | |
1613 | +++ b/net/netfilter/nf_internals.h | |
1614 | @@ -29,7 +29,7 @@ extern int nf_queue(struct sk_buff *skb, | |
a168f21d | 1615 | struct net_device *indev, |
f6396b7e AM |
1616 | struct net_device *outdev, |
1617 | int (*okfn)(struct sk_buff *), | |
a168f21d AM |
1618 | - unsigned int queuenum); |
1619 | + unsigned int queuenum, unsigned int queuetype); | |
f6396b7e AM |
1620 | extern int __init netfilter_queue_init(void); |
1621 | ||
1622 | /* nf_log.c */ | |
e2d28598 JR |
1623 | diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c |
1624 | index 5d24b1f..28317dc 100644 | |
1625 | --- a/net/netfilter/nf_queue.c | |
1626 | +++ b/net/netfilter/nf_queue.c | |
1627 | @@ -27,6 +27,23 @@ | |
514e5dae AM |
1628 | */ |
1629 | static const struct nf_queue_handler __rcu *queue_handler __read_mostly; | |
7f07242b | 1630 | |
1631 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) | |
514e5dae | 1632 | +static const struct nf_queue_handler __rcu *queue_imq_handler __read_mostly; |
7f07242b | 1633 | + |
1634 | +void nf_register_queue_imq_handler(const struct nf_queue_handler *qh) | |
2380c486 | 1635 | +{ |
7f07242b | 1636 | + rcu_assign_pointer(queue_imq_handler, qh); |
2380c486 | 1637 | +} |
f6396b7e | 1638 | +EXPORT_SYMBOL_GPL(nf_register_queue_imq_handler); |
2380c486 | 1639 | + |
7f07242b | 1640 | +void nf_unregister_queue_imq_handler(void) |
2380c486 | 1641 | +{ |
514e5dae AM |
1642 | + RCU_INIT_POINTER(queue_imq_handler, NULL); |
1643 | + synchronize_rcu(); | |
2380c486 | 1644 | +} |
f6396b7e | 1645 | +EXPORT_SYMBOL_GPL(nf_unregister_queue_imq_handler); |
7f07242b | 1646 | +#endif |
2380c486 | 1647 | + |
7f07242b | 1648 | /* return EBUSY when somebody else is registered, return EEXIST if the |
1649 | * same handler is registered, return 0 in case of success. */ | |
514e5dae | 1650 | void nf_register_queue_handler(const struct nf_queue_handler *qh) |
e2d28598 | 1651 | @@ -105,7 +122,8 @@ int nf_queue(struct sk_buff *skb, |
f6396b7e AM |
1652 | struct net_device *indev, |
1653 | struct net_device *outdev, | |
1654 | int (*okfn)(struct sk_buff *), | |
1655 | - unsigned int queuenum) | |
1656 | + unsigned int queuenum, | |
a168f21d | 1657 | + unsigned int queuetype) |
f6396b7e | 1658 | { |
7af23471 | 1659 | int status = -ENOENT; |
f6396b7e | 1660 | struct nf_queue_entry *entry = NULL; |
e2d28598 | 1661 | @@ -115,7 +133,17 @@ int nf_queue(struct sk_buff *skb, |
7af23471 | 1662 | /* QUEUE == DROP if no one is waiting, to be safe. */ |
7f07242b | 1663 | rcu_read_lock(); |
1664 | ||
1716fcea | 1665 | - qh = rcu_dereference(queue_handler); |
a168f21d | 1666 | + if (queuetype == NF_IMQ_QUEUE) { |
7f07242b | 1667 | +#if defined(CONFIG_IMQ) || defined(CONFIG_IMQ_MODULE) |
f6396b7e | 1668 | + qh = rcu_dereference(queue_imq_handler); |
7af23471 | 1669 | +#else |
a168f21d AM |
1670 | + BUG(); |
1671 | + goto err_unlock; | |
7f07242b | 1672 | +#endif |
a168f21d | 1673 | + } else { |
1716fcea | 1674 | + qh = rcu_dereference(queue_handler); |
a168f21d AM |
1675 | + } |
1676 | + | |
7af23471 JR |
1677 | if (!qh) { |
1678 | status = -ESRCH; | |
7f07242b | 1679 | goto err_unlock; |
e2d28598 | 1680 | @@ -205,9 +233,11 @@ void nf_reinject(struct nf_queue_entry *entry, unsigned int verdict) |
a168f21d AM |
1681 | local_bh_enable(); |
1682 | break; | |
f6396b7e | 1683 | case NF_QUEUE: |
a168f21d | 1684 | + case NF_IMQ_QUEUE: |
7770d33f JR |
1685 | err = nf_queue(skb, elem, entry->pf, entry->hook, |
1686 | entry->indev, entry->outdev, entry->okfn, | |
1687 | - verdict >> NF_VERDICT_QBITS); | |
1688 | + verdict >> NF_VERDICT_QBITS, | |
1689 | + verdict & NF_VERDICT_MASK); | |
7af23471 JR |
1690 | if (err < 0) { |
1691 | if (err == -ECANCELED) | |
1692 | goto next_hook; | |
e2d28598 JR |
1693 | diff --git a/net/netfilter/xt_IMQ.c b/net/netfilter/xt_IMQ.c |
1694 | new file mode 100644 | |
1695 | index 0000000..1c3cd66 | |
1696 | --- /dev/null | |
1697 | +++ b/net/netfilter/xt_IMQ.c | |
514e5dae | 1698 | @@ -0,0 +1,72 @@ |
2380c486 JR |
1699 | +/* |
1700 | + * This target marks packets to be enqueued to an imq device | |
1701 | + */ | |
1702 | +#include <linux/module.h> | |
1703 | +#include <linux/skbuff.h> | |
7f07242b | 1704 | +#include <linux/netfilter/x_tables.h> |
1705 | +#include <linux/netfilter/xt_IMQ.h> | |
2380c486 JR |
1706 | +#include <linux/imq.h> |
1707 | + | |
1708 | +static unsigned int imq_target(struct sk_buff *pskb, | |
f6396b7e | 1709 | + const struct xt_action_param *par) |
2380c486 | 1710 | +{ |
7f07242b | 1711 | + const struct xt_imq_info *mr = par->targinfo; |
2380c486 | 1712 | + |
7f07242b | 1713 | + pskb->imq_flags = (mr->todev & IMQ_F_IFMASK) | IMQ_F_ENQUEUE; |
2380c486 JR |
1714 | + |
1715 | + return XT_CONTINUE; | |
1716 | +} | |
1717 | + | |
f6396b7e | 1718 | +static int imq_checkentry(const struct xt_tgchk_param *par) |
2380c486 | 1719 | +{ |
7f07242b | 1720 | + struct xt_imq_info *mr = par->targinfo; |
2380c486 | 1721 | + |
7f07242b | 1722 | + if (mr->todev > IMQ_MAX_DEVS - 1) { |
514e5dae AM |
1723 | + pr_warn("IMQ: invalid device specified, highest is %u\n", |
1724 | + IMQ_MAX_DEVS - 1); | |
f6396b7e | 1725 | + return -EINVAL; |
2380c486 JR |
1726 | + } |
1727 | + | |
f6396b7e | 1728 | + return 0; |
2380c486 JR |
1729 | +} |
1730 | + | |
7f07242b | 1731 | +static struct xt_target xt_imq_reg[] __read_mostly = { |
1732 | + { | |
1733 | + .name = "IMQ", | |
1734 | + .family = AF_INET, | |
1735 | + .checkentry = imq_checkentry, | |
1736 | + .target = imq_target, | |
1737 | + .targetsize = sizeof(struct xt_imq_info), | |
1738 | + .table = "mangle", | |
1739 | + .me = THIS_MODULE | |
1740 | + }, | |
1741 | + { | |
1742 | + .name = "IMQ", | |
1743 | + .family = AF_INET6, | |
1744 | + .checkentry = imq_checkentry, | |
1745 | + .target = imq_target, | |
1746 | + .targetsize = sizeof(struct xt_imq_info), | |
1747 | + .table = "mangle", | |
1748 | + .me = THIS_MODULE | |
1749 | + }, | |
2380c486 JR |
1750 | +}; |
1751 | + | |
7f07242b | 1752 | +static int __init imq_init(void) |
2380c486 | 1753 | +{ |
7f07242b | 1754 | + return xt_register_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg)); |
2380c486 JR |
1755 | +} |
1756 | + | |
7f07242b | 1757 | +static void __exit imq_fini(void) |
2380c486 | 1758 | +{ |
7f07242b | 1759 | + xt_unregister_targets(xt_imq_reg, ARRAY_SIZE(xt_imq_reg)); |
2380c486 JR |
1760 | +} |
1761 | + | |
7f07242b | 1762 | +module_init(imq_init); |
1763 | +module_exit(imq_fini); | |
2380c486 JR |
1764 | + |
1765 | +MODULE_AUTHOR("http://www.linuximq.net"); | |
514e5dae | 1766 | +MODULE_DESCRIPTION("Pseudo-driver for the intermediate queue device. See http://www.linuximq.net/ for more information."); |
2380c486 | 1767 | +MODULE_LICENSE("GPL"); |
7f07242b | 1768 | +MODULE_ALIAS("ipt_IMQ"); |
1769 | +MODULE_ALIAS("ip6t_IMQ"); | |
2380c486 | 1770 | + |