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