blob: c2cd63d2d892b6aeeaad2401c7f55ae0a04f4fb7 [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>
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.
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -08007 */
8
9#include <linux/types.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080010#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>
Martin Josefsson605dcad2006-11-29 02:35:06 +010019#include <net/netfilter/nf_conntrack_l4proto.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080020#include <net/netfilter/nf_conntrack_core.h>
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +010021#include <net/netfilter/nf_conntrack_zones.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080022#include <net/netfilter/nf_log.h>
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080023
Patrick McHardy71320af2009-01-12 00:06:07 +000024static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080025
Gao feng4b626b92012-05-28 21:04:14 +000026static inline struct nf_icmp_net *icmp_pernet(struct net *net)
27{
28 return &net->ct.nf_ct_proto.icmp;
29}
30
Jan Engelhardt09f263c2008-04-14 11:15:53 +020031static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff,
32 struct nf_conntrack_tuple *tuple)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080033{
Jan Engelhardt7cc38642008-01-31 04:53:05 -080034 const struct icmphdr *hp;
35 struct icmphdr _hdr;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080036
37 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
38 if (hp == NULL)
Jan Engelhardt09f263c2008-04-14 11:15:53 +020039 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080040
41 tuple->dst.u.icmp.type = hp->type;
42 tuple->src.u.icmp.id = hp->un.echo.id;
43 tuple->dst.u.icmp.code = hp->code;
44
Jan Engelhardt09f263c2008-04-14 11:15:53 +020045 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080046}
47
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080048/* Add 1; spaces filled with 0. */
49static const u_int8_t invmap[] = {
50 [ICMP_ECHO] = ICMP_ECHOREPLY + 1,
51 [ICMP_ECHOREPLY] = ICMP_ECHO + 1,
52 [ICMP_TIMESTAMP] = ICMP_TIMESTAMPREPLY + 1,
53 [ICMP_TIMESTAMPREPLY] = ICMP_TIMESTAMP + 1,
54 [ICMP_INFO_REQUEST] = ICMP_INFO_REPLY + 1,
55 [ICMP_INFO_REPLY] = ICMP_INFO_REQUEST + 1,
56 [ICMP_ADDRESS] = ICMP_ADDRESSREPLY + 1,
57 [ICMP_ADDRESSREPLY] = ICMP_ADDRESS + 1
58};
59
Jan Engelhardt09f263c2008-04-14 11:15:53 +020060static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
61 const struct nf_conntrack_tuple *orig)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080062{
Joe Perches3666ed12009-11-23 23:17:06 +010063 if (orig->dst.u.icmp.type >= sizeof(invmap) ||
64 !invmap[orig->dst.u.icmp.type])
Jan Engelhardt09f263c2008-04-14 11:15:53 +020065 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080066
67 tuple->src.u.icmp.id = orig->src.u.icmp.id;
68 tuple->dst.u.icmp.type = invmap[orig->dst.u.icmp.type] - 1;
69 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
Jan Engelhardt09f263c2008-04-14 11:15:53 +020070 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080071}
72
73/* Print out the per-protocol part of the tuple. */
74static int icmp_print_tuple(struct seq_file *s,
75 const struct nf_conntrack_tuple *tuple)
76{
77 return seq_printf(s, "type=%u code=%u id=%u ",
78 tuple->dst.u.icmp.type,
79 tuple->dst.u.icmp.code,
80 ntohs(tuple->src.u.icmp.id));
81}
82
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010083static unsigned int *icmp_get_timeouts(struct net *net)
84{
Gao feng4b626b92012-05-28 21:04:14 +000085 return &icmp_pernet(net)->timeout;
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010086}
87
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080088/* Returns verdict for packet, or -1 for invalid. */
89static int icmp_packet(struct nf_conn *ct,
90 const struct sk_buff *skb,
91 unsigned int dataoff,
92 enum ip_conntrack_info ctinfo,
Jan Engelhardt76108ce2008-10-08 11:35:00 +020093 u_int8_t pf,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +010094 unsigned int hooknum,
95 unsigned int *timeout)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -080096{
Jan Kasprzakf87fb662009-06-08 15:53:43 +020097 /* Do not immediately delete the connection after the first
98 successful reply to avoid excessive conntrackd traffic
99 and also to handle correctly ICMP echo reply duplicates. */
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100100 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800101
102 return NF_ACCEPT;
103}
104
105/* Called when a new connection for this protocol found. */
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200106static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100107 unsigned int dataoff, unsigned int *timeouts)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800108{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800109 static const u_int8_t valid_new[] = {
110 [ICMP_ECHO] = 1,
111 [ICMP_TIMESTAMP] = 1,
112 [ICMP_INFO_REQUEST] = 1,
113 [ICMP_ADDRESS] = 1
114 };
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800115
Joe Perches3666ed12009-11-23 23:17:06 +0100116 if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) ||
117 !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) {
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800118 /* Can't create a new ICMP `conn' with this. */
Patrick McHardy0d537782007-07-07 22:39:38 -0700119 pr_debug("icmp: can't create new conn with type %u\n",
Patrick McHardyc88130b2008-01-31 04:42:11 -0800120 ct->tuplehash[0].tuple.dst.u.icmp.type);
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200121 nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple);
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200122 return false;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800123 }
Jan Engelhardt09f263c2008-04-14 11:15:53 +0200124 return true;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800125}
126
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800127/* Returns conntrack if it dealt with ICMP, and filled in skb fields */
128static int
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100129icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900130 enum ip_conntrack_info *ctinfo,
131 unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800132{
133 struct nf_conntrack_tuple innertuple, origtuple;
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800134 const struct nf_conntrack_l4proto *innerproto;
135 const struct nf_conntrack_tuple_hash *h;
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100136 u16 zone = tmpl ? nf_ct_zone(tmpl) : NF_CT_DEFAULT_ZONE;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800137
138 NF_CT_ASSERT(skb->nfct == NULL);
139
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700140 /* Are they talking about one of our connections? */
141 if (!nf_ct_get_tuplepr(skb,
142 skb_network_offset(skb) + ip_hdrlen(skb)
143 + sizeof(struct icmphdr),
144 PF_INET, &origtuple)) {
145 pr_debug("icmp_error_message: failed to get tuple\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800146 return -NF_ACCEPT;
147 }
148
Patrick McHardy923f4902007-02-12 11:12:57 -0800149 /* rcu_read_lock()ed by nf_hook_slow */
Yasuyuki Kozakaie2a31232007-07-14 20:45:14 -0700150 innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800151
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900152 /* Ordinarily, we'd expect the inverted tupleproto, but it's
153 been preserved inside the ICMP. */
154 if (!nf_ct_invert_tuple(&innertuple, &origtuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800155 &nf_conntrack_l3proto_ipv4, innerproto)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700156 pr_debug("icmp_error_message: no match\n");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800157 return -NF_ACCEPT;
158 }
159
160 *ctinfo = IP_CT_RELATED;
161
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100162 h = nf_conntrack_find_get(net, zone, &innertuple);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800163 if (!h) {
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700164 pr_debug("icmp_error_message: no match\n");
165 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800166 }
167
Yasuyuki Kozakai130e7a82007-07-14 20:45:41 -0700168 if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
169 *ctinfo += IP_CT_IS_REPLY;
170
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900171 /* Update skb to refer to this connection */
172 skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general;
173 skb->nfctinfo = *ctinfo;
Pablo Neira Ayuso88ed01d2011-06-02 15:08:45 +0200174 return NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800175}
176
177/* Small and modified version of icmp_rcv */
178static int
Patrick McHardy8fea97e2010-02-15 17:45:08 +0100179icmp_error(struct net *net, struct nf_conn *tmpl,
180 struct sk_buff *skb, unsigned int dataoff,
Jan Engelhardt76108ce2008-10-08 11:35:00 +0200181 enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800182{
Jan Engelhardt7cc38642008-01-31 04:53:05 -0800183 const struct icmphdr *icmph;
184 struct icmphdr _ih;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800185
186 /* Not enough header? */
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300187 icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800188 if (icmph == NULL) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200189 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000190 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL,
191 NULL, "nf_ct_icmp: short packet ");
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800192 return -NF_ACCEPT;
193 }
194
195 /* See ip_conntrack_proto_tcp.c */
Alexey Dobriyanc04d0552008-10-08 11:35:08 +0200196 if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING &&
Patrick McHardy96f6bf82006-04-06 14:19:24 -0700197 nf_ip_checksum(skb, hooknum, dataoff, 0)) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200198 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000199 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800200 "nf_ct_icmp: bad HW ICMP checksum ");
201 return -NF_ACCEPT;
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800202 }
203
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800204 /*
205 * 18 is the highest 'known' ICMP type. Anything else is a mystery
206 *
207 * RFC 1122: 3.2.2 Unknown ICMP messages types MUST be silently
208 * discarded.
209 */
210 if (icmph->type > NR_ICMP_TYPES) {
Alexey Dobriyanc2a2c7e2008-10-08 11:35:08 +0200211 if (LOG_INVALID(net, IPPROTO_ICMP))
Gao feng30e0c6a2013-03-24 23:50:40 +0000212 nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800213 "nf_ct_icmp: invalid ICMP type ");
214 return -NF_ACCEPT;
215 }
216
217 /* Need to track icmp error message? */
Joe Perches3666ed12009-11-23 23:17:06 +0100218 if (icmph->type != ICMP_DEST_UNREACH &&
219 icmph->type != ICMP_SOURCE_QUENCH &&
220 icmph->type != ICMP_TIME_EXCEEDED &&
221 icmph->type != ICMP_PARAMETERPROB &&
222 icmph->type != ICMP_REDIRECT)
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800223 return NF_ACCEPT;
224
Patrick McHardy5d0aa2c2010-02-15 18:13:33 +0100225 return icmp_error_message(net, tmpl, skb, ctinfo, hooknum);
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800226}
227
Patrick McHardye281db5c2007-03-04 15:57:25 -0800228#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800229
230#include <linux/netfilter/nfnetlink.h>
231#include <linux/netfilter/nfnetlink_conntrack.h>
232
Patrick McHardyfdf70832007-09-28 14:37:41 -0700233static int icmp_tuple_to_nlattr(struct sk_buff *skb,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800234 const struct nf_conntrack_tuple *t)
235{
David S. Millerd317e4f2012-04-01 20:40:20 -0400236 if (nla_put_be16(skb, CTA_PROTO_ICMP_ID, t->src.u.icmp.id) ||
237 nla_put_u8(skb, CTA_PROTO_ICMP_TYPE, t->dst.u.icmp.type) ||
238 nla_put_u8(skb, CTA_PROTO_ICMP_CODE, t->dst.u.icmp.code))
239 goto nla_put_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800240 return 0;
241
Patrick McHardydf6fb862007-09-28 14:37:03 -0700242nla_put_failure:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800243 return -1;
244}
245
Patrick McHardyf73e9242007-09-28 14:39:55 -0700246static const struct nla_policy icmp_nla_policy[CTA_PROTO_MAX+1] = {
247 [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
248 [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
249 [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800250};
251
Patrick McHardyfdf70832007-09-28 14:37:41 -0700252static int icmp_nlattr_to_tuple(struct nlattr *tb[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800253 struct nf_conntrack_tuple *tuple)
254{
Joe Perches3666ed12009-11-23 23:17:06 +0100255 if (!tb[CTA_PROTO_ICMP_TYPE] ||
256 !tb[CTA_PROTO_ICMP_CODE] ||
257 !tb[CTA_PROTO_ICMP_ID])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800258 return -EINVAL;
259
Patrick McHardy77236b62007-12-17 22:29:45 -0800260 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
261 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
262 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMP_ID]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800263
Joe Perches3666ed12009-11-23 23:17:06 +0100264 if (tuple->dst.u.icmp.type >= sizeof(invmap) ||
265 !invmap[tuple->dst.u.icmp.type])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800266 return -EINVAL;
267
268 return 0;
269}
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100270
271static int icmp_nlattr_tuple_size(void)
272{
273 return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1);
274}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800275#endif
276
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100277#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
278
279#include <linux/netfilter/nfnetlink.h>
280#include <linux/netfilter/nfnetlink_cttimeout.h>
281
Gao feng8264deb2012-05-28 21:04:23 +0000282static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[],
283 struct net *net, void *data)
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100284{
285 unsigned int *timeout = data;
Gao feng8264deb2012-05-28 21:04:23 +0000286 struct nf_icmp_net *in = icmp_pernet(net);
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100287
288 if (tb[CTA_TIMEOUT_ICMP_TIMEOUT]) {
289 *timeout =
290 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMP_TIMEOUT])) * HZ;
291 } else {
292 /* Set default ICMP timeout. */
Gao feng8264deb2012-05-28 21:04:23 +0000293 *timeout = in->timeout;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100294 }
295 return 0;
296}
297
298static int
299icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
300{
301 const unsigned int *timeout = data;
302
David S. Millerd317e4f2012-04-01 20:40:20 -0400303 if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ)))
304 goto nla_put_failure;
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100305 return 0;
306
307nla_put_failure:
308 return -ENOSPC;
309}
310
311static const struct nla_policy
312icmp_timeout_nla_policy[CTA_TIMEOUT_ICMP_MAX+1] = {
313 [CTA_TIMEOUT_ICMP_TIMEOUT] = { .type = NLA_U32 },
314};
315#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
316
Patrick McHardy933a41e2006-11-29 02:35:18 +0100317#ifdef CONFIG_SYSCTL
Patrick McHardy933a41e2006-11-29 02:35:18 +0100318static struct ctl_table icmp_sysctl_table[] = {
319 {
Patrick McHardy933a41e2006-11-29 02:35:18 +0100320 .procname = "nf_conntrack_icmp_timeout",
Patrick McHardy933a41e2006-11-29 02:35:18 +0100321 .maxlen = sizeof(unsigned int),
322 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800323 .proc_handler = proc_dointvec_jiffies,
Patrick McHardy933a41e2006-11-29 02:35:18 +0100324 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800325 { }
Patrick McHardy933a41e2006-11-29 02:35:18 +0100326};
Patrick McHardya999e682006-11-29 02:35:20 +0100327#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
328static struct ctl_table icmp_compat_sysctl_table[] = {
329 {
Patrick McHardya999e682006-11-29 02:35:20 +0100330 .procname = "ip_conntrack_icmp_timeout",
Patrick McHardya999e682006-11-29 02:35:20 +0100331 .maxlen = sizeof(unsigned int),
332 .mode = 0644,
Alexey Dobriyan6d9f2392008-11-03 18:21:05 -0800333 .proc_handler = proc_dointvec_jiffies,
Patrick McHardya999e682006-11-29 02:35:20 +0100334 },
Eric W. Biedermanf8572d82009-11-05 13:32:03 -0800335 { }
Patrick McHardya999e682006-11-29 02:35:20 +0100336};
337#endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */
Patrick McHardy933a41e2006-11-29 02:35:18 +0100338#endif /* CONFIG_SYSCTL */
339
Gao fenga9082b42012-06-21 04:36:49 +0000340static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn,
341 struct nf_icmp_net *in)
Gao feng4b626b92012-05-28 21:04:14 +0000342{
Gao feng4b626b92012-05-28 21:04:14 +0000343#ifdef CONFIG_SYSCTL
344 pn->ctl_table = kmemdup(icmp_sysctl_table,
345 sizeof(icmp_sysctl_table),
346 GFP_KERNEL);
347 if (!pn->ctl_table)
348 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000349
Gao feng4b626b92012-05-28 21:04:14 +0000350 pn->ctl_table[0].data = &in->timeout;
Gao fenga9082b42012-06-21 04:36:49 +0000351#endif
352 return 0;
353}
354
355static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn,
356 struct nf_icmp_net *in)
357{
358#ifdef CONFIG_SYSCTL
Gao feng4b626b92012-05-28 21:04:14 +0000359#ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT
360 pn->ctl_compat_table = kmemdup(icmp_compat_sysctl_table,
361 sizeof(icmp_compat_sysctl_table),
362 GFP_KERNEL);
Gao fenga9082b42012-06-21 04:36:49 +0000363 if (!pn->ctl_compat_table)
Gao feng4b626b92012-05-28 21:04:14 +0000364 return -ENOMEM;
Gao fenga9082b42012-06-21 04:36:49 +0000365
Gao feng4b626b92012-05-28 21:04:14 +0000366 pn->ctl_compat_table[0].data = &in->timeout;
367#endif
368#endif
369 return 0;
370}
371
Gao fenga9082b42012-06-21 04:36:49 +0000372static int icmp_init_net(struct net *net, u_int16_t proto)
373{
374 int ret;
375 struct nf_icmp_net *in = icmp_pernet(net);
376 struct nf_proto_net *pn = &in->pn;
377
378 in->timeout = nf_ct_icmp_timeout;
379
380 ret = icmp_kmemdup_compat_sysctl_table(pn, in);
381 if (ret < 0)
382 return ret;
383
384 ret = icmp_kmemdup_sysctl_table(pn, in);
385 if (ret < 0)
386 nf_ct_kfree_compat_sysctl_table(pn);
387
388 return ret;
389}
390
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000391static struct nf_proto_net *icmp_get_net_proto(struct net *net)
392{
393 return &net->ct.nf_ct_proto.icmp.pn;
394}
395
Patrick McHardy61075af2007-07-14 20:48:19 -0700396struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly =
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800397{
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800398 .l3proto = PF_INET,
Martin Josefsson605dcad2006-11-29 02:35:06 +0100399 .l4proto = IPPROTO_ICMP,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800400 .name = "icmp",
401 .pkt_to_tuple = icmp_pkt_to_tuple,
402 .invert_tuple = icmp_invert_tuple,
403 .print_tuple = icmp_print_tuple,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800404 .packet = icmp_packet,
Pablo Neira Ayuso2c8503f2012-02-28 18:23:31 +0100405 .get_timeouts = icmp_get_timeouts,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800406 .new = icmp_new,
407 .error = icmp_error,
408 .destroy = NULL,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800409 .me = NULL,
Patrick McHardye281db5c2007-03-04 15:57:25 -0800410#if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
Patrick McHardyfdf70832007-09-28 14:37:41 -0700411 .tuple_to_nlattr = icmp_tuple_to_nlattr,
Holger Eitzenbergera400c302009-03-25 21:53:39 +0100412 .nlattr_tuple_size = icmp_nlattr_tuple_size,
Patrick McHardyfdf70832007-09-28 14:37:41 -0700413 .nlattr_to_tuple = icmp_nlattr_to_tuple,
Patrick McHardyf73e9242007-09-28 14:39:55 -0700414 .nla_policy = icmp_nla_policy,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800415#endif
Pablo Neira Ayuso50978462012-02-28 19:13:48 +0100416#if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT)
417 .ctnl_timeout = {
418 .nlattr_to_obj = icmp_timeout_nlattr_to_obj,
419 .obj_to_nlattr = icmp_timeout_obj_to_nlattr,
420 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
421 .obj_size = sizeof(unsigned int),
422 .nla_policy = icmp_timeout_nla_policy,
423 },
424#endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */
Gao feng4b626b92012-05-28 21:04:14 +0000425 .init_net = icmp_init_net,
Pablo Neira Ayuso08911472012-06-29 05:23:24 +0000426 .get_net_proto = icmp_get_net_proto,
Yasuyuki Kozakai9fb9cbb2005-11-09 16:38:16 -0800427};