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