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