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