1c398230bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1982, 1986, 1988, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 14df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 15df8bae1dSRodney W. Grimes * without specific prior written permission. 16df8bae1dSRodney W. Grimes * 17df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27df8bae1dSRodney W. Grimes * SUCH DAMAGE. 28df8bae1dSRodney W. Grimes * 2925f26ad8SGarrett Wollman * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 30c3aac50fSPeter Wemm * $FreeBSD$ 31df8bae1dSRodney W. Grimes */ 32df8bae1dSRodney W. Grimes 336a800098SYoshinobu Inoue #include "opt_inet6.h" 346a800098SYoshinobu Inoue #include "opt_ipsec.h" 354ea889c6SRobert Watson #include "opt_mac.h" 366a800098SYoshinobu Inoue 37df8bae1dSRodney W. Grimes #include <sys/param.h> 385a59cefcSBosko Milekic #include <sys/jail.h> 39117bcae7SGarrett Wollman #include <sys/kernel.h> 40960ed29cSSeigo Tanimura #include <sys/lock.h> 41df8bae1dSRodney W. Grimes #include <sys/malloc.h> 42df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 434787fd37SPaul Saab #include <sys/proc.h> 44df8bae1dSRodney W. Grimes #include <sys/protosw.h> 45960ed29cSSeigo Tanimura #include <sys/signalvar.h> 46117bcae7SGarrett Wollman #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48960ed29cSSeigo Tanimura #include <sys/sx.h> 49117bcae7SGarrett Wollman #include <sys/sysctl.h> 50960ed29cSSeigo Tanimura #include <sys/systm.h> 518781d8e9SBruce Evans 5269c2d429SJeff Roberson #include <vm/uma.h> 53df8bae1dSRodney W. Grimes 54df8bae1dSRodney W. Grimes #include <net/if.h> 55df8bae1dSRodney W. Grimes #include <net/route.h> 56df8bae1dSRodney W. Grimes 57df8bae1dSRodney W. Grimes #include <netinet/in.h> 58df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 59c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h> 60c1f8a6ceSDavid Greenman #include <netinet/in_var.h> 61960ed29cSSeigo Tanimura #include <netinet/ip.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 63df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h> 64df8bae1dSRodney W. Grimes 65100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h> 66db69a05dSPaul Saab #include <netinet/ip_dummynet.h> 67100ba1a6SJordan K. Hubbard 68b9234fafSSam Leffler #ifdef FAST_IPSEC 69b9234fafSSam Leffler #include <netipsec/ipsec.h> 70b9234fafSSam Leffler #endif /*FAST_IPSEC*/ 71b9234fafSSam Leffler 726a800098SYoshinobu Inoue #ifdef IPSEC 736a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 746a800098SYoshinobu Inoue #endif /*IPSEC*/ 756a800098SYoshinobu Inoue 76aed55708SRobert Watson #include <security/mac/mac_framework.h> 77aed55708SRobert Watson 7882cd038dSYoshinobu Inoue struct inpcbhead ripcb; 7982cd038dSYoshinobu Inoue struct inpcbinfo ripcbinfo; 80df8bae1dSRodney W. Grimes 81db69a05dSPaul Saab /* control hooks for ipfw and dummynet */ 829b932e9eSAndre Oppermann ip_fw_ctl_t *ip_fw_ctl_ptr = NULL; 839b932e9eSAndre Oppermann ip_dn_ctl_t *ip_dn_ctl_ptr = NULL; 84db69a05dSPaul Saab 85df8bae1dSRodney W. Grimes /* 86bbb4330bSLuigi Rizzo * hooks for multicast routing. They all default to NULL, 87bbb4330bSLuigi Rizzo * so leave them not initialized and rely on BSS being set to 0. 88bbb4330bSLuigi Rizzo */ 89bbb4330bSLuigi Rizzo 90bbb4330bSLuigi Rizzo /* The socket used to communicate with the multicast routing daemon. */ 91bbb4330bSLuigi Rizzo struct socket *ip_mrouter; 92bbb4330bSLuigi Rizzo 93bbb4330bSLuigi Rizzo /* The various mrouter and rsvp functions */ 94bbb4330bSLuigi Rizzo int (*ip_mrouter_set)(struct socket *, struct sockopt *); 95bbb4330bSLuigi Rizzo int (*ip_mrouter_get)(struct socket *, struct sockopt *); 96bbb4330bSLuigi Rizzo int (*ip_mrouter_done)(void); 97bbb4330bSLuigi Rizzo int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 98bbb4330bSLuigi Rizzo struct ip_moptions *); 99bbb4330bSLuigi Rizzo int (*mrt_ioctl)(int, caddr_t); 100bbb4330bSLuigi Rizzo int (*legal_vif_num)(int); 101bbb4330bSLuigi Rizzo u_long (*ip_mcast_src)(int); 102bbb4330bSLuigi Rizzo 103bbb4330bSLuigi Rizzo void (*rsvp_input_p)(struct mbuf *m, int off); 104bbb4330bSLuigi Rizzo int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 105bbb4330bSLuigi Rizzo void (*ip_rsvp_force_done)(struct socket *); 106bbb4330bSLuigi Rizzo 107bbb4330bSLuigi Rizzo /* 108df8bae1dSRodney W. Grimes * Nominal space allocated to a raw ip socket. 109df8bae1dSRodney W. Grimes */ 110df8bae1dSRodney W. Grimes #define RIPSNDQ 8192 111df8bae1dSRodney W. Grimes #define RIPRCVQ 8192 112df8bae1dSRodney W. Grimes 113df8bae1dSRodney W. Grimes /* 114df8bae1dSRodney W. Grimes * Raw interface to IP protocol. 115df8bae1dSRodney W. Grimes */ 116df8bae1dSRodney W. Grimes 117df8bae1dSRodney W. Grimes /* 118032dcc76SLuigi Rizzo * Initialize raw connection block q. 119df8bae1dSRodney W. Grimes */ 1204f590175SPaul Saab static void 1214f590175SPaul Saab rip_zone_change(void *tag) 1224f590175SPaul Saab { 1234f590175SPaul Saab 1244f590175SPaul Saab uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 1254f590175SPaul Saab } 1264f590175SPaul Saab 127d915b280SStephan Uphoff static int 128d915b280SStephan Uphoff rip_inpcb_init(void *mem, int size, int flags) 129d915b280SStephan Uphoff { 130d915b280SStephan Uphoff struct inpcb *inp = (struct inpcb *) mem; 131d915b280SStephan Uphoff INP_LOCK_INIT(inp, "inp", "rawinp"); 132d915b280SStephan Uphoff return (0); 133d915b280SStephan Uphoff } 134d915b280SStephan Uphoff 135df8bae1dSRodney W. Grimes void 136032dcc76SLuigi Rizzo rip_init() 137df8bae1dSRodney W. Grimes { 1387a9378e7SJeffrey Hsu INP_INFO_LOCK_INIT(&ripcbinfo, "rip"); 13915bd2b43SDavid Greenman LIST_INIT(&ripcb); 14015bd2b43SDavid Greenman ripcbinfo.listhead = &ripcb; 14115bd2b43SDavid Greenman /* 14215bd2b43SDavid Greenman * XXX We don't use the hash list for raw IP, but it's easier 14315bd2b43SDavid Greenman * to allocate a one entry hash list than it is to check all 14415bd2b43SDavid Greenman * over the place for hashbase == NULL. 14515bd2b43SDavid Greenman */ 146ddd79a97SDavid Greenman ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 147c3229e05SDavid Greenman ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 14869c2d429SJeff Roberson ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb), 149d915b280SStephan Uphoff NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 15069c2d429SJeff Roberson uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 1514f590175SPaul Saab EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, 1524f590175SPaul Saab NULL, EVENTHANDLER_PRI_ANY); 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes 155f6d24a78SPoul-Henning Kamp static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 156df8bae1dSRodney W. Grimes 1573b6dd5a9SSam Leffler static int 1583b6dd5a9SSam Leffler raw_append(struct inpcb *last, struct ip *ip, struct mbuf *n) 1593b6dd5a9SSam Leffler { 1604ea889c6SRobert Watson int policyfail = 0; 16133841545SHajimu UMEMOTO 162cbe42d48SRobert Watson INP_LOCK_ASSERT(last); 163cbe42d48SRobert Watson 164da0f4099SHajimu UMEMOTO #if defined(IPSEC) || defined(FAST_IPSEC) 165da0f4099SHajimu UMEMOTO /* check AH/ESP integrity. */ 166da0f4099SHajimu UMEMOTO if (ipsec4_in_reject(n, last)) { 167da0f4099SHajimu UMEMOTO policyfail = 1; 168cd6c2a88SSeigo Tanimura #ifdef IPSEC 16933841545SHajimu UMEMOTO ipsecstat.in_polvio++; 17033841545SHajimu UMEMOTO #endif /*IPSEC*/ 171b9234fafSSam Leffler /* do not inject data to pcb */ 172b9234fafSSam Leffler } 173da0f4099SHajimu UMEMOTO #endif /*IPSEC || FAST_IPSEC*/ 1744ea889c6SRobert Watson #ifdef MAC 175a557af22SRobert Watson if (!policyfail && mac_check_inpcb_deliver(last, n) != 0) 1764ea889c6SRobert Watson policyfail = 1; 1774ea889c6SRobert Watson #endif 178936cd18dSAndre Oppermann /* Check the minimum TTL for socket. */ 179936cd18dSAndre Oppermann if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl) 180936cd18dSAndre Oppermann policyfail = 1; 1813b6dd5a9SSam Leffler if (!policyfail) { 1823b6dd5a9SSam Leffler struct mbuf *opts = NULL; 1831e4d7da7SRobert Watson struct socket *so; 1843b6dd5a9SSam Leffler 1851e4d7da7SRobert Watson so = last->inp_socket; 1863b6dd5a9SSam Leffler if ((last->inp_flags & INP_CONTROLOPTS) || 1871fd7af26SAndre Oppermann (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 18882c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, n); 1891e4d7da7SRobert Watson SOCKBUF_LOCK(&so->so_rcv); 1901e4d7da7SRobert Watson if (sbappendaddr_locked(&so->so_rcv, 1913b6dd5a9SSam Leffler (struct sockaddr *)&ripsrc, n, opts) == 0) { 192df8bae1dSRodney W. Grimes /* should notify about lost packet */ 193df8bae1dSRodney W. Grimes m_freem(n); 19482c23ebaSBill Fenner if (opts) 19582c23ebaSBill Fenner m_freem(opts); 1961e4d7da7SRobert Watson SOCKBUF_UNLOCK(&so->so_rcv); 1974cc20ab1SSeigo Tanimura } else 1981e4d7da7SRobert Watson sorwakeup_locked(so); 1993b6dd5a9SSam Leffler } else 2003b6dd5a9SSam Leffler m_freem(n); 2013b6dd5a9SSam Leffler return policyfail; 202df8bae1dSRodney W. Grimes } 2033b6dd5a9SSam Leffler 2043b6dd5a9SSam Leffler /* 2053b6dd5a9SSam Leffler * Setup generic address and protocol structures 2063b6dd5a9SSam Leffler * for raw_input routine, then pass them along with 2073b6dd5a9SSam Leffler * mbuf chain. 2083b6dd5a9SSam Leffler */ 2093b6dd5a9SSam Leffler void 2103b6dd5a9SSam Leffler rip_input(struct mbuf *m, int off) 2113b6dd5a9SSam Leffler { 2123b6dd5a9SSam Leffler struct ip *ip = mtod(m, struct ip *); 2133b6dd5a9SSam Leffler int proto = ip->ip_p; 2143b6dd5a9SSam Leffler struct inpcb *inp, *last; 2153b6dd5a9SSam Leffler 2163b6dd5a9SSam Leffler INP_INFO_RLOCK(&ripcbinfo); 2173b6dd5a9SSam Leffler ripsrc.sin_addr = ip->ip_src; 2183b6dd5a9SSam Leffler last = NULL; 2193b6dd5a9SSam Leffler LIST_FOREACH(inp, &ripcb, inp_list) { 2203b6dd5a9SSam Leffler INP_LOCK(inp); 2213b6dd5a9SSam Leffler if (inp->inp_ip_p && inp->inp_ip_p != proto) { 2223b6dd5a9SSam Leffler docontinue: 2233b6dd5a9SSam Leffler INP_UNLOCK(inp); 2243b6dd5a9SSam Leffler continue; 2253b6dd5a9SSam Leffler } 2263b6dd5a9SSam Leffler #ifdef INET6 2273b6dd5a9SSam Leffler if ((inp->inp_vflag & INP_IPV4) == 0) 2283b6dd5a9SSam Leffler goto docontinue; 2293b6dd5a9SSam Leffler #endif 2303b6dd5a9SSam Leffler if (inp->inp_laddr.s_addr && 2313b6dd5a9SSam Leffler inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 2323b6dd5a9SSam Leffler goto docontinue; 2333b6dd5a9SSam Leffler if (inp->inp_faddr.s_addr && 2343b6dd5a9SSam Leffler inp->inp_faddr.s_addr != ip->ip_src.s_addr) 2353b6dd5a9SSam Leffler goto docontinue; 2365a59cefcSBosko Milekic if (jailed(inp->inp_socket->so_cred)) 2371a0c4873SMaxim Konovalov if (htonl(prison_getip(inp->inp_socket->so_cred)) != 2381a0c4873SMaxim Konovalov ip->ip_dst.s_addr) 2395a59cefcSBosko Milekic goto docontinue; 2403b6dd5a9SSam Leffler if (last) { 2413b6dd5a9SSam Leffler struct mbuf *n; 2423b6dd5a9SSam Leffler 2433b6dd5a9SSam Leffler n = m_copy(m, 0, (int)M_COPYALL); 2443b6dd5a9SSam Leffler if (n != NULL) 2453b6dd5a9SSam Leffler (void) raw_append(last, ip, n); 2463b6dd5a9SSam Leffler /* XXX count dropped packet */ 2473b6dd5a9SSam Leffler INP_UNLOCK(last); 248df8bae1dSRodney W. Grimes } 24982c23ebaSBill Fenner last = inp; 250df8bae1dSRodney W. Grimes } 2513b6dd5a9SSam Leffler if (last != NULL) { 2523b6dd5a9SSam Leffler if (raw_append(last, ip, m) != 0) 25333841545SHajimu UMEMOTO ipstat.ips_delivered--; 2543b6dd5a9SSam Leffler INP_UNLOCK(last); 255df8bae1dSRodney W. Grimes } else { 256df8bae1dSRodney W. Grimes m_freem(m); 257df8bae1dSRodney W. Grimes ipstat.ips_noproto++; 258df8bae1dSRodney W. Grimes ipstat.ips_delivered--; 259df8bae1dSRodney W. Grimes } 2603b6dd5a9SSam Leffler INP_INFO_RUNLOCK(&ripcbinfo); 261df8bae1dSRodney W. Grimes } 262df8bae1dSRodney W. Grimes 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * Generate IP header and pass packet to ip_output. 265df8bae1dSRodney W. Grimes * Tack on options user may have setup with control call. 266df8bae1dSRodney W. Grimes */ 267df8bae1dSRodney W. Grimes int 2683b6dd5a9SSam Leffler rip_output(struct mbuf *m, struct socket *so, u_long dst) 269df8bae1dSRodney W. Grimes { 2703b6dd5a9SSam Leffler struct ip *ip; 271ac830b58SBosko Milekic int error; 2723b6dd5a9SSam Leffler struct inpcb *inp = sotoinpcb(so); 273b5d47ff5SJohn-Mark Gurney int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 274b5d47ff5SJohn-Mark Gurney IP_ALLOWBROADCAST; 275df8bae1dSRodney W. Grimes 276df8bae1dSRodney W. Grimes /* 277df8bae1dSRodney W. Grimes * If the user handed us a complete IP packet, use it. 278df8bae1dSRodney W. Grimes * Otherwise, allocate an mbuf for a header and fill it in. 279df8bae1dSRodney W. Grimes */ 280df8bae1dSRodney W. Grimes if ((inp->inp_flags & INP_HDRINCL) == 0) { 281430d30d8SBill Fenner if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 282430d30d8SBill Fenner m_freem(m); 283430d30d8SBill Fenner return(EMSGSIZE); 284430d30d8SBill Fenner } 2852d01d331SRobert Watson M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 2866b48911bSRobert Watson if (m == NULL) 2876b48911bSRobert Watson return(ENOBUFS); 288ac830b58SBosko Milekic 289ac830b58SBosko Milekic INP_LOCK(inp); 290df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 2918ce3f3ddSRuslan Ermilov ip->ip_tos = inp->inp_ip_tos; 292b2828ad2SAndre Oppermann if (inp->inp_flags & INP_DONTFRAG) 293b2828ad2SAndre Oppermann ip->ip_off = IP_DF; 294b2828ad2SAndre Oppermann else 295df8bae1dSRodney W. Grimes ip->ip_off = 0; 296ca98b82cSDavid Greenman ip->ip_p = inp->inp_ip_p; 297df8bae1dSRodney W. Grimes ip->ip_len = m->m_pkthdr.len; 2985a59cefcSBosko Milekic if (jailed(inp->inp_socket->so_cred)) 2995a59cefcSBosko Milekic ip->ip_src.s_addr = 3005a59cefcSBosko Milekic htonl(prison_getip(inp->inp_socket->so_cred)); 3015a59cefcSBosko Milekic else 302df8bae1dSRodney W. Grimes ip->ip_src = inp->inp_laddr; 303df8bae1dSRodney W. Grimes ip->ip_dst.s_addr = dst; 3048ce3f3ddSRuslan Ermilov ip->ip_ttl = inp->inp_ip_ttl; 305df8bae1dSRodney W. Grimes } else { 306430d30d8SBill Fenner if (m->m_pkthdr.len > IP_MAXPACKET) { 307430d30d8SBill Fenner m_freem(m); 308430d30d8SBill Fenner return(EMSGSIZE); 309430d30d8SBill Fenner } 310ac830b58SBosko Milekic INP_LOCK(inp); 311df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 3125a59cefcSBosko Milekic if (jailed(inp->inp_socket->so_cred)) { 3135a59cefcSBosko Milekic if (ip->ip_src.s_addr != 3145a59cefcSBosko Milekic htonl(prison_getip(inp->inp_socket->so_cred))) { 315ac830b58SBosko Milekic INP_UNLOCK(inp); 3165a59cefcSBosko Milekic m_freem(m); 3175a59cefcSBosko Milekic return (EPERM); 3185a59cefcSBosko Milekic } 3195a59cefcSBosko Milekic } 320072b9b24SPaul Traina /* don't allow both user specified and setsockopt options, 321072b9b24SPaul Traina and don't allow packet length sizes that will crash */ 32253be11f6SPoul-Henning Kamp if (((ip->ip_hl != (sizeof (*ip) >> 2)) 3235e2d0696SGarrett Wollman && inp->inp_options) 32491108995SBill Fenner || (ip->ip_len > m->m_pkthdr.len) 32553be11f6SPoul-Henning Kamp || (ip->ip_len < (ip->ip_hl << 2))) { 326ac830b58SBosko Milekic INP_UNLOCK(inp); 327072b9b24SPaul Traina m_freem(m); 328072b9b24SPaul Traina return EINVAL; 329072b9b24SPaul Traina } 330df8bae1dSRodney W. Grimes if (ip->ip_id == 0) 3311f44b0a1SDavid Malone ip->ip_id = ip_newid(); 332df8bae1dSRodney W. Grimes /* XXX prevent ip_output from overwriting header fields */ 333df8bae1dSRodney W. Grimes flags |= IP_RAWOUTPUT; 334df8bae1dSRodney W. Grimes ipstat.ips_rawout++; 335df8bae1dSRodney W. Grimes } 3366a800098SYoshinobu Inoue 3376fbfd582SAndre Oppermann if (inp->inp_flags & INP_ONESBCAST) 3388afa2304SBruce M Simpson flags |= IP_SENDONES; 3398afa2304SBruce M Simpson 340ac830b58SBosko Milekic #ifdef MAC 341ac830b58SBosko Milekic mac_create_mbuf_from_inpcb(inp, m); 342ac830b58SBosko Milekic #endif 343ac830b58SBosko Milekic 344ac830b58SBosko Milekic error = ip_output(m, inp->inp_options, NULL, flags, 345ac830b58SBosko Milekic inp->inp_moptions, inp); 346ac830b58SBosko Milekic INP_UNLOCK(inp); 347ac830b58SBosko Milekic return error; 348df8bae1dSRodney W. Grimes } 349df8bae1dSRodney W. Grimes 350df8bae1dSRodney W. Grimes /* 351df8bae1dSRodney W. Grimes * Raw IP socket option processing. 35283503a92SRobert Watson * 3536c67b8b6SRobert Watson * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 3546c67b8b6SRobert Watson * only be created by a privileged process, and as such, socket option 3556c67b8b6SRobert Watson * operations to manage system properties on any raw socket were allowed to 3566c67b8b6SRobert Watson * take place without explicit additional access control checks. However, 3576c67b8b6SRobert Watson * raw sockets can now also be created in jail(), and therefore explicit 3586c67b8b6SRobert Watson * checks are now required. Likewise, raw sockets can be used by a process 3596c67b8b6SRobert Watson * after it gives up privilege, so some caution is required. For options 3606c67b8b6SRobert Watson * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 3616c67b8b6SRobert Watson * performed in ip_ctloutput() and therefore no check occurs here. 3626c67b8b6SRobert Watson * Unilaterally checking suser() here breaks normal IP socket option 3636c67b8b6SRobert Watson * operations on raw sockets. 3646c67b8b6SRobert Watson * 3656c67b8b6SRobert Watson * When adding new socket options here, make sure to add access control 3666c67b8b6SRobert Watson * checks here as necessary. 367df8bae1dSRodney W. Grimes */ 368df8bae1dSRodney W. Grimes int 3693b6dd5a9SSam Leffler rip_ctloutput(struct socket *so, struct sockopt *sopt) 370df8bae1dSRodney W. Grimes { 371cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 372cfe8b629SGarrett Wollman int error, optval; 373df8bae1dSRodney W. Grimes 374cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) 375df8bae1dSRodney W. Grimes return (EINVAL); 376df8bae1dSRodney W. Grimes 37725f26ad8SGarrett Wollman error = 0; 378cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 379cfe8b629SGarrett Wollman case SOPT_GET: 380cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 381cfe8b629SGarrett Wollman case IP_HDRINCL: 382cfe8b629SGarrett Wollman optval = inp->inp_flags & INP_HDRINCL; 383cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 384cfe8b629SGarrett Wollman break; 385df8bae1dSRodney W. Grimes 3867b109fa4SLuigi Rizzo case IP_FW_ADD: /* ADD actually returns the body... */ 38709bb5f75SPoul-Henning Kamp case IP_FW_GET: 388cd8b5ae0SRuslan Ermilov case IP_FW_TABLE_GETSIZE: 389cd8b5ae0SRuslan Ermilov case IP_FW_TABLE_LIST: 3906c67b8b6SRobert Watson error = suser(curthread); 3916c67b8b6SRobert Watson if (error != 0) 3926c67b8b6SRobert Watson return (error); 3939b932e9eSAndre Oppermann if (ip_fw_ctl_ptr != NULL) 394cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 3957b109fa4SLuigi Rizzo else 3967b109fa4SLuigi Rizzo error = ENOPROTOOPT; 397cfe8b629SGarrett Wollman break; 3984dd1662bSUgen J.S. Antsilevich 399b715f178SLuigi Rizzo case IP_DUMMYNET_GET: 4006c67b8b6SRobert Watson error = suser(curthread); 4016c67b8b6SRobert Watson if (error != 0) 4026c67b8b6SRobert Watson return (error); 4039b932e9eSAndre Oppermann if (ip_dn_ctl_ptr != NULL) 404b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 4057b109fa4SLuigi Rizzo else 4067b109fa4SLuigi Rizzo error = ENOPROTOOPT; 407b715f178SLuigi Rizzo break ; 4081c5de19aSGarrett Wollman 4091c5de19aSGarrett Wollman case MRT_INIT: 4101c5de19aSGarrett Wollman case MRT_DONE: 4111c5de19aSGarrett Wollman case MRT_ADD_VIF: 4121c5de19aSGarrett Wollman case MRT_DEL_VIF: 4131c5de19aSGarrett Wollman case MRT_ADD_MFC: 4141c5de19aSGarrett Wollman case MRT_DEL_MFC: 4151c5de19aSGarrett Wollman case MRT_VERSION: 4161c5de19aSGarrett Wollman case MRT_ASSERT: 4171e78ac21SJeffrey Hsu case MRT_API_SUPPORT: 4181e78ac21SJeffrey Hsu case MRT_API_CONFIG: 4191e78ac21SJeffrey Hsu case MRT_ADD_BW_UPCALL: 4201e78ac21SJeffrey Hsu case MRT_DEL_BW_UPCALL: 4216c67b8b6SRobert Watson error = suser(curthread); 4226c67b8b6SRobert Watson if (error != 0) 4236c67b8b6SRobert Watson return (error); 424bbb4330bSLuigi Rizzo error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 425bbb4330bSLuigi Rizzo EOPNOTSUPP; 426cfe8b629SGarrett Wollman break; 427cfe8b629SGarrett Wollman 428cfe8b629SGarrett Wollman default: 429cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 430cfe8b629SGarrett Wollman break; 431df8bae1dSRodney W. Grimes } 432cfe8b629SGarrett Wollman break; 433cfe8b629SGarrett Wollman 434cfe8b629SGarrett Wollman case SOPT_SET: 435cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 436cfe8b629SGarrett Wollman case IP_HDRINCL: 437cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 438cfe8b629SGarrett Wollman sizeof optval); 439cfe8b629SGarrett Wollman if (error) 440cfe8b629SGarrett Wollman break; 441cfe8b629SGarrett Wollman if (optval) 442cfe8b629SGarrett Wollman inp->inp_flags |= INP_HDRINCL; 443cfe8b629SGarrett Wollman else 444cfe8b629SGarrett Wollman inp->inp_flags &= ~INP_HDRINCL; 445cfe8b629SGarrett Wollman break; 446cfe8b629SGarrett Wollman 4478ba03966SRuslan Ermilov case IP_FW_ADD: 448cfe8b629SGarrett Wollman case IP_FW_DEL: 449cfe8b629SGarrett Wollman case IP_FW_FLUSH: 450cfe8b629SGarrett Wollman case IP_FW_ZERO: 4510b6c1a83SBrian Feldman case IP_FW_RESETLOG: 452cd8b5ae0SRuslan Ermilov case IP_FW_TABLE_ADD: 453cd8b5ae0SRuslan Ermilov case IP_FW_TABLE_DEL: 454cd8b5ae0SRuslan Ermilov case IP_FW_TABLE_FLUSH: 4556c67b8b6SRobert Watson error = suser(curthread); 4566c67b8b6SRobert Watson if (error != 0) 4576c67b8b6SRobert Watson return (error); 4589b932e9eSAndre Oppermann if (ip_fw_ctl_ptr != NULL) 459cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 4607b109fa4SLuigi Rizzo else 4617b109fa4SLuigi Rizzo error = ENOPROTOOPT; 462cfe8b629SGarrett Wollman break; 463cfe8b629SGarrett Wollman 464b715f178SLuigi Rizzo case IP_DUMMYNET_CONFIGURE: 465b715f178SLuigi Rizzo case IP_DUMMYNET_DEL: 466b715f178SLuigi Rizzo case IP_DUMMYNET_FLUSH: 4676c67b8b6SRobert Watson error = suser(curthread); 4686c67b8b6SRobert Watson if (error != 0) 4696c67b8b6SRobert Watson return (error); 4709b932e9eSAndre Oppermann if (ip_dn_ctl_ptr != NULL) 471b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 4727b109fa4SLuigi Rizzo else 4737b109fa4SLuigi Rizzo error = ENOPROTOOPT ; 474b715f178SLuigi Rizzo break ; 475cfe8b629SGarrett Wollman 476cfe8b629SGarrett Wollman case IP_RSVP_ON: 4776c67b8b6SRobert Watson error = suser(curthread); 4786c67b8b6SRobert Watson if (error != 0) 4796c67b8b6SRobert Watson return (error); 480cfe8b629SGarrett Wollman error = ip_rsvp_init(so); 481cfe8b629SGarrett Wollman break; 482cfe8b629SGarrett Wollman 483cfe8b629SGarrett Wollman case IP_RSVP_OFF: 4846c67b8b6SRobert Watson error = suser(curthread); 4856c67b8b6SRobert Watson if (error != 0) 4866c67b8b6SRobert Watson return (error); 487cfe8b629SGarrett Wollman error = ip_rsvp_done(); 488cfe8b629SGarrett Wollman break; 489cfe8b629SGarrett Wollman 490cfe8b629SGarrett Wollman case IP_RSVP_VIF_ON: 491cfe8b629SGarrett Wollman case IP_RSVP_VIF_OFF: 4926c67b8b6SRobert Watson error = suser(curthread); 4936c67b8b6SRobert Watson if (error != 0) 4946c67b8b6SRobert Watson return (error); 495bbb4330bSLuigi Rizzo error = ip_rsvp_vif ? 496bbb4330bSLuigi Rizzo ip_rsvp_vif(so, sopt) : EINVAL; 497cfe8b629SGarrett Wollman break; 498cfe8b629SGarrett Wollman 499cfe8b629SGarrett Wollman case MRT_INIT: 500cfe8b629SGarrett Wollman case MRT_DONE: 501cfe8b629SGarrett Wollman case MRT_ADD_VIF: 502cfe8b629SGarrett Wollman case MRT_DEL_VIF: 503cfe8b629SGarrett Wollman case MRT_ADD_MFC: 504cfe8b629SGarrett Wollman case MRT_DEL_MFC: 505cfe8b629SGarrett Wollman case MRT_VERSION: 506cfe8b629SGarrett Wollman case MRT_ASSERT: 5071e78ac21SJeffrey Hsu case MRT_API_SUPPORT: 5081e78ac21SJeffrey Hsu case MRT_API_CONFIG: 5091e78ac21SJeffrey Hsu case MRT_ADD_BW_UPCALL: 5101e78ac21SJeffrey Hsu case MRT_DEL_BW_UPCALL: 5116c67b8b6SRobert Watson error = suser(curthread); 5126c67b8b6SRobert Watson if (error != 0) 5136c67b8b6SRobert Watson return (error); 514bbb4330bSLuigi Rizzo error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 515bbb4330bSLuigi Rizzo EOPNOTSUPP; 516cfe8b629SGarrett Wollman break; 517cfe8b629SGarrett Wollman 518cfe8b629SGarrett Wollman default: 519cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 520cfe8b629SGarrett Wollman break; 521cfe8b629SGarrett Wollman } 522cfe8b629SGarrett Wollman break; 523cfe8b629SGarrett Wollman } 524cfe8b629SGarrett Wollman 525cfe8b629SGarrett Wollman return (error); 526df8bae1dSRodney W. Grimes } 527df8bae1dSRodney W. Grimes 52839191c8eSGarrett Wollman /* 52939191c8eSGarrett Wollman * This function exists solely to receive the PRC_IFDOWN messages which 53039191c8eSGarrett Wollman * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 53139191c8eSGarrett Wollman * and calls in_ifadown() to remove all routes corresponding to that address. 53239191c8eSGarrett Wollman * It also receives the PRC_IFUP messages from if_up() and reinstalls the 53339191c8eSGarrett Wollman * interface routes. 53439191c8eSGarrett Wollman */ 53539191c8eSGarrett Wollman void 5363b6dd5a9SSam Leffler rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 53739191c8eSGarrett Wollman { 53839191c8eSGarrett Wollman struct in_ifaddr *ia; 53939191c8eSGarrett Wollman struct ifnet *ifp; 54039191c8eSGarrett Wollman int err; 54139191c8eSGarrett Wollman int flags; 54239191c8eSGarrett Wollman 54339191c8eSGarrett Wollman switch (cmd) { 54439191c8eSGarrett Wollman case PRC_IFDOWN: 545462b86feSPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 54639191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa 54739191c8eSGarrett Wollman && (ia->ia_flags & IFA_ROUTE)) { 54839191c8eSGarrett Wollman /* 54939191c8eSGarrett Wollman * in_ifscrub kills the interface route. 55039191c8eSGarrett Wollman */ 55139191c8eSGarrett Wollman in_ifscrub(ia->ia_ifp, ia); 55239191c8eSGarrett Wollman /* 55339191c8eSGarrett Wollman * in_ifadown gets rid of all the rest of 55439191c8eSGarrett Wollman * the routes. This is not quite the right 55539191c8eSGarrett Wollman * thing to do, but at least if we are running 55639191c8eSGarrett Wollman * a routing process they will come back. 55739191c8eSGarrett Wollman */ 55891854268SRuslan Ermilov in_ifadown(&ia->ia_ifa, 0); 55939191c8eSGarrett Wollman break; 56039191c8eSGarrett Wollman } 56139191c8eSGarrett Wollman } 56239191c8eSGarrett Wollman break; 56339191c8eSGarrett Wollman 56439191c8eSGarrett Wollman case PRC_IFUP: 565462b86feSPoul-Henning Kamp TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 56639191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa) 56739191c8eSGarrett Wollman break; 56839191c8eSGarrett Wollman } 56939191c8eSGarrett Wollman if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 57039191c8eSGarrett Wollman return; 57139191c8eSGarrett Wollman flags = RTF_UP; 57239191c8eSGarrett Wollman ifp = ia->ia_ifa.ifa_ifp; 57339191c8eSGarrett Wollman 57439191c8eSGarrett Wollman if ((ifp->if_flags & IFF_LOOPBACK) 57539191c8eSGarrett Wollman || (ifp->if_flags & IFF_POINTOPOINT)) 57639191c8eSGarrett Wollman flags |= RTF_HOST; 57739191c8eSGarrett Wollman 57839191c8eSGarrett Wollman err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 57939191c8eSGarrett Wollman if (err == 0) 58039191c8eSGarrett Wollman ia->ia_flags |= IFA_ROUTE; 58139191c8eSGarrett Wollman break; 58239191c8eSGarrett Wollman } 58339191c8eSGarrett Wollman } 58439191c8eSGarrett Wollman 58582cd038dSYoshinobu Inoue u_long rip_sendspace = RIPSNDQ; 58682cd038dSYoshinobu Inoue u_long rip_recvspace = RIPRCVQ; 587df8bae1dSRodney W. Grimes 588e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 5893d177f46SBill Fumerola &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 590e59898ffSMaxime Henrion SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 5910ca2861fSRuslan Ermilov &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 592117bcae7SGarrett Wollman 593117bcae7SGarrett Wollman static int 594b40ce416SJulian Elischer rip_attach(struct socket *so, int proto, struct thread *td) 595df8bae1dSRodney W. Grimes { 596117bcae7SGarrett Wollman struct inpcb *inp; 5973b6dd5a9SSam Leffler int error; 598c1f8a6ceSDavid Greenman 599117bcae7SGarrett Wollman inp = sotoinpcb(so); 60014ba8addSRobert Watson KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 60114ba8addSRobert Watson if (jailed(td->td_ucred) && !jail_allow_raw_sockets) 6025a59cefcSBosko Milekic return (EPERM); 60314ba8addSRobert Watson if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) != 0) 604a29f300eSGarrett Wollman return error; 60514ba8addSRobert Watson if (proto >= IPPROTO_MAX || proto < 0) 6064d3ffc98SBill Fenner return EPROTONOSUPPORT; 6076a800098SYoshinobu Inoue error = soreserve(so, rip_sendspace, rip_recvspace); 60814ba8addSRobert Watson if (error) 6096a800098SYoshinobu Inoue return error; 61014ba8addSRobert Watson INP_INFO_WLOCK(&ripcbinfo); 611d915b280SStephan Uphoff error = in_pcballoc(so, &ripcbinfo); 6123b6dd5a9SSam Leffler if (error) { 6133b6dd5a9SSam Leffler INP_INFO_WUNLOCK(&ripcbinfo); 61486b3ebceSDavid Greenman return error; 6153b6dd5a9SSam Leffler } 616df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 6173b6dd5a9SSam Leffler INP_INFO_WUNLOCK(&ripcbinfo); 6186a800098SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 619ca98b82cSDavid Greenman inp->inp_ip_p = proto; 6208ce3f3ddSRuslan Ermilov inp->inp_ip_ttl = ip_defttl; 6213b6dd5a9SSam Leffler INP_UNLOCK(inp); 622117bcae7SGarrett Wollman return 0; 623df8bae1dSRodney W. Grimes } 624117bcae7SGarrett Wollman 62550d7c061SSam Leffler static void 626a152f8a3SRobert Watson rip_detach(struct socket *so) 62750d7c061SSam Leffler { 628a152f8a3SRobert Watson struct inpcb *inp; 6293ca1570cSRobert Watson 630a152f8a3SRobert Watson inp = sotoinpcb(so); 631a152f8a3SRobert Watson KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 632a152f8a3SRobert Watson KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 633a152f8a3SRobert Watson ("rip_detach: not closed")); 63450d7c061SSam Leffler 635a152f8a3SRobert Watson INP_INFO_WLOCK(&ripcbinfo); 636a152f8a3SRobert Watson INP_LOCK(inp); 63750d7c061SSam Leffler if (so == ip_mrouter && ip_mrouter_done) 63850d7c061SSam Leffler ip_mrouter_done(); 63950d7c061SSam Leffler if (ip_rsvp_force_done) 64050d7c061SSam Leffler ip_rsvp_force_done(so); 64150d7c061SSam Leffler if (so == ip_rsvpd) 64250d7c061SSam Leffler ip_rsvp_done(); 64350d7c061SSam Leffler in_pcbdetach(inp); 64414ba8addSRobert Watson in_pcbfree(inp); 645a152f8a3SRobert Watson INP_INFO_WUNLOCK(&ripcbinfo); 64650d7c061SSam Leffler } 64750d7c061SSam Leffler 648bc725eafSRobert Watson static void 649a152f8a3SRobert Watson rip_dodisconnect(struct socket *so, struct inpcb *inp) 650117bcae7SGarrett Wollman { 651117bcae7SGarrett Wollman 652a152f8a3SRobert Watson INP_LOCK_ASSERT(inp); 653a152f8a3SRobert Watson 654a152f8a3SRobert Watson inp->inp_faddr.s_addr = INADDR_ANY; 655a152f8a3SRobert Watson SOCK_LOCK(so); 656a152f8a3SRobert Watson so->so_state &= ~SS_ISCONNECTED; 657a152f8a3SRobert Watson SOCK_UNLOCK(so); 658117bcae7SGarrett Wollman } 659df8bae1dSRodney W. Grimes 660ac45e92fSRobert Watson static void 661117bcae7SGarrett Wollman rip_abort(struct socket *so) 662df8bae1dSRodney W. Grimes { 66350d7c061SSam Leffler struct inpcb *inp; 66450d7c061SSam Leffler 66550d7c061SSam Leffler inp = sotoinpcb(so); 66614ba8addSRobert Watson KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 667a152f8a3SRobert Watson 66814ba8addSRobert Watson INP_INFO_WLOCK(&ripcbinfo); 66950d7c061SSam Leffler INP_LOCK(inp); 670a152f8a3SRobert Watson rip_dodisconnect(so, inp); 671a152f8a3SRobert Watson INP_UNLOCK(inp); 672a152f8a3SRobert Watson INP_INFO_WUNLOCK(&ripcbinfo); 673a152f8a3SRobert Watson } 674a152f8a3SRobert Watson 675a152f8a3SRobert Watson static void 676a152f8a3SRobert Watson rip_close(struct socket *so) 677a152f8a3SRobert Watson { 678a152f8a3SRobert Watson struct inpcb *inp; 679a152f8a3SRobert Watson 680a152f8a3SRobert Watson inp = sotoinpcb(so); 681a152f8a3SRobert Watson KASSERT(inp != NULL, ("rip_close: inp == NULL")); 682a152f8a3SRobert Watson 683a152f8a3SRobert Watson INP_INFO_WLOCK(&ripcbinfo); 684a152f8a3SRobert Watson INP_LOCK(inp); 685a152f8a3SRobert Watson rip_dodisconnect(so, inp); 686a152f8a3SRobert Watson INP_UNLOCK(inp); 68750d7c061SSam Leffler INP_INFO_WUNLOCK(&ripcbinfo); 688117bcae7SGarrett Wollman } 689117bcae7SGarrett Wollman 690117bcae7SGarrett Wollman static int 691117bcae7SGarrett Wollman rip_disconnect(struct socket *so) 692117bcae7SGarrett Wollman { 693eb16472fSMaxim Konovalov struct inpcb *inp; 694eb16472fSMaxim Konovalov 6954cc20ab1SSeigo Tanimura if ((so->so_state & SS_ISCONNECTED) == 0) 696117bcae7SGarrett Wollman return ENOTCONN; 697eb16472fSMaxim Konovalov 698eb16472fSMaxim Konovalov inp = sotoinpcb(so); 699eb16472fSMaxim Konovalov KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 700eb16472fSMaxim Konovalov INP_INFO_WLOCK(&ripcbinfo); 701eb16472fSMaxim Konovalov INP_LOCK(inp); 702a152f8a3SRobert Watson rip_dodisconnect(so, inp); 703eb16472fSMaxim Konovalov INP_UNLOCK(inp); 704eb16472fSMaxim Konovalov INP_INFO_WUNLOCK(&ripcbinfo); 70514ba8addSRobert Watson return (0); 706117bcae7SGarrett Wollman } 707117bcae7SGarrett Wollman 708117bcae7SGarrett Wollman static int 709b40ce416SJulian Elischer rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 710117bcae7SGarrett Wollman { 71157bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 71250d7c061SSam Leffler struct inpcb *inp; 713df8bae1dSRodney W. Grimes 71457bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 715117bcae7SGarrett Wollman return EINVAL; 716117bcae7SGarrett Wollman 7175a59cefcSBosko Milekic if (jailed(td->td_ucred)) { 7185a59cefcSBosko Milekic if (addr->sin_addr.s_addr == INADDR_ANY) 7195a59cefcSBosko Milekic addr->sin_addr.s_addr = 7205a59cefcSBosko Milekic htonl(prison_getip(td->td_ucred)); 7211a0c4873SMaxim Konovalov if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr) 7225a59cefcSBosko Milekic return (EADDRNOTAVAIL); 7235a59cefcSBosko Milekic } 7245a59cefcSBosko Milekic 72550d7c061SSam Leffler if (TAILQ_EMPTY(&ifnet) || 72650d7c061SSam Leffler (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 727032dcc76SLuigi Rizzo (addr->sin_addr.s_addr && 728117bcae7SGarrett Wollman ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 729117bcae7SGarrett Wollman return EADDRNOTAVAIL; 73050d7c061SSam Leffler 73150d7c061SSam Leffler inp = sotoinpcb(so); 73214ba8addSRobert Watson KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 73314ba8addSRobert Watson INP_INFO_WLOCK(&ripcbinfo); 73450d7c061SSam Leffler INP_LOCK(inp); 735df8bae1dSRodney W. Grimes inp->inp_laddr = addr->sin_addr; 73650d7c061SSam Leffler INP_UNLOCK(inp); 73750d7c061SSam Leffler INP_INFO_WUNLOCK(&ripcbinfo); 738117bcae7SGarrett Wollman return 0; 739df8bae1dSRodney W. Grimes } 740117bcae7SGarrett Wollman 741117bcae7SGarrett Wollman static int 742b40ce416SJulian Elischer rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 743df8bae1dSRodney W. Grimes { 74457bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 74550d7c061SSam Leffler struct inpcb *inp; 746df8bae1dSRodney W. Grimes 74757bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 748117bcae7SGarrett Wollman return EINVAL; 749117bcae7SGarrett Wollman if (TAILQ_EMPTY(&ifnet)) 750117bcae7SGarrett Wollman return EADDRNOTAVAIL; 75150d7c061SSam Leffler if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 752117bcae7SGarrett Wollman return EAFNOSUPPORT; 75350d7c061SSam Leffler 75450d7c061SSam Leffler inp = sotoinpcb(so); 75514ba8addSRobert Watson KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 75614ba8addSRobert Watson INP_INFO_WLOCK(&ripcbinfo); 75750d7c061SSam Leffler INP_LOCK(inp); 758df8bae1dSRodney W. Grimes inp->inp_faddr = addr->sin_addr; 759df8bae1dSRodney W. Grimes soisconnected(so); 76050d7c061SSam Leffler INP_UNLOCK(inp); 76150d7c061SSam Leffler INP_INFO_WUNLOCK(&ripcbinfo); 762117bcae7SGarrett Wollman return 0; 763df8bae1dSRodney W. Grimes } 764df8bae1dSRodney W. Grimes 765117bcae7SGarrett Wollman static int 766117bcae7SGarrett Wollman rip_shutdown(struct socket *so) 767df8bae1dSRodney W. Grimes { 76850d7c061SSam Leffler struct inpcb *inp; 76950d7c061SSam Leffler 77050d7c061SSam Leffler inp = sotoinpcb(so); 77114ba8addSRobert Watson KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 77250d7c061SSam Leffler INP_LOCK(inp); 773117bcae7SGarrett Wollman socantsendmore(so); 77450d7c061SSam Leffler INP_UNLOCK(inp); 775117bcae7SGarrett Wollman return 0; 776117bcae7SGarrett Wollman } 777117bcae7SGarrett Wollman 778117bcae7SGarrett Wollman static int 77957bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 780b40ce416SJulian Elischer struct mbuf *control, struct thread *td) 781117bcae7SGarrett Wollman { 78250d7c061SSam Leffler struct inpcb *inp; 78350d7c061SSam Leffler u_long dst; 784df8bae1dSRodney W. Grimes 78550d7c061SSam Leffler inp = sotoinpcb(so); 78614ba8addSRobert Watson KASSERT(inp != NULL, ("rip_send: inp == NULL")); 78714ba8addSRobert Watson /* 78814ba8addSRobert Watson * Note: 'dst' reads below are unlocked. 78914ba8addSRobert Watson */ 790df8bae1dSRodney W. Grimes if (so->so_state & SS_ISCONNECTED) { 791df8bae1dSRodney W. Grimes if (nam) { 792117bcae7SGarrett Wollman m_freem(m); 793117bcae7SGarrett Wollman return EISCONN; 794df8bae1dSRodney W. Grimes } 79514ba8addSRobert Watson dst = inp->inp_faddr.s_addr; /* Unlocked read. */ 796df8bae1dSRodney W. Grimes } else { 797df8bae1dSRodney W. Grimes if (nam == NULL) { 798117bcae7SGarrett Wollman m_freem(m); 799117bcae7SGarrett Wollman return ENOTCONN; 800df8bae1dSRodney W. Grimes } 80157bf258eSGarrett Wollman dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 802df8bae1dSRodney W. Grimes } 80314ba8addSRobert Watson return rip_output(m, so, dst); 804df8bae1dSRodney W. Grimes } 805df8bae1dSRodney W. Grimes 80698271db4SGarrett Wollman static int 80782d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS) 80898271db4SGarrett Wollman { 8093b6dd5a9SSam Leffler int error, i, n; 81098271db4SGarrett Wollman struct inpcb *inp, **inp_list; 81198271db4SGarrett Wollman inp_gen_t gencnt; 81298271db4SGarrett Wollman struct xinpgen xig; 81398271db4SGarrett Wollman 81498271db4SGarrett Wollman /* 81598271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 81698271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 81798271db4SGarrett Wollman */ 81898271db4SGarrett Wollman if (req->oldptr == 0) { 81998271db4SGarrett Wollman n = ripcbinfo.ipi_count; 82098271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 82198271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 82298271db4SGarrett Wollman return 0; 82398271db4SGarrett Wollman } 82498271db4SGarrett Wollman 82598271db4SGarrett Wollman if (req->newptr != 0) 82698271db4SGarrett Wollman return EPERM; 82798271db4SGarrett Wollman 82898271db4SGarrett Wollman /* 82998271db4SGarrett Wollman * OK, now we're committed to doing something. 83098271db4SGarrett Wollman */ 8313b6dd5a9SSam Leffler INP_INFO_RLOCK(&ripcbinfo); 83298271db4SGarrett Wollman gencnt = ripcbinfo.ipi_gencnt; 83398271db4SGarrett Wollman n = ripcbinfo.ipi_count; 8343b6dd5a9SSam Leffler INP_INFO_RUNLOCK(&ripcbinfo); 83598271db4SGarrett Wollman 83698271db4SGarrett Wollman xig.xig_len = sizeof xig; 83798271db4SGarrett Wollman xig.xig_count = n; 83898271db4SGarrett Wollman xig.xig_gen = gencnt; 83998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 84098271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 84198271db4SGarrett Wollman if (error) 84298271db4SGarrett Wollman return error; 84398271db4SGarrett Wollman 844a163d034SWarner Losh inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 84598271db4SGarrett Wollman if (inp_list == 0) 84698271db4SGarrett Wollman return ENOMEM; 84798271db4SGarrett Wollman 8483b6dd5a9SSam Leffler INP_INFO_RLOCK(&ripcbinfo); 849fc2ffbe6SPoul-Henning Kamp for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 850fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 8513b6dd5a9SSam Leffler INP_LOCK(inp); 852f34f3a70SSam Leffler if (inp->inp_gencnt <= gencnt && 853f34f3a70SSam Leffler cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) { 8543b6dd5a9SSam Leffler /* XXX held references? */ 85598271db4SGarrett Wollman inp_list[i++] = inp; 85698271db4SGarrett Wollman } 8573b6dd5a9SSam Leffler INP_UNLOCK(inp); 8584787fd37SPaul Saab } 8593b6dd5a9SSam Leffler INP_INFO_RUNLOCK(&ripcbinfo); 86098271db4SGarrett Wollman n = i; 86198271db4SGarrett Wollman 86298271db4SGarrett Wollman error = 0; 86398271db4SGarrett Wollman for (i = 0; i < n; i++) { 86498271db4SGarrett Wollman inp = inp_list[i]; 865d915b280SStephan Uphoff INP_LOCK(inp); 86698271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 86798271db4SGarrett Wollman struct xinpcb xi; 868fd94099eSColin Percival bzero(&xi, sizeof(xi)); 86998271db4SGarrett Wollman xi.xi_len = sizeof xi; 87098271db4SGarrett Wollman /* XXX should avoid extra copy */ 87198271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 87298271db4SGarrett Wollman if (inp->inp_socket) 87398271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 874d915b280SStephan Uphoff INP_UNLOCK(inp); 87598271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 876d915b280SStephan Uphoff } else 877d915b280SStephan Uphoff INP_UNLOCK(inp); 87898271db4SGarrett Wollman } 87998271db4SGarrett Wollman if (!error) { 88098271db4SGarrett Wollman /* 88198271db4SGarrett Wollman * Give the user an updated idea of our state. 88298271db4SGarrett Wollman * If the generation differs from what we told 88398271db4SGarrett Wollman * her before, she knows that something happened 88498271db4SGarrett Wollman * while we were processing this request, and it 88598271db4SGarrett Wollman * might be necessary to retry. 88698271db4SGarrett Wollman */ 8873b6dd5a9SSam Leffler INP_INFO_RLOCK(&ripcbinfo); 88898271db4SGarrett Wollman xig.xig_gen = ripcbinfo.ipi_gencnt; 88998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 89098271db4SGarrett Wollman xig.xig_count = ripcbinfo.ipi_count; 8913b6dd5a9SSam Leffler INP_INFO_RUNLOCK(&ripcbinfo); 89298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 89398271db4SGarrett Wollman } 89498271db4SGarrett Wollman free(inp_list, M_TEMP); 89598271db4SGarrett Wollman return error; 89698271db4SGarrett Wollman } 89798271db4SGarrett Wollman 898f76fcf6dSJeffrey Hsu /* 899f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setsockaddr. We just pass down 900f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 901f76fcf6dSJeffrey Hsu */ 902f76fcf6dSJeffrey Hsu static int 903f76fcf6dSJeffrey Hsu rip_sockaddr(struct socket *so, struct sockaddr **nam) 904f76fcf6dSJeffrey Hsu { 905f76fcf6dSJeffrey Hsu return (in_setsockaddr(so, nam, &ripcbinfo)); 906f76fcf6dSJeffrey Hsu } 907f76fcf6dSJeffrey Hsu 908f76fcf6dSJeffrey Hsu /* 909f76fcf6dSJeffrey Hsu * This is the wrapper function for in_setpeeraddr. We just pass down 910f76fcf6dSJeffrey Hsu * the pcbinfo for in_setpeeraddr to lock. 911f76fcf6dSJeffrey Hsu */ 912f76fcf6dSJeffrey Hsu static int 913f76fcf6dSJeffrey Hsu rip_peeraddr(struct socket *so, struct sockaddr **nam) 914f76fcf6dSJeffrey Hsu { 915f76fcf6dSJeffrey Hsu return (in_setpeeraddr(so, nam, &ripcbinfo)); 916f76fcf6dSJeffrey Hsu } 917f76fcf6dSJeffrey Hsu 918f76fcf6dSJeffrey Hsu 91998271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 92098271db4SGarrett Wollman rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 92198271db4SGarrett Wollman 922117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = { 923756d52a1SPoul-Henning Kamp .pru_abort = rip_abort, 924756d52a1SPoul-Henning Kamp .pru_attach = rip_attach, 925756d52a1SPoul-Henning Kamp .pru_bind = rip_bind, 926756d52a1SPoul-Henning Kamp .pru_connect = rip_connect, 927756d52a1SPoul-Henning Kamp .pru_control = in_control, 928756d52a1SPoul-Henning Kamp .pru_detach = rip_detach, 929756d52a1SPoul-Henning Kamp .pru_disconnect = rip_disconnect, 930756d52a1SPoul-Henning Kamp .pru_peeraddr = rip_peeraddr, 931756d52a1SPoul-Henning Kamp .pru_send = rip_send, 932756d52a1SPoul-Henning Kamp .pru_shutdown = rip_shutdown, 933756d52a1SPoul-Henning Kamp .pru_sockaddr = rip_sockaddr, 934a152f8a3SRobert Watson .pru_sosetlabel = in_pcbsosetlabel, 935a152f8a3SRobert Watson .pru_close = rip_close, 936117bcae7SGarrett Wollman }; 937