]> asedeno.scripts.mit.edu Git - linux.git/blob - net/netfilter/nf_conntrack_proto_icmp.c
Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[linux.git] / net / netfilter / nf_conntrack_proto_icmp.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
3  * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/types.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_ipv4.h>
19 #include <net/netfilter/nf_conntrack_tuple.h>
20 #include <net/netfilter/nf_conntrack_l4proto.h>
21 #include <net/netfilter/nf_conntrack_core.h>
22 #include <net/netfilter/nf_conntrack_timeout.h>
23 #include <net/netfilter/nf_conntrack_zones.h>
24 #include <net/netfilter/nf_log.h>
25
26 static const unsigned int nf_ct_icmp_timeout = 30*HZ;
27
28 bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
29                        struct net *net, struct nf_conntrack_tuple *tuple)
30 {
31         const struct icmphdr *hp;
32         struct icmphdr _hdr;
33
34         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
35         if (hp == NULL)
36                 return false;
37
38         tuple->dst.u.icmp.type = hp->type;
39         tuple->src.u.icmp.id = hp->un.echo.id;
40         tuple->dst.u.icmp.code = hp->code;
41
42         return true;
43 }
44
45 /* Add 1; spaces filled with 0. */
46 static const u_int8_t invmap[] = {
47         [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
48         [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
49         [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
50         [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
51         [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
52         [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
53         [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
54         [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
55 };
56
57 bool nf_conntrack_invert_icmp_tuple(struct nf_conntrack_tuple *tuple,
58                                     const struct nf_conntrack_tuple *orig)
59 {
60         if (orig->dst.u.icmp.type >= sizeof(invmap) ||
61             !invmap[orig->dst.u.icmp.type])
62                 return false;
63
64         tuple->src.u.icmp.id = orig->src.u.icmp.id;
65         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
66         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
67         return true;
68 }
69
70 /* Returns verdict for packet, or -1 for invalid. */
71 int nf_conntrack_icmp_packet(struct nf_conn *ct,
72                              struct sk_buff *skb,
73                              enum ip_conntrack_info ctinfo,
74                              const struct nf_hook_state *state)
75 {
76         /* Do not immediately delete the connection after the first
77            successful reply to avoid excessive conntrackd traffic
78            and also to handle correctly ICMP echo reply duplicates. */
79         unsigned int *timeout = nf_ct_timeout_lookup(ct);
80         static const u_int8_t valid_new[] = {
81                 [ICMP_ECHO] = 1,
82                 [ICMP_TIMESTAMP] = 1,
83                 [ICMP_INFO_REQUEST] = 1,
84                 [ICMP_ADDRESS] = 1
85         };
86
87         if (state->pf != NFPROTO_IPV4)
88                 return -NF_ACCEPT;
89
90         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
91             !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
92                 /* Can't create a new ICMP `conn' with this. */
93                 pr_debug("icmp: can't create new conn with type %u\n",
94                          ct->tuplehash[0].tuple.dst.u.icmp.type);
95                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
96                 return -NF_ACCEPT;
97         }
98
99         if (!timeout)
100                 timeout = &nf_icmp_pernet(nf_ct_net(ct))->timeout;
101
102         nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
103         return NF_ACCEPT;
104 }
105
106 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
107 static int
108 icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
109                    const struct nf_hook_state *state)
110 {
111         struct nf_conntrack_tuple innertuple, origtuple;
112         const struct nf_conntrack_tuple_hash *h;
113         const struct nf_conntrack_zone *zone;
114         enum ip_conntrack_info ctinfo;
115         struct nf_conntrack_zone tmp;
116
117         WARN_ON(skb_nfct(skb));
118         zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
119
120         /* Are they talking about one of our connections? */
121         if (!nf_ct_get_tuplepr(skb,
122                                skb_network_offset(skb) + ip_hdrlen(skb)
123                                                        + sizeof(struct icmphdr),
124                                PF_INET, state->net, &origtuple)) {
125                 pr_debug("icmp_error_message: failed to get tuple\n");
126                 return -NF_ACCEPT;
127         }
128
129         /* Ordinarily, we'd expect the inverted tupleproto, but it's
130            been preserved inside the ICMP. */
131         if (!nf_ct_invert_tuple(&innertuple, &origtuple)) {
132                 pr_debug("icmp_error_message: no match\n");
133                 return -NF_ACCEPT;
134         }
135
136         ctinfo = IP_CT_RELATED;
137
138         h = nf_conntrack_find_get(state->net, zone, &innertuple);
139         if (!h) {
140                 pr_debug("icmp_error_message: no match\n");
141                 return -NF_ACCEPT;
142         }
143
144         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
145                 ctinfo += IP_CT_IS_REPLY;
146
147         /* Update skb to refer to this connection */
148         nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
149         return NF_ACCEPT;
150 }
151
152 static void icmp_error_log(const struct sk_buff *skb,
153                            const struct nf_hook_state *state,
154                            const char *msg)
155 {
156         nf_l4proto_log_invalid(skb, state->net, state->pf,
157                                IPPROTO_ICMP, "%s", msg);
158 }
159
160 /* Small and modified version of icmp_rcv */
161 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
162                               struct sk_buff *skb, unsigned int dataoff,
163                               const struct nf_hook_state *state)
164 {
165         const struct icmphdr *icmph;
166         struct icmphdr _ih;
167
168         /* Not enough header? */
169         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
170         if (icmph == NULL) {
171                 icmp_error_log(skb, state, "short packet");
172                 return -NF_ACCEPT;
173         }
174
175         /* See ip_conntrack_proto_tcp.c */
176         if (state->net->ct.sysctl_checksum &&
177             state->hook == NF_INET_PRE_ROUTING &&
178             nf_ip_checksum(skb, state->hook, dataoff, 0)) {
179                 icmp_error_log(skb, state, "bad hw icmp checksum");
180                 return -NF_ACCEPT;
181         }
182
183         /*
184          *      18 is the highest 'known' ICMP type. Anything else is a mystery
185          *
186          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
187          *                discarded.
188          */
189         if (icmph->type > NR_ICMP_TYPES) {
190                 icmp_error_log(skb, state, "invalid icmp type");
191                 return -NF_ACCEPT;
192         }
193
194         /* Need to track icmp error message? */
195         if (icmph->type != ICMP_DEST_UNREACH &&
196             icmph->type != ICMP_SOURCE_QUENCH &&
197             icmph->type != ICMP_TIME_EXCEEDED &&
198             icmph->type != ICMP_PARAMETERPROB &&
199             icmph->type != ICMP_REDIRECT)
200                 return NF_ACCEPT;
201
202         return icmp_error_message(tmpl, skb, state);
203 }
204
205 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
206
207 #include <linux/netfilter/nfnetlink.h>
208 #include <linux/netfilter/nfnetlink_conntrack.h>
209
210 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
211                                 const struct nf_conntrack_tuple *t)
212 {
213         if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
214             nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
215             nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
216                 goto nla_put_failure;
217         return 0;
218
219 nla_put_failure:
220         return -1;
221 }
222
223 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
224         [CTA_PROTO_ICMP_TYPE]   = { .type = NLA_U8 },
225         [CTA_PROTO_ICMP_CODE]   = { .type = NLA_U8 },
226         [CTA_PROTO_ICMP_ID]     = { .type = NLA_U16 },
227 };
228
229 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
230                                 struct nf_conntrack_tuple *tuple)
231 {
232         if (!tb[CTA_PROTO_ICMP_TYPE] ||
233             !tb[CTA_PROTO_ICMP_CODE] ||
234             !tb[CTA_PROTO_ICMP_ID])
235                 return -EINVAL;
236
237         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
238         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
239         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
240
241         if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
242             !invmap[tuple->dst.u.icmp.type])
243                 return -EINVAL;
244
245         return 0;
246 }
247
248 static unsigned int icmp_nlattr_tuple_size(void)
249 {
250         static unsigned int size __read_mostly;
251
252         if (!size)
253                 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
254
255         return size;
256 }
257 #endif
258
259 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
260
261 #include <linux/netfilter/nfnetlink.h>
262 #include <linux/netfilter/nfnetlink_cttimeout.h>
263
264 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
265                                       struct net *net, void *data)
266 {
267         unsigned int *timeout = data;
268         struct nf_icmp_net *in = nf_icmp_pernet(net);
269
270         if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
271                 if (!timeout)
272                         timeout = &in->timeout;
273                 *timeout =
274                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
275         } else if (timeout) {
276                 /* Set default ICMP timeout. */
277                 *timeout = in->timeout;
278         }
279         return 0;
280 }
281
282 static int
283 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
284 {
285         const unsigned int *timeout = data;
286
287         if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
288                 goto nla_put_failure;
289         return 0;
290
291 nla_put_failure:
292         return -ENOSPC;
293 }
294
295 static const struct nla_policy
296 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
297         [CTA_TIMEOUT_ICMP_TIMEOUT]      = { .type = NLA_U32 },
298 };
299 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
300
301 void nf_conntrack_icmp_init_net(struct net *net)
302 {
303         struct nf_icmp_net *in = nf_icmp_pernet(net);
304
305         in->timeout = nf_ct_icmp_timeout;
306 }
307
308 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
309 {
310         .l4proto                = IPPROTO_ICMP,
311 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
312         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
313         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
314         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
315         .nla_policy             = icmp_nla_policy,
316 #endif
317 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
318         .ctnl_timeout           = {
319                 .nlattr_to_obj  = icmp_timeout_nlattr_to_obj,
320                 .obj_to_nlattr  = icmp_timeout_obj_to_nlattr,
321                 .nlattr_max     = CTA_TIMEOUT_ICMP_MAX,
322                 .obj_size       = sizeof(unsigned int),
323                 .nla_policy     = icmp_timeout_nla_policy,
324         },
325 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
326 };