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