ip_output.c revision 149371
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 * $FreeBSD: head/sys/netinet/ip_output.c 149371 2005-08-22 16:13:08Z andre $ 31 */ 32 33#include "opt_ipfw.h" 34#include "opt_ipsec.h" 35#include "opt_mac.h" 36#include "opt_mbuf_stress_test.h" 37 38#include <sys/param.h> 39#include <sys/systm.h> 40#include <sys/kernel.h> 41#include <sys/mac.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/protosw.h> 45#include <sys/socket.h> 46#include <sys/socketvar.h> 47#include <sys/sysctl.h> 48 49#include <net/if.h> 50#include <net/netisr.h> 51#include <net/pfil.h> 52#include <net/route.h> 53 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#include <netinet/ip.h> 57#include <netinet/in_pcb.h> 58#include <netinet/in_var.h> 59#include <netinet/ip_var.h> 60 61#include <machine/in_cksum.h> 62 63static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 64 65#ifdef IPSEC 66#include <netinet6/ipsec.h> 67#include <netkey/key.h> 68#ifdef IPSEC_DEBUG 69#include <netkey/key_debug.h> 70#else 71#define KEYDEBUG(lev,arg) 72#endif 73#endif /*IPSEC*/ 74 75#ifdef FAST_IPSEC 76#include <netipsec/ipsec.h> 77#include <netipsec/xform.h> 78#include <netipsec/key.h> 79#endif /*FAST_IPSEC*/ 80 81#define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 82 x, (ntohl(a.s_addr)>>24)&0xFF,\ 83 (ntohl(a.s_addr)>>16)&0xFF,\ 84 (ntohl(a.s_addr)>>8)&0xFF,\ 85 (ntohl(a.s_addr))&0xFF, y); 86 87u_short ip_id; 88 89#ifdef MBUF_STRESS_TEST 90int mbuf_frag_size = 0; 91SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 92 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 93#endif 94 95static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 96static struct ifnet *ip_multicast_if(struct in_addr *, int *); 97static void ip_mloopback 98 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 99static int ip_getmoptions(struct inpcb *, struct sockopt *); 100static int ip_pcbopts(struct inpcb *, int, struct mbuf *); 101static int ip_setmoptions(struct inpcb *, struct sockopt *); 102static struct ip_moptions *ip_findmoptions(struct inpcb *inp); 103 104int ip_optcopy(struct ip *, struct ip *); 105 106 107extern struct protosw inetsw[]; 108 109/* 110 * IP output. The packet in mbuf chain m contains a skeletal IP 111 * header (with len, off, ttl, proto, tos, src, dst). 112 * The mbuf chain containing the packet will be freed. 113 * The mbuf opt, if present, will not be freed. 114 * In the IP forwarding case, the packet will arrive with options already 115 * inserted, so must have a NULL opt pointer. 116 */ 117int 118ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, 119 int flags, struct ip_moptions *imo, struct inpcb *inp) 120{ 121 struct ip *ip; 122 struct ifnet *ifp = NULL; /* keep compiler happy */ 123 struct mbuf *m0; 124 int hlen = sizeof (struct ip); 125 int len, error = 0; 126 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 127 struct in_ifaddr *ia = NULL; 128 int isbroadcast, sw_csum; 129 struct route iproute; 130 struct in_addr odst; 131#ifdef IPFIREWALL_FORWARD 132 struct m_tag *fwd_tag = NULL; 133#endif 134#ifdef IPSEC 135 struct secpolicy *sp = NULL; 136#endif 137#ifdef FAST_IPSEC 138 struct secpolicy *sp = NULL; 139 struct tdb_ident *tdbi; 140 struct m_tag *mtag; 141 int s; 142#endif /* FAST_IPSEC */ 143 144 M_ASSERTPKTHDR(m); 145 146 if (ro == NULL) { 147 ro = &iproute; 148 bzero(ro, sizeof (*ro)); 149 } 150 151 if (inp != NULL) 152 INP_LOCK_ASSERT(inp); 153 154 if (opt) { 155 len = 0; 156 m = ip_insertoptions(m, opt, &len); 157 if (len != 0) 158 hlen = len; 159 } 160 ip = mtod(m, struct ip *); 161 162 /* 163 * Fill in IP header. If we are not allowing fragmentation, 164 * then the ip_id field is meaningless, but we don't set it 165 * to zero. Doing so causes various problems when devices along 166 * the path (routers, load balancers, firewalls, etc.) illegally 167 * disable DF on our packet. Note that a 16-bit counter 168 * will wrap around in less than 10 seconds at 100 Mbit/s on a 169 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 170 * for Counting NATted Hosts", Proc. IMW'02, available at 171 * <http://www.research.att.com/~smb/papers/fnat.pdf>. 172 */ 173 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 174 ip->ip_v = IPVERSION; 175 ip->ip_hl = hlen >> 2; 176 ip->ip_id = ip_newid(); 177 ipstat.ips_localout++; 178 } else { 179 hlen = ip->ip_hl << 2; 180 } 181 182 dst = (struct sockaddr_in *)&ro->ro_dst; 183again: 184 /* 185 * If there is a cached route, 186 * check that it is to the same destination 187 * and is still up. If not, free it and try again. 188 * The address family should also be checked in case of sharing the 189 * cache with IPv6. 190 */ 191 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 192 dst->sin_family != AF_INET || 193 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 194 RTFREE(ro->ro_rt); 195 ro->ro_rt = (struct rtentry *)0; 196 } 197#ifdef IPFIREWALL_FORWARD 198 if (ro->ro_rt == NULL && fwd_tag == NULL) { 199#else 200 if (ro->ro_rt == NULL) { 201#endif 202 bzero(dst, sizeof(*dst)); 203 dst->sin_family = AF_INET; 204 dst->sin_len = sizeof(*dst); 205 dst->sin_addr = ip->ip_dst; 206 } 207 /* 208 * If routing to interface only, 209 * short circuit routing lookup. 210 */ 211 if (flags & IP_ROUTETOIF) { 212 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 213 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 214 ipstat.ips_noroute++; 215 error = ENETUNREACH; 216 goto bad; 217 } 218 ifp = ia->ia_ifp; 219 ip->ip_ttl = 1; 220 isbroadcast = in_broadcast(dst->sin_addr, ifp); 221 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 222 imo != NULL && imo->imo_multicast_ifp != NULL) { 223 /* 224 * Bypass the normal routing lookup for multicast 225 * packets if the interface is specified. 226 */ 227 ifp = imo->imo_multicast_ifp; 228 IFP_TO_IA(ifp, ia); 229 isbroadcast = 0; /* fool gcc */ 230 } else { 231 /* 232 * We want to do any cloning requested by the link layer, 233 * as this is probably required in all cases for correct 234 * operation (as it is for ARP). 235 */ 236 if (ro->ro_rt == NULL) 237 rtalloc_ign(ro, 0); 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 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 254 struct in_multi *inm; 255 256 m->m_flags |= M_MCAST; 257 /* 258 * IP destination address is multicast. Make sure "dst" 259 * still points to the address in "ro". (It may have been 260 * changed to point to a gateway address, above.) 261 */ 262 dst = (struct sockaddr_in *)&ro->ro_dst; 263 /* 264 * See if the caller provided any multicast options 265 */ 266 if (imo != NULL) { 267 ip->ip_ttl = imo->imo_multicast_ttl; 268 if (imo->imo_multicast_vif != -1) 269 ip->ip_src.s_addr = 270 ip_mcast_src ? 271 ip_mcast_src(imo->imo_multicast_vif) : 272 INADDR_ANY; 273 } else 274 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 275 /* 276 * Confirm that the outgoing interface supports multicast. 277 */ 278 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 279 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 280 ipstat.ips_noroute++; 281 error = ENETUNREACH; 282 goto bad; 283 } 284 } 285 /* 286 * If source address not specified yet, use address 287 * of outgoing interface. 288 */ 289 if (ip->ip_src.s_addr == INADDR_ANY) { 290 /* Interface may have no addresses. */ 291 if (ia != NULL) 292 ip->ip_src = IA_SIN(ia)->sin_addr; 293 } 294 295 IN_MULTI_LOCK(); 296 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 297 if (inm != NULL && 298 (imo == NULL || imo->imo_multicast_loop)) { 299 IN_MULTI_UNLOCK(); 300 /* 301 * If we belong to the destination multicast group 302 * on the outgoing interface, and the caller did not 303 * forbid loopback, loop back a copy. 304 */ 305 ip_mloopback(ifp, m, dst, hlen); 306 } 307 else { 308 IN_MULTI_UNLOCK(); 309 /* 310 * If we are acting as a multicast router, perform 311 * multicast forwarding as if the packet had just 312 * arrived on the interface to which we are about 313 * to send. The multicast forwarding function 314 * recursively calls this function, using the 315 * IP_FORWARDING flag to prevent infinite recursion. 316 * 317 * Multicasts that are looped back by ip_mloopback(), 318 * above, will be forwarded by the ip_input() routine, 319 * if necessary. 320 */ 321 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 322 /* 323 * If rsvp daemon is not running, do not 324 * set ip_moptions. This ensures that the packet 325 * is multicast and not just sent down one link 326 * as prescribed by rsvpd. 327 */ 328 if (!rsvp_on) 329 imo = NULL; 330 if (ip_mforward && 331 ip_mforward(ip, ifp, m, imo) != 0) { 332 m_freem(m); 333 goto done; 334 } 335 } 336 } 337 338 /* 339 * Multicasts with a time-to-live of zero may be looped- 340 * back, above, but must not be transmitted on a network. 341 * Also, multicasts addressed to the loopback interface 342 * are not sent -- the above call to ip_mloopback() will 343 * loop back a copy if this host actually belongs to the 344 * destination group on the loopback interface. 345 */ 346 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 347 m_freem(m); 348 goto done; 349 } 350 351 goto sendit; 352 } 353#ifndef notdef 354 /* 355 * If the source address is not specified yet, use the address 356 * of the outoing interface. 357 */ 358 if (ip->ip_src.s_addr == INADDR_ANY) { 359 /* Interface may have no addresses. */ 360 if (ia != NULL) { 361 ip->ip_src = IA_SIN(ia)->sin_addr; 362 } 363 } 364#endif /* notdef */ 365 /* 366 * Verify that we have any chance at all of being able to queue the 367 * packet or packet fragments, unless ALTQ is enabled on the given 368 * interface in which case packetdrop should be done by queueing. 369 */ 370#ifdef ALTQ 371 if ((!ALTQ_IS_ENABLED(&ifp->if_snd)) && 372 ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 373 ifp->if_snd.ifq_maxlen)) 374#else 375 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 376 ifp->if_snd.ifq_maxlen) 377#endif /* ALTQ */ 378 { 379 error = ENOBUFS; 380 ipstat.ips_odropped++; 381 goto bad; 382 } 383 384 /* 385 * Look for broadcast address and 386 * verify user is allowed to send 387 * such a packet. 388 */ 389 if (isbroadcast) { 390 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 391 error = EADDRNOTAVAIL; 392 goto bad; 393 } 394 if ((flags & IP_ALLOWBROADCAST) == 0) { 395 error = EACCES; 396 goto bad; 397 } 398 /* don't allow broadcast messages to be fragmented */ 399 if (ip->ip_len > ifp->if_mtu) { 400 error = EMSGSIZE; 401 goto bad; 402 } 403 if (flags & IP_SENDONES) 404 ip->ip_dst.s_addr = INADDR_BROADCAST; 405 m->m_flags |= M_BCAST; 406 } else { 407 m->m_flags &= ~M_BCAST; 408 } 409 410sendit: 411#ifdef IPSEC 412 /* get SP for this packet */ 413 if (inp == NULL) 414 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 415 flags, &error); 416 else 417 sp = ipsec4_getpolicybypcb(m, IPSEC_DIR_OUTBOUND, inp, &error); 418 419 if (sp == NULL) { 420 ipsecstat.out_inval++; 421 goto bad; 422 } 423 424 error = 0; 425 426 /* check policy */ 427 switch (sp->policy) { 428 case IPSEC_POLICY_DISCARD: 429 /* 430 * This packet is just discarded. 431 */ 432 ipsecstat.out_polvio++; 433 goto bad; 434 435 case IPSEC_POLICY_BYPASS: 436 case IPSEC_POLICY_NONE: 437 case IPSEC_POLICY_TCP: 438 /* no need to do IPsec. */ 439 goto skip_ipsec; 440 441 case IPSEC_POLICY_IPSEC: 442 if (sp->req == NULL) { 443 /* acquire a policy */ 444 error = key_spdacquire(sp); 445 goto bad; 446 } 447 break; 448 449 case IPSEC_POLICY_ENTRUST: 450 default: 451 printf("ip_output: Invalid policy found. %d\n", sp->policy); 452 } 453 { 454 struct ipsec_output_state state; 455 bzero(&state, sizeof(state)); 456 state.m = m; 457 if (flags & IP_ROUTETOIF) { 458 state.ro = &iproute; 459 bzero(&iproute, sizeof(iproute)); 460 } else 461 state.ro = ro; 462 state.dst = (struct sockaddr *)dst; 463 464 ip->ip_sum = 0; 465 466 /* 467 * XXX 468 * delayed checksums are not currently compatible with IPsec 469 */ 470 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 471 in_delayed_cksum(m); 472 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 473 } 474 475 ip->ip_len = htons(ip->ip_len); 476 ip->ip_off = htons(ip->ip_off); 477 478 error = ipsec4_output(&state, sp, flags); 479 480 m = state.m; 481 if (flags & IP_ROUTETOIF) { 482 /* 483 * if we have tunnel mode SA, we may need to ignore 484 * IP_ROUTETOIF. 485 */ 486 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 487 flags &= ~IP_ROUTETOIF; 488 ro = state.ro; 489 } 490 } else 491 ro = state.ro; 492 dst = (struct sockaddr_in *)state.dst; 493 if (error) { 494 /* mbuf is already reclaimed in ipsec4_output. */ 495 m = NULL; 496 switch (error) { 497 case EHOSTUNREACH: 498 case ENETUNREACH: 499 case EMSGSIZE: 500 case ENOBUFS: 501 case ENOMEM: 502 break; 503 default: 504 printf("ip4_output (ipsec): error code %d\n", error); 505 /*fall through*/ 506 case ENOENT: 507 /* don't show these error codes to the user */ 508 error = 0; 509 break; 510 } 511 goto bad; 512 } 513 514 /* be sure to update variables that are affected by ipsec4_output() */ 515 ip = mtod(m, struct ip *); 516 hlen = ip->ip_hl << 2; 517 if (ro->ro_rt == NULL) { 518 if ((flags & IP_ROUTETOIF) == 0) { 519 printf("ip_output: " 520 "can't update route after IPsec processing\n"); 521 error = EHOSTUNREACH; /*XXX*/ 522 goto bad; 523 } 524 } else { 525 if (state.encap) { 526 ia = ifatoia(ro->ro_rt->rt_ifa); 527 ifp = ro->ro_rt->rt_ifp; 528 } 529 } 530 } 531 532 /* make it flipped, again. */ 533 ip->ip_len = ntohs(ip->ip_len); 534 ip->ip_off = ntohs(ip->ip_off); 535skip_ipsec: 536#endif /*IPSEC*/ 537#ifdef FAST_IPSEC 538 /* 539 * Check the security policy (SP) for the packet and, if 540 * required, do IPsec-related processing. There are two 541 * cases here; the first time a packet is sent through 542 * it will be untagged and handled by ipsec4_checkpolicy. 543 * If the packet is resubmitted to ip_output (e.g. after 544 * AH, ESP, etc. processing), there will be a tag to bypass 545 * the lookup and related policy checking. 546 */ 547 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 548 s = splnet(); 549 if (mtag != NULL) { 550 tdbi = (struct tdb_ident *)(mtag + 1); 551 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 552 if (sp == NULL) 553 error = -EINVAL; /* force silent drop */ 554 m_tag_delete(m, mtag); 555 } else { 556 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 557 &error, inp); 558 } 559 /* 560 * There are four return cases: 561 * sp != NULL apply IPsec policy 562 * sp == NULL, error == 0 no IPsec handling needed 563 * sp == NULL, error == -EINVAL discard packet w/o error 564 * sp == NULL, error != 0 discard packet, report error 565 */ 566 if (sp != NULL) { 567 /* Loop detection, check if ipsec processing already done */ 568 KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 569 for (mtag = m_tag_first(m); mtag != NULL; 570 mtag = m_tag_next(m, mtag)) { 571 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 572 continue; 573 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 574 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 575 continue; 576 /* 577 * Check if policy has an SA associated with it. 578 * This can happen when an SP has yet to acquire 579 * an SA; e.g. on first reference. If it occurs, 580 * then we let ipsec4_process_packet do its thing. 581 */ 582 if (sp->req->sav == NULL) 583 break; 584 tdbi = (struct tdb_ident *)(mtag + 1); 585 if (tdbi->spi == sp->req->sav->spi && 586 tdbi->proto == sp->req->sav->sah->saidx.proto && 587 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 588 sizeof (union sockaddr_union)) == 0) { 589 /* 590 * No IPsec processing is needed, free 591 * reference to SP. 592 * 593 * NB: null pointer to avoid free at 594 * done: below. 595 */ 596 KEY_FREESP(&sp), sp = NULL; 597 splx(s); 598 goto spd_done; 599 } 600 } 601 602 /* 603 * Do delayed checksums now because we send before 604 * this is done in the normal processing path. 605 */ 606 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 607 in_delayed_cksum(m); 608 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 609 } 610 611 ip->ip_len = htons(ip->ip_len); 612 ip->ip_off = htons(ip->ip_off); 613 614 /* NB: callee frees mbuf */ 615 error = ipsec4_process_packet(m, sp->req, flags, 0); 616 /* 617 * Preserve KAME behaviour: ENOENT can be returned 618 * when an SA acquire is in progress. Don't propagate 619 * this to user-level; it confuses applications. 620 * 621 * XXX this will go away when the SADB is redone. 622 */ 623 if (error == ENOENT) 624 error = 0; 625 splx(s); 626 goto done; 627 } else { 628 splx(s); 629 630 if (error != 0) { 631 /* 632 * Hack: -EINVAL is used to signal that a packet 633 * should be silently discarded. This is typically 634 * because we asked key management for an SA and 635 * it was delayed (e.g. kicked up to IKE). 636 */ 637 if (error == -EINVAL) 638 error = 0; 639 goto bad; 640 } else { 641 /* No IPsec processing for this packet. */ 642 } 643#ifdef notyet 644 /* 645 * If deferred crypto processing is needed, check that 646 * the interface supports it. 647 */ 648 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 649 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 650 /* notify IPsec to do its own crypto */ 651 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 652 error = EHOSTUNREACH; 653 goto bad; 654 } 655#endif 656 } 657spd_done: 658#endif /* FAST_IPSEC */ 659 660 /* Jump over all PFIL processing if hooks are not active. */ 661 if (inet_pfil_hook.ph_busy_count == -1) 662 goto passout; 663 664 /* Run through list of hooks for output packets. */ 665 odst.s_addr = ip->ip_dst.s_addr; 666 error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT, inp); 667 if (error != 0 || m == NULL) 668 goto done; 669 670 ip = mtod(m, struct ip *); 671 672 /* See if destination IP address was changed by packet filter. */ 673 if (odst.s_addr != ip->ip_dst.s_addr) { 674 m->m_flags |= M_SKIP_FIREWALL; 675 /* If destination is now ourself drop to ip_input(). */ 676 if (in_localip(ip->ip_dst)) { 677 m->m_flags |= M_FASTFWD_OURS; 678 if (m->m_pkthdr.rcvif == NULL) 679 m->m_pkthdr.rcvif = loif; 680 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 681 m->m_pkthdr.csum_flags |= 682 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 683 m->m_pkthdr.csum_data = 0xffff; 684 } 685 m->m_pkthdr.csum_flags |= 686 CSUM_IP_CHECKED | CSUM_IP_VALID; 687 688 error = netisr_queue(NETISR_IP, m); 689 goto done; 690 } else 691 goto again; /* Redo the routing table lookup. */ 692 } 693 694#ifdef IPFIREWALL_FORWARD 695 /* See if local, if yes, send it to netisr with IP_FASTFWD_OURS. */ 696 if (m->m_flags & M_FASTFWD_OURS) { 697 if (m->m_pkthdr.rcvif == NULL) 698 m->m_pkthdr.rcvif = loif; 699 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 700 m->m_pkthdr.csum_flags |= 701 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 702 m->m_pkthdr.csum_data = 0xffff; 703 } 704 m->m_pkthdr.csum_flags |= 705 CSUM_IP_CHECKED | CSUM_IP_VALID; 706 707 error = netisr_queue(NETISR_IP, m); 708 goto done; 709 } 710 /* Or forward to some other address? */ 711 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL); 712 if (fwd_tag) { 713#ifndef IPFIREWALL_FORWARD_EXTENDED 714 if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) { 715#endif 716 dst = (struct sockaddr_in *)&ro->ro_dst; 717 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 718 m->m_flags |= M_SKIP_FIREWALL; 719 m_tag_delete(m, fwd_tag); 720 goto again; 721#ifndef IPFIREWALL_FORWARD_EXTENDED 722 } else { 723 m_tag_delete(m, fwd_tag); 724 /* Continue. */ 725 } 726#endif 727 } 728#endif /* IPFIREWALL_FORWARD */ 729 730passout: 731 /* 127/8 must not appear on wire - RFC1122. */ 732 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 733 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 734 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 735 ipstat.ips_badaddr++; 736 error = EADDRNOTAVAIL; 737 goto bad; 738 } 739 } 740 741 m->m_pkthdr.csum_flags |= CSUM_IP; 742 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 743 if (sw_csum & CSUM_DELAY_DATA) { 744 in_delayed_cksum(m); 745 sw_csum &= ~CSUM_DELAY_DATA; 746 } 747 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 748 749 /* 750 * If small enough for interface, or the interface will take 751 * care of the fragmentation for us, can just send directly. 752 */ 753 if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT && 754 ((ip->ip_off & IP_DF) == 0))) { 755 ip->ip_len = htons(ip->ip_len); 756 ip->ip_off = htons(ip->ip_off); 757 ip->ip_sum = 0; 758 if (sw_csum & CSUM_DELAY_IP) 759 ip->ip_sum = in_cksum(m, hlen); 760 761 /* Record statistics for this interface address. */ 762 if (!(flags & IP_FORWARDING) && ia) { 763 ia->ia_ifa.if_opackets++; 764 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 765 } 766 767#ifdef IPSEC 768 /* clean ipsec history once it goes out of the node */ 769 ipsec_delaux(m); 770#endif 771 772#ifdef MBUF_STRESS_TEST 773 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 774 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 775#endif 776 error = (*ifp->if_output)(ifp, m, 777 (struct sockaddr *)dst, ro->ro_rt); 778 goto done; 779 } 780 781 if (ip->ip_off & IP_DF) { 782 error = EMSGSIZE; 783 /* 784 * This case can happen if the user changed the MTU 785 * of an interface after enabling IP on it. Because 786 * most netifs don't keep track of routes pointing to 787 * them, there is no way for one to update all its 788 * routes when the MTU is changed. 789 */ 790 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 791 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 792 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 793 } 794 ipstat.ips_cantfrag++; 795 goto bad; 796 } 797 798 /* 799 * Too large for interface; fragment if possible. If successful, 800 * on return, m will point to a list of packets to be sent. 801 */ 802 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 803 if (error) 804 goto bad; 805 for (; m; m = m0) { 806 m0 = m->m_nextpkt; 807 m->m_nextpkt = 0; 808#ifdef IPSEC 809 /* clean ipsec history once it goes out of the node */ 810 ipsec_delaux(m); 811#endif 812 if (error == 0) { 813 /* Record statistics for this interface address. */ 814 if (ia != NULL) { 815 ia->ia_ifa.if_opackets++; 816 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 817 } 818 819 error = (*ifp->if_output)(ifp, m, 820 (struct sockaddr *)dst, ro->ro_rt); 821 } else 822 m_freem(m); 823 } 824 825 if (error == 0) 826 ipstat.ips_fragmented++; 827 828done: 829 if (ro == &iproute && ro->ro_rt) { 830 RTFREE(ro->ro_rt); 831 } 832#ifdef IPSEC 833 if (sp != NULL) { 834 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 835 printf("DP ip_output call free SP:%p\n", sp)); 836 key_freesp(sp); 837 } 838#endif 839#ifdef FAST_IPSEC 840 if (sp != NULL) 841 KEY_FREESP(&sp); 842#endif 843 return (error); 844bad: 845 m_freem(m); 846 goto done; 847} 848 849/* 850 * Create a chain of fragments which fit the given mtu. m_frag points to the 851 * mbuf to be fragmented; on return it points to the chain with the fragments. 852 * Return 0 if no error. If error, m_frag may contain a partially built 853 * chain of fragments that should be freed by the caller. 854 * 855 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 856 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 857 */ 858int 859ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 860 u_long if_hwassist_flags, int sw_csum) 861{ 862 int error = 0; 863 int hlen = ip->ip_hl << 2; 864 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 865 int off; 866 struct mbuf *m0 = *m_frag; /* the original packet */ 867 int firstlen; 868 struct mbuf **mnext; 869 int nfrags; 870 871 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 872 ipstat.ips_cantfrag++; 873 return EMSGSIZE; 874 } 875 876 /* 877 * Must be able to put at least 8 bytes per fragment. 878 */ 879 if (len < 8) 880 return EMSGSIZE; 881 882 /* 883 * If the interface will not calculate checksums on 884 * fragmented packets, then do it here. 885 */ 886 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 887 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 888 in_delayed_cksum(m0); 889 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 890 } 891 892 if (len > PAGE_SIZE) { 893 /* 894 * Fragment large datagrams such that each segment 895 * contains a multiple of PAGE_SIZE amount of data, 896 * plus headers. This enables a receiver to perform 897 * page-flipping zero-copy optimizations. 898 * 899 * XXX When does this help given that sender and receiver 900 * could have different page sizes, and also mtu could 901 * be less than the receiver's page size ? 902 */ 903 int newlen; 904 struct mbuf *m; 905 906 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 907 off += m->m_len; 908 909 /* 910 * firstlen (off - hlen) must be aligned on an 911 * 8-byte boundary 912 */ 913 if (off < hlen) 914 goto smart_frag_failure; 915 off = ((off - hlen) & ~7) + hlen; 916 newlen = (~PAGE_MASK) & mtu; 917 if ((newlen + sizeof (struct ip)) > mtu) { 918 /* we failed, go back the default */ 919smart_frag_failure: 920 newlen = len; 921 off = hlen + len; 922 } 923 len = newlen; 924 925 } else { 926 off = hlen + len; 927 } 928 929 firstlen = off - hlen; 930 mnext = &m0->m_nextpkt; /* pointer to next packet */ 931 932 /* 933 * Loop through length of segment after first fragment, 934 * make new header and copy data of each part and link onto chain. 935 * Here, m0 is the original packet, m is the fragment being created. 936 * The fragments are linked off the m_nextpkt of the original 937 * packet, which after processing serves as the first fragment. 938 */ 939 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 940 struct ip *mhip; /* ip header on the fragment */ 941 struct mbuf *m; 942 int mhlen = sizeof (struct ip); 943 944 MGETHDR(m, M_DONTWAIT, MT_HEADER); 945 if (m == NULL) { 946 error = ENOBUFS; 947 ipstat.ips_odropped++; 948 goto done; 949 } 950 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 951 /* 952 * In the first mbuf, leave room for the link header, then 953 * copy the original IP header including options. The payload 954 * goes into an additional mbuf chain returned by m_copy(). 955 */ 956 m->m_data += max_linkhdr; 957 mhip = mtod(m, struct ip *); 958 *mhip = *ip; 959 if (hlen > sizeof (struct ip)) { 960 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 961 mhip->ip_v = IPVERSION; 962 mhip->ip_hl = mhlen >> 2; 963 } 964 m->m_len = mhlen; 965 /* XXX do we need to add ip->ip_off below ? */ 966 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 967 if (off + len >= ip->ip_len) { /* last fragment */ 968 len = ip->ip_len - off; 969 m->m_flags |= M_LASTFRAG; 970 } else 971 mhip->ip_off |= IP_MF; 972 mhip->ip_len = htons((u_short)(len + mhlen)); 973 m->m_next = m_copy(m0, off, len); 974 if (m->m_next == NULL) { /* copy failed */ 975 m_free(m); 976 error = ENOBUFS; /* ??? */ 977 ipstat.ips_odropped++; 978 goto done; 979 } 980 m->m_pkthdr.len = mhlen + len; 981 m->m_pkthdr.rcvif = NULL; 982#ifdef MAC 983 mac_create_fragment(m0, m); 984#endif 985 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 986 mhip->ip_off = htons(mhip->ip_off); 987 mhip->ip_sum = 0; 988 if (sw_csum & CSUM_DELAY_IP) 989 mhip->ip_sum = in_cksum(m, mhlen); 990 *mnext = m; 991 mnext = &m->m_nextpkt; 992 } 993 ipstat.ips_ofragments += nfrags; 994 995 /* set first marker for fragment chain */ 996 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 997 m0->m_pkthdr.csum_data = nfrags; 998 999 /* 1000 * Update first fragment by trimming what's been copied out 1001 * and updating header. 1002 */ 1003 m_adj(m0, hlen + firstlen - ip->ip_len); 1004 m0->m_pkthdr.len = hlen + firstlen; 1005 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1006 ip->ip_off |= IP_MF; 1007 ip->ip_off = htons(ip->ip_off); 1008 ip->ip_sum = 0; 1009 if (sw_csum & CSUM_DELAY_IP) 1010 ip->ip_sum = in_cksum(m0, hlen); 1011 1012done: 1013 *m_frag = m0; 1014 return error; 1015} 1016 1017void 1018in_delayed_cksum(struct mbuf *m) 1019{ 1020 struct ip *ip; 1021 u_short csum, offset; 1022 1023 ip = mtod(m, struct ip *); 1024 offset = ip->ip_hl << 2 ; 1025 csum = in_cksum_skip(m, ip->ip_len, offset); 1026 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1027 csum = 0xffff; 1028 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1029 1030 if (offset + sizeof(u_short) > m->m_len) { 1031 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1032 m->m_len, offset, ip->ip_p); 1033 /* 1034 * XXX 1035 * this shouldn't happen, but if it does, the 1036 * correct behavior may be to insert the checksum 1037 * in the existing chain instead of rearranging it. 1038 */ 1039 m = m_pullup(m, offset + sizeof(u_short)); 1040 } 1041 *(u_short *)(m->m_data + offset) = csum; 1042} 1043 1044/* 1045 * Insert IP options into preformed packet. 1046 * Adjust IP destination as required for IP source routing, 1047 * as indicated by a non-zero in_addr at the start of the options. 1048 * 1049 * XXX This routine assumes that the packet has no options in place. 1050 */ 1051static struct mbuf * 1052ip_insertoptions(m, opt, phlen) 1053 register struct mbuf *m; 1054 struct mbuf *opt; 1055 int *phlen; 1056{ 1057 register struct ipoption *p = mtod(opt, struct ipoption *); 1058 struct mbuf *n; 1059 register struct ip *ip = mtod(m, struct ip *); 1060 unsigned optlen; 1061 1062 optlen = opt->m_len - sizeof(p->ipopt_dst); 1063 if (optlen + ip->ip_len > IP_MAXPACKET) { 1064 *phlen = 0; 1065 return (m); /* XXX should fail */ 1066 } 1067 if (p->ipopt_dst.s_addr) 1068 ip->ip_dst = p->ipopt_dst; 1069 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1070 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1071 if (n == NULL) { 1072 *phlen = 0; 1073 return (m); 1074 } 1075 M_MOVE_PKTHDR(n, m); 1076 n->m_pkthdr.rcvif = NULL; 1077#ifdef MAC 1078 mac_copy_mbuf(m, n); 1079#endif 1080 n->m_pkthdr.len += optlen; 1081 m->m_len -= sizeof(struct ip); 1082 m->m_data += sizeof(struct ip); 1083 n->m_next = m; 1084 m = n; 1085 m->m_len = optlen + sizeof(struct ip); 1086 m->m_data += max_linkhdr; 1087 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1088 } else { 1089 m->m_data -= optlen; 1090 m->m_len += optlen; 1091 m->m_pkthdr.len += optlen; 1092 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1093 } 1094 ip = mtod(m, struct ip *); 1095 bcopy(p->ipopt_list, ip + 1, optlen); 1096 *phlen = sizeof(struct ip) + optlen; 1097 ip->ip_v = IPVERSION; 1098 ip->ip_hl = *phlen >> 2; 1099 ip->ip_len += optlen; 1100 return (m); 1101} 1102 1103/* 1104 * Copy options from ip to jp, 1105 * omitting those not copied during fragmentation. 1106 */ 1107int 1108ip_optcopy(ip, jp) 1109 struct ip *ip, *jp; 1110{ 1111 register u_char *cp, *dp; 1112 int opt, optlen, cnt; 1113 1114 cp = (u_char *)(ip + 1); 1115 dp = (u_char *)(jp + 1); 1116 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1117 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1118 opt = cp[0]; 1119 if (opt == IPOPT_EOL) 1120 break; 1121 if (opt == IPOPT_NOP) { 1122 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1123 *dp++ = IPOPT_NOP; 1124 optlen = 1; 1125 continue; 1126 } 1127 1128 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1129 ("ip_optcopy: malformed ipv4 option")); 1130 optlen = cp[IPOPT_OLEN]; 1131 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1132 ("ip_optcopy: malformed ipv4 option")); 1133 1134 /* bogus lengths should have been caught by ip_dooptions */ 1135 if (optlen > cnt) 1136 optlen = cnt; 1137 if (IPOPT_COPIED(opt)) { 1138 bcopy(cp, dp, optlen); 1139 dp += optlen; 1140 } 1141 } 1142 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1143 *dp++ = IPOPT_EOL; 1144 return (optlen); 1145} 1146 1147/* 1148 * IP socket option processing. 1149 */ 1150int 1151ip_ctloutput(so, sopt) 1152 struct socket *so; 1153 struct sockopt *sopt; 1154{ 1155 struct inpcb *inp = sotoinpcb(so); 1156 int error, optval; 1157 1158 error = optval = 0; 1159 if (sopt->sopt_level != IPPROTO_IP) { 1160 return (EINVAL); 1161 } 1162 1163 switch (sopt->sopt_dir) { 1164 case SOPT_SET: 1165 switch (sopt->sopt_name) { 1166 case IP_OPTIONS: 1167#ifdef notyet 1168 case IP_RETOPTS: 1169#endif 1170 { 1171 struct mbuf *m; 1172 if (sopt->sopt_valsize > MLEN) { 1173 error = EMSGSIZE; 1174 break; 1175 } 1176 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1177 if (m == NULL) { 1178 error = ENOBUFS; 1179 break; 1180 } 1181 m->m_len = sopt->sopt_valsize; 1182 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1183 m->m_len); 1184 INP_LOCK(inp); 1185 error = ip_pcbopts(inp, sopt->sopt_name, m); 1186 INP_UNLOCK(inp); 1187 return (error); 1188 } 1189 1190 case IP_TOS: 1191 case IP_TTL: 1192 case IP_MINTTL: 1193 case IP_RECVOPTS: 1194 case IP_RECVRETOPTS: 1195 case IP_RECVDSTADDR: 1196 case IP_RECVTTL: 1197 case IP_RECVIF: 1198 case IP_FAITH: 1199 case IP_ONESBCAST: 1200 error = sooptcopyin(sopt, &optval, sizeof optval, 1201 sizeof optval); 1202 if (error) 1203 break; 1204 1205 switch (sopt->sopt_name) { 1206 case IP_TOS: 1207 inp->inp_ip_tos = optval; 1208 break; 1209 1210 case IP_TTL: 1211 inp->inp_ip_ttl = optval; 1212 break; 1213 1214 case IP_MINTTL: 1215 if (optval > 0 && optval <= MAXTTL) 1216 inp->inp_ip_minttl = optval; 1217 else 1218 error = EINVAL; 1219 break; 1220 1221#define OPTSET(bit) do { \ 1222 INP_LOCK(inp); \ 1223 if (optval) \ 1224 inp->inp_flags |= bit; \ 1225 else \ 1226 inp->inp_flags &= ~bit; \ 1227 INP_UNLOCK(inp); \ 1228} while (0) 1229 1230 case IP_RECVOPTS: 1231 OPTSET(INP_RECVOPTS); 1232 break; 1233 1234 case IP_RECVRETOPTS: 1235 OPTSET(INP_RECVRETOPTS); 1236 break; 1237 1238 case IP_RECVDSTADDR: 1239 OPTSET(INP_RECVDSTADDR); 1240 break; 1241 1242 case IP_RECVTTL: 1243 OPTSET(INP_RECVTTL); 1244 break; 1245 1246 case IP_RECVIF: 1247 OPTSET(INP_RECVIF); 1248 break; 1249 1250 case IP_FAITH: 1251 OPTSET(INP_FAITH); 1252 break; 1253 1254 case IP_ONESBCAST: 1255 OPTSET(INP_ONESBCAST); 1256 break; 1257 } 1258 break; 1259#undef OPTSET 1260 1261 case IP_MULTICAST_IF: 1262 case IP_MULTICAST_VIF: 1263 case IP_MULTICAST_TTL: 1264 case IP_MULTICAST_LOOP: 1265 case IP_ADD_MEMBERSHIP: 1266 case IP_DROP_MEMBERSHIP: 1267 error = ip_setmoptions(inp, sopt); 1268 break; 1269 1270 case IP_PORTRANGE: 1271 error = sooptcopyin(sopt, &optval, sizeof optval, 1272 sizeof optval); 1273 if (error) 1274 break; 1275 1276 INP_LOCK(inp); 1277 switch (optval) { 1278 case IP_PORTRANGE_DEFAULT: 1279 inp->inp_flags &= ~(INP_LOWPORT); 1280 inp->inp_flags &= ~(INP_HIGHPORT); 1281 break; 1282 1283 case IP_PORTRANGE_HIGH: 1284 inp->inp_flags &= ~(INP_LOWPORT); 1285 inp->inp_flags |= INP_HIGHPORT; 1286 break; 1287 1288 case IP_PORTRANGE_LOW: 1289 inp->inp_flags &= ~(INP_HIGHPORT); 1290 inp->inp_flags |= INP_LOWPORT; 1291 break; 1292 1293 default: 1294 error = EINVAL; 1295 break; 1296 } 1297 INP_UNLOCK(inp); 1298 break; 1299 1300#if defined(IPSEC) || defined(FAST_IPSEC) 1301 case IP_IPSEC_POLICY: 1302 { 1303 caddr_t req; 1304 size_t len = 0; 1305 int priv; 1306 struct mbuf *m; 1307 int optname; 1308 1309 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1310 break; 1311 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1312 break; 1313 priv = (sopt->sopt_td != NULL && 1314 suser(sopt->sopt_td) != 0) ? 0 : 1; 1315 req = mtod(m, caddr_t); 1316 len = m->m_len; 1317 optname = sopt->sopt_name; 1318 error = ipsec4_set_policy(inp, optname, req, len, priv); 1319 m_freem(m); 1320 break; 1321 } 1322#endif /*IPSEC*/ 1323 1324 default: 1325 error = ENOPROTOOPT; 1326 break; 1327 } 1328 break; 1329 1330 case SOPT_GET: 1331 switch (sopt->sopt_name) { 1332 case IP_OPTIONS: 1333 case IP_RETOPTS: 1334 if (inp->inp_options) 1335 error = sooptcopyout(sopt, 1336 mtod(inp->inp_options, 1337 char *), 1338 inp->inp_options->m_len); 1339 else 1340 sopt->sopt_valsize = 0; 1341 break; 1342 1343 case IP_TOS: 1344 case IP_TTL: 1345 case IP_MINTTL: 1346 case IP_RECVOPTS: 1347 case IP_RECVRETOPTS: 1348 case IP_RECVDSTADDR: 1349 case IP_RECVTTL: 1350 case IP_RECVIF: 1351 case IP_PORTRANGE: 1352 case IP_FAITH: 1353 case IP_ONESBCAST: 1354 switch (sopt->sopt_name) { 1355 1356 case IP_TOS: 1357 optval = inp->inp_ip_tos; 1358 break; 1359 1360 case IP_TTL: 1361 optval = inp->inp_ip_ttl; 1362 break; 1363 1364 case IP_MINTTL: 1365 optval = inp->inp_ip_minttl; 1366 break; 1367 1368#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1369 1370 case IP_RECVOPTS: 1371 optval = OPTBIT(INP_RECVOPTS); 1372 break; 1373 1374 case IP_RECVRETOPTS: 1375 optval = OPTBIT(INP_RECVRETOPTS); 1376 break; 1377 1378 case IP_RECVDSTADDR: 1379 optval = OPTBIT(INP_RECVDSTADDR); 1380 break; 1381 1382 case IP_RECVTTL: 1383 optval = OPTBIT(INP_RECVTTL); 1384 break; 1385 1386 case IP_RECVIF: 1387 optval = OPTBIT(INP_RECVIF); 1388 break; 1389 1390 case IP_PORTRANGE: 1391 if (inp->inp_flags & INP_HIGHPORT) 1392 optval = IP_PORTRANGE_HIGH; 1393 else if (inp->inp_flags & INP_LOWPORT) 1394 optval = IP_PORTRANGE_LOW; 1395 else 1396 optval = 0; 1397 break; 1398 1399 case IP_FAITH: 1400 optval = OPTBIT(INP_FAITH); 1401 break; 1402 1403 case IP_ONESBCAST: 1404 optval = OPTBIT(INP_ONESBCAST); 1405 break; 1406 } 1407 error = sooptcopyout(sopt, &optval, sizeof optval); 1408 break; 1409 1410 case IP_MULTICAST_IF: 1411 case IP_MULTICAST_VIF: 1412 case IP_MULTICAST_TTL: 1413 case IP_MULTICAST_LOOP: 1414 case IP_ADD_MEMBERSHIP: 1415 case IP_DROP_MEMBERSHIP: 1416 error = ip_getmoptions(inp, sopt); 1417 break; 1418 1419#if defined(IPSEC) || defined(FAST_IPSEC) 1420 case IP_IPSEC_POLICY: 1421 { 1422 struct mbuf *m = NULL; 1423 caddr_t req = NULL; 1424 size_t len = 0; 1425 1426 if (m != 0) { 1427 req = mtod(m, caddr_t); 1428 len = m->m_len; 1429 } 1430 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1431 if (error == 0) 1432 error = soopt_mcopyout(sopt, m); /* XXX */ 1433 if (error == 0) 1434 m_freem(m); 1435 break; 1436 } 1437#endif /*IPSEC*/ 1438 1439 default: 1440 error = ENOPROTOOPT; 1441 break; 1442 } 1443 break; 1444 } 1445 return (error); 1446} 1447 1448/* 1449 * Set up IP options in pcb for insertion in output packets. 1450 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1451 * with destination address if source routed. 1452 */ 1453static int 1454ip_pcbopts(struct inpcb *inp, int optname, struct mbuf *m) 1455{ 1456 register int cnt, optlen; 1457 register u_char *cp; 1458 struct mbuf **pcbopt; 1459 u_char opt; 1460 1461 INP_LOCK_ASSERT(inp); 1462 1463 pcbopt = &inp->inp_options; 1464 1465 /* turn off any old options */ 1466 if (*pcbopt) 1467 (void)m_free(*pcbopt); 1468 *pcbopt = 0; 1469 if (m == NULL || m->m_len == 0) { 1470 /* 1471 * Only turning off any previous options. 1472 */ 1473 if (m != NULL) 1474 (void)m_free(m); 1475 return (0); 1476 } 1477 1478 if (m->m_len % sizeof(int32_t)) 1479 goto bad; 1480 /* 1481 * IP first-hop destination address will be stored before 1482 * actual options; move other options back 1483 * and clear it when none present. 1484 */ 1485 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1486 goto bad; 1487 cnt = m->m_len; 1488 m->m_len += sizeof(struct in_addr); 1489 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1490 bcopy(mtod(m, void *), cp, (unsigned)cnt); 1491 bzero(mtod(m, void *), sizeof(struct in_addr)); 1492 1493 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1494 opt = cp[IPOPT_OPTVAL]; 1495 if (opt == IPOPT_EOL) 1496 break; 1497 if (opt == IPOPT_NOP) 1498 optlen = 1; 1499 else { 1500 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1501 goto bad; 1502 optlen = cp[IPOPT_OLEN]; 1503 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1504 goto bad; 1505 } 1506 switch (opt) { 1507 1508 default: 1509 break; 1510 1511 case IPOPT_LSRR: 1512 case IPOPT_SSRR: 1513 /* 1514 * user process specifies route as: 1515 * ->A->B->C->D 1516 * D must be our final destination (but we can't 1517 * check that since we may not have connected yet). 1518 * A is first hop destination, which doesn't appear in 1519 * actual IP option, but is stored before the options. 1520 */ 1521 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1522 goto bad; 1523 m->m_len -= sizeof(struct in_addr); 1524 cnt -= sizeof(struct in_addr); 1525 optlen -= sizeof(struct in_addr); 1526 cp[IPOPT_OLEN] = optlen; 1527 /* 1528 * Move first hop before start of options. 1529 */ 1530 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1531 sizeof(struct in_addr)); 1532 /* 1533 * Then copy rest of options back 1534 * to close up the deleted entry. 1535 */ 1536 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1537 &cp[IPOPT_OFFSET+1], 1538 (unsigned)cnt - (IPOPT_MINOFF - 1)); 1539 break; 1540 } 1541 } 1542 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1543 goto bad; 1544 *pcbopt = m; 1545 return (0); 1546 1547bad: 1548 (void)m_free(m); 1549 return (EINVAL); 1550} 1551 1552/* 1553 * XXX 1554 * The whole multicast option thing needs to be re-thought. 1555 * Several of these options are equally applicable to non-multicast 1556 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1557 * standard option (IP_TTL). 1558 */ 1559 1560/* 1561 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1562 */ 1563static struct ifnet * 1564ip_multicast_if(a, ifindexp) 1565 struct in_addr *a; 1566 int *ifindexp; 1567{ 1568 int ifindex; 1569 struct ifnet *ifp; 1570 1571 if (ifindexp) 1572 *ifindexp = 0; 1573 if (ntohl(a->s_addr) >> 24 == 0) { 1574 ifindex = ntohl(a->s_addr) & 0xffffff; 1575 if (ifindex < 0 || if_index < ifindex) 1576 return NULL; 1577 ifp = ifnet_byindex(ifindex); 1578 if (ifindexp) 1579 *ifindexp = ifindex; 1580 } else { 1581 INADDR_TO_IFP(*a, ifp); 1582 } 1583 return ifp; 1584} 1585 1586/* 1587 * Given an inpcb, return its multicast options structure pointer. Accepts 1588 * an unlocked inpcb pointer, but will return it locked. May sleep. 1589 */ 1590static struct ip_moptions * 1591ip_findmoptions(struct inpcb *inp) 1592{ 1593 struct ip_moptions *imo; 1594 1595 INP_LOCK(inp); 1596 if (inp->inp_moptions != NULL) 1597 return (inp->inp_moptions); 1598 1599 INP_UNLOCK(inp); 1600 1601 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK); 1602 1603 imo->imo_multicast_ifp = NULL; 1604 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1605 imo->imo_multicast_vif = -1; 1606 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1607 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1608 imo->imo_num_memberships = 0; 1609 1610 INP_LOCK(inp); 1611 if (inp->inp_moptions != NULL) { 1612 free(imo, M_IPMOPTS); 1613 return (inp->inp_moptions); 1614 } 1615 inp->inp_moptions = imo; 1616 return (imo); 1617} 1618 1619/* 1620 * Set the IP multicast options in response to user setsockopt(). 1621 */ 1622static int 1623ip_setmoptions(struct inpcb *inp, struct sockopt *sopt) 1624{ 1625 int error = 0; 1626 int i; 1627 struct in_addr addr; 1628 struct ip_mreq mreq; 1629 struct ifnet *ifp; 1630 struct ip_moptions *imo; 1631 struct route ro; 1632 struct sockaddr_in *dst; 1633 int ifindex; 1634 int s; 1635 1636 switch (sopt->sopt_name) { 1637 /* store an index number for the vif you wanna use in the send */ 1638 case IP_MULTICAST_VIF: 1639 if (legal_vif_num == 0) { 1640 error = EOPNOTSUPP; 1641 break; 1642 } 1643 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1644 if (error) 1645 break; 1646 if (!legal_vif_num(i) && (i != -1)) { 1647 error = EINVAL; 1648 break; 1649 } 1650 imo = ip_findmoptions(inp); 1651 imo->imo_multicast_vif = i; 1652 INP_UNLOCK(inp); 1653 break; 1654 1655 case IP_MULTICAST_IF: 1656 /* 1657 * Select the interface for outgoing multicast packets. 1658 */ 1659 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1660 if (error) 1661 break; 1662 /* 1663 * INADDR_ANY is used to remove a previous selection. 1664 * When no interface is selected, a default one is 1665 * chosen every time a multicast packet is sent. 1666 */ 1667 imo = ip_findmoptions(inp); 1668 if (addr.s_addr == INADDR_ANY) { 1669 imo->imo_multicast_ifp = NULL; 1670 INP_UNLOCK(inp); 1671 break; 1672 } 1673 /* 1674 * The selected interface is identified by its local 1675 * IP address. Find the interface and confirm that 1676 * it supports multicasting. 1677 */ 1678 s = splimp(); 1679 ifp = ip_multicast_if(&addr, &ifindex); 1680 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1681 INP_UNLOCK(inp); 1682 splx(s); 1683 error = EADDRNOTAVAIL; 1684 break; 1685 } 1686 imo->imo_multicast_ifp = ifp; 1687 if (ifindex) 1688 imo->imo_multicast_addr = addr; 1689 else 1690 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1691 INP_UNLOCK(inp); 1692 splx(s); 1693 break; 1694 1695 case IP_MULTICAST_TTL: 1696 /* 1697 * Set the IP time-to-live for outgoing multicast packets. 1698 * The original multicast API required a char argument, 1699 * which is inconsistent with the rest of the socket API. 1700 * We allow either a char or an int. 1701 */ 1702 if (sopt->sopt_valsize == 1) { 1703 u_char ttl; 1704 error = sooptcopyin(sopt, &ttl, 1, 1); 1705 if (error) 1706 break; 1707 imo = ip_findmoptions(inp); 1708 imo->imo_multicast_ttl = ttl; 1709 INP_UNLOCK(inp); 1710 } else { 1711 u_int ttl; 1712 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1713 sizeof ttl); 1714 if (error) 1715 break; 1716 if (ttl > 255) 1717 error = EINVAL; 1718 else { 1719 imo = ip_findmoptions(inp); 1720 imo->imo_multicast_ttl = ttl; 1721 INP_UNLOCK(inp); 1722 } 1723 } 1724 break; 1725 1726 case IP_MULTICAST_LOOP: 1727 /* 1728 * Set the loopback flag for outgoing multicast packets. 1729 * Must be zero or one. The original multicast API required a 1730 * char argument, which is inconsistent with the rest 1731 * of the socket API. We allow either a char or an int. 1732 */ 1733 if (sopt->sopt_valsize == 1) { 1734 u_char loop; 1735 error = sooptcopyin(sopt, &loop, 1, 1); 1736 if (error) 1737 break; 1738 imo = ip_findmoptions(inp); 1739 imo->imo_multicast_loop = !!loop; 1740 INP_UNLOCK(inp); 1741 } else { 1742 u_int loop; 1743 error = sooptcopyin(sopt, &loop, sizeof loop, 1744 sizeof loop); 1745 if (error) 1746 break; 1747 imo = ip_findmoptions(inp); 1748 imo->imo_multicast_loop = !!loop; 1749 INP_UNLOCK(inp); 1750 } 1751 break; 1752 1753 case IP_ADD_MEMBERSHIP: 1754 /* 1755 * Add a multicast group membership. 1756 * Group must be a valid IP multicast address. 1757 */ 1758 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1759 if (error) 1760 break; 1761 1762 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1763 error = EINVAL; 1764 break; 1765 } 1766 s = splimp(); 1767 /* 1768 * If no interface address was provided, use the interface of 1769 * the route to the given multicast address. 1770 */ 1771 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1772 bzero((caddr_t)&ro, sizeof(ro)); 1773 dst = (struct sockaddr_in *)&ro.ro_dst; 1774 dst->sin_len = sizeof(*dst); 1775 dst->sin_family = AF_INET; 1776 dst->sin_addr = mreq.imr_multiaddr; 1777 rtalloc_ign(&ro, RTF_CLONING); 1778 if (ro.ro_rt == NULL) { 1779 error = EADDRNOTAVAIL; 1780 splx(s); 1781 break; 1782 } 1783 ifp = ro.ro_rt->rt_ifp; 1784 RTFREE(ro.ro_rt); 1785 } 1786 else { 1787 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1788 } 1789 1790 /* 1791 * See if we found an interface, and confirm that it 1792 * supports multicast. 1793 */ 1794 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1795 error = EADDRNOTAVAIL; 1796 splx(s); 1797 break; 1798 } 1799 /* 1800 * See if the membership already exists or if all the 1801 * membership slots are full. 1802 */ 1803 imo = ip_findmoptions(inp); 1804 for (i = 0; i < imo->imo_num_memberships; ++i) { 1805 if (imo->imo_membership[i]->inm_ifp == ifp && 1806 imo->imo_membership[i]->inm_addr.s_addr 1807 == mreq.imr_multiaddr.s_addr) 1808 break; 1809 } 1810 if (i < imo->imo_num_memberships) { 1811 INP_UNLOCK(inp); 1812 error = EADDRINUSE; 1813 splx(s); 1814 break; 1815 } 1816 if (i == IP_MAX_MEMBERSHIPS) { 1817 INP_UNLOCK(inp); 1818 error = ETOOMANYREFS; 1819 splx(s); 1820 break; 1821 } 1822 /* 1823 * Everything looks good; add a new record to the multicast 1824 * address list for the given interface. 1825 */ 1826 if ((imo->imo_membership[i] = 1827 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1828 INP_UNLOCK(inp); 1829 error = ENOBUFS; 1830 splx(s); 1831 break; 1832 } 1833 ++imo->imo_num_memberships; 1834 INP_UNLOCK(inp); 1835 splx(s); 1836 break; 1837 1838 case IP_DROP_MEMBERSHIP: 1839 /* 1840 * Drop a multicast group membership. 1841 * Group must be a valid IP multicast address. 1842 */ 1843 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1844 if (error) 1845 break; 1846 1847 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1848 error = EINVAL; 1849 break; 1850 } 1851 1852 s = splimp(); 1853 /* 1854 * If an interface address was specified, get a pointer 1855 * to its ifnet structure. 1856 */ 1857 if (mreq.imr_interface.s_addr == INADDR_ANY) 1858 ifp = NULL; 1859 else { 1860 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1861 if (ifp == NULL) { 1862 error = EADDRNOTAVAIL; 1863 splx(s); 1864 break; 1865 } 1866 } 1867 /* 1868 * Find the membership in the membership array. 1869 */ 1870 imo = ip_findmoptions(inp); 1871 for (i = 0; i < imo->imo_num_memberships; ++i) { 1872 if ((ifp == NULL || 1873 imo->imo_membership[i]->inm_ifp == ifp) && 1874 imo->imo_membership[i]->inm_addr.s_addr == 1875 mreq.imr_multiaddr.s_addr) 1876 break; 1877 } 1878 if (i == imo->imo_num_memberships) { 1879 INP_UNLOCK(inp); 1880 error = EADDRNOTAVAIL; 1881 splx(s); 1882 break; 1883 } 1884 /* 1885 * Give up the multicast address record to which the 1886 * membership points. 1887 */ 1888 in_delmulti(imo->imo_membership[i]); 1889 /* 1890 * Remove the gap in the membership array. 1891 */ 1892 for (++i; i < imo->imo_num_memberships; ++i) 1893 imo->imo_membership[i-1] = imo->imo_membership[i]; 1894 --imo->imo_num_memberships; 1895 INP_UNLOCK(inp); 1896 splx(s); 1897 break; 1898 1899 default: 1900 error = EOPNOTSUPP; 1901 break; 1902 } 1903 1904 return (error); 1905} 1906 1907/* 1908 * Return the IP multicast options in response to user getsockopt(). 1909 */ 1910static int 1911ip_getmoptions(struct inpcb *inp, struct sockopt *sopt) 1912{ 1913 struct ip_moptions *imo; 1914 struct in_addr addr; 1915 struct in_ifaddr *ia; 1916 int error, optval; 1917 u_char coptval; 1918 1919 INP_LOCK(inp); 1920 imo = inp->inp_moptions; 1921 1922 error = 0; 1923 switch (sopt->sopt_name) { 1924 case IP_MULTICAST_VIF: 1925 if (imo != NULL) 1926 optval = imo->imo_multicast_vif; 1927 else 1928 optval = -1; 1929 INP_UNLOCK(inp); 1930 error = sooptcopyout(sopt, &optval, sizeof optval); 1931 break; 1932 1933 case IP_MULTICAST_IF: 1934 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1935 addr.s_addr = INADDR_ANY; 1936 else if (imo->imo_multicast_addr.s_addr) { 1937 /* return the value user has set */ 1938 addr = imo->imo_multicast_addr; 1939 } else { 1940 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1941 addr.s_addr = (ia == NULL) ? INADDR_ANY 1942 : IA_SIN(ia)->sin_addr.s_addr; 1943 } 1944 INP_UNLOCK(inp); 1945 error = sooptcopyout(sopt, &addr, sizeof addr); 1946 break; 1947 1948 case IP_MULTICAST_TTL: 1949 if (imo == 0) 1950 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1951 else 1952 optval = coptval = imo->imo_multicast_ttl; 1953 INP_UNLOCK(inp); 1954 if (sopt->sopt_valsize == 1) 1955 error = sooptcopyout(sopt, &coptval, 1); 1956 else 1957 error = sooptcopyout(sopt, &optval, sizeof optval); 1958 break; 1959 1960 case IP_MULTICAST_LOOP: 1961 if (imo == 0) 1962 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1963 else 1964 optval = coptval = imo->imo_multicast_loop; 1965 INP_UNLOCK(inp); 1966 if (sopt->sopt_valsize == 1) 1967 error = sooptcopyout(sopt, &coptval, 1); 1968 else 1969 error = sooptcopyout(sopt, &optval, sizeof optval); 1970 break; 1971 1972 default: 1973 INP_UNLOCK(inp); 1974 error = ENOPROTOOPT; 1975 break; 1976 } 1977 INP_UNLOCK_ASSERT(inp); 1978 1979 return (error); 1980} 1981 1982/* 1983 * Discard the IP multicast options. 1984 */ 1985void 1986ip_freemoptions(imo) 1987 register struct ip_moptions *imo; 1988{ 1989 register int i; 1990 1991 if (imo != NULL) { 1992 for (i = 0; i < imo->imo_num_memberships; ++i) 1993 in_delmulti(imo->imo_membership[i]); 1994 free(imo, M_IPMOPTS); 1995 } 1996} 1997 1998/* 1999 * Routine called from ip_output() to loop back a copy of an IP multicast 2000 * packet to the input queue of a specified interface. Note that this 2001 * calls the output routine of the loopback "driver", but with an interface 2002 * pointer that might NOT be a loopback interface -- evil, but easier than 2003 * replicating that code here. 2004 */ 2005static void 2006ip_mloopback(ifp, m, dst, hlen) 2007 struct ifnet *ifp; 2008 register struct mbuf *m; 2009 register struct sockaddr_in *dst; 2010 int hlen; 2011{ 2012 register struct ip *ip; 2013 struct mbuf *copym; 2014 2015 copym = m_copy(m, 0, M_COPYALL); 2016 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 2017 copym = m_pullup(copym, hlen); 2018 if (copym != NULL) { 2019 /* If needed, compute the checksum and mark it as valid. */ 2020 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2021 in_delayed_cksum(copym); 2022 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2023 copym->m_pkthdr.csum_flags |= 2024 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 2025 copym->m_pkthdr.csum_data = 0xffff; 2026 } 2027 /* 2028 * We don't bother to fragment if the IP length is greater 2029 * than the interface's MTU. Can this possibly matter? 2030 */ 2031 ip = mtod(copym, struct ip *); 2032 ip->ip_len = htons(ip->ip_len); 2033 ip->ip_off = htons(ip->ip_off); 2034 ip->ip_sum = 0; 2035 ip->ip_sum = in_cksum(copym, hlen); 2036 /* 2037 * NB: 2038 * It's not clear whether there are any lingering 2039 * reentrancy problems in other areas which might 2040 * be exposed by using ip_input directly (in 2041 * particular, everything which modifies the packet 2042 * in-place). Yet another option is using the 2043 * protosw directly to deliver the looped back 2044 * packet. For the moment, we'll err on the side 2045 * of safety by using if_simloop(). 2046 */ 2047#if 1 /* XXX */ 2048 if (dst->sin_family != AF_INET) { 2049 printf("ip_mloopback: bad address family %d\n", 2050 dst->sin_family); 2051 dst->sin_family = AF_INET; 2052 } 2053#endif 2054 2055#ifdef notdef 2056 copym->m_pkthdr.rcvif = ifp; 2057 ip_input(copym); 2058#else 2059 if_simloop(ifp, copym, dst->sin_family, 0); 2060#endif 2061 } 2062} 2063