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