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