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$ 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 62 #include <machine/in_cksum.h> 63 64 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 65 66 #ifdef IPSEC 67 #include <netinet6/ipsec.h> 68 #include <netkey/key.h> 69 #ifdef IPSEC_DEBUG 70 #include <netkey/key_debug.h> 71 #else 72 #define KEYDEBUG(lev,arg) 73 #endif 74 #endif /*IPSEC*/ 75 76 #ifdef FAST_IPSEC 77 #include <netipsec/ipsec.h> 78 #include <netipsec/xform.h> 79 #include <netipsec/key.h> 80 #endif /*FAST_IPSEC*/ 81 82 #define print_ip(x, a, y) printf("%s %d.%d.%d.%d%s",\ 83 x, (ntohl(a.s_addr)>>24)&0xFF,\ 84 (ntohl(a.s_addr)>>16)&0xFF,\ 85 (ntohl(a.s_addr)>>8)&0xFF,\ 86 (ntohl(a.s_addr))&0xFF, y); 87 88 u_short ip_id; 89 90 #ifdef MBUF_STRESS_TEST 91 int mbuf_frag_size = 0; 92 SYSCTL_INT(_net_inet_ip, OID_AUTO, mbuf_frag_size, CTLFLAG_RW, 93 &mbuf_frag_size, 0, "Fragment outgoing mbufs to this size"); 94 #endif 95 96 static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *); 97 static struct ifnet *ip_multicast_if(struct in_addr *, int *); 98 static void ip_mloopback 99 (struct ifnet *, struct mbuf *, struct sockaddr_in *, int); 100 static int ip_getmoptions 101 (struct sockopt *, struct ip_moptions *); 102 static int ip_pcbopts(int, struct mbuf **, struct mbuf *); 103 static int ip_setmoptions 104 (struct sockopt *, struct ip_moptions **); 105 106 int ip_optcopy(struct ip *, struct ip *); 107 108 109 extern struct protosw inetsw[]; 110 111 /* 112 * IP output. The packet in mbuf chain m contains a skeletal IP 113 * header (with len, off, ttl, proto, tos, src, dst). 114 * The mbuf chain containing the packet will be freed. 115 * The mbuf opt, if present, will not be freed. 116 * In the IP forwarding case, the packet will arrive with options already 117 * inserted, so must have a NULL opt pointer. 118 */ 119 int 120 ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, 121 int flags, struct ip_moptions *imo, struct inpcb *inp) 122 { 123 struct ip *ip; 124 struct ifnet *ifp = NULL; /* keep compiler happy */ 125 struct mbuf *m0; 126 int hlen = sizeof (struct ip); 127 int len, error = 0; 128 struct sockaddr_in *dst = NULL; /* keep compiler happy */ 129 struct in_ifaddr *ia = NULL; 130 int isbroadcast, sw_csum; 131 struct route iproute; 132 struct in_addr odst; 133 #ifdef IPFIREWALL_FORWARD 134 struct m_tag *fwd_tag = NULL; 135 #endif 136 #ifdef IPSEC 137 struct secpolicy *sp = NULL; 138 #endif 139 #ifdef FAST_IPSEC 140 struct secpolicy *sp = NULL; 141 struct tdb_ident *tdbi; 142 struct m_tag *mtag; 143 int s; 144 #endif /* FAST_IPSEC */ 145 146 M_ASSERTPKTHDR(m); 147 148 if (ro == NULL) { 149 ro = &iproute; 150 bzero(ro, sizeof (*ro)); 151 } 152 153 if (inp != NULL) 154 INP_LOCK_ASSERT(inp); 155 156 if (opt) { 157 len = 0; 158 m = ip_insertoptions(m, opt, &len); 159 if (len != 0) 160 hlen = len; 161 } 162 ip = mtod(m, struct ip *); 163 164 /* 165 * Fill in IP header. If we are not allowing fragmentation, 166 * then the ip_id field is meaningless, but we don't set it 167 * to zero. Doing so causes various problems when devices along 168 * the path (routers, load balancers, firewalls, etc.) illegally 169 * disable DF on our packet. Note that a 16-bit counter 170 * will wrap around in less than 10 seconds at 100 Mbit/s on a 171 * medium with MTU 1500. See Steven M. Bellovin, "A Technique 172 * for Counting NATted Hosts", Proc. IMW'02, available at 173 * <http://www.research.att.com/~smb/papers/fnat.pdf>. 174 */ 175 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 176 ip->ip_v = IPVERSION; 177 ip->ip_hl = hlen >> 2; 178 ip->ip_id = ip_newid(); 179 ipstat.ips_localout++; 180 } else { 181 hlen = ip->ip_hl << 2; 182 } 183 184 dst = (struct sockaddr_in *)&ro->ro_dst; 185 again: 186 /* 187 * If there is a cached route, 188 * check that it is to the same destination 189 * and is still up. If not, free it and try again. 190 * The address family should also be checked in case of sharing the 191 * cache with IPv6. 192 */ 193 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 194 dst->sin_family != AF_INET || 195 dst->sin_addr.s_addr != ip->ip_dst.s_addr)) { 196 RTFREE(ro->ro_rt); 197 ro->ro_rt = (struct rtentry *)0; 198 } 199 #ifdef IPFIREWALL_FORWARD 200 if (ro->ro_rt == NULL && fwd_tag == NULL) { 201 #else 202 if (ro->ro_rt == NULL) { 203 #endif 204 bzero(dst, sizeof(*dst)); 205 dst->sin_family = AF_INET; 206 dst->sin_len = sizeof(*dst); 207 dst->sin_addr = ip->ip_dst; 208 } 209 /* 210 * If routing to interface only, 211 * short circuit routing lookup. 212 */ 213 if (flags & IP_ROUTETOIF) { 214 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL && 215 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == NULL) { 216 ipstat.ips_noroute++; 217 error = ENETUNREACH; 218 goto bad; 219 } 220 ifp = ia->ia_ifp; 221 ip->ip_ttl = 1; 222 isbroadcast = in_broadcast(dst->sin_addr, ifp); 223 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 224 imo != NULL && imo->imo_multicast_ifp != NULL) { 225 /* 226 * Bypass the normal routing lookup for multicast 227 * packets if the interface is specified. 228 */ 229 ifp = imo->imo_multicast_ifp; 230 IFP_TO_IA(ifp, ia); 231 isbroadcast = 0; /* fool gcc */ 232 } else { 233 /* 234 * We want to do any cloning requested by the link layer, 235 * as this is probably required in all cases for correct 236 * operation (as it is for ARP). 237 */ 238 if (ro->ro_rt == NULL) 239 rtalloc_ign(ro, 0); 240 if (ro->ro_rt == NULL) { 241 ipstat.ips_noroute++; 242 error = EHOSTUNREACH; 243 goto bad; 244 } 245 ia = ifatoia(ro->ro_rt->rt_ifa); 246 ifp = ro->ro_rt->rt_ifp; 247 ro->ro_rt->rt_rmx.rmx_pksent++; 248 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 249 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 250 if (ro->ro_rt->rt_flags & RTF_HOST) 251 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 252 else 253 isbroadcast = in_broadcast(dst->sin_addr, ifp); 254 } 255 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 256 struct in_multi *inm; 257 258 m->m_flags |= M_MCAST; 259 /* 260 * IP destination address is multicast. Make sure "dst" 261 * still points to the address in "ro". (It may have been 262 * changed to point to a gateway address, above.) 263 */ 264 dst = (struct sockaddr_in *)&ro->ro_dst; 265 /* 266 * See if the caller provided any multicast options 267 */ 268 if (imo != NULL) { 269 ip->ip_ttl = imo->imo_multicast_ttl; 270 if (imo->imo_multicast_vif != -1) 271 ip->ip_src.s_addr = 272 ip_mcast_src ? 273 ip_mcast_src(imo->imo_multicast_vif) : 274 INADDR_ANY; 275 } else 276 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 277 /* 278 * Confirm that the outgoing interface supports multicast. 279 */ 280 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 281 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 282 ipstat.ips_noroute++; 283 error = ENETUNREACH; 284 goto bad; 285 } 286 } 287 /* 288 * If source address not specified yet, use address 289 * of outgoing interface. 290 */ 291 if (ip->ip_src.s_addr == INADDR_ANY) { 292 /* Interface may have no addresses. */ 293 if (ia != NULL) 294 ip->ip_src = IA_SIN(ia)->sin_addr; 295 } 296 297 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 298 if (inm != NULL && 299 (imo == NULL || imo->imo_multicast_loop)) { 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 /* 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 409 sendit: 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); 534 skip_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 } 656 spd_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 if (!in_localip(ip->ip_src) && !in_localaddr(ip->ip_dst)) { 713 dst = (struct sockaddr_in *)&ro->ro_dst; 714 bcopy((fwd_tag+1), dst, sizeof(struct sockaddr_in)); 715 m->m_flags |= M_SKIP_FIREWALL; 716 m_tag_delete(m, fwd_tag); 717 goto again; 718 } else { 719 m_tag_delete(m, fwd_tag); 720 /* Continue. */ 721 } 722 } 723 #endif 724 725 passout: 726 /* 127/8 must not appear on wire - RFC1122. */ 727 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 728 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 729 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 730 ipstat.ips_badaddr++; 731 error = EADDRNOTAVAIL; 732 goto bad; 733 } 734 } 735 736 m->m_pkthdr.csum_flags |= CSUM_IP; 737 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 738 if (sw_csum & CSUM_DELAY_DATA) { 739 in_delayed_cksum(m); 740 sw_csum &= ~CSUM_DELAY_DATA; 741 } 742 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 743 744 /* 745 * If small enough for interface, or the interface will take 746 * care of the fragmentation for us, can just send directly. 747 */ 748 if (ip->ip_len <= ifp->if_mtu || (ifp->if_hwassist & CSUM_FRAGMENT && 749 ((ip->ip_off & IP_DF) == 0))) { 750 ip->ip_len = htons(ip->ip_len); 751 ip->ip_off = htons(ip->ip_off); 752 ip->ip_sum = 0; 753 if (sw_csum & CSUM_DELAY_IP) 754 ip->ip_sum = in_cksum(m, hlen); 755 756 /* Record statistics for this interface address. */ 757 if (!(flags & IP_FORWARDING) && ia) { 758 ia->ia_ifa.if_opackets++; 759 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 760 } 761 762 #ifdef IPSEC 763 /* clean ipsec history once it goes out of the node */ 764 ipsec_delaux(m); 765 #endif 766 767 #ifdef MBUF_STRESS_TEST 768 if (mbuf_frag_size && m->m_pkthdr.len > mbuf_frag_size) 769 m = m_fragment(m, M_DONTWAIT, mbuf_frag_size); 770 #endif 771 error = (*ifp->if_output)(ifp, m, 772 (struct sockaddr *)dst, ro->ro_rt); 773 goto done; 774 } 775 776 if (ip->ip_off & IP_DF) { 777 error = EMSGSIZE; 778 /* 779 * This case can happen if the user changed the MTU 780 * of an interface after enabling IP on it. Because 781 * most netifs don't keep track of routes pointing to 782 * them, there is no way for one to update all its 783 * routes when the MTU is changed. 784 */ 785 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) && 786 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 787 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 788 } 789 ipstat.ips_cantfrag++; 790 goto bad; 791 } 792 793 /* 794 * Too large for interface; fragment if possible. If successful, 795 * on return, m will point to a list of packets to be sent. 796 */ 797 error = ip_fragment(ip, &m, ifp->if_mtu, ifp->if_hwassist, sw_csum); 798 if (error) 799 goto bad; 800 for (; m; m = m0) { 801 m0 = m->m_nextpkt; 802 m->m_nextpkt = 0; 803 #ifdef IPSEC 804 /* clean ipsec history once it goes out of the node */ 805 ipsec_delaux(m); 806 #endif 807 if (error == 0) { 808 /* Record statistics for this interface address. */ 809 if (ia != NULL) { 810 ia->ia_ifa.if_opackets++; 811 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 812 } 813 814 error = (*ifp->if_output)(ifp, m, 815 (struct sockaddr *)dst, ro->ro_rt); 816 } else 817 m_freem(m); 818 } 819 820 if (error == 0) 821 ipstat.ips_fragmented++; 822 823 done: 824 if (ro == &iproute && ro->ro_rt) { 825 RTFREE(ro->ro_rt); 826 } 827 #ifdef IPSEC 828 if (sp != NULL) { 829 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 830 printf("DP ip_output call free SP:%p\n", sp)); 831 key_freesp(sp); 832 } 833 #endif 834 #ifdef FAST_IPSEC 835 if (sp != NULL) 836 KEY_FREESP(&sp); 837 #endif 838 return (error); 839 bad: 840 m_freem(m); 841 goto done; 842 } 843 844 /* 845 * Create a chain of fragments which fit the given mtu. m_frag points to the 846 * mbuf to be fragmented; on return it points to the chain with the fragments. 847 * Return 0 if no error. If error, m_frag may contain a partially built 848 * chain of fragments that should be freed by the caller. 849 * 850 * if_hwassist_flags is the hw offload capabilities (see if_data.ifi_hwassist) 851 * sw_csum contains the delayed checksums flags (e.g., CSUM_DELAY_IP). 852 */ 853 int 854 ip_fragment(struct ip *ip, struct mbuf **m_frag, int mtu, 855 u_long if_hwassist_flags, int sw_csum) 856 { 857 int error = 0; 858 int hlen = ip->ip_hl << 2; 859 int len = (mtu - hlen) & ~7; /* size of payload in each fragment */ 860 int off; 861 struct mbuf *m0 = *m_frag; /* the original packet */ 862 int firstlen; 863 struct mbuf **mnext; 864 int nfrags; 865 866 if (ip->ip_off & IP_DF) { /* Fragmentation not allowed */ 867 ipstat.ips_cantfrag++; 868 return EMSGSIZE; 869 } 870 871 /* 872 * Must be able to put at least 8 bytes per fragment. 873 */ 874 if (len < 8) 875 return EMSGSIZE; 876 877 /* 878 * If the interface will not calculate checksums on 879 * fragmented packets, then do it here. 880 */ 881 if (m0->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 882 (if_hwassist_flags & CSUM_IP_FRAGS) == 0) { 883 in_delayed_cksum(m0); 884 m0->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 885 } 886 887 if (len > PAGE_SIZE) { 888 /* 889 * Fragment large datagrams such that each segment 890 * contains a multiple of PAGE_SIZE amount of data, 891 * plus headers. This enables a receiver to perform 892 * page-flipping zero-copy optimizations. 893 * 894 * XXX When does this help given that sender and receiver 895 * could have different page sizes, and also mtu could 896 * be less than the receiver's page size ? 897 */ 898 int newlen; 899 struct mbuf *m; 900 901 for (m = m0, off = 0; m && (off+m->m_len) <= mtu; m = m->m_next) 902 off += m->m_len; 903 904 /* 905 * firstlen (off - hlen) must be aligned on an 906 * 8-byte boundary 907 */ 908 if (off < hlen) 909 goto smart_frag_failure; 910 off = ((off - hlen) & ~7) + hlen; 911 newlen = (~PAGE_MASK) & mtu; 912 if ((newlen + sizeof (struct ip)) > mtu) { 913 /* we failed, go back the default */ 914 smart_frag_failure: 915 newlen = len; 916 off = hlen + len; 917 } 918 len = newlen; 919 920 } else { 921 off = hlen + len; 922 } 923 924 firstlen = off - hlen; 925 mnext = &m0->m_nextpkt; /* pointer to next packet */ 926 927 /* 928 * Loop through length of segment after first fragment, 929 * make new header and copy data of each part and link onto chain. 930 * Here, m0 is the original packet, m is the fragment being created. 931 * The fragments are linked off the m_nextpkt of the original 932 * packet, which after processing serves as the first fragment. 933 */ 934 for (nfrags = 1; off < ip->ip_len; off += len, nfrags++) { 935 struct ip *mhip; /* ip header on the fragment */ 936 struct mbuf *m; 937 int mhlen = sizeof (struct ip); 938 939 MGETHDR(m, M_DONTWAIT, MT_HEADER); 940 if (m == NULL) { 941 error = ENOBUFS; 942 ipstat.ips_odropped++; 943 goto done; 944 } 945 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 946 /* 947 * In the first mbuf, leave room for the link header, then 948 * copy the original IP header including options. The payload 949 * goes into an additional mbuf chain returned by m_copy(). 950 */ 951 m->m_data += max_linkhdr; 952 mhip = mtod(m, struct ip *); 953 *mhip = *ip; 954 if (hlen > sizeof (struct ip)) { 955 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 956 mhip->ip_v = IPVERSION; 957 mhip->ip_hl = mhlen >> 2; 958 } 959 m->m_len = mhlen; 960 /* XXX do we need to add ip->ip_off below ? */ 961 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 962 if (off + len >= ip->ip_len) { /* last fragment */ 963 len = ip->ip_len - off; 964 m->m_flags |= M_LASTFRAG; 965 } else 966 mhip->ip_off |= IP_MF; 967 mhip->ip_len = htons((u_short)(len + mhlen)); 968 m->m_next = m_copy(m0, off, len); 969 if (m->m_next == NULL) { /* copy failed */ 970 m_free(m); 971 error = ENOBUFS; /* ??? */ 972 ipstat.ips_odropped++; 973 goto done; 974 } 975 m->m_pkthdr.len = mhlen + len; 976 m->m_pkthdr.rcvif = (struct ifnet *)0; 977 #ifdef MAC 978 mac_create_fragment(m0, m); 979 #endif 980 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 981 mhip->ip_off = htons(mhip->ip_off); 982 mhip->ip_sum = 0; 983 if (sw_csum & CSUM_DELAY_IP) 984 mhip->ip_sum = in_cksum(m, mhlen); 985 *mnext = m; 986 mnext = &m->m_nextpkt; 987 } 988 ipstat.ips_ofragments += nfrags; 989 990 /* set first marker for fragment chain */ 991 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 992 m0->m_pkthdr.csum_data = nfrags; 993 994 /* 995 * Update first fragment by trimming what's been copied out 996 * and updating header. 997 */ 998 m_adj(m0, hlen + firstlen - ip->ip_len); 999 m0->m_pkthdr.len = hlen + firstlen; 1000 ip->ip_len = htons((u_short)m0->m_pkthdr.len); 1001 ip->ip_off |= IP_MF; 1002 ip->ip_off = htons(ip->ip_off); 1003 ip->ip_sum = 0; 1004 if (sw_csum & CSUM_DELAY_IP) 1005 ip->ip_sum = in_cksum(m0, hlen); 1006 1007 done: 1008 *m_frag = m0; 1009 return error; 1010 } 1011 1012 void 1013 in_delayed_cksum(struct mbuf *m) 1014 { 1015 struct ip *ip; 1016 u_short csum, offset; 1017 1018 ip = mtod(m, struct ip *); 1019 offset = ip->ip_hl << 2 ; 1020 csum = in_cksum_skip(m, ip->ip_len, offset); 1021 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 1022 csum = 0xffff; 1023 offset += m->m_pkthdr.csum_data; /* checksum offset */ 1024 1025 if (offset + sizeof(u_short) > m->m_len) { 1026 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 1027 m->m_len, offset, ip->ip_p); 1028 /* 1029 * XXX 1030 * this shouldn't happen, but if it does, the 1031 * correct behavior may be to insert the checksum 1032 * in the existing chain instead of rearranging it. 1033 */ 1034 m = m_pullup(m, offset + sizeof(u_short)); 1035 } 1036 *(u_short *)(m->m_data + offset) = csum; 1037 } 1038 1039 /* 1040 * Insert IP options into preformed packet. 1041 * Adjust IP destination as required for IP source routing, 1042 * as indicated by a non-zero in_addr at the start of the options. 1043 * 1044 * XXX This routine assumes that the packet has no options in place. 1045 */ 1046 static struct mbuf * 1047 ip_insertoptions(m, opt, phlen) 1048 register struct mbuf *m; 1049 struct mbuf *opt; 1050 int *phlen; 1051 { 1052 register struct ipoption *p = mtod(opt, struct ipoption *); 1053 struct mbuf *n; 1054 register struct ip *ip = mtod(m, struct ip *); 1055 unsigned optlen; 1056 1057 optlen = opt->m_len - sizeof(p->ipopt_dst); 1058 if (optlen + ip->ip_len > IP_MAXPACKET) { 1059 *phlen = 0; 1060 return (m); /* XXX should fail */ 1061 } 1062 if (p->ipopt_dst.s_addr) 1063 ip->ip_dst = p->ipopt_dst; 1064 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1065 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1066 if (n == NULL) { 1067 *phlen = 0; 1068 return (m); 1069 } 1070 n->m_pkthdr.rcvif = (struct ifnet *)0; 1071 #ifdef MAC 1072 mac_create_mbuf_from_mbuf(m, n); 1073 #endif 1074 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1075 m->m_len -= sizeof(struct ip); 1076 m->m_data += sizeof(struct ip); 1077 n->m_next = m; 1078 m = n; 1079 m->m_len = optlen + sizeof(struct ip); 1080 m->m_data += max_linkhdr; 1081 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1082 } else { 1083 m->m_data -= optlen; 1084 m->m_len += optlen; 1085 m->m_pkthdr.len += optlen; 1086 bcopy(ip, mtod(m, void *), sizeof(struct ip)); 1087 } 1088 ip = mtod(m, struct ip *); 1089 bcopy(p->ipopt_list, ip + 1, optlen); 1090 *phlen = sizeof(struct ip) + optlen; 1091 ip->ip_v = IPVERSION; 1092 ip->ip_hl = *phlen >> 2; 1093 ip->ip_len += optlen; 1094 return (m); 1095 } 1096 1097 /* 1098 * Copy options from ip to jp, 1099 * omitting those not copied during fragmentation. 1100 */ 1101 int 1102 ip_optcopy(ip, jp) 1103 struct ip *ip, *jp; 1104 { 1105 register u_char *cp, *dp; 1106 int opt, optlen, cnt; 1107 1108 cp = (u_char *)(ip + 1); 1109 dp = (u_char *)(jp + 1); 1110 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1111 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1112 opt = cp[0]; 1113 if (opt == IPOPT_EOL) 1114 break; 1115 if (opt == IPOPT_NOP) { 1116 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1117 *dp++ = IPOPT_NOP; 1118 optlen = 1; 1119 continue; 1120 } 1121 1122 KASSERT(cnt >= IPOPT_OLEN + sizeof(*cp), 1123 ("ip_optcopy: malformed ipv4 option")); 1124 optlen = cp[IPOPT_OLEN]; 1125 KASSERT(optlen >= IPOPT_OLEN + sizeof(*cp) && optlen <= cnt, 1126 ("ip_optcopy: malformed ipv4 option")); 1127 1128 /* bogus lengths should have been caught by ip_dooptions */ 1129 if (optlen > cnt) 1130 optlen = cnt; 1131 if (IPOPT_COPIED(opt)) { 1132 bcopy(cp, dp, optlen); 1133 dp += optlen; 1134 } 1135 } 1136 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1137 *dp++ = IPOPT_EOL; 1138 return (optlen); 1139 } 1140 1141 /* 1142 * IP socket option processing. 1143 */ 1144 int 1145 ip_ctloutput(so, sopt) 1146 struct socket *so; 1147 struct sockopt *sopt; 1148 { 1149 struct inpcb *inp = sotoinpcb(so); 1150 int error, optval; 1151 1152 error = optval = 0; 1153 if (sopt->sopt_level != IPPROTO_IP) { 1154 return (EINVAL); 1155 } 1156 1157 switch (sopt->sopt_dir) { 1158 case SOPT_SET: 1159 switch (sopt->sopt_name) { 1160 case IP_OPTIONS: 1161 #ifdef notyet 1162 case IP_RETOPTS: 1163 #endif 1164 { 1165 struct mbuf *m; 1166 if (sopt->sopt_valsize > MLEN) { 1167 error = EMSGSIZE; 1168 break; 1169 } 1170 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1171 if (m == NULL) { 1172 error = ENOBUFS; 1173 break; 1174 } 1175 m->m_len = sopt->sopt_valsize; 1176 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1177 m->m_len); 1178 1179 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1180 m)); 1181 } 1182 1183 case IP_TOS: 1184 case IP_TTL: 1185 case IP_RECVOPTS: 1186 case IP_RECVRETOPTS: 1187 case IP_RECVDSTADDR: 1188 case IP_RECVTTL: 1189 case IP_RECVIF: 1190 case IP_FAITH: 1191 case IP_ONESBCAST: 1192 error = sooptcopyin(sopt, &optval, sizeof optval, 1193 sizeof optval); 1194 if (error) 1195 break; 1196 1197 switch (sopt->sopt_name) { 1198 case IP_TOS: 1199 inp->inp_ip_tos = optval; 1200 break; 1201 1202 case IP_TTL: 1203 inp->inp_ip_ttl = optval; 1204 break; 1205 #define OPTSET(bit) do { \ 1206 INP_LOCK(inp); \ 1207 if (optval) \ 1208 inp->inp_flags |= bit; \ 1209 else \ 1210 inp->inp_flags &= ~bit; \ 1211 INP_UNLOCK(inp); \ 1212 } while (0) 1213 1214 case IP_RECVOPTS: 1215 OPTSET(INP_RECVOPTS); 1216 break; 1217 1218 case IP_RECVRETOPTS: 1219 OPTSET(INP_RECVRETOPTS); 1220 break; 1221 1222 case IP_RECVDSTADDR: 1223 OPTSET(INP_RECVDSTADDR); 1224 break; 1225 1226 case IP_RECVTTL: 1227 OPTSET(INP_RECVTTL); 1228 break; 1229 1230 case IP_RECVIF: 1231 OPTSET(INP_RECVIF); 1232 break; 1233 1234 case IP_FAITH: 1235 OPTSET(INP_FAITH); 1236 break; 1237 1238 case IP_ONESBCAST: 1239 OPTSET(INP_ONESBCAST); 1240 break; 1241 } 1242 break; 1243 #undef OPTSET 1244 1245 case IP_MULTICAST_IF: 1246 case IP_MULTICAST_VIF: 1247 case IP_MULTICAST_TTL: 1248 case IP_MULTICAST_LOOP: 1249 case IP_ADD_MEMBERSHIP: 1250 case IP_DROP_MEMBERSHIP: 1251 error = ip_setmoptions(sopt, &inp->inp_moptions); 1252 break; 1253 1254 case IP_PORTRANGE: 1255 error = sooptcopyin(sopt, &optval, sizeof optval, 1256 sizeof optval); 1257 if (error) 1258 break; 1259 1260 INP_LOCK(inp); 1261 switch (optval) { 1262 case IP_PORTRANGE_DEFAULT: 1263 inp->inp_flags &= ~(INP_LOWPORT); 1264 inp->inp_flags &= ~(INP_HIGHPORT); 1265 break; 1266 1267 case IP_PORTRANGE_HIGH: 1268 inp->inp_flags &= ~(INP_LOWPORT); 1269 inp->inp_flags |= INP_HIGHPORT; 1270 break; 1271 1272 case IP_PORTRANGE_LOW: 1273 inp->inp_flags &= ~(INP_HIGHPORT); 1274 inp->inp_flags |= INP_LOWPORT; 1275 break; 1276 1277 default: 1278 error = EINVAL; 1279 break; 1280 } 1281 INP_UNLOCK(inp); 1282 break; 1283 1284 #if defined(IPSEC) || defined(FAST_IPSEC) 1285 case IP_IPSEC_POLICY: 1286 { 1287 caddr_t req; 1288 size_t len = 0; 1289 int priv; 1290 struct mbuf *m; 1291 int optname; 1292 1293 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1294 break; 1295 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1296 break; 1297 priv = (sopt->sopt_td != NULL && 1298 suser(sopt->sopt_td) != 0) ? 0 : 1; 1299 req = mtod(m, caddr_t); 1300 len = m->m_len; 1301 optname = sopt->sopt_name; 1302 error = ipsec4_set_policy(inp, optname, req, len, priv); 1303 m_freem(m); 1304 break; 1305 } 1306 #endif /*IPSEC*/ 1307 1308 default: 1309 error = ENOPROTOOPT; 1310 break; 1311 } 1312 break; 1313 1314 case SOPT_GET: 1315 switch (sopt->sopt_name) { 1316 case IP_OPTIONS: 1317 case IP_RETOPTS: 1318 if (inp->inp_options) 1319 error = sooptcopyout(sopt, 1320 mtod(inp->inp_options, 1321 char *), 1322 inp->inp_options->m_len); 1323 else 1324 sopt->sopt_valsize = 0; 1325 break; 1326 1327 case IP_TOS: 1328 case IP_TTL: 1329 case IP_RECVOPTS: 1330 case IP_RECVRETOPTS: 1331 case IP_RECVDSTADDR: 1332 case IP_RECVTTL: 1333 case IP_RECVIF: 1334 case IP_PORTRANGE: 1335 case IP_FAITH: 1336 case IP_ONESBCAST: 1337 switch (sopt->sopt_name) { 1338 1339 case IP_TOS: 1340 optval = inp->inp_ip_tos; 1341 break; 1342 1343 case IP_TTL: 1344 optval = inp->inp_ip_ttl; 1345 break; 1346 1347 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1348 1349 case IP_RECVOPTS: 1350 optval = OPTBIT(INP_RECVOPTS); 1351 break; 1352 1353 case IP_RECVRETOPTS: 1354 optval = OPTBIT(INP_RECVRETOPTS); 1355 break; 1356 1357 case IP_RECVDSTADDR: 1358 optval = OPTBIT(INP_RECVDSTADDR); 1359 break; 1360 1361 case IP_RECVTTL: 1362 optval = OPTBIT(INP_RECVTTL); 1363 break; 1364 1365 case IP_RECVIF: 1366 optval = OPTBIT(INP_RECVIF); 1367 break; 1368 1369 case IP_PORTRANGE: 1370 if (inp->inp_flags & INP_HIGHPORT) 1371 optval = IP_PORTRANGE_HIGH; 1372 else if (inp->inp_flags & INP_LOWPORT) 1373 optval = IP_PORTRANGE_LOW; 1374 else 1375 optval = 0; 1376 break; 1377 1378 case IP_FAITH: 1379 optval = OPTBIT(INP_FAITH); 1380 break; 1381 1382 case IP_ONESBCAST: 1383 optval = OPTBIT(INP_ONESBCAST); 1384 break; 1385 } 1386 error = sooptcopyout(sopt, &optval, sizeof optval); 1387 break; 1388 1389 case IP_MULTICAST_IF: 1390 case IP_MULTICAST_VIF: 1391 case IP_MULTICAST_TTL: 1392 case IP_MULTICAST_LOOP: 1393 case IP_ADD_MEMBERSHIP: 1394 case IP_DROP_MEMBERSHIP: 1395 error = ip_getmoptions(sopt, inp->inp_moptions); 1396 break; 1397 1398 #if defined(IPSEC) || defined(FAST_IPSEC) 1399 case IP_IPSEC_POLICY: 1400 { 1401 struct mbuf *m = NULL; 1402 caddr_t req = NULL; 1403 size_t len = 0; 1404 1405 if (m != 0) { 1406 req = mtod(m, caddr_t); 1407 len = m->m_len; 1408 } 1409 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1410 if (error == 0) 1411 error = soopt_mcopyout(sopt, m); /* XXX */ 1412 if (error == 0) 1413 m_freem(m); 1414 break; 1415 } 1416 #endif /*IPSEC*/ 1417 1418 default: 1419 error = ENOPROTOOPT; 1420 break; 1421 } 1422 break; 1423 } 1424 return (error); 1425 } 1426 1427 /* 1428 * Set up IP options in pcb for insertion in output packets. 1429 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1430 * with destination address if source routed. 1431 */ 1432 static int 1433 ip_pcbopts(optname, pcbopt, m) 1434 int optname; 1435 struct mbuf **pcbopt; 1436 register struct mbuf *m; 1437 { 1438 register int cnt, optlen; 1439 register u_char *cp; 1440 u_char opt; 1441 1442 /* turn off any old options */ 1443 if (*pcbopt) 1444 (void)m_free(*pcbopt); 1445 *pcbopt = 0; 1446 if (m == (struct mbuf *)0 || m->m_len == 0) { 1447 /* 1448 * Only turning off any previous options. 1449 */ 1450 if (m) 1451 (void)m_free(m); 1452 return (0); 1453 } 1454 1455 if (m->m_len % sizeof(int32_t)) 1456 goto bad; 1457 /* 1458 * IP first-hop destination address will be stored before 1459 * actual options; move other options back 1460 * and clear it when none present. 1461 */ 1462 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1463 goto bad; 1464 cnt = m->m_len; 1465 m->m_len += sizeof(struct in_addr); 1466 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1467 bcopy(mtod(m, void *), cp, (unsigned)cnt); 1468 bzero(mtod(m, void *), sizeof(struct in_addr)); 1469 1470 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1471 opt = cp[IPOPT_OPTVAL]; 1472 if (opt == IPOPT_EOL) 1473 break; 1474 if (opt == IPOPT_NOP) 1475 optlen = 1; 1476 else { 1477 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1478 goto bad; 1479 optlen = cp[IPOPT_OLEN]; 1480 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1481 goto bad; 1482 } 1483 switch (opt) { 1484 1485 default: 1486 break; 1487 1488 case IPOPT_LSRR: 1489 case IPOPT_SSRR: 1490 /* 1491 * user process specifies route as: 1492 * ->A->B->C->D 1493 * D must be our final destination (but we can't 1494 * check that since we may not have connected yet). 1495 * A is first hop destination, which doesn't appear in 1496 * actual IP option, but is stored before the options. 1497 */ 1498 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1499 goto bad; 1500 m->m_len -= sizeof(struct in_addr); 1501 cnt -= sizeof(struct in_addr); 1502 optlen -= sizeof(struct in_addr); 1503 cp[IPOPT_OLEN] = optlen; 1504 /* 1505 * Move first hop before start of options. 1506 */ 1507 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1508 sizeof(struct in_addr)); 1509 /* 1510 * Then copy rest of options back 1511 * to close up the deleted entry. 1512 */ 1513 bcopy((&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1514 &cp[IPOPT_OFFSET+1], 1515 (unsigned)cnt - (IPOPT_MINOFF - 1)); 1516 break; 1517 } 1518 } 1519 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1520 goto bad; 1521 *pcbopt = m; 1522 return (0); 1523 1524 bad: 1525 (void)m_free(m); 1526 return (EINVAL); 1527 } 1528 1529 /* 1530 * XXX 1531 * The whole multicast option thing needs to be re-thought. 1532 * Several of these options are equally applicable to non-multicast 1533 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1534 * standard option (IP_TTL). 1535 */ 1536 1537 /* 1538 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1539 */ 1540 static struct ifnet * 1541 ip_multicast_if(a, ifindexp) 1542 struct in_addr *a; 1543 int *ifindexp; 1544 { 1545 int ifindex; 1546 struct ifnet *ifp; 1547 1548 if (ifindexp) 1549 *ifindexp = 0; 1550 if (ntohl(a->s_addr) >> 24 == 0) { 1551 ifindex = ntohl(a->s_addr) & 0xffffff; 1552 if (ifindex < 0 || if_index < ifindex) 1553 return NULL; 1554 ifp = ifnet_byindex(ifindex); 1555 if (ifindexp) 1556 *ifindexp = ifindex; 1557 } else { 1558 INADDR_TO_IFP(*a, ifp); 1559 } 1560 return ifp; 1561 } 1562 1563 /* 1564 * Set the IP multicast options in response to user setsockopt(). 1565 */ 1566 static int 1567 ip_setmoptions(sopt, imop) 1568 struct sockopt *sopt; 1569 struct ip_moptions **imop; 1570 { 1571 int error = 0; 1572 int i; 1573 struct in_addr addr; 1574 struct ip_mreq mreq; 1575 struct ifnet *ifp; 1576 struct ip_moptions *imo = *imop; 1577 struct route ro; 1578 struct sockaddr_in *dst; 1579 int ifindex; 1580 int s; 1581 1582 if (imo == NULL) { 1583 /* 1584 * No multicast option buffer attached to the pcb; 1585 * allocate one and initialize to default values. 1586 */ 1587 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1588 M_WAITOK); 1589 1590 if (imo == NULL) 1591 return (ENOBUFS); 1592 *imop = imo; 1593 imo->imo_multicast_ifp = NULL; 1594 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1595 imo->imo_multicast_vif = -1; 1596 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1597 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1598 imo->imo_num_memberships = 0; 1599 } 1600 1601 switch (sopt->sopt_name) { 1602 /* store an index number for the vif you wanna use in the send */ 1603 case IP_MULTICAST_VIF: 1604 if (legal_vif_num == 0) { 1605 error = EOPNOTSUPP; 1606 break; 1607 } 1608 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1609 if (error) 1610 break; 1611 if (!legal_vif_num(i) && (i != -1)) { 1612 error = EINVAL; 1613 break; 1614 } 1615 imo->imo_multicast_vif = i; 1616 break; 1617 1618 case IP_MULTICAST_IF: 1619 /* 1620 * Select the interface for outgoing multicast packets. 1621 */ 1622 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1623 if (error) 1624 break; 1625 /* 1626 * INADDR_ANY is used to remove a previous selection. 1627 * When no interface is selected, a default one is 1628 * chosen every time a multicast packet is sent. 1629 */ 1630 if (addr.s_addr == INADDR_ANY) { 1631 imo->imo_multicast_ifp = NULL; 1632 break; 1633 } 1634 /* 1635 * The selected interface is identified by its local 1636 * IP address. Find the interface and confirm that 1637 * it supports multicasting. 1638 */ 1639 s = splimp(); 1640 ifp = ip_multicast_if(&addr, &ifindex); 1641 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1642 splx(s); 1643 error = EADDRNOTAVAIL; 1644 break; 1645 } 1646 imo->imo_multicast_ifp = ifp; 1647 if (ifindex) 1648 imo->imo_multicast_addr = addr; 1649 else 1650 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1651 splx(s); 1652 break; 1653 1654 case IP_MULTICAST_TTL: 1655 /* 1656 * Set the IP time-to-live for outgoing multicast packets. 1657 * The original multicast API required a char argument, 1658 * which is inconsistent with the rest of the socket API. 1659 * We allow either a char or an int. 1660 */ 1661 if (sopt->sopt_valsize == 1) { 1662 u_char ttl; 1663 error = sooptcopyin(sopt, &ttl, 1, 1); 1664 if (error) 1665 break; 1666 imo->imo_multicast_ttl = ttl; 1667 } else { 1668 u_int ttl; 1669 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1670 sizeof ttl); 1671 if (error) 1672 break; 1673 if (ttl > 255) 1674 error = EINVAL; 1675 else 1676 imo->imo_multicast_ttl = ttl; 1677 } 1678 break; 1679 1680 case IP_MULTICAST_LOOP: 1681 /* 1682 * Set the loopback flag for outgoing multicast packets. 1683 * Must be zero or one. The original multicast API required a 1684 * char argument, which is inconsistent with the rest 1685 * of the socket API. We allow either a char or an int. 1686 */ 1687 if (sopt->sopt_valsize == 1) { 1688 u_char loop; 1689 error = sooptcopyin(sopt, &loop, 1, 1); 1690 if (error) 1691 break; 1692 imo->imo_multicast_loop = !!loop; 1693 } else { 1694 u_int loop; 1695 error = sooptcopyin(sopt, &loop, sizeof loop, 1696 sizeof loop); 1697 if (error) 1698 break; 1699 imo->imo_multicast_loop = !!loop; 1700 } 1701 break; 1702 1703 case IP_ADD_MEMBERSHIP: 1704 /* 1705 * Add a multicast group membership. 1706 * Group must be a valid IP multicast address. 1707 */ 1708 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1709 if (error) 1710 break; 1711 1712 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1713 error = EINVAL; 1714 break; 1715 } 1716 s = splimp(); 1717 /* 1718 * If no interface address was provided, use the interface of 1719 * the route to the given multicast address. 1720 */ 1721 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1722 bzero((caddr_t)&ro, sizeof(ro)); 1723 dst = (struct sockaddr_in *)&ro.ro_dst; 1724 dst->sin_len = sizeof(*dst); 1725 dst->sin_family = AF_INET; 1726 dst->sin_addr = mreq.imr_multiaddr; 1727 rtalloc_ign(&ro, RTF_CLONING); 1728 if (ro.ro_rt == NULL) { 1729 error = EADDRNOTAVAIL; 1730 splx(s); 1731 break; 1732 } 1733 ifp = ro.ro_rt->rt_ifp; 1734 RTFREE(ro.ro_rt); 1735 } 1736 else { 1737 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1738 } 1739 1740 /* 1741 * See if we found an interface, and confirm that it 1742 * supports multicast. 1743 */ 1744 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1745 error = EADDRNOTAVAIL; 1746 splx(s); 1747 break; 1748 } 1749 /* 1750 * See if the membership already exists or if all the 1751 * membership slots are full. 1752 */ 1753 for (i = 0; i < imo->imo_num_memberships; ++i) { 1754 if (imo->imo_membership[i]->inm_ifp == ifp && 1755 imo->imo_membership[i]->inm_addr.s_addr 1756 == mreq.imr_multiaddr.s_addr) 1757 break; 1758 } 1759 if (i < imo->imo_num_memberships) { 1760 error = EADDRINUSE; 1761 splx(s); 1762 break; 1763 } 1764 if (i == IP_MAX_MEMBERSHIPS) { 1765 error = ETOOMANYREFS; 1766 splx(s); 1767 break; 1768 } 1769 /* 1770 * Everything looks good; add a new record to the multicast 1771 * address list for the given interface. 1772 */ 1773 if ((imo->imo_membership[i] = 1774 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1775 error = ENOBUFS; 1776 splx(s); 1777 break; 1778 } 1779 ++imo->imo_num_memberships; 1780 splx(s); 1781 break; 1782 1783 case IP_DROP_MEMBERSHIP: 1784 /* 1785 * Drop a multicast group membership. 1786 * Group must be a valid IP multicast address. 1787 */ 1788 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1789 if (error) 1790 break; 1791 1792 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1793 error = EINVAL; 1794 break; 1795 } 1796 1797 s = splimp(); 1798 /* 1799 * If an interface address was specified, get a pointer 1800 * to its ifnet structure. 1801 */ 1802 if (mreq.imr_interface.s_addr == INADDR_ANY) 1803 ifp = NULL; 1804 else { 1805 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1806 if (ifp == NULL) { 1807 error = EADDRNOTAVAIL; 1808 splx(s); 1809 break; 1810 } 1811 } 1812 /* 1813 * Find the membership in the membership array. 1814 */ 1815 for (i = 0; i < imo->imo_num_memberships; ++i) { 1816 if ((ifp == NULL || 1817 imo->imo_membership[i]->inm_ifp == ifp) && 1818 imo->imo_membership[i]->inm_addr.s_addr == 1819 mreq.imr_multiaddr.s_addr) 1820 break; 1821 } 1822 if (i == imo->imo_num_memberships) { 1823 error = EADDRNOTAVAIL; 1824 splx(s); 1825 break; 1826 } 1827 /* 1828 * Give up the multicast address record to which the 1829 * membership points. 1830 */ 1831 in_delmulti(imo->imo_membership[i]); 1832 /* 1833 * Remove the gap in the membership array. 1834 */ 1835 for (++i; i < imo->imo_num_memberships; ++i) 1836 imo->imo_membership[i-1] = imo->imo_membership[i]; 1837 --imo->imo_num_memberships; 1838 splx(s); 1839 break; 1840 1841 default: 1842 error = EOPNOTSUPP; 1843 break; 1844 } 1845 1846 /* 1847 * If all options have default values, no need to keep the mbuf. 1848 */ 1849 if (imo->imo_multicast_ifp == NULL && 1850 imo->imo_multicast_vif == -1 && 1851 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1852 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1853 imo->imo_num_memberships == 0) { 1854 free(*imop, M_IPMOPTS); 1855 *imop = NULL; 1856 } 1857 1858 return (error); 1859 } 1860 1861 /* 1862 * Return the IP multicast options in response to user getsockopt(). 1863 */ 1864 static int 1865 ip_getmoptions(sopt, imo) 1866 struct sockopt *sopt; 1867 register struct ip_moptions *imo; 1868 { 1869 struct in_addr addr; 1870 struct in_ifaddr *ia; 1871 int error, optval; 1872 u_char coptval; 1873 1874 error = 0; 1875 switch (sopt->sopt_name) { 1876 case IP_MULTICAST_VIF: 1877 if (imo != NULL) 1878 optval = imo->imo_multicast_vif; 1879 else 1880 optval = -1; 1881 error = sooptcopyout(sopt, &optval, sizeof optval); 1882 break; 1883 1884 case IP_MULTICAST_IF: 1885 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1886 addr.s_addr = INADDR_ANY; 1887 else if (imo->imo_multicast_addr.s_addr) { 1888 /* return the value user has set */ 1889 addr = imo->imo_multicast_addr; 1890 } else { 1891 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1892 addr.s_addr = (ia == NULL) ? INADDR_ANY 1893 : IA_SIN(ia)->sin_addr.s_addr; 1894 } 1895 error = sooptcopyout(sopt, &addr, sizeof addr); 1896 break; 1897 1898 case IP_MULTICAST_TTL: 1899 if (imo == 0) 1900 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1901 else 1902 optval = coptval = imo->imo_multicast_ttl; 1903 if (sopt->sopt_valsize == 1) 1904 error = sooptcopyout(sopt, &coptval, 1); 1905 else 1906 error = sooptcopyout(sopt, &optval, sizeof optval); 1907 break; 1908 1909 case IP_MULTICAST_LOOP: 1910 if (imo == 0) 1911 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1912 else 1913 optval = coptval = imo->imo_multicast_loop; 1914 if (sopt->sopt_valsize == 1) 1915 error = sooptcopyout(sopt, &coptval, 1); 1916 else 1917 error = sooptcopyout(sopt, &optval, sizeof optval); 1918 break; 1919 1920 default: 1921 error = ENOPROTOOPT; 1922 break; 1923 } 1924 return (error); 1925 } 1926 1927 /* 1928 * Discard the IP multicast options. 1929 */ 1930 void 1931 ip_freemoptions(imo) 1932 register struct ip_moptions *imo; 1933 { 1934 register int i; 1935 1936 if (imo != NULL) { 1937 for (i = 0; i < imo->imo_num_memberships; ++i) 1938 in_delmulti(imo->imo_membership[i]); 1939 free(imo, M_IPMOPTS); 1940 } 1941 } 1942 1943 /* 1944 * Routine called from ip_output() to loop back a copy of an IP multicast 1945 * packet to the input queue of a specified interface. Note that this 1946 * calls the output routine of the loopback "driver", but with an interface 1947 * pointer that might NOT be a loopback interface -- evil, but easier than 1948 * replicating that code here. 1949 */ 1950 static void 1951 ip_mloopback(ifp, m, dst, hlen) 1952 struct ifnet *ifp; 1953 register struct mbuf *m; 1954 register struct sockaddr_in *dst; 1955 int hlen; 1956 { 1957 register struct ip *ip; 1958 struct mbuf *copym; 1959 1960 copym = m_copy(m, 0, M_COPYALL); 1961 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1962 copym = m_pullup(copym, hlen); 1963 if (copym != NULL) { 1964 /* If needed, compute the checksum and mark it as valid. */ 1965 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1966 in_delayed_cksum(copym); 1967 copym->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1968 copym->m_pkthdr.csum_flags |= 1969 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1970 copym->m_pkthdr.csum_data = 0xffff; 1971 } 1972 /* 1973 * We don't bother to fragment if the IP length is greater 1974 * than the interface's MTU. Can this possibly matter? 1975 */ 1976 ip = mtod(copym, struct ip *); 1977 ip->ip_len = htons(ip->ip_len); 1978 ip->ip_off = htons(ip->ip_off); 1979 ip->ip_sum = 0; 1980 ip->ip_sum = in_cksum(copym, hlen); 1981 /* 1982 * NB: 1983 * It's not clear whether there are any lingering 1984 * reentrancy problems in other areas which might 1985 * be exposed by using ip_input directly (in 1986 * particular, everything which modifies the packet 1987 * in-place). Yet another option is using the 1988 * protosw directly to deliver the looped back 1989 * packet. For the moment, we'll err on the side 1990 * of safety by using if_simloop(). 1991 */ 1992 #if 1 /* XXX */ 1993 if (dst->sin_family != AF_INET) { 1994 printf("ip_mloopback: bad address family %d\n", 1995 dst->sin_family); 1996 dst->sin_family = AF_INET; 1997 } 1998 #endif 1999 2000 #ifdef notdef 2001 copym->m_pkthdr.rcvif = ifp; 2002 ip_input(copym); 2003 #else 2004 if_simloop(ifp, copym, dst->sin_family, 0); 2005 #endif 2006 } 2007 } 2008