1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 34 * $Id: raw_ip.c,v 1.53 1998/03/28 10:18:24 bde Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/proc.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/sysctl.h> 47 48 #include <vm/vm_zone.h> 49 50 #include <net/if.h> 51 #include <net/route.h> 52 53 #define _IP_VHL 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 #include <netinet/in_pcb.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip_var.h> 60 #include <netinet/ip_mroute.h> 61 62 #include <netinet/ip_fw.h> 63 64 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 65 #undef COMPAT_IPFW 66 #define COMPAT_IPFW 1 67 #else 68 #undef COMPAT_IPFW 69 #endif 70 71 static struct inpcbhead ripcb; 72 static struct inpcbinfo ripcbinfo; 73 74 /* 75 * Nominal space allocated to a raw ip socket. 76 */ 77 #define RIPSNDQ 8192 78 #define RIPRCVQ 8192 79 80 /* 81 * Raw interface to IP protocol. 82 */ 83 84 /* 85 * Initialize raw connection block q. 86 */ 87 void 88 rip_init() 89 { 90 LIST_INIT(&ripcb); 91 ripcbinfo.listhead = &ripcb; 92 /* 93 * XXX We don't use the hash list for raw IP, but it's easier 94 * to allocate a one entry hash list than it is to check all 95 * over the place for hashbase == NULL. 96 */ 97 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 98 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 99 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 100 maxsockets, ZONE_INTERRUPT, 0); 101 } 102 103 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 104 /* 105 * Setup generic address and protocol structures 106 * for raw_input routine, then pass them along with 107 * mbuf chain. 108 */ 109 void 110 rip_input(m, iphlen) 111 struct mbuf *m; 112 int iphlen; 113 { 114 register struct ip *ip = mtod(m, struct ip *); 115 register struct inpcb *inp; 116 struct inpcb *last = 0; 117 struct mbuf *opts = 0; 118 119 ripsrc.sin_addr = ip->ip_src; 120 for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 121 if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p) 122 continue; 123 if (inp->inp_laddr.s_addr && 124 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 125 continue; 126 if (inp->inp_faddr.s_addr && 127 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 128 continue; 129 if (last) { 130 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 131 if (n) { 132 if (last->inp_flags & INP_CONTROLOPTS || 133 last->inp_socket->so_options & SO_TIMESTAMP) 134 ip_savecontrol(last, &opts, ip, n); 135 if (sbappendaddr(&last->inp_socket->so_rcv, 136 (struct sockaddr *)&ripsrc, n, 137 opts) == 0) { 138 /* should notify about lost packet */ 139 m_freem(n); 140 if (opts) 141 m_freem(opts); 142 } else 143 sorwakeup(last->inp_socket); 144 opts = 0; 145 } 146 } 147 last = inp; 148 } 149 if (last) { 150 if (last->inp_flags & INP_CONTROLOPTS || 151 last->inp_socket->so_options & SO_TIMESTAMP) 152 ip_savecontrol(last, &opts, ip, m); 153 if (sbappendaddr(&last->inp_socket->so_rcv, 154 (struct sockaddr *)&ripsrc, m, opts) == 0) { 155 m_freem(m); 156 if (opts) 157 m_freem(opts); 158 } else 159 sorwakeup(last->inp_socket); 160 } else { 161 m_freem(m); 162 ipstat.ips_noproto++; 163 ipstat.ips_delivered--; 164 } 165 } 166 167 /* 168 * Generate IP header and pass packet to ip_output. 169 * Tack on options user may have setup with control call. 170 */ 171 int 172 rip_output(m, so, dst) 173 register struct mbuf *m; 174 struct socket *so; 175 u_long dst; 176 { 177 register struct ip *ip; 178 register struct inpcb *inp = sotoinpcb(so); 179 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 180 181 /* 182 * If the user handed us a complete IP packet, use it. 183 * Otherwise, allocate an mbuf for a header and fill it in. 184 */ 185 if ((inp->inp_flags & INP_HDRINCL) == 0) { 186 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 187 m_freem(m); 188 return(EMSGSIZE); 189 } 190 M_PREPEND(m, sizeof(struct ip), M_WAIT); 191 ip = mtod(m, struct ip *); 192 ip->ip_tos = 0; 193 ip->ip_off = 0; 194 ip->ip_p = inp->inp_ip_p; 195 ip->ip_len = m->m_pkthdr.len; 196 ip->ip_src = inp->inp_laddr; 197 ip->ip_dst.s_addr = dst; 198 ip->ip_ttl = MAXTTL; 199 } else { 200 if (m->m_pkthdr.len > IP_MAXPACKET) { 201 m_freem(m); 202 return(EMSGSIZE); 203 } 204 ip = mtod(m, struct ip *); 205 /* don't allow both user specified and setsockopt options, 206 and don't allow packet length sizes that will crash */ 207 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 208 && inp->inp_options) 209 || (ip->ip_len > m->m_pkthdr.len) 210 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 211 m_freem(m); 212 return EINVAL; 213 } 214 if (ip->ip_id == 0) 215 ip->ip_id = htons(ip_id++); 216 /* XXX prevent ip_output from overwriting header fields */ 217 flags |= IP_RAWOUTPUT; 218 ipstat.ips_rawout++; 219 } 220 return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 221 inp->inp_moptions)); 222 } 223 224 /* 225 * Raw IP socket option processing. 226 */ 227 int 228 rip_ctloutput(op, so, level, optname, m, p) 229 int op; 230 struct socket *so; 231 int level, optname; 232 struct mbuf **m; 233 struct proc *p; 234 { 235 register struct inpcb *inp = sotoinpcb(so); 236 register int error; 237 238 if (level != IPPROTO_IP) { 239 if (op == PRCO_SETOPT && *m) 240 (void)m_free(*m); 241 return (EINVAL); 242 } 243 244 switch (optname) { 245 246 case IP_HDRINCL: 247 error = 0; 248 if (op == PRCO_SETOPT) { 249 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) 250 error = EINVAL; 251 else if (*mtod(*m, int *)) 252 inp->inp_flags |= INP_HDRINCL; 253 else 254 inp->inp_flags &= ~INP_HDRINCL; 255 if (*m) 256 (void)m_free(*m); 257 } else { 258 *m = m_get(M_WAIT, MT_SOOPTS); 259 (*m)->m_len = sizeof (int); 260 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL; 261 } 262 return (error); 263 264 #ifdef COMPAT_IPFW 265 case IP_FW_GET: 266 if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) { 267 if (*m) (void)m_free(*m); 268 return(EINVAL); 269 } 270 return (*ip_fw_ctl_ptr)(optname, m); 271 272 case IP_FW_ADD: 273 case IP_FW_DEL: 274 case IP_FW_FLUSH: 275 case IP_FW_ZERO: 276 if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) { 277 if (*m) (void)m_free(*m); 278 return(EINVAL); 279 } 280 return (*ip_fw_ctl_ptr)(optname, m); 281 282 case IP_NAT: 283 if (ip_nat_ctl_ptr == NULL) { 284 if (*m) (void)m_free(*m); 285 return(EINVAL); 286 } 287 return (*ip_nat_ctl_ptr)(op, m); 288 289 #endif 290 case IP_RSVP_ON: 291 return ip_rsvp_init(so); 292 break; 293 294 case IP_RSVP_OFF: 295 return ip_rsvp_done(); 296 break; 297 298 case IP_RSVP_VIF_ON: 299 return ip_rsvp_vif_init(so, *m); 300 301 case IP_RSVP_VIF_OFF: 302 return ip_rsvp_vif_done(so, *m); 303 304 case MRT_INIT: 305 case MRT_DONE: 306 case MRT_ADD_VIF: 307 case MRT_DEL_VIF: 308 case MRT_ADD_MFC: 309 case MRT_DEL_MFC: 310 case MRT_VERSION: 311 case MRT_ASSERT: 312 if (op == PRCO_SETOPT) { 313 error = ip_mrouter_set(optname, so, *m); 314 if (*m) 315 (void)m_free(*m); 316 } else if (op == PRCO_GETOPT) { 317 error = ip_mrouter_get(optname, so, m); 318 } else 319 error = EINVAL; 320 return (error); 321 } 322 return (ip_ctloutput(op, so, level, optname, m, p)); 323 } 324 325 /* 326 * This function exists solely to receive the PRC_IFDOWN messages which 327 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 328 * and calls in_ifadown() to remove all routes corresponding to that address. 329 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 330 * interface routes. 331 */ 332 void 333 rip_ctlinput(cmd, sa, vip) 334 int cmd; 335 struct sockaddr *sa; 336 void *vip; 337 { 338 struct in_ifaddr *ia; 339 struct ifnet *ifp; 340 int err; 341 int flags; 342 343 switch(cmd) { 344 case PRC_IFDOWN: 345 for (ia = in_ifaddrhead.tqh_first; ia; 346 ia = ia->ia_link.tqe_next) { 347 if (ia->ia_ifa.ifa_addr == sa 348 && (ia->ia_flags & IFA_ROUTE)) { 349 /* 350 * in_ifscrub kills the interface route. 351 */ 352 in_ifscrub(ia->ia_ifp, ia); 353 /* 354 * in_ifadown gets rid of all the rest of 355 * the routes. This is not quite the right 356 * thing to do, but at least if we are running 357 * a routing process they will come back. 358 */ 359 in_ifadown(&ia->ia_ifa); 360 break; 361 } 362 } 363 break; 364 365 case PRC_IFUP: 366 for (ia = in_ifaddrhead.tqh_first; ia; 367 ia = ia->ia_link.tqe_next) { 368 if (ia->ia_ifa.ifa_addr == sa) 369 break; 370 } 371 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 372 return; 373 flags = RTF_UP; 374 ifp = ia->ia_ifa.ifa_ifp; 375 376 if ((ifp->if_flags & IFF_LOOPBACK) 377 || (ifp->if_flags & IFF_POINTOPOINT)) 378 flags |= RTF_HOST; 379 380 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 381 if (err == 0) 382 ia->ia_flags |= IFA_ROUTE; 383 break; 384 } 385 } 386 387 static u_long rip_sendspace = RIPSNDQ; 388 static u_long rip_recvspace = RIPRCVQ; 389 390 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, &rip_sendspace, 391 0, ""); 392 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, &rip_recvspace, 393 0, ""); 394 395 static int 396 rip_attach(struct socket *so, int proto, struct proc *p) 397 { 398 struct inpcb *inp; 399 int error, s; 400 401 inp = sotoinpcb(so); 402 if (inp) 403 panic("rip_attach"); 404 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 405 return error; 406 407 s = splnet(); 408 error = in_pcballoc(so, &ripcbinfo, p); 409 splx(s); 410 if (error) 411 return error; 412 error = soreserve(so, rip_sendspace, rip_recvspace); 413 if (error) 414 return error; 415 inp = (struct inpcb *)so->so_pcb; 416 inp->inp_ip_p = proto; 417 return 0; 418 } 419 420 static int 421 rip_detach(struct socket *so) 422 { 423 struct inpcb *inp; 424 425 inp = sotoinpcb(so); 426 if (inp == 0) 427 panic("rip_detach"); 428 if (so == ip_mrouter) 429 ip_mrouter_done(); 430 ip_rsvp_force_done(so); 431 if (so == ip_rsvpd) 432 ip_rsvp_done(); 433 in_pcbdetach(inp); 434 return 0; 435 } 436 437 static int 438 rip_abort(struct socket *so) 439 { 440 soisdisconnected(so); 441 return rip_detach(so); 442 } 443 444 static int 445 rip_disconnect(struct socket *so) 446 { 447 if ((so->so_state & SS_ISCONNECTED) == 0) 448 return ENOTCONN; 449 return rip_abort(so); 450 } 451 452 static int 453 rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 454 { 455 struct inpcb *inp = sotoinpcb(so); 456 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 457 458 if (nam->sa_len != sizeof(*addr)) 459 return EINVAL; 460 461 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 462 (addr->sin_family != AF_IMPLINK)) || 463 (addr->sin_addr.s_addr && 464 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 465 return EADDRNOTAVAIL; 466 inp->inp_laddr = addr->sin_addr; 467 return 0; 468 } 469 470 static int 471 rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 472 { 473 struct inpcb *inp = sotoinpcb(so); 474 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 475 476 if (nam->sa_len != sizeof(*addr)) 477 return EINVAL; 478 if (TAILQ_EMPTY(&ifnet)) 479 return EADDRNOTAVAIL; 480 if ((addr->sin_family != AF_INET) && 481 (addr->sin_family != AF_IMPLINK)) 482 return EAFNOSUPPORT; 483 inp->inp_faddr = addr->sin_addr; 484 soisconnected(so); 485 return 0; 486 } 487 488 static int 489 rip_shutdown(struct socket *so) 490 { 491 socantsendmore(so); 492 return 0; 493 } 494 495 static int 496 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 497 struct mbuf *control, struct proc *p) 498 { 499 struct inpcb *inp = sotoinpcb(so); 500 register u_long dst; 501 502 if (so->so_state & SS_ISCONNECTED) { 503 if (nam) { 504 m_freem(m); 505 return EISCONN; 506 } 507 dst = inp->inp_faddr.s_addr; 508 } else { 509 if (nam == NULL) { 510 m_freem(m); 511 return ENOTCONN; 512 } 513 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 514 } 515 return rip_output(m, so, dst); 516 } 517 518 static int 519 rip_pcblist SYSCTL_HANDLER_ARGS 520 { 521 int error, i, n, s; 522 struct inpcb *inp, **inp_list; 523 inp_gen_t gencnt; 524 struct xinpgen xig; 525 526 /* 527 * The process of preparing the TCB list is too time-consuming and 528 * resource-intensive to repeat twice on every request. 529 */ 530 if (req->oldptr == 0) { 531 n = ripcbinfo.ipi_count; 532 req->oldidx = 2 * (sizeof xig) 533 + (n + n/8) * sizeof(struct xinpcb); 534 return 0; 535 } 536 537 if (req->newptr != 0) 538 return EPERM; 539 540 /* 541 * OK, now we're committed to doing something. 542 */ 543 s = splnet(); 544 gencnt = ripcbinfo.ipi_gencnt; 545 n = ripcbinfo.ipi_count; 546 splx(s); 547 548 xig.xig_len = sizeof xig; 549 xig.xig_count = n; 550 xig.xig_gen = gencnt; 551 xig.xig_sogen = so_gencnt; 552 error = SYSCTL_OUT(req, &xig, sizeof xig); 553 if (error) 554 return error; 555 556 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 557 if (inp_list == 0) 558 return ENOMEM; 559 560 s = splnet(); 561 for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n; 562 inp = inp->inp_list.le_next) { 563 if (inp->inp_gencnt <= gencnt) 564 inp_list[i++] = inp; 565 } 566 splx(s); 567 n = i; 568 569 error = 0; 570 for (i = 0; i < n; i++) { 571 inp = inp_list[i]; 572 if (inp->inp_gencnt <= gencnt) { 573 struct xinpcb xi; 574 xi.xi_len = sizeof xi; 575 /* XXX should avoid extra copy */ 576 bcopy(inp, &xi.xi_inp, sizeof *inp); 577 if (inp->inp_socket) 578 sotoxsocket(inp->inp_socket, &xi.xi_socket); 579 error = SYSCTL_OUT(req, &xi, sizeof xi); 580 } 581 } 582 if (!error) { 583 /* 584 * Give the user an updated idea of our state. 585 * If the generation differs from what we told 586 * her before, she knows that something happened 587 * while we were processing this request, and it 588 * might be necessary to retry. 589 */ 590 s = splnet(); 591 xig.xig_gen = ripcbinfo.ipi_gencnt; 592 xig.xig_sogen = so_gencnt; 593 xig.xig_count = ripcbinfo.ipi_count; 594 splx(s); 595 error = SYSCTL_OUT(req, &xig, sizeof xig); 596 } 597 free(inp_list, M_TEMP); 598 return error; 599 } 600 601 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 602 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 603 604 struct pr_usrreqs rip_usrreqs = { 605 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 606 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 607 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 608 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 609 in_setsockaddr, sosend, soreceive, sopoll 610 }; 611