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