1 /* $OpenBSD: udp6_output.c,v 1.30 2014/06/03 13:32:24 mpi Exp $ */ 2 /* $KAME: udp6_output.c,v 1.21 2001/02/07 11:51:54 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 62 #include <sys/param.h> 63 #include <sys/mbuf.h> 64 #include <sys/protosw.h> 65 #include <sys/socket.h> 66 #include <sys/socketvar.h> 67 #include <sys/errno.h> 68 #include <sys/stat.h> 69 #include <sys/systm.h> 70 #include <sys/syslog.h> 71 72 #include <net/if.h> 73 #include <net/route.h> 74 #include <net/if_types.h> 75 76 #include <netinet/in.h> 77 #include <netinet6/in6_var.h> 78 #include <netinet/in_systm.h> 79 #include <netinet/ip.h> 80 #include <netinet/ip_var.h> 81 #include <netinet/in_pcb.h> 82 #include <netinet/udp.h> 83 #include <netinet/udp_var.h> 84 #include <netinet/ip6.h> 85 #include <netinet6/ip6_var.h> 86 #include <netinet/icmp6.h> 87 #include <netinet6/ip6protosw.h> 88 89 /* 90 * UDP protocol inplementation. 91 * Per RFC 768, August, 1980. 92 */ 93 int 94 udp6_output(struct inpcb *in6p, struct mbuf *m, struct mbuf *addr6, 95 struct mbuf *control) 96 { 97 u_int32_t ulen = m->m_pkthdr.len; 98 u_int32_t plen = sizeof(struct udphdr) + ulen; 99 int error = 0, priv = 0, hlen, flags; 100 struct ip6_hdr *ip6; 101 struct udphdr *udp6; 102 struct in6_addr *laddr, *faddr; 103 struct ip6_pktopts *optp, opt; 104 struct sockaddr_in6 tmp; 105 struct proc *p = curproc; /* XXX */ 106 struct ifnet *ifp; 107 u_short fport; 108 109 if ((in6p->inp_socket->so_state & SS_PRIV) != 0) 110 priv = 1; 111 if (control) { 112 if ((error = ip6_setpktopts(control, &opt, 113 in6p->inp_outputopts6, priv, IPPROTO_UDP)) != 0) 114 goto release; 115 optp = &opt; 116 } else 117 optp = in6p->inp_outputopts6; 118 119 if (addr6) { 120 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *); 121 122 if (addr6->m_len != sizeof(*sin6)) { 123 error = EINVAL; 124 goto release; 125 } 126 if (sin6->sin6_family != AF_INET6) { 127 error = EAFNOSUPPORT; 128 goto release; 129 } 130 if (sin6->sin6_port == 0) { 131 error = EADDRNOTAVAIL; 132 goto release; 133 } 134 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 135 error = EINVAL; 136 goto release; 137 } 138 139 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6)) { 140 error = EISCONN; 141 goto release; 142 } 143 144 /* protect *sin6 from overwrites */ 145 tmp = *sin6; 146 sin6 = &tmp; 147 148 faddr = &sin6->sin6_addr; 149 fport = sin6->sin6_port; /* allow 0 port */ 150 151 /* KAME hack: embed scopeid */ 152 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { 153 error = EINVAL; 154 goto release; 155 } 156 157 error = in6_selectsrc(&laddr, sin6, optp, 158 in6p->inp_moptions6, &in6p->inp_route6, 159 &in6p->inp_laddr6, in6p->inp_rtableid); 160 if (error) 161 goto release; 162 163 if (in6p->inp_lport == 0 && 164 (error = in6_pcbsetport(laddr, in6p, p)) != 0) 165 goto release; 166 } else { 167 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6)) { 168 error = ENOTCONN; 169 goto release; 170 } 171 laddr = &in6p->inp_laddr6; 172 faddr = &in6p->inp_faddr6; 173 fport = in6p->inp_fport; 174 } 175 176 hlen = sizeof(struct ip6_hdr); 177 178 /* 179 * Calculate data length and get a mbuf 180 * for UDP and IP6 headers. 181 */ 182 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 183 if (m == 0) { 184 error = ENOBUFS; 185 goto releaseopt; 186 } 187 188 /* 189 * Stuff checksum and output datagram. 190 */ 191 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 192 udp6->uh_sport = in6p->inp_lport; /* lport is always set in the PCB */ 193 udp6->uh_dport = fport; 194 if (plen <= 0xffff) 195 udp6->uh_ulen = htons((u_short)plen); 196 else 197 udp6->uh_ulen = 0; 198 udp6->uh_sum = 0; 199 200 ip6 = mtod(m, struct ip6_hdr *); 201 ip6->ip6_flow = in6p->inp_flowinfo & IPV6_FLOWINFO_MASK; 202 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 203 ip6->ip6_vfc |= IPV6_VERSION; 204 #if 0 /* ip6_plen will be filled in ip6_output. */ 205 ip6->ip6_plen = htons((u_short)plen); 206 #endif 207 ip6->ip6_nxt = IPPROTO_UDP; 208 ifp = NULL; 209 if (in6p->inp_route6.ro_rt && 210 in6p->inp_route6.ro_rt->rt_flags & RTF_UP) 211 ifp = in6p->inp_route6.ro_rt->rt_ifp; 212 ip6->ip6_hlim = in6_selecthlim(in6p, ifp); 213 ip6->ip6_src = *laddr; 214 ip6->ip6_dst = *faddr; 215 216 m->m_pkthdr.csum_flags |= M_UDP_CSUM_OUT; 217 218 flags = 0; 219 if (in6p->inp_flags & IN6P_MINMTU) 220 flags |= IPV6_MINMTU; 221 222 udpstat.udps_opackets++; 223 224 /* force routing table */ 225 m->m_pkthdr.ph_rtableid = in6p->inp_rtableid; 226 227 error = ip6_output(m, optp, &in6p->inp_route6, 228 flags, in6p->inp_moptions6, NULL, in6p); 229 goto releaseopt; 230 231 release: 232 m_freem(m); 233 234 releaseopt: 235 if (control) { 236 ip6_clearpktopts(&opt, -1); 237 m_freem(control); 238 } 239 return (error); 240 } 241