Linux-2.6.12-rc2
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / net / ipv4 / netfilter / ip_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/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>
16 #include <net/ip.h>
17 #include <net/checksum.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter_ipv4/ip_conntrack.h>
21 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
22 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
23
24 unsigned long ip_ct_icmp_timeout = 30*HZ;
25
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
31
32 static int icmp_pkt_to_tuple(const struct sk_buff *skb,
33 unsigned int dataoff,
34 struct ip_conntrack_tuple *tuple)
35 {
36 struct icmphdr _hdr, *hp;
37
38 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
39 if (hp == NULL)
40 return 0;
41
42 tuple->dst.u.icmp.type = hp->type;
43 tuple->src.u.icmp.id = hp->un.echo.id;
44 tuple->dst.u.icmp.code = hp->code;
45
46 return 1;
47 }
48
49 static int icmp_invert_tuple(struct ip_conntrack_tuple *tuple,
50 const struct ip_conntrack_tuple *orig)
51 {
52 /* Add 1; spaces filled with 0. */
53 static u_int8_t invmap[]
54 = { [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
55 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
56 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
57 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
58 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
59 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
60 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
61 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1};
62
63 if (orig->dst.u.icmp.type >= sizeof(invmap)
64 || !invmap[orig->dst.u.icmp.type])
65 return 0;
66
67 tuple->src.u.icmp.id = orig->src.u.icmp.id;
68 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
69 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
70 return 1;
71 }
72
73 /* Print out the per-protocol part of the tuple. */
74 static int icmp_print_tuple(struct seq_file *s,
75 const struct ip_conntrack_tuple *tuple)
76 {
77 return seq_printf(s, "type=%u code=%u id=%u ",
78 tuple->dst.u.icmp.type,
79 tuple->dst.u.icmp.code,
80 ntohs(tuple->src.u.icmp.id));
81 }
82
83 /* Print out the private part of the conntrack. */
84 static int icmp_print_conntrack(struct seq_file *s,
85 const struct ip_conntrack *conntrack)
86 {
87 return 0;
88 }
89
90 /* Returns verdict for packet, or -1 for invalid. */
91 static int icmp_packet(struct ip_conntrack *ct,
92 const struct sk_buff *skb,
93 enum ip_conntrack_info ctinfo)
94 {
95 /* Try to delete connection immediately after all replies:
96 won't actually vanish as we still have skb, and del_timer
97 means this will only run once even if count hits zero twice
98 (theoretically possible with SMP) */
99 if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) {
100 if (atomic_dec_and_test(&ct->proto.icmp.count)
101 && del_timer(&ct->timeout))
102 ct->timeout.function((unsigned long)ct);
103 } else {
104 atomic_inc(&ct->proto.icmp.count);
105 ip_ct_refresh_acct(ct, ctinfo, skb, ip_ct_icmp_timeout);
106 }
107
108 return NF_ACCEPT;
109 }
110
111 /* Called when a new connection for this protocol found. */
112 static int icmp_new(struct ip_conntrack *conntrack,
113 const struct sk_buff *skb)
114 {
115 static u_int8_t valid_new[]
116 = { [ICMP_ECHO] = 1,
117 [ICMP_TIMESTAMP] = 1,
118 [ICMP_INFO_REQUEST] = 1,
119 [ICMP_ADDRESS] = 1 };
120
121 if (conntrack->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new)
122 || !valid_new[conntrack->tuplehash[0].tuple.dst.u.icmp.type]) {
123 /* Can't create a new ICMP `conn' with this. */
124 DEBUGP("icmp: can't create new conn with type %u\n",
125 conntrack->tuplehash[0].tuple.dst.u.icmp.type);
126 DUMP_TUPLE(&conntrack->tuplehash[0].tuple);
127 return 0;
128 }
129 atomic_set(&conntrack->proto.icmp.count, 0);
130 return 1;
131 }
132
133 static int
134 icmp_error_message(struct sk_buff *skb,
135 enum ip_conntrack_info *ctinfo,
136 unsigned int hooknum)
137 {
138 struct ip_conntrack_tuple innertuple, origtuple;
139 struct {
140 struct icmphdr icmp;
141 struct iphdr ip;
142 } _in, *inside;
143 struct ip_conntrack_protocol *innerproto;
144 struct ip_conntrack_tuple_hash *h;
145 int dataoff;
146
147 IP_NF_ASSERT(skb->nfct == NULL);
148
149 /* Not enough header? */
150 inside = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_in), &_in);
151 if (inside == NULL)
152 return NF_ACCEPT;
153
154 /* Ignore ICMP's containing fragments (shouldn't happen) */
155 if (inside->ip.frag_off & htons(IP_OFFSET)) {
156 DEBUGP("icmp_error_track: fragment of proto %u\n",
157 inside->ip.protocol);
158 return NF_ACCEPT;
159 }
160
161 innerproto = ip_ct_find_proto(inside->ip.protocol);
162 dataoff = skb->nh.iph->ihl*4 + sizeof(inside->icmp) + inside->ip.ihl*4;
163 /* Are they talking about one of our connections? */
164 if (!ip_ct_get_tuple(&inside->ip, skb, dataoff, &origtuple, innerproto)) {
165 DEBUGP("icmp_error: ! get_tuple p=%u", inside->ip.protocol);
166 return NF_ACCEPT;
167 }
168
169 /* Ordinarily, we'd expect the inverted tupleproto, but it's
170 been preserved inside the ICMP. */
171 if (!ip_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
172 DEBUGP("icmp_error_track: Can't invert tuple\n");
173 return NF_ACCEPT;
174 }
175
176 *ctinfo = IP_CT_RELATED;
177
178 h = ip_conntrack_find_get(&innertuple, NULL);
179 if (!h) {
180 /* Locally generated ICMPs will match inverted if they
181 haven't been SNAT'ed yet */
182 /* FIXME: NAT code has to handle half-done double NAT --RR */
183 if (hooknum == NF_IP_LOCAL_OUT)
184 h = ip_conntrack_find_get(&origtuple, NULL);
185
186 if (!h) {
187 DEBUGP("icmp_error_track: no match\n");
188 return NF_ACCEPT;
189 }
190 /* Reverse direction from that found */
191 if (DIRECTION(h) != IP_CT_DIR_REPLY)
192 *ctinfo += IP_CT_IS_REPLY;
193 } else {
194 if (DIRECTION(h) == IP_CT_DIR_REPLY)
195 *ctinfo += IP_CT_IS_REPLY;
196 }
197
198 /* Update skb to refer to this connection */
199 skb->nfct = &tuplehash_to_ctrack(h)->ct_general;
200 skb->nfctinfo = *ctinfo;
201 return -NF_ACCEPT;
202 }
203
204 /* Small and modified version of icmp_rcv */
205 static int
206 icmp_error(struct sk_buff *skb, enum ip_conntrack_info *ctinfo,
207 unsigned int hooknum)
208 {
209 struct icmphdr _ih, *icmph;
210
211 /* Not enough header? */
212 icmph = skb_header_pointer(skb, skb->nh.iph->ihl*4, sizeof(_ih), &_ih);
213 if (icmph == NULL) {
214 if (LOG_INVALID(IPPROTO_ICMP))
215 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
216 "ip_ct_icmp: short packet ");
217 return -NF_ACCEPT;
218 }
219
220 /* See ip_conntrack_proto_tcp.c */
221 if (hooknum != NF_IP_PRE_ROUTING)
222 goto checksum_skipped;
223
224 switch (skb->ip_summed) {
225 case CHECKSUM_HW:
226 if (!(u16)csum_fold(skb->csum))
227 break;
228 if (LOG_INVALID(IPPROTO_ICMP))
229 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
230 "ip_ct_icmp: bad HW ICMP checksum ");
231 return -NF_ACCEPT;
232 case CHECKSUM_NONE:
233 if ((u16)csum_fold(skb_checksum(skb, 0, skb->len, 0))) {
234 if (LOG_INVALID(IPPROTO_ICMP))
235 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
236 "ip_ct_icmp: bad ICMP checksum ");
237 return -NF_ACCEPT;
238 }
239 default:
240 break;
241 }
242
243 checksum_skipped:
244 /*
245 * 18 is the highest 'known' ICMP type. Anything else is a mystery
246 *
247 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
248 * discarded.
249 */
250 if (icmph->type > NR_ICMP_TYPES) {
251 if (LOG_INVALID(IPPROTO_ICMP))
252 nf_log_packet(PF_INET, 0, skb, NULL, NULL,
253 "ip_ct_icmp: invalid ICMP type ");
254 return -NF_ACCEPT;
255 }
256
257 /* Need to track icmp error message? */
258 if (icmph->type != ICMP_DEST_UNREACH
259 && icmph->type != ICMP_SOURCE_QUENCH
260 && icmph->type != ICMP_TIME_EXCEEDED
261 && icmph->type != ICMP_PARAMETERPROB
262 && icmph->type != ICMP_REDIRECT)
263 return NF_ACCEPT;
264
265 return icmp_error_message(skb, ctinfo, hooknum);
266 }
267
268 struct ip_conntrack_protocol ip_conntrack_protocol_icmp =
269 {
270 .proto = IPPROTO_ICMP,
271 .name = "icmp",
272 .pkt_to_tuple = icmp_pkt_to_tuple,
273 .invert_tuple = icmp_invert_tuple,
274 .print_tuple = icmp_print_tuple,
275 .print_conntrack = icmp_print_conntrack,
276 .packet = icmp_packet,
277 .new = icmp_new,
278 .error = icmp_error,
279 };