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