| 1 | /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.13 2003/01/24 05:11:35 sam Exp $ */ |
| 2 | /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei 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. Neither the name of the University nor the names of its contributors |
| 46 | * may be used to endorse or promote products derived from this software |
| 47 | * without specific prior written permission. |
| 48 | * |
| 49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 59 | * SUCH DAMAGE. |
| 60 | * |
| 61 | * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 |
| 62 | */ |
| 63 | |
| 64 | #include "opt_inet.h" |
| 65 | #include "opt_inet6.h" |
| 66 | #include "opt_ipsec.h" |
| 67 | |
| 68 | #include <sys/param.h> |
| 69 | #include <sys/kernel.h> |
| 70 | #include <sys/mbuf.h> |
| 71 | #include <sys/protosw.h> |
| 72 | #include <sys/socket.h> |
| 73 | #include <sys/socketvar.h> |
| 74 | #include <sys/sysctl.h> |
| 75 | #include <sys/errno.h> |
| 76 | #include <sys/stat.h> |
| 77 | #include <sys/systm.h> |
| 78 | #include <sys/syslog.h> |
| 79 | #include <sys/proc.h> |
| 80 | #include <sys/priv.h> |
| 81 | |
| 82 | #include <sys/thread2.h> |
| 83 | #include <sys/socketvar2.h> |
| 84 | #include <sys/msgport2.h> |
| 85 | |
| 86 | #include <net/if.h> |
| 87 | #include <net/route.h> |
| 88 | #include <net/if_types.h> |
| 89 | #include <net/netisr2.h> |
| 90 | |
| 91 | #include <netinet/in.h> |
| 92 | #include <netinet/in_systm.h> |
| 93 | #include <netinet/ip.h> |
| 94 | #include <netinet/in_pcb.h> |
| 95 | #include <netinet/in_var.h> |
| 96 | #include <netinet/ip_var.h> |
| 97 | #include <netinet/udp.h> |
| 98 | #include <netinet/udp_var.h> |
| 99 | #include <netinet/ip6.h> |
| 100 | #include <netinet6/ip6_var.h> |
| 101 | #include <netinet6/in6_pcb.h> |
| 102 | #include <netinet/icmp6.h> |
| 103 | #include <netinet6/udp6_var.h> |
| 104 | #include <netinet6/ip6protosw.h> |
| 105 | |
| 106 | #ifdef IPSEC |
| 107 | #include <netinet6/ipsec.h> |
| 108 | #include <netinet6/ipsec6.h> |
| 109 | #endif /* IPSEC */ |
| 110 | |
| 111 | #ifdef FAST_IPSEC |
| 112 | #include <netproto/ipsec/ipsec.h> |
| 113 | #include <netproto/ipsec/ipsec6.h> |
| 114 | #endif /* FAST_IPSEC */ |
| 115 | |
| 116 | /* |
| 117 | * UDP protocol inplementation. |
| 118 | * Per RFC 768, August, 1980. |
| 119 | */ |
| 120 | |
| 121 | extern struct protosw inetsw[]; |
| 122 | static int in6_mcmatch (struct inpcb *, struct in6_addr *, struct ifnet *); |
| 123 | |
| 124 | static int |
| 125 | in6_mcmatch(struct inpcb *in6p, struct in6_addr *ia6, struct ifnet *ifp) |
| 126 | { |
| 127 | struct ip6_moptions *im6o = in6p->in6p_moptions; |
| 128 | struct in6_multi_mship *imm; |
| 129 | |
| 130 | if (im6o == NULL) |
| 131 | return 0; |
| 132 | |
| 133 | for (imm = im6o->im6o_memberships.lh_first; imm != NULL; |
| 134 | imm = imm->i6mm_chain.le_next) { |
| 135 | if ((ifp == NULL || |
| 136 | imm->i6mm_maddr->in6m_ifp == ifp) && |
| 137 | IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr, |
| 138 | ia6)) |
| 139 | return 1; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | udp6_input(struct mbuf **mp, int *offp, int proto) |
| 146 | { |
| 147 | struct mbuf *m = *mp; |
| 148 | struct ip6_hdr *ip6; |
| 149 | struct udphdr *uh; |
| 150 | struct inpcb *in6p; |
| 151 | struct mbuf *opts = NULL; |
| 152 | int off = *offp; |
| 153 | int plen, ulen; |
| 154 | struct sockaddr_in6 udp_in6; |
| 155 | struct socket *so; |
| 156 | |
| 157 | IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE); |
| 158 | |
| 159 | ip6 = mtod(m, struct ip6_hdr *); |
| 160 | |
| 161 | if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) { |
| 162 | /* XXX send icmp6 host/port unreach? */ |
| 163 | m_freem(m); |
| 164 | return IPPROTO_DONE; |
| 165 | } |
| 166 | |
| 167 | udp_stat.udps_ipackets++; |
| 168 | |
| 169 | plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); |
| 170 | uh = (struct udphdr *)((caddr_t)ip6 + off); |
| 171 | ulen = ntohs((u_short)uh->uh_ulen); |
| 172 | |
| 173 | if (plen != ulen) { |
| 174 | udp_stat.udps_badlen++; |
| 175 | goto bad; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Checksum extended UDP header and data. |
| 180 | */ |
| 181 | if (uh->uh_sum == 0) |
| 182 | udp_stat.udps_nosum++; |
| 183 | else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { |
| 184 | udp_stat.udps_badsum++; |
| 185 | goto bad; |
| 186 | } |
| 187 | |
| 188 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
| 189 | struct inpcb *last; |
| 190 | |
| 191 | /* |
| 192 | * Deliver a multicast datagram to all sockets |
| 193 | * for which the local and remote addresses and ports match |
| 194 | * those of the incoming datagram. This allows more than |
| 195 | * one process to receive multicasts on the same port. |
| 196 | * (This really ought to be done for unicast datagrams as |
| 197 | * well, but that would cause problems with existing |
| 198 | * applications that open both address-specific sockets and |
| 199 | * a wildcard socket listening to the same port -- they would |
| 200 | * end up receiving duplicates of every unicast datagram. |
| 201 | * Those applications open the multiple sockets to overcome an |
| 202 | * inadequacy of the UDP socket interface, but for backwards |
| 203 | * compatibility we avoid the problem here rather than |
| 204 | * fixing the interface. Maybe 4.5BSD will remedy this?) |
| 205 | */ |
| 206 | |
| 207 | /* |
| 208 | * In a case that laddr should be set to the link-local |
| 209 | * address (this happens in RIPng), the multicast address |
| 210 | * specified in the received packet does not match with |
| 211 | * laddr. To cure this situation, the matching is relaxed |
| 212 | * if the receiving interface is the same as one specified |
| 213 | * in the socket and if the destination multicast address |
| 214 | * matches one of the multicast groups specified in the socket. |
| 215 | */ |
| 216 | |
| 217 | /* |
| 218 | * Construct sockaddr format source address. |
| 219 | */ |
| 220 | init_sin6(&udp_in6, m); /* general init */ |
| 221 | udp_in6.sin6_port = uh->uh_sport; |
| 222 | /* |
| 223 | * KAME note: traditionally we dropped udpiphdr from mbuf here. |
| 224 | * We need udphdr for IPsec processing so we do that later. |
| 225 | */ |
| 226 | |
| 227 | /* |
| 228 | * Locate pcb(s) for datagram. |
| 229 | * (Algorithm copied from raw_intr().) |
| 230 | */ |
| 231 | last = NULL; |
| 232 | LIST_FOREACH(in6p, &udbinfo.pcblisthead, inp_list) { |
| 233 | KKASSERT((in6p->inp_flags & INP_PLACEMARKER) == 0); |
| 234 | |
| 235 | if (!(in6p->inp_vflag & INP_IPV6)) |
| 236 | continue; |
| 237 | if (in6p->in6p_lport != uh->uh_dport) |
| 238 | continue; |
| 239 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { |
| 240 | if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, |
| 241 | &ip6->ip6_dst) && |
| 242 | !in6_mcmatch(in6p, &ip6->ip6_dst, |
| 243 | m->m_pkthdr.rcvif)) |
| 244 | continue; |
| 245 | } |
| 246 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { |
| 247 | if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, |
| 248 | &ip6->ip6_src) || |
| 249 | in6p->in6p_fport != uh->uh_sport) |
| 250 | continue; |
| 251 | } |
| 252 | |
| 253 | if (last != NULL) { |
| 254 | struct mbuf *n; |
| 255 | |
| 256 | #ifdef IPSEC |
| 257 | /* |
| 258 | * Check AH/ESP integrity. |
| 259 | */ |
| 260 | if (ipsec6_in_reject_so(m, last->inp_socket)) |
| 261 | ipsec6stat.in_polvio++; |
| 262 | /* do not inject data into pcb */ |
| 263 | else |
| 264 | #endif /* IPSEC */ |
| 265 | #ifdef FAST_IPSEC |
| 266 | /* |
| 267 | * Check AH/ESP integrity. |
| 268 | */ |
| 269 | if (ipsec6_in_reject(m, last)) |
| 270 | ; |
| 271 | else |
| 272 | #endif /* FAST_IPSEC */ |
| 273 | if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { |
| 274 | /* |
| 275 | * KAME NOTE: do not |
| 276 | * m_copy(m, offset, ...) above. |
| 277 | * ssb_appendaddr() expects M_PKTHDR, |
| 278 | * and m_copy() will copy M_PKTHDR |
| 279 | * only if offset is 0. |
| 280 | */ |
| 281 | so = last->in6p_socket; |
| 282 | if ((last->in6p_flags & IN6P_CONTROLOPTS) || |
| 283 | (so->so_options & SO_TIMESTAMP)) { |
| 284 | ip6_savecontrol(last, &opts, |
| 285 | ip6, n); |
| 286 | } |
| 287 | m_adj(n, off + sizeof(struct udphdr)); |
| 288 | lwkt_gettoken(&so->so_rcv.ssb_token); |
| 289 | if (ssb_appendaddr(&so->so_rcv, |
| 290 | (struct sockaddr *)&udp_in6, |
| 291 | n, opts) == 0) { |
| 292 | m_freem(n); |
| 293 | if (opts) |
| 294 | m_freem(opts); |
| 295 | udp_stat.udps_fullsock++; |
| 296 | } else { |
| 297 | sorwakeup(so); |
| 298 | } |
| 299 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 300 | opts = NULL; |
| 301 | } |
| 302 | } |
| 303 | last = in6p; |
| 304 | /* |
| 305 | * Don't look for additional matches if this one does |
| 306 | * not have either the SO_REUSEPORT or SO_REUSEADDR |
| 307 | * socket options set. This heuristic avoids searching |
| 308 | * through all pcbs in the common case of a non-shared |
| 309 | * port. It assumes that an application will never |
| 310 | * clear these options after setting them. |
| 311 | */ |
| 312 | if ((last->in6p_socket->so_options & |
| 313 | (SO_REUSEPORT | SO_REUSEADDR)) == 0) |
| 314 | break; |
| 315 | } |
| 316 | |
| 317 | if (last == NULL) { |
| 318 | /* |
| 319 | * No matching pcb found; discard datagram. |
| 320 | * (No need to send an ICMP Port Unreachable |
| 321 | * for a broadcast or multicast datgram.) |
| 322 | */ |
| 323 | udp_stat.udps_noport++; |
| 324 | udp_stat.udps_noportmcast++; |
| 325 | goto bad; |
| 326 | } |
| 327 | #ifdef IPSEC |
| 328 | /* |
| 329 | * Check AH/ESP integrity. |
| 330 | */ |
| 331 | if (ipsec6_in_reject_so(m, last->inp_socket)) { |
| 332 | ipsec6stat.in_polvio++; |
| 333 | goto bad; |
| 334 | } |
| 335 | #endif /* IPSEC */ |
| 336 | #ifdef FAST_IPSEC |
| 337 | /* |
| 338 | * Check AH/ESP integrity. |
| 339 | */ |
| 340 | if (ipsec6_in_reject(m, last)) { |
| 341 | goto bad; |
| 342 | } |
| 343 | #endif /* FAST_IPSEC */ |
| 344 | if (last->in6p_flags & IN6P_CONTROLOPTS |
| 345 | || last->in6p_socket->so_options & SO_TIMESTAMP) |
| 346 | ip6_savecontrol(last, &opts, ip6, m); |
| 347 | |
| 348 | m_adj(m, off + sizeof(struct udphdr)); |
| 349 | so = last->in6p_socket; |
| 350 | lwkt_gettoken(&so->so_rcv.ssb_token); |
| 351 | if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6, |
| 352 | m, opts) == 0) { |
| 353 | udp_stat.udps_fullsock++; |
| 354 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 355 | goto bad; |
| 356 | } |
| 357 | sorwakeup(so); |
| 358 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 359 | return IPPROTO_DONE; |
| 360 | } |
| 361 | /* |
| 362 | * Locate pcb for datagram. |
| 363 | */ |
| 364 | in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, |
| 365 | &ip6->ip6_dst, uh->uh_dport, 1, |
| 366 | m->m_pkthdr.rcvif); |
| 367 | if (in6p == NULL) { |
| 368 | if (log_in_vain) { |
| 369 | char buf[INET6_ADDRSTRLEN]; |
| 370 | |
| 371 | strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); |
| 372 | log(LOG_INFO, |
| 373 | "Connection attempt to UDP [%s]:%d from [%s]:%d\n", |
| 374 | buf, ntohs(uh->uh_dport), |
| 375 | ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport)); |
| 376 | } |
| 377 | udp_stat.udps_noport++; |
| 378 | if (m->m_flags & M_MCAST) { |
| 379 | kprintf("UDP6: M_MCAST is set in a unicast packet.\n"); |
| 380 | udp_stat.udps_noportmcast++; |
| 381 | goto bad; |
| 382 | } |
| 383 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); |
| 384 | return IPPROTO_DONE; |
| 385 | } |
| 386 | #ifdef IPSEC |
| 387 | /* |
| 388 | * Check AH/ESP integrity. |
| 389 | */ |
| 390 | if (ipsec6_in_reject_so(m, in6p->in6p_socket)) { |
| 391 | ipsec6stat.in_polvio++; |
| 392 | goto bad; |
| 393 | } |
| 394 | #endif /* IPSEC */ |
| 395 | #ifdef FAST_IPSEC |
| 396 | /* |
| 397 | * Check AH/ESP integrity. |
| 398 | */ |
| 399 | if (ipsec6_in_reject(m, in6p)) { |
| 400 | goto bad; |
| 401 | } |
| 402 | #endif /* FAST_IPSEC */ |
| 403 | |
| 404 | /* |
| 405 | * Construct sockaddr format source address. |
| 406 | * Stuff source address and datagram in user buffer. |
| 407 | */ |
| 408 | init_sin6(&udp_in6, m); /* general init */ |
| 409 | udp_in6.sin6_port = uh->uh_sport; |
| 410 | if (in6p->in6p_flags & IN6P_CONTROLOPTS |
| 411 | || in6p->in6p_socket->so_options & SO_TIMESTAMP) |
| 412 | ip6_savecontrol(in6p, &opts, ip6, m); |
| 413 | m_adj(m, off + sizeof(struct udphdr)); |
| 414 | so = in6p->in6p_socket; |
| 415 | lwkt_gettoken(&so->so_rcv.ssb_token); |
| 416 | if (ssb_appendaddr(&so->so_rcv, (struct sockaddr *)&udp_in6, |
| 417 | m, opts) == 0) { |
| 418 | udp_stat.udps_fullsock++; |
| 419 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 420 | goto bad; |
| 421 | } |
| 422 | sorwakeup(so); |
| 423 | lwkt_reltoken(&so->so_rcv.ssb_token); |
| 424 | return IPPROTO_DONE; |
| 425 | bad: |
| 426 | if (m) |
| 427 | m_freem(m); |
| 428 | if (opts) |
| 429 | m_freem(opts); |
| 430 | return IPPROTO_DONE; |
| 431 | } |
| 432 | |
| 433 | void |
| 434 | udp6_ctlinput(netmsg_t msg) |
| 435 | { |
| 436 | int cmd = msg->ctlinput.nm_cmd; |
| 437 | struct sockaddr *sa = msg->ctlinput.nm_arg; |
| 438 | void *d = msg->ctlinput.nm_extra; |
| 439 | struct udphdr uh; |
| 440 | struct ip6_hdr *ip6; |
| 441 | struct mbuf *m; |
| 442 | int off = 0; |
| 443 | struct ip6ctlparam *ip6cp = NULL; |
| 444 | const struct sockaddr_in6 *sa6_src = NULL; |
| 445 | void (*notify) (struct inpcb *, int) = udp_notify; |
| 446 | struct udp_portonly { |
| 447 | u_int16_t uh_sport; |
| 448 | u_int16_t uh_dport; |
| 449 | } *uhp; |
| 450 | |
| 451 | if (sa->sa_family != AF_INET6 || |
| 452 | sa->sa_len != sizeof(struct sockaddr_in6)) |
| 453 | goto out; |
| 454 | |
| 455 | if ((unsigned)cmd >= PRC_NCMDS) |
| 456 | goto out; |
| 457 | if (PRC_IS_REDIRECT(cmd)) |
| 458 | notify = in6_rtchange, d = NULL; |
| 459 | else if (cmd == PRC_HOSTDEAD) |
| 460 | d = NULL; |
| 461 | else if (inet6ctlerrmap[cmd] == 0) |
| 462 | goto out; |
| 463 | |
| 464 | /* if the parameter is from icmp6, decode it. */ |
| 465 | if (d != NULL) { |
| 466 | ip6cp = (struct ip6ctlparam *)d; |
| 467 | m = ip6cp->ip6c_m; |
| 468 | ip6 = ip6cp->ip6c_ip6; |
| 469 | off = ip6cp->ip6c_off; |
| 470 | sa6_src = ip6cp->ip6c_src; |
| 471 | } else { |
| 472 | m = NULL; |
| 473 | ip6 = NULL; |
| 474 | sa6_src = &sa6_any; |
| 475 | } |
| 476 | |
| 477 | if (ip6) { |
| 478 | /* |
| 479 | * XXX: We assume that when IPV6 is non NULL, |
| 480 | * M and OFF are valid. |
| 481 | */ |
| 482 | |
| 483 | /* check if we can safely examine src and dst ports */ |
| 484 | if (m->m_pkthdr.len < off + sizeof(*uhp)) |
| 485 | return; |
| 486 | |
| 487 | bzero(&uh, sizeof(uh)); |
| 488 | m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); |
| 489 | |
| 490 | in6_pcbnotify(&udbinfo.pcblisthead, sa, uh.uh_dport, |
| 491 | (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, |
| 492 | cmd, 0, notify); |
| 493 | } else { |
| 494 | in6_pcbnotify(&udbinfo.pcblisthead, sa, 0, |
| 495 | (const struct sockaddr *)sa6_src, 0, |
| 496 | cmd, 0, notify); |
| 497 | } |
| 498 | out: |
| 499 | lwkt_replymsg(&msg->ctlinput.base.lmsg, 0); |
| 500 | } |
| 501 | |
| 502 | static int |
| 503 | udp6_getcred(SYSCTL_HANDLER_ARGS) |
| 504 | { |
| 505 | struct sockaddr_in6 addrs[2]; |
| 506 | struct inpcb *inp; |
| 507 | int error; |
| 508 | |
| 509 | error = priv_check(req->td, PRIV_ROOT); |
| 510 | if (error) |
| 511 | return (error); |
| 512 | |
| 513 | if (req->newlen != sizeof(addrs)) |
| 514 | return (EINVAL); |
| 515 | if (req->oldlen != sizeof(struct ucred)) |
| 516 | return (EINVAL); |
| 517 | error = SYSCTL_IN(req, addrs, sizeof(addrs)); |
| 518 | if (error) |
| 519 | return (error); |
| 520 | crit_enter(); |
| 521 | inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr, |
| 522 | addrs[1].sin6_port, |
| 523 | &addrs[0].sin6_addr, addrs[0].sin6_port, |
| 524 | 1, NULL); |
| 525 | if (!inp || !inp->inp_socket) { |
| 526 | error = ENOENT; |
| 527 | goto out; |
| 528 | } |
| 529 | error = SYSCTL_OUT(req, inp->inp_socket->so_cred, |
| 530 | sizeof(struct ucred)); |
| 531 | |
| 532 | out: |
| 533 | crit_exit(); |
| 534 | return (error); |
| 535 | } |
| 536 | |
| 537 | SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, |
| 538 | 0, 0, |
| 539 | udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection"); |
| 540 | |
| 541 | /* |
| 542 | * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort() |
| 543 | * will sofree() it when we return. |
| 544 | */ |
| 545 | static void |
| 546 | udp6_abort(netmsg_t msg) |
| 547 | { |
| 548 | struct socket *so = msg->abort.base.nm_so; |
| 549 | struct inpcb *inp; |
| 550 | int error; |
| 551 | |
| 552 | inp = so->so_pcb; |
| 553 | if (inp) { |
| 554 | soisdisconnected(so); |
| 555 | |
| 556 | udbinfo_barrier_set(); |
| 557 | in6_pcbdetach(inp); |
| 558 | udbinfo_barrier_rem(); |
| 559 | error = 0; |
| 560 | } else { |
| 561 | error = EINVAL; |
| 562 | } |
| 563 | lwkt_replymsg(&msg->abort.base.lmsg, error); |
| 564 | } |
| 565 | |
| 566 | static void |
| 567 | udp6_attach(netmsg_t msg) |
| 568 | { |
| 569 | struct socket *so = msg->attach.base.nm_so; |
| 570 | struct pru_attach_info *ai = msg->attach.nm_ai; |
| 571 | struct inpcb *inp; |
| 572 | int error; |
| 573 | |
| 574 | inp = so->so_pcb; |
| 575 | if (inp != NULL) { |
| 576 | error = EINVAL; |
| 577 | goto out; |
| 578 | } |
| 579 | |
| 580 | if (so->so_snd.ssb_hiwat == 0 || so->so_rcv.ssb_hiwat == 0) { |
| 581 | error = soreserve(so, udp_sendspace, udp_recvspace, |
| 582 | ai->sb_rlimit); |
| 583 | if (error) |
| 584 | goto out; |
| 585 | } |
| 586 | |
| 587 | udbinfo_barrier_set(); |
| 588 | error = in_pcballoc(so, &udbinfo); |
| 589 | udbinfo_barrier_rem(); |
| 590 | |
| 591 | if (error) |
| 592 | goto out; |
| 593 | |
| 594 | inp = (struct inpcb *)so->so_pcb; |
| 595 | inp->inp_vflag |= INP_IPV6; |
| 596 | if (!ip6_v6only) |
| 597 | inp->inp_vflag |= INP_IPV4; |
| 598 | inp->in6p_hops = -1; /* use kernel default */ |
| 599 | inp->in6p_cksum = -1; /* just to be sure */ |
| 600 | /* |
| 601 | * XXX: ugly!! |
| 602 | * IPv4 TTL initialization is necessary for an IPv6 socket as well, |
| 603 | * because the socket may be bound to an IPv6 wildcard address, |
| 604 | * which may match an IPv4-mapped IPv6 address. |
| 605 | */ |
| 606 | inp->inp_ip_ttl = ip_defttl; |
| 607 | error = 0; |
| 608 | out: |
| 609 | lwkt_replymsg(&msg->attach.base.lmsg, error); |
| 610 | } |
| 611 | |
| 612 | static void |
| 613 | udp6_bind(netmsg_t msg) |
| 614 | { |
| 615 | struct socket *so =msg->bind.base.nm_so; |
| 616 | struct sockaddr *nam = msg->bind.nm_nam; |
| 617 | struct thread *td = msg->bind.nm_td; |
| 618 | struct sockaddr_in6 *sin6_p = (struct sockaddr_in6 *)nam; |
| 619 | struct inpcb *inp; |
| 620 | int error; |
| 621 | |
| 622 | inp = so->so_pcb; |
| 623 | if (inp == NULL) { |
| 624 | error = EINVAL; |
| 625 | goto out; |
| 626 | } |
| 627 | |
| 628 | inp->inp_vflag &= ~INP_IPV4; |
| 629 | inp->inp_vflag |= INP_IPV6; |
| 630 | if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) { |
| 631 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) |
| 632 | inp->inp_vflag |= INP_IPV4; |
| 633 | else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
| 634 | struct sockaddr_in sin; |
| 635 | |
| 636 | in6_sin6_2_sin(&sin, sin6_p); |
| 637 | inp->inp_vflag |= INP_IPV4; |
| 638 | inp->inp_vflag &= ~INP_IPV6; |
| 639 | crit_enter(); |
| 640 | error = in_pcbbind(inp, (struct sockaddr *)&sin, td); |
| 641 | crit_exit(); |
| 642 | goto out; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | crit_enter(); |
| 647 | error = in6_pcbbind(inp, nam, td); |
| 648 | crit_exit(); |
| 649 | if (error == 0) { |
| 650 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) |
| 651 | inp->inp_flags |= INP_WASBOUND_NOTANY; |
| 652 | |
| 653 | udbinfo_barrier_set(); |
| 654 | in_pcbinswildcardhash(inp); |
| 655 | udbinfo_barrier_rem(); |
| 656 | } |
| 657 | out: |
| 658 | lwkt_replymsg(&msg->bind.base.lmsg, error); |
| 659 | } |
| 660 | |
| 661 | static void |
| 662 | udp6_connect(netmsg_t msg) |
| 663 | { |
| 664 | struct socket *so = msg->connect.base.nm_so; |
| 665 | struct sockaddr *nam = msg->connect.nm_nam; |
| 666 | struct thread *td = msg->connect.nm_td; |
| 667 | struct inpcb *inp; |
| 668 | int error; |
| 669 | |
| 670 | udbinfo_barrier_set(); |
| 671 | |
| 672 | inp = so->so_pcb; |
| 673 | if (inp == NULL) { |
| 674 | error = EINVAL; |
| 675 | goto out; |
| 676 | } |
| 677 | |
| 678 | if (!(inp->inp_flags & IN6P_IPV6_V6ONLY)) { |
| 679 | struct sockaddr_in6 *sin6_p; |
| 680 | |
| 681 | sin6_p = (struct sockaddr_in6 *)nam; |
| 682 | if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
| 683 | struct sockaddr_in sin; |
| 684 | |
| 685 | if (inp->inp_faddr.s_addr != INADDR_ANY) { |
| 686 | error = EISCONN; |
| 687 | goto out; |
| 688 | } |
| 689 | in6_sin6_2_sin(&sin, sin6_p); |
| 690 | crit_enter(); |
| 691 | if (inp->inp_flags & INP_WILDCARD) |
| 692 | in_pcbremwildcardhash(inp); |
| 693 | error = in_pcbconnect(inp, (struct sockaddr *)&sin, td); |
| 694 | crit_exit(); |
| 695 | if (error == 0) { |
| 696 | inp->inp_vflag |= INP_IPV4; |
| 697 | inp->inp_vflag &= ~INP_IPV6; |
| 698 | soisconnected(so); |
| 699 | } |
| 700 | goto out; |
| 701 | } |
| 702 | } |
| 703 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { |
| 704 | error = EISCONN; |
| 705 | goto out; |
| 706 | } |
| 707 | if (inp->inp_flags & INP_WILDCARD) |
| 708 | in_pcbremwildcardhash(inp); |
| 709 | if (!prison_remote_ip(td, nam)) { |
| 710 | error = EAFNOSUPPORT; /* IPv4 only jail */ |
| 711 | goto out; |
| 712 | } |
| 713 | crit_enter(); |
| 714 | error = in6_pcbconnect(inp, nam, td); |
| 715 | crit_exit(); |
| 716 | if (error == 0) { |
| 717 | if (!ip6_v6only) { /* should be non mapped addr */ |
| 718 | inp->inp_vflag &= ~INP_IPV4; |
| 719 | inp->inp_vflag |= INP_IPV6; |
| 720 | } |
| 721 | soisconnected(so); |
| 722 | } else if (error == EAFNOSUPPORT) { /* connection dissolved */ |
| 723 | /* |
| 724 | * Follow traditional BSD behavior and retain |
| 725 | * the local port binding. But, fix the old misbehavior |
| 726 | * of overwriting any previously bound local address. |
| 727 | */ |
| 728 | if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) |
| 729 | inp->in6p_laddr = kin6addr_any; |
| 730 | in_pcbinswildcardhash(inp); |
| 731 | } |
| 732 | out: |
| 733 | udbinfo_barrier_rem(); |
| 734 | lwkt_replymsg(&msg->connect.base.lmsg, error); |
| 735 | } |
| 736 | |
| 737 | static void |
| 738 | udp6_detach(netmsg_t msg) |
| 739 | { |
| 740 | struct socket *so = msg->detach.base.nm_so; |
| 741 | struct inpcb *inp; |
| 742 | int error; |
| 743 | |
| 744 | inp = so->so_pcb; |
| 745 | if (inp) { |
| 746 | udbinfo_barrier_set(); |
| 747 | in6_pcbdetach(inp); |
| 748 | udbinfo_barrier_rem(); |
| 749 | error = 0; |
| 750 | } else { |
| 751 | error = EINVAL; |
| 752 | } |
| 753 | lwkt_replymsg(&msg->detach.base.lmsg, error); |
| 754 | } |
| 755 | |
| 756 | static void |
| 757 | udp6_disconnect(netmsg_t msg) |
| 758 | { |
| 759 | struct socket *so = msg->disconnect.base.nm_so; |
| 760 | struct inpcb *inp; |
| 761 | int error; |
| 762 | |
| 763 | inp = so->so_pcb; |
| 764 | if (inp == NULL) { |
| 765 | error = EINVAL; |
| 766 | goto out; |
| 767 | } |
| 768 | |
| 769 | if (inp->inp_vflag & INP_IPV4) { |
| 770 | const struct pr_usrreqs *pru; |
| 771 | |
| 772 | pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; |
| 773 | pru->pru_disconnect(msg); /* XXX on right port? */ |
| 774 | return; |
| 775 | } |
| 776 | |
| 777 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { |
| 778 | error = ENOTCONN; |
| 779 | } else { |
| 780 | udbinfo_barrier_set(); |
| 781 | in6_pcbdisconnect(inp); |
| 782 | udbinfo_barrier_rem(); |
| 783 | soclrstate(so, SS_ISCONNECTED); /* XXX */ |
| 784 | error = 0; |
| 785 | } |
| 786 | out: |
| 787 | lwkt_replymsg(&msg->disconnect.base.lmsg, error); |
| 788 | } |
| 789 | |
| 790 | static void |
| 791 | udp6_send(netmsg_t msg) |
| 792 | { |
| 793 | struct socket *so = msg->send.base.nm_so; |
| 794 | struct mbuf *m = msg->send.nm_m; |
| 795 | struct sockaddr *addr = msg->send.nm_addr; |
| 796 | struct mbuf *control = msg->send.nm_control; |
| 797 | struct thread *td = msg->send.nm_td; |
| 798 | struct inpcb *inp; |
| 799 | int error = 0; |
| 800 | |
| 801 | inp = so->so_pcb; |
| 802 | if (inp == NULL) { |
| 803 | error = EINVAL; |
| 804 | goto bad; |
| 805 | } |
| 806 | |
| 807 | if (addr) { |
| 808 | if (addr->sa_len != sizeof(struct sockaddr_in6)) { |
| 809 | error = EINVAL; |
| 810 | goto bad; |
| 811 | } |
| 812 | if (addr->sa_family != AF_INET6) { |
| 813 | error = EAFNOSUPPORT; |
| 814 | goto bad; |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | if (!ip6_v6only) { |
| 819 | int hasv4addr; |
| 820 | struct sockaddr_in6 *sin6 = NULL; |
| 821 | |
| 822 | if (addr == NULL) |
| 823 | hasv4addr = (inp->inp_vflag & INP_IPV4); |
| 824 | else { |
| 825 | sin6 = (struct sockaddr_in6 *)addr; |
| 826 | hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) |
| 827 | ? 1 : 0; |
| 828 | } |
| 829 | if (hasv4addr) { |
| 830 | const struct pr_usrreqs *pru; |
| 831 | |
| 832 | if (sin6) |
| 833 | in6_sin6_2_sin_in_sock(addr); |
| 834 | pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs; |
| 835 | pru->pru_send(msg); |
| 836 | /* msg invalid now */ |
| 837 | return; |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | error = udp6_output(inp, m, addr, control, td); |
| 842 | lwkt_replymsg(&msg->send.base.lmsg, error); |
| 843 | return; |
| 844 | bad: |
| 845 | m_freem(m); |
| 846 | lwkt_replymsg(&msg->send.base.lmsg, error); |
| 847 | } |
| 848 | |
| 849 | struct pr_usrreqs udp6_usrreqs = { |
| 850 | .pru_abort = udp6_abort, |
| 851 | .pru_accept = pr_generic_notsupp, |
| 852 | .pru_attach = udp6_attach, |
| 853 | .pru_bind = udp6_bind, |
| 854 | .pru_connect = udp6_connect, |
| 855 | .pru_connect2 = pr_generic_notsupp, |
| 856 | .pru_control = in6_control_dispatch, |
| 857 | .pru_detach = udp6_detach, |
| 858 | .pru_disconnect = udp6_disconnect, |
| 859 | .pru_listen = pr_generic_notsupp, |
| 860 | .pru_peeraddr = in6_mapped_peeraddr_dispatch, |
| 861 | .pru_rcvd = pr_generic_notsupp, |
| 862 | .pru_rcvoob = pr_generic_notsupp, |
| 863 | .pru_send = udp6_send, |
| 864 | .pru_sense = pru_sense_null, |
| 865 | .pru_shutdown = udp_shutdown, |
| 866 | .pru_sockaddr = in6_mapped_sockaddr_dispatch, |
| 867 | .pru_sosend = sosend, |
| 868 | .pru_soreceive = soreceive |
| 869 | }; |
| 870 | |