Merge git://git.infradead.org/battery-2.6
[deliverable/linux.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
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>
21 #include <net/netfilter/nf_log.h>
22
23 static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ;
24
25 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
26 struct nf_conntrack_tuple *tuple)
27 {
28 const struct icmphdr *hp;
29 struct icmphdr _hdr;
30
31 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
32 if (hp == NULL)
33 return false;
34
35 tuple->dst.u.icmp.type = hp->type;
36 tuple->src.u.icmp.id = hp->un.echo.id;
37 tuple->dst.u.icmp.code = hp->code;
38
39 return true;
40 }
41
42 /* Add 1; spaces filled with 0. */
43 static const u_int8_t invmap[] = {
44 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
45 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
46 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
47 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
48 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
49 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
50 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
51 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
52 };
53
54 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
55 const struct nf_conntrack_tuple *orig)
56 {
57 if (orig->dst.u.icmp.type >= sizeof(invmap)
58 || !invmap[orig->dst.u.icmp.type])
59 return false;
60
61 tuple->src.u.icmp.id = orig->src.u.icmp.id;
62 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
63 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
64 return true;
65 }
66
67 /* Print out the per-protocol part of the tuple. */
68 static int icmp_print_tuple(struct seq_file *s,
69 const struct nf_conntrack_tuple *tuple)
70 {
71 return seq_printf(s, "type=%u code=%u id=%u ",
72 tuple->dst.u.icmp.type,
73 tuple->dst.u.icmp.code,
74 ntohs(tuple->src.u.icmp.id));
75 }
76
77 /* Returns verdict for packet, or -1 for invalid. */
78 static int icmp_packet(struct nf_conn *ct,
79 const struct sk_buff *skb,
80 unsigned int dataoff,
81 enum ip_conntrack_info ctinfo,
82 int pf,
83 unsigned int hooknum)
84 {
85 /* Try to delete connection immediately after all replies:
86 won't actually vanish as we still have skb, and del_timer
87 means this will only run once even if count hits zero twice
88 (theoretically possible with SMP) */
89 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
90 if (atomic_dec_and_test(&ct->proto.icmp.count)
91 && del_timer(&ct->timeout))
92 ct->timeout.function((unsigned long)ct);
93 } else {
94 atomic_inc(&ct->proto.icmp.count);
95 nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
96 nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout);
97 }
98
99 return NF_ACCEPT;
100 }
101
102 /* Called when a new connection for this protocol found. */
103 static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
104 unsigned int dataoff)
105 {
106 static const u_int8_t valid_new[] = {
107 [ICMP_ECHO] = 1,
108 [ICMP_TIMESTAMP] = 1,
109 [ICMP_INFO_REQUEST] = 1,
110 [ICMP_ADDRESS] = 1
111 };
112
113 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
114 || !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
115 /* Can't create a new ICMP `conn' with this. */
116 pr_debug("icmp: can't create new conn with type %u\n",
117 ct->tuplehash[0].tuple.dst.u.icmp.type);
118 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
119 return false;
120 }
121 atomic_set(&ct->proto.icmp.count, 0);
122 return true;
123 }
124
125 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
126 static int
127 icmp_error_message(struct sk_buff *skb,
128 enum ip_conntrack_info *ctinfo,
129 unsigned int hooknum)
130 {
131 struct nf_conntrack_tuple innertuple, origtuple;
132 const struct nf_conntrack_l4proto *innerproto;
133 const struct nf_conntrack_tuple_hash *h;
134
135 NF_CT_ASSERT(skb->nfct == NULL);
136
137 /* Are they talking about one of our connections? */
138 if (!nf_ct_get_tuplepr(skb,
139 skb_network_offset(skb) + ip_hdrlen(skb)
140 + sizeof(struct icmphdr),
141 PF_INET, &origtuple)) {
142 pr_debug("icmp_error_message: failed to get tuple\n");
143 return -NF_ACCEPT;
144 }
145
146 /* rcu_read_lock()ed by nf_hook_slow */
147 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
148
149 /* Ordinarily, we'd expect the inverted tupleproto, but it's
150 been preserved inside the ICMP. */
151 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
152 &nf_conntrack_l3proto_ipv4, innerproto)) {
153 pr_debug("icmp_error_message: no match\n");
154 return -NF_ACCEPT;
155 }
156
157 *ctinfo = IP_CT_RELATED;
158
159 h = nf_conntrack_find_get(&innertuple);
160 if (!h) {
161 pr_debug("icmp_error_message: no match\n");
162 return -NF_ACCEPT;
163 }
164
165 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
166 *ctinfo += IP_CT_IS_REPLY;
167
168 /* Update skb to refer to this connection */
169 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
170 skb->nfctinfo = *ctinfo;
171 return -NF_ACCEPT;
172 }
173
174 /* Small and modified version of icmp_rcv */
175 static int
176 icmp_error(struct sk_buff *skb, unsigned int dataoff,
177 enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum)
178 {
179 const struct icmphdr *icmph;
180 struct icmphdr _ih;
181
182 /* Not enough header? */
183 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
184 if (icmph == NULL) {
185 if (LOG_INVALID(IPPROTO_ICMP))
186 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
187 "nf_ct_icmp: short packet ");
188 return -NF_ACCEPT;
189 }
190
191 /* See ip_conntrack_proto_tcp.c */
192 if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING &&
193 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
194 if (LOG_INVALID(IPPROTO_ICMP))
195 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
196 "nf_ct_icmp: bad HW ICMP checksum ");
197 return -NF_ACCEPT;
198 }
199
200 /*
201 * 18 is the highest 'known' ICMP type. Anything else is a mystery
202 *
203 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
204 * discarded.
205 */
206 if (icmph->type > NR_ICMP_TYPES) {
207 if (LOG_INVALID(IPPROTO_ICMP))
208 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
209 "nf_ct_icmp: invalid ICMP type ");
210 return -NF_ACCEPT;
211 }
212
213 /* Need to track icmp error message? */
214 if (icmph->type != ICMP_DEST_UNREACH
215 && icmph->type != ICMP_SOURCE_QUENCH
216 && icmph->type != ICMP_TIME_EXCEEDED
217 && icmph->type != ICMP_PARAMETERPROB
218 && icmph->type != ICMP_REDIRECT)
219 return NF_ACCEPT;
220
221 return icmp_error_message(skb, ctinfo, hooknum);
222 }
223
224 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
225
226 #include <linux/netfilter/nfnetlink.h>
227 #include <linux/netfilter/nfnetlink_conntrack.h>
228
229 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
230 const struct nf_conntrack_tuple *t)
231 {
232 NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id);
233 NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type);
234 NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code);
235
236 return 0;
237
238 nla_put_failure:
239 return -1;
240 }
241
242 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
243 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
244 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
245 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
246 };
247
248 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
249 struct nf_conntrack_tuple *tuple)
250 {
251 if (!tb[CTA_PROTO_ICMP_TYPE]
252 || !tb[CTA_PROTO_ICMP_CODE]
253 || !tb[CTA_PROTO_ICMP_ID])
254 return -EINVAL;
255
256 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
257 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
258 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
259
260 if (tuple->dst.u.icmp.type >= sizeof(invmap)
261 || !invmap[tuple->dst.u.icmp.type])
262 return -EINVAL;
263
264 return 0;
265 }
266 #endif
267
268 #ifdef CONFIG_SYSCTL
269 static struct ctl_table_header *icmp_sysctl_header;
270 static struct ctl_table icmp_sysctl_table[] = {
271 {
272 .procname = "nf_conntrack_icmp_timeout",
273 .data = &nf_ct_icmp_timeout,
274 .maxlen = sizeof(unsigned int),
275 .mode = 0644,
276 .proc_handler = &proc_dointvec_jiffies,
277 },
278 {
279 .ctl_name = 0
280 }
281 };
282 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
283 static struct ctl_table icmp_compat_sysctl_table[] = {
284 {
285 .procname = "ip_conntrack_icmp_timeout",
286 .data = &nf_ct_icmp_timeout,
287 .maxlen = sizeof(unsigned int),
288 .mode = 0644,
289 .proc_handler = &proc_dointvec_jiffies,
290 },
291 {
292 .ctl_name = 0
293 }
294 };
295 #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
296 #endif /* CONFIG_SYSCTL */
297
298 struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
299 {
300 .l3proto = PF_INET,
301 .l4proto = IPPROTO_ICMP,
302 .name = "icmp",
303 .pkt_to_tuple = icmp_pkt_to_tuple,
304 .invert_tuple = icmp_invert_tuple,
305 .print_tuple = icmp_print_tuple,
306 .packet = icmp_packet,
307 .new = icmp_new,
308 .error = icmp_error,
309 .destroy = NULL,
310 .me = NULL,
311 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
312 .tuple_to_nlattr = icmp_tuple_to_nlattr,
313 .nlattr_to_tuple = icmp_nlattr_to_tuple,
314 .nla_policy = icmp_nla_policy,
315 #endif
316 #ifdef CONFIG_SYSCTL
317 .ctl_table_header = &icmp_sysctl_header,
318 .ctl_table = icmp_sysctl_table,
319 #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
320 .ctl_compat_table = icmp_compat_sysctl_table,
321 #endif
322 #endif
323 };
This page took 0.100579 seconds and 6 git commands to generate.