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