1 /* $NetBSD: udp6_usrreq.c,v 1.26 2000/03/01 12:49:50 itojun Exp $ */ 2 /* $KAME: udp6_usrreq.c,v 1.40 2000/02/28 15:44:13 itojun Exp $ */ 3 4 /* 5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the project nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1982, 1986, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 66 */ 67 68 #include "opt_ipsec.h" 69 70 #include <sys/param.h> 71 #include <sys/malloc.h> 72 #include <sys/mbuf.h> 73 #include <sys/protosw.h> 74 #include <sys/socket.h> 75 #include <sys/socketvar.h> 76 #include <sys/errno.h> 77 #include <sys/stat.h> 78 #include <sys/systm.h> 79 #include <sys/proc.h> 80 #include <sys/syslog.h> 81 82 #include <net/if.h> 83 #include <net/route.h> 84 #include <net/if_types.h> 85 86 #include <netinet/in.h> 87 #include <netinet/in_var.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/ip_var.h> 91 #include <netinet/in_pcb.h> 92 #include <netinet/udp.h> 93 #include <netinet/udp_var.h> 94 #include <netinet/ip6.h> 95 #include <netinet6/in6_pcb.h> 96 #include <netinet6/ip6_var.h> 97 #include <netinet/icmp6.h> 98 #include <netinet6/udp6_var.h> 99 #include <netinet6/ip6protosw.h> 100 101 #ifdef IPSEC 102 #include <netinet6/ipsec.h> 103 #endif /*IPSEC*/ 104 105 #include "faith.h" 106 107 /* 108 * UDP protocol inplementation. 109 * Per RFC 768, August, 1980. 110 */ 111 112 struct in6pcb *udp6_last_in6pcb = &udb6; 113 114 #ifdef UDP6 115 static int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *)); 116 #endif 117 static void udp6_detach __P((struct in6pcb *)); 118 static void udp6_notify __P((struct in6pcb *, int)); 119 120 void 121 udp6_init() 122 { 123 udb6.in6p_next = udb6.in6p_prev = &udb6; 124 } 125 126 #ifdef UDP6 127 static int 128 in6_mcmatch(in6p, ia6, ifp) 129 struct in6pcb *in6p; 130 register struct in6_addr *ia6; 131 struct ifnet *ifp; 132 { 133 struct ip6_moptions *im6o = in6p->in6p_moptions; 134 struct in6_multi_mship *imm; 135 136 if (im6o == NULL) 137 return 0; 138 139 for (imm = im6o->im6o_memberships.lh_first; imm != NULL; 140 imm = imm->i6mm_chain.le_next) { 141 if ((ifp == NULL || 142 imm->i6mm_maddr->in6m_ifp == ifp) && 143 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, 144 ia6)) 145 return 1; 146 } 147 return 0; 148 } 149 150 int 151 udp6_input(mp, offp, proto) 152 struct mbuf **mp; 153 int *offp, proto; 154 { 155 struct mbuf *m = *mp; 156 register struct ip6_hdr *ip6; 157 register struct udphdr *uh; 158 register struct in6pcb *in6p; 159 struct mbuf *opts = 0; 160 int off = *offp; 161 u_int32_t plen, ulen; 162 struct sockaddr_in6 udp_in6; 163 164 #if defined(NFAITH) && 0 < NFAITH 165 if (m->m_pkthdr.rcvif) { 166 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 167 /* send icmp6 host unreach? */ 168 m_freem(m); 169 return IPPROTO_DONE; 170 } 171 } 172 #endif 173 udp6stat.udp6s_ipackets++; 174 175 ip6 = mtod(m, struct ip6_hdr *); 176 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */ 177 plen = m->m_pkthdr.len - off; 178 #ifndef PULLDOWN_TEST 179 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); 180 uh = (struct udphdr *)((caddr_t)ip6 + off); 181 #else 182 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr)); 183 if (uh == NULL) { 184 udp6stat.udp6s_hdrops++; 185 return IPPROTO_DONE; 186 } 187 #endif 188 ulen = ntohs((u_short)uh->uh_ulen); 189 if (ulen == 0 && plen > 0xffff) /* jumbogram */ 190 ulen = plen; 191 192 if (plen != ulen) { 193 udp6stat.udp6s_badlen++; 194 goto bad; 195 } 196 197 /* destination port of 0 is illegal, based on RFC768. */ 198 if (uh->uh_dport == 0) 199 goto bad; 200 201 /* Be proactive about malicious use of IPv4 mapped address */ 202 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 203 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 204 /* XXX stat */ 205 goto bad; 206 } 207 208 /* 209 * Checksum extended UDP header and data. 210 */ 211 if (uh->uh_sum == 0) 212 udp6stat.udp6s_nosum++; 213 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 214 udp6stat.udp6s_badsum++; 215 goto bad; 216 } 217 218 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { 219 struct in6pcb *last; 220 221 /* 222 * Deliver a multicast datagram to all sockets 223 * for which the local and remote addresses and ports match 224 * those of the incoming datagram. This allows more than 225 * one process to receive multicasts on the same port. 226 * (This really ought to be done for unicast datagrams as 227 * well, but that would cause problems with existing 228 * applications that open both address-specific sockets and 229 * a wildcard socket listening to the same port -- they would 230 * end up receiving duplicates of every unicast datagram. 231 * Those applications open the multiple sockets to overcome an 232 * inadequacy of the UDP socket interface, but for backwards 233 * compatibility we avoid the problem here rather than 234 * fixing the interface. Maybe 4.5BSD will remedy this?) 235 */ 236 237 /* 238 * In a case that laddr should be set to the link-local 239 * address (this happens in RIPng), the multicast address 240 * specified in the received packet does not match with 241 * laddr. To cure this situation, the matching is relaxed 242 * if the receiving interface is the same as one specified 243 * in the socket and if the destination multicast address 244 * matches one of the multicast groups specified in the socket. 245 */ 246 247 /* 248 * Construct sockaddr format source address. 249 */ 250 bzero(&udp_in6, sizeof(udp_in6)); 251 udp_in6.sin6_len = sizeof(struct sockaddr_in6); 252 udp_in6.sin6_family = AF_INET6; 253 udp_in6.sin6_port = uh->uh_sport; 254 udp_in6.sin6_addr = ip6->ip6_src; 255 if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) 256 udp_in6.sin6_addr.s6_addr16[1] = 0; 257 if (m->m_pkthdr.rcvif) { 258 if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) { 259 udp_in6.sin6_scope_id = 260 m->m_pkthdr.rcvif->if_index; 261 } else 262 udp_in6.sin6_scope_id = 0; 263 } else 264 udp_in6.sin6_scope_id = 0; 265 /* 266 * KAME note: usually we drop udphdr from mbuf here. 267 * We need udphdr for IPsec processing so we do that later. 268 */ 269 270 /* 271 * Locate pcb(s) for datagram. 272 * (Algorithm copied from raw_intr().) 273 */ 274 last = NULL; 275 for (in6p = udb6.in6p_next; 276 in6p != &udb6; 277 in6p = in6p->in6p_next) { 278 if (in6p->in6p_lport != uh->uh_dport) 279 continue; 280 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 281 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 282 &ip6->ip6_dst) && 283 !in6_mcmatch(in6p, &ip6->ip6_dst, 284 m->m_pkthdr.rcvif)) 285 continue; 286 } 287 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 288 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 289 &ip6->ip6_src) || 290 in6p->in6p_fport != uh->uh_sport) 291 continue; 292 } 293 294 if (last != NULL) { 295 struct mbuf *n; 296 297 #ifdef IPSEC 298 /* 299 * Check AH/ESP integrity. 300 */ 301 if (ipsec6_in_reject(m, last)) { 302 ipsec6stat.in_polvio++; 303 /* do not inject data into pcb */ 304 } else 305 #endif /*IPSEC*/ 306 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 307 /* 308 * KAME NOTE: do not 309 * m_copy(m, offset, ...) above. 310 * sbappendaddr() expects M_PKTHDR, 311 * and m_copy() will copy M_PKTHDR 312 * only if offset is 0. 313 */ 314 if (last->in6p_flags & IN6P_CONTROLOPTS 315 || last->in6p_socket->so_options & SO_TIMESTAMP) { 316 ip6_savecontrol(last, &opts, 317 ip6, n); 318 } 319 320 m_adj(n, off + sizeof(struct udphdr)); 321 if (sbappendaddr(&last->in6p_socket->so_rcv, 322 (struct sockaddr *)&udp_in6, 323 n, opts) == 0) { 324 m_freem(n); 325 if (opts) 326 m_freem(opts); 327 udp6stat.udp6s_fullsock++; 328 } else 329 sorwakeup(last->in6p_socket); 330 opts = 0; 331 } 332 } 333 last = in6p; 334 /* 335 * Don't look for additional matches if this one does 336 * not have either the SO_REUSEPORT or SO_REUSEADDR 337 * socket options set. This heuristic avoids searching 338 * through all pcbs in the common case of a non-shared 339 * port. It assumes that an application will never 340 * clear these options after setting them. 341 */ 342 if ((last->in6p_socket->so_options & 343 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 344 break; 345 } 346 347 if (last == NULL) { 348 /* 349 * No matching pcb found; discard datagram. 350 * (No need to send an ICMP Port Unreachable 351 * for a broadcast or multicast datgram.) 352 */ 353 udp6stat.udp6s_noport++; 354 udp6stat.udp6s_noportmcast++; 355 goto bad; 356 } 357 #ifdef IPSEC 358 /* 359 * Check AH/ESP integrity. 360 */ 361 if (last != NULL && ipsec6_in_reject(m, last)) { 362 ipsec6stat.in_polvio++; 363 goto bad; 364 } 365 #endif /*IPSEC*/ 366 if (last->in6p_flags & IN6P_CONTROLOPTS 367 || last->in6p_socket->so_options & SO_TIMESTAMP) { 368 ip6_savecontrol(last, &opts, ip6, m); 369 } 370 371 m_adj(m, off + sizeof(struct udphdr)); 372 if (sbappendaddr(&last->in6p_socket->so_rcv, 373 (struct sockaddr *)&udp_in6, 374 m, opts) == 0) { 375 udp6stat.udp6s_fullsock++; 376 goto bad; 377 } 378 sorwakeup(last->in6p_socket); 379 return IPPROTO_DONE; 380 } 381 /* 382 * Locate pcb for datagram. 383 */ 384 in6p = udp6_last_in6pcb; 385 if (in6p->in6p_lport != uh->uh_dport || 386 in6p->in6p_fport != uh->uh_sport || 387 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) || 388 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) { 389 in6p = in6_pcblookup(&udb6, 390 &ip6->ip6_src, uh->uh_sport, 391 &ip6->ip6_dst, uh->uh_dport, 392 IN6PLOOKUP_WILDCARD); 393 if (in6p) 394 udp6_last_in6pcb = in6p; 395 udp6stat.udp6ps_pcbcachemiss++; 396 } 397 if (in6p == 0) { 398 udp6stat.udp6s_noport++; 399 if (m->m_flags & M_MCAST) { 400 printf("UDP6: M_MCAST is set in a unicast packet.\n"); 401 udp6stat.udp6s_noportmcast++; 402 goto bad; 403 } 404 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 405 return IPPROTO_DONE; 406 } 407 #ifdef IPSEC 408 /* 409 * Check AH/ESP integrity. 410 */ 411 if (in6p != NULL && ipsec6_in_reject(m, in6p)) { 412 ipsec6stat.in_polvio++; 413 goto bad; 414 } 415 #endif /*IPSEC*/ 416 417 /* 418 * Construct sockaddr format source address. 419 * Stuff source address and datagram in user buffer. 420 */ 421 bzero(&udp_in6, sizeof(udp_in6)); 422 udp_in6.sin6_len = sizeof(struct sockaddr_in6); 423 udp_in6.sin6_family = AF_INET6; 424 udp_in6.sin6_port = uh->uh_sport; 425 udp_in6.sin6_addr = ip6->ip6_src; 426 if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) 427 udp_in6.sin6_addr.s6_addr16[1] = 0; 428 if (m->m_pkthdr.rcvif) { 429 if (IN6_IS_SCOPE_LINKLOCAL(&udp_in6.sin6_addr)) 430 udp_in6.sin6_scope_id = m->m_pkthdr.rcvif->if_index; 431 else 432 udp_in6.sin6_scope_id = 0; 433 } else 434 udp_in6.sin6_scope_id = 0; 435 if (in6p->in6p_flags & IN6P_CONTROLOPTS 436 || in6p->in6p_socket->so_options & SO_TIMESTAMP) { 437 ip6_savecontrol(in6p, &opts, ip6, m); 438 } 439 440 m_adj(m, off + sizeof(struct udphdr)); 441 if (sbappendaddr(&in6p->in6p_socket->so_rcv, 442 (struct sockaddr *)&udp_in6, 443 m, opts) == 0) { 444 udp6stat.udp6s_fullsock++; 445 goto bad; 446 } 447 sorwakeup(in6p->in6p_socket); 448 return IPPROTO_DONE; 449 bad: 450 if (m) 451 m_freem(m); 452 if (opts) 453 m_freem(opts); 454 return IPPROTO_DONE; 455 } 456 #endif 457 458 /* 459 * Notify a udp user of an asynchronous error; 460 * just wake up so tat he can collect error status. 461 */ 462 static void 463 udp6_notify(in6p, errno) 464 register struct in6pcb *in6p; 465 int errno; 466 { 467 in6p->in6p_socket->so_error = errno; 468 sorwakeup(in6p->in6p_socket); 469 sowwakeup(in6p->in6p_socket); 470 } 471 472 void 473 udp6_ctlinput(cmd, sa, d) 474 int cmd; 475 struct sockaddr *sa; 476 void *d; 477 { 478 register struct udphdr *uhp; 479 struct udphdr uh; 480 struct sockaddr_in6 sa6; 481 register struct ip6_hdr *ip6; 482 struct mbuf *m; 483 int off; 484 void (*notify) __P((struct in6pcb *, int)) = udp6_notify; 485 486 if (sa->sa_family != AF_INET6 || 487 sa->sa_len != sizeof(struct sockaddr_in6)) 488 return; 489 490 if ((unsigned)cmd >= PRC_NCMDS) 491 return; 492 if (PRC_IS_REDIRECT(cmd)) 493 notify = in6_rtchange, d = NULL; 494 else if (cmd == PRC_HOSTDEAD) 495 d = NULL; 496 else if (inet6ctlerrmap[cmd] == 0) 497 return; 498 499 /* if the parameter is from icmp6, decode it. */ 500 if (d != NULL) { 501 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d; 502 m = ip6cp->ip6c_m; 503 ip6 = ip6cp->ip6c_ip6; 504 off = ip6cp->ip6c_off; 505 } else { 506 m = NULL; 507 ip6 = NULL; 508 } 509 510 /* translate addresses into internal form */ 511 sa6 = *(struct sockaddr_in6 *)sa; 512 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr) && m && m->m_pkthdr.rcvif) 513 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 514 515 if (ip6) { 516 /* 517 * XXX: We assume that when IPV6 is non NULL, 518 * M and OFF are valid. 519 */ 520 struct in6_addr s; 521 522 /* translate addresses into internal form */ 523 memcpy(&s, &ip6->ip6_src, sizeof(s)); 524 if (IN6_IS_ADDR_LINKLOCAL(&s)) 525 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index); 526 527 if (m->m_len < off + sizeof(uh)) { 528 /* 529 * this should be rare case, 530 * so we compromise on this copy... 531 */ 532 m_copydata(m, off, sizeof(uh), (caddr_t)&uh); 533 uhp = &uh; 534 } else 535 uhp = (struct udphdr *)(mtod(m, caddr_t) + off); 536 (void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6, 537 uhp->uh_dport, &s, 538 uhp->uh_sport, cmd, notify); 539 } else { 540 (void) in6_pcbnotify(&udb6, (struct sockaddr *)&sa6, 0, 541 &zeroin6_addr, 0, cmd, notify); 542 } 543 } 544 545 int 546 udp6_output(in6p, m, addr6, control) 547 register struct in6pcb *in6p; 548 register struct mbuf *m; 549 struct mbuf *addr6, *control; 550 { 551 register u_int32_t ulen = m->m_pkthdr.len; 552 u_int32_t plen = sizeof(struct udphdr) + ulen; 553 struct ip6_hdr *ip6; 554 struct udphdr *udp6; 555 struct in6_addr *laddr, *faddr; 556 u_short fport; 557 int error = 0; 558 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts; 559 int priv; 560 struct proc *p = curproc; /* XXX */ 561 int af, hlen; 562 #ifdef INET 563 struct ip *ip; 564 #endif 565 566 priv = 0; 567 if (p && !suser(p->p_ucred, &p->p_acflag)) 568 priv = 1; 569 if (control) { 570 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0) 571 goto release; 572 in6p->in6p_outputopts = &opt; 573 } 574 575 if (addr6) { 576 /* 577 * IPv4 version of udp_output calls in_pcbconnect in this case, 578 * which needs splnet and affects performance. 579 * Since we saw no essential reason for calling in_pcbconnect, 580 * we get rid of such kind of logic, and call in6_selectsrc 581 * and In6_pcbsetport in order to fill in the local address 582 * and the local port. 583 */ 584 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *); 585 586 if (addr6->m_len != sizeof(*sin6)) { 587 error = EINVAL; 588 goto release; 589 } 590 if (sin6->sin6_family != AF_INET6) { 591 error = EAFNOSUPPORT; 592 goto release; 593 } 594 if (sin6->sin6_port == 0) { 595 error = EADDRNOTAVAIL; 596 goto release; 597 } 598 599 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 600 error = EISCONN; 601 goto release; 602 } 603 604 faddr = &sin6->sin6_addr; 605 fport = sin6->sin6_port; /* allow 0 port */ 606 /* 607 * If the scope of the destination is link-local, 608 * embed the interface 609 * index in the address. 610 * 611 * XXX advanced-api value overrides sin6_scope_id 612 */ 613 if (IN6_IS_ADDR_LINKLOCAL(faddr) || 614 IN6_IS_ADDR_MC_LINKLOCAL(faddr)) { 615 struct ip6_pktopts *optp = in6p->in6p_outputopts; 616 struct in6_pktinfo *pi = NULL; 617 struct ifnet *oifp = NULL; 618 struct ip6_moptions *mopt = NULL; 619 620 /* 621 * XXX Boundary check is assumed to be already done in 622 * ip6_setpktoptions(). 623 */ 624 if (optp && (pi = optp->ip6po_pktinfo) && 625 pi->ipi6_ifindex) { 626 faddr->s6_addr16[1] = htons(pi->ipi6_ifindex); 627 oifp = ifindex2ifnet[pi->ipi6_ifindex]; 628 } 629 else if (IN6_IS_ADDR_MULTICAST(faddr) && 630 (mopt = in6p->in6p_moptions) && 631 mopt->im6o_multicast_ifp) { 632 oifp = mopt->im6o_multicast_ifp; 633 faddr->s6_addr16[1] = oifp->if_index; 634 } else if (sin6->sin6_scope_id) { 635 /* boundary check */ 636 if (sin6->sin6_scope_id < 0 637 || if_index < sin6->sin6_scope_id) { 638 error = ENXIO; /* XXX EINVAL? */ 639 goto release; 640 } 641 /* XXX */ 642 faddr->s6_addr16[1] = 643 htons(sin6->sin6_scope_id & 0xffff); 644 } 645 } 646 647 if (!IN6_IS_ADDR_V4MAPPED(faddr)) { 648 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts, 649 in6p->in6p_moptions, 650 &in6p->in6p_route, 651 &in6p->in6p_laddr, &error); 652 } else 653 laddr = &in6p->in6p_laddr; /*XXX*/ 654 if (laddr == NULL) { 655 if (error == 0) 656 error = EADDRNOTAVAIL; 657 goto release; 658 } 659 if (in6p->in6p_lport == 0 && 660 (error = in6_pcbsetport(laddr, in6p)) != 0) 661 goto release; 662 } else { 663 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 664 error = ENOTCONN; 665 goto release; 666 } 667 laddr = &in6p->in6p_laddr; 668 faddr = &in6p->in6p_faddr; 669 fport = in6p->in6p_fport; 670 } 671 672 if (!IN6_IS_ADDR_V4MAPPED(faddr)) { 673 af = AF_INET6; 674 hlen = sizeof(struct ip6_hdr); 675 } else { 676 af = AF_INET; 677 hlen = sizeof(struct ip); 678 } 679 680 /* 681 * Calculate data length and get a mbuf 682 * for UDP and IP6 headers. 683 */ 684 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 685 if (m == 0) { 686 error = ENOBUFS; 687 goto release; 688 } 689 690 /* 691 * Stuff checksum and output datagram. 692 */ 693 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 694 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ 695 udp6->uh_dport = fport; 696 if (plen <= 0xffff) 697 udp6->uh_ulen = htons((u_short)plen); 698 else 699 udp6->uh_ulen = 0; 700 udp6->uh_sum = 0; 701 702 switch (af) { 703 case AF_INET6: 704 ip6 = mtod(m, struct ip6_hdr *); 705 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 706 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 707 ip6->ip6_vfc |= IPV6_VERSION; 708 #if 0 /* ip6_plen will be filled in ip6_output. */ 709 ip6->ip6_plen = htons((u_short)plen); 710 #endif 711 ip6->ip6_nxt = IPPROTO_UDP; 712 ip6->ip6_hlim = in6_selecthlim(in6p, 713 in6p->in6p_route.ro_rt ? 714 in6p->in6p_route.ro_rt->rt_ifp : NULL); 715 ip6->ip6_src = *laddr; 716 ip6->ip6_dst = *faddr; 717 718 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 719 sizeof(struct ip6_hdr), plen)) == 0) { 720 udp6->uh_sum = 0xffff; 721 } 722 723 udp6stat.udp6s_opackets++; 724 #ifdef IPSEC 725 ipsec_setsocket(m, in6p->in6p_socket); 726 #endif /*IPSEC*/ 727 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 728 0, in6p->in6p_moptions, NULL); 729 break; 730 case AF_INET: 731 #ifdef INET 732 /* can't transmit jumbogram over IPv4 */ 733 if (plen > 0xffff) { 734 error = EMSGSIZE; 735 goto release; 736 } 737 738 ip = mtod(m, struct ip *); 739 740 ip->ip_len = plen; 741 ip->ip_p = IPPROTO_UDP; 742 ip->ip_ttl = in6p->in6p_hops; /*XXX*/ 743 ip->ip_tos = 0; /*XXX*/ 744 bcopy(&laddr->s6_addr[12], &ip->ip_src, sizeof(ip->ip_src)); 745 bcopy(&faddr->s6_addr[12], &ip->ip_dst, sizeof(ip->ip_dst)); 746 747 udp6->uh_sum = 0; 748 if ((udp6->uh_sum = in_cksum(m, ulen)) == 0) 749 udp6->uh_sum = 0xffff; 750 751 udpstat.udps_opackets++; 752 #ifdef IPSEC 753 ipsec_setsocket(m, NULL); /*XXX*/ 754 #endif /*IPSEC*/ 755 error = ip_output(m, NULL, &in6p->in6p_route, 0 /*XXX*/); 756 break; 757 #else 758 error = EAFNOSUPPORT; 759 goto release; 760 #endif 761 } 762 goto releaseopt; 763 764 release: 765 m_freem(m); 766 767 releaseopt: 768 if (control) { 769 in6p->in6p_outputopts = stickyopt; 770 m_freem(control); 771 } 772 return(error); 773 } 774 775 extern int udp6_sendspace; 776 extern int udp6_recvspace; 777 778 int 779 udp6_usrreq(so, req, m, addr6, control, p) 780 struct socket *so; 781 int req; 782 struct mbuf *m, *addr6, *control; 783 struct proc *p; 784 { 785 struct in6pcb *in6p = sotoin6pcb(so); 786 int error = 0; 787 int s; 788 789 /* 790 * MAPPED_ADDR implementation info: 791 * Mapped addr support for PRU_CONTROL is not necessary. 792 * Because typical user of PRU_CONTROL is such as ifconfig, 793 * and they don't associate any addr to their socket. Then 794 * socket family is only hint about the PRU_CONTROL'ed address 795 * family, especially when getting addrs from kernel. 796 * So AF_INET socket need to be used to control AF_INET addrs, 797 * and AF_INET6 socket for AF_INET6 addrs. 798 */ 799 if (req == PRU_CONTROL) 800 return(in6_control(so, (u_long)m, (caddr_t)addr6, 801 (struct ifnet *)control, p)); 802 803 if (req == PRU_PURGEIF) { 804 in6_purgeif((struct ifnet *)control); 805 in6_pcbpurgeif(&udb6, (struct ifnet *)control); 806 return (0); 807 } 808 809 if (in6p == NULL && req != PRU_ATTACH) { 810 error = EINVAL; 811 goto release; 812 } 813 814 switch (req) { 815 case PRU_ATTACH: 816 /* 817 * MAPPED_ADDR implementation spec: 818 * Always attach for IPv6, 819 * and only when necessary for IPv4. 820 */ 821 if (in6p != NULL) { 822 error = EINVAL; 823 break; 824 } 825 s = splsoftnet(); 826 error = in6_pcballoc(so, &udb6); 827 splx(s); 828 if (error) 829 break; 830 error = soreserve(so, udp6_sendspace, udp6_recvspace); 831 if (error) 832 break; 833 in6p = sotoin6pcb(so); 834 in6p->in6p_cksum = -1; /* just to be sure */ 835 #ifdef IPSEC 836 error = ipsec_init_policy(so, &in6p->in6p_sp); 837 if (error != 0) { 838 in6_pcbdetach(in6p); 839 break; 840 } 841 #endif /*IPSEC*/ 842 break; 843 844 case PRU_DETACH: 845 udp6_detach(in6p); 846 break; 847 848 case PRU_BIND: 849 s = splsoftnet(); 850 error = in6_pcbbind(in6p, addr6); 851 splx(s); 852 break; 853 854 case PRU_LISTEN: 855 error = EOPNOTSUPP; 856 break; 857 858 case PRU_CONNECT: 859 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 860 error = EISCONN; 861 break; 862 } 863 s = splsoftnet(); 864 error = in6_pcbconnect(in6p, addr6); 865 if (ip6_auto_flowlabel) { 866 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 867 in6p->in6p_flowinfo |= 868 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 869 } 870 splx(s); 871 if (error == 0) 872 soisconnected(so); 873 break; 874 875 case PRU_CONNECT2: 876 error = EOPNOTSUPP; 877 break; 878 879 case PRU_ACCEPT: 880 error = EOPNOTSUPP; 881 break; 882 883 case PRU_DISCONNECT: 884 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 885 error = ENOTCONN; 886 break; 887 } 888 s = splsoftnet(); 889 in6_pcbdisconnect(in6p); 890 bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr)); 891 splx(s); 892 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 893 break; 894 895 case PRU_SHUTDOWN: 896 socantsendmore(so); 897 break; 898 899 case PRU_SEND: 900 return(udp6_output(in6p, m, addr6, control)); 901 902 case PRU_ABORT: 903 soisdisconnected(so); 904 udp6_detach(in6p); 905 break; 906 907 case PRU_SOCKADDR: 908 in6_setsockaddr(in6p, addr6); 909 break; 910 911 case PRU_PEERADDR: 912 in6_setpeeraddr(in6p, addr6); 913 break; 914 915 case PRU_SENSE: 916 /* 917 * stat: don't bother with a blocksize 918 */ 919 return(0); 920 921 case PRU_SENDOOB: 922 case PRU_FASTTIMO: 923 case PRU_SLOWTIMO: 924 case PRU_PROTORCV: 925 case PRU_PROTOSEND: 926 error = EOPNOTSUPP; 927 break; 928 929 case PRU_RCVD: 930 case PRU_RCVOOB: 931 return(EOPNOTSUPP); /* do not free mbuf's */ 932 933 default: 934 panic("udp6_usrreq"); 935 } 936 937 release: 938 if (control) { 939 printf("udp control data unexpectedly retained\n"); 940 m_freem(control); 941 } 942 if (m) 943 m_freem(m); 944 return(error); 945 } 946 947 static void 948 udp6_detach(in6p) 949 struct in6pcb *in6p; 950 { 951 int s = splsoftnet(); 952 953 if (in6p == udp6_last_in6pcb) 954 udp6_last_in6pcb = &udb6; 955 in6_pcbdetach(in6p); 956 splx(s); 957 } 958 959 #include <vm/vm.h> 960 #include <sys/sysctl.h> 961 962 int 963 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 964 int *name; 965 u_int namelen; 966 void *oldp; 967 size_t *oldlenp; 968 void *newp; 969 size_t newlen; 970 { 971 /* All sysctl names at this level are terminal. */ 972 if (namelen != 1) 973 return ENOTDIR; 974 975 switch (name[0]) { 976 977 case UDP6CTL_SENDMAX: 978 return sysctl_int(oldp, oldlenp, newp, newlen, 979 &udp6_sendspace); 980 case UDP6CTL_RECVSPACE: 981 return sysctl_int(oldp, oldlenp, newp, newlen, 982 &udp6_recvspace); 983 default: 984 return ENOPROTOOPT; 985 } 986 /* NOTREACHED */ 987 } 988