]>
Commit | Line | Data |
---|---|---|
64eb12f9 JE |
1 | /* |
2 | * xt_conntrack - Netfilter module to match connection tracking | |
3 | * information. (Superset of Rusty's minimalistic state match.) | |
1da177e4 | 4 | * |
64eb12f9 JE |
5 | * (C) 2001 Marc Boucher ([email protected]). |
6 | * Copyright © CC Computer Consultants GmbH, 2007 - 2008 | |
1da177e4 | 7 | * |
64eb12f9 JE |
8 | * This program is free software; you can redistribute it and/or modify |
9 | * it under the terms of the GNU General Public License version 2 as | |
10 | * published by the Free Software Foundation. | |
1da177e4 | 11 | */ |
8bee4bad | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
1da177e4 LT |
13 | #include <linux/module.h> |
14 | #include <linux/skbuff.h> | |
64eb12f9 | 15 | #include <net/ipv6.h> |
2e4e6a17 HW |
16 | #include <linux/netfilter/x_tables.h> |
17 | #include <linux/netfilter/xt_conntrack.h> | |
587aa641 | 18 | #include <net/netfilter/nf_conntrack.h> |
1da177e4 LT |
19 | |
20 | MODULE_LICENSE("GPL"); | |
21 | MODULE_AUTHOR("Marc Boucher <[email protected]>"); | |
9e05ec4b | 22 | MODULE_AUTHOR("Jan Engelhardt <[email protected]>"); |
2ae15b64 | 23 | MODULE_DESCRIPTION("Xtables: connection tracking state match"); |
2e4e6a17 | 24 | MODULE_ALIAS("ipt_conntrack"); |
64eb12f9 | 25 | MODULE_ALIAS("ip6t_conntrack"); |
1da177e4 | 26 | |
64eb12f9 JE |
27 | static bool |
28 | conntrack_addrcmp(const union nf_inet_addr *kaddr, | |
29 | const union nf_inet_addr *uaddr, | |
30 | const union nf_inet_addr *umask, unsigned int l3proto) | |
31 | { | |
ee999d8b | 32 | if (l3proto == NFPROTO_IPV4) |
6556874d | 33 | return ((kaddr->ip ^ uaddr->ip) & umask->ip) == 0; |
ee999d8b | 34 | else if (l3proto == NFPROTO_IPV6) |
64eb12f9 JE |
35 | return ipv6_masked_addr_cmp(&kaddr->in6, &umask->in6, |
36 | &uaddr->in6) == 0; | |
37 | else | |
38 | return false; | |
39 | } | |
40 | ||
41 | static inline bool | |
42 | conntrack_mt_origsrc(const struct nf_conn *ct, | |
d6d3f08b | 43 | const struct xt_conntrack_mtinfo2 *info, |
76108cea | 44 | u_int8_t family) |
64eb12f9 JE |
45 | { |
46 | return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3, | |
47 | &info->origsrc_addr, &info->origsrc_mask, family); | |
48 | } | |
49 | ||
50 | static inline bool | |
51 | conntrack_mt_origdst(const struct nf_conn *ct, | |
d6d3f08b | 52 | const struct xt_conntrack_mtinfo2 *info, |
76108cea | 53 | u_int8_t family) |
64eb12f9 JE |
54 | { |
55 | return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.u3, | |
56 | &info->origdst_addr, &info->origdst_mask, family); | |
57 | } | |
58 | ||
59 | static inline bool | |
60 | conntrack_mt_replsrc(const struct nf_conn *ct, | |
d6d3f08b | 61 | const struct xt_conntrack_mtinfo2 *info, |
76108cea | 62 | u_int8_t family) |
64eb12f9 JE |
63 | { |
64 | return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_REPLY].tuple.src.u3, | |
65 | &info->replsrc_addr, &info->replsrc_mask, family); | |
66 | } | |
67 | ||
68 | static inline bool | |
69 | conntrack_mt_repldst(const struct nf_conn *ct, | |
d6d3f08b | 70 | const struct xt_conntrack_mtinfo2 *info, |
76108cea | 71 | u_int8_t family) |
64eb12f9 JE |
72 | { |
73 | return conntrack_addrcmp(&ct->tuplehash[IP_CT_DIR_REPLY].tuple.dst.u3, | |
74 | &info->repldst_addr, &info->repldst_mask, family); | |
75 | } | |
76 | ||
b4164998 | 77 | static inline bool |
d6d3f08b | 78 | ct_proto_port_check(const struct xt_conntrack_mtinfo2 *info, |
b4164998 JE |
79 | const struct nf_conn *ct) |
80 | { | |
81 | const struct nf_conntrack_tuple *tuple; | |
82 | ||
83 | tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple; | |
84 | if ((info->match_flags & XT_CONNTRACK_PROTO) && | |
5e8fbe2a | 85 | (nf_ct_protonum(ct) == info->l4proto) ^ |
b4164998 JE |
86 | !(info->invert_flags & XT_CONNTRACK_PROTO)) |
87 | return false; | |
88 | ||
89 | /* Shortcut to match all recognized protocols by using ->src.all. */ | |
90 | if ((info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) && | |
91 | (tuple->src.u.all == info->origsrc_port) ^ | |
92 | !(info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT)) | |
93 | return false; | |
94 | ||
95 | if ((info->match_flags & XT_CONNTRACK_ORIGDST_PORT) && | |
96 | (tuple->dst.u.all == info->origdst_port) ^ | |
97 | !(info->invert_flags & XT_CONNTRACK_ORIGDST_PORT)) | |
98 | return false; | |
99 | ||
100 | tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple; | |
101 | ||
102 | if ((info->match_flags & XT_CONNTRACK_REPLSRC_PORT) && | |
103 | (tuple->src.u.all == info->replsrc_port) ^ | |
104 | !(info->invert_flags & XT_CONNTRACK_REPLSRC_PORT)) | |
105 | return false; | |
106 | ||
107 | if ((info->match_flags & XT_CONNTRACK_REPLDST_PORT) && | |
108 | (tuple->dst.u.all == info->repldst_port) ^ | |
109 | !(info->invert_flags & XT_CONNTRACK_REPLDST_PORT)) | |
110 | return false; | |
111 | ||
112 | return true; | |
113 | } | |
114 | ||
64eb12f9 | 115 | static bool |
62fc8051 | 116 | conntrack_mt(const struct sk_buff *skb, struct xt_action_param *par, |
3a042929 | 117 | u16 state_mask, u16 status_mask) |
64eb12f9 | 118 | { |
d6d3f08b | 119 | const struct xt_conntrack_mtinfo2 *info = par->matchinfo; |
64eb12f9 JE |
120 | enum ip_conntrack_info ctinfo; |
121 | const struct nf_conn *ct; | |
122 | unsigned int statebit; | |
123 | ||
124 | ct = nf_ct_get(skb, &ctinfo); | |
125 | ||
126 | if (ct == &nf_conntrack_untracked) | |
127 | statebit = XT_CONNTRACK_STATE_UNTRACKED; | |
128 | else if (ct != NULL) | |
129 | statebit = XT_CONNTRACK_STATE_BIT(ctinfo); | |
130 | else | |
131 | statebit = XT_CONNTRACK_STATE_INVALID; | |
132 | ||
133 | if (info->match_flags & XT_CONNTRACK_STATE) { | |
134 | if (ct != NULL) { | |
135 | if (test_bit(IPS_SRC_NAT_BIT, &ct->status)) | |
136 | statebit |= XT_CONNTRACK_STATE_SNAT; | |
137 | if (test_bit(IPS_DST_NAT_BIT, &ct->status)) | |
138 | statebit |= XT_CONNTRACK_STATE_DNAT; | |
139 | } | |
3a042929 | 140 | if (!!(state_mask & statebit) ^ |
64eb12f9 JE |
141 | !(info->invert_flags & XT_CONNTRACK_STATE)) |
142 | return false; | |
143 | } | |
144 | ||
145 | if (ct == NULL) | |
146 | return info->match_flags & XT_CONNTRACK_STATE; | |
b4164998 JE |
147 | if ((info->match_flags & XT_CONNTRACK_DIRECTION) && |
148 | (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) ^ | |
149 | !!(info->invert_flags & XT_CONNTRACK_DIRECTION)) | |
64eb12f9 JE |
150 | return false; |
151 | ||
152 | if (info->match_flags & XT_CONNTRACK_ORIGSRC) | |
92f3b2b1 | 153 | if (conntrack_mt_origsrc(ct, info, par->family) ^ |
64eb12f9 JE |
154 | !(info->invert_flags & XT_CONNTRACK_ORIGSRC)) |
155 | return false; | |
156 | ||
157 | if (info->match_flags & XT_CONNTRACK_ORIGDST) | |
92f3b2b1 | 158 | if (conntrack_mt_origdst(ct, info, par->family) ^ |
64eb12f9 JE |
159 | !(info->invert_flags & XT_CONNTRACK_ORIGDST)) |
160 | return false; | |
161 | ||
162 | if (info->match_flags & XT_CONNTRACK_REPLSRC) | |
92f3b2b1 | 163 | if (conntrack_mt_replsrc(ct, info, par->family) ^ |
64eb12f9 JE |
164 | !(info->invert_flags & XT_CONNTRACK_REPLSRC)) |
165 | return false; | |
166 | ||
167 | if (info->match_flags & XT_CONNTRACK_REPLDST) | |
92f3b2b1 | 168 | if (conntrack_mt_repldst(ct, info, par->family) ^ |
64eb12f9 JE |
169 | !(info->invert_flags & XT_CONNTRACK_REPLDST)) |
170 | return false; | |
171 | ||
b4164998 JE |
172 | if (!ct_proto_port_check(info, ct)) |
173 | return false; | |
174 | ||
64eb12f9 | 175 | if ((info->match_flags & XT_CONNTRACK_STATUS) && |
3a042929 | 176 | (!!(status_mask & ct->status) ^ |
64eb12f9 JE |
177 | !(info->invert_flags & XT_CONNTRACK_STATUS))) |
178 | return false; | |
179 | ||
180 | if (info->match_flags & XT_CONNTRACK_EXPIRES) { | |
181 | unsigned long expires = 0; | |
182 | ||
183 | if (timer_pending(&ct->timeout)) | |
184 | expires = (ct->timeout.expires - jiffies) / HZ; | |
185 | if ((expires >= info->expires_min && | |
186 | expires <= info->expires_max) ^ | |
187 | !(info->invert_flags & XT_CONNTRACK_EXPIRES)) | |
188 | return false; | |
189 | } | |
190 | return true; | |
191 | } | |
192 | ||
d6d3f08b | 193 | static bool |
62fc8051 | 194 | conntrack_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
d6d3f08b | 195 | { |
3a042929 | 196 | const struct xt_conntrack_mtinfo1 *info = par->matchinfo; |
d6d3f08b | 197 | |
3a042929 FW |
198 | return conntrack_mt(skb, par, info->state_mask, info->status_mask); |
199 | } | |
200 | ||
201 | static bool | |
62fc8051 | 202 | conntrack_mt_v2(const struct sk_buff *skb, struct xt_action_param *par) |
3a042929 FW |
203 | { |
204 | const struct xt_conntrack_mtinfo2 *info = par->matchinfo; | |
205 | ||
206 | return conntrack_mt(skb, par, info->state_mask, info->status_mask); | |
d6d3f08b JE |
207 | } |
208 | ||
b0f38452 | 209 | static int conntrack_mt_check(const struct xt_mtchk_param *par) |
b9f78f9f | 210 | { |
4a5a5c73 JE |
211 | int ret; |
212 | ||
213 | ret = nf_ct_l3proto_try_module_get(par->family); | |
f95c74e3 | 214 | if (ret < 0) |
8bee4bad JE |
215 | pr_info("cannot load conntrack support for proto=%u\n", |
216 | par->family); | |
f95c74e3 | 217 | return ret; |
b9f78f9f PNA |
218 | } |
219 | ||
6be3d859 | 220 | static void conntrack_mt_destroy(const struct xt_mtdtor_param *par) |
b9f78f9f | 221 | { |
92f3b2b1 | 222 | nf_ct_l3proto_module_put(par->family); |
b9f78f9f PNA |
223 | } |
224 | ||
64eb12f9 | 225 | static struct xt_match conntrack_mt_reg[] __read_mostly = { |
64eb12f9 JE |
226 | { |
227 | .name = "conntrack", | |
228 | .revision = 1, | |
92f3b2b1 | 229 | .family = NFPROTO_UNSPEC, |
64eb12f9 | 230 | .matchsize = sizeof(struct xt_conntrack_mtinfo1), |
d6d3f08b | 231 | .match = conntrack_mt_v1, |
3a042929 FW |
232 | .checkentry = conntrack_mt_check, |
233 | .destroy = conntrack_mt_destroy, | |
d6d3f08b JE |
234 | .me = THIS_MODULE, |
235 | }, | |
236 | { | |
237 | .name = "conntrack", | |
238 | .revision = 2, | |
239 | .family = NFPROTO_UNSPEC, | |
240 | .matchsize = sizeof(struct xt_conntrack_mtinfo2), | |
3a042929 | 241 | .match = conntrack_mt_v2, |
64eb12f9 JE |
242 | .checkentry = conntrack_mt_check, |
243 | .destroy = conntrack_mt_destroy, | |
244 | .me = THIS_MODULE, | |
245 | }, | |
1da177e4 LT |
246 | }; |
247 | ||
d3c5ee6d | 248 | static int __init conntrack_mt_init(void) |
1da177e4 | 249 | { |
64eb12f9 JE |
250 | return xt_register_matches(conntrack_mt_reg, |
251 | ARRAY_SIZE(conntrack_mt_reg)); | |
1da177e4 LT |
252 | } |
253 | ||
d3c5ee6d | 254 | static void __exit conntrack_mt_exit(void) |
1da177e4 | 255 | { |
64eb12f9 | 256 | xt_unregister_matches(conntrack_mt_reg, ARRAY_SIZE(conntrack_mt_reg)); |
1da177e4 LT |
257 | } |
258 | ||
d3c5ee6d JE |
259 | module_init(conntrack_mt_init); |
260 | module_exit(conntrack_mt_exit); |