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