ip_output.c revision 74213
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 74213 2001-03-13 17:07:06Z ru $ 35 */ 36 37#define _IP_VHL 38 39#include "opt_ipfw.h" 40#include "opt_ipdn.h" 41#include "opt_ipdivert.h" 42#include "opt_ipfilter.h" 43#include "opt_ipsec.h" 44#include "opt_pfil_hooks.h" 45 46#include <sys/param.h> 47#include <sys/systm.h> 48#include <sys/kernel.h> 49#include <sys/malloc.h> 50#include <sys/mbuf.h> 51#include <sys/protosw.h> 52#include <sys/socket.h> 53#include <sys/socketvar.h> 54 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 m->m_pkthdr.csum_flags |= CSUM_IP; 788 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_hwassist; 789 if (sw_csum & CSUM_DELAY_DATA) { 790 in_delayed_cksum(m); 791 sw_csum &= ~CSUM_DELAY_DATA; 792 } 793 m->m_pkthdr.csum_flags &= ifp->if_hwassist; 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 if (m->m_pkthdr.csum_flags & CSUM_UDP && csum == 0) 986 csum = 0xffff; 987 offset += m->m_pkthdr.csum_data; /* checksum offset */ 988 989 if (offset + sizeof(u_short) > m->m_len) { 990 printf("delayed m_pullup, m->len: %d off: %d p: %d\n", 991 m->m_len, offset, ip->ip_p); 992 /* 993 * XXX 994 * this shouldn't happen, but if it does, the 995 * correct behavior may be to insert the checksum 996 * in the existing chain instead of rearranging it. 997 */ 998 m = m_pullup(m, offset + sizeof(u_short)); 999 } 1000 *(u_short *)(m->m_data + offset) = csum; 1001} 1002 1003/* 1004 * Insert IP options into preformed packet. 1005 * Adjust IP destination as required for IP source routing, 1006 * as indicated by a non-zero in_addr at the start of the options. 1007 * 1008 * XXX This routine assumes that the packet has no options in place. 1009 */ 1010static struct mbuf * 1011ip_insertoptions(m, opt, phlen) 1012 register struct mbuf *m; 1013 struct mbuf *opt; 1014 int *phlen; 1015{ 1016 register struct ipoption *p = mtod(opt, struct ipoption *); 1017 struct mbuf *n; 1018 register struct ip *ip = mtod(m, struct ip *); 1019 unsigned optlen; 1020 1021 optlen = opt->m_len - sizeof(p->ipopt_dst); 1022 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET) 1023 return (m); /* XXX should fail */ 1024 if (p->ipopt_dst.s_addr) 1025 ip->ip_dst = p->ipopt_dst; 1026 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1027 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1028 if (n == 0) 1029 return (m); 1030 n->m_pkthdr.rcvif = (struct ifnet *)0; 1031 n->m_pkthdr.len = m->m_pkthdr.len + optlen; 1032 m->m_len -= sizeof(struct ip); 1033 m->m_data += sizeof(struct ip); 1034 n->m_next = m; 1035 m = n; 1036 m->m_len = optlen + sizeof(struct ip); 1037 m->m_data += max_linkhdr; 1038 (void)memcpy(mtod(m, void *), ip, sizeof(struct ip)); 1039 } else { 1040 m->m_data -= optlen; 1041 m->m_len += optlen; 1042 m->m_pkthdr.len += optlen; 1043 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1044 } 1045 ip = mtod(m, struct ip *); 1046 bcopy(p->ipopt_list, ip + 1, optlen); 1047 *phlen = sizeof(struct ip) + optlen; 1048 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2); 1049 ip->ip_len += optlen; 1050 return (m); 1051} 1052 1053/* 1054 * Copy options from ip to jp, 1055 * omitting those not copied during fragmentation. 1056 */ 1057int 1058ip_optcopy(ip, jp) 1059 struct ip *ip, *jp; 1060{ 1061 register u_char *cp, *dp; 1062 int opt, optlen, cnt; 1063 1064 cp = (u_char *)(ip + 1); 1065 dp = (u_char *)(jp + 1); 1066 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1067 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1068 opt = cp[0]; 1069 if (opt == IPOPT_EOL) 1070 break; 1071 if (opt == IPOPT_NOP) { 1072 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1073 *dp++ = IPOPT_NOP; 1074 optlen = 1; 1075 continue; 1076 } 1077#ifdef DIAGNOSTIC 1078 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1079 panic("malformed IPv4 option passed to ip_optcopy"); 1080#endif 1081 optlen = cp[IPOPT_OLEN]; 1082#ifdef DIAGNOSTIC 1083 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1084 panic("malformed IPv4 option passed to ip_optcopy"); 1085#endif 1086 /* bogus lengths should have been caught by ip_dooptions */ 1087 if (optlen > cnt) 1088 optlen = cnt; 1089 if (IPOPT_COPIED(opt)) { 1090 bcopy(cp, dp, optlen); 1091 dp += optlen; 1092 } 1093 } 1094 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1095 *dp++ = IPOPT_EOL; 1096 return (optlen); 1097} 1098 1099/* 1100 * IP socket option processing. 1101 */ 1102int 1103ip_ctloutput(so, sopt) 1104 struct socket *so; 1105 struct sockopt *sopt; 1106{ 1107 struct inpcb *inp = sotoinpcb(so); 1108 int error, optval; 1109 1110 error = optval = 0; 1111 if (sopt->sopt_level != IPPROTO_IP) { 1112 return (EINVAL); 1113 } 1114 1115 switch (sopt->sopt_dir) { 1116 case SOPT_SET: 1117 switch (sopt->sopt_name) { 1118 case IP_OPTIONS: 1119#ifdef notyet 1120 case IP_RETOPTS: 1121#endif 1122 { 1123 struct mbuf *m; 1124 if (sopt->sopt_valsize > MLEN) { 1125 error = EMSGSIZE; 1126 break; 1127 } 1128 MGET(m, sopt->sopt_p ? M_TRYWAIT : M_DONTWAIT, MT_HEADER); 1129 if (m == 0) { 1130 error = ENOBUFS; 1131 break; 1132 } 1133 m->m_len = sopt->sopt_valsize; 1134 error = sooptcopyin(sopt, mtod(m, char *), m->m_len, 1135 m->m_len); 1136 1137 return (ip_pcbopts(sopt->sopt_name, &inp->inp_options, 1138 m)); 1139 } 1140 1141 case IP_TOS: 1142 case IP_TTL: 1143 case IP_RECVOPTS: 1144 case IP_RECVRETOPTS: 1145 case IP_RECVDSTADDR: 1146 case IP_RECVIF: 1147#if defined(NFAITH) && NFAITH > 0 1148 case IP_FAITH: 1149#endif 1150 error = sooptcopyin(sopt, &optval, sizeof optval, 1151 sizeof optval); 1152 if (error) 1153 break; 1154 1155 switch (sopt->sopt_name) { 1156 case IP_TOS: 1157 inp->inp_ip_tos = optval; 1158 break; 1159 1160 case IP_TTL: 1161 inp->inp_ip_ttl = optval; 1162 break; 1163#define OPTSET(bit) \ 1164 if (optval) \ 1165 inp->inp_flags |= bit; \ 1166 else \ 1167 inp->inp_flags &= ~bit; 1168 1169 case IP_RECVOPTS: 1170 OPTSET(INP_RECVOPTS); 1171 break; 1172 1173 case IP_RECVRETOPTS: 1174 OPTSET(INP_RECVRETOPTS); 1175 break; 1176 1177 case IP_RECVDSTADDR: 1178 OPTSET(INP_RECVDSTADDR); 1179 break; 1180 1181 case IP_RECVIF: 1182 OPTSET(INP_RECVIF); 1183 break; 1184 1185#if defined(NFAITH) && NFAITH > 0 1186 case IP_FAITH: 1187 OPTSET(INP_FAITH); 1188 break; 1189#endif 1190 } 1191 break; 1192#undef OPTSET 1193 1194 case IP_MULTICAST_IF: 1195 case IP_MULTICAST_VIF: 1196 case IP_MULTICAST_TTL: 1197 case IP_MULTICAST_LOOP: 1198 case IP_ADD_MEMBERSHIP: 1199 case IP_DROP_MEMBERSHIP: 1200 error = ip_setmoptions(sopt, &inp->inp_moptions); 1201 break; 1202 1203 case IP_PORTRANGE: 1204 error = sooptcopyin(sopt, &optval, sizeof optval, 1205 sizeof optval); 1206 if (error) 1207 break; 1208 1209 switch (optval) { 1210 case IP_PORTRANGE_DEFAULT: 1211 inp->inp_flags &= ~(INP_LOWPORT); 1212 inp->inp_flags &= ~(INP_HIGHPORT); 1213 break; 1214 1215 case IP_PORTRANGE_HIGH: 1216 inp->inp_flags &= ~(INP_LOWPORT); 1217 inp->inp_flags |= INP_HIGHPORT; 1218 break; 1219 1220 case IP_PORTRANGE_LOW: 1221 inp->inp_flags &= ~(INP_HIGHPORT); 1222 inp->inp_flags |= INP_LOWPORT; 1223 break; 1224 1225 default: 1226 error = EINVAL; 1227 break; 1228 } 1229 break; 1230 1231#ifdef IPSEC 1232 case IP_IPSEC_POLICY: 1233 { 1234 caddr_t req; 1235 size_t len = 0; 1236 int priv; 1237 struct mbuf *m; 1238 int optname; 1239 1240 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */ 1241 break; 1242 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */ 1243 break; 1244 priv = (sopt->sopt_p != NULL && 1245 suser(sopt->sopt_p) != 0) ? 0 : 1; 1246 req = mtod(m, caddr_t); 1247 len = m->m_len; 1248 optname = sopt->sopt_name; 1249 error = ipsec4_set_policy(inp, optname, req, len, priv); 1250 m_freem(m); 1251 break; 1252 } 1253#endif /*IPSEC*/ 1254 1255 default: 1256 error = ENOPROTOOPT; 1257 break; 1258 } 1259 break; 1260 1261 case SOPT_GET: 1262 switch (sopt->sopt_name) { 1263 case IP_OPTIONS: 1264 case IP_RETOPTS: 1265 if (inp->inp_options) 1266 error = sooptcopyout(sopt, 1267 mtod(inp->inp_options, 1268 char *), 1269 inp->inp_options->m_len); 1270 else 1271 sopt->sopt_valsize = 0; 1272 break; 1273 1274 case IP_TOS: 1275 case IP_TTL: 1276 case IP_RECVOPTS: 1277 case IP_RECVRETOPTS: 1278 case IP_RECVDSTADDR: 1279 case IP_RECVIF: 1280 case IP_PORTRANGE: 1281#if defined(NFAITH) && NFAITH > 0 1282 case IP_FAITH: 1283#endif 1284 switch (sopt->sopt_name) { 1285 1286 case IP_TOS: 1287 optval = inp->inp_ip_tos; 1288 break; 1289 1290 case IP_TTL: 1291 optval = inp->inp_ip_ttl; 1292 break; 1293 1294#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1295 1296 case IP_RECVOPTS: 1297 optval = OPTBIT(INP_RECVOPTS); 1298 break; 1299 1300 case IP_RECVRETOPTS: 1301 optval = OPTBIT(INP_RECVRETOPTS); 1302 break; 1303 1304 case IP_RECVDSTADDR: 1305 optval = OPTBIT(INP_RECVDSTADDR); 1306 break; 1307 1308 case IP_RECVIF: 1309 optval = OPTBIT(INP_RECVIF); 1310 break; 1311 1312 case IP_PORTRANGE: 1313 if (inp->inp_flags & INP_HIGHPORT) 1314 optval = IP_PORTRANGE_HIGH; 1315 else if (inp->inp_flags & INP_LOWPORT) 1316 optval = IP_PORTRANGE_LOW; 1317 else 1318 optval = 0; 1319 break; 1320 1321#if defined(NFAITH) && NFAITH > 0 1322 case IP_FAITH: 1323 optval = OPTBIT(INP_FAITH); 1324 break; 1325#endif 1326 } 1327 error = sooptcopyout(sopt, &optval, sizeof optval); 1328 break; 1329 1330 case IP_MULTICAST_IF: 1331 case IP_MULTICAST_VIF: 1332 case IP_MULTICAST_TTL: 1333 case IP_MULTICAST_LOOP: 1334 case IP_ADD_MEMBERSHIP: 1335 case IP_DROP_MEMBERSHIP: 1336 error = ip_getmoptions(sopt, inp->inp_moptions); 1337 break; 1338 1339#ifdef IPSEC 1340 case IP_IPSEC_POLICY: 1341 { 1342 struct mbuf *m = NULL; 1343 caddr_t req = NULL; 1344 size_t len = 0; 1345 1346 if (m != 0) { 1347 req = mtod(m, caddr_t); 1348 len = m->m_len; 1349 } 1350 error = ipsec4_get_policy(sotoinpcb(so), req, len, &m); 1351 if (error == 0) 1352 error = soopt_mcopyout(sopt, m); /* XXX */ 1353 if (error == 0) 1354 m_freem(m); 1355 break; 1356 } 1357#endif /*IPSEC*/ 1358 1359 default: 1360 error = ENOPROTOOPT; 1361 break; 1362 } 1363 break; 1364 } 1365 return (error); 1366} 1367 1368/* 1369 * Set up IP options in pcb for insertion in output packets. 1370 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1371 * with destination address if source routed. 1372 */ 1373static int 1374ip_pcbopts(optname, pcbopt, m) 1375 int optname; 1376 struct mbuf **pcbopt; 1377 register struct mbuf *m; 1378{ 1379 register int cnt, optlen; 1380 register u_char *cp; 1381 u_char opt; 1382 1383 /* turn off any old options */ 1384 if (*pcbopt) 1385 (void)m_free(*pcbopt); 1386 *pcbopt = 0; 1387 if (m == (struct mbuf *)0 || m->m_len == 0) { 1388 /* 1389 * Only turning off any previous options. 1390 */ 1391 if (m) 1392 (void)m_free(m); 1393 return (0); 1394 } 1395 1396 if (m->m_len % sizeof(int32_t)) 1397 goto bad; 1398 /* 1399 * IP first-hop destination address will be stored before 1400 * actual options; move other options back 1401 * and clear it when none present. 1402 */ 1403 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1404 goto bad; 1405 cnt = m->m_len; 1406 m->m_len += sizeof(struct in_addr); 1407 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1408 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt); 1409 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1410 1411 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1412 opt = cp[IPOPT_OPTVAL]; 1413 if (opt == IPOPT_EOL) 1414 break; 1415 if (opt == IPOPT_NOP) 1416 optlen = 1; 1417 else { 1418 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1419 goto bad; 1420 optlen = cp[IPOPT_OLEN]; 1421 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1422 goto bad; 1423 } 1424 switch (opt) { 1425 1426 default: 1427 break; 1428 1429 case IPOPT_LSRR: 1430 case IPOPT_SSRR: 1431 /* 1432 * user process specifies route as: 1433 * ->A->B->C->D 1434 * D must be our final destination (but we can't 1435 * check that since we may not have connected yet). 1436 * A is first hop destination, which doesn't appear in 1437 * actual IP option, but is stored before the options. 1438 */ 1439 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1440 goto bad; 1441 m->m_len -= sizeof(struct in_addr); 1442 cnt -= sizeof(struct in_addr); 1443 optlen -= sizeof(struct in_addr); 1444 cp[IPOPT_OLEN] = optlen; 1445 /* 1446 * Move first hop before start of options. 1447 */ 1448 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1449 sizeof(struct in_addr)); 1450 /* 1451 * Then copy rest of options back 1452 * to close up the deleted entry. 1453 */ 1454 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] + 1455 sizeof(struct in_addr)), 1456 (caddr_t)&cp[IPOPT_OFFSET+1], 1457 (unsigned)cnt + sizeof(struct in_addr)); 1458 break; 1459 } 1460 } 1461 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1462 goto bad; 1463 *pcbopt = m; 1464 return (0); 1465 1466bad: 1467 (void)m_free(m); 1468 return (EINVAL); 1469} 1470 1471/* 1472 * XXX 1473 * The whole multicast option thing needs to be re-thought. 1474 * Several of these options are equally applicable to non-multicast 1475 * transmission, and one (IP_MULTICAST_TTL) totally duplicates a 1476 * standard option (IP_TTL). 1477 */ 1478/* 1479 * Set the IP multicast options in response to user setsockopt(). 1480 */ 1481static int 1482ip_setmoptions(sopt, imop) 1483 struct sockopt *sopt; 1484 struct ip_moptions **imop; 1485{ 1486 int error = 0; 1487 int i; 1488 struct in_addr addr; 1489 struct ip_mreq mreq; 1490 struct ifnet *ifp; 1491 struct ip_moptions *imo = *imop; 1492 struct route ro; 1493 struct sockaddr_in *dst; 1494 int s; 1495 1496 if (imo == NULL) { 1497 /* 1498 * No multicast option buffer attached to the pcb; 1499 * allocate one and initialize to default values. 1500 */ 1501 imo = (struct ip_moptions*)malloc(sizeof(*imo), M_IPMOPTS, 1502 M_WAITOK); 1503 1504 if (imo == NULL) 1505 return (ENOBUFS); 1506 *imop = imo; 1507 imo->imo_multicast_ifp = NULL; 1508 imo->imo_multicast_vif = -1; 1509 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1510 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1511 imo->imo_num_memberships = 0; 1512 } 1513 1514 switch (sopt->sopt_name) { 1515 /* store an index number for the vif you wanna use in the send */ 1516 case IP_MULTICAST_VIF: 1517 if (legal_vif_num == 0) { 1518 error = EOPNOTSUPP; 1519 break; 1520 } 1521 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 1522 if (error) 1523 break; 1524 if (!legal_vif_num(i) && (i != -1)) { 1525 error = EINVAL; 1526 break; 1527 } 1528 imo->imo_multicast_vif = i; 1529 break; 1530 1531 case IP_MULTICAST_IF: 1532 /* 1533 * Select the interface for outgoing multicast packets. 1534 */ 1535 error = sooptcopyin(sopt, &addr, sizeof addr, sizeof addr); 1536 if (error) 1537 break; 1538 /* 1539 * INADDR_ANY is used to remove a previous selection. 1540 * When no interface is selected, a default one is 1541 * chosen every time a multicast packet is sent. 1542 */ 1543 if (addr.s_addr == INADDR_ANY) { 1544 imo->imo_multicast_ifp = NULL; 1545 break; 1546 } 1547 /* 1548 * The selected interface is identified by its local 1549 * IP address. Find the interface and confirm that 1550 * it supports multicasting. 1551 */ 1552 s = splimp(); 1553 INADDR_TO_IFP(addr, ifp); 1554 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1555 splx(s); 1556 error = EADDRNOTAVAIL; 1557 break; 1558 } 1559 imo->imo_multicast_ifp = ifp; 1560 splx(s); 1561 break; 1562 1563 case IP_MULTICAST_TTL: 1564 /* 1565 * Set the IP time-to-live for outgoing multicast packets. 1566 * The original multicast API required a char argument, 1567 * which is inconsistent with the rest of the socket API. 1568 * We allow either a char or an int. 1569 */ 1570 if (sopt->sopt_valsize == 1) { 1571 u_char ttl; 1572 error = sooptcopyin(sopt, &ttl, 1, 1); 1573 if (error) 1574 break; 1575 imo->imo_multicast_ttl = ttl; 1576 } else { 1577 u_int ttl; 1578 error = sooptcopyin(sopt, &ttl, sizeof ttl, 1579 sizeof ttl); 1580 if (error) 1581 break; 1582 if (ttl > 255) 1583 error = EINVAL; 1584 else 1585 imo->imo_multicast_ttl = ttl; 1586 } 1587 break; 1588 1589 case IP_MULTICAST_LOOP: 1590 /* 1591 * Set the loopback flag for outgoing multicast packets. 1592 * Must be zero or one. The original multicast API required a 1593 * char argument, which is inconsistent with the rest 1594 * of the socket API. We allow either a char or an int. 1595 */ 1596 if (sopt->sopt_valsize == 1) { 1597 u_char loop; 1598 error = sooptcopyin(sopt, &loop, 1, 1); 1599 if (error) 1600 break; 1601 imo->imo_multicast_loop = !!loop; 1602 } else { 1603 u_int loop; 1604 error = sooptcopyin(sopt, &loop, sizeof loop, 1605 sizeof loop); 1606 if (error) 1607 break; 1608 imo->imo_multicast_loop = !!loop; 1609 } 1610 break; 1611 1612 case IP_ADD_MEMBERSHIP: 1613 /* 1614 * Add a multicast group membership. 1615 * Group must be a valid IP multicast address. 1616 */ 1617 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1618 if (error) 1619 break; 1620 1621 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1622 error = EINVAL; 1623 break; 1624 } 1625 s = splimp(); 1626 /* 1627 * If no interface address was provided, use the interface of 1628 * the route to the given multicast address. 1629 */ 1630 if (mreq.imr_interface.s_addr == INADDR_ANY) { 1631 bzero((caddr_t)&ro, sizeof(ro)); 1632 dst = (struct sockaddr_in *)&ro.ro_dst; 1633 dst->sin_len = sizeof(*dst); 1634 dst->sin_family = AF_INET; 1635 dst->sin_addr = mreq.imr_multiaddr; 1636 rtalloc(&ro); 1637 if (ro.ro_rt == NULL) { 1638 error = EADDRNOTAVAIL; 1639 splx(s); 1640 break; 1641 } 1642 ifp = ro.ro_rt->rt_ifp; 1643 rtfree(ro.ro_rt); 1644 } 1645 else { 1646 INADDR_TO_IFP(mreq.imr_interface, ifp); 1647 } 1648 1649 /* 1650 * See if we found an interface, and confirm that it 1651 * supports multicast. 1652 */ 1653 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1654 error = EADDRNOTAVAIL; 1655 splx(s); 1656 break; 1657 } 1658 /* 1659 * See if the membership already exists or if all the 1660 * membership slots are full. 1661 */ 1662 for (i = 0; i < imo->imo_num_memberships; ++i) { 1663 if (imo->imo_membership[i]->inm_ifp == ifp && 1664 imo->imo_membership[i]->inm_addr.s_addr 1665 == mreq.imr_multiaddr.s_addr) 1666 break; 1667 } 1668 if (i < imo->imo_num_memberships) { 1669 error = EADDRINUSE; 1670 splx(s); 1671 break; 1672 } 1673 if (i == IP_MAX_MEMBERSHIPS) { 1674 error = ETOOMANYREFS; 1675 splx(s); 1676 break; 1677 } 1678 /* 1679 * Everything looks good; add a new record to the multicast 1680 * address list for the given interface. 1681 */ 1682 if ((imo->imo_membership[i] = 1683 in_addmulti(&mreq.imr_multiaddr, ifp)) == NULL) { 1684 error = ENOBUFS; 1685 splx(s); 1686 break; 1687 } 1688 ++imo->imo_num_memberships; 1689 splx(s); 1690 break; 1691 1692 case IP_DROP_MEMBERSHIP: 1693 /* 1694 * Drop a multicast group membership. 1695 * Group must be a valid IP multicast address. 1696 */ 1697 error = sooptcopyin(sopt, &mreq, sizeof mreq, sizeof mreq); 1698 if (error) 1699 break; 1700 1701 if (!IN_MULTICAST(ntohl(mreq.imr_multiaddr.s_addr))) { 1702 error = EINVAL; 1703 break; 1704 } 1705 1706 s = splimp(); 1707 /* 1708 * If an interface address was specified, get a pointer 1709 * to its ifnet structure. 1710 */ 1711 if (mreq.imr_interface.s_addr == INADDR_ANY) 1712 ifp = NULL; 1713 else { 1714 INADDR_TO_IFP(mreq.imr_interface, ifp); 1715 if (ifp == NULL) { 1716 error = EADDRNOTAVAIL; 1717 splx(s); 1718 break; 1719 } 1720 } 1721 /* 1722 * Find the membership in the membership array. 1723 */ 1724 for (i = 0; i < imo->imo_num_memberships; ++i) { 1725 if ((ifp == NULL || 1726 imo->imo_membership[i]->inm_ifp == ifp) && 1727 imo->imo_membership[i]->inm_addr.s_addr == 1728 mreq.imr_multiaddr.s_addr) 1729 break; 1730 } 1731 if (i == imo->imo_num_memberships) { 1732 error = EADDRNOTAVAIL; 1733 splx(s); 1734 break; 1735 } 1736 /* 1737 * Give up the multicast address record to which the 1738 * membership points. 1739 */ 1740 in_delmulti(imo->imo_membership[i]); 1741 /* 1742 * Remove the gap in the membership array. 1743 */ 1744 for (++i; i < imo->imo_num_memberships; ++i) 1745 imo->imo_membership[i-1] = imo->imo_membership[i]; 1746 --imo->imo_num_memberships; 1747 splx(s); 1748 break; 1749 1750 default: 1751 error = EOPNOTSUPP; 1752 break; 1753 } 1754 1755 /* 1756 * If all options have default values, no need to keep the mbuf. 1757 */ 1758 if (imo->imo_multicast_ifp == NULL && 1759 imo->imo_multicast_vif == -1 && 1760 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1761 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1762 imo->imo_num_memberships == 0) { 1763 free(*imop, M_IPMOPTS); 1764 *imop = NULL; 1765 } 1766 1767 return (error); 1768} 1769 1770/* 1771 * Return the IP multicast options in response to user getsockopt(). 1772 */ 1773static int 1774ip_getmoptions(sopt, imo) 1775 struct sockopt *sopt; 1776 register struct ip_moptions *imo; 1777{ 1778 struct in_addr addr; 1779 struct in_ifaddr *ia; 1780 int error, optval; 1781 u_char coptval; 1782 1783 error = 0; 1784 switch (sopt->sopt_name) { 1785 case IP_MULTICAST_VIF: 1786 if (imo != NULL) 1787 optval = imo->imo_multicast_vif; 1788 else 1789 optval = -1; 1790 error = sooptcopyout(sopt, &optval, sizeof optval); 1791 break; 1792 1793 case IP_MULTICAST_IF: 1794 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1795 addr.s_addr = INADDR_ANY; 1796 else { 1797 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1798 addr.s_addr = (ia == NULL) ? INADDR_ANY 1799 : IA_SIN(ia)->sin_addr.s_addr; 1800 } 1801 error = sooptcopyout(sopt, &addr, sizeof addr); 1802 break; 1803 1804 case IP_MULTICAST_TTL: 1805 if (imo == 0) 1806 optval = coptval = IP_DEFAULT_MULTICAST_TTL; 1807 else 1808 optval = coptval = imo->imo_multicast_ttl; 1809 if (sopt->sopt_valsize == 1) 1810 error = sooptcopyout(sopt, &coptval, 1); 1811 else 1812 error = sooptcopyout(sopt, &optval, sizeof optval); 1813 break; 1814 1815 case IP_MULTICAST_LOOP: 1816 if (imo == 0) 1817 optval = coptval = IP_DEFAULT_MULTICAST_LOOP; 1818 else 1819 optval = coptval = imo->imo_multicast_loop; 1820 if (sopt->sopt_valsize == 1) 1821 error = sooptcopyout(sopt, &coptval, 1); 1822 else 1823 error = sooptcopyout(sopt, &optval, sizeof optval); 1824 break; 1825 1826 default: 1827 error = ENOPROTOOPT; 1828 break; 1829 } 1830 return (error); 1831} 1832 1833/* 1834 * Discard the IP multicast options. 1835 */ 1836void 1837ip_freemoptions(imo) 1838 register struct ip_moptions *imo; 1839{ 1840 register int i; 1841 1842 if (imo != NULL) { 1843 for (i = 0; i < imo->imo_num_memberships; ++i) 1844 in_delmulti(imo->imo_membership[i]); 1845 free(imo, M_IPMOPTS); 1846 } 1847} 1848 1849/* 1850 * Routine called from ip_output() to loop back a copy of an IP multicast 1851 * packet to the input queue of a specified interface. Note that this 1852 * calls the output routine of the loopback "driver", but with an interface 1853 * pointer that might NOT be a loopback interface -- evil, but easier than 1854 * replicating that code here. 1855 */ 1856static void 1857ip_mloopback(ifp, m, dst, hlen) 1858 struct ifnet *ifp; 1859 register struct mbuf *m; 1860 register struct sockaddr_in *dst; 1861 int hlen; 1862{ 1863 register struct ip *ip; 1864 struct mbuf *copym; 1865 1866 copym = m_copy(m, 0, M_COPYALL); 1867 if (copym != NULL && (copym->m_flags & M_EXT || copym->m_len < hlen)) 1868 copym = m_pullup(copym, hlen); 1869 if (copym != NULL) { 1870 /* 1871 * We don't bother to fragment if the IP length is greater 1872 * than the interface's MTU. Can this possibly matter? 1873 */ 1874 ip = mtod(copym, struct ip *); 1875 HTONS(ip->ip_len); 1876 HTONS(ip->ip_off); 1877 ip->ip_sum = 0; 1878 if (ip->ip_vhl == IP_VHL_BORING) { 1879 ip->ip_sum = in_cksum_hdr(ip); 1880 } else { 1881 ip->ip_sum = in_cksum(copym, hlen); 1882 } 1883 /* 1884 * NB: 1885 * It's not clear whether there are any lingering 1886 * reentrancy problems in other areas which might 1887 * be exposed by using ip_input directly (in 1888 * particular, everything which modifies the packet 1889 * in-place). Yet another option is using the 1890 * protosw directly to deliver the looped back 1891 * packet. For the moment, we'll err on the side 1892 * of safety by using if_simloop(). 1893 */ 1894#if 1 /* XXX */ 1895 if (dst->sin_family != AF_INET) { 1896 printf("ip_mloopback: bad address family %d\n", 1897 dst->sin_family); 1898 dst->sin_family = AF_INET; 1899 } 1900#endif 1901 1902#ifdef notdef 1903 copym->m_pkthdr.rcvif = ifp; 1904 ip_input(copym); 1905#else 1906 /* if the checksum hasn't been computed, mark it as valid */ 1907 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1908 copym->m_pkthdr.csum_flags |= 1909 CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1910 copym->m_pkthdr.csum_data = 0xffff; 1911 } 1912 if_simloop(ifp, copym, dst->sin_family, 0); 1913#endif 1914 } 1915} 1916