NETFILTER: remove unnecessary goto statement for error recovery
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / nf_conntrack_proto_icmp.c
blob78b7d1579d43dc7b7abdd27f14ddfa6c156aaa77
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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 */
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/netfilter.h>
12 #include <linux/in.h>
13 #include <linux/icmp.h>
14 #include <linux/seq_file.h>
15 #include <net/ip.h>
16 #include <net/checksum.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <net/netfilter/nf_conntrack_tuple.h>
19 #include <net/netfilter/nf_conntrack_l4proto.h>
20 #include <net/netfilter/nf_conntrack_core.h>
22 static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(format, args...)
28 #endif
30 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
31 unsigned int dataoff,
32 struct nf_conntrack_tuple *tuple)
34 struct icmphdr _hdr, *hp;
36 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
37 if (hp == NULL)
38 return 0;
40 tuple->dst.u.icmp.type = hp->type;
41 tuple->src.u.icmp.id = hp->un.echo.id;
42 tuple->dst.u.icmp.code = hp->code;
44 return 1;
47 /* Add 1; spaces filled with 0. */
48 static const u_int8_t invmap[] = {
49 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
50 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
51 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
52 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
53 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
54 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
55 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
56 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
59 static int icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
60 const struct nf_conntrack_tuple *orig)
62 if (orig->dst.u.icmp.type >= sizeof(invmap)
63 || !invmap[orig->dst.u.icmp.type])
64 return 0;
66 tuple->src.u.icmp.id = orig->src.u.icmp.id;
67 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
68 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
69 return 1;
72 /* Print out the per-protocol part of the tuple. */
73 static int icmp_print_tuple(struct seq_file *s,
74 const struct nf_conntrack_tuple *tuple)
76 return seq_printf(s, "type=%u code=%u id=%u ",
77 tuple->dst.u.icmp.type,
78 tuple->dst.u.icmp.code,
79 ntohs(tuple->src.u.icmp.id));
82 /* Returns verdict for packet, or -1 for invalid. */
83 static int icmp_packet(struct nf_conn *ct,
84 const struct sk_buff *skb,
85 unsigned int dataoff,
86 enum ip_conntrack_info ctinfo,
87 int pf,
88 unsigned int hooknum)
90 /* Try to delete connection immediately after all replies:
91 won't actually vanish as we still have skb, and del_timer
92 means this will only run once even if count hits zero twice
93 (theoretically possible with SMP) */
94 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
95 if (atomic_dec_and_test(&ct->proto.icmp.count)
96 && del_timer(&ct->timeout))
97 ct->timeout.function((unsigned long)ct);
98 } else {
99 atomic_inc(&ct->proto.icmp.count);
100 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
101 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
104 return NF_ACCEPT;
107 /* Called when a new connection for this protocol found. */
108 static int icmp_new(struct nf_conn *conntrack,
109 const struct sk_buff *skb, unsigned int dataoff)
111 static const u_int8_t valid_new[] = {
112 [ICMP_ECHO] = 1,
113 [ICMP_TIMESTAMP] = 1,
114 [ICMP_INFO_REQUEST] = 1,
115 [ICMP_ADDRESS] = 1
118 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
119 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
120 /* Can't create a new ICMP `conn' with this. */
121 DEBUGP("icmp: can't create new conn with type %u\n",
122 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
123 NF_CT_DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
124 return 0;
126 atomic_set(&conntrack->proto.icmp.count, 0);
127 return 1;
130 extern struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv4;
131 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
132 static int
133 icmp_error_message(struct sk_buff *skb,
134 enum ip_conntrack_info *ctinfo,
135 unsigned int hooknum)
137 struct nf_conntrack_tuple innertuple, origtuple;
138 struct {
139 struct icmphdr icmp;
140 struct iphdr ip;
141 } _in, *inside;
142 struct nf_conntrack_l4proto *innerproto;
143 struct nf_conntrack_tuple_hash *h;
144 int dataoff;
146 NF_CT_ASSERT(skb->nfct == NULL);
148 /* Not enough header? */
149 inside = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_in), &_in);
150 if (inside == NULL)
151 return -NF_ACCEPT;
153 /* Ignore ICMP's containing fragments (shouldn't happen) */
154 if (inside->ip.frag_off & htons(IP_OFFSET)) {
155 DEBUGP("icmp_error_message: fragment of proto %u\n",
156 inside->ip.protocol);
157 return -NF_ACCEPT;
160 /* rcu_read_lock()ed by nf_hook_slow */
161 innerproto = __nf_ct_l4proto_find(PF_INET, inside->ip.protocol);
163 dataoff = ip_hdrlen(skb) + sizeof(inside->icmp);
164 /* Are they talking about one of our connections? */
165 if (!nf_ct_get_tuple(skb, dataoff, dataoff + inside->ip.ihl*4, PF_INET,
166 inside->ip.protocol, &origtuple,
167 &nf_conntrack_l3proto_ipv4, innerproto)) {
168 DEBUGP("icmp_error_message: ! get_tuple p=%u",
169 inside->ip.protocol);
170 return -NF_ACCEPT;
173 /* Ordinarily, we'd expect the inverted tupleproto, but it's
174 been preserved inside the ICMP. */
175 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
176 &nf_conntrack_l3proto_ipv4, innerproto)) {
177 DEBUGP("icmp_error_message: no match\n");
178 return -NF_ACCEPT;
181 *ctinfo = IP_CT_RELATED;
183 h = nf_conntrack_find_get(&innertuple, NULL);
184 if (!h) {
185 DEBUGP("icmp_error_message: no match\n");
186 return -NF_ACCEPT;
189 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
190 *ctinfo += IP_CT_IS_REPLY;
192 /* Update skb to refer to this connection */
193 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
194 skb->nfctinfo = *ctinfo;
195 return -NF_ACCEPT;
198 /* Small and modified version of icmp_rcv */
199 static int
200 icmp_error(struct sk_buff *skb, unsigned int dataoff,
201 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
203 struct icmphdr _ih, *icmph;
205 /* Not enough header? */
206 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
207 if (icmph == NULL) {
208 if (LOG_INVALID(IPPROTO_ICMP))
209 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
210 "nf_ct_icmp: short packet ");
211 return -NF_ACCEPT;
214 /* See ip_conntrack_proto_tcp.c */
215 if (nf_conntrack_checksum && hooknum == NF_IP_PRE_ROUTING &&
216 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
217 if (LOG_INVALID(IPPROTO_ICMP))
218 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
219 "nf_ct_icmp: bad HW ICMP checksum ");
220 return -NF_ACCEPT;
224 * 18 is the highest 'known' ICMP type. Anything else is a mystery
226 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
227 * discarded.
229 if (icmph->type > NR_ICMP_TYPES) {
230 if (LOG_INVALID(IPPROTO_ICMP))
231 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
232 "nf_ct_icmp: invalid ICMP type ");
233 return -NF_ACCEPT;
236 /* Need to track icmp error message? */
237 if (icmph->type != ICMP_DEST_UNREACH
238 && icmph->type != ICMP_SOURCE_QUENCH
239 && icmph->type != ICMP_TIME_EXCEEDED
240 && icmph->type != ICMP_PARAMETERPROB
241 && icmph->type != ICMP_REDIRECT)
242 return NF_ACCEPT;
244 return icmp_error_message(skb, ctinfo, hooknum);
247 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
249 #include <linux/netfilter/nfnetlink.h>
250 #include <linux/netfilter/nfnetlink_conntrack.h>
252 static int icmp_tuple_to_nfattr(struct sk_buff *skb,
253 const struct nf_conntrack_tuple *t)
255 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
256 &t->src.u.icmp.id);
257 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
258 &t->dst.u.icmp.type);
259 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
260 &t->dst.u.icmp.code);
262 return 0;
264 nfattr_failure:
265 return -1;
268 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
269 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
270 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
271 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t)
274 static int icmp_nfattr_to_tuple(struct nfattr *tb[],
275 struct nf_conntrack_tuple *tuple)
277 if (!tb[CTA_PROTO_ICMP_TYPE-1]
278 || !tb[CTA_PROTO_ICMP_CODE-1]
279 || !tb[CTA_PROTO_ICMP_ID-1])
280 return -EINVAL;
282 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
283 return -EINVAL;
285 tuple->dst.u.icmp.type =
286 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
287 tuple->dst.u.icmp.code =
288 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
289 tuple->src.u.icmp.id =
290 *(__be16 *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
292 if (tuple->dst.u.icmp.type >= sizeof(invmap)
293 || !invmap[tuple->dst.u.icmp.type])
294 return -EINVAL;
296 return 0;
298 #endif
300 #ifdef CONFIG_SYSCTL
301 static struct ctl_table_header *icmp_sysctl_header;
302 static struct ctl_table icmp_sysctl_table[] = {
304 .ctl_name = NET_NF_CONNTRACK_ICMP_TIMEOUT,
305 .procname = "nf_conntrack_icmp_timeout",
306 .data = &nf_ct_icmp_timeout,
307 .maxlen = sizeof(unsigned int),
308 .mode = 0644,
309 .proc_handler = &proc_dointvec_jiffies,
312 .ctl_name = 0
315 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
316 static struct ctl_table icmp_compat_sysctl_table[] = {
318 .ctl_name = NET_IPV4_NF_CONNTRACK_ICMP_TIMEOUT,
319 .procname = "ip_conntrack_icmp_timeout",
320 .data = &nf_ct_icmp_timeout,
321 .maxlen = sizeof(unsigned int),
322 .mode = 0644,
323 .proc_handler = &proc_dointvec_jiffies,
326 .ctl_name = 0
329 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
330 #endif /* CONFIG_SYSCTL */
332 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
334 .l3proto = PF_INET,
335 .l4proto = IPPROTO_ICMP,
336 .name = "icmp",
337 .pkt_to_tuple = icmp_pkt_to_tuple,
338 .invert_tuple = icmp_invert_tuple,
339 .print_tuple = icmp_print_tuple,
340 .packet = icmp_packet,
341 .new = icmp_new,
342 .error = icmp_error,
343 .destroy = NULL,
344 .me = NULL,
345 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
346 .tuple_to_nfattr = icmp_tuple_to_nfattr,
347 .nfattr_to_tuple = icmp_nfattr_to_tuple,
348 #endif
349 #ifdef CONFIG_SYSCTL
350 .ctl_table_header = &icmp_sysctl_header,
351 .ctl_table = icmp_sysctl_table,
352 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
353 .ctl_compat_table = icmp_compat_sysctl_table,
354 #endif
355 #endif
357 EXPORT_SYMBOL_GPL(nf_conntrack_l4proto_icmp);