Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.94
      1 /*	$NetBSD: udp6_usrreq.c,v 1.94 2014/05/18 14:46:16 rmind 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.94 2014/05/18 14:46:16 rmind 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 #include <sys/domain.h>
     81 #include <sys/sysctl.h>
     82 
     83 #include <net/if.h>
     84 #include <net/route.h>
     85 #include <net/if_types.h>
     86 
     87 #include <netinet/in.h>
     88 #include <netinet/in_var.h>
     89 #include <netinet/in_systm.h>
     90 #include <netinet/ip.h>
     91 #include <netinet/ip_var.h>
     92 #include <netinet/in_pcb.h>
     93 #include <netinet/udp.h>
     94 #include <netinet/udp_var.h>
     95 #include <netinet/ip6.h>
     96 #include <netinet6/ip6_var.h>
     97 #include <netinet6/in6_pcb.h>
     98 #include <netinet/icmp6.h>
     99 #include <netinet6/udp6_var.h>
    100 #include <netinet6/udp6_private.h>
    101 #include <netinet6/ip6protosw.h>
    102 #include <netinet/in_offload.h>
    103 
    104 #include "faith.h"
    105 #if defined(NFAITH) && NFAITH > 0
    106 #include <net/if_faith.h>
    107 #endif
    108 
    109 /*
    110  * UDP protocol implementation.
    111  * Per RFC 768, August, 1980.
    112  */
    113 
    114 extern struct inpcbtable udbtable;
    115 
    116 percpu_t *udp6stat_percpu;
    117 
    118 static	void udp6_notify(struct in6pcb *, int);
    119 static	void sysctl_net_inet6_udp6_setup(struct sysctllog **);
    120 
    121 void
    122 udp6_init(void)
    123 {
    124 
    125 	sysctl_net_inet6_udp6_setup(NULL);
    126 	udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS);
    127 
    128 	udp_init_common();
    129 }
    130 
    131 /*
    132  * Notify a udp user of an asynchronous error;
    133  * just wake up so that he can collect error status.
    134  */
    135 static	void
    136 udp6_notify(struct in6pcb *in6p, int errno)
    137 {
    138 	in6p->in6p_socket->so_error = errno;
    139 	sorwakeup(in6p->in6p_socket);
    140 	sowwakeup(in6p->in6p_socket);
    141 }
    142 
    143 void *
    144 udp6_ctlinput(int cmd, const struct sockaddr *sa, void *d)
    145 {
    146 	struct udphdr uh;
    147 	struct ip6_hdr *ip6;
    148 	const struct sockaddr_in6 *sa6 = (const struct sockaddr_in6 *)sa;
    149 	struct mbuf *m;
    150 	int off;
    151 	void *cmdarg;
    152 	struct ip6ctlparam *ip6cp = NULL;
    153 	const struct sockaddr_in6 *sa6_src = NULL;
    154 	void (*notify)(struct in6pcb *, int) = udp6_notify;
    155 	struct udp_portonly {
    156 		u_int16_t uh_sport;
    157 		u_int16_t uh_dport;
    158 	} *uhp;
    159 
    160 	if (sa->sa_family != AF_INET6 ||
    161 	    sa->sa_len != sizeof(struct sockaddr_in6))
    162 		return NULL;
    163 
    164 	if ((unsigned)cmd >= PRC_NCMDS)
    165 		return NULL;
    166 	if (PRC_IS_REDIRECT(cmd))
    167 		notify = in6_rtchange, d = NULL;
    168 	else if (cmd == PRC_HOSTDEAD)
    169 		d = NULL;
    170 	else if (cmd == PRC_MSGSIZE) {
    171 		/* special code is present, see below */
    172 		notify = in6_rtchange;
    173 	}
    174 	else if (inet6ctlerrmap[cmd] == 0)
    175 		return NULL;
    176 
    177 	/* if the parameter is from icmp6, decode it. */
    178 	if (d != NULL) {
    179 		ip6cp = (struct ip6ctlparam *)d;
    180 		m = ip6cp->ip6c_m;
    181 		ip6 = ip6cp->ip6c_ip6;
    182 		off = ip6cp->ip6c_off;
    183 		cmdarg = ip6cp->ip6c_cmdarg;
    184 		sa6_src = ip6cp->ip6c_src;
    185 	} else {
    186 		m = NULL;
    187 		ip6 = NULL;
    188 		cmdarg = NULL;
    189 		sa6_src = &sa6_any;
    190 		off = 0;
    191 	}
    192 
    193 	if (ip6) {
    194 		/*
    195 		 * XXX: We assume that when IPV6 is non NULL,
    196 		 * M and OFF are valid.
    197 		 */
    198 
    199 		/* check if we can safely examine src and dst ports */
    200 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    201 			if (cmd == PRC_MSGSIZE)
    202 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    203 			return NULL;
    204 		}
    205 
    206 		memset(&uh, 0, sizeof(uh));
    207 		m_copydata(m, off, sizeof(*uhp), (void *)&uh);
    208 
    209 		if (cmd == PRC_MSGSIZE) {
    210 			int valid = 0;
    211 
    212 			/*
    213 			 * Check to see if we have a valid UDP socket
    214 			 * corresponding to the address in the ICMPv6 message
    215 			 * payload.
    216 			 */
    217 			if (in6_pcblookup_connect(&udbtable, &sa6->sin6_addr,
    218 			    uh.uh_dport, (const struct in6_addr *)&sa6_src->sin6_addr,
    219 						  uh.uh_sport, 0, 0))
    220 				valid++;
    221 #if 0
    222 			/*
    223 			 * As the use of sendto(2) is fairly popular,
    224 			 * we may want to allow non-connected pcb too.
    225 			 * But it could be too weak against attacks...
    226 			 * We should at least check if the local address (= s)
    227 			 * is really ours.
    228 			 */
    229 			else if (in6_pcblookup_bind(&udbtable, &sa6->sin6_addr,
    230 			    uh.uh_dport, 0))
    231 				valid++;
    232 #endif
    233 
    234 			/*
    235 			 * Depending on the value of "valid" and routing table
    236 			 * size (mtudisc_{hi,lo}wat), we will:
    237 			 * - recalculate the new MTU and create the
    238 			 *   corresponding routing entry, or
    239 			 * - ignore the MTU change notification.
    240 			 */
    241 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    242 
    243 			/*
    244 			 * regardless of if we called
    245 			 * icmp6_mtudisc_update(), we need to call
    246 			 * in6_pcbnotify(), to notify path MTU change
    247 			 * to the userland (RFC3542), because some
    248 			 * unconnected sockets may share the same
    249 			 * destination and want to know the path MTU.
    250 			 */
    251 		}
    252 
    253 		(void) in6_pcbnotify(&udbtable, sa, uh.uh_dport,
    254 		    (const struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    255 		    notify);
    256 	} else {
    257 		(void) in6_pcbnotify(&udbtable, sa, 0,
    258 		    (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
    259 	}
    260 	return NULL;
    261 }
    262 
    263 int
    264 udp6_ctloutput(int op, struct socket *so, struct sockopt *sopt)
    265 {
    266 	int s;
    267 	int error = 0;
    268 	int family;
    269 
    270 	family = so->so_proto->pr_domain->dom_family;
    271 
    272 	s = splsoftnet();
    273 	switch (family) {
    274 #ifdef INET
    275 	case PF_INET:
    276 		if (sopt->sopt_level != IPPROTO_UDP) {
    277 			error = ip_ctloutput(op, so, sopt);
    278 			goto end;
    279 		}
    280 		break;
    281 #endif
    282 #ifdef INET6
    283 	case PF_INET6:
    284 		if (sopt->sopt_level != IPPROTO_UDP) {
    285 			error = ip6_ctloutput(op, so, sopt);
    286 			goto end;
    287 		}
    288 		break;
    289 #endif
    290 	default:
    291 		error = EAFNOSUPPORT;
    292 		goto end;
    293 	}
    294 	error = EINVAL;
    295 
    296 end:
    297 	splx(s);
    298 	return error;
    299 }
    300 
    301 
    302 extern	int udp6_sendspace;
    303 extern	int udp6_recvspace;
    304 
    305 int
    306 udp6_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *addr6,
    307     struct mbuf *control, struct lwp *l)
    308 {
    309 	struct	in6pcb *in6p = sotoin6pcb(so);
    310 	int	error = 0;
    311 	int	s;
    312 
    313 	/*
    314 	 * MAPPED_ADDR implementation info:
    315 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    316 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    317 	 *  and they don't associate any addr to their socket.  Then
    318 	 *  socket family is only hint about the PRU_CONTROL'ed address
    319 	 *  family, especially when getting addrs from kernel.
    320 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    321 	 *  and AF_INET6 socket for AF_INET6 addrs.
    322 	 */
    323 	if (req == PRU_CONTROL)
    324 		return in6_control(so, (u_long)m, (void *)addr6,
    325 				   (struct ifnet *)control, l);
    326 
    327 	if (req == PRU_PURGEIF) {
    328 		mutex_enter(softnet_lock);
    329 		in6_pcbpurgeif0(&udbtable, (struct ifnet *)control);
    330 		in6_purgeif((struct ifnet *)control);
    331 		in6_pcbpurgeif(&udbtable, (struct ifnet *)control);
    332 		mutex_exit(softnet_lock);
    333 		return 0;
    334 	}
    335 
    336 	if (req == PRU_ATTACH)
    337 		sosetlock(so);
    338 	else if (in6p == NULL) {
    339 		error = EINVAL;
    340 		goto release;
    341 	}
    342 
    343 	switch (req) {
    344 	case PRU_ATTACH:
    345 		/*
    346 		 * MAPPED_ADDR implementation spec:
    347 		 *  Always attach for IPv6,
    348 		 *  and only when necessary for IPv4.
    349 		 */
    350 		if (in6p != NULL) {
    351 			error = EINVAL;
    352 			break;
    353 		}
    354 		s = splsoftnet();
    355 		error = in6_pcballoc(so, &udbtable);
    356 		splx(s);
    357 		if (error)
    358 			break;
    359 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    360 		if (error)
    361 			break;
    362 		in6p = sotoin6pcb(so);
    363 		in6p->in6p_cksum = -1;	/* just to be sure */
    364 		break;
    365 
    366 	case PRU_DETACH:
    367 		in6_pcbdetach(in6p);
    368 		break;
    369 
    370 	case PRU_BIND:
    371 		s = splsoftnet();
    372 		error = in6_pcbbind(in6p, addr6, l);
    373 		splx(s);
    374 		break;
    375 
    376 	case PRU_CONNECT:
    377 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    378 			error = EISCONN;
    379 			break;
    380 		}
    381 		s = splsoftnet();
    382 		error = in6_pcbconnect(in6p, addr6, l);
    383 		splx(s);
    384 		if (error == 0)
    385 			soisconnected(so);
    386 		break;
    387 
    388 	case PRU_DISCONNECT:
    389 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    390 			error = ENOTCONN;
    391 			break;
    392 		}
    393 		s = splsoftnet();
    394 		in6_pcbdisconnect(in6p);
    395 		memset((void *)&in6p->in6p_laddr, 0, sizeof(in6p->in6p_laddr));
    396 		splx(s);
    397 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    398 		in6_pcbstate(in6p, IN6P_BOUND);		/* XXX */
    399 		break;
    400 
    401 	case PRU_SHUTDOWN:
    402 		socantsendmore(so);
    403 		break;
    404 
    405 	case PRU_SEND:
    406 		s = splsoftnet();
    407 		error = udp6_output(in6p, m, addr6, control, l);
    408 		splx(s);
    409 		return error;
    410 
    411 	case PRU_ABORT:
    412 		soisdisconnected(so);
    413 		in6_pcbdetach(in6p);
    414 		break;
    415 
    416 	case PRU_SOCKADDR:
    417 		in6_setsockaddr(in6p, addr6);
    418 		break;
    419 
    420 	case PRU_PEERADDR:
    421 		in6_setpeeraddr(in6p, addr6);
    422 		break;
    423 
    424 	case PRU_SENSE:
    425 		/*
    426 		 * stat: don't bother with a blocksize
    427 		 */
    428 		return 0;
    429 
    430 	case PRU_LISTEN:
    431 	case PRU_CONNECT2:
    432 	case PRU_ACCEPT:
    433 	case PRU_SENDOOB:
    434 	case PRU_FASTTIMO:
    435 	case PRU_SLOWTIMO:
    436 	case PRU_PROTORCV:
    437 	case PRU_PROTOSEND:
    438 		error = EOPNOTSUPP;
    439 		break;
    440 
    441 	case PRU_RCVD:
    442 	case PRU_RCVOOB:
    443 		return EOPNOTSUPP;	/* do not free mbuf's */
    444 
    445 	default:
    446 		panic("udp6_usrreq");
    447 	}
    448 
    449 release:
    450 	if (control != NULL)
    451 		m_freem(control);
    452 	if (m != NULL)
    453 		m_freem(m);
    454 	return error;
    455 }
    456 
    457 static int
    458 sysctl_net_inet6_udp6_stats(SYSCTLFN_ARGS)
    459 {
    460 
    461 	return (NETSTAT_SYSCTL(udp6stat_percpu, UDP6_NSTATS));
    462 }
    463 
    464 static void
    465 sysctl_net_inet6_udp6_setup(struct sysctllog **clog)
    466 {
    467 
    468 	sysctl_createv(clog, 0, NULL, NULL,
    469 		       CTLFLAG_PERMANENT,
    470 		       CTLTYPE_NODE, "inet6", NULL,
    471 		       NULL, 0, NULL, 0,
    472 		       CTL_NET, PF_INET6, CTL_EOL);
    473 	sysctl_createv(clog, 0, NULL, NULL,
    474 		       CTLFLAG_PERMANENT,
    475 		       CTLTYPE_NODE, "udp6",
    476 		       SYSCTL_DESCR("UDPv6 related settings"),
    477 		       NULL, 0, NULL, 0,
    478 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_EOL);
    479 
    480 	sysctl_createv(clog, 0, NULL, NULL,
    481 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    482 		       CTLTYPE_INT, "sendspace",
    483 		       SYSCTL_DESCR("Default UDP send buffer size"),
    484 		       NULL, 0, &udp6_sendspace, 0,
    485 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_SENDSPACE,
    486 		       CTL_EOL);
    487 	sysctl_createv(clog, 0, NULL, NULL,
    488 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    489 		       CTLTYPE_INT, "recvspace",
    490 		       SYSCTL_DESCR("Default UDP receive buffer size"),
    491 		       NULL, 0, &udp6_recvspace, 0,
    492 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_RECVSPACE,
    493 		       CTL_EOL);
    494 	sysctl_createv(clog, 0, NULL, NULL,
    495 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    496 		       CTLTYPE_INT, "do_loopback_cksum",
    497 		       SYSCTL_DESCR("Perform UDP checksum on loopback"),
    498 		       NULL, 0, &udp_do_loopback_cksum, 0,
    499 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_LOOPBACKCKSUM,
    500 		       CTL_EOL);
    501 	sysctl_createv(clog, 0, NULL, NULL,
    502 		       CTLFLAG_PERMANENT,
    503 		       CTLTYPE_STRUCT, "pcblist",
    504 		       SYSCTL_DESCR("UDP protocol control block list"),
    505 		       sysctl_inpcblist, 0, &udbtable, 0,
    506 		       CTL_NET, PF_INET6, IPPROTO_UDP, CTL_CREATE,
    507 		       CTL_EOL);
    508 	sysctl_createv(clog, 0, NULL, NULL,
    509 		       CTLFLAG_PERMANENT,
    510 		       CTLTYPE_STRUCT, "stats",
    511 		       SYSCTL_DESCR("UDPv6 statistics"),
    512 		       sysctl_net_inet6_udp6_stats, 0, NULL, 0,
    513 		       CTL_NET, PF_INET6, IPPROTO_UDP, UDP6CTL_STATS,
    514 		       CTL_EOL);
    515 }
    516 
    517 void
    518 udp6_statinc(u_int stat)
    519 {
    520 
    521 	KASSERT(stat < UDP6_NSTATS);
    522 	UDP6_STATINC(stat);
    523 }
    524 
    525 PR_WRAP_USRREQ(udp6_usrreq)
    526 
    527 #define	udp6_usrreq	udp6_usrreq_wrapper
    528 
    529 const struct pr_usrreqs udp6_usrreqs = {
    530 	.pr_generic	= udp6_usrreq,
    531 };
    532