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