]>
Commit | Line | Data |
---|---|---|
b0d623f7 | 1 | /* |
5c9f4661 | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
b0d623f7 A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
39037602 | 5 | * |
b0d623f7 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
39037602 | 14 | * |
b0d623f7 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
39037602 | 17 | * |
b0d623f7 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
39037602 | 25 | * |
b0d623f7 A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
9bccf70c A |
29 | /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */ |
30 | /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $ */ | |
1c79356b A |
31 | |
32 | /* | |
33 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | /* | |
62 | * Copyright (c) 1982, 1986, 1988, 1993 | |
63 | * The Regents of the University of California. All rights reserved. | |
64 | * | |
65 | * Redistribution and use in source and binary forms, with or without | |
66 | * modification, are permitted provided that the following conditions | |
67 | * are met: | |
68 | * 1. Redistributions of source code must retain the above copyright | |
69 | * notice, this list of conditions and the following disclaimer. | |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in the | |
72 | * documentation and/or other materials provided with the distribution. | |
73 | * 3. All advertising materials mentioning features or use of this software | |
74 | * must display the following acknowledgement: | |
75 | * This product includes software developed by the University of | |
76 | * California, Berkeley and its contributors. | |
77 | * 4. Neither the name of the University nor the names of its contributors | |
78 | * may be used to endorse or promote products derived from this software | |
79 | * without specific prior written permission. | |
80 | * | |
81 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
82 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
83 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
84 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
85 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
86 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
87 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
88 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
89 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
90 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
91 | * SUCH DAMAGE. | |
92 | * | |
93 | * @(#)ip_icmp.c 8.2 (Berkeley) 1/4/94 | |
94 | */ | |
95 | ||
1c79356b A |
96 | |
97 | #include <sys/param.h> | |
98 | #include <sys/systm.h> | |
91447636 | 99 | #include <sys/lock.h> |
1c79356b | 100 | #include <sys/malloc.h> |
316670eb | 101 | #include <sys/mcache.h> |
1c79356b A |
102 | #include <sys/mbuf.h> |
103 | #include <sys/protosw.h> | |
104 | #include <sys/socket.h> | |
105 | #include <sys/socketvar.h> | |
106 | #include <sys/time.h> | |
107 | #include <sys/kernel.h> | |
108 | #include <sys/syslog.h> | |
109 | #include <sys/domain.h> | |
316670eb | 110 | #include <sys/kauth.h> |
1c79356b A |
111 | |
112 | #include <net/if.h> | |
113 | #include <net/route.h> | |
114 | #include <net/if_dl.h> | |
115 | #include <net/if_types.h> | |
116 | ||
117 | #include <netinet/in.h> | |
118 | #include <netinet/in_var.h> | |
1c79356b A |
119 | #include <netinet/ip6.h> |
120 | #include <netinet6/ip6_var.h> | |
121 | #include <netinet/icmp6.h> | |
122 | #include <netinet6/mld6_var.h> | |
1c79356b | 123 | #include <netinet/in_pcb.h> |
9bccf70c | 124 | #include <netinet6/in6_pcb.h> |
39236c6e | 125 | #include <netinet6/in6_var.h> |
1c79356b A |
126 | #include <netinet6/nd6.h> |
127 | #include <netinet6/in6_ifattach.h> | |
128 | #include <netinet6/ip6protosw.h> | |
6d2010ae | 129 | #include <netinet6/scope6_var.h> |
1c79356b | 130 | |
1c79356b A |
131 | #if IPSEC |
132 | #include <netinet6/ipsec.h> | |
133 | #include <netkey/key.h> | |
1c79356b A |
134 | #endif |
135 | ||
1c79356b A |
136 | #include <net/net_osdep.h> |
137 | ||
fe8ab488 A |
138 | #if NECP |
139 | #include <net/necp.h> | |
140 | #endif | |
141 | ||
1c79356b A |
142 | extern struct ip6protosw *ip6_protox[]; |
143 | ||
b0d623f7 A |
144 | extern uint32_t rip_sendspace; |
145 | extern uint32_t rip_recvspace; | |
2d21ac55 | 146 | |
1c79356b A |
147 | struct icmp6stat icmp6stat; |
148 | ||
1c79356b | 149 | extern struct inpcbhead ripcb; |
9bccf70c | 150 | extern int icmp6errppslim; |
fe8ab488 | 151 | extern int icmp6rappslim; |
9bccf70c | 152 | static int icmp6errpps_count = 0; |
fe8ab488 | 153 | static int icmp6rapps_count = 0; |
9bccf70c | 154 | static struct timeval icmp6errppslim_last; |
fe8ab488 | 155 | static struct timeval icmp6rappslim_last; |
1c79356b | 156 | extern int icmp6_nodeinfo; |
91447636 | 157 | extern struct inpcbinfo ripcbinfo; |
91447636 A |
158 | |
159 | static void icmp6_errcount(struct icmp6errstat *, int, int); | |
160 | static int icmp6_rip6_input(struct mbuf **, int); | |
161 | static int icmp6_ratelimit(const struct in6_addr *, const int, const int); | |
162 | static const char *icmp6_redirect_diag(struct in6_addr *, | |
163 | struct in6_addr *, struct in6_addr *); | |
91447636 A |
164 | static struct mbuf *ni6_input(struct mbuf *, int); |
165 | static struct mbuf *ni6_nametodns(const char *, int, int); | |
166 | static int ni6_dnsmatch(const char *, int, const char *, int); | |
743345f9 | 167 | static int ni6_addrs(struct icmp6_nodeinfo *, |
91447636 A |
168 | struct ifnet **, char *); |
169 | static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, | |
170 | struct ifnet *, int); | |
171 | static int icmp6_notify_error(struct mbuf *, int, int, int); | |
1c79356b | 172 | |
1c79356b | 173 | |
1c79356b A |
174 | |
175 | void | |
39236c6e | 176 | icmp6_init(struct ip6protosw *pp, struct domain *dp) |
1c79356b | 177 | { |
39236c6e A |
178 | #pragma unused(dp) |
179 | static int icmp6_initialized = 0; | |
180 | ||
181 | /* Also called from ip6_init() without pp */ | |
182 | VERIFY(pp == NULL || | |
183 | (pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED); | |
184 | ||
185 | /* This gets called by more than one protocols, so initialize once */ | |
186 | if (!icmp6_initialized) { | |
187 | icmp6_initialized = 1; | |
188 | mld_init(); | |
189 | } | |
9bccf70c A |
190 | } |
191 | ||
192 | static void | |
39037602 | 193 | icmp6_errcount(struct icmp6errstat *stat, int type, int code) |
9bccf70c A |
194 | { |
195 | switch (type) { | |
196 | case ICMP6_DST_UNREACH: | |
197 | switch (code) { | |
198 | case ICMP6_DST_UNREACH_NOROUTE: | |
199 | stat->icp6errs_dst_unreach_noroute++; | |
200 | return; | |
201 | case ICMP6_DST_UNREACH_ADMIN: | |
202 | stat->icp6errs_dst_unreach_admin++; | |
203 | return; | |
204 | case ICMP6_DST_UNREACH_BEYONDSCOPE: | |
205 | stat->icp6errs_dst_unreach_beyondscope++; | |
206 | return; | |
207 | case ICMP6_DST_UNREACH_ADDR: | |
208 | stat->icp6errs_dst_unreach_addr++; | |
209 | return; | |
210 | case ICMP6_DST_UNREACH_NOPORT: | |
211 | stat->icp6errs_dst_unreach_noport++; | |
212 | return; | |
213 | } | |
214 | break; | |
215 | case ICMP6_PACKET_TOO_BIG: | |
216 | stat->icp6errs_packet_too_big++; | |
217 | return; | |
218 | case ICMP6_TIME_EXCEEDED: | |
219 | switch (code) { | |
220 | case ICMP6_TIME_EXCEED_TRANSIT: | |
221 | stat->icp6errs_time_exceed_transit++; | |
222 | return; | |
223 | case ICMP6_TIME_EXCEED_REASSEMBLY: | |
224 | stat->icp6errs_time_exceed_reassembly++; | |
225 | return; | |
226 | } | |
227 | break; | |
228 | case ICMP6_PARAM_PROB: | |
229 | switch (code) { | |
230 | case ICMP6_PARAMPROB_HEADER: | |
231 | stat->icp6errs_paramprob_header++; | |
232 | return; | |
233 | case ICMP6_PARAMPROB_NEXTHEADER: | |
234 | stat->icp6errs_paramprob_nextheader++; | |
235 | return; | |
236 | case ICMP6_PARAMPROB_OPTION: | |
237 | stat->icp6errs_paramprob_option++; | |
238 | return; | |
239 | } | |
240 | break; | |
241 | case ND_REDIRECT: | |
242 | stat->icp6errs_redirect++; | |
243 | return; | |
244 | } | |
245 | stat->icp6errs_unknown++; | |
1c79356b A |
246 | } |
247 | ||
6d2010ae A |
248 | /* |
249 | * A wrapper function for icmp6_error() necessary when the erroneous packet | |
250 | * may not contain enough scope zone information. | |
251 | */ | |
252 | void | |
253 | icmp6_error2(struct mbuf *m, int type, int code, int param, | |
254 | struct ifnet *ifp) | |
255 | { | |
256 | struct ip6_hdr *ip6; | |
257 | ||
258 | if (ifp == NULL) | |
259 | return; | |
260 | ||
261 | #ifndef PULLDOWN_TEST | |
262 | IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr),return ); | |
263 | #else | |
264 | if (m->m_len < sizeof(struct ip6_hdr)) { | |
265 | m = m_pullup(m, sizeof(struct ip6_hdr)); | |
266 | if (m == NULL) | |
267 | return; | |
268 | } | |
269 | #endif | |
270 | ||
271 | ip6 = mtod(m, struct ip6_hdr *); | |
272 | ||
273 | if (in6_setscope(&ip6->ip6_src, ifp, NULL) != 0) | |
274 | return; | |
275 | if (in6_setscope(&ip6->ip6_dst, ifp, NULL) != 0) | |
276 | return; | |
277 | ||
278 | icmp6_error(m, type, code, param); | |
279 | } | |
280 | ||
1c79356b A |
281 | /* |
282 | * Generate an error packet of type error in response to bad IP6 packet. | |
283 | */ | |
284 | void | |
3e170ce0 A |
285 | icmp6_error(struct mbuf *m, int type, int code, int param) { |
286 | icmp6_error_flag(m, type, code, param, ICMP6_ERROR_RST_MRCVIF); | |
287 | } | |
288 | ||
289 | void icmp6_error_flag (struct mbuf *m, int type, int code, int param, int flags) | |
1c79356b A |
290 | { |
291 | struct ip6_hdr *oip6, *nip6; | |
292 | struct icmp6_hdr *icmp6; | |
293 | u_int preplen; | |
294 | int off; | |
295 | int nxt; | |
296 | ||
297 | icmp6stat.icp6s_error++; | |
298 | ||
9bccf70c A |
299 | /* count per-type-code statistics */ |
300 | icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); | |
301 | ||
1c79356b A |
302 | #ifdef M_DECRYPTED /*not openbsd*/ |
303 | if (m->m_flags & M_DECRYPTED) { | |
304 | icmp6stat.icp6s_canterror++; | |
305 | goto freeit; | |
306 | } | |
307 | #endif | |
308 | ||
309 | #ifndef PULLDOWN_TEST | |
91447636 | 310 | IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), return); |
1c79356b A |
311 | #else |
312 | if (m->m_len < sizeof(struct ip6_hdr)) { | |
313 | m = m_pullup(m, sizeof(struct ip6_hdr)); | |
314 | if (m == NULL) | |
315 | return; | |
316 | } | |
317 | #endif | |
318 | oip6 = mtod(m, struct ip6_hdr *); | |
319 | ||
320 | /* | |
6d2010ae A |
321 | * If the destination address of the erroneous packet is a multicast |
322 | * address, or the packet was sent using link-layer multicast, | |
323 | * we should basically suppress sending an error (RFC 2463, Section | |
324 | * 2.4). | |
325 | * We have two exceptions (the item e.2 in that section): | |
326 | * - the Pakcet Too Big message can be sent for path MTU discovery. | |
327 | * - the Parameter Problem Message that can be allowed an icmp6 error | |
328 | * in the option type field. This check has been done in | |
329 | * ip6_unknown_opt(), so we can just check the type and code. | |
1c79356b A |
330 | */ |
331 | if ((m->m_flags & (M_BCAST|M_MCAST) || | |
332 | IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && | |
333 | (type != ICMP6_PACKET_TOO_BIG && | |
334 | (type != ICMP6_PARAM_PROB || | |
335 | code != ICMP6_PARAMPROB_OPTION))) | |
336 | goto freeit; | |
337 | ||
6d2010ae A |
338 | /* |
339 | * RFC 2463, 2.4 (e.5): source address check. | |
340 | * XXX: the case of anycast source? | |
341 | */ | |
1c79356b A |
342 | if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || |
343 | IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) | |
344 | goto freeit; | |
345 | ||
346 | /* | |
347 | * If we are about to send ICMPv6 against ICMPv6 error/redirect, | |
348 | * don't do it. | |
349 | */ | |
350 | nxt = -1; | |
351 | off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); | |
352 | if (off >= 0 && nxt == IPPROTO_ICMPV6) { | |
353 | struct icmp6_hdr *icp; | |
354 | ||
355 | #ifndef PULLDOWN_TEST | |
91447636 | 356 | IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), return); |
1c79356b A |
357 | icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); |
358 | #else | |
359 | IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, | |
360 | sizeof(*icp)); | |
361 | if (icp == NULL) { | |
362 | icmp6stat.icp6s_tooshort++; | |
363 | return; | |
364 | } | |
365 | #endif | |
366 | if (icp->icmp6_type < ICMP6_ECHO_REQUEST || | |
367 | icp->icmp6_type == ND_REDIRECT) { | |
368 | /* | |
369 | * ICMPv6 error | |
370 | * Special case: for redirect (which is | |
371 | * informational) we must not send icmp6 error. | |
372 | */ | |
373 | icmp6stat.icp6s_canterror++; | |
374 | goto freeit; | |
375 | } else { | |
376 | /* ICMPv6 informational - send the error */ | |
377 | } | |
378 | } else { | |
379 | /* non-ICMPv6 - send the error */ | |
380 | } | |
381 | ||
382 | oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ | |
383 | ||
384 | /* Finally, do rate limitation check. */ | |
385 | if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { | |
386 | icmp6stat.icp6s_toofreq++; | |
387 | goto freeit; | |
388 | } | |
389 | ||
390 | /* | |
391 | * OK, ICMP6 can be generated. | |
392 | */ | |
393 | ||
394 | if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) | |
395 | m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); | |
396 | ||
397 | preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
3e170ce0 | 398 | M_PREPEND(m, preplen, M_DONTWAIT, 1); |
1c79356b A |
399 | if (m && m->m_len < preplen) |
400 | m = m_pullup(m, preplen); | |
401 | if (m == NULL) { | |
9bccf70c | 402 | nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); |
1c79356b A |
403 | return; |
404 | } | |
405 | ||
406 | nip6 = mtod(m, struct ip6_hdr *); | |
407 | nip6->ip6_src = oip6->ip6_src; | |
408 | nip6->ip6_dst = oip6->ip6_dst; | |
409 | ||
6d2010ae A |
410 | in6_clearscope(&oip6->ip6_src); |
411 | in6_clearscope(&oip6->ip6_dst); | |
1c79356b A |
412 | |
413 | icmp6 = (struct icmp6_hdr *)(nip6 + 1); | |
414 | icmp6->icmp6_type = type; | |
415 | icmp6->icmp6_code = code; | |
416 | icmp6->icmp6_pptr = htonl((u_int32_t)param); | |
417 | ||
9bccf70c A |
418 | /* |
419 | * icmp6_reflect() is designed to be in the input path. | |
6d2010ae | 420 | * icmp6_error() can be called from both input and output path, |
9bccf70c A |
421 | * and if we are in output path rcvif could contain bogus value. |
422 | * clear m->m_pkthdr.rcvif for safety, we should have enough scope | |
423 | * information in ip header (nip6). | |
424 | */ | |
3e170ce0 A |
425 | if (flags & ICMP6_ERROR_RST_MRCVIF) { |
426 | m->m_pkthdr.rcvif = NULL; | |
427 | } | |
9bccf70c | 428 | |
1c79356b | 429 | icmp6stat.icp6s_outhist[type]++; |
55e303ae | 430 | icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ |
1c79356b A |
431 | |
432 | return; | |
433 | ||
434 | freeit: | |
435 | /* | |
6d2010ae | 436 | * If we can't tell whether or not we can generate ICMP6, free it. |
1c79356b A |
437 | */ |
438 | m_freem(m); | |
439 | } | |
440 | ||
441 | /* | |
442 | * Process a received ICMP6 message. | |
443 | */ | |
444 | int | |
6d2010ae | 445 | icmp6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 446 | { |
6d2010ae | 447 | #pragma unused(proto) |
1c79356b | 448 | struct mbuf *m = *mp, *n; |
6d2010ae | 449 | struct ifnet *ifp; |
1c79356b A |
450 | struct ip6_hdr *ip6, *nip6; |
451 | struct icmp6_hdr *icmp6, *nicmp6; | |
452 | int off = *offp; | |
453 | int icmp6len = m->m_pkthdr.len - *offp; | |
316670eb | 454 | int code, sum, noff, proxy = 0; |
1c79356b | 455 | |
6d2010ae A |
456 | ifp = m->m_pkthdr.rcvif; |
457 | ||
1c79356b | 458 | #ifndef PULLDOWN_TEST |
91447636 | 459 | IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), return IPPROTO_DONE); |
55e303ae | 460 | /* m might change if M_LOOP. So, call mtod after this */ |
1c79356b A |
461 | #endif |
462 | ||
316670eb A |
463 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
464 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
465 | ||
1c79356b A |
466 | /* |
467 | * Locate icmp6 structure in mbuf, and check | |
468 | * that not corrupted and of at least minimum length | |
469 | */ | |
1c79356b A |
470 | ip6 = mtod(m, struct ip6_hdr *); |
471 | if (icmp6len < sizeof(struct icmp6_hdr)) { | |
472 | icmp6stat.icp6s_tooshort++; | |
473 | goto freeit; | |
474 | } | |
475 | ||
39236c6e A |
476 | #ifndef PULLDOWN_TEST |
477 | icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); | |
478 | #else | |
479 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); | |
480 | if (icmp6 == NULL) { | |
481 | icmp6stat.icp6s_tooshort++; | |
482 | return IPPROTO_DONE; | |
483 | } | |
484 | #endif | |
485 | code = icmp6->icmp6_code; | |
486 | ||
39037602 A |
487 | /* |
488 | * Early check for RFC 6980 | |
489 | * Drop certain NDP packets if they came in fragmented | |
490 | */ | |
491 | switch (icmp6->icmp6_type) { | |
492 | case ND_ROUTER_SOLICIT: | |
493 | case ND_ROUTER_ADVERT: | |
494 | case ND_NEIGHBOR_SOLICIT: | |
495 | case ND_NEIGHBOR_ADVERT: | |
496 | case ND_REDIRECT: | |
497 | if (m->m_pkthdr.pkt_flags & PKTF_REASSEMBLED) { | |
498 | icmp6stat.icp6s_rfc6980_drop++; | |
499 | goto freeit; | |
500 | } | |
501 | break; | |
502 | default: | |
503 | break; | |
504 | } | |
505 | ||
39236c6e A |
506 | /* Apply rate limit before checksum validation. */ |
507 | if (icmp6_ratelimit(&ip6->ip6_dst, icmp6->icmp6_type, code)) { | |
508 | icmp6stat.icp6s_toofreq++; | |
509 | goto freeit; | |
510 | } | |
511 | ||
6d2010ae A |
512 | /* |
513 | * Check multicast group membership. | |
514 | * Note: SSM filters are not applied for ICMPv6 traffic. | |
515 | */ | |
516 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
517 | struct in6_multi *inm; | |
518 | ||
519 | in6_multihead_lock_shared(); | |
520 | IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, inm); | |
521 | in6_multihead_lock_done(); | |
522 | ||
523 | if (inm == NULL) { | |
316670eb A |
524 | /* |
525 | * Don't discard if this is a Neighbor Solicitation | |
526 | * that needs to be proxied (see check down below.) | |
527 | */ | |
39236c6e | 528 | if (!(m->m_pkthdr.pkt_flags & PKTF_PROXY_DST)) { |
316670eb A |
529 | ip6stat.ip6s_notmember++; |
530 | in6_ifstat_inc(m->m_pkthdr.rcvif, | |
531 | ifs6_in_discard); | |
532 | goto freeit; | |
533 | } | |
6d2010ae A |
534 | } else { |
535 | IN6M_REMREF(inm); | |
536 | } | |
537 | } | |
538 | ||
1c79356b A |
539 | /* |
540 | * calculate the checksum | |
541 | */ | |
1c79356b | 542 | if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { |
9bccf70c | 543 | nd6log((LOG_ERR, |
1c79356b | 544 | "ICMP6 checksum error(%d|%x) %s\n", |
9bccf70c | 545 | icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); |
1c79356b A |
546 | icmp6stat.icp6s_checksum++; |
547 | goto freeit; | |
548 | } | |
549 | ||
39236c6e | 550 | if (m->m_pkthdr.pkt_flags & PKTF_PROXY_DST) { |
1c79356b | 551 | /* |
316670eb A |
552 | * This is the special case of proxying NS (dst is either |
553 | * solicited-node multicast or unicast); process it locally | |
554 | * but don't deliver it to sockets. It practically lets us | |
555 | * steer the packet to nd6_prproxy_ns_input, where more | |
556 | * specific tests and actions will be taken. | |
1c79356b A |
557 | */ |
558 | switch (icmp6->icmp6_type) { | |
316670eb A |
559 | case ND_NEIGHBOR_SOLICIT: |
560 | proxy = 1; | |
1c79356b A |
561 | break; |
562 | default: | |
563 | goto freeit; | |
564 | } | |
565 | } | |
1c79356b | 566 | |
1c79356b A |
567 | icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; |
568 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); | |
569 | if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) | |
570 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); | |
571 | ||
1c79356b | 572 | switch (icmp6->icmp6_type) { |
1c79356b A |
573 | case ICMP6_DST_UNREACH: |
574 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); | |
575 | switch (code) { | |
576 | case ICMP6_DST_UNREACH_NOROUTE: | |
5ba3f43e | 577 | case ICMP6_DST_UNREACH_ADDR: /* PRC_HOSTDEAD is a DOS */ |
1c79356b A |
578 | code = PRC_UNREACH_NET; |
579 | break; | |
580 | case ICMP6_DST_UNREACH_ADMIN: | |
581 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); | |
582 | code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ | |
583 | break; | |
1c79356b A |
584 | case ICMP6_DST_UNREACH_BEYONDSCOPE: |
585 | /* I mean "source address was incorrect." */ | |
586 | code = PRC_PARAMPROB; | |
587 | break; | |
1c79356b A |
588 | case ICMP6_DST_UNREACH_NOPORT: |
589 | code = PRC_UNREACH_PORT; | |
590 | break; | |
591 | default: | |
592 | goto badcode; | |
593 | } | |
594 | goto deliver; | |
1c79356b A |
595 | |
596 | case ICMP6_PACKET_TOO_BIG: | |
597 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); | |
1c79356b A |
598 | |
599 | code = PRC_MSGSIZE; | |
600 | ||
601 | /* | |
602 | * Updating the path MTU will be done after examining | |
603 | * intermediate extension headers. | |
604 | */ | |
605 | goto deliver; | |
1c79356b A |
606 | |
607 | case ICMP6_TIME_EXCEEDED: | |
608 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); | |
609 | switch (code) { | |
610 | case ICMP6_TIME_EXCEED_TRANSIT: | |
6d2010ae A |
611 | code = PRC_TIMXCEED_INTRANS; |
612 | break; | |
1c79356b | 613 | case ICMP6_TIME_EXCEED_REASSEMBLY: |
6d2010ae | 614 | code = PRC_TIMXCEED_REASS; |
1c79356b A |
615 | break; |
616 | default: | |
617 | goto badcode; | |
618 | } | |
619 | goto deliver; | |
1c79356b A |
620 | |
621 | case ICMP6_PARAM_PROB: | |
622 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); | |
623 | switch (code) { | |
624 | case ICMP6_PARAMPROB_NEXTHEADER: | |
625 | code = PRC_UNREACH_PROTOCOL; | |
626 | break; | |
627 | case ICMP6_PARAMPROB_HEADER: | |
628 | case ICMP6_PARAMPROB_OPTION: | |
629 | code = PRC_PARAMPROB; | |
630 | break; | |
631 | default: | |
632 | goto badcode; | |
633 | } | |
634 | goto deliver; | |
1c79356b A |
635 | |
636 | case ICMP6_ECHO_REQUEST: | |
637 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); | |
638 | if (code != 0) | |
639 | goto badcode; | |
e2fac8b1 | 640 | |
1c79356b A |
641 | if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { |
642 | /* Give up remote */ | |
e2fac8b1 | 643 | goto rate_limit_checked; |
1c79356b A |
644 | } |
645 | if ((n->m_flags & M_EXT) != 0 | |
646 | || n->m_len < off + sizeof(struct icmp6_hdr)) { | |
647 | struct mbuf *n0 = n; | |
648 | const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); | |
649 | ||
650 | /* | |
651 | * Prepare an internal mbuf. m_pullup() doesn't | |
652 | * always copy the length we specified. | |
653 | */ | |
654 | if (maxlen >= MCLBYTES) { | |
1c79356b A |
655 | /* Give up remote */ |
656 | m_freem(n0); | |
e2fac8b1 | 657 | goto rate_limit_checked; |
1c79356b | 658 | } |
2d21ac55 | 659 | MGETHDR(n, M_DONTWAIT, n0->m_type); /* MAC-OK */ |
1c79356b A |
660 | if (n && maxlen >= MHLEN) { |
661 | MCLGET(n, M_DONTWAIT); | |
662 | if ((n->m_flags & M_EXT) == 0) { | |
663 | m_free(n); | |
664 | n = NULL; | |
665 | } | |
666 | } | |
667 | if (n == NULL) { | |
668 | /* Give up remote */ | |
669 | m_freem(n0); | |
e2fac8b1 | 670 | goto rate_limit_checked; |
1c79356b A |
671 | } |
672 | M_COPY_PKTHDR(n, n0); | |
673 | /* | |
674 | * Copy IPv6 and ICMPv6 only. | |
675 | */ | |
676 | nip6 = mtod(n, struct ip6_hdr *); | |
677 | bcopy(ip6, nip6, sizeof(struct ip6_hdr)); | |
678 | nicmp6 = (struct icmp6_hdr *)(nip6 + 1); | |
679 | bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); | |
680 | noff = sizeof(struct ip6_hdr); | |
681 | n->m_pkthdr.len = n->m_len = | |
682 | noff + sizeof(struct icmp6_hdr); | |
683 | /* | |
684 | * Adjust mbuf. ip6_plen will be adjusted in | |
685 | * ip6_output(). | |
686 | */ | |
687 | m_adj(n0, off + sizeof(struct icmp6_hdr)); | |
688 | n->m_pkthdr.len += n0->m_pkthdr.len; | |
689 | n->m_next = n0; | |
690 | n0->m_flags &= ~M_PKTHDR; | |
691 | } else { | |
692 | nip6 = mtod(n, struct ip6_hdr *); | |
2d21ac55 A |
693 | IP6_EXTHDR_GET(nicmp6, struct icmp6_hdr *, n, off, |
694 | sizeof(*nicmp6)); | |
1c79356b A |
695 | noff = off; |
696 | } | |
316670eb A |
697 | if(nicmp6 == NULL) |
698 | panic("nicmp6 is NULL in %s, which isn't good!\n", __FUNCTION__); | |
699 | else { | |
700 | nicmp6->icmp6_type = ICMP6_ECHO_REPLY; | |
701 | nicmp6->icmp6_code = 0; | |
702 | } | |
1c79356b A |
703 | if (n) { |
704 | icmp6stat.icp6s_reflect++; | |
705 | icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; | |
706 | icmp6_reflect(n, noff); | |
707 | } | |
e2fac8b1 | 708 | goto rate_limit_checked; |
1c79356b A |
709 | |
710 | case ICMP6_ECHO_REPLY: | |
711 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); | |
712 | if (code != 0) | |
713 | goto badcode; | |
714 | break; | |
715 | ||
6d2010ae A |
716 | case MLD_LISTENER_QUERY: |
717 | case MLD_LISTENER_REPORT: | |
718 | ||
719 | if (icmp6len < sizeof(struct mld_hdr)) | |
1c79356b | 720 | goto badlen; |
6d2010ae | 721 | if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */ |
1c79356b A |
722 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); |
723 | else | |
724 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); | |
e2fac8b1 | 725 | |
1c79356b A |
726 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { |
727 | /* give up local */ | |
6d2010ae A |
728 | if (mld_input(m, off, icmp6len) == IPPROTO_DONE) |
729 | m = NULL; | |
1c79356b A |
730 | goto freeit; |
731 | } | |
6d2010ae A |
732 | if (mld_input(n, off, icmp6len) != IPPROTO_DONE) |
733 | m_freem(n); | |
1c79356b | 734 | /* m stays. */ |
e2fac8b1 | 735 | goto rate_limit_checked; |
1c79356b | 736 | |
6d2010ae | 737 | case MLD_LISTENER_DONE: |
1c79356b | 738 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); |
6d2010ae | 739 | if (icmp6len < sizeof(struct mld_hdr)) /* necessary? */ |
1c79356b A |
740 | goto badlen; |
741 | break; /* nothing to be done in kernel */ | |
742 | ||
6d2010ae A |
743 | case MLD_MTRACE_RESP: |
744 | case MLD_MTRACE: | |
745 | /* XXX: these two are experimental. not officially defined. */ | |
1c79356b A |
746 | /* XXX: per-interface statistics? */ |
747 | break; /* just pass it to applications */ | |
748 | ||
2d21ac55 | 749 | case ICMP6_NI_QUERY: |
1c79356b A |
750 | if (!icmp6_nodeinfo) |
751 | break; | |
6d2010ae | 752 | //### LD 10/20 Check fbsd differences here. Not sure we're more advanced or not. |
743345f9 | 753 | /* By RFC 4620 refuse to answer queries from global scope addresses */ |
6601e61a A |
754 | if ((icmp6_nodeinfo & 8) != 8 && in6_addrscope(&ip6->ip6_src) == IPV6_ADDR_SCOPE_GLOBAL) |
755 | break; | |
756 | ||
757 | if (icmp6len < sizeof(struct icmp6_nodeinfo)) | |
1c79356b A |
758 | goto badlen; |
759 | ||
1c79356b | 760 | #ifndef PULLDOWN_TEST |
6601e61a A |
761 | IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), |
762 | return IPPROTO_DONE); | |
1c79356b | 763 | #endif |
e2fac8b1 | 764 | |
6601e61a A |
765 | n = m_copy(m, 0, M_COPYALL); |
766 | if (n) | |
767 | n = ni6_input(n, off); | |
1c79356b | 768 | if (n) { |
6601e61a | 769 | noff = sizeof(struct ip6_hdr); |
1c79356b A |
770 | icmp6stat.icp6s_reflect++; |
771 | icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; | |
772 | icmp6_reflect(n, noff); | |
773 | } | |
e2fac8b1 | 774 | goto rate_limit_checked; |
1c79356b A |
775 | |
776 | case ICMP6_WRUREPLY: | |
777 | if (code != 0) | |
778 | goto badcode; | |
779 | break; | |
780 | ||
781 | case ND_ROUTER_SOLICIT: | |
782 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); | |
783 | if (code != 0) | |
784 | goto badcode; | |
785 | if (icmp6len < sizeof(struct nd_router_solicit)) | |
786 | goto badlen; | |
e2fac8b1 | 787 | |
1c79356b A |
788 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { |
789 | /* give up local */ | |
790 | nd6_rs_input(m, off, icmp6len); | |
791 | m = NULL; | |
792 | goto freeit; | |
793 | } | |
794 | nd6_rs_input(n, off, icmp6len); | |
795 | /* m stays. */ | |
e2fac8b1 | 796 | goto rate_limit_checked; |
1c79356b A |
797 | |
798 | case ND_ROUTER_ADVERT: | |
799 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); | |
800 | if (code != 0) | |
801 | goto badcode; | |
802 | if (icmp6len < sizeof(struct nd_router_advert)) | |
803 | goto badlen; | |
e2fac8b1 | 804 | |
1c79356b A |
805 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { |
806 | /* give up local */ | |
807 | nd6_ra_input(m, off, icmp6len); | |
808 | m = NULL; | |
809 | goto freeit; | |
810 | } | |
811 | nd6_ra_input(n, off, icmp6len); | |
812 | /* m stays. */ | |
e2fac8b1 | 813 | goto rate_limit_checked; |
1c79356b A |
814 | |
815 | case ND_NEIGHBOR_SOLICIT: | |
816 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); | |
817 | if (code != 0) | |
818 | goto badcode; | |
819 | if (icmp6len < sizeof(struct nd_neighbor_solicit)) | |
820 | goto badlen; | |
e2fac8b1 | 821 | |
39037602 A |
822 | if (proxy || |
823 | ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL)) { | |
1c79356b A |
824 | /* give up local */ |
825 | nd6_ns_input(m, off, icmp6len); | |
826 | m = NULL; | |
827 | goto freeit; | |
828 | } | |
829 | nd6_ns_input(n, off, icmp6len); | |
830 | /* m stays. */ | |
e2fac8b1 | 831 | goto rate_limit_checked; |
1c79356b A |
832 | |
833 | case ND_NEIGHBOR_ADVERT: | |
834 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); | |
835 | if (code != 0) | |
836 | goto badcode; | |
837 | if (icmp6len < sizeof(struct nd_neighbor_advert)) | |
838 | goto badlen; | |
e2fac8b1 | 839 | |
1c79356b A |
840 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { |
841 | /* give up local */ | |
842 | nd6_na_input(m, off, icmp6len); | |
843 | m = NULL; | |
844 | goto freeit; | |
845 | } | |
846 | nd6_na_input(n, off, icmp6len); | |
847 | /* m stays. */ | |
e2fac8b1 | 848 | goto rate_limit_checked; |
1c79356b A |
849 | |
850 | case ND_REDIRECT: | |
851 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); | |
852 | if (code != 0) | |
853 | goto badcode; | |
854 | if (icmp6len < sizeof(struct nd_redirect)) | |
855 | goto badlen; | |
e2fac8b1 | 856 | |
1c79356b A |
857 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { |
858 | /* give up local */ | |
859 | icmp6_redirect_input(m, off); | |
860 | m = NULL; | |
861 | goto freeit; | |
862 | } | |
863 | icmp6_redirect_input(n, off); | |
864 | /* m stays. */ | |
e2fac8b1 | 865 | goto rate_limit_checked; |
1c79356b A |
866 | |
867 | case ICMP6_ROUTER_RENUMBERING: | |
868 | if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && | |
869 | code != ICMP6_ROUTER_RENUMBERING_RESULT) | |
870 | goto badcode; | |
871 | if (icmp6len < sizeof(struct icmp6_router_renum)) | |
872 | goto badlen; | |
873 | break; | |
874 | ||
875 | default: | |
9bccf70c A |
876 | nd6log((LOG_DEBUG, |
877 | "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", | |
878 | icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), | |
879 | ip6_sprintf(&ip6->ip6_dst), | |
880 | m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); | |
1c79356b A |
881 | if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { |
882 | /* ICMPv6 error: MUST deliver it by spec... */ | |
883 | code = PRC_NCMDS; | |
884 | /* deliver */ | |
885 | } else { | |
886 | /* ICMPv6 informational: MUST not deliver */ | |
e2fac8b1 | 887 | goto rate_limit_checked; |
1c79356b A |
888 | } |
889 | deliver: | |
9bccf70c A |
890 | if (icmp6_notify_error(m, off, icmp6len, code)) { |
891 | /* In this case, m should've been freed. */ | |
892 | return(IPPROTO_DONE); | |
1c79356b | 893 | } |
9bccf70c A |
894 | break; |
895 | ||
896 | badcode: | |
897 | icmp6stat.icp6s_badcode++; | |
898 | break; | |
899 | ||
900 | badlen: | |
901 | icmp6stat.icp6s_badlen++; | |
902 | break; | |
903 | } | |
904 | ||
e2fac8b1 | 905 | rate_limit_checked: |
39037602 A |
906 | icmp6_rip6_input(&m, *offp); |
907 | return IPPROTO_DONE; | |
9bccf70c | 908 | |
316670eb | 909 | freeit: |
9bccf70c A |
910 | m_freem(m); |
911 | return IPPROTO_DONE; | |
912 | } | |
913 | ||
914 | static int | |
39037602 | 915 | icmp6_notify_error(struct mbuf *m, int off, int icmp6len, int code) |
9bccf70c A |
916 | { |
917 | struct icmp6_hdr *icmp6; | |
918 | struct ip6_hdr *eip6; | |
919 | u_int32_t notifymtu; | |
920 | struct sockaddr_in6 icmp6src, icmp6dst; | |
921 | ||
922 | if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { | |
923 | icmp6stat.icp6s_tooshort++; | |
924 | goto freeit; | |
925 | } | |
1c79356b | 926 | #ifndef PULLDOWN_TEST |
9bccf70c A |
927 | IP6_EXTHDR_CHECK(m, off, |
928 | sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), | |
91447636 | 929 | return -1); |
9bccf70c | 930 | icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); |
1c79356b | 931 | #else |
9bccf70c A |
932 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, |
933 | sizeof(*icmp6) + sizeof(struct ip6_hdr)); | |
934 | if (icmp6 == NULL) { | |
935 | icmp6stat.icp6s_tooshort++; | |
936 | return(-1); | |
937 | } | |
1c79356b | 938 | #endif |
9bccf70c | 939 | eip6 = (struct ip6_hdr *)(icmp6 + 1); |
1c79356b | 940 | |
9bccf70c A |
941 | /* Detect the upper level protocol */ |
942 | { | |
5ba3f43e | 943 | void (*ctlfunc)(int, struct sockaddr *, void *, struct ifnet *); |
1c79356b A |
944 | u_int8_t nxt = eip6->ip6_nxt; |
945 | int eoff = off + sizeof(struct icmp6_hdr) + | |
946 | sizeof(struct ip6_hdr); | |
947 | struct ip6ctlparam ip6cp; | |
948 | struct in6_addr *finaldst = NULL; | |
949 | int icmp6type = icmp6->icmp6_type; | |
950 | struct ip6_frag *fh; | |
951 | struct ip6_rthdr *rth; | |
952 | struct ip6_rthdr0 *rth0; | |
953 | int rthlen; | |
954 | ||
55e303ae | 955 | while (1) { /* XXX: should avoid infinite loop explicitly? */ |
1c79356b A |
956 | struct ip6_ext *eh; |
957 | ||
9bccf70c | 958 | switch (nxt) { |
1c79356b A |
959 | case IPPROTO_HOPOPTS: |
960 | case IPPROTO_DSTOPTS: | |
961 | case IPPROTO_AH: | |
962 | #ifndef PULLDOWN_TEST | |
963 | IP6_EXTHDR_CHECK(m, 0, eoff + | |
964 | sizeof(struct ip6_ext), | |
91447636 | 965 | return -1); |
1c79356b A |
966 | eh = (struct ip6_ext *)(mtod(m, caddr_t) |
967 | + eoff); | |
968 | #else | |
969 | IP6_EXTHDR_GET(eh, struct ip6_ext *, m, | |
9bccf70c | 970 | eoff, sizeof(*eh)); |
1c79356b A |
971 | if (eh == NULL) { |
972 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 973 | return(-1); |
1c79356b A |
974 | } |
975 | #endif | |
6d2010ae | 976 | |
1c79356b A |
977 | if (nxt == IPPROTO_AH) |
978 | eoff += (eh->ip6e_len + 2) << 2; | |
979 | else | |
980 | eoff += (eh->ip6e_len + 1) << 3; | |
981 | nxt = eh->ip6e_nxt; | |
982 | break; | |
983 | case IPPROTO_ROUTING: | |
984 | /* | |
985 | * When the erroneous packet contains a | |
986 | * routing header, we should examine the | |
987 | * header to determine the final destination. | |
988 | * Otherwise, we can't properly update | |
989 | * information that depends on the final | |
990 | * destination (e.g. path MTU). | |
991 | */ | |
992 | #ifndef PULLDOWN_TEST | |
993 | IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), | |
91447636 | 994 | return -1); |
1c79356b A |
995 | rth = (struct ip6_rthdr *)(mtod(m, caddr_t) |
996 | + eoff); | |
997 | #else | |
998 | IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, | |
9bccf70c | 999 | eoff, sizeof(*rth)); |
1c79356b A |
1000 | if (rth == NULL) { |
1001 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 1002 | return(-1); |
1c79356b A |
1003 | } |
1004 | #endif | |
1005 | rthlen = (rth->ip6r_len + 1) << 3; | |
1006 | /* | |
1007 | * XXX: currently there is no | |
1008 | * officially defined type other | |
1009 | * than type-0. | |
1010 | * Note that if the segment left field | |
1011 | * is 0, all intermediate hops must | |
1012 | * have been passed. | |
1013 | */ | |
1014 | if (rth->ip6r_segleft && | |
1015 | rth->ip6r_type == IPV6_RTHDR_TYPE_0) { | |
1016 | int hops; | |
1017 | ||
1018 | #ifndef PULLDOWN_TEST | |
1019 | IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, | |
91447636 | 1020 | return -1); |
1c79356b A |
1021 | rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff); |
1022 | #else | |
1023 | IP6_EXTHDR_GET(rth0, | |
1024 | struct ip6_rthdr0 *, m, | |
1025 | eoff, rthlen); | |
1026 | if (rth0 == NULL) { | |
1027 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 1028 | return(-1); |
1c79356b A |
1029 | } |
1030 | #endif | |
1031 | /* just ignore a bogus header */ | |
1032 | if ((rth0->ip6r0_len % 2) == 0 && | |
1033 | (hops = rth0->ip6r0_len/2)) | |
316670eb | 1034 | finaldst = (struct in6_addr *)(void *)(rth0 + 1) + (hops - 1); |
1c79356b A |
1035 | } |
1036 | eoff += rthlen; | |
1037 | nxt = rth->ip6r_nxt; | |
1038 | break; | |
1039 | case IPPROTO_FRAGMENT: | |
1040 | #ifndef PULLDOWN_TEST | |
1041 | IP6_EXTHDR_CHECK(m, 0, eoff + | |
1042 | sizeof(struct ip6_frag), | |
91447636 | 1043 | return -1); |
1c79356b A |
1044 | fh = (struct ip6_frag *)(mtod(m, caddr_t) |
1045 | + eoff); | |
1046 | #else | |
1047 | IP6_EXTHDR_GET(fh, struct ip6_frag *, m, | |
9bccf70c | 1048 | eoff, sizeof(*fh)); |
1c79356b A |
1049 | if (fh == NULL) { |
1050 | icmp6stat.icp6s_tooshort++; | |
6d2010ae | 1051 | return (-1); |
1c79356b A |
1052 | } |
1053 | #endif | |
1054 | /* | |
1055 | * Data after a fragment header is meaningless | |
1056 | * unless it is the first fragment, but | |
1057 | * we'll go to the notify label for path MTU | |
1058 | * discovery. | |
1059 | */ | |
1060 | if (fh->ip6f_offlg & IP6F_OFF_MASK) | |
1061 | goto notify; | |
1062 | ||
1063 | eoff += sizeof(struct ip6_frag); | |
1064 | nxt = fh->ip6f_nxt; | |
1065 | break; | |
1066 | default: | |
1067 | /* | |
1068 | * This case includes ESP and the No Next | |
55e303ae | 1069 | * Header. In such cases going to the notify |
1c79356b A |
1070 | * label does not have any meaning |
1071 | * (i.e. ctlfunc will be NULL), but we go | |
1072 | * anyway since we might have to update | |
1073 | * path MTU information. | |
1074 | */ | |
1075 | goto notify; | |
1076 | } | |
1077 | } | |
9bccf70c | 1078 | notify: |
1c79356b A |
1079 | #ifndef PULLDOWN_TEST |
1080 | icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); | |
1081 | #else | |
1082 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, | |
6d2010ae | 1083 | sizeof(*icmp6) + sizeof(struct ip6_hdr)); |
1c79356b A |
1084 | if (icmp6 == NULL) { |
1085 | icmp6stat.icp6s_tooshort++; | |
6d2010ae | 1086 | return (-1); |
1c79356b A |
1087 | } |
1088 | #endif | |
9bccf70c | 1089 | |
6d2010ae A |
1090 | /* |
1091 | * retrieve parameters from the inner IPv6 header, and convert | |
1092 | * them into sockaddr structures. | |
1093 | * XXX: there is no guarantee that the source or destination | |
1094 | * addresses of the inner packet are in the same scope as | |
1095 | * the addresses of the icmp packet. But there is no other | |
1096 | * way to determine the zone. | |
1097 | */ | |
9bccf70c | 1098 | eip6 = (struct ip6_hdr *)(icmp6 + 1); |
6d2010ae | 1099 | |
9bccf70c A |
1100 | bzero(&icmp6dst, sizeof(icmp6dst)); |
1101 | icmp6dst.sin6_len = sizeof(struct sockaddr_in6); | |
1102 | icmp6dst.sin6_family = AF_INET6; | |
1103 | if (finaldst == NULL) | |
1104 | icmp6dst.sin6_addr = eip6->ip6_dst; | |
1105 | else | |
1106 | icmp6dst.sin6_addr = *finaldst; | |
6d2010ae | 1107 | if (in6_setscope(&icmp6dst.sin6_addr, m->m_pkthdr.rcvif, NULL)) |
9bccf70c | 1108 | goto freeit; |
9bccf70c A |
1109 | bzero(&icmp6src, sizeof(icmp6src)); |
1110 | icmp6src.sin6_len = sizeof(struct sockaddr_in6); | |
1111 | icmp6src.sin6_family = AF_INET6; | |
1112 | icmp6src.sin6_addr = eip6->ip6_src; | |
6d2010ae | 1113 | if (in6_setscope(&icmp6src.sin6_addr, m->m_pkthdr.rcvif, NULL)) |
9bccf70c | 1114 | goto freeit; |
9bccf70c | 1115 | icmp6src.sin6_flowinfo = |
6d2010ae | 1116 | (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); |
9bccf70c A |
1117 | |
1118 | if (finaldst == NULL) | |
1119 | finaldst = &eip6->ip6_dst; | |
1120 | ip6cp.ip6c_m = m; | |
1121 | ip6cp.ip6c_icmp6 = icmp6; | |
1122 | ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); | |
1123 | ip6cp.ip6c_off = eoff; | |
1124 | ip6cp.ip6c_finaldst = finaldst; | |
1125 | ip6cp.ip6c_src = &icmp6src; | |
1126 | ip6cp.ip6c_nxt = nxt; | |
1127 | ||
1c79356b | 1128 | if (icmp6type == ICMP6_PACKET_TOO_BIG) { |
9bccf70c A |
1129 | notifymtu = ntohl(icmp6->icmp6_mtu); |
1130 | ip6cp.ip6c_cmdarg = (void *)¬ifymtu; | |
1131 | icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ | |
1c79356b A |
1132 | } |
1133 | ||
5ba3f43e | 1134 | ctlfunc = ip6_protox[nxt]->pr_ctlinput; |
1c79356b | 1135 | if (ctlfunc) { |
5c9f4661 A |
1136 | LCK_MTX_ASSERT(inet6_domain_mutex, LCK_MTX_ASSERT_OWNED); |
1137 | ||
1138 | lck_mtx_unlock(inet6_domain_mutex); | |
1139 | ||
9bccf70c | 1140 | (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, |
5ba3f43e | 1141 | &ip6cp, m->m_pkthdr.rcvif); |
5c9f4661 A |
1142 | |
1143 | lck_mtx_lock(inet6_domain_mutex); | |
1c79356b | 1144 | } |
1c79356b | 1145 | } |
9bccf70c | 1146 | return(0); |
1c79356b | 1147 | |
b0d623f7 | 1148 | freeit: |
1c79356b | 1149 | m_freem(m); |
9bccf70c | 1150 | return(-1); |
1c79356b A |
1151 | } |
1152 | ||
9bccf70c | 1153 | void |
39037602 | 1154 | icmp6_mtudisc_update(struct ip6ctlparam *ip6cp, int validated) |
1c79356b | 1155 | { |
9bccf70c A |
1156 | struct in6_addr *dst = ip6cp->ip6c_finaldst; |
1157 | struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; | |
1158 | struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ | |
1c79356b A |
1159 | u_int mtu = ntohl(icmp6->icmp6_mtu); |
1160 | struct rtentry *rt = NULL; | |
1161 | struct sockaddr_in6 sin6; | |
6d2010ae A |
1162 | /* |
1163 | * we reject ICMPv6 too big with abnormally small value. | |
1164 | * XXX what is the good definition of "abnormally small"? | |
1165 | */ | |
1166 | if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8) | |
1167 | return; | |
9bccf70c A |
1168 | |
1169 | if (!validated) | |
1170 | return; | |
6d2010ae | 1171 | |
c910b4d9 A |
1172 | /* |
1173 | * In case the suggested mtu is less than IPV6_MMTU, we | |
1174 | * only need to remember that it was for above mentioned | |
1175 | * "alwaysfrag" case. | |
1176 | * Try to be as close to the spec as possible. | |
1177 | */ | |
1178 | if (mtu < IPV6_MMTU) | |
1179 | mtu = IPV6_MMTU - 8; | |
1180 | ||
1c79356b A |
1181 | bzero(&sin6, sizeof(sin6)); |
1182 | sin6.sin6_family = PF_INET6; | |
1183 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
1184 | sin6.sin6_addr = *dst; | |
9bccf70c A |
1185 | /* XXX normally, this won't happen */ |
1186 | if (IN6_IS_ADDR_LINKLOCAL(dst)) { | |
1187 | sin6.sin6_addr.s6_addr16[1] = | |
1188 | htons(m->m_pkthdr.rcvif->if_index); | |
1c79356b | 1189 | } |
9bccf70c | 1190 | /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */ |
39037602 A |
1191 | /* |
1192 | * XXX On a side note, for asymmetric data-path | |
1193 | * the lookup on receive interace is probably not | |
1194 | * what we want to do. | |
1195 | * That requires looking at the cached route for the | |
1196 | * protocol control block. | |
1197 | */ | |
6d2010ae A |
1198 | rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0, |
1199 | RTF_CLONING | RTF_PRCLONING, m->m_pkthdr.rcvif->if_index); | |
b0d623f7 A |
1200 | if (rt != NULL) { |
1201 | RT_LOCK(rt); | |
1202 | if ((rt->rt_flags & RTF_HOST) && | |
6d2010ae A |
1203 | !(rt->rt_rmx.rmx_locks & RTV_MTU) && |
1204 | mtu < IN6_LINKMTU(rt->rt_ifp) && | |
1205 | rt->rt_rmx.rmx_mtu > mtu) { | |
1206 | icmp6stat.icp6s_pmtuchg++; | |
1207 | rt->rt_rmx.rmx_mtu = mtu; | |
1c79356b | 1208 | } |
b0d623f7 | 1209 | RT_UNLOCK(rt); |
9bccf70c | 1210 | rtfree(rt); |
b0d623f7 | 1211 | } |
1c79356b A |
1212 | } |
1213 | ||
1214 | /* | |
9bccf70c A |
1215 | * Process a Node Information Query packet, based on |
1216 | * draft-ietf-ipngwg-icmp-name-lookups-07. | |
6d2010ae | 1217 | * |
9bccf70c A |
1218 | * Spec incompatibilities: |
1219 | * - IPv6 Subject address handling | |
1220 | * - IPv4 Subject address handling support missing | |
1221 | * - Proxy reply (answer even if it's not for me) | |
1222 | * - joins NI group address at in6_ifattach() time only, does not cope | |
1223 | * with hostname changes by sethostname(3) | |
1c79356b | 1224 | */ |
1c79356b | 1225 | #define hostnamelen strlen(hostname) |
1c79356b | 1226 | static struct mbuf * |
39037602 | 1227 | ni6_input(struct mbuf *m, int off) |
1c79356b A |
1228 | { |
1229 | struct icmp6_nodeinfo *ni6, *nni6; | |
1230 | struct mbuf *n = NULL; | |
1231 | u_int16_t qtype; | |
9bccf70c | 1232 | int subjlen; |
1c79356b A |
1233 | int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); |
1234 | struct ni_reply_fqdn *fqdn; | |
1235 | int addrs; /* for NI_QTYPE_NODEADDR */ | |
1236 | struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ | |
9bccf70c A |
1237 | struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */ |
1238 | struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */ | |
1239 | struct ip6_hdr *ip6; | |
1240 | int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ | |
1241 | char *subj = NULL; | |
1c79356b | 1242 | |
9bccf70c | 1243 | ip6 = mtod(m, struct ip6_hdr *); |
1c79356b A |
1244 | #ifndef PULLDOWN_TEST |
1245 | ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); | |
1246 | #else | |
1247 | IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); | |
1248 | if (ni6 == NULL) { | |
1249 | /* m is already reclaimed */ | |
6d2010ae | 1250 | return (NULL); |
1c79356b A |
1251 | } |
1252 | #endif | |
9bccf70c | 1253 | |
6d2010ae A |
1254 | /* |
1255 | * Validate IPv6 source address. | |
1256 | * The default configuration MUST be to refuse answering queries from | |
1257 | * global-scope addresses according to RFC4602. | |
1258 | * Notes: | |
1259 | * - it's not very clear what "refuse" means; this implementation | |
1260 | * simply drops it. | |
1261 | * - it's not very easy to identify global-scope (unicast) addresses | |
1262 | * since there are many prefixes for them. It should be safer | |
1263 | * and in practice sufficient to check "all" but loopback and | |
1264 | * link-local (note that site-local unicast was deprecated and | |
1265 | * ULA is defined as global scope-wise) | |
1266 | */ | |
1267 | if ((icmp6_nodeinfo & ICMP6_NODEINFO_GLOBALOK) == 0 && | |
1268 | !IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) && | |
1269 | !IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) | |
1270 | goto bad; | |
1271 | ||
9bccf70c A |
1272 | /* |
1273 | * Validate IPv6 destination address. | |
1274 | * | |
1275 | * The Responder must discard the Query without further processing | |
1276 | * unless it is one of the Responder's unicast or anycast addresses, or | |
1277 | * a link-local scope multicast address which the Responder has joined. | |
6d2010ae | 1278 | * [RFC4602, Section 5.] |
9bccf70c | 1279 | */ |
6d2010ae A |
1280 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
1281 | if (!IN6_IS_ADDR_MC_LINKLOCAL(&ip6->ip6_dst)) | |
1282 | goto bad; | |
1283 | /* else it's a link-local multicast, fine */ | |
1284 | } else { /* unicast or anycast */ | |
39236c6e | 1285 | uint32_t ia6_flags; |
6d2010ae | 1286 | |
39236c6e | 1287 | if (ip6_getdstifaddr_info(m, NULL, &ia6_flags) != 0) |
6d2010ae A |
1288 | goto bad; /* XXX impossible */ |
1289 | ||
39236c6e | 1290 | if ((ia6_flags & IN6_IFF_TEMPORARY) && |
6d2010ae | 1291 | !(icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK)) { |
9bccf70c A |
1292 | nd6log((LOG_DEBUG, "ni6_input: ignore node info to " |
1293 | "a temporary address in %s:%d", | |
5ba3f43e | 1294 | __func__, __LINE__)); |
9bccf70c A |
1295 | goto bad; |
1296 | } | |
6d2010ae | 1297 | } |
9bccf70c A |
1298 | |
1299 | /* validate query Subject field. */ | |
1c79356b | 1300 | qtype = ntohs(ni6->ni_qtype); |
9bccf70c A |
1301 | subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); |
1302 | switch (qtype) { | |
1303 | case NI_QTYPE_NOOP: | |
1304 | case NI_QTYPE_SUPTYPES: | |
1305 | /* 07 draft */ | |
1306 | if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) | |
1307 | break; | |
1308 | /* FALLTHROUGH */ | |
1309 | case NI_QTYPE_FQDN: | |
1310 | case NI_QTYPE_NODEADDR: | |
6d2010ae | 1311 | case NI_QTYPE_IPV4ADDR: |
9bccf70c A |
1312 | switch (ni6->ni_code) { |
1313 | case ICMP6_NI_SUBJ_IPV6: | |
1314 | #if ICMP6_NI_SUBJ_IPV6 != 0 | |
1315 | case 0: | |
1316 | #endif | |
1317 | /* | |
1318 | * backward compatibility - try to accept 03 draft | |
1319 | * format, where no Subject is present. | |
1320 | */ | |
1321 | if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && | |
1322 | subjlen == 0) { | |
1323 | oldfqdn++; | |
1324 | break; | |
1325 | } | |
1326 | #if ICMP6_NI_SUBJ_IPV6 != 0 | |
1327 | if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) | |
1328 | goto bad; | |
1329 | #endif | |
1330 | ||
6d2010ae | 1331 | if (subjlen != sizeof(struct in6_addr)) |
9bccf70c A |
1332 | goto bad; |
1333 | ||
1334 | /* | |
1335 | * Validate Subject address. | |
1336 | * | |
1337 | * Not sure what exactly "address belongs to the node" | |
1338 | * means in the spec, is it just unicast, or what? | |
1339 | * | |
1340 | * At this moment we consider Subject address as | |
1341 | * "belong to the node" if the Subject address equals | |
1342 | * to the IPv6 destination address; validation for | |
1343 | * IPv6 destination address should have done enough | |
1344 | * check for us. | |
1345 | * | |
1346 | * We do not do proxy at this moment. | |
1347 | */ | |
1348 | /* m_pulldown instead of copy? */ | |
1349 | m_copydata(m, off + sizeof(struct icmp6_nodeinfo), | |
1350 | subjlen, (caddr_t)&sin6.sin6_addr); | |
1351 | sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1352 | &sin6.sin6_addr); | |
6d2010ae A |
1353 | in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL, |
1354 | NULL); | |
9bccf70c A |
1355 | bzero(&sin6_d, sizeof(sin6_d)); |
1356 | sin6_d.sin6_family = AF_INET6; /* not used, actually */ | |
1357 | sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ | |
1358 | sin6_d.sin6_addr = ip6->ip6_dst; | |
1359 | sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1360 | &ip6->ip6_dst); | |
6d2010ae A |
1361 | in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL, |
1362 | NULL); | |
9bccf70c A |
1363 | subj = (char *)&sin6; |
1364 | if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) | |
1365 | break; | |
1366 | ||
1367 | /* | |
1368 | * XXX if we are to allow other cases, we should really | |
1369 | * be careful about scope here. | |
1370 | * basically, we should disallow queries toward IPv6 | |
6d2010ae A |
1371 | * destination X with subject Y, |
1372 | * if scope(X) > scope(Y). | |
9bccf70c A |
1373 | * if we allow scope(X) > scope(Y), it will result in |
1374 | * information leakage across scope boundary. | |
1375 | */ | |
1376 | goto bad; | |
1c79356b | 1377 | |
9bccf70c A |
1378 | case ICMP6_NI_SUBJ_FQDN: |
1379 | /* | |
1380 | * Validate Subject name with gethostname(3). | |
1381 | * | |
1382 | * The behavior may need some debate, since: | |
1383 | * - we are not sure if the node has FQDN as | |
1384 | * hostname (returned by gethostname(3)). | |
1385 | * - the code does wildcard match for truncated names. | |
1386 | * however, we are not sure if we want to perform | |
1387 | * wildcard match, if gethostname(3) side has | |
1388 | * truncated hostname. | |
1389 | */ | |
1390 | n = ni6_nametodns(hostname, hostnamelen, 0); | |
1391 | if (!n || n->m_next || n->m_len == 0) | |
1392 | goto bad; | |
1393 | IP6_EXTHDR_GET(subj, char *, m, | |
1394 | off + sizeof(struct icmp6_nodeinfo), subjlen); | |
1395 | if (subj == NULL) | |
1396 | goto bad; | |
1397 | if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), | |
1398 | n->m_len)) { | |
1399 | goto bad; | |
1400 | } | |
1401 | m_freem(n); | |
1402 | n = NULL; | |
1403 | break; | |
1404 | ||
1405 | case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ | |
1406 | default: | |
1407 | goto bad; | |
1408 | } | |
1409 | break; | |
1c79356b A |
1410 | } |
1411 | ||
9bccf70c A |
1412 | /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ |
1413 | switch (qtype) { | |
1414 | case NI_QTYPE_FQDN: | |
6d2010ae | 1415 | if ((icmp6_nodeinfo & ICMP6_NODEINFO_FQDNOK) == 0) |
9bccf70c A |
1416 | goto bad; |
1417 | break; | |
1418 | case NI_QTYPE_NODEADDR: | |
6d2010ae A |
1419 | case NI_QTYPE_IPV4ADDR: |
1420 | if ((icmp6_nodeinfo & ICMP6_NODEINFO_NODEADDROK) == 0) | |
9bccf70c A |
1421 | goto bad; |
1422 | break; | |
1423 | } | |
1424 | ||
1425 | /* guess reply length */ | |
1426 | switch (qtype) { | |
1427 | case NI_QTYPE_NOOP: | |
1428 | break; /* no reply data */ | |
1429 | case NI_QTYPE_SUPTYPES: | |
1430 | replylen += sizeof(u_int32_t); | |
1431 | break; | |
1432 | case NI_QTYPE_FQDN: | |
1433 | /* XXX will append an mbuf */ | |
1434 | replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); | |
1435 | break; | |
1436 | case NI_QTYPE_NODEADDR: | |
91447636 | 1437 | addrs = ni6_addrs(ni6, &ifp, subj); |
9bccf70c | 1438 | if ((replylen += addrs * (sizeof(struct in6_addr) + |
6d2010ae | 1439 | sizeof(u_int32_t))) > MCLBYTES) |
9bccf70c A |
1440 | replylen = MCLBYTES; /* XXX: will truncate pkt later */ |
1441 | break; | |
6d2010ae A |
1442 | case NI_QTYPE_IPV4ADDR: |
1443 | /* unsupported - should respond with unknown Qtype? */ | |
1444 | break; | |
9bccf70c A |
1445 | default: |
1446 | /* | |
1447 | * XXX: We must return a reply with the ICMP6 code | |
6d2010ae | 1448 | * `unknown Qtype' in this case. However we regard the case |
9bccf70c A |
1449 | * as an FQDN query for backward compatibility. |
1450 | * Older versions set a random value to this field, | |
1451 | * so it rarely varies in the defined qtypes. | |
1452 | * But the mechanism is not reliable... | |
1453 | * maybe we should obsolete older versions. | |
1454 | */ | |
1455 | qtype = NI_QTYPE_FQDN; | |
1456 | /* XXX will append an mbuf */ | |
1457 | replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); | |
1458 | oldfqdn++; | |
1459 | break; | |
1460 | } | |
1461 | ||
1462 | /* allocate an mbuf to reply. */ | |
2d21ac55 | 1463 | MGETHDR(n, M_DONTWAIT, m->m_type); /* MAC-OK */ |
1c79356b A |
1464 | if (n == NULL) { |
1465 | m_freem(m); | |
6d2010ae A |
1466 | if (ifp != NULL) |
1467 | ifnet_release(ifp); | |
1468 | return (NULL); | |
1c79356b A |
1469 | } |
1470 | M_COPY_PKTHDR(n, m); /* just for recvif */ | |
1471 | if (replylen > MHLEN) { | |
9bccf70c A |
1472 | if (replylen > MCLBYTES) { |
1473 | /* | |
1474 | * XXX: should we try to allocate more? But MCLBYTES | |
1475 | * is probably much larger than IPV6_MMTU... | |
1476 | */ | |
1c79356b | 1477 | goto bad; |
9bccf70c | 1478 | } |
1c79356b A |
1479 | MCLGET(n, M_DONTWAIT); |
1480 | if ((n->m_flags & M_EXT) == 0) { | |
1481 | goto bad; | |
1482 | } | |
1483 | } | |
1484 | n->m_pkthdr.len = n->m_len = replylen; | |
1485 | ||
9bccf70c A |
1486 | /* copy mbuf header and IPv6 + Node Information base headers */ |
1487 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); | |
1488 | nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); | |
1489 | bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); | |
1490 | ||
1491 | /* qtype dependent procedure */ | |
1492 | switch (qtype) { | |
1493 | case NI_QTYPE_NOOP: | |
1494 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1495 | nni6->ni_flags = 0; | |
1496 | break; | |
1497 | case NI_QTYPE_SUPTYPES: | |
1498 | { | |
1499 | u_int32_t v; | |
1500 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1501 | nni6->ni_flags = htons(0x0000); /* raw bitmap */ | |
1502 | /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ | |
1503 | v = (u_int32_t)htonl(0x0000000f); | |
1504 | bcopy(&v, nni6 + 1, sizeof(u_int32_t)); | |
1505 | break; | |
1506 | } | |
1507 | case NI_QTYPE_FQDN: | |
1508 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1509 | fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + | |
1510 | sizeof(struct ip6_hdr) + | |
1511 | sizeof(struct icmp6_nodeinfo)); | |
1512 | nni6->ni_flags = 0; /* XXX: meaningless TTL */ | |
1513 | fqdn->ni_fqdn_ttl = 0; /* ditto. */ | |
1514 | /* | |
1515 | * XXX do we really have FQDN in variable "hostname"? | |
1516 | */ | |
1517 | n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); | |
1518 | if (n->m_next == NULL) | |
1519 | goto bad; | |
1520 | /* XXX we assume that n->m_next is not a chain */ | |
1521 | if (n->m_next->m_next != NULL) | |
1522 | goto bad; | |
1523 | n->m_pkthdr.len += n->m_next->m_len; | |
1524 | break; | |
1525 | case NI_QTYPE_NODEADDR: | |
1526 | { | |
1527 | int lenlim, copied; | |
1528 | ||
1529 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1530 | n->m_pkthdr.len = n->m_len = | |
1531 | sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); | |
1532 | lenlim = M_TRAILINGSPACE(n); | |
1533 | copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); | |
1534 | /* XXX: reset mbuf length */ | |
1535 | n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + | |
1536 | sizeof(struct icmp6_nodeinfo) + copied; | |
1537 | break; | |
1538 | } | |
1539 | default: | |
1540 | break; /* XXX impossible! */ | |
1541 | } | |
1542 | ||
1543 | nni6->ni_type = ICMP6_NI_REPLY; | |
1544 | m_freem(m); | |
6d2010ae A |
1545 | if (ifp != NULL) |
1546 | ifnet_release(ifp); | |
1547 | return (n); | |
9bccf70c | 1548 | |
6d2010ae | 1549 | bad: |
9bccf70c A |
1550 | m_freem(m); |
1551 | if (n) | |
1552 | m_freem(n); | |
6d2010ae A |
1553 | if (ifp != NULL) |
1554 | ifnet_release(ifp); | |
1555 | return (NULL); | |
9bccf70c A |
1556 | } |
1557 | #undef hostnamelen | |
1558 | ||
1559 | /* | |
1560 | * make a mbuf with DNS-encoded string. no compression support. | |
1561 | * | |
1562 | * XXX names with less than 2 dots (like "foo" or "foo.section") will be | |
1563 | * treated as truncated name (two \0 at the end). this is a wild guess. | |
1564 | */ | |
1565 | static struct mbuf * | |
39037602 A |
1566 | ni6_nametodns( |
1567 | const char *name, | |
1568 | int namelen, | |
1569 | int old) /* return pascal string if non-zero */ | |
9bccf70c A |
1570 | { |
1571 | struct mbuf *m; | |
1572 | char *cp, *ep; | |
1573 | const char *p, *q; | |
1574 | int i, len, nterm; | |
1575 | ||
1576 | if (old) | |
1577 | len = namelen + 1; | |
1578 | else | |
1579 | len = MCLBYTES; | |
1580 | ||
1581 | /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ | |
1582 | MGET(m, M_DONTWAIT, MT_DATA); | |
1583 | if (m && len > MLEN) { | |
1584 | MCLGET(m, M_DONTWAIT); | |
1585 | if ((m->m_flags & M_EXT) == 0) | |
1586 | goto fail; | |
1587 | } | |
1588 | if (!m) | |
1589 | goto fail; | |
1590 | m->m_next = NULL; | |
1591 | ||
1592 | if (old) { | |
1593 | m->m_len = len; | |
1594 | *mtod(m, char *) = namelen; | |
1595 | bcopy(name, mtod(m, char *) + 1, namelen); | |
1596 | return m; | |
1597 | } else { | |
1598 | m->m_len = 0; | |
1599 | cp = mtod(m, char *); | |
1600 | ep = mtod(m, char *) + M_TRAILINGSPACE(m); | |
1601 | ||
1602 | /* if not certain about my name, return empty buffer */ | |
1603 | if (namelen == 0) | |
1604 | return m; | |
1605 | ||
1606 | /* | |
1607 | * guess if it looks like shortened hostname, or FQDN. | |
1608 | * shortened hostname needs two trailing "\0". | |
1609 | */ | |
1610 | i = 0; | |
1611 | for (p = name; p < name + namelen; p++) { | |
1612 | if (*p && *p == '.') | |
1613 | i++; | |
1614 | } | |
1615 | if (i < 2) | |
1616 | nterm = 2; | |
1617 | else | |
1618 | nterm = 1; | |
1619 | ||
1620 | p = name; | |
1621 | while (cp < ep && p < name + namelen) { | |
1622 | i = 0; | |
1623 | for (q = p; q < name + namelen && *q && *q != '.'; q++) | |
1624 | i++; | |
1625 | /* result does not fit into mbuf */ | |
1626 | if (cp + i + 1 >= ep) | |
1627 | goto fail; | |
1628 | /* | |
1629 | * DNS label length restriction, RFC1035 page 8. | |
1630 | * "i == 0" case is included here to avoid returning | |
1631 | * 0-length label on "foo..bar". | |
1632 | */ | |
1633 | if (i <= 0 || i >= 64) | |
1634 | goto fail; | |
1635 | *cp++ = i; | |
1636 | bcopy(p, cp, i); | |
1637 | cp += i; | |
1638 | p = q; | |
1639 | if (p < name + namelen && *p == '.') | |
1640 | p++; | |
1641 | } | |
1642 | /* termination */ | |
1643 | if (cp + nterm >= ep) | |
1644 | goto fail; | |
1645 | while (nterm-- > 0) | |
1646 | *cp++ = '\0'; | |
1647 | m->m_len = cp - mtod(m, char *); | |
1648 | return m; | |
1649 | } | |
1650 | ||
1651 | panic("should not reach here"); | |
55e303ae | 1652 | /* NOTREACHED */ |
9bccf70c A |
1653 | |
1654 | fail: | |
1655 | if (m) | |
1656 | m_freem(m); | |
1657 | return NULL; | |
1658 | } | |
1659 | ||
1660 | /* | |
1661 | * check if two DNS-encoded string matches. takes care of truncated | |
1662 | * form (with \0\0 at the end). no compression support. | |
1663 | * XXX upper/lowercase match (see RFC2065) | |
1664 | */ | |
1665 | static int | |
39037602 | 1666 | ni6_dnsmatch(const char *a, int alen, const char *b, int blen) |
9bccf70c A |
1667 | { |
1668 | const char *a0, *b0; | |
1669 | int l; | |
1670 | ||
1671 | /* simplest case - need validation? */ | |
1672 | if (alen == blen && bcmp(a, b, alen) == 0) | |
1673 | return 1; | |
1c79356b | 1674 | |
9bccf70c A |
1675 | a0 = a; |
1676 | b0 = b; | |
1c79356b | 1677 | |
9bccf70c A |
1678 | /* termination is mandatory */ |
1679 | if (alen < 2 || blen < 2) | |
1680 | return 0; | |
1681 | if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') | |
1682 | return 0; | |
1683 | alen--; | |
1684 | blen--; | |
1685 | ||
1686 | while (a - a0 < alen && b - b0 < blen) { | |
1687 | if (a - a0 + 1 > alen || b - b0 + 1 > blen) | |
1688 | return 0; | |
1689 | ||
1690 | if ((signed char)a[0] < 0 || (signed char)b[0] < 0) | |
1691 | return 0; | |
1692 | /* we don't support compression yet */ | |
1693 | if (a[0] >= 64 || b[0] >= 64) | |
1694 | return 0; | |
1695 | ||
1696 | /* truncated case */ | |
1697 | if (a[0] == 0 && a - a0 == alen - 1) | |
1698 | return 1; | |
1699 | if (b[0] == 0 && b - b0 == blen - 1) | |
1700 | return 1; | |
1701 | if (a[0] == 0 || b[0] == 0) | |
1702 | return 0; | |
1703 | ||
1704 | if (a[0] != b[0]) | |
1705 | return 0; | |
1706 | l = a[0]; | |
1707 | if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) | |
1708 | return 0; | |
1709 | if (bcmp(a + 1, b + 1, l) != 0) | |
1710 | return 0; | |
1711 | ||
1712 | a += 1 + l; | |
1713 | b += 1 + l; | |
1714 | } | |
1c79356b | 1715 | |
9bccf70c A |
1716 | if (a - a0 == alen && b - b0 == blen) |
1717 | return 1; | |
1718 | else | |
1719 | return 0; | |
1c79356b | 1720 | } |
1c79356b A |
1721 | |
1722 | /* | |
1723 | * calculate the number of addresses to be returned in the node info reply. | |
1724 | */ | |
1725 | static int | |
39037602 | 1726 | ni6_addrs(struct icmp6_nodeinfo *ni6, struct ifnet **ifpp, char *subj) |
1c79356b | 1727 | { |
9bccf70c A |
1728 | struct ifnet *ifp; |
1729 | struct in6_ifaddr *ifa6; | |
1730 | struct ifaddr *ifa; | |
1731 | struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ | |
1c79356b | 1732 | int addrs = 0, addrsofif, iffound = 0; |
9bccf70c A |
1733 | int niflags = ni6->ni_flags; |
1734 | ||
6d2010ae A |
1735 | if (ifpp != NULL) |
1736 | *ifpp = NULL; | |
1737 | ||
9bccf70c A |
1738 | if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { |
1739 | switch (ni6->ni_code) { | |
1740 | case ICMP6_NI_SUBJ_IPV6: | |
1741 | if (subj == NULL) /* must be impossible... */ | |
1742 | return(0); | |
316670eb | 1743 | subj_ip6 = (struct sockaddr_in6 *)(void *)subj; |
9bccf70c A |
1744 | break; |
1745 | default: | |
1746 | /* | |
1747 | * XXX: we only support IPv6 subject address for | |
1748 | * this Qtype. | |
1749 | */ | |
6d2010ae | 1750 | return (0); |
9bccf70c A |
1751 | } |
1752 | } | |
1c79356b | 1753 | |
91447636 A |
1754 | ifnet_head_lock_shared(); |
1755 | TAILQ_FOREACH(ifp, &ifnet_head, if_list) { | |
1c79356b | 1756 | addrsofif = 0; |
91447636 | 1757 | ifnet_lock_shared(ifp); |
9bccf70c | 1758 | TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) |
1c79356b | 1759 | { |
6d2010ae A |
1760 | IFA_LOCK(ifa); |
1761 | if (ifa->ifa_addr->sa_family != AF_INET6) { | |
1762 | IFA_UNLOCK(ifa); | |
1c79356b | 1763 | continue; |
6d2010ae | 1764 | } |
1c79356b A |
1765 | ifa6 = (struct in6_ifaddr *)ifa; |
1766 | ||
9bccf70c A |
1767 | if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && |
1768 | IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, | |
1c79356b A |
1769 | &ifa6->ia_addr.sin6_addr)) |
1770 | iffound = 1; | |
1771 | ||
1772 | /* | |
1773 | * IPv4-mapped addresses can only be returned by a | |
1774 | * Node Information proxy, since they represent | |
1775 | * addresses of IPv4-only nodes, which perforce do | |
1776 | * not implement this protocol. | |
9bccf70c | 1777 | * [icmp-name-lookups-07, Section 5.4] |
1c79356b A |
1778 | * So we don't support NI_NODEADDR_FLAG_COMPAT in |
1779 | * this function at this moment. | |
1780 | */ | |
1781 | ||
1c79356b | 1782 | /* What do we have to do about ::1? */ |
9bccf70c A |
1783 | switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { |
1784 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
6d2010ae A |
1785 | if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) { |
1786 | IFA_UNLOCK(ifa); | |
9bccf70c | 1787 | continue; |
6d2010ae | 1788 | } |
9bccf70c A |
1789 | break; |
1790 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
6d2010ae A |
1791 | if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) { |
1792 | IFA_UNLOCK(ifa); | |
9bccf70c | 1793 | continue; |
6d2010ae | 1794 | } |
1c79356b | 1795 | break; |
9bccf70c | 1796 | case IPV6_ADDR_SCOPE_GLOBAL: |
6d2010ae A |
1797 | if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) { |
1798 | IFA_UNLOCK(ifa); | |
9bccf70c | 1799 | continue; |
6d2010ae | 1800 | } |
1c79356b | 1801 | break; |
9bccf70c | 1802 | default: |
6d2010ae | 1803 | IFA_UNLOCK(ifa); |
9bccf70c A |
1804 | continue; |
1805 | } | |
1806 | ||
1807 | /* | |
1808 | * check if anycast is okay. | |
55e303ae | 1809 | * XXX: just experimental. not in the spec. |
9bccf70c A |
1810 | */ |
1811 | if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && | |
6d2010ae A |
1812 | (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) { |
1813 | IFA_UNLOCK(ifa); | |
9bccf70c | 1814 | continue; /* we need only unicast addresses */ |
6d2010ae | 1815 | } |
9bccf70c | 1816 | if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && |
6d2010ae A |
1817 | (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { |
1818 | IFA_UNLOCK(ifa); | |
9bccf70c | 1819 | continue; |
1c79356b | 1820 | } |
9bccf70c | 1821 | addrsofif++; /* count the address */ |
6d2010ae | 1822 | IFA_UNLOCK(ifa); |
1c79356b | 1823 | } |
91447636 | 1824 | ifnet_lock_done(ifp); |
1c79356b | 1825 | if (iffound) { |
6d2010ae A |
1826 | if (ifpp != NULL) { |
1827 | *ifpp = ifp; | |
1828 | ifnet_reference(ifp); | |
1829 | } | |
91447636 | 1830 | ifnet_head_done(); |
1c79356b A |
1831 | return(addrsofif); |
1832 | } | |
1833 | ||
1834 | addrs += addrsofif; | |
1835 | } | |
91447636 | 1836 | ifnet_head_done(); |
1c79356b | 1837 | |
6d2010ae | 1838 | return (addrs); |
1c79356b A |
1839 | } |
1840 | ||
1841 | static int | |
39037602 A |
1842 | ni6_store_addrs(struct icmp6_nodeinfo *ni6, struct icmp6_nodeinfo *nni6, |
1843 | struct ifnet *ifp0, int resid) | |
1c79356b | 1844 | { |
91447636 | 1845 | struct ifnet *ifp = ifp0; |
9bccf70c A |
1846 | struct in6_ifaddr *ifa6; |
1847 | struct ifaddr *ifa; | |
1848 | struct ifnet *ifp_dep = NULL; | |
1849 | int copied = 0, allow_deprecated = 0; | |
1c79356b | 1850 | u_char *cp = (u_char *)(nni6 + 1); |
9bccf70c A |
1851 | int niflags = ni6->ni_flags; |
1852 | u_int32_t ltime; | |
39236c6e | 1853 | uint64_t now = net_uptime(); |
1c79356b | 1854 | |
9bccf70c | 1855 | if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) |
6d2010ae | 1856 | return (0); /* needless to copy */ |
1c79356b | 1857 | |
9bccf70c A |
1858 | again: |
1859 | ||
91447636 | 1860 | ifnet_head_lock_shared(); |
6d2010ae A |
1861 | if (ifp == NULL) |
1862 | ifp = TAILQ_FIRST(&ifnet_head); | |
1863 | ||
91447636 A |
1864 | for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { |
1865 | ifnet_lock_shared(ifp); | |
1c79356b | 1866 | for (ifa = ifp->if_addrlist.tqh_first; ifa; |
6d2010ae | 1867 | ifa = ifa->ifa_list.tqe_next) { |
39236c6e A |
1868 | struct in6_addrlifetime_i *lt; |
1869 | ||
6d2010ae A |
1870 | IFA_LOCK(ifa); |
1871 | if (ifa->ifa_addr->sa_family != AF_INET6) { | |
1872 | IFA_UNLOCK(ifa); | |
1c79356b | 1873 | continue; |
6d2010ae | 1874 | } |
1c79356b A |
1875 | ifa6 = (struct in6_ifaddr *)ifa; |
1876 | ||
9bccf70c A |
1877 | if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && |
1878 | allow_deprecated == 0) { | |
1879 | /* | |
1880 | * prefererred address should be put before | |
1881 | * deprecated addresses. | |
1882 | */ | |
1883 | ||
1884 | /* record the interface for later search */ | |
1885 | if (ifp_dep == NULL) | |
1886 | ifp_dep = ifp; | |
1887 | ||
6d2010ae | 1888 | IFA_UNLOCK(ifa); |
9bccf70c | 1889 | continue; |
6d2010ae A |
1890 | } else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && |
1891 | allow_deprecated != 0) { | |
1892 | IFA_UNLOCK(ifa); | |
9bccf70c | 1893 | continue; /* we now collect deprecated addrs */ |
6d2010ae | 1894 | } |
1c79356b | 1895 | /* What do we have to do about ::1? */ |
9bccf70c A |
1896 | switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { |
1897 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
6d2010ae A |
1898 | if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) { |
1899 | IFA_UNLOCK(ifa); | |
9bccf70c | 1900 | continue; |
6d2010ae | 1901 | } |
9bccf70c A |
1902 | break; |
1903 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
6d2010ae A |
1904 | if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) { |
1905 | IFA_UNLOCK(ifa); | |
9bccf70c | 1906 | continue; |
6d2010ae | 1907 | } |
1c79356b | 1908 | break; |
9bccf70c | 1909 | case IPV6_ADDR_SCOPE_GLOBAL: |
6d2010ae A |
1910 | if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) { |
1911 | IFA_UNLOCK(ifa); | |
9bccf70c | 1912 | continue; |
6d2010ae | 1913 | } |
1c79356b | 1914 | break; |
9bccf70c | 1915 | default: |
6d2010ae | 1916 | IFA_UNLOCK(ifa); |
9bccf70c | 1917 | continue; |
1c79356b A |
1918 | } |
1919 | ||
9bccf70c A |
1920 | /* |
1921 | * check if anycast is okay. | |
6d2010ae | 1922 | * XXX: just experimental. not in the spec. |
9bccf70c A |
1923 | */ |
1924 | if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && | |
6d2010ae A |
1925 | (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) { |
1926 | IFA_UNLOCK(ifa); | |
9bccf70c | 1927 | continue; |
6d2010ae | 1928 | } |
9bccf70c | 1929 | if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && |
6d2010ae A |
1930 | (icmp6_nodeinfo & ICMP6_NODEINFO_TMPADDROK) == 0) { |
1931 | IFA_UNLOCK(ifa); | |
9bccf70c A |
1932 | continue; |
1933 | } | |
1934 | ||
1935 | /* now we can copy the address */ | |
1936 | if (resid < sizeof(struct in6_addr) + | |
1937 | sizeof(u_int32_t)) { | |
6d2010ae | 1938 | IFA_UNLOCK(ifa); |
9bccf70c A |
1939 | /* |
1940 | * We give up much more copy. | |
1941 | * Set the truncate flag and return. | |
1942 | */ | |
1943 | nni6->ni_flags |= | |
1944 | NI_NODEADDR_FLAG_TRUNCATE; | |
91447636 A |
1945 | ifnet_lock_done(ifp); |
1946 | ifnet_head_done(); | |
9bccf70c A |
1947 | return(copied); |
1948 | } | |
1949 | ||
1950 | /* | |
1951 | * Set the TTL of the address. | |
1952 | * The TTL value should be one of the following | |
1953 | * according to the specification: | |
1954 | * | |
1955 | * 1. The remaining lifetime of a DHCP lease on the | |
1956 | * address, or | |
1957 | * 2. The remaining Valid Lifetime of a prefix from | |
1958 | * which the address was derived through Stateless | |
1959 | * Autoconfiguration. | |
1960 | * | |
1961 | * Note that we currently do not support stateful | |
1962 | * address configuration by DHCPv6, so the former | |
1963 | * case can't happen. | |
1964 | */ | |
39236c6e A |
1965 | lt = &ifa6->ia6_lifetime; |
1966 | if (lt->ia6ti_expire == 0) { | |
9bccf70c | 1967 | ltime = ND6_INFINITE_LIFETIME; |
39236c6e A |
1968 | } else { |
1969 | if (lt->ia6ti_expire > now) | |
1970 | ltime = htonl(lt->ia6ti_expire - now); | |
9bccf70c A |
1971 | else |
1972 | ltime = 0; | |
1c79356b | 1973 | } |
6d2010ae | 1974 | |
9bccf70c A |
1975 | bcopy(<ime, cp, sizeof(u_int32_t)); |
1976 | cp += sizeof(u_int32_t); | |
1977 | ||
1978 | /* copy the address itself */ | |
1979 | bcopy(&ifa6->ia_addr.sin6_addr, cp, | |
1980 | sizeof(struct in6_addr)); | |
1981 | /* XXX: KAME link-local hack; remove ifindex */ | |
1982 | if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) | |
316670eb | 1983 | ((struct in6_addr *)(void *)cp)->s6_addr16[1] = 0; |
9bccf70c | 1984 | cp += sizeof(struct in6_addr); |
6d2010ae | 1985 | |
9bccf70c A |
1986 | resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); |
1987 | copied += (sizeof(struct in6_addr) + | |
1988 | sizeof(u_int32_t)); | |
6d2010ae | 1989 | IFA_UNLOCK(ifa); |
1c79356b | 1990 | } |
91447636 | 1991 | ifnet_lock_done(ifp); |
1c79356b A |
1992 | if (ifp0) /* we need search only on the specified IF */ |
1993 | break; | |
1994 | } | |
91447636 | 1995 | ifnet_head_done(); |
1c79356b | 1996 | |
9bccf70c A |
1997 | if (allow_deprecated == 0 && ifp_dep != NULL) { |
1998 | ifp = ifp_dep; | |
1999 | allow_deprecated = 1; | |
2000 | ||
2001 | goto again; | |
2002 | } | |
2003 | ||
1c79356b A |
2004 | return(copied); |
2005 | } | |
2006 | ||
1c79356b A |
2007 | /* |
2008 | * XXX almost dup'ed code with rip6_input. | |
2009 | */ | |
2010 | static int | |
39037602 | 2011 | icmp6_rip6_input(struct mbuf **mp, int off) |
1c79356b A |
2012 | { |
2013 | struct mbuf *m = *mp; | |
9bccf70c A |
2014 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); |
2015 | struct in6pcb *in6p; | |
1c79356b A |
2016 | struct in6pcb *last = NULL; |
2017 | struct sockaddr_in6 rip6src; | |
2018 | struct icmp6_hdr *icmp6; | |
9bccf70c | 2019 | struct mbuf *opts = NULL; |
6d2010ae | 2020 | int ret = 0; |
39236c6e | 2021 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
1c79356b A |
2022 | |
2023 | #ifndef PULLDOWN_TEST | |
2024 | /* this is assumed to be safe. */ | |
2025 | icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); | |
2026 | #else | |
2027 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); | |
2028 | if (icmp6 == NULL) { | |
2029 | /* m is already reclaimed */ | |
2030 | return IPPROTO_DONE; | |
2031 | } | |
2032 | #endif | |
2033 | ||
6d2010ae A |
2034 | /* |
2035 | * XXX: the address may have embedded scope zone ID, which should be | |
2036 | * hidden from applications. | |
2037 | */ | |
1c79356b | 2038 | bzero(&rip6src, sizeof(rip6src)); |
1c79356b | 2039 | rip6src.sin6_family = AF_INET6; |
6d2010ae A |
2040 | rip6src.sin6_len = sizeof(struct sockaddr_in6); |
2041 | rip6src.sin6_addr = ip6->ip6_src; | |
743345f9 | 2042 | if (sa6_recoverscope(&rip6src, TRUE)) |
6d2010ae | 2043 | return (IPPROTO_DONE); |
316670eb | 2044 | |
39236c6e | 2045 | lck_rw_lock_shared(ripcbinfo.ipi_lock); |
1c79356b | 2046 | LIST_FOREACH(in6p, &ripcb, inp_list) |
1c79356b | 2047 | { |
9bccf70c A |
2048 | if ((in6p->inp_vflag & INP_IPV6) == 0) |
2049 | continue; | |
1c79356b A |
2050 | if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) |
2051 | continue; | |
2052 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && | |
2053 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) | |
2054 | continue; | |
2055 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && | |
2056 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) | |
2057 | continue; | |
2058 | if (in6p->in6p_icmp6filt | |
2059 | && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, | |
2060 | in6p->in6p_icmp6filt)) | |
2061 | continue; | |
39236c6e | 2062 | |
fe8ab488 | 2063 | if (inp_restricted_recv(in6p, ifp)) |
39236c6e A |
2064 | continue; |
2065 | ||
1c79356b A |
2066 | if (last) { |
2067 | struct mbuf *n; | |
2068 | if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { | |
39236c6e | 2069 | if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || |
6d2010ae A |
2070 | (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || |
2071 | (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
2072 | ret = ip6_savecontrol(last, n, &opts); | |
2073 | if (ret != 0) { | |
2074 | m_freem(n); | |
2075 | m_freem(opts); | |
2076 | last = in6p; | |
2077 | continue; | |
2078 | } | |
2079 | } | |
1c79356b A |
2080 | /* strip intermediate headers */ |
2081 | m_adj(n, off); | |
6d2010ae | 2082 | so_recv_data_stat(last->in6p_socket, m, 0); |
1c79356b A |
2083 | if (sbappendaddr(&last->in6p_socket->so_rcv, |
2084 | (struct sockaddr *)&rip6src, | |
91447636 | 2085 | n, opts, NULL) != 0) { |
1c79356b | 2086 | sorwakeup(last->in6p_socket); |
91447636 | 2087 | } |
9bccf70c | 2088 | opts = NULL; |
1c79356b A |
2089 | } |
2090 | } | |
2091 | last = in6p; | |
2092 | } | |
2093 | if (last) { | |
6d2010ae A |
2094 | if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || |
2095 | (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || | |
2096 | (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
2097 | ret = ip6_savecontrol(last, m, &opts); | |
2098 | if (ret != 0) { | |
2099 | goto error; | |
2100 | } | |
2101 | } | |
1c79356b A |
2102 | /* strip intermediate headers */ |
2103 | m_adj(m, off); | |
6d2010ae | 2104 | so_recv_data_stat(last->in6p_socket, m, 0); |
1c79356b | 2105 | if (sbappendaddr(&last->in6p_socket->so_rcv, |
91447636 | 2106 | (struct sockaddr *)&rip6src, m, opts, NULL) != 0) { |
1c79356b | 2107 | sorwakeup(last->in6p_socket); |
91447636 | 2108 | } |
1c79356b | 2109 | } else { |
6d2010ae | 2110 | goto error; |
1c79356b | 2111 | } |
39236c6e | 2112 | lck_rw_done(ripcbinfo.ipi_lock); |
1c79356b | 2113 | return IPPROTO_DONE; |
6d2010ae A |
2114 | |
2115 | error: | |
39236c6e | 2116 | lck_rw_done(ripcbinfo.ipi_lock); |
6d2010ae A |
2117 | m_freem(m); |
2118 | m_freem(opts); | |
2119 | ip6stat.ip6s_delivered--; | |
316670eb | 2120 | return IPPROTO_DONE; |
1c79356b | 2121 | } |
1c79356b A |
2122 | |
2123 | /* | |
2124 | * Reflect the ip6 packet back to the source. | |
2125 | * OFF points to the icmp6 header, counted from the top of the mbuf. | |
2126 | */ | |
2127 | void | |
39037602 | 2128 | icmp6_reflect(struct mbuf *m, size_t off) |
1c79356b | 2129 | { |
39236c6e | 2130 | struct mbuf *m_ip6hdr = m; |
1c79356b A |
2131 | struct ip6_hdr *ip6; |
2132 | struct icmp6_hdr *icmp6; | |
2133 | struct in6_ifaddr *ia; | |
91447636 | 2134 | struct in6_addr t, src_storage, *src = 0; |
1c79356b A |
2135 | int plen; |
2136 | int type, code; | |
2137 | struct ifnet *outif = NULL; | |
9bccf70c | 2138 | struct sockaddr_in6 sa6_src, sa6_dst; |
3e170ce0 | 2139 | struct nd_ifinfo *ndi = NULL; |
b0d623f7 | 2140 | u_int32_t oflow; |
39236c6e | 2141 | struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 }, |
743345f9 A |
2142 | IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR | |
2143 | IP6OAF_INTCOPROC_ALLOWED | IP6OAF_AWDL_UNRESTRICTED, 0, | |
39037602 | 2144 | SO_TC_UNSPEC, _NET_SERVICE_TYPE_UNSPEC }; |
6d2010ae | 2145 | |
39236c6e | 2146 | if (!(m->m_pkthdr.pkt_flags & PKTF_LOOP) && m->m_pkthdr.rcvif != NULL) { |
6d2010ae | 2147 | ip6oa.ip6oa_boundif = m->m_pkthdr.rcvif->if_index; |
316670eb A |
2148 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; |
2149 | } | |
1c79356b A |
2150 | |
2151 | /* too short to reflect */ | |
2152 | if (off < sizeof(struct ip6_hdr)) { | |
9bccf70c A |
2153 | nd6log((LOG_DEBUG, |
2154 | "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", | |
b0d623f7 | 2155 | (u_int32_t)off, (u_int32_t)sizeof(struct ip6_hdr), |
5ba3f43e | 2156 | __func__, __LINE__)); |
1c79356b A |
2157 | goto bad; |
2158 | } | |
2159 | ||
2160 | /* | |
2161 | * If there are extra headers between IPv6 and ICMPv6, strip | |
2162 | * off that header first. | |
2163 | */ | |
2164 | if (off > sizeof(struct ip6_hdr)) { | |
2165 | size_t l; | |
2166 | struct ip6_hdr nip6; | |
2167 | ||
2168 | l = off - sizeof(struct ip6_hdr); | |
2169 | m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); | |
2170 | m_adj(m, l); | |
2171 | l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
2172 | if (m->m_len < l) { | |
39236c6e | 2173 | if ((m_ip6hdr = m_pulldown(m, 0, l, NULL)) == NULL) |
1c79356b A |
2174 | return; |
2175 | } | |
2176 | bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); | |
2177 | } else /* off == sizeof(struct ip6_hdr) */ { | |
2178 | size_t l; | |
2179 | l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
2180 | if (m->m_len < l) { | |
39236c6e | 2181 | if ((m_ip6hdr = m_pulldown(m, 0, l, NULL)) == NULL) |
1c79356b A |
2182 | return; |
2183 | } | |
2184 | } | |
2185 | plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); | |
39236c6e | 2186 | ip6 = mtod(m_ip6hdr, struct ip6_hdr *); |
1c79356b A |
2187 | ip6->ip6_nxt = IPPROTO_ICMPV6; |
2188 | icmp6 = (struct icmp6_hdr *)(ip6 + 1); | |
2189 | type = icmp6->icmp6_type; /* keep type for statistics */ | |
2190 | code = icmp6->icmp6_code; /* ditto. */ | |
2191 | ||
2192 | t = ip6->ip6_dst; | |
2193 | /* | |
2194 | * ip6_input() drops a packet if its src is multicast. | |
2195 | * So, the src is never multicast. | |
2196 | */ | |
2197 | ip6->ip6_dst = ip6->ip6_src; | |
2198 | ||
9bccf70c A |
2199 | /* |
2200 | * XXX: make sure to embed scope zone information, using | |
2201 | * already embedded IDs or the received interface (if any). | |
2202 | * Note that rcvif may be NULL. | |
9bccf70c A |
2203 | */ |
2204 | bzero(&sa6_src, sizeof(sa6_src)); | |
2205 | sa6_src.sin6_family = AF_INET6; | |
2206 | sa6_src.sin6_len = sizeof(sa6_src); | |
2207 | sa6_src.sin6_addr = ip6->ip6_dst; | |
2208 | in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); | |
6d2010ae | 2209 | in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL, NULL); |
9bccf70c A |
2210 | bzero(&sa6_dst, sizeof(sa6_dst)); |
2211 | sa6_dst.sin6_family = AF_INET6; | |
2212 | sa6_dst.sin6_len = sizeof(sa6_dst); | |
2213 | sa6_dst.sin6_addr = t; | |
2214 | in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); | |
6d2010ae | 2215 | in6_embedscope(&t, &sa6_dst, NULL, NULL, NULL); |
1c79356b | 2216 | |
1c79356b A |
2217 | /* |
2218 | * If the incoming packet was addressed directly to us(i.e. unicast), | |
2219 | * use dst as the src for the reply. | |
6d2010ae | 2220 | * The IN6_IFF_NOTREADY case should be VERY rare, but is possible |
1c79356b A |
2221 | * (for example) when we encounter an error while forwarding procedure |
2222 | * destined to a duplicated address of ours. | |
6d2010ae A |
2223 | * Note that ip6_getdstifaddr() may fail if we are in an error handling |
2224 | * procedure of an outgoing packet of our own, in which case we need | |
2225 | * to search in the ifaddr list. | |
1c79356b | 2226 | */ |
6d2010ae A |
2227 | lck_rw_lock_shared(&in6_ifaddr_rwlock); |
2228 | for (ia = in6_ifaddrs; ia; ia = ia->ia_next) { | |
2229 | IFA_LOCK(&ia->ia_ifa); | |
1c79356b A |
2230 | if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && |
2231 | (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { | |
6d2010ae | 2232 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b A |
2233 | src = &t; |
2234 | break; | |
2235 | } | |
6d2010ae A |
2236 | IFA_UNLOCK(&ia->ia_ifa); |
2237 | } | |
2238 | lck_rw_done(&in6_ifaddr_rwlock); | |
39236c6e A |
2239 | if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && |
2240 | ((m->m_flags & M_LOOP) || (m->m_pkthdr.pkt_flags & PKTF_LOOP))) { | |
1c79356b A |
2241 | /* |
2242 | * This is the case if the dst is our link-local address | |
39236c6e A |
2243 | * and the sender is also ourselves. Here we test for both |
2244 | * M_LOOP and PKTF_LOOP, since the former may have been set | |
2245 | * in ip6_output() and that we get here as part of callling | |
2246 | * ip6_process_hopopts(). See comments in <sys/mbuf.h> | |
1c79356b A |
2247 | */ |
2248 | src = &t; | |
2249 | } | |
2250 | ||
6d2010ae | 2251 | if (src == NULL) { |
9bccf70c | 2252 | int e; |
6d2010ae | 2253 | struct sockaddr_in6 sin6; |
9bccf70c A |
2254 | struct route_in6 ro; |
2255 | ||
1c79356b A |
2256 | /* |
2257 | * This case matches to multicasts, our anycast, or unicasts | |
55e303ae | 2258 | * that we do not own. Select a source address based on the |
9bccf70c | 2259 | * source address of the erroneous packet. |
1c79356b | 2260 | */ |
6d2010ae A |
2261 | bzero(&sin6, sizeof(sin6)); |
2262 | sin6.sin6_family = AF_INET6; | |
2263 | sin6.sin6_len = sizeof(sin6); | |
2264 | sin6.sin6_addr = ip6->ip6_dst; /* zone ID should be embedded */ | |
2265 | ||
9bccf70c | 2266 | bzero(&ro, sizeof(ro)); |
316670eb A |
2267 | /* |
2268 | * in6_selectsrc() might return outif with its reference held | |
2269 | * even in the error case, so we always need to release it | |
2270 | * if non-NULL. | |
2271 | */ | |
6d2010ae A |
2272 | src = in6_selectsrc(&sin6, NULL, NULL, &ro, &outif, |
2273 | &src_storage, ip6oa.ip6oa_boundif, &e); | |
39236c6e | 2274 | ROUTE_RELEASE(&ro); |
9bccf70c A |
2275 | if (src == NULL) { |
2276 | nd6log((LOG_DEBUG, | |
2277 | "icmp6_reflect: source can't be determined: " | |
2278 | "dst=%s, error=%d\n", | |
2279 | ip6_sprintf(&sa6_src.sin6_addr), e)); | |
2280 | goto bad; | |
2281 | } | |
2282 | } | |
b0d623f7 | 2283 | oflow = ip6->ip6_flow; /* Save for later */ |
6d2010ae | 2284 | ip6->ip6_src = *src; |
1c79356b A |
2285 | ip6->ip6_flow = 0; |
2286 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
2287 | ip6->ip6_vfc |= IPV6_VERSION; | |
b0d623f7 A |
2288 | if (icmp6->icmp6_type == ICMP6_ECHO_REPLY && icmp6->icmp6_code == 0) { |
2289 | ip6->ip6_flow |= (oflow & htonl(0x0ff00000)); | |
2290 | } | |
1c79356b | 2291 | ip6->ip6_nxt = IPPROTO_ICMPV6; |
316670eb A |
2292 | if (outif != NULL && (ndi = ND_IFINFO(outif)) != NULL && |
2293 | ndi->initialized) { | |
2294 | lck_mtx_lock(&ndi->lock); | |
2295 | ip6->ip6_hlim = ndi->chlim; | |
2296 | lck_mtx_unlock(&ndi->lock); | |
2297 | } | |
2298 | if (m->m_pkthdr.rcvif != NULL && | |
2299 | (ndi = ND_IFINFO(m->m_pkthdr.rcvif)) != NULL && | |
2300 | ndi->initialized) { | |
1c79356b | 2301 | /* XXX: This may not be the outgoing interface */ |
316670eb A |
2302 | lck_mtx_lock(&ndi->lock); |
2303 | ip6->ip6_hlim = ndi->chlim; | |
2304 | lck_mtx_unlock(&ndi->lock); | |
b0d623f7 | 2305 | } else { |
9bccf70c | 2306 | ip6->ip6_hlim = ip6_defhlim; |
b0d623f7 A |
2307 | } |
2308 | /* Use the same traffic class as in the request to match IPv4 */ | |
1c79356b A |
2309 | icmp6->icmp6_cksum = 0; |
2310 | icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, | |
3e170ce0 | 2311 | sizeof(struct ip6_hdr), plen); |
1c79356b A |
2312 | |
2313 | /* | |
9bccf70c | 2314 | * XXX option handling |
1c79356b | 2315 | */ |
1c79356b | 2316 | m->m_flags &= ~(M_BCAST|M_MCAST); |
1c79356b | 2317 | |
6d2010ae A |
2318 | if (outif != NULL) { |
2319 | ifnet_release(outif); | |
2320 | outif = NULL; | |
2321 | } | |
3e170ce0 | 2322 | |
316670eb A |
2323 | m->m_pkthdr.csum_data = 0; |
2324 | m->m_pkthdr.csum_flags = 0; | |
6d2010ae A |
2325 | ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa); |
2326 | if (outif != NULL) { | |
1c79356b | 2327 | icmp6_ifoutstat_inc(outif, type, code); |
6d2010ae A |
2328 | ifnet_release(outif); |
2329 | } | |
1c79356b A |
2330 | return; |
2331 | ||
6d2010ae | 2332 | bad: |
1c79356b | 2333 | m_freem(m); |
6d2010ae A |
2334 | if (outif != NULL) |
2335 | ifnet_release(outif); | |
1c79356b A |
2336 | return; |
2337 | } | |
2338 | ||
1c79356b | 2339 | static const char * |
39037602 A |
2340 | icmp6_redirect_diag(struct in6_addr *src6, |
2341 | struct in6_addr *dst6, | |
2342 | struct in6_addr *tgt6) | |
1c79356b A |
2343 | { |
2344 | static char buf[1024]; | |
1c79356b A |
2345 | snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", |
2346 | ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); | |
1c79356b A |
2347 | return buf; |
2348 | } | |
2349 | ||
2350 | void | |
39037602 | 2351 | icmp6_redirect_input(struct mbuf *m, int off) |
1c79356b A |
2352 | { |
2353 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
2354 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
2355 | struct nd_redirect *nd_rd; | |
2356 | int icmp6len = ntohs(ip6->ip6_plen); | |
2357 | char *lladdr = NULL; | |
2358 | int lladdrlen = 0; | |
2359 | u_char *redirhdr = NULL; | |
2360 | int redirhdrlen = 0; | |
2361 | struct rtentry *rt = NULL; | |
2362 | int is_router; | |
2363 | int is_onlink; | |
2364 | struct in6_addr src6 = ip6->ip6_src; | |
2365 | struct in6_addr redtgt6; | |
2366 | struct in6_addr reddst6; | |
2367 | union nd_opts ndopts; | |
2368 | ||
2369 | if (!m || !ifp) | |
2370 | return; | |
2371 | ||
316670eb A |
2372 | /* |
2373 | * If we are an advertising router on this interface, | |
2374 | * don't update route by icmp6 redirect. | |
2375 | */ | |
2376 | if (ifp->if_eflags & IFEF_IPV6_ROUTER) | |
1c79356b A |
2377 | goto freeit; |
2378 | if (!icmp6_rediraccept) | |
2379 | goto freeit; | |
2380 | ||
2381 | #ifndef PULLDOWN_TEST | |
91447636 | 2382 | IP6_EXTHDR_CHECK(m, off, icmp6len, return); |
1c79356b A |
2383 | nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); |
2384 | #else | |
2385 | IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); | |
2386 | if (nd_rd == NULL) { | |
2387 | icmp6stat.icp6s_tooshort++; | |
2388 | return; | |
2389 | } | |
2390 | #endif | |
2391 | redtgt6 = nd_rd->nd_rd_target; | |
2392 | reddst6 = nd_rd->nd_rd_dst; | |
2393 | ||
6d2010ae A |
2394 | if (in6_setscope(&redtgt6, m->m_pkthdr.rcvif, NULL) || |
2395 | in6_setscope(&reddst6, m->m_pkthdr.rcvif, NULL)) { | |
2396 | goto freeit; | |
2397 | } | |
1c79356b A |
2398 | |
2399 | /* validation */ | |
2400 | if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { | |
9bccf70c | 2401 | nd6log((LOG_ERR, |
1c79356b | 2402 | "ICMP6 redirect sent from %s rejected; " |
9bccf70c A |
2403 | "must be from linklocal\n", ip6_sprintf(&src6))); |
2404 | goto bad; | |
1c79356b A |
2405 | } |
2406 | if (ip6->ip6_hlim != 255) { | |
9bccf70c | 2407 | nd6log((LOG_ERR, |
1c79356b A |
2408 | "ICMP6 redirect sent from %s rejected; " |
2409 | "hlim=%d (must be 255)\n", | |
9bccf70c A |
2410 | ip6_sprintf(&src6), ip6->ip6_hlim)); |
2411 | goto bad; | |
1c79356b A |
2412 | } |
2413 | { | |
2414 | /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ | |
2415 | struct sockaddr_in6 sin6; | |
2416 | struct in6_addr *gw6; | |
2417 | ||
2418 | bzero(&sin6, sizeof(sin6)); | |
2419 | sin6.sin6_family = AF_INET6; | |
2420 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
2421 | bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); | |
6d2010ae | 2422 | rt = rtalloc1_scoped((struct sockaddr *)&sin6, 0, 0, ifp->if_index); |
1c79356b | 2423 | if (rt) { |
b0d623f7 | 2424 | RT_LOCK(rt); |
9bccf70c A |
2425 | if (rt->rt_gateway == NULL || |
2426 | rt->rt_gateway->sa_family != AF_INET6) { | |
2427 | nd6log((LOG_ERR, | |
2428 | "ICMP6 redirect rejected; no route " | |
2429 | "with inet6 gateway found for redirect dst: %s\n", | |
2430 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); | |
b0d623f7 | 2431 | RT_UNLOCK(rt); |
9bccf70c A |
2432 | rtfree(rt); |
2433 | goto bad; | |
2434 | } | |
2435 | ||
316670eb A |
2436 | gw6 = &(((struct sockaddr_in6 *)(void *) |
2437 | rt->rt_gateway)->sin6_addr); | |
1c79356b | 2438 | if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { |
9bccf70c | 2439 | nd6log((LOG_ERR, |
1c79356b A |
2440 | "ICMP6 redirect rejected; " |
2441 | "not equal to gw-for-src=%s (must be same): " | |
2442 | "%s\n", | |
2443 | ip6_sprintf(gw6), | |
9bccf70c | 2444 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
b0d623f7 | 2445 | RT_UNLOCK(rt); |
9bccf70c A |
2446 | rtfree(rt); |
2447 | goto bad; | |
1c79356b A |
2448 | } |
2449 | } else { | |
9bccf70c | 2450 | nd6log((LOG_ERR, |
1c79356b A |
2451 | "ICMP6 redirect rejected; " |
2452 | "no route found for redirect dst: %s\n", | |
9bccf70c A |
2453 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2454 | goto bad; | |
1c79356b | 2455 | } |
b0d623f7 | 2456 | RT_UNLOCK(rt); |
9bccf70c | 2457 | rtfree(rt); |
1c79356b A |
2458 | rt = NULL; |
2459 | } | |
2460 | if (IN6_IS_ADDR_MULTICAST(&reddst6)) { | |
9bccf70c | 2461 | nd6log((LOG_ERR, |
1c79356b A |
2462 | "ICMP6 redirect rejected; " |
2463 | "redirect dst must be unicast: %s\n", | |
9bccf70c A |
2464 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2465 | goto bad; | |
1c79356b A |
2466 | } |
2467 | ||
2468 | is_router = is_onlink = 0; | |
2469 | if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) | |
2470 | is_router = 1; /* router case */ | |
2471 | if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) | |
2472 | is_onlink = 1; /* on-link destination case */ | |
2473 | if (!is_router && !is_onlink) { | |
9bccf70c | 2474 | nd6log((LOG_ERR, |
1c79356b A |
2475 | "ICMP6 redirect rejected; " |
2476 | "neither router case nor onlink case: %s\n", | |
9bccf70c A |
2477 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2478 | goto bad; | |
1c79356b A |
2479 | } |
2480 | /* validation passed */ | |
2481 | ||
2482 | icmp6len -= sizeof(*nd_rd); | |
2483 | nd6_option_init(nd_rd + 1, icmp6len, &ndopts); | |
2484 | if (nd6_options(&ndopts) < 0) { | |
9bccf70c | 2485 | nd6log((LOG_INFO, "icmp6_redirect_input: " |
1c79356b | 2486 | "invalid ND option, rejected: %s\n", |
9bccf70c A |
2487 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2488 | /* nd6_options have incremented stats */ | |
1c79356b A |
2489 | goto freeit; |
2490 | } | |
2491 | ||
2492 | if (ndopts.nd_opts_tgt_lladdr) { | |
2493 | lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); | |
2494 | lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; | |
2495 | } | |
2496 | ||
2497 | if (ndopts.nd_opts_rh) { | |
2498 | redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; | |
2499 | redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ | |
2500 | } | |
2501 | ||
2502 | if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { | |
9bccf70c | 2503 | nd6log((LOG_INFO, |
1c79356b A |
2504 | "icmp6_redirect_input: lladdrlen mismatch for %s " |
2505 | "(if %d, icmp6 packet %d): %s\n", | |
2506 | ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, | |
9bccf70c A |
2507 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2508 | goto bad; | |
1c79356b A |
2509 | } |
2510 | ||
2511 | /* RFC 2461 8.3 */ | |
2512 | nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, | |
2513 | is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); | |
2514 | ||
55e303ae | 2515 | if (!is_onlink) { /* better router case. perform rtredirect. */ |
1c79356b A |
2516 | /* perform rtredirect */ |
2517 | struct sockaddr_in6 sdst; | |
2518 | struct sockaddr_in6 sgw; | |
2519 | struct sockaddr_in6 ssrc; | |
1c79356b A |
2520 | |
2521 | bzero(&sdst, sizeof(sdst)); | |
2522 | bzero(&sgw, sizeof(sgw)); | |
2523 | bzero(&ssrc, sizeof(ssrc)); | |
2524 | sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; | |
2525 | sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = | |
2526 | sizeof(struct sockaddr_in6); | |
2527 | bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); | |
2528 | bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); | |
2529 | bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); | |
c910b4d9 A |
2530 | rtredirect(ifp, (struct sockaddr *)&sdst, |
2531 | (struct sockaddr *)&sgw, NULL, RTF_GATEWAY | RTF_HOST, | |
2532 | (struct sockaddr *)&ssrc, NULL); | |
1c79356b A |
2533 | } |
2534 | /* finally update cached route in each socket via pfctlinput */ | |
2535 | { | |
2536 | struct sockaddr_in6 sdst; | |
1c79356b A |
2537 | |
2538 | bzero(&sdst, sizeof(sdst)); | |
2539 | sdst.sin6_family = AF_INET6; | |
2540 | sdst.sin6_len = sizeof(struct sockaddr_in6); | |
2541 | bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); | |
316670eb | 2542 | |
1c79356b | 2543 | pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); |
1c79356b A |
2544 | #if IPSEC |
2545 | key_sa_routechange((struct sockaddr *)&sdst); | |
2546 | #endif | |
2547 | } | |
2548 | ||
2549 | freeit: | |
2550 | m_freem(m); | |
9bccf70c A |
2551 | return; |
2552 | ||
2553 | bad: | |
2554 | icmp6stat.icp6s_badredirect++; | |
2555 | m_freem(m); | |
1c79356b A |
2556 | } |
2557 | ||
2558 | void | |
39037602 | 2559 | icmp6_redirect_output(struct mbuf *m0, struct rtentry *rt) |
1c79356b A |
2560 | { |
2561 | struct ifnet *ifp; /* my outgoing interface */ | |
91447636 | 2562 | struct in6_addr ifp_ll6; |
1c79356b A |
2563 | struct in6_addr *router_ll6; |
2564 | struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ | |
2565 | struct mbuf *m = NULL; /* newly allocated one */ | |
2566 | struct ip6_hdr *ip6; /* m as struct ip6_hdr */ | |
2567 | struct nd_redirect *nd_rd; | |
2568 | size_t maxlen; | |
2569 | u_char *p; | |
2570 | struct ifnet *outif = NULL; | |
9bccf70c | 2571 | struct sockaddr_in6 src_sa; |
39236c6e | 2572 | struct ip6_out_args ip6oa = { IFSCOPE_NONE, { 0 }, |
39037602 A |
2573 | IP6OAF_SELECT_SRCIF | IP6OAF_BOUND_SRCADDR, 0, |
2574 | SO_TC_UNSPEC, _NET_SERVICE_TYPE_UNSPEC }; | |
9bccf70c A |
2575 | |
2576 | icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); | |
1c79356b | 2577 | |
b0d623f7 A |
2578 | if (rt != NULL) |
2579 | RT_LOCK(rt); | |
2580 | ||
1c79356b A |
2581 | /* sanity check */ |
2582 | if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) | |
2583 | goto fail; | |
2584 | ||
316670eb A |
2585 | /* |
2586 | * If we are not a router to begin with, or not an advertising | |
2587 | * router on this interface, don't send icmp6 redirect. | |
2588 | */ | |
2589 | if (!ip6_forwarding || !(ifp->if_eflags & IFEF_IPV6_ROUTER)) | |
55e303ae A |
2590 | goto fail; |
2591 | ||
1c79356b A |
2592 | /* |
2593 | * Address check: | |
2594 | * the source address must identify a neighbor, and | |
2595 | * the destination address must not be a multicast address | |
2596 | * [RFC 2461, sec 8.2] | |
2597 | */ | |
2598 | sip6 = mtod(m0, struct ip6_hdr *); | |
9bccf70c A |
2599 | bzero(&src_sa, sizeof(src_sa)); |
2600 | src_sa.sin6_family = AF_INET6; | |
2601 | src_sa.sin6_len = sizeof(src_sa); | |
2602 | src_sa.sin6_addr = sip6->ip6_src; | |
2603 | /* we don't currently use sin6_scope_id, but eventually use it */ | |
2604 | src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src); | |
b0d623f7 A |
2605 | RT_UNLOCK(rt); |
2606 | if (nd6_is_addr_neighbor(&src_sa, ifp, 0) == 0) { | |
2607 | /* already unlocked */ | |
2608 | rt = NULL; | |
1c79356b | 2609 | goto fail; |
b0d623f7 A |
2610 | } |
2611 | RT_LOCK(rt); | |
1c79356b A |
2612 | if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) |
2613 | goto fail; /* what should we do here? */ | |
2614 | ||
2615 | /* rate limit */ | |
2616 | if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) | |
2617 | goto fail; | |
2618 | ||
2619 | /* | |
2620 | * Since we are going to append up to 1280 bytes (= IPV6_MMTU), | |
2621 | * we almost always ask for an mbuf cluster for simplicity. | |
2622 | * (MHLEN < IPV6_MMTU is almost always true) | |
2623 | */ | |
2624 | #if IPV6_MMTU >= MCLBYTES | |
2625 | # error assumption failed about IPV6_MMTU and MCLBYTES | |
2626 | #endif | |
2d21ac55 | 2627 | MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */ |
1c79356b A |
2628 | if (m && IPV6_MMTU >= MHLEN) |
2629 | MCLGET(m, M_DONTWAIT); | |
2630 | if (!m) | |
2631 | goto fail; | |
9bccf70c A |
2632 | m->m_pkthdr.rcvif = NULL; |
2633 | m->m_len = 0; | |
2634 | maxlen = M_TRAILINGSPACE(m); | |
1c79356b A |
2635 | maxlen = min(IPV6_MMTU, maxlen); |
2636 | /* just for safety */ | |
2637 | if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + | |
2638 | ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { | |
2639 | goto fail; | |
2640 | } | |
2641 | ||
2642 | { | |
2643 | /* get ip6 linklocal address for ifp(my outgoing interface). */ | |
2644 | struct in6_ifaddr *ia; | |
2645 | if ((ia = in6ifa_ifpforlinklocal(ifp, | |
2646 | IN6_IFF_NOTREADY| | |
2647 | IN6_IFF_ANYCAST)) == NULL) | |
2648 | goto fail; | |
6d2010ae | 2649 | IFA_LOCK(&ia->ia_ifa); |
91447636 | 2650 | ifp_ll6 = ia->ia_addr.sin6_addr; |
6d2010ae A |
2651 | IFA_UNLOCK(&ia->ia_ifa); |
2652 | IFA_REMREF(&ia->ia_ifa); | |
1c79356b A |
2653 | } |
2654 | ||
2655 | /* get ip6 linklocal address for the router. */ | |
2656 | if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { | |
2657 | struct sockaddr_in6 *sin6; | |
316670eb | 2658 | sin6 = (struct sockaddr_in6 *)(void *)rt->rt_gateway; |
1c79356b A |
2659 | router_ll6 = &sin6->sin6_addr; |
2660 | if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) | |
2661 | router_ll6 = (struct in6_addr *)NULL; | |
2662 | } else | |
2663 | router_ll6 = (struct in6_addr *)NULL; | |
2664 | ||
2665 | /* ip6 */ | |
2666 | ip6 = mtod(m, struct ip6_hdr *); | |
2667 | ip6->ip6_flow = 0; | |
2668 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
2669 | ip6->ip6_vfc |= IPV6_VERSION; | |
2670 | /* ip6->ip6_plen will be set later */ | |
2671 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
2672 | ip6->ip6_hlim = 255; | |
2673 | /* ip6->ip6_src must be linklocal addr for my outgoing if. */ | |
91447636 | 2674 | bcopy(&ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); |
1c79356b A |
2675 | bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); |
2676 | ||
2677 | /* ND Redirect */ | |
2678 | nd_rd = (struct nd_redirect *)(ip6 + 1); | |
2679 | nd_rd->nd_rd_type = ND_REDIRECT; | |
2680 | nd_rd->nd_rd_code = 0; | |
2681 | nd_rd->nd_rd_reserved = 0; | |
2682 | if (rt->rt_flags & RTF_GATEWAY) { | |
2683 | /* | |
2684 | * nd_rd->nd_rd_target must be a link-local address in | |
2685 | * better router cases. | |
2686 | */ | |
2687 | if (!router_ll6) | |
2688 | goto fail; | |
2689 | bcopy(router_ll6, &nd_rd->nd_rd_target, | |
2690 | sizeof(nd_rd->nd_rd_target)); | |
2691 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, | |
2692 | sizeof(nd_rd->nd_rd_dst)); | |
2693 | } else { | |
2694 | /* make sure redtgt == reddst */ | |
2695 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, | |
2696 | sizeof(nd_rd->nd_rd_target)); | |
2697 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, | |
2698 | sizeof(nd_rd->nd_rd_dst)); | |
2699 | } | |
b0d623f7 A |
2700 | RT_UNLOCK(rt); |
2701 | rt = NULL; | |
1c79356b A |
2702 | |
2703 | p = (u_char *)(nd_rd + 1); | |
2704 | ||
2705 | if (!router_ll6) | |
2706 | goto nolladdropt; | |
2707 | ||
6d2010ae A |
2708 | { |
2709 | /* target lladdr option */ | |
2710 | struct rtentry *rt_router = NULL; | |
2711 | int len; | |
2712 | struct sockaddr_dl *sdl; | |
2713 | struct nd_opt_hdr *nd_opt; | |
2714 | char *lladdr; | |
2715 | ||
2716 | /* Callee returns a locked route upon success */ | |
2717 | rt_router = nd6_lookup(router_ll6, 0, ifp, 0); | |
2718 | if (!rt_router) | |
2719 | goto nolladdropt; | |
2720 | RT_LOCK_ASSERT_HELD(rt_router); | |
2721 | len = sizeof(*nd_opt) + ifp->if_addrlen; | |
2722 | len = (len + 7) & ~7; /* round by 8 */ | |
2723 | /* safety check */ | |
2724 | if (len + (p - (u_char *)ip6) > maxlen) { | |
2725 | RT_REMREF_LOCKED(rt_router); | |
2726 | RT_UNLOCK(rt_router); | |
2727 | goto nolladdropt; | |
2728 | } | |
2729 | ||
2730 | if (!(rt_router->rt_flags & RTF_GATEWAY) && | |
2731 | (rt_router->rt_flags & RTF_LLINFO) && | |
2732 | (rt_router->rt_gateway->sa_family == AF_LINK) && | |
316670eb A |
2733 | (sdl = (struct sockaddr_dl *)(void *) |
2734 | rt_router->rt_gateway) && sdl->sdl_alen) { | |
6d2010ae A |
2735 | nd_opt = (struct nd_opt_hdr *)p; |
2736 | nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; | |
2737 | nd_opt->nd_opt_len = len >> 3; | |
2738 | lladdr = (char *)(nd_opt + 1); | |
2739 | bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); | |
2740 | p += len; | |
2741 | } | |
b0d623f7 A |
2742 | RT_REMREF_LOCKED(rt_router); |
2743 | RT_UNLOCK(rt_router); | |
316670eb | 2744 | } |
6d2010ae | 2745 | |
1c79356b A |
2746 | nolladdropt:; |
2747 | ||
2748 | m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; | |
2749 | ||
2750 | /* just to be safe */ | |
9bccf70c | 2751 | #ifdef M_DECRYPTED /*not openbsd*/ |
1c79356b A |
2752 | if (m0->m_flags & M_DECRYPTED) |
2753 | goto noredhdropt; | |
2754 | #endif | |
2755 | if (p - (u_char *)ip6 > maxlen) | |
2756 | goto noredhdropt; | |
2757 | ||
2758 | { | |
2759 | /* redirected header option */ | |
2760 | int len; | |
2761 | struct nd_opt_rd_hdr *nd_opt_rh; | |
2762 | ||
2763 | /* | |
2764 | * compute the maximum size for icmp6 redirect header option. | |
2765 | * XXX room for auth header? | |
2766 | */ | |
2767 | len = maxlen - (p - (u_char *)ip6); | |
2768 | len &= ~7; | |
2769 | ||
2770 | /* This is just for simplicity. */ | |
2771 | if (m0->m_pkthdr.len != m0->m_len) { | |
2772 | if (m0->m_next) { | |
2773 | m_freem(m0->m_next); | |
2774 | m0->m_next = NULL; | |
2775 | } | |
2776 | m0->m_pkthdr.len = m0->m_len; | |
2777 | } | |
2778 | ||
2779 | /* | |
2780 | * Redirected header option spec (RFC2461 4.6.3) talks nothing | |
2781 | * about padding/truncate rule for the original IP packet. | |
2782 | * From the discussion on IPv6imp in Feb 1999, the consensus was: | |
2783 | * - "attach as much as possible" is the goal | |
2784 | * - pad if not aligned (original size can be guessed by original | |
2785 | * ip6 header) | |
2786 | * Following code adds the padding if it is simple enough, | |
2787 | * and truncates if not. | |
2788 | */ | |
2789 | if (m0->m_next || m0->m_pkthdr.len != m0->m_len) | |
5ba3f43e | 2790 | panic("assumption failed in %s:%d\n", __func__, __LINE__); |
1c79356b A |
2791 | |
2792 | if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { | |
2793 | /* not enough room, truncate */ | |
2794 | m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); | |
2795 | } else { | |
2796 | /* enough room, pad or truncate */ | |
2797 | size_t extra; | |
2798 | ||
2799 | extra = m0->m_pkthdr.len % 8; | |
2800 | if (extra) { | |
2801 | /* pad if easy enough, truncate if not */ | |
2802 | if (8 - extra <= M_TRAILINGSPACE(m0)) { | |
2803 | /* pad */ | |
2804 | m0->m_len += (8 - extra); | |
2805 | m0->m_pkthdr.len += (8 - extra); | |
2806 | } else { | |
2807 | /* truncate */ | |
2808 | m0->m_pkthdr.len -= extra; | |
2809 | m0->m_len -= extra; | |
2810 | } | |
2811 | } | |
2812 | len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); | |
2813 | m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); | |
2814 | } | |
2815 | ||
2816 | nd_opt_rh = (struct nd_opt_rd_hdr *)p; | |
2817 | bzero(nd_opt_rh, sizeof(*nd_opt_rh)); | |
2818 | nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; | |
2819 | nd_opt_rh->nd_opt_rh_len = len >> 3; | |
2820 | p += sizeof(*nd_opt_rh); | |
2821 | m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; | |
2822 | ||
2823 | /* connect m0 to m */ | |
2824 | m->m_next = m0; | |
2825 | m->m_pkthdr.len = m->m_len + m0->m_len; | |
2826 | } | |
2827 | noredhdropt:; | |
2828 | ||
6d2010ae A |
2829 | /* XXX: clear embedded link IDs in the inner header */ |
2830 | in6_clearscope(&sip6->ip6_src); | |
2831 | in6_clearscope(&sip6->ip6_dst); | |
2832 | in6_clearscope(&nd_rd->nd_rd_target); | |
2833 | in6_clearscope(&nd_rd->nd_rd_dst); | |
1c79356b A |
2834 | |
2835 | ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); | |
2836 | ||
2837 | nd_rd->nd_rd_cksum = 0; | |
2838 | nd_rd->nd_rd_cksum | |
2839 | = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); | |
2840 | ||
2841 | /* send the packet to outside... */ | |
6d2010ae | 2842 | ip6oa.ip6oa_boundif = ifp->if_index; |
316670eb | 2843 | ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF; |
6d2010ae A |
2844 | |
2845 | ip6_output(m, NULL, NULL, IPV6_OUTARGS, NULL, &outif, &ip6oa); | |
1c79356b A |
2846 | if (outif) { |
2847 | icmp6_ifstat_inc(outif, ifs6_out_msg); | |
2848 | icmp6_ifstat_inc(outif, ifs6_out_redirect); | |
6d2010ae | 2849 | ifnet_release(outif); |
1c79356b A |
2850 | } |
2851 | icmp6stat.icp6s_outhist[ND_REDIRECT]++; | |
2852 | ||
2853 | return; | |
2854 | ||
2855 | fail: | |
b0d623f7 A |
2856 | if (rt != NULL) |
2857 | RT_UNLOCK(rt); | |
1c79356b A |
2858 | if (m) |
2859 | m_freem(m); | |
2860 | if (m0) | |
2861 | m_freem(m0); | |
2862 | } | |
2863 | ||
1c79356b A |
2864 | /* |
2865 | * ICMPv6 socket option processing. | |
1c79356b A |
2866 | */ |
2867 | int | |
39037602 | 2868 | icmp6_ctloutput(struct socket *so, struct sockopt *sopt) |
1c79356b A |
2869 | { |
2870 | int error = 0; | |
2871 | int optlen; | |
9bccf70c | 2872 | struct inpcb *inp = sotoinpcb(so); |
1c79356b A |
2873 | int level, op, optname; |
2874 | ||
2875 | if (sopt) { | |
2876 | level = sopt->sopt_level; | |
2877 | op = sopt->sopt_dir; | |
2878 | optname = sopt->sopt_name; | |
2879 | optlen = sopt->sopt_valsize; | |
2880 | } else | |
2881 | level = op = optname = optlen = 0; | |
1c79356b A |
2882 | |
2883 | if (level != IPPROTO_ICMPV6) { | |
1c79356b A |
2884 | return EINVAL; |
2885 | } | |
2886 | ||
9bccf70c | 2887 | switch (op) { |
1c79356b A |
2888 | case PRCO_SETOPT: |
2889 | switch (optname) { | |
2890 | case ICMP6_FILTER: | |
2891 | { | |
2892 | struct icmp6_filter *p; | |
2893 | ||
6d2010ae | 2894 | if (optlen != 0 && optlen != sizeof(*p)) { |
1c79356b A |
2895 | error = EMSGSIZE; |
2896 | break; | |
2897 | } | |
1c79356b A |
2898 | if (inp->in6p_icmp6filt == NULL) { |
2899 | error = EINVAL; | |
2900 | break; | |
2901 | } | |
6d2010ae A |
2902 | |
2903 | if (optlen == 0) { | |
743345f9 | 2904 | /* According to RFC 3542, an installed filter can be |
6d2010ae A |
2905 | * cleared by issuing a setsockopt for ICMP6_FILTER |
2906 | * with a zero length. | |
2907 | */ | |
2908 | ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); | |
2909 | } else { | |
2910 | error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, | |
2911 | optlen); | |
2912 | } | |
1c79356b A |
2913 | break; |
2914 | } | |
2915 | ||
2916 | default: | |
2917 | error = ENOPROTOOPT; | |
2918 | break; | |
2919 | } | |
1c79356b A |
2920 | break; |
2921 | ||
2922 | case PRCO_GETOPT: | |
2923 | switch (optname) { | |
2924 | case ICMP6_FILTER: | |
2925 | { | |
1c79356b A |
2926 | if (inp->in6p_icmp6filt == NULL) { |
2927 | error = EINVAL; | |
2928 | break; | |
2929 | } | |
2930 | error = sooptcopyout(sopt, inp->in6p_icmp6filt, | |
6d2010ae | 2931 | min(sizeof(struct icmp6_filter), optlen)); |
1c79356b A |
2932 | break; |
2933 | } | |
2934 | ||
2935 | default: | |
2936 | error = ENOPROTOOPT; | |
2937 | break; | |
2938 | } | |
2939 | break; | |
2940 | } | |
2941 | ||
2942 | return(error); | |
2943 | } | |
9bccf70c | 2944 | |
2d21ac55 A |
2945 | /* |
2946 | * ICMPv6 socket datagram option processing. | |
2947 | */ | |
2948 | int | |
2949 | icmp6_dgram_ctloutput(struct socket *so, struct sockopt *sopt) | |
2950 | { | |
316670eb | 2951 | if (kauth_cred_issuser(so->so_cred)) |
2d21ac55 A |
2952 | return icmp6_ctloutput(so, sopt); |
2953 | ||
2954 | if (sopt->sopt_level == IPPROTO_ICMPV6) { | |
2955 | switch (sopt->sopt_name) { | |
2956 | case ICMP6_FILTER: | |
2957 | return icmp6_ctloutput(so, sopt); | |
2958 | default: | |
2959 | return EPERM; | |
2960 | } | |
2961 | } | |
316670eb | 2962 | |
2d21ac55 A |
2963 | if (sopt->sopt_level != IPPROTO_IPV6) |
2964 | return EINVAL; | |
316670eb | 2965 | |
2d21ac55 | 2966 | switch (sopt->sopt_name) { |
2d21ac55 A |
2967 | case IPV6_UNICAST_HOPS: |
2968 | case IPV6_CHECKSUM: | |
2d21ac55 | 2969 | case IPV6_V6ONLY: |
6d2010ae A |
2970 | case IPV6_USE_MIN_MTU: |
2971 | case IPV6_RECVRTHDR: | |
2972 | case IPV6_RECVPKTINFO: | |
2973 | case IPV6_RECVHOPLIMIT: | |
2974 | case IPV6_PATHMTU: | |
2d21ac55 A |
2975 | case IPV6_PKTINFO: |
2976 | case IPV6_HOPLIMIT: | |
2977 | case IPV6_HOPOPTS: | |
2978 | case IPV6_DSTOPTS: | |
2d21ac55 A |
2979 | case IPV6_MULTICAST_IF: |
2980 | case IPV6_MULTICAST_HOPS: | |
2981 | case IPV6_MULTICAST_LOOP: | |
2982 | case IPV6_JOIN_GROUP: | |
2983 | case IPV6_LEAVE_GROUP: | |
2984 | case IPV6_PORTRANGE: | |
2985 | case IPV6_IPSEC_POLICY: | |
b0d623f7 A |
2986 | case IPV6_RECVTCLASS: |
2987 | case IPV6_TCLASS: | |
6d2010ae A |
2988 | case IPV6_2292PKTOPTIONS: |
2989 | case IPV6_2292PKTINFO: | |
2990 | case IPV6_2292HOPLIMIT: | |
2991 | case IPV6_2292HOPOPTS: | |
2992 | case IPV6_2292DSTOPTS: | |
2993 | case IPV6_2292RTHDR: | |
2994 | case IPV6_BOUND_IF: | |
2995 | case IPV6_NO_IFT_CELLULAR: | |
2996 | ||
2d21ac55 | 2997 | return ip6_ctloutput(so, sopt); |
316670eb | 2998 | |
2d21ac55 A |
2999 | default: |
3000 | return EPERM; | |
2d21ac55 A |
3001 | } |
3002 | } | |
3003 | ||
3004 | __private_extern__ int | |
6d2010ae A |
3005 | icmp6_dgram_send(struct socket *so, int flags, struct mbuf *m, |
3006 | struct sockaddr *nam, struct mbuf *control, struct proc *p) | |
2d21ac55 | 3007 | { |
6d2010ae | 3008 | #pragma unused(flags, p) |
2d21ac55 A |
3009 | int error = 0; |
3010 | struct inpcb *inp = sotoinpcb(so); | |
3011 | struct sockaddr_in6 tmp; | |
316670eb | 3012 | struct sockaddr_in6 *dst = (struct sockaddr_in6 *)(void *)nam; |
2d21ac55 A |
3013 | struct icmp6_hdr *icmp6; |
3014 | ||
fe8ab488 A |
3015 | if (inp == NULL |
3016 | #if NECP | |
3017 | || (necp_socket_should_use_flow_divert(inp)) | |
3018 | #endif /* NECP */ | |
3019 | ) { | |
39236c6e A |
3020 | error = (inp == NULL ? EINVAL : EPROTOTYPE); |
3021 | goto bad; | |
3022 | } | |
3023 | ||
316670eb | 3024 | if (kauth_cred_issuser(so->so_cred)) |
39236c6e | 3025 | return (rip6_output(m, so, SIN6(nam), control, 0)); |
2d21ac55 A |
3026 | |
3027 | /* always copy sockaddr to avoid overwrites */ | |
3028 | if (so->so_state & SS_ISCONNECTED) { | |
39236c6e A |
3029 | if (nam != NULL) { |
3030 | error = EISCONN; | |
3031 | goto bad; | |
2d21ac55 A |
3032 | } |
3033 | /* XXX */ | |
3034 | bzero(&tmp, sizeof(tmp)); | |
3035 | tmp.sin6_family = AF_INET6; | |
3036 | tmp.sin6_len = sizeof(struct sockaddr_in6); | |
3037 | bcopy(&inp->in6p_faddr, &tmp.sin6_addr, | |
3038 | sizeof(struct in6_addr)); | |
3039 | dst = &tmp; | |
3040 | } else { | |
3041 | if (nam == NULL) { | |
39236c6e A |
3042 | error = ENOTCONN; |
3043 | goto bad; | |
2d21ac55 | 3044 | } |
316670eb | 3045 | tmp = *(struct sockaddr_in6 *)(void *)nam; |
2d21ac55 A |
3046 | dst = &tmp; |
3047 | } | |
3048 | ||
3049 | /* | |
3050 | * For an ICMPv6 packet, we should know its type and code | |
3051 | */ | |
39236c6e | 3052 | if (SOCK_PROTO(so) == IPPROTO_ICMPV6) { |
2d21ac55 A |
3053 | if (m->m_len < sizeof(struct icmp6_hdr) && |
3054 | (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { | |
3055 | error = ENOBUFS; | |
3056 | goto bad; | |
3057 | } | |
3058 | icmp6 = mtod(m, struct icmp6_hdr *); | |
316670eb | 3059 | |
2d21ac55 | 3060 | /* |
b0d623f7 | 3061 | * Allow only to send echo request and node information request |
2d21ac55 A |
3062 | * See RFC 2463 for Echo Request Message format |
3063 | */ | |
39236c6e A |
3064 | if ((icmp6->icmp6_type == ICMP6_ECHO_REQUEST && |
3065 | icmp6->icmp6_code == 0) || | |
3066 | (icmp6->icmp6_type == ICMP6_NI_QUERY && | |
3067 | (icmp6->icmp6_code == ICMP6_NI_SUBJ_IPV6 || | |
3068 | icmp6->icmp6_code == ICMP6_NI_SUBJ_FQDN))) { | |
b0d623f7 A |
3069 | /* Good */ |
3070 | ; | |
3071 | } else { | |
2d21ac55 A |
3072 | error = EPERM; |
3073 | goto bad; | |
3074 | } | |
3075 | } | |
3076 | ||
3077 | #if ENABLE_DEFAULT_SCOPE | |
3078 | if (dst->sin6_scope_id == 0) { /* not change if specified */ | |
39236c6e | 3079 | dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr); |
2d21ac55 A |
3080 | } |
3081 | #endif | |
3082 | ||
39236c6e | 3083 | return (rip6_output(m, so, dst, control, 0)); |
2d21ac55 | 3084 | bad: |
39236c6e A |
3085 | VERIFY(error != 0); |
3086 | ||
3087 | if (m != NULL) | |
3088 | m_freem(m); | |
3089 | if (control != NULL) | |
3090 | m_freem(control); | |
3091 | ||
3092 | return (error); | |
2d21ac55 A |
3093 | } |
3094 | ||
3095 | /* Like rip6_attach but without root privilege enforcement */ | |
3096 | __private_extern__ int | |
3097 | icmp6_dgram_attach(struct socket *so, int proto, struct proc *p) | |
3098 | { | |
3099 | struct inpcb *inp; | |
3100 | int error; | |
3101 | ||
3102 | inp = sotoinpcb(so); | |
3103 | if (inp) | |
3104 | panic("icmp6_dgram_attach"); | |
316670eb | 3105 | |
2d21ac55 A |
3106 | if (proto != IPPROTO_ICMPV6) |
3107 | return EINVAL; | |
316670eb | 3108 | |
2d21ac55 A |
3109 | error = soreserve(so, rip_sendspace, rip_recvspace); |
3110 | if (error) | |
3111 | return error; | |
3112 | error = in_pcballoc(so, &ripcbinfo, p); | |
3113 | if (error) | |
3114 | return error; | |
3115 | inp = (struct inpcb *)so->so_pcb; | |
3116 | inp->inp_vflag |= INP_IPV6; | |
3117 | inp->in6p_ip6_nxt = IPPROTO_ICMPV6; | |
3118 | inp->in6p_hops = -1; /* use kernel default */ | |
3119 | inp->in6p_cksum = -1; | |
3120 | MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *, | |
3121 | sizeof(struct icmp6_filter), M_PCB, M_WAITOK); | |
3122 | if (inp->in6p_icmp6filt == NULL) | |
3123 | return (ENOMEM); | |
3124 | ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); | |
3125 | return 0; | |
3126 | } | |
3127 | ||
3128 | ||
1c79356b A |
3129 | /* |
3130 | * Perform rate limit check. | |
3131 | * Returns 0 if it is okay to send the icmp6 packet. | |
3132 | * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate | |
3133 | * limitation. | |
3134 | * | |
fe8ab488 | 3135 | * XXX per-destination check necessary? |
1c79356b A |
3136 | */ |
3137 | static int | |
2d21ac55 A |
3138 | icmp6_ratelimit( |
3139 | __unused const struct in6_addr *dst, /* not used at this moment */ | |
fe8ab488 A |
3140 | const int type, |
3141 | __unused const int code) | |
1c79356b | 3142 | { |
9bccf70c | 3143 | int ret; |
1c79356b | 3144 | |
55e303ae | 3145 | ret = 0; /* okay to send */ |
1c79356b | 3146 | |
9bccf70c | 3147 | /* PPS limit */ |
fe8ab488 A |
3148 | if (type == ND_ROUTER_ADVERT) { |
3149 | if (!ppsratecheck(&icmp6rappslim_last, &icmp6rapps_count, | |
3150 | icmp6rappslim)) | |
3151 | ret++; | |
3152 | } else if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, | |
9bccf70c | 3153 | icmp6errppslim)) { |
1c79356b | 3154 | /* The packet is subject to rate limit */ |
9bccf70c | 3155 | ret++; |
1c79356b | 3156 | } |
1c79356b | 3157 | |
9bccf70c | 3158 | return ret; |
1c79356b | 3159 | } |