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 * $FreeBSD$ 35 */ 36 37 #include "opt_inet6.h" 38 #include "opt_ipsec.h" 39 #include "opt_random_ip_id.h" 40 41 #include <sys/param.h> 42 #include <sys/systm.h> 43 #include <sys/kernel.h> 44 #include <sys/malloc.h> 45 #include <sys/mbuf.h> 46 #include <sys/proc.h> 47 #include <sys/protosw.h> 48 #include <sys/socket.h> 49 #include <sys/socketvar.h> 50 #include <sys/sysctl.h> 51 52 #include <vm/vm_zone.h> 53 54 #include <net/if.h> 55 #include <net/route.h> 56 57 #define _IP_VHL 58 #include <netinet/in.h> 59 #include <netinet/in_systm.h> 60 #include <netinet/ip.h> 61 #include <netinet/in_pcb.h> 62 #include <netinet/in_var.h> 63 #include <netinet/ip_var.h> 64 #include <netinet/ip_mroute.h> 65 66 #include <netinet/ip_fw.h> 67 #include <netinet/ip_dummynet.h> 68 69 #ifdef IPSEC 70 #include <netinet6/ipsec.h> 71 #endif /*IPSEC*/ 72 73 #include "opt_ipdn.h" 74 75 struct inpcbhead ripcb; 76 struct inpcbinfo ripcbinfo; 77 78 /* control hooks for ipfw and dummynet */ 79 ip_fw_ctl_t *ip_fw_ctl_ptr; 80 ip_dn_ctl_t *ip_dn_ctl_ptr; 81 82 /* 83 * Nominal space allocated to a raw ip socket. 84 */ 85 #define RIPSNDQ 8192 86 #define RIPRCVQ 8192 87 88 /* 89 * Raw interface to IP protocol. 90 */ 91 92 /* 93 * Initialize raw connection block q. 94 */ 95 void 96 rip_init() 97 { 98 LIST_INIT(&ripcb); 99 ripcbinfo.listhead = &ripcb; 100 /* 101 * XXX We don't use the hash list for raw IP, but it's easier 102 * to allocate a one entry hash list than it is to check all 103 * over the place for hashbase == NULL. 104 */ 105 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 106 ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 107 ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb), 108 maxsockets, ZONE_INTERRUPT, 0); 109 } 110 111 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 112 /* 113 * Setup generic address and protocol structures 114 * for raw_input routine, then pass them along with 115 * mbuf chain. 116 */ 117 void 118 rip_input(m, off) 119 struct mbuf *m; 120 int off; 121 { 122 register struct ip *ip = mtod(m, struct ip *); 123 register struct inpcb *inp; 124 struct inpcb *last = 0; 125 struct mbuf *opts = 0; 126 int proto = ip->ip_p; 127 128 ripsrc.sin_addr = ip->ip_src; 129 LIST_FOREACH(inp, &ripcb, inp_list) { 130 #ifdef INET6 131 if ((inp->inp_vflag & INP_IPV4) == 0) 132 continue; 133 #endif 134 if (inp->inp_ip_p && inp->inp_ip_p != proto) 135 continue; 136 if (inp->inp_laddr.s_addr && 137 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 138 continue; 139 if (inp->inp_faddr.s_addr && 140 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 141 continue; 142 if (last) { 143 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 144 145 #ifdef IPSEC 146 /* check AH/ESP integrity. */ 147 if (n && ipsec4_in_reject_so(n, last->inp_socket)) { 148 m_freem(n); 149 ipsecstat.in_polvio++; 150 /* do not inject data to pcb */ 151 } else 152 #endif /*IPSEC*/ 153 if (n) { 154 if (last->inp_flags & INP_CONTROLOPTS || 155 last->inp_socket->so_options & SO_TIMESTAMP) 156 ip_savecontrol(last, &opts, ip, n); 157 if (sbappendaddr(&last->inp_socket->so_rcv, 158 (struct sockaddr *)&ripsrc, n, 159 opts) == 0) { 160 /* should notify about lost packet */ 161 m_freem(n); 162 if (opts) 163 m_freem(opts); 164 } else 165 sorwakeup(last->inp_socket); 166 opts = 0; 167 } 168 } 169 last = inp; 170 } 171 #ifdef IPSEC 172 /* check AH/ESP integrity. */ 173 if (last && ipsec4_in_reject_so(m, last->inp_socket)) { 174 m_freem(m); 175 ipsecstat.in_polvio++; 176 ipstat.ips_delivered--; 177 /* do not inject data to pcb */ 178 } else 179 #endif /*IPSEC*/ 180 if (last) { 181 if (last->inp_flags & INP_CONTROLOPTS || 182 last->inp_socket->so_options & SO_TIMESTAMP) 183 ip_savecontrol(last, &opts, ip, m); 184 if (sbappendaddr(&last->inp_socket->so_rcv, 185 (struct sockaddr *)&ripsrc, m, opts) == 0) { 186 m_freem(m); 187 if (opts) 188 m_freem(opts); 189 } else 190 sorwakeup(last->inp_socket); 191 } else { 192 m_freem(m); 193 ipstat.ips_noproto++; 194 ipstat.ips_delivered--; 195 } 196 } 197 198 /* 199 * Generate IP header and pass packet to ip_output. 200 * Tack on options user may have setup with control call. 201 */ 202 int 203 rip_output(m, so, dst) 204 struct mbuf *m; 205 struct socket *so; 206 u_long dst; 207 { 208 register struct ip *ip; 209 register struct inpcb *inp = sotoinpcb(so); 210 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 211 212 /* 213 * If the user handed us a complete IP packet, use it. 214 * Otherwise, allocate an mbuf for a header and fill it in. 215 */ 216 if ((inp->inp_flags & INP_HDRINCL) == 0) { 217 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 218 m_freem(m); 219 return(EMSGSIZE); 220 } 221 M_PREPEND(m, sizeof(struct ip), M_TRYWAIT); 222 ip = mtod(m, struct ip *); 223 ip->ip_tos = inp->inp_ip_tos; 224 ip->ip_off = 0; 225 ip->ip_p = inp->inp_ip_p; 226 ip->ip_len = m->m_pkthdr.len; 227 ip->ip_src = inp->inp_laddr; 228 ip->ip_dst.s_addr = dst; 229 ip->ip_ttl = inp->inp_ip_ttl; 230 } else { 231 if (m->m_pkthdr.len > IP_MAXPACKET) { 232 m_freem(m); 233 return(EMSGSIZE); 234 } 235 ip = mtod(m, struct ip *); 236 /* don't allow both user specified and setsockopt options, 237 and don't allow packet length sizes that will crash */ 238 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 239 && inp->inp_options) 240 || (ip->ip_len > m->m_pkthdr.len) 241 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 242 m_freem(m); 243 return EINVAL; 244 } 245 if (ip->ip_id == 0) 246 #ifdef RANDOM_IP_ID 247 ip->ip_id = ip_randomid(); 248 #else 249 ip->ip_id = htons(ip_id++); 250 #endif 251 /* XXX prevent ip_output from overwriting header fields */ 252 flags |= IP_RAWOUTPUT; 253 ipstat.ips_rawout++; 254 } 255 256 #ifdef IPSEC 257 if (ipsec_setsocket(m, so) != 0) { 258 m_freem(m); 259 return ENOBUFS; 260 } 261 #endif /*IPSEC*/ 262 263 return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 264 inp->inp_moptions)); 265 } 266 267 /* 268 * Raw IP socket option processing. 269 */ 270 int 271 rip_ctloutput(so, sopt) 272 struct socket *so; 273 struct sockopt *sopt; 274 { 275 struct inpcb *inp = sotoinpcb(so); 276 int error, optval; 277 278 if (sopt->sopt_level != IPPROTO_IP) 279 return (EINVAL); 280 281 error = 0; 282 283 switch (sopt->sopt_dir) { 284 case SOPT_GET: 285 switch (sopt->sopt_name) { 286 case IP_HDRINCL: 287 optval = inp->inp_flags & INP_HDRINCL; 288 error = sooptcopyout(sopt, &optval, sizeof optval); 289 break; 290 291 case IP_FW_ADD: 292 case IP_FW_GET: 293 if (ip_fw_ctl_ptr == NULL) 294 error = ENOPROTOOPT; 295 else 296 error = ip_fw_ctl_ptr(sopt); 297 break; 298 299 case IP_DUMMYNET_GET: 300 if (ip_dn_ctl_ptr == NULL) 301 error = ENOPROTOOPT; 302 else 303 error = ip_dn_ctl_ptr(sopt); 304 break ; 305 306 case MRT_INIT: 307 case MRT_DONE: 308 case MRT_ADD_VIF: 309 case MRT_DEL_VIF: 310 case MRT_ADD_MFC: 311 case MRT_DEL_MFC: 312 case MRT_VERSION: 313 case MRT_ASSERT: 314 error = ip_mrouter_get(so, sopt); 315 break; 316 317 default: 318 error = ip_ctloutput(so, sopt); 319 break; 320 } 321 break; 322 323 case SOPT_SET: 324 switch (sopt->sopt_name) { 325 case IP_HDRINCL: 326 error = sooptcopyin(sopt, &optval, sizeof optval, 327 sizeof optval); 328 if (error) 329 break; 330 if (optval) 331 inp->inp_flags |= INP_HDRINCL; 332 else 333 inp->inp_flags &= ~INP_HDRINCL; 334 break; 335 336 case IP_FW_ADD: 337 case IP_FW_DEL: 338 case IP_FW_FLUSH: 339 case IP_FW_ZERO: 340 case IP_FW_RESETLOG: 341 if (ip_fw_ctl_ptr == 0) 342 error = ENOPROTOOPT; 343 else 344 error = ip_fw_ctl_ptr(sopt); 345 break; 346 347 case IP_DUMMYNET_CONFIGURE: 348 case IP_DUMMYNET_DEL: 349 case IP_DUMMYNET_FLUSH: 350 if (ip_dn_ctl_ptr == NULL) 351 error = ENOPROTOOPT ; 352 else 353 error = ip_dn_ctl_ptr(sopt); 354 break ; 355 356 case IP_RSVP_ON: 357 error = ip_rsvp_init(so); 358 break; 359 360 case IP_RSVP_OFF: 361 error = ip_rsvp_done(); 362 break; 363 364 /* XXX - should be combined */ 365 case IP_RSVP_VIF_ON: 366 error = ip_rsvp_vif_init(so, sopt); 367 break; 368 369 case IP_RSVP_VIF_OFF: 370 error = ip_rsvp_vif_done(so, sopt); 371 break; 372 373 case MRT_INIT: 374 case MRT_DONE: 375 case MRT_ADD_VIF: 376 case MRT_DEL_VIF: 377 case MRT_ADD_MFC: 378 case MRT_DEL_MFC: 379 case MRT_VERSION: 380 case MRT_ASSERT: 381 error = ip_mrouter_set(so, sopt); 382 break; 383 384 default: 385 error = ip_ctloutput(so, sopt); 386 break; 387 } 388 break; 389 } 390 391 return (error); 392 } 393 394 /* 395 * This function exists solely to receive the PRC_IFDOWN messages which 396 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 397 * and calls in_ifadown() to remove all routes corresponding to that address. 398 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 399 * interface routes. 400 */ 401 void 402 rip_ctlinput(cmd, sa, vip) 403 int cmd; 404 struct sockaddr *sa; 405 void *vip; 406 { 407 struct in_ifaddr *ia; 408 struct ifnet *ifp; 409 int err; 410 int flags; 411 412 switch (cmd) { 413 case PRC_IFDOWN: 414 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 415 if (ia->ia_ifa.ifa_addr == sa 416 && (ia->ia_flags & IFA_ROUTE)) { 417 /* 418 * in_ifscrub kills the interface route. 419 */ 420 in_ifscrub(ia->ia_ifp, ia); 421 /* 422 * in_ifadown gets rid of all the rest of 423 * the routes. This is not quite the right 424 * thing to do, but at least if we are running 425 * a routing process they will come back. 426 */ 427 in_ifadown(&ia->ia_ifa, 0); 428 break; 429 } 430 } 431 break; 432 433 case PRC_IFUP: 434 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 435 if (ia->ia_ifa.ifa_addr == sa) 436 break; 437 } 438 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 439 return; 440 flags = RTF_UP; 441 ifp = ia->ia_ifa.ifa_ifp; 442 443 if ((ifp->if_flags & IFF_LOOPBACK) 444 || (ifp->if_flags & IFF_POINTOPOINT)) 445 flags |= RTF_HOST; 446 447 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 448 if (err == 0) 449 ia->ia_flags |= IFA_ROUTE; 450 break; 451 } 452 } 453 454 u_long rip_sendspace = RIPSNDQ; 455 u_long rip_recvspace = RIPRCVQ; 456 457 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 458 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 459 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 460 &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); 461 462 static int 463 rip_attach(struct socket *so, int proto, struct thread *td) 464 { 465 struct inpcb *inp; 466 int error, s; 467 468 inp = sotoinpcb(so); 469 if (inp) 470 panic("rip_attach"); 471 if (td && (error = suser_td(td)) != 0) 472 return error; 473 474 error = soreserve(so, rip_sendspace, rip_recvspace); 475 if (error) 476 return error; 477 s = splnet(); 478 error = in_pcballoc(so, &ripcbinfo, td); 479 splx(s); 480 if (error) 481 return error; 482 inp = (struct inpcb *)so->so_pcb; 483 inp->inp_vflag |= INP_IPV4; 484 inp->inp_ip_p = proto; 485 inp->inp_ip_ttl = ip_defttl; 486 return 0; 487 } 488 489 static int 490 rip_detach(struct socket *so) 491 { 492 struct inpcb *inp; 493 494 inp = sotoinpcb(so); 495 if (inp == 0) 496 panic("rip_detach"); 497 if (so == ip_mrouter) 498 ip_mrouter_done(); 499 ip_rsvp_force_done(so); 500 if (so == ip_rsvpd) 501 ip_rsvp_done(); 502 in_pcbdetach(inp); 503 return 0; 504 } 505 506 static int 507 rip_abort(struct socket *so) 508 { 509 soisdisconnected(so); 510 return rip_detach(so); 511 } 512 513 static int 514 rip_disconnect(struct socket *so) 515 { 516 if ((so->so_state & SS_ISCONNECTED) == 0) 517 return ENOTCONN; 518 return rip_abort(so); 519 } 520 521 static int 522 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 523 { 524 struct inpcb *inp = sotoinpcb(so); 525 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 526 527 if (nam->sa_len != sizeof(*addr)) 528 return EINVAL; 529 530 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 531 (addr->sin_family != AF_IMPLINK)) || 532 (addr->sin_addr.s_addr && 533 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 534 return EADDRNOTAVAIL; 535 inp->inp_laddr = addr->sin_addr; 536 return 0; 537 } 538 539 static int 540 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 541 { 542 struct inpcb *inp = sotoinpcb(so); 543 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 544 545 if (nam->sa_len != sizeof(*addr)) 546 return EINVAL; 547 if (TAILQ_EMPTY(&ifnet)) 548 return EADDRNOTAVAIL; 549 if ((addr->sin_family != AF_INET) && 550 (addr->sin_family != AF_IMPLINK)) 551 return EAFNOSUPPORT; 552 inp->inp_faddr = addr->sin_addr; 553 soisconnected(so); 554 return 0; 555 } 556 557 static int 558 rip_shutdown(struct socket *so) 559 { 560 socantsendmore(so); 561 return 0; 562 } 563 564 static int 565 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 566 struct mbuf *control, struct thread *td) 567 { 568 struct inpcb *inp = sotoinpcb(so); 569 register u_long dst; 570 571 if (so->so_state & SS_ISCONNECTED) { 572 if (nam) { 573 m_freem(m); 574 return EISCONN; 575 } 576 dst = inp->inp_faddr.s_addr; 577 } else { 578 if (nam == NULL) { 579 m_freem(m); 580 return ENOTCONN; 581 } 582 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 583 } 584 return rip_output(m, so, dst); 585 } 586 587 static int 588 rip_pcblist(SYSCTL_HANDLER_ARGS) 589 { 590 int error, i, n, s; 591 struct inpcb *inp, **inp_list; 592 inp_gen_t gencnt; 593 struct xinpgen xig; 594 595 /* 596 * The process of preparing the TCB list is too time-consuming and 597 * resource-intensive to repeat twice on every request. 598 */ 599 if (req->oldptr == 0) { 600 n = ripcbinfo.ipi_count; 601 req->oldidx = 2 * (sizeof xig) 602 + (n + n/8) * sizeof(struct xinpcb); 603 return 0; 604 } 605 606 if (req->newptr != 0) 607 return EPERM; 608 609 /* 610 * OK, now we're committed to doing something. 611 */ 612 s = splnet(); 613 gencnt = ripcbinfo.ipi_gencnt; 614 n = ripcbinfo.ipi_count; 615 splx(s); 616 617 xig.xig_len = sizeof xig; 618 xig.xig_count = n; 619 xig.xig_gen = gencnt; 620 xig.xig_sogen = so_gencnt; 621 error = SYSCTL_OUT(req, &xig, sizeof xig); 622 if (error) 623 return error; 624 625 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 626 if (inp_list == 0) 627 return ENOMEM; 628 629 s = splnet(); 630 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 631 inp = LIST_NEXT(inp, inp_list)) { 632 if (inp->inp_gencnt <= gencnt) { 633 if (cr_cansee(req->p->p_ucred, 634 inp->inp_socket->so_cred)) 635 continue; 636 inp_list[i++] = inp; 637 } 638 } 639 splx(s); 640 n = i; 641 642 error = 0; 643 for (i = 0; i < n; i++) { 644 inp = inp_list[i]; 645 if (inp->inp_gencnt <= gencnt) { 646 struct xinpcb xi; 647 xi.xi_len = sizeof xi; 648 /* XXX should avoid extra copy */ 649 bcopy(inp, &xi.xi_inp, sizeof *inp); 650 if (inp->inp_socket) 651 sotoxsocket(inp->inp_socket, &xi.xi_socket); 652 error = SYSCTL_OUT(req, &xi, sizeof xi); 653 } 654 } 655 if (!error) { 656 /* 657 * Give the user an updated idea of our state. 658 * If the generation differs from what we told 659 * her before, she knows that something happened 660 * while we were processing this request, and it 661 * might be necessary to retry. 662 */ 663 s = splnet(); 664 xig.xig_gen = ripcbinfo.ipi_gencnt; 665 xig.xig_sogen = so_gencnt; 666 xig.xig_count = ripcbinfo.ipi_count; 667 splx(s); 668 error = SYSCTL_OUT(req, &xig, sizeof xig); 669 } 670 free(inp_list, M_TEMP); 671 return error; 672 } 673 674 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 675 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 676 677 struct pr_usrreqs rip_usrreqs = { 678 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 679 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 680 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 681 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 682 in_setsockaddr, sosend, soreceive, sopoll 683 }; 684