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