xref: /openbsd-src/sys/netinet6/udp6_output.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: udp6_output.c,v 1.23 2013/10/23 19:57:50 deraadt 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. 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 
62 #include <sys/param.h>
63 #include <sys/malloc.h>
64 #include <sys/mbuf.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/errno.h>
69 #include <sys/stat.h>
70 #include <sys/systm.h>
71 #include <sys/syslog.h>
72 
73 #include <net/if.h>
74 #include <net/route.h>
75 #include <net/if_types.h>
76 
77 #include <netinet/in.h>
78 #include <netinet6/in6_var.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/udp.h>
84 #include <netinet/udp_var.h>
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet/icmp6.h>
88 #include <netinet6/ip6protosw.h>
89 
90 /*
91  * UDP protocol inplementation.
92  * Per RFC 768, August, 1980.
93  */
94 int
95 udp6_output(struct inpcb *in6p, struct mbuf *m, struct mbuf *addr6,
96 	struct mbuf *control)
97 {
98 	u_int32_t ulen = m->m_pkthdr.len;
99 	u_int32_t plen = sizeof(struct udphdr) + ulen;
100 	int error = 0, priv = 0, af, hlen, flags;
101 	struct ip6_hdr *ip6;
102 	struct udphdr *udp6;
103 	struct in6_addr *laddr, *faddr;
104 	struct ip6_pktopts *optp, opt;
105 	struct sockaddr_in6 tmp;
106 	struct proc *p = curproc;	/* XXX */
107 	struct ifnet *ifp;
108 	u_short fport;
109 
110 	if ((in6p->inp_socket->so_state & SS_PRIV) != 0)
111 		priv = 1;
112 	if (control) {
113 		if ((error = ip6_setpktopts(control, &opt,
114 		    in6p->inp_outputopts6, priv, IPPROTO_UDP)) != 0)
115 			goto release;
116 		optp = &opt;
117 	} else
118 		optp = in6p->inp_outputopts6;
119 
120 	if (addr6) {
121 		/*
122 		 * IPv4 version of udp_output calls in_pcbconnect in this case,
123 		 * which needs splnet and affects performance.
124 		 * Since we saw no essential reason for calling in_pcbconnect,
125 		 * we get rid of such kind of logic, and call in6_selectsrc
126 		 * and in6_pcbsetport in order to fill in the local address
127 		 * and the local port.
128 		 */
129 		struct sockaddr_in6 *sin6 = mtod(addr6, struct sockaddr_in6 *);
130 
131 		if (addr6->m_len != sizeof(*sin6)) {
132 			error = EINVAL;
133 			goto release;
134 		}
135 		if (sin6->sin6_family != AF_INET6) {
136 			error = EAFNOSUPPORT;
137 			goto release;
138 		}
139 		if (sin6->sin6_port == 0) {
140 			error = EADDRNOTAVAIL;
141 			goto release;
142 		}
143 
144 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6)) {
145 			error = EISCONN;
146 			goto release;
147 		}
148 
149 		/* protect *sin6 from overwrites */
150 		tmp = *sin6;
151 		sin6 = &tmp;
152 
153 		faddr = &sin6->sin6_addr;
154 		fport = sin6->sin6_port; /* allow 0 port */
155 
156 		/* KAME hack: embed scopeid */
157 		if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0) {
158 			error = EINVAL;
159 			goto release;
160 		}
161 
162 		if (1) {	/* we don't support IPv4 mapped address */
163 			laddr = in6_selectsrc(sin6, optp,
164 			    in6p->inp_moptions6, &in6p->inp_route6,
165 			    &in6p->inp_laddr6, &error, in6p->inp_rtableid);
166 		} else
167 			laddr = &in6p->inp_laddr6;	/*XXX*/
168 		if (laddr == NULL) {
169 			if (error == 0)
170 				error = EADDRNOTAVAIL;
171 			goto release;
172 		}
173 		if (in6p->inp_lport == 0 &&
174 		    (error = in6_pcbsetport(laddr, in6p, p)) != 0)
175 			goto release;
176 	} else {
177 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->inp_faddr6)) {
178 			error = ENOTCONN;
179 			goto release;
180 		}
181 		laddr = &in6p->inp_laddr6;
182 		faddr = &in6p->inp_faddr6;
183 		fport = in6p->inp_fport;
184 	}
185 
186 	if (1) {	/* we don't support IPv4 mapped address */
187 		af = AF_INET6;
188 		hlen = sizeof(struct ip6_hdr);
189 	} else {
190 		af = AF_INET;
191 		hlen = sizeof(struct ip);
192 	}
193 
194 	/*
195 	 * Calculate data length and get a mbuf
196 	 * for UDP and IP6 headers.
197 	 */
198 	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
199 	if (m == 0) {
200 		error = ENOBUFS;
201 		goto release;
202 	}
203 
204 	/*
205 	 * Stuff checksum and output datagram.
206 	 */
207 	udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
208 	udp6->uh_sport = in6p->inp_lport; /* lport is always set in the PCB */
209 	udp6->uh_dport = fport;
210 	if (plen <= 0xffff)
211 		udp6->uh_ulen = htons((u_short)plen);
212 	else
213 		udp6->uh_ulen = 0;
214 	udp6->uh_sum = 0;
215 
216 	switch (af) {
217 	case AF_INET6:
218 		ip6 = mtod(m, struct ip6_hdr *);
219 		ip6->ip6_flow	= in6p->inp_flowinfo & IPV6_FLOWINFO_MASK;
220 		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
221 		ip6->ip6_vfc 	|= IPV6_VERSION;
222 #if 0				/* ip6_plen will be filled in ip6_output. */
223 		ip6->ip6_plen	= htons((u_short)plen);
224 #endif
225 		ip6->ip6_nxt	= IPPROTO_UDP;
226 		ifp = NULL;
227 		if (in6p->inp_route6.ro_rt &&
228 		    in6p->inp_route6.ro_rt->rt_flags & RTF_UP)
229 			ifp = in6p->inp_route6.ro_rt->rt_ifp;
230 		ip6->ip6_hlim	= in6_selecthlim(in6p, ifp);
231 		ip6->ip6_src	= *laddr;
232 		ip6->ip6_dst	= *faddr;
233 
234 		m->m_pkthdr.csum_flags |= M_UDP_CSUM_OUT;
235 
236 		flags = 0;
237 		if (in6p->inp_flags & IN6P_MINMTU)
238 			flags |= IPV6_MINMTU;
239 
240 		udpstat.udps_opackets++;
241 
242 		/* force routing domain */
243 		m->m_pkthdr.rdomain = in6p->inp_rtableid;
244 
245 		error = ip6_output(m, optp, &in6p->inp_route6,
246 		    flags, in6p->inp_moptions6, NULL, in6p);
247 		break;
248 	case AF_INET:
249 		error = EAFNOSUPPORT;
250 		goto release;
251 	}
252 	goto releaseopt;
253 
254 release:
255 	m_freem(m);
256 
257 releaseopt:
258 	if (control) {
259 		ip6_clearpktopts(&opt, -1);
260 		m_freem(control);
261 	}
262 	return (error);
263 }
264