Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C)2003,2004 USAGI/WIDE Project |
| 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. |
| 7 | * |
| 8 | * Author: |
| 9 | * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/types.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 13 | #include <linux/timer.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/netfilter.h> |
| 16 | #include <linux/in6.h> |
| 17 | #include <linux/icmpv6.h> |
| 18 | #include <linux/ipv6.h> |
| 19 | #include <net/ipv6.h> |
| 20 | #include <net/ip6_checksum.h> |
| 21 | #include <linux/seq_file.h> |
| 22 | #include <linux/netfilter_ipv6.h> |
| 23 | #include <net/netfilter/nf_conntrack_tuple.h> |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 24 | #include <net/netfilter/nf_conntrack_l4proto.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 25 | #include <net/netfilter/nf_conntrack_core.h> |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 26 | #include <net/netfilter/nf_conntrack_zones.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 27 | #include <net/netfilter/ipv6/nf_conntrack_icmpv6.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 28 | #include <net/netfilter/nf_log.h> |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 29 | |
Patrick McHardy | 71320af | 2009-01-12 00:06:07 +0000 | [diff] [blame] | 30 | static unsigned int nf_ct_icmpv6_timeout __read_mostly = 30*HZ; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 31 | |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 32 | static inline struct nf_icmp_net *icmpv6_pernet(struct net *net) |
| 33 | { |
| 34 | return &net->ct.nf_ct_proto.icmpv6; |
| 35 | } |
| 36 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 37 | static bool icmpv6_pkt_to_tuple(const struct sk_buff *skb, |
| 38 | unsigned int dataoff, |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 39 | struct net *net, |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 40 | struct nf_conntrack_tuple *tuple) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 41 | { |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 42 | const struct icmp6hdr *hp; |
| 43 | struct icmp6hdr _hdr; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 44 | |
| 45 | hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr); |
| 46 | if (hp == NULL) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 47 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 48 | tuple->dst.u.icmp.type = hp->icmp6_type; |
| 49 | tuple->src.u.icmp.id = hp->icmp6_identifier; |
| 50 | tuple->dst.u.icmp.code = hp->icmp6_code; |
| 51 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 52 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 55 | /* Add 1; spaces filled with 0. */ |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 56 | static const u_int8_t invmap[] = { |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 57 | [ICMPV6_ECHO_REQUEST - 128] = ICMPV6_ECHO_REPLY + 1, |
| 58 | [ICMPV6_ECHO_REPLY - 128] = ICMPV6_ECHO_REQUEST + 1, |
Eric Leblond | a51f42f | 2009-02-09 14:33:03 -0800 | [diff] [blame] | 59 | [ICMPV6_NI_QUERY - 128] = ICMPV6_NI_REPLY + 1, |
Ian Morris | f9527ea | 2015-10-11 17:32:17 +0100 | [diff] [blame] | 60 | [ICMPV6_NI_REPLY - 128] = ICMPV6_NI_QUERY + 1 |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 61 | }; |
| 62 | |
Eric Leblond | 3f90071 | 2009-02-09 14:33:20 -0800 | [diff] [blame] | 63 | static const u_int8_t noct_valid_new[] = { |
| 64 | [ICMPV6_MGM_QUERY - 130] = 1, |
Ian Morris | f9527ea | 2015-10-11 17:32:17 +0100 | [diff] [blame] | 65 | [ICMPV6_MGM_REPORT - 130] = 1, |
Eric Leblond | 3f90071 | 2009-02-09 14:33:20 -0800 | [diff] [blame] | 66 | [ICMPV6_MGM_REDUCTION - 130] = 1, |
| 67 | [NDISC_ROUTER_SOLICITATION - 130] = 1, |
| 68 | [NDISC_ROUTER_ADVERTISEMENT - 130] = 1, |
| 69 | [NDISC_NEIGHBOUR_SOLICITATION - 130] = 1, |
| 70 | [NDISC_NEIGHBOUR_ADVERTISEMENT - 130] = 1, |
| 71 | [ICMPV6_MLD2_REPORT - 130] = 1 |
| 72 | }; |
| 73 | |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 74 | static bool icmpv6_invert_tuple(struct nf_conntrack_tuple *tuple, |
| 75 | const struct nf_conntrack_tuple *orig) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 76 | { |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 77 | int type = orig->dst.u.icmp.type - 128; |
| 78 | if (type < 0 || type >= sizeof(invmap) || !invmap[type]) |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 79 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 80 | |
| 81 | tuple->src.u.icmp.id = orig->src.u.icmp.id; |
| 82 | tuple->dst.u.icmp.type = invmap[type] - 1; |
| 83 | tuple->dst.u.icmp.code = orig->dst.u.icmp.code; |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 84 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | /* Print out the per-protocol part of the tuple. */ |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 88 | static void icmpv6_print_tuple(struct seq_file *s, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 89 | const struct nf_conntrack_tuple *tuple) |
| 90 | { |
Joe Perches | 824f1fb | 2014-09-29 16:08:22 -0700 | [diff] [blame] | 91 | seq_printf(s, "type=%u code=%u id=%u ", |
| 92 | tuple->dst.u.icmp.type, |
| 93 | tuple->dst.u.icmp.code, |
| 94 | ntohs(tuple->src.u.icmp.id)); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 97 | static unsigned int *icmpv6_get_timeouts(struct net *net) |
| 98 | { |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 99 | return &icmpv6_pernet(net)->timeout; |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 100 | } |
| 101 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 102 | /* Returns verdict for packet, or -1 for invalid. */ |
| 103 | static int icmpv6_packet(struct nf_conn *ct, |
| 104 | const struct sk_buff *skb, |
| 105 | unsigned int dataoff, |
| 106 | enum ip_conntrack_info ctinfo, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 107 | u_int8_t pf, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 108 | unsigned int hooknum, |
| 109 | unsigned int *timeout) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 110 | { |
Jan Kasprzak | f87fb66 | 2009-06-08 15:53:43 +0200 | [diff] [blame] | 111 | /* Do not immediately delete the connection after the first |
| 112 | successful reply to avoid excessive conntrackd traffic |
| 113 | and also to handle correctly ICMP echo reply duplicates. */ |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 114 | nf_ct_refresh_acct(ct, ctinfo, skb, *timeout); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 115 | |
| 116 | return NF_ACCEPT; |
| 117 | } |
| 118 | |
| 119 | /* Called when a new connection for this protocol found. */ |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 120 | static bool icmpv6_new(struct nf_conn *ct, const struct sk_buff *skb, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 121 | unsigned int dataoff, unsigned int *timeouts) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 122 | { |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 123 | static const u_int8_t valid_new[] = { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 124 | [ICMPV6_ECHO_REQUEST - 128] = 1, |
| 125 | [ICMPV6_NI_QUERY - 128] = 1 |
| 126 | }; |
Patrick McHardy | c88130b | 2008-01-31 04:42:11 -0800 | [diff] [blame] | 127 | int type = ct->tuplehash[0].tuple.dst.u.icmp.type - 128; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 128 | |
Yasuyuki Kozakai | f16c910 | 2005-12-05 13:32:50 -0800 | [diff] [blame] | 129 | if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) { |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 130 | /* Can't create a new ICMPv6 `conn' with this. */ |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 131 | pr_debug("icmpv6: can't create new conn with type %u\n", |
| 132 | type + 128); |
Jan Engelhardt | 3c9fba6 | 2008-04-14 11:15:54 +0200 | [diff] [blame] | 133 | nf_ct_dump_tuple_ipv6(&ct->tuplehash[0].tuple); |
Eric Leblond | 55df4ac | 2009-02-18 16:30:56 +0100 | [diff] [blame] | 134 | if (LOG_INVALID(nf_ct_net(ct), IPPROTO_ICMPV6)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 135 | nf_log_packet(nf_ct_net(ct), PF_INET6, 0, skb, NULL, |
| 136 | NULL, NULL, |
Eric Leblond | 55df4ac | 2009-02-18 16:30:56 +0100 | [diff] [blame] | 137 | "nf_ct_icmpv6: invalid new with type %d ", |
| 138 | type + 128); |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 139 | return false; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 140 | } |
Jan Engelhardt | 09f263c | 2008-04-14 11:15:53 +0200 | [diff] [blame] | 141 | return true; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 142 | } |
| 143 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 144 | static int |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 145 | icmpv6_error_message(struct net *net, struct nf_conn *tmpl, |
Alexey Dobriyan | 74c51a1 | 2008-10-08 11:35:05 +0200 | [diff] [blame] | 146 | struct sk_buff *skb, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 147 | unsigned int icmp6off, |
| 148 | enum ip_conntrack_info *ctinfo, |
| 149 | unsigned int hooknum) |
| 150 | { |
| 151 | struct nf_conntrack_tuple intuple, origtuple; |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 152 | const struct nf_conntrack_tuple_hash *h; |
| 153 | const struct nf_conntrack_l4proto *inproto; |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 154 | struct nf_conntrack_zone tmp; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 155 | |
| 156 | NF_CT_ASSERT(skb->nfct == NULL); |
| 157 | |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 158 | /* Are they talking about one of our connections? */ |
| 159 | if (!nf_ct_get_tuplepr(skb, |
| 160 | skb_network_offset(skb) |
| 161 | + sizeof(struct ipv6hdr) |
| 162 | + sizeof(struct icmp6hdr), |
Eric W. Biederman | a31f1ad | 2015-09-18 14:33:04 -0500 | [diff] [blame] | 163 | PF_INET6, net, &origtuple)) { |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 164 | pr_debug("icmpv6_error: Can't get tuple\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 165 | return -NF_ACCEPT; |
| 166 | } |
| 167 | |
Aaron Conole | e2361cb | 2016-09-21 11:35:04 -0400 | [diff] [blame] | 168 | /* rcu_read_lock()ed by nf_hook_thresh */ |
Yasuyuki Kozakai | e2a3123 | 2007-07-14 20:45:14 -0700 | [diff] [blame] | 169 | inproto = __nf_ct_l4proto_find(PF_INET6, origtuple.dst.protonum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 170 | |
| 171 | /* Ordinarily, we'd expect the inverted tupleproto, but it's |
| 172 | been preserved inside the ICMP. */ |
| 173 | if (!nf_ct_invert_tuple(&intuple, &origtuple, |
| 174 | &nf_conntrack_l3proto_ipv6, inproto)) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 175 | pr_debug("icmpv6_error: Can't invert tuple\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 176 | return -NF_ACCEPT; |
| 177 | } |
| 178 | |
| 179 | *ctinfo = IP_CT_RELATED; |
| 180 | |
Daniel Borkmann | 5e8018f | 2015-08-14 16:03:40 +0200 | [diff] [blame] | 181 | h = nf_conntrack_find_get(net, nf_ct_zone_tmpl(tmpl, skb, &tmp), |
| 182 | &intuple); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 183 | if (!h) { |
Patrick McHardy | 0d53778 | 2007-07-07 22:39:38 -0700 | [diff] [blame] | 184 | pr_debug("icmpv6_error: no match\n"); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 185 | return -NF_ACCEPT; |
| 186 | } else { |
| 187 | if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY) |
| 188 | *ctinfo += IP_CT_IS_REPLY; |
| 189 | } |
| 190 | |
| 191 | /* Update skb to refer to this connection */ |
| 192 | skb->nfct = &nf_ct_tuplehash_to_ctrack(h)->ct_general; |
| 193 | skb->nfctinfo = *ctinfo; |
Pablo Neira Ayuso | 88ed01d | 2011-06-02 15:08:45 +0200 | [diff] [blame] | 194 | return NF_ACCEPT; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int |
Patrick McHardy | 8fea97e | 2010-02-15 17:45:08 +0100 | [diff] [blame] | 198 | icmpv6_error(struct net *net, struct nf_conn *tmpl, |
| 199 | struct sk_buff *skb, unsigned int dataoff, |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 200 | enum ip_conntrack_info *ctinfo, u_int8_t pf, unsigned int hooknum) |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 201 | { |
Jan Engelhardt | 7cc3864 | 2008-01-31 04:53:05 -0800 | [diff] [blame] | 202 | const struct icmp6hdr *icmp6h; |
| 203 | struct icmp6hdr _ih; |
Eric Leblond | 3f90071 | 2009-02-09 14:33:20 -0800 | [diff] [blame] | 204 | int type; |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 205 | |
| 206 | icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih); |
| 207 | if (icmp6h == NULL) { |
Alexey Dobriyan | c2a2c7e | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 208 | if (LOG_INVALID(net, IPPROTO_ICMPV6)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 209 | nf_log_packet(net, PF_INET6, 0, skb, NULL, NULL, NULL, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 210 | "nf_ct_icmpv6: short packet "); |
| 211 | return -NF_ACCEPT; |
| 212 | } |
| 213 | |
Alexey Dobriyan | c04d055 | 2008-10-08 11:35:08 +0200 | [diff] [blame] | 214 | if (net->ct.sysctl_checksum && hooknum == NF_INET_PRE_ROUTING && |
Patrick McHardy | 96f6bf8 | 2006-04-06 14:19:24 -0700 | [diff] [blame] | 215 | nf_ip6_checksum(skb, hooknum, dataoff, IPPROTO_ICMPV6)) { |
Eric Leblond | 4aa3b2e | 2009-02-18 15:28:46 +0100 | [diff] [blame] | 216 | if (LOG_INVALID(net, IPPROTO_ICMPV6)) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame] | 217 | nf_log_packet(net, PF_INET6, 0, skb, NULL, NULL, NULL, |
Eric Leblond | 4aa3b2e | 2009-02-18 15:28:46 +0100 | [diff] [blame] | 218 | "nf_ct_icmpv6: ICMPv6 checksum failed "); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 219 | return -NF_ACCEPT; |
| 220 | } |
| 221 | |
Eric Leblond | 3f90071 | 2009-02-09 14:33:20 -0800 | [diff] [blame] | 222 | type = icmp6h->icmp6_type - 130; |
| 223 | if (type >= 0 && type < sizeof(noct_valid_new) && |
| 224 | noct_valid_new[type]) { |
Eric Dumazet | 5bfddbd | 2010-06-08 16:09:52 +0200 | [diff] [blame] | 225 | skb->nfct = &nf_ct_untracked_get()->ct_general; |
Eric Leblond | 3f90071 | 2009-02-09 14:33:20 -0800 | [diff] [blame] | 226 | skb->nfctinfo = IP_CT_NEW; |
| 227 | nf_conntrack_get(skb->nfct); |
| 228 | return NF_ACCEPT; |
| 229 | } |
| 230 | |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 231 | /* is not error message ? */ |
| 232 | if (icmp6h->icmp6_type >= 128) |
| 233 | return NF_ACCEPT; |
| 234 | |
Patrick McHardy | 5d0aa2c | 2010-02-15 18:13:33 +0100 | [diff] [blame] | 235 | return icmpv6_error_message(net, tmpl, skb, dataoff, ctinfo, hooknum); |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 238 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 239 | |
| 240 | #include <linux/netfilter/nfnetlink.h> |
| 241 | #include <linux/netfilter/nfnetlink_conntrack.h> |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 242 | static int icmpv6_tuple_to_nlattr(struct sk_buff *skb, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 243 | const struct nf_conntrack_tuple *t) |
| 244 | { |
David S. Miller | e549a6b | 2012-04-01 20:28:52 -0400 | [diff] [blame] | 245 | if (nla_put_be16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id) || |
| 246 | nla_put_u8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type) || |
| 247 | nla_put_u8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code)) |
| 248 | goto nla_put_failure; |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 249 | return 0; |
| 250 | |
Patrick McHardy | df6fb86 | 2007-09-28 14:37:03 -0700 | [diff] [blame] | 251 | nla_put_failure: |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 252 | return -1; |
| 253 | } |
| 254 | |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 255 | static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = { |
| 256 | [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 }, |
| 257 | [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 }, |
| 258 | [CTA_PROTO_ICMPV6_ID] = { .type = NLA_U16 }, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 259 | }; |
| 260 | |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 261 | static int icmpv6_nlattr_to_tuple(struct nlattr *tb[], |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 262 | struct nf_conntrack_tuple *tuple) |
| 263 | { |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 264 | if (!tb[CTA_PROTO_ICMPV6_TYPE] || |
| 265 | !tb[CTA_PROTO_ICMPV6_CODE] || |
| 266 | !tb[CTA_PROTO_ICMPV6_ID]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 267 | return -EINVAL; |
| 268 | |
Patrick McHardy | 77236b6 | 2007-12-17 22:29:45 -0800 | [diff] [blame] | 269 | tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]); |
| 270 | tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]); |
| 271 | tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]); |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 272 | |
Joe Perches | 3666ed1 | 2009-11-23 23:17:06 +0100 | [diff] [blame] | 273 | if (tuple->dst.u.icmp.type < 128 || |
| 274 | tuple->dst.u.icmp.type - 128 >= sizeof(invmap) || |
| 275 | !invmap[tuple->dst.u.icmp.type - 128]) |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 276 | return -EINVAL; |
| 277 | |
| 278 | return 0; |
| 279 | } |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 280 | |
| 281 | static int icmpv6_nlattr_tuple_size(void) |
| 282 | { |
| 283 | return nla_policy_len(icmpv6_nla_policy, CTA_PROTO_MAX + 1); |
| 284 | } |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 285 | #endif |
| 286 | |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 287 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 288 | |
| 289 | #include <linux/netfilter/nfnetlink.h> |
| 290 | #include <linux/netfilter/nfnetlink_cttimeout.h> |
| 291 | |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 292 | static int icmpv6_timeout_nlattr_to_obj(struct nlattr *tb[], |
| 293 | struct net *net, void *data) |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 294 | { |
| 295 | unsigned int *timeout = data; |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 296 | struct nf_icmp_net *in = icmpv6_pernet(net); |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 297 | |
| 298 | if (tb[CTA_TIMEOUT_ICMPV6_TIMEOUT]) { |
| 299 | *timeout = |
| 300 | ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMPV6_TIMEOUT])) * HZ; |
| 301 | } else { |
| 302 | /* Set default ICMPv6 timeout. */ |
Gao feng | 8264deb | 2012-05-28 21:04:23 +0000 | [diff] [blame] | 303 | *timeout = in->timeout; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 304 | } |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | static int |
| 309 | icmpv6_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data) |
| 310 | { |
| 311 | const unsigned int *timeout = data; |
| 312 | |
David S. Miller | e549a6b | 2012-04-01 20:28:52 -0400 | [diff] [blame] | 313 | if (nla_put_be32(skb, CTA_TIMEOUT_ICMPV6_TIMEOUT, htonl(*timeout / HZ))) |
| 314 | goto nla_put_failure; |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 315 | return 0; |
| 316 | |
| 317 | nla_put_failure: |
| 318 | return -ENOSPC; |
| 319 | } |
| 320 | |
| 321 | static const struct nla_policy |
| 322 | icmpv6_timeout_nla_policy[CTA_TIMEOUT_ICMPV6_MAX+1] = { |
| 323 | [CTA_TIMEOUT_ICMPV6_TIMEOUT] = { .type = NLA_U32 }, |
| 324 | }; |
| 325 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
| 326 | |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 327 | #ifdef CONFIG_SYSCTL |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 328 | static struct ctl_table icmpv6_sysctl_table[] = { |
| 329 | { |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 330 | .procname = "nf_conntrack_icmpv6_timeout", |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 331 | .maxlen = sizeof(unsigned int), |
| 332 | .mode = 0644, |
Alexey Dobriyan | 6d9f239 | 2008-11-03 18:21:05 -0800 | [diff] [blame] | 333 | .proc_handler = proc_dointvec_jiffies, |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 334 | }, |
Eric W. Biederman | f8572d8 | 2009-11-05 13:32:03 -0800 | [diff] [blame] | 335 | { } |
Patrick McHardy | 933a41e | 2006-11-29 02:35:18 +0100 | [diff] [blame] | 336 | }; |
| 337 | #endif /* CONFIG_SYSCTL */ |
| 338 | |
Gao feng | 8fc0278 | 2012-06-21 04:36:50 +0000 | [diff] [blame] | 339 | static int icmpv6_kmemdup_sysctl_table(struct nf_proto_net *pn, |
| 340 | struct nf_icmp_net *in) |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 341 | { |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 342 | #ifdef CONFIG_SYSCTL |
| 343 | pn->ctl_table = kmemdup(icmpv6_sysctl_table, |
| 344 | sizeof(icmpv6_sysctl_table), |
| 345 | GFP_KERNEL); |
| 346 | if (!pn->ctl_table) |
| 347 | return -ENOMEM; |
Gao feng | 8fc0278 | 2012-06-21 04:36:50 +0000 | [diff] [blame] | 348 | |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 349 | pn->ctl_table[0].data = &in->timeout; |
| 350 | #endif |
| 351 | return 0; |
| 352 | } |
| 353 | |
Gao feng | 8fc0278 | 2012-06-21 04:36:50 +0000 | [diff] [blame] | 354 | static int icmpv6_init_net(struct net *net, u_int16_t proto) |
| 355 | { |
| 356 | struct nf_icmp_net *in = icmpv6_pernet(net); |
| 357 | struct nf_proto_net *pn = &in->pn; |
| 358 | |
| 359 | in->timeout = nf_ct_icmpv6_timeout; |
| 360 | |
| 361 | return icmpv6_kmemdup_sysctl_table(pn, in); |
| 362 | } |
| 363 | |
Pablo Neira Ayuso | 0891147 | 2012-06-29 05:23:24 +0000 | [diff] [blame] | 364 | static struct nf_proto_net *icmpv6_get_net_proto(struct net *net) |
| 365 | { |
| 366 | return &net->ct.nf_ct_proto.icmpv6.pn; |
| 367 | } |
| 368 | |
Patrick McHardy | 61075af | 2007-07-14 20:48:19 -0700 | [diff] [blame] | 369 | struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 __read_mostly = |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 370 | { |
| 371 | .l3proto = PF_INET6, |
Martin Josefsson | 605dcad | 2006-11-29 02:35:06 +0100 | [diff] [blame] | 372 | .l4proto = IPPROTO_ICMPV6, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 373 | .name = "icmpv6", |
| 374 | .pkt_to_tuple = icmpv6_pkt_to_tuple, |
| 375 | .invert_tuple = icmpv6_invert_tuple, |
| 376 | .print_tuple = icmpv6_print_tuple, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 377 | .packet = icmpv6_packet, |
Pablo Neira Ayuso | 2c8503f | 2012-02-28 18:23:31 +0100 | [diff] [blame] | 378 | .get_timeouts = icmpv6_get_timeouts, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 379 | .new = icmpv6_new, |
| 380 | .error = icmpv6_error, |
Amerigo Wang | 07a9362 | 2012-10-29 16:23:10 +0000 | [diff] [blame] | 381 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK) |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 382 | .tuple_to_nlattr = icmpv6_tuple_to_nlattr, |
Holger Eitzenberger | a400c30 | 2009-03-25 21:53:39 +0100 | [diff] [blame] | 383 | .nlattr_tuple_size = icmpv6_nlattr_tuple_size, |
Patrick McHardy | fdf7083 | 2007-09-28 14:37:41 -0700 | [diff] [blame] | 384 | .nlattr_to_tuple = icmpv6_nlattr_to_tuple, |
Patrick McHardy | f73e924 | 2007-09-28 14:39:55 -0700 | [diff] [blame] | 385 | .nla_policy = icmpv6_nla_policy, |
Pablo Neira Ayuso | c1d10ad | 2006-01-05 12:19:05 -0800 | [diff] [blame] | 386 | #endif |
Pablo Neira Ayuso | 5097846 | 2012-02-28 19:13:48 +0100 | [diff] [blame] | 387 | #if IS_ENABLED(CONFIG_NF_CT_NETLINK_TIMEOUT) |
| 388 | .ctnl_timeout = { |
| 389 | .nlattr_to_obj = icmpv6_timeout_nlattr_to_obj, |
| 390 | .obj_to_nlattr = icmpv6_timeout_obj_to_nlattr, |
| 391 | .nlattr_max = CTA_TIMEOUT_ICMP_MAX, |
| 392 | .obj_size = sizeof(unsigned int), |
| 393 | .nla_policy = icmpv6_timeout_nla_policy, |
| 394 | }, |
| 395 | #endif /* CONFIG_NF_CT_NETLINK_TIMEOUT */ |
Gao feng | 7080ba0 | 2012-05-28 21:04:15 +0000 | [diff] [blame] | 396 | .init_net = icmpv6_init_net, |
Pablo Neira Ayuso | 0891147 | 2012-06-29 05:23:24 +0000 | [diff] [blame] | 397 | .get_net_proto = icmpv6_get_net_proto, |
Yasuyuki Kozakai | 9fb9cbb | 2005-11-09 16:38:16 -0800 | [diff] [blame] | 398 | }; |