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