ip_output.c revision 89614
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 34 * $FreeBSD: head/sys/netinet/ip_output.c 89614 2002-01-21 13:59:42Z ru $ 35 */ 36 37#define _IP_VHL 38 39#include "opt_ipfw.h" 40#include "opt_ipdn.h" 41#include "opt_ipdivert.h" 42#include "opt_ipfilter.h" 43#include "opt_ipsec.h" 44#include "opt_pfil_hooks.h" 45#include "opt_random_ip_id.h" 46 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/malloc.h> 51#include <sys/mbuf.h> 52#include <sys/protosw.h> 53#include <sys/socket.h> 54#include <sys/socketvar.h> 55 56#include <net/if.h> 57#include <net/route.h> 58 59#include <netinet/in.h> 60#include <netinet/in_systm.h> 61#include <netinet/ip.h> 62#include <netinet/in_pcb.h> 63#include <netinet/in_var.h> 64#include <netinet/ip_var.h> 65 66#include <machine/in_cksum.h> 67 68static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options"); 69 70#ifdef IPSEC 71#include <netinet6/ipsec.h> 72#include <netkey/key.h> 73#ifdef IPSEC_DEBUG 74#include <netkey/key_debug.h> 75#else 76#define KEYDEBUG(lev,arg) 77#endif 78#endif /*IPSEC*/ 79 80#include <netinet/ip_fw.h> 81#include <netinet/ip_dummynet.h> 82 83#ifdef IPFIREWALL_FORWARD_DEBUG 84#define print_ip(a) printf("%ld.%ld.%ld.%ld",(ntohl(a.s_addr)>>24)&0xFF,\ 85 (ntohl(a.s_addr)>>16)&0xFF,\ 86 (ntohl(a.s_addr)>>8)&0xFF,\ 87 (ntohl(a.s_addr))&0xFF); 88#endif 89 90u_short ip_id; 91 92static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 93static struct ifnet *ip_multicast_if __P((struct in_addr *, int *)); 94static void ip_mloopback 95 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *, int)); 96static int ip_getmoptions 97 __P((struct sockopt *, struct ip_moptions *)); 98static int ip_pcbopts __P((int, struct mbuf **, struct mbuf *)); 99static int ip_setmoptions 100 __P((struct sockopt *, struct ip_moptions **)); 101 102int ip_optcopy __P((struct ip *, struct ip *)); 103 104 105extern struct protosw inetsw[]; 106 107/* 108 * IP output. The packet in mbuf chain m contains a skeletal IP 109 * header (with len, off, ttl, proto, tos, src, dst). 110 * The mbuf chain containing the packet will be freed. 111 * The mbuf opt, if present, will not be freed. 112 */ 113int 114ip_output(m0, opt, ro, flags, imo) 115 struct mbuf *m0; 116 struct mbuf *opt; 117 struct route *ro; 118 int flags; 119 struct ip_moptions *imo; 120{ 121 struct ip *ip, *mhip; 122 struct ifnet *ifp; 123 struct mbuf *m = m0; 124 int hlen = sizeof (struct ip); 125 int len, off, error = 0; 126 struct route iproute; 127 struct sockaddr_in *dst; 128 struct in_ifaddr *ia; 129 int isbroadcast, sw_csum; 130 struct in_addr pkt_dst; 131#ifdef IPSEC 132 struct socket *so = NULL; 133 struct secpolicy *sp = NULL; 134#endif 135 u_int16_t divert_cookie; /* firewall cookie */ 136#ifdef PFIL_HOOKS 137 struct packet_filter_hook *pfh; 138 struct mbuf *m1; 139 int rv; 140#endif /* PFIL_HOOKS */ 141#ifdef IPFIREWALL_FORWARD 142 int fwd_rewrite_src = 0; 143#endif 144 struct ip_fw *rule = NULL; 145 146#ifdef IPDIVERT 147 /* Get and reset firewall cookie */ 148 divert_cookie = ip_divert_cookie; 149 ip_divert_cookie = 0; 150#else 151 divert_cookie = 0; 152#endif 153 154 /* 155 * dummynet packet are prepended a vestigial mbuf with 156 * m_type = MT_DUMMYNET and m_data pointing to the matching 157 * rule. 158 */ 159 if (m->m_type == MT_DUMMYNET) { 160 /* 161 * the packet was already tagged, so part of the 162 * processing was already done, and we need to go down. 163 * Get parameters from the header. 164 */ 165 rule = (struct ip_fw *)(m->m_data) ; 166 opt = NULL ; 167 ro = & ( ((struct dn_pkt *)m)->ro ) ; 168 imo = NULL ; 169 dst = ((struct dn_pkt *)m)->dn_dst ; 170 ifp = ((struct dn_pkt *)m)->ifp ; 171 flags = ((struct dn_pkt *)m)->flags ; 172 173 m0 = m = m->m_next ; 174#ifdef IPSEC 175 so = ipsec_getsocket(m); 176 (void)ipsec_setsocket(m, NULL); 177#endif 178 ip = mtod(m, struct ip *); 179 hlen = IP_VHL_HL(ip->ip_vhl) << 2 ; 180 ia = ifatoia(ro->ro_rt->rt_ifa); 181 goto sendit; 182 } else 183 rule = NULL ; 184#ifdef IPSEC 185 so = ipsec_getsocket(m); 186 (void)ipsec_setsocket(m, NULL); 187#endif 188 189#ifdef DIAGNOSTIC 190 if ((m->m_flags & M_PKTHDR) == 0) 191 panic("ip_output no HDR"); 192#endif 193 if (opt) { 194 m = ip_insertoptions(m, opt, &len); 195 hlen = len; 196 } 197 ip = mtod(m, struct ip *); 198 pkt_dst = ip_fw_fwd_addr == NULL 199 ? ip->ip_dst : ip_fw_fwd_addr->sin_addr; 200 201 /* 202 * Fill in IP header. 203 */ 204 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 205 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2); 206 ip->ip_off &= IP_DF; 207#ifdef RANDOM_IP_ID 208 ip->ip_id = ip_randomid(); 209#else 210 ip->ip_id = htons(ip_id++); 211#endif 212 ipstat.ips_localout++; 213 } else { 214 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 215 } 216 217 /* Route packet. */ 218 if (ro == NULL) { 219 ro = &iproute; 220 bzero(ro, sizeof(*ro)); 221 } 222 dst = (struct sockaddr_in *)&ro->ro_dst; 223 /* 224 * If there is a cached route, 225 * check that it is to the same destination 226 * and is still up. If not, free it and try again. 227 */ 228 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 229 dst->sin_addr.s_addr != pkt_dst.s_addr)) { 230 RTFREE(ro->ro_rt); 231 ro->ro_rt = (struct rtentry *)0; 232 } 233 if (ro->ro_rt == 0) { 234 dst->sin_family = AF_INET; 235 dst->sin_len = sizeof(*dst); 236 dst->sin_addr = pkt_dst; 237 } 238 /* 239 * If routing to interface only, 240 * short circuit routing lookup. 241 */ 242 if (flags & IP_ROUTETOIF) { 243 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == 0 && 244 (ia = ifatoia(ifa_ifwithnet(sintosa(dst)))) == 0) { 245 ipstat.ips_noroute++; 246 error = ENETUNREACH; 247 goto bad; 248 } 249 ifp = ia->ia_ifp; 250 ip->ip_ttl = 1; 251 isbroadcast = in_broadcast(dst->sin_addr, ifp); 252 } else if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) && 253 imo != NULL && imo->imo_multicast_ifp != NULL) { 254 /* 255 * Bypass the normal routing lookup for multicast 256 * packets if the interface is specified. 257 */ 258 ifp = imo->imo_multicast_ifp; 259 IFP_TO_IA(ifp, ia); 260 isbroadcast = 0; /* fool gcc */ 261 } else { 262 /* 263 * If this is the case, we probably don't want to allocate 264 * a protocol-cloned route since we didn't get one from the 265 * ULP. This lets TCP do its thing, while not burdening 266 * forwarding or ICMP with the overhead of cloning a route. 267 * Of course, we still want to do any cloning requested by 268 * the link layer, as this is probably required in all cases 269 * for correct operation (as it is for ARP). 270 */ 271 if (ro->ro_rt == 0) 272 rtalloc_ign(ro, RTF_PRCLONING); 273 if (ro->ro_rt == 0) { 274 ipstat.ips_noroute++; 275 error = EHOSTUNREACH; 276 goto bad; 277 } 278 ia = ifatoia(ro->ro_rt->rt_ifa); 279 ifp = ro->ro_rt->rt_ifp; 280 ro->ro_rt->rt_use++; 281 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 282 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 283 if (ro->ro_rt->rt_flags & RTF_HOST) 284 isbroadcast = (ro->ro_rt->rt_flags & RTF_BROADCAST); 285 else 286 isbroadcast = in_broadcast(dst->sin_addr, ifp); 287 } 288 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) { 289 struct in_multi *inm; 290 291 m->m_flags |= M_MCAST; 292 /* 293 * IP destination address is multicast. Make sure "dst" 294 * still points to the address in "ro". (It may have been 295 * changed to point to a gateway address, above.) 296 */ 297 dst = (struct sockaddr_in *)&ro->ro_dst; 298 /* 299 * See if the caller provided any multicast options 300 */ 301 if (imo != NULL) { 302 ip->ip_ttl = imo->imo_multicast_ttl; 303 if (imo->imo_multicast_vif != -1) 304 ip->ip_src.s_addr = 305 ip_mcast_src(imo->imo_multicast_vif); 306 } else 307 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 308 /* 309 * Confirm that the outgoing interface supports multicast. 310 */ 311 if ((imo == NULL) || (imo->imo_multicast_vif == -1)) { 312 if ((ifp->if_flags & IFF_MULTICAST) == 0) { 313 ipstat.ips_noroute++; 314 error = ENETUNREACH; 315 goto bad; 316 } 317 } 318 /* 319 * If source address not specified yet, use address 320 * of outgoing interface. 321 */ 322 if (ip->ip_src.s_addr == INADDR_ANY) { 323 /* Interface may have no addresses. */ 324 if (ia != NULL) 325 ip->ip_src = IA_SIN(ia)->sin_addr; 326 } 327 328 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 329 /* 330 * XXX 331 * delayed checksums are not currently 332 * compatible with IP multicast routing 333 */ 334 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 335 in_delayed_cksum(m); 336 m->m_pkthdr.csum_flags &= 337 ~CSUM_DELAY_DATA; 338 } 339 } 340 IN_LOOKUP_MULTI(pkt_dst, ifp, inm); 341 if (inm != NULL && 342 (imo == NULL || imo->imo_multicast_loop)) { 343 /* 344 * If we belong to the destination multicast group 345 * on the outgoing interface, and the caller did not 346 * forbid loopback, loop back a copy. 347 */ 348 ip_mloopback(ifp, m, dst, hlen); 349 } 350 else { 351 /* 352 * If we are acting as a multicast router, perform 353 * multicast forwarding as if the packet had just 354 * arrived on the interface to which we are about 355 * to send. The multicast forwarding function 356 * recursively calls this function, using the 357 * IP_FORWARDING flag to prevent infinite recursion. 358 * 359 * Multicasts that are looped back by ip_mloopback(), 360 * above, will be forwarded by the ip_input() routine, 361 * if necessary. 362 */ 363 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 364 /* 365 * Check if rsvp daemon is running. If not, don't 366 * set ip_moptions. This ensures that the packet 367 * is multicast and not just sent down one link 368 * as prescribed by rsvpd. 369 */ 370 if (!rsvp_on) 371 imo = NULL; 372 if (ip_mforward(ip, ifp, m, imo) != 0) { 373 m_freem(m); 374 goto done; 375 } 376 } 377 } 378 379 /* 380 * Multicasts with a time-to-live of zero may be looped- 381 * back, above, but must not be transmitted on a network. 382 * Also, multicasts addressed to the loopback interface 383 * are not sent -- the above call to ip_mloopback() will 384 * loop back a copy if this host actually belongs to the 385 * destination group on the loopback interface. 386 */ 387 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) { 388 m_freem(m); 389 goto done; 390 } 391 392 goto sendit; 393 } 394#ifndef notdef 395 /* 396 * If source address not specified yet, use address 397 * of outgoing interface. 398 */ 399 if (ip->ip_src.s_addr == INADDR_ANY) { 400 /* Interface may have no addresses. */ 401 if (ia != NULL) { 402 ip->ip_src = IA_SIN(ia)->sin_addr; 403#ifdef IPFIREWALL_FORWARD 404 /* Keep note that we did this - if the firewall changes 405 * the next-hop, our interface may change, changing the 406 * default source IP. It's a shame so much effort happens 407 * twice. Oh well. 408 */ 409 fwd_rewrite_src++; 410#endif /* IPFIREWALL_FORWARD */ 411 } 412 } 413#endif /* notdef */ 414 /* 415 * Verify that we have any chance at all of being able to queue 416 * the packet or packet fragments 417 */ 418 if ((ifp->if_snd.ifq_len + ip->ip_len / ifp->if_mtu + 1) >= 419 ifp->if_snd.ifq_maxlen) { 420 error = ENOBUFS; 421 ipstat.ips_odropped++; 422 goto bad; 423 } 424 425 /* 127/8 must not appear on wire - RFC1122. */ 426 if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || 427 (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { 428 if ((ifp->if_flags & IFF_LOOPBACK) == 0) { 429 ipstat.ips_badaddr++; 430 error = EADDRNOTAVAIL; 431 goto bad; 432 } 433 } 434 435 /* 436 * Look for broadcast address and 437 * verify user is allowed to send 438 * such a packet. 439 */ 440 if (isbroadcast) { 441 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 442 error = EADDRNOTAVAIL; 443 goto bad; 444 } 445 if ((flags & IP_ALLOWBROADCAST) == 0) { 446 error = EACCES; 447 goto bad; 448 } 449 /* don't allow broadcast messages to be fragmented */ 450 if ((u_short)ip->ip_len > ifp->if_mtu) { 451 error = EMSGSIZE; 452 goto bad; 453 } 454 m->m_flags |= M_BCAST; 455 } else { 456 m->m_flags &= ~M_BCAST; 457 } 458 459sendit: 460#ifdef IPSEC 461 /* get SP for this packet */ 462 if (so == NULL) 463 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, flags, &error); 464 else 465 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 466 467 if (sp == NULL) { 468 ipsecstat.out_inval++; 469 goto bad; 470 } 471 472 error = 0; 473 474 /* check policy */ 475 switch (sp->policy) { 476 case IPSEC_POLICY_DISCARD: 477 /* 478 * This packet is just discarded. 479 */ 480 ipsecstat.out_polvio++; 481 goto bad; 482 483 case IPSEC_POLICY_BYPASS: 484 case IPSEC_POLICY_NONE: 485 /* no need to do IPsec. */ 486 goto skip_ipsec; 487 488 case IPSEC_POLICY_IPSEC: 489 if (sp->req == NULL) { 490 /* acquire a policy */ 491 error = key_spdacquire(sp); 492 goto bad; 493 } 494 break; 495 496 case IPSEC_POLICY_ENTRUST: 497 default: 498 printf("ip_output: Invalid policy found. %d\n", sp->policy); 499 } 500 { 501 struct ipsec_output_state state; 502 bzero(&state, sizeof(state)); 503 state.m = m; 504 if (flags & IP_ROUTETOIF) { 505 state.ro = &iproute; 506 bzero(&iproute, sizeof(iproute)); 507 } else 508 state.ro = ro; 509 state.dst = (struct sockaddr *)dst; 510 511 ip->ip_sum = 0; 512 513 /* 514 * XXX 515 * delayed checksums are not currently compatible with IPsec 516 */ 517 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 518 in_delayed_cksum(m); 519 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 520 } 521 522 HTONS(ip->ip_len); 523 HTONS(ip->ip_off); 524 525 error = ipsec4_output(&state, sp, flags); 526 527 m = state.m; 528 if (flags & IP_ROUTETOIF) { 529 /* 530 * if we have tunnel mode SA, we may need to ignore 531 * IP_ROUTETOIF. 532 */ 533 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 534 flags &= ~IP_ROUTETOIF; 535 ro = state.ro; 536 } 537 } else 538 ro = state.ro; 539 dst = (struct sockaddr_in *)state.dst; 540 if (error) { 541 /* mbuf is already reclaimed in ipsec4_output. */ 542 m0 = NULL; 543 switch (error) { 544 case EHOSTUNREACH: 545 case ENETUNREACH: 546 case EMSGSIZE: 547 case ENOBUFS: 548 case ENOMEM: 549 break; 550 default: 551 printf("ip4_output (ipsec): error code %d\n", error); 552 /*fall through*/ 553 case ENOENT: 554 /* don't show these error codes to the user */ 555 error = 0; 556 break; 557 } 558 goto bad; 559 } 560 } 561 562 /* be sure to update variables that are affected by ipsec4_output() */ 563 ip = mtod(m, struct ip *); 564#ifdef _IP_VHL 565 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 566#else 567 hlen = ip->ip_hl << 2; 568#endif 569 if (ro->ro_rt == NULL) { 570 if ((flags & IP_ROUTETOIF) == 0) { 571 printf("ip_output: " 572 "can't update route after IPsec processing\n"); 573 error = EHOSTUNREACH; /*XXX*/ 574 goto bad; 575 } 576 } else { 577 ia = ifatoia(ro->ro_rt->rt_ifa); 578 ifp = ro->ro_rt->rt_ifp; 579 } 580 581 /* make it flipped, again. */ 582 NTOHS(ip->ip_len); 583 NTOHS(ip->ip_off); 584skip_ipsec: 585#endif /*IPSEC*/ 586 587 /* 588 * IpHack's section. 589 * - Xlate: translate packet's addr/port (NAT). 590 * - Firewall: deny/allow/etc. 591 * - Wrap: fake packet's addr/port <unimpl.> 592 * - Encapsulate: put it in another IP and send out. <unimp.> 593 */ 594#ifdef PFIL_HOOKS 595 /* 596 * Run through list of hooks for output packets. 597 */ 598 m1 = m; 599 pfh = pfil_hook_get(PFIL_OUT, &inetsw[ip_protox[IPPROTO_IP]].pr_pfh); 600 for (; pfh; pfh = TAILQ_NEXT(pfh, pfil_link)) 601 if (pfh->pfil_func) { 602 rv = pfh->pfil_func(ip, hlen, ifp, 1, &m1); 603 if (rv) { 604 error = EHOSTUNREACH; 605 goto done; 606 } 607 m = m1; 608 if (m == NULL) 609 goto done; 610 ip = mtod(m, struct ip *); 611 } 612#endif /* PFIL_HOOKS */ 613 614 /* 615 * Check with the firewall... 616 * but not if we are already being fwd'd from a firewall. 617 */ 618 if (fw_enable && IPFW_LOADED && !ip_fw_fwd_addr) { 619 struct sockaddr_in *old = dst; 620 621 off = ip_fw_chk_ptr(&ip, 622 hlen, ifp, &divert_cookie, &m, &rule, &dst); 623 /* 624 * On return we must do the following: 625 * m == NULL -> drop the pkt (old interface, deprecated) 626 * (off & IP_FW_PORT_DENY_FLAG) -> drop the pkt (new interface) 627 * 1<=off<= 0xffff -> DIVERT 628 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe 629 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet 630 * dst != old -> IPFIREWALL_FORWARD 631 * off==0, dst==old -> accept 632 * If some of the above modules are not compiled in, then 633 * we should't have to check the corresponding condition 634 * (because the ipfw control socket should not accept 635 * unsupported rules), but better play safe and drop 636 * packets in case of doubt. 637 */ 638 if (off & IP_FW_PORT_DENY_FLAG) { /* XXX new interface-denied */ 639 if (m) 640 m_freem(m); 641 error = EACCES; 642 goto done; 643 } 644 if (!m) { /* firewall said to reject */ 645 static int __debug=10; 646 647 if (__debug > 0) { 648 printf( 649 "firewall returns NULL, please update!\n"); 650 __debug--; 651 } 652 error = EACCES; 653 goto done; 654 } 655 if (off == 0 && dst == old) /* common case */ 656 goto pass; 657 if (DUMMYNET_LOADED && (off & IP_FW_PORT_DYNT_FLAG) != 0) { 658 /* 659 * pass the pkt to dummynet. Need to include 660 * pipe number, m, ifp, ro, dst because these are 661 * not recomputed in the next pass. 662 * All other parameters have been already used and 663 * so they are not needed anymore. 664 * XXX note: if the ifp or ro entry are deleted 665 * while a pkt is in dummynet, we are in trouble! 666 */ 667 error = ip_dn_io_ptr(off & 0xffff, DN_TO_IP_OUT, m, 668 ifp, ro, dst, rule, flags); 669 goto done; 670 } 671#ifdef IPDIVERT 672 if (off != 0 && (off & IP_FW_PORT_DYNT_FLAG) == 0) { 673 struct mbuf *clone = NULL; 674 675 /* Clone packet if we're doing a 'tee' */ 676 if ((off & IP_FW_PORT_TEE_FLAG) != 0) 677 clone = m_dup(m, M_DONTWAIT); 678 679 /* 680 * XXX 681 * delayed checksums are not currently compatible 682 * with divert sockets. 683 */ 684 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 685 in_delayed_cksum(m); 686 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 687 } 688 689 /* Restore packet header fields to original values */ 690 HTONS(ip->ip_len); 691 HTONS(ip->ip_off); 692 693 /* Deliver packet to divert input routine */ 694 ip_divert_cookie = divert_cookie; 695 divert_packet(m, 0, off & 0xffff); 696 697 /* If 'tee', continue with original packet */ 698 if (clone != NULL) { 699 m = clone; 700 ip = mtod(m, struct ip *); 701 goto pass; 702 } 703 goto done; 704 } 705#endif 706 707#ifdef IPFIREWALL_FORWARD 708 /* Here we check dst to make sure it's directly reachable on the 709 * interface we previously thought it was. 710 * If it isn't (which may be likely in some situations) we have 711 * to re-route it (ie, find a route for the next-hop and the 712 * associated interface) and set them here. This is nested 713 * forwarding which in most cases is undesirable, except where 714 * such control is nigh impossible. So we do it here. 715 * And I'm babbling. 716 */ 717 if (off == 0 && old != dst) { 718 struct in_ifaddr *ia; 719 720 /* It's changed... */ 721 /* There must be a better way to do this next line... */ 722 static struct route sro_fwd, *ro_fwd = &sro_fwd; 723#ifdef IPFIREWALL_FORWARD_DEBUG 724 printf("IPFIREWALL_FORWARD: New dst ip: "); 725 print_ip(dst->sin_addr); 726 printf("\n"); 727#endif 728 /* 729 * We need to figure out if we have been forwarded 730 * to a local socket. If so then we should somehow 731 * "loop back" to ip_input, and get directed to the 732 * PCB as if we had received this packet. This is 733 * because it may be dificult to identify the packets 734 * you want to forward until they are being output 735 * and have selected an interface. (e.g. locally 736 * initiated packets) If we used the loopback inteface, 737 * we would not be able to control what happens 738 * as the packet runs through ip_input() as 739 * it is done through a ISR. 740 */ 741 LIST_FOREACH(ia, 742 INADDR_HASH(dst->sin_addr.s_addr), ia_hash) { 743 /* 744 * If the addr to forward to is one 745 * of ours, we pretend to 746 * be the destination for this packet. 747 */ 748 if (IA_SIN(ia)->sin_addr.s_addr == 749 dst->sin_addr.s_addr) 750 break; 751 } 752 if (ia) { 753 /* tell ip_input "dont filter" */ 754 ip_fw_fwd_addr = dst; 755 if (m->m_pkthdr.rcvif == NULL) 756 m->m_pkthdr.rcvif = ifunit("lo0"); 757 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 758 m->m_pkthdr.csum_flags |= 759 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 760 m0->m_pkthdr.csum_data = 0xffff; 761 } 762 m->m_pkthdr.csum_flags |= 763 CSUM_IP_CHECKED | CSUM_IP_VALID; 764 HTONS(ip->ip_len); 765 HTONS(ip->ip_off); 766 ip_input(m); 767 goto done; 768 } 769 /* Some of the logic for this was 770 * nicked from above. 771 * 772 * This rewrites the cached route in a local PCB. 773 * Is this what we want to do? 774 */ 775 bcopy(dst, &ro_fwd->ro_dst, sizeof(*dst)); 776 777 ro_fwd->ro_rt = 0; 778 rtalloc_ign(ro_fwd, RTF_PRCLONING); 779 780 if (ro_fwd->ro_rt == 0) { 781 ipstat.ips_noroute++; 782 error = EHOSTUNREACH; 783 goto bad; 784 } 785 786 ia = ifatoia(ro_fwd->ro_rt->rt_ifa); 787 ifp = ro_fwd->ro_rt->rt_ifp; 788 ro_fwd->ro_rt->rt_use++; 789 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY) 790 dst = (struct sockaddr_in *)ro_fwd->ro_rt->rt_gateway; 791 if (ro_fwd->ro_rt->rt_flags & RTF_HOST) 792 isbroadcast = 793 (ro_fwd->ro_rt->rt_flags & RTF_BROADCAST); 794 else 795 isbroadcast = in_broadcast(dst->sin_addr, ifp); 796 if (ro->ro_rt) 797 RTFREE(ro->ro_rt); 798 ro->ro_rt = ro_fwd->ro_rt; 799 dst = (struct sockaddr_in *)&ro_fwd->ro_dst; 800 801 /* 802 * If we added a default src ip earlier, 803 * which would have been gotten from the-then 804 * interface, do it again, from the new one. 805 */ 806 if (fwd_rewrite_src) 807 ip->ip_src = IA_SIN(ia)->sin_addr; 808 goto pass ; 809 } 810#endif /* IPFIREWALL_FORWARD */ 811 /* 812 * if we get here, none of the above matches, and 813 * we have to drop the pkt 814 */ 815 m_freem(m); 816 error = EACCES; /* not sure this is the right error msg */ 817 goto done; 818 } 819 820 ip_fw_fwd_addr = NULL; 821pass: 822 m->m_pkthdr.csum_flags |= CSUM_IP; 823 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 824 if (sw_csum & CSUM_DELAY_DATA) { 825 in_delayed_cksum(m); 826 sw_csum &= ~CSUM_DELAY_DATA; 827 } 828 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 829 830 /* 831 * If small enough for interface, or the interface will take 832 * care of the fragmentation for us, can just send directly. 833 */ 834 if ((u_short)ip->ip_len <= ifp->if_mtu || 835 ifp->if_hwassist & CSUM_FRAGMENT) { 836 HTONS(ip->ip_len); 837 HTONS(ip->ip_off); 838 ip->ip_sum = 0; 839 if (sw_csum & CSUM_DELAY_IP) { 840 if (ip->ip_vhl == IP_VHL_BORING) { 841 ip->ip_sum = in_cksum_hdr(ip); 842 } else { 843 ip->ip_sum = in_cksum(m, hlen); 844 } 845 } 846 847 /* Record statistics for this interface address. */ 848 if (!(flags & IP_FORWARDING) && ia) { 849 ia->ia_ifa.if_opackets++; 850 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 851 } 852 853#ifdef IPSEC 854 /* clean ipsec history once it goes out of the node */ 855 ipsec_delaux(m); 856#endif 857 858 error = (*ifp->if_output)(ifp, m, 859 (struct sockaddr *)dst, ro->ro_rt); 860 goto done; 861 } 862 /* 863 * Too large for interface; fragment if possible. 864 * Must be able to put at least 8 bytes per fragment. 865 */ 866 if (ip->ip_off & IP_DF) { 867 error = EMSGSIZE; 868 /* 869 * This case can happen if the user changed the MTU 870 * of an interface after enabling IP on it. Because 871 * most netifs don't keep track of routes pointing to 872 * them, there is no way for one to update all its 873 * routes when the MTU is changed. 874 */ 875 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) 876 && !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) 877 && (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) { 878 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu; 879 } 880 ipstat.ips_cantfrag++; 881 goto bad; 882 } 883 len = (ifp->if_mtu - hlen) &~ 7; 884 if (len < 8) { 885 error = EMSGSIZE; 886 goto bad; 887 } 888 889 /* 890 * if the interface will not calculate checksums on 891 * fragmented packets, then do it here. 892 */ 893 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA && 894 (ifp->if_hwassist & CSUM_IP_FRAGS) == 0) { 895 in_delayed_cksum(m); 896 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 897 } 898 899 { 900 int mhlen, firstlen = len; 901 struct mbuf **mnext = &m->m_nextpkt; 902 int nfrags = 1; 903 904 /* 905 * Loop through length of segment after first fragment, 906 * make new header and copy data of each part and link onto chain. 907 */ 908 m0 = m; 909 mhlen = sizeof (struct ip); 910 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) { 911 MGETHDR(m, M_DONTWAIT, MT_HEADER); 912 if (m == 0) { 913 error = ENOBUFS; 914 ipstat.ips_odropped++; 915 goto sendorfree; 916 } 917 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG; 918 m->m_data += max_linkhdr; 919 mhip = mtod(m, struct ip *); 920 *mhip = *ip; 921 if (hlen > sizeof (struct ip)) { 922 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 923 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2); 924 } 925 m->m_len = mhlen; 926 mhip->ip_off = ((off - hlen) >> 3) + ip->ip_off; 927 if (off + len >= (u_short)ip->ip_len) 928 len = (u_short)ip->ip_len - off; 929 else 930 mhip->ip_off |= IP_MF; 931 mhip->ip_len = htons((u_short)(len + mhlen)); 932 m->m_next = m_copy(m0, off, len); 933 if (m->m_next == 0) { 934 (void) m_free(m); 935 error = ENOBUFS; /* ??? */ 936 ipstat.ips_odropped++; 937 goto sendorfree; 938 } 939 m->m_pkthdr.len = mhlen + len; 940 m->m_pkthdr.rcvif = (struct ifnet *)0; 941 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags; 942 HTONS(mhip->ip_off); 943 mhip->ip_sum = 0; 944 if (sw_csum & CSUM_DELAY_IP) { 945 if (mhip->ip_vhl == IP_VHL_BORING) { 946 mhip->ip_sum = in_cksum_hdr(mhip); 947 } else { 948 mhip->ip_sum = in_cksum(m, mhlen); 949 } 950 } 951 *mnext = m; 952 mnext = &m->m_nextpkt; 953 nfrags++; 954 } 955 ipstat.ips_ofragments += nfrags; 956 957 /* set first/last markers for fragment chain */ 958 m->m_flags |= M_LASTFRAG; 959 m0->m_flags |= M_FIRSTFRAG | M_FRAG; 960 m0->m_pkthdr.csum_data = nfrags; 961 962 /* 963 * Update first fragment by trimming what's been copied out 964 * and updating header, then send each fragment (in order). 965 */ 966 m = m0; 967 m_adj(m, hlen + firstlen - (u_short)ip->ip_len); 968 m->m_pkthdr.len = hlen + firstlen; 969 ip->ip_len = htons((u_short)m->m_pkthdr.len); 970 ip->ip_off |= IP_MF; 971 HTONS(ip->ip_off); 972 ip->ip_sum = 0; 973 if (sw_csum & CSUM_DELAY_IP) { 974 if (ip->ip_vhl == IP_VHL_BORING) { 975 ip->ip_sum = in_cksum_hdr(ip); 976 } else { 977 ip->ip_sum = in_cksum(m, hlen); 978 } 979 } 980sendorfree: 981 for (m = m0; m; m = m0) { 982 m0 = m->m_nextpkt; 983 m->m_nextpkt = 0; 984#ifdef IPSEC 985 /* clean ipsec history once it goes out of the node */ 986 ipsec_delaux(m); 987#endif 988 if (error == 0) { 989 /* Record statistics for this interface address. */ 990 if (ia != NULL) { 991 ia->ia_ifa.if_opackets++; 992 ia->ia_ifa.if_obytes += m->m_pkthdr.len; 993 } 994 995 error = (*ifp->if_output)(ifp, m, 996 (struct sockaddr *)dst, ro->ro_rt); 997 } else 998 m_freem(m); 999 } 1000 1001 if (error == 0) 1002 ipstat.ips_fragmented++; 1003 } 1004done: 1005#ifdef IPSEC 1006 if (ro == &iproute && ro->ro_rt) { 1007 RTFREE(ro->ro_rt); 1008 ro->ro_rt = NULL; 1009 } 1010 if (sp != NULL) { 1011 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 1012 printf("DP ip_output call free SP:%p\n", sp)); 1013 key_freesp(sp); 1014 } 1015#endif /* IPSEC */ 1016 return (error); 1017bad: 1018 m_freem(m); 1019 goto done; 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_VHL_HL(ip->ip_vhl) << 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 + (u_short)ip->ip_len > IP_MAXPACKET) 1069 return (m); /* XXX should fail */ 1070 if (p->ipopt_dst.s_addr) 1071 ip->ip_dst = p->ipopt_dst; 1072 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1073 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1074 if (n == 0) 1075 return (m); 1076 n->m_pkthdr.rcvif = (struct ifnet *)0; 1077 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1078 m->m_len -= sizeof(struct ip); 1079 m->m_data += sizeof(struct ip); 1080 n->m_next = m; 1081 m = n; 1082 m->m_len = optlen + sizeof(struct ip); 1083 m->m_data += max_linkhdr; 1084 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1085 } else { 1086 m->m_data -= optlen; 1087 m->m_len += optlen; 1088 m->m_pkthdr.len += optlen; 1089 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1090 } 1091 ip = mtod(m, struct ip *); 1092 bcopy(p->ipopt_list, ip + 1, optlen); 1093 *phlen = sizeof(struct ip) + optlen; 1094 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1095 ip->ip_len += optlen; 1096 return (m); 1097} 1098 1099/* 1100 * Copy options from ip to jp, 1101 * omitting those not copied during fragmentation. 1102 */ 1103int 1104ip_optcopy(ip, jp) 1105 struct ip *ip, *jp; 1106{ 1107 register u_char *cp, *dp; 1108 int opt, optlen, cnt; 1109 1110 cp = (u_char *)(ip + 1); 1111 dp = (u_char *)(jp + 1); 1112 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1113 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1114 opt = cp[0]; 1115 if (opt == IPOPT_EOL) 1116 break; 1117 if (opt == IPOPT_NOP) { 1118 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1119 *dp++ = IPOPT_NOP; 1120 optlen = 1; 1121 continue; 1122 } 1123#ifdef DIAGNOSTIC 1124 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1125 panic("malformed IPv4 option passed to ip_optcopy"); 1126#endif 1127 optlen = cp[IPOPT_OLEN]; 1128#ifdef DIAGNOSTIC 1129 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1130 panic("malformed IPv4 option passed to ip_optcopy"); 1131#endif 1132 /* bogus lengths should have been caught by ip_dooptions */ 1133 if (optlen > cnt) 1134 optlen = cnt; 1135 if (IPOPT_COPIED(opt)) { 1136 bcopy(cp, dp, optlen); 1137 dp += optlen; 1138 } 1139 } 1140 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1141 *dp++ = IPOPT_EOL; 1142 return (optlen); 1143} 1144 1145/* 1146 * IP socket option processing. 1147 */ 1148int 1149ip_ctloutput(so, sopt) 1150 struct socket *so; 1151 struct sockopt *sopt; 1152{ 1153 struct inpcb *inp = sotoinpcb(so); 1154 int error, optval; 1155 1156 error = optval = 0; 1157 if (sopt->sopt_level != IPPROTO_IP) { 1158 return (EINVAL); 1159 } 1160 1161 switch (sopt->sopt_dir) { 1162 case SOPT_SET: 1163 switch (sopt->sopt_name) { 1164 case IP_OPTIONS: 1165#ifdef notyet 1166 case IP_RETOPTS: 1167#endif 1168 { 1169 struct mbuf *m; 1170 if (sopt->sopt_valsize > MLEN) { 1171 error = EMSGSIZE; 1172 break; 1173 } 1174 MGET(m, sopt->sopt_td ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1175 if (m == 0) { 1176 error = ENOBUFS; 1177 break; 1178 } 1179 m->m_len = sopt->sopt_valsize; 1180 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1181 m->m_len); 1182 1183 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1184 m)); 1185 } 1186 1187 case IP_TOS: 1188 case IP_TTL: 1189 case IP_RECVOPTS: 1190 case IP_RECVRETOPTS: 1191 case IP_RECVDSTADDR: 1192 case IP_RECVIF: 1193 case IP_FAITH: 1194 error = sooptcopyin(sopt, &optval, sizeof optval, 1195 sizeof optval); 1196 if (error) 1197 break; 1198 1199 switch (sopt->sopt_name) { 1200 case IP_TOS: 1201 inp->inp_ip_tos = optval; 1202 break; 1203 1204 case IP_TTL: 1205 inp->inp_ip_ttl = optval; 1206 break; 1207#define OPTSET(bit) \ 1208 if (optval) \ 1209 inp->inp_flags |= bit; \ 1210 else \ 1211 inp->inp_flags &= ~bit; 1212 1213 case IP_RECVOPTS: 1214 OPTSET(INP_RECVOPTS); 1215 break; 1216 1217 case IP_RECVRETOPTS: 1218 OPTSET(INP_RECVRETOPTS); 1219 break; 1220 1221 case IP_RECVDSTADDR: 1222 OPTSET(INP_RECVDSTADDR); 1223 break; 1224 1225 case IP_RECVIF: 1226 OPTSET(INP_RECVIF); 1227 break; 1228 1229 case IP_FAITH: 1230 OPTSET(INP_FAITH); 1231 break; 1232 } 1233 break; 1234#undef OPTSET 1235 1236 case IP_MULTICAST_IF: 1237 case IP_MULTICAST_VIF: 1238 case IP_MULTICAST_TTL: 1239 case IP_MULTICAST_LOOP: 1240 case IP_ADD_MEMBERSHIP: 1241 case IP_DROP_MEMBERSHIP: 1242 error = ip_setmoptions(sopt, &inp->inp_moptions); 1243 break; 1244 1245 case IP_PORTRANGE: 1246 error = sooptcopyin(sopt, &optval, sizeof optval, 1247 sizeof optval); 1248 if (error) 1249 break; 1250 1251 switch (optval) { 1252 case IP_PORTRANGE_DEFAULT: 1253 inp->inp_flags &= ~(INP_LOWPORT); 1254 inp->inp_flags &= ~(INP_HIGHPORT); 1255 break; 1256 1257 case IP_PORTRANGE_HIGH: 1258 inp->inp_flags &= ~(INP_LOWPORT); 1259 inp->inp_flags |= INP_HIGHPORT; 1260 break; 1261 1262 case IP_PORTRANGE_LOW: 1263 inp->inp_flags &= ~(INP_HIGHPORT); 1264 inp->inp_flags |= INP_LOWPORT; 1265 break; 1266 1267 default: 1268 error = EINVAL; 1269 break; 1270 } 1271 break; 1272 1273#ifdef IPSEC 1274 case IP_IPSEC_POLICY: 1275 { 1276 caddr_t req; 1277 size_t len = 0; 1278 int priv; 1279 struct mbuf *m; 1280 int optname; 1281 1282 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1283 break; 1284 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1285 break; 1286 priv = (sopt->sopt_td != NULL && 1287 suser_td(sopt->sopt_td) != 0) ? 0 : 1; 1288 req = mtod(m, caddr_t); 1289 len = m->m_len; 1290 optname = sopt->sopt_name; 1291 error = ipsec4_set_policy(inp, optname, req, len, priv); 1292 m_freem(m); 1293 break; 1294 } 1295#endif /*IPSEC*/ 1296 1297 default: 1298 error = ENOPROTOOPT; 1299 break; 1300 } 1301 break; 1302 1303 case SOPT_GET: 1304 switch (sopt->sopt_name) { 1305 case IP_OPTIONS: 1306 case IP_RETOPTS: 1307 if (inp->inp_options) 1308 error = sooptcopyout(sopt, 1309 mtod(inp->inp_options, 1310 char *), 1311 inp->inp_options->m_len); 1312 else 1313 sopt->sopt_valsize = 0; 1314 break; 1315 1316 case IP_TOS: 1317 case IP_TTL: 1318 case IP_RECVOPTS: 1319 case IP_RECVRETOPTS: 1320 case IP_RECVDSTADDR: 1321 case IP_RECVIF: 1322 case IP_PORTRANGE: 1323 case IP_FAITH: 1324 switch (sopt->sopt_name) { 1325 1326 case IP_TOS: 1327 optval = inp->inp_ip_tos; 1328 break; 1329 1330 case IP_TTL: 1331 optval = inp->inp_ip_ttl; 1332 break; 1333 1334#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1335 1336 case IP_RECVOPTS: 1337 optval = OPTBIT(INP_RECVOPTS); 1338 break; 1339 1340 case IP_RECVRETOPTS: 1341 optval = OPTBIT(INP_RECVRETOPTS); 1342 break; 1343 1344 case IP_RECVDSTADDR: 1345 optval = OPTBIT(INP_RECVDSTADDR); 1346 break; 1347 1348 case IP_RECVIF: 1349 optval = OPTBIT(INP_RECVIF); 1350 break; 1351 1352 case IP_PORTRANGE: 1353 if (inp->inp_flags & INP_HIGHPORT) 1354 optval = IP_PORTRANGE_HIGH; 1355 else if (inp->inp_flags & INP_LOWPORT) 1356 optval = IP_PORTRANGE_LOW; 1357 else 1358 optval = 0; 1359 break; 1360 1361 case IP_FAITH: 1362 optval = OPTBIT(INP_FAITH); 1363 break; 1364 } 1365 error = sooptcopyout(sopt, &optval, sizeof optval); 1366 break; 1367 1368 case IP_MULTICAST_IF: 1369 case IP_MULTICAST_VIF: 1370 case IP_MULTICAST_TTL: 1371 case IP_MULTICAST_LOOP: 1372 case IP_ADD_MEMBERSHIP: 1373 case IP_DROP_MEMBERSHIP: 1374 error = ip_getmoptions(sopt, inp->inp_moptions); 1375 break; 1376 1377#ifdef IPSEC 1378 case IP_IPSEC_POLICY: 1379 { 1380 struct mbuf *m = NULL; 1381 caddr_t req = NULL; 1382 size_t len = 0; 1383 1384 if (m != 0) { 1385 req = mtod(m, caddr_t); 1386 len = m->m_len; 1387 } 1388 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1389 if (error == 0) 1390 error = soopt_mcopyout(sopt, m); /* XXX */ 1391 if (error == 0) 1392 m_freem(m); 1393 break; 1394 } 1395#endif /*IPSEC*/ 1396 1397 default: 1398 error = ENOPROTOOPT; 1399 break; 1400 } 1401 break; 1402 } 1403 return (error); 1404} 1405 1406/* 1407 * Set up IP options in pcb for insertion in output packets. 1408 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1409 * with destination address if source routed. 1410 */ 1411static int 1412ip_pcbopts(optname, pcbopt, m) 1413 int optname; 1414 struct mbuf **pcbopt; 1415 register struct mbuf *m; 1416{ 1417 register int cnt, optlen; 1418 register u_char *cp; 1419 u_char opt; 1420 1421 /* turn off any old options */ 1422 if (*pcbopt) 1423 (void)m_free(*pcbopt); 1424 *pcbopt = 0; 1425 if (m == (struct mbuf *)0 || m->m_len == 0) { 1426 /* 1427 * Only turning off any previous options. 1428 */ 1429 if (m) 1430 (void)m_free(m); 1431 return (0); 1432 } 1433 1434 if (m->m_len % sizeof(int32_t)) 1435 goto bad; 1436 /* 1437 * IP first-hop destination address will be stored before 1438 * actual options; move other options back 1439 * and clear it when none present. 1440 */ 1441 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1442 goto bad; 1443 cnt = m->m_len; 1444 m->m_len += sizeof(struct in_addr); 1445 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1446 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1447 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1448 1449 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1450 opt = cp[IPOPT_OPTVAL]; 1451 if (opt == IPOPT_EOL) 1452 break; 1453 if (opt == IPOPT_NOP) 1454 optlen = 1; 1455 else { 1456 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1457 goto bad; 1458 optlen = cp[IPOPT_OLEN]; 1459 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1460 goto bad; 1461 } 1462 switch (opt) { 1463 1464 default: 1465 break; 1466 1467 case IPOPT_LSRR: 1468 case IPOPT_SSRR: 1469 /* 1470 * user process specifies route as: 1471 * ->A->B->C->D 1472 * D must be our final destination (but we can't 1473 * check that since we may not have connected yet). 1474 * A is first hop destination, which doesn't appear in 1475 * actual IP option, but is stored before the options. 1476 */ 1477 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1478 goto bad; 1479 m->m_len -= sizeof(struct in_addr); 1480 cnt -= sizeof(struct in_addr); 1481 optlen -= sizeof(struct in_addr); 1482 cp[IPOPT_OLEN] = optlen; 1483 /* 1484 * Move first hop before start of options. 1485 */ 1486 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1487 sizeof(struct in_addr)); 1488 /* 1489 * Then copy rest of options back 1490 * to close up the deleted entry. 1491 */ 1492 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1493 sizeof(struct in_addr)), 1494 (caddr_t)&cp[IPOPT_OFFSET+1], 1495 (unsigned)cnt + sizeof(struct in_addr)); 1496 break; 1497 } 1498 } 1499 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1500 goto bad; 1501 *pcbopt = m; 1502 return (0); 1503 1504bad: 1505 (void)m_free(m); 1506 return (EINVAL); 1507} 1508 1509/* 1510 * XXX 1511 * The whole multicast option thing needs to be re-thought. 1512 * Several of these options are equally applicable to non-multicast 1513 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1514 * standard option (IP_TTL). 1515 */ 1516 1517/* 1518 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1519 */ 1520static struct ifnet * 1521ip_multicast_if(a, ifindexp) 1522 struct in_addr *a; 1523 int *ifindexp; 1524{ 1525 int ifindex; 1526 struct ifnet *ifp; 1527 1528 if (ifindexp) 1529 *ifindexp = 0; 1530 if (ntohl(a->s_addr) >> 24 == 0) { 1531 ifindex = ntohl(a->s_addr) & 0xffffff; 1532 if (ifindex < 0 || if_index < ifindex) 1533 return NULL; 1534 ifp = ifnet_byindex(ifindex); 1535 if (ifindexp) 1536 *ifindexp = ifindex; 1537 } else { 1538 INADDR_TO_IFP(*a, ifp); 1539 } 1540 return ifp; 1541} 1542 1543/* 1544 * Set the IP multicast options in response to user setsockopt(). 1545 */ 1546static int 1547ip_setmoptions(sopt, imop) 1548 struct sockopt *sopt; 1549 struct ip_moptions **imop; 1550{ 1551 int error = 0; 1552 int i; 1553 struct in_addr addr; 1554 struct ip_mreq mreq; 1555 struct ifnet *ifp; 1556 struct ip_moptions *imo = *imop; 1557 struct route ro; 1558 struct sockaddr_in *dst; 1559 int ifindex; 1560 int s; 1561 1562 if (imo == NULL) { 1563 /* 1564 * No multicast option buffer attached to the pcb; 1565 * allocate one and initialize to default values. 1566 */ 1567 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1568 M_WAITOK); 1569 1570 if (imo == NULL) 1571 return (ENOBUFS); 1572 *imop = imo; 1573 imo->imo_multicast_ifp = NULL; 1574 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1575 imo->imo_multicast_vif = -1; 1576 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1577 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1578 imo->imo_num_memberships = 0; 1579 } 1580 1581 switch (sopt->sopt_name) { 1582 /* store an index number for the vif you wanna use in the send */ 1583 case IP_MULTICAST_VIF: 1584 if (legal_vif_num == 0) { 1585 error = EOPNOTSUPP; 1586 break; 1587 } 1588 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1589 if (error) 1590 break; 1591 if (!legal_vif_num(i) && (i != -1)) { 1592 error = EINVAL; 1593 break; 1594 } 1595 imo->imo_multicast_vif = i; 1596 break; 1597 1598 case IP_MULTICAST_IF: 1599 /* 1600 * Select the interface for outgoing multicast packets. 1601 */ 1602 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1603 if (error) 1604 break; 1605 /* 1606 * INADDR_ANY is used to remove a previous selection. 1607 * When no interface is selected, a default one is 1608 * chosen every time a multicast packet is sent. 1609 */ 1610 if (addr.s_addr == INADDR_ANY) { 1611 imo->imo_multicast_ifp = NULL; 1612 break; 1613 } 1614 /* 1615 * The selected interface is identified by its local 1616 * IP address. Find the interface and confirm that 1617 * it supports multicasting. 1618 */ 1619 s = splimp(); 1620 ifp = ip_multicast_if(&addr, &ifindex); 1621 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1622 splx(s); 1623 error = EADDRNOTAVAIL; 1624 break; 1625 } 1626 imo->imo_multicast_ifp = ifp; 1627 if (ifindex) 1628 imo->imo_multicast_addr = addr; 1629 else 1630 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1631 splx(s); 1632 break; 1633 1634 case IP_MULTICAST_TTL: 1635 /* 1636 * Set the IP time-to-live for outgoing multicast packets. 1637 * The original multicast API required a char argument, 1638 * which is inconsistent with the rest of the socket API. 1639 * We allow either a char or an int. 1640 */ 1641 if (sopt->sopt_valsize == 1) { 1642 u_char ttl; 1643 error = sooptcopyin(sopt, &ttl, 1, 1); 1644 if (error) 1645 break; 1646 imo->imo_multicast_ttl = ttl; 1647 } else { 1648 u_int ttl; 1649 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1650 sizeof ttl); 1651 if (error) 1652 break; 1653 if (ttl > 255) 1654 error = EINVAL; 1655 else 1656 imo->imo_multicast_ttl = ttl; 1657 } 1658 break; 1659 1660 case IP_MULTICAST_LOOP: 1661 /* 1662 * Set the loopback flag for outgoing multicast packets. 1663 * Must be zero or one. The original multicast API required a 1664 * char argument, which is inconsistent with the rest 1665 * of the socket API. We allow either a char or an int. 1666 */ 1667 if (sopt->sopt_valsize == 1) { 1668 u_char loop; 1669 error = sooptcopyin(sopt, &loop, 1, 1); 1670 if (error) 1671 break; 1672 imo->imo_multicast_loop = !!loop; 1673 } else { 1674 u_int loop; 1675 error = sooptcopyin(sopt, &loop, sizeof loop, 1676 sizeof loop); 1677 if (error) 1678 break; 1679 imo->imo_multicast_loop = !!loop; 1680 } 1681 break; 1682 1683 case IP_ADD_MEMBERSHIP: 1684 /* 1685 * Add a multicast group membership. 1686 * Group must be a valid IP multicast address. 1687 */ 1688 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1689 if (error) 1690 break; 1691 1692 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1693 error = EINVAL; 1694 break; 1695 } 1696 s = splimp(); 1697 /* 1698 * If no interface address was provided, use the interface of 1699 * the route to the given multicast address. 1700 */ 1701 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1702 bzero((caddr_t)&ro, sizeof(ro)); 1703 dst = (struct sockaddr_in *)&ro.ro_dst; 1704 dst->sin_len = sizeof(*dst); 1705 dst->sin_family = AF_INET; 1706 dst->sin_addr = mreq.imr_multiaddr; 1707 rtalloc(&ro); 1708 if (ro.ro_rt == NULL) { 1709 error = EADDRNOTAVAIL; 1710 splx(s); 1711 break; 1712 } 1713 ifp = ro.ro_rt->rt_ifp; 1714 rtfree(ro.ro_rt); 1715 } 1716 else { 1717 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1718 } 1719 1720 /* 1721 * See if we found an interface, and confirm that it 1722 * supports multicast. 1723 */ 1724 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1725 error = EADDRNOTAVAIL; 1726 splx(s); 1727 break; 1728 } 1729 /* 1730 * See if the membership already exists or if all the 1731 * membership slots are full. 1732 */ 1733 for (i = 0; i < imo->imo_num_memberships; ++i) { 1734 if (imo->imo_membership[i]->inm_ifp == ifp && 1735 imo->imo_membership[i]->inm_addr.s_addr 1736 == mreq.imr_multiaddr.s_addr) 1737 break; 1738 } 1739 if (i < imo->imo_num_memberships) { 1740 error = EADDRINUSE; 1741 splx(s); 1742 break; 1743 } 1744 if (i == IP_MAX_MEMBERSHIPS) { 1745 error = ETOOMANYREFS; 1746 splx(s); 1747 break; 1748 } 1749 /* 1750 * Everything looks good; add a new record to the multicast 1751 * address list for the given interface. 1752 */ 1753 if ((imo->imo_membership[i] = 1754 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1755 error = ENOBUFS; 1756 splx(s); 1757 break; 1758 } 1759 ++imo->imo_num_memberships; 1760 splx(s); 1761 break; 1762 1763 case IP_DROP_MEMBERSHIP: 1764 /* 1765 * Drop a multicast group membership. 1766 * Group must be a valid IP multicast address. 1767 */ 1768 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1769 if (error) 1770 break; 1771 1772 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1773 error = EINVAL; 1774 break; 1775 } 1776 1777 s = splimp(); 1778 /* 1779 * If an interface address was specified, get a pointer 1780 * to its ifnet structure. 1781 */ 1782 if (mreq.imr_interface.s_addr == INADDR_ANY) 1783 ifp = NULL; 1784 else { 1785 ifp = ip_multicast_if(&mreq.imr_interface, NULL); 1786 if (ifp == NULL) { 1787 error = EADDRNOTAVAIL; 1788 splx(s); 1789 break; 1790 } 1791 } 1792 /* 1793 * Find the membership in the membership array. 1794 */ 1795 for (i = 0; i < imo->imo_num_memberships; ++i) { 1796 if ((ifp == NULL || 1797 imo->imo_membership[i]->inm_ifp == ifp) && 1798 imo->imo_membership[i]->inm_addr.s_addr == 1799 mreq.imr_multiaddr.s_addr) 1800 break; 1801 } 1802 if (i == imo->imo_num_memberships) { 1803 error = EADDRNOTAVAIL; 1804 splx(s); 1805 break; 1806 } 1807 /* 1808 * Give up the multicast address record to which the 1809 * membership points. 1810 */ 1811 in_delmulti(imo->imo_membership[i]); 1812 /* 1813 * Remove the gap in the membership array. 1814 */ 1815 for (++i; i < imo->imo_num_memberships; ++i) 1816 imo->imo_membership[i-1] = imo->imo_membership[i]; 1817 --imo->imo_num_memberships; 1818 splx(s); 1819 break; 1820 1821 default: 1822 error = EOPNOTSUPP; 1823 break; 1824 } 1825 1826 /* 1827 * If all options have default values, no need to keep the mbuf. 1828 */ 1829 if (imo->imo_multicast_ifp == NULL && 1830 imo->imo_multicast_vif == -1 && 1831 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1832 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1833 imo->imo_num_memberships == 0) { 1834 free(*imop, M_IPMOPTS); 1835 *imop = NULL; 1836 } 1837 1838 return (error); 1839} 1840 1841/* 1842 * Return the IP multicast options in response to user getsockopt(). 1843 */ 1844static int 1845ip_getmoptions(sopt, imo) 1846 struct sockopt *sopt; 1847 register struct ip_moptions *imo; 1848{ 1849 struct in_addr addr; 1850 struct in_ifaddr *ia; 1851 int error, optval; 1852 u_char coptval; 1853 1854 error = 0; 1855 switch (sopt->sopt_name) { 1856 case IP_MULTICAST_VIF: 1857 if (imo != NULL) 1858 optval = imo->imo_multicast_vif; 1859 else 1860 optval = -1; 1861 error = sooptcopyout(sopt, &optval, sizeof optval); 1862 break; 1863 1864 case IP_MULTICAST_IF: 1865 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1866 addr.s_addr = INADDR_ANY; 1867 else if (imo->imo_multicast_addr.s_addr) { 1868 /* return the value user has set */ 1869 addr = imo->imo_multicast_addr; 1870 } else { 1871 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1872 addr.s_addr = (ia == NULL) ? INADDR_ANY 1873 : IA_SIN(ia)->sin_addr.s_addr; 1874 } 1875 error = sooptcopyout(sopt, &addr, sizeof addr); 1876 break; 1877 1878 case IP_MULTICAST_TTL: 1879 if (imo == 0) 1880 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1881 else 1882 optval = coptval = imo->imo_multicast_ttl; 1883 if (sopt->sopt_valsize == 1) 1884 error = sooptcopyout(sopt, &coptval, 1); 1885 else 1886 error = sooptcopyout(sopt, &optval, sizeof optval); 1887 break; 1888 1889 case IP_MULTICAST_LOOP: 1890 if (imo == 0) 1891 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1892 else 1893 optval = coptval = imo->imo_multicast_loop; 1894 if (sopt->sopt_valsize == 1) 1895 error = sooptcopyout(sopt, &coptval, 1); 1896 else 1897 error = sooptcopyout(sopt, &optval, sizeof optval); 1898 break; 1899 1900 default: 1901 error = ENOPROTOOPT; 1902 break; 1903 } 1904 return (error); 1905} 1906 1907/* 1908 * Discard the IP multicast options. 1909 */ 1910void 1911ip_freemoptions(imo) 1912 register struct ip_moptions *imo; 1913{ 1914 register int i; 1915 1916 if (imo != NULL) { 1917 for (i = 0; i < imo->imo_num_memberships; ++i) 1918 in_delmulti(imo->imo_membership[i]); 1919 free(imo, M_IPMOPTS); 1920 } 1921} 1922 1923/* 1924 * Routine called from ip_output() to loop back a copy of an IP multicast 1925 * packet to the input queue of a specified interface. Note that this 1926 * calls the output routine of the loopback "driver", but with an interface 1927 * pointer that might NOT be a loopback interface -- evil, but easier than 1928 * replicating that code here. 1929 */ 1930static void 1931ip_mloopback(ifp, m, dst, hlen) 1932 struct ifnet *ifp; 1933 register struct mbuf *m; 1934 register struct sockaddr_in *dst; 1935 int hlen; 1936{ 1937 register struct ip *ip; 1938 struct mbuf *copym; 1939 1940 copym = m_copy(m, 0, M_COPYALL); 1941 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1942 copym = m_pullup(copym, hlen); 1943 if (copym != NULL) { 1944 /* 1945 * We don't bother to fragment if the IP length is greater 1946 * than the interface's MTU. Can this possibly matter? 1947 */ 1948 ip = mtod(copym, struct ip *); 1949 HTONS(ip->ip_len); 1950 HTONS(ip->ip_off); 1951 ip->ip_sum = 0; 1952 if (ip->ip_vhl == IP_VHL_BORING) { 1953 ip->ip_sum = in_cksum_hdr(ip); 1954 } else { 1955 ip->ip_sum = in_cksum(copym, hlen); 1956 } 1957 /* 1958 * NB: 1959 * It's not clear whether there are any lingering 1960 * reentrancy problems in other areas which might 1961 * be exposed by using ip_input directly (in 1962 * particular, everything which modifies the packet 1963 * in-place). Yet another option is using the 1964 * protosw directly to deliver the looped back 1965 * packet. For the moment, we'll err on the side 1966 * of safety by using if_simloop(). 1967 */ 1968#if 1 /* XXX */ 1969 if (dst->sin_family != AF_INET) { 1970 printf("ip_mloopback: bad address family %d\n", 1971 dst->sin_family); 1972 dst->sin_family = AF_INET; 1973 } 1974#endif 1975 1976#ifdef notdef 1977 copym->m_pkthdr.rcvif = ifp; 1978 ip_input(copym); 1979#else 1980 /* if the checksum hasn't been computed, mark it as valid */ 1981 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1982 copym->m_pkthdr.csum_flags |= 1983 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1984 copym->m_pkthdr.csum_data = 0xffff; 1985 } 1986 if_simloop(ifp, copym, dst->sin_family, 0); 1987#endif 1988 } 1989} 1990