1caf43b02SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 482cd038dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 582cd038dSYoshinobu Inoue * All rights reserved. 682cd038dSYoshinobu Inoue * 782cd038dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 882cd038dSYoshinobu Inoue * modification, are permitted provided that the following conditions 982cd038dSYoshinobu Inoue * are met: 1082cd038dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 1182cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 1282cd038dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 1382cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 1482cd038dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 1582cd038dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 1682cd038dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 1782cd038dSYoshinobu Inoue * without specific prior written permission. 1882cd038dSYoshinobu Inoue * 1982cd038dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2082cd038dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2182cd038dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2282cd038dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2382cd038dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2482cd038dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2582cd038dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2682cd038dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2782cd038dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2882cd038dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2982cd038dSYoshinobu Inoue * SUCH DAMAGE. 3082cd038dSYoshinobu Inoue */ 3182cd038dSYoshinobu Inoue 32caf43b02SWarner Losh /*- 3382cd038dSYoshinobu Inoue * Copyright (c) 1982, 1986, 1988, 1993 340ae76120SRobert Watson * The Regents of the University of California. 350ae76120SRobert Watson * All rights reserved. 3682cd038dSYoshinobu Inoue * 3782cd038dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 3882cd038dSYoshinobu Inoue * modification, are permitted provided that the following conditions 3982cd038dSYoshinobu Inoue * are met: 4082cd038dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 4182cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 4282cd038dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 4382cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 4482cd038dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 45fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 4682cd038dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 4782cd038dSYoshinobu Inoue * without specific prior written permission. 4882cd038dSYoshinobu Inoue * 4982cd038dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 5082cd038dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5182cd038dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5282cd038dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 5382cd038dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 5482cd038dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5582cd038dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5682cd038dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5782cd038dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5882cd038dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5982cd038dSYoshinobu Inoue * SUCH DAMAGE. 6082cd038dSYoshinobu Inoue * 6182cd038dSYoshinobu Inoue * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 6282cd038dSYoshinobu Inoue */ 6382cd038dSYoshinobu Inoue 64b48287a3SDavid E. O'Brien #include <sys/cdefs.h> 65b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$"); 66b48287a3SDavid E. O'Brien 676a800098SYoshinobu Inoue #include "opt_ipsec.h" 6833841545SHajimu UMEMOTO #include "opt_inet6.h" 690c325f53SAlexander V. Chernikov #include "opt_route.h" 706a800098SYoshinobu Inoue 7182cd038dSYoshinobu Inoue #include <sys/param.h> 7282cd038dSYoshinobu Inoue #include <sys/errno.h> 73413628a7SBjoern A. Zeeb #include <sys/jail.h> 74a786f679SAndrey V. Elsukov #include <sys/kernel.h> 75960ed29cSSeigo Tanimura #include <sys/lock.h> 76960ed29cSSeigo Tanimura #include <sys/malloc.h> 77960ed29cSSeigo Tanimura #include <sys/mbuf.h> 78c2259ba4SRobert Watson #include <sys/priv.h> 79960ed29cSSeigo Tanimura #include <sys/proc.h> 80960ed29cSSeigo Tanimura #include <sys/protosw.h> 81960ed29cSSeigo Tanimura #include <sys/signalvar.h> 82960ed29cSSeigo Tanimura #include <sys/socket.h> 83960ed29cSSeigo Tanimura #include <sys/socketvar.h> 84960ed29cSSeigo Tanimura #include <sys/sx.h> 85a1f7e5f8SHajimu UMEMOTO #include <sys/syslog.h> 8682cd038dSYoshinobu Inoue 8782cd038dSYoshinobu Inoue #include <net/if.h> 8876039bc8SGleb Smirnoff #include <net/if_var.h> 8982cd038dSYoshinobu Inoue #include <net/if_types.h> 90960ed29cSSeigo Tanimura #include <net/route.h> 914b79449eSBjoern A. Zeeb #include <net/vnet.h> 9282cd038dSYoshinobu Inoue 9382cd038dSYoshinobu Inoue #include <netinet/in.h> 9482cd038dSYoshinobu Inoue #include <netinet/in_var.h> 9582cd038dSYoshinobu Inoue #include <netinet/in_systm.h> 9682cd038dSYoshinobu Inoue #include <netinet/in_pcb.h> 974b79449eSBjoern A. Zeeb 984b79449eSBjoern A. Zeeb #include <netinet/icmp6.h> 99960ed29cSSeigo Tanimura #include <netinet/ip6.h> 1001db8d1f8SAna Kukec #include <netinet/ip_var.h> 101686cdd19SJun-ichiro itojun Hagino #include <netinet6/ip6protosw.h> 102960ed29cSSeigo Tanimura #include <netinet6/ip6_mroute.h> 103960ed29cSSeigo Tanimura #include <netinet6/in6_pcb.h> 104960ed29cSSeigo Tanimura #include <netinet6/ip6_var.h> 105960ed29cSSeigo Tanimura #include <netinet6/nd6.h> 106960ed29cSSeigo Tanimura #include <netinet6/raw_ip6.h> 1070c325f53SAlexander V. Chernikov #include <netinet6/in6_fib.h> 108686cdd19SJun-ichiro itojun Hagino #include <netinet6/scope6_var.h> 1091db8d1f8SAna Kukec #include <netinet6/send.h> 11082cd038dSYoshinobu Inoue 111fcf59617SAndrey V. Elsukov #include <netipsec/ipsec_support.h> 112b9234fafSSam Leffler 11382cd038dSYoshinobu Inoue #include <machine/stdarg.h> 11482cd038dSYoshinobu Inoue 11582cd038dSYoshinobu Inoue #define satosin6(sa) ((struct sockaddr_in6 *)(sa)) 11682cd038dSYoshinobu Inoue #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) 11782cd038dSYoshinobu Inoue 11882cd038dSYoshinobu Inoue /* 11982cd038dSYoshinobu Inoue * Raw interface to IP6 protocol. 12082cd038dSYoshinobu Inoue */ 12182cd038dSYoshinobu Inoue 122eddfbb76SRobert Watson VNET_DECLARE(struct inpcbinfo, ripcbinfo); 1231e77c105SRobert Watson #define V_ripcbinfo VNET(ripcbinfo) 124eddfbb76SRobert Watson 12597021c24SMarko Zec extern u_long rip_sendspace; 12697021c24SMarko Zec extern u_long rip_recvspace; 12797021c24SMarko Zec 128a786f679SAndrey V. Elsukov VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat); 129a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSINIT(rip6stat); 130a786f679SAndrey V. Elsukov 131a786f679SAndrey V. Elsukov #ifdef VIMAGE 132a786f679SAndrey V. Elsukov VNET_PCPUSTAT_SYSUNINIT(rip6stat); 133a786f679SAndrey V. Elsukov #endif /* VIMAGE */ 13482cea7e6SBjoern A. Zeeb 13582cd038dSYoshinobu Inoue /* 13633cde130SBruce M Simpson * Hooks for multicast routing. They all default to NULL, so leave them not 13733cde130SBruce M Simpson * initialized and rely on BSS being set to 0. 1386be2e366SBruce M Simpson */ 13933cde130SBruce M Simpson 14033cde130SBruce M Simpson /* 14133cde130SBruce M Simpson * The socket used to communicate with the multicast routing daemon. 14233cde130SBruce M Simpson */ 143eddfbb76SRobert Watson VNET_DEFINE(struct socket *, ip6_mrouter); 14433cde130SBruce M Simpson 14533cde130SBruce M Simpson /* 14633cde130SBruce M Simpson * The various mrouter functions. 14733cde130SBruce M Simpson */ 1486be2e366SBruce M Simpson int (*ip6_mrouter_set)(struct socket *, struct sockopt *); 1496be2e366SBruce M Simpson int (*ip6_mrouter_get)(struct socket *, struct sockopt *); 1506be2e366SBruce M Simpson int (*ip6_mrouter_done)(void); 1516be2e366SBruce M Simpson int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *); 152e40bae9aSRoman Divacky int (*mrt6_ioctl)(u_long, caddr_t); 1536be2e366SBruce M Simpson 154db0ac6deSCy Schubert struct rip6_inp_match_ctx { 155db0ac6deSCy Schubert struct ip6_hdr *ip6; 156db0ac6deSCy Schubert int proto; 157db0ac6deSCy Schubert }; 158db0ac6deSCy Schubert 159db0ac6deSCy Schubert static bool 160db0ac6deSCy Schubert rip6_inp_match(const struct inpcb *inp, void *v) 161db0ac6deSCy Schubert { 162db0ac6deSCy Schubert struct rip6_inp_match_ctx *c = v; 163db0ac6deSCy Schubert struct ip6_hdr *ip6 = c->ip6; 164db0ac6deSCy Schubert int proto = c->proto; 165db0ac6deSCy Schubert 166db0ac6deSCy Schubert /* XXX inp locking */ 167db0ac6deSCy Schubert if ((inp->inp_vflag & INP_IPV6) == 0) 168db0ac6deSCy Schubert return (false); 169db0ac6deSCy Schubert if (inp->inp_ip_p && inp->inp_ip_p != proto) 170db0ac6deSCy Schubert return (false); 171db0ac6deSCy Schubert if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 172db0ac6deSCy Schubert !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst)) 173db0ac6deSCy Schubert return (false); 174db0ac6deSCy Schubert if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && 175db0ac6deSCy Schubert !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src)) 176db0ac6deSCy Schubert return (false); 177db0ac6deSCy Schubert 178db0ac6deSCy Schubert return (true); 179db0ac6deSCy Schubert } 180db0ac6deSCy Schubert 1816be2e366SBruce M Simpson /* 1820ae76120SRobert Watson * Setup generic address and protocol structures for raw_input routine, then 1830ae76120SRobert Watson * pass them along with mbuf chain. 18482cd038dSYoshinobu Inoue */ 18582cd038dSYoshinobu Inoue int 1861272577eSXin LI rip6_input(struct mbuf **mp, int *offp, int proto) 18782cd038dSYoshinobu Inoue { 18833cde130SBruce M Simpson struct ifnet *ifp; 189db0ac6deSCy Schubert struct mbuf *n, *m = *mp; 1903e85b721SEd Maste struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 1910ecd976eSBjoern A. Zeeb struct inpcb *inp; 19233841545SHajimu UMEMOTO struct mbuf *opts = NULL; 193af12e09eSHajimu UMEMOTO struct sockaddr_in6 fromsa; 194db0ac6deSCy Schubert struct rip6_inp_match_ctx ctx = { .ip6 = ip6, .proto = proto }; 195db0ac6deSCy Schubert struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo, 196db0ac6deSCy Schubert INPLOOKUP_RLOCKPCB, rip6_inp_match, &ctx); 197db0ac6deSCy Schubert int delivered = 0; 198f42347c3SGleb Smirnoff 199f42347c3SGleb Smirnoff NET_EPOCH_ASSERT(); 20082cd038dSYoshinobu Inoue 201f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_ipackets); 20233841545SHajimu UMEMOTO 203dce33a45SErmal Luçi init_sin6(&fromsa, m, 0); /* general init */ 20482cd038dSYoshinobu Inoue 20533cde130SBruce M Simpson ifp = m->m_pkthdr.rcvif; 20633cde130SBruce M Simpson 207db0ac6deSCy Schubert while ((inp = inp_next(&inpi)) != NULL) { 208db0ac6deSCy Schubert INP_RLOCK_ASSERT(inp); 2091ff6e7a8SBjoern A. Zeeb #if defined(IPSEC) || defined(IPSEC_SUPPORT) 2101ff6e7a8SBjoern A. Zeeb /* 2111ff6e7a8SBjoern A. Zeeb * Check AH/ESP integrity. 2121ff6e7a8SBjoern A. Zeeb */ 213db0ac6deSCy Schubert if (IPSEC_ENABLED(ipv6) && 214db0ac6deSCy Schubert IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) { 2151ff6e7a8SBjoern A. Zeeb /* Do not inject data into pcb. */ 216db0ac6deSCy Schubert continue; 2171ff6e7a8SBjoern A. Zeeb } 2181ff6e7a8SBjoern A. Zeeb #endif /* IPSEC */ 219db0ac6deSCy Schubert if (jailed_without_vnet(inp->inp_cred) && 220db0ac6deSCy Schubert !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && 221db0ac6deSCy Schubert prison_check_ip6(inp->inp_cred, &ip6->ip6_dst) != 0) 22233cde130SBruce M Simpson /* 22333cde130SBruce M Simpson * Allow raw socket in jail to receive multicast; 22433cde130SBruce M Simpson * assume process had PRIV_NETINET_RAW at attach, 22533cde130SBruce M Simpson * and fall through into normal filter path if so. 22633cde130SBruce M Simpson */ 227db0ac6deSCy Schubert continue; 2280ecd976eSBjoern A. Zeeb if (inp->in6p_cksum != -1) { 229f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_isum); 2300ecd976eSBjoern A. Zeeb if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 || 23170a0f3dcSMichael Tuexen in6_cksum(m, proto, *offp, 23282cd038dSYoshinobu Inoue m->m_pkthdr.len - *offp)) { 233f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_badsum); 234fb288770SMichael Tuexen /* 235fb288770SMichael Tuexen * Drop the received message, don't send an 236fb288770SMichael Tuexen * ICMP6 message. Set proto to IPPROTO_NONE 237fb288770SMichael Tuexen * to achieve that. 238fb288770SMichael Tuexen */ 239db0ac6deSCy Schubert INP_RUNLOCK(inp); 240fb288770SMichael Tuexen proto = IPPROTO_NONE; 241db0ac6deSCy Schubert break; 24282cd038dSYoshinobu Inoue } 24333841545SHajimu UMEMOTO } 24433cde130SBruce M Simpson /* 24533cde130SBruce M Simpson * If this raw socket has multicast state, and we 24633cde130SBruce M Simpson * have received a multicast, check if this socket 24733cde130SBruce M Simpson * should receive it, as multicast filtering is now 24833cde130SBruce M Simpson * the responsibility of the transport layer. 24933cde130SBruce M Simpson */ 2500ecd976eSBjoern A. Zeeb if (inp->in6p_moptions && 25133cde130SBruce M Simpson IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 2527ab5a5cdSBruce M Simpson /* 2537ab5a5cdSBruce M Simpson * If the incoming datagram is for MLD, allow it 2547ab5a5cdSBruce M Simpson * through unconditionally to the raw socket. 2557ab5a5cdSBruce M Simpson * 2567ab5a5cdSBruce M Simpson * Use the M_RTALERT_MLD flag to check for MLD 2577ab5a5cdSBruce M Simpson * traffic without having to inspect the mbuf chain 2587ab5a5cdSBruce M Simpson * more deeply, as all MLDv1/v2 host messages MUST 2597ab5a5cdSBruce M Simpson * contain the Router Alert option. 2607ab5a5cdSBruce M Simpson * 2617ab5a5cdSBruce M Simpson * In the case of MLDv1, we may not have explicitly 2627ab5a5cdSBruce M Simpson * joined the group, and may have set IFF_ALLMULTI 2637ab5a5cdSBruce M Simpson * on the interface. im6o_mc_filter() may discard 2647ab5a5cdSBruce M Simpson * control traffic we actually need to see. 2657ab5a5cdSBruce M Simpson * 2667ab5a5cdSBruce M Simpson * Userland multicast routing daemons should continue 2677ab5a5cdSBruce M Simpson * filter the control traffic appropriately. 2687ab5a5cdSBruce M Simpson */ 26933cde130SBruce M Simpson int blocked; 27033cde130SBruce M Simpson 2717ab5a5cdSBruce M Simpson blocked = MCAST_PASS; 2727ab5a5cdSBruce M Simpson if ((m->m_flags & M_RTALERT_MLD) == 0) { 2737ab5a5cdSBruce M Simpson struct sockaddr_in6 mcaddr; 2747ab5a5cdSBruce M Simpson 27533cde130SBruce M Simpson bzero(&mcaddr, sizeof(struct sockaddr_in6)); 27633cde130SBruce M Simpson mcaddr.sin6_len = sizeof(struct sockaddr_in6); 27733cde130SBruce M Simpson mcaddr.sin6_family = AF_INET6; 27833cde130SBruce M Simpson mcaddr.sin6_addr = ip6->ip6_dst; 27933cde130SBruce M Simpson 2800ecd976eSBjoern A. Zeeb blocked = im6o_mc_filter(inp->in6p_moptions, 2817ab5a5cdSBruce M Simpson ifp, 28233cde130SBruce M Simpson (struct sockaddr *)&mcaddr, 28333cde130SBruce M Simpson (struct sockaddr *)&fromsa); 2847ab5a5cdSBruce M Simpson } 28533cde130SBruce M Simpson if (blocked != MCAST_PASS) { 28633cde130SBruce M Simpson IP6STAT_INC(ip6s_notmember); 287de2d4784SGleb Smirnoff continue; 288266f97b5SCy Schubert } 289db0ac6deSCy Schubert } 290db0ac6deSCy Schubert if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL) 291db0ac6deSCy Schubert continue; 292db0ac6deSCy Schubert if (inp->inp_flags & INP_CONTROLOPTS || 293db0ac6deSCy Schubert inp->inp_socket->so_options & SO_TIMESTAMP) 294db0ac6deSCy Schubert ip6_savecontrol(inp, n, &opts); 295db0ac6deSCy Schubert /* strip intermediate headers */ 296db0ac6deSCy Schubert m_adj(n, *offp); 297db0ac6deSCy Schubert if (sbappendaddr(&inp->inp_socket->so_rcv, 298db0ac6deSCy Schubert (struct sockaddr *)&fromsa, n, opts) == 0) { 299db0ac6deSCy Schubert soroverflow(inp->inp_socket); 300db0ac6deSCy Schubert m_freem(n); 30182cd038dSYoshinobu Inoue if (opts) 30282cd038dSYoshinobu Inoue m_freem(opts); 303f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_fullsock); 30482cd038dSYoshinobu Inoue } else { 305db0ac6deSCy Schubert sorwakeup(inp->inp_socket); 306db0ac6deSCy Schubert delivered++; 307db0ac6deSCy Schubert } 308db0ac6deSCy Schubert opts = NULL; 309db0ac6deSCy Schubert } 310db0ac6deSCy Schubert if (delivered == 0) { 311f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_nosock); 31233841545SHajimu UMEMOTO if (m->m_flags & M_MCAST) 313f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_nosockmcast); 31482cd038dSYoshinobu Inoue if (proto == IPPROTO_NONE) 31582cd038dSYoshinobu Inoue m_freem(m); 31668e0e5a6SAndrey V. Elsukov else 31782cd038dSYoshinobu Inoue icmp6_error(m, ICMP6_PARAM_PROB, 31882cd038dSYoshinobu Inoue ICMP6_PARAMPROB_NEXTHEADER, 31968e0e5a6SAndrey V. Elsukov ip6_get_prevhdr(m, *offp)); 3209cb8d207SAndrey V. Elsukov IP6STAT_DEC(ip6s_delivered); 321db0ac6deSCy Schubert } else 322db0ac6deSCy Schubert m_freem(m); 3230ae76120SRobert Watson return (IPPROTO_DONE); 32482cd038dSYoshinobu Inoue } 32582cd038dSYoshinobu Inoue 326686cdd19SJun-ichiro itojun Hagino void 3271272577eSXin LI rip6_ctlinput(int cmd, struct sockaddr *sa, void *d) 328686cdd19SJun-ichiro itojun Hagino { 32933841545SHajimu UMEMOTO struct ip6ctlparam *ip6cp = NULL; 33033841545SHajimu UMEMOTO const struct sockaddr_in6 *sa6_src = NULL; 331efddf5c6SHajimu UMEMOTO void *cmdarg; 3329233d8f3SDavid E. O'Brien struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange; 333686cdd19SJun-ichiro itojun Hagino 334686cdd19SJun-ichiro itojun Hagino if (sa->sa_family != AF_INET6 || 335686cdd19SJun-ichiro itojun Hagino sa->sa_len != sizeof(struct sockaddr_in6)) 336686cdd19SJun-ichiro itojun Hagino return; 337686cdd19SJun-ichiro itojun Hagino 338686cdd19SJun-ichiro itojun Hagino if ((unsigned)cmd >= PRC_NCMDS) 339686cdd19SJun-ichiro itojun Hagino return; 340686cdd19SJun-ichiro itojun Hagino if (PRC_IS_REDIRECT(cmd)) 341686cdd19SJun-ichiro itojun Hagino notify = in6_rtchange, d = NULL; 342686cdd19SJun-ichiro itojun Hagino else if (cmd == PRC_HOSTDEAD) 343686cdd19SJun-ichiro itojun Hagino d = NULL; 344686cdd19SJun-ichiro itojun Hagino else if (inet6ctlerrmap[cmd] == 0) 345686cdd19SJun-ichiro itojun Hagino return; 346686cdd19SJun-ichiro itojun Hagino 3470ae76120SRobert Watson /* 3480ae76120SRobert Watson * If the parameter is from icmp6, decode it. 3490ae76120SRobert Watson */ 350686cdd19SJun-ichiro itojun Hagino if (d != NULL) { 35133841545SHajimu UMEMOTO ip6cp = (struct ip6ctlparam *)d; 352efddf5c6SHajimu UMEMOTO cmdarg = ip6cp->ip6c_cmdarg; 35333841545SHajimu UMEMOTO sa6_src = ip6cp->ip6c_src; 354686cdd19SJun-ichiro itojun Hagino } else { 355efddf5c6SHajimu UMEMOTO cmdarg = NULL; 35633841545SHajimu UMEMOTO sa6_src = &sa6_any; 357686cdd19SJun-ichiro itojun Hagino } 358686cdd19SJun-ichiro itojun Hagino 359603724d3SBjoern A. Zeeb (void) in6_pcbnotify(&V_ripcbinfo, sa, 0, 3600ae76120SRobert Watson (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 361686cdd19SJun-ichiro itojun Hagino } 362686cdd19SJun-ichiro itojun Hagino 36382cd038dSYoshinobu Inoue /* 3640ae76120SRobert Watson * Generate IPv6 header and pass packet to ip6_output. Tack on options user 3650ae76120SRobert Watson * may have setup with control call. 36682cd038dSYoshinobu Inoue */ 367e0b40500SGleb Smirnoff static int 368e0b40500SGleb Smirnoff rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 369e0b40500SGleb Smirnoff struct mbuf *control, struct thread *td) 37082cd038dSYoshinobu Inoue { 371b9555453SGleb Smirnoff struct epoch_tracker et; 3720ecd976eSBjoern A. Zeeb struct inpcb *inp; 373e0b40500SGleb Smirnoff struct sockaddr_in6 tmp, *dstsock; 374e0b40500SGleb Smirnoff struct m_tag *mtag; 375e0b40500SGleb Smirnoff struct ip6_hdr *ip6; 37682cd038dSYoshinobu Inoue u_int plen = m->m_pkthdr.len; 377e07db7aaSHajimu UMEMOTO struct ip6_pktopts opt, *optp; 37882cd038dSYoshinobu Inoue struct ifnet *oifp = NULL; 379e0b40500SGleb Smirnoff int error; 38082cd038dSYoshinobu Inoue int type = 0, code = 0; /* for ICMPv6 output statistics only */ 381a1f7e5f8SHajimu UMEMOTO int scope_ambiguous = 0; 382388288b2SHajimu UMEMOTO int use_defzone = 0; 383601c0b8bSAlexander V. Chernikov int hlim = 0; 38488d166bfSBjoern A. Zeeb struct in6_addr in6a; 38582cd038dSYoshinobu Inoue 3860ecd976eSBjoern A. Zeeb inp = sotoinpcb(so); 387e0b40500SGleb Smirnoff KASSERT(inp != NULL, ("rip6_send: inp == NULL")); 388e0b40500SGleb Smirnoff 389e0b40500SGleb Smirnoff /* Always copy sockaddr to avoid overwrites. */ 390e0b40500SGleb Smirnoff /* Unlocked read. */ 391e0b40500SGleb Smirnoff if (so->so_state & SS_ISCONNECTED) { 392e0b40500SGleb Smirnoff if (nam) { 393e0b40500SGleb Smirnoff error = EISCONN; 394e0b40500SGleb Smirnoff goto release; 395e0b40500SGleb Smirnoff } 396e0b40500SGleb Smirnoff tmp = (struct sockaddr_in6 ){ 397e0b40500SGleb Smirnoff .sin6_family = AF_INET6, 398e0b40500SGleb Smirnoff .sin6_len = sizeof(struct sockaddr_in6), 399e0b40500SGleb Smirnoff }; 400e0b40500SGleb Smirnoff INP_RLOCK(inp); 401e0b40500SGleb Smirnoff bcopy(&inp->in6p_faddr, &tmp.sin6_addr, 402e0b40500SGleb Smirnoff sizeof(struct in6_addr)); 403e0b40500SGleb Smirnoff INP_RUNLOCK(inp); 404e0b40500SGleb Smirnoff dstsock = &tmp; 405e0b40500SGleb Smirnoff } else { 406e0b40500SGleb Smirnoff if (nam == NULL) 407e0b40500SGleb Smirnoff error = ENOTCONN; 408e0b40500SGleb Smirnoff else if (nam->sa_family != AF_INET6) 409e0b40500SGleb Smirnoff error = EAFNOSUPPORT; 410e0b40500SGleb Smirnoff else if (nam->sa_len != sizeof(struct sockaddr_in6)) 411e0b40500SGleb Smirnoff error = EINVAL; 412e0b40500SGleb Smirnoff else 413e0b40500SGleb Smirnoff error = 0; 414e0b40500SGleb Smirnoff if (error != 0) 415e0b40500SGleb Smirnoff goto release; 416e0b40500SGleb Smirnoff dstsock = (struct sockaddr_in6 *)nam; 417e0b40500SGleb Smirnoff if (dstsock->sin6_family != AF_INET6) { 418e0b40500SGleb Smirnoff error = EAFNOSUPPORT; 419e0b40500SGleb Smirnoff goto release; 420e0b40500SGleb Smirnoff } 421e0b40500SGleb Smirnoff } 422e0b40500SGleb Smirnoff 4230ecd976eSBjoern A. Zeeb INP_WLOCK(inp); 42482cd038dSYoshinobu Inoue 42575bab8b8SKip Macy if (control != NULL) { 4262290dfb4SRyan Stone NET_EPOCH_ENTER(et); 427*61f7427fSGleb Smirnoff error = ip6_setpktopts(control, &opt, inp->in6p_outputopts, 428*61f7427fSGleb Smirnoff so->so_cred, inp->inp_ip_p); 4292290dfb4SRyan Stone NET_EPOCH_EXIT(et); 4302290dfb4SRyan Stone 4312290dfb4SRyan Stone if (error != 0) { 43282cd038dSYoshinobu Inoue goto bad; 433f95d4633SHajimu UMEMOTO } 434e07db7aaSHajimu UMEMOTO optp = &opt; 435e07db7aaSHajimu UMEMOTO } else 4360ecd976eSBjoern A. Zeeb optp = inp->in6p_outputopts; 43782cd038dSYoshinobu Inoue 43882cd038dSYoshinobu Inoue /* 439a1f7e5f8SHajimu UMEMOTO * Check and convert scope zone ID into internal form. 4400ae76120SRobert Watson * 441a1f7e5f8SHajimu UMEMOTO * XXX: we may still need to determine the zone later. 442a1f7e5f8SHajimu UMEMOTO */ 443a1f7e5f8SHajimu UMEMOTO if (!(so->so_state & SS_ISCONNECTED)) { 444365ccde0SHajimu UMEMOTO if (!optp || !optp->ip6po_pktinfo || 445365ccde0SHajimu UMEMOTO !optp->ip6po_pktinfo->ipi6_ifindex) 446388288b2SHajimu UMEMOTO use_defzone = V_ip6_use_defzone; 447388288b2SHajimu UMEMOTO if (dstsock->sin6_scope_id == 0 && !use_defzone) 448a1f7e5f8SHajimu UMEMOTO scope_ambiguous = 1; 449388288b2SHajimu UMEMOTO if ((error = sa6_embedscope(dstsock, use_defzone)) != 0) 450a1f7e5f8SHajimu UMEMOTO goto bad; 451a1f7e5f8SHajimu UMEMOTO } 452a1f7e5f8SHajimu UMEMOTO 453a1f7e5f8SHajimu UMEMOTO /* 4540ae76120SRobert Watson * For an ICMPv6 packet, we should know its type and code to update 4550ae76120SRobert Watson * statistics. 45682cd038dSYoshinobu Inoue */ 457*61f7427fSGleb Smirnoff if (inp->inp_ip_p == IPPROTO_ICMPV6) { 45882cd038dSYoshinobu Inoue struct icmp6_hdr *icmp6; 45982cd038dSYoshinobu Inoue if (m->m_len < sizeof(struct icmp6_hdr) && 46082cd038dSYoshinobu Inoue (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) { 46182cd038dSYoshinobu Inoue error = ENOBUFS; 46282cd038dSYoshinobu Inoue goto bad; 46382cd038dSYoshinobu Inoue } 46482cd038dSYoshinobu Inoue icmp6 = mtod(m, struct icmp6_hdr *); 46582cd038dSYoshinobu Inoue type = icmp6->icmp6_type; 46682cd038dSYoshinobu Inoue code = icmp6->icmp6_code; 46782cd038dSYoshinobu Inoue } 46882cd038dSYoshinobu Inoue 469eb1b1807SGleb Smirnoff M_PREPEND(m, sizeof(*ip6), M_NOWAIT); 4708a0c4da8SRobert Watson if (m == NULL) { 4718a0c4da8SRobert Watson error = ENOBUFS; 4728a0c4da8SRobert Watson goto bad; 4738a0c4da8SRobert Watson } 47482cd038dSYoshinobu Inoue ip6 = mtod(m, struct ip6_hdr *); 47582cd038dSYoshinobu Inoue 4760c325f53SAlexander V. Chernikov #ifdef ROUTE_MPATH 4770c325f53SAlexander V. Chernikov if (CALC_FLOWID_OUTBOUND) { 4780c325f53SAlexander V. Chernikov uint32_t hash_type, hash_val; 4790c325f53SAlexander V. Chernikov 4800c325f53SAlexander V. Chernikov hash_val = fib6_calc_software_hash(&inp->in6p_laddr, 481*61f7427fSGleb Smirnoff &dstsock->sin6_addr, 0, 0, inp->inp_ip_p, &hash_type); 4820c325f53SAlexander V. Chernikov inp->inp_flowid = hash_val; 4830c325f53SAlexander V. Chernikov inp->inp_flowtype = hash_type; 4840c325f53SAlexander V. Chernikov } 4850c325f53SAlexander V. Chernikov #endif 48682cd038dSYoshinobu Inoue /* 48782cd038dSYoshinobu Inoue * Source address selection. 48882cd038dSYoshinobu Inoue */ 489605284b8SAlexander V. Chernikov NET_EPOCH_ENTER(et); 4900ecd976eSBjoern A. Zeeb error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred, 491601c0b8bSAlexander V. Chernikov scope_ambiguous, &in6a, &hlim); 492605284b8SAlexander V. Chernikov NET_EPOCH_EXIT(et); 493601c0b8bSAlexander V. Chernikov 49488d166bfSBjoern A. Zeeb if (error) 49582cd038dSYoshinobu Inoue goto bad; 4960ecd976eSBjoern A. Zeeb error = prison_check_ip6(inp->inp_cred, &in6a); 497b89e82ddSJamie Gritton if (error != 0) 498413628a7SBjoern A. Zeeb goto bad; 49988d166bfSBjoern A. Zeeb ip6->ip6_src = in6a; 500a1f7e5f8SHajimu UMEMOTO 501a1f7e5f8SHajimu UMEMOTO ip6->ip6_dst = dstsock->sin6_addr; 502a1f7e5f8SHajimu UMEMOTO 5030ae76120SRobert Watson /* 5040ae76120SRobert Watson * Fill in the rest of the IPv6 header fields. 5050ae76120SRobert Watson */ 5066a800098SYoshinobu Inoue ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 5070ecd976eSBjoern A. Zeeb (inp->inp_flow & IPV6_FLOWINFO_MASK); 5086a800098SYoshinobu Inoue ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 5096a800098SYoshinobu Inoue (IPV6_VERSION & IPV6_VERSION_MASK); 5100ae76120SRobert Watson 5110ae76120SRobert Watson /* 5120ae76120SRobert Watson * ip6_plen will be filled in ip6_output, so not fill it here. 5130ae76120SRobert Watson */ 5140ecd976eSBjoern A. Zeeb ip6->ip6_nxt = inp->inp_ip_p; 515601c0b8bSAlexander V. Chernikov ip6->ip6_hlim = hlim; 51682cd038dSYoshinobu Inoue 517*61f7427fSGleb Smirnoff if (inp->inp_ip_p == IPPROTO_ICMPV6 || inp->in6p_cksum != -1) { 51882cd038dSYoshinobu Inoue struct mbuf *n; 51982cd038dSYoshinobu Inoue int off; 52082cd038dSYoshinobu Inoue u_int16_t *p; 52182cd038dSYoshinobu Inoue 5220ae76120SRobert Watson /* Compute checksum. */ 523*61f7427fSGleb Smirnoff if (inp->inp_ip_p == IPPROTO_ICMPV6) 52482cd038dSYoshinobu Inoue off = offsetof(struct icmp6_hdr, icmp6_cksum); 52582cd038dSYoshinobu Inoue else 5260ecd976eSBjoern A. Zeeb off = inp->in6p_cksum; 527ae7c65b1SMichael Tuexen if (plen < off + 2) { 52882cd038dSYoshinobu Inoue error = EINVAL; 52982cd038dSYoshinobu Inoue goto bad; 53082cd038dSYoshinobu Inoue } 53182cd038dSYoshinobu Inoue off += sizeof(struct ip6_hdr); 53282cd038dSYoshinobu Inoue 53382cd038dSYoshinobu Inoue n = m; 53482cd038dSYoshinobu Inoue while (n && n->m_len <= off) { 53582cd038dSYoshinobu Inoue off -= n->m_len; 53682cd038dSYoshinobu Inoue n = n->m_next; 53782cd038dSYoshinobu Inoue } 53882cd038dSYoshinobu Inoue if (!n) 53982cd038dSYoshinobu Inoue goto bad; 54082cd038dSYoshinobu Inoue p = (u_int16_t *)(mtod(n, caddr_t) + off); 54182cd038dSYoshinobu Inoue *p = 0; 54282cd038dSYoshinobu Inoue *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen); 54382cd038dSYoshinobu Inoue } 54482cd038dSYoshinobu Inoue 5451db8d1f8SAna Kukec /* 5461db8d1f8SAna Kukec * Send RA/RS messages to user land for protection, before sending 5471db8d1f8SAna Kukec * them to rtadvd/rtsol. 5481db8d1f8SAna Kukec */ 5491db8d1f8SAna Kukec if ((send_sendso_input_hook != NULL) && 550*61f7427fSGleb Smirnoff inp->inp_ip_p == IPPROTO_ICMPV6) { 5511db8d1f8SAna Kukec switch (type) { 5521db8d1f8SAna Kukec case ND_ROUTER_ADVERT: 5531db8d1f8SAna Kukec case ND_ROUTER_SOLICIT: 5541db8d1f8SAna Kukec mtag = m_tag_get(PACKET_TAG_ND_OUTGOING, 5551db8d1f8SAna Kukec sizeof(unsigned short), M_NOWAIT); 5561db8d1f8SAna Kukec if (mtag == NULL) 5571db8d1f8SAna Kukec goto bad; 5581db8d1f8SAna Kukec m_tag_prepend(m, mtag); 5591db8d1f8SAna Kukec } 5601db8d1f8SAna Kukec } 5611db8d1f8SAna Kukec 562b9555453SGleb Smirnoff NET_EPOCH_ENTER(et); 5630ecd976eSBjoern A. Zeeb error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp); 564b9555453SGleb Smirnoff NET_EPOCH_EXIT(et); 565*61f7427fSGleb Smirnoff if (inp->inp_ip_p == IPPROTO_ICMPV6) { 56682cd038dSYoshinobu Inoue if (oifp) 56782cd038dSYoshinobu Inoue icmp6_ifoutstat_inc(oifp, type, code); 568e27b0c87SRobert Watson ICMP6STAT_INC(icp6s_outhist[type]); 56933841545SHajimu UMEMOTO } else 570f1d7ebfeSAndrey V. Elsukov RIP6STAT_INC(rip6s_opackets); 57182cd038dSYoshinobu Inoue 57282cd038dSYoshinobu Inoue goto freectl; 57382cd038dSYoshinobu Inoue 57482cd038dSYoshinobu Inoue bad: 57582cd038dSYoshinobu Inoue if (m) 57682cd038dSYoshinobu Inoue m_freem(m); 57782cd038dSYoshinobu Inoue 57882cd038dSYoshinobu Inoue freectl: 57975bab8b8SKip Macy if (control != NULL) { 580e07db7aaSHajimu UMEMOTO ip6_clearpktopts(&opt, -1); 58182cd038dSYoshinobu Inoue m_freem(control); 58233841545SHajimu UMEMOTO } 5830ecd976eSBjoern A. Zeeb INP_WUNLOCK(inp); 58482cd038dSYoshinobu Inoue return (error); 585e0b40500SGleb Smirnoff 586e0b40500SGleb Smirnoff release: 587e0b40500SGleb Smirnoff if (control != NULL) 588e0b40500SGleb Smirnoff m_freem(control); 589e0b40500SGleb Smirnoff m_freem(m); 590e0b40500SGleb Smirnoff return (error); 59182cd038dSYoshinobu Inoue } 59282cd038dSYoshinobu Inoue 59382cd038dSYoshinobu Inoue /* 59482cd038dSYoshinobu Inoue * Raw IPv6 socket option processing. 59582cd038dSYoshinobu Inoue */ 59682cd038dSYoshinobu Inoue int 5971272577eSXin LI rip6_ctloutput(struct socket *so, struct sockopt *sopt) 59882cd038dSYoshinobu Inoue { 599ee799639SBjoern A. Zeeb struct inpcb *inp; 60082cd038dSYoshinobu Inoue int error; 60182cd038dSYoshinobu Inoue 60282cd038dSYoshinobu Inoue if (sopt->sopt_level == IPPROTO_ICMPV6) 60382cd038dSYoshinobu Inoue /* 60482cd038dSYoshinobu Inoue * XXX: is it better to call icmp6_ctloutput() directly 60582cd038dSYoshinobu Inoue * from protosw? 60682cd038dSYoshinobu Inoue */ 60782cd038dSYoshinobu Inoue return (icmp6_ctloutput(so, sopt)); 608ee799639SBjoern A. Zeeb else if (sopt->sopt_level != IPPROTO_IPV6) { 609ee799639SBjoern A. Zeeb if (sopt->sopt_level == SOL_SOCKET && 610ee799639SBjoern A. Zeeb sopt->sopt_name == SO_SETFIB) { 611ee799639SBjoern A. Zeeb inp = sotoinpcb(so); 612ee799639SBjoern A. Zeeb INP_WLOCK(inp); 613ee799639SBjoern A. Zeeb inp->inp_inc.inc_fibnum = so->so_fibnum; 614ee799639SBjoern A. Zeeb INP_WUNLOCK(inp); 615ee799639SBjoern A. Zeeb return (0); 616ee799639SBjoern A. Zeeb } 61782cd038dSYoshinobu Inoue return (EINVAL); 618ee799639SBjoern A. Zeeb } 61982cd038dSYoshinobu Inoue 62082cd038dSYoshinobu Inoue error = 0; 62182cd038dSYoshinobu Inoue 62282cd038dSYoshinobu Inoue switch (sopt->sopt_dir) { 62382cd038dSYoshinobu Inoue case SOPT_GET: 62482cd038dSYoshinobu Inoue switch (sopt->sopt_name) { 62591ec0a1eSYoshinobu Inoue case MRT6_INIT: 62691ec0a1eSYoshinobu Inoue case MRT6_DONE: 62791ec0a1eSYoshinobu Inoue case MRT6_ADD_MIF: 62891ec0a1eSYoshinobu Inoue case MRT6_DEL_MIF: 62991ec0a1eSYoshinobu Inoue case MRT6_ADD_MFC: 63091ec0a1eSYoshinobu Inoue case MRT6_DEL_MFC: 63191ec0a1eSYoshinobu Inoue case MRT6_PIM: 6326be2e366SBruce M Simpson error = ip6_mrouter_get ? ip6_mrouter_get(so, sopt) : 6336be2e366SBruce M Simpson EOPNOTSUPP; 63491ec0a1eSYoshinobu Inoue break; 63502b9a206SHajimu UMEMOTO case IPV6_CHECKSUM: 63602b9a206SHajimu UMEMOTO error = ip6_raw_ctloutput(so, sopt); 63702b9a206SHajimu UMEMOTO break; 63882cd038dSYoshinobu Inoue default: 63982cd038dSYoshinobu Inoue error = ip6_ctloutput(so, sopt); 64082cd038dSYoshinobu Inoue break; 64182cd038dSYoshinobu Inoue } 64282cd038dSYoshinobu Inoue break; 64382cd038dSYoshinobu Inoue 64482cd038dSYoshinobu Inoue case SOPT_SET: 64582cd038dSYoshinobu Inoue switch (sopt->sopt_name) { 64691ec0a1eSYoshinobu Inoue case MRT6_INIT: 64791ec0a1eSYoshinobu Inoue case MRT6_DONE: 64891ec0a1eSYoshinobu Inoue case MRT6_ADD_MIF: 64991ec0a1eSYoshinobu Inoue case MRT6_DEL_MIF: 65091ec0a1eSYoshinobu Inoue case MRT6_ADD_MFC: 65191ec0a1eSYoshinobu Inoue case MRT6_DEL_MFC: 65291ec0a1eSYoshinobu Inoue case MRT6_PIM: 6536be2e366SBruce M Simpson error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) : 6546be2e366SBruce M Simpson EOPNOTSUPP; 65591ec0a1eSYoshinobu Inoue break; 65602b9a206SHajimu UMEMOTO case IPV6_CHECKSUM: 65702b9a206SHajimu UMEMOTO error = ip6_raw_ctloutput(so, sopt); 65802b9a206SHajimu UMEMOTO break; 65982cd038dSYoshinobu Inoue default: 66082cd038dSYoshinobu Inoue error = ip6_ctloutput(so, sopt); 66182cd038dSYoshinobu Inoue break; 66282cd038dSYoshinobu Inoue } 66382cd038dSYoshinobu Inoue break; 66482cd038dSYoshinobu Inoue } 66582cd038dSYoshinobu Inoue 66682cd038dSYoshinobu Inoue return (error); 66782cd038dSYoshinobu Inoue } 66882cd038dSYoshinobu Inoue 66982cd038dSYoshinobu Inoue static int 670b40ce416SJulian Elischer rip6_attach(struct socket *so, int proto, struct thread *td) 67182cd038dSYoshinobu Inoue { 67282cd038dSYoshinobu Inoue struct inpcb *inp; 6738a9d54dfSSam Leffler struct icmp6_filter *filter; 674ff7425ceSRobert Watson int error; 67582cd038dSYoshinobu Inoue 67682cd038dSYoshinobu Inoue inp = sotoinpcb(so); 67714ba8addSRobert Watson KASSERT(inp == NULL, ("rip6_attach: inp != NULL")); 6780ae76120SRobert Watson 67979ba3952SBjoern A. Zeeb error = priv_check(td, PRIV_NETINET_RAW); 68079ba3952SBjoern A. Zeeb if (error) 6810ae76120SRobert Watson return (error); 682*61f7427fSGleb Smirnoff if (proto >= IPPROTO_MAX || proto < 0) 683*61f7427fSGleb Smirnoff return (EPROTONOSUPPORT); 68482cd038dSYoshinobu Inoue error = soreserve(so, rip_sendspace, rip_recvspace); 68514ba8addSRobert Watson if (error) 6860ae76120SRobert Watson return (error); 6871ede983cSDag-Erling Smørgrav filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT); 68814ba8addSRobert Watson if (filter == NULL) 6890ae76120SRobert Watson return (ENOMEM); 690603724d3SBjoern A. Zeeb error = in_pcballoc(so, &V_ripcbinfo); 69107385abdSRobert Watson if (error) { 6921ede983cSDag-Erling Smørgrav free(filter, M_PCB); 6930ae76120SRobert Watson return (error); 69407385abdSRobert Watson } 69582cd038dSYoshinobu Inoue inp = (struct inpcb *)so->so_pcb; 696*61f7427fSGleb Smirnoff inp->inp_ip_p = proto; 69782cd038dSYoshinobu Inoue inp->in6p_cksum = -1; 6988a9d54dfSSam Leffler inp->in6p_icmp6filt = filter; 69982cd038dSYoshinobu Inoue ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt); 7008501a69cSRobert Watson INP_WUNLOCK(inp); 7010ae76120SRobert Watson return (0); 70282cd038dSYoshinobu Inoue } 70382cd038dSYoshinobu Inoue 704bc725eafSRobert Watson static void 70582cd038dSYoshinobu Inoue rip6_detach(struct socket *so) 70682cd038dSYoshinobu Inoue { 70782cd038dSYoshinobu Inoue struct inpcb *inp; 70882cd038dSYoshinobu Inoue 70982cd038dSYoshinobu Inoue inp = sotoinpcb(so); 71014ba8addSRobert Watson KASSERT(inp != NULL, ("rip6_detach: inp == NULL")); 711a152f8a3SRobert Watson 71233cde130SBruce M Simpson if (so == V_ip6_mrouter && ip6_mrouter_done) 713e7e3ecb6SYoshinobu Inoue ip6_mrouter_done(); 7146be2e366SBruce M Simpson /* xxx: RSVP */ 7158501a69cSRobert Watson INP_WLOCK(inp); 7161ede983cSDag-Erling Smørgrav free(inp->in6p_icmp6filt, M_PCB); 7170206cdb8SBjoern A. Zeeb in_pcbdetach(inp); 7186aee2fc5SBjoern A. Zeeb in_pcbfree(inp); 71982cd038dSYoshinobu Inoue } 72082cd038dSYoshinobu Inoue 721a152f8a3SRobert Watson /* XXXRW: This can't ever be called. */ 722ac45e92fSRobert Watson static void 72382cd038dSYoshinobu Inoue rip6_abort(struct socket *so) 72482cd038dSYoshinobu Inoue { 725a98bb75fSJohn Baldwin struct inpcb *inp __diagused; 726a152f8a3SRobert Watson 727a152f8a3SRobert Watson inp = sotoinpcb(so); 728a152f8a3SRobert Watson KASSERT(inp != NULL, ("rip6_abort: inp == NULL")); 729a152f8a3SRobert Watson 73082cd038dSYoshinobu Inoue soisdisconnected(so); 731a152f8a3SRobert Watson } 732a152f8a3SRobert Watson 733a152f8a3SRobert Watson static void 734a152f8a3SRobert Watson rip6_close(struct socket *so) 735a152f8a3SRobert Watson { 736a98bb75fSJohn Baldwin struct inpcb *inp __diagused; 737a152f8a3SRobert Watson 738a152f8a3SRobert Watson inp = sotoinpcb(so); 739a152f8a3SRobert Watson KASSERT(inp != NULL, ("rip6_close: inp == NULL")); 740a152f8a3SRobert Watson 741a152f8a3SRobert Watson soisdisconnected(so); 74282cd038dSYoshinobu Inoue } 74382cd038dSYoshinobu Inoue 74482cd038dSYoshinobu Inoue static int 74582cd038dSYoshinobu Inoue rip6_disconnect(struct socket *so) 74682cd038dSYoshinobu Inoue { 7470ae76120SRobert Watson struct inpcb *inp; 7480ae76120SRobert Watson 7490ae76120SRobert Watson inp = sotoinpcb(so); 7500ae76120SRobert Watson KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL")); 75182cd038dSYoshinobu Inoue 7524cc20ab1SSeigo Tanimura if ((so->so_state & SS_ISCONNECTED) == 0) 7530ae76120SRobert Watson return (ENOTCONN); 75482cd038dSYoshinobu Inoue inp->in6p_faddr = in6addr_any; 755ac45e92fSRobert Watson rip6_abort(so); 75614ba8addSRobert Watson return (0); 75782cd038dSYoshinobu Inoue } 75882cd038dSYoshinobu Inoue 75982cd038dSYoshinobu Inoue static int 760b40ce416SJulian Elischer rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 76182cd038dSYoshinobu Inoue { 7623af7f97cSGleb Smirnoff struct epoch_tracker et; 7630ae76120SRobert Watson struct inpcb *inp; 76482cd038dSYoshinobu Inoue struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 7658c0fec80SRobert Watson struct ifaddr *ifa = NULL; 766a1f7e5f8SHajimu UMEMOTO int error = 0; 76782cd038dSYoshinobu Inoue 7680ae76120SRobert Watson inp = sotoinpcb(so); 76914ba8addSRobert Watson KASSERT(inp != NULL, ("rip6_bind: inp == NULL")); 7700ae76120SRobert Watson 771f161d294SMark Johnston if (nam->sa_family != AF_INET6) 772f161d294SMark Johnston return (EAFNOSUPPORT); 77382cd038dSYoshinobu Inoue if (nam->sa_len != sizeof(*addr)) 7740ae76120SRobert Watson return (EINVAL); 775b89e82ddSJamie Gritton if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0) 776b89e82ddSJamie Gritton return (error); 7774f6c66ccSMatt Macy if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6) 7780ae76120SRobert Watson return (EADDRNOTAVAIL); 779603724d3SBjoern A. Zeeb if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 780a1f7e5f8SHajimu UMEMOTO return (error); 781a1f7e5f8SHajimu UMEMOTO 7823af7f97cSGleb Smirnoff NET_EPOCH_ENTER(et); 78382cd038dSYoshinobu Inoue if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) && 7843af7f97cSGleb Smirnoff (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) { 7853af7f97cSGleb Smirnoff NET_EPOCH_EXIT(et); 7860ae76120SRobert Watson return (EADDRNOTAVAIL); 7873af7f97cSGleb Smirnoff } 7888c0fec80SRobert Watson if (ifa != NULL && 7898c0fec80SRobert Watson ((struct in6_ifaddr *)ifa)->ia6_flags & 79082cd038dSYoshinobu Inoue (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| 7913af7f97cSGleb Smirnoff IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { 7923af7f97cSGleb Smirnoff NET_EPOCH_EXIT(et); 79382cd038dSYoshinobu Inoue return (EADDRNOTAVAIL); 7943af7f97cSGleb Smirnoff } 7953af7f97cSGleb Smirnoff NET_EPOCH_EXIT(et); 7968501a69cSRobert Watson INP_WLOCK(inp); 797a14465e1SMark Johnston INP_INFO_WLOCK(&V_ripcbinfo); 79882cd038dSYoshinobu Inoue inp->in6p_laddr = addr->sin6_addr; 799603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_ripcbinfo); 800a14465e1SMark Johnston INP_WUNLOCK(inp); 8010ae76120SRobert Watson return (0); 80282cd038dSYoshinobu Inoue } 80382cd038dSYoshinobu Inoue 80482cd038dSYoshinobu Inoue static int 805b40ce416SJulian Elischer rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 80682cd038dSYoshinobu Inoue { 8070ae76120SRobert Watson struct inpcb *inp; 80882cd038dSYoshinobu Inoue struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam; 80988d166bfSBjoern A. Zeeb struct in6_addr in6a; 810605284b8SAlexander V. Chernikov struct epoch_tracker et; 811a1f7e5f8SHajimu UMEMOTO int error = 0, scope_ambiguous = 0; 81282cd038dSYoshinobu Inoue 8130ae76120SRobert Watson inp = sotoinpcb(so); 81414ba8addSRobert Watson KASSERT(inp != NULL, ("rip6_connect: inp == NULL")); 8150ae76120SRobert Watson 81682cd038dSYoshinobu Inoue if (nam->sa_len != sizeof(*addr)) 8170ae76120SRobert Watson return (EINVAL); 8184f6c66ccSMatt Macy if (CK_STAILQ_EMPTY(&V_ifnet)) 8190ae76120SRobert Watson return (EADDRNOTAVAIL); 82082cd038dSYoshinobu Inoue if (addr->sin6_family != AF_INET6) 8210ae76120SRobert Watson return (EAFNOSUPPORT); 822a1f7e5f8SHajimu UMEMOTO 823a1f7e5f8SHajimu UMEMOTO /* 8240ae76120SRobert Watson * Application should provide a proper zone ID or the use of default 8250ae76120SRobert Watson * zone IDs should be enabled. Unfortunately, some applications do 8260ae76120SRobert Watson * not behave as it should, so we need a workaround. Even if an 8270ae76120SRobert Watson * appropriate ID is not determined, we'll see if we can determine 8280ae76120SRobert Watson * the outgoing interface. If we can, determine the zone ID based on 8290ae76120SRobert Watson * the interface below. 830a1f7e5f8SHajimu UMEMOTO */ 831603724d3SBjoern A. Zeeb if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone) 832a1f7e5f8SHajimu UMEMOTO scope_ambiguous = 1; 833603724d3SBjoern A. Zeeb if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0) 834a1f7e5f8SHajimu UMEMOTO return (error); 835a1f7e5f8SHajimu UMEMOTO 8368501a69cSRobert Watson INP_WLOCK(inp); 83771a1539eSMateusz Guzik INP_INFO_WLOCK(&V_ripcbinfo); 83882cd038dSYoshinobu Inoue /* Source address selection. XXX: need pcblookup? */ 839605284b8SAlexander V. Chernikov NET_EPOCH_ENTER(et); 840601c0b8bSAlexander V. Chernikov error = in6_selectsrc_socket(addr, inp->in6p_outputopts, 841601c0b8bSAlexander V. Chernikov inp, so->so_cred, scope_ambiguous, &in6a, NULL); 842605284b8SAlexander V. Chernikov NET_EPOCH_EXIT(et); 84388d166bfSBjoern A. Zeeb if (error) { 844603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_ripcbinfo); 84571a1539eSMateusz Guzik INP_WUNLOCK(inp); 84688d166bfSBjoern A. Zeeb return (error); 84707385abdSRobert Watson } 848a1f7e5f8SHajimu UMEMOTO 84982cd038dSYoshinobu Inoue inp->in6p_faddr = addr->sin6_addr; 85088d166bfSBjoern A. Zeeb inp->in6p_laddr = in6a; 85182cd038dSYoshinobu Inoue soisconnected(so); 852603724d3SBjoern A. Zeeb INP_INFO_WUNLOCK(&V_ripcbinfo); 85371a1539eSMateusz Guzik INP_WUNLOCK(inp); 8540ae76120SRobert Watson return (0); 85582cd038dSYoshinobu Inoue } 85682cd038dSYoshinobu Inoue 85782cd038dSYoshinobu Inoue static int 85882cd038dSYoshinobu Inoue rip6_shutdown(struct socket *so) 85982cd038dSYoshinobu Inoue { 86007385abdSRobert Watson struct inpcb *inp; 86107385abdSRobert Watson 86207385abdSRobert Watson inp = sotoinpcb(so); 86314ba8addSRobert Watson KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL")); 8640ae76120SRobert Watson 8658501a69cSRobert Watson INP_WLOCK(inp); 86682cd038dSYoshinobu Inoue socantsendmore(so); 8678501a69cSRobert Watson INP_WUNLOCK(inp); 8680ae76120SRobert Watson return (0); 86982cd038dSYoshinobu Inoue } 87082cd038dSYoshinobu Inoue 871e7d02be1SGleb Smirnoff struct protosw rip6_protosw = { 872*61f7427fSGleb Smirnoff .pr_type = SOCK_RAW, 873*61f7427fSGleb Smirnoff .pr_flags = PR_ATOMIC|PR_ADDR, 874*61f7427fSGleb Smirnoff .pr_ctloutput = rip6_ctloutput, 875*61f7427fSGleb Smirnoff .pr_abort = rip6_abort, 876*61f7427fSGleb Smirnoff .pr_attach = rip6_attach, 877*61f7427fSGleb Smirnoff .pr_bind = rip6_bind, 878*61f7427fSGleb Smirnoff .pr_connect = rip6_connect, 879*61f7427fSGleb Smirnoff .pr_control = in6_control, 880*61f7427fSGleb Smirnoff .pr_detach = rip6_detach, 881*61f7427fSGleb Smirnoff .pr_disconnect = rip6_disconnect, 882*61f7427fSGleb Smirnoff .pr_peeraddr = in6_getpeeraddr, 883*61f7427fSGleb Smirnoff .pr_send = rip6_send, 884*61f7427fSGleb Smirnoff .pr_shutdown = rip6_shutdown, 885*61f7427fSGleb Smirnoff .pr_sockaddr = in6_getsockaddr, 886*61f7427fSGleb Smirnoff .pr_close = rip6_close 88782cd038dSYoshinobu Inoue }; 888