blob: b91b2641adda6b2f35768f307ef6c13c10a8d351 [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,
33 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. */
75static int icmp_print_tuple(struct seq_file *s,
76 const struct nf_conntrack_tuple *tuple)
77{
78 return 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));
82}
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;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100137 u16 zone = tmpl ? nf_ct_zone(tmpl) : NF_CT_DEFAULT_ZONE;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800138
139 NF_CT_ASSERT(skb->nfct == NULL);
140
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700141 /* Are they talking about one of our connections? */
142 if (!nf_ct_get_tuplepr(skb,
143 skb_network_offset(skb) + ip_hdrlen(skb)
144 + sizeof(struct icmphdr),
145 PF_INET, &origtuple)) {
146 pr_debug("icmp_error_message: failed to get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800147 return -NF_ACCEPT;
148 }
149
Patrick McHardy923f4902007-02-12 11:12:57 -0800150 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700151 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800152
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900153 /* Ordinarily, we'd expect the inverted tupleproto, but it's
154 been preserved inside the ICMP. */
155 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800156 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700157 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800158 return -NF_ACCEPT;
159 }
160
161 *ctinfo = IP_CT_RELATED;
162
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100163 h = nf_conntrack_find_get(net, zone, &innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800164 if (!h) {
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700165 pr_debug("icmp_error_message: no match\n");
166 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800167 }
168
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700169 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
170 *ctinfo += IP_CT_IS_REPLY;
171
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900172 /* Update skb to refer to this connection */
173 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
174 skb->nfctinfo = *ctinfo;
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +0200175 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800176}
177
178/* Small and modified version of icmp_rcv */
179static int
Patrick McHardy8fea97e2010-02-15 17:45:08 +0100180icmp_error(struct net *net, struct nf_conn *tmpl,
181 struct sk_buff *skb, unsigned int dataoff,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200182 enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800183{
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800184 const struct icmphdr *icmph;
185 struct icmphdr _ih;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800186
187 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300188 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800189 if (icmph == NULL) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200190 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000191 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL,
192 NULL, "nf_ct_icmp: short packet ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800193 return -NF_ACCEPT;
194 }
195
196 /* See ip_conntrack_proto_tcp.c */
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200197 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700198 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200199 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000200 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800201 "nf_ct_icmp: bad HW ICMP checksum ");
202 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800203 }
204
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800205 /*
206 * 18 is the highest 'known' ICMP type. Anything else is a mystery
207 *
208 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
209 * discarded.
210 */
211 if (icmph->type > NR_ICMP_TYPES) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200212 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000213 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800214 "nf_ct_icmp: invalid ICMP type ");
215 return -NF_ACCEPT;
216 }
217
218 /* Need to track icmp error message? */
Joe Perches3666ed12009-11-23 23:17:06 +0100219 if (icmph->type != ICMP_DEST_UNREACH &&
220 icmph->type != ICMP_SOURCE_QUENCH &&
221 icmph->type != ICMP_TIME_EXCEEDED &&
222 icmph->type != ICMP_PARAMETERPROB &&
223 icmph->type != ICMP_REDIRECT)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800224 return NF_ACCEPT;
225
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100226 return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800227}
228
Duan Jiong24de3d32014-06-30 09:19:32 +0800229#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800230
231#include <linux/netfilter/nfnetlink.h>
232#include <linux/netfilter/nfnetlink_conntrack.h>
233
Patrick McHardyfdf70832007-09-28 14:37:41 -0700234static int icmp_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800235 const struct nf_conntrack_tuple *t)
236{
David S. Millerd317e4f2012-04-01 20:40:20 -0400237 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
238 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
239 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
240 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800241 return 0;
242
Patrick McHardydf6fb862007-09-28 14:37:03 -0700243nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800244 return -1;
245}
246
Patrick McHardyf73e9242007-09-28 14:39:55 -0700247static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
248 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
249 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
250 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800251};
252
Patrick McHardyfdf70832007-09-28 14:37:41 -0700253static int icmp_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800254 struct nf_conntrack_tuple *tuple)
255{
Joe Perches3666ed12009-11-23 23:17:06 +0100256 if (!tb[CTA_PROTO_ICMP_TYPE] ||
257 !tb[CTA_PROTO_ICMP_CODE] ||
258 !tb[CTA_PROTO_ICMP_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800259 return -EINVAL;
260
Patrick McHardy77236b62007-12-17 22:29:45 -0800261 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
262 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
263 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800264
Joe Perches3666ed12009-11-23 23:17:06 +0100265 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
266 !invmap[tuple->dst.u.icmp.type])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800267 return -EINVAL;
268
269 return 0;
270}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100271
272static int icmp_nlattr_tuple_size(void)
273{
274 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
275}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800276#endif
277
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100278#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
279
280#include <linux/netfilter/nfnetlink.h>
281#include <linux/netfilter/nfnetlink_cttimeout.h>
282
Gao feng8264deb2012-05-28 21:04:23 +0000283static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
284 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100285{
286 unsigned int *timeout = data;
Gao feng8264deb2012-05-28 21:04:23 +0000287 struct nf_icmp_net *in = icmp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100288
289 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
290 *timeout =
291 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
292 } else {
293 /* Set default ICMP timeout. */
Gao feng8264deb2012-05-28 21:04:23 +0000294 *timeout = in->timeout;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100295 }
296 return 0;
297}
298
299static int
300icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
301{
302 const unsigned int *timeout = data;
303
David S. Millerd317e4f2012-04-01 20:40:20 -0400304 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
305 goto nla_put_failure;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100306 return 0;
307
308nla_put_failure:
309 return -ENOSPC;
310}
311
312static const struct nla_policy
313icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
314 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
315};
316#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
317
Patrick McHardy933a41e2006-11-29 02:35:18 +0100318#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100319static struct ctl_table icmp_sysctl_table[] = {
320 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100321 .procname = "nf_conntrack_icmp_timeout",
Patrick McHardy933a41e2006-11-29 02:35:18 +0100322 .maxlen = sizeof(unsigned int),
323 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800324 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100325 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800326 { }
Patrick McHardy933a41e2006-11-29 02:35:18 +0100327};
Patrick McHardya999e682006-11-29 02:35:20 +0100328#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
329static struct ctl_table icmp_compat_sysctl_table[] = {
330 {
Patrick McHardya999e682006-11-29 02:35:20 +0100331 .procname = "ip_conntrack_icmp_timeout",
Patrick McHardya999e682006-11-29 02:35:20 +0100332 .maxlen = sizeof(unsigned int),
333 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800334 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100335 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800336 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100337};
338#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100339#endif /* CONFIG_SYSCTL */
340
Gao fenga9082b42012-06-21 04:36:49 +0000341static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
342 struct nf_icmp_net *in)
Gao feng4b626b92012-05-28 21:04:14 +0000343{
Gao feng4b626b92012-05-28 21:04:14 +0000344#ifdef CONFIG_SYSCTL
345 pn->ctl_table = kmemdup(icmp_sysctl_table,
346 sizeof(icmp_sysctl_table),
347 GFP_KERNEL);
348 if (!pn->ctl_table)
349 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000350
Gao feng4b626b92012-05-28 21:04:14 +0000351 pn->ctl_table[0].data = &in->timeout;
Gao fenga9082b42012-06-21 04:36:49 +0000352#endif
353 return 0;
354}
355
356static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
357 struct nf_icmp_net *in)
358{
359#ifdef CONFIG_SYSCTL
Gao feng4b626b92012-05-28 21:04:14 +0000360#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
361 pn->ctl_compat_table = kmemdup(icmp_compat_sysctl_table,
362 sizeof(icmp_compat_sysctl_table),
363 GFP_KERNEL);
Gao fenga9082b42012-06-21 04:36:49 +0000364 if (!pn->ctl_compat_table)
Gao feng4b626b92012-05-28 21:04:14 +0000365 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000366
Gao feng4b626b92012-05-28 21:04:14 +0000367 pn->ctl_compat_table[0].data = &in->timeout;
368#endif
369#endif
370 return 0;
371}
372
Gao fenga9082b42012-06-21 04:36:49 +0000373static int icmp_init_net(struct net *net, u_int16_t proto)
374{
375 int ret;
376 struct nf_icmp_net *in = icmp_pernet(net);
377 struct nf_proto_net *pn = &in->pn;
378
379 in->timeout = nf_ct_icmp_timeout;
380
381 ret = icmp_kmemdup_compat_sysctl_table(pn, in);
382 if (ret < 0)
383 return ret;
384
385 ret = icmp_kmemdup_sysctl_table(pn, in);
386 if (ret < 0)
387 nf_ct_kfree_compat_sysctl_table(pn);
388
389 return ret;
390}
391
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000392static struct nf_proto_net *icmp_get_net_proto(struct net *net)
393{
394 return &net->ct.nf_ct_proto.icmp.pn;
395}
396
Patrick McHardy61075af2007-07-14 20:48:19 -0700397struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800399 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100400 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800401 .name = "icmp",
402 .pkt_to_tuple = icmp_pkt_to_tuple,
403 .invert_tuple = icmp_invert_tuple,
404 .print_tuple = icmp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800405 .packet = icmp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100406 .get_timeouts = icmp_get_timeouts,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800407 .new = icmp_new,
408 .error = icmp_error,
409 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800410 .me = NULL,
Duan Jiong24de3d32014-06-30 09:19:32 +0800411#if IS_ENABLED(CONFIG_NF_CT_NETLINK)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700412 .tuple_to_nlattr = icmp_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100413 .nlattr_tuple_size = icmp_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700414 .nlattr_to_tuple = icmp_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700415 .nla_policy = icmp_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800416#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100417#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
418 .ctnl_timeout = {
419 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
420 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
421 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
422 .obj_size = sizeof(unsigned int),
423 .nla_policy = icmp_timeout_nla_policy,
424 },
425#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng4b626b92012-05-28 21:04:14 +0000426 .init_net = icmp_init_net,
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000427 .get_net_proto = icmp_get_net_proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800428};