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 376a800098SYoshinobu Inoue #include "opt_inet6.h" 386a800098SYoshinobu Inoue #include "opt_ipsec.h" 396a800098SYoshinobu Inoue 40df8bae1dSRodney W. Grimes #include <sys/param.h> 41117bcae7SGarrett Wollman #include <sys/systm.h> 42117bcae7SGarrett Wollman #include <sys/kernel.h> 43df8bae1dSRodney W. Grimes #include <sys/malloc.h> 44df8bae1dSRodney W. Grimes #include <sys/mbuf.h> 45df8bae1dSRodney W. Grimes #include <sys/protosw.h> 46117bcae7SGarrett Wollman #include <sys/socket.h> 47df8bae1dSRodney W. Grimes #include <sys/socketvar.h> 48117bcae7SGarrett Wollman #include <sys/sysctl.h> 498781d8e9SBruce Evans 503d4d47f3SGarrett Wollman #include <vm/vm_zone.h> 51df8bae1dSRodney W. Grimes 52df8bae1dSRodney W. Grimes #include <net/if.h> 53df8bae1dSRodney W. Grimes #include <net/route.h> 54df8bae1dSRodney W. Grimes 555e2d0696SGarrett Wollman #define _IP_VHL 56df8bae1dSRodney W. Grimes #include <netinet/in.h> 57df8bae1dSRodney W. Grimes #include <netinet/in_systm.h> 58df8bae1dSRodney W. Grimes #include <netinet/ip.h> 59c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h> 60c1f8a6ceSDavid Greenman #include <netinet/in_var.h> 61df8bae1dSRodney W. Grimes #include <netinet/ip_var.h> 62df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h> 63df8bae1dSRodney W. Grimes 64100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h> 65100ba1a6SJordan K. Hubbard 666a800098SYoshinobu Inoue #ifdef IPSEC 676a800098SYoshinobu Inoue #include <netinet6/ipsec.h> 686a800098SYoshinobu Inoue #endif /*IPSEC*/ 696a800098SYoshinobu Inoue 70b715f178SLuigi Rizzo #include "opt_ipdn.h" 71b715f178SLuigi Rizzo #ifdef DUMMYNET 72b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h> 73b715f178SLuigi Rizzo #endif 74f9493383SGarrett Wollman 7582cd038dSYoshinobu Inoue struct inpcbhead ripcb; 7682cd038dSYoshinobu Inoue struct inpcbinfo ripcbinfo; 77df8bae1dSRodney W. Grimes 78df8bae1dSRodney W. Grimes /* 79df8bae1dSRodney W. Grimes * Nominal space allocated to a raw ip socket. 80df8bae1dSRodney W. Grimes */ 81df8bae1dSRodney W. Grimes #define RIPSNDQ 8192 82df8bae1dSRodney W. Grimes #define RIPRCVQ 8192 83df8bae1dSRodney W. Grimes 84df8bae1dSRodney W. Grimes /* 85df8bae1dSRodney W. Grimes * Raw interface to IP protocol. 86df8bae1dSRodney W. Grimes */ 87df8bae1dSRodney W. Grimes 88df8bae1dSRodney W. Grimes /* 89df8bae1dSRodney W. Grimes * Initialize raw connection block q. 90df8bae1dSRodney W. Grimes */ 91df8bae1dSRodney W. Grimes void 92df8bae1dSRodney W. Grimes rip_init() 93df8bae1dSRodney W. Grimes { 9415bd2b43SDavid Greenman LIST_INIT(&ripcb); 9515bd2b43SDavid Greenman ripcbinfo.listhead = &ripcb; 9615bd2b43SDavid Greenman /* 9715bd2b43SDavid Greenman * XXX We don't use the hash list for raw IP, but it's easier 9815bd2b43SDavid Greenman * to allocate a one entry hash list than it is to check all 9915bd2b43SDavid Greenman * over the place for hashbase == NULL. 10015bd2b43SDavid Greenman */ 101ddd79a97SDavid Greenman ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 102c3229e05SDavid Greenman ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 1033d4d47f3SGarrett Wollman ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 10498271db4SGarrett Wollman maxsockets, ZONE_INTERRUPT, 0); 105df8bae1dSRodney W. Grimes } 106df8bae1dSRodney W. Grimes 107f6d24a78SPoul-Henning Kamp static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 108df8bae1dSRodney W. Grimes /* 109df8bae1dSRodney W. Grimes * Setup generic address and protocol structures 110df8bae1dSRodney W. Grimes * for raw_input routine, then pass them along with 111df8bae1dSRodney W. Grimes * mbuf chain. 112df8bae1dSRodney W. Grimes */ 113df8bae1dSRodney W. Grimes void 1146a800098SYoshinobu Inoue rip_input(m, off, proto) 115df8bae1dSRodney W. Grimes struct mbuf *m; 1166a800098SYoshinobu Inoue int off, proto; 117df8bae1dSRodney W. Grimes { 118df8bae1dSRodney W. Grimes register struct ip *ip = mtod(m, struct ip *); 119df8bae1dSRodney W. Grimes register struct inpcb *inp; 12082c23ebaSBill Fenner struct inpcb *last = 0; 12182c23ebaSBill Fenner struct mbuf *opts = 0; 122df8bae1dSRodney W. Grimes 123df8bae1dSRodney W. Grimes ripsrc.sin_addr = ip->ip_src; 1246a800098SYoshinobu Inoue LIST_FOREACH(inp, &ripcb, inp_list) { 1256a800098SYoshinobu Inoue #ifdef INET6 1266a800098SYoshinobu Inoue if ((inp->inp_vflag & INP_IPV4) == 0) 1276a800098SYoshinobu Inoue continue; 1286a800098SYoshinobu Inoue #endif 1296a800098SYoshinobu Inoue if (inp->inp_ip_p && inp->inp_ip_p != proto) 130df8bae1dSRodney W. Grimes continue; 131df8bae1dSRodney W. Grimes if (inp->inp_laddr.s_addr && 132d99c7a23SGarrett Wollman inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 133df8bae1dSRodney W. Grimes continue; 134df8bae1dSRodney W. Grimes if (inp->inp_faddr.s_addr && 135d99c7a23SGarrett Wollman inp->inp_faddr.s_addr != ip->ip_src.s_addr) 136df8bae1dSRodney W. Grimes continue; 137df8bae1dSRodney W. Grimes if (last) { 138623ae52eSPoul-Henning Kamp struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 139623ae52eSPoul-Henning Kamp if (n) { 14082c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS || 14182c23ebaSBill Fenner last->inp_socket->so_options & SO_TIMESTAMP) 14282c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, n); 14382c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 144623ae52eSPoul-Henning Kamp (struct sockaddr *)&ripsrc, n, 14582c23ebaSBill Fenner opts) == 0) { 146df8bae1dSRodney W. Grimes /* should notify about lost packet */ 147df8bae1dSRodney W. Grimes m_freem(n); 14882c23ebaSBill Fenner if (opts) 14982c23ebaSBill Fenner m_freem(opts); 15082c23ebaSBill Fenner } else 15182c23ebaSBill Fenner sorwakeup(last->inp_socket); 15282c23ebaSBill Fenner opts = 0; 153df8bae1dSRodney W. Grimes } 154df8bae1dSRodney W. Grimes } 15582c23ebaSBill Fenner last = inp; 156df8bae1dSRodney W. Grimes } 157df8bae1dSRodney W. Grimes if (last) { 15882c23ebaSBill Fenner if (last->inp_flags & INP_CONTROLOPTS || 15982c23ebaSBill Fenner last->inp_socket->so_options & SO_TIMESTAMP) 16082c23ebaSBill Fenner ip_savecontrol(last, &opts, ip, m); 16182c23ebaSBill Fenner if (sbappendaddr(&last->inp_socket->so_rcv, 16282c23ebaSBill Fenner (struct sockaddr *)&ripsrc, m, opts) == 0) { 163df8bae1dSRodney W. Grimes m_freem(m); 16482c23ebaSBill Fenner if (opts) 16582c23ebaSBill Fenner m_freem(opts); 16682c23ebaSBill Fenner } else 16782c23ebaSBill Fenner sorwakeup(last->inp_socket); 168df8bae1dSRodney W. Grimes } else { 169df8bae1dSRodney W. Grimes m_freem(m); 170df8bae1dSRodney W. Grimes ipstat.ips_noproto++; 171df8bae1dSRodney W. Grimes ipstat.ips_delivered--; 172df8bae1dSRodney W. Grimes } 173df8bae1dSRodney W. Grimes } 174df8bae1dSRodney W. Grimes 175df8bae1dSRodney W. Grimes /* 176df8bae1dSRodney W. Grimes * Generate IP header and pass packet to ip_output. 177df8bae1dSRodney W. Grimes * Tack on options user may have setup with control call. 178df8bae1dSRodney W. Grimes */ 179df8bae1dSRodney W. Grimes int 180df8bae1dSRodney W. Grimes rip_output(m, so, dst) 181d25f3712SBrian Feldman struct mbuf *m; 182df8bae1dSRodney W. Grimes struct socket *so; 183df8bae1dSRodney W. Grimes u_long dst; 184df8bae1dSRodney W. Grimes { 185df8bae1dSRodney W. Grimes register struct ip *ip; 186df8bae1dSRodney W. Grimes register struct inpcb *inp = sotoinpcb(so); 187df8bae1dSRodney W. Grimes int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 188df8bae1dSRodney W. Grimes 189df8bae1dSRodney W. Grimes /* 190df8bae1dSRodney W. Grimes * If the user handed us a complete IP packet, use it. 191df8bae1dSRodney W. Grimes * Otherwise, allocate an mbuf for a header and fill it in. 192df8bae1dSRodney W. Grimes */ 193df8bae1dSRodney W. Grimes if ((inp->inp_flags & INP_HDRINCL) == 0) { 194430d30d8SBill Fenner if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 195430d30d8SBill Fenner m_freem(m); 196430d30d8SBill Fenner return(EMSGSIZE); 197430d30d8SBill Fenner } 1982a0c503eSBosko Milekic M_PREPEND(m, sizeof(struct ip), M_TRYWAIT); 199df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 200df8bae1dSRodney W. Grimes ip->ip_tos = 0; 201df8bae1dSRodney W. Grimes ip->ip_off = 0; 202ca98b82cSDavid Greenman ip->ip_p = inp->inp_ip_p; 203df8bae1dSRodney W. Grimes ip->ip_len = m->m_pkthdr.len; 204df8bae1dSRodney W. Grimes ip->ip_src = inp->inp_laddr; 205df8bae1dSRodney W. Grimes ip->ip_dst.s_addr = dst; 206df8bae1dSRodney W. Grimes ip->ip_ttl = MAXTTL; 207df8bae1dSRodney W. Grimes } else { 208430d30d8SBill Fenner if (m->m_pkthdr.len > IP_MAXPACKET) { 209430d30d8SBill Fenner m_freem(m); 210430d30d8SBill Fenner return(EMSGSIZE); 211430d30d8SBill Fenner } 212df8bae1dSRodney W. Grimes ip = mtod(m, struct ip *); 213072b9b24SPaul Traina /* don't allow both user specified and setsockopt options, 214072b9b24SPaul Traina and don't allow packet length sizes that will crash */ 2155e2d0696SGarrett Wollman if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 2165e2d0696SGarrett Wollman && inp->inp_options) 21791108995SBill Fenner || (ip->ip_len > m->m_pkthdr.len) 21891108995SBill Fenner || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 219072b9b24SPaul Traina m_freem(m); 220072b9b24SPaul Traina return EINVAL; 221072b9b24SPaul Traina } 222df8bae1dSRodney W. Grimes if (ip->ip_id == 0) 223e30177e0SRuslan Ermilov ip->ip_id = htons(ip_id++); 224df8bae1dSRodney W. Grimes /* XXX prevent ip_output from overwriting header fields */ 225df8bae1dSRodney W. Grimes flags |= IP_RAWOUTPUT; 226df8bae1dSRodney W. Grimes ipstat.ips_rawout++; 227df8bae1dSRodney W. Grimes } 2286a800098SYoshinobu Inoue 2296a800098SYoshinobu Inoue #ifdef IPSEC 230686cdd19SJun-ichiro itojun Hagino ipsec_setsocket(m, so); 2316a800098SYoshinobu Inoue #endif /*IPSEC*/ 2326a800098SYoshinobu Inoue 233686cdd19SJun-ichiro itojun Hagino return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 234072b9b24SPaul Traina inp->inp_moptions)); 235df8bae1dSRodney W. Grimes } 236df8bae1dSRodney W. Grimes 237df8bae1dSRodney W. Grimes /* 238df8bae1dSRodney W. Grimes * Raw IP socket option processing. 239df8bae1dSRodney W. Grimes */ 240df8bae1dSRodney W. Grimes int 241cfe8b629SGarrett Wollman rip_ctloutput(so, sopt) 242df8bae1dSRodney W. Grimes struct socket *so; 243cfe8b629SGarrett Wollman struct sockopt *sopt; 244df8bae1dSRodney W. Grimes { 245cfe8b629SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 246cfe8b629SGarrett Wollman int error, optval; 247df8bae1dSRodney W. Grimes 248cfe8b629SGarrett Wollman if (sopt->sopt_level != IPPROTO_IP) 249df8bae1dSRodney W. Grimes return (EINVAL); 250df8bae1dSRodney W. Grimes 25125f26ad8SGarrett Wollman error = 0; 252cfe8b629SGarrett Wollman 253cfe8b629SGarrett Wollman switch (sopt->sopt_dir) { 254cfe8b629SGarrett Wollman case SOPT_GET: 255cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 256cfe8b629SGarrett Wollman case IP_HDRINCL: 257cfe8b629SGarrett Wollman optval = inp->inp_flags & INP_HDRINCL; 258cfe8b629SGarrett Wollman error = sooptcopyout(sopt, &optval, sizeof optval); 259cfe8b629SGarrett Wollman break; 260df8bae1dSRodney W. Grimes 261bc95ac80SRuslan Ermilov case IP_FW_ADD: 26209bb5f75SPoul-Henning Kamp case IP_FW_GET: 263cfe8b629SGarrett Wollman if (ip_fw_ctl_ptr == 0) 264cfe8b629SGarrett Wollman error = ENOPROTOOPT; 265cfe8b629SGarrett Wollman else 266cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 267cfe8b629SGarrett Wollman break; 2684dd1662bSUgen J.S. Antsilevich 269b715f178SLuigi Rizzo #ifdef DUMMYNET 270b715f178SLuigi Rizzo case IP_DUMMYNET_GET: 271b715f178SLuigi Rizzo if (ip_dn_ctl_ptr == NULL) 272b715f178SLuigi Rizzo error = ENOPROTOOPT ; 273b715f178SLuigi Rizzo else 274b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 275b715f178SLuigi Rizzo break ; 276b715f178SLuigi Rizzo #endif /* DUMMYNET */ 2771c5de19aSGarrett Wollman 2781c5de19aSGarrett Wollman case MRT_INIT: 2791c5de19aSGarrett Wollman case MRT_DONE: 2801c5de19aSGarrett Wollman case MRT_ADD_VIF: 2811c5de19aSGarrett Wollman case MRT_DEL_VIF: 2821c5de19aSGarrett Wollman case MRT_ADD_MFC: 2831c5de19aSGarrett Wollman case MRT_DEL_MFC: 2841c5de19aSGarrett Wollman case MRT_VERSION: 2851c5de19aSGarrett Wollman case MRT_ASSERT: 286cfe8b629SGarrett Wollman error = ip_mrouter_get(so, sopt); 287cfe8b629SGarrett Wollman break; 288cfe8b629SGarrett Wollman 289cfe8b629SGarrett Wollman default: 290cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 291cfe8b629SGarrett Wollman break; 292df8bae1dSRodney W. Grimes } 293cfe8b629SGarrett Wollman break; 294cfe8b629SGarrett Wollman 295cfe8b629SGarrett Wollman case SOPT_SET: 296cfe8b629SGarrett Wollman switch (sopt->sopt_name) { 297cfe8b629SGarrett Wollman case IP_HDRINCL: 298cfe8b629SGarrett Wollman error = sooptcopyin(sopt, &optval, sizeof optval, 299cfe8b629SGarrett Wollman sizeof optval); 300cfe8b629SGarrett Wollman if (error) 301cfe8b629SGarrett Wollman break; 302cfe8b629SGarrett Wollman if (optval) 303cfe8b629SGarrett Wollman inp->inp_flags |= INP_HDRINCL; 304cfe8b629SGarrett Wollman else 305cfe8b629SGarrett Wollman inp->inp_flags &= ~INP_HDRINCL; 306cfe8b629SGarrett Wollman break; 307cfe8b629SGarrett Wollman 308cfe8b629SGarrett Wollman case IP_FW_ADD: 309cfe8b629SGarrett Wollman case IP_FW_DEL: 310cfe8b629SGarrett Wollman case IP_FW_FLUSH: 311cfe8b629SGarrett Wollman case IP_FW_ZERO: 3120b6c1a83SBrian Feldman case IP_FW_RESETLOG: 313cfe8b629SGarrett Wollman if (ip_fw_ctl_ptr == 0) 314cfe8b629SGarrett Wollman error = ENOPROTOOPT; 315cfe8b629SGarrett Wollman else 316cfe8b629SGarrett Wollman error = ip_fw_ctl_ptr(sopt); 317cfe8b629SGarrett Wollman break; 318cfe8b629SGarrett Wollman 319b715f178SLuigi Rizzo #ifdef DUMMYNET 320b715f178SLuigi Rizzo case IP_DUMMYNET_CONFIGURE: 321b715f178SLuigi Rizzo case IP_DUMMYNET_DEL: 322b715f178SLuigi Rizzo case IP_DUMMYNET_FLUSH: 323b715f178SLuigi Rizzo if (ip_dn_ctl_ptr == NULL) 324b715f178SLuigi Rizzo error = ENOPROTOOPT ; 325b715f178SLuigi Rizzo else 326b715f178SLuigi Rizzo error = ip_dn_ctl_ptr(sopt); 327b715f178SLuigi Rizzo break ; 328b715f178SLuigi Rizzo #endif 329cfe8b629SGarrett Wollman 330cfe8b629SGarrett Wollman case IP_RSVP_ON: 331cfe8b629SGarrett Wollman error = ip_rsvp_init(so); 332cfe8b629SGarrett Wollman break; 333cfe8b629SGarrett Wollman 334cfe8b629SGarrett Wollman case IP_RSVP_OFF: 335cfe8b629SGarrett Wollman error = ip_rsvp_done(); 336cfe8b629SGarrett Wollman break; 337cfe8b629SGarrett Wollman 338cfe8b629SGarrett Wollman /* XXX - should be combined */ 339cfe8b629SGarrett Wollman case IP_RSVP_VIF_ON: 340cfe8b629SGarrett Wollman error = ip_rsvp_vif_init(so, sopt); 341cfe8b629SGarrett Wollman break; 342cfe8b629SGarrett Wollman 343cfe8b629SGarrett Wollman case IP_RSVP_VIF_OFF: 344cfe8b629SGarrett Wollman error = ip_rsvp_vif_done(so, sopt); 345cfe8b629SGarrett Wollman break; 346cfe8b629SGarrett Wollman 347cfe8b629SGarrett Wollman case MRT_INIT: 348cfe8b629SGarrett Wollman case MRT_DONE: 349cfe8b629SGarrett Wollman case MRT_ADD_VIF: 350cfe8b629SGarrett Wollman case MRT_DEL_VIF: 351cfe8b629SGarrett Wollman case MRT_ADD_MFC: 352cfe8b629SGarrett Wollman case MRT_DEL_MFC: 353cfe8b629SGarrett Wollman case MRT_VERSION: 354cfe8b629SGarrett Wollman case MRT_ASSERT: 355cfe8b629SGarrett Wollman error = ip_mrouter_set(so, sopt); 356cfe8b629SGarrett Wollman break; 357cfe8b629SGarrett Wollman 358cfe8b629SGarrett Wollman default: 359cfe8b629SGarrett Wollman error = ip_ctloutput(so, sopt); 360cfe8b629SGarrett Wollman break; 361cfe8b629SGarrett Wollman } 362cfe8b629SGarrett Wollman break; 363cfe8b629SGarrett Wollman } 364cfe8b629SGarrett Wollman 365cfe8b629SGarrett Wollman return (error); 366df8bae1dSRodney W. Grimes } 367df8bae1dSRodney W. Grimes 36839191c8eSGarrett Wollman /* 36939191c8eSGarrett Wollman * This function exists solely to receive the PRC_IFDOWN messages which 37039191c8eSGarrett Wollman * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 37139191c8eSGarrett Wollman * and calls in_ifadown() to remove all routes corresponding to that address. 37239191c8eSGarrett Wollman * It also receives the PRC_IFUP messages from if_up() and reinstalls the 37339191c8eSGarrett Wollman * interface routes. 37439191c8eSGarrett Wollman */ 37539191c8eSGarrett Wollman void 37639191c8eSGarrett Wollman rip_ctlinput(cmd, sa, vip) 37739191c8eSGarrett Wollman int cmd; 37839191c8eSGarrett Wollman struct sockaddr *sa; 37939191c8eSGarrett Wollman void *vip; 38039191c8eSGarrett Wollman { 38139191c8eSGarrett Wollman struct in_ifaddr *ia; 38239191c8eSGarrett Wollman struct ifnet *ifp; 38339191c8eSGarrett Wollman int err; 38439191c8eSGarrett Wollman int flags; 38539191c8eSGarrett Wollman 38639191c8eSGarrett Wollman switch (cmd) { 38739191c8eSGarrett Wollman case PRC_IFDOWN: 388fc2ffbe6SPoul-Henning Kamp for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 389fc2ffbe6SPoul-Henning Kamp ia = TAILQ_NEXT(ia, ia_link)) { 39039191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa 39139191c8eSGarrett Wollman && (ia->ia_flags & IFA_ROUTE)) { 39239191c8eSGarrett Wollman /* 39339191c8eSGarrett Wollman * in_ifscrub kills the interface route. 39439191c8eSGarrett Wollman */ 39539191c8eSGarrett Wollman in_ifscrub(ia->ia_ifp, ia); 39639191c8eSGarrett Wollman /* 39739191c8eSGarrett Wollman * in_ifadown gets rid of all the rest of 39839191c8eSGarrett Wollman * the routes. This is not quite the right 39939191c8eSGarrett Wollman * thing to do, but at least if we are running 40039191c8eSGarrett Wollman * a routing process they will come back. 40139191c8eSGarrett Wollman */ 40239191c8eSGarrett Wollman in_ifadown(&ia->ia_ifa); 40339191c8eSGarrett Wollman break; 40439191c8eSGarrett Wollman } 40539191c8eSGarrett Wollman } 40639191c8eSGarrett Wollman break; 40739191c8eSGarrett Wollman 40839191c8eSGarrett Wollman case PRC_IFUP: 409fc2ffbe6SPoul-Henning Kamp for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 410fc2ffbe6SPoul-Henning Kamp ia = TAILQ_NEXT(ia, ia_link)) { 41139191c8eSGarrett Wollman if (ia->ia_ifa.ifa_addr == sa) 41239191c8eSGarrett Wollman break; 41339191c8eSGarrett Wollman } 41439191c8eSGarrett Wollman if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 41539191c8eSGarrett Wollman return; 41639191c8eSGarrett Wollman flags = RTF_UP; 41739191c8eSGarrett Wollman ifp = ia->ia_ifa.ifa_ifp; 41839191c8eSGarrett Wollman 41939191c8eSGarrett Wollman if ((ifp->if_flags & IFF_LOOPBACK) 42039191c8eSGarrett Wollman || (ifp->if_flags & IFF_POINTOPOINT)) 42139191c8eSGarrett Wollman flags |= RTF_HOST; 42239191c8eSGarrett Wollman 42339191c8eSGarrett Wollman err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 42439191c8eSGarrett Wollman if (err == 0) 42539191c8eSGarrett Wollman ia->ia_flags |= IFA_ROUTE; 42639191c8eSGarrett Wollman break; 42739191c8eSGarrett Wollman } 42839191c8eSGarrett Wollman } 42939191c8eSGarrett Wollman 43082cd038dSYoshinobu Inoue u_long rip_sendspace = RIPSNDQ; 43182cd038dSYoshinobu Inoue u_long rip_recvspace = RIPRCVQ; 432df8bae1dSRodney W. Grimes 4333d177f46SBill Fumerola SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 4343d177f46SBill Fumerola &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 4353d177f46SBill Fumerola SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 4363d177f46SBill Fumerola &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 437117bcae7SGarrett Wollman 438117bcae7SGarrett Wollman static int 439a29f300eSGarrett Wollman rip_attach(struct socket *so, int proto, struct proc *p) 440df8bae1dSRodney W. Grimes { 441117bcae7SGarrett Wollman struct inpcb *inp; 44286b3ebceSDavid Greenman int error, s; 443c1f8a6ceSDavid Greenman 444117bcae7SGarrett Wollman inp = sotoinpcb(so); 445df8bae1dSRodney W. Grimes if (inp) 446df8bae1dSRodney W. Grimes panic("rip_attach"); 447f711d546SPoul-Henning Kamp if (p && (error = suser(p)) != 0) 448a29f300eSGarrett Wollman return error; 449117bcae7SGarrett Wollman 4506a800098SYoshinobu Inoue error = soreserve(so, rip_sendspace, rip_recvspace); 4516a800098SYoshinobu Inoue if (error) 4526a800098SYoshinobu Inoue return error; 45386b3ebceSDavid Greenman s = splnet(); 45486b3ebceSDavid Greenman error = in_pcballoc(so, &ripcbinfo, p); 45586b3ebceSDavid Greenman splx(s); 45686b3ebceSDavid Greenman if (error) 45786b3ebceSDavid Greenman return error; 458df8bae1dSRodney W. Grimes inp = (struct inpcb *)so->so_pcb; 4596a800098SYoshinobu Inoue inp->inp_vflag |= INP_IPV4; 460ca98b82cSDavid Greenman inp->inp_ip_p = proto; 4616a800098SYoshinobu Inoue #ifdef IPSEC 4626a800098SYoshinobu Inoue error = ipsec_init_policy(so, &inp->inp_sp); 4636a800098SYoshinobu Inoue if (error != 0) { 4646a800098SYoshinobu Inoue in_pcbdetach(inp); 4656a800098SYoshinobu Inoue return error; 4666a800098SYoshinobu Inoue } 4676a800098SYoshinobu Inoue #endif /*IPSEC*/ 468117bcae7SGarrett Wollman return 0; 469df8bae1dSRodney W. Grimes } 470117bcae7SGarrett Wollman 471117bcae7SGarrett Wollman static int 472117bcae7SGarrett Wollman rip_detach(struct socket *so) 473117bcae7SGarrett Wollman { 474117bcae7SGarrett Wollman struct inpcb *inp; 475117bcae7SGarrett Wollman 476117bcae7SGarrett Wollman inp = sotoinpcb(so); 477df8bae1dSRodney W. Grimes if (inp == 0) 478df8bae1dSRodney W. Grimes panic("rip_detach"); 479df8bae1dSRodney W. Grimes if (so == ip_mrouter) 480df8bae1dSRodney W. Grimes ip_mrouter_done(); 481b4489dc3SGarrett Wollman ip_rsvp_force_done(so); 482838ecf42SGarrett Wollman if (so == ip_rsvpd) 483838ecf42SGarrett Wollman ip_rsvp_done(); 484df8bae1dSRodney W. Grimes in_pcbdetach(inp); 485117bcae7SGarrett Wollman return 0; 486117bcae7SGarrett Wollman } 487df8bae1dSRodney W. Grimes 488117bcae7SGarrett Wollman static int 489117bcae7SGarrett Wollman rip_abort(struct socket *so) 490df8bae1dSRodney W. Grimes { 491117bcae7SGarrett Wollman soisdisconnected(so); 492117bcae7SGarrett Wollman return rip_detach(so); 493117bcae7SGarrett Wollman } 494117bcae7SGarrett Wollman 495117bcae7SGarrett Wollman static int 496117bcae7SGarrett Wollman rip_disconnect(struct socket *so) 497117bcae7SGarrett Wollman { 498117bcae7SGarrett Wollman if ((so->so_state & SS_ISCONNECTED) == 0) 499117bcae7SGarrett Wollman return ENOTCONN; 500117bcae7SGarrett Wollman return rip_abort(so); 501117bcae7SGarrett Wollman } 502117bcae7SGarrett Wollman 503117bcae7SGarrett Wollman static int 50457bf258eSGarrett Wollman rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 505117bcae7SGarrett Wollman { 506117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 50757bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 508df8bae1dSRodney W. Grimes 50957bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 510117bcae7SGarrett Wollman return EINVAL; 511117bcae7SGarrett Wollman 512117bcae7SGarrett Wollman if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 513df8bae1dSRodney W. Grimes (addr->sin_family != AF_IMPLINK)) || 514df8bae1dSRodney W. Grimes (addr->sin_addr.s_addr && 515117bcae7SGarrett Wollman ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 516117bcae7SGarrett Wollman return EADDRNOTAVAIL; 517df8bae1dSRodney W. Grimes inp->inp_laddr = addr->sin_addr; 518117bcae7SGarrett Wollman return 0; 519df8bae1dSRodney W. Grimes } 520117bcae7SGarrett Wollman 521117bcae7SGarrett Wollman static int 52257bf258eSGarrett Wollman rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 523df8bae1dSRodney W. Grimes { 524117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 52557bf258eSGarrett Wollman struct sockaddr_in *addr = (struct sockaddr_in *)nam; 526df8bae1dSRodney W. Grimes 52757bf258eSGarrett Wollman if (nam->sa_len != sizeof(*addr)) 528117bcae7SGarrett Wollman return EINVAL; 529117bcae7SGarrett Wollman if (TAILQ_EMPTY(&ifnet)) 530117bcae7SGarrett Wollman return EADDRNOTAVAIL; 531df8bae1dSRodney W. Grimes if ((addr->sin_family != AF_INET) && 532117bcae7SGarrett Wollman (addr->sin_family != AF_IMPLINK)) 533117bcae7SGarrett Wollman return EAFNOSUPPORT; 534df8bae1dSRodney W. Grimes inp->inp_faddr = addr->sin_addr; 535df8bae1dSRodney W. Grimes soisconnected(so); 536117bcae7SGarrett Wollman return 0; 537df8bae1dSRodney W. Grimes } 538df8bae1dSRodney W. Grimes 539117bcae7SGarrett Wollman static int 540117bcae7SGarrett Wollman rip_shutdown(struct socket *so) 541df8bae1dSRodney W. Grimes { 542117bcae7SGarrett Wollman socantsendmore(so); 543117bcae7SGarrett Wollman return 0; 544117bcae7SGarrett Wollman } 545117bcae7SGarrett Wollman 546117bcae7SGarrett Wollman static int 54757bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 548a29f300eSGarrett Wollman struct mbuf *control, struct proc *p) 549117bcae7SGarrett Wollman { 550117bcae7SGarrett Wollman struct inpcb *inp = sotoinpcb(so); 551df8bae1dSRodney W. Grimes register u_long dst; 552df8bae1dSRodney W. Grimes 553df8bae1dSRodney W. Grimes if (so->so_state & SS_ISCONNECTED) { 554df8bae1dSRodney W. Grimes if (nam) { 555117bcae7SGarrett Wollman m_freem(m); 556117bcae7SGarrett Wollman return EISCONN; 557df8bae1dSRodney W. Grimes } 558df8bae1dSRodney W. Grimes dst = inp->inp_faddr.s_addr; 559df8bae1dSRodney W. Grimes } else { 560df8bae1dSRodney W. Grimes if (nam == NULL) { 561117bcae7SGarrett Wollman m_freem(m); 562117bcae7SGarrett Wollman return ENOTCONN; 563df8bae1dSRodney W. Grimes } 56457bf258eSGarrett Wollman dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 565df8bae1dSRodney W. Grimes } 566117bcae7SGarrett Wollman return rip_output(m, so, dst); 567df8bae1dSRodney W. Grimes } 568df8bae1dSRodney W. Grimes 56998271db4SGarrett Wollman static int 57082d9ae4eSPoul-Henning Kamp rip_pcblist(SYSCTL_HANDLER_ARGS) 57198271db4SGarrett Wollman { 57298271db4SGarrett Wollman int error, i, n, s; 57398271db4SGarrett Wollman struct inpcb *inp, **inp_list; 57498271db4SGarrett Wollman inp_gen_t gencnt; 57598271db4SGarrett Wollman struct xinpgen xig; 57698271db4SGarrett Wollman 57798271db4SGarrett Wollman /* 57898271db4SGarrett Wollman * The process of preparing the TCB list is too time-consuming and 57998271db4SGarrett Wollman * resource-intensive to repeat twice on every request. 58098271db4SGarrett Wollman */ 58198271db4SGarrett Wollman if (req->oldptr == 0) { 58298271db4SGarrett Wollman n = ripcbinfo.ipi_count; 58398271db4SGarrett Wollman req->oldidx = 2 * (sizeof xig) 58498271db4SGarrett Wollman + (n + n/8) * sizeof(struct xinpcb); 58598271db4SGarrett Wollman return 0; 58698271db4SGarrett Wollman } 58798271db4SGarrett Wollman 58898271db4SGarrett Wollman if (req->newptr != 0) 58998271db4SGarrett Wollman return EPERM; 59098271db4SGarrett Wollman 59198271db4SGarrett Wollman /* 59298271db4SGarrett Wollman * OK, now we're committed to doing something. 59398271db4SGarrett Wollman */ 59498271db4SGarrett Wollman s = splnet(); 59598271db4SGarrett Wollman gencnt = ripcbinfo.ipi_gencnt; 59698271db4SGarrett Wollman n = ripcbinfo.ipi_count; 59798271db4SGarrett Wollman splx(s); 59898271db4SGarrett Wollman 59998271db4SGarrett Wollman xig.xig_len = sizeof xig; 60098271db4SGarrett Wollman xig.xig_count = n; 60198271db4SGarrett Wollman xig.xig_gen = gencnt; 60298271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 60398271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 60498271db4SGarrett Wollman if (error) 60598271db4SGarrett Wollman return error; 60698271db4SGarrett Wollman 60798271db4SGarrett Wollman inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 60898271db4SGarrett Wollman if (inp_list == 0) 60998271db4SGarrett Wollman return ENOMEM; 61098271db4SGarrett Wollman 61198271db4SGarrett Wollman s = splnet(); 612fc2ffbe6SPoul-Henning Kamp for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 613fc2ffbe6SPoul-Henning Kamp inp = LIST_NEXT(inp, inp_list)) { 61498271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) 61598271db4SGarrett Wollman inp_list[i++] = inp; 61698271db4SGarrett Wollman } 61798271db4SGarrett Wollman splx(s); 61898271db4SGarrett Wollman n = i; 61998271db4SGarrett Wollman 62098271db4SGarrett Wollman error = 0; 62198271db4SGarrett Wollman for (i = 0; i < n; i++) { 62298271db4SGarrett Wollman inp = inp_list[i]; 62398271db4SGarrett Wollman if (inp->inp_gencnt <= gencnt) { 62498271db4SGarrett Wollman struct xinpcb xi; 62598271db4SGarrett Wollman xi.xi_len = sizeof xi; 62698271db4SGarrett Wollman /* XXX should avoid extra copy */ 62798271db4SGarrett Wollman bcopy(inp, &xi.xi_inp, sizeof *inp); 62898271db4SGarrett Wollman if (inp->inp_socket) 62998271db4SGarrett Wollman sotoxsocket(inp->inp_socket, &xi.xi_socket); 63098271db4SGarrett Wollman error = SYSCTL_OUT(req, &xi, sizeof xi); 63198271db4SGarrett Wollman } 63298271db4SGarrett Wollman } 63398271db4SGarrett Wollman if (!error) { 63498271db4SGarrett Wollman /* 63598271db4SGarrett Wollman * Give the user an updated idea of our state. 63698271db4SGarrett Wollman * If the generation differs from what we told 63798271db4SGarrett Wollman * her before, she knows that something happened 63898271db4SGarrett Wollman * while we were processing this request, and it 63998271db4SGarrett Wollman * might be necessary to retry. 64098271db4SGarrett Wollman */ 64198271db4SGarrett Wollman s = splnet(); 64298271db4SGarrett Wollman xig.xig_gen = ripcbinfo.ipi_gencnt; 64398271db4SGarrett Wollman xig.xig_sogen = so_gencnt; 64498271db4SGarrett Wollman xig.xig_count = ripcbinfo.ipi_count; 64598271db4SGarrett Wollman splx(s); 64698271db4SGarrett Wollman error = SYSCTL_OUT(req, &xig, sizeof xig); 64798271db4SGarrett Wollman } 64898271db4SGarrett Wollman free(inp_list, M_TEMP); 64998271db4SGarrett Wollman return error; 65098271db4SGarrett Wollman } 65198271db4SGarrett Wollman 65298271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 65398271db4SGarrett Wollman rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 65498271db4SGarrett Wollman 655117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = { 656117bcae7SGarrett Wollman rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 657117bcae7SGarrett Wollman pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 658117bcae7SGarrett Wollman pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 659117bcae7SGarrett Wollman pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 660f8f6cbbaSPeter Wemm in_setsockaddr, sosend, soreceive, sopoll 661117bcae7SGarrett Wollman }; 662