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