Commit | Line | Data |
---|---|---|
9fb9cbb1 YK |
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. | |
9fb9cbb1 YK |
7 | */ |
8 | ||
9 | #include <linux/types.h> | |
9fb9cbb1 YK |
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> | |
605dcad6 | 19 | #include <net/netfilter/nf_conntrack_l4proto.h> |
9fb9cbb1 | 20 | #include <net/netfilter/nf_conntrack_core.h> |
f01ffbd6 | 21 | #include <net/netfilter/nf_log.h> |
9fb9cbb1 | 22 | |
933a41e7 | 23 | static unsigned long nf_ct_icmp_timeout __read_mostly = 30*HZ; |
9fb9cbb1 | 24 | |
09f263cd JE |
25 | static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
26 | struct nf_conntrack_tuple *tuple) | |
9fb9cbb1 | 27 | { |
7cc3864d JE |
28 | const struct icmphdr *hp; |
29 | struct icmphdr _hdr; | |
9fb9cbb1 YK |
30 | |
31 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); | |
32 | if (hp == NULL) | |
09f263cd | 33 | return false; |
9fb9cbb1 YK |
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 | ||
09f263cd | 39 | return true; |
9fb9cbb1 YK |
40 | } |
41 | ||
c1d10adb PNA |
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 | ||
09f263cd JE |
54 | static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple, |
55 | const struct nf_conntrack_tuple *orig) | |
9fb9cbb1 | 56 | { |
9fb9cbb1 YK |
57 | if (orig->dst.u.icmp.type >= sizeof(invmap) |
58 | || !invmap[orig->dst.u.icmp.type]) | |
09f263cd | 59 | return false; |
9fb9cbb1 YK |
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; | |
09f263cd | 64 | return true; |
9fb9cbb1 YK |
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 | ||
9fb9cbb1 YK |
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: | |
e905a9ed YH |
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) */ | |
9fb9cbb1 | 89 | if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY) { |
51091764 | 90 | if (atomic_dec_and_test(&ct->proto.icmp.count)) |
718d4ad9 | 91 | nf_ct_kill_acct(ct, ctinfo, skb); |
9fb9cbb1 YK |
92 | } else { |
93 | atomic_inc(&ct->proto.icmp.count); | |
94 | nf_conntrack_event_cache(IPCT_PROTOINFO_VOLATILE, skb); | |
95 | nf_ct_refresh_acct(ct, ctinfo, skb, nf_ct_icmp_timeout); | |
96 | } | |
97 | ||
98 | return NF_ACCEPT; | |
99 | } | |
100 | ||
101 | /* Called when a new connection for this protocol found. */ | |
09f263cd JE |
102 | static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb, |
103 | unsigned int dataoff) | |
9fb9cbb1 | 104 | { |
c1d10adb PNA |
105 | static const u_int8_t valid_new[] = { |
106 | [ICMP_ECHO] = 1, | |
107 | [ICMP_TIMESTAMP] = 1, | |
108 | [ICMP_INFO_REQUEST] = 1, | |
109 | [ICMP_ADDRESS] = 1 | |
110 | }; | |
9fb9cbb1 | 111 | |
c88130bc PM |
112 | if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) |
113 | || !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) { | |
9fb9cbb1 | 114 | /* Can't create a new ICMP `conn' with this. */ |
0d53778e | 115 | pr_debug("icmp: can't create new conn with type %u\n", |
c88130bc | 116 | ct->tuplehash[0].tuple.dst.u.icmp.type); |
3c9fba65 | 117 | nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple); |
09f263cd | 118 | return false; |
9fb9cbb1 | 119 | } |
c88130bc | 120 | atomic_set(&ct->proto.icmp.count, 0); |
09f263cd | 121 | return true; |
9fb9cbb1 YK |
122 | } |
123 | ||
9fb9cbb1 YK |
124 | /* Returns conntrack if it dealt with ICMP, and filled in skb fields */ |
125 | static int | |
126 | icmp_error_message(struct sk_buff *skb, | |
e905a9ed YH |
127 | enum ip_conntrack_info *ctinfo, |
128 | unsigned int hooknum) | |
9fb9cbb1 YK |
129 | { |
130 | struct nf_conntrack_tuple innertuple, origtuple; | |
7cc3864d JE |
131 | const struct nf_conntrack_l4proto *innerproto; |
132 | const struct nf_conntrack_tuple_hash *h; | |
9fb9cbb1 YK |
133 | |
134 | NF_CT_ASSERT(skb->nfct == NULL); | |
135 | ||
e2a3123f YK |
136 | /* Are they talking about one of our connections? */ |
137 | if (!nf_ct_get_tuplepr(skb, | |
138 | skb_network_offset(skb) + ip_hdrlen(skb) | |
139 | + sizeof(struct icmphdr), | |
140 | PF_INET, &origtuple)) { | |
141 | pr_debug("icmp_error_message: failed to get tuple\n"); | |
9fb9cbb1 YK |
142 | return -NF_ACCEPT; |
143 | } | |
144 | ||
923f4902 | 145 | /* rcu_read_lock()ed by nf_hook_slow */ |
e2a3123f | 146 | innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum); |
9fb9cbb1 | 147 | |
e905a9ed YH |
148 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
149 | been preserved inside the ICMP. */ | |
150 | if (!nf_ct_invert_tuple(&innertuple, &origtuple, | |
9fb9cbb1 | 151 | &nf_conntrack_l3proto_ipv4, innerproto)) { |
0d53778e | 152 | pr_debug("icmp_error_message: no match\n"); |
9fb9cbb1 YK |
153 | return -NF_ACCEPT; |
154 | } | |
155 | ||
156 | *ctinfo = IP_CT_RELATED; | |
157 | ||
330f7db5 | 158 | h = nf_conntrack_find_get(&innertuple); |
9fb9cbb1 | 159 | if (!h) { |
130e7a83 YK |
160 | pr_debug("icmp_error_message: no match\n"); |
161 | return -NF_ACCEPT; | |
9fb9cbb1 YK |
162 | } |
163 | ||
130e7a83 YK |
164 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
165 | *ctinfo += IP_CT_IS_REPLY; | |
166 | ||
e905a9ed YH |
167 | /* Update skb to refer to this connection */ |
168 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; | |
169 | skb->nfctinfo = *ctinfo; | |
170 | return -NF_ACCEPT; | |
9fb9cbb1 YK |
171 | } |
172 | ||
173 | /* Small and modified version of icmp_rcv */ | |
174 | static int | |
175 | icmp_error(struct sk_buff *skb, unsigned int dataoff, | |
176 | enum ip_conntrack_info *ctinfo, int pf, unsigned int hooknum) | |
177 | { | |
7cc3864d JE |
178 | const struct icmphdr *icmph; |
179 | struct icmphdr _ih; | |
9fb9cbb1 YK |
180 | |
181 | /* Not enough header? */ | |
c9bdd4b5 | 182 | icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih); |
9fb9cbb1 YK |
183 | if (icmph == NULL) { |
184 | if (LOG_INVALID(IPPROTO_ICMP)) | |
185 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
186 | "nf_ct_icmp: short packet "); | |
187 | return -NF_ACCEPT; | |
188 | } | |
189 | ||
190 | /* See ip_conntrack_proto_tcp.c */ | |
6e23ae2a | 191 | if (nf_conntrack_checksum && hooknum == NF_INET_PRE_ROUTING && |
96f6bf82 | 192 | nf_ip_checksum(skb, hooknum, dataoff, 0)) { |
9fb9cbb1 YK |
193 | if (LOG_INVALID(IPPROTO_ICMP)) |
194 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
195 | "nf_ct_icmp: bad HW ICMP checksum "); | |
196 | return -NF_ACCEPT; | |
9fb9cbb1 YK |
197 | } |
198 | ||
9fb9cbb1 YK |
199 | /* |
200 | * 18 is the highest 'known' ICMP type. Anything else is a mystery | |
201 | * | |
202 | * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently | |
203 | * discarded. | |
204 | */ | |
205 | if (icmph->type > NR_ICMP_TYPES) { | |
206 | if (LOG_INVALID(IPPROTO_ICMP)) | |
207 | nf_log_packet(PF_INET, 0, skb, NULL, NULL, NULL, | |
208 | "nf_ct_icmp: invalid ICMP type "); | |
209 | return -NF_ACCEPT; | |
210 | } | |
211 | ||
212 | /* Need to track icmp error message? */ | |
213 | if (icmph->type != ICMP_DEST_UNREACH | |
214 | && icmph->type != ICMP_SOURCE_QUENCH | |
215 | && icmph->type != ICMP_TIME_EXCEEDED | |
216 | && icmph->type != ICMP_PARAMETERPROB | |
217 | && icmph->type != ICMP_REDIRECT) | |
218 | return NF_ACCEPT; | |
219 | ||
220 | return icmp_error_message(skb, ctinfo, hooknum); | |
221 | } | |
222 | ||
e281db5c | 223 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
c1d10adb PNA |
224 | |
225 | #include <linux/netfilter/nfnetlink.h> | |
226 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
227 | ||
fdf70832 | 228 | static int icmp_tuple_to_nlattr(struct sk_buff *skb, |
c1d10adb PNA |
229 | const struct nf_conntrack_tuple *t) |
230 | { | |
77236b6e PM |
231 | NLA_PUT_BE16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id); |
232 | NLA_PUT_U8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type); | |
233 | NLA_PUT_U8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code); | |
c1d10adb PNA |
234 | |
235 | return 0; | |
236 | ||
df6fb868 | 237 | nla_put_failure: |
c1d10adb PNA |
238 | return -1; |
239 | } | |
240 | ||
f73e924c PM |
241 | static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = { |
242 | [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 }, | |
243 | [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 }, | |
244 | [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 }, | |
c1d10adb PNA |
245 | }; |
246 | ||
fdf70832 | 247 | static int icmp_nlattr_to_tuple(struct nlattr *tb[], |
c1d10adb PNA |
248 | struct nf_conntrack_tuple *tuple) |
249 | { | |
df6fb868 PM |
250 | if (!tb[CTA_PROTO_ICMP_TYPE] |
251 | || !tb[CTA_PROTO_ICMP_CODE] | |
252 | || !tb[CTA_PROTO_ICMP_ID]) | |
c1d10adb PNA |
253 | return -EINVAL; |
254 | ||
77236b6e PM |
255 | tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]); |
256 | tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]); | |
257 | tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]); | |
c1d10adb PNA |
258 | |
259 | if (tuple->dst.u.icmp.type >= sizeof(invmap) | |
260 | || !invmap[tuple->dst.u.icmp.type]) | |
261 | return -EINVAL; | |
262 | ||
263 | return 0; | |
264 | } | |
265 | #endif | |
266 | ||
933a41e7 PM |
267 | #ifdef CONFIG_SYSCTL |
268 | static struct ctl_table_header *icmp_sysctl_header; | |
269 | static struct ctl_table icmp_sysctl_table[] = { | |
270 | { | |
933a41e7 PM |
271 | .procname = "nf_conntrack_icmp_timeout", |
272 | .data = &nf_ct_icmp_timeout, | |
273 | .maxlen = sizeof(unsigned int), | |
274 | .mode = 0644, | |
275 | .proc_handler = &proc_dointvec_jiffies, | |
276 | }, | |
e905a9ed | 277 | { |
933a41e7 PM |
278 | .ctl_name = 0 |
279 | } | |
280 | }; | |
a999e683 PM |
281 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
282 | static struct ctl_table icmp_compat_sysctl_table[] = { | |
283 | { | |
a999e683 PM |
284 | .procname = "ip_conntrack_icmp_timeout", |
285 | .data = &nf_ct_icmp_timeout, | |
286 | .maxlen = sizeof(unsigned int), | |
287 | .mode = 0644, | |
288 | .proc_handler = &proc_dointvec_jiffies, | |
289 | }, | |
e905a9ed | 290 | { |
a999e683 PM |
291 | .ctl_name = 0 |
292 | } | |
293 | }; | |
294 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ | |
933a41e7 PM |
295 | #endif /* CONFIG_SYSCTL */ |
296 | ||
61075af5 | 297 | struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly = |
9fb9cbb1 | 298 | { |
9fb9cbb1 | 299 | .l3proto = PF_INET, |
605dcad6 | 300 | .l4proto = IPPROTO_ICMP, |
9fb9cbb1 YK |
301 | .name = "icmp", |
302 | .pkt_to_tuple = icmp_pkt_to_tuple, | |
303 | .invert_tuple = icmp_invert_tuple, | |
304 | .print_tuple = icmp_print_tuple, | |
9fb9cbb1 YK |
305 | .packet = icmp_packet, |
306 | .new = icmp_new, | |
307 | .error = icmp_error, | |
308 | .destroy = NULL, | |
c1d10adb | 309 | .me = NULL, |
e281db5c | 310 | #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE) |
fdf70832 PM |
311 | .tuple_to_nlattr = icmp_tuple_to_nlattr, |
312 | .nlattr_to_tuple = icmp_nlattr_to_tuple, | |
f73e924c | 313 | .nla_policy = icmp_nla_policy, |
c1d10adb | 314 | #endif |
933a41e7 PM |
315 | #ifdef CONFIG_SYSCTL |
316 | .ctl_table_header = &icmp_sysctl_header, | |
317 | .ctl_table = icmp_sysctl_table, | |
a999e683 PM |
318 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
319 | .ctl_compat_table = icmp_compat_sysctl_table, | |
320 | #endif | |
933a41e7 | 321 | #endif |
9fb9cbb1 | 322 | }; |