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

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

Version: ~ [ linux-5.8 ] ~ [ linux-5.7.12 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.55 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.136 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.191 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.232 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.232 ] ~ [ 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.85 ] ~ [ 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-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 logging packets to userspace via
  3  * nfetlink.
  4  *
  5  * (C) 2005 by Harald Welte <laforge@netfilter.org>
  6  *
  7  * Based on the old ipv4-only ipt_ULOG.c:
  8  * (C) 2000-2004 by Harald Welte <laforge@netfilter.org>
  9  *
 10  * This program is free software; you can redistribute it and/or modify
 11  * it under the terms of the GNU General Public License version 2 as
 12  * published by the Free Software Foundation.
 13  */
 14 #include <linux/module.h>
 15 #include <linux/skbuff.h>
 16 #include <linux/init.h>
 17 #include <linux/ip.h>
 18 #include <linux/ipv6.h>
 19 #include <linux/netdevice.h>
 20 #include <linux/netfilter.h>
 21 #include <linux/netlink.h>
 22 #include <linux/netfilter/nfnetlink.h>
 23 #include <linux/netfilter/nfnetlink_log.h>
 24 #include <linux/spinlock.h>
 25 #include <linux/sysctl.h>
 26 #include <linux/proc_fs.h>
 27 #include <linux/security.h>
 28 #include <linux/list.h>
 29 #include <linux/jhash.h>
 30 #include <linux/random.h>
 31 #include <net/sock.h>
 32 #include <net/netfilter/nf_log.h>
 33 #include <net/netfilter/nfnetlink_log.h>
 34 
 35 #include <asm/atomic.h>
 36 
 37 #ifdef CONFIG_BRIDGE_NETFILTER
 38 #include "../bridge/br_private.h"
 39 #endif
 40 
 41 #define NFULNL_NLBUFSIZ_DEFAULT NLMSG_GOODSIZE
 42 #define NFULNL_TIMEOUT_DEFAULT  100     /* every second */
 43 #define NFULNL_QTHRESH_DEFAULT  100     /* 100 packets */
 44 /* max packet size is limited by 16-bit struct nfattr nfa_len field */
 45 #define NFULNL_COPY_RANGE_MAX   (0xFFFF - NLA_HDRLEN)
 46 
 47 #define PRINTR(x, args...)      do { if (net_ratelimit()) \
 48                                      printk(x, ## args); } while (0);
 49 
 50 struct nfulnl_instance {
 51         struct hlist_node hlist;        /* global list of instances */
 52         spinlock_t lock;
 53         atomic_t use;                   /* use count */
 54 
 55         unsigned int qlen;              /* number of nlmsgs in skb */
 56         struct sk_buff *skb;            /* pre-allocatd skb */
 57         struct timer_list timer;
 58         int peer_pid;                   /* PID of the peer process */
 59 
 60         /* configurable parameters */
 61         unsigned int flushtimeout;      /* timeout until queue flush */
 62         unsigned int nlbufsiz;          /* netlink buffer allocation size */
 63         unsigned int qthreshold;        /* threshold of the queue */
 64         u_int32_t copy_range;
 65         u_int32_t seq;                  /* instance-local sequential counter */
 66         u_int16_t group_num;            /* number of this queue */
 67         u_int16_t flags;
 68         u_int8_t copy_mode;
 69 };
 70 
 71 static DEFINE_RWLOCK(instances_lock);
 72 static atomic_t global_seq;
 73 
 74 #define INSTANCE_BUCKETS        16
 75 static struct hlist_head instance_table[INSTANCE_BUCKETS];
 76 static unsigned int hash_init;
 77 
 78 static inline u_int8_t instance_hashfn(u_int16_t group_num)
 79 {
 80         return ((group_num & 0xff) % INSTANCE_BUCKETS);
 81 }
 82 
 83 static struct nfulnl_instance *
 84 __instance_lookup(u_int16_t group_num)
 85 {
 86         struct hlist_head *head;
 87         struct hlist_node *pos;
 88         struct nfulnl_instance *inst;
 89 
 90         head = &instance_table[instance_hashfn(group_num)];
 91         hlist_for_each_entry(inst, pos, head, hlist) {
 92                 if (inst->group_num == group_num)
 93                         return inst;
 94         }
 95         return NULL;
 96 }
 97 
 98 static inline void
 99 instance_get(struct nfulnl_instance *inst)
100 {
101         atomic_inc(&inst->use);
102 }
103 
104 static struct nfulnl_instance *
105 instance_lookup_get(u_int16_t group_num)
106 {
107         struct nfulnl_instance *inst;
108 
109         read_lock_bh(&instances_lock);
110         inst = __instance_lookup(group_num);
111         if (inst)
112                 instance_get(inst);
113         read_unlock_bh(&instances_lock);
114 
115         return inst;
116 }
117 
118 static void
119 instance_put(struct nfulnl_instance *inst)
120 {
121         if (inst && atomic_dec_and_test(&inst->use)) {
122                 kfree(inst);
123                 module_put(THIS_MODULE);
124         }
125 }
126 
127 static void nfulnl_timer(unsigned long data);
128 
129 static struct nfulnl_instance *
130 instance_create(u_int16_t group_num, int pid)
131 {
132         struct nfulnl_instance *inst;
133         int err;
134 
135         write_lock_bh(&instances_lock);
136         if (__instance_lookup(group_num)) {
137                 err = -EEXIST;
138                 goto out_unlock;
139         }
140 
141         inst = kzalloc(sizeof(*inst), GFP_ATOMIC);
142         if (!inst) {
143                 err = -ENOMEM;
144                 goto out_unlock;
145         }
146 
147         if (!try_module_get(THIS_MODULE)) {
148                 kfree(inst);
149                 err = -EAGAIN;
150                 goto out_unlock;
151         }
152 
153         INIT_HLIST_NODE(&inst->hlist);
154         spin_lock_init(&inst->lock);
155         /* needs to be two, since we _put() after creation */
156         atomic_set(&inst->use, 2);
157 
158         setup_timer(&inst->timer, nfulnl_timer, (unsigned long)inst);
159 
160         inst->peer_pid = pid;
161         inst->group_num = group_num;
162 
163         inst->qthreshold        = NFULNL_QTHRESH_DEFAULT;
164         inst->flushtimeout      = NFULNL_TIMEOUT_DEFAULT;
165         inst->nlbufsiz          = NFULNL_NLBUFSIZ_DEFAULT;
166         inst->copy_mode         = NFULNL_COPY_PACKET;
167         inst->copy_range        = NFULNL_COPY_RANGE_MAX;
168 
169         hlist_add_head(&inst->hlist,
170                        &instance_table[instance_hashfn(group_num)]);
171 
172         write_unlock_bh(&instances_lock);
173 
174         return inst;
175 
176 out_unlock:
177         write_unlock_bh(&instances_lock);
178         return ERR_PTR(err);
179 }
180 
181 static void __nfulnl_flush(struct nfulnl_instance *inst);
182 
183 static void
184 __instance_destroy(struct nfulnl_instance *inst)
185 {
186         /* first pull it out of the global list */
187         hlist_del(&inst->hlist);
188 
189         /* then flush all pending packets from skb */
190 
191         spin_lock_bh(&inst->lock);
192         if (inst->skb)
193                 __nfulnl_flush(inst);
194         spin_unlock_bh(&inst->lock);
195 
196         /* and finally put the refcount */
197         instance_put(inst);
198 }
199 
200 static inline void
201 instance_destroy(struct nfulnl_instance *inst)
202 {
203         write_lock_bh(&instances_lock);
204         __instance_destroy(inst);
205         write_unlock_bh(&instances_lock);
206 }
207 
208 static int
209 nfulnl_set_mode(struct nfulnl_instance *inst, u_int8_t mode,
210                   unsigned int range)
211 {
212         int status = 0;
213 
214         spin_lock_bh(&inst->lock);
215 
216         switch (mode) {
217         case NFULNL_COPY_NONE:
218         case NFULNL_COPY_META:
219                 inst->copy_mode = mode;
220                 inst->copy_range = 0;
221                 break;
222 
223         case NFULNL_COPY_PACKET:
224                 inst->copy_mode = mode;
225                 if (range == 0)
226                         range = NFULNL_COPY_RANGE_MAX;
227                 inst->copy_range = min_t(unsigned int,
228                                          range, NFULNL_COPY_RANGE_MAX);
229                 break;
230 
231         default:
232                 status = -EINVAL;
233                 break;
234         }
235 
236         spin_unlock_bh(&inst->lock);
237 
238         return status;
239 }
240 
241 static int
242 nfulnl_set_nlbufsiz(struct nfulnl_instance *inst, u_int32_t nlbufsiz)
243 {
244         int status;
245 
246         spin_lock_bh(&inst->lock);
247         if (nlbufsiz < NFULNL_NLBUFSIZ_DEFAULT)
248                 status = -ERANGE;
249         else if (nlbufsiz > 131072)
250                 status = -ERANGE;
251         else {
252                 inst->nlbufsiz = nlbufsiz;
253                 status = 0;
254         }
255         spin_unlock_bh(&inst->lock);
256 
257         return status;
258 }
259 
260 static int
261 nfulnl_set_timeout(struct nfulnl_instance *inst, u_int32_t timeout)
262 {
263         spin_lock_bh(&inst->lock);
264         inst->flushtimeout = timeout;
265         spin_unlock_bh(&inst->lock);
266 
267         return 0;
268 }
269 
270 static int
271 nfulnl_set_qthresh(struct nfulnl_instance *inst, u_int32_t qthresh)
272 {
273         spin_lock_bh(&inst->lock);
274         inst->qthreshold = qthresh;
275         spin_unlock_bh(&inst->lock);
276 
277         return 0;
278 }
279 
280 static int
281 nfulnl_set_flags(struct nfulnl_instance *inst, u_int16_t flags)
282 {
283         spin_lock_bh(&inst->lock);
284         inst->flags = flags;
285         spin_unlock_bh(&inst->lock);
286 
287         return 0;
288 }
289 
290 static struct sk_buff *
291 nfulnl_alloc_skb(unsigned int inst_size, unsigned int pkt_size)
292 {
293         struct sk_buff *skb;
294         unsigned int n;
295 
296         /* alloc skb which should be big enough for a whole multipart
297          * message.  WARNING: has to be <= 128k due to slab restrictions */
298 
299         n = max(inst_size, pkt_size);
300         skb = alloc_skb(n, GFP_ATOMIC);
301         if (!skb) {
302                 PRINTR("nfnetlink_log: can't alloc whole buffer (%u bytes)\n",
303                         inst_size);
304 
305                 if (n > pkt_size) {
306                         /* try to allocate only as much as we need for current
307                          * packet */
308 
309                         skb = alloc_skb(pkt_size, GFP_ATOMIC);
310                         if (!skb)
311                                 PRINTR("nfnetlink_log: can't even alloc %u "
312                                        "bytes\n", pkt_size);
313                 }
314         }
315 
316         return skb;
317 }
318 
319 static int
320 __nfulnl_send(struct nfulnl_instance *inst)
321 {
322         int status = -1;
323 
324         if (inst->qlen > 1)
325                 NLMSG_PUT(inst->skb, 0, 0,
326                           NLMSG_DONE,
327                           sizeof(struct nfgenmsg));
328 
329         status = nfnetlink_unicast(inst->skb, inst->peer_pid, MSG_DONTWAIT);
330 
331         inst->qlen = 0;
332         inst->skb = NULL;
333 
334 nlmsg_failure:
335         return status;
336 }
337 
338 static void
339 __nfulnl_flush(struct nfulnl_instance *inst)
340 {
341         /* timer holds a reference */
342         if (del_timer(&inst->timer))
343                 instance_put(inst);
344         if (inst->skb)
345                 __nfulnl_send(inst);
346 }
347 
348 static void
349 nfulnl_timer(unsigned long data)
350 {
351         struct nfulnl_instance *inst = (struct nfulnl_instance *)data;
352 
353         spin_lock_bh(&inst->lock);
354         if (inst->skb)
355                 __nfulnl_send(inst);
356         spin_unlock_bh(&inst->lock);
357         instance_put(inst);
358 }
359 
360 /* This is an inline function, we don't really care about a long
361  * list of arguments */
362 static inline int
363 __build_packet_message(struct nfulnl_instance *inst,
364                         const struct sk_buff *skb,
365                         unsigned int data_len,
366                         u_int8_t pf,
367                         unsigned int hooknum,
368                         const struct net_device *indev,
369                         const struct net_device *outdev,
370                         const struct nf_loginfo *li,
371                         const char *prefix, unsigned int plen)
372 {
373         struct nfulnl_msg_packet_hdr pmsg;
374         struct nlmsghdr *nlh;
375         struct nfgenmsg *nfmsg;
376         __be32 tmp_uint;
377         sk_buff_data_t old_tail = inst->skb->tail;
378 
379         nlh = NLMSG_PUT(inst->skb, 0, 0,
380                         NFNL_SUBSYS_ULOG << 8 | NFULNL_MSG_PACKET,
381                         sizeof(struct nfgenmsg));
382         nfmsg = NLMSG_DATA(nlh);
383         nfmsg->nfgen_family = pf;
384         nfmsg->version = NFNETLINK_V0;
385         nfmsg->res_id = htons(inst->group_num);
386 
387         pmsg.hw_protocol        = skb->protocol;
388         pmsg.hook               = hooknum;
389 
390         NLA_PUT(inst->skb, NFULA_PACKET_HDR, sizeof(pmsg), &pmsg);
391 
392         if (prefix)
393                 NLA_PUT(inst->skb, NFULA_PREFIX, plen, prefix);
394 
395         if (indev) {
396 #ifndef CONFIG_BRIDGE_NETFILTER
397                 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
398                              htonl(indev->ifindex));
399 #else
400                 if (pf == PF_BRIDGE) {
401                         /* Case 1: outdev is physical input device, we need to
402                          * look for bridge group (when called from
403                          * netfilter_bridge) */
404                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
405                                      htonl(indev->ifindex));
406                         /* this is the bridge group "brX" */
407                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
408                                      htonl(indev->br_port->br->dev->ifindex));
409                 } else {
410                         /* Case 2: indev is bridge group, we need to look for
411                          * physical device (when called from ipv4) */
412                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_INDEV,
413                                      htonl(indev->ifindex));
414                         if (skb->nf_bridge && skb->nf_bridge->physindev)
415                                 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSINDEV,
416                                              htonl(skb->nf_bridge->physindev->ifindex));
417                 }
418 #endif
419         }
420 
421         if (outdev) {
422                 tmp_uint = htonl(outdev->ifindex);
423 #ifndef CONFIG_BRIDGE_NETFILTER
424                 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
425                              htonl(outdev->ifindex));
426 #else
427                 if (pf == PF_BRIDGE) {
428                         /* Case 1: outdev is physical output device, we need to
429                          * look for bridge group (when called from
430                          * netfilter_bridge) */
431                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
432                                      htonl(outdev->ifindex));
433                         /* this is the bridge group "brX" */
434                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
435                                      htonl(outdev->br_port->br->dev->ifindex));
436                 } else {
437                         /* Case 2: indev is a bridge group, we need to look
438                          * for physical device (when called from ipv4) */
439                         NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_OUTDEV,
440                                      htonl(outdev->ifindex));
441                         if (skb->nf_bridge && skb->nf_bridge->physoutdev)
442                                 NLA_PUT_BE32(inst->skb, NFULA_IFINDEX_PHYSOUTDEV,
443                                              htonl(skb->nf_bridge->physoutdev->ifindex));
444                 }
445 #endif
446         }
447 
448         if (skb->mark)
449                 NLA_PUT_BE32(inst->skb, NFULA_MARK, htonl(skb->mark));
450 
451         if (indev && skb->dev) {
452                 struct nfulnl_msg_packet_hw phw;
453                 int len = dev_parse_header(skb, phw.hw_addr);
454                 if (len > 0) {
455                         phw.hw_addrlen = htons(len);
456                         NLA_PUT(inst->skb, NFULA_HWADDR, sizeof(phw), &phw);
457                 }
458         }
459 
460         if (indev && skb_mac_header_was_set(skb)) {
461                 NLA_PUT_BE16(inst->skb, NFULA_HWTYPE, htons(skb->dev->type));
462                 NLA_PUT_BE16(inst->skb, NFULA_HWLEN,
463                              htons(skb->dev->hard_header_len));
464                 NLA_PUT(inst->skb, NFULA_HWHEADER, skb->dev->hard_header_len,
465                         skb_mac_header(skb));
466         }
467 
468         if (skb->tstamp.tv64) {
469                 struct nfulnl_msg_packet_timestamp ts;
470                 struct timeval tv = ktime_to_timeval(skb->tstamp);
471                 ts.sec = cpu_to_be64(tv.tv_sec);
472                 ts.usec = cpu_to_be64(tv.tv_usec);
473 
474                 NLA_PUT(inst->skb, NFULA_TIMESTAMP, sizeof(ts), &ts);
475         }
476 
477         /* UID */
478         if (skb->sk) {
479                 read_lock_bh(&skb->sk->sk_callback_lock);
480                 if (skb->sk->sk_socket && skb->sk->sk_socket->file) {
481                         struct file *file = skb->sk->sk_socket->file;
482                         __be32 uid = htonl(file->f_cred->fsuid);
483                         __be32 gid = htonl(file->f_cred->fsgid);
484                         /* need to unlock here since NLA_PUT may goto */
485                         read_unlock_bh(&skb->sk->sk_callback_lock);
486                         NLA_PUT_BE32(inst->skb, NFULA_UID, uid);
487                         NLA_PUT_BE32(inst->skb, NFULA_GID, gid);
488                 } else
489                         read_unlock_bh(&skb->sk->sk_callback_lock);
490         }
491 
492         /* local sequence number */
493         if (inst->flags & NFULNL_CFG_F_SEQ)
494                 NLA_PUT_BE32(inst->skb, NFULA_SEQ, htonl(inst->seq++));
495 
496         /* global sequence number */
497         if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
498                 NLA_PUT_BE32(inst->skb, NFULA_SEQ_GLOBAL,
499                              htonl(atomic_inc_return(&global_seq)));
500 
501         if (data_len) {
502                 struct nlattr *nla;
503                 int size = nla_attr_size(data_len);
504 
505                 if (skb_tailroom(inst->skb) < nla_total_size(data_len)) {
506                         printk(KERN_WARNING "nfnetlink_log: no tailroom!\n");
507                         goto nlmsg_failure;
508                 }
509 
510                 nla = (struct nlattr *)skb_put(inst->skb, nla_total_size(data_len));
511                 nla->nla_type = NFULA_PAYLOAD;
512                 nla->nla_len = size;
513 
514                 if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
515                         BUG();
516         }
517 
518         nlh->nlmsg_len = inst->skb->tail - old_tail;
519         return 0;
520 
521 nlmsg_failure:
522 nla_put_failure:
523         PRINTR(KERN_ERR "nfnetlink_log: error creating log nlmsg\n");
524         return -1;
525 }
526 
527 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0)
528 
529 static struct nf_loginfo default_loginfo = {
530         .type =         NF_LOG_TYPE_ULOG,
531         .u = {
532                 .ulog = {
533                         .copy_len       = 0xffff,
534                         .group          = 0,
535                         .qthreshold     = 1,
536                 },
537         },
538 };
539 
540 /* log handler for internal netfilter logging api */
541 void
542 nfulnl_log_packet(u_int8_t pf,
543                   unsigned int hooknum,
544                   const struct sk_buff *skb,
545                   const struct net_device *in,
546                   const struct net_device *out,
547                   const struct nf_loginfo *li_user,
548                   const char *prefix)
549 {
550         unsigned int size, data_len;
551         struct nfulnl_instance *inst;
552         const struct nf_loginfo *li;
553         unsigned int qthreshold;
554         unsigned int plen;
555 
556         if (li_user && li_user->type == NF_LOG_TYPE_ULOG)
557                 li = li_user;
558         else
559                 li = &default_loginfo;
560 
561         inst = instance_lookup_get(li->u.ulog.group);
562         if (!inst)
563                 return;
564 
565         plen = 0;
566         if (prefix)
567                 plen = strlen(prefix) + 1;
568 
569         /* FIXME: do we want to make the size calculation conditional based on
570          * what is actually present?  way more branches and checks, but more
571          * memory efficient... */
572         size =    NLMSG_SPACE(sizeof(struct nfgenmsg))
573                 + nla_total_size(sizeof(struct nfulnl_msg_packet_hdr))
574                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
575                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
576 #ifdef CONFIG_BRIDGE_NETFILTER
577                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
578                 + nla_total_size(sizeof(u_int32_t))     /* ifindex */
579 #endif
580                 + nla_total_size(sizeof(u_int32_t))     /* mark */
581                 + nla_total_size(sizeof(u_int32_t))     /* uid */
582                 + nla_total_size(sizeof(u_int32_t))     /* gid */
583                 + nla_total_size(plen)                  /* prefix */
584                 + nla_total_size(sizeof(struct nfulnl_msg_packet_hw))
585                 + nla_total_size(sizeof(struct nfulnl_msg_packet_timestamp))
586                 + nla_total_size(sizeof(struct nfgenmsg));      /* NLMSG_DONE */
587 
588         if (in && skb_mac_header_was_set(skb)) {
589                 size +=   nla_total_size(skb->dev->hard_header_len)
590                         + nla_total_size(sizeof(u_int16_t))     /* hwtype */
591                         + nla_total_size(sizeof(u_int16_t));    /* hwlen */
592         }
593 
594         spin_lock_bh(&inst->lock);
595 
596         if (inst->flags & NFULNL_CFG_F_SEQ)
597                 size += nla_total_size(sizeof(u_int32_t));
598         if (inst->flags & NFULNL_CFG_F_SEQ_GLOBAL)
599                 size += nla_total_size(sizeof(u_int32_t));
600 
601         qthreshold = inst->qthreshold;
602         /* per-rule qthreshold overrides per-instance */
603         if (li->u.ulog.qthreshold)
604                 if (qthreshold > li->u.ulog.qthreshold)
605                         qthreshold = li->u.ulog.qthreshold;
606 
607 
608         switch (inst->copy_mode) {
609         case NFULNL_COPY_META:
610         case NFULNL_COPY_NONE:
611                 data_len = 0;
612                 break;
613 
614         case NFULNL_COPY_PACKET:
615                 if (inst->copy_range > skb->len)
616                         data_len = skb->len;
617                 else
618                         data_len = inst->copy_range;
619 
620                 size += nla_total_size(data_len);
621                 break;
622 
623         default:
624                 goto unlock_and_release;
625         }
626 
627         if (inst->skb && size > skb_tailroom(inst->skb)) {
628                 /* either the queue len is too high or we don't have
629                  * enough room in the skb left. flush to userspace. */
630                 __nfulnl_flush(inst);
631         }
632 
633         if (!inst->skb) {
634                 inst->skb = nfulnl_alloc_skb(inst->nlbufsiz, size);
635                 if (!inst->skb)
636                         goto alloc_failure;
637         }
638 
639         inst->qlen++;
640 
641         __build_packet_message(inst, skb, data_len, pf,
642                                 hooknum, in, out, li, prefix, plen);
643 
644         if (inst->qlen >= qthreshold)
645                 __nfulnl_flush(inst);
646         /* timer_pending always called within inst->lock, so there
647          * is no chance of a race here */
648         else if (!timer_pending(&inst->timer)) {
649                 instance_get(inst);
650                 inst->timer.expires = jiffies + (inst->flushtimeout*HZ/100);
651                 add_timer(&inst->timer);
652         }
653 
654 unlock_and_release:
655         spin_unlock_bh(&inst->lock);
656         instance_put(inst);
657         return;
658 
659 alloc_failure:
660         /* FIXME: statistics */
661         goto unlock_and_release;
662 }
663 EXPORT_SYMBOL_GPL(nfulnl_log_packet);
664 
665 static int
666 nfulnl_rcv_nl_event(struct notifier_block *this,
667                    unsigned long event, void *ptr)
668 {
669         struct netlink_notify *n = ptr;
670 
671         if (event == NETLINK_URELEASE &&
672             n->protocol == NETLINK_NETFILTER && n->pid) {
673                 int i;
674 
675                 /* destroy all instances for this pid */
676                 write_lock_bh(&instances_lock);
677                 for  (i = 0; i < INSTANCE_BUCKETS; i++) {
678                         struct hlist_node *tmp, *t2;
679                         struct nfulnl_instance *inst;
680                         struct hlist_head *head = &instance_table[i];
681 
682                         hlist_for_each_entry_safe(inst, tmp, t2, head, hlist) {
683                                 if ((n->net == &init_net) &&
684                                     (n->pid == inst->peer_pid))
685                                         __instance_destroy(inst);
686                         }
687                 }
688                 write_unlock_bh(&instances_lock);
689         }
690         return NOTIFY_DONE;
691 }
692 
693 static struct notifier_block nfulnl_rtnl_notifier = {
694         .notifier_call  = nfulnl_rcv_nl_event,
695 };
696 
697 static int
698 nfulnl_recv_unsupp(struct sock *ctnl, struct sk_buff *skb,
699                    const struct nlmsghdr *nlh,
700                    const struct nlattr * const nfqa[])
701 {
702         return -ENOTSUPP;
703 }
704 
705 static struct nf_logger nfulnl_logger __read_mostly = {
706         .name   = "nfnetlink_log",
707         .logfn  = &nfulnl_log_packet,
708         .me     = THIS_MODULE,
709 };
710 
711 static const struct nla_policy nfula_cfg_policy[NFULA_CFG_MAX+1] = {
712         [NFULA_CFG_CMD]         = { .len = sizeof(struct nfulnl_msg_config_cmd) },
713         [NFULA_CFG_MODE]        = { .len = sizeof(struct nfulnl_msg_config_mode) },
714         [NFULA_CFG_TIMEOUT]     = { .type = NLA_U32 },
715         [NFULA_CFG_QTHRESH]     = { .type = NLA_U32 },
716         [NFULA_CFG_NLBUFSIZ]    = { .type = NLA_U32 },
717         [NFULA_CFG_FLAGS]       = { .type = NLA_U16 },
718 };
719 
720 static int
721 nfulnl_recv_config(struct sock *ctnl, struct sk_buff *skb,
722                    const struct nlmsghdr *nlh,
723                    const struct nlattr * const nfula[])
724 {
725         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
726         u_int16_t group_num = ntohs(nfmsg->res_id);
727         struct nfulnl_instance *inst;
728         struct nfulnl_msg_config_cmd *cmd = NULL;
729         int ret = 0;
730 
731         if (nfula[NFULA_CFG_CMD]) {
732                 u_int8_t pf = nfmsg->nfgen_family;
733                 cmd = nla_data(nfula[NFULA_CFG_CMD]);
734 
735                 /* Commands without queue context */
736                 switch (cmd->command) {
737                 case NFULNL_CFG_CMD_PF_BIND:
738                         return nf_log_bind_pf(pf, &nfulnl_logger);
739                 case NFULNL_CFG_CMD_PF_UNBIND:
740                         nf_log_unbind_pf(pf);
741                         return 0;
742                 }
743         }
744 
745         inst = instance_lookup_get(group_num);
746         if (inst && inst->peer_pid != NETLINK_CB(skb).pid) {
747                 ret = -EPERM;
748                 goto out_put;
749         }
750 
751         if (cmd != NULL) {
752                 switch (cmd->command) {
753                 case NFULNL_CFG_CMD_BIND:
754                         if (inst) {
755                                 ret = -EBUSY;
756                                 goto out_put;
757                         }
758 
759                         inst = instance_create(group_num,
760                                                NETLINK_CB(skb).pid);
761                         if (IS_ERR(inst)) {
762                                 ret = PTR_ERR(inst);
763                                 goto out;
764                         }
765                         break;
766                 case NFULNL_CFG_CMD_UNBIND:
767                         if (!inst) {
768                                 ret = -ENODEV;
769                                 goto out;
770                         }
771 
772                         instance_destroy(inst);
773                         goto out;
774                 default:
775                         ret = -ENOTSUPP;
776                         break;
777                 }
778         }
779 
780         if (nfula[NFULA_CFG_MODE]) {
781                 struct nfulnl_msg_config_mode *params;
782                 params = nla_data(nfula[NFULA_CFG_MODE]);
783 
784                 if (!inst) {
785                         ret = -ENODEV;
786                         goto out;
787                 }
788                 nfulnl_set_mode(inst, params->copy_mode,
789                                 ntohl(params->copy_range));
790         }
791 
792         if (nfula[NFULA_CFG_TIMEOUT]) {
793                 __be32 timeout = nla_get_be32(nfula[NFULA_CFG_TIMEOUT]);
794 
795                 if (!inst) {
796                         ret = -ENODEV;
797                         goto out;
798                 }
799                 nfulnl_set_timeout(inst, ntohl(timeout));
800         }
801 
802         if (nfula[NFULA_CFG_NLBUFSIZ]) {
803                 __be32 nlbufsiz = nla_get_be32(nfula[NFULA_CFG_NLBUFSIZ]);
804 
805                 if (!inst) {
806                         ret = -ENODEV;
807                         goto out;
808                 }
809                 nfulnl_set_nlbufsiz(inst, ntohl(nlbufsiz));
810         }
811 
812         if (nfula[NFULA_CFG_QTHRESH]) {
813                 __be32 qthresh = nla_get_be32(nfula[NFULA_CFG_QTHRESH]);
814 
815                 if (!inst) {
816                         ret = -ENODEV;
817                         goto out;
818                 }
819                 nfulnl_set_qthresh(inst, ntohl(qthresh));
820         }
821 
822         if (nfula[NFULA_CFG_FLAGS]) {
823                 __be16 flags = nla_get_be16(nfula[NFULA_CFG_FLAGS]);
824 
825                 if (!inst) {
826                         ret = -ENODEV;
827                         goto out;
828                 }
829                 nfulnl_set_flags(inst, ntohs(flags));
830         }
831 
832 out_put:
833         instance_put(inst);
834 out:
835         return ret;
836 }
837 
838 static const struct nfnl_callback nfulnl_cb[NFULNL_MSG_MAX] = {
839         [NFULNL_MSG_PACKET]     = { .call = nfulnl_recv_unsupp,
840                                     .attr_count = NFULA_MAX, },
841         [NFULNL_MSG_CONFIG]     = { .call = nfulnl_recv_config,
842                                     .attr_count = NFULA_CFG_MAX,
843                                     .policy = nfula_cfg_policy },
844 };
845 
846 static const struct nfnetlink_subsystem nfulnl_subsys = {
847         .name           = "log",
848         .subsys_id      = NFNL_SUBSYS_ULOG,
849         .cb_count       = NFULNL_MSG_MAX,
850         .cb             = nfulnl_cb,
851 };
852 
853 #ifdef CONFIG_PROC_FS
854 struct iter_state {
855         unsigned int bucket;
856 };
857 
858 static struct hlist_node *get_first(struct iter_state *st)
859 {
860         if (!st)
861                 return NULL;
862 
863         for (st->bucket = 0; st->bucket < INSTANCE_BUCKETS; st->bucket++) {
864                 if (!hlist_empty(&instance_table[st->bucket]))
865                         return instance_table[st->bucket].first;
866         }
867         return NULL;
868 }
869 
870 static struct hlist_node *get_next(struct iter_state *st, struct hlist_node *h)
871 {
872         h = h->next;
873         while (!h) {
874                 if (++st->bucket >= INSTANCE_BUCKETS)
875                         return NULL;
876 
877                 h = instance_table[st->bucket].first;
878         }
879         return h;
880 }
881 
882 static struct hlist_node *get_idx(struct iter_state *st, loff_t pos)
883 {
884         struct hlist_node *head;
885         head = get_first(st);
886 
887         if (head)
888                 while (pos && (head = get_next(st, head)))
889                         pos--;
890         return pos ? NULL : head;
891 }
892 
893 static void *seq_start(struct seq_file *seq, loff_t *pos)
894         __acquires(instances_lock)
895 {
896         read_lock_bh(&instances_lock);
897         return get_idx(seq->private, *pos);
898 }
899 
900 static void *seq_next(struct seq_file *s, void *v, loff_t *pos)
901 {
902         (*pos)++;
903         return get_next(s->private, v);
904 }
905 
906 static void seq_stop(struct seq_file *s, void *v)
907         __releases(instances_lock)
908 {
909         read_unlock_bh(&instances_lock);
910 }
911 
912 static int seq_show(struct seq_file *s, void *v)
913 {
914         const struct nfulnl_instance *inst = v;
915 
916         return seq_printf(s, "%5d %6d %5d %1d %5d %6d %2d\n",
917                           inst->group_num,
918                           inst->peer_pid, inst->qlen,
919                           inst->copy_mode, inst->copy_range,
920                           inst->flushtimeout, atomic_read(&inst->use));
921 }
922 
923 static const struct seq_operations nful_seq_ops = {
924         .start  = seq_start,
925         .next   = seq_next,
926         .stop   = seq_stop,
927         .show   = seq_show,
928 };
929 
930 static int nful_open(struct inode *inode, struct file *file)
931 {
932         return seq_open_private(file, &nful_seq_ops,
933                         sizeof(struct iter_state));
934 }
935 
936 static const struct file_operations nful_file_ops = {
937         .owner   = THIS_MODULE,
938         .open    = nful_open,
939         .read    = seq_read,
940         .llseek  = seq_lseek,
941         .release = seq_release_private,
942 };
943 
944 #endif /* PROC_FS */
945 
946 static int __init nfnetlink_log_init(void)
947 {
948         int i, status = -ENOMEM;
949 
950         for (i = 0; i < INSTANCE_BUCKETS; i++)
951                 INIT_HLIST_HEAD(&instance_table[i]);
952 
953         /* it's not really all that important to have a random value, so
954          * we can do this from the init function, even if there hasn't
955          * been that much entropy yet */
956         get_random_bytes(&hash_init, sizeof(hash_init));
957 
958         netlink_register_notifier(&nfulnl_rtnl_notifier);
959         status = nfnetlink_subsys_register(&nfulnl_subsys);
960         if (status < 0) {
961                 printk(KERN_ERR "log: failed to create netlink socket\n");
962                 goto cleanup_netlink_notifier;
963         }
964 
965         status = nf_log_register(NFPROTO_UNSPEC, &nfulnl_logger);
966         if (status < 0) {
967                 printk(KERN_ERR "log: failed to register logger\n");
968                 goto cleanup_subsys;
969         }
970 
971 #ifdef CONFIG_PROC_FS
972         if (!proc_create("nfnetlink_log", 0440,
973                          proc_net_netfilter, &nful_file_ops))
974                 goto cleanup_logger;
975 #endif
976         return status;
977 
978 #ifdef CONFIG_PROC_FS
979 cleanup_logger:
980         nf_log_unregister(&nfulnl_logger);
981 #endif
982 cleanup_subsys:
983         nfnetlink_subsys_unregister(&nfulnl_subsys);
984 cleanup_netlink_notifier:
985         netlink_unregister_notifier(&nfulnl_rtnl_notifier);
986         return status;
987 }
988 
989 static void __exit nfnetlink_log_fini(void)
990 {
991         nf_log_unregister(&nfulnl_logger);
992 #ifdef CONFIG_PROC_FS
993         remove_proc_entry("nfnetlink_log", proc_net_netfilter);
994 #endif
995         nfnetlink_subsys_unregister(&nfulnl_subsys);
996         netlink_unregister_notifier(&nfulnl_rtnl_notifier);
997 }
998 
999 MODULE_DESCRIPTION("netfilter userspace logging");
1000 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
1001 MODULE_LICENSE("GPL");
1002 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_ULOG);
1003 
1004 module_init(nfnetlink_log_init);
1005 module_exit(nfnetlink_log_fini);
1006 

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