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$"); 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 93 VNET_DEFINE(u_short, ip_id); 94 95 #ifdef MBUF_STRESS_TEST 96 static int mbuf_frag_size = 0; 97 SYSCTL_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 101 static void ip_mloopback 102 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 103 104 105 extern int in_mcast_loop; 106 extern 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 */ 120 int 121 ip_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 (inp->inp_flags & (INP_HW_FLOWID|INP_SW_FLOWID)) { 149 m->m_pkthdr.flowid = inp->inp_flowid; 150 M_HASHTYPE_SET(m, inp->inp_flowtype); 151 m->m_flags |= M_FLOWID; 152 } 153 } 154 155 if (ro == NULL) { 156 ro = &iproute; 157 bzero(ro, sizeof (*ro)); 158 } 159 160 #ifdef FLOWTABLE 161 if (ro->ro_rt == NULL) 162 (void )flowtable_lookup(AF_INET, m, ro); 163 #endif 164 165 if (opt) { 166 int len = 0; 167 m = ip_insertoptions(m, opt, &len); 168 if (len != 0) 169 hlen = len; /* ip->ip_hl is updated above */ 170 } 171 ip = mtod(m, struct ip *); 172 ip_len = ntohs(ip->ip_len); 173 ip_off = ntohs(ip->ip_off); 174 175 /* 176 * Fill in IP header. If we are not allowing fragmentation, 177 * then the ip_id field is meaningless, but we don't set it 178 * to zero. Doing so causes various problems when devices along 179 * the path (routers, load balancers, firewalls, etc.) illegally 180 * disable DF on our packet. Note that a 16-bit counter 181 * will wrap around in less than 10 seconds at 100 Mbit/s on a 182 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 183 * for Counting NATted Hosts", Proc. IMW'02, available at 184 * <http://www.cs.columbia.edu/~smb/papers/fnat.pdf>. 185 */ 186 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 187 ip->ip_v = IPVERSION; 188 ip->ip_hl = hlen >> 2; 189 ip->ip_id = ip_newid(); 190 IPSTAT_INC(ips_localout); 191 } else { 192 /* Header already set, fetch hlen from there */ 193 hlen = ip->ip_hl << 2; 194 } 195 196 /* 197 * dst/gw handling: 198 * 199 * dst can be rewritten but always points to &ro->ro_dst. 200 * gw is readonly but can point either to dst OR rt_gateway, 201 * therefore we need restore gw if we're redoing lookup. 202 */ 203 gw = dst = (struct sockaddr_in *)&ro->ro_dst; 204 again: 205 ia = NULL; 206 have_ia_ref = 0; 207 /* 208 * If there is a cached route, check that it is to the same 209 * destination and is still up. If not, free it and try again. 210 * The address family should also be checked in case of sharing 211 * the cache with IPv6. 212 */ 213 rte = ro->ro_rt; 214 if (rte && ((rte->rt_flags & RTF_UP) == 0 || 215 rte->rt_ifp == NULL || 216 !RT_LINK_IS_UP(rte->rt_ifp) || 217 dst->sin_family != AF_INET || 218 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 219 RO_RTFREE(ro); 220 ro->ro_lle = NULL; 221 rte = NULL; 222 gw = dst; 223 } 224 if (rte == NULL && fwd_tag == NULL) { 225 bzero(dst, sizeof(*dst)); 226 dst->sin_family = AF_INET; 227 dst->sin_len = sizeof(*dst); 228 dst->sin_addr = ip->ip_dst; 229 } 230 /* 231 * If routing to interface only, short circuit routing lookup. 232 * The use of an all-ones broadcast address implies this; an 233 * interface is specified by the broadcast address of an interface, 234 * or the destination address of a ptp interface. 235 */ 236 if (flags & IP_SENDONES) { 237 if ((ia = ifatoia(ifa_ifwithbroadaddr(sintosa(dst)))) == NULL && 238 (ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) { 239 IPSTAT_INC(ips_noroute); 240 error = ENETUNREACH; 241 goto bad; 242 } 243 have_ia_ref = 1; 244 ip->ip_dst.s_addr = INADDR_BROADCAST; 245 dst->sin_addr = ip->ip_dst; 246 ifp = ia->ia_ifp; 247 ip->ip_ttl = 1; 248 isbroadcast = 1; 249 } else if (flags & IP_ROUTETOIF) { 250 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 251 (ia = ifatoia(ifa_ifwithnet(sintosa(dst), 0))) == NULL) { 252 IPSTAT_INC(ips_noroute); 253 error = ENETUNREACH; 254 goto bad; 255 } 256 have_ia_ref = 1; 257 ifp = ia->ia_ifp; 258 ip->ip_ttl = 1; 259 isbroadcast = in_broadcast(dst->sin_addr, ifp); 260 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 261 imo != NULL && imo->imo_multicast_ifp != NULL) { 262 /* 263 * Bypass the normal routing lookup for multicast 264 * packets if the interface is specified. 265 */ 266 ifp = imo->imo_multicast_ifp; 267 IFP_TO_IA(ifp, ia); 268 if (ia) 269 have_ia_ref = 1; 270 isbroadcast = 0; /* fool gcc */ 271 } else { 272 /* 273 * We want to do any cloning requested by the link layer, 274 * as this is probably required in all cases for correct 275 * operation (as it is for ARP). 276 */ 277 if (rte == NULL) { 278 #ifdef RADIX_MPATH 279 rtalloc_mpath_fib(ro, 280 ntohl(ip->ip_src.s_addr ^ ip->ip_dst.s_addr), 281 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 282 #else 283 in_rtalloc_ign(ro, 0, 284 inp ? inp->inp_inc.inc_fibnum : M_GETFIB(m)); 285 #endif 286 rte = ro->ro_rt; 287 } 288 if (rte == NULL || 289 rte->rt_ifp == NULL || 290 !RT_LINK_IS_UP(rte->rt_ifp)) { 291 #ifdef IPSEC 292 /* 293 * There is no route for this packet, but it is 294 * possible that a matching SPD entry exists. 295 */ 296 no_route_but_check_spd = 1; 297 mtu = 0; /* Silence GCC warning. */ 298 goto sendit; 299 #endif 300 IPSTAT_INC(ips_noroute); 301 error = EHOSTUNREACH; 302 goto bad; 303 } 304 ia = ifatoia(rte->rt_ifa); 305 ifp = rte->rt_ifp; 306 counter_u64_add(rte->rt_pksent, 1); 307 if (rte->rt_flags & RTF_GATEWAY) 308 gw = (struct sockaddr_in *)rte->rt_gateway; 309 if (rte->rt_flags & RTF_HOST) 310 isbroadcast = (rte->rt_flags & RTF_BROADCAST); 311 else 312 isbroadcast = in_broadcast(gw->sin_addr, ifp); 313 } 314 /* 315 * Calculate MTU. If we have a route that is up, use that, 316 * otherwise use the interface's MTU. 317 */ 318 if (rte != NULL && (rte->rt_flags & (RTF_UP|RTF_HOST))) { 319 /* 320 * This case can happen if the user changed the MTU 321 * of an interface after enabling IP on it. Because 322 * most netifs don't keep track of routes pointing to 323 * them, there is no way for one to update all its 324 * routes when the MTU is changed. 325 */ 326 if (rte->rt_mtu > ifp->if_mtu) 327 rte->rt_mtu = ifp->if_mtu; 328 mtu = rte->rt_mtu; 329 } else { 330 mtu = ifp->if_mtu; 331 } 332 /* Catch a possible divide by zero later. */ 333 KASSERT(mtu > 0, ("%s: mtu %d <= 0, rte=%p (rt_flags=0x%08x) ifp=%p", 334 __func__, mtu, rte, (rte != NULL) ? rte->rt_flags : 0, ifp)); 335 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 336 m->m_flags |= M_MCAST; 337 /* 338 * IP destination address is multicast. Make sure "gw" 339 * still points to the address in "ro". (It may have been 340 * changed to point to a gateway address, above.) 341 */ 342 gw = dst; 343 /* 344 * See if the caller provided any multicast options 345 */ 346 if (imo != NULL) { 347 ip->ip_ttl = imo->imo_multicast_ttl; 348 if (imo->imo_multicast_vif != -1) 349 ip->ip_src.s_addr = 350 ip_mcast_src ? 351 ip_mcast_src(imo->imo_multicast_vif) : 352 INADDR_ANY; 353 } else 354 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 355 /* 356 * Confirm that the outgoing interface supports multicast. 357 */ 358 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 359 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 360 IPSTAT_INC(ips_noroute); 361 error = ENETUNREACH; 362 goto bad; 363 } 364 } 365 /* 366 * If source address not specified yet, use address 367 * of outgoing interface. 368 */ 369 if (ip->ip_src.s_addr == INADDR_ANY) { 370 /* Interface may have no addresses. */ 371 if (ia != NULL) 372 ip->ip_src = IA_SIN(ia)->sin_addr; 373 } 374 375 if ((imo == NULL && in_mcast_loop) || 376 (imo && imo->imo_multicast_loop)) { 377 /* 378 * Loop back multicast datagram if not expressly 379 * forbidden to do so, even if we are not a member 380 * of the group; ip_input() will filter it later, 381 * thus deferring a hash lookup and mutex acquisition 382 * at the expense of a cheap copy using m_copym(). 383 */ 384 ip_mloopback(ifp, m, dst, hlen); 385 } else { 386 /* 387 * If we are acting as a multicast router, perform 388 * multicast forwarding as if the packet had just 389 * arrived on the interface to which we are about 390 * to send. The multicast forwarding function 391 * recursively calls this function, using the 392 * IP_FORWARDING flag to prevent infinite recursion. 393 * 394 * Multicasts that are looped back by ip_mloopback(), 395 * above, will be forwarded by the ip_input() routine, 396 * if necessary. 397 */ 398 if (V_ip_mrouter && (flags & IP_FORWARDING) == 0) { 399 /* 400 * If rsvp daemon is not running, do not 401 * set ip_moptions. This ensures that the packet 402 * is multicast and not just sent down one link 403 * as prescribed by rsvpd. 404 */ 405 if (!V_rsvp_on) 406 imo = NULL; 407 if (ip_mforward && 408 ip_mforward(ip, ifp, m, imo) != 0) { 409 m_freem(m); 410 goto done; 411 } 412 } 413 } 414 415 /* 416 * Multicasts with a time-to-live of zero may be looped- 417 * back, above, but must not be transmitted on a network. 418 * Also, multicasts addressed to the loopback interface 419 * are not sent -- the above call to ip_mloopback() will 420 * loop back a copy. ip_input() will drop the copy if 421 * this host does not belong to the destination group on 422 * the loopback interface. 423 */ 424 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 425 m_freem(m); 426 goto done; 427 } 428 429 goto sendit; 430 } 431 432 /* 433 * If the source address is not specified yet, use the address 434 * of the outoing interface. 435 */ 436 if (ip->ip_src.s_addr == INADDR_ANY) { 437 /* Interface may have no addresses. */ 438 if (ia != NULL) { 439 ip->ip_src = IA_SIN(ia)->sin_addr; 440 } 441 } 442 443 /* 444 * Both in the SMP world, pre-emption world if_transmit() world, 445 * the following code doesn't really function as intended any further. 446 * 447 * + There can and will be multiple CPUs running this code path 448 * in parallel, and we do no lock holding when checking the 449 * queue depth; 450 * + And since other threads can be running concurrently, even if 451 * we do pass this check, another thread may queue some frames 452 * before this thread does and it will end up partially or fully 453 * failing to send anyway; 454 * + if_transmit() based drivers don't necessarily set ifq_len 455 * at all. 456 * 457 * This should be replaced with a method of pushing an entire list 458 * of fragment frames to the driver and have the driver decide 459 * whether it can queue or not queue the entire set. 460 */ 461 #if 0 462 /* 463 * Verify that we have any chance at all of being able to queue the 464 * packet or packet fragments, unless ALTQ is enabled on the given 465 * interface in which case packetdrop should be done by queueing. 466 */ 467 n = ip_len / mtu + 1; /* how many fragments ? */ 468 if ( 469 #ifdef ALTQ 470 (!ALTQ_IS_ENABLED(&ifp->if_snd)) && 471 #endif /* ALTQ */ 472 (ifp->if_snd.ifq_len + n) >= ifp->if_snd.ifq_maxlen ) { 473 error = ENOBUFS; 474 IPSTAT_INC(ips_odropped); 475 ifp->if_snd.ifq_drops += n; 476 goto bad; 477 } 478 #endif 479 480 /* 481 * Look for broadcast address and 482 * verify user is allowed to send 483 * such a packet. 484 */ 485 if (isbroadcast) { 486 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 487 error = EADDRNOTAVAIL; 488 goto bad; 489 } 490 if ((flags & IP_ALLOWBROADCAST) == 0) { 491 error = EACCES; 492 goto bad; 493 } 494 /* don't allow broadcast messages to be fragmented */ 495 if (ip_len > mtu) { 496 error = EMSGSIZE; 497 goto bad; 498 } 499 m->m_flags |= M_BCAST; 500 } else { 501 m->m_flags &= ~M_BCAST; 502 } 503 504 sendit: 505 #ifdef IPSEC 506 switch(ip_ipsec_output(&m, inp, &flags, &error)) { 507 case 1: 508 goto bad; 509 case -1: 510 goto done; 511 case 0: 512 default: 513 break; /* Continue with packet processing. */ 514 } 515 /* 516 * Check if there was a route for this packet; return error if not. 517 */ 518 if (no_route_but_check_spd) { 519 IPSTAT_INC(ips_noroute); 520 error = EHOSTUNREACH; 521 goto bad; 522 } 523 /* Update variables that are affected by ipsec4_output(). */ 524 ip = mtod(m, struct ip *); 525 hlen = ip->ip_hl << 2; 526 #endif /* IPSEC */ 527 528 /* Jump over all PFIL processing if hooks are not active. */ 529 if (!PFIL_HOOKED(&V_inet_pfil_hook)) 530 goto passout; 531 532 /* Run through list of hooks for output packets. */ 533 odst.s_addr = ip->ip_dst.s_addr; 534 error = pfil_run_hooks(&V_inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 535 if (error != 0 || m == NULL) 536 goto done; 537 538 ip = mtod(m, struct ip *); 539 540 /* See if destination IP address was changed by packet filter. */ 541 if (odst.s_addr != ip->ip_dst.s_addr) { 542 m->m_flags |= M_SKIP_FIREWALL; 543 /* If destination is now ourself drop to ip_input(). */ 544 if (in_localip(ip->ip_dst)) { 545 m->m_flags |= M_FASTFWD_OURS; 546 if (m->m_pkthdr.rcvif == NULL) 547 m->m_pkthdr.rcvif = V_loif; 548 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 549 m->m_pkthdr.csum_flags |= 550 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 551 m->m_pkthdr.csum_data = 0xffff; 552 } 553 m->m_pkthdr.csum_flags |= 554 CSUM_IP_CHECKED | CSUM_IP_VALID; 555 #ifdef SCTP 556 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 557 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 558 #endif 559 error = netisr_queue(NETISR_IP, m); 560 goto done; 561 } else { 562 if (have_ia_ref) 563 ifa_free(&ia->ia_ifa); 564 goto again; /* Redo the routing table lookup. */ 565 } 566 } 567 568 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 569 if (m->m_flags & M_FASTFWD_OURS) { 570 if (m->m_pkthdr.rcvif == NULL) 571 m->m_pkthdr.rcvif = V_loif; 572 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 573 m->m_pkthdr.csum_flags |= 574 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 575 m->m_pkthdr.csum_data = 0xffff; 576 } 577 #ifdef SCTP 578 if (m->m_pkthdr.csum_flags & CSUM_SCTP) 579 m->m_pkthdr.csum_flags |= CSUM_SCTP_VALID; 580 #endif 581 m->m_pkthdr.csum_flags |= 582 CSUM_IP_CHECKED | CSUM_IP_VALID; 583 584 error = netisr_queue(NETISR_IP, m); 585 goto done; 586 } 587 /* Or forward to some other address? */ 588 if ((m->m_flags & M_IP_NEXTHOP) && 589 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) { 590 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 591 m->m_flags |= M_SKIP_FIREWALL; 592 m->m_flags &= ~M_IP_NEXTHOP; 593 m_tag_delete(m, fwd_tag); 594 if (have_ia_ref) 595 ifa_free(&ia->ia_ifa); 596 goto again; 597 } 598 599 passout: 600 /* 127/8 must not appear on wire - RFC1122. */ 601 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 602 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 603 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 604 IPSTAT_INC(ips_badaddr); 605 error = EADDRNOTAVAIL; 606 goto bad; 607 } 608 } 609 610 m->m_pkthdr.csum_flags |= CSUM_IP; 611 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA & ~ifp->if_hwassist) { 612 in_delayed_cksum(m); 613 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 614 } 615 #ifdef SCTP 616 if (m->m_pkthdr.csum_flags & CSUM_SCTP & ~ifp->if_hwassist) { 617 sctp_delayed_cksum(m, (uint32_t)(ip->ip_hl << 2)); 618 m->m_pkthdr.csum_flags &= ~CSUM_SCTP; 619 } 620 #endif 621 622 /* 623 * If small enough for interface, or the interface will take 624 * care of the fragmentation for us, we can just send directly. 625 */ 626 if (ip_len <= mtu || 627 (m->m_pkthdr.csum_flags & ifp->if_hwassist & CSUM_TSO) != 0 || 628 ((ip_off & IP_DF) == 0 && (ifp->if_hwassist & CSUM_FRAGMENT))) { 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 705 done: 706 if (ro == &iproute) 707 RO_RTFREE(ro); 708 if (have_ia_ref) 709 ifa_free(&ia->ia_ifa); 710 return (error); 711 bad: 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 */ 724 int 725 ip_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 */ 793 smart_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 884 done: 885 *m_frag = m0; 886 return error; 887 } 888 889 void 890 in_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 */ 916 int 917 ip_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 error = sooptcopyin(sopt, &optval, sizeof optval, 1020 sizeof optval); 1021 if (error) 1022 break; 1023 1024 switch (sopt->sopt_name) { 1025 case IP_TOS: 1026 inp->inp_ip_tos = optval; 1027 break; 1028 1029 case IP_TTL: 1030 inp->inp_ip_ttl = optval; 1031 break; 1032 1033 case IP_MINTTL: 1034 if (optval >= 0 && optval <= MAXTTL) 1035 inp->inp_ip_minttl = optval; 1036 else 1037 error = EINVAL; 1038 break; 1039 1040 #define OPTSET(bit) do { \ 1041 INP_WLOCK(inp); \ 1042 if (optval) \ 1043 inp->inp_flags |= bit; \ 1044 else \ 1045 inp->inp_flags &= ~bit; \ 1046 INP_WUNLOCK(inp); \ 1047 } while (0) 1048 1049 #define OPTSET2(bit, val) do { \ 1050 INP_WLOCK(inp); \ 1051 if (val) \ 1052 inp->inp_flags2 |= bit; \ 1053 else \ 1054 inp->inp_flags2 &= ~bit; \ 1055 INP_WUNLOCK(inp); \ 1056 } while (0) 1057 1058 case IP_RECVOPTS: 1059 OPTSET(INP_RECVOPTS); 1060 break; 1061 1062 case IP_RECVRETOPTS: 1063 OPTSET(INP_RECVRETOPTS); 1064 break; 1065 1066 case IP_RECVDSTADDR: 1067 OPTSET(INP_RECVDSTADDR); 1068 break; 1069 1070 case IP_RECVTTL: 1071 OPTSET(INP_RECVTTL); 1072 break; 1073 1074 case IP_RECVIF: 1075 OPTSET(INP_RECVIF); 1076 break; 1077 1078 case IP_FAITH: 1079 OPTSET(INP_FAITH); 1080 break; 1081 1082 case IP_ONESBCAST: 1083 OPTSET(INP_ONESBCAST); 1084 break; 1085 case IP_DONTFRAG: 1086 OPTSET(INP_DONTFRAG); 1087 break; 1088 case IP_BINDANY: 1089 OPTSET(INP_BINDANY); 1090 break; 1091 case IP_RECVTOS: 1092 OPTSET(INP_RECVTOS); 1093 break; 1094 case IP_BINDMULTI: 1095 OPTSET2(INP_BINDMULTI, optval); 1096 break; 1097 #ifdef RSS 1098 case IP_RSS_LISTEN_BUCKET: 1099 if ((optval >= 0) && 1100 (optval < rss_getnumbuckets())) { 1101 inp->inp_rss_listen_bucket = optval; 1102 OPTSET2(INP_RSS_BUCKET_SET, 1); 1103 } else { 1104 error = EINVAL; 1105 } 1106 break; 1107 #endif 1108 } 1109 break; 1110 #undef OPTSET 1111 #undef OPTSET2 1112 1113 /* 1114 * Multicast socket options are processed by the in_mcast 1115 * module. 1116 */ 1117 case IP_MULTICAST_IF: 1118 case IP_MULTICAST_VIF: 1119 case IP_MULTICAST_TTL: 1120 case IP_MULTICAST_LOOP: 1121 case IP_ADD_MEMBERSHIP: 1122 case IP_DROP_MEMBERSHIP: 1123 case IP_ADD_SOURCE_MEMBERSHIP: 1124 case IP_DROP_SOURCE_MEMBERSHIP: 1125 case IP_BLOCK_SOURCE: 1126 case IP_UNBLOCK_SOURCE: 1127 case IP_MSFILTER: 1128 case MCAST_JOIN_GROUP: 1129 case MCAST_LEAVE_GROUP: 1130 case MCAST_JOIN_SOURCE_GROUP: 1131 case MCAST_LEAVE_SOURCE_GROUP: 1132 case MCAST_BLOCK_SOURCE: 1133 case MCAST_UNBLOCK_SOURCE: 1134 error = inp_setmoptions(inp, sopt); 1135 break; 1136 1137 case IP_PORTRANGE: 1138 error = sooptcopyin(sopt, &optval, sizeof optval, 1139 sizeof optval); 1140 if (error) 1141 break; 1142 1143 INP_WLOCK(inp); 1144 switch (optval) { 1145 case IP_PORTRANGE_DEFAULT: 1146 inp->inp_flags &= ~(INP_LOWPORT); 1147 inp->inp_flags &= ~(INP_HIGHPORT); 1148 break; 1149 1150 case IP_PORTRANGE_HIGH: 1151 inp->inp_flags &= ~(INP_LOWPORT); 1152 inp->inp_flags |= INP_HIGHPORT; 1153 break; 1154 1155 case IP_PORTRANGE_LOW: 1156 inp->inp_flags &= ~(INP_HIGHPORT); 1157 inp->inp_flags |= INP_LOWPORT; 1158 break; 1159 1160 default: 1161 error = EINVAL; 1162 break; 1163 } 1164 INP_WUNLOCK(inp); 1165 break; 1166 1167 #ifdef IPSEC 1168 case IP_IPSEC_POLICY: 1169 { 1170 caddr_t req; 1171 struct mbuf *m; 1172 1173 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1174 break; 1175 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1176 break; 1177 req = mtod(m, caddr_t); 1178 error = ipsec_set_policy(inp, sopt->sopt_name, req, 1179 m->m_len, (sopt->sopt_td != NULL) ? 1180 sopt->sopt_td->td_ucred : NULL); 1181 m_freem(m); 1182 break; 1183 } 1184 #endif /* IPSEC */ 1185 1186 default: 1187 error = ENOPROTOOPT; 1188 break; 1189 } 1190 break; 1191 1192 case SOPT_GET: 1193 switch (sopt->sopt_name) { 1194 case IP_OPTIONS: 1195 case IP_RETOPTS: 1196 if (inp->inp_options) 1197 error = sooptcopyout(sopt, 1198 mtod(inp->inp_options, 1199 char *), 1200 inp->inp_options->m_len); 1201 else 1202 sopt->sopt_valsize = 0; 1203 break; 1204 1205 case IP_TOS: 1206 case IP_TTL: 1207 case IP_MINTTL: 1208 case IP_RECVOPTS: 1209 case IP_RECVRETOPTS: 1210 case IP_RECVDSTADDR: 1211 case IP_RECVTTL: 1212 case IP_RECVIF: 1213 case IP_PORTRANGE: 1214 case IP_FAITH: 1215 case IP_ONESBCAST: 1216 case IP_DONTFRAG: 1217 case IP_BINDANY: 1218 case IP_RECVTOS: 1219 case IP_BINDMULTI: 1220 case IP_FLOWID: 1221 case IP_FLOWTYPE: 1222 #ifdef RSS 1223 case IP_RSSBUCKETID: 1224 #endif 1225 switch (sopt->sopt_name) { 1226 1227 case IP_TOS: 1228 optval = inp->inp_ip_tos; 1229 break; 1230 1231 case IP_TTL: 1232 optval = inp->inp_ip_ttl; 1233 break; 1234 1235 case IP_MINTTL: 1236 optval = inp->inp_ip_minttl; 1237 break; 1238 1239 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1240 #define OPTBIT2(bit) (inp->inp_flags2 & bit ? 1 : 0) 1241 1242 case IP_RECVOPTS: 1243 optval = OPTBIT(INP_RECVOPTS); 1244 break; 1245 1246 case IP_RECVRETOPTS: 1247 optval = OPTBIT(INP_RECVRETOPTS); 1248 break; 1249 1250 case IP_RECVDSTADDR: 1251 optval = OPTBIT(INP_RECVDSTADDR); 1252 break; 1253 1254 case IP_RECVTTL: 1255 optval = OPTBIT(INP_RECVTTL); 1256 break; 1257 1258 case IP_RECVIF: 1259 optval = OPTBIT(INP_RECVIF); 1260 break; 1261 1262 case IP_PORTRANGE: 1263 if (inp->inp_flags & INP_HIGHPORT) 1264 optval = IP_PORTRANGE_HIGH; 1265 else if (inp->inp_flags & INP_LOWPORT) 1266 optval = IP_PORTRANGE_LOW; 1267 else 1268 optval = 0; 1269 break; 1270 1271 case IP_FAITH: 1272 optval = OPTBIT(INP_FAITH); 1273 break; 1274 1275 case IP_ONESBCAST: 1276 optval = OPTBIT(INP_ONESBCAST); 1277 break; 1278 case IP_DONTFRAG: 1279 optval = OPTBIT(INP_DONTFRAG); 1280 break; 1281 case IP_BINDANY: 1282 optval = OPTBIT(INP_BINDANY); 1283 break; 1284 case IP_RECVTOS: 1285 optval = OPTBIT(INP_RECVTOS); 1286 break; 1287 case IP_FLOWID: 1288 optval = inp->inp_flowid; 1289 break; 1290 case IP_FLOWTYPE: 1291 optval = inp->inp_flowtype; 1292 break; 1293 #ifdef RSS 1294 case IP_RSSBUCKETID: 1295 retval = rss_hash2bucket(inp->inp_flowid, 1296 inp->inp_flowtype, 1297 &rss_bucket); 1298 if (retval == 0) 1299 optval = rss_bucket; 1300 else 1301 error = EINVAL; 1302 break; 1303 #endif 1304 case IP_BINDMULTI: 1305 optval = OPTBIT2(INP_BINDMULTI); 1306 break; 1307 } 1308 error = sooptcopyout(sopt, &optval, sizeof optval); 1309 break; 1310 1311 /* 1312 * Multicast socket options are processed by the in_mcast 1313 * module. 1314 */ 1315 case IP_MULTICAST_IF: 1316 case IP_MULTICAST_VIF: 1317 case IP_MULTICAST_TTL: 1318 case IP_MULTICAST_LOOP: 1319 case IP_MSFILTER: 1320 error = inp_getmoptions(inp, sopt); 1321 break; 1322 1323 #ifdef IPSEC 1324 case IP_IPSEC_POLICY: 1325 { 1326 struct mbuf *m = NULL; 1327 caddr_t req = NULL; 1328 size_t len = 0; 1329 1330 if (m != 0) { 1331 req = mtod(m, caddr_t); 1332 len = m->m_len; 1333 } 1334 error = ipsec_get_policy(sotoinpcb(so), req, len, &m); 1335 if (error == 0) 1336 error = soopt_mcopyout(sopt, m); /* XXX */ 1337 if (error == 0) 1338 m_freem(m); 1339 break; 1340 } 1341 #endif /* IPSEC */ 1342 1343 default: 1344 error = ENOPROTOOPT; 1345 break; 1346 } 1347 break; 1348 } 1349 return (error); 1350 } 1351 1352 /* 1353 * Routine called from ip_output() to loop back a copy of an IP multicast 1354 * packet to the input queue of a specified interface. Note that this 1355 * calls the output routine of the loopback "driver", but with an interface 1356 * pointer that might NOT be a loopback interface -- evil, but easier than 1357 * replicating that code here. 1358 */ 1359 static void 1360 ip_mloopback(struct ifnet *ifp, struct mbuf *m, struct sockaddr_in *dst, 1361 int hlen) 1362 { 1363 register struct ip *ip; 1364 struct mbuf *copym; 1365 1366 /* 1367 * Make a deep copy of the packet because we're going to 1368 * modify the pack in order to generate checksums. 1369 */ 1370 copym = m_dup(m, M_NOWAIT); 1371 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1372 copym = m_pullup(copym, hlen); 1373 if (copym != NULL) { 1374 /* If needed, compute the checksum and mark it as valid. */ 1375 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1376 in_delayed_cksum(copym); 1377 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1378 copym->m_pkthdr.csum_flags |= 1379 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1380 copym->m_pkthdr.csum_data = 0xffff; 1381 } 1382 /* 1383 * We don't bother to fragment if the IP length is greater 1384 * than the interface's MTU. Can this possibly matter? 1385 */ 1386 ip = mtod(copym, struct ip *); 1387 ip->ip_sum = 0; 1388 ip->ip_sum = in_cksum(copym, hlen); 1389 #if 1 /* XXX */ 1390 if (dst->sin_family != AF_INET) { 1391 printf("ip_mloopback: bad address family %d\n", 1392 dst->sin_family); 1393 dst->sin_family = AF_INET; 1394 } 1395 #endif 1396 if_simloop(ifp, copym, dst->sin_family, 0); 1397 } 1398 } 1399