Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.49
      1 /*	$NetBSD: udp6_usrreq.c,v 1.49 2001/10/24 06:36:39 itojun 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. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)udp_var.h	8.1 (Berkeley) 6/10/93
     66  */
     67 
     68 #include "opt_ipsec.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/malloc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/protosw.h>
     74 #include <sys/socket.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/errno.h>
     77 #include <sys/stat.h>
     78 #include <sys/systm.h>
     79 #include <sys/proc.h>
     80 #include <sys/syslog.h>
     81 
     82 #include <net/if.h>
     83 #include <net/route.h>
     84 #include <net/if_types.h>
     85 
     86 #include <netinet/in.h>
     87 #include <netinet/in_var.h>
     88 #include <netinet/in_systm.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/ip6.h>
     95 #include <netinet6/ip6_var.h>
     96 #include <netinet6/in6_pcb.h>
     97 #include <netinet/icmp6.h>
     98 #include <netinet6/udp6_var.h>
     99 #include <netinet6/ip6protosw.h>
    100 
    101 #ifdef IPSEC
    102 #include <netinet6/ipsec.h>
    103 #endif /* IPSEC */
    104 
    105 #include "faith.h"
    106 #if defined(NFAITH) && NFAITH > 0
    107 #include <net/if_faith.h>
    108 #endif
    109 
    110 /*
    111  * UDP protocol inplementation.
    112  * Per RFC 768, August, 1980.
    113  */
    114 
    115 struct	in6pcb *udp6_last_in6pcb = &udb6;
    116 
    117 static	void udp6_detach __P((struct in6pcb *));
    118 static	void udp6_notify __P((struct in6pcb *, int));
    119 
    120 void
    121 udp6_init()
    122 {
    123 	udb6.in6p_next = udb6.in6p_prev = &udb6;
    124 }
    125 
    126 /*
    127  * Notify a udp user of an asynchronous error;
    128  * just wake up so that he can collect error status.
    129  */
    130 static	void
    131 udp6_notify(in6p, errno)
    132 	struct in6pcb *in6p;
    133 	int errno;
    134 {
    135 	in6p->in6p_socket->so_error = errno;
    136 	sorwakeup(in6p->in6p_socket);
    137 	sowwakeup(in6p->in6p_socket);
    138 }
    139 
    140 void
    141 udp6_ctlinput(cmd, sa, d)
    142 	int cmd;
    143 	struct sockaddr *sa;
    144 	void *d;
    145 {
    146 	struct udphdr uh;
    147 	struct ip6_hdr *ip6;
    148 	struct sockaddr_in6 *sa6 = (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) __P((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;
    163 
    164 	if ((unsigned)cmd >= PRC_NCMDS)
    165 		return;
    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;
    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 	}
    191 
    192 	if (ip6) {
    193 		/*
    194 		 * XXX: We assume that when IPV6 is non NULL,
    195 		 * M and OFF are valid.
    196 		 */
    197 
    198 		/* check if we can safely examine src and dst ports */
    199 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    200 			if (cmd == PRC_MSGSIZE)
    201 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    202 			return;
    203 		}
    204 
    205 		bzero(&uh, sizeof(uh));
    206 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
    207 
    208 		if (cmd == PRC_MSGSIZE) {
    209 			int valid = 0;
    210 
    211 			/*
    212 			 * Check to see if we have a valid UDP socket
    213 			 * corresponding to the address in the ICMPv6 message
    214 			 * payload.
    215 			 */
    216 			if (in6_pcblookup_connect(&udb6, &sa6->sin6_addr,
    217 			    uh.uh_dport, (struct in6_addr *)&sa6_src->sin6_addr,
    218 			    uh.uh_sport, 0))
    219 				valid++;
    220 #if 0
    221 			/*
    222 			 * As the use of sendto(2) is fairly popular,
    223 			 * we may want to allow non-connected pcb too.
    224 			 * But it could be too weak against attacks...
    225 			 * We should at least check if the local address (= s)
    226 			 * is really ours.
    227 			 */
    228 			else if (in6_pcblookup_bind(&udb6, &sa6->sin6_addr,
    229 						    uh.uh_dport, 0))
    230 				valid++;
    231 #endif
    232 
    233 			/*
    234 			 * Depending on the value of "valid" and routing table
    235 			 * size (mtudisc_{hi,lo}wat), we will:
    236 			 * - recalculate the new MTU and create the
    237 			 *   corresponding routing entry, or
    238 			 * - ignore the MTU change notification.
    239 			 */
    240 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    241 
    242 			/*
    243 			 * regardless of if we called icmp6_mtudisc_update(),
    244 			 * we need to call in6_pcbnotify(), to notify path
    245 			 * MTU change to the userland (2292bis-02), because
    246 			 * some unconnected sockets may share the same
    247 			 * destination and want to know the path MTU.
    248 			 */
    249 		}
    250 
    251 		(void) in6_pcbnotify(&udb6, sa, uh.uh_dport,
    252 		    (struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    253 		    notify);
    254 	} else {
    255 		(void) in6_pcbnotify(&udb6, sa, 0, (struct sockaddr *)sa6_src,
    256 		    0, cmd, cmdarg, notify);
    257 	}
    258 }
    259 
    260 extern	int udp6_sendspace;
    261 extern	int udp6_recvspace;
    262 
    263 int
    264 udp6_usrreq(so, req, m, addr6, control, p)
    265 	struct socket *so;
    266 	int req;
    267 	struct mbuf *m, *addr6, *control;
    268 	struct proc *p;
    269 {
    270 	struct	in6pcb *in6p = sotoin6pcb(so);
    271 	int	error = 0;
    272 	int	s;
    273 
    274 	/*
    275 	 * MAPPED_ADDR implementation info:
    276 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    277 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    278 	 *  and they don't associate any addr to their socket.  Then
    279 	 *  socket family is only hint about the PRU_CONTROL'ed address
    280 	 *  family, especially when getting addrs from kernel.
    281 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    282 	 *  and AF_INET6 socket for AF_INET6 addrs.
    283 	 */
    284 	if (req == PRU_CONTROL)
    285 		return(in6_control(so, (u_long)m, (caddr_t)addr6,
    286 				   (struct ifnet *)control, p));
    287 
    288 	if (req == PRU_PURGEIF) {
    289 		in6_pcbpurgeif0(&udb6, (struct ifnet *)control);
    290 		in6_purgeif((struct ifnet *)control);
    291 		in6_pcbpurgeif(&udb6, (struct ifnet *)control);
    292 		return (0);
    293 	}
    294 
    295 	if (in6p == NULL && req != PRU_ATTACH) {
    296 		error = EINVAL;
    297 		goto release;
    298 	}
    299 
    300 	switch (req) {
    301 	case PRU_ATTACH:
    302 		/*
    303 		 * MAPPED_ADDR implementation spec:
    304 		 *  Always attach for IPv6,
    305 		 *  and only when necessary for IPv4.
    306 		 */
    307 		if (in6p != NULL) {
    308 			error = EINVAL;
    309 			break;
    310 		}
    311 		s = splsoftnet();
    312 		error = in6_pcballoc(so, &udb6);
    313 		splx(s);
    314 		if (error)
    315 			break;
    316 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    317 		if (error)
    318 			break;
    319 		in6p = sotoin6pcb(so);
    320 		in6p->in6p_cksum = -1;	/* just to be sure */
    321 		break;
    322 
    323 	case PRU_DETACH:
    324 		udp6_detach(in6p);
    325 		break;
    326 
    327 	case PRU_BIND:
    328 		s = splsoftnet();
    329 		error = in6_pcbbind(in6p, addr6, p);
    330 		splx(s);
    331 		break;
    332 
    333 	case PRU_LISTEN:
    334 		error = EOPNOTSUPP;
    335 		break;
    336 
    337 	case PRU_CONNECT:
    338 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    339 			error = EISCONN;
    340 			break;
    341 		}
    342 		s = splsoftnet();
    343 		error = in6_pcbconnect(in6p, addr6);
    344 		if (ip6_auto_flowlabel) {
    345 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    346 			in6p->in6p_flowinfo |=
    347 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    348 		}
    349 		splx(s);
    350 		if (error == 0)
    351 			soisconnected(so);
    352 		break;
    353 
    354 	case PRU_CONNECT2:
    355 		error = EOPNOTSUPP;
    356 		break;
    357 
    358 	case PRU_ACCEPT:
    359 		error = EOPNOTSUPP;
    360 		break;
    361 
    362 	case PRU_DISCONNECT:
    363 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    364 			error = ENOTCONN;
    365 			break;
    366 		}
    367 		s = splsoftnet();
    368 		in6_pcbdisconnect(in6p);
    369 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
    370 		splx(s);
    371 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    372 		break;
    373 
    374 	case PRU_SHUTDOWN:
    375 		socantsendmore(so);
    376 		break;
    377 
    378 	case PRU_SEND:
    379 		return(udp6_output(in6p, m, addr6, control, p));
    380 
    381 	case PRU_ABORT:
    382 		soisdisconnected(so);
    383 		udp6_detach(in6p);
    384 		break;
    385 
    386 	case PRU_SOCKADDR:
    387 		in6_setsockaddr(in6p, addr6);
    388 		break;
    389 
    390 	case PRU_PEERADDR:
    391 		in6_setpeeraddr(in6p, addr6);
    392 		break;
    393 
    394 	case PRU_SENSE:
    395 		/*
    396 		 * stat: don't bother with a blocksize
    397 		 */
    398 		return(0);
    399 
    400 	case PRU_SENDOOB:
    401 	case PRU_FASTTIMO:
    402 	case PRU_SLOWTIMO:
    403 	case PRU_PROTORCV:
    404 	case PRU_PROTOSEND:
    405 		error = EOPNOTSUPP;
    406 		break;
    407 
    408 	case PRU_RCVD:
    409 	case PRU_RCVOOB:
    410 		return(EOPNOTSUPP);	/* do not free mbuf's */
    411 
    412 	default:
    413 		panic("udp6_usrreq");
    414 	}
    415 
    416 release:
    417 	if (control)
    418 		m_freem(control);
    419 	if (m)
    420 		m_freem(m);
    421 	return(error);
    422 }
    423 
    424 static void
    425 udp6_detach(in6p)
    426 	struct in6pcb *in6p;
    427 {
    428 	int s = splsoftnet();
    429 
    430 	if (in6p == udp6_last_in6pcb)
    431 		udp6_last_in6pcb = &udb6;
    432 	in6_pcbdetach(in6p);
    433 	splx(s);
    434 }
    435 
    436 #include <uvm/uvm_extern.h>
    437 #include <sys/sysctl.h>
    438 
    439 int
    440 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    441 	int *name;
    442 	u_int namelen;
    443 	void *oldp;
    444 	size_t *oldlenp;
    445 	void *newp;
    446 	size_t newlen;
    447 {
    448 	/* All sysctl names at this level are terminal. */
    449 	if (namelen != 1)
    450 		return ENOTDIR;
    451 
    452 	switch (name[0]) {
    453 
    454 	case UDP6CTL_SENDSPACE:
    455 		return sysctl_int(oldp, oldlenp, newp, newlen,
    456 		    &udp6_sendspace);
    457 	case UDP6CTL_RECVSPACE:
    458 		return sysctl_int(oldp, oldlenp, newp, newlen,
    459 		    &udp6_recvspace);
    460 	default:
    461 		return ENOPROTOOPT;
    462 	}
    463 	/* NOTREACHED */
    464 }
    465