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