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