1 /* $OpenBSD: udp6_output.c,v 1.4 2001/06/09 06:43:38 angelos 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. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the University of 48 * California, Berkeley and its contributors. 49 * 4. Neither the name of the University nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 63 * SUCH DAMAGE. 64 * 65 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 66 */ 67 68 #include <sys/param.h> 69 #include <sys/malloc.h> 70 #include <sys/mbuf.h> 71 #include <sys/protosw.h> 72 #include <sys/socket.h> 73 #include <sys/socketvar.h> 74 #include <sys/errno.h> 75 #include <sys/stat.h> 76 #include <sys/systm.h> 77 #include <sys/syslog.h> 78 79 #include <net/if.h> 80 #include <net/route.h> 81 #include <net/if_types.h> 82 83 #include <netinet/in.h> 84 #include <netinet/in_var.h> 85 #include <netinet/in_systm.h> 86 #include <netinet/ip.h> 87 #include <netinet/ip_var.h> 88 #include <netinet/in_pcb.h> 89 #include <netinet/udp.h> 90 #include <netinet/udp_var.h> 91 #include <netinet/ip6.h> 92 #include <netinet6/ip6_var.h> 93 #include <netinet/icmp6.h> 94 #include <netinet6/ip6protosw.h> 95 96 #undef IPSEC 97 98 #include "faith.h" 99 100 /* 101 * UDP protocol inplementation. 102 * Per RFC 768, August, 1980. 103 */ 104 105 #define in6pcb inpcb 106 #define in6p_outputopts inp_outputopts6 107 #define in6p_socket inp_socket 108 #define in6p_faddr inp_faddr6 109 #define in6p_laddr inp_laddr6 110 #define in6p_fport inp_fport 111 #define in6p_lport inp_lport 112 #define in6p_flags inp_flags 113 #define in6p_moptions inp_moptions6 114 #define in6p_route inp_route6 115 #define in6p_flowinfo inp_flowinfo 116 #define udp6stat udpstat 117 #define udp6s_opackets udps_opackets 118 119 int 120 udp6_output(in6p, m, addr6, control) 121 struct in6pcb *in6p; 122 struct mbuf *m; 123 struct mbuf *addr6, *control; 124 { 125 u_int32_t ulen = m->m_pkthdr.len; 126 u_int32_t plen = sizeof(struct udphdr) + ulen; 127 struct ip6_hdr *ip6; 128 struct udphdr *udp6; 129 struct in6_addr *laddr, *faddr; 130 u_short fport; 131 int error = 0; 132 struct ip6_pktopts opt, *stickyopt; 133 int priv; 134 int af, hlen; 135 int flags; 136 struct sockaddr_in6 tmp; 137 138 priv = 0; 139 if ((in6p->in6p_socket->so_state & SS_PRIV) != 0) 140 priv = 1; 141 stickyopt = in6p->in6p_outputopts; 142 if (control) { 143 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0) 144 goto release; 145 in6p->in6p_outputopts = &opt; 146 } 147 148 if (addr6) { 149 /* 150 * IPv4 version of udp_output calls in_pcbconnect in this case, 151 * which needs splnet and affects performance. 152 * Since we saw no essential reason for calling in_pcbconnect, 153 * we get rid of such kind of logic, and call in6_selectsrc 154 * and in6_pcbsetport in order to fill in the local address 155 * and the local port. 156 */ 157 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *); 158 159 if (addr6->m_len != sizeof(*sin6)) { 160 error = EINVAL; 161 goto release; 162 } 163 if (sin6->sin6_family != AF_INET6) { 164 error = EAFNOSUPPORT; 165 goto release; 166 } 167 if (sin6->sin6_port == 0) { 168 error = EADDRNOTAVAIL; 169 goto release; 170 } 171 172 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 173 error = EISCONN; 174 goto release; 175 } 176 177 /* protect *sin6 from overwrites */ 178 tmp = *sin6; 179 sin6 = &tmp; 180 181 faddr = &sin6->sin6_addr; 182 fport = sin6->sin6_port; /* allow 0 port */ 183 184 /* KAME hack: embed scopeid */ 185 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) { 186 error = EINVAL; 187 goto release; 188 } 189 190 if (1) /* we don't support IPv4 mapped address */ 191 { 192 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts, 193 in6p->in6p_moptions, 194 &in6p->in6p_route, 195 &in6p->in6p_laddr, &error); 196 } else 197 laddr = &in6p->in6p_laddr; /*XXX*/ 198 if (laddr == NULL) { 199 if (error == 0) 200 error = EADDRNOTAVAIL; 201 goto release; 202 } 203 if (in6p->in6p_lport == 0 && 204 (error = in6_pcbsetport(laddr, in6p)) != 0) 205 goto release; 206 } else { 207 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 208 error = ENOTCONN; 209 goto release; 210 } 211 laddr = &in6p->in6p_laddr; 212 faddr = &in6p->in6p_faddr; 213 fport = in6p->in6p_fport; 214 } 215 216 if (1) /* we don't support IPv4 mapped address */ 217 { 218 af = AF_INET6; 219 hlen = sizeof(struct ip6_hdr); 220 } else { 221 af = AF_INET; 222 hlen = sizeof(struct ip); 223 } 224 225 /* 226 * Calculate data length and get a mbuf 227 * for UDP and IP6 headers. 228 */ 229 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT); 230 if (m == 0) { 231 error = ENOBUFS; 232 goto release; 233 } 234 235 /* 236 * Stuff checksum and output datagram. 237 */ 238 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen); 239 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */ 240 udp6->uh_dport = fport; 241 if (plen <= 0xffff) 242 udp6->uh_ulen = htons((u_short)plen); 243 else 244 udp6->uh_ulen = 0; 245 udp6->uh_sum = 0; 246 247 switch (af) { 248 case AF_INET6: 249 ip6 = mtod(m, struct ip6_hdr *); 250 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK; 251 ip6->ip6_vfc &= ~IPV6_VERSION_MASK; 252 ip6->ip6_vfc |= IPV6_VERSION; 253 #if 0 /* ip6_plen will be filled in ip6_output. */ 254 ip6->ip6_plen = htons((u_short)plen); 255 #endif 256 ip6->ip6_nxt = IPPROTO_UDP; 257 ip6->ip6_hlim = in6_selecthlim(in6p, 258 in6p->in6p_route.ro_rt ? 259 in6p->in6p_route.ro_rt->rt_ifp : NULL); 260 ip6->ip6_src = *laddr; 261 ip6->ip6_dst = *faddr; 262 263 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP, 264 sizeof(struct ip6_hdr), plen)) == 0) { 265 udp6->uh_sum = 0xffff; 266 } 267 268 flags = 0; 269 #ifdef IPV6_MINMTU 270 if (in6p->in6p_flags & IN6P_MINMTU) 271 flags |= IPV6_MINMTU; 272 #endif 273 274 udp6stat.udp6s_opackets++; 275 #ifdef IPSEC 276 ipsec_setsocket(m, in6p->in6p_socket); 277 #endif /*IPSEC*/ 278 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route, 279 flags, in6p->in6p_moptions, NULL); 280 break; 281 case AF_INET: 282 error = EAFNOSUPPORT; 283 goto release; 284 } 285 goto releaseopt; 286 287 release: 288 m_freem(m); 289 290 releaseopt: 291 if (control) { 292 in6p->in6p_outputopts = stickyopt; 293 m_freem(control); 294 } 295 return(error); 296 } 297