1 /* $NetBSD: udp6_output.c,v 1.17.2.1 2004/06/14 18:00:48 tron Exp $ */
2 /* $KAME: udp6_output.c,v 1.43 2001/10/15 09:19:52 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/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: udp6_output.c,v 1.17.2.1 2004/06/14 18:00:48 tron Exp $");
66
67 #include "opt_inet.h"
68
69 #include <sys/param.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.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 #include "faith.h"
101
102 #include <net/net_osdep.h>
103
104 /*
105 * UDP protocol inplementation.
106 * Per RFC 768, August, 1980.
107 */
108
109 int
110 udp6_output(in6p, m, addr6, control, p)
111 struct in6pcb *in6p;
112 struct mbuf *m;
113 struct mbuf *addr6, *control;
114 struct proc *p;
115 {
116 u_int32_t ulen = m->m_pkthdr.len;
117 u_int32_t plen = sizeof(struct udphdr) + ulen;
118 struct ip6_hdr *ip6;
119 struct udphdr *udp6;
120 struct in6_addr *laddr, *faddr;
121 struct in6_addr laddr_mapped; /* XXX ugly */
122 u_int16_t fport;
123 int error = 0;
124 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
125 int priv;
126 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
127 #ifdef INET
128 struct ip *ip;
129 struct udpiphdr *ui;
130 #endif
131 int flags = 0;
132 struct sockaddr_in6 tmp;
133
134 priv = 0;
135 if (p && !suser(p->p_ucred, &p->p_acflag))
136 priv = 1;
137 if (control) {
138 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
139 goto release;
140 in6p->in6p_outputopts = &opt;
141 }
142
143 if (addr6) {
144 /*
145 * IPv4 version of udp_output calls in_pcbconnect in this case,
146 * which needs splnet and affects performance.
147 * Since we saw no essential reason for calling in_pcbconnect,
148 * we get rid of such kind of logic, and call in6_selectsrc
149 * and in6_pcbsetport in order to fill in the local address
150 * and the local port.
151 */
152 struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *);
153
154 if (addr6->m_len != sizeof(*sin6)) {
155 error = EINVAL;
156 goto release;
157 }
158 if (sin6->sin6_family != AF_INET6) {
159 error = EAFNOSUPPORT;
160 goto release;
161 }
162 if (sin6->sin6_port == 0) {
163 error = EADDRNOTAVAIL;
164 goto release;
165 }
166
167 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
168 /* how about ::ffff:0.0.0.0 case? */
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 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
181 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
182 {
183 /*
184 * I believe we should explicitly discard the
185 * packet when mapped addresses are disabled,
186 * rather than send the packet as an IPv6 one.
187 * If we chose the latter approach, the packet
188 * might be sent out on the wire based on the
189 * default route, the situation which we'd
190 * probably want to avoid.
191 * (20010421 jinmei@kame.net)
192 */
193 error = EINVAL;
194 goto release;
195 }
196 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
197 && !IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
198 /*
199 * when remote addr is an IPv4-mapped address,
200 * local addr should not be an IPv6 address,
201 * since you cannot determine how to map IPv6
202 * source address to IPv4.
203 */
204 error = EINVAL;
205 goto release;
206 }
207
208 af = AF_INET;
209 }
210
211 /* KAME hack: embed scopeid */
212 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
213 error = EINVAL;
214 goto release;
215 }
216
217 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
218 laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
219 in6p->in6p_moptions,
220 &in6p->in6p_route,
221 &in6p->in6p_laddr, &error);
222 } else {
223 /*
224 * XXX: freebsd[34] does not have in_selectsrc, but
225 * we can omit the whole part because freebsd4 calls
226 * udp_output() directly in this case, and thus we'll
227 * never see this path.
228 */
229 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
230 struct sockaddr_in *sinp, sin_dst;
231
232 bzero(&sin_dst, sizeof(sin_dst));
233 sin_dst.sin_family = AF_INET;
234 sin_dst.sin_len = sizeof(sin_dst);
235 bcopy(&faddr->s6_addr[12], &sin_dst.sin_addr,
236 sizeof(sin_dst.sin_addr));
237 sinp = in_selectsrc(&sin_dst,
238 (struct route *)&in6p->in6p_route,
239 in6p->in6p_socket->so_options,
240 NULL, &error);
241 if (sinp == NULL) {
242 if (error == 0)
243 error = EADDRNOTAVAIL;
244 goto release;
245 }
246 bzero(&laddr_mapped, sizeof(laddr_mapped));
247 laddr_mapped.s6_addr16[5] = 0xffff; /* ugly */
248 bcopy(&sinp->sin_addr,
249 &laddr_mapped.s6_addr[12],
250 sizeof(sinp->sin_addr));
251 laddr = &laddr_mapped;
252 } else
253 {
254 laddr = &in6p->in6p_laddr; /* XXX */
255 }
256 }
257 if (laddr == NULL) {
258 if (error == 0)
259 error = EADDRNOTAVAIL;
260 goto release;
261 }
262 if (in6p->in6p_lport == 0 &&
263 (error = in6_pcbsetport(laddr, in6p, p)) != 0)
264 goto release;
265 } else {
266 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
267 error = ENOTCONN;
268 goto release;
269 }
270 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
271 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
272 {
273 /*
274 * XXX: this case would happen when the
275 * application sets the V6ONLY flag after
276 * connecting the foreign address.
277 * Such applications should be fixed,
278 * so we bark here.
279 */
280 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
281 "option was set for a connected socket\n");
282 error = EINVAL;
283 goto release;
284 } else
285 af = AF_INET;
286 }
287 laddr = &in6p->in6p_laddr;
288 faddr = &in6p->in6p_faddr;
289 fport = in6p->in6p_fport;
290 }
291
292 if (af == AF_INET)
293 hlen = sizeof(struct ip);
294
295 /*
296 * Calculate data length and get a mbuf
297 * for UDP and IP6 headers.
298 */
299 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
300 if (m == 0) {
301 error = ENOBUFS;
302 goto release;
303 }
304
305 /*
306 * Stuff checksum and output datagram.
307 */
308 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
309 udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
310 udp6->uh_dport = fport;
311 if (plen <= 0xffff)
312 udp6->uh_ulen = htons((u_int16_t)plen);
313 else
314 udp6->uh_ulen = 0;
315 udp6->uh_sum = 0;
316
317 switch (af) {
318 case AF_INET6:
319 ip6 = mtod(m, struct ip6_hdr *);
320 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
321 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
322 ip6->ip6_vfc |= IPV6_VERSION;
323 #if 0 /* ip6_plen will be filled in ip6_output. */
324 ip6->ip6_plen = htons((u_int16_t)plen);
325 #endif
326 ip6->ip6_nxt = IPPROTO_UDP;
327 ip6->ip6_hlim = in6_selecthlim(in6p,
328 in6p->in6p_route.ro_rt ?
329 in6p->in6p_route.ro_rt->rt_ifp : NULL);
330 ip6->ip6_src = *laddr;
331 ip6->ip6_dst = *faddr;
332
333 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
334 sizeof(struct ip6_hdr), plen)) == 0) {
335 udp6->uh_sum = 0xffff;
336 }
337
338 if (in6p->in6p_flags & IN6P_MINMTU)
339 flags |= IPV6_MINMTU;
340
341 udp6stat.udp6s_opackets++;
342 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
343 flags, in6p->in6p_moptions, in6p->in6p_socket, NULL);
344 break;
345 case AF_INET:
346 #ifdef INET
347 /* can't transmit jumbogram over IPv4 */
348 if (plen > 0xffff) {
349 error = EMSGSIZE;
350 goto release;
351 }
352
353 ip = mtod(m, struct ip *);
354 ui = (struct udpiphdr *)ip;
355 bzero(ui->ui_x1, sizeof ui->ui_x1);
356 ui->ui_pr = IPPROTO_UDP;
357 ui->ui_len = htons(plen);
358 bcopy(&laddr->s6_addr[12], &ui->ui_src, sizeof(ui->ui_src));
359 ui->ui_ulen = ui->ui_len;
360
361 flags = (in6p->in6p_socket->so_options &
362 (SO_DONTROUTE | SO_BROADCAST));
363 bcopy(&faddr->s6_addr[12], &ui->ui_dst, sizeof(ui->ui_dst));
364 udp6->uh_sum = in_cksum(m, hlen + plen);
365 if (udp6->uh_sum == 0)
366 udp6->uh_sum = 0xffff;
367
368 ip->ip_len = htons(hlen + plen);
369 ip->ip_ttl = in6_selecthlim(in6p, NULL); /* XXX */
370 ip->ip_tos = 0; /* XXX */
371
372 udpstat.udps_opackets++;
373 error = ip_output(m, NULL, &in6p->in6p_route, flags /* XXX */,
374 (struct ip_moptions *)NULL,
375 (struct socket *)in6p->in6p_socket);
376 break;
377 #else
378 error = EAFNOSUPPORT;
379 goto release;
380 #endif
381 }
382 goto releaseopt;
383
384 release:
385 m_freem(m);
386
387 releaseopt:
388 if (control) {
389 in6p->in6p_outputopts = stickyopt;
390 m_freem(control);
391 }
392 return (error);
393 }
Cache object: cedfb41eb4649d90f5ced407a9623fde
|