udp6_usrreq.c revision 1.107
1/*	$NetBSD: udp6_usrreq.c,v 1.107 2014/07/09 14:41:42 rtr Exp $	*/
2/*	$KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 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_usrreq.c,v 1.107 2014/07/09 14:41:42 rtr Exp $");
66
67#include "opt_inet.h"
68#include "opt_inet_csum.h"
69
70#include <sys/param.h>
71#include <sys/mbuf.h>
72#include <sys/protosw.h>
73#include <sys/socket.h>
74#include <sys/socketvar.h>
75#include <sys/systm.h>
76#include <sys/proc.h>
77#include <sys/syslog.h>
78#include <sys/domain.h>
79#include <sys/sysctl.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/in_offload.h>
89#include <netinet/ip.h>
90#include <netinet/ip_var.h>
91#include <netinet/in_pcb.h>
92#include <netinet/udp.h>
93#include <netinet/udp_var.h>
94#include <netinet/udp_private.h>
95
96#include <netinet/ip6.h>
97#include <netinet/icmp6.h>
98#include <netinet6/ip6_var.h>
99#include <netinet6/ip6_private.h>
100#include <netinet6/in6_pcb.h>
101#include <netinet6/udp6_var.h>
102#include <netinet6/udp6_private.h>
103#include <netinet6/ip6protosw.h>
104#include <netinet6/scope6_var.h>
105
106#include "faith.h"
107#if defined(NFAITH) && NFAITH > 0
108#include <net/if_faith.h>
109#endif
110
111/*
112 * UDP protocol implementation.
113 * Per RFC 768, August, 1980.
114 */
115
116extern struct inpcbtable udbtable;
117
118percpu_t *udp6stat_percpu;
119
120/* UDP on IP6 parameters */
121static int	udp6_sendspace = 9216;	/* really max datagram size */
122static int	udp6_recvspace = 40 * (1024 + sizeof(struct sockaddr_in6));
123					/* 40 1K datagrams */
124
125static	void udp6_notify(struct in6pcb *, int);
126static	void sysctl_net_inet6_udp6_setup(struct sysctllog **);
127
128#ifdef UDP_CSUM_COUNTERS
129#include <sys/device.h>
130struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
131    NULL, "udp6", "hwcsum bad");
132struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
133    NULL, "udp6", "hwcsum ok");
134struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
135    NULL, "udp6", "hwcsum data");
136struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
137    NULL, "udp6", "swcsum");
138
139EVCNT_ATTACH_STATIC(udp6_hwcsum_bad);
140EVCNT_ATTACH_STATIC(udp6_hwcsum_ok);
141EVCNT_ATTACH_STATIC(udp6_hwcsum_data);
142EVCNT_ATTACH_STATIC(udp6_swcsum);
143
144#define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
145#else
146#define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
147#endif
148
149void
150udp6_init(void)
151{
152	sysctl_net_inet6_udp6_setup(NULL);
153	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
154
155	udp_init_common();
156}
157
158/*
159 * Notify a udp user of an asynchronous error;
160 * just wake up so that he can collect error status.
161 */
162static	void
163udp6_notify(struct in6pcb *in6p, int errno)
164{
165	in6p->in6p_socket->so_error = errno;
166	sorwakeup(in6p->in6p_socket);
167	sowwakeup(in6p->in6p_socket);
168}
169
170void *
171udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
172{
173	struct udphdr uh;
174	struct ip6_hdr *ip6;
175	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
176	struct mbuf *m;
177	int off;
178	void *cmdarg;
179	struct ip6ctlparam *ip6cp = NULL;
180	const struct sockaddr_in6 *sa6_src = NULL;
181	void (*notify)(struct in6pcb *, int) = udp6_notify;
182	struct udp_portonly {
183		u_int16_t uh_sport;
184		u_int16_t uh_dport;
185	} *uhp;
186
187	if (sa->sa_family != AF_INET6 ||
188	    sa->sa_len != sizeof(struct sockaddr_in6))
189		return NULL;
190
191	if ((unsigned)cmd >= PRC_NCMDS)
192		return NULL;
193	if (PRC_IS_REDIRECT(cmd))
194		notify = in6_rtchange, d = NULL;
195	else if (cmd == PRC_HOSTDEAD)
196		d = NULL;
197	else if (cmd == PRC_MSGSIZE) {
198		/* special code is present, see below */
199		notify = in6_rtchange;
200	}
201	else if (inet6ctlerrmap[cmd] == 0)
202		return NULL;
203
204	/* if the parameter is from icmp6, decode it. */
205	if (d != NULL) {
206		ip6cp = (struct ip6ctlparam *)d;
207		m = ip6cp->ip6c_m;
208		ip6 = ip6cp->ip6c_ip6;
209		off = ip6cp->ip6c_off;
210		cmdarg = ip6cp->ip6c_cmdarg;
211		sa6_src = ip6cp->ip6c_src;
212	} else {
213		m = NULL;
214		ip6 = NULL;
215		cmdarg = NULL;
216		sa6_src = &sa6_any;
217		off = 0;
218	}
219
220	if (ip6) {
221		/*
222		 * XXX: We assume that when IPV6 is non NULL,
223		 * M and OFF are valid.
224		 */
225
226		/* check if we can safely examine src and dst ports */
227		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
228			if (cmd == PRC_MSGSIZE)
229				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
230			return NULL;
231		}
232
233		memset(&uh, 0, sizeof(uh));
234		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
235
236		if (cmd == PRC_MSGSIZE) {
237			int valid = 0;
238
239			/*
240			 * Check to see if we have a valid UDP socket
241			 * corresponding to the address in the ICMPv6 message
242			 * payload.
243			 */
244			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
245			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
246						  uh.uh_sport, 0, 0))
247				valid++;
248#if 0
249			/*
250			 * As the use of sendto(2) is fairly popular,
251			 * we may want to allow non-connected pcb too.
252			 * But it could be too weak against attacks...
253			 * We should at least check if the local address (= s)
254			 * is really ours.
255			 */
256			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
257			    uh.uh_dport, 0))
258				valid++;
259#endif
260
261			/*
262			 * Depending on the value of "valid" and routing table
263			 * size (mtudisc_{hi,lo}wat), we will:
264			 * - recalculate the new MTU and create the
265			 *   corresponding routing entry, or
266			 * - ignore the MTU change notification.
267			 */
268			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
269
270			/*
271			 * regardless of if we called
272			 * icmp6_mtudisc_update(), we need to call
273			 * in6_pcbnotify(), to notify path MTU change
274			 * to the userland (RFC3542), because some
275			 * unconnected sockets may share the same
276			 * destination and want to know the path MTU.
277			 */
278		}
279
280		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
281		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
282		    notify);
283	} else {
284		(void) in6_pcbnotify(&udbtable, sa, 0,
285		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
286	}
287	return NULL;
288}
289
290int
291udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
292{
293	int s;
294	int error = 0;
295	int family;
296
297	family = so->so_proto->pr_domain->dom_family;
298
299	s = splsoftnet();
300	switch (family) {
301#ifdef INET
302	case PF_INET:
303		if (sopt->sopt_level != IPPROTO_UDP) {
304			error = ip_ctloutput(op, so, sopt);
305			goto end;
306		}
307		break;
308#endif
309#ifdef INET6
310	case PF_INET6:
311		if (sopt->sopt_level != IPPROTO_UDP) {
312			error = ip6_ctloutput(op, so, sopt);
313			goto end;
314		}
315		break;
316#endif
317	default:
318		error = EAFNOSUPPORT;
319		goto end;
320	}
321	error = EINVAL;
322
323end:
324	splx(s);
325	return error;
326}
327
328static void
329udp6_sendup(struct mbuf *m, int off /* offset of data portion */,
330	struct sockaddr *src, struct socket *so)
331{
332	struct mbuf *opts = NULL;
333	struct mbuf *n;
334	struct in6pcb *in6p = NULL;
335
336	if (!so)
337		return;
338	if (so->so_proto->pr_domain->dom_family != AF_INET6)
339		return;
340	in6p = sotoin6pcb(so);
341
342#if defined(IPSEC)
343	/* check AH/ESP integrity. */
344	if (ipsec_used && so != NULL && ipsec6_in_reject_so(m, so)) {
345		IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO);
346		if ((n = m_copypacket(m, M_DONTWAIT)) != NULL)
347			icmp6_error(n, ICMP6_DST_UNREACH,
348			    ICMP6_DST_UNREACH_ADMIN, 0);
349		return;
350	}
351#endif /*IPSEC*/
352
353	if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) {
354		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
355#ifdef SO_OTIMESTAMP
356		    || in6p->in6p_socket->so_options & SO_OTIMESTAMP
357#endif
358		    || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
359			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
360			ip6_savecontrol(in6p, &opts, ip6, n);
361		}
362
363		m_adj(n, off);
364		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
365			m_freem(n);
366			if (opts)
367				m_freem(opts);
368			so->so_rcv.sb_overflowed++;
369			UDP6_STATINC(UDP6_STAT_FULLSOCK);
370		} else
371			sorwakeup(so);
372	}
373}
374
375int
376udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst,
377	struct mbuf *m, int off)
378{
379	u_int16_t sport, dport;
380	int rcvcnt;
381	struct in6_addr src6, *dst6;
382	const struct in_addr *dst4;
383	struct inpcb_hdr *inph;
384	struct in6pcb *in6p;
385
386	rcvcnt = 0;
387	off += sizeof(struct udphdr);	/* now, offset of payload */
388
389	if (af != AF_INET && af != AF_INET6)
390		goto bad;
391	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
392		goto bad;
393
394	src6 = src->sin6_addr;
395	if (sa6_recoverscope(src) != 0) {
396		/* XXX: should be impossible. */
397		goto bad;
398	}
399	sport = src->sin6_port;
400
401	dport = dst->sin6_port;
402	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
403	dst6 = &dst->sin6_addr;
404
405	if (IN6_IS_ADDR_MULTICAST(dst6) ||
406	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
407		/*
408		 * Deliver a multicast or broadcast datagram to *all* sockets
409		 * for which the local and remote addresses and ports match
410		 * those of the incoming datagram.  This allows more than
411		 * one process to receive multi/broadcasts on the same port.
412		 * (This really ought to be done for unicast datagrams as
413		 * well, but that would cause problems with existing
414		 * applications that open both address-specific sockets and
415		 * a wildcard socket listening to the same port -- they would
416		 * end up receiving duplicates of every unicast datagram.
417		 * Those applications open the multiple sockets to overcome an
418		 * inadequacy of the UDP socket interface, but for backwards
419		 * compatibility we avoid the problem here rather than
420		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
421		 */
422
423		/*
424		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
425		 * we need udpiphdr for IPsec processing so we do that later.
426		 */
427		/*
428		 * Locate pcb(s) for datagram.
429		 */
430		TAILQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) {
431			in6p = (struct in6pcb *)inph;
432			if (in6p->in6p_af != AF_INET6)
433				continue;
434
435			if (in6p->in6p_lport != dport)
436				continue;
437			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
438				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
439				    dst6))
440					continue;
441			} else {
442				if (IN6_IS_ADDR_V4MAPPED(dst6) &&
443				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
444					continue;
445			}
446			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
447				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
448				    &src6) || in6p->in6p_fport != sport)
449					continue;
450			} else {
451				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
452				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
453					continue;
454			}
455
456			udp6_sendup(m, off, (struct sockaddr *)src,
457				in6p->in6p_socket);
458			rcvcnt++;
459
460			/*
461			 * Don't look for additional matches if this one does
462			 * not have either the SO_REUSEPORT or SO_REUSEADDR
463			 * socket options set.  This heuristic avoids searching
464			 * through all pcbs in the common case of a non-shared
465			 * port.  It assumes that an application will never
466			 * clear these options after setting them.
467			 */
468			if ((in6p->in6p_socket->so_options &
469			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
470				break;
471		}
472	} else {
473		/*
474		 * Locate pcb for datagram.
475		 */
476		in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6,
477					     dport, 0, 0);
478		if (in6p == 0) {
479			UDP_STATINC(UDP_STAT_PCBHASHMISS);
480			in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0);
481			if (in6p == 0)
482				return rcvcnt;
483		}
484
485		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
486		rcvcnt++;
487	}
488
489bad:
490	return rcvcnt;
491}
492
493int
494udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len)
495{
496
497	/*
498	 * XXX it's better to record and check if this mbuf is
499	 * already checked.
500	 */
501
502	if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) {
503		goto good;
504	}
505	if (uh->uh_sum == 0) {
506		UDP6_STATINC(UDP6_STAT_NOSUM);
507		goto bad;
508	}
509
510	switch (m->m_pkthdr.csum_flags &
511	    ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) |
512	    M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
513	case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD:
514		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad);
515		UDP6_STATINC(UDP6_STAT_BADSUM);
516		goto bad;
517
518#if 0 /* notyet */
519	case M_CSUM_UDPv6|M_CSUM_DATA:
520#endif
521
522	case M_CSUM_UDPv6:
523		/* Checksum was okay. */
524		UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok);
525		break;
526
527	default:
528		/*
529		 * Need to compute it ourselves.  Maybe skip checksum
530		 * on loopback interfaces.
531		 */
532		UDP_CSUM_COUNTER_INCR(&udp6_swcsum);
533		if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) {
534			UDP6_STATINC(UDP6_STAT_BADSUM);
535			goto bad;
536		}
537	}
538
539good:
540	return 0;
541bad:
542	return -1;
543}
544
545int
546udp6_input(struct mbuf **mp, int *offp, int proto)
547{
548	struct mbuf *m = *mp;
549	int off = *offp;
550	struct sockaddr_in6 src, dst;
551	struct ip6_hdr *ip6;
552	struct udphdr *uh;
553	u_int32_t plen, ulen;
554
555	ip6 = mtod(m, struct ip6_hdr *);
556
557#if defined(NFAITH) && 0 < NFAITH
558	if (faithprefix(&ip6->ip6_dst)) {
559		/* send icmp6 host unreach? */
560		m_freem(m);
561		return IPPROTO_DONE;
562	}
563#endif
564
565	UDP6_STATINC(UDP6_STAT_IPACKETS);
566
567	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
568	plen = m->m_pkthdr.len - off;
569	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
570	if (uh == NULL) {
571		IP6_STATINC(IP6_STAT_TOOSHORT);
572		return IPPROTO_DONE;
573	}
574	KASSERT(UDP_HDR_ALIGNED_P(uh));
575	ulen = ntohs((u_short)uh->uh_ulen);
576	/*
577	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
578	 * iff payload length > 0xffff.
579	 */
580	if (ulen == 0 && plen > 0xffff)
581		ulen = plen;
582
583	if (plen != ulen) {
584		UDP6_STATINC(UDP6_STAT_BADLEN);
585		goto bad;
586	}
587
588	/* destination port of 0 is illegal, based on RFC768. */
589	if (uh->uh_dport == 0)
590		goto bad;
591
592	/* Be proactive about malicious use of IPv4 mapped address */
593	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
594	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
595		/* XXX stat */
596		goto bad;
597	}
598
599	/*
600	 * Checksum extended UDP header and data.  Maybe skip checksum
601	 * on loopback interfaces.
602	 */
603	if (udp6_input_checksum(m, uh, off, ulen))
604		goto bad;
605
606	/*
607	 * Construct source and dst sockaddrs.
608	 */
609	memset(&src, 0, sizeof(src));
610	src.sin6_family = AF_INET6;
611	src.sin6_len = sizeof(struct sockaddr_in6);
612	src.sin6_addr = ip6->ip6_src;
613	src.sin6_port = uh->uh_sport;
614	memset(&dst, 0, sizeof(dst));
615	dst.sin6_family = AF_INET6;
616	dst.sin6_len = sizeof(struct sockaddr_in6);
617	dst.sin6_addr = ip6->ip6_dst;
618	dst.sin6_port = uh->uh_dport;
619
620	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
621		if (m->m_flags & M_MCAST) {
622			UDP6_STATINC(UDP6_STAT_NOPORTMCAST);
623			goto bad;
624		}
625		UDP6_STATINC(UDP6_STAT_NOPORT);
626		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
627		m = NULL;
628	}
629
630bad:
631	if (m)
632		m_freem(m);
633	return IPPROTO_DONE;
634}
635
636static int
637udp6_attach(struct socket *so, int proto)
638{
639	struct in6pcb *in6p;
640	int s, error;
641
642	KASSERT(sotoin6pcb(so) == NULL);
643	sosetlock(so);
644
645	/*
646	 * MAPPED_ADDR implementation spec:
647	 *  Always attach for IPv6, and only when necessary for IPv4.
648	 */
649	s = splsoftnet();
650	error = in6_pcballoc(so, &udbtable);
651	splx(s);
652	if (error) {
653		return error;
654	}
655	error = soreserve(so, udp6_sendspace, udp6_recvspace);
656	if (error) {
657		return error;
658	}
659	in6p = sotoin6pcb(so);
660	in6p->in6p_cksum = -1;	/* just to be sure */
661
662	KASSERT(solocked(so));
663	return 0;
664}
665
666static void
667udp6_detach(struct socket *so)
668{
669	struct in6pcb *in6p = sotoin6pcb(so);
670	int s;
671
672	KASSERT(solocked(so));
673	KASSERT(in6p != NULL);
674
675	s = splsoftnet();
676	in6_pcbdetach(in6p);
677	splx(s);
678}
679
680static int
681udp6_accept(struct socket *so, struct mbuf *nam)
682{
683	KASSERT(solocked(so));
684
685	return EOPNOTSUPP;
686}
687
688static int
689udp6_ioctl(struct socket *so, u_long cmd, void *addr6, struct ifnet *ifp)
690{
691	/*
692	 * MAPPED_ADDR implementation info:
693	 *  Mapped addr support for PRU_CONTROL is not necessary.
694	 *  Because typical user of PRU_CONTROL is such as ifconfig,
695	 *  and they don't associate any addr to their socket.  Then
696	 *  socket family is only hint about the PRU_CONTROL'ed address
697	 *  family, especially when getting addrs from kernel.
698	 *  So AF_INET socket need to be used to control AF_INET addrs,
699	 *  and AF_INET6 socket for AF_INET6 addrs.
700	 */
701	return in6_control(so, cmd, addr6, ifp);
702}
703
704static int
705udp6_stat(struct socket *so, struct stat *ub)
706{
707	KASSERT(solocked(so));
708
709	/* stat: don't bother with a blocksize */
710	return 0;
711}
712
713static int
714udp6_peeraddr(struct socket *so, struct mbuf *nam)
715{
716	KASSERT(solocked(so));
717	KASSERT(sotoin6pcb(so) != NULL);
718	KASSERT(nam != NULL);
719
720	in6_setpeeraddr(sotoin6pcb(so), nam);
721	return 0;
722}
723
724static int
725udp6_sockaddr(struct socket *so, struct mbuf *nam)
726{
727	KASSERT(solocked(so));
728	KASSERT(sotoin6pcb(so) != NULL);
729	KASSERT(nam != NULL);
730
731	in6_setsockaddr(sotoin6pcb(so), nam);
732	return 0;
733}
734
735int
736udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
737    struct mbuf *control, struct lwp *l)
738{
739	struct in6pcb *in6p = sotoin6pcb(so);
740	int s, error = 0;
741
742	KASSERT(req != PRU_ATTACH);
743	KASSERT(req != PRU_DETACH);
744	KASSERT(req != PRU_ACCEPT);
745	KASSERT(req != PRU_CONTROL);
746	KASSERT(req != PRU_SENSE);
747	KASSERT(req != PRU_PEERADDR);
748	KASSERT(req != PRU_SOCKADDR);
749
750	if (req == PRU_PURGEIF) {
751		mutex_enter(softnet_lock);
752		in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
753		in6_purgeif((struct ifnet *)control);
754		in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
755		mutex_exit(softnet_lock);
756		return 0;
757	}
758	if (in6p == NULL) {
759		error = EINVAL;
760		goto release;
761	}
762
763	switch (req) {
764
765	case PRU_BIND:
766		s = splsoftnet();
767		error = in6_pcbbind(in6p, addr6, l);
768		splx(s);
769		break;
770
771	case PRU_CONNECT:
772		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
773			error = EISCONN;
774			break;
775		}
776		s = splsoftnet();
777		error = in6_pcbconnect(in6p, addr6, l);
778		splx(s);
779		if (error == 0)
780			soisconnected(so);
781		break;
782
783	case PRU_DISCONNECT:
784		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
785			error = ENOTCONN;
786			break;
787		}
788		s = splsoftnet();
789		in6_pcbdisconnect(in6p);
790		memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
791		splx(s);
792		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
793		in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
794		break;
795
796	case PRU_SHUTDOWN:
797		socantsendmore(so);
798		break;
799
800	case PRU_SEND:
801		s = splsoftnet();
802		error = udp6_output(in6p, m, addr6, control, l);
803		splx(s);
804		return error;
805
806	case PRU_ABORT:
807		soisdisconnected(so);
808		in6_pcbdetach(in6p);
809		break;
810
811	case PRU_LISTEN:
812	case PRU_CONNECT2:
813	case PRU_SENDOOB:
814	case PRU_FASTTIMO:
815	case PRU_SLOWTIMO:
816	case PRU_PROTORCV:
817	case PRU_PROTOSEND:
818		error = EOPNOTSUPP;
819		break;
820
821	case PRU_RCVD:
822	case PRU_RCVOOB:
823		return EOPNOTSUPP;	/* do not free mbuf's */
824
825	default:
826		panic("udp6_usrreq");
827	}
828
829release:
830	if (control != NULL)
831		m_freem(control);
832	if (m != NULL)
833		m_freem(m);
834	return error;
835}
836
837static int
838sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
839{
840
841	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
842}
843
844static void
845sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
846{
847
848	sysctl_createv(clog, 0, NULL, NULL,
849		       CTLFLAG_PERMANENT,
850		       CTLTYPE_NODE, "inet6", NULL,
851		       NULL, 0, NULL, 0,
852		       CTL_NET, PF_INET6, CTL_EOL);
853	sysctl_createv(clog, 0, NULL, NULL,
854		       CTLFLAG_PERMANENT,
855		       CTLTYPE_NODE, "udp6",
856		       SYSCTL_DESCR("UDPv6 related settings"),
857		       NULL, 0, NULL, 0,
858		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
859
860	sysctl_createv(clog, 0, NULL, NULL,
861		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
862		       CTLTYPE_INT, "sendspace",
863		       SYSCTL_DESCR("Default UDP send buffer size"),
864		       NULL, 0, &udp6_sendspace, 0,
865		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
866		       CTL_EOL);
867	sysctl_createv(clog, 0, NULL, NULL,
868		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
869		       CTLTYPE_INT, "recvspace",
870		       SYSCTL_DESCR("Default UDP receive buffer size"),
871		       NULL, 0, &udp6_recvspace, 0,
872		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
873		       CTL_EOL);
874	sysctl_createv(clog, 0, NULL, NULL,
875		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
876		       CTLTYPE_INT, "do_loopback_cksum",
877		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
878		       NULL, 0, &udp_do_loopback_cksum, 0,
879		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
880		       CTL_EOL);
881	sysctl_createv(clog, 0, NULL, NULL,
882		       CTLFLAG_PERMANENT,
883		       CTLTYPE_STRUCT, "pcblist",
884		       SYSCTL_DESCR("UDP protocol control block list"),
885		       sysctl_inpcblist, 0, &udbtable, 0,
886		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
887		       CTL_EOL);
888	sysctl_createv(clog, 0, NULL, NULL,
889		       CTLFLAG_PERMANENT,
890		       CTLTYPE_STRUCT, "stats",
891		       SYSCTL_DESCR("UDPv6 statistics"),
892		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
893		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
894		       CTL_EOL);
895}
896
897void
898udp6_statinc(u_int stat)
899{
900
901	KASSERT(stat < UDP6_NSTATS);
902	UDP6_STATINC(stat);
903}
904
905PR_WRAP_USRREQS(udp6)
906#define	udp6_attach	udp6_attach_wrapper
907#define	udp6_detach	udp6_detach_wrapper
908#define	udp6_accept	udp6_accept_wrapper
909#define	udp6_ioctl	udp6_ioctl_wrapper
910#define	udp6_stat	udp6_stat_wrapper
911#define	udp6_peeraddr	udp6_peeraddr_wrapper
912#define	udp6_sockaddr	udp6_sockaddr_wrapper
913#define	udp6_usrreq	udp6_usrreq_wrapper
914
915const struct pr_usrreqs udp6_usrreqs = {
916	.pr_attach	= udp6_attach,
917	.pr_detach	= udp6_detach,
918	.pr_accept	= udp6_accept,
919	.pr_ioctl	= udp6_ioctl,
920	.pr_stat	= udp6_stat,
921	.pr_peeraddr	= udp6_peeraddr,
922	.pr_sockaddr	= udp6_sockaddr,
923	.pr_generic	= udp6_usrreq,
924};
925