Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* Kernel module to match connection tracking information. |
2 | * Superset of Rusty's minimalistic state match. | |
3 | * | |
4 | * (C) 2001 Marc Boucher (marc@mbsi.ca). | |
5 | * | |
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. | |
9 | */ | |
10 | ||
11 | #include <linux/module.h> | |
12 | #include <linux/skbuff.h> | |
2e4e6a17 HW |
13 | #include <linux/netfilter/x_tables.h> |
14 | #include <linux/netfilter/xt_conntrack.h> | |
587aa641 | 15 | #include <net/netfilter/nf_conntrack.h> |
1da177e4 LT |
16 | |
17 | MODULE_LICENSE("GPL"); | |
18 | MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>"); | |
19 | MODULE_DESCRIPTION("iptables connection tracking match module"); | |
2e4e6a17 | 20 | MODULE_ALIAS("ipt_conntrack"); |
1da177e4 | 21 | |
9fb9cbb1 YK |
22 | static int |
23 | match(const struct sk_buff *skb, | |
24 | const struct net_device *in, | |
25 | const struct net_device *out, | |
c4986734 | 26 | const struct xt_match *match, |
9fb9cbb1 YK |
27 | const void *matchinfo, |
28 | int offset, | |
2e4e6a17 | 29 | unsigned int protoff, |
9fb9cbb1 YK |
30 | int *hotdrop) |
31 | { | |
2e4e6a17 | 32 | const struct xt_conntrack_info *sinfo = matchinfo; |
9fb9cbb1 YK |
33 | struct nf_conn *ct; |
34 | enum ip_conntrack_info ctinfo; | |
35 | unsigned int statebit; | |
36 | ||
37 | ct = nf_ct_get((struct sk_buff *)skb, &ctinfo); | |
38 | ||
39 | #define FWINV(bool,invflg) ((bool) ^ !!(sinfo->invflags & invflg)) | |
40 | ||
41 | if (ct == &nf_conntrack_untracked) | |
2e4e6a17 | 42 | statebit = XT_CONNTRACK_STATE_UNTRACKED; |
9fb9cbb1 | 43 | else if (ct) |
601e68e1 YH |
44 | statebit = XT_CONNTRACK_STATE_BIT(ctinfo); |
45 | else | |
46 | statebit = XT_CONNTRACK_STATE_INVALID; | |
47 | ||
50b9f1d5 | 48 | if (sinfo->flags & XT_CONNTRACK_STATE) { |
9fb9cbb1 | 49 | if (ct) { |
50b9f1d5 | 50 | if (test_bit(IPS_SRC_NAT_BIT, &ct->status)) |
2e4e6a17 | 51 | statebit |= XT_CONNTRACK_STATE_SNAT; |
50b9f1d5 | 52 | if (test_bit(IPS_DST_NAT_BIT, &ct->status)) |
2e4e6a17 | 53 | statebit |= XT_CONNTRACK_STATE_DNAT; |
9fb9cbb1 | 54 | } |
50b9f1d5 PM |
55 | if (FWINV((statebit & sinfo->statemask) == 0, |
56 | XT_CONNTRACK_STATE)) | |
9fb9cbb1 YK |
57 | return 0; |
58 | } | |
59 | ||
50b9f1d5 PM |
60 | if (ct == NULL) { |
61 | if (sinfo->flags & ~XT_CONNTRACK_STATE) | |
9fb9cbb1 | 62 | return 0; |
50b9f1d5 | 63 | return 1; |
9fb9cbb1 YK |
64 | } |
65 | ||
50b9f1d5 PM |
66 | if (sinfo->flags & XT_CONNTRACK_PROTO && |
67 | FWINV(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum != | |
601e68e1 | 68 | sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.protonum, |
50b9f1d5 | 69 | XT_CONNTRACK_PROTO)) |
601e68e1 | 70 | return 0; |
50b9f1d5 PM |
71 | |
72 | if (sinfo->flags & XT_CONNTRACK_ORIGSRC && | |
73 | FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip & | |
601e68e1 | 74 | sinfo->sipmsk[IP_CT_DIR_ORIGINAL].s_addr) != |
50b9f1d5 PM |
75 | sinfo->tuple[IP_CT_DIR_ORIGINAL].src.ip, |
76 | XT_CONNTRACK_ORIGSRC)) | |
77 | return 0; | |
9fb9cbb1 | 78 | |
50b9f1d5 PM |
79 | if (sinfo->flags & XT_CONNTRACK_ORIGDST && |
80 | FWINV((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3.ip & | |
601e68e1 | 81 | sinfo->dipmsk[IP_CT_DIR_ORIGINAL].s_addr) != |
50b9f1d5 PM |
82 | sinfo->tuple[IP_CT_DIR_ORIGINAL].dst.ip, |
83 | XT_CONNTRACK_ORIGDST)) | |
84 | return 0; | |
9fb9cbb1 | 85 | |
50b9f1d5 PM |
86 | if (sinfo->flags & XT_CONNTRACK_REPLSRC && |
87 | FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3.ip & | |
601e68e1 | 88 | sinfo->sipmsk[IP_CT_DIR_REPLY].s_addr) != |
50b9f1d5 PM |
89 | sinfo->tuple[IP_CT_DIR_REPLY].src.ip, |
90 | XT_CONNTRACK_REPLSRC)) | |
91 | return 0; | |
9fb9cbb1 | 92 | |
50b9f1d5 PM |
93 | if (sinfo->flags & XT_CONNTRACK_REPLDST && |
94 | FWINV((ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3.ip & | |
601e68e1 | 95 | sinfo->dipmsk[IP_CT_DIR_REPLY].s_addr) != |
50b9f1d5 PM |
96 | sinfo->tuple[IP_CT_DIR_REPLY].dst.ip, |
97 | XT_CONNTRACK_REPLDST)) | |
98 | return 0; | |
9fb9cbb1 | 99 | |
50b9f1d5 PM |
100 | if (sinfo->flags & XT_CONNTRACK_STATUS && |
101 | FWINV((ct->status & sinfo->statusmask) == 0, | |
601e68e1 | 102 | XT_CONNTRACK_STATUS)) |
50b9f1d5 | 103 | return 0; |
9fb9cbb1 | 104 | |
50b9f1d5 PM |
105 | if(sinfo->flags & XT_CONNTRACK_EXPIRES) { |
106 | unsigned long expires = timer_pending(&ct->timeout) ? | |
107 | (ct->timeout.expires - jiffies)/HZ : 0; | |
9fb9cbb1 | 108 | |
50b9f1d5 PM |
109 | if (FWINV(!(expires >= sinfo->expires_min && |
110 | expires <= sinfo->expires_max), | |
111 | XT_CONNTRACK_EXPIRES)) | |
9fb9cbb1 YK |
112 | return 0; |
113 | } | |
9fb9cbb1 YK |
114 | return 1; |
115 | } | |
116 | ||
b9f78f9f PNA |
117 | static int |
118 | checkentry(const char *tablename, | |
119 | const void *ip, | |
120 | const struct xt_match *match, | |
121 | void *matchinfo, | |
b9f78f9f PNA |
122 | unsigned int hook_mask) |
123 | { | |
b9f78f9f | 124 | if (nf_ct_l3proto_try_module_get(match->family) < 0) { |
fe0b9294 | 125 | printk(KERN_WARNING "can't load conntrack support for " |
b9f78f9f PNA |
126 | "proto=%d\n", match->family); |
127 | return 0; | |
128 | } | |
b9f78f9f PNA |
129 | return 1; |
130 | } | |
131 | ||
50b9f1d5 | 132 | static void destroy(const struct xt_match *match, void *matchinfo) |
b9f78f9f | 133 | { |
b9f78f9f | 134 | nf_ct_l3proto_module_put(match->family); |
b9f78f9f PNA |
135 | } |
136 | ||
2e4e6a17 | 137 | static struct xt_match conntrack_match = { |
1da177e4 | 138 | .name = "conntrack", |
5d04bff0 | 139 | .match = match, |
b9f78f9f PNA |
140 | .checkentry = checkentry, |
141 | .destroy = destroy, | |
5d04bff0 | 142 | .matchsize = sizeof(struct xt_conntrack_info), |
a45049c5 | 143 | .family = AF_INET, |
1da177e4 LT |
144 | .me = THIS_MODULE, |
145 | }; | |
146 | ||
65b4b4e8 | 147 | static int __init xt_conntrack_init(void) |
1da177e4 | 148 | { |
4470bbc7 | 149 | return xt_register_match(&conntrack_match); |
1da177e4 LT |
150 | } |
151 | ||
65b4b4e8 | 152 | static void __exit xt_conntrack_fini(void) |
1da177e4 | 153 | { |
a45049c5 | 154 | xt_unregister_match(&conntrack_match); |
1da177e4 LT |
155 | } |
156 | ||
65b4b4e8 AM |
157 | module_init(xt_conntrack_init); |
158 | module_exit(xt_conntrack_fini); |