]> nv-tegra.nvidia Code Review - linux-2.6.git/blobdiff - net/netfilter/nf_conntrack_netlink.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/cmarinas...
[linux-2.6.git] / net / netfilter / nf_conntrack_netlink.c
index 349bbefe5517682fd8d5592472606454a3282f71..482e90c6185087f2827b9f27448c74ac87eb0ef6 100644 (file)
 #include <linux/rculist_nulls.h>
 #include <linux/types.h>
 #include <linux/timer.h>
+#include <linux/security.h>
 #include <linux/skbuff.h>
 #include <linux/errno.h>
 #include <linux/netlink.h>
 #include <linux/spinlock.h>
 #include <linux/interrupt.h>
-#include <linux/notifier.h>
+#include <linux/slab.h>
 
 #include <linux/netfilter.h>
 #include <net/netlink.h>
+#include <net/sock.h>
 #include <net/netfilter/nf_conntrack.h>
 #include <net/netfilter/nf_conntrack_core.h>
 #include <net/netfilter/nf_conntrack_expect.h>
@@ -39,6 +41,8 @@
 #include <net/netfilter/nf_conntrack_l4proto.h>
 #include <net/netfilter/nf_conntrack_tuple.h>
 #include <net/netfilter/nf_conntrack_acct.h>
+#include <net/netfilter/nf_conntrack_zones.h>
+#include <net/netfilter/nf_conntrack_timestamp.h>
 #ifdef CONFIG_NF_NAT_NEEDED
 #include <net/netfilter/nf_nat_core.h>
 #include <net/netfilter/nf_nat_protocol.h>
@@ -144,7 +148,7 @@ nla_put_failure:
 }
 
 static inline int
-ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
+ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
 {
        struct nf_conntrack_l4proto *l4proto;
        struct nlattr *nest_proto;
@@ -227,6 +231,33 @@ nla_put_failure:
        return -1;
 }
 
+static int
+ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
+{
+       struct nlattr *nest_count;
+       const struct nf_conn_tstamp *tstamp;
+
+       tstamp = nf_conn_tstamp_find(ct);
+       if (!tstamp)
+               return 0;
+
+       nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
+       if (!nest_count)
+               goto nla_put_failure;
+
+       NLA_PUT_BE64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start));
+       if (tstamp->stop != 0) {
+               NLA_PUT_BE64(skb, CTA_TIMESTAMP_STOP,
+                            cpu_to_be64(tstamp->stop));
+       }
+       nla_nest_end(skb, nest_count);
+
+       return 0;
+
+nla_put_failure:
+       return -1;
+}
+
 #ifdef CONFIG_NF_CONNTRACK_MARK
 static inline int
 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
@@ -243,16 +274,31 @@ nla_put_failure:
 
 #ifdef CONFIG_NF_CONNTRACK_SECMARK
 static inline int
-ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
+ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
 {
-       NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
-       return 0;
+       struct nlattr *nest_secctx;
+       int len, ret;
+       char *secctx;
 
+       ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
+       if (ret)
+               return 0;
+
+       ret = -1;
+       nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
+       if (!nest_secctx)
+               goto nla_put_failure;
+
+       NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
+       nla_nest_end(skb, nest_secctx);
+
+       ret = 0;
 nla_put_failure:
-       return -1;
+       security_release_secctx(secctx, len);
+       return ret;
 }
 #else
-#define ctnetlink_dump_secmark(a, b) (0)
+#define ctnetlink_dump_secctx(a, b) (0)
 #endif
 
 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
@@ -346,23 +392,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, 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,85 +414,175 @@ 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);
 
+       if (nf_ct_zone(ct))
+               NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
+
        if (ctnetlink_dump_status(skb, ct) < 0 ||
            ctnetlink_dump_timeout(skb, ct) < 0 ||
            ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
            ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
+           ctnetlink_dump_timestamp(skb, ct) < 0 ||
            ctnetlink_dump_protoinfo(skb, ct) < 0 ||
            ctnetlink_dump_helpinfo(skb, ct) < 0 ||
            ctnetlink_dump_mark(skb, ct) < 0 ||
-           ctnetlink_dump_secmark(skb, ct) < 0 ||
+           ctnetlink_dump_secctx(skb, ct) < 0 ||
            ctnetlink_dump_id(skb, ct) < 0 ||
            ctnetlink_dump_use(skb, ct) < 0 ||
            ctnetlink_dump_master(skb, ct) < 0 ||
            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
-static int ctnetlink_conntrack_event(struct notifier_block *this,
-                                    unsigned long events, void *ptr)
+static inline size_t
+ctnetlink_proto_size(const struct nf_conn *ct)
+{
+       struct nf_conntrack_l3proto *l3proto;
+       struct nf_conntrack_l4proto *l4proto;
+       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_counters_size(const struct nf_conn *ct)
+{
+       if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
+               return 0;
+       return 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 */
+              ;
+}
+
+static inline int
+ctnetlink_secctx_size(const struct nf_conn *ct)
 {
+#ifdef CONFIG_NF_CONNTRACK_SECMARK
+       int len, ret;
+
+       ret = security_secid_to_secctx(ct->secmark, NULL, &len);
+       if (ret)
+               return 0;
+
+       return nla_total_size(0) /* CTA_SECCTX */
+              + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
+#else
+       return 0;
+#endif
+}
+
+static inline size_t
+ctnetlink_timestamp_size(const struct nf_conn *ct)
+{
+#ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
+       if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
+               return 0;
+       return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
+#else
+       return 0;
+#endif
+}
+
+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 */
+              + ctnetlink_counters_size(ct)
+              + ctnetlink_timestamp_size(ct)
+              + 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 */
+              + ctnetlink_secctx_size(ct)
+#ifdef CONFIG_NF_NAT_NEEDED
+              + 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_total_size(sizeof(u_int32_t)) /* CTA_MARK */
+#endif
+              + ctnetlink_proto_size(ct)
+              ;
+}
+
+static int
+ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
+{
+       struct net *net;
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
        struct nlattr *nest_parms;
-       struct nf_ct_event *item = (struct nf_ct_event *)ptr;
        struct nf_conn *ct = item->ct;
        struct sk_buff *skb;
        unsigned int type;
-       sk_buff_data_t b;
        unsigned int flags = 0, group;
+       int err;
 
        /* ignore our fake conntrack entry */
-       if (ct == &nf_conntrack_untracked)
-               return NOTIFY_DONE;
+       if (nf_ct_is_untracked(ct))
+               return 0;
 
-       if (events & IPCT_DESTROY) {
+       if (events & (1 << IPCT_DESTROY)) {
                type = IPCTNL_MSG_CT_DELETE;
                group = NFNLGRP_CONNTRACK_DESTROY;
-       } else  if (events & (IPCT_NEW | IPCT_RELATED)) {
+       } else  if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
                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
-               return NOTIFY_DONE;
-
-       if (!item->report && !nfnetlink_has_listeners(group))
-               return NOTIFY_DONE;
+               return 0;
 
-       skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (!skb)
-               return NOTIFY_DONE;
+       net = nf_ct_net(ct);
+       if (!item->report && !nfnetlink_has_listeners(net, group))
+               return 0;
 
-       b = skb->tail;
+       skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
+       if (skb == NULL)
+               goto errout;
 
        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;
@@ -457,71 +591,83 @@ 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);
 
+       if (nf_ct_zone(ct))
+               NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
+
        if (ctnetlink_dump_id(skb, ct) < 0)
                goto nla_put_failure;
 
        if (ctnetlink_dump_status(skb, ct) < 0)
                goto nla_put_failure;
 
-       if (events & IPCT_DESTROY) {
+       if (events & (1 << IPCT_DESTROY)) {
                if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
-                   ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
+                   ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
+                   ctnetlink_dump_timestamp(skb, ct) < 0)
                        goto nla_put_failure;
        } else {
                if (ctnetlink_dump_timeout(skb, ct) < 0)
                        goto nla_put_failure;
 
-               if (events & IPCT_PROTOINFO
+               if (events & (1 << IPCT_PROTOINFO)
                    && ctnetlink_dump_protoinfo(skb, ct) < 0)
                        goto nla_put_failure;
 
-               if ((events & IPCT_HELPER || nfct_help(ct))
+               if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
                    && ctnetlink_dump_helpinfo(skb, ct) < 0)
                        goto nla_put_failure;
 
 #ifdef CONFIG_NF_CONNTRACK_SECMARK
-               if ((events & IPCT_SECMARK || ct->secmark)
-                   && ctnetlink_dump_secmark(skb, ct) < 0)
+               if ((events & (1 << IPCT_SECMARK) || ct->secmark)
+                   && ctnetlink_dump_secctx(skb, ct) < 0)
                        goto nla_put_failure;
 #endif
 
-               if (events & IPCT_RELATED &&
+               if (events & (1 << IPCT_RELATED) &&
                    ctnetlink_dump_master(skb, ct) < 0)
                        goto nla_put_failure;
 
-               if (events & IPCT_NATSEQADJ &&
+               if (events & (1 << IPCT_NATSEQADJ) &&
                    ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
                        goto nla_put_failure;
        }
 
 #ifdef CONFIG_NF_CONNTRACK_MARK
-       if ((events & IPCT_MARK || ct->mark)
+       if ((events & (1 << IPCT_MARK) || ct->mark)
            && ctnetlink_dump_mark(skb, ct) < 0)
                goto nla_put_failure;
 #endif
        rcu_read_unlock();
 
-       nlh->nlmsg_len = skb->tail - b;
-       nfnetlink_send(skb, item->pid, group, item->report);
-       return NOTIFY_DONE;
+       nlmsg_end(skb, nlh);
+       err = nfnetlink_send(skb, net, item->pid, group, item->report,
+                            GFP_ATOMIC);
+       if (err == -ENOBUFS || err == -EAGAIN)
+               return -ENOBUFS;
+
+       return 0;
 
 nla_put_failure:
        rcu_read_unlock();
+       nlmsg_cancel(skb, nlh);
 nlmsg_failure:
-       nfnetlink_set_err(0, group, -ENOBUFS);
        kfree_skb(skb);
-       return NOTIFY_DONE;
+errout:
+       if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
+               return -ENOBUFS;
+
+       return 0;
 }
 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
 
@@ -535,37 +681,36 @@ static int ctnetlink_done(struct netlink_callback *cb)
 static int
 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       struct net *net = sock_net(skb->sk);
        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();
+       spin_lock_bh(&nf_conntrack_lock);
        last = (struct nf_conn *)cb->args[1];
-       for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
+       for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
 restart:
-               hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
+               hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
                                         hnnode) {
                        if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
                                continue;
                        ct = nf_ct_tuplehash_to_ctrack(h);
-                       if (!atomic_inc_not_zero(&ct->ct_general.use))
-                               continue;
                        /* Dump entries of a given L3 protocol number.
                         * If it is not specified, ie. l3proto == 0,
                         * then dump everything. */
                        if (l3proto && nf_ct_l3num(ct) != l3proto)
-                               goto releasect;
+                               continue;
                        if (cb->args[1]) {
                                if (ct != last)
-                                       goto releasect;
+                                       continue;
                                cb->args[1] = 0;
                        }
                        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) {
+                               nf_conntrack_get(&ct->ct_general);
                                cb->args[1] = (unsigned long)ct;
                                goto out;
                        }
@@ -578,8 +723,6 @@ restart:
                                if (acct)
                                        memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
                        }
-releasect:
-               nf_ct_put(ct);
                }
                if (cb->args[1]) {
                        cb->args[1] = 0;
@@ -587,7 +730,7 @@ releasect:
                }
        }
 out:
-       rcu_read_unlock();
+       spin_unlock_bh(&nf_conntrack_lock);
        if (last)
                nf_ct_put(last);
 
@@ -653,16 +796,22 @@ ctnetlink_parse_tuple_proto(struct nlattr *attr,
        return ret;
 }
 
+static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
+       [CTA_TUPLE_IP]          = { .type = NLA_NESTED },
+       [CTA_TUPLE_PROTO]       = { .type = NLA_NESTED },
+};
+
 static int
-ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
-                     enum ctattr_tuple type, u_int8_t l3num)
+ctnetlink_parse_tuple(const struct nlattr * const cda[],
+                     struct nf_conntrack_tuple *tuple,
+                     enum ctattr_type type, u_int8_t l3num)
 {
        struct nlattr *tb[CTA_TUPLE_MAX+1];
        int err;
 
        memset(tuple, 0, sizeof(*tuple));
 
-       nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
+       nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
 
        if (!tb[CTA_TUPLE_IP])
                return -EINVAL;
@@ -689,12 +838,31 @@ ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
        return 0;
 }
 
+static int
+ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
+{
+       if (attr)
+#ifdef CONFIG_NF_CONNTRACK_ZONES
+               *zone = ntohs(nla_get_be16(attr));
+#else
+               return -EOPNOTSUPP;
+#endif
+       else
+               *zone = 0;
+
+       return 0;
+}
+
+static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
+       [CTA_HELP_NAME]         = { .type = NLA_NUL_STRING },
+};
+
 static inline int
-ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
+ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
 {
        struct nlattr *tb[CTA_HELP_MAX+1];
 
-       nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
+       nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
 
        if (!tb[CTA_HELP_NAME])
                return -EINVAL;
@@ -705,23 +873,37 @@ ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
 }
 
 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
+       [CTA_TUPLE_ORIG]        = { .type = NLA_NESTED },
+       [CTA_TUPLE_REPLY]       = { .type = NLA_NESTED },
        [CTA_STATUS]            = { .type = NLA_U32 },
+       [CTA_PROTOINFO]         = { .type = NLA_NESTED },
+       [CTA_HELP]              = { .type = NLA_NESTED },
+       [CTA_NAT_SRC]           = { .type = NLA_NESTED },
        [CTA_TIMEOUT]           = { .type = NLA_U32 },
        [CTA_MARK]              = { .type = NLA_U32 },
-       [CTA_USE]               = { .type = NLA_U32 },
        [CTA_ID]                = { .type = NLA_U32 },
+       [CTA_NAT_DST]           = { .type = NLA_NESTED },
+       [CTA_TUPLE_MASTER]      = { .type = NLA_NESTED },
+       [CTA_ZONE]              = { .type = NLA_U16 },
 };
 
 static int
 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
-                       struct nlmsghdr *nlh, struct nlattr *cda[])
+                       const struct nlmsghdr *nlh,
+                       const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        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;
+       u16 zone;
+       int err;
+
+       err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
+       if (err < 0)
+               return err;
 
        if (cda[CTA_TUPLE_ORIG])
                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
@@ -729,16 +911,16 @@ 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(net,
+                                        NETLINK_CB(skb).pid,
+                                        nlmsg_report(nlh));
                return 0;
        }
 
        if (err < 0)
                return err;
 
-       h = nf_conntrack_find_get(&init_net, &tuple);
+       h = nf_conntrack_find_get(net, zone, &tuple);
        if (!h)
                return -ENOENT;
 
@@ -752,10 +934,15 @@ ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
                }
        }
 
-       nf_conntrack_event_report(IPCT_DESTROY,
-                                 ct,
-                                 NETLINK_CB(skb).pid,
-                                 nlmsg_report(nlh));
+       if (nf_conntrack_event_report(IPCT_DESTROY, ct,
+                                     NETLINK_CB(skb).pid,
+                                     nlmsg_report(nlh)) < 0) {
+               nf_ct_delete_from_lists(ct);
+               /* we failed to report the event, try later */
+               nf_ct_insert_dying_list(ct);
+               nf_ct_put(ct);
+               return 0;
+       }
 
        /* death_by_timeout would report the event again */
        set_bit(IPS_DYING_BIT, &ct->status);
@@ -768,20 +955,27 @@ ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
 
 static int
 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
-                       struct nlmsghdr *nlh, struct nlattr *cda[])
+                       const struct nlmsghdr *nlh,
+                       const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        struct nf_conntrack_tuple_hash *h;
        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;
+       u16 zone;
+       int err;
 
        if (nlh->nlmsg_flags & NLM_F_DUMP)
                return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
                                          ctnetlink_done);
 
+       err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
+       if (err < 0)
+               return err;
+
        if (cda[CTA_TUPLE_ORIG])
                err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
        else if (cda[CTA_TUPLE_REPLY])
@@ -792,22 +986,22 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
        if (err < 0)
                return err;
 
-       h = nf_conntrack_find_get(&init_net, &tuple);
+       h = nf_conntrack_find_get(net, zone, &tuple);
        if (!h)
                return -ENOENT;
 
        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)
@@ -822,14 +1016,15 @@ ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
 free:
        kfree_skb(skb2);
 out:
-       return err;
+       /* this avoids a loop in nfnetlink. */
+       return err == -EAGAIN ? -ENOBUFS : err;
 }
 
 #ifdef CONFIG_NF_NAT_NEEDED
 static int
 ctnetlink_parse_nat_setup(struct nf_conn *ct,
                          enum nf_nat_manip_type manip,
-                         struct nlattr *attr)
+                         const struct nlattr *attr)
 {
        typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
 
@@ -859,7 +1054,7 @@ ctnetlink_parse_nat_setup(struct nf_conn *ct,
 #endif
 
 static int
-ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
 {
        unsigned long d;
        unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
@@ -885,7 +1080,7 @@ ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
 }
 
 static int
-ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
 {
 #ifdef CONFIG_NF_NAT_NEEDED
        int ret;
@@ -911,11 +1106,11 @@ ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
 }
 
 static inline int
-ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
 {
        struct nf_conntrack_helper *helper;
        struct nf_conn_help *help = nfct_help(ct);
-       char *helpname;
+       char *helpname = NULL;
        int err;
 
        /* don't change helper of sibling connections */
@@ -936,7 +1131,8 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
                return 0;
        }
 
-       helper = __nf_conntrack_helper_find_byname(helpname);
+       helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
+                                           nf_ct_protonum(ct));
        if (helper == NULL) {
 #ifdef CONFIG_MODULES
                spin_unlock_bh(&nf_conntrack_lock);
@@ -947,7 +1143,8 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
                }
 
                spin_lock_bh(&nf_conntrack_lock);
-               helper = __nf_conntrack_helper_find_byname(helpname);
+               helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
+                                                   nf_ct_protonum(ct));
                if (helper)
                        return -EAGAIN;
 #endif
@@ -962,9 +1159,8 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
                /* need to zero data of old helper */
                memset(&help->help, 0, sizeof(help->help));
        } else {
-               help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
-               if (help == NULL)
-                       return -ENOMEM;
+               /* we cannot set a helper for an existing conntrack */
+               return -EOPNOTSUPP;
        }
 
        rcu_assign_pointer(help->helper, helper);
@@ -973,7 +1169,7 @@ ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
 }
 
 static inline int
-ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
 {
        u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
 
@@ -986,14 +1182,21 @@ ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
        return 0;
 }
 
+static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
+       [CTA_PROTOINFO_TCP]     = { .type = NLA_NESTED },
+       [CTA_PROTOINFO_DCCP]    = { .type = NLA_NESTED },
+       [CTA_PROTOINFO_SCTP]    = { .type = NLA_NESTED },
+};
+
 static inline int
-ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
 {
-       struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
+       const struct nlattr *attr = cda[CTA_PROTOINFO];
+       struct nlattr *tb[CTA_PROTOINFO_MAX+1];
        struct nf_conntrack_l4proto *l4proto;
        int err = 0;
 
-       nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
+       nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
 
        rcu_read_lock();
        l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
@@ -1005,12 +1208,18 @@ ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
 }
 
 #ifdef CONFIG_NF_NAT_NEEDED
+static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
+       [CTA_NAT_SEQ_CORRECTION_POS]    = { .type = NLA_U32 },
+       [CTA_NAT_SEQ_OFFSET_BEFORE]     = { .type = NLA_U32 },
+       [CTA_NAT_SEQ_OFFSET_AFTER]      = { .type = NLA_U32 },
+};
+
 static inline int
-change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
+change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
 {
        struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
 
-       nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
+       nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
 
        if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
                return -EINVAL;
@@ -1034,7 +1243,8 @@ change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
 }
 
 static int
-ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
+                            const struct nlattr * const cda[])
 {
        int ret = 0;
        struct nf_conn_nat *nat = nfct_nat(ct);
@@ -1065,7 +1275,8 @@ ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
 #endif
 
 static int
-ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
+ctnetlink_change_conntrack(struct nf_conn *ct,
+                          const struct nlattr * const cda[])
 {
        int err;
 
@@ -1113,30 +1324,9 @@ ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
        return 0;
 }
 
-static inline void
-ctnetlink_event_report(struct nf_conn *ct, u32 pid, int report)
-{
-       unsigned int events = 0;
-
-       if (test_bit(IPS_EXPECTED_BIT, &ct->status))
-               events |= IPCT_RELATED;
-       else
-               events |= IPCT_NEW;
-
-       nf_conntrack_event_report(IPCT_STATUS |
-                                 IPCT_HELPER |
-                                 IPCT_REFRESH |
-                                 IPCT_PROTOINFO |
-                                 IPCT_NATSEQADJ |
-                                 IPCT_MARK |
-                                 events,
-                                 ct,
-                                 pid,
-                                 report);
-}
-
 static struct nf_conn *
-ctnetlink_create_conntrack(struct nlattr *cda[],
+ctnetlink_create_conntrack(struct net *net, u16 zone,
+                          const struct nlattr * const cda[],
                           struct nf_conntrack_tuple *otuple,
                           struct nf_conntrack_tuple *rtuple,
                           u8 u3)
@@ -1144,8 +1334,9 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
        struct nf_conn *ct;
        int err = -EINVAL;
        struct nf_conntrack_helper *helper;
+       struct nf_conn_tstamp *tstamp;
 
-       ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
+       ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
        if (IS_ERR(ct))
                return ERR_PTR(-ENOMEM);
 
@@ -1154,17 +1345,17 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
        ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
 
        ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
-       ct->status |= IPS_CONFIRMED;
 
        rcu_read_lock();
        if (cda[CTA_HELP]) {
-               char *helpname;
+               char *helpname = NULL;
  
                err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
                if (err < 0)
                        goto err2;
 
-               helper = __nf_conntrack_helper_find_byname(helpname);
+               helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
+                                                   nf_ct_protonum(ct));
                if (helper == NULL) {
                        rcu_read_unlock();
 #ifdef CONFIG_MODULES
@@ -1174,7 +1365,9 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
                        }
 
                        rcu_read_lock();
-                       helper = __nf_conntrack_helper_find_byname(helpname);
+                       helper = __nf_conntrack_helper_find(helpname,
+                                                           nf_ct_l3num(ct),
+                                                           nf_ct_protonum(ct));
                        if (helper) {
                                err = -EAGAIN;
                                goto err2;
@@ -1197,19 +1390,25 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
                }
        } else {
                /* try an implicit helper assignation */
-               err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
+               err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
                if (err < 0)
                        goto err2;
        }
 
-       if (cda[CTA_STATUS]) {
-               err = ctnetlink_change_status(ct, cda);
+       if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
+               err = ctnetlink_change_nat(ct, cda);
                if (err < 0)
                        goto err2;
        }
 
-       if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
-               err = ctnetlink_change_nat(ct, cda);
+       nf_ct_acct_ext_add(ct, GFP_ATOMIC);
+       nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
+       nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
+       /* we must add conntrack extensions before confirmation. */
+       ct->status |= IPS_CONFIRMED;
+
+       if (cda[CTA_STATUS]) {
+               err = ctnetlink_change_status(ct, cda);
                if (err < 0)
                        goto err2;
        }
@@ -1222,14 +1421,13 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
        }
 #endif
 
+       memset(&ct->proto, 0, sizeof(ct->proto));
        if (cda[CTA_PROTOINFO]) {
                err = ctnetlink_change_protoinfo(ct, cda);
                if (err < 0)
                        goto err2;
        }
 
-       nf_ct_acct_ext_add(ct, GFP_ATOMIC);
-
 #if defined(CONFIG_NF_CONNTRACK_MARK)
        if (cda[CTA_MARK])
                ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
@@ -1245,7 +1443,7 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
                if (err < 0)
                        goto err2;
 
-               master_h = nf_conntrack_find_get(&init_net, &master);
+               master_h = nf_conntrack_find_get(net, zone, &master);
                if (master_h == NULL) {
                        err = -ENOENT;
                        goto err2;
@@ -1254,6 +1452,9 @@ ctnetlink_create_conntrack(struct nlattr *cda[],
                __set_bit(IPS_EXPECTED_BIT, &ct->status);
                ct->master = master_ct;
        }
+       tstamp = nf_conn_tstamp_find(ct);
+       if (tstamp)
+               tstamp->start = ktime_to_ns(ktime_get_real());
 
        add_timer(&ct->timeout);
        nf_conntrack_hash_insert(ct);
@@ -1270,13 +1471,20 @@ err1:
 
 static int
 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
-                       struct nlmsghdr *nlh, struct nlattr *cda[])
+                       const struct nlmsghdr *nlh,
+                       const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        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;
+       u16 zone;
+       int err;
+
+       err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
+       if (err < 0)
+               return err;
 
        if (cda[CTA_TUPLE_ORIG]) {
                err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
@@ -1292,16 +1500,17 @@ ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
 
        spin_lock_bh(&nf_conntrack_lock);
        if (cda[CTA_TUPLE_ORIG])
-               h = __nf_conntrack_find(&init_net, &otuple);
+               h = __nf_conntrack_find(net, zone, &otuple);
        else if (cda[CTA_TUPLE_REPLY])
-               h = __nf_conntrack_find(&init_net, &rtuple);
+               h = __nf_conntrack_find(net, zone, &rtuple);
 
        if (h == NULL) {
                err = -ENOENT;
                if (nlh->nlmsg_flags & NLM_F_CREATE) {
                        struct nf_conn *ct;
+                       enum ip_conntrack_events events;
 
-                       ct = ctnetlink_create_conntrack(cda, &otuple,
+                       ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
                                                        &rtuple, u3);
                        if (IS_ERR(ct)) {
                                err = PTR_ERR(ct);
@@ -1310,9 +1519,19 @@ ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
                        err = 0;
                        nf_conntrack_get(&ct->ct_general);
                        spin_unlock_bh(&nf_conntrack_lock);
-                       ctnetlink_event_report(ct,
-                                              NETLINK_CB(skb).pid,
-                                              nlmsg_report(nlh));
+                       if (test_bit(IPS_EXPECTED_BIT, &ct->status))
+                               events = IPCT_RELATED;
+                       else
+                               events = IPCT_NEW;
+
+                       nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
+                                                     (1 << IPCT_ASSURED) |
+                                                     (1 << IPCT_HELPER) |
+                                                     (1 << IPCT_PROTOINFO) |
+                                                     (1 << IPCT_NATSEQADJ) |
+                                                     (1 << IPCT_MARK) | events,
+                                                     ct, NETLINK_CB(skb).pid,
+                                                     nlmsg_report(nlh));
                        nf_ct_put(ct);
                } else
                        spin_unlock_bh(&nf_conntrack_lock);
@@ -1331,9 +1550,14 @@ ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
                if (err == 0) {
                        nf_conntrack_get(&ct->ct_general);
                        spin_unlock_bh(&nf_conntrack_lock);
-                       ctnetlink_event_report(ct,
-                                              NETLINK_CB(skb).pid,
-                                              nlmsg_report(nlh));
+                       nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
+                                                     (1 << IPCT_ASSURED) |
+                                                     (1 << IPCT_HELPER) |
+                                                     (1 << IPCT_PROTOINFO) |
+                                                     (1 << IPCT_NATSEQADJ) |
+                                                     (1 << IPCT_MARK),
+                                                     ct, NETLINK_CB(skb).pid,
+                                                     nlmsg_report(nlh));
                        nf_ct_put(ct);
                } else
                        spin_unlock_bh(&nf_conntrack_lock);
@@ -1382,8 +1606,9 @@ ctnetlink_exp_dump_mask(struct sk_buff *skb,
        struct nlattr *nest_parms;
 
        memset(&m, 0xFF, sizeof(m));
-       m.src.u.all = mask->src.u.all;
        memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
+       m.src.u.all = mask->src.u.all;
+       m.dst.protonum = tuple->dst.protonum;
 
        nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
        if (!nest_parms)
@@ -1414,6 +1639,7 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
 {
        struct nf_conn *master = exp->master;
        long timeout = (exp->timeout.expires - jiffies) / HZ;
+       struct nf_conn_help *help;
 
        if (timeout < 0)
                timeout = 0;
@@ -1429,6 +1655,15 @@ ctnetlink_exp_dump_expect(struct sk_buff *skb,
 
        NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
        NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
+       NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
+       help = nfct_help(master);
+       if (help) {
+               struct nf_conntrack_helper *helper;
+
+               helper = rcu_dereference(help->helper);
+               if (helper)
+                       NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
+       }
 
        return 0;
 
@@ -1438,19 +1673,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;
@@ -1458,49 +1692,50 @@ 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;
 }
 
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
-static int ctnetlink_expect_event(struct notifier_block *this,
-                                 unsigned long events, void *ptr)
+static int
+ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
 {
+       struct nf_conntrack_expect *exp = item->exp;
+       struct net *net = nf_ct_exp_net(exp);
        struct nlmsghdr *nlh;
        struct nfgenmsg *nfmsg;
-       struct nf_exp_event *item = (struct nf_exp_event *)ptr;
-       struct nf_conntrack_expect *exp = item->exp;
        struct sk_buff *skb;
-       unsigned int type;
-       sk_buff_data_t b;
+       unsigned int type, group;
        int flags = 0;
 
-       if (events & IPEXP_NEW) {
+       if (events & (1 << IPEXP_DESTROY)) {
+               type = IPCTNL_MSG_EXP_DELETE;
+               group = NFNLGRP_CONNTRACK_EXP_DESTROY;
+       } else if (events & (1 << IPEXP_NEW)) {
                type = IPCTNL_MSG_EXP_NEW;
                flags = NLM_F_CREATE|NLM_F_EXCL;
+               group = NFNLGRP_CONNTRACK_EXP_NEW;
        } else
-               return NOTIFY_DONE;
-
-       if (!item->report &&
-           !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
-               return NOTIFY_DONE;
+               return 0;
 
-       skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
-       if (!skb)
-               return NOTIFY_DONE;
+       if (!item->report && !nfnetlink_has_listeners(net, group))
+               return 0;
 
-       b = skb->tail;
+       skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
+       if (skb == NULL)
+               goto errout;
 
        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;
@@ -1510,16 +1745,18 @@ static int ctnetlink_expect_event(struct notifier_block *this,
                goto nla_put_failure;
        rcu_read_unlock();
 
-       nlh->nlmsg_len = skb->tail - b;
-       nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
-       return NOTIFY_DONE;
+       nlmsg_end(skb, nlh);
+       nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
+       return 0;
 
 nla_put_failure:
        rcu_read_unlock();
+       nlmsg_cancel(skb, nlh);
 nlmsg_failure:
-       nfnetlink_set_err(0, 0, -ENOBUFS);
        kfree_skb(skb);
-       return NOTIFY_DONE;
+errout:
+       nfnetlink_set_err(net, 0, 0, -ENOBUFS);
+       return 0;
 }
 #endif
 static int ctnetlink_exp_done(struct netlink_callback *cb)
@@ -1532,9 +1769,9 @@ static int ctnetlink_exp_done(struct netlink_callback *cb)
 static int
 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
 {
-       struct net *net = &init_net;
+       struct net *net = sock_net(skb->sk);
        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;
 
@@ -1551,10 +1788,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;
@@ -1575,20 +1813,29 @@ out:
 }
 
 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
+       [CTA_EXPECT_MASTER]     = { .type = NLA_NESTED },
+       [CTA_EXPECT_TUPLE]      = { .type = NLA_NESTED },
+       [CTA_EXPECT_MASK]       = { .type = NLA_NESTED },
        [CTA_EXPECT_TIMEOUT]    = { .type = NLA_U32 },
        [CTA_EXPECT_ID]         = { .type = NLA_U32 },
+       [CTA_EXPECT_HELP_NAME]  = { .type = NLA_NUL_STRING },
+       [CTA_EXPECT_ZONE]       = { .type = NLA_U16 },
+       [CTA_EXPECT_FLAGS]      = { .type = NLA_U32 },
 };
 
 static int
 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
-                    struct nlmsghdr *nlh, struct nlattr *cda[])
+                    const struct nlmsghdr *nlh,
+                    const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        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;
+       u16 zone;
+       int err;
 
        if (nlh->nlmsg_flags & NLM_F_DUMP) {
                return netlink_dump_start(ctnl, skb, nlh,
@@ -1596,6 +1843,10 @@ ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
                                          ctnetlink_exp_done);
        }
 
+       err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
+       if (err < 0)
+               return err;
+
        if (cda[CTA_EXPECT_MASTER])
                err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
        else
@@ -1604,7 +1855,7 @@ ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
        if (err < 0)
                return err;
 
-       exp = nf_ct_expect_find_get(&init_net, &tuple);
+       exp = nf_ct_expect_find_get(net, zone, &tuple);
        if (!exp)
                return -ENOENT;
 
@@ -1617,14 +1868,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;
@@ -1642,25 +1892,31 @@ out:
 
 static int
 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
-                    struct nlmsghdr *nlh, struct nlattr *cda[])
+                    const struct nlmsghdr *nlh,
+                    const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        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;
+       u16 zone;
        int err;
 
        if (cda[CTA_EXPECT_TUPLE]) {
                /* delete a single expect by tuple */
+               err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
+               if (err < 0)
+                       return err;
+
                err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
                if (err < 0)
                        return err;
 
                /* bump usage count to 2 */
-               exp = nf_ct_expect_find_get(&init_net, &tuple);
+               exp = nf_ct_expect_find_get(net, zone, &tuple);
                if (!exp)
                        return -ENOENT;
 
@@ -1673,7 +1929,13 @@ ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
                }
 
                /* after list removal, usage count == 1 */
-               nf_ct_unexpect_related(exp);
+               spin_lock_bh(&nf_conntrack_lock);
+               if (del_timer(&exp->timeout)) {
+                       nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
+                                                  nlmsg_report(nlh));
+                       nf_ct_expect_put(exp);
+               }
+               spin_unlock_bh(&nf_conntrack_lock);
                /* have to put what we 'get' above.
                 * after this line usage count == 0 */
                nf_ct_expect_put(exp);
@@ -1683,19 +1945,16 @@ ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
 
                /* delete all expectations for this helper */
                spin_lock_bh(&nf_conntrack_lock);
-               h = __nf_conntrack_helper_find_byname(name);
-               if (!h) {
-                       spin_unlock_bh(&nf_conntrack_lock);
-                       return -EOPNOTSUPP;
-               }
                for (i = 0; i < nf_ct_expect_hsize; i++) {
                        hlist_for_each_entry_safe(exp, n, next,
-                                                 &init_net.ct.expect_hash[i],
+                                                 &net->ct.expect_hash[i],
                                                  hnode) {
                                m_help = nfct_help(exp->master);
-                               if (m_help->helper == h
-                                   && del_timer(&exp->timeout)) {
-                                       nf_ct_unlink_expect(exp);
+                               if (!strcmp(m_help->helper->name, name) &&
+                                   del_timer(&exp->timeout)) {
+                                       nf_ct_unlink_expect_report(exp,
+                                                       NETLINK_CB(skb).pid,
+                                                       nlmsg_report(nlh));
                                        nf_ct_expect_put(exp);
                                }
                        }
@@ -1706,10 +1965,12 @@ ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
                spin_lock_bh(&nf_conntrack_lock);
                for (i = 0; i < nf_ct_expect_hsize; i++) {
                        hlist_for_each_entry_safe(exp, n, next,
-                                                 &init_net.ct.expect_hash[i],
+                                                 &net->ct.expect_hash[i],
                                                  hnode) {
                                if (del_timer(&exp->timeout)) {
-                                       nf_ct_unlink_expect(exp);
+                                       nf_ct_unlink_expect_report(exp,
+                                                       NETLINK_CB(skb).pid,
+                                                       nlmsg_report(nlh));
                                        nf_ct_expect_put(exp);
                                }
                        }
@@ -1720,13 +1981,17 @@ ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
        return 0;
 }
 static int
-ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
+ctnetlink_change_expect(struct nf_conntrack_expect *x,
+                       const struct nlattr * const cda[])
 {
        return -EOPNOTSUPP;
 }
 
 static int
-ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
+ctnetlink_create_expect(struct net *net, u16 zone,
+                       const struct nlattr * const cda[],
+                       u_int8_t u3,
+                       u32 pid, int report)
 {
        struct nf_conntrack_tuple tuple, mask, master_tuple;
        struct nf_conntrack_tuple_hash *h = NULL;
@@ -1747,26 +2012,39 @@ ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
                return err;
 
        /* Look for master conntrack of this expectation */
-       h = nf_conntrack_find_get(&init_net, &master_tuple);
+       h = nf_conntrack_find_get(net, zone, &master_tuple);
        if (!h)
                return -ENOENT;
        ct = nf_ct_tuplehash_to_ctrack(h);
-       help = nfct_help(ct);
-
-       if (!help || !help->helper) {
-               /* such conntrack hasn't got any helper, abort */
-               err = -EOPNOTSUPP;
-               goto out;
-       }
-
        exp = nf_ct_expect_alloc(ct);
        if (!exp) {
                err = -ENOMEM;
                goto out;
        }
+       help = nfct_help(ct);
+       if (!help) {
+               if (!cda[CTA_EXPECT_TIMEOUT]) {
+                       err = -EINVAL;
+                       goto out;
+               }
+               exp->timeout.expires =
+                 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
+
+               exp->flags = NF_CT_EXPECT_USERSPACE;
+               if (cda[CTA_EXPECT_FLAGS]) {
+                       exp->flags |=
+                               ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
+               }
+       } else {
+               if (cda[CTA_EXPECT_FLAGS]) {
+                       exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
+                       exp->flags &= ~NF_CT_EXPECT_USERSPACE;
+               } else
+                       exp->flags = 0;
+       }
 
+       exp->class = 0;
        exp->expectfn = NULL;
-       exp->flags = 0;
        exp->master = ct;
        exp->helper = NULL;
        memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
@@ -1783,31 +2061,38 @@ out:
 
 static int
 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
-                    struct nlmsghdr *nlh, struct nlattr *cda[])
+                    const struct nlmsghdr *nlh,
+                    const struct nlattr * const cda[])
 {
+       struct net *net = sock_net(ctnl);
        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;
+       u16 zone;
+       int err;
 
        if (!cda[CTA_EXPECT_TUPLE]
            || !cda[CTA_EXPECT_MASK]
            || !cda[CTA_EXPECT_MASTER])
                return -EINVAL;
 
+       err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
+       if (err < 0)
+               return err;
+
        err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
        if (err < 0)
                return err;
 
        spin_lock_bh(&nf_conntrack_lock);
-       exp = __nf_ct_expect_find(&init_net, &tuple);
+       exp = __nf_ct_expect_find(net, zone, &tuple);
 
        if (!exp) {
                spin_unlock_bh(&nf_conntrack_lock);
                err = -ENOENT;
                if (nlh->nlmsg_flags & NLM_F_CREATE) {
-                       err = ctnetlink_create_expect(cda,
+                       err = ctnetlink_create_expect(net, zone, cda,
                                                      u3,
                                                      NETLINK_CB(skb).pid,
                                                      nlmsg_report(nlh));
@@ -1824,12 +2109,12 @@ ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
 }
 
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
-static struct notifier_block ctnl_notifier = {
-       .notifier_call  = ctnetlink_conntrack_event,
+static struct nf_ct_event_notifier ctnl_notifier = {
+       .fcn = ctnetlink_conntrack_event,
 };
 
-static struct notifier_block ctnl_notifier_exp = {
-       .notifier_call  = ctnetlink_expect_event,
+static struct nf_exp_event_notifier ctnl_notifier_exp = {
+       .fcn = ctnetlink_expect_event,
 };
 #endif
 
@@ -1882,29 +2167,29 @@ static int __init ctnetlink_init(void)
 {
        int ret;
 
-       printk("ctnetlink v%s: registering with nfnetlink.\n", version);
+       pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
        ret = nfnetlink_subsys_register(&ctnl_subsys);
        if (ret < 0) {
-               printk("ctnetlink_init: cannot register with nfnetlink.\n");
+               pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
                goto err_out;
        }
 
        ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
        if (ret < 0) {
-               printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
+               pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
                goto err_unreg_subsys;
        }
 
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
        ret = nf_conntrack_register_notifier(&ctnl_notifier);
        if (ret < 0) {
-               printk("ctnetlink_init: cannot register notifier.\n");
+               pr_err("ctnetlink_init: cannot register notifier.\n");
                goto err_unreg_exp_subsys;
        }
 
        ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
        if (ret < 0) {
-               printk("ctnetlink_init: cannot expect register notifier.\n");
+               pr_err("ctnetlink_init: cannot expect register notifier.\n");
                goto err_unreg_notifier;
        }
 #endif
@@ -1925,8 +2210,9 @@ err_out:
 
 static void __exit ctnetlink_exit(void)
 {
-       printk("ctnetlink: unregistering from nfnetlink.\n");
+       pr_info("ctnetlink: unregistering from nfnetlink.\n");
 
+       nf_ct_remove_userspace_expectations();
 #ifdef CONFIG_NF_CONNTRACK_EVENTS
        nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
        nf_conntrack_unregister_notifier(&ctnl_notifier);
@@ -1934,7 +2220,6 @@ static void __exit ctnetlink_exit(void)
 
        nfnetlink_subsys_unregister(&ctnl_exp_subsys);
        nfnetlink_subsys_unregister(&ctnl_subsys);
-       return;
 }
 
 module_init(ctnetlink_init);