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