1 /*- 2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the project nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ 30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ 31 */ 32 33 /*- 34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 35 * The Regents of the University of California. 36 * All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 4. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 63 */ 64 65 #include <sys/cdefs.h> 66 __FBSDID("$FreeBSD$"); 67 68 #include "opt_inet.h" 69 #include "opt_inet6.h" 70 #include "opt_ipsec.h" 71 #include "opt_mac.h" 72 73 #include <sys/param.h> 74 #include <sys/kernel.h> 75 #include <sys/lock.h> 76 #include <sys/mbuf.h> 77 #include <sys/priv.h> 78 #include <sys/proc.h> 79 #include <sys/protosw.h> 80 #include <sys/signalvar.h> 81 #include <sys/socket.h> 82 #include <sys/socketvar.h> 83 #include <sys/sx.h> 84 #include <sys/sysctl.h> 85 #include <sys/syslog.h> 86 #include <sys/systm.h> 87 88 #include <net/if.h> 89 #include <net/if_types.h> 90 #include <net/route.h> 91 92 #include <netinet/in.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/in_systm.h> 95 #include <netinet/in_var.h> 96 #include <netinet/ip.h> 97 #include <netinet/ip_icmp.h> 98 #include <netinet/ip6.h> 99 #include <netinet/icmp_var.h> 100 #include <netinet/icmp6.h> 101 #include <netinet/ip_var.h> 102 #include <netinet/udp.h> 103 #include <netinet/udp_var.h> 104 #include <netinet6/ip6protosw.h> 105 #include <netinet6/ip6_var.h> 106 #include <netinet6/in6_pcb.h> 107 #include <netinet6/udp6_var.h> 108 #include <netinet6/scope6_var.h> 109 110 #ifdef IPSEC 111 #include <netipsec/ipsec.h> 112 #include <netipsec/ipsec6.h> 113 #endif /* IPSEC */ 114 115 #include <security/mac/mac_framework.h> 116 117 /* 118 * UDP protocol implementation. 119 * Per RFC 768, August, 1980. 120 */ 121 122 extern struct protosw inetsw[]; 123 static void udp6_detach(struct socket *so); 124 125 static void 126 udp6_append(struct inpcb *inp, struct mbuf *n, int off, 127 struct sockaddr_in6 *fromsa) 128 { 129 struct socket *so; 130 struct mbuf *opts; 131 132 INP_LOCK_ASSERT(inp); 133 134 #ifdef IPSEC 135 /* Check AH/ESP integrity. */ 136 if (ipsec6_in_reject(n, inp)) { 137 m_freem(n); 138 ipsec6stat.in_polvio++; 139 return; 140 } 141 #endif /* IPSEC */ 142 #ifdef MAC 143 if (mac_inpcb_check_deliver(inp, n) != 0) { 144 m_freem(n); 145 return; 146 } 147 #endif 148 opts = NULL; 149 if (inp->in6p_flags & IN6P_CONTROLOPTS || 150 inp->inp_socket->so_options & SO_TIMESTAMP) 151 ip6_savecontrol(inp, n, &opts); 152 m_adj(n, off + sizeof(struct udphdr)); 153 154 so = inp->inp_socket; 155 SOCKBUF_LOCK(&so->so_rcv); 156 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n, 157 opts) == 0) { 158 SOCKBUF_UNLOCK(&so->so_rcv); 159 m_freem(n); 160 if (opts) 161 m_freem(opts); 162 udpstat.udps_fullsock++; 163 } else 164 sorwakeup_locked(so); 165 } 166 167 int 168 udp6_input(struct mbuf **mp, int *offp, int proto) 169 { 170 struct mbuf *m = *mp; 171 struct ip6_hdr *ip6; 172 struct udphdr *uh; 173 struct inpcb *inp; 174 int off = *offp; 175 int plen, ulen; 176 struct sockaddr_in6 fromsa; 177 178 ip6 = mtod(m, struct ip6_hdr *); 179 180 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { 181 /* XXX send icmp6 host/port unreach? */ 182 m_freem(m); 183 return (IPPROTO_DONE); 184 } 185 186 #ifndef PULLDOWN_TEST 187 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 188 ip6 = mtod(m, struct ip6_hdr *); 189 uh = (struct udphdr *)((caddr_t)ip6 + off); 190 #else 191 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh)); 192 if (!uh) 193 return (IPPROTO_DONE); 194 #endif 195 196 udpstat.udps_ipackets++; 197 198 /* 199 * Destination port of 0 is illegal, based on RFC768. 200 */ 201 if (uh->uh_dport == 0) 202 goto badunlocked; 203 204 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 205 ulen = ntohs((u_short)uh->uh_ulen); 206 207 if (plen != ulen) { 208 udpstat.udps_badlen++; 209 goto badunlocked; 210 } 211 212 /* 213 * Checksum extended UDP header and data. 214 */ 215 if (uh->uh_sum == 0) { 216 udpstat.udps_nosum++; 217 goto badunlocked; 218 } 219 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 220 udpstat.udps_badsum++; 221 goto badunlocked; 222 } 223 224 /* 225 * Construct sockaddr format source address. 226 */ 227 init_sin6(&fromsa, m); 228 fromsa.sin6_port = uh->uh_sport; 229 230 INP_INFO_RLOCK(&udbinfo); 231 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 232 struct inpcb *last; 233 234 /* 235 * In the event that laddr should be set to the link-local 236 * address (this happens in RIPng), the multicast address 237 * specified in the received packet will not match laddr. To 238 * handle this situation, matching is relaxed if the 239 * receiving interface is the same as one specified in the 240 * socket and if the destination multicast address matches 241 * one of the multicast groups specified in the socket. 242 */ 243 244 /* 245 * KAME note: traditionally we dropped udpiphdr from mbuf 246 * here. We need udphdr for IPsec processing so we do that 247 * later. 248 */ 249 last = NULL; 250 LIST_FOREACH(inp, &udb, inp_list) { 251 if ((inp->inp_vflag & INP_IPV6) == 0) 252 continue; 253 if (inp->in6p_lport != uh->uh_dport) 254 continue; 255 /* 256 * XXX: Do not check source port of incoming datagram 257 * unless inp_connect() has been called to bind the 258 * fport part of the 4-tuple; the source could be 259 * trying to talk to us with an ephemeral port. 260 */ 261 if (inp->inp_fport != 0 && 262 inp->inp_fport != uh->uh_sport) 263 continue; 264 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { 265 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, 266 &ip6->ip6_dst)) 267 continue; 268 } 269 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 270 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, 271 &ip6->ip6_src) || 272 inp->in6p_fport != uh->uh_sport) 273 continue; 274 } 275 276 if (last != NULL) { 277 struct mbuf *n; 278 279 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 280 INP_LOCK(last); 281 udp6_append(last, n, off, &fromsa); 282 INP_UNLOCK(last); 283 } 284 } 285 last = inp; 286 /* 287 * Don't look for additional matches if this one does 288 * not have either the SO_REUSEPORT or SO_REUSEADDR 289 * socket options set. This heuristic avoids 290 * searching through all pcbs in the common case of a 291 * non-shared port. It assumes that an application 292 * will never clear these options after setting them. 293 */ 294 if ((last->inp_socket->so_options & 295 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 296 break; 297 } 298 299 if (last == NULL) { 300 /* 301 * No matching pcb found; discard datagram. (No need 302 * to send an ICMP Port Unreachable for a broadcast 303 * or multicast datgram.) 304 */ 305 udpstat.udps_noport++; 306 udpstat.udps_noportmcast++; 307 goto badheadlocked; 308 } 309 INP_LOCK(last); 310 udp6_append(last, m, off, &fromsa); 311 INP_UNLOCK(last); 312 INP_INFO_RUNLOCK(&udbinfo); 313 return (IPPROTO_DONE); 314 } 315 /* 316 * Locate pcb for datagram. 317 */ 318 inp = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 319 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 320 if (inp == NULL) { 321 if (udp_log_in_vain) { 322 char ip6bufs[INET6_ADDRSTRLEN]; 323 char ip6bufd[INET6_ADDRSTRLEN]; 324 325 log(LOG_INFO, 326 "Connection attempt to UDP [%s]:%d from [%s]:%d\n", 327 ip6_sprintf(ip6bufd, &ip6->ip6_dst), 328 ntohs(uh->uh_dport), 329 ip6_sprintf(ip6bufs, &ip6->ip6_src), 330 ntohs(uh->uh_sport)); 331 } 332 udpstat.udps_noport++; 333 if (m->m_flags & M_MCAST) { 334 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 335 udpstat.udps_noportmcast++; 336 goto badheadlocked; 337 } 338 INP_INFO_RUNLOCK(&udbinfo); 339 if (udp_blackhole) 340 goto badunlocked; 341 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0) 342 goto badunlocked; 343 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 344 return (IPPROTO_DONE); 345 } 346 INP_LOCK(inp); 347 udp6_append(inp, m, off, &fromsa); 348 INP_UNLOCK(inp); 349 INP_INFO_RUNLOCK(&udbinfo); 350 return (IPPROTO_DONE); 351 352 badheadlocked: 353 INP_INFO_RUNLOCK(&udbinfo); 354 badunlocked: 355 if (m) 356 m_freem(m); 357 return (IPPROTO_DONE); 358 } 359 360 void 361 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d) 362 { 363 struct udphdr uh; 364 struct ip6_hdr *ip6; 365 struct mbuf *m; 366 int off = 0; 367 struct ip6ctlparam *ip6cp = NULL; 368 const struct sockaddr_in6 *sa6_src = NULL; 369 void *cmdarg; 370 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify; 371 struct udp_portonly { 372 u_int16_t uh_sport; 373 u_int16_t uh_dport; 374 } *uhp; 375 376 if (sa->sa_family != AF_INET6 || 377 sa->sa_len != sizeof(struct sockaddr_in6)) 378 return; 379 380 if ((unsigned)cmd >= PRC_NCMDS) 381 return; 382 if (PRC_IS_REDIRECT(cmd)) 383 notify = in6_rtchange, d = NULL; 384 else if (cmd == PRC_HOSTDEAD) 385 d = NULL; 386 else if (inet6ctlerrmap[cmd] == 0) 387 return; 388 389 /* if the parameter is from icmp6, decode it. */ 390 if (d != NULL) { 391 ip6cp = (struct ip6ctlparam *)d; 392 m = ip6cp->ip6c_m; 393 ip6 = ip6cp->ip6c_ip6; 394 off = ip6cp->ip6c_off; 395 cmdarg = ip6cp->ip6c_cmdarg; 396 sa6_src = ip6cp->ip6c_src; 397 } else { 398 m = NULL; 399 ip6 = NULL; 400 cmdarg = NULL; 401 sa6_src = &sa6_any; 402 } 403 404 if (ip6) { 405 /* 406 * XXX: We assume that when IPV6 is non NULL, 407 * M and OFF are valid. 408 */ 409 410 /* Check if we can safely examine src and dst ports. */ 411 if (m->m_pkthdr.len < off + sizeof(*uhp)) 412 return; 413 414 bzero(&uh, sizeof(uh)); 415 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 416 417 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport, 418 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd, 419 cmdarg, notify); 420 } else 421 (void) in6_pcbnotify(&udbinfo, sa, 0, 422 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify); 423 } 424 425 static int 426 udp6_getcred(SYSCTL_HANDLER_ARGS) 427 { 428 struct xucred xuc; 429 struct sockaddr_in6 addrs[2]; 430 struct inpcb *inp; 431 int error; 432 433 error = priv_check(req->td, PRIV_NETINET_GETCRED); 434 if (error) 435 return (error); 436 437 if (req->newlen != sizeof(addrs)) 438 return (EINVAL); 439 if (req->oldlen != sizeof(struct xucred)) 440 return (EINVAL); 441 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 442 if (error) 443 return (error); 444 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 || 445 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) { 446 return (error); 447 } 448 INP_INFO_RLOCK(&udbinfo); 449 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 450 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1, 451 NULL); 452 if (inp == NULL) { 453 INP_INFO_RUNLOCK(&udbinfo); 454 return (ENOENT); 455 } 456 INP_LOCK(inp); 457 if (inp->inp_socket == NULL) { 458 error = ENOENT; 459 goto out; 460 } 461 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket); 462 if (error) 463 goto out; 464 cru2x(inp->inp_socket->so_cred, &xuc); 465 out: 466 INP_UNLOCK(inp); 467 INP_INFO_RUNLOCK(&udbinfo); 468 if (error == 0) 469 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred)); 470 return (error); 471 } 472 473 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0, 474 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection"); 475 476 static int 477 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6, 478 struct mbuf *control, struct thread *td) 479 { 480 u_int32_t ulen = m->m_pkthdr.len; 481 u_int32_t plen = sizeof(struct udphdr) + ulen; 482 struct ip6_hdr *ip6; 483 struct udphdr *udp6; 484 struct in6_addr *laddr, *faddr; 485 struct sockaddr_in6 *sin6 = NULL; 486 struct ifnet *oifp = NULL; 487 int scope_ambiguous = 0; 488 u_short fport; 489 int error = 0; 490 struct ip6_pktopts *optp, opt; 491 int priv; 492 int af = AF_INET6, hlen = sizeof(struct ip6_hdr); 493 int flags; 494 struct sockaddr_in6 tmp; 495 496 INP_LOCK_ASSERT(inp); 497 498 priv = 0; 499 if (td && !suser(td)) 500 priv = 1; 501 502 if (addr6) { 503 /* addr6 has been validated in udp6_send(). */ 504 sin6 = (struct sockaddr_in6 *)addr6; 505 506 /* protect *sin6 from overwrites */ 507 tmp = *sin6; 508 sin6 = &tmp; 509 510 /* 511 * Application should provide a proper zone ID or the use of 512 * default zone IDs should be enabled. Unfortunately, some 513 * applications do not behave as it should, so we need a 514 * workaround. Even if an appropriate ID is not determined, 515 * we'll see if we can determine the outgoing interface. If we 516 * can, determine the zone ID based on the interface below. 517 */ 518 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) 519 scope_ambiguous = 1; 520 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) 521 return (error); 522 } 523 524 if (control) { 525 if ((error = ip6_setpktopts(control, &opt, 526 inp->in6p_outputopts, priv, IPPROTO_UDP)) != 0) 527 goto release; 528 optp = &opt; 529 } else 530 optp = inp->in6p_outputopts; 531 532 if (sin6) { 533 faddr = &sin6->sin6_addr; 534 535 /* 536 * IPv4 version of udp_output calls in_pcbconnect in this case, 537 * which needs splnet and affects performance. 538 * Since we saw no essential reason for calling in_pcbconnect, 539 * we get rid of such kind of logic, and call in6_selectsrc 540 * and in6_pcbsetport in order to fill in the local address 541 * and the local port. 542 */ 543 if (sin6->sin6_port == 0) { 544 error = EADDRNOTAVAIL; 545 goto release; 546 } 547 548 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 549 /* how about ::ffff:0.0.0.0 case? */ 550 error = EISCONN; 551 goto release; 552 } 553 554 fport = sin6->sin6_port; /* allow 0 port */ 555 556 if (IN6_IS_ADDR_V4MAPPED(faddr)) { 557 if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) { 558 /* 559 * I believe we should explicitly discard the 560 * packet when mapped addresses are disabled, 561 * rather than send the packet as an IPv6 one. 562 * If we chose the latter approach, the packet 563 * might be sent out on the wire based on the 564 * default route, the situation which we'd 565 * probably want to avoid. 566 * (20010421 jinmei@kame.net) 567 */ 568 error = EINVAL; 569 goto release; 570 } 571 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 572 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) { 573 /* 574 * when remote addr is an IPv4-mapped address, 575 * local addr should not be an IPv6 address, 576 * since you cannot determine how to map IPv6 577 * source address to IPv4. 578 */ 579 error = EINVAL; 580 goto release; 581 } 582 583 af = AF_INET; 584 } 585 586 if (!IN6_IS_ADDR_V4MAPPED(faddr)) { 587 laddr = in6_selectsrc(sin6, optp, inp->in6p_moptions, 588 NULL, &inp->in6p_laddr, &oifp, &error); 589 if (oifp && scope_ambiguous && 590 (error = in6_setscope(&sin6->sin6_addr, 591 oifp, NULL))) { 592 goto release; 593 } 594 } else 595 laddr = &inp->in6p_laddr; /* XXX */ 596 if (laddr == NULL) { 597 if (error == 0) 598 error = EADDRNOTAVAIL; 599 goto release; 600 } 601 if (inp->in6p_lport == 0 && 602 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) 603 goto release; 604 } else { 605 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 606 error = ENOTCONN; 607 goto release; 608 } 609 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) { 610 if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) { 611 /* 612 * XXX: this case would happen when the 613 * application sets the V6ONLY flag after 614 * connecting the foreign address. 615 * Such applications should be fixed, 616 * so we bark here. 617 */ 618 log(LOG_INFO, "udp6_output: IPV6_V6ONLY " 619 "option was set for a connected socket\n"); 620 error = EINVAL; 621 goto release; 622 } else 623 af = AF_INET; 624 } 625 laddr = &inp->in6p_laddr; 626 faddr = &inp->in6p_faddr; 627 fport = inp->in6p_fport; 628 } 629 630 if (af == AF_INET) 631 hlen = sizeof(struct ip); 632 633 /* 634 * Calculate data length and get a mbuf 635 * for UDP and IP6 headers. 636 */ 637 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 638 if (m == 0) { 639 error = ENOBUFS; 640 goto release; 641 } 642 643 /* 644 * Stuff checksum and output datagram. 645 */ 646 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 647 udp6->uh_sport = inp->in6p_lport; /* lport is always set in the PCB */ 648 udp6->uh_dport = fport; 649 if (plen <= 0xffff) 650 udp6->uh_ulen = htons((u_short)plen); 651 else 652 udp6->uh_ulen = 0; 653 udp6->uh_sum = 0; 654 655 switch (af) { 656 case AF_INET6: 657 ip6 = mtod(m, struct ip6_hdr *); 658 ip6->ip6_flow = inp->in6p_flowinfo & IPV6_FLOWINFO_MASK; 659 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 660 ip6->ip6_vfc |= IPV6_VERSION; 661 #if 0 /* ip6_plen will be filled in ip6_output. */ 662 ip6->ip6_plen = htons((u_short)plen); 663 #endif 664 ip6->ip6_nxt = IPPROTO_UDP; 665 ip6->ip6_hlim = in6_selecthlim(inp, NULL); 666 ip6->ip6_src = *laddr; 667 ip6->ip6_dst = *faddr; 668 669 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 670 sizeof(struct ip6_hdr), plen)) == 0) { 671 udp6->uh_sum = 0xffff; 672 } 673 674 flags = 0; 675 676 udpstat.udps_opackets++; 677 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions, 678 NULL, inp); 679 break; 680 case AF_INET: 681 error = EAFNOSUPPORT; 682 goto release; 683 } 684 goto releaseopt; 685 686 release: 687 m_freem(m); 688 689 releaseopt: 690 if (control) { 691 ip6_clearpktopts(&opt, -1); 692 m_freem(control); 693 } 694 return (error); 695 } 696 697 static void 698 udp6_abort(struct socket *so) 699 { 700 struct inpcb *inp; 701 702 inp = sotoinpcb(so); 703 KASSERT(inp != NULL, ("udp6_abort: inp == NULL")); 704 705 #ifdef INET 706 if (inp->inp_vflag & INP_IPV4) { 707 struct pr_usrreqs *pru; 708 709 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 710 (*pru->pru_abort)(so); 711 return; 712 } 713 #endif 714 715 INP_INFO_WLOCK(&udbinfo); 716 INP_LOCK(inp); 717 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 718 in6_pcbdisconnect(inp); 719 inp->in6p_laddr = in6addr_any; 720 soisdisconnected(so); 721 } 722 INP_UNLOCK(inp); 723 INP_INFO_WUNLOCK(&udbinfo); 724 } 725 726 static int 727 udp6_attach(struct socket *so, int proto, struct thread *td) 728 { 729 struct inpcb *inp; 730 int error; 731 732 inp = sotoinpcb(so); 733 KASSERT(inp == NULL, ("udp6_attach: inp != NULL")); 734 735 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 736 error = soreserve(so, udp_sendspace, udp_recvspace); 737 if (error) 738 return (error); 739 } 740 INP_INFO_WLOCK(&udbinfo); 741 error = in_pcballoc(so, &udbinfo); 742 if (error) { 743 INP_INFO_WUNLOCK(&udbinfo); 744 return (error); 745 } 746 inp = (struct inpcb *)so->so_pcb; 747 INP_INFO_WUNLOCK(&udbinfo); 748 inp->inp_vflag |= INP_IPV6; 749 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) 750 inp->inp_vflag |= INP_IPV4; 751 inp->in6p_hops = -1; /* use kernel default */ 752 inp->in6p_cksum = -1; /* just to be sure */ 753 /* 754 * XXX: ugly!! 755 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 756 * because the socket may be bound to an IPv6 wildcard address, 757 * which may match an IPv4-mapped IPv6 address. 758 */ 759 inp->inp_ip_ttl = ip_defttl; 760 INP_UNLOCK(inp); 761 return (0); 762 } 763 764 static int 765 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 766 { 767 struct inpcb *inp; 768 int error; 769 770 inp = sotoinpcb(so); 771 KASSERT(inp != NULL, ("udp6_bind: inp == NULL")); 772 773 INP_INFO_WLOCK(&udbinfo); 774 INP_LOCK(inp); 775 inp->inp_vflag &= ~INP_IPV4; 776 inp->inp_vflag |= INP_IPV6; 777 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 778 struct sockaddr_in6 *sin6_p; 779 780 sin6_p = (struct sockaddr_in6 *)nam; 781 782 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 783 inp->inp_vflag |= INP_IPV4; 784 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 785 struct sockaddr_in sin; 786 787 in6_sin6_2_sin(&sin, sin6_p); 788 inp->inp_vflag |= INP_IPV4; 789 inp->inp_vflag &= ~INP_IPV6; 790 error = in_pcbbind(inp, (struct sockaddr *)&sin, 791 td->td_ucred); 792 goto out; 793 } 794 } 795 796 error = in6_pcbbind(inp, nam, td->td_ucred); 797 out: 798 INP_UNLOCK(inp); 799 INP_INFO_WUNLOCK(&udbinfo); 800 return (error); 801 } 802 803 static void 804 udp6_close(struct socket *so) 805 { 806 struct inpcb *inp; 807 808 inp = sotoinpcb(so); 809 KASSERT(inp != NULL, ("udp6_close: inp == NULL")); 810 811 #ifdef INET 812 if (inp->inp_vflag & INP_IPV4) { 813 struct pr_usrreqs *pru; 814 815 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 816 (*pru->pru_disconnect)(so); 817 return; 818 } 819 #endif 820 INP_INFO_WLOCK(&udbinfo); 821 INP_LOCK(inp); 822 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 823 in6_pcbdisconnect(inp); 824 inp->in6p_laddr = in6addr_any; 825 soisdisconnected(so); 826 } 827 INP_UNLOCK(inp); 828 INP_INFO_WUNLOCK(&udbinfo); 829 } 830 831 static int 832 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 833 { 834 struct inpcb *inp; 835 int error; 836 837 inp = sotoinpcb(so); 838 KASSERT(inp != NULL, ("udp6_connect: inp == NULL")); 839 840 INP_INFO_WLOCK(&udbinfo); 841 INP_LOCK(inp); 842 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 843 struct sockaddr_in6 *sin6_p; 844 845 sin6_p = (struct sockaddr_in6 *)nam; 846 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 847 struct sockaddr_in sin; 848 849 if (inp->inp_faddr.s_addr != INADDR_ANY) { 850 error = EISCONN; 851 goto out; 852 } 853 in6_sin6_2_sin(&sin, sin6_p); 854 error = in_pcbconnect(inp, (struct sockaddr *)&sin, 855 td->td_ucred); 856 if (error == 0) { 857 inp->inp_vflag |= INP_IPV4; 858 inp->inp_vflag &= ~INP_IPV6; 859 soisconnected(so); 860 } 861 goto out; 862 } 863 } 864 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 865 error = EISCONN; 866 goto out; 867 } 868 error = in6_pcbconnect(inp, nam, td->td_ucred); 869 if (error == 0) { 870 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 871 /* should be non mapped addr */ 872 inp->inp_vflag &= ~INP_IPV4; 873 inp->inp_vflag |= INP_IPV6; 874 } 875 soisconnected(so); 876 } 877 out: 878 INP_UNLOCK(inp); 879 INP_INFO_WUNLOCK(&udbinfo); 880 return (error); 881 } 882 883 static void 884 udp6_detach(struct socket *so) 885 { 886 struct inpcb *inp; 887 888 inp = sotoinpcb(so); 889 KASSERT(inp != NULL, ("udp6_detach: inp == NULL")); 890 891 INP_INFO_WLOCK(&udbinfo); 892 INP_LOCK(inp); 893 in6_pcbdetach(inp); 894 in6_pcbfree(inp); 895 INP_INFO_WUNLOCK(&udbinfo); 896 } 897 898 static int 899 udp6_disconnect(struct socket *so) 900 { 901 struct inpcb *inp; 902 int error; 903 904 inp = sotoinpcb(so); 905 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL")); 906 907 INP_INFO_WLOCK(&udbinfo); 908 INP_LOCK(inp); 909 910 #ifdef INET 911 if (inp->inp_vflag & INP_IPV4) { 912 struct pr_usrreqs *pru; 913 914 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 915 error = (*pru->pru_disconnect)(so); 916 goto out; 917 } 918 #endif 919 920 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { 921 error = ENOTCONN; 922 goto out; 923 } 924 925 in6_pcbdisconnect(inp); 926 inp->in6p_laddr = in6addr_any; 927 /* XXXRW: so_state locking? */ 928 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 929 out: 930 INP_UNLOCK(inp); 931 INP_INFO_WUNLOCK(&udbinfo); 932 return (0); 933 } 934 935 static int 936 udp6_send(struct socket *so, int flags, struct mbuf *m, 937 struct sockaddr *addr, struct mbuf *control, struct thread *td) 938 { 939 struct inpcb *inp; 940 int error = 0; 941 942 inp = sotoinpcb(so); 943 KASSERT(inp != NULL, ("udp6_send: inp == NULL")); 944 945 INP_INFO_WLOCK(&udbinfo); 946 INP_LOCK(inp); 947 if (addr) { 948 if (addr->sa_len != sizeof(struct sockaddr_in6)) { 949 error = EINVAL; 950 goto bad; 951 } 952 if (addr->sa_family != AF_INET6) { 953 error = EAFNOSUPPORT; 954 goto bad; 955 } 956 } 957 958 #ifdef INET 959 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { 960 int hasv4addr; 961 struct sockaddr_in6 *sin6 = 0; 962 963 if (addr == 0) 964 hasv4addr = (inp->inp_vflag & INP_IPV4); 965 else { 966 sin6 = (struct sockaddr_in6 *)addr; 967 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 968 ? 1 : 0; 969 } 970 if (hasv4addr) { 971 struct pr_usrreqs *pru; 972 973 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) && 974 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) { 975 /* 976 * When remote addr is IPv4-mapped address, 977 * local addr should not be an IPv6 address; 978 * since you cannot determine how to map IPv6 979 * source address to IPv4. 980 */ 981 error = EINVAL; 982 goto out; 983 } 984 if (sin6) 985 in6_sin6_2_sin_in_sock(addr); 986 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 987 error = ((*pru->pru_send)(so, flags, m, addr, control, 988 td)); 989 /* addr will just be freed in sendit(). */ 990 goto out; 991 } 992 } 993 #endif 994 #ifdef MAC 995 mac_inpcb_create_mbuf(inp, m); 996 #endif 997 error = udp6_output(inp, m, addr, control, td); 998 out: 999 INP_UNLOCK(inp); 1000 INP_INFO_WUNLOCK(&udbinfo); 1001 return (error); 1002 1003 bad: 1004 INP_UNLOCK(inp); 1005 INP_INFO_WUNLOCK(&udbinfo); 1006 m_freem(m); 1007 return (error); 1008 } 1009 1010 struct pr_usrreqs udp6_usrreqs = { 1011 .pru_abort = udp6_abort, 1012 .pru_attach = udp6_attach, 1013 .pru_bind = udp6_bind, 1014 .pru_connect = udp6_connect, 1015 .pru_control = in6_control, 1016 .pru_detach = udp6_detach, 1017 .pru_disconnect = udp6_disconnect, 1018 .pru_peeraddr = in6_mapped_peeraddr, 1019 .pru_send = udp6_send, 1020 .pru_shutdown = udp_shutdown, 1021 .pru_sockaddr = in6_mapped_sockaddr, 1022 .pru_sosetlabel = in_pcbsosetlabel, 1023 .pru_close = udp6_close 1024 }; 1025