Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 2 | * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org> |
Patrick McHardy | f229f6c | 2013-04-06 15:24:29 +0200 | [diff] [blame] | 3 | * (C) 2006-2010 Patrick McHardy <kaber@trash.net> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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. |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/types.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 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> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 20 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 21 | #include <net/netfilter/nf_conntrack_core.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 22 | #include <net/netfilter/nf_conntrack_zones.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 23 | #include <net/netfilter/nf_log.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 24 | |
Patrick McHardy | 71320af | 2009-01-12 00:06:07 +0000 | [diff] [blame] | 25 | static unsigned int nf_ct_icmp_timeout __read_mostly = 30*HZ; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 26 | |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 27 | static inline struct nf_icmp_net *icmp_pernet(struct net *net) |
| 28 | { |
| 29 | return &net->ct.nf_ct_proto.icmp; |
| 30 | } |
| 31 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 32 | static bool icmp_pkt_to_tuple(const struct sk_buff *skb, unsigned int dataoff, |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 33 | struct net *net, struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 34 | { |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 35 | const struct icmphdr *hp; |
| 36 | struct icmphdr _hdr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 37 | |
| 38 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 39 | if (hp == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 40 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 41 | |
| 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 Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 46 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 49 | /* Add 1; spaces filled with 0. */ |
| 50 | static 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 Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 61 | static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 62 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 63 | { |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 64 | if (orig->dst.u.icmp.type >= sizeof(invmap) || |
| 65 | !invmap[orig->dst.u.icmp.type]) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 66 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 67 | |
| 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 Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 71 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Print out the per-protocol part of the tuple. */ |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 75 | static void icmp_print_tuple(struct seq_file *s, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 76 | const struct nf_conntrack_tuple *tuple) |
| 77 | { |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 78 | 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 84 | static unsigned int *icmp_get_timeouts(struct net *net) |
| 85 | { |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 86 | return &icmp_pernet(net)->timeout; |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 89 | /* Returns verdict for packet, or -1 for invalid. */ |
| 90 | static int icmp_packet(struct nf_conn *ct, |
| 91 | const struct sk_buff *skb, |
| 92 | unsigned int dataoff, |
| 93 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 94 | u_int8_t pf, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 95 | unsigned int hooknum, |
| 96 | unsigned int *timeout) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 97 | { |
Jan Kasprzak | f87fb66 | 2009-06-08 15:53:43 +0200 | [diff] [blame] | 98 | /* 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 Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 101 | nf_ct_refresh_acct(ct, ctinfo, skb, *timeout); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 102 | |
| 103 | return NF_ACCEPT; |
| 104 | } |
| 105 | |
| 106 | /* Called when a new connection for this protocol found. */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 107 | static bool icmp_new(struct nf_conn *ct, const struct sk_buff *skb, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 108 | unsigned int dataoff, unsigned int *timeouts) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 109 | { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 110 | 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 116 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 117 | if (ct->tuplehash[0].tuple.dst.u.icmp.type >= sizeof(valid_new) || |
| 118 | !valid_new[ct->tuplehash[0].tuple.dst.u.icmp.type]) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 119 | /* Can't create a new ICMP `conn' with this. */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 120 | pr_debug("icmp: can't create new conn with type %u\n", |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 121 | ct->tuplehash[0].tuple.dst.u.icmp.type); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 122 | nf_ct_dump_tuple_ip(&ct->tuplehash[0].tuple); |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 123 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 124 | } |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 125 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 128 | /* Returns conntrack if it dealt with ICMP, and filled in skb fields */ |
| 129 | static int |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 130 | icmp_error_message(struct net *net, struct nf_conn *tmpl, struct sk_buff *skb, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 131 | enum ip_conntrack_info *ctinfo, |
| 132 | unsigned int hooknum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 133 | { |
| 134 | struct nf_conntrack_tuple innertuple, origtuple; |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 135 | const struct nf_conntrack_l4proto *innerproto; |
| 136 | const struct nf_conntrack_tuple_hash *h; |
Daniel Borkmann | 308ac91 | 2015-08-08 21:40:01 +0200 | [diff] [blame] | 137 | const struct nf_conntrack_zone *zone; |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 138 | struct nf_conntrack_zone tmp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 139 | |
| 140 | NF_CT_ASSERT(skb->nfct == NULL); |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 141 | zone = nf_ct_zone_tmpl(tmpl, skb, &tmp); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 142 | |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 143 | /* 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. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 147 | PF_INET, net, &origtuple)) { |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 148 | pr_debug("icmp_error_message: failed to get tuple\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 149 | return -NF_ACCEPT; |
| 150 | } |
| 151 | |
Patrick McHardy | 923f490 | 2007-02-12 11:12:57 -0800 | [diff] [blame] | 152 | /* rcu_read_lock()ed by nf_hook_slow */ |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 153 | innerproto = __nf_ct_l4proto_find(PF_INET, origtuple.dst.protonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 154 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 155 | /* 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 158 | &nf_conntrack_l3proto_ipv4, innerproto)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 159 | pr_debug("icmp_error_message: no match\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 160 | return -NF_ACCEPT; |
| 161 | } |
| 162 | |
| 163 | *ctinfo = IP_CT_RELATED; |
| 164 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 165 | h = nf_conntrack_find_get(net, zone, &innertuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 166 | if (!h) { |
Yasuyuki Kozakai | 130e7a8 | 2007-07-14 20:45:41 -0700 | [diff] [blame] | 167 | pr_debug("icmp_error_message: no match\n"); |
| 168 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Yasuyuki Kozakai | 130e7a8 | 2007-07-14 20:45:41 -0700 | [diff] [blame] | 171 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 172 | *ctinfo += IP_CT_IS_REPLY; |
| 173 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 174 | /* Update skb to refer to this connection */ |
| 175 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; |
| 176 | skb->nfctinfo = *ctinfo; |
Pablo Neira Ayuso | 88ed01d | 2011-06-02 15:08:45 +0200 | [diff] [blame] | 177 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /* Small and modified version of icmp_rcv */ |
| 181 | static int |
Patrick McHardy | 8fea97e | 2010-02-15 17:45:08 +0100 | [diff] [blame] | 182 | icmp_error(struct net *net, struct nf_conn *tmpl, |
| 183 | struct sk_buff *skb, unsigned int dataoff, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 184 | enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 185 | { |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 186 | const struct icmphdr *icmph; |
| 187 | struct icmphdr _ih; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 188 | |
| 189 | /* Not enough header? */ |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 190 | icmph = skb_header_pointer(skb, ip_hdrlen(skb), sizeof(_ih), &_ih); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 191 | if (icmph == NULL) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 192 | if (LOG_INVALID(net, IPPROTO_ICMP)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 193 | nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, |
| 194 | NULL, "nf_ct_icmp: short packet "); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 195 | return -NF_ACCEPT; |
| 196 | } |
| 197 | |
| 198 | /* See ip_conntrack_proto_tcp.c */ |
Alexey Dobriyan | c04d055 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 199 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 200 | nf_ip_checksum(skb, hooknum, dataoff, 0)) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 201 | if (LOG_INVALID(net, IPPROTO_ICMP)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 202 | nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 203 | "nf_ct_icmp: bad HW ICMP checksum "); |
| 204 | return -NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 207 | /* |
| 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 Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 214 | if (LOG_INVALID(net, IPPROTO_ICMP)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 215 | nf_log_packet(net, PF_INET, 0, skb, NULL, NULL, NULL, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 216 | "nf_ct_icmp: invalid ICMP type "); |
| 217 | return -NF_ACCEPT; |
| 218 | } |
| 219 | |
| 220 | /* Need to track icmp error message? */ |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 221 | 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 Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 226 | return NF_ACCEPT; |
| 227 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 228 | return icmp_error_message(net, tmpl, skb, ctinfo, hooknum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 229 | } |
| 230 | |
Duan Jiong | 24de3d3 | 2014-06-30 09:19:32 +0800 | [diff] [blame] | 231 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 232 | |
| 233 | #include <linux/netfilter/nfnetlink.h> |
| 234 | #include <linux/netfilter/nfnetlink_conntrack.h> |
| 235 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 236 | static int icmp_tuple_to_nlattr(struct sk_buff *skb, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 237 | const struct nf_conntrack_tuple *t) |
| 238 | { |
David S. Miller | d317e4f | 2012-04-01 20:40:20 -0400 | [diff] [blame] | 239 | 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 Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 245 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 246 | return -1; |
| 247 | } |
| 248 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 249 | static 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 Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 253 | }; |
| 254 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 255 | static int icmp_nlattr_to_tuple(struct nlattr *tb[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 256 | struct nf_conntrack_tuple *tuple) |
| 257 | { |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 258 | if (!tb[CTA_PROTO_ICMP_TYPE] || |
| 259 | !tb[CTA_PROTO_ICMP_CODE] || |
| 260 | !tb[CTA_PROTO_ICMP_ID]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 263 | 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 Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 266 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 267 | if (tuple->dst.u.icmp.type >= sizeof(invmap) || |
| 268 | !invmap[tuple->dst.u.icmp.type]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 269 | return -EINVAL; |
| 270 | |
| 271 | return 0; |
| 272 | } |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 273 | |
| 274 | static int icmp_nlattr_tuple_size(void) |
| 275 | { |
| 276 | return nla_policy_len(icmp_nla_policy, CTA_PROTO_MAX + 1); |
| 277 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 278 | #endif |
| 279 | |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 280 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 281 | |
| 282 | #include <linux/netfilter/nfnetlink.h> |
| 283 | #include <linux/netfilter/nfnetlink_cttimeout.h> |
| 284 | |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 285 | static int icmp_timeout_nlattr_to_obj(struct nlattr *tb[], |
| 286 | struct net *net, void *data) |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 287 | { |
| 288 | unsigned int *timeout = data; |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 289 | struct nf_icmp_net *in = icmp_pernet(net); |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 290 | |
| 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 feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 296 | *timeout = in->timeout; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 297 | } |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | static int |
| 302 | icmp_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) |
| 303 | { |
| 304 | const unsigned int *timeout = data; |
| 305 | |
David S. Miller | d317e4f | 2012-04-01 20:40:20 -0400 | [diff] [blame] | 306 | if (nla_put_be32(skb, CTA_TIMEOUT_ICMP_TIMEOUT, htonl(*timeout / HZ))) |
| 307 | goto nla_put_failure; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 308 | return 0; |
| 309 | |
| 310 | nla_put_failure: |
| 311 | return -ENOSPC; |
| 312 | } |
| 313 | |
| 314 | static const struct nla_policy |
| 315 | icmp_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 McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 320 | #ifdef CONFIG_SYSCTL |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 321 | static struct ctl_table icmp_sysctl_table[] = { |
| 322 | { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 323 | .procname = "nf_conntrack_icmp_timeout", |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 324 | .maxlen = sizeof(unsigned int), |
| 325 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 326 | .proc_handler = proc_dointvec_jiffies, |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 327 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 328 | { } |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 329 | }; |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 330 | #ifdef CONFIG_NF_CONNTRACK_PROC_COMPAT |
| 331 | static struct ctl_table icmp_compat_sysctl_table[] = { |
| 332 | { |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 333 | .procname = "ip_conntrack_icmp_timeout", |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 334 | .maxlen = sizeof(unsigned int), |
| 335 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 336 | .proc_handler = proc_dointvec_jiffies, |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 337 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 338 | { } |
Patrick McHardy | a999e68 | 2006-11-29 02:35:20 +0100 | [diff] [blame] | 339 | }; |
| 340 | #endif /* CONFIG_NF_CONNTRACK_PROC_COMPAT */ |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 341 | #endif /* CONFIG_SYSCTL */ |
| 342 | |
Gao feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 343 | static int icmp_kmemdup_sysctl_table(struct nf_proto_net *pn, |
| 344 | struct nf_icmp_net *in) |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 345 | { |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 346 | #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 feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 352 | |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 353 | pn->ctl_table[0].data = &in->timeout; |
Gao feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 354 | #endif |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int icmp_kmemdup_compat_sysctl_table(struct nf_proto_net *pn, |
| 359 | struct nf_icmp_net *in) |
| 360 | { |
| 361 | #ifdef CONFIG_SYSCTL |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 362 | #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 feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 366 | if (!pn->ctl_compat_table) |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 367 | return -ENOMEM; |
Gao feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 368 | |
Gao feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 369 | pn->ctl_compat_table[0].data = &in->timeout; |
| 370 | #endif |
| 371 | #endif |
| 372 | return 0; |
| 373 | } |
| 374 | |
Gao feng | a9082b4 | 2012-06-21 04:36:49 +0000 | [diff] [blame] | 375 | static 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 Ayuso | 0891147 | 2012-06-29 05:23:24 +0000 | [diff] [blame] | 394 | static struct nf_proto_net *icmp_get_net_proto(struct net *net) |
| 395 | { |
| 396 | return &net->ct.nf_ct_proto.icmp.pn; |
| 397 | } |
| 398 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 399 | struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp __read_mostly = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 400 | { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 401 | .l3proto = PF_INET, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 402 | .l4proto = IPPROTO_ICMP, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 403 | .name = "icmp", |
| 404 | .pkt_to_tuple = icmp_pkt_to_tuple, |
| 405 | .invert_tuple = icmp_invert_tuple, |
| 406 | .print_tuple = icmp_print_tuple, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 407 | .packet = icmp_packet, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 408 | .get_timeouts = icmp_get_timeouts, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 409 | .new = icmp_new, |
| 410 | .error = icmp_error, |
| 411 | .destroy = NULL, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 412 | .me = NULL, |
Duan Jiong | 24de3d3 | 2014-06-30 09:19:32 +0800 | [diff] [blame] | 413 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 414 | .tuple_to_nlattr = icmp_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 415 | .nlattr_tuple_size = icmp_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 416 | .nlattr_to_tuple = icmp_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 417 | .nla_policy = icmp_nla_policy, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 418 | #endif |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 419 | #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 feng | 4b626b9 | 2012-05-28 21:04:14 +0000 | [diff] [blame] | 428 | .init_net = icmp_init_net, |
Pablo Neira Ayuso | 0891147 | 2012-06-29 05:23:24 +0000 | [diff] [blame] | 429 | .get_net_proto = icmp_get_net_proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 430 | }; |