raw_ip.c revision 158588
1249259Sdim/*- 2249259Sdim * Copyright (c) 1982, 1986, 1988, 1993 3249259Sdim * The Regents of the University of California. All rights reserved. 4249259Sdim * 5249259Sdim * Redistribution and use in source and binary forms, with or without 6249259Sdim * modification, are permitted provided that the following conditions 7249259Sdim * are met: 8249259Sdim * 1. Redistributions of source code must retain the above copyright 9249259Sdim * notice, this list of conditions and the following disclaimer. 10249259Sdim * 2. Redistributions in binary form must reproduce the above copyright 11249259Sdim * notice, this list of conditions and the following disclaimer in the 12249259Sdim * documentation and/or other materials provided with the distribution. 13249259Sdim * 4. Neither the name of the University nor the names of its contributors 14249259Sdim * may be used to endorse or promote products derived from this software 15249259Sdim * without specific prior written permission. 16249259Sdim * 17249259Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20249259Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22249259Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27249259Sdim * SUCH DAMAGE. 28249259Sdim * 29249259Sdim * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 30249259Sdim * $FreeBSD: head/sys/netinet/raw_ip.c 158588 2006-05-15 09:28:57Z maxim $ 31249259Sdim */ 32249259Sdim 33249259Sdim#include "opt_inet6.h" 34249259Sdim#include "opt_ipsec.h" 35249259Sdim#include "opt_mac.h" 36249259Sdim 37249259Sdim#include <sys/param.h> 38249259Sdim#include <sys/jail.h> 39249259Sdim#include <sys/kernel.h> 40249259Sdim#include <sys/lock.h> 41249259Sdim#include <sys/mac.h> 42249259Sdim#include <sys/malloc.h> 43249259Sdim#include <sys/mbuf.h> 44249259Sdim#include <sys/proc.h> 45249259Sdim#include <sys/protosw.h> 46249259Sdim#include <sys/signalvar.h> 47249259Sdim#include <sys/socket.h> 48249259Sdim#include <sys/socketvar.h> 49249259Sdim#include <sys/sx.h> 50249259Sdim#include <sys/sysctl.h> 51249259Sdim#include <sys/systm.h> 52249259Sdim 53249259Sdim#include <vm/uma.h> 54249259Sdim 55249259Sdim#include <net/if.h> 56249259Sdim#include <net/route.h> 57249259Sdim 58249259Sdim#include <netinet/in.h> 59249259Sdim#include <netinet/in_systm.h> 60249259Sdim#include <netinet/in_pcb.h> 61249259Sdim#include <netinet/in_var.h> 62249259Sdim#include <netinet/ip.h> 63249259Sdim#include <netinet/ip_var.h> 64249259Sdim#include <netinet/ip_mroute.h> 65249259Sdim 66249259Sdim#include <netinet/ip_fw.h> 67249259Sdim#include <netinet/ip_dummynet.h> 68249259Sdim 69249259Sdim#ifdef FAST_IPSEC 70249259Sdim#include <netipsec/ipsec.h> 71249259Sdim#endif /*FAST_IPSEC*/ 72249259Sdim 73249259Sdim#ifdef IPSEC 74249259Sdim#include <netinet6/ipsec.h> 75249259Sdim#endif /*IPSEC*/ 76249259Sdim 77249259Sdimstruct inpcbhead ripcb; 78249259Sdimstruct inpcbinfo ripcbinfo; 79249259Sdim 80249259Sdim/* control hooks for ipfw and dummynet */ 81249259Sdimip_fw_ctl_t *ip_fw_ctl_ptr = NULL; 82249259Sdimip_dn_ctl_t *ip_dn_ctl_ptr = NULL; 83249259Sdim 84249259Sdim/* 85249259Sdim * hooks for multicast routing. They all default to NULL, 86249259Sdim * so leave them not initialized and rely on BSS being set to 0. 87249259Sdim */ 88249259Sdim 89249259Sdim/* The socket used to communicate with the multicast routing daemon. */ 90249259Sdimstruct socket *ip_mrouter; 91249259Sdim 92249259Sdim/* The various mrouter and rsvp functions */ 93249259Sdimint (*ip_mrouter_set)(struct socket *, struct sockopt *); 94249259Sdimint (*ip_mrouter_get)(struct socket *, struct sockopt *); 95249259Sdimint (*ip_mrouter_done)(void); 96249259Sdimint (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 97249259Sdim struct ip_moptions *); 98249259Sdimint (*mrt_ioctl)(int, caddr_t); 99249259Sdimint (*legal_vif_num)(int); 100249259Sdimu_long (*ip_mcast_src)(int); 101249259Sdim 102249259Sdimvoid (*rsvp_input_p)(struct mbuf *m, int off); 103249259Sdimint (*ip_rsvp_vif)(struct socket *, struct sockopt *); 104249259Sdimvoid (*ip_rsvp_force_done)(struct socket *); 105249259Sdim 106249259Sdim/* 107249259Sdim * Nominal space allocated to a raw ip socket. 108249259Sdim */ 109249259Sdim#define RIPSNDQ 8192 110249259Sdim#define RIPRCVQ 8192 111249259Sdim 112249259Sdim/* 113249259Sdim * Raw interface to IP protocol. 114249259Sdim */ 115249259Sdim 116249259Sdim/* 117249259Sdim * Initialize raw connection block q. 118249259Sdim */ 119249259Sdimstatic void 120249259Sdimrip_zone_change(void *tag) 121249259Sdim{ 122249259Sdim 123249259Sdim uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 124249259Sdim} 125249259Sdim 126249259Sdimvoid 127249259Sdimrip_init() 128249259Sdim{ 129249259Sdim INP_INFO_LOCK_INIT(&ripcbinfo, "rip"); 130249259Sdim LIST_INIT(&ripcb); 131249259Sdim ripcbinfo.listhead = &ripcb; 132249259Sdim /* 133249259Sdim * XXX We don't use the hash list for raw IP, but it's easier 134249259Sdim * to allocate a one entry hash list than it is to check all 135249259Sdim * over the place for hashbase == NULL. 136249259Sdim */ 137249259Sdim ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 138249259Sdim ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); 139249259Sdim ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb), 140249259Sdim NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 141249259Sdim uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets); 142249259Sdim EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, 143249259Sdim NULL, EVENTHANDLER_PRI_ANY); 144249259Sdim} 145249259Sdim 146249259Sdimstatic struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 147249259Sdim 148249259Sdimstatic int 149249259Sdimraw_append(struct inpcb *last, struct ip *ip, struct mbuf *n) 150249259Sdim{ 151249259Sdim int policyfail = 0; 152249259Sdim 153249259Sdim INP_LOCK_ASSERT(last); 154249259Sdim 155249259Sdim#if defined(IPSEC) || defined(FAST_IPSEC) 156249259Sdim /* check AH/ESP integrity. */ 157249259Sdim if (ipsec4_in_reject(n, last)) { 158249259Sdim policyfail = 1; 159249259Sdim#ifdef IPSEC 160249259Sdim ipsecstat.in_polvio++; 161249259Sdim#endif /*IPSEC*/ 162249259Sdim /* do not inject data to pcb */ 163249259Sdim } 164249259Sdim#endif /*IPSEC || FAST_IPSEC*/ 165249259Sdim#ifdef MAC 166249259Sdim if (!policyfail && mac_check_inpcb_deliver(last, n) != 0) 167249259Sdim policyfail = 1; 168249259Sdim#endif 169249259Sdim /* Check the minimum TTL for socket. */ 170249259Sdim if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl) 171249259Sdim policyfail = 1; 172249259Sdim if (!policyfail) { 173249259Sdim struct mbuf *opts = NULL; 174249259Sdim struct socket *so; 175249259Sdim 176249259Sdim so = last->inp_socket; 177249259Sdim if ((last->inp_flags & INP_CONTROLOPTS) || 178249259Sdim (so->so_options & (SO_TIMESTAMP | SO_BINTIME))) 179249259Sdim ip_savecontrol(last, &opts, ip, n); 180249259Sdim SOCKBUF_LOCK(&so->so_rcv); 181249259Sdim if (sbappendaddr_locked(&so->so_rcv, 182249259Sdim (struct sockaddr *)&ripsrc, n, opts) == 0) { 183249259Sdim /* should notify about lost packet */ 184249259Sdim m_freem(n); 185249259Sdim if (opts) 186249259Sdim m_freem(opts); 187249259Sdim SOCKBUF_UNLOCK(&so->so_rcv); 188249259Sdim } else 189249259Sdim sorwakeup_locked(so); 190249259Sdim } else 191249259Sdim m_freem(n); 192249259Sdim return policyfail; 193249259Sdim} 194249259Sdim 195249259Sdim/* 196249259Sdim * Setup generic address and protocol structures 197249259Sdim * for raw_input routine, then pass them along with 198249259Sdim * mbuf chain. 199249259Sdim */ 200249259Sdimvoid 201249259Sdimrip_input(struct mbuf *m, int off) 202249259Sdim{ 203249259Sdim struct ip *ip = mtod(m, struct ip *); 204249259Sdim int proto = ip->ip_p; 205249259Sdim struct inpcb *inp, *last; 206249259Sdim 207249259Sdim INP_INFO_RLOCK(&ripcbinfo); 208249259Sdim ripsrc.sin_addr = ip->ip_src; 209249259Sdim last = NULL; 210249259Sdim LIST_FOREACH(inp, &ripcb, inp_list) { 211249259Sdim INP_LOCK(inp); 212249259Sdim if (inp->inp_ip_p && inp->inp_ip_p != proto) { 213249259Sdim docontinue: 214249259Sdim INP_UNLOCK(inp); 215249259Sdim continue; 216249259Sdim } 217249259Sdim#ifdef INET6 218249259Sdim if ((inp->inp_vflag & INP_IPV4) == 0) 219249259Sdim goto docontinue; 220249259Sdim#endif 221249259Sdim if (inp->inp_laddr.s_addr && 222249259Sdim inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 223249259Sdim goto docontinue; 224249259Sdim if (inp->inp_faddr.s_addr && 225249259Sdim inp->inp_faddr.s_addr != ip->ip_src.s_addr) 226249259Sdim goto docontinue; 227249259Sdim if (jailed(inp->inp_socket->so_cred)) 228249259Sdim if (htonl(prison_getip(inp->inp_socket->so_cred)) != 229249259Sdim ip->ip_dst.s_addr) 230249259Sdim goto docontinue; 231249259Sdim if (last) { 232249259Sdim struct mbuf *n; 233249259Sdim 234249259Sdim n = m_copy(m, 0, (int)M_COPYALL); 235249259Sdim if (n != NULL) 236249259Sdim (void) raw_append(last, ip, n); 237249259Sdim /* XXX count dropped packet */ 238249259Sdim INP_UNLOCK(last); 239249259Sdim } 240249259Sdim last = inp; 241249259Sdim } 242249259Sdim if (last != NULL) { 243249259Sdim if (raw_append(last, ip, m) != 0) 244249259Sdim ipstat.ips_delivered--; 245249259Sdim INP_UNLOCK(last); 246249259Sdim } else { 247249259Sdim m_freem(m); 248249259Sdim ipstat.ips_noproto++; 249249259Sdim ipstat.ips_delivered--; 250249259Sdim } 251249259Sdim INP_INFO_RUNLOCK(&ripcbinfo); 252249259Sdim} 253249259Sdim 254249259Sdim/* 255249259Sdim * Generate IP header and pass packet to ip_output. 256249259Sdim * Tack on options user may have setup with control call. 257249259Sdim */ 258249259Sdimint 259249259Sdimrip_output(struct mbuf *m, struct socket *so, u_long dst) 260249259Sdim{ 261249259Sdim struct ip *ip; 262249259Sdim int error; 263249259Sdim struct inpcb *inp = sotoinpcb(so); 264249259Sdim int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) | 265249259Sdim IP_ALLOWBROADCAST; 266249259Sdim 267249259Sdim /* 268249259Sdim * If the user handed us a complete IP packet, use it. 269249259Sdim * Otherwise, allocate an mbuf for a header and fill it in. 270249259Sdim */ 271249259Sdim if ((inp->inp_flags & INP_HDRINCL) == 0) { 272249259Sdim if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 273249259Sdim m_freem(m); 274249259Sdim return(EMSGSIZE); 275249259Sdim } 276249259Sdim M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 277249259Sdim if (m == NULL) 278249259Sdim return(ENOBUFS); 279249259Sdim 280249259Sdim INP_LOCK(inp); 281249259Sdim ip = mtod(m, struct ip *); 282249259Sdim ip->ip_tos = inp->inp_ip_tos; 283249259Sdim if (inp->inp_flags & INP_DONTFRAG) 284249259Sdim ip->ip_off = IP_DF; 285249259Sdim else 286249259Sdim ip->ip_off = 0; 287249259Sdim ip->ip_p = inp->inp_ip_p; 288249259Sdim ip->ip_len = m->m_pkthdr.len; 289249259Sdim if (jailed(inp->inp_socket->so_cred)) 290249259Sdim ip->ip_src.s_addr = 291249259Sdim htonl(prison_getip(inp->inp_socket->so_cred)); 292249259Sdim else 293249259Sdim ip->ip_src = inp->inp_laddr; 294249259Sdim ip->ip_dst.s_addr = dst; 295249259Sdim ip->ip_ttl = inp->inp_ip_ttl; 296249259Sdim } else { 297249259Sdim if (m->m_pkthdr.len > IP_MAXPACKET) { 298249259Sdim m_freem(m); 299249259Sdim return(EMSGSIZE); 300249259Sdim } 301249259Sdim INP_LOCK(inp); 302249259Sdim ip = mtod(m, struct ip *); 303249259Sdim if (jailed(inp->inp_socket->so_cred)) { 304249259Sdim if (ip->ip_src.s_addr != 305249259Sdim htonl(prison_getip(inp->inp_socket->so_cred))) { 306249259Sdim INP_UNLOCK(inp); 307249259Sdim m_freem(m); 308249259Sdim return (EPERM); 309249259Sdim } 310249259Sdim } 311249259Sdim /* don't allow both user specified and setsockopt options, 312249259Sdim and don't allow packet length sizes that will crash */ 313249259Sdim if (((ip->ip_hl != (sizeof (*ip) >> 2)) 314249259Sdim && inp->inp_options) 315249259Sdim || (ip->ip_len > m->m_pkthdr.len) 316249259Sdim || (ip->ip_len < (ip->ip_hl << 2))) { 317249259Sdim INP_UNLOCK(inp); 318249259Sdim m_freem(m); 319249259Sdim return EINVAL; 320249259Sdim } 321249259Sdim if (ip->ip_id == 0) 322249259Sdim ip->ip_id = ip_newid(); 323249259Sdim /* XXX prevent ip_output from overwriting header fields */ 324249259Sdim flags |= IP_RAWOUTPUT; 325249259Sdim ipstat.ips_rawout++; 326249259Sdim } 327249259Sdim 328249259Sdim if (inp->inp_vflag & INP_ONESBCAST) 329249259Sdim flags |= IP_SENDONES; 330249259Sdim 331249259Sdim#ifdef MAC 332249259Sdim mac_create_mbuf_from_inpcb(inp, m); 333249259Sdim#endif 334249259Sdim 335249259Sdim error = ip_output(m, inp->inp_options, NULL, flags, 336249259Sdim inp->inp_moptions, inp); 337249259Sdim INP_UNLOCK(inp); 338249259Sdim return error; 339249259Sdim} 340249259Sdim 341249259Sdim/* 342249259Sdim * Raw IP socket option processing. 343249259Sdim * 344249259Sdim * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could 345249259Sdim * only be created by a privileged process, and as such, socket option 346249259Sdim * operations to manage system properties on any raw socket were allowed to 347249259Sdim * take place without explicit additional access control checks. However, 348249259Sdim * raw sockets can now also be created in jail(), and therefore explicit 349249259Sdim * checks are now required. Likewise, raw sockets can be used by a process 350249259Sdim * after it gives up privilege, so some caution is required. For options 351249259Sdim * passed down to the IP layer via ip_ctloutput(), checks are assumed to be 352249259Sdim * performed in ip_ctloutput() and therefore no check occurs here. 353249259Sdim * Unilaterally checking suser() here breaks normal IP socket option 354249259Sdim * operations on raw sockets. 355249259Sdim * 356249259Sdim * When adding new socket options here, make sure to add access control 357249259Sdim * checks here as necessary. 358249259Sdim */ 359249259Sdimint 360249259Sdimrip_ctloutput(struct socket *so, struct sockopt *sopt) 361249259Sdim{ 362249259Sdim struct inpcb *inp = sotoinpcb(so); 363249259Sdim int error, optval; 364249259Sdim 365249259Sdim if (sopt->sopt_level != IPPROTO_IP) 366249259Sdim return (EINVAL); 367249259Sdim 368249259Sdim error = 0; 369249259Sdim switch (sopt->sopt_dir) { 370249259Sdim case SOPT_GET: 371249259Sdim switch (sopt->sopt_name) { 372 case IP_HDRINCL: 373 optval = inp->inp_flags & INP_HDRINCL; 374 error = sooptcopyout(sopt, &optval, sizeof optval); 375 break; 376 377 case IP_FW_ADD: /* ADD actually returns the body... */ 378 case IP_FW_GET: 379 case IP_FW_TABLE_GETSIZE: 380 case IP_FW_TABLE_LIST: 381 error = suser(curthread); 382 if (error != 0) 383 return (error); 384 if (ip_fw_ctl_ptr != NULL) 385 error = ip_fw_ctl_ptr(sopt); 386 else 387 error = ENOPROTOOPT; 388 break; 389 390 case IP_DUMMYNET_GET: 391 error = suser(curthread); 392 if (error != 0) 393 return (error); 394 if (ip_dn_ctl_ptr != NULL) 395 error = ip_dn_ctl_ptr(sopt); 396 else 397 error = ENOPROTOOPT; 398 break ; 399 400 case MRT_INIT: 401 case MRT_DONE: 402 case MRT_ADD_VIF: 403 case MRT_DEL_VIF: 404 case MRT_ADD_MFC: 405 case MRT_DEL_MFC: 406 case MRT_VERSION: 407 case MRT_ASSERT: 408 case MRT_API_SUPPORT: 409 case MRT_API_CONFIG: 410 case MRT_ADD_BW_UPCALL: 411 case MRT_DEL_BW_UPCALL: 412 error = suser(curthread); 413 if (error != 0) 414 return (error); 415 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) : 416 EOPNOTSUPP; 417 break; 418 419 default: 420 error = ip_ctloutput(so, sopt); 421 break; 422 } 423 break; 424 425 case SOPT_SET: 426 switch (sopt->sopt_name) { 427 case IP_HDRINCL: 428 error = sooptcopyin(sopt, &optval, sizeof optval, 429 sizeof optval); 430 if (error) 431 break; 432 if (optval) 433 inp->inp_flags |= INP_HDRINCL; 434 else 435 inp->inp_flags &= ~INP_HDRINCL; 436 break; 437 438 case IP_FW_ADD: 439 case IP_FW_DEL: 440 case IP_FW_FLUSH: 441 case IP_FW_ZERO: 442 case IP_FW_RESETLOG: 443 case IP_FW_TABLE_ADD: 444 case IP_FW_TABLE_DEL: 445 case IP_FW_TABLE_FLUSH: 446 error = suser(curthread); 447 if (error != 0) 448 return (error); 449 if (ip_fw_ctl_ptr != NULL) 450 error = ip_fw_ctl_ptr(sopt); 451 else 452 error = ENOPROTOOPT; 453 break; 454 455 case IP_DUMMYNET_CONFIGURE: 456 case IP_DUMMYNET_DEL: 457 case IP_DUMMYNET_FLUSH: 458 error = suser(curthread); 459 if (error != 0) 460 return (error); 461 if (ip_dn_ctl_ptr != NULL) 462 error = ip_dn_ctl_ptr(sopt); 463 else 464 error = ENOPROTOOPT ; 465 break ; 466 467 case IP_RSVP_ON: 468 error = suser(curthread); 469 if (error != 0) 470 return (error); 471 error = ip_rsvp_init(so); 472 break; 473 474 case IP_RSVP_OFF: 475 error = suser(curthread); 476 if (error != 0) 477 return (error); 478 error = ip_rsvp_done(); 479 break; 480 481 case IP_RSVP_VIF_ON: 482 case IP_RSVP_VIF_OFF: 483 error = suser(curthread); 484 if (error != 0) 485 return (error); 486 error = ip_rsvp_vif ? 487 ip_rsvp_vif(so, sopt) : EINVAL; 488 break; 489 490 case MRT_INIT: 491 case MRT_DONE: 492 case MRT_ADD_VIF: 493 case MRT_DEL_VIF: 494 case MRT_ADD_MFC: 495 case MRT_DEL_MFC: 496 case MRT_VERSION: 497 case MRT_ASSERT: 498 case MRT_API_SUPPORT: 499 case MRT_API_CONFIG: 500 case MRT_ADD_BW_UPCALL: 501 case MRT_DEL_BW_UPCALL: 502 error = suser(curthread); 503 if (error != 0) 504 return (error); 505 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) : 506 EOPNOTSUPP; 507 break; 508 509 default: 510 error = ip_ctloutput(so, sopt); 511 break; 512 } 513 break; 514 } 515 516 return (error); 517} 518 519/* 520 * This function exists solely to receive the PRC_IFDOWN messages which 521 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 522 * and calls in_ifadown() to remove all routes corresponding to that address. 523 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 524 * interface routes. 525 */ 526void 527rip_ctlinput(int cmd, struct sockaddr *sa, void *vip) 528{ 529 struct in_ifaddr *ia; 530 struct ifnet *ifp; 531 int err; 532 int flags; 533 534 switch (cmd) { 535 case PRC_IFDOWN: 536 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 537 if (ia->ia_ifa.ifa_addr == sa 538 && (ia->ia_flags & IFA_ROUTE)) { 539 /* 540 * in_ifscrub kills the interface route. 541 */ 542 in_ifscrub(ia->ia_ifp, ia); 543 /* 544 * in_ifadown gets rid of all the rest of 545 * the routes. This is not quite the right 546 * thing to do, but at least if we are running 547 * a routing process they will come back. 548 */ 549 in_ifadown(&ia->ia_ifa, 0); 550 break; 551 } 552 } 553 break; 554 555 case PRC_IFUP: 556 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) { 557 if (ia->ia_ifa.ifa_addr == sa) 558 break; 559 } 560 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 561 return; 562 flags = RTF_UP; 563 ifp = ia->ia_ifa.ifa_ifp; 564 565 if ((ifp->if_flags & IFF_LOOPBACK) 566 || (ifp->if_flags & IFF_POINTOPOINT)) 567 flags |= RTF_HOST; 568 569 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 570 if (err == 0) 571 ia->ia_flags |= IFA_ROUTE; 572 break; 573 } 574} 575 576u_long rip_sendspace = RIPSNDQ; 577u_long rip_recvspace = RIPRCVQ; 578 579SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, 580 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); 581SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, 582 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams"); 583 584static int 585rip_attach(struct socket *so, int proto, struct thread *td) 586{ 587 struct inpcb *inp; 588 int error; 589 590 inp = sotoinpcb(so); 591 KASSERT(inp == NULL, ("rip_attach: inp != NULL")); 592 if (jailed(td->td_ucred) && !jail_allow_raw_sockets) 593 return (EPERM); 594 if ((error = suser_cred(td->td_ucred, SUSER_ALLOWJAIL)) != 0) 595 return error; 596 if (proto >= IPPROTO_MAX || proto < 0) 597 return EPROTONOSUPPORT; 598 error = soreserve(so, rip_sendspace, rip_recvspace); 599 if (error) 600 return error; 601 INP_INFO_WLOCK(&ripcbinfo); 602 error = in_pcballoc(so, &ripcbinfo, "rawinp"); 603 if (error) { 604 INP_INFO_WUNLOCK(&ripcbinfo); 605 return error; 606 } 607 inp = (struct inpcb *)so->so_pcb; 608 INP_LOCK(inp); 609 INP_INFO_WUNLOCK(&ripcbinfo); 610 inp->inp_vflag |= INP_IPV4; 611 inp->inp_ip_p = proto; 612 inp->inp_ip_ttl = ip_defttl; 613 INP_UNLOCK(inp); 614 return 0; 615} 616 617static void 618rip_pcbdetach(struct socket *so, struct inpcb *inp) 619{ 620 621 INP_INFO_WLOCK_ASSERT(&ripcbinfo); 622 INP_LOCK_ASSERT(inp); 623 624 if (so == ip_mrouter && ip_mrouter_done) 625 ip_mrouter_done(); 626 if (ip_rsvp_force_done) 627 ip_rsvp_force_done(so); 628 if (so == ip_rsvpd) 629 ip_rsvp_done(); 630 in_pcbdetach(inp); 631 in_pcbfree(inp); 632} 633 634static void 635rip_detach(struct socket *so) 636{ 637 struct inpcb *inp; 638 639 inp = sotoinpcb(so); 640 KASSERT(inp != NULL, ("rip_detach: inp == NULL")); 641 INP_INFO_WLOCK(&ripcbinfo); 642 INP_LOCK(inp); 643 rip_pcbdetach(so, inp); 644 INP_INFO_WUNLOCK(&ripcbinfo); 645} 646 647static void 648rip_abort(struct socket *so) 649{ 650 struct inpcb *inp; 651 652 inp = sotoinpcb(so); 653 KASSERT(inp != NULL, ("rip_abort: inp == NULL")); 654 INP_INFO_WLOCK(&ripcbinfo); 655 INP_LOCK(inp); 656 soisdisconnected(so); 657 rip_pcbdetach(so, inp); 658 INP_INFO_WUNLOCK(&ripcbinfo); 659} 660 661static int 662rip_disconnect(struct socket *so) 663{ 664 struct inpcb *inp; 665 666 if ((so->so_state & SS_ISCONNECTED) == 0) 667 return ENOTCONN; 668 669 inp = sotoinpcb(so); 670 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL")); 671 INP_INFO_WLOCK(&ripcbinfo); 672 INP_LOCK(inp); 673 inp->inp_faddr.s_addr = INADDR_ANY; 674 INP_UNLOCK(inp); 675 INP_INFO_WUNLOCK(&ripcbinfo); 676 so->so_state &= ~SS_ISCONNECTED; 677 return (0); 678} 679 680static int 681rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 682{ 683 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 684 struct inpcb *inp; 685 686 if (nam->sa_len != sizeof(*addr)) 687 return EINVAL; 688 689 if (jailed(td->td_ucred)) { 690 if (addr->sin_addr.s_addr == INADDR_ANY) 691 addr->sin_addr.s_addr = 692 htonl(prison_getip(td->td_ucred)); 693 if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr) 694 return (EADDRNOTAVAIL); 695 } 696 697 if (TAILQ_EMPTY(&ifnet) || 698 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) || 699 (addr->sin_addr.s_addr && 700 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 701 return EADDRNOTAVAIL; 702 703 inp = sotoinpcb(so); 704 KASSERT(inp != NULL, ("rip_bind: inp == NULL")); 705 INP_INFO_WLOCK(&ripcbinfo); 706 INP_LOCK(inp); 707 inp->inp_laddr = addr->sin_addr; 708 INP_UNLOCK(inp); 709 INP_INFO_WUNLOCK(&ripcbinfo); 710 return 0; 711} 712 713static int 714rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 715{ 716 struct sockaddr_in *addr = (struct sockaddr_in *)nam; 717 struct inpcb *inp; 718 719 if (nam->sa_len != sizeof(*addr)) 720 return EINVAL; 721 if (TAILQ_EMPTY(&ifnet)) 722 return EADDRNOTAVAIL; 723 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) 724 return EAFNOSUPPORT; 725 726 inp = sotoinpcb(so); 727 KASSERT(inp != NULL, ("rip_connect: inp == NULL")); 728 INP_INFO_WLOCK(&ripcbinfo); 729 INP_LOCK(inp); 730 inp->inp_faddr = addr->sin_addr; 731 soisconnected(so); 732 INP_UNLOCK(inp); 733 INP_INFO_WUNLOCK(&ripcbinfo); 734 return 0; 735} 736 737static int 738rip_shutdown(struct socket *so) 739{ 740 struct inpcb *inp; 741 742 inp = sotoinpcb(so); 743 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL")); 744 INP_LOCK(inp); 745 socantsendmore(so); 746 INP_UNLOCK(inp); 747 return 0; 748} 749 750static int 751rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 752 struct mbuf *control, struct thread *td) 753{ 754 struct inpcb *inp; 755 u_long dst; 756 757 inp = sotoinpcb(so); 758 KASSERT(inp != NULL, ("rip_send: inp == NULL")); 759 /* 760 * Note: 'dst' reads below are unlocked. 761 */ 762 if (so->so_state & SS_ISCONNECTED) { 763 if (nam) { 764 m_freem(m); 765 return EISCONN; 766 } 767 dst = inp->inp_faddr.s_addr; /* Unlocked read. */ 768 } else { 769 if (nam == NULL) { 770 m_freem(m); 771 return ENOTCONN; 772 } 773 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; 774 } 775 return rip_output(m, so, dst); 776} 777 778static int 779rip_pcblist(SYSCTL_HANDLER_ARGS) 780{ 781 int error, i, n; 782 struct inpcb *inp, **inp_list; 783 inp_gen_t gencnt; 784 struct xinpgen xig; 785 786 /* 787 * The process of preparing the TCB list is too time-consuming and 788 * resource-intensive to repeat twice on every request. 789 */ 790 if (req->oldptr == 0) { 791 n = ripcbinfo.ipi_count; 792 req->oldidx = 2 * (sizeof xig) 793 + (n + n/8) * sizeof(struct xinpcb); 794 return 0; 795 } 796 797 if (req->newptr != 0) 798 return EPERM; 799 800 /* 801 * OK, now we're committed to doing something. 802 */ 803 INP_INFO_RLOCK(&ripcbinfo); 804 gencnt = ripcbinfo.ipi_gencnt; 805 n = ripcbinfo.ipi_count; 806 INP_INFO_RUNLOCK(&ripcbinfo); 807 808 xig.xig_len = sizeof xig; 809 xig.xig_count = n; 810 xig.xig_gen = gencnt; 811 xig.xig_sogen = so_gencnt; 812 error = SYSCTL_OUT(req, &xig, sizeof xig); 813 if (error) 814 return error; 815 816 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 817 if (inp_list == 0) 818 return ENOMEM; 819 820 INP_INFO_RLOCK(&ripcbinfo); 821 for (inp = LIST_FIRST(ripcbinfo.listhead), i = 0; inp && i < n; 822 inp = LIST_NEXT(inp, inp_list)) { 823 INP_LOCK(inp); 824 if (inp->inp_gencnt <= gencnt && 825 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) { 826 /* XXX held references? */ 827 inp_list[i++] = inp; 828 } 829 INP_UNLOCK(inp); 830 } 831 INP_INFO_RUNLOCK(&ripcbinfo); 832 n = i; 833 834 error = 0; 835 for (i = 0; i < n; i++) { 836 inp = inp_list[i]; 837 if (inp->inp_gencnt <= gencnt) { 838 struct xinpcb xi; 839 bzero(&xi, sizeof(xi)); 840 xi.xi_len = sizeof xi; 841 /* XXX should avoid extra copy */ 842 bcopy(inp, &xi.xi_inp, sizeof *inp); 843 if (inp->inp_socket) 844 sotoxsocket(inp->inp_socket, &xi.xi_socket); 845 error = SYSCTL_OUT(req, &xi, sizeof xi); 846 } 847 } 848 if (!error) { 849 /* 850 * Give the user an updated idea of our state. 851 * If the generation differs from what we told 852 * her before, she knows that something happened 853 * while we were processing this request, and it 854 * might be necessary to retry. 855 */ 856 INP_INFO_RLOCK(&ripcbinfo); 857 xig.xig_gen = ripcbinfo.ipi_gencnt; 858 xig.xig_sogen = so_gencnt; 859 xig.xig_count = ripcbinfo.ipi_count; 860 INP_INFO_RUNLOCK(&ripcbinfo); 861 error = SYSCTL_OUT(req, &xig, sizeof xig); 862 } 863 free(inp_list, M_TEMP); 864 return error; 865} 866 867/* 868 * This is the wrapper function for in_setsockaddr. We just pass down 869 * the pcbinfo for in_setpeeraddr to lock. 870 */ 871static int 872rip_sockaddr(struct socket *so, struct sockaddr **nam) 873{ 874 return (in_setsockaddr(so, nam, &ripcbinfo)); 875} 876 877/* 878 * This is the wrapper function for in_setpeeraddr. We just pass down 879 * the pcbinfo for in_setpeeraddr to lock. 880 */ 881static int 882rip_peeraddr(struct socket *so, struct sockaddr **nam) 883{ 884 return (in_setpeeraddr(so, nam, &ripcbinfo)); 885} 886 887 888SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, 889 rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); 890 891struct pr_usrreqs rip_usrreqs = { 892 .pru_abort = rip_abort, 893 .pru_attach = rip_attach, 894 .pru_bind = rip_bind, 895 .pru_connect = rip_connect, 896 .pru_control = in_control, 897 .pru_detach = rip_detach, 898 .pru_disconnect = rip_disconnect, 899 .pru_peeraddr = rip_peeraddr, 900 .pru_send = rip_send, 901 .pru_shutdown = rip_shutdown, 902 .pru_sockaddr = rip_sockaddr, 903 .pru_sosetlabel = in_pcbsosetlabel 904}; 905