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