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