udp6_usrreq.c revision 61958
1160814Ssimon/* 2160814Ssimon * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 3160814Ssimon * All rights reserved. 4160814Ssimon * 5160814Ssimon * Redistribution and use in source and binary forms, with or without 6160814Ssimon * modification, are permitted provided that the following conditions 7160814Ssimon * are met: 8160814Ssimon * 1. Redistributions of source code must retain the above copyright 9280297Sjkim * notice, this list of conditions and the following disclaimer. 10160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 11160814Ssimon * notice, this list of conditions and the following disclaimer in the 12160814Ssimon * documentation and/or other materials provided with the distribution. 13160814Ssimon * 3. Neither the name of the project nor the names of its contributors 14160814Ssimon * may be used to endorse or promote products derived from this software 15160814Ssimon * without specific prior written permission. 16160814Ssimon * 17160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 18160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20160814Ssimon * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 21160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27160814Ssimon * SUCH DAMAGE. 28160814Ssimon */ 29160814Ssimon 30160814Ssimon/* 31160814Ssimon * Copyright (c) 1982, 1986, 1989, 1993 32160814Ssimon * The Regents of the University of California. All rights reserved. 33160814Ssimon * 34160814Ssimon * Redistribution and use in source and binary forms, with or without 35160814Ssimon * modification, are permitted provided that the following conditions 36160814Ssimon * are met: 37160814Ssimon * 1. Redistributions of source code must retain the above copyright 38160814Ssimon * notice, this list of conditions and the following disclaimer. 39160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright 40160814Ssimon * notice, this list of conditions and the following disclaimer in the 41160814Ssimon * documentation and/or other materials provided with the distribution. 42160814Ssimon * 3. All advertising materials mentioning features or use of this software 43160814Ssimon * must display the following acknowledgement: 44160814Ssimon * This product includes software developed by the University of 45160814Ssimon * California, Berkeley and its contributors. 46160814Ssimon * 4. Neither the name of the University nor the names of its contributors 47160814Ssimon * may be used to endorse or promote products derived from this software 48160814Ssimon * without specific prior written permission. 49160814Ssimon * 50160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53160814Ssimon * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56280297Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58194206Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59194206Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60194206Ssimon * SUCH DAMAGE. 61194206Ssimon * 62160814Ssimon * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 63280297Sjkim * $FreeBSD: head/sys/netinet6/udp6_usrreq.c 61958 2000-06-22 16:48:59Z itojun $ 64280297Sjkim */ 65160814Ssimon 66160814Ssimon#include "opt_ipsec.h" 67160814Ssimon 68160814Ssimon#include <sys/param.h> 69160814Ssimon#include <sys/kernel.h> 70280297Sjkim#include <sys/mbuf.h> 71160814Ssimon#include <sys/protosw.h> 72160814Ssimon#include <sys/socket.h> 73160814Ssimon#include <sys/socketvar.h> 74160814Ssimon#include <sys/sysctl.h> 75280297Sjkim#include <sys/errno.h> 76280297Sjkim#include <sys/stat.h> 77280297Sjkim#include <sys/systm.h> 78280297Sjkim#include <sys/syslog.h> 79280297Sjkim#include <sys/proc.h> 80280297Sjkim 81280297Sjkim#include <net/if.h> 82280297Sjkim#include <net/route.h> 83280297Sjkim#include <net/if_types.h> 84280297Sjkim 85160814Ssimon#include <netinet/in.h> 86160814Ssimon#include <netinet/in_systm.h> 87280297Sjkim#include <netinet/ip.h> 88280297Sjkim#include <netinet/in_pcb.h> 89280297Sjkim#include <netinet/in_var.h> 90280297Sjkim#include <netinet/ip_var.h> 91280297Sjkim#include <netinet/udp.h> 92280297Sjkim#include <netinet/udp_var.h> 93280297Sjkim#include <netinet6/ip6.h> 94280297Sjkim#include <netinet6/ip6_var.h> 95280297Sjkim#include <netinet6/in6_pcb.h> 96280297Sjkim#include <netinet6/icmp6.h> 97280297Sjkim#include <netinet6/udp6_var.h> 98280297Sjkim#include <netinet6/ip6protosw.h> 99280297Sjkim 100280297Sjkim#ifdef IPSEC 101160814Ssimon#include <netinet6/ipsec.h> 102160814Ssimon#include <netinet6/ipsec6.h> 103160814Ssimon#endif /*IPSEC*/ 104160814Ssimon 105160814Ssimon#include "faith.h" 106 107/* 108 * UDP protocol inplementation. 109 * Per RFC 768, August, 1980. 110 */ 111 112extern struct protosw inetsw[]; 113static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *)); 114static int udp6_detach __P((struct socket *so)); 115 116static int 117in6_mcmatch(in6p, ia6, ifp) 118 struct inpcb *in6p; 119 register struct in6_addr *ia6; 120 struct ifnet *ifp; 121{ 122 struct ip6_moptions *im6o = in6p->in6p_moptions; 123 struct in6_multi_mship *imm; 124 125 if (im6o == NULL) 126 return 0; 127 128 for (imm = im6o->im6o_memberships.lh_first; imm != NULL; 129 imm = imm->i6mm_chain.le_next) { 130 if ((ifp == NULL || 131 imm->i6mm_maddr->in6m_ifp == ifp) && 132 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 133 ia6)) 134 return 1; 135 } 136 return 0; 137} 138 139int 140udp6_input(mp, offp, proto) 141 struct mbuf **mp; 142 int *offp, proto; 143{ 144 struct mbuf *m = *mp; 145 register struct ip6_hdr *ip6; 146 register struct udphdr *uh; 147 register struct inpcb *in6p; 148 struct mbuf *opts = 0; 149 int off = *offp; 150 int plen, ulen; 151 struct sockaddr_in6 udp_in6; 152 153#if defined(NFAITH) && 0 < NFAITH 154 if (m->m_pkthdr.rcvif) { 155 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 156 /* XXX send icmp6 host/port unreach? */ 157 m_freem(m); 158 return IPPROTO_DONE; 159 } 160 } 161#endif 162 udpstat.udps_ipackets++; 163 164 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 165 166 ip6 = mtod(m, struct ip6_hdr *); 167 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); 168 uh = (struct udphdr *)((caddr_t)ip6 + off); 169 ulen = ntohs((u_short)uh->uh_ulen); 170 171 if (plen != ulen) { 172 udpstat.udps_badlen++; 173 goto bad; 174 } 175 176 /* 177 * Checksum extended UDP header and data. 178 */ 179 if (uh->uh_sum == 0) 180 udpstat.udps_nosum++; 181 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 182 udpstat.udps_badsum++; 183 goto bad; 184 } 185 186 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 187 struct inpcb *last; 188 189 /* 190 * Deliver a multicast datagram to all sockets 191 * for which the local and remote addresses and ports match 192 * those of the incoming datagram. This allows more than 193 * one process to receive multicasts on the same port. 194 * (This really ought to be done for unicast datagrams as 195 * well, but that would cause problems with existing 196 * applications that open both address-specific sockets and 197 * a wildcard socket listening to the same port -- they would 198 * end up receiving duplicates of every unicast datagram. 199 * Those applications open the multiple sockets to overcome an 200 * inadequacy of the UDP socket interface, but for backwards 201 * compatibility we avoid the problem here rather than 202 * fixing the interface. Maybe 4.5BSD will remedy this?) 203 */ 204 205 /* 206 * In a case that laddr should be set to the link-local 207 * address (this happens in RIPng), the multicast address 208 * specified in the received packet does not match with 209 * laddr. To cure this situation, the matching is relaxed 210 * if the receiving interface is the same as one specified 211 * in the socket and if the destination multicast address 212 * matches one of the multicast groups specified in the socket. 213 */ 214 215 /* 216 * Construct sockaddr format source address. 217 */ 218 init_sin6(&udp_in6, m); /* general init */ 219 udp_in6.sin6_port = uh->uh_sport; 220 /* 221 * KAME note: usually we drop udphdr from mbuf here. 222 * We need udphdr for IPsec processing so we do that later. 223 */ 224 225 /* 226 * Locate pcb(s) for datagram. 227 * (Algorithm copied from raw_intr().) 228 */ 229 last = NULL; 230 LIST_FOREACH(in6p, &udb, inp_list) { 231 if ((in6p->inp_vflag & INP_IPV6) == 0) 232 continue; 233 if (in6p->in6p_lport != uh->uh_dport) 234 continue; 235 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 236 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 237 &ip6->ip6_dst) && 238 !in6_mcmatch(in6p, &ip6->ip6_dst, 239 m->m_pkthdr.rcvif)) 240 continue; 241 } 242 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 243 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 244 &ip6->ip6_src) || 245 in6p->in6p_fport != uh->uh_sport) 246 continue; 247 } 248 249 if (last != NULL) { 250 struct mbuf *n; 251 252#ifdef IPSEC 253 /* 254 * Check AH/ESP integrity. 255 */ 256 if (ipsec6_in_reject_so(m, last->inp_socket)) 257 ipsec6stat.in_polvio++; 258 /* do not inject data into pcb */ 259 else 260#endif /*IPSEC*/ 261 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 262 /* 263 * KAME NOTE: do not 264 * m_copy(m, offset, ...) above. 265 * sbappendaddr() expects M_PKTHDR, 266 * and m_copy() will copy M_PKTHDR 267 * only if offset is 0. 268 */ 269 if (last->in6p_flags & IN6P_CONTROLOPTS 270 || last->in6p_socket->so_options & SO_TIMESTAMP) 271 ip6_savecontrol(last, &opts, 272 ip6, n); 273 m_adj(n, off + sizeof(struct udphdr)); 274 if (sbappendaddr(&last->in6p_socket->so_rcv, 275 (struct sockaddr *)&udp_in6, 276 n, opts) == 0) { 277 m_freem(n); 278 if (opts) 279 m_freem(opts); 280 udpstat.udps_fullsock++; 281 } else 282 sorwakeup(last->in6p_socket); 283 opts = 0; 284 } 285 } 286 last = in6p; 287 /* 288 * Don't look for additional matches if this one does 289 * not have either the SO_REUSEPORT or SO_REUSEADDR 290 * socket options set. This heuristic avoids searching 291 * through all pcbs in the common case of a non-shared 292 * port. It assumes that an application will never 293 * clear these options after setting them. 294 */ 295 if ((last->in6p_socket->so_options & 296 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 297 break; 298 } 299 300 if (last == NULL) { 301 /* 302 * No matching pcb found; discard datagram. 303 * (No need to send an ICMP Port Unreachable 304 * for a broadcast or multicast datgram.) 305 */ 306 udpstat.udps_noport++; 307 udpstat.udps_noportmcast++; 308 goto bad; 309 } 310#ifdef IPSEC 311 /* 312 * Check AH/ESP integrity. 313 */ 314 if (ipsec6_in_reject_so(m, last->inp_socket)) { 315 ipsec6stat.in_polvio++; 316 goto bad; 317 } 318#endif /*IPSEC*/ 319 if (last->in6p_flags & IN6P_CONTROLOPTS 320 || last->in6p_socket->so_options & SO_TIMESTAMP) 321 ip6_savecontrol(last, &opts, ip6, m); 322 323 m_adj(m, off + sizeof(struct udphdr)); 324 if (sbappendaddr(&last->in6p_socket->so_rcv, 325 (struct sockaddr *)&udp_in6, 326 m, opts) == 0) { 327 udpstat.udps_fullsock++; 328 goto bad; 329 } 330 sorwakeup(last->in6p_socket); 331 return IPPROTO_DONE; 332 } 333 /* 334 * Locate pcb for datagram. 335 */ 336 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, 337 &ip6->ip6_dst, uh->uh_dport, 1, 338 m->m_pkthdr.rcvif); 339 if (in6p == 0) { 340 if (log_in_vain) { 341 char buf[INET6_ADDRSTRLEN]; 342 343 strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); 344 log(LOG_INFO, 345 "Connection attempt to UDP %s:%d from %s:%d\n", 346 buf, ntohs(uh->uh_dport), 347 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)); 348 } 349 udpstat.udps_noport++; 350 if (m->m_flags & M_MCAST) { 351 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 352 udpstat.udps_noportmcast++; 353 goto bad; 354 } 355 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 356 return IPPROTO_DONE; 357 } 358#ifdef IPSEC 359 /* 360 * Check AH/ESP integrity. 361 */ 362 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) { 363 ipsec6stat.in_polvio++; 364 goto bad; 365 } 366#endif /*IPSEC*/ 367 368 /* 369 * Construct sockaddr format source address. 370 * Stuff source address and datagram in user buffer. 371 */ 372 init_sin6(&udp_in6, m); /* general init */ 373 udp_in6.sin6_port = uh->uh_sport; 374 if (in6p->in6p_flags & IN6P_CONTROLOPTS 375 || in6p->in6p_socket->so_options & SO_TIMESTAMP) 376 ip6_savecontrol(in6p, &opts, ip6, m); 377 m_adj(m, off + sizeof(struct udphdr)); 378 if (sbappendaddr(&in6p->in6p_socket->so_rcv, 379 (struct sockaddr *)&udp_in6, 380 m, opts) == 0) { 381 udpstat.udps_fullsock++; 382 goto bad; 383 } 384 sorwakeup(in6p->in6p_socket); 385 return IPPROTO_DONE; 386bad: 387 if (m) 388 m_freem(m); 389 if (opts) 390 m_freem(opts); 391 return IPPROTO_DONE; 392} 393 394void 395udp6_ctlinput(cmd, sa, d) 396 int cmd; 397 struct sockaddr *sa; 398 void *d; 399{ 400 register struct udphdr *uhp; 401 struct udphdr uh; 402 struct sockaddr_in6 sa6; 403 struct ip6_hdr *ip6; 404 struct mbuf *m; 405 int off; 406 407 if (sa->sa_family != AF_INET6 || 408 sa->sa_len != sizeof(struct sockaddr_in6)) 409 return; 410 411 off = 0; 412 if (!PRC_IS_REDIRECT(cmd) && 413 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0)) 414 return; 415 416 /* if the parameter is from icmp6, decode it. */ 417 if (d != NULL) { 418 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 419 m = ip6cp->ip6c_m; 420 ip6 = ip6cp->ip6c_ip6; 421 off = ip6cp->ip6c_off; 422 } else { 423 m = NULL; 424 ip6 = NULL; 425 } 426 427 /* translate addresses into internal form */ 428 sa6 = *(struct sockaddr_in6 *)sa; 429 if (m != NULL && IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr)) 430 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 431 432 if (ip6) { 433 /* 434 * XXX: We assume that when IPV6 is non NULL, 435 * M and OFF are valid. 436 */ 437 struct in6_addr s; 438 439 /* translate addresses into internal form */ 440 memcpy(&s, &ip6->ip6_src, sizeof(s)); 441 if (IN6_IS_ADDR_LINKLOCAL(&s)) 442 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 443 444 if (m->m_len < off + sizeof(uh)) { 445 /* 446 * this should be rare case, 447 * so we compromise on this copy... 448 */ 449 m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 450 uhp = &uh; 451 } else 452 uhp = (struct udphdr *)(mtod(m, caddr_t) + off); 453 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 454 uhp->uh_dport, &s, 455 uhp->uh_sport, cmd, udp_notify); 456 } else 457 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0, 458 &zeroin6_addr, 0, cmd, udp_notify); 459} 460 461static int 462udp6_getcred SYSCTL_HANDLER_ARGS 463{ 464 struct sockaddr_in6 addrs[2]; 465 struct inpcb *inp; 466 int error, s; 467 468 error = suser(req->p); 469 if (error) 470 return (error); 471 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 472 if (error) 473 return (error); 474 s = splnet(); 475 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, 476 addrs[1].sin6_port, 477 &addrs[0].sin6_addr, addrs[0].sin6_port, 478 1, NULL); 479 if (!inp || !inp->inp_socket) { 480 error = ENOENT; 481 goto out; 482 } 483 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, 484 sizeof(struct ucred)); 485 486out: 487 splx(s); 488 return (error); 489} 490 491SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 492 0, 0, 493 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection"); 494 495int 496udp6_output(in6p, m, addr6, control, p) 497 register struct inpcb *in6p; 498 struct mbuf *m; 499 struct sockaddr *addr6; 500 struct mbuf *control; 501 struct proc *p; 502{ 503 register int ulen = m->m_pkthdr.len; 504 int plen = sizeof(struct udphdr) + ulen; 505 struct ip6_hdr *ip6; 506 struct udphdr *udp6; 507 struct in6_addr laddr6; 508 int s = 0, error = 0; 509 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts; 510 511 if (control) { 512 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0) 513 goto release; 514 in6p->in6p_outputopts = &opt; 515 } 516 517 if (addr6) { 518 laddr6 = in6p->in6p_laddr; 519 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 520 error = EISCONN; 521 goto release; 522 } 523 /* 524 * Must block input while temporarily connected. 525 */ 526 s = splnet(); 527 /* 528 * XXX: the user might want to overwrite the local address 529 * via an ancillary data. 530 */ 531 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr)); 532 error = in6_pcbconnect(in6p, addr6, p); 533 if (error) { 534 splx(s); 535 goto release; 536 } 537 } else { 538 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 539 error = ENOTCONN; 540 goto release; 541 } 542 } 543 /* 544 * Calculate data length and get a mbuf 545 * for UDP and IP6 headers. 546 */ 547 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr), 548 M_DONTWAIT); 549 if (m == 0) { 550 error = ENOBUFS; 551 if (addr6) 552 splx(s); 553 goto release; 554 } 555 556 /* 557 * Stuff checksum and output datagram. 558 */ 559 ip6 = mtod(m, struct ip6_hdr *); 560 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) | 561 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK); 562 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) | 563 (IPV6_VERSION & IPV6_VERSION_MASK); 564 /* ip6_plen will be filled in ip6_output. */ 565 ip6->ip6_nxt = IPPROTO_UDP; 566 ip6->ip6_hlim = in6_selecthlim(in6p, 567 in6p->in6p_route.ro_rt ? 568 in6p->in6p_route.ro_rt->rt_ifp : 569 NULL); 570 ip6->ip6_src = in6p->in6p_laddr; 571 ip6->ip6_dst = in6p->in6p_faddr; 572 573 udp6 = (struct udphdr *)(ip6 + 1); 574 udp6->uh_sport = in6p->in6p_lport; 575 udp6->uh_dport = in6p->in6p_fport; 576 udp6->uh_ulen = htons((u_short)plen); 577 udp6->uh_sum = 0; 578 579 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 580 sizeof(struct ip6_hdr), plen)) == 0) { 581 udp6->uh_sum = 0xffff; 582 } 583 584 udpstat.udps_opackets++; 585 586#ifdef IPSEC 587 m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket; 588#endif /*IPSEC*/ 589 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 590 IPV6_SOCKINMRCVIF, in6p->in6p_moptions, NULL); 591 592 if (addr6) { 593 in6_pcbdisconnect(in6p); 594 in6p->in6p_laddr = laddr6; 595 splx(s); 596 } 597 goto releaseopt; 598 599release: 600 m_freem(m); 601 602releaseopt: 603 if (control) { 604 in6p->in6p_outputopts = stickyopt; 605 m_freem(control); 606 } 607 return(error); 608} 609 610static int 611udp6_abort(struct socket *so) 612{ 613 struct inpcb *inp; 614 int s; 615 616 inp = sotoinpcb(so); 617 if (inp == 0) 618 return EINVAL; /* ??? possible? panic instead? */ 619 soisdisconnected(so); 620 s = splnet(); 621 in6_pcbdetach(inp); 622 splx(s); 623 return 0; 624} 625 626static int 627udp6_attach(struct socket *so, int proto, struct proc *p) 628{ 629 struct inpcb *inp; 630 int s, error; 631 632 inp = sotoinpcb(so); 633 if (inp != 0) 634 return EINVAL; 635 636 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 637 error = soreserve(so, udp_sendspace, udp_recvspace); 638 if (error) 639 return error; 640 } 641 s = splnet(); 642 error = in_pcballoc(so, &udbinfo, p); 643 splx(s); 644 if (error) 645 return error; 646 inp = (struct inpcb *)so->so_pcb; 647 inp->inp_vflag |= INP_IPV6; 648 inp->in6p_hops = -1; /* use kernel default */ 649 inp->in6p_cksum = -1; /* just to be sure */ 650 /* 651 * XXX: ugly!! 652 * IPv4 TTL initialization is necessary for an IPv6 socket as well, 653 * because the socket may be bound to an IPv6 wildcard address, 654 * which may match an IPv4-mapped IPv6 address. 655 */ 656 inp->inp_ip_ttl = ip_defttl; 657#ifdef IPSEC 658 error = ipsec_init_policy(so, &inp->in6p_sp); 659 if (error != 0) { 660 in6_pcbdetach(inp); 661 return (error); 662 } 663#endif /*IPSEC*/ 664 return 0; 665} 666 667static int 668udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 669{ 670 struct inpcb *inp; 671 int s, error; 672 673 inp = sotoinpcb(so); 674 if (inp == 0) 675 return EINVAL; 676 677 inp->inp_vflag &= ~INP_IPV4; 678 inp->inp_vflag |= INP_IPV6; 679 if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 680 struct sockaddr_in6 *sin6_p; 681 682 sin6_p = (struct sockaddr_in6 *)nam; 683 684 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) 685 inp->inp_vflag |= INP_IPV4; 686 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 687 struct sockaddr_in sin; 688 689 in6_sin6_2_sin(&sin, sin6_p); 690 inp->inp_vflag |= INP_IPV4; 691 inp->inp_vflag &= ~INP_IPV6; 692 s = splnet(); 693 error = in_pcbbind(inp, (struct sockaddr *)&sin, p); 694 splx(s); 695 return error; 696 } 697 } 698 699 s = splnet(); 700 error = in6_pcbbind(inp, nam, p); 701 splx(s); 702 return error; 703} 704 705static int 706udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 707{ 708 struct inpcb *inp; 709 int s, error; 710 711 inp = sotoinpcb(so); 712 if (inp == 0) 713 return EINVAL; 714 if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 715 struct sockaddr_in6 *sin6_p; 716 717 sin6_p = (struct sockaddr_in6 *)nam; 718 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { 719 struct sockaddr_in sin; 720 721 if (inp->inp_faddr.s_addr != INADDR_ANY) 722 return EISCONN; 723 in6_sin6_2_sin(&sin, sin6_p); 724 s = splnet(); 725 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p); 726 splx(s); 727 if (error == 0) { 728 inp->inp_vflag |= INP_IPV4; 729 inp->inp_vflag &= ~INP_IPV6; 730 soisconnected(so); 731 } 732 return error; 733 } 734 } 735 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 736 return EISCONN; 737 s = splnet(); 738 error = in6_pcbconnect(inp, nam, p); 739 splx(s); 740 if (error == 0) { 741 inp->inp_vflag &= ~INP_IPV4; 742 inp->inp_vflag |= INP_IPV6; 743 soisconnected(so); 744 } 745 return error; 746} 747 748static int 749udp6_detach(struct socket *so) 750{ 751 struct inpcb *inp; 752 int s; 753 754 inp = sotoinpcb(so); 755 if (inp == 0) 756 return EINVAL; 757 s = splnet(); 758 in6_pcbdetach(inp); 759 splx(s); 760 return 0; 761} 762 763static int 764udp6_disconnect(struct socket *so) 765{ 766 struct inpcb *inp; 767 int s; 768 769 inp = sotoinpcb(so); 770 if (inp == 0) 771 return EINVAL; 772 773 if (inp->inp_vflag & INP_IPV4) { 774 struct pr_usrreqs *pru; 775 776 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 777 return ((*pru->pru_disconnect)(so)); 778 } 779 780 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) 781 return ENOTCONN; 782 783 s = splnet(); 784 in6_pcbdisconnect(inp); 785 inp->in6p_laddr = in6addr_any; 786 splx(s); 787 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 788 return 0; 789} 790 791static int 792udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 793 struct mbuf *control, struct proc *p) 794{ 795 struct inpcb *inp; 796 797 inp = sotoinpcb(so); 798 if (inp == 0) { 799 m_freem(m); 800 return EINVAL; 801 } 802 803 if ((inp->inp_flags & IN6P_BINDV6ONLY) == 0) { 804 int hasv4addr; 805 struct sockaddr_in6 *sin6 = 0; 806 807 if (addr == 0) 808 hasv4addr = (inp->inp_vflag & INP_IPV4); 809 else { 810 sin6 = (struct sockaddr_in6 *)addr; 811 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) 812 ? 1 : 0; 813 } 814 if (hasv4addr) { 815 struct pr_usrreqs *pru; 816 int error; 817 818 if (sin6) 819 in6_sin6_2_sin_in_sock(addr); 820 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; 821 error = ((*pru->pru_send)(so, flags, m, addr, control, 822 p)); 823 /* addr will just be freed in sendit(). */ 824 return error; 825 } 826 } 827 828 return udp6_output(inp, m, addr, control, p); 829} 830 831struct pr_usrreqs udp6_usrreqs = { 832 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect, 833 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect, 834 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp, 835 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown, 836 in6_mapped_sockaddr, sosend, soreceive, sopoll 837}; 838