xref: /openbsd-src/sys/netinet6/udp6_output.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: udp6_output.c,v 1.5 2001/12/06 04:19:26 itojun 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 #include "faith.h"
97 
98 /*
99  * UDP protocol inplementation.
100  * Per RFC 768, August, 1980.
101  */
102 
103 #define in6pcb		inpcb
104 #define in6p_outputopts	inp_outputopts6
105 #define in6p_socket	inp_socket
106 #define in6p_faddr	inp_faddr6
107 #define in6p_laddr	inp_laddr6
108 #define in6p_fport	inp_fport
109 #define in6p_lport	inp_lport
110 #define in6p_flags	inp_flags
111 #define in6p_moptions	inp_moptions6
112 #define in6p_route	inp_route6
113 #define in6p_flowinfo	inp_flowinfo
114 #define udp6stat	udpstat
115 #define udp6s_opackets	udps_opackets
116 
117 int
118 udp6_output(in6p, m, addr6, control)
119 	struct in6pcb *in6p;
120 	struct mbuf *m;
121 	struct mbuf *addr6, *control;
122 {
123 	u_int32_t ulen = m->m_pkthdr.len;
124 	u_int32_t plen = sizeof(struct udphdr) + ulen;
125 	struct ip6_hdr *ip6;
126 	struct udphdr *udp6;
127 	struct	in6_addr *laddr, *faddr;
128 	u_short fport;
129 	int error = 0;
130 	struct ip6_pktopts opt, *stickyopt;
131 	int priv;
132 	int af, hlen;
133 	int flags;
134 	struct sockaddr_in6 tmp;
135 
136 	priv = 0;
137 	if ((in6p->in6p_socket->so_state & SS_PRIV) != 0)
138 		priv = 1;
139 	stickyopt = in6p->in6p_outputopts;
140 	if (control) {
141 		if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
142 			goto release;
143 		in6p->in6p_outputopts = &opt;
144 	}
145 
146 	if (addr6) {
147 		/*
148 		 * IPv4 version of udp_output calls in_pcbconnect in this case,
149 		 * which needs splnet and affects performance.
150 		 * Since we saw no essential reason for calling in_pcbconnect,
151 		 * we get rid of such kind of logic, and call in6_selectsrc
152 		 * and in6_pcbsetport in order to fill in the local address
153 		 * and the local port.
154 		 */
155 		struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *);
156 
157 		if (addr6->m_len != sizeof(*sin6)) {
158 			error = EINVAL;
159 			goto release;
160 		}
161 		if (sin6->sin6_family != AF_INET6) {
162 			error = EAFNOSUPPORT;
163 			goto release;
164 		}
165 		if (sin6->sin6_port == 0) {
166 			error = EADDRNOTAVAIL;
167 			goto release;
168 		}
169 
170 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
171 			error = EISCONN;
172 			goto release;
173 		}
174 
175 		/* protect *sin6 from overwrites */
176 		tmp = *sin6;
177 		sin6 = &tmp;
178 
179 		faddr = &sin6->sin6_addr;
180 		fport = sin6->sin6_port; /* allow 0 port */
181 
182 		/* KAME hack: embed scopeid */
183 		if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
184 			error = EINVAL;
185 			goto release;
186 		}
187 
188 		if (1)	/* we don't support IPv4 mapped address */
189 		{
190 			laddr = in6_selectsrc(sin6, in6p->in6p_outputopts,
191 					      in6p->in6p_moptions,
192 					      &in6p->in6p_route,
193 					      &in6p->in6p_laddr, &error);
194 		} else
195 			laddr = &in6p->in6p_laddr;	/*XXX*/
196 		if (laddr == NULL) {
197 			if (error == 0)
198 				error = EADDRNOTAVAIL;
199 			goto release;
200 		}
201 		if (in6p->in6p_lport == 0 &&
202 		    (error = in6_pcbsetport(laddr, in6p)) != 0)
203 			goto release;
204 	} else {
205 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
206 			error = ENOTCONN;
207 			goto release;
208 		}
209 		laddr = &in6p->in6p_laddr;
210 		faddr = &in6p->in6p_faddr;
211 		fport = in6p->in6p_fport;
212 	}
213 
214 	if (1)	/* we don't support IPv4 mapped address */
215 	{
216 		af = AF_INET6;
217 		hlen = sizeof(struct ip6_hdr);
218 	} else {
219 		af = AF_INET;
220 		hlen = sizeof(struct ip);
221 	}
222 
223 	/*
224 	 * Calculate data length and get a mbuf
225 	 * for UDP and IP6 headers.
226 	 */
227 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
228 	if (m == 0) {
229 		error = ENOBUFS;
230 		goto release;
231 	}
232 
233 	/*
234 	 * Stuff checksum and output datagram.
235 	 */
236 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
237 	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
238 	udp6->uh_dport = fport;
239 	if (plen <= 0xffff)
240 		udp6->uh_ulen = htons((u_short)plen);
241 	else
242 		udp6->uh_ulen = 0;
243 	udp6->uh_sum = 0;
244 
245 	switch (af) {
246 	case AF_INET6:
247 		ip6 = mtod(m, struct ip6_hdr *);
248 		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
249 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
250 		ip6->ip6_vfc 	|= IPV6_VERSION;
251 #if 0				/* ip6_plen will be filled in ip6_output. */
252 		ip6->ip6_plen	= htons((u_short)plen);
253 #endif
254 		ip6->ip6_nxt	= IPPROTO_UDP;
255 		ip6->ip6_hlim	= in6_selecthlim(in6p,
256 						 in6p->in6p_route.ro_rt ?
257 						 in6p->in6p_route.ro_rt->rt_ifp : NULL);
258 		ip6->ip6_src	= *laddr;
259 		ip6->ip6_dst	= *faddr;
260 
261 		if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
262 				sizeof(struct ip6_hdr), plen)) == 0) {
263 			udp6->uh_sum = 0xffff;
264 		}
265 
266 		flags = 0;
267 #ifdef IPV6_MINMTU
268 		if (in6p->in6p_flags & IN6P_MINMTU)
269 			flags |= IPV6_MINMTU;
270 #endif
271 
272 		udp6stat.udp6s_opackets++;
273 		error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
274 			    flags, in6p->in6p_moptions, NULL);
275 		break;
276 	case AF_INET:
277 		error = EAFNOSUPPORT;
278 		goto release;
279 	}
280 	goto releaseopt;
281 
282 release:
283 	m_freem(m);
284 
285 releaseopt:
286 	if (control) {
287 		in6p->in6p_outputopts = stickyopt;
288 		m_freem(control);
289 	}
290 	return(error);
291 }
292