1/*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29
30/*	$FreeBSD: src/sys/netinet6/udp6_output.c,v 1.1.2.3 2001/08/31 13:49:58 jlemon Exp $	*/
31/*	$KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $	*/
32
33/*
34 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
35 * 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 project 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 PROJECT 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 PROJECT 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/*
63 * Copyright (c) 1982, 1986, 1989, 1993
64 *	The Regents of the University of California.  All rights reserved.
65 *
66 * Redistribution and use in source and binary forms, with or without
67 * modification, are permitted provided that the following conditions
68 * are met:
69 * 1. Redistributions of source code must retain the above copyright
70 *    notice, this list of conditions and the following disclaimer.
71 * 2. Redistributions in binary form must reproduce the above copyright
72 *    notice, this list of conditions and the following disclaimer in the
73 *    documentation and/or other materials provided with the distribution.
74 * 3. All advertising materials mentioning features or use of this software
75 *    must display the following acknowledgement:
76 *	This product includes software developed by the University of
77 *	California, Berkeley and its contributors.
78 * 4. Neither the name of the University nor the names of its contributors
79 *    may be used to endorse or promote products derived from this software
80 *    without specific prior written permission.
81 *
82 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92 * SUCH DAMAGE.
93 *
94 *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
95 */
96
97#include <sys/param.h>
98#include <sys/malloc.h>
99#include <sys/mbuf.h>
100#include <sys/protosw.h>
101#include <sys/socket.h>
102#include <sys/socketvar.h>
103#include <sys/sysctl.h>
104#include <sys/errno.h>
105#include <sys/stat.h>
106#include <sys/systm.h>
107#include <sys/proc.h>
108#include <sys/syslog.h>
109
110#include <machine/endian.h>
111
112#include <net/if.h>
113#include <net/route.h>
114#include <net/if_types.h>
115#include <net/ntstat.h>
116
117#include <netinet/in.h>
118#include <netinet/in_var.h>
119#include <netinet/in_systm.h>
120#include <netinet/ip.h>
121#include <netinet/ip_var.h>
122#include <netinet/in_pcb.h>
123#include <netinet/udp.h>
124#include <netinet/udp_var.h>
125#include <netinet/ip6.h>
126#include <netinet6/ip6_var.h>
127#include <netinet6/in6_pcb.h>
128#include <netinet6/udp6_var.h>
129#include <netinet/icmp6.h>
130#include <netinet6/ip6protosw.h>
131
132#ifdef IPSEC
133#include <netinet6/ipsec.h>
134#ifdef INET6
135#include <netinet6/ipsec6.h>
136#endif
137extern int ipsec_bypass;
138#endif /*IPSEC*/
139
140#include <net/net_osdep.h>
141
142/*
143 * UDP protocol inplementation.
144 * Per RFC 768, August, 1980.
145 */
146
147#define in6pcb		inpcb
148#define udp6stat	udpstat
149#define udp6s_opackets	udps_opackets
150
151static __inline__ u_int16_t
152get_socket_id(struct socket * s)
153{
154	u_int16_t 		val;
155
156	if (s == NULL) {
157	    return (0);
158	}
159	val = (u_int16_t)(((uintptr_t)s) / sizeof(struct socket));
160	if (val == 0) {
161		val = 0xffff;
162	}
163	return (val);
164}
165
166int
167udp6_output(in6p, m, addr6, control, p)
168	struct in6pcb *in6p;
169	struct mbuf *m;
170	struct mbuf *control;
171	struct sockaddr *addr6;
172	struct proc *p;
173{
174	u_int32_t ulen = m->m_pkthdr.len;
175	u_int32_t plen = sizeof(struct udphdr) + ulen;
176	struct ip6_hdr *ip6;
177	struct udphdr *udp6;
178	struct in6_addr *laddr, *faddr;
179	u_short fport;
180	int error = 0;
181	struct ip6_pktopts opt, *optp = NULL;
182	struct ip6_moptions *im6o;
183	int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
184	int flags;
185	struct sockaddr_in6 tmp;
186	struct	in6_addr storage;
187	mbuf_svc_class_t msc = MBUF_SC_UNSPEC;
188	struct ip6_out_args ip6oa =
189	    { IFSCOPE_NONE, { 0 }, IP6OAF_SELECT_SRCIF };
190	struct flowadv *adv = &ip6oa.ip6oa_flowadv;
191	int flowadv = 0;
192
193	/* Enable flow advisory only when connected */
194	flowadv = (in6p->inp_socket->so_state & SS_ISCONNECTED) ? 1 : 0;
195
196	if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(in6p)) {
197		error = ENOBUFS;
198		goto release;
199	}
200
201	if (in6p->inp_flags & INP_BOUND_IF) {
202		ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
203		ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
204	}
205	if (in6p->inp_flags & INP_NO_IFT_CELLULAR)
206		ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
207
208	if (control) {
209		msc = mbuf_service_class_from_control(control);
210
211		if ((error = ip6_setpktopts(control, &opt, NULL, IPPROTO_UDP)) != 0)
212			goto release;
213		optp = &opt;
214	} else
215		optp = in6p->in6p_outputopts;
216
217	if (addr6) {
218		/*
219		 * IPv4 version of udp_output calls in_pcbconnect in this case,
220		 * which needs splnet and affects performance.
221		 * Since we saw no essential reason for calling in_pcbconnect,
222		 * we get rid of such kind of logic, and call in6_selectsrc
223		 * and in6_pcbsetport in order to fill in the local address
224		 * and the local port.
225		 */
226		struct sockaddr_in6 *sin6 =
227		    (struct sockaddr_in6 *)(void *)addr6;
228
229		if (sin6->sin6_port == 0) {
230			error = EADDRNOTAVAIL;
231			goto release;
232		}
233
234		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
235			/* how about ::ffff:0.0.0.0 case? */
236			error = EISCONN;
237			goto release;
238		}
239
240		/* protect *sin6 from overwrites */
241		tmp = *sin6;
242		sin6 = &tmp;
243
244		faddr = &sin6->sin6_addr;
245		fport = sin6->sin6_port; /* allow 0 port */
246
247		if (IN6_IS_ADDR_V4MAPPED(faddr)) {
248			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
249				/*
250				 * I believe we should explicitly discard the
251				 * packet when mapped addresses are disabled,
252				 * rather than send the packet as an IPv6 one.
253				 * If we chose the latter approach, the packet
254				 * might be sent out on the wire based on the
255				 * default route, the situation which we'd
256				 * probably want to avoid.
257				 * (20010421 jinmei@kame.net)
258				 */
259				error = EINVAL;
260				goto release;
261			} else
262				af = AF_INET;
263		}
264
265		/* KAME hack: embed scopeid */
266		if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL,
267		    optp) != 0) {
268			error = EINVAL;
269			goto release;
270		}
271
272		if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
273			laddr = in6_selectsrc(sin6, optp,
274			    in6p, &in6p->in6p_route, NULL, &storage,
275			    ip6oa.ip6oa_boundif, &error);
276		} else
277			laddr = &in6p->in6p_laddr;	/* XXX */
278		if (laddr == NULL) {
279			if (error == 0)
280				error = EADDRNOTAVAIL;
281			goto release;
282		}
283		if (in6p->in6p_lport == 0 &&
284		    (error = in6_pcbsetport(laddr, in6p, p, 0)) != 0)
285			goto release;
286	} else {
287		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
288			error = ENOTCONN;
289			goto release;
290		}
291		if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
292			if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY)) {
293				/*
294				 * XXX: this case would happen when the
295				 * application sets the V6ONLY flag after
296				 * connecting the foreign address.
297				 * Such applications should be fixed,
298				 * so we bark here.
299				 */
300				log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
301				    "option was set for a connected socket\n");
302				error = EINVAL;
303				goto release;
304			} else
305				af = AF_INET;
306		}
307		laddr = &in6p->in6p_laddr;
308		faddr = &in6p->in6p_faddr;
309		fport = in6p->in6p_fport;
310	}
311
312	if (in6p->inp_flowhash == 0)
313		in6p->inp_flowhash = inp_calc_flowhash(in6p);
314
315	if (af == AF_INET)
316		hlen = sizeof(struct ip);
317
318	/*
319	 * Calculate data length and get a mbuf
320	 * for UDP and IP6 headers.
321	 */
322	M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
323	if (m == 0) {
324		error = ENOBUFS;
325		goto release;
326	}
327
328	/*
329	 * Stuff checksum and output datagram.
330	 */
331	udp6 = (struct udphdr *)(void *)(mtod(m, caddr_t) + hlen);
332	udp6->uh_sport = in6p->in6p_lport; /* lport is always set in the PCB */
333	udp6->uh_dport = fport;
334	if (plen <= 0xffff)
335		udp6->uh_ulen = htons((u_short)plen);
336	else
337		udp6->uh_ulen = 0;
338	udp6->uh_sum = 0;
339
340	switch (af) {
341	case AF_INET6:
342		ip6 = mtod(m, struct ip6_hdr *);
343		ip6->ip6_flow	= in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
344		ip6->ip6_vfc 	&= ~IPV6_VERSION_MASK;
345		ip6->ip6_vfc 	|= IPV6_VERSION;
346#if 0				/* ip6_plen will be filled in ip6_output. */
347		ip6->ip6_plen	= htons((u_short)plen);
348#endif
349		ip6->ip6_nxt	= IPPROTO_UDP;
350		ip6->ip6_hlim	= in6_selecthlim(in6p,
351						 in6p->in6p_route.ro_rt ?
352						 in6p->in6p_route.ro_rt->rt_ifp : NULL);
353		ip6->ip6_src	= *laddr;
354		ip6->ip6_dst	= *faddr;
355
356		udp6->uh_sum = in6_cksum_phdr(laddr, faddr,
357		    htonl(plen), htonl(IPPROTO_UDP));
358		m->m_pkthdr.csum_flags = CSUM_UDPIPV6;
359		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
360
361		if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
362			ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
363
364		flags = IPV6_OUTARGS;
365
366		udp6stat.udp6s_opackets++;
367#ifdef IPSEC
368		if (ipsec_bypass == 0 && ipsec_setsocket(m, in6p->in6p_socket) != 0) {
369			error = ENOBUFS;
370			goto release;
371		}
372#endif /*IPSEC*/
373		m->m_pkthdr.socket_id = get_socket_id(in6p->in6p_socket);
374
375		set_packet_service_class(m, in6p->in6p_socket, msc, PKT_SCF_IPV6);
376
377		m->m_pkthdr.m_flowhash = in6p->inp_flowhash;
378		m->m_pkthdr.m_fhflags |= PF_TAG_FLOWHASH;
379		if (flowadv)
380			m->m_pkthdr.m_fhflags |= PF_TAG_FLOWADV;
381
382		im6o = in6p->in6p_moptions;
383		if (im6o != NULL) {
384			IM6O_LOCK(im6o);
385			IM6O_ADDREF_LOCKED(im6o);
386			if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
387			    im6o->im6o_multicast_ifp != NULL) {
388				in6p->in6p_last_outifp = im6o->im6o_multicast_ifp;
389			}
390			IM6O_UNLOCK(im6o);
391		}
392
393		in6p->inp_sndinprog_cnt++;
394
395		error = ip6_output(m, optp, &in6p->in6p_route,
396		    flags, im6o, NULL, &ip6oa);
397
398		if (im6o != NULL)
399			IM6O_REMREF(im6o);
400
401		if (error == 0 && nstat_collect) {
402			locked_add_64(&in6p->inp_stat->txpackets, 1);
403			locked_add_64(&in6p->inp_stat->txbytes, ulen);
404		}
405
406		if (flowadv && (adv->code == FADV_FLOW_CONTROLLED ||
407		    adv->code == FADV_SUSPENDED)) {
408			/* return an error to indicate
409			 * that the packet has been dropped
410			 */
411			error = ENOBUFS;
412			inp_set_fc_state(in6p, adv->code);
413		}
414
415		VERIFY(in6p->inp_sndinprog_cnt > 0);
416		if ( --in6p->inp_sndinprog_cnt == 0)
417			in6p->inp_flags &= ~(INP_FC_FEEDBACK);
418
419		if (in6p->in6p_route.ro_rt != NULL) {
420			struct rtentry *rt = in6p->in6p_route.ro_rt;
421			struct ifnet *outif;
422
423			if ((rt->rt_flags & RTF_MULTICAST) ||
424			    in6p->in6p_socket == NULL ||
425			    !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
426				rt = NULL;	/* unusable */
427			}
428			/*
429			 * Always discard the cached route for unconnected
430			 * socket or if it is a multicast route.
431			 */
432			if (rt == NULL) {
433				rtfree(in6p->in6p_route.ro_rt);
434				in6p->in6p_route.ro_rt = NULL;
435			}
436			/*
437			 * If this is a connected socket and the destination
438			 * route is not multicast, update outif with that of
439			 * the route interface used by IP.
440			 */
441			if (rt != NULL &&
442			    (outif = rt->rt_ifp) != in6p->in6p_last_outifp)
443				in6p->in6p_last_outifp = outif;
444		}
445		break;
446	case AF_INET:
447		error = EAFNOSUPPORT;
448		goto release;
449	}
450	goto releaseopt;
451
452release:
453	m_freem(m);
454
455releaseopt:
456	if (control) {
457		if (optp == &opt)
458			ip6_clearpktopts(optp, -1);
459		m_freem(control);
460	}
461	return(error);
462}
463