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

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

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

~ [ 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