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