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