ip_output.c revision 271293
1/*- 2 * Copyright (c) 1982, 1986, 1988, 1990, 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/netinet/ip_output.c 271293 2014-09-09 01:45:39Z adrian $"); 34 35#include "opt_inet.h" 36#include "opt_ipfw.h" 37#include "opt_ipsec.h" 38#include "opt_mbuf_stress_test.h" 39#include "opt_mpath.h" 40#include "opt_route.h" 41#include "opt_sctp.h" 42#include "opt_rss.h" 43 44#include <sys/param.h> 45#include <sys/systm.h> 46#include <sys/kernel.h> 47#include <sys/malloc.h> 48#include <sys/mbuf.h> 49#include <sys/priv.h> 50#include <sys/proc.h> 51#include <sys/protosw.h> 52#include <sys/sdt.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55#include <sys/sysctl.h> 56#include <sys/ucred.h> 57 58#include <net/if.h> 59#include <net/if_var.h> 60#include <net/if_llatbl.h> 61#include <net/netisr.h> 62#include <net/pfil.h> 63#include <net/route.h> 64#include <net/flowtable.h> 65#ifdef RADIX_MPATH 66#include <net/radix_mpath.h> 67#endif 68#include <net/vnet.h> 69 70#include <netinet/in.h> 71#include <netinet/in_kdtrace.h> 72#include <netinet/in_systm.h> 73#include <netinet/ip.h> 74#include <netinet/in_pcb.h> 75#include <netinet/in_rss.h> 76#include <netinet/in_var.h> 77#include <netinet/ip_var.h> 78#include <netinet/ip_options.h> 79#ifdef SCTP 80#include <netinet/sctp.h> 81#include <netinet/sctp_crc32.h> 82#endif 83 84#ifdef IPSEC 85#include <netinet/ip_ipsec.h> 86#include <netipsec/ipsec.h> 87#endif /* IPSEC*/ 88 89#include <machine/in_cksum.h> 90 91#include <security/mac/mac_framework.h> 92 93VNET_DEFINE(u_short, ip_id); 94 95#ifdef MBUF_STRESS_TEST 96static int mbuf_frag_size = 0; 97SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 98 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 99#endif 100 101static void ip_mloopback 102 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 103 104 105extern int in_mcast_loop; 106extern struct protosw inetsw[]; 107 108/* 109 * IP output. The packet in mbuf chain m contains a skeletal IP 110 * header (with len, off, ttl, proto, tos, src, dst). 111 * The mbuf chain containing the packet will be freed. 112 * The mbuf opt, if present, will not be freed. 113 * If route ro is present and has ro_rt initialized, route lookup would be 114 * skipped and ro->ro_rt would be used. If ro is present but ro->ro_rt is NULL, 115 * then result of route lookup is stored in ro->ro_rt. 116 * 117 * In the IP forwarding case, the packet will arrive with options already 118 * inserted, so must have a NULL opt pointer. 119 */ 120int 121ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags, 122 struct ip_moptions *imo, struct inpcb *inp) 123{ 124 struct ip *ip; 125 struct ifnet *ifp = NULL; /* keep compiler happy */ 126 struct mbuf *m0; 127 int hlen = sizeof (struct ip); 128 int mtu; 129 int error = 0; 130 struct sockaddr_in *dst; 131 const struct sockaddr_in *gw; 132 struct in_ifaddr *ia; 133 int isbroadcast; 134 uint16_t ip_len, ip_off; 135 struct route iproute; 136 struct rtentry *rte; /* cache for ro->ro_rt */ 137 struct in_addr odst; 138 struct m_tag *fwd_tag = NULL; 139 int have_ia_ref; 140#ifdef IPSEC 141 int no_route_but_check_spd = 0; 142#endif 143 M_ASSERTPKTHDR(m); 144 145 if (inp != NULL) { 146 INP_LOCK_ASSERT(inp); 147 M_SETFIB(m, inp->inp_inc.inc_fibnum); 148 if (((flags & IP_NODEFAULTFLOWID) == 0) && 149 inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) { 150 m->m_pkthdr.flowid = inp->inp_flowid; 151 M_HASHTYPE_SET(m, inp->inp_flowtype); 152 m->m_flags |= M_FLOWID; 153 } 154 } 155 156 if (ro == NULL) { 157 ro = &iproute; 158 bzero(ro, sizeof (*ro)); 159 } 160 161#ifdef FLOWTABLE 162 if (ro->ro_rt == NULL) 163 (void )flowtable_lookup(AF_INET, m, ro); 164#endif 165 166 if (opt) { 167 int len = 0; 168 m = ip_insertoptions(m, opt, &len); 169 if (len != 0) 170 hlen = len; /* ip->ip_hl is updated above */ 171 } 172 ip = mtod(m, struct ip *); 173 ip_len = ntohs(ip->ip_len); 174 ip_off = ntohs(ip->ip_off); 175 176 /* 177 * Fill in IP header. If we are not allowing fragmentation, 178 * then the ip_id field is meaningless, but we don't set it 179 * to zero. Doing so causes various problems when devices along 180 * the path (routers, load balancers, firewalls, etc.) illegally 181 * disable DF on our packet. Note that a 16-bit counter 182 * will wrap around in less than 10 seconds at 100 Mbit/s on a 183 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 184 * for Counting NATted Hosts", Proc. IMW'02, available at 185 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 186 */ 187 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 188 ip->ip_v = IPVERSION; 189 ip->ip_hl = hlen >> 2; 190 ip->ip_id = ip_newid(); 191 IPSTAT_INC(ips_localout); 192 } else { 193 /* Header already set, fetch hlen from there */ 194 hlen = ip->ip_hl << 2; 195 } 196 197 /* 198 * dst/gw handling: 199 * 200 * dst can be rewritten but always points to &ro->ro_dst. 201 * gw is readonly but can point either to dst OR rt_gateway, 202 * therefore we need restore gw if we're redoing lookup. 203 */ 204 gw = dst = (struct sockaddr_in *)&ro->ro_dst; 205again: 206 ia = NULL; 207 have_ia_ref = 0; 208 /* 209 * If there is a cached route, check that it is to the same 210 * destination and is still up. If not, free it and try again. 211 * The address family should also be checked in case of sharing 212 * the cache with IPv6. 213 */ 214 rte = ro->ro_rt; 215 if (rte && ((rte->rt_flags & RTF_UP) == 0 || 216 rte->rt_ifp == NULL || 217 !RT_LINK_IS_UP(rte->rt_ifp) || 218 dst->sin_family != AF_INET || 219 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 220 RO_RTFREE(ro); 221 ro->ro_lle = NULL; 222 rte = NULL; 223 gw = dst; 224 } 225 if (rte == NULL && fwd_tag == NULL) { 226 bzero(dst, sizeof(*dst)); 227 dst->sin_family = AF_INET; 228 dst->sin_len = sizeof(*dst); 229 dst->sin_addr = ip->ip_dst; 230 } 231 /* 232 * If routing to interface only, short circuit routing lookup. 233 * The use of an all-ones broadcast address implies this; an 234 * interface is specified by the broadcast address of an interface, 235 * or the destination address of a ptp interface. 236 */ 237 if (flags & IP_SENDONES) { 238 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 239 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 240 IPSTAT_INC(ips_noroute); 241 error = ENETUNREACH; 242 goto bad; 243 } 244 have_ia_ref = 1; 245 ip->ip_dst.s_addr = INADDR_BROADCAST; 246 dst->sin_addr = ip->ip_dst; 247 ifp = ia->ia_ifp; 248 ip->ip_ttl = 1; 249 isbroadcast = 1; 250 } else if (flags & IP_ROUTETOIF) { 251 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 252 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) { 253 IPSTAT_INC(ips_noroute); 254 error = ENETUNREACH; 255 goto bad; 256 } 257 have_ia_ref = 1; 258 ifp = ia->ia_ifp; 259 ip->ip_ttl = 1; 260 isbroadcast = in_broadcast(dst->sin_addr, ifp); 261 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 262 imo != NULL && imo->imo_multicast_ifp != NULL) { 263 /* 264 * Bypass the normal routing lookup for multicast 265 * packets if the interface is specified. 266 */ 267 ifp = imo->imo_multicast_ifp; 268 IFP_TO_IA(ifp, ia); 269 if (ia) 270 have_ia_ref = 1; 271 isbroadcast = 0; /* fool gcc */ 272 } else { 273 /* 274 * We want to do any cloning requested by the link layer, 275 * as this is probably required in all cases for correct 276 * operation (as it is for ARP). 277 */ 278 if (rte == NULL) { 279#ifdef RADIX_MPATH 280 rtalloc_mpath_fib(ro, 281 ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 282 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 283#else 284 in_rtalloc_ign(ro, 0, 285 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 286#endif 287 rte = ro->ro_rt; 288 } 289 if (rte == NULL || 290 rte->rt_ifp == NULL || 291 !RT_LINK_IS_UP(rte->rt_ifp)) { 292#ifdef IPSEC 293 /* 294 * There is no route for this packet, but it is 295 * possible that a matching SPD entry exists. 296 */ 297 no_route_but_check_spd = 1; 298 mtu = 0; /* Silence GCC warning. */ 299 goto sendit; 300#endif 301 IPSTAT_INC(ips_noroute); 302 error = EHOSTUNREACH; 303 goto bad; 304 } 305 ia = ifatoia(rte->rt_ifa); 306 ifp = rte->rt_ifp; 307 counter_u64_add(rte->rt_pksent, 1); 308 if (rte->rt_flags & RTF_GATEWAY) 309 gw = (struct sockaddr_in *)rte->rt_gateway; 310 if (rte->rt_flags & RTF_HOST) 311 isbroadcast = (rte->rt_flags & RTF_BROADCAST); 312 else 313 isbroadcast = in_broadcast(gw->sin_addr, ifp); 314 } 315 /* 316 * Calculate MTU. If we have a route that is up, use that, 317 * otherwise use the interface's MTU. 318 */ 319 if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) { 320 /* 321 * This case can happen if the user changed the MTU 322 * of an interface after enabling IP on it. Because 323 * most netifs don't keep track of routes pointing to 324 * them, there is no way for one to update all its 325 * routes when the MTU is changed. 326 */ 327 if (rte->rt_mtu > ifp->if_mtu) 328 rte->rt_mtu = ifp->if_mtu; 329 mtu = rte->rt_mtu; 330 } else { 331 mtu = ifp->if_mtu; 332 } 333 /* Catch a possible divide by zero later. */ 334 KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 335 __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 336 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 337 m->m_flags |= M_MCAST; 338 /* 339 * IP destination address is multicast. Make sure "gw" 340 * still points to the address in "ro". (It may have been 341 * changed to point to a gateway address, above.) 342 */ 343 gw = dst; 344 /* 345 * See if the caller provided any multicast options 346 */ 347 if (imo != NULL) { 348 ip->ip_ttl = imo->imo_multicast_ttl; 349 if (imo->imo_multicast_vif != -1) 350 ip->ip_src.s_addr = 351 ip_mcast_src ? 352 ip_mcast_src(imo->imo_multicast_vif) : 353 INADDR_ANY; 354 } else 355 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 356 /* 357 * Confirm that the outgoing interface supports multicast. 358 */ 359 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 360 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 361 IPSTAT_INC(ips_noroute); 362 error = ENETUNREACH; 363 goto bad; 364 } 365 } 366 /* 367 * If source address not specified yet, use address 368 * of outgoing interface. 369 */ 370 if (ip->ip_src.s_addr == INADDR_ANY) { 371 /* Interface may have no addresses. */ 372 if (ia != NULL) 373 ip->ip_src = IA_SIN(ia)->sin_addr; 374 } 375 376 if ((imo == NULL && in_mcast_loop) || 377 (imo && imo->imo_multicast_loop)) { 378 /* 379 * Loop back multicast datagram if not expressly 380 * forbidden to do so, even if we are not a member 381 * of the group; ip_input() will filter it later, 382 * thus deferring a hash lookup and mutex acquisition 383 * at the expense of a cheap copy using m_copym(). 384 */ 385 ip_mloopback(ifp, m, dst, hlen); 386 } else { 387 /* 388 * If we are acting as a multicast router, perform 389 * multicast forwarding as if the packet had just 390 * arrived on the interface to which we are about 391 * to send. The multicast forwarding function 392 * recursively calls this function, using the 393 * IP_FORWARDING flag to prevent infinite recursion. 394 * 395 * Multicasts that are looped back by ip_mloopback(), 396 * above, will be forwarded by the ip_input() routine, 397 * if necessary. 398 */ 399 if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 400 /* 401 * If rsvp daemon is not running, do not 402 * set ip_moptions. This ensures that the packet 403 * is multicast and not just sent down one link 404 * as prescribed by rsvpd. 405 */ 406 if (!V_rsvp_on) 407 imo = NULL; 408 if (ip_mforward && 409 ip_mforward(ip, ifp, m, imo) != 0) { 410 m_freem(m); 411 goto done; 412 } 413 } 414 } 415 416 /* 417 * Multicasts with a time-to-live of zero may be looped- 418 * back, above, but must not be transmitted on a network. 419 * Also, multicasts addressed to the loopback interface 420 * are not sent -- the above call to ip_mloopback() will 421 * loop back a copy. ip_input() will drop the copy if 422 * this host does not belong to the destination group on 423 * the loopback interface. 424 */ 425 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 426 m_freem(m); 427 goto done; 428 } 429 430 goto sendit; 431 } 432 433 /* 434 * If the source address is not specified yet, use the address 435 * of the outoing interface. 436 */ 437 if (ip->ip_src.s_addr == INADDR_ANY) { 438 /* Interface may have no addresses. */ 439 if (ia != NULL) { 440 ip->ip_src = IA_SIN(ia)->sin_addr; 441 } 442 } 443 444 /* 445 * Both in the SMP world, pre-emption world if_transmit() world, 446 * the following code doesn't really function as intended any further. 447 * 448 * + There can and will be multiple CPUs running this code path 449 * in parallel, and we do no lock holding when checking the 450 * queue depth; 451 * + And since other threads can be running concurrently, even if 452 * we do pass this check, another thread may queue some frames 453 * before this thread does and it will end up partially or fully 454 * failing to send anyway; 455 * + if_transmit() based drivers don't necessarily set ifq_len 456 * at all. 457 * 458 * This should be replaced with a method of pushing an entire list 459 * of fragment frames to the driver and have the driver decide 460 * whether it can queue or not queue the entire set. 461 */ 462#if 0 463 /* 464 * Verify that we have any chance at all of being able to queue the 465 * packet or packet fragments, unless ALTQ is enabled on the given 466 * interface in which case packetdrop should be done by queueing. 467 */ 468 n = ip_len / mtu + 1; /* how many fragments ? */ 469 if ( 470#ifdef ALTQ 471 (!ALTQ_IS_ENABLED(&ifp->if_snd)) && 472#endif /* ALTQ */ 473 (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) { 474 error = ENOBUFS; 475 IPSTAT_INC(ips_odropped); 476 ifp->if_snd.ifq_drops += n; 477 goto bad; 478 } 479#endif 480 481 /* 482 * Look for broadcast address and 483 * verify user is allowed to send 484 * such a packet. 485 */ 486 if (isbroadcast) { 487 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 488 error = EADDRNOTAVAIL; 489 goto bad; 490 } 491 if ((flags & IP_ALLOWBROADCAST) == 0) { 492 error = EACCES; 493 goto bad; 494 } 495 /* don't allow broadcast messages to be fragmented */ 496 if (ip_len > mtu) { 497 error = EMSGSIZE; 498 goto bad; 499 } 500 m->m_flags |= M_BCAST; 501 } else { 502 m->m_flags &= ~M_BCAST; 503 } 504 505sendit: 506#ifdef IPSEC 507 switch(ip_ipsec_output(&m, inp, &flags, &error)) { 508 case 1: 509 goto bad; 510 case -1: 511 goto done; 512 case 0: 513 default: 514 break; /* Continue with packet processing. */ 515 } 516 /* 517 * Check if there was a route for this packet; return error if not. 518 */ 519 if (no_route_but_check_spd) { 520 IPSTAT_INC(ips_noroute); 521 error = EHOSTUNREACH; 522 goto bad; 523 } 524 /* Update variables that are affected by ipsec4_output(). */ 525 ip = mtod(m, struct ip *); 526 hlen = ip->ip_hl << 2; 527#endif /* IPSEC */ 528 529 /* Jump over all PFIL processing if hooks are not active. */ 530 if (!PFIL_HOOKED(&V_inet_pfil_hook)) 531 goto passout; 532 533 /* Run through list of hooks for output packets. */ 534 odst.s_addr = ip->ip_dst.s_addr; 535 error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 536 if (error != 0 || m == NULL) 537 goto done; 538 539 ip = mtod(m, struct ip *); 540 541 /* See if destination IP address was changed by packet filter. */ 542 if (odst.s_addr != ip->ip_dst.s_addr) { 543 m->m_flags |= M_SKIP_FIREWALL; 544 /* If destination is now ourself drop to ip_input(). */ 545 if (in_localip(ip->ip_dst)) { 546 m->m_flags |= M_FASTFWD_OURS; 547 if (m->m_pkthdr.rcvif == NULL) 548 m->m_pkthdr.rcvif = V_loif; 549 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 550 m->m_pkthdr.csum_flags |= 551 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 552 m->m_pkthdr.csum_data = 0xffff; 553 } 554 m->m_pkthdr.csum_flags |= 555 CSUM_IP_CHECKED | CSUM_IP_VALID; 556#ifdef SCTP 557 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 558 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 559#endif 560 error = netisr_queue(NETISR_IP, m); 561 goto done; 562 } else { 563 if (have_ia_ref) 564 ifa_free(&ia->ia_ifa); 565 goto again; /* Redo the routing table lookup. */ 566 } 567 } 568 569 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 570 if (m->m_flags & M_FASTFWD_OURS) { 571 if (m->m_pkthdr.rcvif == NULL) 572 m->m_pkthdr.rcvif = V_loif; 573 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 574 m->m_pkthdr.csum_flags |= 575 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 576 m->m_pkthdr.csum_data = 0xffff; 577 } 578#ifdef SCTP 579 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 580 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 581#endif 582 m->m_pkthdr.csum_flags |= 583 CSUM_IP_CHECKED | CSUM_IP_VALID; 584 585 error = netisr_queue(NETISR_IP, m); 586 goto done; 587 } 588 /* Or forward to some other address? */ 589 if ((m->m_flags & M_IP_NEXTHOP) && 590 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 591 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 592 m->m_flags |= M_SKIP_FIREWALL; 593 m->m_flags &= ~M_IP_NEXTHOP; 594 m_tag_delete(m, fwd_tag); 595 if (have_ia_ref) 596 ifa_free(&ia->ia_ifa); 597 goto again; 598 } 599 600passout: 601 /* 127/8 must not appear on wire - RFC1122. */ 602 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 603 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 604 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 605 IPSTAT_INC(ips_badaddr); 606 error = EADDRNOTAVAIL; 607 goto bad; 608 } 609 } 610 611 m->m_pkthdr.csum_flags |= CSUM_IP; 612 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 613 in_delayed_cksum(m); 614 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 615 } 616#ifdef SCTP 617 if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 618 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 619 m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 620 } 621#endif 622 623 /* 624 * If small enough for interface, or the interface will take 625 * care of the fragmentation for us, we can just send directly. 626 */ 627 if (ip_len <= mtu || 628 (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0) { 629 ip->ip_sum = 0; 630 if (m->m_pkthdr.csum_flags & CSUM_IP & ~ifp->if_hwassist) { 631 ip->ip_sum = in_cksum(m, hlen); 632 m->m_pkthdr.csum_flags &= ~CSUM_IP; 633 } 634 635 /* 636 * Record statistics for this interface address. 637 * With CSUM_TSO the byte/packet count will be slightly 638 * incorrect because we count the IP+TCP headers only 639 * once instead of for every generated packet. 640 */ 641 if (!(flags & IP_FORWARDING) && ia) { 642 if (m->m_pkthdr.csum_flags & CSUM_TSO) 643 counter_u64_add(ia->ia_ifa.ifa_opackets, 644 m->m_pkthdr.len / m->m_pkthdr.tso_segsz); 645 else 646 counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 647 648 counter_u64_add(ia->ia_ifa.ifa_obytes, m->m_pkthdr.len); 649 } 650#ifdef MBUF_STRESS_TEST 651 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 652 m = m_fragment(m, M_NOWAIT, mbuf_frag_size); 653#endif 654 /* 655 * Reset layer specific mbuf flags 656 * to avoid confusing lower layers. 657 */ 658 m_clrprotoflags(m); 659 IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 660 error = (*ifp->if_output)(ifp, m, 661 (const struct sockaddr *)gw, ro); 662 goto done; 663 } 664 665 /* Balk when DF bit is set or the interface didn't support TSO. */ 666 if ((ip_off & IP_DF) || (m->m_pkthdr.csum_flags & CSUM_TSO)) { 667 error = EMSGSIZE; 668 IPSTAT_INC(ips_cantfrag); 669 goto bad; 670 } 671 672 /* 673 * Too large for interface; fragment if possible. If successful, 674 * on return, m will point to a list of packets to be sent. 675 */ 676 error = ip_fragment(ip, &m, mtu, ifp->if_hwassist); 677 if (error) 678 goto bad; 679 for (; m; m = m0) { 680 m0 = m->m_nextpkt; 681 m->m_nextpkt = 0; 682 if (error == 0) { 683 /* Record statistics for this interface address. */ 684 if (ia != NULL) { 685 counter_u64_add(ia->ia_ifa.ifa_opackets, 1); 686 counter_u64_add(ia->ia_ifa.ifa_obytes, 687 m->m_pkthdr.len); 688 } 689 /* 690 * Reset layer specific mbuf flags 691 * to avoid confusing upper layers. 692 */ 693 m_clrprotoflags(m); 694 695 IP_PROBE(send, NULL, NULL, ip, ifp, ip, NULL); 696 error = (*ifp->if_output)(ifp, m, 697 (const struct sockaddr *)gw, ro); 698 } else 699 m_freem(m); 700 } 701 702 if (error == 0) 703 IPSTAT_INC(ips_fragmented); 704 705done: 706 if (ro == &iproute) 707 RO_RTFREE(ro); 708 if (have_ia_ref) 709 ifa_free(&ia->ia_ifa); 710 return (error); 711bad: 712 m_freem(m); 713 goto done; 714} 715 716/* 717 * Create a chain of fragments which fit the given mtu. m_frag points to the 718 * mbuf to be fragmented; on return it points to the chain with the fragments. 719 * Return 0 if no error. If error, m_frag may contain a partially built 720 * chain of fragments that should be freed by the caller. 721 * 722 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 723 */ 724int 725ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 726 u_long if_hwassist_flags) 727{ 728 int error = 0; 729 int hlen = ip->ip_hl << 2; 730 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 731 int off; 732 struct mbuf *m0 = *m_frag; /* the original packet */ 733 int firstlen; 734 struct mbuf **mnext; 735 int nfrags; 736 uint16_t ip_len, ip_off; 737 738 ip_len = ntohs(ip->ip_len); 739 ip_off = ntohs(ip->ip_off); 740 741 if (ip_off & IP_DF) { /* Fragmentation not allowed */ 742 IPSTAT_INC(ips_cantfrag); 743 return EMSGSIZE; 744 } 745 746 /* 747 * Must be able to put at least 8 bytes per fragment. 748 */ 749 if (len < 8) 750 return EMSGSIZE; 751 752 /* 753 * If the interface will not calculate checksums on 754 * fragmented packets, then do it here. 755 */ 756 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 757 in_delayed_cksum(m0); 758 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 759 } 760#ifdef SCTP 761 if (m0->m_pkthdr.csum_flags & CSUM_SCTP) { 762 sctp_delayed_cksum(m0, hlen); 763 m0->m_pkthdr.csum_flags &= ~CSUM_SCTP; 764 } 765#endif 766 if (len > PAGE_SIZE) { 767 /* 768 * Fragment large datagrams such that each segment 769 * contains a multiple of PAGE_SIZE amount of data, 770 * plus headers. This enables a receiver to perform 771 * page-flipping zero-copy optimizations. 772 * 773 * XXX When does this help given that sender and receiver 774 * could have different page sizes, and also mtu could 775 * be less than the receiver's page size ? 776 */ 777 int newlen; 778 struct mbuf *m; 779 780 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 781 off += m->m_len; 782 783 /* 784 * firstlen (off - hlen) must be aligned on an 785 * 8-byte boundary 786 */ 787 if (off < hlen) 788 goto smart_frag_failure; 789 off = ((off - hlen) & ~7) + hlen; 790 newlen = (~PAGE_MASK) & mtu; 791 if ((newlen + sizeof (struct ip)) > mtu) { 792 /* we failed, go back the default */ 793smart_frag_failure: 794 newlen = len; 795 off = hlen + len; 796 } 797 len = newlen; 798 799 } else { 800 off = hlen + len; 801 } 802 803 firstlen = off - hlen; 804 mnext = &m0->m_nextpkt; /* pointer to next packet */ 805 806 /* 807 * Loop through length of segment after first fragment, 808 * make new header and copy data of each part and link onto chain. 809 * Here, m0 is the original packet, m is the fragment being created. 810 * The fragments are linked off the m_nextpkt of the original 811 * packet, which after processing serves as the first fragment. 812 */ 813 for (nfrags = 1; off < ip_len; off += len, nfrags++) { 814 struct ip *mhip; /* ip header on the fragment */ 815 struct mbuf *m; 816 int mhlen = sizeof (struct ip); 817 818 m = m_gethdr(M_NOWAIT, MT_DATA); 819 if (m == NULL) { 820 error = ENOBUFS; 821 IPSTAT_INC(ips_odropped); 822 goto done; 823 } 824 m->m_flags |= (m0->m_flags & M_MCAST); 825 /* 826 * In the first mbuf, leave room for the link header, then 827 * copy the original IP header including options. The payload 828 * goes into an additional mbuf chain returned by m_copym(). 829 */ 830 m->m_data += max_linkhdr; 831 mhip = mtod(m, struct ip *); 832 *mhip = *ip; 833 if (hlen > sizeof (struct ip)) { 834 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 835 mhip->ip_v = IPVERSION; 836 mhip->ip_hl = mhlen >> 2; 837 } 838 m->m_len = mhlen; 839 /* XXX do we need to add ip_off below ? */ 840 mhip->ip_off = ((off - hlen) >> 3) + ip_off; 841 if (off + len >= ip_len) 842 len = ip_len - off; 843 else 844 mhip->ip_off |= IP_MF; 845 mhip->ip_len = htons((u_short)(len + mhlen)); 846 m->m_next = m_copym(m0, off, len, M_NOWAIT); 847 if (m->m_next == NULL) { /* copy failed */ 848 m_free(m); 849 error = ENOBUFS; /* ??? */ 850 IPSTAT_INC(ips_odropped); 851 goto done; 852 } 853 m->m_pkthdr.len = mhlen + len; 854 m->m_pkthdr.rcvif = NULL; 855#ifdef MAC 856 mac_netinet_fragment(m0, m); 857#endif 858 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 859 mhip->ip_off = htons(mhip->ip_off); 860 mhip->ip_sum = 0; 861 if (m->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 862 mhip->ip_sum = in_cksum(m, mhlen); 863 m->m_pkthdr.csum_flags &= ~CSUM_IP; 864 } 865 *mnext = m; 866 mnext = &m->m_nextpkt; 867 } 868 IPSTAT_ADD(ips_ofragments, nfrags); 869 870 /* 871 * Update first fragment by trimming what's been copied out 872 * and updating header. 873 */ 874 m_adj(m0, hlen + firstlen - ip_len); 875 m0->m_pkthdr.len = hlen + firstlen; 876 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 877 ip->ip_off = htons(ip_off | IP_MF); 878 ip->ip_sum = 0; 879 if (m0->m_pkthdr.csum_flags & CSUM_IP & ~if_hwassist_flags) { 880 ip->ip_sum = in_cksum(m0, hlen); 881 m0->m_pkthdr.csum_flags &= ~CSUM_IP; 882 } 883 884done: 885 *m_frag = m0; 886 return error; 887} 888 889void 890in_delayed_cksum(struct mbuf *m) 891{ 892 struct ip *ip; 893 uint16_t csum, offset, ip_len; 894 895 ip = mtod(m, struct ip *); 896 offset = ip->ip_hl << 2 ; 897 ip_len = ntohs(ip->ip_len); 898 csum = in_cksum_skip(m, ip_len, offset); 899 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 900 csum = 0xffff; 901 offset += m->m_pkthdr.csum_data; /* checksum offset */ 902 903 /* find the mbuf in the chain where the checksum starts*/ 904 while ((m != NULL) && (offset >= m->m_len)) { 905 offset -= m->m_len; 906 m = m->m_next; 907 } 908 KASSERT(m != NULL, ("in_delayed_cksum: checksum outside mbuf chain.")); 909 KASSERT(offset + sizeof(u_short) <= m->m_len, ("in_delayed_cksum: checksum split between mbufs.")); 910 *(u_short *)(m->m_data + offset) = csum; 911} 912 913/* 914 * IP socket option processing. 915 */ 916int 917ip_ctloutput(struct socket *so, struct sockopt *sopt) 918{ 919 struct inpcb *inp = sotoinpcb(so); 920 int error, optval; 921#ifdef RSS 922 uint32_t rss_bucket; 923 int retval; 924#endif 925 926 error = optval = 0; 927 if (sopt->sopt_level != IPPROTO_IP) { 928 error = EINVAL; 929 930 if (sopt->sopt_level == SOL_SOCKET && 931 sopt->sopt_dir == SOPT_SET) { 932 switch (sopt->sopt_name) { 933 case SO_REUSEADDR: 934 INP_WLOCK(inp); 935 if ((so->so_options & SO_REUSEADDR) != 0) 936 inp->inp_flags2 |= INP_REUSEADDR; 937 else 938 inp->inp_flags2 &= ~INP_REUSEADDR; 939 INP_WUNLOCK(inp); 940 error = 0; 941 break; 942 case SO_REUSEPORT: 943 INP_WLOCK(inp); 944 if ((so->so_options & SO_REUSEPORT) != 0) 945 inp->inp_flags2 |= INP_REUSEPORT; 946 else 947 inp->inp_flags2 &= ~INP_REUSEPORT; 948 INP_WUNLOCK(inp); 949 error = 0; 950 break; 951 case SO_SETFIB: 952 INP_WLOCK(inp); 953 inp->inp_inc.inc_fibnum = so->so_fibnum; 954 INP_WUNLOCK(inp); 955 error = 0; 956 break; 957 default: 958 break; 959 } 960 } 961 return (error); 962 } 963 964 switch (sopt->sopt_dir) { 965 case SOPT_SET: 966 switch (sopt->sopt_name) { 967 case IP_OPTIONS: 968#ifdef notyet 969 case IP_RETOPTS: 970#endif 971 { 972 struct mbuf *m; 973 if (sopt->sopt_valsize > MLEN) { 974 error = EMSGSIZE; 975 break; 976 } 977 m = m_get(sopt->sopt_td ? M_WAITOK : M_NOWAIT, MT_DATA); 978 if (m == NULL) { 979 error = ENOBUFS; 980 break; 981 } 982 m->m_len = sopt->sopt_valsize; 983 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 984 m->m_len); 985 if (error) { 986 m_free(m); 987 break; 988 } 989 INP_WLOCK(inp); 990 error = ip_pcbopts(inp, sopt->sopt_name, m); 991 INP_WUNLOCK(inp); 992 return (error); 993 } 994 995 case IP_BINDANY: 996 if (sopt->sopt_td != NULL) { 997 error = priv_check(sopt->sopt_td, 998 PRIV_NETINET_BINDANY); 999 if (error) 1000 break; 1001 } 1002 /* FALLTHROUGH */ 1003 case IP_BINDMULTI: 1004#ifdef RSS 1005 case IP_RSS_LISTEN_BUCKET: 1006#endif 1007 case IP_TOS: 1008 case IP_TTL: 1009 case IP_MINTTL: 1010 case IP_RECVOPTS: 1011 case IP_RECVRETOPTS: 1012 case IP_RECVDSTADDR: 1013 case IP_RECVTTL: 1014 case IP_RECVIF: 1015 case IP_FAITH: 1016 case IP_ONESBCAST: 1017 case IP_DONTFRAG: 1018 case IP_RECVTOS: 1019 case IP_RECVFLOWID: 1020#ifdef RSS 1021 case IP_RECVRSSBUCKETID: 1022#endif 1023 error = sooptcopyin(sopt, &optval, sizeof optval, 1024 sizeof optval); 1025 if (error) 1026 break; 1027 1028 switch (sopt->sopt_name) { 1029 case IP_TOS: 1030 inp->inp_ip_tos = optval; 1031 break; 1032 1033 case IP_TTL: 1034 inp->inp_ip_ttl = optval; 1035 break; 1036 1037 case IP_MINTTL: 1038 if (optval >= 0 && optval <= MAXTTL) 1039 inp->inp_ip_minttl = optval; 1040 else 1041 error = EINVAL; 1042 break; 1043 1044#define OPTSET(bit) do { \ 1045 INP_WLOCK(inp); \ 1046 if (optval) \ 1047 inp->inp_flags |= bit; \ 1048 else \ 1049 inp->inp_flags &= ~bit; \ 1050 INP_WUNLOCK(inp); \ 1051} while (0) 1052 1053#define OPTSET2(bit, val) do { \ 1054 INP_WLOCK(inp); \ 1055 if (val) \ 1056 inp->inp_flags2 |= bit; \ 1057 else \ 1058 inp->inp_flags2 &= ~bit; \ 1059 INP_WUNLOCK(inp); \ 1060} while (0) 1061 1062 case IP_RECVOPTS: 1063 OPTSET(INP_RECVOPTS); 1064 break; 1065 1066 case IP_RECVRETOPTS: 1067 OPTSET(INP_RECVRETOPTS); 1068 break; 1069 1070 case IP_RECVDSTADDR: 1071 OPTSET(INP_RECVDSTADDR); 1072 break; 1073 1074 case IP_RECVTTL: 1075 OPTSET(INP_RECVTTL); 1076 break; 1077 1078 case IP_RECVIF: 1079 OPTSET(INP_RECVIF); 1080 break; 1081 1082 case IP_FAITH: 1083 OPTSET(INP_FAITH); 1084 break; 1085 1086 case IP_ONESBCAST: 1087 OPTSET(INP_ONESBCAST); 1088 break; 1089 case IP_DONTFRAG: 1090 OPTSET(INP_DONTFRAG); 1091 break; 1092 case IP_BINDANY: 1093 OPTSET(INP_BINDANY); 1094 break; 1095 case IP_RECVTOS: 1096 OPTSET(INP_RECVTOS); 1097 break; 1098 case IP_BINDMULTI: 1099 OPTSET2(INP_BINDMULTI, optval); 1100 break; 1101 case IP_RECVFLOWID: 1102 OPTSET2(INP_RECVFLOWID, optval); 1103 break; 1104#ifdef RSS 1105 case IP_RSS_LISTEN_BUCKET: 1106 if ((optval >= 0) && 1107 (optval < rss_getnumbuckets())) { 1108 inp->inp_rss_listen_bucket = optval; 1109 OPTSET2(INP_RSS_BUCKET_SET, 1); 1110 } else { 1111 error = EINVAL; 1112 } 1113 break; 1114 case IP_RECVRSSBUCKETID: 1115 OPTSET2(INP_RECVRSSBUCKETID, optval); 1116 break; 1117#endif 1118 } 1119 break; 1120#undef OPTSET 1121#undef OPTSET2 1122 1123 /* 1124 * Multicast socket options are processed by the in_mcast 1125 * module. 1126 */ 1127 case IP_MULTICAST_IF: 1128 case IP_MULTICAST_VIF: 1129 case IP_MULTICAST_TTL: 1130 case IP_MULTICAST_LOOP: 1131 case IP_ADD_MEMBERSHIP: 1132 case IP_DROP_MEMBERSHIP: 1133 case IP_ADD_SOURCE_MEMBERSHIP: 1134 case IP_DROP_SOURCE_MEMBERSHIP: 1135 case IP_BLOCK_SOURCE: 1136 case IP_UNBLOCK_SOURCE: 1137 case IP_MSFILTER: 1138 case MCAST_JOIN_GROUP: 1139 case MCAST_LEAVE_GROUP: 1140 case MCAST_JOIN_SOURCE_GROUP: 1141 case MCAST_LEAVE_SOURCE_GROUP: 1142 case MCAST_BLOCK_SOURCE: 1143 case MCAST_UNBLOCK_SOURCE: 1144 error = inp_setmoptions(inp, sopt); 1145 break; 1146 1147 case IP_PORTRANGE: 1148 error = sooptcopyin(sopt, &optval, sizeof optval, 1149 sizeof optval); 1150 if (error) 1151 break; 1152 1153 INP_WLOCK(inp); 1154 switch (optval) { 1155 case IP_PORTRANGE_DEFAULT: 1156 inp->inp_flags &= ~(INP_LOWPORT); 1157 inp->inp_flags &= ~(INP_HIGHPORT); 1158 break; 1159 1160 case IP_PORTRANGE_HIGH: 1161 inp->inp_flags &= ~(INP_LOWPORT); 1162 inp->inp_flags |= INP_HIGHPORT; 1163 break; 1164 1165 case IP_PORTRANGE_LOW: 1166 inp->inp_flags &= ~(INP_HIGHPORT); 1167 inp->inp_flags |= INP_LOWPORT; 1168 break; 1169 1170 default: 1171 error = EINVAL; 1172 break; 1173 } 1174 INP_WUNLOCK(inp); 1175 break; 1176 1177#ifdef IPSEC 1178 case IP_IPSEC_POLICY: 1179 { 1180 caddr_t req; 1181 struct mbuf *m; 1182 1183 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1184 break; 1185 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1186 break; 1187 req = mtod(m, caddr_t); 1188 error = ipsec_set_policy(inp, sopt->sopt_name, req, 1189 m->m_len, (sopt->sopt_td != NULL) ? 1190 sopt->sopt_td->td_ucred : NULL); 1191 m_freem(m); 1192 break; 1193 } 1194#endif /* IPSEC */ 1195 1196 default: 1197 error = ENOPROTOOPT; 1198 break; 1199 } 1200 break; 1201 1202 case SOPT_GET: 1203 switch (sopt->sopt_name) { 1204 case IP_OPTIONS: 1205 case IP_RETOPTS: 1206 if (inp->inp_options) 1207 error = sooptcopyout(sopt, 1208 mtod(inp->inp_options, 1209 char *), 1210 inp->inp_options->m_len); 1211 else 1212 sopt->sopt_valsize = 0; 1213 break; 1214 1215 case IP_TOS: 1216 case IP_TTL: 1217 case IP_MINTTL: 1218 case IP_RECVOPTS: 1219 case IP_RECVRETOPTS: 1220 case IP_RECVDSTADDR: 1221 case IP_RECVTTL: 1222 case IP_RECVIF: 1223 case IP_PORTRANGE: 1224 case IP_FAITH: 1225 case IP_ONESBCAST: 1226 case IP_DONTFRAG: 1227 case IP_BINDANY: 1228 case IP_RECVTOS: 1229 case IP_BINDMULTI: 1230 case IP_FLOWID: 1231 case IP_FLOWTYPE: 1232 case IP_RECVFLOWID: 1233#ifdef RSS 1234 case IP_RSSBUCKETID: 1235 case IP_RECVRSSBUCKETID: 1236#endif 1237 switch (sopt->sopt_name) { 1238 1239 case IP_TOS: 1240 optval = inp->inp_ip_tos; 1241 break; 1242 1243 case IP_TTL: 1244 optval = inp->inp_ip_ttl; 1245 break; 1246 1247 case IP_MINTTL: 1248 optval = inp->inp_ip_minttl; 1249 break; 1250 1251#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1252#define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1253 1254 case IP_RECVOPTS: 1255 optval = OPTBIT(INP_RECVOPTS); 1256 break; 1257 1258 case IP_RECVRETOPTS: 1259 optval = OPTBIT(INP_RECVRETOPTS); 1260 break; 1261 1262 case IP_RECVDSTADDR: 1263 optval = OPTBIT(INP_RECVDSTADDR); 1264 break; 1265 1266 case IP_RECVTTL: 1267 optval = OPTBIT(INP_RECVTTL); 1268 break; 1269 1270 case IP_RECVIF: 1271 optval = OPTBIT(INP_RECVIF); 1272 break; 1273 1274 case IP_PORTRANGE: 1275 if (inp->inp_flags & INP_HIGHPORT) 1276 optval = IP_PORTRANGE_HIGH; 1277 else if (inp->inp_flags & INP_LOWPORT) 1278 optval = IP_PORTRANGE_LOW; 1279 else 1280 optval = 0; 1281 break; 1282 1283 case IP_FAITH: 1284 optval = OPTBIT(INP_FAITH); 1285 break; 1286 1287 case IP_ONESBCAST: 1288 optval = OPTBIT(INP_ONESBCAST); 1289 break; 1290 case IP_DONTFRAG: 1291 optval = OPTBIT(INP_DONTFRAG); 1292 break; 1293 case IP_BINDANY: 1294 optval = OPTBIT(INP_BINDANY); 1295 break; 1296 case IP_RECVTOS: 1297 optval = OPTBIT(INP_RECVTOS); 1298 break; 1299 case IP_FLOWID: 1300 optval = inp->inp_flowid; 1301 break; 1302 case IP_FLOWTYPE: 1303 optval = inp->inp_flowtype; 1304 break; 1305 case IP_RECVFLOWID: 1306 optval = OPTBIT2(INP_RECVFLOWID); 1307 break; 1308#ifdef RSS 1309 case IP_RSSBUCKETID: 1310 retval = rss_hash2bucket(inp->inp_flowid, 1311 inp->inp_flowtype, 1312 &rss_bucket); 1313 if (retval == 0) 1314 optval = rss_bucket; 1315 else 1316 error = EINVAL; 1317 break; 1318 case IP_RECVRSSBUCKETID: 1319 optval = OPTBIT2(INP_RECVRSSBUCKETID); 1320 break; 1321#endif 1322 case IP_BINDMULTI: 1323 optval = OPTBIT2(INP_BINDMULTI); 1324 break; 1325 } 1326 error = sooptcopyout(sopt, &optval, sizeof optval); 1327 break; 1328 1329 /* 1330 * Multicast socket options are processed by the in_mcast 1331 * module. 1332 */ 1333 case IP_MULTICAST_IF: 1334 case IP_MULTICAST_VIF: 1335 case IP_MULTICAST_TTL: 1336 case IP_MULTICAST_LOOP: 1337 case IP_MSFILTER: 1338 error = inp_getmoptions(inp, sopt); 1339 break; 1340 1341#ifdef IPSEC 1342 case IP_IPSEC_POLICY: 1343 { 1344 struct mbuf *m = NULL; 1345 caddr_t req = NULL; 1346 size_t len = 0; 1347 1348 if (m != 0) { 1349 req = mtod(m, caddr_t); 1350 len = m->m_len; 1351 } 1352 error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 1353 if (error == 0) 1354 error = soopt_mcopyout(sopt, m); /* XXX */ 1355 if (error == 0) 1356 m_freem(m); 1357 break; 1358 } 1359#endif /* IPSEC */ 1360 1361 default: 1362 error = ENOPROTOOPT; 1363 break; 1364 } 1365 break; 1366 } 1367 return (error); 1368} 1369 1370/* 1371 * Routine called from ip_output() to loop back a copy of an IP multicast 1372 * packet to the input queue of a specified interface. Note that this 1373 * calls the output routine of the loopback "driver", but with an interface 1374 * pointer that might NOT be a loopback interface -- evil, but easier than 1375 * replicating that code here. 1376 */ 1377static void 1378ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1379 int hlen) 1380{ 1381 register struct ip *ip; 1382 struct mbuf *copym; 1383 1384 /* 1385 * Make a deep copy of the packet because we're going to 1386 * modify the pack in order to generate checksums. 1387 */ 1388 copym = m_dup(m, M_NOWAIT); 1389 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1390 copym = m_pullup(copym, hlen); 1391 if (copym != NULL) { 1392 /* If needed, compute the checksum and mark it as valid. */ 1393 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1394 in_delayed_cksum(copym); 1395 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1396 copym->m_pkthdr.csum_flags |= 1397 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1398 copym->m_pkthdr.csum_data = 0xffff; 1399 } 1400 /* 1401 * We don't bother to fragment if the IP length is greater 1402 * than the interface's MTU. Can this possibly matter? 1403 */ 1404 ip = mtod(copym, struct ip *); 1405 ip->ip_sum = 0; 1406 ip->ip_sum = in_cksum(copym, hlen); 1407#if 1 /* XXX */ 1408 if (dst->sin_family != AF_INET) { 1409 printf("ip_mloopback: bad address family %d\n", 1410 dst->sin_family); 1411 dst->sin_family = AF_INET; 1412 } 1413#endif 1414 if_simloop(ifp, copym, dst->sin_family, 0); 1415 } 1416} 1417