]>
Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
1 | /* (C) 1999-2001 Paul `Rusty' Russell |
2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> | |
9 | * - enable working with Layer 3 protocol independent connection tracking. | |
10 | * | |
11 | * Derived from net/ipv4/netfilter/ip_conntrack_proto_icmp.c | |
12 | */ | |
13 | ||
14 | #include <linux/types.h> | |
15 | #include <linux/sched.h> | |
16 | #include <linux/timer.h> | |
17 | #include <linux/netfilter.h> | |
18 | #include <linux/in.h> | |
19 | #include <linux/icmp.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include <net/ip.h> | |
22 | #include <net/checksum.h> | |
23 | #include <linux/netfilter_ipv4.h> | |
24 | #include <net/netfilter/nf_conntrack_tuple.h> | |
25 | #include <net/netfilter/nf_conntrack_protocol.h> | |
26 | #include <net/netfilter/nf_conntrack_core.h> | |
27 | ||
28 | unsigned long nf_ct_icmp_timeout = 30*HZ; | |
29 | ||
30 | #if 0 | |
31 | #define DEBUGP printk | |
32 | #else | |
33 | #define DEBUGP(format, args...) | |
34 | #endif | |
35 | ||
36 | static int icmp_pkt_to_tuple(const struct sk_buff *skb, | |
37 | unsigned int dataoff, | |
38 | struct nf_conntrack_tuple *tuple) | |
39 | { | |
40 | struct icmphdr _hdr, *hp; | |
41 | ||
42 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
43 | if (hp == NULL) | |
44 | return 0; | |
45 | ||
46 | tuple->dst.u.icmp.type = hp->type; | |
47 | tuple->src.u.icmp.id = hp->un.echo.id; | |
48 | tuple->dst.u.icmp.code = hp->code; | |
49 | ||
50 | return 1; | |
51 | } | |
52 | ||
c1d10adb PNA |
53 | /* Add 1; spaces filled with 0. */ |
54 | static const u_int8_t invmap[] = { | |
55 | [ICMP_ECHO] = ICMP_ECHOREPLY + 1, | |
56 | [ICMP_ECHOREPLY] = ICMP_ECHO + 1, | |
57 | [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1, | |
58 | [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1, | |
59 | [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1, | |
60 | [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1, | |
61 | [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1, | |
62 | [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1 | |
63 | }; | |
64 | ||
9fb9cbb1 YK |
65 | static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple, |
66 | const struct nf_conntrack_tuple *orig) | |
67 | { | |
9fb9cbb1 YK |
68 | if (orig->dst.u.icmp.type >= sizeof(invmap) |
69 | || !invmap[orig->dst.u.icmp.type]) | |
70 | return 0; | |
71 | ||
72 | tuple->src.u.icmp.id = orig->src.u.icmp.id; | |
73 | tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1; | |
74 | tuple->dst.u.icmp.code = orig->dst.u.icmp.code; | |
75 | return 1; | |
76 | } | |
77 | ||
78 | /* Print out the per-protocol part of the tuple. */ | |
79 | static int icmp_print_tuple(struct seq_file *s, | |
80 | const struct nf_conntrack_tuple *tuple) | |
81 | { | |
82 | return seq_printf(s, "type=%u code=%u id=%u ", | |
83 | tuple->dst.u.icmp.type, | |
84 | tuple->dst.u.icmp.code, | |
85 | ntohs(tuple->src.u.icmp.id)); | |
86 | } | |
87 | ||
88 | /* Print out the private part of the conntrack. */ | |
89 | static int icmp_print_conntrack(struct seq_file *s, | |
90 | const struct nf_conn *conntrack) | |
91 | { | |
92 | return 0; | |
93 | } | |
94 | ||
95 | /* Returns verdict for packet, or -1 for invalid. */ | |
96 | static int icmp_packet(struct nf_conn *ct, | |
97 | const struct sk_buff *skb, | |
98 | unsigned int dataoff, | |
99 | enum ip_conntrack_info ctinfo, | |
100 | int pf, | |
101 | unsigned int hooknum) | |
102 | { | |
103 | /* Try to delete connection immediately after all replies: | |
104 | won't actually vanish as we still have skb, and del_timer | |
105 | means this will only run once even if count hits zero twice | |
106 | (theoretically possible with SMP) */ | |
107 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) { | |
108 | if (atomic_dec_and_test(&ct->proto.icmp.count) | |
109 | && del_timer(&ct->timeout)) | |
110 | ct->timeout.function((unsigned long)ct); | |
111 | } else { | |
112 | atomic_inc(&ct->proto.icmp.count); | |
113 | nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); | |
114 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout); | |
115 | } | |
116 | ||
117 | return NF_ACCEPT; | |
118 | } | |
119 | ||
120 | /* Called when a new connection for this protocol found. */ | |
121 | static int icmp_new(struct nf_conn *conntrack, | |
122 | const struct sk_buff *skb, unsigned int dataoff) | |
123 | { | |
c1d10adb PNA |
124 | static const u_int8_t valid_new[] = { |
125 | [ICMP_ECHO] = 1, | |
126 | [ICMP_TIMESTAMP] = 1, | |
127 | [ICMP_INFO_REQUEST] = 1, | |
128 | [ICMP_ADDRESS] = 1 | |
129 | }; | |
9fb9cbb1 YK |
130 | |
131 | if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) | |
132 | || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) { | |
133 | /* Can't create a new ICMP `conn' with this. */ | |
134 | DEBUGP("icmp: can't create new conn with type %u\n", | |
135 | conntrack->tuplehash[0].tuple.dst.u.icmp.type); | |
136 | NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple); | |
137 | return 0; | |
138 | } | |
139 | atomic_set(&conntrack->proto.icmp.count, 0); | |
140 | return 1; | |
141 | } | |
142 | ||
143 | extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4; | |
144 | /* Returns conntrack if it dealt with ICMP, and filled in skb fields */ | |
145 | static int | |
146 | icmp_error_message(struct sk_buff *skb, | |
147 | enum ip_conntrack_info *ctinfo, | |
148 | unsigned int hooknum) | |
149 | { | |
150 | struct nf_conntrack_tuple innertuple, origtuple; | |
151 | struct { | |
152 | struct icmphdr icmp; | |
153 | struct iphdr ip; | |
154 | } _in, *inside; | |
155 | struct nf_conntrack_protocol *innerproto; | |
156 | struct nf_conntrack_tuple_hash *h; | |
157 | int dataoff; | |
158 | ||
159 | NF_CT_ASSERT(skb->nfct == NULL); | |
160 | ||
161 | /* Not enough header? */ | |
162 | inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in); | |
163 | if (inside == NULL) | |
164 | return -NF_ACCEPT; | |
165 | ||
166 | /* Ignore ICMP's containing fragments (shouldn't happen) */ | |
167 | if (inside->ip.frag_off & htons(IP_OFFSET)) { | |
168 | DEBUGP("icmp_error_message: fragment of proto %u\n", | |
169 | inside->ip.protocol); | |
170 | return -NF_ACCEPT; | |
171 | } | |
172 | ||
c1d10adb | 173 | innerproto = __nf_ct_proto_find(PF_INET, inside->ip.protocol); |
9fb9cbb1 YK |
174 | dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp); |
175 | /* Are they talking about one of our connections? */ | |
176 | if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET, | |
177 | inside->ip.protocol, &origtuple, | |
178 | &nf_conntrack_l3proto_ipv4, innerproto)) { | |
179 | DEBUGP("icmp_error_message: ! get_tuple p=%u", | |
180 | inside->ip.protocol); | |
181 | return -NF_ACCEPT; | |
182 | } | |
183 | ||
184 | /* Ordinarily, we'd expect the inverted tupleproto, but it's | |
185 | been preserved inside the ICMP. */ | |
186 | if (!nf_ct_invert_tuple(&innertuple, &origtuple, | |
187 | &nf_conntrack_l3proto_ipv4, innerproto)) { | |
188 | DEBUGP("icmp_error_message: no match\n"); | |
189 | return -NF_ACCEPT; | |
190 | } | |
191 | ||
192 | *ctinfo = IP_CT_RELATED; | |
193 | ||
194 | h = nf_conntrack_find_get(&innertuple, NULL); | |
195 | if (!h) { | |
196 | /* Locally generated ICMPs will match inverted if they | |
197 | haven't been SNAT'ed yet */ | |
198 | /* FIXME: NAT code has to handle half-done double NAT --RR */ | |
199 | if (hooknum == NF_IP_LOCAL_OUT) | |
200 | h = nf_conntrack_find_get(&origtuple, NULL); | |
201 | ||
202 | if (!h) { | |
203 | DEBUGP("icmp_error_message: no match\n"); | |
204 | return -NF_ACCEPT; | |
205 | } | |
206 | ||
207 | /* Reverse direction from that found */ | |
208 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) | |
209 | *ctinfo += IP_CT_IS_REPLY; | |
210 | } else { | |
211 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) | |
212 | *ctinfo += IP_CT_IS_REPLY; | |
213 | } | |
214 | ||
215 | /* Update skb to refer to this connection */ | |
216 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; | |
217 | skb->nfctinfo = *ctinfo; | |
218 | return -NF_ACCEPT; | |
219 | } | |
220 | ||
221 | /* Small and modified version of icmp_rcv */ | |
222 | static int | |
223 | icmp_error(struct sk_buff *skb, unsigned int dataoff, | |
224 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) | |
225 | { | |
226 | struct icmphdr _ih, *icmph; | |
227 | ||
228 | /* Not enough header? */ | |
229 | icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih); | |
230 | if (icmph == NULL) { | |
231 | if (LOG_INVALID(IPPROTO_ICMP)) | |
232 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
233 | "nf_ct_icmp: short packet "); | |
234 | return -NF_ACCEPT; | |
235 | } | |
236 | ||
237 | /* See ip_conntrack_proto_tcp.c */ | |
238 | if (hooknum != NF_IP_PRE_ROUTING) | |
239 | goto checksum_skipped; | |
240 | ||
241 | switch (skb->ip_summed) { | |
242 | case CHECKSUM_HW: | |
243 | if (!(u16)csum_fold(skb->csum)) | |
244 | break; | |
245 | if (LOG_INVALID(IPPROTO_ICMP)) | |
246 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
247 | "nf_ct_icmp: bad HW ICMP checksum "); | |
248 | return -NF_ACCEPT; | |
249 | case CHECKSUM_NONE: | |
250 | if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0))) { | |
251 | if (LOG_INVALID(IPPROTO_ICMP)) | |
252 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, | |
253 | NULL, | |
254 | "nf_ct_icmp: bad ICMP checksum "); | |
255 | return -NF_ACCEPT; | |
256 | } | |
257 | default: | |
258 | break; | |
259 | } | |
260 | ||
261 | checksum_skipped: | |
262 | /* | |
263 | * 18 is the highest 'known' ICMP type. Anything else is a mystery | |
264 | * | |
265 | * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently | |
266 | * discarded. | |
267 | */ | |
268 | if (icmph->type > NR_ICMP_TYPES) { | |
269 | if (LOG_INVALID(IPPROTO_ICMP)) | |
270 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
271 | "nf_ct_icmp: invalid ICMP type "); | |
272 | return -NF_ACCEPT; | |
273 | } | |
274 | ||
275 | /* Need to track icmp error message? */ | |
276 | if (icmph->type != ICMP_DEST_UNREACH | |
277 | && icmph->type != ICMP_SOURCE_QUENCH | |
278 | && icmph->type != ICMP_TIME_EXCEEDED | |
279 | && icmph->type != ICMP_PARAMETERPROB | |
280 | && icmph->type != ICMP_REDIRECT) | |
281 | return NF_ACCEPT; | |
282 | ||
283 | return icmp_error_message(skb, ctinfo, hooknum); | |
284 | } | |
285 | ||
c1d10adb PNA |
286 | #if defined(CONFIG_NF_CT_NETLINK) || \ |
287 | defined(CONFIG_NF_CT_NETLINK_MODULE) | |
288 | ||
289 | #include <linux/netfilter/nfnetlink.h> | |
290 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
291 | ||
292 | static int icmp_tuple_to_nfattr(struct sk_buff *skb, | |
293 | const struct nf_conntrack_tuple *t) | |
294 | { | |
295 | NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t), | |
296 | &t->src.u.icmp.id); | |
297 | NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t), | |
298 | &t->dst.u.icmp.type); | |
299 | NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t), | |
300 | &t->dst.u.icmp.code); | |
301 | ||
302 | return 0; | |
303 | ||
304 | nfattr_failure: | |
305 | return -1; | |
306 | } | |
307 | ||
308 | static const size_t cta_min_proto[CTA_PROTO_MAX] = { | |
309 | [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t), | |
310 | [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t), | |
311 | [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t) | |
312 | }; | |
313 | ||
314 | static int icmp_nfattr_to_tuple(struct nfattr *tb[], | |
315 | struct nf_conntrack_tuple *tuple) | |
316 | { | |
317 | if (!tb[CTA_PROTO_ICMP_TYPE-1] | |
318 | || !tb[CTA_PROTO_ICMP_CODE-1] | |
319 | || !tb[CTA_PROTO_ICMP_ID-1]) | |
320 | return -EINVAL; | |
321 | ||
322 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
323 | return -EINVAL; | |
324 | ||
325 | tuple->dst.u.icmp.type = | |
326 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]); | |
327 | tuple->dst.u.icmp.code = | |
328 | *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]); | |
329 | tuple->src.u.icmp.id = | |
330 | *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]); | |
331 | ||
332 | if (tuple->dst.u.icmp.type >= sizeof(invmap) | |
333 | || !invmap[tuple->dst.u.icmp.type]) | |
334 | return -EINVAL; | |
335 | ||
336 | return 0; | |
337 | } | |
338 | #endif | |
339 | ||
9fb9cbb1 YK |
340 | struct nf_conntrack_protocol nf_conntrack_protocol_icmp = |
341 | { | |
342 | .list = { NULL, NULL }, | |
343 | .l3proto = PF_INET, | |
344 | .proto = IPPROTO_ICMP, | |
345 | .name = "icmp", | |
346 | .pkt_to_tuple = icmp_pkt_to_tuple, | |
347 | .invert_tuple = icmp_invert_tuple, | |
348 | .print_tuple = icmp_print_tuple, | |
349 | .print_conntrack = icmp_print_conntrack, | |
350 | .packet = icmp_packet, | |
351 | .new = icmp_new, | |
352 | .error = icmp_error, | |
353 | .destroy = NULL, | |
c1d10adb PNA |
354 | .me = NULL, |
355 | #if defined(CONFIG_NF_CT_NETLINK) || \ | |
356 | defined(CONFIG_NF_CT_NETLINK_MODULE) | |
357 | .tuple_to_nfattr = icmp_tuple_to_nfattr, | |
358 | .nfattr_to_tuple = icmp_nfattr_to_tuple, | |
359 | #endif | |
9fb9cbb1 YK |
360 | }; |
361 | ||
362 | EXPORT_SYMBOL(nf_conntrack_protocol_icmp); |