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