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