1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C)2003,2004 USAGI/WIDE Project
4 *
5 * Author:
6 * Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
7 */
8
9 #include <linux/types.h>
10 #include <linux/timer.h>
11 #include <linux/module.h>
12 #include <linux/netfilter.h>
13 #include <linux/in6.h>
14 #include <linux/icmpv6.h>
15 #include <linux/ipv6.h>
16 #include <net/ipv6.h>
17 #include <net/ip6_checksum.h>
18 #include <linux/seq_file.h>
19 #include <linux/netfilter_ipv6.h>
20 #include <net/netfilter/nf_conntrack_tuple.h>
21 #include <net/netfilter/nf_conntrack_l4proto.h>
22 #include <net/netfilter/nf_conntrack_core.h>
23 #include <net/netfilter/nf_conntrack_timeout.h>
24 #include <net/netfilter/nf_conntrack_zones.h>
25 #include <net/netfilter/nf_log.h>
26
27 static const unsigned int nf_ct_icmpv6_timeout = 30*HZ;
28
icmpv6_pkt_to_tuple(const struct sk_buff * skb,unsigned int dataoff,struct net * net,struct nf_conntrack_tuple * tuple)29 bool icmpv6_pkt_to_tuple(const struct sk_buff *skb,
30 unsigned int dataoff,
31 struct net *net,
32 struct nf_conntrack_tuple *tuple)
33 {
34 const struct icmp6hdr *hp;
35 struct icmp6hdr _hdr;
36
37 hp = skb_header_pointer(skb, dataoff, sizeof(_hdr), &_hdr);
38 if (hp == NULL)
39 return false;
40 tuple->dst.u.icmp.type = hp->icmp6_type;
41 tuple->src.u.icmp.id = hp->icmp6_identifier;
42 tuple->dst.u.icmp.code = hp->icmp6_code;
43
44 return true;
45 }
46
47 /* Add 1; spaces filled with 0. */
48 static const u_int8_t invmap[] = {
49 [ICMPV6_ECHO_REQUEST - 128] = ICMPV6_ECHO_REPLY + 1,
50 [ICMPV6_ECHO_REPLY - 128] = ICMPV6_ECHO_REQUEST + 1,
51 [ICMPV6_NI_QUERY - 128] = ICMPV6_NI_REPLY + 1,
52 [ICMPV6_NI_REPLY - 128] = ICMPV6_NI_QUERY + 1
53 };
54
55 static const u_int8_t noct_valid_new[] = {
56 [ICMPV6_MGM_QUERY - 130] = 1,
57 [ICMPV6_MGM_REPORT - 130] = 1,
58 [ICMPV6_MGM_REDUCTION - 130] = 1,
59 [NDISC_ROUTER_SOLICITATION - 130] = 1,
60 [NDISC_ROUTER_ADVERTISEMENT - 130] = 1,
61 [NDISC_NEIGHBOUR_SOLICITATION - 130] = 1,
62 [NDISC_NEIGHBOUR_ADVERTISEMENT - 130] = 1,
63 [ICMPV6_MLD2_REPORT - 130] = 1
64 };
65
nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple * tuple,const struct nf_conntrack_tuple * orig)66 bool nf_conntrack_invert_icmpv6_tuple(struct nf_conntrack_tuple *tuple,
67 const struct nf_conntrack_tuple *orig)
68 {
69 int type = orig->dst.u.icmp.type - 128;
70 if (type < 0 || type >= sizeof(invmap) || !invmap[type])
71 return false;
72
73 tuple->src.u.icmp.id = orig->src.u.icmp.id;
74 tuple->dst.u.icmp.type = invmap[type] - 1;
75 tuple->dst.u.icmp.code = orig->dst.u.icmp.code;
76 return true;
77 }
78
icmpv6_get_timeouts(struct net * net)79 static unsigned int *icmpv6_get_timeouts(struct net *net)
80 {
81 return &nf_icmpv6_pernet(net)->timeout;
82 }
83
84 /* Returns verdict for packet, or -1 for invalid. */
nf_conntrack_icmpv6_packet(struct nf_conn * ct,struct sk_buff * skb,enum ip_conntrack_info ctinfo,const struct nf_hook_state * state)85 int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
86 struct sk_buff *skb,
87 enum ip_conntrack_info ctinfo,
88 const struct nf_hook_state *state)
89 {
90 unsigned int *timeout = nf_ct_timeout_lookup(ct);
91 static const u8 valid_new[] = {
92 [ICMPV6_ECHO_REQUEST - 128] = 1,
93 [ICMPV6_NI_QUERY - 128] = 1
94 };
95
96 if (state->pf != NFPROTO_IPV6)
97 return -NF_ACCEPT;
98
99 if (!nf_ct_is_confirmed(ct)) {
100 int type = ct->tuplehash[0].tuple.dst.u.icmp.type - 128;
101
102 if (type < 0 || type >= sizeof(valid_new) || !valid_new[type]) {
103 /* Can't create a new ICMPv6 `conn' with this. */
104 pr_debug("icmpv6: can't create new conn with type %u\n",
105 type + 128);
106 nf_ct_dump_tuple_ipv6(&ct->tuplehash[0].tuple);
107 return -NF_ACCEPT;
108 }
109 }
110
111 if (!timeout)
112 timeout = icmpv6_get_timeouts(nf_ct_net(ct));
113
114 /* Do not immediately delete the connection after the first
115 successful reply to avoid excessive conntrackd traffic
116 and also to handle correctly ICMP echo reply duplicates. */
117 nf_ct_refresh_acct(ct, ctinfo, skb, *timeout);
118
119 return NF_ACCEPT;
120 }
121
122
icmpv6_error_log(const struct sk_buff * skb,const struct nf_hook_state * state,const char * msg)123 static void icmpv6_error_log(const struct sk_buff *skb,
124 const struct nf_hook_state *state,
125 const char *msg)
126 {
127 nf_l4proto_log_invalid(skb, state->net, state->pf,
128 IPPROTO_ICMPV6, "%s", msg);
129 }
130
131 static noinline_for_stack int
nf_conntrack_icmpv6_redirect(struct nf_conn * tmpl,struct sk_buff * skb,unsigned int dataoff,const struct nf_hook_state * state)132 nf_conntrack_icmpv6_redirect(struct nf_conn *tmpl, struct sk_buff *skb,
133 unsigned int dataoff,
134 const struct nf_hook_state *state)
135 {
136 u8 hl = ipv6_hdr(skb)->hop_limit;
137 union nf_inet_addr outer_daddr;
138 union {
139 struct nd_opt_hdr nd_opt;
140 struct rd_msg rd_msg;
141 } tmp;
142 const struct nd_opt_hdr *nd_opt;
143 const struct rd_msg *rd_msg;
144
145 rd_msg = skb_header_pointer(skb, dataoff, sizeof(*rd_msg), &tmp.rd_msg);
146 if (!rd_msg) {
147 icmpv6_error_log(skb, state, "short redirect");
148 return -NF_ACCEPT;
149 }
150
151 if (rd_msg->icmph.icmp6_code != 0)
152 return NF_ACCEPT;
153
154 if (hl != 255 || !(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) {
155 icmpv6_error_log(skb, state, "invalid saddr or hoplimit for redirect");
156 return -NF_ACCEPT;
157 }
158
159 dataoff += sizeof(*rd_msg);
160
161 /* warning: rd_msg no longer usable after this call */
162 nd_opt = skb_header_pointer(skb, dataoff, sizeof(*nd_opt), &tmp.nd_opt);
163 if (!nd_opt || nd_opt->nd_opt_len == 0) {
164 icmpv6_error_log(skb, state, "redirect without options");
165 return -NF_ACCEPT;
166 }
167
168 /* We could call ndisc_parse_options(), but it would need
169 * skb_linearize() and a bit more work.
170 */
171 if (nd_opt->nd_opt_type != ND_OPT_REDIRECT_HDR)
172 return NF_ACCEPT;
173
174 memcpy(&outer_daddr.ip6, &ipv6_hdr(skb)->daddr,
175 sizeof(outer_daddr.ip6));
176 dataoff += 8;
177 return nf_conntrack_inet_error(tmpl, skb, dataoff, state,
178 IPPROTO_ICMPV6, &outer_daddr);
179 }
180
nf_conntrack_icmpv6_error(struct nf_conn * tmpl,struct sk_buff * skb,unsigned int dataoff,const struct nf_hook_state * state)181 int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
182 struct sk_buff *skb,
183 unsigned int dataoff,
184 const struct nf_hook_state *state)
185 {
186 union nf_inet_addr outer_daddr;
187 const struct icmp6hdr *icmp6h;
188 struct icmp6hdr _ih;
189 int type;
190
191 icmp6h = skb_header_pointer(skb, dataoff, sizeof(_ih), &_ih);
192 if (icmp6h == NULL) {
193 icmpv6_error_log(skb, state, "short packet");
194 return -NF_ACCEPT;
195 }
196
197 if (state->hook == NF_INET_PRE_ROUTING &&
198 state->net->ct.sysctl_checksum &&
199 nf_ip6_checksum(skb, state->hook, dataoff, IPPROTO_ICMPV6)) {
200 icmpv6_error_log(skb, state, "ICMPv6 checksum failed");
201 return -NF_ACCEPT;
202 }
203
204 type = icmp6h->icmp6_type - 130;
205 if (type >= 0 && type < sizeof(noct_valid_new) &&
206 noct_valid_new[type]) {
207 nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
208 return NF_ACCEPT;
209 }
210
211 if (icmp6h->icmp6_type == NDISC_REDIRECT)
212 return nf_conntrack_icmpv6_redirect(tmpl, skb, dataoff, state);
213
214 /* is not error message ? */
215 if (icmp6h->icmp6_type >= 128)
216 return NF_ACCEPT;
217
218 memcpy(&outer_daddr.ip6, &ipv6_hdr(skb)->daddr,
219 sizeof(outer_daddr.ip6));
220 dataoff += sizeof(*icmp6h);
221 return nf_conntrack_inet_error(tmpl, skb, dataoff, state,
222 IPPROTO_ICMPV6, &outer_daddr);
223 }
224
225 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
226
227 #include <linux/netfilter/nfnetlink.h>
228 #include <linux/netfilter/nfnetlink_conntrack.h>
icmpv6_tuple_to_nlattr(struct sk_buff * skb,const struct nf_conntrack_tuple * t)229 static int icmpv6_tuple_to_nlattr(struct sk_buff *skb,
230 const struct nf_conntrack_tuple *t)
231 {
232 if (nla_put_be16(skb, CTA_PROTO_ICMPV6_ID, t->src.u.icmp.id) ||
233 nla_put_u8(skb, CTA_PROTO_ICMPV6_TYPE, t->dst.u.icmp.type) ||
234 nla_put_u8(skb, CTA_PROTO_ICMPV6_CODE, t->dst.u.icmp.code))
235 goto nla_put_failure;
236 return 0;
237
238 nla_put_failure:
239 return -1;
240 }
241
242 static const struct nla_policy icmpv6_nla_policy[CTA_PROTO_MAX+1] = {
243 [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 },
244 [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 },
245 [CTA_PROTO_ICMPV6_ID] = { .type = NLA_U16 },
246 };
247
icmpv6_nlattr_to_tuple(struct nlattr * tb[],struct nf_conntrack_tuple * tuple)248 static int icmpv6_nlattr_to_tuple(struct nlattr *tb[],
249 struct nf_conntrack_tuple *tuple)
250 {
251 if (!tb[CTA_PROTO_ICMPV6_TYPE] ||
252 !tb[CTA_PROTO_ICMPV6_CODE] ||
253 !tb[CTA_PROTO_ICMPV6_ID])
254 return -EINVAL;
255
256 tuple->dst.u.icmp.type = nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]);
257 tuple->dst.u.icmp.code = nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]);
258 tuple->src.u.icmp.id = nla_get_be16(tb[CTA_PROTO_ICMPV6_ID]);
259
260 if (tuple->dst.u.icmp.type < 128 ||
261 tuple->dst.u.icmp.type - 128 >= sizeof(invmap) ||
262 !invmap[tuple->dst.u.icmp.type - 128])
263 return -EINVAL;
264
265 return 0;
266 }
267
icmpv6_nlattr_tuple_size(void)268 static unsigned int icmpv6_nlattr_tuple_size(void)
269 {
270 static unsigned int size __read_mostly;
271
272 if (!size)
273 size = nla_policy_len(icmpv6_nla_policy, CTA_PROTO_MAX + 1);
274
275 return size;
276 }
277 #endif
278
279 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
280
281 #include <linux/netfilter/nfnetlink.h>
282 #include <linux/netfilter/nfnetlink_cttimeout.h>
283
icmpv6_timeout_nlattr_to_obj(struct nlattr * tb[],struct net * net,void * data)284 static int icmpv6_timeout_nlattr_to_obj(struct nlattr *tb[],
285 struct net *net, void *data)
286 {
287 unsigned int *timeout = data;
288 struct nf_icmp_net *in = nf_icmpv6_pernet(net);
289
290 if (!timeout)
291 timeout = icmpv6_get_timeouts(net);
292 if (tb[CTA_TIMEOUT_ICMPV6_TIMEOUT]) {
293 *timeout =
294 ntohl(nla_get_be32(tb[CTA_TIMEOUT_ICMPV6_TIMEOUT])) * HZ;
295 } else {
296 /* Set default ICMPv6 timeout. */
297 *timeout = in->timeout;
298 }
299 return 0;
300 }
301
302 static int
icmpv6_timeout_obj_to_nlattr(struct sk_buff * skb,const void * data)303 icmpv6_timeout_obj_to_nlattr(struct sk_buff *skb, const void *data)
304 {
305 const unsigned int *timeout = data;
306
307 if (nla_put_be32(skb, CTA_TIMEOUT_ICMPV6_TIMEOUT, htonl(*timeout / HZ)))
308 goto nla_put_failure;
309 return 0;
310
311 nla_put_failure:
312 return -ENOSPC;
313 }
314
315 static const struct nla_policy
316 icmpv6_timeout_nla_policy[CTA_TIMEOUT_ICMPV6_MAX+1] = {
317 [CTA_TIMEOUT_ICMPV6_TIMEOUT] = { .type = NLA_U32 },
318 };
319 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
320
nf_conntrack_icmpv6_init_net(struct net * net)321 void nf_conntrack_icmpv6_init_net(struct net *net)
322 {
323 struct nf_icmp_net *in = nf_icmpv6_pernet(net);
324
325 in->timeout = nf_ct_icmpv6_timeout;
326 }
327
328 const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
329 {
330 .l4proto = IPPROTO_ICMPV6,
331 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
332 .tuple_to_nlattr = icmpv6_tuple_to_nlattr,
333 .nlattr_tuple_size = icmpv6_nlattr_tuple_size,
334 .nlattr_to_tuple = icmpv6_nlattr_to_tuple,
335 .nla_policy = icmpv6_nla_policy,
336 #endif
337 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
338 .ctnl_timeout = {
339 .nlattr_to_obj = icmpv6_timeout_nlattr_to_obj,
340 .obj_to_nlattr = icmpv6_timeout_obj_to_nlattr,
341 .nlattr_max = CTA_TIMEOUT_ICMP_MAX,
342 .obj_size = sizeof(unsigned int),
343 .nla_policy = icmpv6_timeout_nla_policy,
344 },
345 #endif /* CONFIG_NF_CONNTRACK_TIMEOUT */
346 };
347