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