1 /* Kernel module to match connection tracking information.
2 * Superset of Rusty's minimalistic state match.
4 * (C) 2001 Marc Boucher (marc@mbsi.ca).
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
14 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
15 #include <linux/netfilter_ipv4/ip_conntrack.h>
16 #include <linux/netfilter_ipv4/ip_conntrack_tuple.h>
18 #include <net/netfilter/nf_conntrack.h>
21 #include <linux/netfilter/x_tables.h>
22 #include <linux/netfilter/xt_conntrack.h>
24 MODULE_LICENSE("GPL");
25 MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
26 MODULE_DESCRIPTION("iptables connection tracking match module");
27 MODULE_ALIAS("ipt_conntrack");
29 #if defined(CONFIG_IP_NF_CONNTRACK) || defined(CONFIG_IP_NF_CONNTRACK_MODULE)
32 match(const struct sk_buff
*skb
,
33 const struct net_device
*in
,
34 const struct net_device
*out
,
35 const struct xt_match
*match
,
36 const void *matchinfo
,
41 const struct xt_conntrack_info
*sinfo
= matchinfo
;
42 struct ip_conntrack
*ct
;
43 enum ip_conntrack_info ctinfo
;
44 unsigned int statebit
;
46 ct
= ip_conntrack_get((struct sk_buff
*)skb
, &ctinfo
);
48 #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
50 if (ct
== &ip_conntrack_untracked
)
51 statebit
= XT_CONNTRACK_STATE_UNTRACKED
;
53 statebit
= XT_CONNTRACK_STATE_BIT(ctinfo
);
55 statebit
= XT_CONNTRACK_STATE_INVALID
;
57 if(sinfo
->flags
& XT_CONNTRACK_STATE
) {
59 if(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.ip
!=
60 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.ip
)
61 statebit
|= XT_CONNTRACK_STATE_SNAT
;
63 if(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.ip
!=
64 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.ip
)
65 statebit
|= XT_CONNTRACK_STATE_DNAT
;
68 if (FWINV((statebit
& sinfo
->statemask
) == 0, XT_CONNTRACK_STATE
))
72 if(sinfo
->flags
& XT_CONNTRACK_PROTO
) {
73 if (!ct
|| FWINV(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
!= sinfo
->tuple
[IP_CT_DIR_ORIGINAL
].dst
.protonum
, XT_CONNTRACK_PROTO
))
77 if(sinfo
->flags
& XT_CONNTRACK_ORIGSRC
) {
78 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
, XT_CONNTRACK_ORIGSRC
))
82 if(sinfo
->flags
& XT_CONNTRACK_ORIGDST
) {
83 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
, XT_CONNTRACK_ORIGDST
))
87 if(sinfo
->flags
& XT_CONNTRACK_REPLSRC
) {
88 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
, XT_CONNTRACK_REPLSRC
))
92 if(sinfo
->flags
& XT_CONNTRACK_REPLDST
) {
93 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
, XT_CONNTRACK_REPLDST
))
97 if(sinfo
->flags
& XT_CONNTRACK_STATUS
) {
98 if (!ct
|| FWINV((ct
->status
& sinfo
->statusmask
) == 0, XT_CONNTRACK_STATUS
))
102 if(sinfo
->flags
& XT_CONNTRACK_EXPIRES
) {
103 unsigned long expires
;
108 expires
= timer_pending(&ct
->timeout
) ? (ct
->timeout
.expires
- jiffies
)/HZ
: 0;
110 if (FWINV(!(expires
>= sinfo
->expires_min
&& expires
<= sinfo
->expires_max
), XT_CONNTRACK_EXPIRES
))
117 #else /* CONFIG_IP_NF_CONNTRACK */
119 match(const struct sk_buff
*skb
,
120 const struct net_device
*in
,
121 const struct net_device
*out
,
122 const struct xt_match
*match
,
123 const void *matchinfo
,
125 unsigned int protoff
,
128 const struct xt_conntrack_info
*sinfo
= matchinfo
;
130 enum ip_conntrack_info ctinfo
;
131 unsigned int statebit
;
133 ct
= nf_ct_get((struct sk_buff
*)skb
, &ctinfo
);
135 #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg))
137 if (ct
== &nf_conntrack_untracked
)
138 statebit
= XT_CONNTRACK_STATE_UNTRACKED
;
140 statebit
= XT_CONNTRACK_STATE_BIT(ctinfo
);
142 statebit
= XT_CONNTRACK_STATE_INVALID
;
144 if(sinfo
->flags
& XT_CONNTRACK_STATE
) {
146 if(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u3
.ip
!=
147 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
)
148 statebit
|= XT_CONNTRACK_STATE_SNAT
;
150 if(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.u3
.ip
!=
151 ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
)
152 statebit
|= XT_CONNTRACK_STATE_DNAT
;
155 if (FWINV((statebit
& sinfo
->statemask
) == 0, XT_CONNTRACK_STATE
))
159 if(sinfo
->flags
& XT_CONNTRACK_PROTO
) {
160 if (!ct
|| FWINV(ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.protonum
!= sinfo
->tuple
[IP_CT_DIR_ORIGINAL
].dst
.protonum
, XT_CONNTRACK_PROTO
))
164 if(sinfo
->flags
& XT_CONNTRACK_ORIGSRC
) {
165 if (!ct
|| FWINV((ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.src
.u3
.ip
&sinfo
->sipmsk
[IP_CT_DIR_ORIGINAL
].s_addr
) != sinfo
->tuple
[IP_CT_DIR_ORIGINAL
].src
.ip
, XT_CONNTRACK_ORIGSRC
))
169 if(sinfo
->flags
& XT_CONNTRACK_ORIGDST
) {
170 if (!ct
|| FWINV((ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
.dst
.u3
.ip
&sinfo
->dipmsk
[IP_CT_DIR_ORIGINAL
].s_addr
) != sinfo
->tuple
[IP_CT_DIR_ORIGINAL
].dst
.ip
, XT_CONNTRACK_ORIGDST
))
174 if(sinfo
->flags
& XT_CONNTRACK_REPLSRC
) {
175 if (!ct
|| FWINV((ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.src
.u3
.ip
&sinfo
->sipmsk
[IP_CT_DIR_REPLY
].s_addr
) != sinfo
->tuple
[IP_CT_DIR_REPLY
].src
.ip
, XT_CONNTRACK_REPLSRC
))
179 if(sinfo
->flags
& XT_CONNTRACK_REPLDST
) {
180 if (!ct
|| FWINV((ct
->tuplehash
[IP_CT_DIR_REPLY
].tuple
.dst
.u3
.ip
&sinfo
->dipmsk
[IP_CT_DIR_REPLY
].s_addr
) != sinfo
->tuple
[IP_CT_DIR_REPLY
].dst
.ip
, XT_CONNTRACK_REPLDST
))
184 if(sinfo
->flags
& XT_CONNTRACK_STATUS
) {
185 if (!ct
|| FWINV((ct
->status
& sinfo
->statusmask
) == 0, XT_CONNTRACK_STATUS
))
189 if(sinfo
->flags
& XT_CONNTRACK_EXPIRES
) {
190 unsigned long expires
;
195 expires
= timer_pending(&ct
->timeout
) ? (ct
->timeout
.expires
- jiffies
)/HZ
: 0;
197 if (FWINV(!(expires
>= sinfo
->expires_min
&& expires
<= sinfo
->expires_max
), XT_CONNTRACK_EXPIRES
))
204 #endif /* CONFIG_NF_IP_CONNTRACK */
207 checkentry(const char *tablename
,
209 const struct xt_match
*match
,
211 unsigned int hook_mask
)
213 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
214 if (nf_ct_l3proto_try_module_get(match
->family
) < 0) {
215 printk(KERN_WARNING
"can't load nf_conntrack support for "
216 "proto=%d\n", match
->family
);
224 destroy(const struct xt_match
*match
, void *matchinfo
)
226 #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
227 nf_ct_l3proto_module_put(match
->family
);
231 static struct xt_match conntrack_match
= {
234 .checkentry
= checkentry
,
236 .matchsize
= sizeof(struct xt_conntrack_info
),
241 static int __init
xt_conntrack_init(void)
244 return xt_register_match(&conntrack_match
);
247 static void __exit
xt_conntrack_fini(void)
249 xt_unregister_match(&conntrack_match
);
252 module_init(xt_conntrack_init
);
253 module_exit(xt_conntrack_fini
);