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