Containerized syslog working properly
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / ipt_LOG.c
index 0af1413..74761de 100644 (file)
@@ -9,8 +9,9 @@
  * it under the terms of the GNU General Public License version 2 as
  * published by the Free Software Foundation.
  */
-
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 #include <linux/module.h>
+#include <linux/syslog.h>
 #include <linux/spinlock.h>
 #include <linux/skbuff.h>
 #include <linux/ip.h>
@@ -54,8 +55,8 @@ static void dump_packet(const struct nf_loginfo *info,
        /* Important fields:
         * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
        /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
-       printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ",
-              NIPQUAD(ih->saddr), NIPQUAD(ih->daddr));
+       printk("SRC=%pI4 DST=%pI4 ",
+              &ih->saddr, &ih->daddr);
 
        /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
        printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
@@ -74,8 +75,8 @@ static void dump_packet(const struct nf_loginfo *info,
        if (ntohs(ih->frag_off) & IP_OFFSET)
                printk("FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
 
-       if ((logflags & IPT_LOG_IPOPT)
-           && ih->ihl * 4 > sizeof(struct iphdr)) {
+       if ((logflags & IPT_LOG_IPOPT) &&
+           ih->ihl * 4 > sizeof(struct iphdr)) {
                const unsigned char *op;
                unsigned char _opt[4 * 15 - sizeof(struct iphdr)];
                unsigned int i, optsize;
@@ -146,8 +147,8 @@ static void dump_packet(const struct nf_loginfo *info,
                /* Max length: 11 "URGP=65535 " */
                printk("URGP=%u ", ntohs(th->urg_ptr));
 
-               if ((logflags & IPT_LOG_TCPOPT)
-                   && th->doff * 4 > sizeof(struct tcphdr)) {
+               if ((logflags & IPT_LOG_TCPOPT) &&
+                   th->doff * 4 > sizeof(struct tcphdr)) {
                        unsigned char _opt[4 * 15 - sizeof(struct tcphdr)];
                        const unsigned char *op;
                        unsigned int i, optsize;
@@ -238,9 +239,9 @@ static void dump_packet(const struct nf_loginfo *info,
                printk("TYPE=%u CODE=%u ", ich->type, ich->code);
 
                /* Max length: 25 "INCOMPLETE [65535 bytes] " */
-               if (ich->type <= NR_ICMP_TYPES
-                   && required_len[ich->type]
-                   && skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
+               if (ich->type <= NR_ICMP_TYPES &&
+                   required_len[ich->type] &&
+                   skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
                        printk("INCOMPLETE [%u bytes] ",
                               skb->len - iphoff - ih->ihl*4);
                        break;
@@ -262,8 +263,7 @@ static void dump_packet(const struct nf_loginfo *info,
                        break;
                case ICMP_REDIRECT:
                        /* Max length: 24 "GATEWAY=255.255.255.255 " */
-                       printk("GATEWAY=%u.%u.%u.%u ",
-                              NIPQUAD(ich->un.gateway));
+                       printk("GATEWAY=%pI4 ", &ich->un.gateway);
                        /* Fall through */
                case ICMP_DEST_UNREACH:
                case ICMP_SOURCE_QUENCH:
@@ -277,8 +277,8 @@ static void dump_packet(const struct nf_loginfo *info,
                        }
 
                        /* Max length: 10 "MTU=65535 " */
-                       if (ich->type == ICMP_DEST_UNREACH
-                           && ich->code == ICMP_FRAG_NEEDED)
+                       if (ich->type == ICMP_DEST_UNREACH &&
+                           ich->code == ICMP_FRAG_NEEDED)
                                printk("MTU=%u ", ntohs(ich->un.frag.mtu));
                }
                break;
@@ -340,8 +340,8 @@ static void dump_packet(const struct nf_loginfo *info,
                read_lock_bh(&skb->sk->sk_callback_lock);
                if (skb->sk->sk_socket && skb->sk->sk_socket->file)
                        printk("UID=%u GID=%u ",
-                               skb->sk->sk_socket->file->f_uid,
-                               skb->sk->sk_socket->file->f_gid);
+                               skb->sk->sk_socket->file->f_cred->fsuid,
+                               skb->sk->sk_socket->file->f_cred->fsgid);
                read_unlock_bh(&skb->sk->sk_callback_lock);
        }
 
@@ -368,14 +368,14 @@ static struct nf_loginfo default_loginfo = {
        .type   = NF_LOG_TYPE_LOG,
        .u = {
                .log = {
-                       .level    = 0,
+                       .level    = 5,
                        .logflags = NF_LOG_MASK,
                },
        },
 };
 
 static void
-ipt_log_packet(unsigned int pf,
+ipt_log_packet(u_int8_t pf,
               unsigned int hooknum,
               const struct sk_buff *skb,
               const struct net_device *in,
@@ -383,6 +383,14 @@ ipt_log_packet(unsigned int pf,
               const struct nf_loginfo *loginfo,
               const char *prefix)
 {
+       register struct syslog_ns *syslog_ns;
+
+
+       syslog_ns = (struct syslog_ns *)0;
+       if (skb->dev)   /*another syslog_ns possible?   */
+               syslog_ns = skb->dev->syslog_ns;
+       syslog_ns = switch_syslog_ns(syslog_ns);
+
        if (!loginfo)
                loginfo = &default_loginfo;
 
@@ -408,8 +416,8 @@ ipt_log_packet(unsigned int pf,
        if (in && !out) {
                /* MAC logging for input chain only. */
                printk("MAC=");
-               if (skb->dev && skb->dev->hard_header_len
-                   && skb->mac_header != skb->network_header) {
+               if (skb->dev && skb->dev->hard_header_len &&
+                   skb->mac_header != skb->network_header) {
                        int i;
                        const unsigned char *p = skb_mac_header(skb);
                        for (i = 0; i < skb->dev->hard_header_len; i++,p++)
@@ -423,54 +431,49 @@ ipt_log_packet(unsigned int pf,
        dump_packet(loginfo, skb, 0);
        printk("\n");
        spin_unlock_bh(&log_lock);
+       (void) switch_syslog_ns(syslog_ns);
 }
 
 static unsigned int
-log_tg(struct sk_buff *skb, const struct net_device *in,
-       const struct net_device *out, unsigned int hooknum,
-       const struct xt_target *target, const void *targinfo)
+log_tg(struct sk_buff *skb, const struct xt_action_param *par)
 {
-       const struct ipt_log_info *loginfo = targinfo;
+       const struct ipt_log_info *loginfo = par->targinfo;
        struct nf_loginfo li;
 
        li.type = NF_LOG_TYPE_LOG;
        li.u.log.level = loginfo->level;
        li.u.log.logflags = loginfo->logflags;
 
-       ipt_log_packet(PF_INET, hooknum, skb, in, out, &li,
+       ipt_log_packet(NFPROTO_IPV4, par->hooknum, skb, par->in, par->out, &li,
                       loginfo->prefix);
        return XT_CONTINUE;
 }
 
-static bool
-log_tg_check(const char *tablename, const void *e,
-             const struct xt_target *target, void *targinfo,
-             unsigned int hook_mask)
+static int log_tg_check(const struct xt_tgchk_param *par)
 {
-       const struct ipt_log_info *loginfo = targinfo;
+       const struct ipt_log_info *loginfo = par->targinfo;
 
        if (loginfo->level >= 8) {
-               pr_debug("LOG: level %u >= 8\n", loginfo->level);
-               return false;
+               pr_debug("level %u >= 8\n", loginfo->level);
+               return -EINVAL;
        }
        if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
-               pr_debug("LOG: prefix term %i\n",
-                        loginfo->prefix[sizeof(loginfo->prefix)-1]);
-               return false;
+               pr_debug("prefix is not null-terminated\n");
+               return -EINVAL;
        }
-       return true;
+       return 0;
 }
 
 static struct xt_target log_tg_reg __read_mostly = {
        .name           = "LOG",
-       .family         = AF_INET,
+       .family         = NFPROTO_IPV4,
        .target         = log_tg,
        .targetsize     = sizeof(struct ipt_log_info),
        .checkentry     = log_tg_check,
        .me             = THIS_MODULE,
 };
 
-static const struct nf_logger ipt_log_logger ={
+static struct nf_logger ipt_log_logger __read_mostly = {
        .name           = "ipt_LOG",
        .logfn          = &ipt_log_packet,
        .me             = THIS_MODULE,
@@ -483,7 +486,7 @@ static int __init log_tg_init(void)
        ret = xt_register_target(&log_tg_reg);
        if (ret < 0)
                return ret;
-       nf_log_register(PF_INET, &ipt_log_logger);
+       nf_log_register(NFPROTO_IPV4, &ipt_log_logger);
        return 0;
 }