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