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