blob: c567e1b5d7990d977383453d58b4ae80f387d25c [file] [log] [blame]
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08001/* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
Patrick McHardyf229f6c2013-04-06 15:24:29 +02003 * (C) 2006-2010 Patrick McHardy <kaber@trash.net>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08004 *
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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08008 */
9
10#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080011#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>
Martin Josefsson605dcad2006-11-29 02:35:06 +010020#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080021#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010022#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080023#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080024
Patrick McHardy71320af2009-01-12 00:06:07 +000025static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080026
Gao feng4b626b92012-05-28 21:04:14 +000027static inline struct nf_icmp_net *icmp_pernet(struct net *net)
28{
29 return &net->ct.nf_ct_proto.icmp;
30}
31
Jan Engelhardt09f263c2008-04-14 11:15:53 +020032static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -050033 struct net *net, struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080034{
Jan Engelhardt7cc38642008-01-31 04:53:05 -080035 const struct icmphdr *hp;
36 struct icmphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080037
38 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
39 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +020040 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080041
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
Jan Engelhardt09f263c2008-04-14 11:15:53 +020046 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080047}
48
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080049/* Add 1; spaces filled with 0. */
50static const u_int8_t invmap[] = {
51 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
52 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
53 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
54 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
55 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
56 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
57 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
58 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
59};
60
Jan Engelhardt09f263c2008-04-14 11:15:53 +020061static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
62 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080063{
Joe Perches3666ed12009-11-23 23:17:06 +010064 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
65 !invmap[orig->dst.u.icmp.type])
Jan Engelhardt09f263c2008-04-14 11:15:53 +020066 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080067
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;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020071 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080072}
73
74/* Print out the per-protocol part of the tuple. */
Joe Perches824f1fb2014-09-29 16:08:22 -070075static void icmp_print_tuple(struct seq_file *s,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080076 const struct nf_conntrack_tuple *tuple)
77{
Joe Perches824f1fb2014-09-29 16:08:22 -070078 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));
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080082}
83
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010084static unsigned int *icmp_get_timeouts(struct net *net)
85{
Gao feng4b626b92012-05-28 21:04:14 +000086 return &icmp_pernet(net)->timeout;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010087}
88
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080089/* Returns verdict for packet, or -1 for invalid. */
90static int icmp_packet(struct nf_conn *ct,
91 const struct sk_buff *skb,
92 unsigned int dataoff,
93 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020094 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010095 unsigned int hooknum,
96 unsigned int *timeout)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080097{
Jan Kasprzakf87fb662009-06-08 15:53:43 +020098 /* Do not immediately delete the connection after the first
99 successful reply to avoid excessive conntrackd traffic
100 and also to handle correctly ICMP echo reply duplicates. */
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100101 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800102
103 return NF_ACCEPT;
104}
105
106/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200107static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100108 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800109{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800110 static const u_int8_t valid_new[] = {
111 [ICMP_ECHO] = 1,
112 [ICMP_TIMESTAMP] = 1,
113 [ICMP_INFO_REQUEST] = 1,
114 [ICMP_ADDRESS] = 1
115 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800116
Joe Perches3666ed12009-11-23 23:17:06 +0100117 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
118 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800119 /* Can't create a new ICMP `conn' with this. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700120 pr_debug("icmp: can't create new conn with type %u\n",
Patrick McHardyc88130b2008-01-31 04:42:11 -0800121 ct->tuplehash[0].tuple.dst.u.icmp.type);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200122 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200123 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800124 }
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200125 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800126}
127
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800128/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
129static int
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100130icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900131 enum ip_conntrack_info *ctinfo,
132 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800133{
134 struct nf_conntrack_tuple innertuple, origtuple;
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800135 const struct nf_conntrack_l4proto *innerproto;
136 const struct nf_conntrack_tuple_hash *h;
Daniel Borkmann308ac912015-08-08 21:40:01 +0200137 const struct nf_conntrack_zone *zone;
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200138 struct nf_conntrack_zone tmp;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800139
140 NF_CT_ASSERT(skb->nfct == NULL);
Daniel Borkmann5e8018f2015-08-14 16:03:40 +0200141 zone = nf_ct_zone_tmpl(tmpl, skb, &tmp);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800142
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700143 /* Are they talking about one of our connections? */
144 if (!nf_ct_get_tuplepr(skb,
145 skb_network_offset(skb) + ip_hdrlen(skb)
146 + sizeof(struct icmphdr),
Eric W. Biedermana31f1ad2015-09-18 14:33:04 -0500147 PF_INET, net, &origtuple)) {
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700148 pr_debug("icmp_error_message: failed to get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800149 return -NF_ACCEPT;
150 }
151
Patrick McHardy923f4902007-02-12 11:12:57 -0800152 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700153 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800154
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900155 /* Ordinarily, we'd expect the inverted tupleproto, but it's
156 been preserved inside the ICMP. */
157 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700159 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800160 return -NF_ACCEPT;
161 }
162
163 *ctinfo = IP_CT_RELATED;
164
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100165 h = nf_conntrack_find_get(net, zone, &innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166 if (!h) {
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700167 pr_debug("icmp_error_message: no match\n");
168 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800169 }
170
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700171 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
172 *ctinfo += IP_CT_IS_REPLY;
173
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900174 /* Update skb to refer to this connection */
175 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
176 skb->nfctinfo = *ctinfo;
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +0200177 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800178}
179
180/* Small and modified version of icmp_rcv */
181static int
Patrick McHardy8fea97e2010-02-15 17:45:08 +0100182icmp_error(struct net *net, struct nf_conn *tmpl,
183 struct sk_buff *skb, unsigned int dataoff,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200184 enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185{
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800186 const struct icmphdr *icmph;
187 struct icmphdr _ih;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188
189 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300190 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800191 if (icmph == NULL) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200192 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000193 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL,
194 NULL, "nf_ct_icmp: short packet ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800195 return -NF_ACCEPT;
196 }
197
198 /* See ip_conntrack_proto_tcp.c */
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200199 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700200 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200201 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000202 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 "nf_ct_icmp: bad HW ICMP checksum ");
204 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 }
206
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800207 /*
208 * 18 is the highest 'known' ICMP type. Anything else is a mystery
209 *
210 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
211 * discarded.
212 */
213 if (icmph->type > NR_ICMP_TYPES) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200214 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000215 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800216 "nf_ct_icmp: invalid ICMP type ");
217 return -NF_ACCEPT;
218 }
219
220 /* Need to track icmp error message? */
Joe Perches3666ed12009-11-23 23:17:06 +0100221 if (icmph->type != ICMP_DEST_UNREACH &&
222 icmph->type != ICMP_SOURCE_QUENCH &&
223 icmph->type != ICMP_TIME_EXCEEDED &&
224 icmph->type != ICMP_PARAMETERPROB &&
225 icmph->type != ICMP_REDIRECT)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226 return NF_ACCEPT;
227
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100228 return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800229}
230
Duan Jiong24de3d32014-06-30 09:19:32 +0800231#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800232
233#include <linux/netfilter/nfnetlink.h>
234#include <linux/netfilter/nfnetlink_conntrack.h>
235
Patrick McHardyfdf70832007-09-28 14:37:41 -0700236static int icmp_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800237 const struct nf_conntrack_tuple *t)
238{
David S. Millerd317e4f2012-04-01 20:40:20 -0400239 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
240 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
241 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
242 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800243 return 0;
244
Patrick McHardydf6fb862007-09-28 14:37:03 -0700245nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800246 return -1;
247}
248
Patrick McHardyf73e9242007-09-28 14:39:55 -0700249static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
250 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
251 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
252 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800253};
254
Patrick McHardyfdf70832007-09-28 14:37:41 -0700255static int icmp_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800256 struct nf_conntrack_tuple *tuple)
257{
Joe Perches3666ed12009-11-23 23:17:06 +0100258 if (!tb[CTA_PROTO_ICMP_TYPE] ||
259 !tb[CTA_PROTO_ICMP_CODE] ||
260 !tb[CTA_PROTO_ICMP_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800261 return -EINVAL;
262
Patrick McHardy77236b62007-12-17 22:29:45 -0800263 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
264 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
265 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800266
Joe Perches3666ed12009-11-23 23:17:06 +0100267 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
268 !invmap[tuple->dst.u.icmp.type])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800269 return -EINVAL;
270
271 return 0;
272}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100273
274static int icmp_nlattr_tuple_size(void)
275{
276 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
277}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800278#endif
279
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100280#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
281
282#include <linux/netfilter/nfnetlink.h>
283#include <linux/netfilter/nfnetlink_cttimeout.h>
284
Gao feng8264deb2012-05-28 21:04:23 +0000285static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
286 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100287{
288 unsigned int *timeout = data;
Gao feng8264deb2012-05-28 21:04:23 +0000289 struct nf_icmp_net *in = icmp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100290
291 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
292 *timeout =
293 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
294 } else {
295 /* Set default ICMP timeout. */
Gao feng8264deb2012-05-28 21:04:23 +0000296 *timeout = in->timeout;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100297 }
298 return 0;
299}
300
301static int
302icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
303{
304 const unsigned int *timeout = data;
305
David S. Millerd317e4f2012-04-01 20:40:20 -0400306 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
307 goto nla_put_failure;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100308 return 0;
309
310nla_put_failure:
311 return -ENOSPC;
312}
313
314static const struct nla_policy
315icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
316 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
317};
318#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
319
Patrick McHardy933a41e2006-11-29 02:35:18 +0100320#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100321static struct ctl_table icmp_sysctl_table[] = {
322 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100323 .procname = "nf_conntrack_icmp_timeout",
Patrick McHardy933a41e2006-11-29 02:35:18 +0100324 .maxlen = sizeof(unsigned int),
325 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800326 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100327 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800328 { }
Patrick McHardy933a41e2006-11-29 02:35:18 +0100329};
Patrick McHardya999e682006-11-29 02:35:20 +0100330#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
331static struct ctl_table icmp_compat_sysctl_table[] = {
332 {
Patrick McHardya999e682006-11-29 02:35:20 +0100333 .procname = "ip_conntrack_icmp_timeout",
Patrick McHardya999e682006-11-29 02:35:20 +0100334 .maxlen = sizeof(unsigned int),
335 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800336 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100337 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800338 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100339};
340#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100341#endif /* CONFIG_SYSCTL */
342
Gao fenga9082b42012-06-21 04:36:49 +0000343static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
344 struct nf_icmp_net *in)
Gao feng4b626b92012-05-28 21:04:14 +0000345{
Gao feng4b626b92012-05-28 21:04:14 +0000346#ifdef CONFIG_SYSCTL
347 pn->ctl_table = kmemdup(icmp_sysctl_table,
348 sizeof(icmp_sysctl_table),
349 GFP_KERNEL);
350 if (!pn->ctl_table)
351 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000352
Gao feng4b626b92012-05-28 21:04:14 +0000353 pn->ctl_table[0].data = &in->timeout;
Gao fenga9082b42012-06-21 04:36:49 +0000354#endif
355 return 0;
356}
357
358static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
359 struct nf_icmp_net *in)
360{
361#ifdef CONFIG_SYSCTL
Gao feng4b626b92012-05-28 21:04:14 +0000362#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
363 pn->ctl_compat_table = kmemdup(icmp_compat_sysctl_table,
364 sizeof(icmp_compat_sysctl_table),
365 GFP_KERNEL);
Gao fenga9082b42012-06-21 04:36:49 +0000366 if (!pn->ctl_compat_table)
Gao feng4b626b92012-05-28 21:04:14 +0000367 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000368
Gao feng4b626b92012-05-28 21:04:14 +0000369 pn->ctl_compat_table[0].data = &in->timeout;
370#endif
371#endif
372 return 0;
373}
374
Gao fenga9082b42012-06-21 04:36:49 +0000375static int icmp_init_net(struct net *net, u_int16_t proto)
376{
377 int ret;
378 struct nf_icmp_net *in = icmp_pernet(net);
379 struct nf_proto_net *pn = &in->pn;
380
381 in->timeout = nf_ct_icmp_timeout;
382
383 ret = icmp_kmemdup_compat_sysctl_table(pn, in);
384 if (ret < 0)
385 return ret;
386
387 ret = icmp_kmemdup_sysctl_table(pn, in);
388 if (ret < 0)
389 nf_ct_kfree_compat_sysctl_table(pn);
390
391 return ret;
392}
393
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000394static struct nf_proto_net *icmp_get_net_proto(struct net *net)
395{
396 return &net->ct.nf_ct_proto.icmp.pn;
397}
398
Patrick McHardy61075af2007-07-14 20:48:19 -0700399struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100402 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800403 .name = "icmp",
404 .pkt_to_tuple = icmp_pkt_to_tuple,
405 .invert_tuple = icmp_invert_tuple,
406 .print_tuple = icmp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407 .packet = icmp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100408 .get_timeouts = icmp_get_timeouts,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800409 .new = icmp_new,
410 .error = icmp_error,
411 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800412 .me = NULL,
Duan Jiong24de3d32014-06-30 09:19:32 +0800413#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700414 .tuple_to_nlattr = icmp_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100415 .nlattr_tuple_size = icmp_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700416 .nlattr_to_tuple = icmp_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700417 .nla_policy = icmp_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800418#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100419#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
420 .ctnl_timeout = {
421 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
422 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
423 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
424 .obj_size = sizeof(unsigned int),
425 .nla_policy = icmp_timeout_nla_policy,
426 },
427#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng4b626b92012-05-28 21:04:14 +0000428 .init_net = icmp_init_net,
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000429 .get_net_proto = icmp_get_net_proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800430};