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