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