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