1 /* $OpenBSD: udp6_output.c,v 1.16 2008/11/23 13:30:59 claudio 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 struct ifnet *ifp; 132 133 priv = 0; 134 if ((in6p->in6p_socket->so_state & SS_PRIV) != 0) 135 priv = 1; 136 if (control) { 137 if ((error = ip6_setpktopts(control, &opt, 138 in6p->in6p_outputopts, priv, IPPROTO_UDP)) != 0) 139 goto release; 140 optp = &opt; 141 } else 142 optp = in6p->in6p_outputopts; 143 144 if (addr6) { 145 /* 146 * IPv4 version of udp_output calls in_pcbconnect in this case, 147 * which needs splnet and affects performance. 148 * Since we saw no essential reason for calling in_pcbconnect, 149 * we get rid of such kind of logic, and call in6_selectsrc 150 * and in6_pcbsetport in order to fill in the local address 151 * and the local port. 152 */ 153 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *); 154 155 if (addr6->m_len != sizeof(*sin6)) { 156 error = EINVAL; 157 goto release; 158 } 159 if (sin6->sin6_family != AF_INET6) { 160 error = EAFNOSUPPORT; 161 goto release; 162 } 163 if (sin6->sin6_port == 0) { 164 error = EADDRNOTAVAIL; 165 goto release; 166 } 167 168 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 169 error = EISCONN; 170 goto release; 171 } 172 173 /* protect *sin6 from overwrites */ 174 tmp = *sin6; 175 sin6 = &tmp; 176 177 faddr = &sin6->sin6_addr; 178 fport = sin6->sin6_port; /* allow 0 port */ 179 180 /* KAME hack: embed scopeid */ 181 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { 182 error = EINVAL; 183 goto release; 184 } 185 186 if (1) /* we don't support IPv4 mapped address */ 187 { 188 laddr = in6_selectsrc(sin6, optp, 189 in6p->in6p_moptions, 190 &in6p->in6p_route, 191 &in6p->in6p_laddr, &error); 192 } else 193 laddr = &in6p->in6p_laddr; /*XXX*/ 194 if (laddr == NULL) { 195 if (error == 0) 196 error = EADDRNOTAVAIL; 197 goto release; 198 } 199 if (in6p->in6p_lport == 0 && 200 (error = in6_pcbsetport(laddr, in6p, p)) != 0) 201 goto release; 202 } else { 203 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 204 error = ENOTCONN; 205 goto release; 206 } 207 laddr = &in6p->in6p_laddr; 208 faddr = &in6p->in6p_faddr; 209 fport = in6p->in6p_fport; 210 } 211 212 if (1) /* we don't support IPv4 mapped address */ 213 { 214 af = AF_INET6; 215 hlen = sizeof(struct ip6_hdr); 216 } else { 217 af = AF_INET; 218 hlen = sizeof(struct ip); 219 } 220 221 /* 222 * Calculate data length and get a mbuf 223 * for UDP and IP6 headers. 224 */ 225 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 226 if (m == 0) { 227 error = ENOBUFS; 228 goto release; 229 } 230 231 /* 232 * Stuff checksum and output datagram. 233 */ 234 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 235 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ 236 udp6->uh_dport = fport; 237 if (plen <= 0xffff) 238 udp6->uh_ulen = htons((u_short)plen); 239 else 240 udp6->uh_ulen = 0; 241 udp6->uh_sum = 0; 242 243 switch (af) { 244 case AF_INET6: 245 ip6 = mtod(m, struct ip6_hdr *); 246 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 247 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 248 ip6->ip6_vfc |= IPV6_VERSION; 249 #if 0 /* ip6_plen will be filled in ip6_output. */ 250 ip6->ip6_plen = htons((u_short)plen); 251 #endif 252 ip6->ip6_nxt = IPPROTO_UDP; 253 ifp = NULL; 254 if (in6p->in6p_route.ro_rt && 255 in6p->in6p_route.ro_rt->rt_flags & RTF_UP) 256 ifp = in6p->in6p_route.ro_rt->rt_ifp; 257 ip6->ip6_hlim = in6_selecthlim(in6p, ifp); 258 ip6->ip6_src = *laddr; 259 ip6->ip6_dst = *faddr; 260 261 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 262 sizeof(struct ip6_hdr), plen)) == 0) { 263 udp6->uh_sum = 0xffff; 264 } 265 266 flags = 0; 267 if (in6p->in6p_flags & IN6P_MINMTU) 268 flags |= IPV6_MINMTU; 269 270 udp6stat.udp6s_opackets++; 271 error = ip6_output(m, optp, &in6p->in6p_route, 272 flags, in6p->in6p_moptions, NULL, in6p); 273 break; 274 case AF_INET: 275 error = EAFNOSUPPORT; 276 goto release; 277 } 278 goto releaseopt; 279 280 release: 281 m_freem(m); 282 283 releaseopt: 284 if (control) { 285 ip6_clearpktopts(&opt, -1); 286 m_freem(control); 287 } 288 return (error); 289 } 290