icmp6.c revision 121472
1221828Sgrehan/*	$FreeBSD: head/sys/netinet6/icmp6.c 121472 2003-10-24 18:26:30Z ume $	*/
2221828Sgrehan/*	$KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 itojun Exp $	*/
3221828Sgrehan
4221828Sgrehan/*
5221828Sgrehan * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6221828Sgrehan * All rights reserved.
7221828Sgrehan *
8221828Sgrehan * Redistribution and use in source and binary forms, with or without
9221828Sgrehan * modification, are permitted provided that the following conditions
10221828Sgrehan * are met:
11221828Sgrehan * 1. Redistributions of source code must retain the above copyright
12221828Sgrehan *    notice, this list of conditions and the following disclaimer.
13221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
14221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
15221828Sgrehan *    documentation and/or other materials provided with the distribution.
16221828Sgrehan * 3. Neither the name of the project nor the names of its contributors
17221828Sgrehan *    may be used to endorse or promote products derived from this software
18221828Sgrehan *    without specific prior written permission.
19221828Sgrehan *
20221828Sgrehan * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21221828Sgrehan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22221828Sgrehan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24221828Sgrehan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25221828Sgrehan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26245678Sneel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27221828Sgrehan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29221828Sgrehan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30221828Sgrehan * SUCH DAMAGE.
31221828Sgrehan */
32265062Sneel
33265062Sneel/*
34265062Sneel * Copyright (c) 1982, 1986, 1988, 1993
35265062Sneel *	The Regents of the University of California.  All rights reserved.
36265203Sneel *
37265062Sneel * Redistribution and use in source and binary forms, with or without
38265062Sneel * modification, are permitted provided that the following conditions
39265062Sneel * are met:
40221828Sgrehan * 1. Redistributions of source code must retain the above copyright
41221828Sgrehan *    notice, this list of conditions and the following disclaimer.
42221828Sgrehan * 2. Redistributions in binary form must reproduce the above copyright
43221828Sgrehan *    notice, this list of conditions and the following disclaimer in the
44221828Sgrehan *    documentation and/or other materials provided with the distribution.
45262506Sneel * 3. All advertising materials mentioning features or use of this software
46221828Sgrehan *    must display the following acknowledgement:
47221828Sgrehan *	This product includes software developed by the University of
48221828Sgrehan *	California, Berkeley and its contributors.
49221828Sgrehan * 4. Neither the name of the University nor the names of its contributors
50258579Sneel *    may be used to endorse or promote products derived from this software
51258075Sneel *    without specific prior written permission.
52221828Sgrehan *
53256072Sneel * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54256072Sneel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55256072Sneel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56221828Sgrehan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57266573Sneel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58240922Sneel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59240922Sneel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60260466Sneel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61221828Sgrehan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62259782Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63256072Sneel * SUCH DAMAGE.
64256072Sneel *
65263780Sneel *	@(#)ip_icmp.c	8.2 (Berkeley) 1/4/94
66263780Sneel */
67221828Sgrehan
68221828Sgrehan#include "opt_inet.h"
69221828Sgrehan#include "opt_inet6.h"
70221828Sgrehan#include "opt_ipsec.h"
71221828Sgrehan
72221828Sgrehan#include <sys/param.h>
73221828Sgrehan#include <sys/domain.h>
74221828Sgrehan#include <sys/kernel.h>
75221828Sgrehan#include <sys/lock.h>
76221828Sgrehan#include <sys/malloc.h>
77221828Sgrehan#include <sys/mbuf.h>
78256072Sneel#include <sys/protosw.h>
79256072Sneel#include <sys/signalvar.h>
80259863Sneel#include <sys/socket.h>
81259863Sneel#include <sys/socketvar.h>
82221828Sgrehan#include <sys/sx.h>
83221828Sgrehan#include <sys/syslog.h>
84221828Sgrehan#include <sys/systm.h>
85221828Sgrehan#include <sys/time.h>
86259782Sjhb
87221828Sgrehan#include <net/if.h>
88221828Sgrehan#include <net/if_dl.h>
89221828Sgrehan#include <net/if_types.h>
90221828Sgrehan#include <net/route.h>
91221828Sgrehan
92221828Sgrehan#include <netinet/in.h>
93221828Sgrehan#include <netinet/in_pcb.h>
94221828Sgrehan#include <netinet/in_var.h>
95221828Sgrehan#include <netinet/ip6.h>
96221828Sgrehan#include <netinet/icmp6.h>
97256072Sneel#include <netinet6/in6_ifattach.h>
98256072Sneel#include <netinet6/in6_pcb.h>
99259863Sneel#include <netinet6/ip6protosw.h>
100259863Sneel#include <netinet6/ip6_var.h>
101221828Sgrehan#include <netinet6/mld6_var.h>
102221828Sgrehan#include <netinet6/nd6.h>
103221828Sgrehan
104221828Sgrehan#ifdef IPSEC
105221828Sgrehan#include <netinet6/ipsec.h>
106249396Sneel#include <netkey/key.h>
107221828Sgrehan#endif
108221828Sgrehan
109241041Sneel#ifdef FAST_IPSEC
110221828Sgrehan#include <netipsec/ipsec.h>
111221828Sgrehan#include <netipsec/key.h>
112256072Sneel#define	IPSEC
113256072Sneel#endif
114256072Sneel
115221828Sgrehan#include <net/net_osdep.h>
116221828Sgrehan
117256072Sneelextern struct domain inet6domain;
118256072Sneel
119256072Sneelstruct icmp6stat icmp6stat;
120221828Sgrehan
121221828Sgrehanextern struct inpcbhead ripcb;
122221828Sgrehanextern int icmp6errppslim;
123221828Sgrehanstatic int icmp6errpps_count = 0;
124221828Sgrehanstatic struct timeval icmp6errppslim_last;
125221828Sgrehanextern int icmp6_nodeinfo;
126221828Sgrehan
127265062Sneelstatic void icmp6_errcount __P((struct icmp6errstat *, int, int));
128221828Sgrehanstatic int icmp6_rip6_input __P((struct mbuf **, int));
129241982Sneelstatic int icmp6_ratelimit __P((const struct in6_addr *, const int, const int));
130241982Sneelstatic const char *icmp6_redirect_diag __P((struct in6_addr *,
131263211Stychon	struct in6_addr *, struct in6_addr *));
132263211Stychonstatic struct mbuf *ni6_input __P((struct mbuf *, int));
133263211Stychonstatic struct mbuf *ni6_nametodns __P((const char *, int, int));
134221828Sgrehanstatic int ni6_dnsmatch __P((const char *, int, const char *, int));
135221828Sgrehanstatic int ni6_addrs __P((struct icmp6_nodeinfo *, struct mbuf *,
136258075Sneel			  struct ifnet **, char *));
137258579Sneelstatic int ni6_store_addrs __P((struct icmp6_nodeinfo *, struct icmp6_nodeinfo *,
138221828Sgrehan				struct ifnet *, int));
139221828Sgrehanstatic int icmp6_notify_error __P((struct mbuf *, int, int, int));
140240922Sneel
141240922Sneel#ifdef COMPAT_RFC1885
142258075Sneelstatic struct route_in6 icmp6_reflect_rt;
143221828Sgrehan#endif
144223621Sgrehan
145240894Sneel
146265062Sneelvoid
147221828Sgrehanicmp6_init()
148221828Sgrehan{
149260619Sneel	mld6_init();
150260619Sneel}
151260619Sneel
152260619Sneelstatic void
153260619Sneelicmp6_errcount(stat, type, code)
154260619Sneel	struct icmp6errstat *stat;
155260619Sneel	int type, code;
156260619Sneel{
157260619Sneel	switch (type) {
158260619Sneel	case ICMP6_DST_UNREACH:
159260619Sneel		switch (code) {
160260619Sneel		case ICMP6_DST_UNREACH_NOROUTE:
161260619Sneel			stat->icp6errs_dst_unreach_noroute++;
162260619Sneel			return;
163260619Sneel		case ICMP6_DST_UNREACH_ADMIN:
164260619Sneel			stat->icp6errs_dst_unreach_admin++;
165260619Sneel			return;
166260619Sneel		case ICMP6_DST_UNREACH_BEYONDSCOPE:
167260619Sneel			stat->icp6errs_dst_unreach_beyondscope++;
168260619Sneel			return;
169260619Sneel		case ICMP6_DST_UNREACH_ADDR:
170260619Sneel			stat->icp6errs_dst_unreach_addr++;
171260619Sneel			return;
172260619Sneel		case ICMP6_DST_UNREACH_NOPORT:
173263780Sneel			stat->icp6errs_dst_unreach_noport++;
174263780Sneel			return;
175263780Sneel		}
176263780Sneel		break;
177263780Sneel	case ICMP6_PACKET_TOO_BIG:
178263780Sneel		stat->icp6errs_packet_too_big++;
179263780Sneel		return;
180260619Sneel	case ICMP6_TIME_EXCEEDED:
181221828Sgrehan		switch (code) {
182221828Sgrehan		case ICMP6_TIME_EXCEED_TRANSIT:
183221828Sgrehan			stat->icp6errs_time_exceed_transit++;
184221828Sgrehan			return;
185221828Sgrehan		case ICMP6_TIME_EXCEED_REASSEMBLY:
186221828Sgrehan			stat->icp6errs_time_exceed_reassembly++;
187221828Sgrehan			return;
188221828Sgrehan		}
189221828Sgrehan		break;
190241489Sneel	case ICMP6_PARAM_PROB:
191241489Sneel		switch (code) {
192256072Sneel		case ICMP6_PARAMPROB_HEADER:
193241489Sneel			stat->icp6errs_paramprob_header++;
194256072Sneel			return;
195241489Sneel		case ICMP6_PARAMPROB_NEXTHEADER:
196221828Sgrehan			stat->icp6errs_paramprob_nextheader++;
197259737Sneel			return;
198259737Sneel		case ICMP6_PARAMPROB_OPTION:
199249879Sgrehan			stat->icp6errs_paramprob_option++;
200221828Sgrehan			return;
201221828Sgrehan		}
202249879Sgrehan		break;
203221828Sgrehan	case ND_REDIRECT:
204249879Sgrehan		stat->icp6errs_redirect++;
205221828Sgrehan		return;
206221828Sgrehan	}
207241489Sneel	stat->icp6errs_unknown++;
208259863Sneel}
209256072Sneel
210256072Sneel/*
211256072Sneel * Generate an error packet of type error in response to bad IP6 packet.
212263035Stychon */
213263744Stychonvoid
214262506Sneelicmp6_error(m, type, code, param)
215262506Sneel	struct mbuf *m;
216262506Sneel	int type, code, param;
217262506Sneel{
218262506Sneel	struct ip6_hdr *oip6, *nip6;
219262506Sneel	struct icmp6_hdr *icmp6;
220262506Sneel	u_int preplen;
221262506Sneel	int off;
222262506Sneel	int nxt;
223262506Sneel
224262506Sneel	icmp6stat.icp6s_error++;
225262506Sneel
226262506Sneel	/* count per-type-code statistics */
227262506Sneel	icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code);
228262506Sneel
229262506Sneel#ifdef M_DECRYPTED	/*not openbsd*/
230262506Sneel	if (m->m_flags & M_DECRYPTED) {
231262506Sneel		icmp6stat.icp6s_canterror++;
232262506Sneel		goto freeit;
233262506Sneel	}
234262506Sneel#endif
235262506Sneel
236262506Sneel#ifndef PULLDOWN_TEST
237262506Sneel	IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), );
238262506Sneel#else
239262506Sneel	if (m->m_len < sizeof(struct ip6_hdr)) {
240266626Sneel		m = m_pullup(m, sizeof(struct ip6_hdr));
241262506Sneel		if (m == NULL)
242266573Sneel			return;
243266573Sneel	}
244221828Sgrehan#endif
245221828Sgrehan	oip6 = mtod(m, struct ip6_hdr *);
246243640Sneel
247243640Sneel	/*
248255438Sgrehan	 * If the destination address of the erroneous packet is a multicast
249221828Sgrehan	 * address, or the packet was sent using link-layer multicast,
250221828Sgrehan	 * we should basically suppress sending an error (RFC 2463, Section
251221828Sgrehan	 * 2.4).
252221828Sgrehan	 * We have two exceptions (the item e.2 in that section):
253221828Sgrehan	 * - the Pakcet Too Big message can be sent for path MTU discovery.
254221828Sgrehan	 * - the Parameter Problem Message that can be allowed an icmp6 error
255221828Sgrehan	 *   in the option type field.  This check has been done in
256221828Sgrehan	 *   ip6_unknown_opt(), so we can just check the type and code.
257221828Sgrehan	 */
258221828Sgrehan	if ((m->m_flags & (M_BCAST|M_MCAST) ||
259221828Sgrehan	     IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) &&
260221828Sgrehan	    (type != ICMP6_PACKET_TOO_BIG &&
261221828Sgrehan	     (type != ICMP6_PARAM_PROB ||
262221828Sgrehan	      code != ICMP6_PARAMPROB_OPTION)))
263221828Sgrehan		goto freeit;
264221828Sgrehan
265221828Sgrehan	/*
266221828Sgrehan	 * RFC 2463, 2.4 (e.5): source address check.
267221828Sgrehan	 * XXX: the case of anycast source?
268221828Sgrehan	 */
269221828Sgrehan	if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) ||
270221828Sgrehan	    IN6_IS_ADDR_MULTICAST(&oip6->ip6_src))
271221828Sgrehan		goto freeit;
272221828Sgrehan
273221828Sgrehan	/*
274221828Sgrehan	 * If we are about to send ICMPv6 against ICMPv6 error/redirect,
275221828Sgrehan	 * don't do it.
276221828Sgrehan	 */
277221828Sgrehan	nxt = -1;
278221828Sgrehan	off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
279221828Sgrehan	if (off >= 0 && nxt == IPPROTO_ICMPV6) {
280221828Sgrehan		struct icmp6_hdr *icp;
281221828Sgrehan
282221828Sgrehan#ifndef PULLDOWN_TEST
283221828Sgrehan		IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), );
284221828Sgrehan		icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
285221828Sgrehan#else
286221828Sgrehan		IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off,
287266626Sneel			sizeof(*icp));
288221828Sgrehan		if (icp == NULL) {
289221828Sgrehan			icmp6stat.icp6s_tooshort++;
290221828Sgrehan			return;
291221828Sgrehan		}
292221828Sgrehan#endif
293221828Sgrehan		if (icp->icmp6_type < ICMP6_ECHO_REQUEST ||
294221828Sgrehan		    icp->icmp6_type == ND_REDIRECT) {
295221828Sgrehan			/*
296221828Sgrehan			 * ICMPv6 error
297221828Sgrehan			 * Special case: for redirect (which is
298221828Sgrehan			 * informational) we must not send icmp6 error.
299256645Sneel			 */
300221828Sgrehan			icmp6stat.icp6s_canterror++;
301221828Sgrehan			goto freeit;
302221828Sgrehan		} else {
303240922Sneel			/* ICMPv6 informational - send the error */
304262236Sneel		}
305240922Sneel	} else {
306240922Sneel		/* non-ICMPv6 - send the error */
307240922Sneel	}
308240922Sneel
309266125Sjhb	oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */
310266125Sjhb
311266125Sjhb	/* Finally, do rate limitation check. */
312266125Sjhb	if (icmp6_ratelimit(&oip6->ip6_src, type, code)) {
313266125Sjhb		icmp6stat.icp6s_toofreq++;
314221828Sgrehan		goto freeit;
315221828Sgrehan	}
316221828Sgrehan
317221828Sgrehan	/*
318221828Sgrehan	 * OK, ICMP6 can be generated.
319221828Sgrehan	 */
320221828Sgrehan
321221828Sgrehan	if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN)
322221828Sgrehan		m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len);
323221828Sgrehan
324221828Sgrehan	preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
325221828Sgrehan	M_PREPEND(m, preplen, M_DONTWAIT);
326221828Sgrehan	if (m && m->m_len < preplen)
327221828Sgrehan		m = m_pullup(m, preplen);
328221828Sgrehan	if (m == NULL) {
329221828Sgrehan		nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__));
330221828Sgrehan		return;
331221828Sgrehan	}
332221828Sgrehan
333221828Sgrehan	nip6 = mtod(m, struct ip6_hdr *);
334221828Sgrehan	nip6->ip6_src  = oip6->ip6_src;
335221828Sgrehan	nip6->ip6_dst  = oip6->ip6_dst;
336234761Sgrehan
337256072Sneel	in6_clearscope(&oip6->ip6_src);
338240912Sneel	in6_clearscope(&oip6->ip6_dst);
339265101Sneel
340260619Sneel	icmp6 = (struct icmp6_hdr *)(nip6 + 1);
341261170Sneel	icmp6->icmp6_type = type;
342263780Sneel	icmp6->icmp6_code = code;
343266573Sneel	icmp6->icmp6_pptr = htonl((u_int32_t)param);
344234761Sgrehan
345221828Sgrehan	/*
346221828Sgrehan	 * icmp6_reflect() is designed to be in the input path.
347266573Sneel	 * icmp6_error() can be called from both input and outut path,
348266573Sneel	 * and if we are in output path rcvif could contain bogus value.
349266573Sneel	 * clear m->m_pkthdr.rcvif for safety, we should have enough scope
350266573Sneel	 * information in ip header (nip6).
351266573Sneel	 */
352266573Sneel	m->m_pkthdr.rcvif = NULL;
353266573Sneel
354266573Sneel	icmp6stat.icp6s_outhist[type]++;
355266573Sneel	icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */
356266573Sneel
357266573Sneel	return;
358266573Sneel
359266573Sneel  freeit:
360266573Sneel	/*
361266573Sneel	 * If we can't tell wheter or not we can generate ICMP6, free it.
362266573Sneel	 */
363266573Sneel	m_freem(m);
364266573Sneel}
365266573Sneel
366266573Sneel/*
367266573Sneel * Process a received ICMP6 message.
368266573Sneel */
369266573Sneelint
370266573Sneelicmp6_input(mp, offp, proto)
371266573Sneel	struct mbuf **mp;
372266573Sneel	int *offp, proto;
373221828Sgrehan{
374221828Sgrehan	struct mbuf *m = *mp, *n;
375221828Sgrehan	struct ip6_hdr *ip6, *nip6;
376221828Sgrehan	struct icmp6_hdr *icmp6, *nicmp6;
377221828Sgrehan	int off = *offp;
378266573Sneel	int icmp6len = m->m_pkthdr.len - *offp;
379266573Sneel	int code, sum, noff;
380221828Sgrehan
381241497Sgrehan#ifndef PULLDOWN_TEST
382256072Sneel	IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), IPPROTO_DONE);
383256072Sneel	/* m might change if M_LOOP.  So, call mtod after this */
384256072Sneel#endif
385256072Sneel
386256072Sneel	/*
387256072Sneel	 * Locate icmp6 structure in mbuf, and check
388261504Sjhb	 * that not corrupted and of at least minimum length
389261504Sjhb	 */
390266424Sneel
391243640Sneel	ip6 = mtod(m, struct ip6_hdr *);
392256072Sneel	if (icmp6len < sizeof(struct icmp6_hdr)) {
393221828Sgrehan		icmp6stat.icp6s_tooshort++;
394221828Sgrehan		goto freeit;
395221828Sgrehan	}
396221828Sgrehan
397221828Sgrehan	/*
398260167Sneel	 * calculate the checksum
399260167Sneel	 */
400260167Sneel#ifndef PULLDOWN_TEST
401260167Sneel	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
402260167Sneel#else
403221828Sgrehan	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
404221828Sgrehan	if (icmp6 == NULL) {
405260167Sneel		icmp6stat.icp6s_tooshort++;
406260167Sneel		return IPPROTO_DONE;
407260167Sneel	}
408260167Sneel#endif
409260167Sneel	code = icmp6->icmp6_code;
410260167Sneel
411221828Sgrehan	if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) {
412221828Sgrehan		nd6log((LOG_ERR,
413221828Sgrehan		    "ICMP6 checksum error(%d|%x) %s\n",
414221828Sgrehan		    icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src)));
415221828Sgrehan		icmp6stat.icp6s_checksum++;
416240912Sneel		goto freeit;
417240912Sneel	}
418240912Sneel
419240912Sneel	if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
420259081Sneel		/*
421259081Sneel		 * Deliver very specific ICMP6 type only.
422259081Sneel		 * This is important to deilver TOOBIG.  Otherwise PMTUD
423261170Sneel		 * will not work.
424261170Sneel		 */
425261170Sneel		switch (icmp6->icmp6_type) {
426265062Sneel		case ICMP6_DST_UNREACH:
427265062Sneel		case ICMP6_PACKET_TOO_BIG:
428265062Sneel		case ICMP6_TIME_EXCEEDED:
429221828Sgrehan			break;
430221828Sgrehan		default:
431221828Sgrehan			goto freeit;
432221828Sgrehan		}
433	}
434
435	icmp6stat.icp6s_inhist[icmp6->icmp6_type]++;
436	icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg);
437	if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK)
438		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error);
439
440	switch (icmp6->icmp6_type) {
441	case ICMP6_DST_UNREACH:
442		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach);
443		switch (code) {
444		case ICMP6_DST_UNREACH_NOROUTE:
445			code = PRC_UNREACH_NET;
446			break;
447		case ICMP6_DST_UNREACH_ADMIN:
448			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib);
449			code = PRC_UNREACH_PROTOCOL; /* is this a good code? */
450			break;
451		case ICMP6_DST_UNREACH_ADDR:
452			code = PRC_HOSTDEAD;
453			break;
454#ifdef COMPAT_RFC1885
455		case ICMP6_DST_UNREACH_NOTNEIGHBOR:
456			code = PRC_UNREACH_SRCFAIL;
457			break;
458#else
459		case ICMP6_DST_UNREACH_BEYONDSCOPE:
460			/* I mean "source address was incorrect." */
461			code = PRC_PARAMPROB;
462			break;
463#endif
464		case ICMP6_DST_UNREACH_NOPORT:
465			code = PRC_UNREACH_PORT;
466			break;
467		default:
468			goto badcode;
469		}
470		goto deliver;
471		break;
472
473	case ICMP6_PACKET_TOO_BIG:
474		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig);
475		if (code != 0)
476			goto badcode;
477
478		code = PRC_MSGSIZE;
479
480		/*
481		 * Updating the path MTU will be done after examining
482		 * intermediate extension headers.
483		 */
484		goto deliver;
485		break;
486
487	case ICMP6_TIME_EXCEEDED:
488		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed);
489		switch (code) {
490		case ICMP6_TIME_EXCEED_TRANSIT:
491		case ICMP6_TIME_EXCEED_REASSEMBLY:
492			code += PRC_TIMXCEED_INTRANS;
493			break;
494		default:
495			goto badcode;
496		}
497		goto deliver;
498		break;
499
500	case ICMP6_PARAM_PROB:
501		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob);
502		switch (code) {
503		case ICMP6_PARAMPROB_NEXTHEADER:
504			code = PRC_UNREACH_PROTOCOL;
505			break;
506		case ICMP6_PARAMPROB_HEADER:
507		case ICMP6_PARAMPROB_OPTION:
508			code = PRC_PARAMPROB;
509			break;
510		default:
511			goto badcode;
512		}
513		goto deliver;
514		break;
515
516	case ICMP6_ECHO_REQUEST:
517		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo);
518		if (code != 0)
519			goto badcode;
520		if ((n = m_copy(m, 0, M_COPYALL)) == NULL) {
521			/* Give up remote */
522			break;
523		}
524		if ((n->m_flags & M_EXT) != 0
525		 || n->m_len < off + sizeof(struct icmp6_hdr)) {
526			struct mbuf *n0 = n;
527			const int maxlen = sizeof(*nip6) + sizeof(*nicmp6);
528			int n0len;
529
530			/*
531			 * Prepare an internal mbuf.  m_pullup() doesn't
532			 * always copy the length we specified.
533			 */
534			if (maxlen >= MCLBYTES) {
535				/* Give up remote */
536				m_freem(n0);
537				break;
538			}
539			MGETHDR(n, M_DONTWAIT, n0->m_type);
540			n0len = n0->m_pkthdr.len;	/* save for use below */
541			if (n)
542				M_MOVE_PKTHDR(n, n0);
543			if (n && maxlen >= MHLEN) {
544				MCLGET(n, M_DONTWAIT);
545				if ((n->m_flags & M_EXT) == 0) {
546					m_free(n);
547					n = NULL;
548				}
549			}
550			if (n == NULL) {
551				/* Give up remote */
552				m_freem(n0);
553				break;
554			}
555			/*
556			 * Copy IPv6 and ICMPv6 only.
557			 */
558			nip6 = mtod(n, struct ip6_hdr *);
559			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
560			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
561			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
562			noff = sizeof(struct ip6_hdr);
563			/* new mbuf contains only ipv6+icmpv6 headers */
564			n->m_len = noff + sizeof(struct icmp6_hdr);
565			/*
566			 * Adjust mbuf.  ip6_plen will be adjusted in
567			 * ip6_output().
568			 */
569			m_adj(n0, off + sizeof(struct icmp6_hdr));
570			/* recalculate complete packet size */
571			n->m_pkthdr.len = n0len + (noff - off);
572			n->m_next = n0;
573		} else {
574			nip6 = mtod(n, struct ip6_hdr *);
575			nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off);
576			noff = off;
577		}
578		nicmp6->icmp6_type = ICMP6_ECHO_REPLY;
579		nicmp6->icmp6_code = 0;
580		if (n) {
581			icmp6stat.icp6s_reflect++;
582			icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++;
583			icmp6_reflect(n, noff);
584		}
585		break;
586
587	case ICMP6_ECHO_REPLY:
588		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply);
589		if (code != 0)
590			goto badcode;
591		break;
592
593	case MLD_LISTENER_QUERY:
594	case MLD_LISTENER_REPORT:
595		if (icmp6len < sizeof(struct mld_hdr))
596			goto badlen;
597		if (icmp6->icmp6_type == MLD_LISTENER_QUERY) /* XXX: ugly... */
598			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery);
599		else
600			icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport);
601		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
602			/* give up local */
603			mld6_input(m, off);
604			m = NULL;
605			goto freeit;
606		}
607		mld6_input(n, off);
608		/* m stays. */
609		break;
610
611	case MLD_LISTENER_DONE:
612		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone);
613		if (icmp6len < sizeof(struct mld_hdr))	/* necessary? */
614			goto badlen;
615		break;		/* nothing to be done in kernel */
616
617	case MLD_MTRACE_RESP:
618	case MLD_MTRACE:
619		/* XXX: these two are experimental.  not officially defined. */
620		/* XXX: per-interface statistics? */
621		break;		/* just pass it to applications */
622
623	case ICMP6_WRUREQUEST:	/* ICMP6_FQDN_QUERY */
624	    {
625		enum { WRU, FQDN } mode;
626
627		if (!icmp6_nodeinfo)
628			break;
629
630		if (icmp6len == sizeof(struct icmp6_hdr) + 4)
631			mode = WRU;
632		else if (icmp6len >= sizeof(struct icmp6_nodeinfo))
633			mode = FQDN;
634		else
635			goto badlen;
636
637#define hostnamelen	strlen(hostname)
638		if (mode == FQDN) {
639#ifndef PULLDOWN_TEST
640			IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo),
641			    IPPROTO_DONE);
642#endif
643			n = m_copy(m, 0, M_COPYALL);
644			if (n)
645				n = ni6_input(n, off);
646			/* XXX meaningless if n == NULL */
647			noff = sizeof(struct ip6_hdr);
648		} else {
649			u_char *p;
650			int maxlen, maxhlen;
651
652			if ((icmp6_nodeinfo & 5) != 5)
653				break;
654
655			if (code != 0)
656				goto badcode;
657			maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4;
658			if (maxlen >= MCLBYTES) {
659				/* Give up remote */
660				break;
661			}
662			MGETHDR(n, M_DONTWAIT, m->m_type);
663			if (n && maxlen > MHLEN) {
664				MCLGET(n, M_DONTWAIT);
665				if ((n->m_flags & M_EXT) == 0) {
666					m_free(n);
667					n = NULL;
668				}
669			}
670			if (!m_dup_pkthdr(n, m, M_DONTWAIT)) {
671				/*
672				 * Previous code did a blind M_COPY_PKTHDR
673				 * and said "just for rcvif".  If true, then
674				 * we could tolerate the dup failing (due to
675				 * the deep copy of the tag chain).  For now
676				 * be conservative and just fail.
677				 */
678				m_free(n);
679				n = NULL;
680			}
681			if (n == NULL) {
682				/* Give up remote */
683				break;
684			}
685			n->m_pkthdr.rcvif = NULL;
686			n->m_len = 0;
687			maxhlen = M_TRAILINGSPACE(n) - maxlen;
688			if (maxhlen > hostnamelen)
689				maxhlen = hostnamelen;
690			/*
691			 * Copy IPv6 and ICMPv6 only.
692			 */
693			nip6 = mtod(n, struct ip6_hdr *);
694			bcopy(ip6, nip6, sizeof(struct ip6_hdr));
695			nicmp6 = (struct icmp6_hdr *)(nip6 + 1);
696			bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr));
697			p = (u_char *)(nicmp6 + 1);
698			bzero(p, 4);
699			bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */
700			noff = sizeof(struct ip6_hdr);
701			n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
702				sizeof(struct icmp6_hdr) + 4 + maxhlen;
703			nicmp6->icmp6_type = ICMP6_WRUREPLY;
704			nicmp6->icmp6_code = 0;
705		}
706#undef hostnamelen
707		if (n) {
708			icmp6stat.icp6s_reflect++;
709			icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++;
710			icmp6_reflect(n, noff);
711		}
712		break;
713	    }
714
715	case ICMP6_WRUREPLY:
716		if (code != 0)
717			goto badcode;
718		break;
719
720	case ND_ROUTER_SOLICIT:
721		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit);
722		if (code != 0)
723			goto badcode;
724		if (icmp6len < sizeof(struct nd_router_solicit))
725			goto badlen;
726		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
727			/* give up local */
728			nd6_rs_input(m, off, icmp6len);
729			m = NULL;
730			goto freeit;
731		}
732		nd6_rs_input(n, off, icmp6len);
733		/* m stays. */
734		break;
735
736	case ND_ROUTER_ADVERT:
737		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert);
738		if (code != 0)
739			goto badcode;
740		if (icmp6len < sizeof(struct nd_router_advert))
741			goto badlen;
742		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
743			/* give up local */
744			nd6_ra_input(m, off, icmp6len);
745			m = NULL;
746			goto freeit;
747		}
748		nd6_ra_input(n, off, icmp6len);
749		/* m stays. */
750		break;
751
752	case ND_NEIGHBOR_SOLICIT:
753		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit);
754		if (code != 0)
755			goto badcode;
756		if (icmp6len < sizeof(struct nd_neighbor_solicit))
757			goto badlen;
758		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
759			/* give up local */
760			nd6_ns_input(m, off, icmp6len);
761			m = NULL;
762			goto freeit;
763		}
764		nd6_ns_input(n, off, icmp6len);
765		/* m stays. */
766		break;
767
768	case ND_NEIGHBOR_ADVERT:
769		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert);
770		if (code != 0)
771			goto badcode;
772		if (icmp6len < sizeof(struct nd_neighbor_advert))
773			goto badlen;
774		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
775			/* give up local */
776			nd6_na_input(m, off, icmp6len);
777			m = NULL;
778			goto freeit;
779		}
780		nd6_na_input(n, off, icmp6len);
781		/* m stays. */
782		break;
783
784	case ND_REDIRECT:
785		icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect);
786		if (code != 0)
787			goto badcode;
788		if (icmp6len < sizeof(struct nd_redirect))
789			goto badlen;
790		if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) {
791			/* give up local */
792			icmp6_redirect_input(m, off);
793			m = NULL;
794			goto freeit;
795		}
796		icmp6_redirect_input(n, off);
797		/* m stays. */
798		break;
799
800	case ICMP6_ROUTER_RENUMBERING:
801		if (code != ICMP6_ROUTER_RENUMBERING_COMMAND &&
802		    code != ICMP6_ROUTER_RENUMBERING_RESULT)
803			goto badcode;
804		if (icmp6len < sizeof(struct icmp6_router_renum))
805			goto badlen;
806		break;
807
808	default:
809		nd6log((LOG_DEBUG,
810		    "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n",
811		    icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src),
812		    ip6_sprintf(&ip6->ip6_dst),
813		    m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0));
814		if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) {
815			/* ICMPv6 error: MUST deliver it by spec... */
816			code = PRC_NCMDS;
817			/* deliver */
818		} else {
819			/* ICMPv6 informational: MUST not deliver */
820			break;
821		}
822	deliver:
823		if (icmp6_notify_error(m, off, icmp6len, code)) {
824			/* In this case, m should've been freed. */
825			return (IPPROTO_DONE);
826		}
827		break;
828
829	badcode:
830		icmp6stat.icp6s_badcode++;
831		break;
832
833	badlen:
834		icmp6stat.icp6s_badlen++;
835		break;
836	}
837
838	/* deliver the packet to appropriate sockets */
839	icmp6_rip6_input(&m, *offp);
840
841	return IPPROTO_DONE;
842
843 freeit:
844	m_freem(m);
845	return IPPROTO_DONE;
846}
847
848static int
849icmp6_notify_error(m, off, icmp6len, code)
850	struct mbuf *m;
851	int off, icmp6len, code;
852{
853	struct icmp6_hdr *icmp6;
854	struct ip6_hdr *eip6;
855	u_int32_t notifymtu;
856	struct sockaddr_in6 icmp6src, icmp6dst;
857
858	if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) {
859		icmp6stat.icp6s_tooshort++;
860		goto freeit;
861	}
862#ifndef PULLDOWN_TEST
863	IP6_EXTHDR_CHECK(m, off,
864	    sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), -1);
865	icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
866#else
867	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
868	    sizeof(*icmp6) + sizeof(struct ip6_hdr));
869	if (icmp6 == NULL) {
870		icmp6stat.icp6s_tooshort++;
871		return (-1);
872	}
873#endif
874	eip6 = (struct ip6_hdr *)(icmp6 + 1);
875
876	/* Detect the upper level protocol */
877	{
878		void (*ctlfunc) __P((int, struct sockaddr *, void *));
879		u_int8_t nxt = eip6->ip6_nxt;
880		int eoff = off + sizeof(struct icmp6_hdr) +
881		    sizeof(struct ip6_hdr);
882		struct ip6ctlparam ip6cp;
883		struct in6_addr *finaldst = NULL;
884		int icmp6type = icmp6->icmp6_type;
885		struct ip6_frag *fh;
886		struct ip6_rthdr *rth;
887		struct ip6_rthdr0 *rth0;
888		int rthlen;
889
890		while (1) { /* XXX: should avoid infinite loop explicitly? */
891			struct ip6_ext *eh;
892
893			switch (nxt) {
894			case IPPROTO_HOPOPTS:
895			case IPPROTO_DSTOPTS:
896			case IPPROTO_AH:
897#ifndef PULLDOWN_TEST
898				IP6_EXTHDR_CHECK(m, 0,
899				    eoff + sizeof(struct ip6_ext), -1);
900				eh = (struct ip6_ext *)(mtod(m, caddr_t) + eoff);
901#else
902				IP6_EXTHDR_GET(eh, struct ip6_ext *, m,
903				    eoff, sizeof(*eh));
904				if (eh == NULL) {
905					icmp6stat.icp6s_tooshort++;
906					return (-1);
907				}
908#endif
909
910				if (nxt == IPPROTO_AH)
911					eoff += (eh->ip6e_len + 2) << 2;
912				else
913					eoff += (eh->ip6e_len + 1) << 3;
914				nxt = eh->ip6e_nxt;
915				break;
916			case IPPROTO_ROUTING:
917				/*
918				 * When the erroneous packet contains a
919				 * routing header, we should examine the
920				 * header to determine the final destination.
921				 * Otherwise, we can't properly update
922				 * information that depends on the final
923				 * destination (e.g. path MTU).
924				 */
925#ifndef PULLDOWN_TEST
926				IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), -1);
927				rth = (struct ip6_rthdr *)
928				    (mtod(m, caddr_t) + eoff);
929#else
930				IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m,
931				    eoff, sizeof(*rth));
932				if (rth == NULL) {
933					icmp6stat.icp6s_tooshort++;
934					return (-1);
935				}
936#endif
937				rthlen = (rth->ip6r_len + 1) << 3;
938				/*
939				 * XXX: currently there is no
940				 * officially defined type other
941				 * than type-0.
942				 * Note that if the segment left field
943				 * is 0, all intermediate hops must
944				 * have been passed.
945				 */
946				if (rth->ip6r_segleft &&
947				    rth->ip6r_type == IPV6_RTHDR_TYPE_0) {
948					int hops;
949
950#ifndef PULLDOWN_TEST
951					IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, -1);
952					rth0 = (struct ip6_rthdr0 *)
953					    (mtod(m, caddr_t) + eoff);
954#else
955					IP6_EXTHDR_GET(rth0,
956					    struct ip6_rthdr0 *, m,
957					    eoff, rthlen);
958					if (rth0 == NULL) {
959						icmp6stat.icp6s_tooshort++;
960						return (-1);
961					}
962#endif
963					/* just ignore a bogus header */
964					if ((rth0->ip6r0_len % 2) == 0 &&
965					    (hops = rth0->ip6r0_len/2))
966						finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1);
967				}
968				eoff += rthlen;
969				nxt = rth->ip6r_nxt;
970				break;
971			case IPPROTO_FRAGMENT:
972#ifndef PULLDOWN_TEST
973				IP6_EXTHDR_CHECK(m, 0, eoff +
974				    sizeof(struct ip6_frag), -1);
975				fh = (struct ip6_frag *)(mtod(m, caddr_t) +
976				    eoff);
977#else
978				IP6_EXTHDR_GET(fh, struct ip6_frag *, m,
979				    eoff, sizeof(*fh));
980				if (fh == NULL) {
981					icmp6stat.icp6s_tooshort++;
982					return (-1);
983				}
984#endif
985				/*
986				 * Data after a fragment header is meaningless
987				 * unless it is the first fragment, but
988				 * we'll go to the notify label for path MTU
989				 * discovery.
990				 */
991				if (fh->ip6f_offlg & IP6F_OFF_MASK)
992					goto notify;
993
994				eoff += sizeof(struct ip6_frag);
995				nxt = fh->ip6f_nxt;
996				break;
997			default:
998				/*
999				 * This case includes ESP and the No Next
1000				 * Header.  In such cases going to the notify
1001				 * label does not have any meaning
1002				 * (i.e. ctlfunc will be NULL), but we go
1003				 * anyway since we might have to update
1004				 * path MTU information.
1005				 */
1006				goto notify;
1007			}
1008		}
1009	  notify:
1010#ifndef PULLDOWN_TEST
1011		icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off);
1012#else
1013		IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off,
1014		    sizeof(*icmp6) + sizeof(struct ip6_hdr));
1015		if (icmp6 == NULL) {
1016			icmp6stat.icp6s_tooshort++;
1017			return (-1);
1018		}
1019#endif
1020
1021		/*
1022		 * retrieve parameters from the inner IPv6 header, and convert
1023		 * them into sockaddr structures.
1024		 * XXX: there is no guarantee that the source or destination
1025		 * addresses of the inner packet are in the same scope as
1026		 * the addresses of the icmp packet.  But there is no other
1027		 * way to determine the zone.
1028		 */
1029		eip6 = (struct ip6_hdr *)(icmp6 + 1);
1030
1031		bzero(&icmp6dst, sizeof(icmp6dst));
1032		icmp6dst.sin6_len = sizeof(struct sockaddr_in6);
1033		icmp6dst.sin6_family = AF_INET6;
1034		if (finaldst == NULL)
1035			icmp6dst.sin6_addr = eip6->ip6_dst;
1036		else
1037			icmp6dst.sin6_addr = *finaldst;
1038		if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6dst.sin6_addr,
1039		    &icmp6dst.sin6_scope_id))
1040			goto freeit;
1041		if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst,
1042				   NULL, NULL)) {
1043			/* should be impossbile */
1044			nd6log((LOG_DEBUG,
1045			    "icmp6_notify_error: in6_embedscope failed\n"));
1046			goto freeit;
1047		}
1048
1049		/*
1050		 * retrieve parameters from the inner IPv6 header, and convert
1051		 * them into sockaddr structures.
1052		 */
1053		bzero(&icmp6src, sizeof(icmp6src));
1054		icmp6src.sin6_len = sizeof(struct sockaddr_in6);
1055		icmp6src.sin6_family = AF_INET6;
1056		icmp6src.sin6_addr = eip6->ip6_src;
1057		if (in6_addr2zoneid(m->m_pkthdr.rcvif, &icmp6src.sin6_addr,
1058		    &icmp6src.sin6_scope_id)) {
1059			goto freeit;
1060		}
1061		if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src,
1062				   NULL, NULL)) {
1063			/* should be impossbile */
1064			nd6log((LOG_DEBUG,
1065			    "icmp6_notify_error: in6_embedscope failed\n"));
1066			goto freeit;
1067		}
1068		icmp6src.sin6_flowinfo = (eip6->ip6_flow & IPV6_FLOWLABEL_MASK);
1069
1070		if (finaldst == NULL)
1071			finaldst = &eip6->ip6_dst;
1072		ip6cp.ip6c_m = m;
1073		ip6cp.ip6c_icmp6 = icmp6;
1074		ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1);
1075		ip6cp.ip6c_off = eoff;
1076		ip6cp.ip6c_finaldst = finaldst;
1077		ip6cp.ip6c_src = &icmp6src;
1078		ip6cp.ip6c_nxt = nxt;
1079
1080		if (icmp6type == ICMP6_PACKET_TOO_BIG) {
1081			notifymtu = ntohl(icmp6->icmp6_mtu);
1082			ip6cp.ip6c_cmdarg = (void *)&notifymtu;
1083			icmp6_mtudisc_update(&ip6cp, 1);	/*XXX*/
1084		}
1085
1086		ctlfunc = (void (*) __P((int, struct sockaddr *, void *)))
1087		    (inet6sw[ip6_protox[nxt]].pr_ctlinput);
1088		if (ctlfunc) {
1089			(void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst,
1090			    &ip6cp);
1091		}
1092	}
1093	return (0);
1094
1095  freeit:
1096	m_freem(m);
1097	return (-1);
1098}
1099
1100void
1101icmp6_mtudisc_update(ip6cp, validated)
1102	struct ip6ctlparam *ip6cp;
1103	int validated;
1104{
1105	struct in6_addr *dst = ip6cp->ip6c_finaldst;
1106	struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6;
1107	struct mbuf *m = ip6cp->ip6c_m;	/* will be necessary for scope issue */
1108	u_int mtu = ntohl(icmp6->icmp6_mtu);
1109	struct rtentry *rt = NULL;
1110	struct sockaddr_in6 sin6;
1111
1112#if 0
1113	/*
1114	 * RFC2460 section 5, last paragraph.
1115	 * even though minimum link MTU for IPv6 is IPV6_MMTU,
1116	 * we may see ICMPv6 too big with mtu < IPV6_MMTU
1117	 * due to packet translator in the middle.
1118	 * see ip6_output() and ip6_getpmtu() "alwaysfrag" case for
1119	 * special handling.
1120	 */
1121	if (mtu < IPV6_MMTU)
1122		return;
1123#endif
1124
1125	/*
1126	 * we reject ICMPv6 too big with abnormally small value.
1127	 * XXX what is the good definition of "abnormally small"?
1128	 */
1129	if (mtu < sizeof(struct ip6_hdr) + sizeof(struct ip6_frag) + 8)
1130		return;
1131
1132	if (!validated)
1133		return;
1134
1135	bzero(&sin6, sizeof(sin6));
1136	sin6.sin6_family = PF_INET6;
1137	sin6.sin6_len = sizeof(struct sockaddr_in6);
1138	sin6.sin6_addr = *dst;
1139	/* XXX normally, this won't happen */
1140	if (IN6_IS_ADDR_LINKLOCAL(dst)) {
1141		sin6.sin6_addr.s6_addr16[1] =
1142		    htons(m->m_pkthdr.rcvif->if_index);
1143	}
1144	/* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */
1145	rt = rtalloc1((struct sockaddr *)&sin6, 0,
1146		      RTF_CLONING | RTF_PRCLONING);
1147
1148	if (rt && (rt->rt_flags & RTF_HOST) &&
1149	    !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1150		if (mtu < IPV6_MMTU) {
1151				/* xxx */
1152			rt->rt_rmx.rmx_locks |= RTV_MTU;
1153		} else if (mtu < rt->rt_ifp->if_mtu &&
1154			   rt->rt_rmx.rmx_mtu > mtu) {
1155			icmp6stat.icp6s_pmtuchg++;
1156			rt->rt_rmx.rmx_mtu = mtu;
1157		}
1158	}
1159	if (rt)
1160		rtfree(rt);
1161}
1162
1163/*
1164 * Process a Node Information Query packet, based on
1165 * draft-ietf-ipngwg-icmp-name-lookups-07.
1166 *
1167 * Spec incompatibilities:
1168 * - IPv6 Subject address handling
1169 * - IPv4 Subject address handling support missing
1170 * - Proxy reply (answer even if it's not for me)
1171 * - joins NI group address at in6_ifattach() time only, does not cope
1172 *   with hostname changes by sethostname(3)
1173 */
1174#define hostnamelen	strlen(hostname)
1175static struct mbuf *
1176ni6_input(m, off)
1177	struct mbuf *m;
1178	int off;
1179{
1180	struct icmp6_nodeinfo *ni6, *nni6;
1181	struct mbuf *n = NULL;
1182	u_int16_t qtype;
1183	int subjlen;
1184	int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1185	struct ni_reply_fqdn *fqdn;
1186	int addrs;		/* for NI_QTYPE_NODEADDR */
1187	struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */
1188	struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */
1189	struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */
1190	struct ip6_hdr *ip6;
1191	int oldfqdn = 0;	/* if 1, return pascal string (03 draft) */
1192	char *subj = NULL;
1193	struct in6_ifaddr *ia6 = NULL;
1194
1195	ip6 = mtod(m, struct ip6_hdr *);
1196#ifndef PULLDOWN_TEST
1197	ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off);
1198#else
1199	IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6));
1200	if (ni6 == NULL) {
1201		/* m is already reclaimed */
1202		return (NULL);
1203	}
1204#endif
1205
1206	/*
1207	 * Validate IPv6 destination address.
1208	 *
1209	 * The Responder must discard the Query without further processing
1210	 * unless it is one of the Responder's unicast or anycast addresses, or
1211	 * a link-local scope multicast address which the Responder has joined.
1212	 * [icmp-name-lookups-07, Section 4.]
1213	 */
1214	bzero(&sin6, sizeof(sin6));
1215	sin6.sin6_family = AF_INET6;
1216	sin6.sin6_len = sizeof(struct sockaddr_in6);
1217	bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr));
1218	/* XXX scopeid */
1219	if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) {
1220		/* unicast/anycast, fine */
1221		if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1222		    (icmp6_nodeinfo & 4) == 0) {
1223			nd6log((LOG_DEBUG, "ni6_input: ignore node info to "
1224				"a temporary address in %s:%d",
1225			       __FILE__, __LINE__));
1226			goto bad;
1227		}
1228	} else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr))
1229		; /* link-local multicast, fine */
1230	else
1231		goto bad;
1232
1233	/* validate query Subject field. */
1234	qtype = ntohs(ni6->ni_qtype);
1235	subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo);
1236	switch (qtype) {
1237	case NI_QTYPE_NOOP:
1238	case NI_QTYPE_SUPTYPES:
1239		/* 07 draft */
1240		if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0)
1241			break;
1242		/* FALLTHROUGH */
1243	case NI_QTYPE_FQDN:
1244	case NI_QTYPE_NODEADDR:
1245		switch (ni6->ni_code) {
1246		case ICMP6_NI_SUBJ_IPV6:
1247#if ICMP6_NI_SUBJ_IPV6 != 0
1248		case 0:
1249#endif
1250			/*
1251			 * backward compatibility - try to accept 03 draft
1252			 * format, where no Subject is present.
1253			 */
1254			if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 &&
1255			    subjlen == 0) {
1256				oldfqdn++;
1257				break;
1258			}
1259#if ICMP6_NI_SUBJ_IPV6 != 0
1260			if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6)
1261				goto bad;
1262#endif
1263
1264			if (subjlen != sizeof(sin6.sin6_addr))
1265				goto bad;
1266
1267			/*
1268			 * Validate Subject address.
1269			 *
1270			 * Not sure what exactly "address belongs to the node"
1271			 * means in the spec, is it just unicast, or what?
1272			 *
1273			 * At this moment we consider Subject address as
1274			 * "belong to the node" if the Subject address equals
1275			 * to the IPv6 destination address; validation for
1276			 * IPv6 destination address should have done enough
1277			 * check for us.
1278			 *
1279			 * We do not do proxy at this moment.
1280			 */
1281			/* m_pulldown instead of copy? */
1282			m_copydata(m, off + sizeof(struct icmp6_nodeinfo),
1283			    subjlen, (caddr_t)&sin6.sin6_addr);
1284			if (in6_addr2zoneid(m->m_pkthdr.rcvif,
1285			    &sin6.sin6_addr, &sin6.sin6_scope_id)) {
1286				goto bad;
1287			}
1288			in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL);
1289			bzero(&sin6_d, sizeof(sin6_d));
1290			sin6_d.sin6_family = AF_INET6; /* not used, actually */
1291			sin6_d.sin6_len = sizeof(sin6_d); /* ditto */
1292			sin6_d.sin6_addr = ip6->ip6_dst;
1293			if (in6_addr2zoneid(m->m_pkthdr.rcvif,
1294			    &ip6->ip6_dst, &sin6_d.sin6_scope_id)) {
1295				goto bad;
1296			}
1297			in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL);
1298			subj = (char *)&sin6;
1299			if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d))
1300				break;
1301
1302			/*
1303			 * XXX if we are to allow other cases, we should really
1304			 * be careful about scope here.
1305			 * basically, we should disallow queries toward IPv6
1306			 * destination X with subject Y,
1307			 * if scope(X) > scope(Y).
1308			 * if we allow scope(X) > scope(Y), it will result in
1309			 * information leakage across scope boundary.
1310			 */
1311			goto bad;
1312
1313		case ICMP6_NI_SUBJ_FQDN:
1314			/*
1315			 * Validate Subject name with gethostname(3).
1316			 *
1317			 * The behavior may need some debate, since:
1318			 * - we are not sure if the node has FQDN as
1319			 *   hostname (returned by gethostname(3)).
1320			 * - the code does wildcard match for truncated names.
1321			 *   however, we are not sure if we want to perform
1322			 *   wildcard match, if gethostname(3) side has
1323			 *   truncated hostname.
1324			 */
1325			n = ni6_nametodns(hostname, hostnamelen, 0);
1326			if (!n || n->m_next || n->m_len == 0)
1327				goto bad;
1328			IP6_EXTHDR_GET(subj, char *, m,
1329			    off + sizeof(struct icmp6_nodeinfo), subjlen);
1330			if (subj == NULL)
1331				goto bad;
1332			if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *),
1333			    n->m_len)) {
1334				goto bad;
1335			}
1336			m_freem(n);
1337			n = NULL;
1338			break;
1339
1340		case ICMP6_NI_SUBJ_IPV4:	/* XXX: to be implemented? */
1341		default:
1342			goto bad;
1343		}
1344		break;
1345	}
1346
1347	/* refuse based on configuration.  XXX ICMP6_NI_REFUSED? */
1348	switch (qtype) {
1349	case NI_QTYPE_FQDN:
1350		if ((icmp6_nodeinfo & 1) == 0)
1351			goto bad;
1352		break;
1353	case NI_QTYPE_NODEADDR:
1354		if ((icmp6_nodeinfo & 2) == 0)
1355			goto bad;
1356		break;
1357	}
1358
1359	/* guess reply length */
1360	switch (qtype) {
1361	case NI_QTYPE_NOOP:
1362		break;		/* no reply data */
1363	case NI_QTYPE_SUPTYPES:
1364		replylen += sizeof(u_int32_t);
1365		break;
1366	case NI_QTYPE_FQDN:
1367		/* XXX will append an mbuf */
1368		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1369		break;
1370	case NI_QTYPE_NODEADDR:
1371		addrs = ni6_addrs(ni6, m, &ifp, subj);
1372		if ((replylen += addrs * (sizeof(struct in6_addr) +
1373		    sizeof(u_int32_t))) > MCLBYTES)
1374			replylen = MCLBYTES; /* XXX: will truncate pkt later */
1375		break;
1376	default:
1377		/*
1378		 * XXX: We must return a reply with the ICMP6 code
1379		 * `unknown Qtype' in this case.  However we regard the case
1380		 * as an FQDN query for backward compatibility.
1381		 * Older versions set a random value to this field,
1382		 * so it rarely varies in the defined qtypes.
1383		 * But the mechanism is not reliable...
1384		 * maybe we should obsolete older versions.
1385		 */
1386		qtype = NI_QTYPE_FQDN;
1387		/* XXX will append an mbuf */
1388		replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen);
1389		oldfqdn++;
1390		break;
1391	}
1392
1393	/* allocate an mbuf to reply. */
1394	MGETHDR(n, M_DONTWAIT, m->m_type);
1395	if (n == NULL) {
1396		m_freem(m);
1397		return (NULL);
1398	}
1399	M_MOVE_PKTHDR(n, m); /* just for recvif */
1400	if (replylen > MHLEN) {
1401		if (replylen > MCLBYTES) {
1402			/*
1403			 * XXX: should we try to allocate more? But MCLBYTES
1404			 * is probably much larger than IPV6_MMTU...
1405			 */
1406			goto bad;
1407		}
1408		MCLGET(n, M_DONTWAIT);
1409		if ((n->m_flags & M_EXT) == 0) {
1410			goto bad;
1411		}
1412	}
1413	n->m_pkthdr.len = n->m_len = replylen;
1414
1415	/* copy mbuf header and IPv6 + Node Information base headers */
1416	bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr));
1417	nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1);
1418	bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo));
1419
1420	/* qtype dependent procedure */
1421	switch (qtype) {
1422	case NI_QTYPE_NOOP:
1423		nni6->ni_code = ICMP6_NI_SUCCESS;
1424		nni6->ni_flags = 0;
1425		break;
1426	case NI_QTYPE_SUPTYPES:
1427	{
1428		u_int32_t v;
1429		nni6->ni_code = ICMP6_NI_SUCCESS;
1430		nni6->ni_flags = htons(0x0000);	/* raw bitmap */
1431		/* supports NOOP, SUPTYPES, FQDN, and NODEADDR */
1432		v = (u_int32_t)htonl(0x0000000f);
1433		bcopy(&v, nni6 + 1, sizeof(u_int32_t));
1434		break;
1435	}
1436	case NI_QTYPE_FQDN:
1437		nni6->ni_code = ICMP6_NI_SUCCESS;
1438		fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) +
1439		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo));
1440		nni6->ni_flags = 0; /* XXX: meaningless TTL */
1441		fqdn->ni_fqdn_ttl = 0;	/* ditto. */
1442		/*
1443		 * XXX do we really have FQDN in variable "hostname"?
1444		 */
1445		n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn);
1446		if (n->m_next == NULL)
1447			goto bad;
1448		/* XXX we assume that n->m_next is not a chain */
1449		if (n->m_next->m_next != NULL)
1450			goto bad;
1451		n->m_pkthdr.len += n->m_next->m_len;
1452		break;
1453	case NI_QTYPE_NODEADDR:
1454	{
1455		int lenlim, copied;
1456
1457		nni6->ni_code = ICMP6_NI_SUCCESS;
1458		n->m_pkthdr.len = n->m_len =
1459		    sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo);
1460		lenlim = M_TRAILINGSPACE(n);
1461		copied = ni6_store_addrs(ni6, nni6, ifp, lenlim);
1462		/* XXX: reset mbuf length */
1463		n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) +
1464		    sizeof(struct icmp6_nodeinfo) + copied;
1465		break;
1466	}
1467	default:
1468		break;		/* XXX impossible! */
1469	}
1470
1471	nni6->ni_type = ICMP6_NI_REPLY;
1472	m_freem(m);
1473	return (n);
1474
1475  bad:
1476	m_freem(m);
1477	if (n)
1478		m_freem(n);
1479	return (NULL);
1480}
1481#undef hostnamelen
1482
1483/*
1484 * make a mbuf with DNS-encoded string.  no compression support.
1485 *
1486 * XXX names with less than 2 dots (like "foo" or "foo.section") will be
1487 * treated as truncated name (two \0 at the end).  this is a wild guess.
1488 */
1489static struct mbuf *
1490ni6_nametodns(name, namelen, old)
1491	const char *name;
1492	int namelen;
1493	int old;	/* return pascal string if non-zero */
1494{
1495	struct mbuf *m;
1496	char *cp, *ep;
1497	const char *p, *q;
1498	int i, len, nterm;
1499
1500	if (old)
1501		len = namelen + 1;
1502	else
1503		len = MCLBYTES;
1504
1505	/* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */
1506	MGET(m, M_DONTWAIT, MT_DATA);
1507	if (m && len > MLEN) {
1508		MCLGET(m, M_DONTWAIT);
1509		if ((m->m_flags & M_EXT) == 0)
1510			goto fail;
1511	}
1512	if (!m)
1513		goto fail;
1514	m->m_next = NULL;
1515
1516	if (old) {
1517		m->m_len = len;
1518		*mtod(m, char *) = namelen;
1519		bcopy(name, mtod(m, char *) + 1, namelen);
1520		return m;
1521	} else {
1522		m->m_len = 0;
1523		cp = mtod(m, char *);
1524		ep = mtod(m, char *) + M_TRAILINGSPACE(m);
1525
1526		/* if not certain about my name, return empty buffer */
1527		if (namelen == 0)
1528			return m;
1529
1530		/*
1531		 * guess if it looks like shortened hostname, or FQDN.
1532		 * shortened hostname needs two trailing "\0".
1533		 */
1534		i = 0;
1535		for (p = name; p < name + namelen; p++) {
1536			if (*p && *p == '.')
1537				i++;
1538		}
1539		if (i < 2)
1540			nterm = 2;
1541		else
1542			nterm = 1;
1543
1544		p = name;
1545		while (cp < ep && p < name + namelen) {
1546			i = 0;
1547			for (q = p; q < name + namelen && *q && *q != '.'; q++)
1548				i++;
1549			/* result does not fit into mbuf */
1550			if (cp + i + 1 >= ep)
1551				goto fail;
1552			/*
1553			 * DNS label length restriction, RFC1035 page 8.
1554			 * "i == 0" case is included here to avoid returning
1555			 * 0-length label on "foo..bar".
1556			 */
1557			if (i <= 0 || i >= 64)
1558				goto fail;
1559			*cp++ = i;
1560			bcopy(p, cp, i);
1561			cp += i;
1562			p = q;
1563			if (p < name + namelen && *p == '.')
1564				p++;
1565		}
1566		/* termination */
1567		if (cp + nterm >= ep)
1568			goto fail;
1569		while (nterm-- > 0)
1570			*cp++ = '\0';
1571		m->m_len = cp - mtod(m, char *);
1572		return m;
1573	}
1574
1575	panic("should not reach here");
1576	/* NOTREACHED */
1577
1578 fail:
1579	if (m)
1580		m_freem(m);
1581	return NULL;
1582}
1583
1584/*
1585 * check if two DNS-encoded string matches.  takes care of truncated
1586 * form (with \0\0 at the end).  no compression support.
1587 * XXX upper/lowercase match (see RFC2065)
1588 */
1589static int
1590ni6_dnsmatch(a, alen, b, blen)
1591	const char *a;
1592	int alen;
1593	const char *b;
1594	int blen;
1595{
1596	const char *a0, *b0;
1597	int l;
1598
1599	/* simplest case - need validation? */
1600	if (alen == blen && bcmp(a, b, alen) == 0)
1601		return 1;
1602
1603	a0 = a;
1604	b0 = b;
1605
1606	/* termination is mandatory */
1607	if (alen < 2 || blen < 2)
1608		return 0;
1609	if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0')
1610		return 0;
1611	alen--;
1612	blen--;
1613
1614	while (a - a0 < alen && b - b0 < blen) {
1615		if (a - a0 + 1 > alen || b - b0 + 1 > blen)
1616			return 0;
1617
1618		if ((signed char)a[0] < 0 || (signed char)b[0] < 0)
1619			return 0;
1620		/* we don't support compression yet */
1621		if (a[0] >= 64 || b[0] >= 64)
1622			return 0;
1623
1624		/* truncated case */
1625		if (a[0] == 0 && a - a0 == alen - 1)
1626			return 1;
1627		if (b[0] == 0 && b - b0 == blen - 1)
1628			return 1;
1629		if (a[0] == 0 || b[0] == 0)
1630			return 0;
1631
1632		if (a[0] != b[0])
1633			return 0;
1634		l = a[0];
1635		if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen)
1636			return 0;
1637		if (bcmp(a + 1, b + 1, l) != 0)
1638			return 0;
1639
1640		a += 1 + l;
1641		b += 1 + l;
1642	}
1643
1644	if (a - a0 == alen && b - b0 == blen)
1645		return 1;
1646	else
1647		return 0;
1648}
1649
1650/*
1651 * calculate the number of addresses to be returned in the node info reply.
1652 */
1653static int
1654ni6_addrs(ni6, m, ifpp, subj)
1655	struct icmp6_nodeinfo *ni6;
1656	struct mbuf *m;
1657	struct ifnet **ifpp;
1658	char *subj;
1659{
1660	struct ifnet *ifp;
1661	struct in6_ifaddr *ifa6;
1662	struct ifaddr *ifa;
1663	struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */
1664	int addrs = 0, addrsofif, iffound = 0;
1665	int niflags = ni6->ni_flags;
1666
1667	if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) {
1668		switch (ni6->ni_code) {
1669		case ICMP6_NI_SUBJ_IPV6:
1670			if (subj == NULL) /* must be impossible... */
1671				return (0);
1672			subj_ip6 = (struct sockaddr_in6 *)subj;
1673			break;
1674		default:
1675			/*
1676			 * XXX: we only support IPv6 subject address for
1677			 * this Qtype.
1678			 */
1679			return (0);
1680		}
1681	}
1682
1683	IFNET_RLOCK();
1684	for (ifp = TAILQ_FIRST(&ifnet); ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1685		addrsofif = 0;
1686		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
1687			if (ifa->ifa_addr->sa_family != AF_INET6)
1688				continue;
1689			ifa6 = (struct in6_ifaddr *)ifa;
1690
1691			if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 &&
1692			    IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr,
1693			    &ifa6->ia_addr.sin6_addr))
1694				iffound = 1;
1695
1696			/*
1697			 * IPv4-mapped addresses can only be returned by a
1698			 * Node Information proxy, since they represent
1699			 * addresses of IPv4-only nodes, which perforce do
1700			 * not implement this protocol.
1701			 * [icmp-name-lookups-07, Section 5.4]
1702			 * So we don't support NI_NODEADDR_FLAG_COMPAT in
1703			 * this function at this moment.
1704			 */
1705
1706			/* What do we have to do about ::1? */
1707			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1708			case IPV6_ADDR_SCOPE_LINKLOCAL:
1709				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1710					continue;
1711				break;
1712			case IPV6_ADDR_SCOPE_SITELOCAL:
1713				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1714					continue;
1715				break;
1716			case IPV6_ADDR_SCOPE_GLOBAL:
1717				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1718					continue;
1719				break;
1720			default:
1721				continue;
1722			}
1723
1724			/*
1725			 * check if anycast is okay.
1726			 * XXX: just experimental.  not in the spec.
1727			 */
1728			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1729			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1730				continue; /* we need only unicast addresses */
1731			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1732			    (icmp6_nodeinfo & 4) == 0) {
1733				continue;
1734			}
1735			addrsofif++; /* count the address */
1736		}
1737		if (iffound) {
1738			*ifpp = ifp;
1739			IFNET_RUNLOCK();
1740			return (addrsofif);
1741		}
1742
1743		addrs += addrsofif;
1744	}
1745	IFNET_RUNLOCK();
1746
1747	return (addrs);
1748}
1749
1750static int
1751ni6_store_addrs(ni6, nni6, ifp0, resid)
1752	struct icmp6_nodeinfo *ni6, *nni6;
1753	struct ifnet *ifp0;
1754	int resid;
1755{
1756	struct ifnet *ifp = ifp0 ? ifp0 : TAILQ_FIRST(&ifnet);
1757	struct in6_ifaddr *ifa6;
1758	struct ifaddr *ifa;
1759	struct ifnet *ifp_dep = NULL;
1760	int copied = 0, allow_deprecated = 0;
1761	u_char *cp = (u_char *)(nni6 + 1);
1762	int niflags = ni6->ni_flags;
1763	u_int32_t ltime;
1764
1765	if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL))
1766		return (0);	/* needless to copy */
1767
1768	IFNET_RLOCK();
1769  again:
1770
1771	for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) {
1772		for (ifa = ifp->if_addrlist.tqh_first; ifa;
1773		     ifa = ifa->ifa_list.tqe_next) {
1774			if (ifa->ifa_addr->sa_family != AF_INET6)
1775				continue;
1776			ifa6 = (struct in6_ifaddr *)ifa;
1777
1778			if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 &&
1779			    allow_deprecated == 0) {
1780				/*
1781				 * prefererred address should be put before
1782				 * deprecated addresses.
1783				 */
1784
1785				/* record the interface for later search */
1786				if (ifp_dep == NULL)
1787					ifp_dep = ifp;
1788
1789				continue;
1790			} else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 &&
1791			    allow_deprecated != 0)
1792				continue; /* we now collect deprecated addrs */
1793
1794			/* What do we have to do about ::1? */
1795			switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) {
1796			case IPV6_ADDR_SCOPE_LINKLOCAL:
1797				if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0)
1798					continue;
1799				break;
1800			case IPV6_ADDR_SCOPE_SITELOCAL:
1801				if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0)
1802					continue;
1803				break;
1804			case IPV6_ADDR_SCOPE_GLOBAL:
1805				if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0)
1806					continue;
1807				break;
1808			default:
1809				continue;
1810			}
1811
1812			/*
1813			 * check if anycast is okay.
1814			 * XXX: just experimental.  not in the spec.
1815			 */
1816			if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 &&
1817			    (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0)
1818				continue;
1819			if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 &&
1820			    (icmp6_nodeinfo & 4) == 0) {
1821				continue;
1822			}
1823
1824			/* now we can copy the address */
1825			if (resid < sizeof(struct in6_addr) +
1826			    sizeof(u_int32_t)) {
1827				/*
1828				 * We give up much more copy.
1829				 * Set the truncate flag and return.
1830				 */
1831				nni6->ni_flags |= NI_NODEADDR_FLAG_TRUNCATE;
1832				IFNET_RUNLOCK();
1833				return (copied);
1834			}
1835
1836			/*
1837			 * Set the TTL of the address.
1838			 * The TTL value should be one of the following
1839			 * according to the specification:
1840			 *
1841			 * 1. The remaining lifetime of a DHCP lease on the
1842			 *    address, or
1843			 * 2. The remaining Valid Lifetime of a prefix from
1844			 *    which the address was derived through Stateless
1845			 *    Autoconfiguration.
1846			 *
1847			 * Note that we currently do not support stateful
1848			 * address configuration by DHCPv6, so the former
1849			 * case can't happen.
1850			 */
1851			if (ifa6->ia6_lifetime.ia6t_expire == 0)
1852				ltime = ND6_INFINITE_LIFETIME;
1853			else {
1854				if (ifa6->ia6_lifetime.ia6t_expire >
1855				    time_second)
1856					ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - time_second);
1857				else
1858					ltime = 0;
1859			}
1860
1861			bcopy(&ltime, cp, sizeof(u_int32_t));
1862			cp += sizeof(u_int32_t);
1863
1864			/* copy the address itself */
1865			bcopy(&ifa6->ia_addr.sin6_addr, cp,
1866			    sizeof(struct in6_addr));
1867			in6_clearscope((struct in6_addr *)cp); /* XXX */
1868			cp += sizeof(struct in6_addr);
1869
1870			resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t));
1871			copied += (sizeof(struct in6_addr) + sizeof(u_int32_t));
1872		}
1873		if (ifp0)	/* we need search only on the specified IF */
1874			break;
1875	}
1876
1877	if (allow_deprecated == 0 && ifp_dep != NULL) {
1878		ifp = ifp_dep;
1879		allow_deprecated = 1;
1880
1881		goto again;
1882	}
1883
1884	IFNET_RUNLOCK();
1885
1886	return (copied);
1887}
1888
1889/*
1890 * XXX almost dup'ed code with rip6_input.
1891 */
1892static int
1893icmp6_rip6_input(mp, off)
1894	struct	mbuf **mp;
1895	int	off;
1896{
1897	struct mbuf *m = *mp;
1898	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1899	struct in6pcb *in6p;
1900	struct in6pcb *last = NULL;
1901	struct sockaddr_in6 fromsa;
1902	struct icmp6_hdr *icmp6;
1903	struct mbuf *opts = NULL;
1904
1905#ifndef PULLDOWN_TEST
1906	/* this is assumed to be safe. */
1907	icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
1908#else
1909	IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6));
1910	if (icmp6 == NULL) {
1911		/* m is already reclaimed */
1912		return (IPPROTO_DONE);
1913	}
1914#endif
1915
1916	bzero(&fromsa, sizeof(fromsa));
1917	fromsa.sin6_len = sizeof(struct sockaddr_in6);
1918	fromsa.sin6_family = AF_INET6;
1919	/* KAME hack: recover scopeid */
1920	(void)in6_recoverscope(&fromsa, &ip6->ip6_src, m->m_pkthdr.rcvif);
1921
1922	LIST_FOREACH(in6p, &ripcb, inp_list) {
1923		if ((in6p->inp_vflag & INP_IPV6) == 0)
1924			continue;
1925		if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6)
1926			continue;
1927		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
1928		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
1929			continue;
1930		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
1931		   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
1932			continue;
1933		if (in6p->in6p_icmp6filt
1934		    && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type,
1935				 in6p->in6p_icmp6filt))
1936			continue;
1937		if (last) {
1938			struct	mbuf *n;
1939			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1940				if (last->in6p_flags & IN6P_CONTROLOPTS)
1941					ip6_savecontrol(last, &opts, ip6, n);
1942				/* strip intermediate headers */
1943				m_adj(n, off);
1944				if (sbappendaddr(&last->in6p_socket->so_rcv,
1945				    (struct sockaddr *)&fromsa, n, opts)
1946				    == 0) {
1947					/* should notify about lost packet */
1948					m_freem(n);
1949					if (opts) {
1950						m_freem(opts);
1951					}
1952				} else
1953					sorwakeup(last->in6p_socket);
1954				opts = NULL;
1955			}
1956		}
1957		last = in6p;
1958	}
1959	if (last) {
1960		if (last->in6p_flags & IN6P_CONTROLOPTS)
1961			ip6_savecontrol(last, &opts, ip6, m);
1962		/* strip intermediate headers */
1963		m_adj(m, off);
1964		if (sbappendaddr(&last->in6p_socket->so_rcv,
1965		    (struct sockaddr *)&fromsa, m, opts) == 0) {
1966			m_freem(m);
1967			if (opts)
1968				m_freem(opts);
1969		} else
1970			sorwakeup(last->in6p_socket);
1971	} else {
1972		m_freem(m);
1973		ip6stat.ip6s_delivered--;
1974	}
1975	return IPPROTO_DONE;
1976}
1977
1978/*
1979 * Reflect the ip6 packet back to the source.
1980 * OFF points to the icmp6 header, counted from the top of the mbuf.
1981 */
1982void
1983icmp6_reflect(m, off)
1984	struct	mbuf *m;
1985	size_t off;
1986{
1987	struct ip6_hdr *ip6;
1988	struct icmp6_hdr *icmp6;
1989	struct in6_ifaddr *ia;
1990	struct in6_addr t, *src = 0;
1991	int plen;
1992	int type, code;
1993	struct ifnet *outif = NULL;
1994	struct sockaddr_in6 sa6_src, sa6_dst;
1995#ifdef COMPAT_RFC1885
1996	int mtu = IPV6_MMTU;
1997	struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst;
1998#endif
1999
2000	/* too short to reflect */
2001	if (off < sizeof(struct ip6_hdr)) {
2002		nd6log((LOG_DEBUG,
2003		    "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n",
2004		    (u_long)off, (u_long)sizeof(struct ip6_hdr),
2005		    __FILE__, __LINE__));
2006		goto bad;
2007	}
2008
2009	/*
2010	 * If there are extra headers between IPv6 and ICMPv6, strip
2011	 * off that header first.
2012	 */
2013#ifdef DIAGNOSTIC
2014	if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN)
2015		panic("assumption failed in icmp6_reflect");
2016#endif
2017	if (off > sizeof(struct ip6_hdr)) {
2018		size_t l;
2019		struct ip6_hdr nip6;
2020
2021		l = off - sizeof(struct ip6_hdr);
2022		m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6);
2023		m_adj(m, l);
2024		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2025		if (m->m_len < l) {
2026			if ((m = m_pullup(m, l)) == NULL)
2027				return;
2028		}
2029		bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6));
2030	} else /* off == sizeof(struct ip6_hdr) */ {
2031		size_t l;
2032		l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr);
2033		if (m->m_len < l) {
2034			if ((m = m_pullup(m, l)) == NULL)
2035				return;
2036		}
2037	}
2038	plen = m->m_pkthdr.len - sizeof(struct ip6_hdr);
2039	ip6 = mtod(m, struct ip6_hdr *);
2040	ip6->ip6_nxt = IPPROTO_ICMPV6;
2041	icmp6 = (struct icmp6_hdr *)(ip6 + 1);
2042	type = icmp6->icmp6_type; /* keep type for statistics */
2043	code = icmp6->icmp6_code; /* ditto. */
2044
2045	t = ip6->ip6_dst;
2046	/*
2047	 * ip6_input() drops a packet if its src is multicast.
2048	 * So, the src is never multicast.
2049	 */
2050	ip6->ip6_dst = ip6->ip6_src;
2051
2052	/*
2053	 * XXX: make sure to embed scope zone information, using
2054	 * already embedded IDs or the received interface (if any).
2055	 * Note that rcvif may be NULL.
2056	 * TODO: scoped routing case (XXX).
2057	 */
2058	bzero(&sa6_src, sizeof(sa6_src));
2059	sa6_src.sin6_family = AF_INET6;
2060	sa6_src.sin6_len = sizeof(sa6_src);
2061	sa6_src.sin6_addr = ip6->ip6_dst;
2062	in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif);
2063	in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL);
2064	bzero(&sa6_dst, sizeof(sa6_dst));
2065	sa6_dst.sin6_family = AF_INET6;
2066	sa6_dst.sin6_len = sizeof(sa6_dst);
2067	sa6_dst.sin6_addr = t;
2068	in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif);
2069	in6_embedscope(&t, &sa6_dst, NULL, NULL);
2070
2071#ifdef COMPAT_RFC1885
2072	/*
2073	 * xxx guess MTU
2074	 * RFC 1885 requires that echo reply should be truncated if it
2075	 * does not fit in with (return) path MTU, but the description was
2076	 * removed in the new spec.
2077	 */
2078	if (icmp6_reflect_rt.ro_rt == 0 ||
2079	    ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) {
2080		if (icmp6_reflect_rt.ro_rt) {
2081			RTFREE(icmp6_reflect_rt.ro_rt);
2082			icmp6_reflect_rt.ro_rt = 0;
2083		}
2084		bzero(sin6, sizeof(*sin6));
2085		sin6->sin6_family = PF_INET6;
2086		sin6->sin6_len = sizeof(struct sockaddr_in6);
2087		sin6->sin6_addr = ip6->ip6_dst;
2088
2089		rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt,
2090			    RTF_PRCLONING);
2091	}
2092
2093	if (icmp6_reflect_rt.ro_rt == 0)
2094		goto bad;
2095
2096	if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST)
2097	    && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu)
2098		mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu;
2099
2100	if (mtu < m->m_pkthdr.len) {
2101		plen -= (m->m_pkthdr.len - mtu);
2102		m_adj(m, mtu - m->m_pkthdr.len);
2103	}
2104#endif
2105	/*
2106	 * If the incoming packet was addressed directly to us (i.e. unicast),
2107	 * use dst as the src for the reply.
2108	 * The IN6_IFF_NOTREADY case should be VERY rare, but is possible
2109	 * (for example) when we encounter an error while forwarding procedure
2110	 * destined to a duplicated address of ours.
2111	 */
2112	for (ia = in6_ifaddr; ia; ia = ia->ia_next)
2113		if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) &&
2114		    (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) {
2115			src = &t;
2116			break;
2117		}
2118	if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) {
2119		/*
2120		 * This is the case if the dst is our link-local address
2121		 * and the sender is also ourselves.
2122		 */
2123		src = &t;
2124	}
2125
2126	if (src == 0) {
2127		int e;
2128		struct route_in6 ro;
2129
2130		/*
2131		 * This case matches to multicasts, our anycast, or unicasts
2132		 * that we do not own.  Select a source address based on the
2133		 * source address of the erroneous packet.
2134		 */
2135		bzero(&ro, sizeof(ro));
2136		src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &e);
2137		if (ro.ro_rt)
2138			RTFREE(ro.ro_rt); /* XXX: we could use this */
2139		if (src == NULL) {
2140			nd6log((LOG_DEBUG,
2141			    "icmp6_reflect: source can't be determined: "
2142			    "dst=%s, error=%d\n",
2143			    ip6_sprintf(&sa6_src.sin6_addr), e));
2144			goto bad;
2145		}
2146	}
2147
2148	ip6->ip6_src = *src;
2149
2150	ip6->ip6_flow = 0;
2151	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2152	ip6->ip6_vfc |= IPV6_VERSION;
2153	ip6->ip6_nxt = IPPROTO_ICMPV6;
2154	if (outif)
2155		ip6->ip6_hlim = ND_IFINFO(outif)->chlim;
2156	else if (m->m_pkthdr.rcvif) {
2157		/* XXX: This may not be the outgoing interface */
2158		ip6->ip6_hlim = ND_IFINFO(m->m_pkthdr.rcvif)->chlim;
2159	} else
2160		ip6->ip6_hlim = ip6_defhlim;
2161
2162	icmp6->icmp6_cksum = 0;
2163	icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2164	    sizeof(struct ip6_hdr), plen);
2165
2166	/*
2167	 * XXX option handling
2168	 */
2169
2170	m->m_flags &= ~(M_BCAST|M_MCAST);
2171
2172#ifdef COMPAT_RFC1885
2173	ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, NULL);
2174#else
2175	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2176#endif
2177	if (outif)
2178		icmp6_ifoutstat_inc(outif, type, code);
2179
2180	return;
2181
2182 bad:
2183	m_freem(m);
2184	return;
2185}
2186
2187void
2188icmp6_fasttimo()
2189{
2190
2191	mld6_fasttimeo();
2192}
2193
2194static const char *
2195icmp6_redirect_diag(src6, dst6, tgt6)
2196	struct in6_addr *src6;
2197	struct in6_addr *dst6;
2198	struct in6_addr *tgt6;
2199{
2200	static char buf[1024];
2201	snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)",
2202	    ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6));
2203	return buf;
2204}
2205
2206void
2207icmp6_redirect_input(m, off)
2208	struct mbuf *m;
2209	int off;
2210{
2211	struct ifnet *ifp = m->m_pkthdr.rcvif;
2212	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
2213	struct nd_redirect *nd_rd;
2214	int icmp6len = ntohs(ip6->ip6_plen);
2215	char *lladdr = NULL;
2216	int lladdrlen = 0;
2217	u_char *redirhdr = NULL;
2218	int redirhdrlen = 0;
2219	struct rtentry *rt = NULL;
2220	int is_router;
2221	int is_onlink;
2222	struct in6_addr src6 = ip6->ip6_src;
2223	struct in6_addr redtgt6;
2224	struct in6_addr reddst6;
2225	union nd_opts ndopts;
2226
2227	if (!m || !ifp)
2228		return;
2229
2230	/* XXX if we are router, we don't update route by icmp6 redirect */
2231	if (ip6_forwarding)
2232		goto freeit;
2233	if (!icmp6_rediraccept)
2234		goto freeit;
2235
2236#ifndef PULLDOWN_TEST
2237	IP6_EXTHDR_CHECK(m, off, icmp6len,);
2238	nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off);
2239#else
2240	IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len);
2241	if (nd_rd == NULL) {
2242		icmp6stat.icp6s_tooshort++;
2243		return;
2244	}
2245#endif
2246	redtgt6 = nd_rd->nd_rd_target;
2247	reddst6 = nd_rd->nd_rd_dst;
2248
2249	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2250		redtgt6.s6_addr16[1] = htons(ifp->if_index);
2251	if (IN6_IS_ADDR_LINKLOCAL(&reddst6))
2252		reddst6.s6_addr16[1] = htons(ifp->if_index);
2253
2254	/* validation */
2255	if (!IN6_IS_ADDR_LINKLOCAL(&src6)) {
2256		nd6log((LOG_ERR,
2257		    "ICMP6 redirect sent from %s rejected; "
2258		    "must be from linklocal\n",
2259		    ip6_sprintf(&src6)));
2260		goto bad;
2261	}
2262	if (ip6->ip6_hlim != 255) {
2263		nd6log((LOG_ERR,
2264		    "ICMP6 redirect sent from %s rejected; "
2265		    "hlim=%d (must be 255)\n",
2266		    ip6_sprintf(&src6), ip6->ip6_hlim));
2267		goto bad;
2268	}
2269    {
2270	/* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */
2271	struct sockaddr_in6 sin6;
2272	struct in6_addr *gw6;
2273
2274	bzero(&sin6, sizeof(sin6));
2275	sin6.sin6_family = AF_INET6;
2276	sin6.sin6_len = sizeof(struct sockaddr_in6);
2277	bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6));
2278	rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL);
2279	if (rt) {
2280		if (rt->rt_gateway == NULL ||
2281		    rt->rt_gateway->sa_family != AF_INET6) {
2282			nd6log((LOG_ERR,
2283			    "ICMP6 redirect rejected; no route "
2284			    "with inet6 gateway found for redirect dst: %s\n",
2285			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2286			RTFREE_LOCKED(rt);
2287			goto bad;
2288		}
2289
2290		gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr);
2291		if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) {
2292			nd6log((LOG_ERR,
2293			    "ICMP6 redirect rejected; "
2294			    "not equal to gw-for-src=%s (must be same): "
2295			    "%s\n",
2296			    ip6_sprintf(gw6),
2297			    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2298			RTFREE_LOCKED(rt);
2299			goto bad;
2300		}
2301	} else {
2302		nd6log((LOG_ERR,
2303		    "ICMP6 redirect rejected; "
2304		    "no route found for redirect dst: %s\n",
2305		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2306		goto bad;
2307	}
2308	RTFREE_LOCKED(rt);
2309	rt = NULL;
2310    }
2311	if (IN6_IS_ADDR_MULTICAST(&reddst6)) {
2312		nd6log((LOG_ERR,
2313		    "ICMP6 redirect rejected; "
2314		    "redirect dst must be unicast: %s\n",
2315		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2316		goto bad;
2317	}
2318
2319	is_router = is_onlink = 0;
2320	if (IN6_IS_ADDR_LINKLOCAL(&redtgt6))
2321		is_router = 1;	/* router case */
2322	if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0)
2323		is_onlink = 1;	/* on-link destination case */
2324	if (!is_router && !is_onlink) {
2325		nd6log((LOG_ERR,
2326		    "ICMP6 redirect rejected; "
2327		    "neither router case nor onlink case: %s\n",
2328		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2329		goto bad;
2330	}
2331	/* validation passed */
2332
2333	icmp6len -= sizeof(*nd_rd);
2334	nd6_option_init(nd_rd + 1, icmp6len, &ndopts);
2335	if (nd6_options(&ndopts) < 0) {
2336		nd6log((LOG_INFO, "icmp6_redirect_input: "
2337		    "invalid ND option, rejected: %s\n",
2338		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2339		/* nd6_options have incremented stats */
2340		goto freeit;
2341	}
2342
2343	if (ndopts.nd_opts_tgt_lladdr) {
2344		lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1);
2345		lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3;
2346	}
2347
2348	if (ndopts.nd_opts_rh) {
2349		redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len;
2350		redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */
2351	}
2352
2353	if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) {
2354		nd6log((LOG_INFO,
2355		    "icmp6_redirect_input: lladdrlen mismatch for %s "
2356		    "(if %d, icmp6 packet %d): %s\n",
2357		    ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2,
2358		    icmp6_redirect_diag(&src6, &reddst6, &redtgt6)));
2359		goto bad;
2360	}
2361
2362	/* RFC 2461 8.3 */
2363	nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT,
2364	    is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER);
2365
2366	if (!is_onlink) {	/* better router case.  perform rtredirect. */
2367		/* perform rtredirect */
2368		struct sockaddr_in6 sdst;
2369		struct sockaddr_in6 sgw;
2370		struct sockaddr_in6 ssrc;
2371
2372		bzero(&sdst, sizeof(sdst));
2373		bzero(&sgw, sizeof(sgw));
2374		bzero(&ssrc, sizeof(ssrc));
2375		sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6;
2376		sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len =
2377			sizeof(struct sockaddr_in6);
2378		bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr));
2379		bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2380		bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr));
2381		rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw,
2382		    (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST,
2383		    (struct sockaddr *)&ssrc);
2384	}
2385	/* finally update cached route in each socket via pfctlinput */
2386    {
2387	struct sockaddr_in6 sdst;
2388
2389	bzero(&sdst, sizeof(sdst));
2390	sdst.sin6_family = AF_INET6;
2391	sdst.sin6_len = sizeof(struct sockaddr_in6);
2392	bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr));
2393	pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst);
2394#ifdef IPSEC
2395	key_sa_routechange((struct sockaddr *)&sdst);
2396#endif
2397    }
2398
2399 freeit:
2400	m_freem(m);
2401	return;
2402
2403 bad:
2404	icmp6stat.icp6s_badredirect++;
2405	m_freem(m);
2406}
2407
2408void
2409icmp6_redirect_output(m0, rt)
2410	struct mbuf *m0;
2411	struct rtentry *rt;
2412{
2413	struct ifnet *ifp;	/* my outgoing interface */
2414	struct in6_addr *ifp_ll6;
2415	struct in6_addr *router_ll6;
2416	struct ip6_hdr *sip6;	/* m0 as struct ip6_hdr */
2417	struct mbuf *m = NULL;	/* newly allocated one */
2418	struct ip6_hdr *ip6;	/* m as struct ip6_hdr */
2419	struct nd_redirect *nd_rd;
2420	size_t maxlen;
2421	u_char *p;
2422	struct ifnet *outif = NULL;
2423	struct sockaddr_in6 src_sa;
2424
2425	icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0);
2426
2427	/* if we are not router, we don't send icmp6 redirect */
2428	if (!ip6_forwarding || ip6_accept_rtadv)
2429		goto fail;
2430
2431	/* sanity check */
2432	if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp))
2433		goto fail;
2434
2435	/*
2436	 * Address check:
2437	 *  the source address must identify a neighbor, and
2438	 *  the destination address must not be a multicast address
2439	 *  [RFC 2461, sec 8.2]
2440	 */
2441	sip6 = mtod(m0, struct ip6_hdr *);
2442	bzero(&src_sa, sizeof(src_sa));
2443	src_sa.sin6_family = AF_INET6;
2444	src_sa.sin6_len = sizeof(src_sa);
2445	src_sa.sin6_addr = sip6->ip6_src;
2446	/* we don't currently use sin6_scope_id, but eventually use it */
2447	if (in6_addr2zoneid(ifp, &sip6->ip6_src, &src_sa.sin6_scope_id))
2448		goto fail;
2449	if (nd6_is_addr_neighbor(&src_sa, ifp) == 0)
2450		goto fail;
2451	if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst))
2452		goto fail;	/* what should we do here? */
2453
2454	/* rate limit */
2455	if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0))
2456		goto fail;
2457
2458	/*
2459	 * Since we are going to append up to 1280 bytes (= IPV6_MMTU),
2460	 * we almost always ask for an mbuf cluster for simplicity.
2461	 * (MHLEN < IPV6_MMTU is almost always true)
2462	 */
2463#if IPV6_MMTU >= MCLBYTES
2464# error assumption failed about IPV6_MMTU and MCLBYTES
2465#endif
2466	MGETHDR(m, M_DONTWAIT, MT_HEADER);
2467	if (m && IPV6_MMTU >= MHLEN)
2468		MCLGET(m, M_DONTWAIT);
2469	if (!m)
2470		goto fail;
2471	m->m_pkthdr.rcvif = NULL;
2472	m->m_len = 0;
2473	maxlen = M_TRAILINGSPACE(m);
2474	maxlen = min(IPV6_MMTU, maxlen);
2475	/* just for safety */
2476	if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) +
2477	    ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) {
2478		goto fail;
2479	}
2480
2481	{
2482		/* get ip6 linklocal address for ifp(my outgoing interface). */
2483		struct in6_ifaddr *ia;
2484		if ((ia = in6ifa_ifpforlinklocal(ifp,
2485						 IN6_IFF_NOTREADY|
2486						 IN6_IFF_ANYCAST)) == NULL)
2487			goto fail;
2488		ifp_ll6 = &ia->ia_addr.sin6_addr;
2489	}
2490
2491	/* get ip6 linklocal address for the router. */
2492	if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) {
2493		struct sockaddr_in6 *sin6;
2494		sin6 = (struct sockaddr_in6 *)rt->rt_gateway;
2495		router_ll6 = &sin6->sin6_addr;
2496		if (!IN6_IS_ADDR_LINKLOCAL(router_ll6))
2497			router_ll6 = (struct in6_addr *)NULL;
2498	} else
2499		router_ll6 = (struct in6_addr *)NULL;
2500
2501	/* ip6 */
2502	ip6 = mtod(m, struct ip6_hdr *);
2503	ip6->ip6_flow = 0;
2504	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
2505	ip6->ip6_vfc |= IPV6_VERSION;
2506	/* ip6->ip6_plen will be set later */
2507	ip6->ip6_nxt = IPPROTO_ICMPV6;
2508	ip6->ip6_hlim = 255;
2509	/* ip6->ip6_src must be linklocal addr for my outgoing if. */
2510	bcopy(ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr));
2511	bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr));
2512
2513	/* ND Redirect */
2514	nd_rd = (struct nd_redirect *)(ip6 + 1);
2515	nd_rd->nd_rd_type = ND_REDIRECT;
2516	nd_rd->nd_rd_code = 0;
2517	nd_rd->nd_rd_reserved = 0;
2518	if (rt->rt_flags & RTF_GATEWAY) {
2519		/*
2520		 * nd_rd->nd_rd_target must be a link-local address in
2521		 * better router cases.
2522		 */
2523		if (!router_ll6)
2524			goto fail;
2525		bcopy(router_ll6, &nd_rd->nd_rd_target,
2526		    sizeof(nd_rd->nd_rd_target));
2527		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2528		    sizeof(nd_rd->nd_rd_dst));
2529	} else {
2530		/* make sure redtgt == reddst */
2531		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target,
2532		    sizeof(nd_rd->nd_rd_target));
2533		bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst,
2534		    sizeof(nd_rd->nd_rd_dst));
2535	}
2536
2537	p = (u_char *)(nd_rd + 1);
2538
2539	if (!router_ll6)
2540		goto nolladdropt;
2541
2542	{
2543		/* target lladdr option */
2544		struct rtentry *rt_router = NULL;
2545		int len;
2546		struct sockaddr_dl *sdl;
2547		struct nd_opt_hdr *nd_opt;
2548		char *lladdr;
2549
2550		rt_router = nd6_lookup(router_ll6, 0, ifp);
2551		if (!rt_router)
2552			goto nolladdropt;
2553		len = sizeof(*nd_opt) + ifp->if_addrlen;
2554		len = (len + 7) & ~7;	/* round by 8 */
2555		/* safety check */
2556		if (len + (p - (u_char *)ip6) > maxlen)
2557			goto nolladdropt;
2558		if (!(rt_router->rt_flags & RTF_GATEWAY) &&
2559		    (rt_router->rt_flags & RTF_LLINFO) &&
2560		    (rt_router->rt_gateway->sa_family == AF_LINK) &&
2561		    (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) &&
2562		    sdl->sdl_alen) {
2563			nd_opt = (struct nd_opt_hdr *)p;
2564			nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR;
2565			nd_opt->nd_opt_len = len >> 3;
2566			lladdr = (char *)(nd_opt + 1);
2567			bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen);
2568			p += len;
2569		}
2570	}
2571nolladdropt:;
2572
2573	m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2574
2575	/* just to be safe */
2576#ifdef M_DECRYPTED	/*not openbsd*/
2577	if (m0->m_flags & M_DECRYPTED)
2578		goto noredhdropt;
2579#endif
2580	if (p - (u_char *)ip6 > maxlen)
2581		goto noredhdropt;
2582
2583	{
2584		/* redirected header option */
2585		int len;
2586		struct nd_opt_rd_hdr *nd_opt_rh;
2587
2588		/*
2589		 * compute the maximum size for icmp6 redirect header option.
2590		 * XXX room for auth header?
2591		 */
2592		len = maxlen - (p - (u_char *)ip6);
2593		len &= ~7;
2594
2595		/* This is just for simplicity. */
2596		if (m0->m_pkthdr.len != m0->m_len) {
2597			if (m0->m_next) {
2598				m_freem(m0->m_next);
2599				m0->m_next = NULL;
2600			}
2601			m0->m_pkthdr.len = m0->m_len;
2602		}
2603
2604		/*
2605		 * Redirected header option spec (RFC2461 4.6.3) talks nothing
2606		 * about padding/truncate rule for the original IP packet.
2607		 * From the discussion on IPv6imp in Feb 1999,
2608		 * the consensus was:
2609		 * - "attach as much as possible" is the goal
2610		 * - pad if not aligned (original size can be guessed by
2611		 *   original ip6 header)
2612		 * Following code adds the padding if it is simple enough,
2613		 * and truncates if not.
2614		 */
2615		if (m0->m_next || m0->m_pkthdr.len != m0->m_len)
2616			panic("assumption failed in %s:%d", __FILE__,
2617			    __LINE__);
2618
2619		if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) {
2620			/* not enough room, truncate */
2621			m0->m_pkthdr.len = m0->m_len = len -
2622			    sizeof(*nd_opt_rh);
2623		} else {
2624			/* enough room, pad or truncate */
2625			size_t extra;
2626
2627			extra = m0->m_pkthdr.len % 8;
2628			if (extra) {
2629				/* pad if easy enough, truncate if not */
2630				if (8 - extra <= M_TRAILINGSPACE(m0)) {
2631					/* pad */
2632					m0->m_len += (8 - extra);
2633					m0->m_pkthdr.len += (8 - extra);
2634				} else {
2635					/* truncate */
2636					m0->m_pkthdr.len -= extra;
2637					m0->m_len -= extra;
2638				}
2639			}
2640			len = m0->m_pkthdr.len + sizeof(*nd_opt_rh);
2641			m0->m_pkthdr.len = m0->m_len = len -
2642			    sizeof(*nd_opt_rh);
2643		}
2644
2645		nd_opt_rh = (struct nd_opt_rd_hdr *)p;
2646		bzero(nd_opt_rh, sizeof(*nd_opt_rh));
2647		nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER;
2648		nd_opt_rh->nd_opt_rh_len = len >> 3;
2649		p += sizeof(*nd_opt_rh);
2650		m->m_pkthdr.len = m->m_len = p - (u_char *)ip6;
2651
2652		/* connect m0 to m */
2653		m_tag_delete_chain(m0, NULL);
2654		m0->m_flags &= ~M_PKTHDR;
2655		m->m_next = m0;
2656		m->m_pkthdr.len = m->m_len + m0->m_len;
2657		m0 = NULL;
2658	}
2659noredhdropt:;
2660	if (m0) {
2661		m_freem(m0);
2662		m0 = NULL;
2663	}
2664
2665	/* XXX: clear embedded link IDs in the inner header */
2666	in6_clearscope(&sip6->ip6_src);
2667	in6_clearscope(&sip6->ip6_dst);
2668	in6_clearscope(&nd_rd->nd_rd_target);
2669	in6_clearscope(&nd_rd->nd_rd_dst);
2670
2671	ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
2672
2673	nd_rd->nd_rd_cksum = 0;
2674	nd_rd->nd_rd_cksum = in6_cksum(m, IPPROTO_ICMPV6,
2675	    sizeof(*ip6), ntohs(ip6->ip6_plen));
2676
2677	/* send the packet to outside... */
2678	ip6_output(m, NULL, NULL, 0, NULL, &outif, NULL);
2679	if (outif) {
2680		icmp6_ifstat_inc(outif, ifs6_out_msg);
2681		icmp6_ifstat_inc(outif, ifs6_out_redirect);
2682	}
2683	icmp6stat.icp6s_outhist[ND_REDIRECT]++;
2684
2685	return;
2686
2687fail:
2688	if (m)
2689		m_freem(m);
2690	if (m0)
2691		m_freem(m0);
2692}
2693
2694/*
2695 * ICMPv6 socket option processing.
2696 */
2697int
2698icmp6_ctloutput(so, sopt)
2699	struct socket *so;
2700	struct sockopt *sopt;
2701{
2702	int error = 0;
2703	int optlen;
2704	struct inpcb *inp = sotoinpcb(so);
2705	int level, op, optname;
2706
2707	if (sopt) {
2708		level = sopt->sopt_level;
2709		op = sopt->sopt_dir;
2710		optname = sopt->sopt_name;
2711		optlen = sopt->sopt_valsize;
2712	} else
2713		level = op = optname = optlen = 0;
2714
2715	if (level != IPPROTO_ICMPV6) {
2716		return EINVAL;
2717	}
2718
2719	switch (op) {
2720	case PRCO_SETOPT:
2721		switch (optname) {
2722		case ICMP6_FILTER:
2723		    {
2724			struct icmp6_filter *p;
2725
2726			if (optlen != sizeof(*p)) {
2727				error = EMSGSIZE;
2728				break;
2729			}
2730			if (inp->in6p_icmp6filt == NULL) {
2731				error = EINVAL;
2732				break;
2733			}
2734			error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen,
2735				optlen);
2736			break;
2737		    }
2738
2739		default:
2740			error = ENOPROTOOPT;
2741			break;
2742		}
2743		break;
2744
2745	case PRCO_GETOPT:
2746		switch (optname) {
2747		case ICMP6_FILTER:
2748		    {
2749			if (inp->in6p_icmp6filt == NULL) {
2750				error = EINVAL;
2751				break;
2752			}
2753			error = sooptcopyout(sopt, inp->in6p_icmp6filt,
2754				sizeof(struct icmp6_filter));
2755			break;
2756		    }
2757
2758		default:
2759			error = ENOPROTOOPT;
2760			break;
2761		}
2762		break;
2763	}
2764
2765	return (error);
2766}
2767
2768/*
2769 * Perform rate limit check.
2770 * Returns 0 if it is okay to send the icmp6 packet.
2771 * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate
2772 * limitation.
2773 *
2774 * XXX per-destination/type check necessary?
2775 */
2776static int
2777icmp6_ratelimit(dst, type, code)
2778	const struct in6_addr *dst;	/* not used at this moment */
2779	const int type;			/* not used at this moment */
2780	const int code;			/* not used at this moment */
2781{
2782	int ret;
2783
2784	ret = 0;	/* okay to send */
2785
2786	/* PPS limit */
2787	if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count,
2788	    icmp6errppslim)) {
2789		/* The packet is subject to rate limit */
2790		ret++;
2791	}
2792
2793	return ret;
2794}
2795