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