]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - net/netfilter/nf_conntrack_netlink.c
netfilter: conntrack: simplify event caching system
[karo-tx-linux.git] / net / netfilter / nf_conntrack_netlink.c
index c523f0b8cee53bd9e304af4db77a4a474c44bcbe..b1b9e4fb7dedb53356f922809db9cc58dad83ea7 100644 (file)
@@ -346,23 +346,21 @@ nla_put_failure:
        return -1;
 }
 
-#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
-
 static int
 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
-                   int event, int nowait,
-                   const struct nf_conn *ct)
+                   int event, const struct nf_conn *ct)
 {
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
        struct nlattr *nest_parms;
-       unsigned char *b = skb_tail_pointer(skb);
+       unsigned int flags = pid ? NLM_F_MULTI : 0;
 
        event |= NFNL_SUBSYS_CTNETLINK << 8;
-       nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
-       nfmsg  = NLMSG_DATA(nlh);
+       nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
-       nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
+       nfmsg = nlmsg_data(nlh);
        nfmsg->nfgen_family = nf_ct_l3num(ct);
        nfmsg->version      = NFNETLINK_V0;
        nfmsg->res_id       = 0;
@@ -370,14 +368,14 @@ ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
        nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
        if (!nest_parms)
                goto nla_put_failure;
-       if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
+       if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
                goto nla_put_failure;
        nla_nest_end(skb, nest_parms);
 
        nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
        if (!nest_parms)
                goto nla_put_failure;
-       if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
+       if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
                goto nla_put_failure;
        nla_nest_end(skb, nest_parms);
 
@@ -395,86 +393,65 @@ ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
            ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
                goto nla_put_failure;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       nlmsg_end(skb, nlh);
        return skb->len;
 
 nlmsg_failure:
 nla_put_failure:
-       nlmsg_trim(skb, b);
+       nlmsg_cancel(skb, nlh);
        return -1;
 }
 
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
-/*
- * The general structure of a ctnetlink event is
- *
- *  CTA_TUPLE_ORIG
- *    <l3/l4-proto-attributes>
- *  CTA_TUPLE_REPLY
- *    <l3/l4-proto-attributes>
- *  CTA_ID
- *  ...
- *  CTA_PROTOINFO
- *    <l4-proto-attributes>
- *  CTA_TUPLE_MASTER
- *    <l3/l4-proto-attributes>
- *
- * Therefore the formular is
- *
- *   size = sizeof(headers) + sizeof(generic_nlas) + 3 * sizeof(tuple_nlas)
- *             + sizeof(protoinfo_nlas)
- */
-static struct sk_buff *
-ctnetlink_alloc_skb(const struct nf_conntrack_tuple *tuple, gfp_t gfp)
+static inline size_t
+ctnetlink_proto_size(const struct nf_conn *ct)
 {
        struct nf_conntrack_l3proto *l3proto;
        struct nf_conntrack_l4proto *l4proto;
-       int len;
-
-#define NLA_TYPE_SIZE(type)            nla_total_size(sizeof(type))
-
-       /* proto independant part */
-       len = NLMSG_SPACE(sizeof(struct nfgenmsg))
-               + 3 * nla_total_size(0)         /* CTA_TUPLE_ORIG|REPL|MASTER */
-               + 3 * nla_total_size(0)         /* CTA_TUPLE_IP */
-               + 3 * nla_total_size(0)         /* CTA_TUPLE_PROTO */
-               + 3 * NLA_TYPE_SIZE(u_int8_t)   /* CTA_PROTO_NUM */
-               + NLA_TYPE_SIZE(u_int32_t)      /* CTA_ID */
-               + NLA_TYPE_SIZE(u_int32_t)      /* CTA_STATUS */
+       size_t len = 0;
+
+       rcu_read_lock();
+       l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
+       len += l3proto->nla_size;
+
+       l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
+       len += l4proto->nla_size;
+       rcu_read_unlock();
+
+       return len;
+}
+
+static inline size_t
+ctnetlink_nlmsg_size(const struct nf_conn *ct)
+{
+       return NLMSG_ALIGN(sizeof(struct nfgenmsg))
+              + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
+              + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
+              + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
+              + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
+              + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
+              + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
 #ifdef CONFIG_NF_CT_ACCT
-               + 2 * nla_total_size(0)         /* CTA_COUNTERS_ORIG|REPL */
-               + 2 * NLA_TYPE_SIZE(uint64_t)   /* CTA_COUNTERS_PACKETS */
-               + 2 * NLA_TYPE_SIZE(uint64_t)   /* CTA_COUNTERS_BYTES */
+              + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
+              + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
+              + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
 #endif
-               + NLA_TYPE_SIZE(u_int32_t)      /* CTA_TIMEOUT */
-               + nla_total_size(0)             /* CTA_PROTOINFO */
-               + nla_total_size(0)             /* CTA_HELP */
-               + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
+              + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
+              + nla_total_size(0) /* CTA_PROTOINFO */
+              + nla_total_size(0) /* CTA_HELP */
+              + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
 #ifdef CONFIG_NF_CONNTRACK_SECMARK
-               + NLA_TYPE_SIZE(u_int32_t)      /* CTA_SECMARK */
+              + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
 #endif
 #ifdef CONFIG_NF_NAT_NEEDED
-               + 2 * nla_total_size(0)         /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
-               + 2 * NLA_TYPE_SIZE(u_int32_t)  /* CTA_NAT_SEQ_CORRECTION_POS */
-               + 2 * NLA_TYPE_SIZE(u_int32_t)  /* CTA_NAT_SEQ_CORRECTION_BEFORE */
-               + 2 * NLA_TYPE_SIZE(u_int32_t)  /* CTA_NAT_SEQ_CORRECTION_AFTER */
+              + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
+              + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
 #endif
 #ifdef CONFIG_NF_CONNTRACK_MARK
-               + NLA_TYPE_SIZE(u_int32_t)      /* CTA_MARK */
+              + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
 #endif
-               ;
-
-#undef NLA_TYPE_SIZE
-
-       rcu_read_lock();
-       l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
-       len += l3proto->nla_size;
-
-       l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
-       len += l4proto->nla_size;
-       rcu_read_unlock();
-
-       return alloc_skb(len, gfp);
+              + ctnetlink_proto_size(ct)
+              ;
 }
 
 static int ctnetlink_conntrack_event(struct notifier_block *this,
@@ -487,7 +464,6 @@ static int ctnetlink_conntrack_event(struct notifier_block *this,
        struct nf_conn *ct = item->ct;
        struct sk_buff *skb;
        unsigned int type;
-       sk_buff_data_t b;
        unsigned int flags = 0, group;
 
        /* ignore our fake conntrack entry */
@@ -501,7 +477,7 @@ static int ctnetlink_conntrack_event(struct notifier_block *this,
                type = IPCTNL_MSG_CT_NEW;
                flags = NLM_F_CREATE|NLM_F_EXCL;
                group = NFNLGRP_CONNTRACK_NEW;
-       } else  if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
+       } else  if (events) {
                type = IPCTNL_MSG_CT_NEW;
                group = NFNLGRP_CONNTRACK_UPDATE;
        } else
@@ -510,17 +486,16 @@ static int ctnetlink_conntrack_event(struct notifier_block *this,
        if (!item->report && !nfnetlink_has_listeners(group))
                return NOTIFY_DONE;
 
-       skb = ctnetlink_alloc_skb(tuple(ct, IP_CT_DIR_ORIGINAL), GFP_ATOMIC);
-       if (!skb)
+       skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
+       if (skb == NULL)
                goto errout;
 
-       b = skb->tail;
-
        type |= NFNL_SUBSYS_CTNETLINK << 8;
-       nlh   = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
-       nfmsg = NLMSG_DATA(nlh);
+       nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
-       nlh->nlmsg_flags    = flags;
+       nfmsg = nlmsg_data(nlh);
        nfmsg->nfgen_family = nf_ct_l3num(ct);
        nfmsg->version  = NFNETLINK_V0;
        nfmsg->res_id   = 0;
@@ -529,14 +504,14 @@ static int ctnetlink_conntrack_event(struct notifier_block *this,
        nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
        if (!nest_parms)
                goto nla_put_failure;
-       if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
+       if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
                goto nla_put_failure;
        nla_nest_end(skb, nest_parms);
 
        nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
        if (!nest_parms)
                goto nla_put_failure;
-       if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
+       if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
                goto nla_put_failure;
        nla_nest_end(skb, nest_parms);
 
@@ -584,12 +559,13 @@ static int ctnetlink_conntrack_event(struct notifier_block *this,
 #endif
        rcu_read_unlock();
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
        nfnetlink_send(skb, item->pid, group, item->report);
        return NOTIFY_DONE;
 
 nla_put_failure:
        rcu_read_unlock();
+       nlmsg_cancel(skb, nlh);
 nlmsg_failure:
        kfree_skb(skb);
 errout:
@@ -611,7 +587,7 @@ ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
        struct nf_conn *ct, *last;
        struct nf_conntrack_tuple_hash *h;
        struct hlist_nulls_node *n;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
        u_int8_t l3proto = nfmsg->nfgen_family;
 
        rcu_read_lock();
@@ -637,8 +613,7 @@ restart:
                        }
                        if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
                                                cb->nlh->nlmsg_seq,
-                                               IPCTNL_MSG_CT_NEW,
-                                               1, ct) < 0) {
+                                               IPCTNL_MSG_CT_NEW, ct) < 0) {
                                cb->args[1] = (unsigned long)ct;
                                goto out;
                        }
@@ -792,7 +767,7 @@ ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
        struct nf_conntrack_tuple_hash *h;
        struct nf_conntrack_tuple tuple;
        struct nf_conn *ct;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        u_int8_t u3 = nfmsg->nfgen_family;
        int err = 0;
 
@@ -802,9 +777,9 @@ ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
        else {
                /* Flush the whole table */
-               nf_conntrack_flush(&init_net, 
-                                  NETLINK_CB(skb).pid, 
-                                  nlmsg_report(nlh));
+               nf_conntrack_flush_report(&init_net,
+                                        NETLINK_CB(skb).pid,
+                                        nlmsg_report(nlh));
                return 0;
        }
 
@@ -847,7 +822,7 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
        struct nf_conntrack_tuple tuple;
        struct nf_conn *ct;
        struct sk_buff *skb2 = NULL;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        u_int8_t u3 = nfmsg->nfgen_family;
        int err = 0;
 
@@ -872,15 +847,15 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
        ct = nf_ct_tuplehash_to_ctrack(h);
 
        err = -ENOMEM;
-       skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
-       if (!skb2) {
+       skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+       if (skb2 == NULL) {
                nf_ct_put(ct);
                return -ENOMEM;
        }
 
        rcu_read_lock();
        err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
-                                 IPCTNL_MSG_CT_NEW, 1, ct);
+                                 IPCTNL_MSG_CT_NEW, ct);
        rcu_read_unlock();
        nf_ct_put(ct);
        if (err <= 0)
@@ -1325,7 +1300,7 @@ ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
 {
        struct nf_conntrack_tuple otuple, rtuple;
        struct nf_conntrack_tuple_hash *h = NULL;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        u_int8_t u3 = nfmsg->nfgen_family;
        int err = 0;
 
@@ -1503,19 +1478,18 @@ nla_put_failure:
 
 static int
 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
-                   int event,
-                   int nowait,
-                   const struct nf_conntrack_expect *exp)
+                       int event, const struct nf_conntrack_expect *exp)
 {
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
-       unsigned char *b = skb_tail_pointer(skb);
+       unsigned int flags = pid ? NLM_F_MULTI : 0;
 
        event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
-       nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
-       nfmsg  = NLMSG_DATA(nlh);
+       nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
-       nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
+       nfmsg = nlmsg_data(nlh);
        nfmsg->nfgen_family = exp->tuple.src.l3num;
        nfmsg->version      = NFNETLINK_V0;
        nfmsg->res_id       = 0;
@@ -1523,12 +1497,12 @@ ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
        if (ctnetlink_exp_dump_expect(skb, exp) < 0)
                goto nla_put_failure;
 
-       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       nlmsg_end(skb, nlh);
        return skb->len;
 
 nlmsg_failure:
 nla_put_failure:
-       nlmsg_trim(skb, b);
+       nlmsg_cancel(skb, nlh);
        return -1;
 }
 
@@ -1542,7 +1516,6 @@ static int ctnetlink_expect_event(struct notifier_block *this,
        struct nf_conntrack_expect *exp = item->exp;
        struct sk_buff *skb;
        unsigned int type;
-       sk_buff_data_t b;
        int flags = 0;
 
        if (events & IPEXP_NEW) {
@@ -1555,17 +1528,16 @@ static int ctnetlink_expect_event(struct notifier_block *this,
            !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
                return NOTIFY_DONE;
 
-       skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (!skb)
+       skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
+       if (skb == NULL)
                goto errout;
 
-       b = skb->tail;
-
        type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
-       nlh   = NLMSG_PUT(skb, item->pid, 0, type, sizeof(struct nfgenmsg));
-       nfmsg = NLMSG_DATA(nlh);
+       nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
+       if (nlh == NULL)
+               goto nlmsg_failure;
 
-       nlh->nlmsg_flags    = flags;
+       nfmsg = nlmsg_data(nlh);
        nfmsg->nfgen_family = exp->tuple.src.l3num;
        nfmsg->version      = NFNETLINK_V0;
        nfmsg->res_id       = 0;
@@ -1575,12 +1547,13 @@ static int ctnetlink_expect_event(struct notifier_block *this,
                goto nla_put_failure;
        rcu_read_unlock();
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlmsg_end(skb, nlh);
        nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
        return NOTIFY_DONE;
 
 nla_put_failure:
        rcu_read_unlock();
+       nlmsg_cancel(skb, nlh);
 nlmsg_failure:
        kfree_skb(skb);
 errout:
@@ -1600,7 +1573,7 @@ ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 {
        struct net *net = &init_net;
        struct nf_conntrack_expect *exp, *last;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
        struct hlist_node *n;
        u_int8_t l3proto = nfmsg->nfgen_family;
 
@@ -1617,10 +1590,11 @@ restart:
                                        continue;
                                cb->args[1] = 0;
                        }
-                       if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
+                       if (ctnetlink_exp_fill_info(skb,
+                                                   NETLINK_CB(cb->skb).pid,
                                                    cb->nlh->nlmsg_seq,
                                                    IPCTNL_MSG_EXP_NEW,
-                                                   1, exp) < 0) {
+                                                   exp) < 0) {
                                if (!atomic_inc_not_zero(&exp->use))
                                        continue;
                                cb->args[1] = (unsigned long)exp;
@@ -1652,7 +1626,7 @@ ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
        struct nf_conntrack_tuple tuple;
        struct nf_conntrack_expect *exp;
        struct sk_buff *skb2;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        u_int8_t u3 = nfmsg->nfgen_family;
        int err = 0;
 
@@ -1683,14 +1657,13 @@ ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
        }
 
        err = -ENOMEM;
-       skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
-       if (!skb2)
+       skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+       if (skb2 == NULL)
                goto out;
 
        rcu_read_lock();
        err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
-                                     nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
-                                     1, exp);
+                                     nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
        rcu_read_unlock();
        if (err <= 0)
                goto free;
@@ -1713,7 +1686,7 @@ ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
        struct nf_conntrack_expect *exp;
        struct nf_conntrack_tuple tuple;
        struct nf_conntrack_helper *h;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        struct hlist_node *n, *next;
        u_int8_t u3 = nfmsg->nfgen_family;
        unsigned int i;
@@ -1854,7 +1827,7 @@ ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
 {
        struct nf_conntrack_tuple tuple;
        struct nf_conntrack_expect *exp;
-       struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
+       struct nfgenmsg *nfmsg = nlmsg_data(nlh);
        u_int8_t u3 = nfmsg->nfgen_family;
        int err = 0;