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