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