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