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