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