1 /* $FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.6 2003/01/23 21:06:47 sam Exp $ */
2 /* $DragonFly: src/sys/netinet6/udp6_output.c,v 1.9 2008/09/04 09:08:22 hasso Exp $ */
3 /* $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $ */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1989, 1993
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
69 #include "opt_ipsec.h"
71 #include "opt_inet6.h"
73 #include <sys/param.h>
74 #include <sys/malloc.h>
76 #include <sys/protosw.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sysctl.h>
80 #include <sys/errno.h>
82 #include <sys/systm.h>
85 #include <sys/syslog.h>
88 #include <net/route.h>
89 #include <net/if_types.h>
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/ip.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/in_pcb.h>
97 #include <netinet/udp.h>
98 #include <netinet/udp_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet6/ip6_var.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/udp6_var.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6protosw.h>
107 #include <netinet6/ipsec.h>
109 #include <netinet6/ipsec6.h>
113 #include <net/net_osdep.h>
116 * UDP protocol inplementation.
117 * Per RFC 768, August, 1980.
121 #define udp6stat udpstat
122 #define udp6s_opackets udps_opackets
125 udp6_output(struct in6pcb *in6p, struct mbuf *m, struct sockaddr *addr6,
126 struct mbuf *control, struct thread *td)
128 u_int32_t ulen = m->m_pkthdr.len;
129 u_int32_t plen = sizeof(struct udphdr) + ulen;
132 struct in6_addr *laddr, *faddr;
135 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
137 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
139 struct sockaddr_in6 tmp;
141 priv = !priv_check(td, PRIV_ROOT); /* 1 if privileged, 0 if not */
143 if ((error = ip6_setpktoptions(control, &opt,
144 in6p->in6p_outputopts,
145 IPPROTO_UDP, priv)) != 0)
147 in6p->in6p_outputopts = &opt;
152 * IPv4 version of udp_output calls in_pcbconnect in this case,
153 * which needs splnet and affects performance.
154 * Since we saw no essential reason for calling in_pcbconnect,
155 * we get rid of such kind of logic, and call in6_selectsrc
156 * and in6_pcbsetport in order to fill in the local address
157 * and the local port.
159 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
160 if (sin6->sin6_port == 0) {
161 error = EADDRNOTAVAIL;
165 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
166 /* how about ::ffff:0.0.0.0 case? */
170 if (!prison_remote_ip(td, (struct sockaddr *)addr6)) {
171 error = EAFNOSUPPORT; /* IPv4 only jail */
175 /* protect *sin6 from overwrites */
179 faddr = &sin6->sin6_addr;
180 fport = sin6->sin6_port; /* allow 0 port */
182 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
183 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
185 * I believe we should explicitly discard the
186 * packet when mapped addresses are disabled,
187 * rather than send the packet as an IPv6 one.
188 * If we chose the latter approach, the packet
189 * might be sent out on the wire based on the
190 * default route, the situation which we'd
191 * probably want to avoid.
192 * (20010421 jinmei@kame.net)
200 /* KAME hack: embed scopeid */
201 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
206 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
207 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
210 &in6p->in6p_laddr, &error, NULL);
212 laddr = &in6p->in6p_laddr; /* XXX */
215 error = EADDRNOTAVAIL;
218 if (in6p->in6p_lport == 0 &&
219 (error = in6_pcbsetport(laddr, in6p, td)) != 0)
222 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
226 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
227 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
229 * XXX: this case would happen when the
230 * application sets the V6ONLY flag after
231 * connecting the foreign address.
232 * Such applications should be fixed,
235 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
236 "option was set for a connected socket\n");
242 laddr = &in6p->in6p_laddr;
243 faddr = &in6p->in6p_faddr;
244 fport = in6p->in6p_fport;
248 hlen = sizeof(struct ip);
251 * Calculate data length and get a mbuf
252 * for UDP and IP6 headers.
254 M_PREPEND(m, hlen + sizeof(struct udphdr), MB_DONTWAIT);
261 * Stuff checksum and output datagram.
263 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
264 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
265 udp6->uh_dport = fport;
267 udp6->uh_ulen = htons((u_short)plen);
274 ip6 = mtod(m, struct ip6_hdr *);
275 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
276 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
277 ip6->ip6_vfc |= IPV6_VERSION;
278 #if 0 /* ip6_plen will be filled in ip6_output. */
279 ip6->ip6_plen = htons((u_short)plen);
281 ip6->ip6_nxt = IPPROTO_UDP;
282 ip6->ip6_hlim = in6_selecthlim(in6p,
283 in6p->in6p_route.ro_rt ?
284 in6p->in6p_route.ro_rt->rt_ifp : NULL);
285 ip6->ip6_src = *laddr;
286 ip6->ip6_dst = *faddr;
288 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
289 sizeof(struct ip6_hdr), plen)) == 0) {
290 udp6->uh_sum = 0xffff;
295 udp6stat.udp6s_opackets++;
296 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
297 flags, in6p->in6p_moptions, NULL, in6p);
300 error = EAFNOSUPPORT;
310 ip6_clearpktopts(in6p->in6p_outputopts, -1);
311 in6p->in6p_outputopts = stickyopt;