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