1 /* Kernel module to match connection tracking information.
2 * Superset of Rusty's minimalistic state match.
3 * GPL (C) 2001 Marc Boucher (marc@mbsi.ca).
5 #include <linux/module.h>
6 #include <linux/skbuff.h>
7 #include <linux/netfilter_ipv4/ip_conntrack.h>
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ipt_conntrack.h>
12 match(const struct sk_buff *skb,
13 const struct net_device *in,
14 const struct net_device *out,
15 const void *matchinfo,
21 const struct ipt_conntrack_info *sinfo = matchinfo;
22 struct ip_conntrack *ct;
23 enum ip_conntrack_info ctinfo;
24 unsigned int statebit;
26 ct = ip_conntrack_get((struct sk_buff *)skb, &ctinfo);
28 #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
31 statebit = IPT_CONNTRACK_STATE_BIT(ctinfo);
33 statebit = IPT_CONNTRACK_STATE_INVALID;
35 if(sinfo->flags & IPT_CONNTRACK_STATE) {
37 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip !=
38 ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip)
39 statebit |= IPT_CONNTRACK_STATE_SNAT;
41 if(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip !=
42 ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip)
43 statebit |= IPT_CONNTRACK_STATE_DNAT;
46 if (FWINV((statebit & sinfo->statemask) == 0, IPT_CONNTRACK_STATE))
50 if(sinfo->flags & IPT_CONNTRACK_PROTO) {
51 if (!ct || FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum, IPT_CONNTRACK_PROTO))
55 if(sinfo->flags & IPT_CONNTRACK_ORIGSRC) {
56 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.ip&sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip, IPT_CONNTRACK_ORIGSRC))
60 if(sinfo->flags & IPT_CONNTRACK_ORIGDST) {
61 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.ip&sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) != sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip, IPT_CONNTRACK_ORIGDST))
65 if(sinfo->flags & IPT_CONNTRACK_REPLSRC) {
66 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.ip&sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].src.ip, IPT_CONNTRACK_REPLSRC))
70 if(sinfo->flags & IPT_CONNTRACK_REPLDST) {
71 if (!ct || FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.ip&sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) != sinfo->tuple[IP_CT_DIR_REPLY].dst.ip, IPT_CONNTRACK_REPLDST))
75 if(sinfo->flags & IPT_CONNTRACK_STATUS) {
76 if (!ct || FWINV((ct->status & sinfo->statusmask) == 0, IPT_CONNTRACK_STATUS))
80 if(sinfo->flags & IPT_CONNTRACK_EXPIRES) {
81 unsigned long expires;
86 expires = timer_pending(&ct->timeout) ? (ct->timeout.expires - jiffies)/HZ : 0;
88 if (FWINV(!(expires >= sinfo->expires_min && expires <= sinfo->expires_max), IPT_CONNTRACK_EXPIRES))
95 static int check(const char *tablename,
96 const struct ipt_ip *ip,
98 unsigned int matchsize,
99 unsigned int hook_mask)
101 if (matchsize != IPT_ALIGN(sizeof(struct ipt_conntrack_info)))
107 static struct ipt_match conntrack_match
108 = { { NULL, NULL }, "conntrack", &match, &check, NULL, THIS_MODULE };
110 static int __init init(void)
112 return ipt_register_match(&conntrack_match);
115 static void __exit fini(void)
117 ipt_unregister_match(&conntrack_match);
122 MODULE_LICENSE("GPL");