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