[NET] IPV4: Fix whitespace errors.
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / nf_conntrack_proto_icmp.c
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_l4proto.h>
26 #include <net/netfilter/nf_conntrack_core.h>
27
28 static unsigned long nf_ct_icmp_timeout __read_mostly = 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
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
65 static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
66 const struct nf_conntrack_tuple *orig)
67 {
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 {
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 };
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_l4proto *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
173 innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
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 (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
239 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
240 if (LOG_INVALID(IPPROTO_ICMP))
241 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
242 "nf_ct_icmp: bad HW ICMP checksum ");
243 return -NF_ACCEPT;
244 }
245
246 /*
247 * 18 is the highest 'known' ICMP type. Anything else is a mystery
248 *
249 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
250 * discarded.
251 */
252 if (icmph->type > NR_ICMP_TYPES) {
253 if (LOG_INVALID(IPPROTO_ICMP))
254 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
255 "nf_ct_icmp: invalid ICMP type ");
256 return -NF_ACCEPT;
257 }
258
259 /* Need to track icmp error message? */
260 if (icmph->type != ICMP_DEST_UNREACH
261 && icmph->type != ICMP_SOURCE_QUENCH
262 && icmph->type != ICMP_TIME_EXCEEDED
263 && icmph->type != ICMP_PARAMETERPROB
264 && icmph->type != ICMP_REDIRECT)
265 return NF_ACCEPT;
266
267 return icmp_error_message(skb, ctinfo, hooknum);
268 }
269
270 #if defined(CONFIG_NF_CT_NETLINK) || \
271 defined(CONFIG_NF_CT_NETLINK_MODULE)
272
273 #include <linux/netfilter/nfnetlink.h>
274 #include <linux/netfilter/nfnetlink_conntrack.h>
275
276 static int icmp_tuple_to_nfattr(struct sk_buff *skb,
277 const struct nf_conntrack_tuple *t)
278 {
279 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
280 &t->src.u.icmp.id);
281 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
282 &t->dst.u.icmp.type);
283 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
284 &t->dst.u.icmp.code);
285
286 return 0;
287
288 nfattr_failure:
289 return -1;
290 }
291
292 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
293 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
294 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
295 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
296 };
297
298 static int icmp_nfattr_to_tuple(struct nfattr *tb[],
299 struct nf_conntrack_tuple *tuple)
300 {
301 if (!tb[CTA_PROTO_ICMP_TYPE-1]
302 || !tb[CTA_PROTO_ICMP_CODE-1]
303 || !tb[CTA_PROTO_ICMP_ID-1])
304 return -EINVAL;
305
306 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
307 return -EINVAL;
308
309 tuple->dst.u.icmp.type =
310 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
311 tuple->dst.u.icmp.code =
312 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
313 tuple->src.u.icmp.id =
314 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
315
316 if (tuple->dst.u.icmp.type >= sizeof(invmap)
317 || !invmap[tuple->dst.u.icmp.type])
318 return -EINVAL;
319
320 return 0;
321 }
322 #endif
323
324 #ifdef CONFIG_SYSCTL
325 static struct ctl_table_header *icmp_sysctl_header;
326 static struct ctl_table icmp_sysctl_table[] = {
327 {
328 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
329 .procname = "nf_conntrack_icmp_timeout",
330 .data = &nf_ct_icmp_timeout,
331 .maxlen = sizeof(unsigned int),
332 .mode = 0644,
333 .proc_handler = &proc_dointvec_jiffies,
334 },
335 {
336 .ctl_name = 0
337 }
338 };
339 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
340 static struct ctl_table icmp_compat_sysctl_table[] = {
341 {
342 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
343 .procname = "ip_conntrack_icmp_timeout",
344 .data = &nf_ct_icmp_timeout,
345 .maxlen = sizeof(unsigned int),
346 .mode = 0644,
347 .proc_handler = &proc_dointvec_jiffies,
348 },
349 {
350 .ctl_name = 0
351 }
352 };
353 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
354 #endif /* CONFIG_SYSCTL */
355
356 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
357 {
358 .l3proto = PF_INET,
359 .l4proto = IPPROTO_ICMP,
360 .name = "icmp",
361 .pkt_to_tuple = icmp_pkt_to_tuple,
362 .invert_tuple = icmp_invert_tuple,
363 .print_tuple = icmp_print_tuple,
364 .print_conntrack = icmp_print_conntrack,
365 .packet = icmp_packet,
366 .new = icmp_new,
367 .error = icmp_error,
368 .destroy = NULL,
369 .me = NULL,
370 #if defined(CONFIG_NF_CT_NETLINK) || \
371 defined(CONFIG_NF_CT_NETLINK_MODULE)
372 .tuple_to_nfattr = icmp_tuple_to_nfattr,
373 .nfattr_to_tuple = icmp_nfattr_to_tuple,
374 #endif
375 #ifdef CONFIG_SYSCTL
376 .ctl_table_header = &icmp_sysctl_header,
377 .ctl_table = icmp_sysctl_table,
378 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
379 .ctl_compat_table = icmp_compat_sysctl_table,
380 #endif
381 #endif
382 };
383 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);