]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/udp6_output.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / bsd / netinet6 / udp6_output.c
CommitLineData
9bccf70c
A
1/* $FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.3 2001/08/31 13:49:58 jlemon Exp $ */
2/* $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei 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/sysctl.h>
75#include <sys/errno.h>
76#include <sys/stat.h>
77#include <sys/systm.h>
78#include <sys/proc.h>
79#include <sys/syslog.h>
80
81#include <net/if.h>
82#include <net/route.h>
83#include <net/if_types.h>
84
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>
99
100#ifdef IPSEC
101#include <netinet6/ipsec.h>
102#ifdef INET6
103#include <netinet6/ipsec6.h>
104#endif
105extern int ipsec_bypass;
106#endif /*IPSEC*/
107
108#include "faith.h"
109
110#include <net/net_osdep.h>
111
112/*
113 * UDP protocol inplementation.
114 * Per RFC 768, August, 1980.
115 */
116
117#define in6pcb inpcb
118#define udp6stat udpstat
119#define udp6s_opackets udps_opackets
120
91447636
A
121static __inline__ u_int16_t
122get_socket_id(struct socket * s)
123{
124 u_int16_t val;
125
126 if (s == NULL) {
127 return (0);
128 }
129 val = (u_int16_t)(((u_int32_t)s) / sizeof(struct socket));
130 if (val == 0) {
131 val = 0xffff;
132 }
133 return (val);
134}
135
9bccf70c
A
136int
137udp6_output(in6p, m, addr6, control, p)
138 struct in6pcb *in6p;
139 struct mbuf *m;
140 struct mbuf *control;
141 struct sockaddr *addr6;
142 struct proc *p;
143{
144 u_int32_t ulen = m->m_pkthdr.len;
145 u_int32_t plen = sizeof(struct udphdr) + ulen;
146 struct ip6_hdr *ip6;
147 struct udphdr *udp6;
148 struct in6_addr *laddr, *faddr;
149 u_short fport;
150 int error = 0;
151 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
152 int priv;
153 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
154 int flags;
155 struct sockaddr_in6 tmp;
91447636 156 struct in6_addr storage;
9bccf70c
A
157
158 priv = 0;
159#ifdef __APPLE__
91447636 160 if (p && !proc_suser(p))
9bccf70c
A
161#else
162 if (p && !suser(p))
163#endif
164 priv = 1;
165 if (control) {
166 if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0)
167 goto release;
168 in6p->in6p_outputopts = &opt;
169 }
170
171 if (addr6) {
172 /*
173 * IPv4 version of udp_output calls in_pcbconnect in this case,
174 * which needs splnet and affects performance.
175 * Since we saw no essential reason for calling in_pcbconnect,
176 * we get rid of such kind of logic, and call in6_selectsrc
177 * and in6_pcbsetport in order to fill in the local address
178 * and the local port.
179 */
180 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addr6;
181 if (sin6->sin6_port == 0) {
182 error = EADDRNOTAVAIL;
183 goto release;
184 }
185
186 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
187 /* how about ::ffff:0.0.0.0 case? */
188 error = EISCONN;
189 goto release;
190 }
191
192 /* protect *sin6 from overwrites */
193 tmp = *sin6;
194 sin6 = &tmp;
195
196 faddr = &sin6->sin6_addr;
197 fport = sin6->sin6_port; /* allow 0 port */
198
199 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
200 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
201 /*
202 * I believe we should explicitly discard the
203 * packet when mapped addresses are disabled,
204 * rather than send the packet as an IPv6 one.
205 * If we chose the latter approach, the packet
206 * might be sent out on the wire based on the
207 * default route, the situation which we'd
208 * probably want to avoid.
209 * (20010421 jinmei@kame.net)
210 */
211 error = EINVAL;
212 goto release;
213 } else
214 af = AF_INET;
215 }
216
217 /* KAME hack: embed scopeid */
218 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
219 error = EINVAL;
220 goto release;
221 }
222
223 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
224 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
225 in6p->in6p_moptions,
226 &in6p->in6p_route,
91447636 227 &in6p->in6p_laddr, &storage, &error);
9bccf70c
A
228 } else
229 laddr = &in6p->in6p_laddr; /* XXX */
230 if (laddr == NULL) {
231 if (error == 0)
232 error = EADDRNOTAVAIL;
233 goto release;
234 }
235 if (in6p->in6p_lport == 0 &&
91447636 236 (error = in6_pcbsetport(laddr, in6p, p, 0)) != 0)
9bccf70c
A
237 goto release;
238 } else {
239 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
240 error = ENOTCONN;
241 goto release;
242 }
243 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
244 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
245 /*
246 * XXX: this case would happen when the
247 * application sets the V6ONLY flag after
248 * connecting the foreign address.
249 * Such applications should be fixed,
250 * so we bark here.
251 */
252 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
253 "option was set for a connected socket\n");
254 error = EINVAL;
255 goto release;
256 } else
257 af = AF_INET;
258 }
259 laddr = &in6p->in6p_laddr;
260 faddr = &in6p->in6p_faddr;
261 fport = in6p->in6p_fport;
262 }
263
264 if (af == AF_INET)
265 hlen = sizeof(struct ip);
266
267 /*
268 * Calculate data length and get a mbuf
269 * for UDP and IP6 headers.
270 */
271 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
272 if (m == 0) {
273 error = ENOBUFS;
274 goto release;
275 }
276
277 /*
278 * Stuff checksum and output datagram.
279 */
280 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
281 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
282 udp6->uh_dport = fport;
283 if (plen <= 0xffff)
284 udp6->uh_ulen = htons((u_short)plen);
285 else
286 udp6->uh_ulen = 0;
287 udp6->uh_sum = 0;
288
289 switch (af) {
290 case AF_INET6:
291 ip6 = mtod(m, struct ip6_hdr *);
292 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
293 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
294 ip6->ip6_vfc |= IPV6_VERSION;
295#if 0 /* ip6_plen will be filled in ip6_output. */
296 ip6->ip6_plen = htons((u_short)plen);
297#endif
298 ip6->ip6_nxt = IPPROTO_UDP;
299 ip6->ip6_hlim = in6_selecthlim(in6p,
300 in6p->in6p_route.ro_rt ?
301 in6p->in6p_route.ro_rt->rt_ifp : NULL);
302 ip6->ip6_src = *laddr;
303 ip6->ip6_dst = *faddr;
304
305 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
306 sizeof(struct ip6_hdr), plen)) == 0) {
307 udp6->uh_sum = 0xffff;
308 }
309
310 flags = 0;
311
312 udp6stat.udp6s_opackets++;
313#ifdef IPSEC
314 if (ipsec_bypass == 0 && ipsec_setsocket(m, in6p->in6p_socket) != 0) {
315 error = ENOBUFS;
316 goto release;
317 }
318#endif /*IPSEC*/
91447636 319 m->m_pkthdr.socket_id = get_socket_id(in6p->in6p_socket);
9bccf70c 320 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
91447636 321 flags, in6p->in6p_moptions, NULL, 0);
9bccf70c
A
322 break;
323 case AF_INET:
324 error = EAFNOSUPPORT;
325 goto release;
326 }
327 goto releaseopt;
328
329release:
330 m_freem(m);
331
332releaseopt:
333 if (control) {
334 ip6_clearpktopts(in6p->in6p_outputopts, 0, -1);
335 in6p->in6p_outputopts = stickyopt;
336 m_freem(control);
337 }
338 return(error);
339}