1 1.149 roy /* $NetBSD: nd6_rtr.c,v 1.149 2020/06/12 11:04:45 roy Exp $ */ 2 1.20 itojun /* $KAME: nd6_rtr.c,v 1.95 2001/02/07 08:09:47 itojun Exp $ */ 3 1.3 thorpej 4 1.2 itojun /* 5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 1.2 itojun * All rights reserved. 7 1.16 itojun * 8 1.2 itojun * Redistribution and use in source and binary forms, with or without 9 1.2 itojun * modification, are permitted provided that the following conditions 10 1.2 itojun * are met: 11 1.2 itojun * 1. Redistributions of source code must retain the above copyright 12 1.2 itojun * notice, this list of conditions and the following disclaimer. 13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright 14 1.2 itojun * notice, this list of conditions and the following disclaimer in the 15 1.2 itojun * documentation and/or other materials provided with the distribution. 16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors 17 1.2 itojun * may be used to endorse or promote products derived from this software 18 1.2 itojun * without specific prior written permission. 19 1.16 itojun * 20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 1.2 itojun * SUCH DAMAGE. 31 1.2 itojun */ 32 1.27 lukem 33 1.27 lukem #include <sys/cdefs.h> 34 1.149 roy __KERNEL_RCSID(0, "$NetBSD: nd6_rtr.c,v 1.149 2020/06/12 11:04:45 roy Exp $"); 35 1.134 msaitoh 36 1.134 msaitoh #ifdef _KERNEL_OPT 37 1.134 msaitoh #include "opt_net_mpsafe.h" 38 1.134 msaitoh #endif 39 1.2 itojun 40 1.2 itojun #include <sys/mbuf.h> 41 1.2 itojun #include <sys/syslog.h> 42 1.2 itojun 43 1.2 itojun #include <net/if.h> 44 1.2 itojun 45 1.2 itojun #include <netinet/in.h> 46 1.2 itojun #include <netinet6/in6_var.h> 47 1.11 itojun #include <netinet/ip6.h> 48 1.2 itojun #include <netinet6/nd6.h> 49 1.11 itojun #include <netinet/icmp6.h> 50 1.75 thorpej #include <netinet6/icmp6_private.h> 51 1.101 ozaki 52 1.2 itojun /* 53 1.149 roy * Cache the source link layer address of Router Advertisement 54 1.149 roy * and Solicition messages. 55 1.2 itojun */ 56 1.2 itojun void 57 1.149 roy nd6_rtr_cache(struct mbuf *m, int off, int icmp6len, int icmp6_type) 58 1.2 itojun { 59 1.111 ozaki struct ifnet *ifp; 60 1.2 itojun struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 61 1.12 itojun struct nd_router_solicit *nd_rs; 62 1.149 roy struct nd_router_advert *nd_ra; 63 1.2 itojun struct in6_addr saddr6 = ip6->ip6_src; 64 1.2 itojun char *lladdr = NULL; 65 1.2 itojun int lladdrlen = 0; 66 1.2 itojun union nd_opts ndopts; 67 1.111 ozaki struct psref psref; 68 1.130 ryo char ip6bufs[INET6_ADDRSTRLEN], ip6bufd[INET6_ADDRSTRLEN]; 69 1.111 ozaki 70 1.111 ozaki ifp = m_get_rcvif_psref(m, &psref); 71 1.111 ozaki if (ifp == NULL) 72 1.111 ozaki goto freeit; 73 1.111 ozaki 74 1.2 itojun /* Sanity checks */ 75 1.2 itojun if (ip6->ip6_hlim != 255) { 76 1.107 ozaki nd6log(LOG_ERR, "invalid hlim (%d) from %s to %s on %s\n", 77 1.131 christos ip6->ip6_hlim, IN6_PRINT(ip6bufs, &ip6->ip6_src), 78 1.131 christos IN6_PRINT(ip6bufd, &ip6->ip6_dst), if_name(ifp)); 79 1.20 itojun goto bad; 80 1.2 itojun } 81 1.2 itojun 82 1.149 roy switch (icmp6_type) { 83 1.149 roy case ND_ROUTER_SOLICIT: 84 1.149 roy /* 85 1.149 roy * Don't update the neighbor cache, if src = ::. 86 1.149 roy * This indicates that the src has no IP address assigned yet. 87 1.149 roy */ 88 1.149 roy if (IN6_IS_ADDR_UNSPECIFIED(&saddr6)) 89 1.149 roy goto freeit; 90 1.12 itojun 91 1.149 roy IP6_EXTHDR_GET(nd_rs, struct nd_router_solicit *, m, off, 92 1.149 roy icmp6len); 93 1.149 roy if (nd_rs == NULL) { 94 1.149 roy ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 95 1.149 roy m_put_rcvif_psref(ifp, &psref); 96 1.149 roy return; 97 1.149 roy } 98 1.149 roy 99 1.149 roy icmp6len -= sizeof(*nd_rs); 100 1.149 roy nd6_option_init(nd_rs + 1, icmp6len, &ndopts); 101 1.149 roy break; 102 1.149 roy case ND_ROUTER_ADVERT: 103 1.149 roy if (!IN6_IS_ADDR_LINKLOCAL(&saddr6)) { 104 1.149 roy nd6log(LOG_ERR, "src %s is not link-local\n", 105 1.149 roy IN6_PRINT(ip6bufs, &saddr6)); 106 1.149 roy goto bad; 107 1.149 roy } 108 1.149 roy 109 1.149 roy IP6_EXTHDR_GET(nd_ra, struct nd_router_advert *, m, off, 110 1.149 roy icmp6len); 111 1.149 roy if (nd_ra == NULL) { 112 1.149 roy ICMP6_STATINC(ICMP6_STAT_TOOSHORT); 113 1.149 roy m_put_rcvif_psref(ifp, &psref); 114 1.149 roy return; 115 1.149 roy } 116 1.149 roy 117 1.149 roy icmp6len -= sizeof(*nd_ra); 118 1.149 roy nd6_option_init(nd_ra + 1, icmp6len, &ndopts); 119 1.149 roy break; 120 1.12 itojun } 121 1.2 itojun 122 1.2 itojun if (nd6_options(&ndopts) < 0) { 123 1.107 ozaki nd6log(LOG_INFO, "invalid ND option, ignored\n"); 124 1.20 itojun /* nd6_options have incremented stats */ 125 1.12 itojun goto freeit; 126 1.2 itojun } 127 1.2 itojun 128 1.2 itojun if (ndopts.nd_opts_src_lladdr) { 129 1.2 itojun lladdr = (char *)(ndopts.nd_opts_src_lladdr + 1); 130 1.2 itojun lladdrlen = ndopts.nd_opts_src_lladdr->nd_opt_len << 3; 131 1.2 itojun } 132 1.2 itojun 133 1.2 itojun if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { 134 1.107 ozaki nd6log(LOG_INFO, "lladdrlen mismatch for %s " 135 1.149 roy "(if %d, %s packet %d)\n", 136 1.131 christos IN6_PRINT(ip6bufs, &saddr6), 137 1.149 roy ifp->if_addrlen, 138 1.149 roy icmp6_type == ND_ROUTER_SOLICIT ? "RS" : "RA", 139 1.149 roy lladdrlen - 2); 140 1.20 itojun goto bad; 141 1.2 itojun } 142 1.2 itojun 143 1.149 roy nd6_cache_lladdr(ifp, &saddr6, lladdr, lladdrlen, icmp6_type, 0); 144 1.7 itojun 145 1.149 roy freeit: 146 1.111 ozaki m_put_rcvif_psref(ifp, &psref); 147 1.20 itojun m_freem(m); 148 1.20 itojun return; 149 1.20 itojun 150 1.149 roy bad: 151 1.149 roy ICMP6_STATINC(icmp6_type == ND_ROUTER_SOLICIT ? 152 1.149 roy ICMP6_STAT_BADRS : ICMP6_STAT_BADRA); 153 1.111 ozaki m_put_rcvif_psref(ifp, &psref); 154 1.12 itojun m_freem(m); 155 1.2 itojun } 156