[NETFILTER]: Add ctnetlink port for nf_conntrack
[h-e-n] / net / ipv4 / netfilter / nf_conntrack_l3proto_ipv4.c
index 8202c1c..385867e 100644 (file)
@@ -392,6 +392,48 @@ getorigdst(struct sock *sk, int optval, void __user *user, int *len)
        return -ENOENT;
 }
 
+#if defined(CONFIG_NF_CT_NETLINK) || \
+    defined(CONFIG_NF_CT_NETLINK_MODULE)
+
+#include <linux/netfilter/nfnetlink.h>
+#include <linux/netfilter/nfnetlink_conntrack.h>
+
+static int ipv4_tuple_to_nfattr(struct sk_buff *skb,
+                               const struct nf_conntrack_tuple *tuple)
+{
+       NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t),
+               &tuple->src.u3.ip);
+       NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t),
+               &tuple->dst.u3.ip);
+       return 0;
+
+nfattr_failure:
+       return -1;
+}
+
+static const size_t cta_min_ip[CTA_IP_MAX] = {
+       [CTA_IP_V4_SRC-1]       = sizeof(u_int32_t),
+       [CTA_IP_V4_DST-1]       = sizeof(u_int32_t),
+};
+
+static int ipv4_nfattr_to_tuple(struct nfattr *tb[],
+                               struct nf_conntrack_tuple *t)
+{
+       if (!tb[CTA_IP_V4_SRC-1] || !tb[CTA_IP_V4_DST-1])
+               return -EINVAL;
+
+       if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
+               return -EINVAL;
+
+       t->src.u3.ip =
+               *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
+       t->dst.u3.ip =
+               *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
+
+       return 0;
+}
+#endif
+
 static struct nf_sockopt_ops so_getorigdst = {
        .pf             = PF_INET,
        .get_optmin     = SO_ORIGINAL_DST,
@@ -408,6 +450,11 @@ struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4 = {
        .print_conntrack = ipv4_print_conntrack,
        .prepare         = ipv4_prepare,
        .get_features    = ipv4_get_features,
+#if defined(CONFIG_NF_CT_NETLINK) || \
+    defined(CONFIG_NF_CT_NETLINK_MODULE)
+       .tuple_to_nfattr = ipv4_tuple_to_nfattr,
+       .nfattr_to_tuple = ipv4_nfattr_to_tuple,
+#endif
        .me              = THIS_MODULE,
 };