1 /*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 4. Neither the name of the University nor the names of its contributors 15 * may be used to endorse or promote products derived from this software 16 * without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 31 */ 32 33 #include <sys/cdefs.h> 34 __FBSDID("$FreeBSD$"); 35 36 #include "opt_inet6.h" 37 #include "opt_ipsec.h" 38 39 #include <sys/param.h> 40 #include <sys/jail.h> 41 #include <sys/kernel.h> 42 #include <sys/lock.h> 43 #include <sys/malloc.h> 44 #include <sys/mbuf.h> 45 #include <sys/priv.h> 46 #include <sys/proc.h> 47 #include <sys/protosw.h> 48 #include <sys/rwlock.h> 49 #include <sys/signalvar.h> 50 #include <sys/socket.h> 51 #include <sys/socketvar.h> 52 #include <sys/sx.h> 53 #include <sys/sysctl.h> 54 #include <sys/systm.h> 55 56 #include <vm/uma.h> 57 58 #include <net/if.h> 59 #include <net/route.h> 60 #include <net/vnet.h> 61 62 #include <netinet/in.h> 63 #include <netinet/in_systm.h> 64 #include <netinet/in_pcb.h> 65 #include <netinet/in_var.h> 66 #include <netinet/ip.h> 67 #include <netinet/ip_var.h> 68 #include <netinet/ip_mroute.h> 69 70 #ifdef IPSEC 71 #include <netipsec/ipsec.h> 72 #endif /*IPSEC*/ 73 74 #include <security/mac/mac_framework.h> 75 76 VNET_DEFINE(struct inpcbhead, ripcb); 77 VNET_DEFINE(struct inpcbinfo, ripcbinfo); 78 79 #define V_ripcb VNET(ripcb) 80 #define V_ripcbinfo VNET(ripcbinfo) 81 82 /* 83 * Control and data hooks for ipfw and dummynet. 84 * The data hooks are not used here but it is convenient 85 * to keep them all in one place. 86 */ 87 int (*ip_fw_ctl_ptr)(struct sockopt *) = NULL; 88 int (*ip_dn_ctl_ptr)(struct sockopt *) = NULL; 89 int (*ip_fw_chk_ptr)(struct ip_fw_args *args) = NULL; 90 int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa) = NULL; 91 92 /* 93 * Hooks for multicast routing. They all default to NULL, so leave them not 94 * initialized and rely on BSS being set to 0. 95 */ 96 97 /* 98 * The socket used to communicate with the multicast routing daemon. 99 */ 100 VNET_DEFINE(struct socket *, ip_mrouter); 101 102 /* 103 * The various mrouter and rsvp functions. 104 */ 105 int (*ip_mrouter_set)(struct socket *, struct sockopt *); 106 int (*ip_mrouter_get)(struct socket *, struct sockopt *); 107 int (*ip_mrouter_done)(void); 108 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 109 struct ip_moptions *); 110 int (*mrt_ioctl)(u_long, caddr_t, int); 111 int (*legal_vif_num)(int); 112 u_long (*ip_mcast_src)(int); 113 114 void (*rsvp_input_p)(struct mbuf *m, int off); 115 int (*ip_rsvp_vif)(struct socket *, struct sockopt *); 116 void (*ip_rsvp_force_done)(struct socket *); 117 118 /* 119 * Hash functions 120 */ 121 122 #define INP_PCBHASH_RAW_SIZE 256 123 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \ 124 (((proto) + (laddr) + (faddr)) % (mask) + 1) 125 126 static void 127 rip_inshash(struct inpcb *inp) 128 { 129 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; 130 struct inpcbhead *pcbhash; 131 int hash; 132 133 INP_INFO_WLOCK_ASSERT(pcbinfo); 134 INP_WLOCK_ASSERT(inp); 135 136 if (inp->inp_ip_p != 0 && 137 inp->inp_laddr.s_addr != INADDR_ANY && 138 inp->inp_faddr.s_addr != INADDR_ANY) { 139 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr, 140 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask); 141 } else 142 hash = 0; 143 pcbhash = &pcbinfo->ipi_hashbase[hash]; 144 LIST_INSERT_HEAD(pcbhash, inp, inp_hash); 145 } 146 147 static void 148 rip_delhash(struct inpcb *inp) 149 { 150 151 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 152 INP_WLOCK_ASSERT(inp); 153 154 LIST_REMOVE(inp, inp_hash); 155 } 156 157 /* 158 * Raw interface to IP protocol. 159 */ 160 161 /* 162 * Initialize raw connection block q. 163 */ 164 static void 165 rip_zone_change(void *tag) 166 { 167 168 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets); 169 } 170 171 static int 172 rip_inpcb_init(void *mem, int size, int flags) 173 { 174 struct inpcb *inp = mem; 175 176 INP_LOCK_INIT(inp, "inp", "rawinp"); 177 return (0); 178 } 179 180 void 181 rip_init(void) 182 { 183 184 INP_INFO_LOCK_INIT(&V_ripcbinfo, "rip"); 185 LIST_INIT(&V_ripcb); 186 #ifdef VIMAGE 187 V_ripcbinfo.ipi_vnet = curvnet; 188 #endif 189 V_ripcbinfo.ipi_listhead = &V_ripcb; 190 V_ripcbinfo.ipi_hashbase = 191 hashinit(INP_PCBHASH_RAW_SIZE, M_PCB, &V_ripcbinfo.ipi_hashmask); 192 V_ripcbinfo.ipi_porthashbase = 193 hashinit(1, M_PCB, &V_ripcbinfo.ipi_porthashmask); 194 V_ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb), 195 NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 196 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets); 197 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL, 198 EVENTHANDLER_PRI_ANY); 199 } 200 201 #ifdef VIMAGE 202 void 203 rip_destroy(void) 204 { 205 206 hashdestroy(V_ripcbinfo.ipi_hashbase, M_PCB, 207 V_ripcbinfo.ipi_hashmask); 208 hashdestroy(V_ripcbinfo.ipi_porthashbase, M_PCB, 209 V_ripcbinfo.ipi_porthashmask); 210 } 211 #endif 212 213 static int 214 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n, 215 struct sockaddr_in *ripsrc) 216 { 217 int policyfail = 0; 218 219 INP_RLOCK_ASSERT(last); 220 221 #ifdef IPSEC 222 /* check AH/ESP integrity. */ 223 if (ipsec4_in_reject(n, last)) { 224 policyfail = 1; 225 } 226 #endif /* IPSEC */ 227 #ifdef MAC 228 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0) 229 policyfail = 1; 230 #endif 231 /* Check the minimum TTL for socket. */ 232 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl) 233 policyfail = 1; 234 if (!policyfail) { 235 struct mbuf *opts = NULL; 236 struct socket *so; 237 238 so = last->inp_socket; 239 if ((last->inp_flags & INP_CONTROLOPTS) || 240 (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 241 ip_savecontrol(last, &opts, ip, n); 242 SOCKBUF_LOCK(&so->so_rcv); 243 if (sbappendaddr_locked(&so->so_rcv, 244 (struct sockaddr *)ripsrc, n, opts) == 0) { 245 /* should notify about lost packet */ 246 m_freem(n); 247 if (opts) 248 m_freem(opts); 249 SOCKBUF_UNLOCK(&so->so_rcv); 250 } else 251 sorwakeup_locked(so); 252 } else 253 m_freem(n); 254 return (policyfail); 255 } 256 257 /* 258 * Setup generic address and protocol structures for raw_input routine, then 259 * pass them along with mbuf chain. 260 */ 261 void 262 rip_input(struct mbuf *m, int off) 263 { 264 struct ifnet *ifp; 265 struct ip *ip = mtod(m, struct ip *); 266 int proto = ip->ip_p; 267 struct inpcb *inp, *last; 268 struct sockaddr_in ripsrc; 269 int hash; 270 271 bzero(&ripsrc, sizeof(ripsrc)); 272 ripsrc.sin_len = sizeof(ripsrc); 273 ripsrc.sin_family = AF_INET; 274 ripsrc.sin_addr = ip->ip_src; 275 last = NULL; 276 277 ifp = m->m_pkthdr.rcvif; 278 279 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr, 280 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask); 281 INP_INFO_RLOCK(&V_ripcbinfo); 282 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) { 283 if (inp->inp_ip_p != proto) 284 continue; 285 #ifdef INET6 286 /* XXX inp locking */ 287 if ((inp->inp_vflag & INP_IPV4) == 0) 288 continue; 289 #endif 290 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 291 continue; 292 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr) 293 continue; 294 if (jailed(inp->inp_cred)) { 295 /* 296 * XXX: If faddr was bound to multicast group, 297 * jailed raw socket will drop datagram. 298 */ 299 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0) 300 continue; 301 } 302 if (last != NULL) { 303 struct mbuf *n; 304 305 n = m_copy(m, 0, (int)M_COPYALL); 306 if (n != NULL) 307 (void) rip_append(last, ip, n, &ripsrc); 308 /* XXX count dropped packet */ 309 INP_RUNLOCK(last); 310 } 311 INP_RLOCK(inp); 312 last = inp; 313 } 314 LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) { 315 if (inp->inp_ip_p && inp->inp_ip_p != proto) 316 continue; 317 #ifdef INET6 318 /* XXX inp locking */ 319 if ((inp->inp_vflag & INP_IPV4) == 0) 320 continue; 321 #endif 322 if (!in_nullhost(inp->inp_laddr) && 323 !in_hosteq(inp->inp_laddr, ip->ip_dst)) 324 continue; 325 if (!in_nullhost(inp->inp_faddr) && 326 !in_hosteq(inp->inp_faddr, ip->ip_src)) 327 continue; 328 if (jailed(inp->inp_cred)) { 329 /* 330 * Allow raw socket in jail to receive multicast; 331 * assume process had PRIV_NETINET_RAW at attach, 332 * and fall through into normal filter path if so. 333 */ 334 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 335 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0) 336 continue; 337 } 338 /* 339 * If this raw socket has multicast state, and we 340 * have received a multicast, check if this socket 341 * should receive it, as multicast filtering is now 342 * the responsibility of the transport layer. 343 */ 344 if (inp->inp_moptions != NULL && 345 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 346 struct sockaddr_in group; 347 int blocked; 348 349 bzero(&group, sizeof(struct sockaddr_in)); 350 group.sin_len = sizeof(struct sockaddr_in); 351 group.sin_family = AF_INET; 352 group.sin_addr = ip->ip_dst; 353 354 blocked = imo_multi_filter(inp->inp_moptions, ifp, 355 (struct sockaddr *)&group, 356 (struct sockaddr *)&ripsrc); 357 if (blocked != MCAST_PASS) { 358 IPSTAT_INC(ips_notmember); 359 continue; 360 } 361 } 362 if (last != NULL) { 363 struct mbuf *n; 364 365 n = m_copy(m, 0, (int)M_COPYALL); 366 if (n != NULL) 367 (void) rip_append(last, ip, n, &ripsrc); 368 /* XXX count dropped packet */ 369 INP_RUNLOCK(last); 370 } 371 INP_RLOCK(inp); 372 last = inp; 373 } 374 INP_INFO_RUNLOCK(&V_ripcbinfo); 375 if (last != NULL) { 376 if (rip_append(last, ip, m, &ripsrc) != 0) 377 IPSTAT_INC(ips_delivered); 378 INP_RUNLOCK(last); 379 } else { 380 m_freem(m); 381 IPSTAT_INC(ips_noproto); 382 IPSTAT_DEC(ips_delivered); 383 } 384 } 385 386 /* 387 * Generate IP header and pass packet to ip_output. Tack on options user may 388 * have setup with control call. 389 */ 390 int 391 rip_output(struct mbuf *m, struct socket *so, u_long dst) 392 { 393 struct ip *ip; 394 int error; 395 struct inpcb *inp = sotoinpcb(so); 396 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 397 IP_ALLOWBROADCAST; 398 399 /* 400 * If the user handed us a complete IP packet, use it. Otherwise, 401 * allocate an mbuf for a header and fill it in. 402 */ 403 if ((inp->inp_flags & INP_HDRINCL) == 0) { 404 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 405 m_freem(m); 406 return(EMSGSIZE); 407 } 408 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 409 if (m == NULL) 410 return(ENOBUFS); 411 412 INP_RLOCK(inp); 413 ip = mtod(m, struct ip *); 414 ip->ip_tos = inp->inp_ip_tos; 415 if (inp->inp_flags & INP_DONTFRAG) 416 ip->ip_off = IP_DF; 417 else 418 ip->ip_off = 0; 419 ip->ip_p = inp->inp_ip_p; 420 ip->ip_len = m->m_pkthdr.len; 421 ip->ip_src = inp->inp_laddr; 422 error = prison_get_ip4(inp->inp_cred, &ip->ip_src); 423 if (error != 0) { 424 INP_RUNLOCK(inp); 425 m_freem(m); 426 return (error); 427 } 428 ip->ip_dst.s_addr = dst; 429 ip->ip_ttl = inp->inp_ip_ttl; 430 } else { 431 if (m->m_pkthdr.len > IP_MAXPACKET) { 432 m_freem(m); 433 return(EMSGSIZE); 434 } 435 INP_RLOCK(inp); 436 ip = mtod(m, struct ip *); 437 error = prison_check_ip4(inp->inp_cred, &ip->ip_src); 438 if (error != 0) { 439 INP_RUNLOCK(inp); 440 m_freem(m); 441 return (error); 442 } 443 444 /* 445 * Don't allow both user specified and setsockopt options, 446 * and don't allow packet length sizes that will crash. 447 */ 448 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) 449 || (ip->ip_len > m->m_pkthdr.len) 450 || (ip->ip_len < (ip->ip_hl << 2))) { 451 INP_RUNLOCK(inp); 452 m_freem(m); 453 return (EINVAL); 454 } 455 if (ip->ip_id == 0) 456 ip->ip_id = ip_newid(); 457 458 /* 459 * XXX prevent ip_output from overwriting header fields. 460 */ 461 flags |= IP_RAWOUTPUT; 462 IPSTAT_INC(ips_rawout); 463 } 464 465 if (inp->inp_flags & INP_ONESBCAST) 466 flags |= IP_SENDONES; 467 468 #ifdef MAC 469 mac_inpcb_create_mbuf(inp, m); 470 #endif 471 472 error = ip_output(m, inp->inp_options, NULL, flags, 473 inp->inp_moptions, inp); 474 INP_RUNLOCK(inp); 475 return (error); 476 } 477 478 /* 479 * Raw IP socket option processing. 480 * 481 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 482 * only be created by a privileged process, and as such, socket option 483 * operations to manage system properties on any raw socket were allowed to 484 * take place without explicit additional access control checks. However, 485 * raw sockets can now also be created in jail(), and therefore explicit 486 * checks are now required. Likewise, raw sockets can be used by a process 487 * after it gives up privilege, so some caution is required. For options 488 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 489 * performed in ip_ctloutput() and therefore no check occurs here. 490 * Unilaterally checking priv_check() here breaks normal IP socket option 491 * operations on raw sockets. 492 * 493 * When adding new socket options here, make sure to add access control 494 * checks here as necessary. 495 */ 496 int 497 rip_ctloutput(struct socket *so, struct sockopt *sopt) 498 { 499 struct inpcb *inp = sotoinpcb(so); 500 int error, optval; 501 502 if (sopt->sopt_level != IPPROTO_IP) { 503 if ((sopt->sopt_level == SOL_SOCKET) && 504 (sopt->sopt_name == SO_SETFIB)) { 505 inp->inp_inc.inc_fibnum = so->so_fibnum; 506 return (0); 507 } 508 return (EINVAL); 509 } 510 511 error = 0; 512 switch (sopt->sopt_dir) { 513 case SOPT_GET: 514 switch (sopt->sopt_name) { 515 case IP_HDRINCL: 516 optval = inp->inp_flags & INP_HDRINCL; 517 error = sooptcopyout(sopt, &optval, sizeof optval); 518 break; 519 520 case IP_FW_ADD: /* ADD actually returns the body... */ 521 case IP_FW_GET: 522 case IP_FW_TABLE_GETSIZE: 523 case IP_FW_TABLE_LIST: 524 case IP_FW_NAT_GET_CONFIG: 525 case IP_FW_NAT_GET_LOG: 526 if (ip_fw_ctl_ptr != NULL) 527 error = ip_fw_ctl_ptr(sopt); 528 else 529 error = ENOPROTOOPT; 530 break; 531 532 case IP_DUMMYNET_GET: 533 if (ip_dn_ctl_ptr != NULL) 534 error = ip_dn_ctl_ptr(sopt); 535 else 536 error = ENOPROTOOPT; 537 break ; 538 539 case MRT_INIT: 540 case MRT_DONE: 541 case MRT_ADD_VIF: 542 case MRT_DEL_VIF: 543 case MRT_ADD_MFC: 544 case MRT_DEL_MFC: 545 case MRT_VERSION: 546 case MRT_ASSERT: 547 case MRT_API_SUPPORT: 548 case MRT_API_CONFIG: 549 case MRT_ADD_BW_UPCALL: 550 case MRT_DEL_BW_UPCALL: 551 error = priv_check(curthread, PRIV_NETINET_MROUTE); 552 if (error != 0) 553 return (error); 554 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 555 EOPNOTSUPP; 556 break; 557 558 default: 559 error = ip_ctloutput(so, sopt); 560 break; 561 } 562 break; 563 564 case SOPT_SET: 565 switch (sopt->sopt_name) { 566 case IP_HDRINCL: 567 error = sooptcopyin(sopt, &optval, sizeof optval, 568 sizeof optval); 569 if (error) 570 break; 571 if (optval) 572 inp->inp_flags |= INP_HDRINCL; 573 else 574 inp->inp_flags &= ~INP_HDRINCL; 575 break; 576 577 case IP_FW_ADD: 578 case IP_FW_DEL: 579 case IP_FW_FLUSH: 580 case IP_FW_ZERO: 581 case IP_FW_RESETLOG: 582 case IP_FW_TABLE_ADD: 583 case IP_FW_TABLE_DEL: 584 case IP_FW_TABLE_FLUSH: 585 case IP_FW_NAT_CFG: 586 case IP_FW_NAT_DEL: 587 if (ip_fw_ctl_ptr != NULL) 588 error = ip_fw_ctl_ptr(sopt); 589 else 590 error = ENOPROTOOPT; 591 break; 592 593 case IP_DUMMYNET_CONFIGURE: 594 case IP_DUMMYNET_DEL: 595 case IP_DUMMYNET_FLUSH: 596 if (ip_dn_ctl_ptr != NULL) 597 error = ip_dn_ctl_ptr(sopt); 598 else 599 error = ENOPROTOOPT ; 600 break ; 601 602 case IP_RSVP_ON: 603 error = priv_check(curthread, PRIV_NETINET_MROUTE); 604 if (error != 0) 605 return (error); 606 error = ip_rsvp_init(so); 607 break; 608 609 case IP_RSVP_OFF: 610 error = priv_check(curthread, PRIV_NETINET_MROUTE); 611 if (error != 0) 612 return (error); 613 error = ip_rsvp_done(); 614 break; 615 616 case IP_RSVP_VIF_ON: 617 case IP_RSVP_VIF_OFF: 618 error = priv_check(curthread, PRIV_NETINET_MROUTE); 619 if (error != 0) 620 return (error); 621 error = ip_rsvp_vif ? 622 ip_rsvp_vif(so, sopt) : EINVAL; 623 break; 624 625 case MRT_INIT: 626 case MRT_DONE: 627 case MRT_ADD_VIF: 628 case MRT_DEL_VIF: 629 case MRT_ADD_MFC: 630 case MRT_DEL_MFC: 631 case MRT_VERSION: 632 case MRT_ASSERT: 633 case MRT_API_SUPPORT: 634 case MRT_API_CONFIG: 635 case MRT_ADD_BW_UPCALL: 636 case MRT_DEL_BW_UPCALL: 637 error = priv_check(curthread, PRIV_NETINET_MROUTE); 638 if (error != 0) 639 return (error); 640 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 641 EOPNOTSUPP; 642 break; 643 644 default: 645 error = ip_ctloutput(so, sopt); 646 break; 647 } 648 break; 649 } 650 651 return (error); 652 } 653 654 /* 655 * This function exists solely to receive the PRC_IFDOWN messages which are 656 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls 657 * in_ifadown() to remove all routes corresponding to that address. It also 658 * receives the PRC_IFUP messages from if_up() and reinstalls the interface 659 * routes. 660 */ 661 void 662 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 663 { 664 struct in_ifaddr *ia; 665 struct ifnet *ifp; 666 int err; 667 int flags; 668 669 switch (cmd) { 670 case PRC_IFDOWN: 671 IN_IFADDR_RLOCK(); 672 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 673 if (ia->ia_ifa.ifa_addr == sa 674 && (ia->ia_flags & IFA_ROUTE)) { 675 ifa_ref(&ia->ia_ifa); 676 IN_IFADDR_RUNLOCK(); 677 /* 678 * in_ifscrub kills the interface route. 679 */ 680 in_ifscrub(ia->ia_ifp, ia); 681 /* 682 * in_ifadown gets rid of all the rest of the 683 * routes. This is not quite the right thing 684 * to do, but at least if we are running a 685 * routing process they will come back. 686 */ 687 in_ifadown(&ia->ia_ifa, 0); 688 ifa_free(&ia->ia_ifa); 689 break; 690 } 691 } 692 if (ia == NULL) /* If ia matched, already unlocked. */ 693 IN_IFADDR_RUNLOCK(); 694 break; 695 696 case PRC_IFUP: 697 IN_IFADDR_RLOCK(); 698 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) { 699 if (ia->ia_ifa.ifa_addr == sa) 700 break; 701 } 702 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) { 703 IN_IFADDR_RUNLOCK(); 704 return; 705 } 706 ifa_ref(&ia->ia_ifa); 707 IN_IFADDR_RUNLOCK(); 708 flags = RTF_UP; 709 ifp = ia->ia_ifa.ifa_ifp; 710 711 if ((ifp->if_flags & IFF_LOOPBACK) 712 || (ifp->if_flags & IFF_POINTOPOINT)) 713 flags |= RTF_HOST; 714 715 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 716 if (err == 0) 717 ia->ia_flags |= IFA_ROUTE; 718 ifa_free(&ia->ia_ifa); 719 break; 720 } 721 } 722 723 u_long rip_sendspace = 9216; 724 u_long rip_recvspace = 9216; 725 726 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 727 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 728 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 729 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 730 731 static int 732 rip_attach(struct socket *so, int proto, struct thread *td) 733 { 734 struct inpcb *inp; 735 int error; 736 737 inp = sotoinpcb(so); 738 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 739 740 error = priv_check(td, PRIV_NETINET_RAW); 741 if (error) 742 return (error); 743 if (proto >= IPPROTO_MAX || proto < 0) 744 return EPROTONOSUPPORT; 745 error = soreserve(so, rip_sendspace, rip_recvspace); 746 if (error) 747 return (error); 748 INP_INFO_WLOCK(&V_ripcbinfo); 749 error = in_pcballoc(so, &V_ripcbinfo); 750 if (error) { 751 INP_INFO_WUNLOCK(&V_ripcbinfo); 752 return (error); 753 } 754 inp = (struct inpcb *)so->so_pcb; 755 inp->inp_vflag |= INP_IPV4; 756 inp->inp_ip_p = proto; 757 inp->inp_ip_ttl = V_ip_defttl; 758 rip_inshash(inp); 759 INP_INFO_WUNLOCK(&V_ripcbinfo); 760 INP_WUNLOCK(inp); 761 return (0); 762 } 763 764 static void 765 rip_detach(struct socket *so) 766 { 767 struct inpcb *inp; 768 769 inp = sotoinpcb(so); 770 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 771 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY, 772 ("rip_detach: not closed")); 773 774 INP_INFO_WLOCK(&V_ripcbinfo); 775 INP_WLOCK(inp); 776 rip_delhash(inp); 777 if (so == V_ip_mrouter && ip_mrouter_done) 778 ip_mrouter_done(); 779 if (ip_rsvp_force_done) 780 ip_rsvp_force_done(so); 781 if (so == V_ip_rsvpd) 782 ip_rsvp_done(); 783 in_pcbdetach(inp); 784 in_pcbfree(inp); 785 INP_INFO_WUNLOCK(&V_ripcbinfo); 786 } 787 788 static void 789 rip_dodisconnect(struct socket *so, struct inpcb *inp) 790 { 791 792 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo); 793 INP_WLOCK_ASSERT(inp); 794 795 rip_delhash(inp); 796 inp->inp_faddr.s_addr = INADDR_ANY; 797 rip_inshash(inp); 798 SOCK_LOCK(so); 799 so->so_state &= ~SS_ISCONNECTED; 800 SOCK_UNLOCK(so); 801 } 802 803 static void 804 rip_abort(struct socket *so) 805 { 806 struct inpcb *inp; 807 808 inp = sotoinpcb(so); 809 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 810 811 INP_INFO_WLOCK(&V_ripcbinfo); 812 INP_WLOCK(inp); 813 rip_dodisconnect(so, inp); 814 INP_WUNLOCK(inp); 815 INP_INFO_WUNLOCK(&V_ripcbinfo); 816 } 817 818 static void 819 rip_close(struct socket *so) 820 { 821 struct inpcb *inp; 822 823 inp = sotoinpcb(so); 824 KASSERT(inp != NULL, ("rip_close: inp == NULL")); 825 826 INP_INFO_WLOCK(&V_ripcbinfo); 827 INP_WLOCK(inp); 828 rip_dodisconnect(so, inp); 829 INP_WUNLOCK(inp); 830 INP_INFO_WUNLOCK(&V_ripcbinfo); 831 } 832 833 static int 834 rip_disconnect(struct socket *so) 835 { 836 struct inpcb *inp; 837 838 if ((so->so_state & SS_ISCONNECTED) == 0) 839 return (ENOTCONN); 840 841 inp = sotoinpcb(so); 842 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 843 844 INP_INFO_WLOCK(&V_ripcbinfo); 845 INP_WLOCK(inp); 846 rip_dodisconnect(so, inp); 847 INP_WUNLOCK(inp); 848 INP_INFO_WUNLOCK(&V_ripcbinfo); 849 return (0); 850 } 851 852 static int 853 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 854 { 855 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 856 struct inpcb *inp; 857 int error; 858 859 if (nam->sa_len != sizeof(*addr)) 860 return (EINVAL); 861 862 error = prison_check_ip4(td->td_ucred, &addr->sin_addr); 863 if (error != 0) 864 return (error); 865 866 inp = sotoinpcb(so); 867 KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 868 869 if (TAILQ_EMPTY(&V_ifnet) || 870 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 871 (addr->sin_addr.s_addr && 872 (inp->inp_flags & INP_BINDANY) == 0 && 873 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0)) 874 return (EADDRNOTAVAIL); 875 876 INP_INFO_WLOCK(&V_ripcbinfo); 877 INP_WLOCK(inp); 878 rip_delhash(inp); 879 inp->inp_laddr = addr->sin_addr; 880 rip_inshash(inp); 881 INP_WUNLOCK(inp); 882 INP_INFO_WUNLOCK(&V_ripcbinfo); 883 return (0); 884 } 885 886 static int 887 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 888 { 889 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 890 struct inpcb *inp; 891 892 if (nam->sa_len != sizeof(*addr)) 893 return (EINVAL); 894 if (TAILQ_EMPTY(&V_ifnet)) 895 return (EADDRNOTAVAIL); 896 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 897 return (EAFNOSUPPORT); 898 899 inp = sotoinpcb(so); 900 KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 901 902 INP_INFO_WLOCK(&V_ripcbinfo); 903 INP_WLOCK(inp); 904 rip_delhash(inp); 905 inp->inp_faddr = addr->sin_addr; 906 rip_inshash(inp); 907 soisconnected(so); 908 INP_WUNLOCK(inp); 909 INP_INFO_WUNLOCK(&V_ripcbinfo); 910 return (0); 911 } 912 913 static int 914 rip_shutdown(struct socket *so) 915 { 916 struct inpcb *inp; 917 918 inp = sotoinpcb(so); 919 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 920 921 INP_WLOCK(inp); 922 socantsendmore(so); 923 INP_WUNLOCK(inp); 924 return (0); 925 } 926 927 static int 928 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 929 struct mbuf *control, struct thread *td) 930 { 931 struct inpcb *inp; 932 u_long dst; 933 934 inp = sotoinpcb(so); 935 KASSERT(inp != NULL, ("rip_send: inp == NULL")); 936 937 /* 938 * Note: 'dst' reads below are unlocked. 939 */ 940 if (so->so_state & SS_ISCONNECTED) { 941 if (nam) { 942 m_freem(m); 943 return (EISCONN); 944 } 945 dst = inp->inp_faddr.s_addr; /* Unlocked read. */ 946 } else { 947 if (nam == NULL) { 948 m_freem(m); 949 return (ENOTCONN); 950 } 951 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 952 } 953 return (rip_output(m, so, dst)); 954 } 955 956 static int 957 rip_pcblist(SYSCTL_HANDLER_ARGS) 958 { 959 int error, i, n; 960 struct inpcb *inp, **inp_list; 961 inp_gen_t gencnt; 962 struct xinpgen xig; 963 964 /* 965 * The process of preparing the TCB list is too time-consuming and 966 * resource-intensive to repeat twice on every request. 967 */ 968 if (req->oldptr == 0) { 969 n = V_ripcbinfo.ipi_count; 970 req->oldidx = 2 * (sizeof xig) 971 + (n + n/8) * sizeof(struct xinpcb); 972 return (0); 973 } 974 975 if (req->newptr != 0) 976 return (EPERM); 977 978 /* 979 * OK, now we're committed to doing something. 980 */ 981 INP_INFO_RLOCK(&V_ripcbinfo); 982 gencnt = V_ripcbinfo.ipi_gencnt; 983 n = V_ripcbinfo.ipi_count; 984 INP_INFO_RUNLOCK(&V_ripcbinfo); 985 986 xig.xig_len = sizeof xig; 987 xig.xig_count = n; 988 xig.xig_gen = gencnt; 989 xig.xig_sogen = so_gencnt; 990 error = SYSCTL_OUT(req, &xig, sizeof xig); 991 if (error) 992 return (error); 993 994 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 995 if (inp_list == 0) 996 return (ENOMEM); 997 998 INP_INFO_RLOCK(&V_ripcbinfo); 999 for (inp = LIST_FIRST(V_ripcbinfo.ipi_listhead), i = 0; inp && i < n; 1000 inp = LIST_NEXT(inp, inp_list)) { 1001 INP_RLOCK(inp); 1002 if (inp->inp_gencnt <= gencnt && 1003 cr_canseeinpcb(req->td->td_ucred, inp) == 0) { 1004 /* XXX held references? */ 1005 inp_list[i++] = inp; 1006 } 1007 INP_RUNLOCK(inp); 1008 } 1009 INP_INFO_RUNLOCK(&V_ripcbinfo); 1010 n = i; 1011 1012 error = 0; 1013 for (i = 0; i < n; i++) { 1014 inp = inp_list[i]; 1015 INP_RLOCK(inp); 1016 if (inp->inp_gencnt <= gencnt) { 1017 struct xinpcb xi; 1018 1019 bzero(&xi, sizeof(xi)); 1020 xi.xi_len = sizeof xi; 1021 /* XXX should avoid extra copy */ 1022 bcopy(inp, &xi.xi_inp, sizeof *inp); 1023 if (inp->inp_socket) 1024 sotoxsocket(inp->inp_socket, &xi.xi_socket); 1025 INP_RUNLOCK(inp); 1026 error = SYSCTL_OUT(req, &xi, sizeof xi); 1027 } else 1028 INP_RUNLOCK(inp); 1029 } 1030 if (!error) { 1031 /* 1032 * Give the user an updated idea of our state. If the 1033 * generation differs from what we told her before, she knows 1034 * that something happened while we were processing this 1035 * request, and it might be necessary to retry. 1036 */ 1037 INP_INFO_RLOCK(&V_ripcbinfo); 1038 xig.xig_gen = V_ripcbinfo.ipi_gencnt; 1039 xig.xig_sogen = so_gencnt; 1040 xig.xig_count = V_ripcbinfo.ipi_count; 1041 INP_INFO_RUNLOCK(&V_ripcbinfo); 1042 error = SYSCTL_OUT(req, &xig, sizeof xig); 1043 } 1044 free(inp_list, M_TEMP); 1045 return (error); 1046 } 1047 1048 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 1049 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 1050 1051 struct pr_usrreqs rip_usrreqs = { 1052 .pru_abort = rip_abort, 1053 .pru_attach = rip_attach, 1054 .pru_bind = rip_bind, 1055 .pru_connect = rip_connect, 1056 .pru_control = in_control, 1057 .pru_detach = rip_detach, 1058 .pru_disconnect = rip_disconnect, 1059 .pru_peeraddr = in_getpeeraddr, 1060 .pru_send = rip_send, 1061 .pru_shutdown = rip_shutdown, 1062 .pru_sockaddr = in_getsockaddr, 1063 .pru_sosetlabel = in_pcbsosetlabel, 1064 .pru_close = rip_close, 1065 }; 1066