~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/nfnetlink_queue_core.c

Version: ~ [ linux-5.2-rc5 ] ~ [ linux-5.1.12 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.53 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.128 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.182 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.182 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.68 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * This is a module which is used for queueing packets and communicating with
  3  * userspace via nfnetlink.
  4  *
  5  * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6  * (C) 2007 by Patrick McHardy <kaber@trash.net>
  7  *
  8  * Based on the old ipv4-only ip_queue.c:
  9  * (C) 2000-2002 James Morris <jmorris@intercode.com.au>
 10  * (C) 2003-2005 Netfilter Core Team <coreteam@netfilter.org>
 11  *
 12  * This program is free software; you can redistribute it and/or modify
 13  * it under the terms of the GNU General Public License version 2 as
 14  * published by the Free Software Foundation.
 15  *
 16  */
 17 #include <linux/module.h>
 18 #include <linux/skbuff.h>
 19 #include <linux/init.h>
 20 #include <linux/spinlock.h>
 21 #include <linux/slab.h>
 22 #include <linux/notifier.h>
 23 #include <linux/netdevice.h>
 24 #include <linux/netfilter.h>
 25 #include <linux/proc_fs.h>
 26 #include <linux/netfilter_ipv4.h>
 27 #include <linux/netfilter_ipv6.h>
 28 #include <linux/netfilter/nfnetlink.h>
 29 #include <linux/netfilter/nfnetlink_queue.h>
 30 #include <linux/list.h>
 31 #include <net/sock.h>
 32 #include <net/netfilter/nf_queue.h>
 33 #include <net/netns/generic.h>
 34 #include <net/netfilter/nfnetlink_queue.h>
 35 
 36 #include <linux/atomic.h>
 37 
 38 #ifdef CONFIG_BRIDGE_NETFILTER
 39 #include "../bridge/br_private.h"
 40 #endif
 41 
 42 #define NFQNL_QMAX_DEFAULT 1024
 43 
 44 /* We're using struct nlattr which has 16bit nla_len. Note that nla_len
 45  * includes the header length. Thus, the maximum packet length that we
 46  * support is 65531 bytes. We send truncated packets if the specified length
 47  * is larger than that.  Userspace can check for presence of NFQA_CAP_LEN
 48  * attribute to detect truncation.
 49  */
 50 #define NFQNL_MAX_COPY_RANGE (0xffff - NLA_HDRLEN)
 51 
 52 struct nfqnl_instance {
 53         struct hlist_node hlist;                /* global list of queues */
 54         struct rcu_head rcu;
 55 
 56         int peer_portid;
 57         unsigned int queue_maxlen;
 58         unsigned int copy_range;
 59         unsigned int queue_dropped;
 60         unsigned int queue_user_dropped;
 61 
 62 
 63         u_int16_t queue_num;                    /* number of this queue */
 64         u_int8_t copy_mode;
 65         u_int32_t flags;                        /* Set using NFQA_CFG_FLAGS */
 66 /*
 67  * Following fields are dirtied for each queued packet,
 68  * keep them in same cache line if possible.
 69  */
 70         spinlock_t      lock;
 71         unsigned int    queue_total;
 72         unsigned int    id_sequence;            /* 'sequence' of pkt ids */
 73         struct list_head queue_list;            /* packets in queue */
 74 };
 75 
 76 typedef int (*nfqnl_cmpfn)(struct nf_queue_entry *, unsigned long);
 77 
 78 static int nfnl_queue_net_id __read_mostly;
 79 
 80 #define INSTANCE_BUCKETS        16
 81 struct nfnl_queue_net {
 82         spinlock_t instances_lock;
 83         struct hlist_head instance_table[INSTANCE_BUCKETS];
 84 };
 85 
 86 static struct nfnl_queue_net *nfnl_queue_pernet(struct net *net)
 87 {
 88         return net_generic(net, nfnl_queue_net_id);
 89 }
 90 
 91 static inline u_int8_t instance_hashfn(u_int16_t queue_num)
 92 {
 93         return ((queue_num >> 8) ^ queue_num) % INSTANCE_BUCKETS;
 94 }
 95 
 96 static struct nfqnl_instance *
 97 instance_lookup(struct nfnl_queue_net *q, u_int16_t queue_num)
 98 {
 99         struct hlist_head *head;
100         struct nfqnl_instance *inst;
101 
102         head = &q->instance_table[instance_hashfn(queue_num)];
103         hlist_for_each_entry_rcu(inst, head, hlist) {
104                 if (inst->queue_num == queue_num)
105                         return inst;
106         }
107         return NULL;
108 }
109 
110 static struct nfqnl_instance *
111 instance_create(struct nfnl_queue_net *q, u_int16_t queue_num,
112                 int portid)
113 {
114         struct nfqnl_instance *inst;
115         unsigned int h;
116         int err;
117 
118         spin_lock(&q->instances_lock);
119         if (instance_lookup(q, queue_num)) {
120                 err = -EEXIST;
121                 goto out_unlock;
122         }
123 
124         inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
125         if (!inst) {
126                 err = -ENOMEM;
127                 goto out_unlock;
128         }
129 
130         inst->queue_num = queue_num;
131         inst->peer_portid = portid;
132         inst->queue_maxlen = NFQNL_QMAX_DEFAULT;
133         inst->copy_range = NFQNL_MAX_COPY_RANGE;
134         inst->copy_mode = NFQNL_COPY_NONE;
135         spin_lock_init(&inst->lock);
136         INIT_LIST_HEAD(&inst->queue_list);
137 
138         if (!try_module_get(THIS_MODULE)) {
139                 err = -EAGAIN;
140                 goto out_free;
141         }
142 
143         h = instance_hashfn(queue_num);
144         hlist_add_head_rcu(&inst->hlist, &q->instance_table[h]);
145 
146         spin_unlock(&q->instances_lock);
147 
148         return inst;
149 
150 out_free:
151         kfree(inst);
152 out_unlock:
153         spin_unlock(&q->instances_lock);
154         return ERR_PTR(err);
155 }
156 
157 static void nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn,
158                         unsigned long data);
159 
160 static void
161 instance_destroy_rcu(struct rcu_head *head)
162 {
163         struct nfqnl_instance *inst = container_of(head, struct nfqnl_instance,
164                                                    rcu);
165 
166         nfqnl_flush(inst, NULL, 0);
167         kfree(inst);
168         module_put(THIS_MODULE);
169 }
170 
171 static void
172 __instance_destroy(struct nfqnl_instance *inst)
173 {
174         hlist_del_rcu(&inst->hlist);
175         call_rcu(&inst->rcu, instance_destroy_rcu);
176 }
177 
178 static void
179 instance_destroy(struct nfnl_queue_net *q, struct nfqnl_instance *inst)
180 {
181         spin_lock(&q->instances_lock);
182         __instance_destroy(inst);
183         spin_unlock(&q->instances_lock);
184 }
185 
186 static inline void
187 __enqueue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
188 {
189        list_add_tail(&entry->list, &queue->queue_list);
190        queue->queue_total++;
191 }
192 
193 static void
194 __dequeue_entry(struct nfqnl_instance *queue, struct nf_queue_entry *entry)
195 {
196         list_del(&entry->list);
197         queue->queue_total--;
198 }
199 
200 static struct nf_queue_entry *
201 find_dequeue_entry(struct nfqnl_instance *queue, unsigned int id)
202 {
203         struct nf_queue_entry *entry = NULL, *i;
204 
205         spin_lock_bh(&queue->lock);
206 
207         list_for_each_entry(i, &queue->queue_list, list) {
208                 if (i->id == id) {
209                         entry = i;
210                         break;
211                 }
212         }
213 
214         if (entry)
215                 __dequeue_entry(queue, entry);
216 
217         spin_unlock_bh(&queue->lock);
218 
219         return entry;
220 }
221 
222 static void
223 nfqnl_flush(struct nfqnl_instance *queue, nfqnl_cmpfn cmpfn, unsigned long data)
224 {
225         struct nf_queue_entry *entry, *next;
226 
227         spin_lock_bh(&queue->lock);
228         list_for_each_entry_safe(entry, next, &queue->queue_list, list) {
229                 if (!cmpfn || cmpfn(entry, data)) {
230                         list_del(&entry->list);
231                         queue->queue_total--;
232                         nf_reinject(entry, NF_DROP);
233                 }
234         }
235         spin_unlock_bh(&queue->lock);
236 }
237 
238 static void
239 nfqnl_zcopy(struct sk_buff *to, const struct sk_buff *from, int len, int hlen)
240 {
241         int i, j = 0;
242         int plen = 0; /* length of skb->head fragment */
243         struct page *page;
244         unsigned int offset;
245 
246         /* dont bother with small payloads */
247         if (len <= skb_tailroom(to)) {
248                 skb_copy_bits(from, 0, skb_put(to, len), len);
249                 return;
250         }
251 
252         if (hlen) {
253                 skb_copy_bits(from, 0, skb_put(to, hlen), hlen);
254                 len -= hlen;
255         } else {
256                 plen = min_t(int, skb_headlen(from), len);
257                 if (plen) {
258                         page = virt_to_head_page(from->head);
259                         offset = from->data - (unsigned char *)page_address(page);
260                         __skb_fill_page_desc(to, 0, page, offset, plen);
261                         get_page(page);
262                         j = 1;
263                         len -= plen;
264                 }
265         }
266 
267         to->truesize += len + plen;
268         to->len += len + plen;
269         to->data_len += len + plen;
270 
271         for (i = 0; i < skb_shinfo(from)->nr_frags; i++) {
272                 if (!len)
273                         break;
274                 skb_shinfo(to)->frags[j] = skb_shinfo(from)->frags[i];
275                 skb_shinfo(to)->frags[j].size = min_t(int, skb_shinfo(to)->frags[j].size, len);
276                 len -= skb_shinfo(to)->frags[j].size;
277                 skb_frag_ref(to, j);
278                 j++;
279         }
280         skb_shinfo(to)->nr_frags = j;
281 }
282 
283 static int
284 nfqnl_put_packet_info(struct sk_buff *nlskb, struct sk_buff *packet,
285                       bool csum_verify)
286 {
287         __u32 flags = 0;
288 
289         if (packet->ip_summed == CHECKSUM_PARTIAL)
290                 flags = NFQA_SKB_CSUMNOTREADY;
291         else if (csum_verify)
292                 flags = NFQA_SKB_CSUM_NOTVERIFIED;
293 
294         if (skb_is_gso(packet))
295                 flags |= NFQA_SKB_GSO;
296 
297         return flags ? nla_put_be32(nlskb, NFQA_SKB_INFO, htonl(flags)) : 0;
298 }
299 
300 static struct sk_buff *
301 nfqnl_build_packet_message(struct nfqnl_instance *queue,
302                            struct nf_queue_entry *entry,
303                            __be32 **packet_id_ptr)
304 {
305         size_t size;
306         size_t data_len = 0, cap_len = 0;
307         int hlen = 0;
308         struct sk_buff *skb;
309         struct nlattr *nla;
310         struct nfqnl_msg_packet_hdr *pmsg;
311         struct nlmsghdr *nlh;
312         struct nfgenmsg *nfmsg;
313         struct sk_buff *entskb = entry->skb;
314         struct net_device *indev;
315         struct net_device *outdev;
316         struct nf_conn *ct = NULL;
317         enum ip_conntrack_info uninitialized_var(ctinfo);
318         bool csum_verify;
319 
320         size =    nlmsg_total_size(sizeof(struct nfgenmsg))
321                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hdr))
322                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
323                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
324 #ifdef CONFIG_BRIDGE_NETFILTER
325                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
326                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
327 #endif
328                 + nla_total_size(sizeof(u_int32_t))     /* mark */
329                 + nla_total_size(sizeof(struct nfqnl_msg_packet_hw))
330                 + nla_total_size(sizeof(u_int32_t))     /* skbinfo */
331                 + nla_total_size(sizeof(u_int32_t));    /* cap_len */
332 
333         if (entskb->tstamp.tv64)
334                 size += nla_total_size(sizeof(struct nfqnl_msg_packet_timestamp));
335 
336         if (entry->hook <= NF_INET_FORWARD ||
337            (entry->hook == NF_INET_POST_ROUTING && entskb->sk == NULL))
338                 csum_verify = !skb_csum_unnecessary(entskb);
339         else
340                 csum_verify = false;
341 
342         outdev = entry->outdev;
343 
344         switch ((enum nfqnl_config_mode)ACCESS_ONCE(queue->copy_mode)) {
345         case NFQNL_COPY_META:
346         case NFQNL_COPY_NONE:
347                 break;
348 
349         case NFQNL_COPY_PACKET:
350                 if (!(queue->flags & NFQA_CFG_F_GSO) &&
351                     entskb->ip_summed == CHECKSUM_PARTIAL &&
352                     skb_checksum_help(entskb))
353                         return NULL;
354 
355                 data_len = ACCESS_ONCE(queue->copy_range);
356                 if (data_len > entskb->len)
357                         data_len = entskb->len;
358 
359                 if (!entskb->head_frag ||
360                     skb_headlen(entskb) < L1_CACHE_BYTES ||
361                     skb_shinfo(entskb)->nr_frags >= MAX_SKB_FRAGS)
362                         hlen = skb_headlen(entskb);
363 
364                 if (skb_has_frag_list(entskb))
365                         hlen = entskb->len;
366                 hlen = min_t(int, data_len, hlen);
367                 size += sizeof(struct nlattr) + hlen;
368                 cap_len = entskb->len;
369                 break;
370         }
371 
372         if (queue->flags & NFQA_CFG_F_CONNTRACK)
373                 ct = nfqnl_ct_get(entskb, &size, &ctinfo);
374 
375         skb = nfnetlink_alloc_skb(&init_net, size, queue->peer_portid,
376                                   GFP_ATOMIC);
377         if (!skb)
378                 return NULL;
379 
380         nlh = nlmsg_put(skb, 0, 0,
381                         NFNL_SUBSYS_QUEUE << 8 | NFQNL_MSG_PACKET,
382                         sizeof(struct nfgenmsg), 0);
383         if (!nlh) {
384                 kfree_skb(skb);
385                 return NULL;
386         }
387         nfmsg = nlmsg_data(nlh);
388         nfmsg->nfgen_family = entry->pf;
389         nfmsg->version = NFNETLINK_V0;
390         nfmsg->res_id = htons(queue->queue_num);
391 
392         nla = __nla_reserve(skb, NFQA_PACKET_HDR, sizeof(*pmsg));
393         pmsg = nla_data(nla);
394         pmsg->hw_protocol       = entskb->protocol;
395         pmsg->hook              = entry->hook;
396         *packet_id_ptr          = &pmsg->packet_id;
397 
398         indev = entry->indev;
399         if (indev) {
400 #ifndef CONFIG_BRIDGE_NETFILTER
401                 if (nla_put_be32(skb, NFQA_IFINDEX_INDEV, htonl(indev->ifindex)))
402                         goto nla_put_failure;
403 #else
404                 if (entry->pf == PF_BRIDGE) {
405                         /* Case 1: indev is physical input device, we need to
406                          * look for bridge group (when called from
407                          * netfilter_bridge) */
408                         if (nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
409                                          htonl(indev->ifindex)) ||
410                         /* this is the bridge group "brX" */
411                         /* rcu_read_lock()ed by __nf_queue */
412                             nla_put_be32(skb, NFQA_IFINDEX_INDEV,
413                                          htonl(br_port_get_rcu(indev)->br->dev->ifindex)))
414                                 goto nla_put_failure;
415                 } else {
416                         /* Case 2: indev is bridge group, we need to look for
417                          * physical device (when called from ipv4) */
418                         if (nla_put_be32(skb, NFQA_IFINDEX_INDEV,
419                                          htonl(indev->ifindex)))
420                                 goto nla_put_failure;
421                         if (entskb->nf_bridge && entskb->nf_bridge->physindev &&
422                             nla_put_be32(skb, NFQA_IFINDEX_PHYSINDEV,
423                                          htonl(entskb->nf_bridge->physindev->ifindex)))
424                                 goto nla_put_failure;
425                 }
426 #endif
427         }
428 
429         if (outdev) {
430 #ifndef CONFIG_BRIDGE_NETFILTER
431                 if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV, htonl(outdev->ifindex)))
432                         goto nla_put_failure;
433 #else
434                 if (entry->pf == PF_BRIDGE) {
435                         /* Case 1: outdev is physical output device, we need to
436                          * look for bridge group (when called from
437                          * netfilter_bridge) */
438                         if (nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
439                                          htonl(outdev->ifindex)) ||
440                         /* this is the bridge group "brX" */
441                         /* rcu_read_lock()ed by __nf_queue */
442                             nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
443                                          htonl(br_port_get_rcu(outdev)->br->dev->ifindex)))
444                                 goto nla_put_failure;
445                 } else {
446                         /* Case 2: outdev is bridge group, we need to look for
447                          * physical output device (when called from ipv4) */
448                         if (nla_put_be32(skb, NFQA_IFINDEX_OUTDEV,
449                                          htonl(outdev->ifindex)))
450                                 goto nla_put_failure;
451                         if (entskb->nf_bridge && entskb->nf_bridge->physoutdev &&
452                             nla_put_be32(skb, NFQA_IFINDEX_PHYSOUTDEV,
453                                          htonl(entskb->nf_bridge->physoutdev->ifindex)))
454                                 goto nla_put_failure;
455                 }
456 #endif
457         }
458 
459         if (entskb->mark &&
460             nla_put_be32(skb, NFQA_MARK, htonl(entskb->mark)))
461                 goto nla_put_failure;
462 
463         if (indev && entskb->dev &&
464             entskb->mac_header != entskb->network_header) {
465                 struct nfqnl_msg_packet_hw phw;
466                 int len;
467 
468                 memset(&phw, 0, sizeof(phw));
469                 len = dev_parse_header(entskb, phw.hw_addr);
470                 if (len) {
471                         phw.hw_addrlen = htons(len);
472                         if (nla_put(skb, NFQA_HWADDR, sizeof(phw), &phw))
473                                 goto nla_put_failure;
474                 }
475         }
476 
477         if (entskb->tstamp.tv64) {
478                 struct nfqnl_msg_packet_timestamp ts;
479                 struct timeval tv = ktime_to_timeval(entskb->tstamp);
480                 ts.sec = cpu_to_be64(tv.tv_sec);
481                 ts.usec = cpu_to_be64(tv.tv_usec);
482 
483                 if (nla_put(skb, NFQA_TIMESTAMP, sizeof(ts), &ts))
484                         goto nla_put_failure;
485         }
486 
487         if (ct && nfqnl_ct_put(skb, ct, ctinfo) < 0)
488                 goto nla_put_failure;
489 
490         if (cap_len > data_len &&
491             nla_put_be32(skb, NFQA_CAP_LEN, htonl(cap_len)))
492                 goto nla_put_failure;
493 
494         if (nfqnl_put_packet_info(skb, entskb, csum_verify))
495                 goto nla_put_failure;
496 
497         if (data_len) {
498                 struct nlattr *nla;
499 
500                 if (skb_tailroom(skb) < sizeof(*nla) + hlen)
501                         goto nla_put_failure;
502 
503                 nla = (struct nlattr *)skb_put(skb, sizeof(*nla));
504                 nla->nla_type = NFQA_PAYLOAD;
505                 nla->nla_len = nla_attr_size(data_len);
506 
507                 nfqnl_zcopy(skb, entskb, data_len, hlen);
508         }
509 
510         nlh->nlmsg_len = skb->len;
511         return skb;
512 
513 nla_put_failure:
514         kfree_skb(skb);
515         net_err_ratelimited("nf_queue: error creating packet message\n");
516         return NULL;
517 }
518 
519 static int
520 __nfqnl_enqueue_packet(struct net *net, struct nfqnl_instance *queue,
521                         struct nf_queue_entry *entry)
522 {
523         struct sk_buff *nskb;
524         int err = -ENOBUFS;
525         __be32 *packet_id_ptr;
526         int failopen = 0;
527 
528         nskb = nfqnl_build_packet_message(queue, entry, &packet_id_ptr);
529         if (nskb == NULL) {
530                 err = -ENOMEM;
531                 goto err_out;
532         }
533         spin_lock_bh(&queue->lock);
534 
535         if (queue->queue_total >= queue->queue_maxlen) {
536                 if (queue->flags & NFQA_CFG_F_FAIL_OPEN) {
537                         failopen = 1;
538                         err = 0;
539                 } else {
540                         queue->queue_dropped++;
541                         net_warn_ratelimited("nf_queue: full at %d entries, dropping packets(s)\n",
542                                              queue->queue_total);
543                 }
544                 goto err_out_free_nskb;
545         }
546         entry->id = ++queue->id_sequence;
547         *packet_id_ptr = htonl(entry->id);
548 
549         /* nfnetlink_unicast will either free the nskb or add it to a socket */
550         err = nfnetlink_unicast(nskb, net, queue->peer_portid, MSG_DONTWAIT);
551         if (err < 0) {
552                 queue->queue_user_dropped++;
553                 goto err_out_unlock;
554         }
555 
556         __enqueue_entry(queue, entry);
557 
558         spin_unlock_bh(&queue->lock);
559         return 0;
560 
561 err_out_free_nskb:
562         kfree_skb(nskb);
563 err_out_unlock:
564         spin_unlock_bh(&queue->lock);
565         if (failopen)
566                 nf_reinject(entry, NF_ACCEPT);
567 err_out:
568         return err;
569 }
570 
571 static struct nf_queue_entry *
572 nf_queue_entry_dup(struct nf_queue_entry *e)
573 {
574         struct nf_queue_entry *entry = kmemdup(e, e->size, GFP_ATOMIC);
575         if (entry) {
576                 if (nf_queue_entry_get_refs(entry))
577                         return entry;
578                 kfree(entry);
579         }
580         return NULL;
581 }
582 
583 #ifdef CONFIG_BRIDGE_NETFILTER
584 /* When called from bridge netfilter, skb->data must point to MAC header
585  * before calling skb_gso_segment(). Else, original MAC header is lost
586  * and segmented skbs will be sent to wrong destination.
587  */
588 static void nf_bridge_adjust_skb_data(struct sk_buff *skb)
589 {
590         if (skb->nf_bridge)
591                 __skb_push(skb, skb->network_header - skb->mac_header);
592 }
593 
594 static void nf_bridge_adjust_segmented_data(struct sk_buff *skb)
595 {
596         if (skb->nf_bridge)
597                 __skb_pull(skb, skb->network_header - skb->mac_header);
598 }
599 #else
600 #define nf_bridge_adjust_skb_data(s) do {} while (0)
601 #define nf_bridge_adjust_segmented_data(s) do {} while (0)
602 #endif
603 
604 static void free_entry(struct nf_queue_entry *entry)
605 {
606         nf_queue_entry_release_refs(entry);
607         kfree(entry);
608 }
609 
610 static int
611 __nfqnl_enqueue_packet_gso(struct net *net, struct nfqnl_instance *queue,
612                            struct sk_buff *skb, struct nf_queue_entry *entry)
613 {
614         int ret = -ENOMEM;
615         struct nf_queue_entry *entry_seg;
616 
617         nf_bridge_adjust_segmented_data(skb);
618 
619         if (skb->next == NULL) { /* last packet, no need to copy entry */
620                 struct sk_buff *gso_skb = entry->skb;
621                 entry->skb = skb;
622                 ret = __nfqnl_enqueue_packet(net, queue, entry);
623                 if (ret)
624                         entry->skb = gso_skb;
625                 return ret;
626         }
627 
628         skb->next = NULL;
629 
630         entry_seg = nf_queue_entry_dup(entry);
631         if (entry_seg) {
632                 entry_seg->skb = skb;
633                 ret = __nfqnl_enqueue_packet(net, queue, entry_seg);
634                 if (ret)
635                         free_entry(entry_seg);
636         }
637         return ret;
638 }
639 
640 static int
641 nfqnl_enqueue_packet(struct nf_queue_entry *entry, unsigned int queuenum)
642 {
643         unsigned int queued;
644         struct nfqnl_instance *queue;
645         struct sk_buff *skb, *segs;
646         int err = -ENOBUFS;
647         struct net *net = dev_net(entry->indev ?
648                                   entry->indev : entry->outdev);
649         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
650 
651         /* rcu_read_lock()ed by nf_hook_slow() */
652         queue = instance_lookup(q, queuenum);
653         if (!queue)
654                 return -ESRCH;
655 
656         if (queue->copy_mode == NFQNL_COPY_NONE)
657                 return -EINVAL;
658 
659         skb = entry->skb;
660 
661         switch (entry->pf) {
662         case NFPROTO_IPV4:
663                 skb->protocol = htons(ETH_P_IP);
664                 break;
665         case NFPROTO_IPV6:
666                 skb->protocol = htons(ETH_P_IPV6);
667                 break;
668         }
669 
670         if ((queue->flags & NFQA_CFG_F_GSO) || !skb_is_gso(skb))
671                 return __nfqnl_enqueue_packet(net, queue, entry);
672 
673         nf_bridge_adjust_skb_data(skb);
674         segs = skb_gso_segment(skb, 0);
675         /* Does not use PTR_ERR to limit the number of error codes that can be
676          * returned by nf_queue.  For instance, callers rely on -ECANCELED to
677          * mean 'ignore this hook'.
678          */
679         if (IS_ERR(segs))
680                 goto out_err;
681         queued = 0;
682         err = 0;
683         do {
684                 struct sk_buff *nskb = segs->next;
685                 if (err == 0)
686                         err = __nfqnl_enqueue_packet_gso(net, queue,
687                                                         segs, entry);
688                 if (err == 0)
689                         queued++;
690                 else
691                         kfree_skb(segs);
692                 segs = nskb;
693         } while (segs);
694 
695         if (queued) {
696                 if (err) /* some segments are already queued */
697                         free_entry(entry);
698                 kfree_skb(skb);
699                 return 0;
700         }
701  out_err:
702         nf_bridge_adjust_segmented_data(skb);
703         return err;
704 }
705 
706 static int
707 nfqnl_mangle(void *data, int data_len, struct nf_queue_entry *e, int diff)
708 {
709         struct sk_buff *nskb;
710 
711         if (diff < 0) {
712                 if (pskb_trim(e->skb, data_len))
713                         return -ENOMEM;
714         } else if (diff > 0) {
715                 if (data_len > 0xFFFF)
716                         return -EINVAL;
717                 if (diff > skb_tailroom(e->skb)) {
718                         nskb = skb_copy_expand(e->skb, skb_headroom(e->skb),
719                                                diff, GFP_ATOMIC);
720                         if (!nskb) {
721                                 printk(KERN_WARNING "nf_queue: OOM "
722                                       "in mangle, dropping packet\n");
723                                 return -ENOMEM;
724                         }
725                         kfree_skb(e->skb);
726                         e->skb = nskb;
727                 }
728                 skb_put(e->skb, diff);
729         }
730         if (!skb_make_writable(e->skb, data_len))
731                 return -ENOMEM;
732         skb_copy_to_linear_data(e->skb, data, data_len);
733         e->skb->ip_summed = CHECKSUM_NONE;
734         return 0;
735 }
736 
737 static int
738 nfqnl_set_mode(struct nfqnl_instance *queue,
739                unsigned char mode, unsigned int range)
740 {
741         int status = 0;
742 
743         spin_lock_bh(&queue->lock);
744         switch (mode) {
745         case NFQNL_COPY_NONE:
746         case NFQNL_COPY_META:
747                 queue->copy_mode = mode;
748                 queue->copy_range = 0;
749                 break;
750 
751         case NFQNL_COPY_PACKET:
752                 queue->copy_mode = mode;
753                 if (range == 0 || range > NFQNL_MAX_COPY_RANGE)
754                         queue->copy_range = NFQNL_MAX_COPY_RANGE;
755                 else
756                         queue->copy_range = range;
757                 break;
758 
759         default:
760                 status = -EINVAL;
761 
762         }
763         spin_unlock_bh(&queue->lock);
764 
765         return status;
766 }
767 
768 static int
769 dev_cmp(struct nf_queue_entry *entry, unsigned long ifindex)
770 {
771         if (entry->indev)
772                 if (entry->indev->ifindex == ifindex)
773                         return 1;
774         if (entry->outdev)
775                 if (entry->outdev->ifindex == ifindex)
776                         return 1;
777 #ifdef CONFIG_BRIDGE_NETFILTER
778         if (entry->skb->nf_bridge) {
779                 if (entry->skb->nf_bridge->physindev &&
780                     entry->skb->nf_bridge->physindev->ifindex == ifindex)
781                         return 1;
782                 if (entry->skb->nf_bridge->physoutdev &&
783                     entry->skb->nf_bridge->physoutdev->ifindex == ifindex)
784                         return 1;
785         }
786 #endif
787         return 0;
788 }
789 
790 /* drop all packets with either indev or outdev == ifindex from all queue
791  * instances */
792 static void
793 nfqnl_dev_drop(struct net *net, int ifindex)
794 {
795         int i;
796         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
797 
798         rcu_read_lock();
799 
800         for (i = 0; i < INSTANCE_BUCKETS; i++) {
801                 struct nfqnl_instance *inst;
802                 struct hlist_head *head = &q->instance_table[i];
803 
804                 hlist_for_each_entry_rcu(inst, head, hlist)
805                         nfqnl_flush(inst, dev_cmp, ifindex);
806         }
807 
808         rcu_read_unlock();
809 }
810 
811 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
812 
813 static int
814 nfqnl_rcv_dev_event(struct notifier_block *this,
815                     unsigned long event, void *ptr)
816 {
817         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
818 
819         /* Drop any packets associated with the downed device */
820         if (event == NETDEV_DOWN)
821                 nfqnl_dev_drop(dev_net(dev), dev->ifindex);
822         return NOTIFY_DONE;
823 }
824 
825 static struct notifier_block nfqnl_dev_notifier = {
826         .notifier_call  = nfqnl_rcv_dev_event,
827 };
828 
829 static int
830 nfqnl_rcv_nl_event(struct notifier_block *this,
831                    unsigned long event, void *ptr)
832 {
833         struct netlink_notify *n = ptr;
834         struct nfnl_queue_net *q = nfnl_queue_pernet(n->net);
835 
836         if (event == NETLINK_URELEASE && n->protocol == NETLINK_NETFILTER) {
837                 int i;
838 
839                 /* destroy all instances for this portid */
840                 spin_lock(&q->instances_lock);
841                 for (i = 0; i < INSTANCE_BUCKETS; i++) {
842                         struct hlist_node *t2;
843                         struct nfqnl_instance *inst;
844                         struct hlist_head *head = &q->instance_table[i];
845 
846                         hlist_for_each_entry_safe(inst, t2, head, hlist) {
847                                 if (n->portid == inst->peer_portid)
848                                         __instance_destroy(inst);
849                         }
850                 }
851                 spin_unlock(&q->instances_lock);
852         }
853         return NOTIFY_DONE;
854 }
855 
856 static struct notifier_block nfqnl_rtnl_notifier = {
857         .notifier_call  = nfqnl_rcv_nl_event,
858 };
859 
860 static const struct nla_policy nfqa_verdict_policy[NFQA_MAX+1] = {
861         [NFQA_VERDICT_HDR]      = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
862         [NFQA_MARK]             = { .type = NLA_U32 },
863         [NFQA_PAYLOAD]          = { .type = NLA_UNSPEC },
864         [NFQA_CT]               = { .type = NLA_UNSPEC },
865 };
866 
867 static const struct nla_policy nfqa_verdict_batch_policy[NFQA_MAX+1] = {
868         [NFQA_VERDICT_HDR]      = { .len = sizeof(struct nfqnl_msg_verdict_hdr) },
869         [NFQA_MARK]             = { .type = NLA_U32 },
870 };
871 
872 static struct nfqnl_instance *
873 verdict_instance_lookup(struct nfnl_queue_net *q, u16 queue_num, int nlportid)
874 {
875         struct nfqnl_instance *queue;
876 
877         queue = instance_lookup(q, queue_num);
878         if (!queue)
879                 return ERR_PTR(-ENODEV);
880 
881         if (queue->peer_portid != nlportid)
882                 return ERR_PTR(-EPERM);
883 
884         return queue;
885 }
886 
887 static struct nfqnl_msg_verdict_hdr*
888 verdicthdr_get(const struct nlattr * const nfqa[])
889 {
890         struct nfqnl_msg_verdict_hdr *vhdr;
891         unsigned int verdict;
892 
893         if (!nfqa[NFQA_VERDICT_HDR])
894                 return NULL;
895 
896         vhdr = nla_data(nfqa[NFQA_VERDICT_HDR]);
897         verdict = ntohl(vhdr->verdict) & NF_VERDICT_MASK;
898         if (verdict > NF_MAX_VERDICT || verdict == NF_STOLEN)
899                 return NULL;
900         return vhdr;
901 }
902 
903 static int nfq_id_after(unsigned int id, unsigned int max)
904 {
905         return (int)(id - max) > 0;
906 }
907 
908 static int
909 nfqnl_recv_verdict_batch(struct sock *ctnl, struct sk_buff *skb,
910                    const struct nlmsghdr *nlh,
911                    const struct nlattr * const nfqa[])
912 {
913         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
914         struct nf_queue_entry *entry, *tmp;
915         unsigned int verdict, maxid;
916         struct nfqnl_msg_verdict_hdr *vhdr;
917         struct nfqnl_instance *queue;
918         LIST_HEAD(batch_list);
919         u16 queue_num = ntohs(nfmsg->res_id);
920 
921         struct net *net = sock_net(ctnl);
922         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
923 
924         queue = verdict_instance_lookup(q, queue_num,
925                                         NETLINK_CB(skb).portid);
926         if (IS_ERR(queue))
927                 return PTR_ERR(queue);
928 
929         vhdr = verdicthdr_get(nfqa);
930         if (!vhdr)
931                 return -EINVAL;
932 
933         verdict = ntohl(vhdr->verdict);
934         maxid = ntohl(vhdr->id);
935 
936         spin_lock_bh(&queue->lock);
937 
938         list_for_each_entry_safe(entry, tmp, &queue->queue_list, list) {
939                 if (nfq_id_after(entry->id, maxid))
940                         break;
941                 __dequeue_entry(queue, entry);
942                 list_add_tail(&entry->list, &batch_list);
943         }
944 
945         spin_unlock_bh(&queue->lock);
946 
947         if (list_empty(&batch_list))
948                 return -ENOENT;
949 
950         list_for_each_entry_safe(entry, tmp, &batch_list, list) {
951                 if (nfqa[NFQA_MARK])
952                         entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
953                 nf_reinject(entry, verdict);
954         }
955         return 0;
956 }
957 
958 static int
959 nfqnl_recv_verdict(struct sock *ctnl, struct sk_buff *skb,
960                    const struct nlmsghdr *nlh,
961                    const struct nlattr * const nfqa[])
962 {
963         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
964         u_int16_t queue_num = ntohs(nfmsg->res_id);
965 
966         struct nfqnl_msg_verdict_hdr *vhdr;
967         struct nfqnl_instance *queue;
968         unsigned int verdict;
969         struct nf_queue_entry *entry;
970         enum ip_conntrack_info uninitialized_var(ctinfo);
971         struct nf_conn *ct = NULL;
972 
973         struct net *net = sock_net(ctnl);
974         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
975 
976         queue = instance_lookup(q, queue_num);
977         if (!queue)
978                 queue = verdict_instance_lookup(q, queue_num,
979                                                 NETLINK_CB(skb).portid);
980         if (IS_ERR(queue))
981                 return PTR_ERR(queue);
982 
983         vhdr = verdicthdr_get(nfqa);
984         if (!vhdr)
985                 return -EINVAL;
986 
987         verdict = ntohl(vhdr->verdict);
988 
989         entry = find_dequeue_entry(queue, ntohl(vhdr->id));
990         if (entry == NULL)
991                 return -ENOENT;
992 
993         rcu_read_lock();
994         if (nfqa[NFQA_CT] && (queue->flags & NFQA_CFG_F_CONNTRACK))
995                 ct = nfqnl_ct_parse(entry->skb, nfqa[NFQA_CT], &ctinfo);
996 
997         if (nfqa[NFQA_PAYLOAD]) {
998                 u16 payload_len = nla_len(nfqa[NFQA_PAYLOAD]);
999                 int diff = payload_len - entry->skb->len;
1000 
1001                 if (nfqnl_mangle(nla_data(nfqa[NFQA_PAYLOAD]),
1002                                  payload_len, entry, diff) < 0)
1003                         verdict = NF_DROP;
1004 
1005                 if (ct)
1006                         nfqnl_ct_seq_adjust(skb, ct, ctinfo, diff);
1007         }
1008         rcu_read_unlock();
1009 
1010         if (nfqa[NFQA_MARK])
1011                 entry->skb->mark = ntohl(nla_get_be32(nfqa[NFQA_MARK]));
1012 
1013         nf_reinject(entry, verdict);
1014         return 0;
1015 }
1016 
1017 static int
1018 nfqnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
1019                   const struct nlmsghdr *nlh,
1020                   const struct nlattr * const nfqa[])
1021 {
1022         return -ENOTSUPP;
1023 }
1024 
1025 static const struct nla_policy nfqa_cfg_policy[NFQA_CFG_MAX+1] = {
1026         [NFQA_CFG_CMD]          = { .len = sizeof(struct nfqnl_msg_config_cmd) },
1027         [NFQA_CFG_PARAMS]       = { .len = sizeof(struct nfqnl_msg_config_params) },
1028 };
1029 
1030 static const struct nf_queue_handler nfqh = {
1031         .outfn  = &nfqnl_enqueue_packet,
1032 };
1033 
1034 static int
1035 nfqnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
1036                   const struct nlmsghdr *nlh,
1037                   const struct nlattr * const nfqa[])
1038 {
1039         struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1040         u_int16_t queue_num = ntohs(nfmsg->res_id);
1041         struct nfqnl_instance *queue;
1042         struct nfqnl_msg_config_cmd *cmd = NULL;
1043         struct net *net = sock_net(ctnl);
1044         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1045         int ret = 0;
1046 
1047         if (nfqa[NFQA_CFG_CMD]) {
1048                 cmd = nla_data(nfqa[NFQA_CFG_CMD]);
1049 
1050                 /* Obsolete commands without queue context */
1051                 switch (cmd->command) {
1052                 case NFQNL_CFG_CMD_PF_BIND: return 0;
1053                 case NFQNL_CFG_CMD_PF_UNBIND: return 0;
1054                 }
1055         }
1056 
1057         rcu_read_lock();
1058         queue = instance_lookup(q, queue_num);
1059         if (queue && queue->peer_portid != NETLINK_CB(skb).portid) {
1060                 ret = -EPERM;
1061                 goto err_out_unlock;
1062         }
1063 
1064         if (cmd != NULL) {
1065                 switch (cmd->command) {
1066                 case NFQNL_CFG_CMD_BIND:
1067                         if (queue) {
1068                                 ret = -EBUSY;
1069                                 goto err_out_unlock;
1070                         }
1071                         queue = instance_create(q, queue_num,
1072                                                 NETLINK_CB(skb).portid);
1073                         if (IS_ERR(queue)) {
1074                                 ret = PTR_ERR(queue);
1075                                 goto err_out_unlock;
1076                         }
1077                         break;
1078                 case NFQNL_CFG_CMD_UNBIND:
1079                         if (!queue) {
1080                                 ret = -ENODEV;
1081                                 goto err_out_unlock;
1082                         }
1083                         instance_destroy(q, queue);
1084                         break;
1085                 case NFQNL_CFG_CMD_PF_BIND:
1086                 case NFQNL_CFG_CMD_PF_UNBIND:
1087                         break;
1088                 default:
1089                         ret = -ENOTSUPP;
1090                         break;
1091                 }
1092         }
1093 
1094         if (nfqa[NFQA_CFG_PARAMS]) {
1095                 struct nfqnl_msg_config_params *params;
1096 
1097                 if (!queue) {
1098                         ret = -ENODEV;
1099                         goto err_out_unlock;
1100                 }
1101                 params = nla_data(nfqa[NFQA_CFG_PARAMS]);
1102                 nfqnl_set_mode(queue, params->copy_mode,
1103                                 ntohl(params->copy_range));
1104         }
1105 
1106         if (nfqa[NFQA_CFG_QUEUE_MAXLEN]) {
1107                 __be32 *queue_maxlen;
1108 
1109                 if (!queue) {
1110                         ret = -ENODEV;
1111                         goto err_out_unlock;
1112                 }
1113                 queue_maxlen = nla_data(nfqa[NFQA_CFG_QUEUE_MAXLEN]);
1114                 spin_lock_bh(&queue->lock);
1115                 queue->queue_maxlen = ntohl(*queue_maxlen);
1116                 spin_unlock_bh(&queue->lock);
1117         }
1118 
1119         if (nfqa[NFQA_CFG_FLAGS]) {
1120                 __u32 flags, mask;
1121 
1122                 if (!queue) {
1123                         ret = -ENODEV;
1124                         goto err_out_unlock;
1125                 }
1126 
1127                 if (!nfqa[NFQA_CFG_MASK]) {
1128                         /* A mask is needed to specify which flags are being
1129                          * changed.
1130                          */
1131                         ret = -EINVAL;
1132                         goto err_out_unlock;
1133                 }
1134 
1135                 flags = ntohl(nla_get_be32(nfqa[NFQA_CFG_FLAGS]));
1136                 mask = ntohl(nla_get_be32(nfqa[NFQA_CFG_MASK]));
1137 
1138                 if (flags >= NFQA_CFG_F_MAX) {
1139                         ret = -EOPNOTSUPP;
1140                         goto err_out_unlock;
1141                 }
1142 
1143                 spin_lock_bh(&queue->lock);
1144                 queue->flags &= ~mask;
1145                 queue->flags |= flags & mask;
1146                 spin_unlock_bh(&queue->lock);
1147         }
1148 
1149 err_out_unlock:
1150         rcu_read_unlock();
1151         return ret;
1152 }
1153 
1154 static const struct nfnl_callback nfqnl_cb[NFQNL_MSG_MAX] = {
1155         [NFQNL_MSG_PACKET]      = { .call_rcu = nfqnl_recv_unsupp,
1156                                     .attr_count = NFQA_MAX, },
1157         [NFQNL_MSG_VERDICT]     = { .call_rcu = nfqnl_recv_verdict,
1158                                     .attr_count = NFQA_MAX,
1159                                     .policy = nfqa_verdict_policy },
1160         [NFQNL_MSG_CONFIG]      = { .call = nfqnl_recv_config,
1161                                     .attr_count = NFQA_CFG_MAX,
1162                                     .policy = nfqa_cfg_policy },
1163         [NFQNL_MSG_VERDICT_BATCH]={ .call_rcu = nfqnl_recv_verdict_batch,
1164                                     .attr_count = NFQA_MAX,
1165                                     .policy = nfqa_verdict_batch_policy },
1166 };
1167 
1168 static const struct nfnetlink_subsystem nfqnl_subsys = {
1169         .name           = "nf_queue",
1170         .subsys_id      = NFNL_SUBSYS_QUEUE,
1171         .cb_count       = NFQNL_MSG_MAX,
1172         .cb             = nfqnl_cb,
1173 };
1174 
1175 #ifdef CONFIG_PROC_FS
1176 struct iter_state {
1177         struct seq_net_private p;
1178         unsigned int bucket;
1179 };
1180 
1181 static struct hlist_node *get_first(struct seq_file *seq)
1182 {
1183         struct iter_state *st = seq->private;
1184         struct net *net;
1185         struct nfnl_queue_net *q;
1186 
1187         if (!st)
1188                 return NULL;
1189 
1190         net = seq_file_net(seq);
1191         q = nfnl_queue_pernet(net);
1192         for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
1193                 if (!hlist_empty(&q->instance_table[st->bucket]))
1194                         return q->instance_table[st->bucket].first;
1195         }
1196         return NULL;
1197 }
1198 
1199 static struct hlist_node *get_next(struct seq_file *seq, struct hlist_node *h)
1200 {
1201         struct iter_state *st = seq->private;
1202         struct net *net = seq_file_net(seq);
1203 
1204         h = h->next;
1205         while (!h) {
1206                 struct nfnl_queue_net *q;
1207 
1208                 if (++st->bucket >= INSTANCE_BUCKETS)
1209                         return NULL;
1210 
1211                 q = nfnl_queue_pernet(net);
1212                 h = q->instance_table[st->bucket].first;
1213         }
1214         return h;
1215 }
1216 
1217 static struct hlist_node *get_idx(struct seq_file *seq, loff_t pos)
1218 {
1219         struct hlist_node *head;
1220         head = get_first(seq);
1221 
1222         if (head)
1223                 while (pos && (head = get_next(seq, head)))
1224                         pos--;
1225         return pos ? NULL : head;
1226 }
1227 
1228 static void *seq_start(struct seq_file *s, loff_t *pos)
1229         __acquires(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
1230 {
1231         spin_lock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1232         return get_idx(s, *pos);
1233 }
1234 
1235 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
1236 {
1237         (*pos)++;
1238         return get_next(s, v);
1239 }
1240 
1241 static void seq_stop(struct seq_file *s, void *v)
1242         __releases(nfnl_queue_pernet(seq_file_net(s))->instances_lock)
1243 {
1244         spin_unlock(&nfnl_queue_pernet(seq_file_net(s))->instances_lock);
1245 }
1246 
1247 static int seq_show(struct seq_file *s, void *v)
1248 {
1249         const struct nfqnl_instance *inst = v;
1250 
1251         return seq_printf(s, "%5d %6d %5d %1d %5d %5d %5d %8d %2d\n",
1252                           inst->queue_num,
1253                           inst->peer_portid, inst->queue_total,
1254                           inst->copy_mode, inst->copy_range,
1255                           inst->queue_dropped, inst->queue_user_dropped,
1256                           inst->id_sequence, 1);
1257 }
1258 
1259 static const struct seq_operations nfqnl_seq_ops = {
1260         .start  = seq_start,
1261         .next   = seq_next,
1262         .stop   = seq_stop,
1263         .show   = seq_show,
1264 };
1265 
1266 static int nfqnl_open(struct inode *inode, struct file *file)
1267 {
1268         return seq_open_net(inode, file, &nfqnl_seq_ops,
1269                         sizeof(struct iter_state));
1270 }
1271 
1272 static const struct file_operations nfqnl_file_ops = {
1273         .owner   = THIS_MODULE,
1274         .open    = nfqnl_open,
1275         .read    = seq_read,
1276         .llseek  = seq_lseek,
1277         .release = seq_release_net,
1278 };
1279 
1280 #endif /* PROC_FS */
1281 
1282 static int __net_init nfnl_queue_net_init(struct net *net)
1283 {
1284         unsigned int i;
1285         struct nfnl_queue_net *q = nfnl_queue_pernet(net);
1286 
1287         for (i = 0; i < INSTANCE_BUCKETS; i++)
1288                 INIT_HLIST_HEAD(&q->instance_table[i]);
1289 
1290         spin_lock_init(&q->instances_lock);
1291 
1292 #ifdef CONFIG_PROC_FS
1293         if (!proc_create("nfnetlink_queue", 0440,
1294                          net->nf.proc_netfilter, &nfqnl_file_ops))
1295                 return -ENOMEM;
1296 #endif
1297         return 0;
1298 }
1299 
1300 static void __net_exit nfnl_queue_net_exit(struct net *net)
1301 {
1302 #ifdef CONFIG_PROC_FS
1303         remove_proc_entry("nfnetlink_queue", net->nf.proc_netfilter);
1304 #endif
1305 }
1306 
1307 static struct pernet_operations nfnl_queue_net_ops = {
1308         .init   = nfnl_queue_net_init,
1309         .exit   = nfnl_queue_net_exit,
1310         .id     = &nfnl_queue_net_id,
1311         .size   = sizeof(struct nfnl_queue_net),
1312 };
1313 
1314 static int __init nfnetlink_queue_init(void)
1315 {
1316         int status = -ENOMEM;
1317 
1318         netlink_register_notifier(&nfqnl_rtnl_notifier);
1319         status = nfnetlink_subsys_register(&nfqnl_subsys);
1320         if (status < 0) {
1321                 pr_err("nf_queue: failed to create netlink socket\n");
1322                 goto cleanup_netlink_notifier;
1323         }
1324 
1325         status = register_pernet_subsys(&nfnl_queue_net_ops);
1326         if (status < 0) {
1327                 pr_err("nf_queue: failed to register pernet ops\n");
1328                 goto cleanup_subsys;
1329         }
1330         register_netdevice_notifier(&nfqnl_dev_notifier);
1331         nf_register_queue_handler(&nfqh);
1332         return status;
1333 
1334 cleanup_subsys:
1335         nfnetlink_subsys_unregister(&nfqnl_subsys);
1336 cleanup_netlink_notifier:
1337         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1338         return status;
1339 }
1340 
1341 static void __exit nfnetlink_queue_fini(void)
1342 {
1343         nf_unregister_queue_handler();
1344         unregister_netdevice_notifier(&nfqnl_dev_notifier);
1345         unregister_pernet_subsys(&nfnl_queue_net_ops);
1346         nfnetlink_subsys_unregister(&nfqnl_subsys);
1347         netlink_unregister_notifier(&nfqnl_rtnl_notifier);
1348 
1349         rcu_barrier(); /* Wait for completion of call_rcu()'s */
1350 }
1351 
1352 MODULE_DESCRIPTION("netfilter packet queue handler");
1353 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1354 MODULE_LICENSE("GPL");
1355 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_QUEUE);
1356 
1357 module_init(nfnetlink_queue_init);
1358 module_exit(nfnetlink_queue_fini);
1359 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp