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