1 /* $OpenBSD: udp6_output.c,v 1.15 2008/06/11 19:00:50 mcbride 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 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 62 */ 63 64 #include <sys/param.h> 65 #include <sys/malloc.h> 66 #include <sys/mbuf.h> 67 #include <sys/protosw.h> 68 #include <sys/socket.h> 69 #include <sys/socketvar.h> 70 #include <sys/errno.h> 71 #include <sys/stat.h> 72 #include <sys/systm.h> 73 #include <sys/proc.h> 74 #include <sys/syslog.h> 75 76 #include <net/if.h> 77 #include <net/route.h> 78 #include <net/if_types.h> 79 80 #include <netinet/in.h> 81 #include <netinet/in_var.h> 82 #include <netinet/in_systm.h> 83 #include <netinet/ip.h> 84 #include <netinet/ip_var.h> 85 #include <netinet/in_pcb.h> 86 #include <netinet/udp.h> 87 #include <netinet/udp_var.h> 88 #include <netinet/ip6.h> 89 #include <netinet6/ip6_var.h> 90 #include <netinet/icmp6.h> 91 #include <netinet6/ip6protosw.h> 92 93 #include "faith.h" 94 95 /* 96 * UDP protocol inplementation. 97 * Per RFC 768, August, 1980. 98 */ 99 100 #define in6pcb inpcb 101 #define in6p_outputopts inp_outputopts6 102 #define in6p_socket inp_socket 103 #define in6p_faddr inp_faddr6 104 #define in6p_laddr inp_laddr6 105 #define in6p_fport inp_fport 106 #define in6p_lport inp_lport 107 #define in6p_flags inp_flags 108 #define in6p_moptions inp_moptions6 109 #define in6p_route inp_route6 110 #define in6p_flowinfo inp_flowinfo 111 #define udp6stat udpstat 112 #define udp6s_opackets udps_opackets 113 114 int 115 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct mbuf *addr6, 116 struct mbuf *control) 117 { 118 u_int32_t ulen = m->m_pkthdr.len; 119 u_int32_t plen = sizeof(struct udphdr) + ulen; 120 struct ip6_hdr *ip6; 121 struct udphdr *udp6; 122 struct in6_addr *laddr, *faddr; 123 u_short fport; 124 int error = 0; 125 struct ip6_pktopts *optp, opt; 126 int priv; 127 int af, hlen; 128 int flags; 129 struct sockaddr_in6 tmp; 130 struct proc *p = curproc; /* XXX */ 131 132 priv = 0; 133 if ((in6p->in6p_socket->so_state & SS_PRIV) != 0) 134 priv = 1; 135 if (control) { 136 if ((error = ip6_setpktopts(control, &opt, 137 in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0) 138 goto release; 139 optp = &opt; 140 } else 141 optp = in6p->in6p_outputopts; 142 143 if (addr6) { 144 /* 145 * IPv4 version of udp_output calls in_pcbconnect in this case, 146 * which needs splnet and affects performance. 147 * Since we saw no essential reason for calling in_pcbconnect, 148 * we get rid of such kind of logic, and call in6_selectsrc 149 * and in6_pcbsetport in order to fill in the local address 150 * and the local port. 151 */ 152 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *); 153 154 if (addr6->m_len != sizeof(*sin6)) { 155 error = EINVAL; 156 goto release; 157 } 158 if (sin6->sin6_family != AF_INET6) { 159 error = EAFNOSUPPORT; 160 goto release; 161 } 162 if (sin6->sin6_port == 0) { 163 error = EADDRNOTAVAIL; 164 goto release; 165 } 166 167 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 168 error = EISCONN; 169 goto release; 170 } 171 172 /* protect *sin6 from overwrites */ 173 tmp = *sin6; 174 sin6 = &tmp; 175 176 faddr = &sin6->sin6_addr; 177 fport = sin6->sin6_port; /* allow 0 port */ 178 179 /* KAME hack: embed scopeid */ 180 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { 181 error = EINVAL; 182 goto release; 183 } 184 185 if (1) /* we don't support IPv4 mapped address */ 186 { 187 laddr = in6_selectsrc(sin6, optp, 188 in6p->in6p_moptions, 189 &in6p->in6p_route, 190 &in6p->in6p_laddr, &error); 191 } else 192 laddr = &in6p->in6p_laddr; /*XXX*/ 193 if (laddr == NULL) { 194 if (error == 0) 195 error = EADDRNOTAVAIL; 196 goto release; 197 } 198 if (in6p->in6p_lport == 0 && 199 (error = in6_pcbsetport(laddr, in6p, p)) != 0) 200 goto release; 201 } else { 202 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 203 error = ENOTCONN; 204 goto release; 205 } 206 laddr = &in6p->in6p_laddr; 207 faddr = &in6p->in6p_faddr; 208 fport = in6p->in6p_fport; 209 } 210 211 if (1) /* we don't support IPv4 mapped address */ 212 { 213 af = AF_INET6; 214 hlen = sizeof(struct ip6_hdr); 215 } else { 216 af = AF_INET; 217 hlen = sizeof(struct ip); 218 } 219 220 /* 221 * Calculate data length and get a mbuf 222 * for UDP and IP6 headers. 223 */ 224 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 225 if (m == 0) { 226 error = ENOBUFS; 227 goto release; 228 } 229 230 /* 231 * Stuff checksum and output datagram. 232 */ 233 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 234 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ 235 udp6->uh_dport = fport; 236 if (plen <= 0xffff) 237 udp6->uh_ulen = htons((u_short)plen); 238 else 239 udp6->uh_ulen = 0; 240 udp6->uh_sum = 0; 241 242 switch (af) { 243 case AF_INET6: 244 ip6 = mtod(m, struct ip6_hdr *); 245 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 246 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 247 ip6->ip6_vfc |= IPV6_VERSION; 248 #if 0 /* ip6_plen will be filled in ip6_output. */ 249 ip6->ip6_plen = htons((u_short)plen); 250 #endif 251 ip6->ip6_nxt = IPPROTO_UDP; 252 ip6->ip6_hlim = in6_selecthlim(in6p, 253 in6p->in6p_route.ro_rt ? 254 in6p->in6p_route.ro_rt->rt_ifp : NULL); 255 ip6->ip6_src = *laddr; 256 ip6->ip6_dst = *faddr; 257 258 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 259 sizeof(struct ip6_hdr), plen)) == 0) { 260 udp6->uh_sum = 0xffff; 261 } 262 263 flags = 0; 264 if (in6p->in6p_flags & IN6P_MINMTU) 265 flags |= IPV6_MINMTU; 266 267 udp6stat.udp6s_opackets++; 268 error = ip6_output(m, optp, &in6p->in6p_route, 269 flags, in6p->in6p_moptions, NULL, in6p); 270 break; 271 case AF_INET: 272 error = EAFNOSUPPORT; 273 goto release; 274 } 275 goto releaseopt; 276 277 release: 278 m_freem(m); 279 280 releaseopt: 281 if (control) { 282 ip6_clearpktopts(&opt, -1); 283 m_freem(control); 284 } 285 return (error); 286 } 287