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