net: alx: make alx_drv_name static
[sfrench/cifs-2.6.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 static inline struct nf_icmp_net *icmp_pernet(struct net *net)
29 {
30         return &net->ct.nf_ct_proto.icmp;
31 }
32
33 static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
34                               struct net *net, struct nf_conntrack_tuple *tuple)
35 {
36         const struct icmphdr *hp;
37         struct icmphdr _hdr;
38
39         hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
40         if (hp == NULL)
41                 return false;
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 true;
48 }
49
50 /* Add 1; spaces filled with 0. */
51 static const u_int8_t invmap[] = {
52         [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
53         [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
54         [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
55         [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
56         [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
57         [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
58         [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
59         [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
60 };
61
62 static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
63                               const struct nf_conntrack_tuple *orig)
64 {
65         if (orig->dst.u.icmp.type >= sizeof(invmap) ||
66             !invmap[orig->dst.u.icmp.type])
67                 return false;
68
69         tuple->src.u.icmp.id = orig->src.u.icmp.id;
70         tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
71         tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
72         return true;
73 }
74
75 /* Returns verdict for packet, or -1 for invalid. */
76 static int icmp_packet(struct nf_conn *ct,
77                        struct sk_buff *skb,
78                        unsigned int dataoff,
79                        enum ip_conntrack_info ctinfo,
80                        const struct nf_hook_state *state)
81 {
82         /* Do not immediately delete the connection after the first
83            successful reply to avoid excessive conntrackd traffic
84            and also to handle correctly ICMP echo reply duplicates. */
85         unsigned int *timeout = nf_ct_timeout_lookup(ct);
86         static const u_int8_t valid_new[] = {
87                 [ICMP_ECHO] = 1,
88                 [ICMP_TIMESTAMP] = 1,
89                 [ICMP_INFO_REQUEST] = 1,
90                 [ICMP_ADDRESS] = 1
91         };
92
93         if (state->pf != NFPROTO_IPV4)
94                 return -NF_ACCEPT;
95
96         if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
97             !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
98                 /* Can't create a new ICMP `conn' with this. */
99                 pr_debug("icmp: can't create new conn with type %u\n",
100                          ct->tuplehash[0].tuple.dst.u.icmp.type);
101                 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
102                 return -NF_ACCEPT;
103         }
104
105         if (!timeout)
106                 timeout = &icmp_pernet(nf_ct_net(ct))->timeout;
107
108         nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
109         return NF_ACCEPT;
110 }
111
112 /* Returns conntrack if it dealt with ICMP, and filled in skb fields */
113 static int
114 icmp_error_message(struct nf_conn *tmpl, struct sk_buff *skb,
115                    const struct nf_hook_state *state)
116 {
117         struct nf_conntrack_tuple innertuple, origtuple;
118         const struct nf_conntrack_l4proto *innerproto;
119         const struct nf_conntrack_tuple_hash *h;
120         const struct nf_conntrack_zone *zone;
121         enum ip_conntrack_info ctinfo;
122         struct nf_conntrack_zone tmp;
123
124         WARN_ON(skb_nfct(skb));
125         zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
126
127         /* Are they talking about one of our connections? */
128         if (!nf_ct_get_tuplepr(skb,
129                                skb_network_offset(skb) + ip_hdrlen(skb)
130                                                        + sizeof(struct icmphdr),
131                                PF_INET, state->net, &origtuple)) {
132                 pr_debug("icmp_error_message: failed to get tuple\n");
133                 return -NF_ACCEPT;
134         }
135
136         /* rcu_read_lock()ed by nf_hook_thresh */
137         innerproto = __nf_ct_l4proto_find(origtuple.dst.protonum);
138
139         /* Ordinarily, we'd expect the inverted tupleproto, but it's
140            been preserved inside the ICMP. */
141         if (!nf_ct_invert_tuple(&innertuple, &origtuple, innerproto)) {
142                 pr_debug("icmp_error_message: no match\n");
143                 return -NF_ACCEPT;
144         }
145
146         ctinfo = IP_CT_RELATED;
147
148         h = nf_conntrack_find_get(state->net, zone, &innertuple);
149         if (!h) {
150                 pr_debug("icmp_error_message: no match\n");
151                 return -NF_ACCEPT;
152         }
153
154         if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
155                 ctinfo += IP_CT_IS_REPLY;
156
157         /* Update skb to refer to this connection */
158         nf_ct_set(skb, nf_ct_tuplehash_to_ctrack(h), ctinfo);
159         return NF_ACCEPT;
160 }
161
162 static void icmp_error_log(const struct sk_buff *skb,
163                            const struct nf_hook_state *state,
164                            const char *msg)
165 {
166         nf_l4proto_log_invalid(skb, state->net, state->pf,
167                                IPPROTO_ICMP, "%s", msg);
168 }
169
170 /* Small and modified version of icmp_rcv */
171 int nf_conntrack_icmpv4_error(struct nf_conn *tmpl,
172                               struct sk_buff *skb, unsigned int dataoff,
173                               const struct nf_hook_state *state)
174 {
175         const struct icmphdr *icmph;
176         struct icmphdr _ih;
177
178         /* Not enough header? */
179         icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
180         if (icmph == NULL) {
181                 icmp_error_log(skb, state, "short packet");
182                 return -NF_ACCEPT;
183         }
184
185         /* See ip_conntrack_proto_tcp.c */
186         if (state->net->ct.sysctl_checksum &&
187             state->hook == NF_INET_PRE_ROUTING &&
188             nf_ip_checksum(skb, state->hook, dataoff, 0)) {
189                 icmp_error_log(skb, state, "bad hw icmp checksum");
190                 return -NF_ACCEPT;
191         }
192
193         /*
194          *      18 is the highest 'known' ICMP type. Anything else is a mystery
195          *
196          *      RFC 1122: 3.2.2  Unknown ICMP messages types MUST be silently
197          *                discarded.
198          */
199         if (icmph->type > NR_ICMP_TYPES) {
200                 icmp_error_log(skb, state, "invalid icmp type");
201                 return -NF_ACCEPT;
202         }
203
204         /* Need to track icmp error message? */
205         if (icmph->type != ICMP_DEST_UNREACH &&
206             icmph->type != ICMP_SOURCE_QUENCH &&
207             icmph->type != ICMP_TIME_EXCEEDED &&
208             icmph->type != ICMP_PARAMETERPROB &&
209             icmph->type != ICMP_REDIRECT)
210                 return NF_ACCEPT;
211
212         return icmp_error_message(tmpl, skb, state);
213 }
214
215 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
216
217 #include <linux/netfilter/nfnetlink.h>
218 #include <linux/netfilter/nfnetlink_conntrack.h>
219
220 static int icmp_tuple_to_nlattr(struct sk_buff *skb,
221                                 const struct nf_conntrack_tuple *t)
222 {
223         if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
224             nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
225             nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
226                 goto nla_put_failure;
227         return 0;
228
229 nla_put_failure:
230         return -1;
231 }
232
233 static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
234         [CTA_PROTO_ICMP_TYPE]   = { .type = NLA_U8 },
235         [CTA_PROTO_ICMP_CODE]   = { .type = NLA_U8 },
236         [CTA_PROTO_ICMP_ID]     = { .type = NLA_U16 },
237 };
238
239 static int icmp_nlattr_to_tuple(struct nlattr *tb[],
240                                 struct nf_conntrack_tuple *tuple)
241 {
242         if (!tb[CTA_PROTO_ICMP_TYPE] ||
243             !tb[CTA_PROTO_ICMP_CODE] ||
244             !tb[CTA_PROTO_ICMP_ID])
245                 return -EINVAL;
246
247         tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
248         tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
249         tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
250
251         if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
252             !invmap[tuple->dst.u.icmp.type])
253                 return -EINVAL;
254
255         return 0;
256 }
257
258 static unsigned int icmp_nlattr_tuple_size(void)
259 {
260         static unsigned int size __read_mostly;
261
262         if (!size)
263                 size = nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
264
265         return size;
266 }
267 #endif
268
269 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
270
271 #include <linux/netfilter/nfnetlink.h>
272 #include <linux/netfilter/nfnetlink_cttimeout.h>
273
274 static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
275                                       struct net *net, void *data)
276 {
277         unsigned int *timeout = data;
278         struct nf_icmp_net *in = icmp_pernet(net);
279
280         if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
281                 if (!timeout)
282                         timeout = &in->timeout;
283                 *timeout =
284                         ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
285         } else if (timeout) {
286                 /* Set default ICMP timeout. */
287                 *timeout = in->timeout;
288         }
289         return 0;
290 }
291
292 static int
293 icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
294 {
295         const unsigned int *timeout = data;
296
297         if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
298                 goto nla_put_failure;
299         return 0;
300
301 nla_put_failure:
302         return -ENOSPC;
303 }
304
305 static const struct nla_policy
306 icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
307         [CTA_TIMEOUT_ICMP_TIMEOUT]      = { .type = NLA_U32 },
308 };
309 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
310
311 #ifdef CONFIG_SYSCTL
312 static struct ctl_table icmp_sysctl_table[] = {
313         {
314                 .procname       = "nf_conntrack_icmp_timeout",
315                 .maxlen         = sizeof(unsigned int),
316                 .mode           = 0644,
317                 .proc_handler   = proc_dointvec_jiffies,
318         },
319         { }
320 };
321 #endif /* CONFIG_SYSCTL */
322
323 static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
324                                      struct nf_icmp_net *in)
325 {
326 #ifdef CONFIG_SYSCTL
327         pn->ctl_table = kmemdup(icmp_sysctl_table,
328                                 sizeof(icmp_sysctl_table),
329                                 GFP_KERNEL);
330         if (!pn->ctl_table)
331                 return -ENOMEM;
332
333         pn->ctl_table[0].data = &in->timeout;
334 #endif
335         return 0;
336 }
337
338 static int icmp_init_net(struct net *net)
339 {
340         struct nf_icmp_net *in = icmp_pernet(net);
341         struct nf_proto_net *pn = &in->pn;
342
343         in->timeout = nf_ct_icmp_timeout;
344
345         return icmp_kmemdup_sysctl_table(pn, in);
346 }
347
348 static struct nf_proto_net *icmp_get_net_proto(struct net *net)
349 {
350         return &net->ct.nf_ct_proto.icmp.pn;
351 }
352
353 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
354 {
355         .l4proto                = IPPROTO_ICMP,
356         .pkt_to_tuple           = icmp_pkt_to_tuple,
357         .invert_tuple           = icmp_invert_tuple,
358         .packet                 = icmp_packet,
359         .destroy                = NULL,
360         .me                     = NULL,
361 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
362         .tuple_to_nlattr        = icmp_tuple_to_nlattr,
363         .nlattr_tuple_size      = icmp_nlattr_tuple_size,
364         .nlattr_to_tuple        = icmp_nlattr_to_tuple,
365         .nla_policy             = icmp_nla_policy,
366 #endif
367 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
368         .ctnl_timeout           = {
369                 .nlattr_to_obj  = icmp_timeout_nlattr_to_obj,
370                 .obj_to_nlattr  = icmp_timeout_obj_to_nlattr,
371                 .nlattr_max     = CTA_TIMEOUT_ICMP_MAX,
372                 .obj_size       = sizeof(unsigned int),
373                 .nla_policy     = icmp_timeout_nla_policy,
374         },
375 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
376         .init_net               = icmp_init_net,
377         .get_net_proto          = icmp_get_net_proto,
378 };