2 /* $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
64 #include "opt_ipsec.h"
66 #include "opt_inet6.h"
68 #include <sys/param.h>
70 #include <sys/malloc.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sysctl.h>
76 #include <sys/errno.h>
78 #include <sys/systm.h>
79 #include <sys/syslog.h>
82 #include <net/route.h>
83 #include <net/if_types.h>
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/udp6_var.h>
97 #include <netinet/icmp6.h>
98 #include <netinet6/ip6protosw.h>
101 #include <netinet6/ipsec.h>
103 #include <netinet6/ipsec6.h>
107 #include <net/net_osdep.h>
110 * UDP protocol inplementation.
111 * Per RFC 768, August, 1980.
115 #define udp6stat udpstat
116 #define udp6s_opackets udps_opackets
119 udp6_output(in6p, m, addr6, control, td)
122 struct mbuf *control;
123 struct sockaddr *addr6;
126 u_int32_t ulen = m->m_pkthdr.len;
127 u_int32_t plen = sizeof(struct udphdr) + ulen;
130 struct in6_addr *laddr, *faddr;
133 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
135 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
137 struct sockaddr_in6 tmp;
140 if (td && !suser(td))
143 if ((error = ip6_setpktoptions(control, &opt, stickyopt, priv,
144 0, IPPROTO_UDP)) != 0)
146 in6p->in6p_outputopts = &opt;
151 * IPv4 version of udp_output calls in_pcbconnect in this case,
152 * which needs splnet and affects performance.
153 * Since we saw no essential reason for calling in_pcbconnect,
154 * we get rid of such kind of logic, and call in6_selectsrc
155 * and in6_pcbsetport in order to fill in the local address
156 * and the local port.
158 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
159 if (sin6->sin6_port == 0) {
160 error = EADDRNOTAVAIL;
164 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
165 /* how about ::ffff:0.0.0.0 case? */
170 /* protect *sin6 from overwrites */
174 faddr = &sin6->sin6_addr;
175 fport = sin6->sin6_port; /* allow 0 port */
177 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
178 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
180 * I believe we should explicitly discard the
181 * packet when mapped addresses are disabled,
182 * rather than send the packet as an IPv6 one.
183 * If we chose the latter approach, the packet
184 * might be sent out on the wire based on the
185 * default route, the situation which we'd
186 * probably want to avoid.
187 * (20010421 jinmei@kame.net)
195 /* KAME hack: embed scopeid */
196 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
201 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
202 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
203 in6p->in6p_moptions, NULL,
204 &in6p->in6p_laddr, &error);
206 laddr = &in6p->in6p_laddr; /* XXX */
209 error = EADDRNOTAVAIL;
212 if (in6p->in6p_lport == 0 &&
213 (error = in6_pcbsetport(laddr, in6p, td->td_ucred)) != 0)
216 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
220 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
221 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
223 * XXX: this case would happen when the
224 * application sets the V6ONLY flag after
225 * connecting the foreign address.
226 * Such applications should be fixed,
229 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
230 "option was set for a connected socket\n");
236 laddr = &in6p->in6p_laddr;
237 faddr = &in6p->in6p_faddr;
238 fport = in6p->in6p_fport;
242 hlen = sizeof(struct ip);
245 * Calculate data length and get a mbuf
246 * for UDP and IP6 headers.
248 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
255 * Stuff checksum and output datagram.
257 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
258 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
259 udp6->uh_dport = fport;
261 udp6->uh_ulen = htons((u_short)plen);
268 ip6 = mtod(m, struct ip6_hdr *);
269 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
270 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
271 ip6->ip6_vfc |= IPV6_VERSION;
272 #if 0 /* ip6_plen will be filled in ip6_output. */
273 ip6->ip6_plen = htons((u_short)plen);
275 ip6->ip6_nxt = IPPROTO_UDP;
276 ip6->ip6_hlim = in6_selecthlim(in6p, NULL);
277 ip6->ip6_src = *laddr;
278 ip6->ip6_dst = *faddr;
280 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
281 sizeof(struct ip6_hdr), plen)) == 0) {
282 udp6->uh_sum = 0xffff;
287 udp6stat.udp6s_opackets++;
288 error = ip6_output(m, in6p->in6p_outputopts, NULL,
289 flags, in6p->in6p_moptions, NULL, in6p);
292 error = EAFNOSUPPORT;
302 ip6_clearpktopts(in6p->in6p_outputopts, -1);
303 in6p->in6p_outputopts = stickyopt;