| 1 | /* $NetBSD: udp6_output.c,v 1.54 2016/10/31 04:16:25 ozaki-r Exp $ */ |
| 2 | /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 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. 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 <sys/cdefs.h> |
| 65 | __KERNEL_RCSID(0, "$NetBSD: udp6_output.c,v 1.54 2016/10/31 04:16:25 ozaki-r Exp $" ); |
| 66 | |
| 67 | #ifdef _KERNEL_OPT |
| 68 | #include "opt_inet.h" |
| 69 | #endif |
| 70 | |
| 71 | #include <sys/param.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 | #include <sys/kauth.h> |
| 82 | #include <sys/domain.h> |
| 83 | |
| 84 | #include <net/if.h> |
| 85 | #include <net/if_types.h> |
| 86 | |
| 87 | #include <netinet/in.h> |
| 88 | #include <netinet/in_var.h> |
| 89 | #include <netinet/in_systm.h> |
| 90 | #include <netinet/ip.h> |
| 91 | #include <netinet/ip_var.h> |
| 92 | #include <netinet/in_pcb.h> |
| 93 | #include <netinet/udp.h> |
| 94 | #include <netinet/udp_var.h> |
| 95 | #include <netinet/udp_private.h> |
| 96 | #include <netinet/ip6.h> |
| 97 | #include <netinet6/ip6_var.h> |
| 98 | #include <netinet6/in6_pcb.h> |
| 99 | #include <netinet6/udp6_var.h> |
| 100 | #include <netinet6/udp6_private.h> |
| 101 | #include <netinet/icmp6.h> |
| 102 | #include <netinet6/ip6protosw.h> |
| 103 | #include <netinet6/scope6_var.h> |
| 104 | |
| 105 | #include "faith.h" |
| 106 | |
| 107 | #include <net/net_osdep.h> |
| 108 | |
| 109 | /* |
| 110 | * UDP protocol inplementation. |
| 111 | * Per RFC 768, August, 1980. |
| 112 | */ |
| 113 | |
| 114 | int |
| 115 | udp6_output(struct in6pcb * const in6p, struct mbuf *m, |
| 116 | struct sockaddr_in6 * const addr6, struct mbuf * const control, |
| 117 | struct lwp * const l) |
| 118 | { |
| 119 | u_int32_t ulen = m->m_pkthdr.len; |
| 120 | u_int32_t plen = sizeof(struct udphdr) + ulen; |
| 121 | struct ip6_hdr *ip6; |
| 122 | struct udphdr *udp6; |
| 123 | struct in6_addr _laddr, *laddr, *faddr; |
| 124 | struct in6_addr laddr_mapped; /* XXX ugly */ |
| 125 | struct sockaddr_in6 *sin6 = NULL; |
| 126 | struct ifnet *oifp = NULL; |
| 127 | int scope_ambiguous = 0; |
| 128 | u_int16_t fport; |
| 129 | int error = 0; |
| 130 | struct ip6_pktopts *optp = NULL; |
| 131 | struct ip6_pktopts opt; |
| 132 | int af = AF_INET6, hlen = sizeof(struct ip6_hdr); |
| 133 | #ifdef INET |
| 134 | struct ip *ip; |
| 135 | struct udpiphdr *ui; |
| 136 | int flags = 0; |
| 137 | #endif |
| 138 | struct sockaddr_in6 tmp; |
| 139 | |
| 140 | if (addr6) { |
| 141 | sin6 = addr6; |
| 142 | if (sin6->sin6_family != AF_INET6) { |
| 143 | error = EAFNOSUPPORT; |
| 144 | goto release; |
| 145 | } |
| 146 | |
| 147 | /* protect *sin6 from overwrites */ |
| 148 | tmp = *sin6; |
| 149 | sin6 = &tmp; |
| 150 | |
| 151 | /* |
| 152 | * Application should provide a proper zone ID or the use of |
| 153 | * default zone IDs should be enabled. Unfortunately, some |
| 154 | * applications do not behave as it should, so we need a |
| 155 | * workaround. Even if an appropriate ID is not determined, |
| 156 | * we'll see if we can determine the outgoing interface. If we |
| 157 | * can, determine the zone ID based on the interface below. |
| 158 | */ |
| 159 | if (sin6->sin6_scope_id == 0 && !ip6_use_defzone) |
| 160 | scope_ambiguous = 1; |
| 161 | if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0) |
| 162 | goto release; |
| 163 | } |
| 164 | |
| 165 | if (control) { |
| 166 | if ((error = ip6_setpktopts(control, &opt, |
| 167 | in6p->in6p_outputopts, l->l_cred, IPPROTO_UDP)) != 0) |
| 168 | goto release; |
| 169 | optp = &opt; |
| 170 | } else |
| 171 | optp = in6p->in6p_outputopts; |
| 172 | |
| 173 | |
| 174 | if (sin6) { |
| 175 | /* |
| 176 | * Slightly different than v4 version in that we call |
| 177 | * in6_selectsrc and in6_pcbsetport to fill in the local |
| 178 | * address and port rather than in_pcbconnect. in_pcbconnect |
| 179 | * sets in6p_faddr which causes EISCONN below to be hit on |
| 180 | * subsequent sendto. |
| 181 | */ |
| 182 | if (sin6->sin6_port == 0) { |
| 183 | error = EADDRNOTAVAIL; |
| 184 | goto release; |
| 185 | } |
| 186 | |
| 187 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { |
| 188 | /* how about ::ffff:0.0.0.0 case? */ |
| 189 | error = EISCONN; |
| 190 | goto release; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | faddr = &sin6->sin6_addr; |
| 195 | fport = sin6->sin6_port; /* allow 0 port */ |
| 196 | |
| 197 | if (IN6_IS_ADDR_V4MAPPED(faddr)) { |
| 198 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) |
| 199 | { |
| 200 | /* |
| 201 | * I believe we should explicitly discard the |
| 202 | * packet when mapped addresses are disabled, |
| 203 | * rather than send the packet as an IPv6 one. |
| 204 | * If we chose the latter approach, the packet |
| 205 | * might be sent out on the wire based on the |
| 206 | * default route, the situation which we'd |
| 207 | * probably want to avoid. |
| 208 | * (20010421 jinmei@kame.net) |
| 209 | */ |
| 210 | error = EINVAL; |
| 211 | goto release; |
| 212 | } |
| 213 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) |
| 214 | && !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) { |
| 215 | /* |
| 216 | * when remote addr is an IPv4-mapped address, |
| 217 | * local addr should not be an IPv6 address, |
| 218 | * since you cannot determine how to map IPv6 |
| 219 | * source address to IPv4. |
| 220 | */ |
| 221 | error = EINVAL; |
| 222 | goto release; |
| 223 | } |
| 224 | |
| 225 | af = AF_INET; |
| 226 | } |
| 227 | |
| 228 | if (!IN6_IS_ADDR_V4MAPPED(faddr)) { |
| 229 | struct psref psref; |
| 230 | int bound = curlwp_bind(); |
| 231 | |
| 232 | error = in6_selectsrc(sin6, optp, |
| 233 | in6p->in6p_moptions, |
| 234 | &in6p->in6p_route, |
| 235 | &in6p->in6p_laddr, &oifp, &psref, &_laddr); |
| 236 | /* XXX need error check? */ |
| 237 | if (oifp && scope_ambiguous && |
| 238 | (error = in6_setscope(&sin6->sin6_addr, |
| 239 | oifp, NULL))) { |
| 240 | if_put(oifp, &psref); |
| 241 | curlwp_bindx(bound); |
| 242 | goto release; |
| 243 | } |
| 244 | if_put(oifp, &psref); |
| 245 | curlwp_bindx(bound); |
| 246 | laddr = &_laddr; |
| 247 | } else { |
| 248 | /* |
| 249 | * XXX: freebsd[34] does not have in_selectsrc, but |
| 250 | * we can omit the whole part because freebsd4 calls |
| 251 | * udp_output() directly in this case, and thus we'll |
| 252 | * never see this path. |
| 253 | */ |
| 254 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { |
| 255 | struct sockaddr_in sin_dst; |
| 256 | struct in_addr ina; |
| 257 | struct in_ifaddr *ia4; |
| 258 | struct psref _psref; |
| 259 | int bound; |
| 260 | |
| 261 | memcpy(&ina, &faddr->s6_addr[12], sizeof(ina)); |
| 262 | sockaddr_in_init(&sin_dst, &ina, 0); |
| 263 | bound = curlwp_bind(); |
| 264 | ia4 = in_selectsrc(&sin_dst, &in6p->in6p_route, |
| 265 | in6p->in6p_socket->so_options, NULL, |
| 266 | &error, &_psref); |
| 267 | if (ia4 == NULL) { |
| 268 | curlwp_bindx(bound); |
| 269 | if (error == 0) |
| 270 | error = EADDRNOTAVAIL; |
| 271 | goto release; |
| 272 | } |
| 273 | memset(&laddr_mapped, 0, sizeof(laddr_mapped)); |
| 274 | laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */ |
| 275 | memcpy(&laddr_mapped.s6_addr[12], |
| 276 | &IA_SIN(ia4)->sin_addr, |
| 277 | sizeof(IA_SIN(ia4)->sin_addr)); |
| 278 | ia4_release(ia4, &_psref); |
| 279 | curlwp_bindx(bound); |
| 280 | laddr = &laddr_mapped; |
| 281 | } else |
| 282 | { |
| 283 | laddr = &in6p->in6p_laddr; /* XXX */ |
| 284 | } |
| 285 | } |
| 286 | if (laddr == NULL) { |
| 287 | if (error == 0) |
| 288 | error = EADDRNOTAVAIL; |
| 289 | goto release; |
| 290 | } |
| 291 | if (in6p->in6p_lport == 0) { |
| 292 | /* |
| 293 | * Craft a sockaddr_in6 for the local endpoint. Use the |
| 294 | * "any" as a base, set the address, and recover the |
| 295 | * scope. |
| 296 | */ |
| 297 | struct sockaddr_in6 lsin6 = |
| 298 | *((const struct sockaddr_in6 *)in6p->in6p_socket->so_proto->pr_domain->dom_sa_any); |
| 299 | lsin6.sin6_addr = *laddr; |
| 300 | error = sa6_recoverscope(&lsin6); |
| 301 | if (error) |
| 302 | goto release; |
| 303 | |
| 304 | error = in6_pcbsetport(&lsin6, in6p, l); |
| 305 | |
| 306 | if (error) { |
| 307 | in6p->in6p_laddr = in6addr_any; |
| 308 | goto release; |
| 309 | } |
| 310 | } |
| 311 | } else { |
| 312 | if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { |
| 313 | error = ENOTCONN; |
| 314 | goto release; |
| 315 | } |
| 316 | if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) { |
| 317 | if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) |
| 318 | { |
| 319 | /* |
| 320 | * XXX: this case would happen when the |
| 321 | * application sets the V6ONLY flag after |
| 322 | * connecting the foreign address. |
| 323 | * Such applications should be fixed, |
| 324 | * so we bark here. |
| 325 | */ |
| 326 | log(LOG_INFO, "udp6_output: IPV6_V6ONLY " |
| 327 | "option was set for a connected socket\n" ); |
| 328 | error = EINVAL; |
| 329 | goto release; |
| 330 | } else |
| 331 | af = AF_INET; |
| 332 | } |
| 333 | laddr = &in6p->in6p_laddr; |
| 334 | faddr = &in6p->in6p_faddr; |
| 335 | fport = in6p->in6p_fport; |
| 336 | } |
| 337 | |
| 338 | if (af == AF_INET) |
| 339 | hlen = sizeof(struct ip); |
| 340 | |
| 341 | /* |
| 342 | * Calculate data length and get a mbuf |
| 343 | * for UDP and IP6 headers. |
| 344 | */ |
| 345 | M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); |
| 346 | if (m == 0) { |
| 347 | error = ENOBUFS; |
| 348 | goto release; |
| 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Stuff checksum and output datagram. |
| 353 | */ |
| 354 | udp6 = (struct udphdr *)(mtod(m, char *) + hlen); |
| 355 | udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ |
| 356 | udp6->uh_dport = fport; |
| 357 | if (plen <= 0xffff) |
| 358 | udp6->uh_ulen = htons((u_int16_t)plen); |
| 359 | else |
| 360 | udp6->uh_ulen = 0; |
| 361 | udp6->uh_sum = 0; |
| 362 | |
| 363 | switch (af) { |
| 364 | case AF_INET6: |
| 365 | ip6 = mtod(m, struct ip6_hdr *); |
| 366 | ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; |
| 367 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; |
| 368 | ip6->ip6_vfc |= IPV6_VERSION; |
| 369 | #if 0 /* ip6_plen will be filled in ip6_output. */ |
| 370 | ip6->ip6_plen = htons((u_int16_t)plen); |
| 371 | #endif |
| 372 | ip6->ip6_nxt = IPPROTO_UDP; |
| 373 | ip6->ip6_hlim = in6_selecthlim_rt(in6p); |
| 374 | ip6->ip6_src = *laddr; |
| 375 | ip6->ip6_dst = *faddr; |
| 376 | |
| 377 | udp6->uh_sum = in6_cksum_phdr(laddr, faddr, |
| 378 | htonl(plen), htonl(IPPROTO_UDP)); |
| 379 | m->m_pkthdr.csum_flags = M_CSUM_UDPv6; |
| 380 | m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); |
| 381 | |
| 382 | UDP6_STATINC(UDP6_STAT_OPACKETS); |
| 383 | error = ip6_output(m, optp, &in6p->in6p_route, 0, |
| 384 | in6p->in6p_moptions, in6p->in6p_socket, NULL); |
| 385 | break; |
| 386 | case AF_INET: |
| 387 | #ifdef INET |
| 388 | /* can't transmit jumbogram over IPv4 */ |
| 389 | if (plen > 0xffff) { |
| 390 | error = EMSGSIZE; |
| 391 | goto release; |
| 392 | } |
| 393 | |
| 394 | ip = mtod(m, struct ip *); |
| 395 | ui = (struct udpiphdr *)ip; |
| 396 | memset(ui->ui_x1, 0, sizeof(ui->ui_x1)); |
| 397 | ui->ui_pr = IPPROTO_UDP; |
| 398 | ui->ui_len = htons(plen); |
| 399 | memcpy(&ui->ui_src, &laddr->s6_addr[12], sizeof(ui->ui_src)); |
| 400 | ui->ui_ulen = ui->ui_len; |
| 401 | |
| 402 | flags = (in6p->in6p_socket->so_options & |
| 403 | (SO_DONTROUTE | SO_BROADCAST)); |
| 404 | memcpy(&ui->ui_dst, &faddr->s6_addr[12], sizeof(ui->ui_dst)); |
| 405 | |
| 406 | udp6->uh_sum = in_cksum(m, hlen + plen); |
| 407 | if (udp6->uh_sum == 0) |
| 408 | udp6->uh_sum = 0xffff; |
| 409 | |
| 410 | ip->ip_len = htons(hlen + plen); |
| 411 | ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */ |
| 412 | ip->ip_tos = 0; /* XXX */ |
| 413 | |
| 414 | UDP_STATINC(UDP_STAT_OPACKETS); |
| 415 | error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */, |
| 416 | in6p->in6p_v4moptions, (struct socket *)in6p->in6p_socket); |
| 417 | break; |
| 418 | #else |
| 419 | error = EAFNOSUPPORT; |
| 420 | goto release; |
| 421 | #endif |
| 422 | } |
| 423 | goto releaseopt; |
| 424 | |
| 425 | release: |
| 426 | m_freem(m); |
| 427 | |
| 428 | releaseopt: |
| 429 | if (control) { |
| 430 | if (optp == &opt) |
| 431 | ip6_clearpktopts(&opt, -1); |
| 432 | m_freem(control); |
| 433 | } |
| 434 | return (error); |
| 435 | } |
| 436 | |