1 /* $NetBSD: ip_output.c,v 1.119 2003/08/27 02:09:59 itojun Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /*- 33 * Copyright (c) 1998 The NetBSD Foundation, Inc. 34 * All rights reserved. 35 * 36 * This code is derived from software contributed to The NetBSD Foundation 37 * by Public Access Networks Corporation ("Panix"). It was developed under 38 * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon. 39 * 40 * Redistribution and use in source and binary forms, with or without 41 * modification, are permitted provided that the following conditions 42 * are met: 43 * 1. Redistributions of source code must retain the above copyright 44 * notice, this list of conditions and the following disclaimer. 45 * 2. Redistributions in binary form must reproduce the above copyright 46 * notice, this list of conditions and the following disclaimer in the 47 * documentation and/or other materials provided with the distribution. 48 * 3. All advertising materials mentioning features or use of this software 49 * must display the following acknowledgement: 50 * This product includes software developed by the NetBSD 51 * Foundation, Inc. and its contributors. 52 * 4. Neither the name of The NetBSD Foundation nor the names of its 53 * contributors may be used to endorse or promote products derived 54 * from this software without specific prior written permission. 55 * 56 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 57 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 59 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 60 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 61 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 62 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 63 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 64 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 65 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 66 * POSSIBILITY OF SUCH DAMAGE. 67 */ 68 69 /* 70 * Copyright (c) 1982, 1986, 1988, 1990, 1993 71 * The Regents of the University of California. All rights reserved. 72 * 73 * Redistribution and use in source and binary forms, with or without 74 * modification, are permitted provided that the following conditions 75 * are met: 76 * 1. Redistributions of source code must retain the above copyright 77 * notice, this list of conditions and the following disclaimer. 78 * 2. Redistributions in binary form must reproduce the above copyright 79 * notice, this list of conditions and the following disclaimer in the 80 * documentation and/or other materials provided with the distribution. 81 * 3. Neither the name of the University nor the names of its contributors 82 * may be used to endorse or promote products derived from this software 83 * without specific prior written permission. 84 * 85 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 86 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 87 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 88 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 89 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 90 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 91 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 92 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 93 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 94 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 95 * SUCH DAMAGE. 96 * 97 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94 98 */ 99 100 #include <sys/cdefs.h> 101 __KERNEL_RCSID(0, "$NetBSD: ip_output.c,v 1.119 2003/08/27 02:09:59 itojun Exp $"); 102 103 #include "opt_pfil_hooks.h" 104 #include "opt_ipsec.h" 105 #include "opt_mrouting.h" 106 107 #include <sys/param.h> 108 #include <sys/malloc.h> 109 #include <sys/mbuf.h> 110 #include <sys/errno.h> 111 #include <sys/protosw.h> 112 #include <sys/socket.h> 113 #include <sys/socketvar.h> 114 #ifdef FAST_IPSEC 115 #include <sys/domain.h> 116 #endif 117 #include <sys/systm.h> 118 #include <sys/proc.h> 119 120 #include <net/if.h> 121 #include <net/route.h> 122 #include <net/pfil.h> 123 124 #include <netinet/in.h> 125 #include <netinet/in_systm.h> 126 #include <netinet/ip.h> 127 #include <netinet/in_pcb.h> 128 #include <netinet/in_var.h> 129 #include <netinet/ip_var.h> 130 131 #ifdef MROUTING 132 #include <netinet/ip_mroute.h> 133 #endif 134 135 #include <machine/stdarg.h> 136 137 #ifdef IPSEC 138 #include <netinet6/ipsec.h> 139 #include <netkey/key.h> 140 #include <netkey/key_debug.h> 141 #endif /*IPSEC*/ 142 143 #ifdef FAST_IPSEC 144 #include <netipsec/ipsec.h> 145 #include <netipsec/key.h> 146 #include <netipsec/xform.h> 147 #endif /* FAST_IPSEC*/ 148 149 static struct mbuf *ip_insertoptions __P((struct mbuf *, struct mbuf *, int *)); 150 static struct ifnet *ip_multicast_if __P((struct in_addr *, int *)); 151 static void ip_mloopback 152 __P((struct ifnet *, struct mbuf *, struct sockaddr_in *)); 153 154 #ifdef PFIL_HOOKS 155 extern struct pfil_head inet_pfil_hook; /* XXX */ 156 #endif 157 158 /* 159 * IP output. The packet in mbuf chain m contains a skeletal IP 160 * header (with len, off, ttl, proto, tos, src, dst). 161 * The mbuf chain containing the packet will be freed. 162 * The mbuf opt, if present, will not be freed. 163 */ 164 int 165 #if __STDC__ 166 ip_output(struct mbuf *m0, ...) 167 #else 168 ip_output(m0, va_alist) 169 struct mbuf *m0; 170 va_dcl 171 #endif 172 { 173 struct ip *ip; 174 struct ifnet *ifp; 175 struct mbuf *m = m0; 176 int hlen = sizeof (struct ip); 177 int len, error = 0; 178 struct route iproute; 179 struct sockaddr_in *dst; 180 struct in_ifaddr *ia; 181 struct mbuf *opt; 182 struct route *ro; 183 int flags, sw_csum; 184 int *mtu_p; 185 u_long mtu; 186 struct ip_moptions *imo; 187 struct socket *so; 188 va_list ap; 189 #ifdef IPSEC 190 struct secpolicy *sp = NULL; 191 #endif /*IPSEC*/ 192 #ifdef FAST_IPSEC 193 struct inpcb *inp; 194 struct m_tag *mtag; 195 struct secpolicy *sp = NULL; 196 struct tdb_ident *tdbi; 197 int s; 198 #endif 199 u_int16_t ip_len; 200 201 len = 0; 202 va_start(ap, m0); 203 opt = va_arg(ap, struct mbuf *); 204 ro = va_arg(ap, struct route *); 205 flags = va_arg(ap, int); 206 imo = va_arg(ap, struct ip_moptions *); 207 so = va_arg(ap, struct socket *); 208 if (flags & IP_RETURNMTU) 209 mtu_p = va_arg(ap, int *); 210 else 211 mtu_p = NULL; 212 va_end(ap); 213 214 MCLAIM(m, &ip_tx_mowner); 215 #ifdef FAST_IPSEC 216 if (so->so_proto->pr_domain->dom_family == AF_INET) 217 inp = (struct inpcb *)so->so_pcb; 218 else 219 inp = NULL; 220 #endif /*IPSEC*/ 221 222 #ifdef DIAGNOSTIC 223 if ((m->m_flags & M_PKTHDR) == 0) 224 panic("ip_output no HDR"); 225 #endif 226 if (opt) { 227 m = ip_insertoptions(m, opt, &len); 228 if (len >= sizeof(struct ip)) 229 hlen = len; 230 } 231 ip = mtod(m, struct ip *); 232 /* 233 * Fill in IP header. 234 */ 235 if ((flags & (IP_FORWARDING|IP_RAWOUTPUT)) == 0) { 236 ip->ip_v = IPVERSION; 237 ip->ip_off = htons(0); 238 ip->ip_id = htons(ip_id++); 239 ip->ip_hl = hlen >> 2; 240 ipstat.ips_localout++; 241 } else { 242 hlen = ip->ip_hl << 2; 243 } 244 /* 245 * Route packet. 246 */ 247 if (ro == 0) { 248 ro = &iproute; 249 bzero((caddr_t)ro, sizeof (*ro)); 250 } 251 dst = satosin(&ro->ro_dst); 252 /* 253 * If there is a cached route, 254 * check that it is to the same destination 255 * and is still up. If not, free it and try again. 256 * The address family should also be checked in case of sharing the 257 * cache with IPv6. 258 */ 259 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 || 260 dst->sin_family != AF_INET || 261 !in_hosteq(dst->sin_addr, ip->ip_dst))) { 262 RTFREE(ro->ro_rt); 263 ro->ro_rt = (struct rtentry *)0; 264 } 265 if (ro->ro_rt == 0) { 266 bzero(dst, sizeof(*dst)); 267 dst->sin_family = AF_INET; 268 dst->sin_len = sizeof(*dst); 269 dst->sin_addr = ip->ip_dst; 270 } 271 /* 272 * If routing to interface only, 273 * short circuit routing lookup. 274 */ 275 if (flags & IP_ROUTETOIF) { 276 if ((ia = ifatoia(ifa_ifwithladdr(sintosa(dst)))) == 0) { 277 ipstat.ips_noroute++; 278 error = ENETUNREACH; 279 goto bad; 280 } 281 ifp = ia->ia_ifp; 282 mtu = ifp->if_mtu; 283 ip->ip_ttl = 1; 284 } else if ((IN_MULTICAST(ip->ip_dst.s_addr) || 285 ip->ip_dst.s_addr == INADDR_BROADCAST) && 286 imo != NULL && imo->imo_multicast_ifp != NULL) { 287 ifp = imo->imo_multicast_ifp; 288 mtu = ifp->if_mtu; 289 IFP_TO_IA(ifp, ia); 290 } else { 291 if (ro->ro_rt == 0) 292 rtalloc(ro); 293 if (ro->ro_rt == 0) { 294 ipstat.ips_noroute++; 295 error = EHOSTUNREACH; 296 goto bad; 297 } 298 ia = ifatoia(ro->ro_rt->rt_ifa); 299 ifp = ro->ro_rt->rt_ifp; 300 if ((mtu = ro->ro_rt->rt_rmx.rmx_mtu) == 0) 301 mtu = ifp->if_mtu; 302 ro->ro_rt->rt_use++; 303 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 304 dst = satosin(ro->ro_rt->rt_gateway); 305 } 306 if (IN_MULTICAST(ip->ip_dst.s_addr) || 307 (ip->ip_dst.s_addr == INADDR_BROADCAST)) { 308 struct in_multi *inm; 309 310 m->m_flags |= (ip->ip_dst.s_addr == INADDR_BROADCAST) ? 311 M_BCAST : M_MCAST; 312 /* 313 * IP destination address is multicast. Make sure "dst" 314 * still points to the address in "ro". (It may have been 315 * changed to point to a gateway address, above.) 316 */ 317 dst = satosin(&ro->ro_dst); 318 /* 319 * See if the caller provided any multicast options 320 */ 321 if (imo != NULL) 322 ip->ip_ttl = imo->imo_multicast_ttl; 323 else 324 ip->ip_ttl = IP_DEFAULT_MULTICAST_TTL; 325 326 /* 327 * if we don't know the outgoing ifp yet, we can't generate 328 * output 329 */ 330 if (!ifp) { 331 ipstat.ips_noroute++; 332 error = ENETUNREACH; 333 goto bad; 334 } 335 336 /* 337 * If the packet is multicast or broadcast, confirm that 338 * the outgoing interface can transmit it. 339 */ 340 if (((m->m_flags & M_MCAST) && 341 (ifp->if_flags & IFF_MULTICAST) == 0) || 342 ((m->m_flags & M_BCAST) && 343 (ifp->if_flags & (IFF_BROADCAST|IFF_POINTOPOINT)) == 0)) { 344 ipstat.ips_noroute++; 345 error = ENETUNREACH; 346 goto bad; 347 } 348 /* 349 * If source address not specified yet, use an address 350 * of outgoing interface. 351 */ 352 if (in_nullhost(ip->ip_src)) { 353 struct in_ifaddr *ia; 354 355 IFP_TO_IA(ifp, ia); 356 if (!ia) { 357 error = EADDRNOTAVAIL; 358 goto bad; 359 } 360 ip->ip_src = ia->ia_addr.sin_addr; 361 } 362 363 IN_LOOKUP_MULTI(ip->ip_dst, ifp, inm); 364 if (inm != NULL && 365 (imo == NULL || imo->imo_multicast_loop)) { 366 /* 367 * If we belong to the destination multicast group 368 * on the outgoing interface, and the caller did not 369 * forbid loopback, loop back a copy. 370 */ 371 ip_mloopback(ifp, m, dst); 372 } 373 #ifdef MROUTING 374 else { 375 /* 376 * If we are acting as a multicast router, perform 377 * multicast forwarding as if the packet had just 378 * arrived on the interface to which we are about 379 * to send. The multicast forwarding function 380 * recursively calls this function, using the 381 * IP_FORWARDING flag to prevent infinite recursion. 382 * 383 * Multicasts that are looped back by ip_mloopback(), 384 * above, will be forwarded by the ip_input() routine, 385 * if necessary. 386 */ 387 extern struct socket *ip_mrouter; 388 389 if (ip_mrouter && (flags & IP_FORWARDING) == 0) { 390 if (ip_mforward(m, ifp) != 0) { 391 m_freem(m); 392 goto done; 393 } 394 } 395 } 396 #endif 397 /* 398 * Multicasts with a time-to-live of zero may be looped- 399 * back, above, but must not be transmitted on a network. 400 * Also, multicasts addressed to the loopback interface 401 * are not sent -- the above call to ip_mloopback() will 402 * loop back a copy if this host actually belongs to the 403 * destination group on the loopback interface. 404 */ 405 if (ip->ip_ttl == 0 || (ifp->if_flags & IFF_LOOPBACK) != 0) { 406 m_freem(m); 407 goto done; 408 } 409 410 goto sendit; 411 } 412 #ifndef notdef 413 /* 414 * If source address not specified yet, use address 415 * of outgoing interface. 416 */ 417 if (in_nullhost(ip->ip_src)) 418 ip->ip_src = ia->ia_addr.sin_addr; 419 #endif 420 421 /* 422 * packets with Class-D address as source are not valid per 423 * RFC 1112 424 */ 425 if (IN_MULTICAST(ip->ip_src.s_addr)) { 426 ipstat.ips_odropped++; 427 error = EADDRNOTAVAIL; 428 goto bad; 429 } 430 431 /* 432 * Look for broadcast address and 433 * and verify user is allowed to send 434 * such a packet. 435 */ 436 if (in_broadcast(dst->sin_addr, ifp)) { 437 if ((ifp->if_flags & IFF_BROADCAST) == 0) { 438 error = EADDRNOTAVAIL; 439 goto bad; 440 } 441 if ((flags & IP_ALLOWBROADCAST) == 0) { 442 error = EACCES; 443 goto bad; 444 } 445 /* don't allow broadcast messages to be fragmented */ 446 if (ntohs(ip->ip_len) > ifp->if_mtu) { 447 error = EMSGSIZE; 448 goto bad; 449 } 450 m->m_flags |= M_BCAST; 451 } else 452 m->m_flags &= ~M_BCAST; 453 454 sendit: 455 /* 456 * If we're doing Path MTU Discovery, we need to set DF unless 457 * the route's MTU is locked. 458 */ 459 if ((flags & IP_MTUDISC) != 0 && ro->ro_rt != NULL && 460 (ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) == 0) 461 ip->ip_off |= htons(IP_DF); 462 463 /* Remember the current ip_len */ 464 ip_len = ntohs(ip->ip_len); 465 466 #ifdef IPSEC 467 /* get SP for this packet */ 468 if (so == NULL) 469 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND, 470 flags, &error); 471 else 472 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND, so, &error); 473 474 if (sp == NULL) { 475 ipsecstat.out_inval++; 476 goto bad; 477 } 478 479 error = 0; 480 481 /* check policy */ 482 switch (sp->policy) { 483 case IPSEC_POLICY_DISCARD: 484 /* 485 * This packet is just discarded. 486 */ 487 ipsecstat.out_polvio++; 488 goto bad; 489 490 case IPSEC_POLICY_BYPASS: 491 case IPSEC_POLICY_NONE: 492 /* no need to do IPsec. */ 493 goto skip_ipsec; 494 495 case IPSEC_POLICY_IPSEC: 496 if (sp->req == NULL) { 497 /* XXX should be panic ? */ 498 printf("ip_output: No IPsec request specified.\n"); 499 error = EINVAL; 500 goto bad; 501 } 502 break; 503 504 case IPSEC_POLICY_ENTRUST: 505 default: 506 printf("ip_output: Invalid policy found. %d\n", sp->policy); 507 } 508 509 /* 510 * ipsec4_output() expects ip_len and ip_off in network 511 * order. They have been set to network order above. 512 */ 513 514 { 515 struct ipsec_output_state state; 516 bzero(&state, sizeof(state)); 517 state.m = m; 518 if (flags & IP_ROUTETOIF) { 519 state.ro = &iproute; 520 bzero(&iproute, sizeof(iproute)); 521 } else 522 state.ro = ro; 523 state.dst = (struct sockaddr *)dst; 524 525 /* 526 * We can't defer the checksum of payload data if 527 * we're about to encrypt/authenticate it. 528 * 529 * XXX When we support crypto offloading functions of 530 * XXX network interfaces, we need to reconsider this, 531 * XXX since it's likely that they'll support checksumming, 532 * XXX as well. 533 */ 534 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 535 in_delayed_cksum(m); 536 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 537 } 538 539 error = ipsec4_output(&state, sp, flags); 540 541 m = state.m; 542 if (flags & IP_ROUTETOIF) { 543 /* 544 * if we have tunnel mode SA, we may need to ignore 545 * IP_ROUTETOIF. 546 */ 547 if (state.ro != &iproute || state.ro->ro_rt != NULL) { 548 flags &= ~IP_ROUTETOIF; 549 ro = state.ro; 550 } 551 } else 552 ro = state.ro; 553 dst = (struct sockaddr_in *)state.dst; 554 if (error) { 555 /* mbuf is already reclaimed in ipsec4_output. */ 556 m0 = NULL; 557 switch (error) { 558 case EHOSTUNREACH: 559 case ENETUNREACH: 560 case EMSGSIZE: 561 case ENOBUFS: 562 case ENOMEM: 563 break; 564 default: 565 printf("ip4_output (ipsec): error code %d\n", error); 566 /*fall through*/ 567 case ENOENT: 568 /* don't show these error codes to the user */ 569 error = 0; 570 break; 571 } 572 goto bad; 573 } 574 575 /* be sure to update variables that are affected by ipsec4_output() */ 576 ip = mtod(m, struct ip *); 577 hlen = ip->ip_hl << 2; 578 ip_len = ntohs(ip->ip_len); 579 580 if (ro->ro_rt == NULL) { 581 if ((flags & IP_ROUTETOIF) == 0) { 582 printf("ip_output: " 583 "can't update route after IPsec processing\n"); 584 error = EHOSTUNREACH; /*XXX*/ 585 goto bad; 586 } 587 } else { 588 /* nobody uses ia beyond here */ 589 if (state.encap) 590 ifp = ro->ro_rt->rt_ifp; 591 } 592 } 593 skip_ipsec: 594 #endif /*IPSEC*/ 595 #ifdef FAST_IPSEC 596 /* 597 * Check the security policy (SP) for the packet and, if 598 * required, do IPsec-related processing. There are two 599 * cases here; the first time a packet is sent through 600 * it will be untagged and handled by ipsec4_checkpolicy. 601 * If the packet is resubmitted to ip_output (e.g. after 602 * AH, ESP, etc. processing), there will be a tag to bypass 603 * the lookup and related policy checking. 604 */ 605 mtag = m_tag_find(m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 606 s = splsoftnet(); 607 if (mtag != NULL) { 608 tdbi = (struct tdb_ident *)(mtag + 1); 609 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 610 if (sp == NULL) 611 error = -EINVAL; /* force silent drop */ 612 m_tag_delete(m, mtag); 613 } else { 614 sp = ipsec4_checkpolicy(m, IPSEC_DIR_OUTBOUND, flags, 615 &error, inp); 616 } 617 /* 618 * There are four return cases: 619 * sp != NULL apply IPsec policy 620 * sp == NULL, error == 0 no IPsec handling needed 621 * sp == NULL, error == -EINVAL discard packet w/o error 622 * sp == NULL, error != 0 discard packet, report error 623 */ 624 if (sp != NULL) { 625 /* Loop detection, check if ipsec processing already done */ 626 IPSEC_ASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 627 for (mtag = m_tag_first(m); mtag != NULL; 628 mtag = m_tag_next(m, mtag)) { 629 #ifdef MTAG_ABI_COMPAT 630 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 631 continue; 632 #endif 633 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 634 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 635 continue; 636 /* 637 * Check if policy has an SA associated with it. 638 * This can happen when an SP has yet to acquire 639 * an SA; e.g. on first reference. If it occurs, 640 * then we let ipsec4_process_packet do its thing. 641 */ 642 if (sp->req->sav == NULL) 643 break; 644 tdbi = (struct tdb_ident *)(mtag + 1); 645 if (tdbi->spi == sp->req->sav->spi && 646 tdbi->proto == sp->req->sav->sah->saidx.proto && 647 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 648 sizeof (union sockaddr_union)) == 0) { 649 /* 650 * No IPsec processing is needed, free 651 * reference to SP. 652 * 653 * NB: null pointer to avoid free at 654 * done: below. 655 */ 656 KEY_FREESP(&sp), sp = NULL; 657 splx(s); 658 goto spd_done; 659 } 660 } 661 662 /* 663 * Do delayed checksums now because we send before 664 * this is done in the normal processing path. 665 */ 666 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 667 in_delayed_cksum(m); 668 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 669 } 670 671 #ifdef __FreeBSD__ 672 ip->ip_len = htons(ip->ip_len); 673 ip->ip_off = htons(ip->ip_off); 674 #endif 675 676 /* NB: callee frees mbuf */ 677 error = ipsec4_process_packet(m, sp->req, flags, 0); 678 /* 679 * Preserve KAME behaviour: ENOENT can be returned 680 * when an SA acquire is in progress. Don't propagate 681 * this to user-level; it confuses applications. 682 * 683 * XXX this will go away when the SADB is redone. 684 */ 685 if (error == ENOENT) 686 error = 0; 687 splx(s); 688 goto done; 689 } else { 690 splx(s); 691 692 if (error != 0) { 693 /* 694 * Hack: -EINVAL is used to signal that a packet 695 * should be silently discarded. This is typically 696 * because we asked key management for an SA and 697 * it was delayed (e.g. kicked up to IKE). 698 */ 699 if (error == -EINVAL) 700 error = 0; 701 goto bad; 702 } else { 703 /* No IPsec processing for this packet. */ 704 } 705 #ifdef notyet 706 /* 707 * If deferred crypto processing is needed, check that 708 * the interface supports it. 709 */ 710 mtag = m_tag_find(m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 711 if (mtag != NULL && (ifp->if_capenable & IFCAP_IPSEC) == 0) { 712 /* notify IPsec to do its own crypto */ 713 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 714 error = EHOSTUNREACH; 715 goto bad; 716 } 717 #endif 718 } 719 spd_done: 720 #endif /* FAST_IPSEC */ 721 722 #ifdef PFIL_HOOKS 723 /* 724 * Run through list of hooks for output packets. 725 */ 726 if ((error = pfil_run_hooks(&inet_pfil_hook, &m, ifp, PFIL_OUT)) != 0) 727 goto done; 728 if (m == NULL) 729 goto done; 730 731 ip = mtod(m, struct ip *); 732 hlen = ip->ip_hl << 2; 733 #endif /* PFIL_HOOKS */ 734 735 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 736 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 737 /* 738 * If small enough for mtu of path, can just send directly. 739 */ 740 if (ip_len <= mtu) { 741 #if IFA_STATS 742 /* 743 * search for the source address structure to 744 * maintain output statistics. 745 */ 746 INADDR_TO_IA(ip->ip_src, ia); 747 if (ia) 748 ia->ia_ifa.ifa_data.ifad_outbytes += ip_len; 749 #endif 750 /* 751 * Always initialize the sum to 0! Some HW assisted 752 * checksumming requires this. 753 */ 754 ip->ip_sum = 0; 755 756 /* 757 * Perform any checksums that the hardware can't do 758 * for us. 759 * 760 * XXX Does any hardware require the {th,uh}_sum 761 * XXX fields to be 0? 762 */ 763 if (sw_csum & M_CSUM_IPv4) { 764 ip->ip_sum = in_cksum(m, hlen); 765 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 766 } 767 if (sw_csum & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 768 in_delayed_cksum(m); 769 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 770 } 771 772 #ifdef IPSEC 773 /* clean ipsec history once it goes out of the node */ 774 ipsec_delaux(m); 775 #endif 776 error = (*ifp->if_output)(ifp, m, sintosa(dst), ro->ro_rt); 777 goto done; 778 } 779 780 /* 781 * We can't use HW checksumming if we're about to 782 * to fragment the packet. 783 * 784 * XXX Some hardware can do this. 785 */ 786 if (m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 787 in_delayed_cksum(m); 788 m->m_pkthdr.csum_flags &= ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 789 } 790 791 /* 792 * Too large for interface; fragment if possible. 793 * Must be able to put at least 8 bytes per fragment. 794 */ 795 if (ntohs(ip->ip_off) & IP_DF) { 796 if (flags & IP_RETURNMTU) 797 *mtu_p = mtu; 798 error = EMSGSIZE; 799 ipstat.ips_cantfrag++; 800 goto bad; 801 } 802 803 error = ip_fragment(m, ifp, mtu); 804 if (error == EMSGSIZE) 805 goto bad; 806 807 for (; m; m = m0) { 808 m0 = m->m_nextpkt; 809 m->m_nextpkt = 0; 810 if (error == 0) { 811 #if IFA_STATS 812 /* 813 * search for the source address structure to 814 * maintain output statistics. 815 */ 816 INADDR_TO_IA(ip->ip_src, ia); 817 if (ia) { 818 ia->ia_ifa.ifa_data.ifad_outbytes += 819 ntohs(ip->ip_len); 820 } 821 #endif 822 #ifdef IPSEC 823 /* clean ipsec history once it goes out of the node */ 824 ipsec_delaux(m); 825 #endif 826 KASSERT((m->m_pkthdr.csum_flags & 827 (M_CSUM_UDPv4 | M_CSUM_TCPv4)) == 0); 828 error = (*ifp->if_output)(ifp, m, sintosa(dst), 829 ro->ro_rt); 830 } else 831 m_freem(m); 832 } 833 834 if (error == 0) 835 ipstat.ips_fragmented++; 836 done: 837 if (ro == &iproute && (flags & IP_ROUTETOIF) == 0 && ro->ro_rt) { 838 RTFREE(ro->ro_rt); 839 ro->ro_rt = 0; 840 } 841 842 #ifdef IPSEC 843 if (sp != NULL) { 844 KEYDEBUG(KEYDEBUG_IPSEC_STAMP, 845 printf("DP ip_output call free SP:%p\n", sp)); 846 key_freesp(sp); 847 } 848 #endif /* IPSEC */ 849 #ifdef FAST_IPSEC 850 if (sp != NULL) 851 KEY_FREESP(&sp); 852 #endif /* FAST_IPSEC */ 853 854 return (error); 855 bad: 856 m_freem(m); 857 goto done; 858 } 859 860 int 861 ip_fragment(struct mbuf *m, struct ifnet *ifp, u_long mtu) 862 { 863 struct ip *ip, *mhip; 864 struct mbuf *m0; 865 int len, hlen, off; 866 int mhlen, firstlen; 867 struct mbuf **mnext; 868 int sw_csum; 869 int fragments = 0; 870 int s; 871 int error = 0; 872 873 ip = mtod(m, struct ip *); 874 hlen = ip->ip_hl << 2; 875 sw_csum = m->m_pkthdr.csum_flags & ~ifp->if_csum_flags_tx; 876 877 len = (mtu - hlen) &~ 7; 878 if (len < 8) 879 return (EMSGSIZE); 880 881 firstlen = len; 882 mnext = &m->m_nextpkt; 883 884 /* 885 * Loop through length of segment after first fragment, 886 * make new header and copy data of each part and link onto chain. 887 */ 888 m0 = m; 889 mhlen = sizeof (struct ip); 890 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 891 MGETHDR(m, M_DONTWAIT, MT_HEADER); 892 if (m == 0) { 893 error = ENOBUFS; 894 ipstat.ips_odropped++; 895 goto sendorfree; 896 } 897 MCLAIM(m, m0->m_owner); 898 *mnext = m; 899 mnext = &m->m_nextpkt; 900 m->m_data += max_linkhdr; 901 mhip = mtod(m, struct ip *); 902 *mhip = *ip; 903 /* we must inherit MCAST and BCAST flags */ 904 m->m_flags |= m0->m_flags & (M_MCAST|M_BCAST); 905 if (hlen > sizeof (struct ip)) { 906 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 907 mhip->ip_hl = mhlen >> 2; 908 } 909 m->m_len = mhlen; 910 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF); 911 if (ip->ip_off & IP_MF) 912 mhip->ip_off |= IP_MF; 913 if (off + len >= ntohs(ip->ip_len)) 914 len = ntohs(ip->ip_len) - off; 915 else 916 mhip->ip_off |= IP_MF; 917 HTONS(mhip->ip_off); 918 mhip->ip_len = htons((u_int16_t)(len + mhlen)); 919 m->m_next = m_copy(m0, off, len); 920 if (m->m_next == 0) { 921 error = ENOBUFS; /* ??? */ 922 ipstat.ips_odropped++; 923 goto sendorfree; 924 } 925 m->m_pkthdr.len = mhlen + len; 926 m->m_pkthdr.rcvif = (struct ifnet *)0; 927 mhip->ip_sum = 0; 928 if (sw_csum & M_CSUM_IPv4) { 929 mhip->ip_sum = in_cksum(m, mhlen); 930 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_IPv4) == 0); 931 } else { 932 m->m_pkthdr.csum_flags |= M_CSUM_IPv4; 933 } 934 ipstat.ips_ofragments++; 935 fragments++; 936 } 937 /* 938 * Update first fragment by trimming what's been copied out 939 * and updating header, then send each fragment (in order). 940 */ 941 m = m0; 942 m_adj(m, hlen + firstlen - ntohs(ip->ip_len)); 943 m->m_pkthdr.len = hlen + firstlen; 944 ip->ip_len = htons((u_int16_t)m->m_pkthdr.len); 945 ip->ip_off |= htons(IP_MF); 946 ip->ip_sum = 0; 947 if (sw_csum & M_CSUM_IPv4) { 948 ip->ip_sum = in_cksum(m, hlen); 949 m->m_pkthdr.csum_flags &= ~M_CSUM_IPv4; 950 } else { 951 KASSERT(m->m_pkthdr.csum_flags & M_CSUM_IPv4); 952 } 953 sendorfree: 954 /* 955 * If there is no room for all the fragments, don't queue 956 * any of them. 957 */ 958 s = splnet(); 959 if (ifp->if_snd.ifq_maxlen - ifp->if_snd.ifq_len < fragments) 960 error = ENOBUFS; 961 splx(s); 962 return (error); 963 } 964 965 /* 966 * Process a delayed payload checksum calculation. 967 */ 968 void 969 in_delayed_cksum(struct mbuf *m) 970 { 971 struct ip *ip; 972 u_int16_t csum, offset; 973 974 ip = mtod(m, struct ip *); 975 offset = ip->ip_hl << 2; 976 csum = in4_cksum(m, 0, offset, ntohs(ip->ip_len) - offset); 977 if (csum == 0 && (m->m_pkthdr.csum_flags & M_CSUM_UDPv4) != 0) 978 csum = 0xffff; 979 980 offset += m->m_pkthdr.csum_data; /* checksum offset */ 981 982 if ((offset + sizeof(u_int16_t)) > m->m_len) { 983 /* This happen when ip options were inserted 984 printf("in_delayed_cksum: pullup len %d off %d proto %d\n", 985 m->m_len, offset, ip->ip_p); 986 */ 987 m_copyback(m, offset, sizeof(csum), (caddr_t) &csum); 988 } else 989 *(u_int16_t *)(mtod(m, caddr_t) + offset) = csum; 990 } 991 992 /* 993 * Determine the maximum length of the options to be inserted; 994 * we would far rather allocate too much space rather than too little. 995 */ 996 997 u_int 998 ip_optlen(inp) 999 struct inpcb *inp; 1000 { 1001 struct mbuf *m = inp->inp_options; 1002 1003 if (m && m->m_len > offsetof(struct ipoption, ipopt_dst)) 1004 return (m->m_len - offsetof(struct ipoption, ipopt_dst)); 1005 else 1006 return 0; 1007 } 1008 1009 1010 /* 1011 * Insert IP options into preformed packet. 1012 * Adjust IP destination as required for IP source routing, 1013 * as indicated by a non-zero in_addr at the start of the options. 1014 */ 1015 static struct mbuf * 1016 ip_insertoptions(m, opt, phlen) 1017 struct mbuf *m; 1018 struct mbuf *opt; 1019 int *phlen; 1020 { 1021 struct ipoption *p = mtod(opt, struct ipoption *); 1022 struct mbuf *n; 1023 struct ip *ip = mtod(m, struct ip *); 1024 unsigned optlen; 1025 1026 optlen = opt->m_len - sizeof(p->ipopt_dst); 1027 if (optlen + ntohs(ip->ip_len) > IP_MAXPACKET) 1028 return (m); /* XXX should fail */ 1029 if (!in_nullhost(p->ipopt_dst)) 1030 ip->ip_dst = p->ipopt_dst; 1031 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) { 1032 MGETHDR(n, M_DONTWAIT, MT_HEADER); 1033 if (n == 0) 1034 return (m); 1035 MCLAIM(n, m->m_owner); 1036 M_COPY_PKTHDR(n, m); 1037 m->m_flags &= ~M_PKTHDR; 1038 m->m_len -= sizeof(struct ip); 1039 m->m_data += sizeof(struct ip); 1040 n->m_next = m; 1041 m = n; 1042 m->m_len = optlen + sizeof(struct ip); 1043 m->m_data += max_linkhdr; 1044 bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip)); 1045 } else { 1046 m->m_data -= optlen; 1047 m->m_len += optlen; 1048 memmove(mtod(m, caddr_t), ip, sizeof(struct ip)); 1049 } 1050 m->m_pkthdr.len += optlen; 1051 ip = mtod(m, struct ip *); 1052 bcopy((caddr_t)p->ipopt_list, (caddr_t)(ip + 1), (unsigned)optlen); 1053 *phlen = sizeof(struct ip) + optlen; 1054 ip->ip_len = htons(ntohs(ip->ip_len) + optlen); 1055 return (m); 1056 } 1057 1058 /* 1059 * Copy options from ip to jp, 1060 * omitting those not copied during fragmentation. 1061 */ 1062 int 1063 ip_optcopy(ip, jp) 1064 struct ip *ip, *jp; 1065 { 1066 u_char *cp, *dp; 1067 int opt, optlen, cnt; 1068 1069 cp = (u_char *)(ip + 1); 1070 dp = (u_char *)(jp + 1); 1071 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 1072 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1073 opt = cp[0]; 1074 if (opt == IPOPT_EOL) 1075 break; 1076 if (opt == IPOPT_NOP) { 1077 /* Preserve for IP mcast tunnel's LSRR alignment. */ 1078 *dp++ = IPOPT_NOP; 1079 optlen = 1; 1080 continue; 1081 } 1082 #ifdef DIAGNOSTIC 1083 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1084 panic("malformed IPv4 option passed to ip_optcopy"); 1085 #endif 1086 optlen = cp[IPOPT_OLEN]; 1087 #ifdef DIAGNOSTIC 1088 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1089 panic("malformed IPv4 option passed to ip_optcopy"); 1090 #endif 1091 /* bogus lengths should have been caught by ip_dooptions */ 1092 if (optlen > cnt) 1093 optlen = cnt; 1094 if (IPOPT_COPIED(opt)) { 1095 bcopy((caddr_t)cp, (caddr_t)dp, (unsigned)optlen); 1096 dp += optlen; 1097 } 1098 } 1099 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++) 1100 *dp++ = IPOPT_EOL; 1101 return (optlen); 1102 } 1103 1104 /* 1105 * IP socket option processing. 1106 */ 1107 int 1108 ip_ctloutput(op, so, level, optname, mp) 1109 int op; 1110 struct socket *so; 1111 int level, optname; 1112 struct mbuf **mp; 1113 { 1114 struct inpcb *inp = sotoinpcb(so); 1115 struct mbuf *m = *mp; 1116 int optval = 0; 1117 int error = 0; 1118 #if defined(IPSEC) || defined(FAST_IPSEC) 1119 struct proc *p = curproc; /*XXX*/ 1120 #endif 1121 1122 if (level != IPPROTO_IP) { 1123 error = EINVAL; 1124 if (op == PRCO_SETOPT && *mp) 1125 (void) m_free(*mp); 1126 } else switch (op) { 1127 1128 case PRCO_SETOPT: 1129 switch (optname) { 1130 case IP_OPTIONS: 1131 #ifdef notyet 1132 case IP_RETOPTS: 1133 return (ip_pcbopts(optname, &inp->inp_options, m)); 1134 #else 1135 return (ip_pcbopts(&inp->inp_options, m)); 1136 #endif 1137 1138 case IP_TOS: 1139 case IP_TTL: 1140 case IP_RECVOPTS: 1141 case IP_RECVRETOPTS: 1142 case IP_RECVDSTADDR: 1143 case IP_RECVIF: 1144 if (m == NULL || m->m_len != sizeof(int)) 1145 error = EINVAL; 1146 else { 1147 optval = *mtod(m, int *); 1148 switch (optname) { 1149 1150 case IP_TOS: 1151 inp->inp_ip.ip_tos = optval; 1152 break; 1153 1154 case IP_TTL: 1155 inp->inp_ip.ip_ttl = optval; 1156 break; 1157 #define OPTSET(bit) \ 1158 if (optval) \ 1159 inp->inp_flags |= bit; \ 1160 else \ 1161 inp->inp_flags &= ~bit; 1162 1163 case IP_RECVOPTS: 1164 OPTSET(INP_RECVOPTS); 1165 break; 1166 1167 case IP_RECVRETOPTS: 1168 OPTSET(INP_RECVRETOPTS); 1169 break; 1170 1171 case IP_RECVDSTADDR: 1172 OPTSET(INP_RECVDSTADDR); 1173 break; 1174 1175 case IP_RECVIF: 1176 OPTSET(INP_RECVIF); 1177 break; 1178 } 1179 } 1180 break; 1181 #undef OPTSET 1182 1183 case IP_MULTICAST_IF: 1184 case IP_MULTICAST_TTL: 1185 case IP_MULTICAST_LOOP: 1186 case IP_ADD_MEMBERSHIP: 1187 case IP_DROP_MEMBERSHIP: 1188 error = ip_setmoptions(optname, &inp->inp_moptions, m); 1189 break; 1190 1191 case IP_PORTRANGE: 1192 if (m == 0 || m->m_len != sizeof(int)) 1193 error = EINVAL; 1194 else { 1195 optval = *mtod(m, int *); 1196 1197 switch (optval) { 1198 1199 case IP_PORTRANGE_DEFAULT: 1200 case IP_PORTRANGE_HIGH: 1201 inp->inp_flags &= ~(INP_LOWPORT); 1202 break; 1203 1204 case IP_PORTRANGE_LOW: 1205 inp->inp_flags |= INP_LOWPORT; 1206 break; 1207 1208 default: 1209 error = EINVAL; 1210 break; 1211 } 1212 } 1213 break; 1214 1215 #if defined(IPSEC) || defined(FAST_IPSEC) 1216 case IP_IPSEC_POLICY: 1217 { 1218 caddr_t req = NULL; 1219 size_t len = 0; 1220 int priv = 0; 1221 1222 #ifdef __NetBSD__ 1223 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) 1224 priv = 0; 1225 else 1226 priv = 1; 1227 #else 1228 priv = (in6p->in6p_socket->so_state & SS_PRIV); 1229 #endif 1230 if (m) { 1231 req = mtod(m, caddr_t); 1232 len = m->m_len; 1233 } 1234 error = ipsec4_set_policy(inp, optname, req, len, priv); 1235 break; 1236 } 1237 #endif /*IPSEC*/ 1238 1239 default: 1240 error = ENOPROTOOPT; 1241 break; 1242 } 1243 if (m) 1244 (void)m_free(m); 1245 break; 1246 1247 case PRCO_GETOPT: 1248 switch (optname) { 1249 case IP_OPTIONS: 1250 case IP_RETOPTS: 1251 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1252 MCLAIM(m, so->so_mowner); 1253 if (inp->inp_options) { 1254 m->m_len = inp->inp_options->m_len; 1255 bcopy(mtod(inp->inp_options, caddr_t), 1256 mtod(m, caddr_t), (unsigned)m->m_len); 1257 } else 1258 m->m_len = 0; 1259 break; 1260 1261 case IP_TOS: 1262 case IP_TTL: 1263 case IP_RECVOPTS: 1264 case IP_RECVRETOPTS: 1265 case IP_RECVDSTADDR: 1266 case IP_RECVIF: 1267 case IP_ERRORMTU: 1268 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1269 MCLAIM(m, so->so_mowner); 1270 m->m_len = sizeof(int); 1271 switch (optname) { 1272 1273 case IP_TOS: 1274 optval = inp->inp_ip.ip_tos; 1275 break; 1276 1277 case IP_TTL: 1278 optval = inp->inp_ip.ip_ttl; 1279 break; 1280 1281 case IP_ERRORMTU: 1282 optval = inp->inp_errormtu; 1283 break; 1284 1285 #define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0) 1286 1287 case IP_RECVOPTS: 1288 optval = OPTBIT(INP_RECVOPTS); 1289 break; 1290 1291 case IP_RECVRETOPTS: 1292 optval = OPTBIT(INP_RECVRETOPTS); 1293 break; 1294 1295 case IP_RECVDSTADDR: 1296 optval = OPTBIT(INP_RECVDSTADDR); 1297 break; 1298 1299 case IP_RECVIF: 1300 optval = OPTBIT(INP_RECVIF); 1301 break; 1302 } 1303 *mtod(m, int *) = optval; 1304 break; 1305 1306 #if defined(IPSEC) || defined(FAST_IPSEC) 1307 case IP_IPSEC_POLICY: 1308 { 1309 caddr_t req = NULL; 1310 size_t len = 0; 1311 1312 if (m) { 1313 req = mtod(m, caddr_t); 1314 len = m->m_len; 1315 } 1316 error = ipsec4_get_policy(inp, req, len, mp); 1317 break; 1318 } 1319 #endif /*IPSEC*/ 1320 1321 case IP_MULTICAST_IF: 1322 case IP_MULTICAST_TTL: 1323 case IP_MULTICAST_LOOP: 1324 case IP_ADD_MEMBERSHIP: 1325 case IP_DROP_MEMBERSHIP: 1326 error = ip_getmoptions(optname, inp->inp_moptions, mp); 1327 if (*mp) 1328 MCLAIM(*mp, so->so_mowner); 1329 break; 1330 1331 case IP_PORTRANGE: 1332 *mp = m = m_get(M_WAIT, MT_SOOPTS); 1333 MCLAIM(m, so->so_mowner); 1334 m->m_len = sizeof(int); 1335 1336 if (inp->inp_flags & INP_LOWPORT) 1337 optval = IP_PORTRANGE_LOW; 1338 else 1339 optval = IP_PORTRANGE_DEFAULT; 1340 1341 *mtod(m, int *) = optval; 1342 break; 1343 1344 default: 1345 error = ENOPROTOOPT; 1346 break; 1347 } 1348 break; 1349 } 1350 return (error); 1351 } 1352 1353 /* 1354 * Set up IP options in pcb for insertion in output packets. 1355 * Store in mbuf with pointer in pcbopt, adding pseudo-option 1356 * with destination address if source routed. 1357 */ 1358 int 1359 #ifdef notyet 1360 ip_pcbopts(optname, pcbopt, m) 1361 int optname; 1362 #else 1363 ip_pcbopts(pcbopt, m) 1364 #endif 1365 struct mbuf **pcbopt; 1366 struct mbuf *m; 1367 { 1368 int cnt, optlen; 1369 u_char *cp; 1370 u_char opt; 1371 1372 /* turn off any old options */ 1373 if (*pcbopt) 1374 (void)m_free(*pcbopt); 1375 *pcbopt = 0; 1376 if (m == (struct mbuf *)0 || m->m_len == 0) { 1377 /* 1378 * Only turning off any previous options. 1379 */ 1380 if (m) 1381 (void)m_free(m); 1382 return (0); 1383 } 1384 1385 #ifndef __vax__ 1386 if (m->m_len % sizeof(int32_t)) 1387 goto bad; 1388 #endif 1389 /* 1390 * IP first-hop destination address will be stored before 1391 * actual options; move other options back 1392 * and clear it when none present. 1393 */ 1394 if (m->m_data + m->m_len + sizeof(struct in_addr) >= &m->m_dat[MLEN]) 1395 goto bad; 1396 cnt = m->m_len; 1397 m->m_len += sizeof(struct in_addr); 1398 cp = mtod(m, u_char *) + sizeof(struct in_addr); 1399 memmove(cp, mtod(m, caddr_t), (unsigned)cnt); 1400 bzero(mtod(m, caddr_t), sizeof(struct in_addr)); 1401 1402 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1403 opt = cp[IPOPT_OPTVAL]; 1404 if (opt == IPOPT_EOL) 1405 break; 1406 if (opt == IPOPT_NOP) 1407 optlen = 1; 1408 else { 1409 if (cnt < IPOPT_OLEN + sizeof(*cp)) 1410 goto bad; 1411 optlen = cp[IPOPT_OLEN]; 1412 if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) 1413 goto bad; 1414 } 1415 switch (opt) { 1416 1417 default: 1418 break; 1419 1420 case IPOPT_LSRR: 1421 case IPOPT_SSRR: 1422 /* 1423 * user process specifies route as: 1424 * ->A->B->C->D 1425 * D must be our final destination (but we can't 1426 * check that since we may not have connected yet). 1427 * A is first hop destination, which doesn't appear in 1428 * actual IP option, but is stored before the options. 1429 */ 1430 if (optlen < IPOPT_MINOFF - 1 + sizeof(struct in_addr)) 1431 goto bad; 1432 m->m_len -= sizeof(struct in_addr); 1433 cnt -= sizeof(struct in_addr); 1434 optlen -= sizeof(struct in_addr); 1435 cp[IPOPT_OLEN] = optlen; 1436 /* 1437 * Move first hop before start of options. 1438 */ 1439 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t), 1440 sizeof(struct in_addr)); 1441 /* 1442 * Then copy rest of options back 1443 * to close up the deleted entry. 1444 */ 1445 memmove(&cp[IPOPT_OFFSET+1], 1446 (caddr_t)(&cp[IPOPT_OFFSET+1] + sizeof(struct in_addr)), 1447 (unsigned)cnt + sizeof(struct in_addr)); 1448 break; 1449 } 1450 } 1451 if (m->m_len > MAX_IPOPTLEN + sizeof(struct in_addr)) 1452 goto bad; 1453 *pcbopt = m; 1454 return (0); 1455 1456 bad: 1457 (void)m_free(m); 1458 return (EINVAL); 1459 } 1460 1461 /* 1462 * following RFC1724 section 3.3, 0.0.0.0/8 is interpreted as interface index. 1463 */ 1464 static struct ifnet * 1465 ip_multicast_if(a, ifindexp) 1466 struct in_addr *a; 1467 int *ifindexp; 1468 { 1469 int ifindex; 1470 struct ifnet *ifp = NULL; 1471 struct in_ifaddr *ia; 1472 1473 if (ifindexp) 1474 *ifindexp = 0; 1475 if (ntohl(a->s_addr) >> 24 == 0) { 1476 ifindex = ntohl(a->s_addr) & 0xffffff; 1477 if (ifindex < 0 || if_index < ifindex) 1478 return NULL; 1479 ifp = ifindex2ifnet[ifindex]; 1480 if (ifindexp) 1481 *ifindexp = ifindex; 1482 } else { 1483 LIST_FOREACH(ia, &IN_IFADDR_HASH(a->s_addr), ia_hash) { 1484 if (in_hosteq(ia->ia_addr.sin_addr, *a) && 1485 (ia->ia_ifp->if_flags & IFF_MULTICAST) != 0) { 1486 ifp = ia->ia_ifp; 1487 break; 1488 } 1489 } 1490 } 1491 return ifp; 1492 } 1493 1494 /* 1495 * Set the IP multicast options in response to user setsockopt(). 1496 */ 1497 int 1498 ip_setmoptions(optname, imop, m) 1499 int optname; 1500 struct ip_moptions **imop; 1501 struct mbuf *m; 1502 { 1503 int error = 0; 1504 u_char loop; 1505 int i; 1506 struct in_addr addr; 1507 struct ip_mreq *mreq; 1508 struct ifnet *ifp; 1509 struct ip_moptions *imo = *imop; 1510 struct route ro; 1511 struct sockaddr_in *dst; 1512 int ifindex; 1513 1514 if (imo == NULL) { 1515 /* 1516 * No multicast option buffer attached to the pcb; 1517 * allocate one and initialize to default values. 1518 */ 1519 imo = (struct ip_moptions *)malloc(sizeof(*imo), M_IPMOPTS, 1520 M_WAITOK); 1521 1522 if (imo == NULL) 1523 return (ENOBUFS); 1524 *imop = imo; 1525 imo->imo_multicast_ifp = NULL; 1526 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1527 imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL; 1528 imo->imo_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 1529 imo->imo_num_memberships = 0; 1530 } 1531 1532 switch (optname) { 1533 1534 case IP_MULTICAST_IF: 1535 /* 1536 * Select the interface for outgoing multicast packets. 1537 */ 1538 if (m == NULL || m->m_len != sizeof(struct in_addr)) { 1539 error = EINVAL; 1540 break; 1541 } 1542 addr = *(mtod(m, struct in_addr *)); 1543 /* 1544 * INADDR_ANY is used to remove a previous selection. 1545 * When no interface is selected, a default one is 1546 * chosen every time a multicast packet is sent. 1547 */ 1548 if (in_nullhost(addr)) { 1549 imo->imo_multicast_ifp = NULL; 1550 break; 1551 } 1552 /* 1553 * The selected interface is identified by its local 1554 * IP address. Find the interface and confirm that 1555 * it supports multicasting. 1556 */ 1557 ifp = ip_multicast_if(&addr, &ifindex); 1558 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1559 error = EADDRNOTAVAIL; 1560 break; 1561 } 1562 imo->imo_multicast_ifp = ifp; 1563 if (ifindex) 1564 imo->imo_multicast_addr = addr; 1565 else 1566 imo->imo_multicast_addr.s_addr = INADDR_ANY; 1567 break; 1568 1569 case IP_MULTICAST_TTL: 1570 /* 1571 * Set the IP time-to-live for outgoing multicast packets. 1572 */ 1573 if (m == NULL || m->m_len != 1) { 1574 error = EINVAL; 1575 break; 1576 } 1577 imo->imo_multicast_ttl = *(mtod(m, u_char *)); 1578 break; 1579 1580 case IP_MULTICAST_LOOP: 1581 /* 1582 * Set the loopback flag for outgoing multicast packets. 1583 * Must be zero or one. 1584 */ 1585 if (m == NULL || m->m_len != 1 || 1586 (loop = *(mtod(m, u_char *))) > 1) { 1587 error = EINVAL; 1588 break; 1589 } 1590 imo->imo_multicast_loop = loop; 1591 break; 1592 1593 case IP_ADD_MEMBERSHIP: 1594 /* 1595 * Add a multicast group membership. 1596 * Group must be a valid IP multicast address. 1597 */ 1598 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1599 error = EINVAL; 1600 break; 1601 } 1602 mreq = mtod(m, struct ip_mreq *); 1603 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1604 error = EINVAL; 1605 break; 1606 } 1607 /* 1608 * If no interface address was provided, use the interface of 1609 * the route to the given multicast address. 1610 */ 1611 if (in_nullhost(mreq->imr_interface)) { 1612 bzero((caddr_t)&ro, sizeof(ro)); 1613 ro.ro_rt = NULL; 1614 dst = satosin(&ro.ro_dst); 1615 dst->sin_len = sizeof(*dst); 1616 dst->sin_family = AF_INET; 1617 dst->sin_addr = mreq->imr_multiaddr; 1618 rtalloc(&ro); 1619 if (ro.ro_rt == NULL) { 1620 error = EADDRNOTAVAIL; 1621 break; 1622 } 1623 ifp = ro.ro_rt->rt_ifp; 1624 rtfree(ro.ro_rt); 1625 } else { 1626 ifp = ip_multicast_if(&mreq->imr_interface, NULL); 1627 } 1628 /* 1629 * See if we found an interface, and confirm that it 1630 * supports multicast. 1631 */ 1632 if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { 1633 error = EADDRNOTAVAIL; 1634 break; 1635 } 1636 /* 1637 * See if the membership already exists or if all the 1638 * membership slots are full. 1639 */ 1640 for (i = 0; i < imo->imo_num_memberships; ++i) { 1641 if (imo->imo_membership[i]->inm_ifp == ifp && 1642 in_hosteq(imo->imo_membership[i]->inm_addr, 1643 mreq->imr_multiaddr)) 1644 break; 1645 } 1646 if (i < imo->imo_num_memberships) { 1647 error = EADDRINUSE; 1648 break; 1649 } 1650 if (i == IP_MAX_MEMBERSHIPS) { 1651 error = ETOOMANYREFS; 1652 break; 1653 } 1654 /* 1655 * Everything looks good; add a new record to the multicast 1656 * address list for the given interface. 1657 */ 1658 if ((imo->imo_membership[i] = 1659 in_addmulti(&mreq->imr_multiaddr, ifp)) == NULL) { 1660 error = ENOBUFS; 1661 break; 1662 } 1663 ++imo->imo_num_memberships; 1664 break; 1665 1666 case IP_DROP_MEMBERSHIP: 1667 /* 1668 * Drop a multicast group membership. 1669 * Group must be a valid IP multicast address. 1670 */ 1671 if (m == NULL || m->m_len != sizeof(struct ip_mreq)) { 1672 error = EINVAL; 1673 break; 1674 } 1675 mreq = mtod(m, struct ip_mreq *); 1676 if (!IN_MULTICAST(mreq->imr_multiaddr.s_addr)) { 1677 error = EINVAL; 1678 break; 1679 } 1680 /* 1681 * If an interface address was specified, get a pointer 1682 * to its ifnet structure. 1683 */ 1684 if (in_nullhost(mreq->imr_interface)) 1685 ifp = NULL; 1686 else { 1687 ifp = ip_multicast_if(&mreq->imr_interface, NULL); 1688 if (ifp == NULL) { 1689 error = EADDRNOTAVAIL; 1690 break; 1691 } 1692 } 1693 /* 1694 * Find the membership in the membership array. 1695 */ 1696 for (i = 0; i < imo->imo_num_memberships; ++i) { 1697 if ((ifp == NULL || 1698 imo->imo_membership[i]->inm_ifp == ifp) && 1699 in_hosteq(imo->imo_membership[i]->inm_addr, 1700 mreq->imr_multiaddr)) 1701 break; 1702 } 1703 if (i == imo->imo_num_memberships) { 1704 error = EADDRNOTAVAIL; 1705 break; 1706 } 1707 /* 1708 * Give up the multicast address record to which the 1709 * membership points. 1710 */ 1711 in_delmulti(imo->imo_membership[i]); 1712 /* 1713 * Remove the gap in the membership array. 1714 */ 1715 for (++i; i < imo->imo_num_memberships; ++i) 1716 imo->imo_membership[i-1] = imo->imo_membership[i]; 1717 --imo->imo_num_memberships; 1718 break; 1719 1720 default: 1721 error = EOPNOTSUPP; 1722 break; 1723 } 1724 1725 /* 1726 * If all options have default values, no need to keep the mbuf. 1727 */ 1728 if (imo->imo_multicast_ifp == NULL && 1729 imo->imo_multicast_ttl == IP_DEFAULT_MULTICAST_TTL && 1730 imo->imo_multicast_loop == IP_DEFAULT_MULTICAST_LOOP && 1731 imo->imo_num_memberships == 0) { 1732 free(*imop, M_IPMOPTS); 1733 *imop = NULL; 1734 } 1735 1736 return (error); 1737 } 1738 1739 /* 1740 * Return the IP multicast options in response to user getsockopt(). 1741 */ 1742 int 1743 ip_getmoptions(optname, imo, mp) 1744 int optname; 1745 struct ip_moptions *imo; 1746 struct mbuf **mp; 1747 { 1748 u_char *ttl; 1749 u_char *loop; 1750 struct in_addr *addr; 1751 struct in_ifaddr *ia; 1752 1753 *mp = m_get(M_WAIT, MT_SOOPTS); 1754 1755 switch (optname) { 1756 1757 case IP_MULTICAST_IF: 1758 addr = mtod(*mp, struct in_addr *); 1759 (*mp)->m_len = sizeof(struct in_addr); 1760 if (imo == NULL || imo->imo_multicast_ifp == NULL) 1761 *addr = zeroin_addr; 1762 else if (imo->imo_multicast_addr.s_addr) { 1763 /* return the value user has set */ 1764 *addr = imo->imo_multicast_addr; 1765 } else { 1766 IFP_TO_IA(imo->imo_multicast_ifp, ia); 1767 *addr = ia ? ia->ia_addr.sin_addr : zeroin_addr; 1768 } 1769 return (0); 1770 1771 case IP_MULTICAST_TTL: 1772 ttl = mtod(*mp, u_char *); 1773 (*mp)->m_len = 1; 1774 *ttl = imo ? imo->imo_multicast_ttl 1775 : IP_DEFAULT_MULTICAST_TTL; 1776 return (0); 1777 1778 case IP_MULTICAST_LOOP: 1779 loop = mtod(*mp, u_char *); 1780 (*mp)->m_len = 1; 1781 *loop = imo ? imo->imo_multicast_loop 1782 : IP_DEFAULT_MULTICAST_LOOP; 1783 return (0); 1784 1785 default: 1786 return (EOPNOTSUPP); 1787 } 1788 } 1789 1790 /* 1791 * Discard the IP multicast options. 1792 */ 1793 void 1794 ip_freemoptions(imo) 1795 struct ip_moptions *imo; 1796 { 1797 int i; 1798 1799 if (imo != NULL) { 1800 for (i = 0; i < imo->imo_num_memberships; ++i) 1801 in_delmulti(imo->imo_membership[i]); 1802 free(imo, M_IPMOPTS); 1803 } 1804 } 1805 1806 /* 1807 * Routine called from ip_output() to loop back a copy of an IP multicast 1808 * packet to the input queue of a specified interface. Note that this 1809 * calls the output routine of the loopback "driver", but with an interface 1810 * pointer that might NOT be &loif -- easier than replicating that code here. 1811 */ 1812 static void 1813 ip_mloopback(ifp, m, dst) 1814 struct ifnet *ifp; 1815 struct mbuf *m; 1816 struct sockaddr_in *dst; 1817 { 1818 struct ip *ip; 1819 struct mbuf *copym; 1820 1821 copym = m_copy(m, 0, M_COPYALL); 1822 if (copym != NULL 1823 && (copym->m_flags & M_EXT || copym->m_len < sizeof(struct ip))) 1824 copym = m_pullup(copym, sizeof(struct ip)); 1825 if (copym != NULL) { 1826 /* 1827 * We don't bother to fragment if the IP length is greater 1828 * than the interface's MTU. Can this possibly matter? 1829 */ 1830 ip = mtod(copym, struct ip *); 1831 1832 if (copym->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_UDPv4)) { 1833 in_delayed_cksum(copym); 1834 copym->m_pkthdr.csum_flags &= 1835 ~(M_CSUM_TCPv4|M_CSUM_UDPv4); 1836 } 1837 1838 ip->ip_sum = 0; 1839 ip->ip_sum = in_cksum(copym, ip->ip_hl << 2); 1840 (void) looutput(ifp, copym, sintosa(dst), NULL); 1841 } 1842 } 1843