icmp6.c revision 1.40
1/*	$NetBSD: icmp6.c,v 1.40 2000/08/03 14:31:05 itojun Exp $	*/
2/*	$KAME: icmp6.c,v 1.130 2000/08/03 14:22:10 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, based on
1046 * draft-ietf-ipngwg-icmp-name-lookups-06.
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 * - joins NI group address at in6_ifattach() time only, does not cope
1053 *   with hostname changes by sethostname(3)
1054 */
1055#ifndef offsetof		/* XXX */
1056#define	offsetof(type, member)	((size_t)(&((type *)0)->member))
1057#endif
1058static struct mbuf *
1059ni6_input(m, off)
1060	struct mbuf *m;
1061	int off;
1062{
1063	struct icmp6_nodeinfo *ni6, *nni6;
1064	struct mbuf *n = NULL;
1065	u_int16_t qtype;
1066	int subjlen;
1067	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1068	struct ni_reply_fqdn *fqdn;
1069	int addrs;		/* for NI_QTYPE_NODEADDR */
1070	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1071	struct sockaddr_in6 sin6;
1072	struct ip6_hdr *ip6;
1073	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
1074	char *subj;
1075
1076	ip6 = mtod(m, struct ip6_hdr *);
1077#ifndef PULLDOWN_TEST
1078	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1079#else
1080	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1081	if (ni6 == NULL) {
1082		/* m is already reclaimed */
1083		return NULL;
1084	}
1085#endif
1086
1087	/*
1088	 * Validate IPv6 destination address.
1089	 *
1090	 * We accept packets with the following IPv6 destination address:
1091	 * - Responder's unicast/anycast address, and
1092	 * - link-local multicast address (including NI group address)
1093	 */
1094	bzero(&sin6, sizeof(sin6));
1095	sin6.sin6_family = AF_INET6;
1096	sin6.sin6_len = sizeof(struct sockaddr_in6);
1097	bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1098	/* XXX scopeid */
1099	if (ifa_ifwithaddr((struct sockaddr *)&sin6))
1100		; /*unicast/anycast, fine*/
1101	else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1102		; /*violates spec slightly, see above*/
1103	else
1104		goto bad;
1105
1106	/* guess reply length */
1107	qtype = ntohs(ni6->ni_qtype);
1108	switch (qtype) {
1109	case NI_QTYPE_NOOP:
1110		break;		/* no reply data */
1111	case NI_QTYPE_SUPTYPES:
1112		replylen += sizeof(u_int32_t);
1113		break;
1114	case NI_QTYPE_FQDN:
1115		/* XXX will append a mbuf */
1116		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1117		break;
1118	case NI_QTYPE_NODEADDR:
1119		addrs = ni6_addrs(ni6, m, &ifp);
1120		if ((replylen += addrs * sizeof(struct in6_addr)) > MCLBYTES)
1121			replylen = MCLBYTES; /* XXX: we'll truncate later */
1122		break;
1123	default:
1124		/*
1125		 * XXX: We must return a reply with the ICMP6 code
1126		 * `unknown Qtype' in this case. However we regard the case
1127		 * as an FQDN query for backward compatibility.
1128		 * Older versions set a random value to this field,
1129		 * so it rarely varies in the defined qtypes.
1130		 * But the mechanism is not reliable...
1131		 * maybe we should obsolete older versions.
1132		 */
1133		qtype = NI_QTYPE_FQDN;
1134		/* XXX will append a mbuf */
1135		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1136		oldfqdn++;
1137		break;
1138	}
1139
1140	/* validate query Subject field. */
1141	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1142	switch (qtype) {
1143	case NI_QTYPE_NOOP:
1144	case NI_QTYPE_SUPTYPES:
1145		/* 06 draft */
1146		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1147			break;
1148		/*FALLTHROUGH*/
1149	case NI_QTYPE_FQDN:
1150	case NI_QTYPE_NODEADDR:
1151		switch (ni6->ni_code) {
1152		case ICMP6_NI_SUBJ_IPV6:
1153#if ICMP6_NI_SUBJ_IPV6 != 0
1154		case 0:
1155#endif
1156			/*
1157			 * backward compatibility - try to accept 03 draft
1158			 * format, where no Subject is present.
1159			 */
1160			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1161			    subjlen == 0) {
1162				oldfqdn++;
1163				break;
1164			}
1165#if ICMP6_NI_SUBJ_IPV6 != 0
1166			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1167				goto bad;
1168#endif
1169
1170			if (subjlen != sizeof(sin6.sin6_addr))
1171				goto bad;
1172
1173			/*
1174			 * Validate Subject address.
1175			 *
1176			 * Not sure what exactly does "address belongs to the
1177			 * node" mean in the spec, is it just unicast, or what?
1178			 *
1179			 * At this moment we consider Subject address as
1180			 * "belong to the node" if the Subject address equals
1181			 * to the IPv6 destination address; validation for
1182			 * IPv6 destination address should have done enough
1183			 * check for us.
1184			 *
1185			 * We do not do proxy at this moment.
1186			 */
1187			/* m_pulldown instead of copy? */
1188			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1189			    subjlen, (caddr_t)&sin6.sin6_addr);
1190			/* XXX kame scope hack */
1191			if (IN6_IS_SCOPE_LINKLOCAL(&sin6.sin6_addr)) {
1192#ifdef FAKE_LOOPBACK_IF
1193				if ((m->m_flags & M_PKTHDR) != 0 &&
1194				    m->m_pkthdr.rcvif) {
1195					sin6.sin6_addr.s6_addr16[1] =
1196					    htons(m->m_pkthdr.rcvif->if_index);
1197				}
1198#else
1199				if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
1200					sin6.sin6_addr.s6_addr16[1] =
1201					    ip6->ip6_dst.s6_addr16[1];
1202				}
1203#endif
1204			}
1205			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &sin6.sin6_addr))
1206				break;
1207			/*
1208			 * XXX if we are to allow other cases, we should really
1209			 * be careful about scope here.
1210			 * basically, we should disallow queries toward IPv6
1211			 * destination X with subject Y, if scope(X) > scope(Y).
1212			 * if we allow scope(X) > scope(Y), it will result in
1213			 * information leakage across scope boundary.
1214			 */
1215			goto bad;
1216
1217		case ICMP6_NI_SUBJ_FQDN:
1218			/*
1219			 * Validate Subject name with gethostname(3).
1220			 *
1221			 * The behavior may need some debate, since:
1222			 * - we are not sure if the node has FQDN as
1223			 *   hostname (returned by gethostname(3)).
1224			 * - the code does wildcard match for truncated names.
1225			 *   however, we are not sure if we want to perform
1226			 *   wildcard match, if gethostname(3) side has
1227			 *   truncated hostname.
1228			 */
1229			n = ni6_nametodns(hostname, hostnamelen, 0);
1230			if (!n || n->m_next || n->m_len == 0)
1231				goto bad;
1232			IP6_EXTHDR_GET(subj, char *, m,
1233			    off + sizeof(struct icmp6_nodeinfo), subjlen);
1234			if (subj == NULL)
1235				goto bad;
1236			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1237					n->m_len)) {
1238				goto bad;
1239			}
1240			m_freem(n);
1241			n = NULL;
1242			break;
1243
1244		case ICMP6_NI_SUBJ_IPV4:	/* xxx: to be implemented? */
1245		default:
1246			goto bad;
1247		}
1248		break;
1249	}
1250
1251	/* allocate a mbuf to reply. */
1252	MGETHDR(n, M_DONTWAIT, m->m_type);
1253	if (n == NULL) {
1254		m_freem(m);
1255		return(NULL);
1256	}
1257	M_COPY_PKTHDR(n, m); /* just for recvif */
1258	if (replylen > MHLEN) {
1259		if (replylen > MCLBYTES) {
1260			 /*
1261			  * XXX: should we try to allocate more? But MCLBYTES is
1262			  * probably much larger than IPV6_MMTU...
1263			  */
1264			goto bad;
1265		}
1266		MCLGET(n, M_DONTWAIT);
1267		if ((n->m_flags & M_EXT) == 0) {
1268			goto bad;
1269		}
1270	}
1271	n->m_pkthdr.len = n->m_len = replylen;
1272
1273	/* copy mbuf header and IPv6 + Node Information base headers */
1274	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1275	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1276	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1277
1278	/* qtype dependent procedure */
1279	switch (qtype) {
1280	case NI_QTYPE_NOOP:
1281		nni6->ni_code = ICMP6_NI_SUCCESS;
1282		nni6->ni_flags = 0;
1283		break;
1284	case NI_QTYPE_SUPTYPES:
1285		nni6->ni_code = ICMP6_NI_SUCCESS;
1286		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
1287		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1288		*(u_int32_t *)(nni6 + 1) = htonl(0x000f);
1289		break;
1290	case NI_QTYPE_FQDN:
1291		nni6->ni_code = ICMP6_NI_SUCCESS;
1292		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1293						sizeof(struct ip6_hdr) +
1294						sizeof(struct icmp6_nodeinfo));
1295		nni6->ni_flags = 0; /* XXX: meaningless TTL */
1296		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
1297		/*
1298		 * XXX do we really have FQDN in variable "hostname"?
1299		 */
1300		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1301		if (n->m_next == NULL)
1302			goto bad;
1303		/* XXX we assume that n->m_next is not a chain */
1304		if (n->m_next->m_next != NULL)
1305			goto bad;
1306		n->m_pkthdr.len += n->m_next->m_len;
1307		break;
1308	case NI_QTYPE_NODEADDR:
1309	{
1310		int lenlim, copied;
1311
1312		nni6->ni_code = ICMP6_NI_SUCCESS;
1313		if (n->m_flags & M_EXT)
1314			lenlim = MCLBYTES - sizeof(struct ip6_hdr) -
1315				sizeof(struct icmp6_nodeinfo);
1316		else
1317			lenlim = MHLEN - sizeof(struct ip6_hdr) -
1318				sizeof(struct icmp6_nodeinfo);
1319		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1320		/* XXX: reset mbuf length */
1321		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1322			sizeof(struct icmp6_nodeinfo) + copied;
1323		break;
1324	}
1325	default:
1326		break;		/* XXX impossible! */
1327	}
1328
1329	nni6->ni_type = ICMP6_NI_REPLY;
1330	m_freem(m);
1331	return(n);
1332
1333  bad:
1334	m_freem(m);
1335	if (n)
1336		m_freem(n);
1337	return(NULL);
1338}
1339#undef hostnamelen
1340
1341/*
1342 * make a mbuf with DNS-encoded string.  no compression support.
1343 *
1344 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1345 * treated as truncated name (two \0 at the end).  this is a wild guess.
1346 */
1347static struct mbuf *
1348ni6_nametodns(name, namelen, old)
1349	const char *name;
1350	int namelen;
1351	int old;	/* return pascal string if non-zero */
1352{
1353	struct mbuf *m;
1354	char *cp, *ep;
1355	const char *p, *q;
1356	int i, len, nterm;
1357
1358	if (old)
1359		len = namelen + 1;
1360	else
1361		len = MCLBYTES;
1362
1363	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1364	MGET(m, M_DONTWAIT, MT_DATA);
1365	if (m && len > MLEN) {
1366		MCLGET(m, M_DONTWAIT);
1367		if ((m->m_flags & M_EXT) == 0)
1368			goto fail;
1369	}
1370	if (!m)
1371		goto fail;
1372	m->m_next = NULL;
1373
1374	if (old) {
1375		m->m_len = len;
1376		*mtod(m, char *) = namelen;
1377		bcopy(name, mtod(m, char *) + 1, namelen);
1378		return m;
1379	} else {
1380		m->m_len = 0;
1381		cp = mtod(m, char *);
1382		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1383
1384		/* if not certain about my name, return empty buffer */
1385		if (namelen == 0)
1386			return m;
1387
1388		/*
1389		 * guess if it looks like shortened hostname, or FQDN.
1390		 * shortened hostname needs two trailing "\0".
1391		 */
1392		i = 0;
1393		for (p = name; p < name + namelen; p++) {
1394			if (*p && *p == '.')
1395				i++;
1396		}
1397		if (i < 2)
1398			nterm = 2;
1399		else
1400			nterm = 1;
1401
1402		p = name;
1403		while (cp < ep && p < name + namelen) {
1404			i = 0;
1405			for (q = p; q < name + namelen && *q && *q != '.'; q++)
1406				i++;
1407			/* result does not fit into mbuf */
1408			if (cp + i + 1 >= ep)
1409				goto fail;
1410			/* DNS label length restriction, RFC1035 page 8 */
1411			if (i >= 64)
1412				goto fail;
1413			*cp++ = i;
1414			bcopy(p, cp, i);
1415			cp += i;
1416			p = q;
1417			if (p < name + namelen && *p == '.')
1418				p++;
1419		}
1420		/* termination */
1421		if (cp + nterm >= ep)
1422			goto fail;
1423		while (nterm-- > 0)
1424			*cp++ = '\0';
1425		m->m_len = cp - mtod(m, char *);
1426		return m;
1427	}
1428
1429	panic("should not reach here");
1430	/*NOTREACHED*/
1431
1432 fail:
1433	if (m)
1434		m_freem(m);
1435	return NULL;
1436}
1437
1438/*
1439 * check if two DNS-encoded string matches.  takes care of truncated
1440 * form (with \0\0 at the end).  no compression support.
1441 * XXX upper/lowercase match (see RFC2065)
1442 */
1443static int
1444ni6_dnsmatch(a, alen, b, blen)
1445	const char *a;
1446	int alen;
1447	const char *b;
1448	int blen;
1449{
1450	const char *a0, *b0;
1451	int l;
1452
1453	/* simplest case - need validation? */
1454	if (alen == blen && bcmp(a, b, alen) == 0)
1455		return 1;
1456
1457	a0 = a;
1458	b0 = b;
1459
1460	/* termination is mandatory */
1461	if (alen < 2 || blen < 2)
1462		return 0;
1463	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1464		return 0;
1465	alen--;
1466	blen--;
1467
1468	while (a - a0 < alen && b - b0 < blen) {
1469		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1470			return 0;
1471
1472		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1473			return 0;
1474		/* we don't support compression yet */
1475		if (a[0] >= 64 || b[0] >= 64)
1476			return 0;
1477
1478		/* truncated case */
1479		if (a[0] == 0 && a - a0 == alen - 1)
1480			return 1;
1481		if (b[0] == 0 && b - b0 == blen - 1)
1482			return 1;
1483		if (a[0] == 0 || b[0] == 0)
1484			return 0;
1485
1486		if (a[0] != b[0])
1487			return 0;
1488		l = a[0];
1489		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1490			return 0;
1491		if (bcmp(a + 1, b + 1, l) != 0)
1492			return 0;
1493
1494		a += 1 + l;
1495		b += 1 + l;
1496	}
1497
1498	if (a - a0 == alen && b - b0 == blen)
1499		return 1;
1500	else
1501		return 0;
1502}
1503
1504/*
1505 * calculate the number of addresses to be returned in the node info reply.
1506 */
1507static int
1508ni6_addrs(ni6, m, ifpp)
1509	struct icmp6_nodeinfo *ni6;
1510	struct mbuf *m;
1511	struct ifnet **ifpp;
1512{
1513	register struct ifnet *ifp;
1514	register struct in6_ifaddr *ifa6;
1515	register struct ifaddr *ifa;
1516	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1517	int addrs = 0, addrsofif, iffound = 0;
1518
1519	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list))
1520	{
1521		addrsofif = 0;
1522		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1523		     ifa = ifa->ifa_list.tqe_next)
1524		{
1525			if (ifa->ifa_addr->sa_family != AF_INET6)
1526				continue;
1527			ifa6 = (struct in6_ifaddr *)ifa;
1528
1529			if (!(ni6->ni_flags & NI_NODEADDR_FLAG_ALL) &&
1530			    IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
1531					       &ifa6->ia_addr.sin6_addr))
1532				iffound = 1;
1533
1534			/*
1535			 * IPv4-mapped addresses can only be returned by a
1536			 * Node Information proxy, since they represent
1537			 * addresses of IPv4-only nodes, which perforce do
1538			 * not implement this protocol.
1539			 * [icmp-name-lookups-05]
1540			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1541			 * this function at this moment.
1542			 */
1543
1544			if (ifa6->ia6_flags & IN6_IFF_ANYCAST)
1545				continue; /* we need only unicast addresses */
1546
1547			if ((ni6->ni_flags & (NI_NODEADDR_FLAG_LINKLOCAL |
1548					      NI_NODEADDR_FLAG_SITELOCAL |
1549					      NI_NODEADDR_FLAG_GLOBAL)) == 0)
1550				continue;
1551
1552			/* What do we have to do about ::1? */
1553			switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1554			 case IPV6_ADDR_SCOPE_LINKLOCAL:
1555				if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
1556					addrsofif++;
1557				break;
1558			 case IPV6_ADDR_SCOPE_SITELOCAL:
1559				if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
1560					addrsofif++;
1561				break;
1562			 case IPV6_ADDR_SCOPE_GLOBAL:
1563				 if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
1564					 addrsofif++;
1565				 break;
1566			 default:
1567				 continue;
1568			}
1569		}
1570		if (iffound) {
1571			*ifpp = ifp;
1572			return(addrsofif);
1573		}
1574
1575		addrs += addrsofif;
1576	}
1577
1578	return(addrs);
1579}
1580
1581static int
1582ni6_store_addrs(ni6, nni6, ifp0, resid)
1583	struct icmp6_nodeinfo *ni6, *nni6;
1584	struct ifnet *ifp0;
1585	int resid;
1586{
1587	register struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1588	register struct in6_ifaddr *ifa6;
1589	register struct ifaddr *ifa;
1590	int docopy, copied = 0;
1591	u_char *cp = (u_char *)(nni6 + 1);
1592
1593	if (ifp0 == NULL && !(ni6->ni_flags & NI_NODEADDR_FLAG_ALL))
1594		return(0);	/* needless to copy */
1595
1596	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list))
1597	{
1598		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1599		     ifa = ifa->ifa_list.tqe_next)
1600		{
1601			docopy = 0;
1602
1603			if (ifa->ifa_addr->sa_family != AF_INET6)
1604				continue;
1605			ifa6 = (struct in6_ifaddr *)ifa;
1606
1607			if (ifa6->ia6_flags & IN6_IFF_ANYCAST) {
1608				/* just experimental. not in the spec. */
1609				if (ni6->ni_flags & NI_NODEADDR_FLAG_ANYCAST)
1610					docopy = 1;
1611				else
1612					continue;
1613			}
1614			else {	/* unicast address */
1615				if (ni6->ni_flags & NI_NODEADDR_FLAG_ANYCAST)
1616					continue;
1617				else
1618					docopy = 1;
1619			}
1620
1621			/* What do we have to do about ::1? */
1622			switch(in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1623			 case IPV6_ADDR_SCOPE_LINKLOCAL:
1624				if (ni6->ni_flags & NI_NODEADDR_FLAG_LINKLOCAL)
1625					docopy = 1;
1626				break;
1627			 case IPV6_ADDR_SCOPE_SITELOCAL:
1628				if (ni6->ni_flags & NI_NODEADDR_FLAG_SITELOCAL)
1629					docopy = 1;
1630				break;
1631			 case IPV6_ADDR_SCOPE_GLOBAL:
1632				 if (ni6->ni_flags & NI_NODEADDR_FLAG_GLOBAL)
1633					 docopy = 1;
1634				 break;
1635			 default:
1636				 continue;
1637			}
1638
1639			if (docopy) {
1640				if (resid < sizeof(struct in6_addr)) {
1641					/*
1642					 * We give up much more copy.
1643					 * Set the truncate flag and return.
1644					 */
1645					nni6->ni_flags |=
1646						NI_NODEADDR_FLAG_TRUNCATE;
1647					return(copied);
1648				}
1649				bcopy(&ifa6->ia_addr.sin6_addr, cp,
1650				      sizeof(struct in6_addr));
1651				/* XXX: KAME link-local hack; remove ifindex */
1652				if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr))
1653					((struct in6_addr *)cp)->s6_addr16[1] = 0;
1654				cp += sizeof(struct in6_addr);
1655				resid -= sizeof(struct in6_addr);
1656				copied += sizeof(struct in6_addr);
1657			}
1658		}
1659		if (ifp0)	/* we need search only on the specified IF */
1660			break;
1661	}
1662
1663	return(copied);
1664}
1665
1666/*
1667 * XXX almost dup'ed code with rip6_input.
1668 */
1669static int
1670icmp6_rip6_input(mp, off)
1671	struct	mbuf **mp;
1672	int	off;
1673{
1674	struct mbuf *m = *mp;
1675	register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1676	register struct in6pcb *in6p;
1677	struct in6pcb *last = NULL;
1678	struct sockaddr_in6 rip6src;
1679	struct icmp6_hdr *icmp6;
1680	struct mbuf *opts = NULL;
1681
1682#ifndef PULLDOWN_TEST
1683	/* this is assumed to be safe. */
1684	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1685#else
1686	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1687	if (icmp6 == NULL) {
1688		/* m is already reclaimed */
1689		return IPPROTO_DONE;
1690	}
1691#endif
1692
1693	bzero(&rip6src, sizeof(rip6src));
1694	rip6src.sin6_len = sizeof(struct sockaddr_in6);
1695	rip6src.sin6_family = AF_INET6;
1696	/* KAME hack: recover scopeid */
1697	(void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
1698
1699	for (in6p = rawin6pcb.in6p_next;
1700	     in6p != &rawin6pcb; in6p = in6p->in6p_next)
1701	{
1702		if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1703			continue;
1704		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1705		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1706			continue;
1707		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1708		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1709			continue;
1710		if (in6p->in6p_icmp6filt
1711		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1712				 in6p->in6p_icmp6filt))
1713			continue;
1714		if (last) {
1715			struct	mbuf *n;
1716			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1717				if (last->in6p_flags & IN6P_CONTROLOPTS)
1718					ip6_savecontrol(last, &opts, ip6, n);
1719				/* strip intermediate headers */
1720				m_adj(n, off);
1721				if (sbappendaddr(&last->in6p_socket->so_rcv,
1722						 (struct sockaddr *)&rip6src,
1723						 n, opts) == 0) {
1724					/* should notify about lost packet */
1725					m_freem(n);
1726					if (opts)
1727						m_freem(opts);
1728				} else
1729					sorwakeup(last->in6p_socket);
1730				opts = NULL;
1731			}
1732		}
1733		last = in6p;
1734	}
1735	if (last) {
1736		if (last->in6p_flags & IN6P_CONTROLOPTS)
1737			ip6_savecontrol(last, &opts, ip6, m);
1738		/* strip intermediate headers */
1739		m_adj(m, off);
1740		if (sbappendaddr(&last->in6p_socket->so_rcv,
1741				(struct sockaddr *)&rip6src, m, opts) == 0) {
1742			m_freem(m);
1743			if (opts)
1744				m_freem(opts);
1745		} else
1746			sorwakeup(last->in6p_socket);
1747	} else {
1748		m_freem(m);
1749		ip6stat.ip6s_delivered--;
1750	}
1751	return IPPROTO_DONE;
1752}
1753
1754/*
1755 * Reflect the ip6 packet back to the source.
1756 * OFF points to the icmp6 header, counted from the top of the mbuf.
1757 */
1758void
1759icmp6_reflect(m, off)
1760	struct	mbuf *m;
1761	size_t off;
1762{
1763	struct ip6_hdr *ip6;
1764	struct icmp6_hdr *icmp6;
1765	struct in6_ifaddr *ia;
1766	struct in6_addr t, *src = 0;
1767	int plen;
1768	int type, code;
1769	struct ifnet *outif = NULL;
1770#ifdef COMPAT_RFC1885
1771	int mtu = IPV6_MMTU;
1772	struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1773#endif
1774
1775	/* too short to reflect */
1776	if (off < sizeof(struct ip6_hdr)) {
1777		printf("sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
1778		       (u_long)off, (u_long)sizeof(struct ip6_hdr),
1779		       __FILE__, __LINE__);
1780		goto bad;
1781	}
1782
1783	/*
1784	 * If there are extra headers between IPv6 and ICMPv6, strip
1785	 * off that header first.
1786	 */
1787#ifdef DIAGNOSTIC
1788	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
1789		panic("assumption failed in icmp6_reflect");
1790#endif
1791	if (off > sizeof(struct ip6_hdr)) {
1792		size_t l;
1793		struct ip6_hdr nip6;
1794
1795		l = off - sizeof(struct ip6_hdr);
1796		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
1797		m_adj(m, l);
1798		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1799		if (m->m_len < l) {
1800			if ((m = m_pullup(m, l)) == NULL)
1801				return;
1802		}
1803		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
1804	} else /* off == sizeof(struct ip6_hdr) */ {
1805		size_t l;
1806		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
1807		if (m->m_len < l) {
1808			if ((m = m_pullup(m, l)) == NULL)
1809				return;
1810		}
1811	}
1812	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
1813	ip6 = mtod(m, struct ip6_hdr *);
1814	ip6->ip6_nxt = IPPROTO_ICMPV6;
1815	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
1816	type = icmp6->icmp6_type; /* keep type for statistics */
1817	code = icmp6->icmp6_code; /* ditto. */
1818
1819	t = ip6->ip6_dst;
1820	/*
1821	 * ip6_input() drops a packet if its src is multicast.
1822	 * So, the src is never multicast.
1823	 */
1824	ip6->ip6_dst = ip6->ip6_src;
1825
1826	/* XXX hack for link-local addresses */
1827	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
1828		ip6->ip6_dst.s6_addr16[1] =
1829			htons(m->m_pkthdr.rcvif->if_index);
1830	if (IN6_IS_ADDR_LINKLOCAL(&t))
1831		t.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
1832
1833#ifdef COMPAT_RFC1885
1834	/*
1835	 * xxx guess MTU
1836	 * RFC 1885 requires that echo reply should be truncated if it
1837	 * does not fit in with (return) path MTU, but the description was
1838	 * removed in the new spec.
1839	 */
1840	if (icmp6_reflect_rt.ro_rt == 0 ||
1841	    ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
1842		if (icmp6_reflect_rt.ro_rt) {
1843			icmp6_reflect_rt.ro_rt = 0;
1844		}
1845		bzero(sin6, sizeof(*sin6));
1846		sin6->sin6_family = PF_INET6;
1847		sin6->sin6_len = sizeof(struct sockaddr_in6);
1848		sin6->sin6_addr = ip6->ip6_dst;
1849
1850		rtalloc((struct route *)&icmp6_reflect_rt.ro_rt);
1851	}
1852
1853	if (icmp6_reflect_rt.ro_rt == 0)
1854		goto bad;
1855
1856	if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
1857	    && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
1858		mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
1859
1860	if (mtu < m->m_pkthdr.len) {
1861		plen -= (m->m_pkthdr.len - mtu);
1862		m_adj(m, mtu - m->m_pkthdr.len);
1863	}
1864#endif
1865	/*
1866	 * If the incoming packet was addressed directly to us(i.e. unicast),
1867	 * use dst as the src for the reply.
1868	 * The IN6_IFF_NOTREADY case would be VERY rare, but is possible
1869	 * (for example) when we encounter an error while forwarding procedure
1870	 * destined to a duplicated address of ours.
1871	 */
1872	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
1873		if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
1874		    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
1875			src = &t;
1876			break;
1877		}
1878	if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
1879		/*
1880		 * This is the case if the dst is our link-local address
1881		 * and the sender is also ourseleves.
1882		 */
1883		src = &t;
1884	}
1885
1886	if (src == 0)
1887		/*
1888		 * This case matches to multicasts, our anycast, or unicasts
1889		 * that we do not own. Select a source address which has the
1890		 * same scope.
1891		 * XXX: for (non link-local) multicast addresses, this might
1892		 * not be a good choice.
1893		 */
1894		if ((ia = in6_ifawithscope(m->m_pkthdr.rcvif, &t)) != 0)
1895			src = &IA6_SIN6(ia)->sin6_addr;
1896
1897	if (src == 0)
1898		goto bad;
1899
1900	ip6->ip6_src = *src;
1901
1902	ip6->ip6_flow = 0;
1903	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
1904	ip6->ip6_vfc |= IPV6_VERSION;
1905	ip6->ip6_nxt = IPPROTO_ICMPV6;
1906	if (m->m_pkthdr.rcvif) {
1907		/* XXX: This may not be the outgoing interface */
1908		ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim;
1909	}
1910
1911	icmp6->icmp6_cksum = 0;
1912	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
1913					sizeof(struct ip6_hdr), plen);
1914
1915	/*
1916	 * xxx option handling
1917	 */
1918
1919	m->m_flags &= ~(M_BCAST|M_MCAST);
1920#ifdef IPSEC
1921	/* Don't lookup socket */
1922	ipsec_setsocket(m, NULL);
1923#endif /*IPSEC*/
1924
1925#ifdef COMPAT_RFC1885
1926	ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif);
1927#else
1928	ip6_output(m, NULL, NULL, 0, NULL, &outif);
1929#endif
1930	if (outif)
1931		icmp6_ifoutstat_inc(outif, type, code);
1932
1933	return;
1934
1935 bad:
1936	m_freem(m);
1937	return;
1938}
1939
1940void
1941icmp6_fasttimo()
1942{
1943
1944	mld6_fasttimeo();
1945}
1946
1947static const char *
1948icmp6_redirect_diag(src6, dst6, tgt6)
1949	struct in6_addr *src6;
1950	struct in6_addr *dst6;
1951	struct in6_addr *tgt6;
1952{
1953	static char buf[1024];
1954	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
1955		ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
1956	return buf;
1957}
1958
1959void
1960icmp6_redirect_input(m, off)
1961	register struct mbuf *m;
1962	int off;
1963{
1964	struct ifnet *ifp = m->m_pkthdr.rcvif;
1965	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1966	struct nd_redirect *nd_rd;
1967	int icmp6len = ntohs(ip6->ip6_plen);
1968	char *lladdr = NULL;
1969	int lladdrlen = 0;
1970	u_char *redirhdr = NULL;
1971	int redirhdrlen = 0;
1972	struct rtentry *rt = NULL;
1973	int is_router;
1974	int is_onlink;
1975	struct in6_addr src6 = ip6->ip6_src;
1976	struct in6_addr redtgt6;
1977	struct in6_addr reddst6;
1978	union nd_opts ndopts;
1979
1980	if (!m || !ifp)
1981		return;
1982
1983	/* XXX if we are router, we don't update route by icmp6 redirect */
1984	if (ip6_forwarding)
1985		goto freeit;
1986	if (!icmp6_rediraccept)
1987		goto freeit;
1988
1989#ifndef PULLDOWN_TEST
1990	IP6_EXTHDR_CHECK(m, off, icmp6len,);
1991	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
1992#else
1993	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
1994	if (nd_rd == NULL) {
1995		icmp6stat.icp6s_tooshort++;
1996		return;
1997	}
1998#endif
1999	redtgt6 = nd_rd->nd_rd_target;
2000	reddst6 = nd_rd->nd_rd_dst;
2001
2002	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2003		redtgt6.s6_addr16[1] = htons(ifp->if_index);
2004	if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2005		reddst6.s6_addr16[1] = htons(ifp->if_index);
2006
2007	/* validation */
2008	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2009		log(LOG_ERR,
2010			"ICMP6 redirect sent from %s rejected; "
2011			"must be from linklocal\n", ip6_sprintf(&src6));
2012		goto freeit;
2013	}
2014	if (ip6->ip6_hlim != 255) {
2015		log(LOG_ERR,
2016			"ICMP6 redirect sent from %s rejected; "
2017			"hlim=%d (must be 255)\n",
2018			ip6_sprintf(&src6), ip6->ip6_hlim);
2019		goto freeit;
2020	}
2021    {
2022	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2023	struct sockaddr_in6 sin6;
2024	struct in6_addr *gw6;
2025
2026	bzero(&sin6, sizeof(sin6));
2027	sin6.sin6_family = AF_INET6;
2028	sin6.sin6_len = sizeof(struct sockaddr_in6);
2029	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2030	rt = rtalloc1((struct sockaddr *)&sin6, 0);
2031	if (rt) {
2032		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2033		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2034			log(LOG_ERR,
2035				"ICMP6 redirect rejected; "
2036				"not equal to gw-for-src=%s (must be same): "
2037				"%s\n",
2038				ip6_sprintf(gw6),
2039				icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2040			RTFREE(rt);
2041			goto freeit;
2042		}
2043	} else {
2044		log(LOG_ERR,
2045			"ICMP6 redirect rejected; "
2046			"no route found for redirect dst: %s\n",
2047			icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2048		goto freeit;
2049	}
2050	RTFREE(rt);
2051	rt = NULL;
2052    }
2053	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2054		log(LOG_ERR,
2055			"ICMP6 redirect rejected; "
2056			"redirect dst must be unicast: %s\n",
2057			icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2058		goto freeit;
2059	}
2060
2061	is_router = is_onlink = 0;
2062	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2063		is_router = 1;	/* router case */
2064	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2065		is_onlink = 1;	/* on-link destination case */
2066	if (!is_router && !is_onlink) {
2067		log(LOG_ERR,
2068			"ICMP6 redirect rejected; "
2069			"neither router case nor onlink case: %s\n",
2070			icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2071		goto freeit;
2072	}
2073	/* validation passed */
2074
2075	icmp6len -= sizeof(*nd_rd);
2076	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2077	if (nd6_options(&ndopts) < 0) {
2078		log(LOG_INFO, "icmp6_redirect_input: "
2079			"invalid ND option, rejected: %s\n",
2080			icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2081		goto freeit;
2082	}
2083
2084	if (ndopts.nd_opts_tgt_lladdr) {
2085		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2086		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2087	}
2088
2089	if (ndopts.nd_opts_rh) {
2090		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2091		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2092	}
2093
2094	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2095		log(LOG_INFO,
2096			"icmp6_redirect_input: lladdrlen mismatch for %s "
2097			"(if %d, icmp6 packet %d): %s\n",
2098			ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2099			icmp6_redirect_diag(&src6, &reddst6, &redtgt6));
2100	}
2101
2102	/* RFC 2461 8.3 */
2103	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2104			 is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2105
2106	if (!is_onlink) {	/* better router case. perform rtredirect. */
2107		/* perform rtredirect */
2108		struct sockaddr_in6 sdst;
2109		struct sockaddr_in6 sgw;
2110		struct sockaddr_in6 ssrc;
2111
2112		bzero(&sdst, sizeof(sdst));
2113		bzero(&sgw, sizeof(sgw));
2114		bzero(&ssrc, sizeof(ssrc));
2115		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2116		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2117			sizeof(struct sockaddr_in6);
2118		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2119		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2120		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2121		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2122			   (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2123			   (struct sockaddr *)&ssrc,
2124			   (struct rtentry **)NULL
2125			   );
2126	}
2127	/* finally update cached route in each socket via pfctlinput */
2128    {
2129	struct sockaddr_in6 sdst;
2130#if 1
2131#else
2132	struct ip6protosw *pr;
2133#endif
2134
2135	bzero(&sdst, sizeof(sdst));
2136	sdst.sin6_family = AF_INET6;
2137	sdst.sin6_len = sizeof(struct sockaddr_in6);
2138	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2139#if 1
2140	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2141#else
2142	/*
2143	 * do not use pfctlinput() here, we have different prototype for
2144	 * xx_ctlinput() in ip6proto.
2145	 */
2146	for (pr = (struct ip6protosw *)inet6domain.dom_protosw;
2147	     pr < (struct ip6protosw *)inet6domain.dom_protoswNPROTOSW;
2148	     pr++) {
2149		if (pr->pr_ctlinput) {
2150			(*pr->pr_ctlinput)(PRC_REDIRECT_HOST,
2151				(struct sockaddr *)&sdst, NULL, NULL, 0);
2152		}
2153	}
2154#endif
2155#ifdef IPSEC
2156	key_sa_routechange((struct sockaddr *)&sdst);
2157#endif
2158    }
2159
2160 freeit:
2161	m_freem(m);
2162}
2163
2164void
2165icmp6_redirect_output(m0, rt)
2166	struct mbuf *m0;
2167	struct rtentry *rt;
2168{
2169	struct ifnet *ifp;	/* my outgoing interface */
2170	struct in6_addr *ifp_ll6;
2171	struct in6_addr *router_ll6;
2172	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
2173	struct mbuf *m = NULL;	/* newly allocated one */
2174	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
2175	struct nd_redirect *nd_rd;
2176	size_t maxlen;
2177	u_char *p;
2178	struct ifnet *outif = NULL;
2179	struct sockaddr_in6 src_sa;
2180
2181	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2182
2183	/* if we are not router, we don't send icmp6 redirect */
2184	if (!ip6_forwarding || ip6_accept_rtadv)
2185		goto fail;
2186
2187	/* sanity check */
2188	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2189		goto fail;
2190
2191	/*
2192	 * Address check:
2193	 *  the source address must identify a neighbor, and
2194	 *  the destination address must not be a multicast address
2195	 *  [RFC 2461, sec 8.2]
2196	 */
2197	sip6 = mtod(m0, struct ip6_hdr *);
2198	bzero(&src_sa, sizeof(src_sa));
2199	src_sa.sin6_family = AF_INET6;
2200	src_sa.sin6_len = sizeof(src_sa);
2201	src_sa.sin6_addr = sip6->ip6_src;
2202	/* we don't currently use sin6_scope_id, but eventually use it */
2203	src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src);
2204	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2205		goto fail;
2206	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2207		goto fail;	/* what should we do here? */
2208
2209	/* rate limit */
2210	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2211		goto fail;
2212
2213	/*
2214	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2215	 * we almost always ask for an mbuf cluster for simplicity.
2216	 * (MHLEN < IPV6_MMTU is almost always true)
2217	 */
2218#if IPV6_MMTU >= MCLBYTES
2219# error assumption failed about IPV6_MMTU and MCLBYTES
2220#endif
2221	MGETHDR(m, M_DONTWAIT, MT_HEADER);
2222	if (m && IPV6_MMTU >= MHLEN)
2223		MCLGET(m, M_DONTWAIT);
2224	if (!m)
2225		goto fail;
2226	maxlen = (m->m_flags & M_EXT) ? MCLBYTES : MHLEN;
2227	maxlen = min(IPV6_MMTU, maxlen);
2228	/* just for safety */
2229	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2230	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2231		goto fail;
2232	}
2233
2234	{
2235		/* get ip6 linklocal address for ifp(my outgoing interface). */
2236		struct in6_ifaddr *ia;
2237		if ((ia = in6ifa_ifpforlinklocal(ifp,
2238						 IN6_IFF_NOTREADY|
2239						 IN6_IFF_ANYCAST)) == NULL)
2240			goto fail;
2241		ifp_ll6 = &ia->ia_addr.sin6_addr;
2242	}
2243
2244	/* get ip6 linklocal address for the router. */
2245	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2246		struct sockaddr_in6 *sin6;
2247		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2248		router_ll6 = &sin6->sin6_addr;
2249		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2250			router_ll6 = (struct in6_addr *)NULL;
2251	} else
2252		router_ll6 = (struct in6_addr *)NULL;
2253
2254	/* ip6 */
2255	ip6 = mtod(m, struct ip6_hdr *);
2256	ip6->ip6_flow = 0;
2257	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2258	ip6->ip6_vfc |= IPV6_VERSION;
2259	/* ip6->ip6_plen will be set later */
2260	ip6->ip6_nxt = IPPROTO_ICMPV6;
2261	ip6->ip6_hlim = 255;
2262	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
2263	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2264	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2265
2266	/* ND Redirect */
2267	nd_rd = (struct nd_redirect *)(ip6 + 1);
2268	nd_rd->nd_rd_type = ND_REDIRECT;
2269	nd_rd->nd_rd_code = 0;
2270	nd_rd->nd_rd_reserved = 0;
2271	if (rt->rt_flags & RTF_GATEWAY) {
2272		/*
2273		 * nd_rd->nd_rd_target must be a link-local address in
2274		 * better router cases.
2275		 */
2276		if (!router_ll6)
2277			goto fail;
2278		bcopy(router_ll6, &nd_rd->nd_rd_target,
2279		      sizeof(nd_rd->nd_rd_target));
2280		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2281		      sizeof(nd_rd->nd_rd_dst));
2282	} else {
2283		/* make sure redtgt == reddst */
2284		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2285		      sizeof(nd_rd->nd_rd_target));
2286		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2287		      sizeof(nd_rd->nd_rd_dst));
2288	}
2289
2290	p = (u_char *)(nd_rd + 1);
2291
2292	if (!router_ll6)
2293		goto nolladdropt;
2294
2295    {
2296	/* target lladdr option */
2297	struct rtentry *rt_router = NULL;
2298	int len;
2299	struct sockaddr_dl *sdl;
2300	struct nd_opt_hdr *nd_opt;
2301	char *lladdr;
2302
2303	rt_router = nd6_lookup(router_ll6, 0, ifp);
2304	if (!rt_router)
2305		goto nolladdropt;
2306	len = sizeof(*nd_opt) + ifp->if_addrlen;
2307	len = (len + 7) & ~7;	/*round by 8*/
2308	/* safety check */
2309	if (len + (p - (u_char *)ip6) > maxlen)
2310		goto nolladdropt;
2311	if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2312	    (rt_router->rt_flags & RTF_LLINFO) &&
2313	    (rt_router->rt_gateway->sa_family == AF_LINK) &&
2314	    (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2315	    sdl->sdl_alen) {
2316		nd_opt = (struct nd_opt_hdr *)p;
2317		nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2318		nd_opt->nd_opt_len = len >> 3;
2319		lladdr = (char *)(nd_opt + 1);
2320		bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2321		p += len;
2322	}
2323    }
2324nolladdropt:;
2325
2326	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2327
2328	/* just to be safe */
2329	if (m0->m_flags & M_DECRYPTED)
2330		goto noredhdropt;
2331	if (p - (u_char *)ip6 > maxlen)
2332		goto noredhdropt;
2333
2334    {
2335	/* redirected header option */
2336	int len;
2337	struct nd_opt_rd_hdr *nd_opt_rh;
2338
2339	/*
2340	 * compute the maximum size for icmp6 redirect header option.
2341	 * XXX room for auth header?
2342	 */
2343	len = maxlen - (p - (u_char *)ip6);
2344	len &= ~7;
2345
2346	/* This is just for simplicity. */
2347	if (m0->m_pkthdr.len != m0->m_len) {
2348		if (m0->m_next) {
2349			m_freem(m0->m_next);
2350			m0->m_next = NULL;
2351		}
2352		m0->m_pkthdr.len = m0->m_len;
2353	}
2354
2355	/*
2356	 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2357	 * about padding/truncate rule for the original IP packet.
2358	 * From the discussion on IPv6imp in Feb 1999, the consensus was:
2359	 * - "attach as much as possible" is the goal
2360	 * - pad if not aligned (original size can be guessed by original
2361	 *   ip6 header)
2362	 * Following code adds the padding if it is simple enough,
2363	 * and truncates if not.
2364	 */
2365	if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2366		panic("assumption failed in %s:%d\n", __FILE__, __LINE__);
2367
2368	if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2369		/* not enough room, truncate */
2370		m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2371	} else {
2372		/* enough room, pad or truncate */
2373		size_t extra;
2374
2375		extra = m0->m_pkthdr.len % 8;
2376		if (extra) {
2377			/* pad if easy enough, truncate if not */
2378			if (8 - extra <= M_TRAILINGSPACE(m0)) {
2379				/* pad */
2380				m0->m_len += (8 - extra);
2381				m0->m_pkthdr.len += (8 - extra);
2382			} else {
2383				/* truncate */
2384				m0->m_pkthdr.len -= extra;
2385				m0->m_len -= extra;
2386			}
2387		}
2388		len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2389		m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh);
2390	}
2391
2392	nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2393	bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2394	nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2395	nd_opt_rh->nd_opt_rh_len = len >> 3;
2396	p += sizeof(*nd_opt_rh);
2397	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2398
2399	/* connect m0 to m */
2400	m->m_next = m0;
2401	m->m_pkthdr.len = m->m_len + m0->m_len;
2402    }
2403noredhdropt:;
2404
2405	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src))
2406		sip6->ip6_src.s6_addr16[1] = 0;
2407	if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst))
2408		sip6->ip6_dst.s6_addr16[1] = 0;
2409#if 0
2410	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src))
2411		ip6->ip6_src.s6_addr16[1] = 0;
2412	if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst))
2413		ip6->ip6_dst.s6_addr16[1] = 0;
2414#endif
2415	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target))
2416		nd_rd->nd_rd_target.s6_addr16[1] = 0;
2417	if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst))
2418		nd_rd->nd_rd_dst.s6_addr16[1] = 0;
2419
2420	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2421
2422	nd_rd->nd_rd_cksum = 0;
2423	nd_rd->nd_rd_cksum
2424		= in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen));
2425
2426	/* send the packet to outside... */
2427#ifdef IPSEC
2428	/* Don't lookup socket */
2429	ipsec_setsocket(m, NULL);
2430#endif /*IPSEC*/
2431	ip6_output(m, NULL, NULL, 0, NULL, &outif);
2432	if (outif) {
2433		icmp6_ifstat_inc(outif, ifs6_out_msg);
2434		icmp6_ifstat_inc(outif, ifs6_out_redirect);
2435	}
2436	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2437
2438	return;
2439
2440fail:
2441	if (m)
2442		m_freem(m);
2443	if (m0)
2444		m_freem(m0);
2445}
2446
2447/*
2448 * ICMPv6 socket option processing.
2449 */
2450int
2451icmp6_ctloutput(op, so, level, optname, mp)
2452	int op;
2453	struct socket *so;
2454	int level, optname;
2455	struct mbuf **mp;
2456{
2457	register struct in6pcb *in6p = sotoin6pcb(so);
2458	register struct mbuf *m = *mp;
2459	int error = 0;
2460
2461	if (level != IPPROTO_ICMPV6) {
2462		error = EINVAL;
2463		if (op == PRCO_SETOPT && m)
2464			(void)m_free(m);
2465	} else switch(op) {
2466	 case PRCO_SETOPT:
2467		 switch (optname) {
2468		  case ICMP6_FILTER:
2469		  {
2470			  struct icmp6_filter *p;
2471
2472			  p = mtod(m, struct icmp6_filter *);
2473			  if (!p || !in6p->in6p_icmp6filt) {
2474				  error = EINVAL;
2475				  break;
2476			  }
2477			  bcopy(p, in6p->in6p_icmp6filt,
2478				sizeof(struct icmp6_filter));
2479			  error = 0;
2480			  break;
2481		  }
2482
2483		  default:
2484			  error = ENOPROTOOPT;
2485			  break;
2486		 }
2487		 if (m)
2488			 (void)m_free(m);
2489		 break;
2490
2491	 case PRCO_GETOPT:
2492		 switch (optname) {
2493		  case ICMP6_FILTER:
2494		  {
2495			  struct icmp6_filter *p;
2496
2497			  if (!in6p->in6p_icmp6filt) {
2498				  error = EINVAL;
2499				  break;
2500			  }
2501			  *mp = m = m_get(M_WAIT, MT_SOOPTS);
2502			  m->m_len = sizeof(struct icmp6_filter);
2503			  p = mtod(m, struct icmp6_filter *);
2504			  bcopy(in6p->in6p_icmp6filt, p,
2505				sizeof(struct icmp6_filter));
2506			  error = 0;
2507			  break;
2508		  }
2509
2510		  default:
2511			  error = ENOPROTOOPT;
2512			  break;
2513		 }
2514		 break;
2515	}
2516
2517	return(error);
2518}
2519
2520/*
2521 * Perform rate limit check.
2522 * Returns 0 if it is okay to send the icmp6 packet.
2523 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2524 * limitation.
2525 *
2526 * XXX per-destination/type check necessary?
2527 */
2528static int
2529icmp6_ratelimit(dst, type, code)
2530	const struct in6_addr *dst;	/* not used at this moment */
2531	const int type;			/* not used at this moment */
2532	const int code;			/* not used at this moment */
2533{
2534	int ret;
2535
2536	ret = 0;	/*okay to send*/
2537
2538	/* PPS limit */
2539	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2540	    icmp6errppslim)) {
2541		/* The packet is subject to rate limit */
2542		ret++;
2543	}
2544
2545	return ret;
2546}
2547
2548static struct rtentry *
2549icmp6_mtudisc_clone(dst)
2550	struct sockaddr *dst;
2551{
2552	struct rtentry *rt;
2553	int    error;
2554
2555	rt = rtalloc1(dst, 1);
2556	if (rt == 0)
2557		return NULL;
2558
2559	/* If we didn't get a host route, allocate one */
2560	if ((rt->rt_flags & RTF_HOST) == 0) {
2561		struct rtentry *nrt;
2562
2563		error = rtrequest((int) RTM_ADD, dst,
2564		    (struct sockaddr *) rt->rt_gateway,
2565		    (struct sockaddr *) 0,
2566		    RTF_GATEWAY | RTF_HOST | RTF_DYNAMIC, &nrt);
2567		if (error) {
2568			rtfree(rt);
2569			rtfree(nrt);
2570			return NULL;
2571		}
2572		nrt->rt_rmx = rt->rt_rmx;
2573		rtfree(rt);
2574		rt = nrt;
2575	}
2576	error = rt_timer_add(rt, icmp6_mtudisc_timeout,
2577			icmp6_mtudisc_timeout_q);
2578	if (error) {
2579		rtfree(rt);
2580		return NULL;
2581	}
2582
2583	return rt;	/* caller need to call rtfree() */
2584}
2585
2586static void
2587icmp6_mtudisc_timeout(rt, r)
2588	struct rtentry *rt;
2589	struct rttimer *r;
2590{
2591	if (rt == NULL)
2592		panic("icmp6_mtudisc_timeout: bad route to timeout");
2593	if ((rt->rt_flags & (RTF_DYNAMIC | RTF_HOST)) ==
2594	    (RTF_DYNAMIC | RTF_HOST)) {
2595		rtrequest((int) RTM_DELETE, (struct sockaddr *)rt_key(rt),
2596		    rt->rt_gateway, rt_mask(rt), rt->rt_flags, 0);
2597	} else {
2598		if ((rt->rt_rmx.rmx_locks & RTV_MTU) == 0) {
2599			rt->rt_rmx.rmx_mtu = 0;
2600		}
2601	}
2602}
2603
2604#include <uvm/uvm_extern.h>
2605#include <sys/sysctl.h>
2606
2607int
2608icmp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
2609	int *name;
2610	u_int namelen;
2611	void *oldp;
2612	size_t *oldlenp;
2613	void *newp;
2614	size_t newlen;
2615{
2616
2617	/* All sysctl names at this level are terminal. */
2618	if (namelen != 1)
2619		return ENOTDIR;
2620
2621	switch (name[0]) {
2622
2623	case ICMPV6CTL_REDIRACCEPT:
2624		return sysctl_int(oldp, oldlenp, newp, newlen,
2625				&icmp6_rediraccept);
2626	case ICMPV6CTL_REDIRTIMEOUT:
2627		return sysctl_int(oldp, oldlenp, newp, newlen,
2628				&icmp6_redirtimeout);
2629	case ICMPV6CTL_STATS:
2630		return sysctl_rdstruct(oldp, oldlenp, newp,
2631				&icmp6stat, sizeof(icmp6stat));
2632	case ICMPV6CTL_ND6_PRUNE:
2633		return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_prune);
2634	case ICMPV6CTL_ND6_DELAY:
2635		return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_delay);
2636	case ICMPV6CTL_ND6_UMAXTRIES:
2637		return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_umaxtries);
2638	case ICMPV6CTL_ND6_MMAXTRIES:
2639		return sysctl_int(oldp, oldlenp, newp, newlen, &nd6_mmaxtries);
2640	case ICMPV6CTL_ND6_USELOOPBACK:
2641		return sysctl_int(oldp, oldlenp, newp, newlen,
2642				&nd6_useloopback);
2643	case ICMPV6CTL_NODEINFO:
2644		return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6_nodeinfo);
2645	case ICMPV6CTL_ERRPPSLIMIT:
2646		return sysctl_int(oldp, oldlenp, newp, newlen, &icmp6errppslim);
2647	case ICMPV6CTL_ND6_MAXNUDHINT:
2648		return sysctl_int(oldp, oldlenp, newp, newlen,
2649				&nd6_maxnudhint);
2650	default:
2651		return ENOPROTOOPT;
2652	}
2653	/* NOTREACHED */
2654}
2655