[NET]: Detect hardware rx checksum faults correctly
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / ipv4 / netfilter / ip_conntrack_proto_icmp.c
CommitLineData
1da177e4
LT
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/sched.h>
11#include <linux/timer.h>
12#include <linux/netfilter.h>
13#include <linux/in.h>
14#include <linux/icmp.h>
15#include <linux/seq_file.h>
fb286bb2 16#include <linux/skbuff.h>
1da177e4
LT
17#include <net/ip.h>
18#include <net/checksum.h>
19#include <linux/netfilter.h>
20#include <linux/netfilter_ipv4.h>
21#include <linux/netfilter_ipv4/ip_conntrack.h>
22#include <linux/netfilter_ipv4/ip_conntrack_core.h>
23#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
24
25unsigned long ip_ct_icmp_timeout = 30*HZ;
26
27#if 0
28#define DEBUGP printk
29#else
30#define DEBUGP(format, args...)
31#endif
32
33static int icmp_pkt_to_tuple(const struct sk_buff *skb,
34 unsigned int dataoff,
35 struct ip_conntrack_tuple *tuple)
36{
37 struct icmphdr _hdr, *hp;
38
39 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
40 if (hp == NULL)
41 return 0;
42
43 tuple->dst.u.icmp.type = hp->type;
44 tuple->src.u.icmp.id = hp->un.echo.id;
45 tuple->dst.u.icmp.code = hp->code;
46
47 return 1;
48}
49
50static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
51 const struct ip_conntrack_tuple *orig)
52{
53 /* Add 1; spaces filled with 0. */
54 static 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 if (orig->dst.u.icmp.type >= sizeof(invmap)
65 || !invmap[orig->dst.u.icmp.type])
66 return 0;
67
68 tuple->src.u.icmp.id = orig->src.u.icmp.id;
69 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
70 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
71 return 1;
72}
73
74/* Print out the per-protocol part of the tuple. */
75static int icmp_print_tuple(struct seq_file *s,
76 const struct ip_conntrack_tuple *tuple)
77{
78 return seq_printf(s, "type=%u code=%u id=%u ",
79 tuple->dst.u.icmp.type,
80 tuple->dst.u.icmp.code,
81 ntohs(tuple->src.u.icmp.id));
82}
83
84/* Print out the private part of the conntrack. */
85static int icmp_print_conntrack(struct seq_file *s,
86 const struct ip_conntrack *conntrack)
87{
88 return 0;
89}
90
91/* Returns verdict for packet, or -1 for invalid. */
92static int icmp_packet(struct ip_conntrack *ct,
93 const struct sk_buff *skb,
94 enum ip_conntrack_info ctinfo)
95{
96 /* Try to delete connection immediately after all replies:
97 won't actually vanish as we still have skb, and del_timer
98 means this will only run once even if count hits zero twice
99 (theoretically possible with SMP) */
100 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
101 if (atomic_dec_and_test(&ct->proto.icmp.count)
102 && del_timer(&ct->timeout))
103 ct->timeout.function((unsigned long)ct);
104 } else {
105 atomic_inc(&ct->proto.icmp.count);
ac3247ba 106 ip_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb);
1da177e4
LT
107 ip_ct_refresh_acct(ct, ctinfo, skb, ip_ct_icmp_timeout);
108 }
109
110 return NF_ACCEPT;
111}
112
080774a2
HW
113static u_int8_t valid_new[] = {
114 [ICMP_ECHO] = 1,
115 [ICMP_TIMESTAMP] = 1,
116 [ICMP_INFO_REQUEST] = 1,
117 [ICMP_ADDRESS] = 1
118};
119
1da177e4
LT
120/* Called when a new connection for this protocol found. */
121static int icmp_new(struct ip_conntrack *conntrack,
122 const struct sk_buff *skb)
123{
1da177e4
LT
124 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
125 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
126 /* Can't create a new ICMP `conn' with this. */
127 DEBUGP("icmp: can't create new conn with type %u\n",
128 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
129 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
130 return 0;
131 }
132 atomic_set(&conntrack->proto.icmp.count, 0);
133 return 1;
134}
135
136static int
137icmp_error_message(struct sk_buff *skb,
138 enum ip_conntrack_info *ctinfo,
139 unsigned int hooknum)
140{
141 struct ip_conntrack_tuple innertuple, origtuple;
142 struct {
143 struct icmphdr icmp;
144 struct iphdr ip;
145 } _in, *inside;
146 struct ip_conntrack_protocol *innerproto;
147 struct ip_conntrack_tuple_hash *h;
148 int dataoff;
149
150 IP_NF_ASSERT(skb->nfct == NULL);
151
152 /* Not enough header? */
153 inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
154 if (inside == NULL)
d63a9281 155 return -NF_ACCEPT;
1da177e4
LT
156
157 /* Ignore ICMP's containing fragments (shouldn't happen) */
158 if (inside->ip.frag_off & htons(IP_OFFSET)) {
159 DEBUGP("icmp_error_track: fragment of proto %u\n",
160 inside->ip.protocol);
d63a9281 161 return -NF_ACCEPT;
1da177e4
LT
162 }
163
080774a2 164 innerproto = ip_conntrack_proto_find_get(inside->ip.protocol);
1da177e4
LT
165 dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4;
166 /* Are they talking about one of our connections? */
167 if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) {
168 DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol);
080774a2 169 ip_conntrack_proto_put(innerproto);
d63a9281 170 return -NF_ACCEPT;
1da177e4
LT
171 }
172
173 /* Ordinarily, we'd expect the inverted tupleproto, but it's
174 been preserved inside the ICMP. */
175 if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
176 DEBUGP("icmp_error_track: Can't invert tuple\n");
080774a2 177 ip_conntrack_proto_put(innerproto);
d63a9281 178 return -NF_ACCEPT;
1da177e4 179 }
080774a2 180 ip_conntrack_proto_put(innerproto);
1da177e4
LT
181
182 *ctinfo = IP_CT_RELATED;
183
184 h = ip_conntrack_find_get(&innertuple, NULL);
185 if (!h) {
186 /* Locally generated ICMPs will match inverted if they
187 haven't been SNAT'ed yet */
188 /* FIXME: NAT code has to handle half-done double NAT --RR */
189 if (hooknum == NF_IP_LOCAL_OUT)
190 h = ip_conntrack_find_get(&origtuple, NULL);
191
192 if (!h) {
193 DEBUGP("icmp_error_track: no match\n");
d63a9281 194 return -NF_ACCEPT;
1da177e4
LT
195 }
196 /* Reverse direction from that found */
197 if (DIRECTION(h) != IP_CT_DIR_REPLY)
198 *ctinfo += IP_CT_IS_REPLY;
199 } else {
200 if (DIRECTION(h) == IP_CT_DIR_REPLY)
201 *ctinfo += IP_CT_IS_REPLY;
202 }
203
204 /* Update skb to refer to this connection */
205 skb->nfct = &tuplehash_to_ctrack(h)->ct_general;
206 skb->nfctinfo = *ctinfo;
207 return -NF_ACCEPT;
208}
209
210/* Small and modified version of icmp_rcv */
211static int
212icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
213 unsigned int hooknum)
214{
215 struct icmphdr _ih, *icmph;
216
217 /* Not enough header? */
218 icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
219 if (icmph == NULL) {
220 if (LOG_INVALID(IPPROTO_ICMP))
608c8e4f 221 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
1da177e4
LT
222 "ip_ct_icmp: short packet ");
223 return -NF_ACCEPT;
224 }
225
226 /* See ip_conntrack_proto_tcp.c */
227 if (hooknum != NF_IP_PRE_ROUTING)
228 goto checksum_skipped;
229
230 switch (skb->ip_summed) {
231 case CHECKSUM_HW:
232 if (!(u16)csum_fold(skb->csum))
233 break;
fb286bb2 234 /* fall through */
1da177e4 235 case CHECKSUM_NONE:
fb286bb2
HX
236 skb->csum = 0;
237 if (__skb_checksum_complete(skb)) {
1da177e4 238 if (LOG_INVALID(IPPROTO_ICMP))
608c8e4f 239 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
1da177e4
LT
240 "ip_ct_icmp: bad ICMP checksum ");
241 return -NF_ACCEPT;
242 }
1da177e4
LT
243 }
244
245checksum_skipped:
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))
608c8e4f 254 nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL,
1da177e4
LT
255 "ip_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
080774a2
HW
270#if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
271 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
272static int icmp_tuple_to_nfattr(struct sk_buff *skb,
273 const struct ip_conntrack_tuple *t)
274{
275 NFA_PUT(skb, CTA_PROTO_ICMP_ID, sizeof(u_int16_t),
276 &t->src.u.icmp.id);
277 NFA_PUT(skb, CTA_PROTO_ICMP_TYPE, sizeof(u_int8_t),
278 &t->dst.u.icmp.type);
279 NFA_PUT(skb, CTA_PROTO_ICMP_CODE, sizeof(u_int8_t),
280 &t->dst.u.icmp.code);
281
282 if (t->dst.u.icmp.type >= sizeof(valid_new)
283 || !valid_new[t->dst.u.icmp.type])
284 return -EINVAL;
285
286 return 0;
287
288nfattr_failure:
289 return -1;
290}
291
292static int icmp_nfattr_to_tuple(struct nfattr *tb[],
293 struct ip_conntrack_tuple *tuple)
294{
295 if (!tb[CTA_PROTO_ICMP_TYPE-1]
439a9994
KPO
296 || !tb[CTA_PROTO_ICMP_CODE-1]
297 || !tb[CTA_PROTO_ICMP_ID-1])
080774a2
HW
298 return -1;
299
300 tuple->dst.u.icmp.type =
301 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_TYPE-1]);
302 tuple->dst.u.icmp.code =
303 *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_ICMP_CODE-1]);
304 tuple->src.u.icmp.id =
5fd52fe0 305 *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_ICMP_ID-1]);
080774a2
HW
306
307 return 0;
308}
309#endif
310
1da177e4
LT
311struct ip_conntrack_protocol ip_conntrack_protocol_icmp =
312{
313 .proto = IPPROTO_ICMP,
314 .name = "icmp",
315 .pkt_to_tuple = icmp_pkt_to_tuple,
316 .invert_tuple = icmp_invert_tuple,
317 .print_tuple = icmp_print_tuple,
318 .print_conntrack = icmp_print_conntrack,
319 .packet = icmp_packet,
320 .new = icmp_new,
321 .error = icmp_error,
080774a2
HW
322#if defined(CONFIG_IP_NF_CONNTRACK_NETLINK) || \
323 defined(CONFIG_IP_NF_CONNTRACK_NETLINK_MODULE)
324 .tuple_to_nfattr = icmp_tuple_to_nfattr,
325 .nfattr_to_tuple = icmp_nfattr_to_tuple,
326#endif
1da177e4 327};