1df8bae1dSRodney W. Grimes /* 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 * 3. All advertising materials mentioning features or use of this software 14df8bae1dSRodney W. Grimes * must display the following acknowledgement: 15df8bae1dSRodney W. Grimes * This product includes software developed by the University of 16df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 17df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 18df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 19df8bae1dSRodney W. Grimes * without specific prior written permission. 20df8bae1dSRodney W. Grimes * 21df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31df8bae1dSRodney W. Grimes * SUCH DAMAGE. 32df8bae1dSRodney W. Grimes * 3325f26ad8SGarrett Wollman * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 34c3aac50fSPeter Wemm * $FreeBSD$ 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 37df8bae1dSRodney W. Grimes #include <sys/param.h> 38117bcae7SGarrett Wollman #include <sys/systm.h> 39117bcae7SGarrett Wollman #include <sys/kernel.h> 40df8bae1dSRodney W. Grimes #include <sys/malloc.h> 41df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 42a29f300eSGarrett Wollman #include <sys/proc.h> 43df8bae1dSRodney W. Grimes #include <sys/protosw.h> 44117bcae7SGarrett Wollman #include <sys/socket.h> 45df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 46117bcae7SGarrett Wollman #include <sys/sysctl.h> 478781d8e9SBruce Evans 483d4d47f3SGarrett Wollman #include <vm/vm_zone.h> 49df8bae1dSRodney W. Grimes 50df8bae1dSRodney W. Grimes #include <net/if.h> 51df8bae1dSRodney W. Grimes #include <net/route.h> 52df8bae1dSRodney W. Grimes 535e2d0696SGarrett Wollman #define _IP_VHL 54df8bae1dSRodney W. Grimes #include <netinet/in.h> 55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 56df8bae1dSRodney W. Grimes #include <netinet/ip.h> 57c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h> 58c1f8a6ceSDavid Greenman #include <netinet/in_var.h> 59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 60df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h> 61df8bae1dSRodney W. Grimes 62100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h> 63100ba1a6SJordan K. Hubbard 64b715f178SLuigi Rizzo #include "opt_ipdn.h" 65b715f178SLuigi Rizzo #ifdef DUMMYNET 66b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h> 67b715f178SLuigi Rizzo #endif 68f9493383SGarrett Wollman 6982cd038dSYoshinobu Inoue struct inpcbhead ripcb; 7082cd038dSYoshinobu Inoue struct inpcbinfo ripcbinfo; 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Nominal space allocated to a raw ip socket. 74df8bae1dSRodney W. Grimes */ 75df8bae1dSRodney W. Grimes #define RIPSNDQ 8192 76df8bae1dSRodney W. Grimes #define RIPRCVQ 8192 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * Raw interface to IP protocol. 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes /* 83df8bae1dSRodney W. Grimes * Initialize raw connection block q. 84df8bae1dSRodney W. Grimes */ 85df8bae1dSRodney W. Grimes void 86df8bae1dSRodney W. Grimes rip_init() 87df8bae1dSRodney W. Grimes { 8815bd2b43SDavid Greenman LIST_INIT(&ripcb); 8915bd2b43SDavid Greenman ripcbinfo.listhead = &ripcb; 9015bd2b43SDavid Greenman /* 9115bd2b43SDavid Greenman * XXX We don't use the hash list for raw IP, but it's easier 9215bd2b43SDavid Greenman * to allocate a one entry hash list than it is to check all 9315bd2b43SDavid Greenman * over the place for hashbase == NULL. 9415bd2b43SDavid Greenman */ 95ddd79a97SDavid Greenman ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 96c3229e05SDavid Greenman ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 973d4d47f3SGarrett Wollman ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 9898271db4SGarrett Wollman maxsockets, ZONE_INTERRUPT, 0); 99df8bae1dSRodney W. Grimes } 100df8bae1dSRodney W. Grimes 101f6d24a78SPoul-Henning Kamp static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 102df8bae1dSRodney W. Grimes /* 103df8bae1dSRodney W. Grimes * Setup generic address and protocol structures 104df8bae1dSRodney W. Grimes * for raw_input routine, then pass them along with 105df8bae1dSRodney W. Grimes * mbuf chain. 106df8bae1dSRodney W. Grimes */ 107df8bae1dSRodney W. Grimes void 108e62b8c49SBill Fenner rip_input(m, iphlen) 109df8bae1dSRodney W. Grimes struct mbuf *m; 110e62b8c49SBill Fenner int iphlen; 111df8bae1dSRodney W. Grimes { 112df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 113df8bae1dSRodney W. Grimes register struct inpcb *inp; 11482c23ebaSBill Fenner struct inpcb *last = 0; 11582c23ebaSBill Fenner struct mbuf *opts = 0; 116df8bae1dSRodney W. Grimes 117df8bae1dSRodney W. Grimes ripsrc.sin_addr = ip->ip_src; 11815bd2b43SDavid Greenman for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 119ca98b82cSDavid Greenman if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p) 120df8bae1dSRodney W. Grimes continue; 121df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr && 122d99c7a23SGarrett Wollman inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 123df8bae1dSRodney W. Grimes continue; 124df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr && 125d99c7a23SGarrett Wollman inp->inp_faddr.s_addr != ip->ip_src.s_addr) 126df8bae1dSRodney W. Grimes continue; 127df8bae1dSRodney W. Grimes if (last) { 128623ae52eSPoul-Henning Kamp struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 129623ae52eSPoul-Henning Kamp if (n) { 13082c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS || 13182c23ebaSBill Fenner last->inp_socket->so_options & SO_TIMESTAMP) 13282c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, n); 13382c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 134623ae52eSPoul-Henning Kamp (struct sockaddr *)&ripsrc, n, 13582c23ebaSBill Fenner opts) == 0) { 136df8bae1dSRodney W. Grimes /* should notify about lost packet */ 137df8bae1dSRodney W. Grimes m_freem(n); 13882c23ebaSBill Fenner if (opts) 13982c23ebaSBill Fenner m_freem(opts); 14082c23ebaSBill Fenner } else 14182c23ebaSBill Fenner sorwakeup(last->inp_socket); 14282c23ebaSBill Fenner opts = 0; 143df8bae1dSRodney W. Grimes } 144df8bae1dSRodney W. Grimes } 14582c23ebaSBill Fenner last = inp; 146df8bae1dSRodney W. Grimes } 147df8bae1dSRodney W. Grimes if (last) { 14882c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS || 14982c23ebaSBill Fenner last->inp_socket->so_options & SO_TIMESTAMP) 15082c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, m); 15182c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 15282c23ebaSBill Fenner (struct sockaddr *)&ripsrc, m, opts) == 0) { 153df8bae1dSRodney W. Grimes m_freem(m); 15482c23ebaSBill Fenner if (opts) 15582c23ebaSBill Fenner m_freem(opts); 15682c23ebaSBill Fenner } else 15782c23ebaSBill Fenner sorwakeup(last->inp_socket); 158df8bae1dSRodney W. Grimes } else { 159df8bae1dSRodney W. Grimes m_freem(m); 160df8bae1dSRodney W. Grimes ipstat.ips_noproto++; 161df8bae1dSRodney W. Grimes ipstat.ips_delivered--; 162df8bae1dSRodney W. Grimes } 163df8bae1dSRodney W. Grimes } 164df8bae1dSRodney W. Grimes 165df8bae1dSRodney W. Grimes /* 166df8bae1dSRodney W. Grimes * Generate IP header and pass packet to ip_output. 167df8bae1dSRodney W. Grimes * Tack on options user may have setup with control call. 168df8bae1dSRodney W. Grimes */ 169df8bae1dSRodney W. Grimes int 170df8bae1dSRodney W. Grimes rip_output(m, so, dst) 171d25f3712SBrian Feldman struct mbuf *m; 172df8bae1dSRodney W. Grimes struct socket *so; 173df8bae1dSRodney W. Grimes u_long dst; 174df8bae1dSRodney W. Grimes { 175df8bae1dSRodney W. Grimes register struct ip *ip; 176df8bae1dSRodney W. Grimes register struct inpcb *inp = sotoinpcb(so); 177df8bae1dSRodney W. Grimes int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 178df8bae1dSRodney W. Grimes 179df8bae1dSRodney W. Grimes /* 180df8bae1dSRodney W. Grimes * If the user handed us a complete IP packet, use it. 181df8bae1dSRodney W. Grimes * Otherwise, allocate an mbuf for a header and fill it in. 182df8bae1dSRodney W. Grimes */ 183df8bae1dSRodney W. Grimes if ((inp->inp_flags & INP_HDRINCL) == 0) { 184430d30d8SBill Fenner if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 185430d30d8SBill Fenner m_freem(m); 186430d30d8SBill Fenner return(EMSGSIZE); 187430d30d8SBill Fenner } 188df8bae1dSRodney W. Grimes M_PREPEND(m, sizeof(struct ip), M_WAIT); 189df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 190df8bae1dSRodney W. Grimes ip->ip_tos = 0; 191df8bae1dSRodney W. Grimes ip->ip_off = 0; 192ca98b82cSDavid Greenman ip->ip_p = inp->inp_ip_p; 193df8bae1dSRodney W. Grimes ip->ip_len = m->m_pkthdr.len; 194df8bae1dSRodney W. Grimes ip->ip_src = inp->inp_laddr; 195df8bae1dSRodney W. Grimes ip->ip_dst.s_addr = dst; 196df8bae1dSRodney W. Grimes ip->ip_ttl = MAXTTL; 197df8bae1dSRodney W. Grimes } else { 198430d30d8SBill Fenner if (m->m_pkthdr.len > IP_MAXPACKET) { 199430d30d8SBill Fenner m_freem(m); 200430d30d8SBill Fenner return(EMSGSIZE); 201430d30d8SBill Fenner } 202df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 203072b9b24SPaul Traina /* don't allow both user specified and setsockopt options, 204072b9b24SPaul Traina and don't allow packet length sizes that will crash */ 2055e2d0696SGarrett Wollman if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 2065e2d0696SGarrett Wollman && inp->inp_options) 20791108995SBill Fenner || (ip->ip_len > m->m_pkthdr.len) 20891108995SBill Fenner || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 209072b9b24SPaul Traina m_freem(m); 210072b9b24SPaul Traina return EINVAL; 211072b9b24SPaul Traina } 212df8bae1dSRodney W. Grimes if (ip->ip_id == 0) 213df8bae1dSRodney W. Grimes ip->ip_id = htons(ip_id++); 214df8bae1dSRodney W. Grimes /* XXX prevent ip_output from overwriting header fields */ 215df8bae1dSRodney W. Grimes flags |= IP_RAWOUTPUT; 216df8bae1dSRodney W. Grimes ipstat.ips_rawout++; 217df8bae1dSRodney W. Grimes } 218072b9b24SPaul Traina return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 219072b9b24SPaul Traina inp->inp_moptions)); 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes /* 223df8bae1dSRodney W. Grimes * Raw IP socket option processing. 224df8bae1dSRodney W. Grimes */ 225df8bae1dSRodney W. Grimes int 226cfe8b629SGarrett Wollman rip_ctloutput(so, sopt) 227df8bae1dSRodney W. Grimes struct socket *so; 228cfe8b629SGarrett Wollman struct sockopt *sopt; 229df8bae1dSRodney W. Grimes { 230cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 231cfe8b629SGarrett Wollman int error, optval; 232df8bae1dSRodney W. Grimes 233cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) 234df8bae1dSRodney W. Grimes return (EINVAL); 235df8bae1dSRodney W. Grimes 23625f26ad8SGarrett Wollman error = 0; 237cfe8b629SGarrett Wollman 238cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 239cfe8b629SGarrett Wollman case SOPT_GET: 240cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 241cfe8b629SGarrett Wollman case IP_HDRINCL: 242cfe8b629SGarrett Wollman optval = inp->inp_flags & INP_HDRINCL; 243cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 244cfe8b629SGarrett Wollman break; 245df8bae1dSRodney W. Grimes 24609bb5f75SPoul-Henning Kamp case IP_FW_GET: 247cfe8b629SGarrett Wollman if (ip_fw_ctl_ptr == 0) 248cfe8b629SGarrett Wollman error = ENOPROTOOPT; 249cfe8b629SGarrett Wollman else 250cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 251cfe8b629SGarrett Wollman break; 2524dd1662bSUgen J.S. Antsilevich 253b715f178SLuigi Rizzo #ifdef DUMMYNET 254b715f178SLuigi Rizzo case IP_DUMMYNET_GET: 255b715f178SLuigi Rizzo if (ip_dn_ctl_ptr == NULL) 256b715f178SLuigi Rizzo error = ENOPROTOOPT ; 257b715f178SLuigi Rizzo else 258b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 259b715f178SLuigi Rizzo break ; 260b715f178SLuigi Rizzo #endif /* DUMMYNET */ 2611c5de19aSGarrett Wollman 2621c5de19aSGarrett Wollman case MRT_INIT: 2631c5de19aSGarrett Wollman case MRT_DONE: 2641c5de19aSGarrett Wollman case MRT_ADD_VIF: 2651c5de19aSGarrett Wollman case MRT_DEL_VIF: 2661c5de19aSGarrett Wollman case MRT_ADD_MFC: 2671c5de19aSGarrett Wollman case MRT_DEL_MFC: 2681c5de19aSGarrett Wollman case MRT_VERSION: 2691c5de19aSGarrett Wollman case MRT_ASSERT: 270cfe8b629SGarrett Wollman error = ip_mrouter_get(so, sopt); 271cfe8b629SGarrett Wollman break; 272cfe8b629SGarrett Wollman 273cfe8b629SGarrett Wollman default: 274cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 275cfe8b629SGarrett Wollman break; 276df8bae1dSRodney W. Grimes } 277cfe8b629SGarrett Wollman break; 278cfe8b629SGarrett Wollman 279cfe8b629SGarrett Wollman case SOPT_SET: 280cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 281cfe8b629SGarrett Wollman case IP_HDRINCL: 282cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 283cfe8b629SGarrett Wollman sizeof optval); 284cfe8b629SGarrett Wollman if (error) 285cfe8b629SGarrett Wollman break; 286cfe8b629SGarrett Wollman if (optval) 287cfe8b629SGarrett Wollman inp->inp_flags |= INP_HDRINCL; 288cfe8b629SGarrett Wollman else 289cfe8b629SGarrett Wollman inp->inp_flags &= ~INP_HDRINCL; 290cfe8b629SGarrett Wollman break; 291cfe8b629SGarrett Wollman 292cfe8b629SGarrett Wollman case IP_FW_ADD: 293cfe8b629SGarrett Wollman case IP_FW_DEL: 294cfe8b629SGarrett Wollman case IP_FW_FLUSH: 295cfe8b629SGarrett Wollman case IP_FW_ZERO: 2960b6c1a83SBrian Feldman case IP_FW_RESETLOG: 297cfe8b629SGarrett Wollman if (ip_fw_ctl_ptr == 0) 298cfe8b629SGarrett Wollman error = ENOPROTOOPT; 299cfe8b629SGarrett Wollman else 300cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 301cfe8b629SGarrett Wollman break; 302cfe8b629SGarrett Wollman 303b715f178SLuigi Rizzo #ifdef DUMMYNET 304b715f178SLuigi Rizzo case IP_DUMMYNET_CONFIGURE: 305b715f178SLuigi Rizzo case IP_DUMMYNET_DEL: 306b715f178SLuigi Rizzo case IP_DUMMYNET_FLUSH: 307b715f178SLuigi Rizzo if (ip_dn_ctl_ptr == NULL) 308b715f178SLuigi Rizzo error = ENOPROTOOPT ; 309b715f178SLuigi Rizzo else 310b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 311b715f178SLuigi Rizzo break ; 312b715f178SLuigi Rizzo #endif 313cfe8b629SGarrett Wollman 314cfe8b629SGarrett Wollman case IP_RSVP_ON: 315cfe8b629SGarrett Wollman error = ip_rsvp_init(so); 316cfe8b629SGarrett Wollman break; 317cfe8b629SGarrett Wollman 318cfe8b629SGarrett Wollman case IP_RSVP_OFF: 319cfe8b629SGarrett Wollman error = ip_rsvp_done(); 320cfe8b629SGarrett Wollman break; 321cfe8b629SGarrett Wollman 322cfe8b629SGarrett Wollman /* XXX - should be combined */ 323cfe8b629SGarrett Wollman case IP_RSVP_VIF_ON: 324cfe8b629SGarrett Wollman error = ip_rsvp_vif_init(so, sopt); 325cfe8b629SGarrett Wollman break; 326cfe8b629SGarrett Wollman 327cfe8b629SGarrett Wollman case IP_RSVP_VIF_OFF: 328cfe8b629SGarrett Wollman error = ip_rsvp_vif_done(so, sopt); 329cfe8b629SGarrett Wollman break; 330cfe8b629SGarrett Wollman 331cfe8b629SGarrett Wollman case MRT_INIT: 332cfe8b629SGarrett Wollman case MRT_DONE: 333cfe8b629SGarrett Wollman case MRT_ADD_VIF: 334cfe8b629SGarrett Wollman case MRT_DEL_VIF: 335cfe8b629SGarrett Wollman case MRT_ADD_MFC: 336cfe8b629SGarrett Wollman case MRT_DEL_MFC: 337cfe8b629SGarrett Wollman case MRT_VERSION: 338cfe8b629SGarrett Wollman case MRT_ASSERT: 339cfe8b629SGarrett Wollman error = ip_mrouter_set(so, sopt); 340cfe8b629SGarrett Wollman break; 341cfe8b629SGarrett Wollman 342cfe8b629SGarrett Wollman default: 343cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 344cfe8b629SGarrett Wollman break; 345cfe8b629SGarrett Wollman } 346cfe8b629SGarrett Wollman break; 347cfe8b629SGarrett Wollman } 348cfe8b629SGarrett Wollman 349cfe8b629SGarrett Wollman return (error); 350df8bae1dSRodney W. Grimes } 351df8bae1dSRodney W. Grimes 35239191c8eSGarrett Wollman /* 35339191c8eSGarrett Wollman * This function exists solely to receive the PRC_IFDOWN messages which 35439191c8eSGarrett Wollman * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 35539191c8eSGarrett Wollman * and calls in_ifadown() to remove all routes corresponding to that address. 35639191c8eSGarrett Wollman * It also receives the PRC_IFUP messages from if_up() and reinstalls the 35739191c8eSGarrett Wollman * interface routes. 35839191c8eSGarrett Wollman */ 35939191c8eSGarrett Wollman void 36039191c8eSGarrett Wollman rip_ctlinput(cmd, sa, vip) 36139191c8eSGarrett Wollman int cmd; 36239191c8eSGarrett Wollman struct sockaddr *sa; 36339191c8eSGarrett Wollman void *vip; 36439191c8eSGarrett Wollman { 36539191c8eSGarrett Wollman struct in_ifaddr *ia; 36639191c8eSGarrett Wollman struct ifnet *ifp; 36739191c8eSGarrett Wollman int err; 36839191c8eSGarrett Wollman int flags; 36939191c8eSGarrett Wollman 37039191c8eSGarrett Wollman switch (cmd) { 37139191c8eSGarrett Wollman case PRC_IFDOWN: 37239191c8eSGarrett Wollman for (ia = in_ifaddrhead.tqh_first; ia; 37339191c8eSGarrett Wollman ia = ia->ia_link.tqe_next) { 37439191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa 37539191c8eSGarrett Wollman && (ia->ia_flags & IFA_ROUTE)) { 37639191c8eSGarrett Wollman /* 37739191c8eSGarrett Wollman * in_ifscrub kills the interface route. 37839191c8eSGarrett Wollman */ 37939191c8eSGarrett Wollman in_ifscrub(ia->ia_ifp, ia); 38039191c8eSGarrett Wollman /* 38139191c8eSGarrett Wollman * in_ifadown gets rid of all the rest of 38239191c8eSGarrett Wollman * the routes. This is not quite the right 38339191c8eSGarrett Wollman * thing to do, but at least if we are running 38439191c8eSGarrett Wollman * a routing process they will come back. 38539191c8eSGarrett Wollman */ 38639191c8eSGarrett Wollman in_ifadown(&ia->ia_ifa); 38739191c8eSGarrett Wollman break; 38839191c8eSGarrett Wollman } 38939191c8eSGarrett Wollman } 39039191c8eSGarrett Wollman break; 39139191c8eSGarrett Wollman 39239191c8eSGarrett Wollman case PRC_IFUP: 39339191c8eSGarrett Wollman for (ia = in_ifaddrhead.tqh_first; ia; 39439191c8eSGarrett Wollman ia = ia->ia_link.tqe_next) { 39539191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa) 39639191c8eSGarrett Wollman break; 39739191c8eSGarrett Wollman } 39839191c8eSGarrett Wollman if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 39939191c8eSGarrett Wollman return; 40039191c8eSGarrett Wollman flags = RTF_UP; 40139191c8eSGarrett Wollman ifp = ia->ia_ifa.ifa_ifp; 40239191c8eSGarrett Wollman 40339191c8eSGarrett Wollman if ((ifp->if_flags & IFF_LOOPBACK) 40439191c8eSGarrett Wollman || (ifp->if_flags & IFF_POINTOPOINT)) 40539191c8eSGarrett Wollman flags |= RTF_HOST; 40639191c8eSGarrett Wollman 40739191c8eSGarrett Wollman err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 40839191c8eSGarrett Wollman if (err == 0) 40939191c8eSGarrett Wollman ia->ia_flags |= IFA_ROUTE; 41039191c8eSGarrett Wollman break; 41139191c8eSGarrett Wollman } 41239191c8eSGarrett Wollman } 41339191c8eSGarrett Wollman 41482cd038dSYoshinobu Inoue u_long rip_sendspace = RIPSNDQ; 41582cd038dSYoshinobu Inoue u_long rip_recvspace = RIPRCVQ; 416df8bae1dSRodney W. Grimes 4173d177f46SBill Fumerola SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 4183d177f46SBill Fumerola &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 4193d177f46SBill Fumerola SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 4203d177f46SBill Fumerola &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 421117bcae7SGarrett Wollman 422117bcae7SGarrett Wollman static int 423a29f300eSGarrett Wollman rip_attach(struct socket *so, int proto, struct proc *p) 424df8bae1dSRodney W. Grimes { 425117bcae7SGarrett Wollman struct inpcb *inp; 42686b3ebceSDavid Greenman int error, s; 427c1f8a6ceSDavid Greenman 428117bcae7SGarrett Wollman inp = sotoinpcb(so); 429df8bae1dSRodney W. Grimes if (inp) 430df8bae1dSRodney W. Grimes panic("rip_attach"); 431f711d546SPoul-Henning Kamp if (p && (error = suser(p)) != 0) 432a29f300eSGarrett Wollman return error; 433117bcae7SGarrett Wollman 43486b3ebceSDavid Greenman s = splnet(); 43586b3ebceSDavid Greenman error = in_pcballoc(so, &ripcbinfo, p); 43686b3ebceSDavid Greenman splx(s); 43786b3ebceSDavid Greenman if (error) 43886b3ebceSDavid Greenman return error; 43986b3ebceSDavid Greenman error = soreserve(so, rip_sendspace, rip_recvspace); 44086b3ebceSDavid Greenman if (error) 441117bcae7SGarrett Wollman return error; 442df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 443ca98b82cSDavid Greenman inp->inp_ip_p = proto; 444117bcae7SGarrett Wollman return 0; 445df8bae1dSRodney W. Grimes } 446117bcae7SGarrett Wollman 447117bcae7SGarrett Wollman static int 448117bcae7SGarrett Wollman rip_detach(struct socket *so) 449117bcae7SGarrett Wollman { 450117bcae7SGarrett Wollman struct inpcb *inp; 451117bcae7SGarrett Wollman 452117bcae7SGarrett Wollman inp = sotoinpcb(so); 453df8bae1dSRodney W. Grimes if (inp == 0) 454df8bae1dSRodney W. Grimes panic("rip_detach"); 455df8bae1dSRodney W. Grimes if (so == ip_mrouter) 456df8bae1dSRodney W. Grimes ip_mrouter_done(); 457b4489dc3SGarrett Wollman ip_rsvp_force_done(so); 458838ecf42SGarrett Wollman if (so == ip_rsvpd) 459838ecf42SGarrett Wollman ip_rsvp_done(); 460df8bae1dSRodney W. Grimes in_pcbdetach(inp); 461117bcae7SGarrett Wollman return 0; 462117bcae7SGarrett Wollman } 463df8bae1dSRodney W. Grimes 464117bcae7SGarrett Wollman static int 465117bcae7SGarrett Wollman rip_abort(struct socket *so) 466df8bae1dSRodney W. Grimes { 467117bcae7SGarrett Wollman soisdisconnected(so); 468117bcae7SGarrett Wollman return rip_detach(so); 469117bcae7SGarrett Wollman } 470117bcae7SGarrett Wollman 471117bcae7SGarrett Wollman static int 472117bcae7SGarrett Wollman rip_disconnect(struct socket *so) 473117bcae7SGarrett Wollman { 474117bcae7SGarrett Wollman if ((so->so_state & SS_ISCONNECTED) == 0) 475117bcae7SGarrett Wollman return ENOTCONN; 476117bcae7SGarrett Wollman return rip_abort(so); 477117bcae7SGarrett Wollman } 478117bcae7SGarrett Wollman 479117bcae7SGarrett Wollman static int 48057bf258eSGarrett Wollman rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 481117bcae7SGarrett Wollman { 482117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 48357bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 484df8bae1dSRodney W. Grimes 48557bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 486117bcae7SGarrett Wollman return EINVAL; 487117bcae7SGarrett Wollman 488117bcae7SGarrett Wollman if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 489df8bae1dSRodney W. Grimes (addr->sin_family != AF_IMPLINK)) || 490df8bae1dSRodney W. Grimes (addr->sin_addr.s_addr && 491117bcae7SGarrett Wollman ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 492117bcae7SGarrett Wollman return EADDRNOTAVAIL; 493df8bae1dSRodney W. Grimes inp->inp_laddr = addr->sin_addr; 494117bcae7SGarrett Wollman return 0; 495df8bae1dSRodney W. Grimes } 496117bcae7SGarrett Wollman 497117bcae7SGarrett Wollman static int 49857bf258eSGarrett Wollman rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 499df8bae1dSRodney W. Grimes { 500117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 50157bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 502df8bae1dSRodney W. Grimes 50357bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 504117bcae7SGarrett Wollman return EINVAL; 505117bcae7SGarrett Wollman if (TAILQ_EMPTY(&ifnet)) 506117bcae7SGarrett Wollman return EADDRNOTAVAIL; 507df8bae1dSRodney W. Grimes if ((addr->sin_family != AF_INET) && 508117bcae7SGarrett Wollman (addr->sin_family != AF_IMPLINK)) 509117bcae7SGarrett Wollman return EAFNOSUPPORT; 510df8bae1dSRodney W. Grimes inp->inp_faddr = addr->sin_addr; 511df8bae1dSRodney W. Grimes soisconnected(so); 512117bcae7SGarrett Wollman return 0; 513df8bae1dSRodney W. Grimes } 514df8bae1dSRodney W. Grimes 515117bcae7SGarrett Wollman static int 516117bcae7SGarrett Wollman rip_shutdown(struct socket *so) 517df8bae1dSRodney W. Grimes { 518117bcae7SGarrett Wollman socantsendmore(so); 519117bcae7SGarrett Wollman return 0; 520117bcae7SGarrett Wollman } 521117bcae7SGarrett Wollman 522117bcae7SGarrett Wollman static int 52357bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 524a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 525117bcae7SGarrett Wollman { 526117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 527df8bae1dSRodney W. Grimes register u_long dst; 528df8bae1dSRodney W. Grimes 529df8bae1dSRodney W. Grimes if (so->so_state & SS_ISCONNECTED) { 530df8bae1dSRodney W. Grimes if (nam) { 531117bcae7SGarrett Wollman m_freem(m); 532117bcae7SGarrett Wollman return EISCONN; 533df8bae1dSRodney W. Grimes } 534df8bae1dSRodney W. Grimes dst = inp->inp_faddr.s_addr; 535df8bae1dSRodney W. Grimes } else { 536df8bae1dSRodney W. Grimes if (nam == NULL) { 537117bcae7SGarrett Wollman m_freem(m); 538117bcae7SGarrett Wollman return ENOTCONN; 539df8bae1dSRodney W. Grimes } 54057bf258eSGarrett Wollman dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 541df8bae1dSRodney W. Grimes } 542117bcae7SGarrett Wollman return rip_output(m, so, dst); 543df8bae1dSRodney W. Grimes } 544df8bae1dSRodney W. Grimes 54598271db4SGarrett Wollman static int 54698271db4SGarrett Wollman rip_pcblist SYSCTL_HANDLER_ARGS 54798271db4SGarrett Wollman { 54898271db4SGarrett Wollman int error, i, n, s; 54998271db4SGarrett Wollman struct inpcb *inp, **inp_list; 55098271db4SGarrett Wollman inp_gen_t gencnt; 55198271db4SGarrett Wollman struct xinpgen xig; 55298271db4SGarrett Wollman 55398271db4SGarrett Wollman /* 55498271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 55598271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 55698271db4SGarrett Wollman */ 55798271db4SGarrett Wollman if (req->oldptr == 0) { 55898271db4SGarrett Wollman n = ripcbinfo.ipi_count; 55998271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 56098271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 56198271db4SGarrett Wollman return 0; 56298271db4SGarrett Wollman } 56398271db4SGarrett Wollman 56498271db4SGarrett Wollman if (req->newptr != 0) 56598271db4SGarrett Wollman return EPERM; 56698271db4SGarrett Wollman 56798271db4SGarrett Wollman /* 56898271db4SGarrett Wollman * OK, now we're committed to doing something. 56998271db4SGarrett Wollman */ 57098271db4SGarrett Wollman s = splnet(); 57198271db4SGarrett Wollman gencnt = ripcbinfo.ipi_gencnt; 57298271db4SGarrett Wollman n = ripcbinfo.ipi_count; 57398271db4SGarrett Wollman splx(s); 57498271db4SGarrett Wollman 57598271db4SGarrett Wollman xig.xig_len = sizeof xig; 57698271db4SGarrett Wollman xig.xig_count = n; 57798271db4SGarrett Wollman xig.xig_gen = gencnt; 57898271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 57998271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 58098271db4SGarrett Wollman if (error) 58198271db4SGarrett Wollman return error; 58298271db4SGarrett Wollman 58398271db4SGarrett Wollman inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 58498271db4SGarrett Wollman if (inp_list == 0) 58598271db4SGarrett Wollman return ENOMEM; 58698271db4SGarrett Wollman 58798271db4SGarrett Wollman s = splnet(); 58898271db4SGarrett Wollman for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n; 58998271db4SGarrett Wollman inp = inp->inp_list.le_next) { 59098271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) 59198271db4SGarrett Wollman inp_list[i++] = inp; 59298271db4SGarrett Wollman } 59398271db4SGarrett Wollman splx(s); 59498271db4SGarrett Wollman n = i; 59598271db4SGarrett Wollman 59698271db4SGarrett Wollman error = 0; 59798271db4SGarrett Wollman for (i = 0; i < n; i++) { 59898271db4SGarrett Wollman inp = inp_list[i]; 59998271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 60098271db4SGarrett Wollman struct xinpcb xi; 60198271db4SGarrett Wollman xi.xi_len = sizeof xi; 60298271db4SGarrett Wollman /* XXX should avoid extra copy */ 60398271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 60498271db4SGarrett Wollman if (inp->inp_socket) 60598271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 60698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 60798271db4SGarrett Wollman } 60898271db4SGarrett Wollman } 60998271db4SGarrett Wollman if (!error) { 61098271db4SGarrett Wollman /* 61198271db4SGarrett Wollman * Give the user an updated idea of our state. 61298271db4SGarrett Wollman * If the generation differs from what we told 61398271db4SGarrett Wollman * her before, she knows that something happened 61498271db4SGarrett Wollman * while we were processing this request, and it 61598271db4SGarrett Wollman * might be necessary to retry. 61698271db4SGarrett Wollman */ 61798271db4SGarrett Wollman s = splnet(); 61898271db4SGarrett Wollman xig.xig_gen = ripcbinfo.ipi_gencnt; 61998271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 62098271db4SGarrett Wollman xig.xig_count = ripcbinfo.ipi_count; 62198271db4SGarrett Wollman splx(s); 62298271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 62398271db4SGarrett Wollman } 62498271db4SGarrett Wollman free(inp_list, M_TEMP); 62598271db4SGarrett Wollman return error; 62698271db4SGarrett Wollman } 62798271db4SGarrett Wollman 62898271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 62998271db4SGarrett Wollman rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 63098271db4SGarrett Wollman 631117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = { 632117bcae7SGarrett Wollman rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 633117bcae7SGarrett Wollman pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 634117bcae7SGarrett Wollman pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 635117bcae7SGarrett Wollman pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 636f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 637117bcae7SGarrett Wollman }; 638