Home | History | Annotate | Line # | Download | only in netinet6
udp6_usrreq.c revision 1.44
      1 /*	$NetBSD: udp6_usrreq.c,v 1.44 2001/07/03 08:06:20 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 #ifdef UDP6
    118 static	int in6_mcmatch __P((struct in6pcb *, struct in6_addr *, struct ifnet *));
    119 #endif
    120 static	void udp6_detach __P((struct in6pcb *));
    121 static	void udp6_notify __P((struct in6pcb *, int));
    122 
    123 void
    124 udp6_init()
    125 {
    126 	udb6.in6p_next = udb6.in6p_prev = &udb6;
    127 }
    128 
    129 #ifdef UDP6
    130 static int
    131 in6_mcmatch(in6p, ia6, ifp)
    132 	struct in6pcb *in6p;
    133 	struct in6_addr *ia6;
    134 	struct ifnet *ifp;
    135 {
    136 	struct ip6_moptions *im6o = in6p->in6p_moptions;
    137 	struct in6_multi_mship *imm;
    138 
    139 	if (im6o == NULL)
    140 		return 0;
    141 
    142 	for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
    143 	     imm = imm->i6mm_chain.le_next) {
    144 		if ((ifp == NULL ||
    145 		     imm->i6mm_maddr->in6m_ifp == ifp) &&
    146 		    IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
    147 				       ia6))
    148 			return 1;
    149 	}
    150 	return 0;
    151 }
    152 
    153 int
    154 udp6_input(mp, offp, proto)
    155 	struct mbuf **mp;
    156 	int *offp, proto;
    157 {
    158 	struct mbuf *m = *mp;
    159 	struct ip6_hdr *ip6;
    160 	struct udphdr *uh;
    161 	struct in6pcb *in6p;
    162 	struct	mbuf *opts = 0;
    163 	int off = *offp;
    164 	u_int32_t plen, ulen;
    165 	struct sockaddr_in6 udp_in6;
    166 
    167 	ip6 = mtod(m, struct ip6_hdr *);
    168 
    169 #if defined(NFAITH) && 0 < NFAITH
    170 	if (faithprefix(&ip6->ip6_dst)) {
    171 		/* send icmp6 host unreach? */
    172 		m_freem(m);
    173 		return IPPROTO_DONE;
    174 	}
    175 #endif
    176 
    177 	udp6stat.udp6s_ipackets++;
    178 
    179 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
    180 	plen = m->m_pkthdr.len - off;
    181 #ifndef PULLDOWN_TEST
    182 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
    183 	uh = (struct udphdr *)((caddr_t)ip6 + off);
    184 #else
    185 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
    186 	if (uh == NULL) {
    187 		udp6stat.udp6s_hdrops++;
    188 		return IPPROTO_DONE;
    189 	}
    190 #endif
    191 	ulen = ntohs((u_short)uh->uh_ulen);
    192 	/*
    193 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
    194 	 * iff payload length > 0xffff.
    195 	 */
    196 	if (ulen == 0 && plen > 0xffff)
    197 		ulen = plen;
    198 
    199 	if (plen != ulen) {
    200 		udp6stat.udp6s_badlen++;
    201 		goto bad;
    202 	}
    203 
    204 	/* destination port of 0 is illegal, based on RFC768. */
    205 	if (uh->uh_dport == 0)
    206 		goto bad;
    207 
    208 	/* Be proactive about malicious use of IPv4 mapped address */
    209 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
    210 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
    211 		/* XXX stat */
    212 		goto bad;
    213 	}
    214 
    215 	/*
    216 	 * Checksum extended UDP header and data.
    217 	 */
    218 	if (uh->uh_sum == 0)
    219 		udp6stat.udp6s_nosum++;
    220 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
    221 		udp6stat.udp6s_badsum++;
    222 		goto bad;
    223 	}
    224 
    225 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    226 		struct	in6pcb *last;
    227 
    228 		/*
    229 		 * Deliver a multicast datagram to all sockets
    230 		 * for which the local and remote addresses and ports match
    231 		 * those of the incoming datagram.  This allows more than
    232 		 * one process to receive multicasts on the same port.
    233 		 * (This really ought to be done for unicast datagrams as
    234 		 * well, but that would cause problems with existing
    235 		 * applications that open both address-specific sockets and
    236 		 * a wildcard socket listening to the same port -- they would
    237 		 * end up receiving duplicates of every unicast datagram.
    238 		 * Those applications open the multiple sockets to overcome an
    239 		 * inadequacy of the UDP socket interface, but for backwards
    240 		 * compatibility we avoid the problem here rather than
    241 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
    242 		 */
    243 
    244 		/*
    245 		 * In a case that laddr should be set to the link-local
    246 		 * address (this happens in RIPng), the multicast address
    247 		 * specified in the received packet does not match with
    248 		 * laddr. To cure this situation, the matching is relaxed
    249 		 * if the receiving interface is the same as one specified
    250 		 * in the socket and if the destination multicast address
    251 		 * matches one of the multicast groups specified in the socket.
    252 		 */
    253 
    254 		/*
    255 		 * Construct sockaddr format source address.
    256 		 */
    257 		bzero(&udp_in6, sizeof(udp_in6));
    258 		udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    259 		udp_in6.sin6_family = AF_INET6;
    260 		udp_in6.sin6_port = uh->uh_sport;
    261 #if 0 /*XXX inbound flowinfo */
    262 		udp_in6.sin6_flowinfo = ip6->ip6_flow & IPV6_FLOWINFO_MASK;
    263 #endif
    264 		/* KAME hack: recover scopeid */
    265 		(void)in6_recoverscope(&udp_in6, &ip6->ip6_src,
    266 		    m->m_pkthdr.rcvif);
    267 
    268 		/*
    269 		 * KAME note: usually we drop udphdr from mbuf here.
    270 		 * We need udphdr for IPsec processing so we do that later.
    271 		 */
    272 
    273 		/*
    274 		 * Locate pcb(s) for datagram.
    275 		 * (Algorithm copied from raw_intr().)
    276 		 */
    277 		last = NULL;
    278 		for (in6p = udb6.in6p_next;
    279 		     in6p != &udb6;
    280 		     in6p = in6p->in6p_next) {
    281 			if (in6p->in6p_lport != uh->uh_dport)
    282 				continue;
    283 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
    284 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
    285 							&ip6->ip6_dst) &&
    286 				    !in6_mcmatch(in6p, &ip6->ip6_dst,
    287 						 m->m_pkthdr.rcvif))
    288 					continue;
    289 			}
    290 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    291 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
    292 							&ip6->ip6_src) ||
    293 				   in6p->in6p_fport != uh->uh_sport)
    294 					continue;
    295 			}
    296 
    297 			if (last != NULL) {
    298 				struct	mbuf *n;
    299 
    300 #ifdef IPSEC
    301 				/*
    302 				 * Check AH/ESP integrity.
    303 				 */
    304 				if (ipsec6_in_reject(m, last)) {
    305 					ipsec6stat.in_polvio++;
    306 					/* do not inject data into pcb */
    307 				} else
    308 #endif /*IPSEC*/
    309 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
    310 					/*
    311 					 * KAME NOTE: do not
    312 					 * m_copy(m, offset, ...) above.
    313 					 * sbappendaddr() expects M_PKTHDR,
    314 					 * and m_copy() will copy M_PKTHDR
    315 					 * only if offset is 0.
    316 					 */
    317 					if (last->in6p_flags & IN6P_CONTROLOPTS
    318 					 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    319 						ip6_savecontrol(last, &opts,
    320 								ip6, n);
    321 					}
    322 
    323 					m_adj(n, off + sizeof(struct udphdr));
    324 					if (sbappendaddr(&last->in6p_socket->so_rcv,
    325 							(struct sockaddr *)&udp_in6,
    326 							n, opts) == 0) {
    327 						m_freem(n);
    328 						if (opts)
    329 							m_freem(opts);
    330 						udp6stat.udp6s_fullsock++;
    331 					} else
    332 						sorwakeup(last->in6p_socket);
    333 					opts = 0;
    334 				}
    335 			}
    336 			last = in6p;
    337 			/*
    338 			 * Don't look for additional matches if this one does
    339 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
    340 			 * socket options set.  This heuristic avoids searching
    341 			 * through all pcbs in the common case of a non-shared
    342 			 * port.  It assumes that an application will never
    343 			 * clear these options after setting them.
    344 			 */
    345 			if ((last->in6p_socket->so_options &
    346 			     (SO_REUSEPORT|SO_REUSEADDR)) == 0)
    347 				break;
    348 		}
    349 
    350 		if (last == NULL) {
    351 			/*
    352 			 * No matching pcb found; discard datagram.
    353 			 * (No need to send an ICMP Port Unreachable
    354 			 * for a broadcast or multicast datgram.)
    355 			 */
    356 			udp6stat.udp6s_noport++;
    357 			udp6stat.udp6s_noportmcast++;
    358 			goto bad;
    359 		}
    360 #ifdef IPSEC
    361 		/*
    362 		 * Check AH/ESP integrity.
    363 		 */
    364 		if (last != NULL && ipsec6_in_reject(m, last)) {
    365 			ipsec6stat.in_polvio++;
    366 			goto bad;
    367 		}
    368 #endif /*IPSEC*/
    369 		if (last->in6p_flags & IN6P_CONTROLOPTS
    370 		 || last->in6p_socket->so_options & SO_TIMESTAMP) {
    371 			ip6_savecontrol(last, &opts, ip6, m);
    372 		}
    373 
    374 		m_adj(m, off + sizeof(struct udphdr));
    375 		if (sbappendaddr(&last->in6p_socket->so_rcv,
    376 				(struct sockaddr *)&udp_in6,
    377 				m, opts) == 0) {
    378 			udp6stat.udp6s_fullsock++;
    379 			goto bad;
    380 		}
    381 		sorwakeup(last->in6p_socket);
    382 		return IPPROTO_DONE;
    383 	}
    384 	/*
    385 	 * Locate pcb for datagram.
    386 	 */
    387 	in6p = udp6_last_in6pcb;
    388 	if (in6p->in6p_lport != uh->uh_dport ||
    389 	   in6p->in6p_fport != uh->uh_sport ||
    390 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src) ||
    391 	   !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
    392 		in6p = in6_pcblookup(&udb6,
    393 				     &ip6->ip6_src, uh->uh_sport,
    394 				     &ip6->ip6_dst, uh->uh_dport,
    395 				     IN6PLOOKUP_WILDCARD);
    396 		if (in6p)
    397 			udp6_last_in6pcb = in6p;
    398 		udp6stat.udp6ps_pcbcachemiss++;
    399 	}
    400 	if (in6p == 0) {
    401 		udp6stat.udp6s_noport++;
    402 		if (m->m_flags & M_MCAST) {
    403 			udp6stat.udp6s_noportmcast++;
    404 			goto bad;
    405 		}
    406 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
    407 		return IPPROTO_DONE;
    408 	}
    409 #ifdef IPSEC
    410 	/*
    411 	 * Check AH/ESP integrity.
    412 	 */
    413 	if (in6p != NULL && ipsec6_in_reject(m, in6p)) {
    414 		ipsec6stat.in_polvio++;
    415 		goto bad;
    416 	}
    417 #endif /*IPSEC*/
    418 
    419 	/*
    420 	 * Construct sockaddr format source address.
    421 	 * Stuff source address and datagram in user buffer.
    422 	 */
    423 	bzero(&udp_in6, sizeof(udp_in6));
    424 	udp_in6.sin6_len = sizeof(struct sockaddr_in6);
    425 	udp_in6.sin6_family = AF_INET6;
    426 	udp_in6.sin6_port = uh->uh_sport;
    427 	/* KAME hack: recover scopeid */
    428 	(void)in6_recoverscope(&udp_in6, &ip6->ip6_src, m->m_pkthdr.rcvif);
    429 	if (in6p->in6p_flags & IN6P_CONTROLOPTS
    430 	 || in6p->in6p_socket->so_options & SO_TIMESTAMP) {
    431 		ip6_savecontrol(in6p, &opts, ip6, m);
    432 	}
    433 
    434 	m_adj(m, off + sizeof(struct udphdr));
    435 	if (sbappendaddr(&in6p->in6p_socket->so_rcv,
    436 			(struct sockaddr *)&udp_in6,
    437 			m, opts) == 0) {
    438 		udp6stat.udp6s_fullsock++;
    439 		goto bad;
    440 	}
    441 	sorwakeup(in6p->in6p_socket);
    442 	return IPPROTO_DONE;
    443 bad:
    444 	if (m)
    445 		m_freem(m);
    446 	if (opts)
    447 		m_freem(opts);
    448 	return IPPROTO_DONE;
    449 }
    450 #endif
    451 
    452 /*
    453  * Notify a udp user of an asynchronous error;
    454  * just wake up so that he can collect error status.
    455  */
    456 static	void
    457 udp6_notify(in6p, errno)
    458 	struct in6pcb *in6p;
    459 	int errno;
    460 {
    461 	in6p->in6p_socket->so_error = errno;
    462 	sorwakeup(in6p->in6p_socket);
    463 	sowwakeup(in6p->in6p_socket);
    464 }
    465 
    466 void
    467 udp6_ctlinput(cmd, sa, d)
    468 	int cmd;
    469 	struct sockaddr *sa;
    470 	void *d;
    471 {
    472 	struct udphdr uh;
    473 	struct ip6_hdr *ip6;
    474 	struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
    475 	struct mbuf *m;
    476 	int off;
    477 	void *cmdarg;
    478 	struct ip6ctlparam *ip6cp = NULL;
    479 	const struct sockaddr_in6 *sa6_src = NULL;
    480 	void (*notify) __P((struct in6pcb *, int)) = udp6_notify;
    481 	struct udp_portonly {
    482 		u_int16_t uh_sport;
    483 		u_int16_t uh_dport;
    484 	} *uhp;
    485 
    486 	if (sa->sa_family != AF_INET6 ||
    487 	    sa->sa_len != sizeof(struct sockaddr_in6))
    488 		return;
    489 
    490 	if ((unsigned)cmd >= PRC_NCMDS)
    491 		return;
    492 	if (PRC_IS_REDIRECT(cmd))
    493 		notify = in6_rtchange, d = NULL;
    494 	else if (cmd == PRC_HOSTDEAD)
    495 		d = NULL;
    496 	else if (cmd == PRC_MSGSIZE)
    497 		; /* special code is present, see below */
    498 	else if (inet6ctlerrmap[cmd] == 0)
    499 		return;
    500 
    501 	/* if the parameter is from icmp6, decode it. */
    502 	if (d != NULL) {
    503 		ip6cp = (struct ip6ctlparam *)d;
    504 		m = ip6cp->ip6c_m;
    505 		ip6 = ip6cp->ip6c_ip6;
    506 		off = ip6cp->ip6c_off;
    507 		cmdarg = ip6cp->ip6c_cmdarg;
    508 		sa6_src = ip6cp->ip6c_src;
    509 	} else {
    510 		m = NULL;
    511 		ip6 = NULL;
    512 		cmdarg = NULL;
    513 		sa6_src = &sa6_any;
    514 	}
    515 
    516 	if (ip6) {
    517 		/*
    518 		 * XXX: We assume that when IPV6 is non NULL,
    519 		 * M and OFF are valid.
    520 		 */
    521 
    522 		/* check if we can safely examine src and dst ports */
    523 		if (m->m_pkthdr.len < off + sizeof(*uhp)) {
    524 			if (cmd == PRC_MSGSIZE)
    525 				icmp6_mtudisc_update((struct ip6ctlparam *)d, 0);
    526 			return;
    527 		}
    528 
    529 		bzero(&uh, sizeof(uh));
    530 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
    531 
    532 		if (cmd == PRC_MSGSIZE) {
    533 			int valid = 0;
    534 
    535 			/*
    536 			 * Check to see if we have a valid UDP socket
    537 			 * corresponding to the address in the ICMPv6 message
    538 			 * payload.
    539 			 */
    540 			if (in6_pcblookup_connect(&udb6, &sa6->sin6_addr,
    541 			    uh.uh_dport, (struct in6_addr *)&sa6_src->sin6_addr,
    542 			    uh.uh_sport, 0))
    543 				valid++;
    544 #if 0
    545 			/*
    546 			 * As the use of sendto(2) is fairly popular,
    547 			 * we may want to allow non-connected pcb too.
    548 			 * But it could be too weak against attacks...
    549 			 * We should at least check if the local address (= s)
    550 			 * is really ours.
    551 			 */
    552 			else if (in6_pcblookup_bind(&udb6, &sa6->sin6_addr,
    553 						    uh.uh_dport, 0))
    554 				valid++;
    555 #endif
    556 
    557 			/*
    558 			 * Depending on the value of "valid" and routing table
    559 			 * size (mtudisc_{hi,lo}wat), we will:
    560 			 * - recalcurate the new MTU and create the
    561 			 *   corresponding routing entry, or
    562 			 * - ignore the MTU change notification.
    563 			 */
    564 			icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
    565 
    566 			/*
    567 			 * regardless of if we called icmp6_mtudisc_update(),
    568 			 * we need to call in6_pcbnotify(), to notify path
    569 			 * MTU change to the userland (2292bis-02), because
    570 			 * some unconnected sockets may share the same
    571 			 * destination and want to know the path MTU.
    572 			 */
    573 		}
    574 
    575 		(void) in6_pcbnotify(&udb6, sa, uh.uh_dport,
    576 		    (struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg,
    577 		    notify);
    578 	} else {
    579 		(void) in6_pcbnotify(&udb6, sa, 0, (struct sockaddr *)sa6_src,
    580 		    0, cmd, cmdarg, notify);
    581 	}
    582 }
    583 
    584 extern	int udp6_sendspace;
    585 extern	int udp6_recvspace;
    586 
    587 int
    588 udp6_usrreq(so, req, m, addr6, control, p)
    589 	struct socket *so;
    590 	int req;
    591 	struct mbuf *m, *addr6, *control;
    592 	struct proc *p;
    593 {
    594 	struct	in6pcb *in6p = sotoin6pcb(so);
    595 	int	error = 0;
    596 	int	s;
    597 
    598 	/*
    599 	 * MAPPED_ADDR implementation info:
    600 	 *  Mapped addr support for PRU_CONTROL is not necessary.
    601 	 *  Because typical user of PRU_CONTROL is such as ifconfig,
    602 	 *  and they don't associate any addr to their socket.  Then
    603 	 *  socket family is only hint about the PRU_CONTROL'ed address
    604 	 *  family, especially when getting addrs from kernel.
    605 	 *  So AF_INET socket need to be used to control AF_INET addrs,
    606 	 *  and AF_INET6 socket for AF_INET6 addrs.
    607 	 */
    608 	if (req == PRU_CONTROL)
    609 		return(in6_control(so, (u_long)m, (caddr_t)addr6,
    610 				   (struct ifnet *)control, p));
    611 
    612 	if (req == PRU_PURGEIF) {
    613 		in6_pcbpurgeif0(&udb6, (struct ifnet *)control);
    614 		in6_purgeif((struct ifnet *)control);
    615 		in6_pcbpurgeif(&udb6, (struct ifnet *)control);
    616 		return (0);
    617 	}
    618 
    619 	if (in6p == NULL && req != PRU_ATTACH) {
    620 		error = EINVAL;
    621 		goto release;
    622 	}
    623 
    624 	switch (req) {
    625 	case PRU_ATTACH:
    626 		/*
    627 		 * MAPPED_ADDR implementation spec:
    628 		 *  Always attach for IPv6,
    629 		 *  and only when necessary for IPv4.
    630 		 */
    631 		if (in6p != NULL) {
    632 			error = EINVAL;
    633 			break;
    634 		}
    635 		s = splsoftnet();
    636 		error = in6_pcballoc(so, &udb6);
    637 		splx(s);
    638 		if (error)
    639 			break;
    640 		error = soreserve(so, udp6_sendspace, udp6_recvspace);
    641 		if (error)
    642 			break;
    643 		in6p = sotoin6pcb(so);
    644 		in6p->in6p_cksum = -1;	/* just to be sure */
    645 #ifdef IPSEC
    646 		error = ipsec_init_policy(so, &in6p->in6p_sp);
    647 		if (error != 0) {
    648 			in6_pcbdetach(in6p);
    649 			break;
    650 		}
    651 #endif /*IPSEC*/
    652 		break;
    653 
    654 	case PRU_DETACH:
    655 		udp6_detach(in6p);
    656 		break;
    657 
    658 	case PRU_BIND:
    659 		s = splsoftnet();
    660 		error = in6_pcbbind(in6p, addr6, p);
    661 		splx(s);
    662 		break;
    663 
    664 	case PRU_LISTEN:
    665 		error = EOPNOTSUPP;
    666 		break;
    667 
    668 	case PRU_CONNECT:
    669 		if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    670 			error = EISCONN;
    671 			break;
    672 		}
    673 		s = splsoftnet();
    674 		error = in6_pcbconnect(in6p, addr6);
    675 		if (ip6_auto_flowlabel) {
    676 			in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
    677 			in6p->in6p_flowinfo |=
    678 				(htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
    679 		}
    680 		splx(s);
    681 		if (error == 0)
    682 			soisconnected(so);
    683 		break;
    684 
    685 	case PRU_CONNECT2:
    686 		error = EOPNOTSUPP;
    687 		break;
    688 
    689 	case PRU_ACCEPT:
    690 		error = EOPNOTSUPP;
    691 		break;
    692 
    693 	case PRU_DISCONNECT:
    694 		if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
    695 			error = ENOTCONN;
    696 			break;
    697 		}
    698 		s = splsoftnet();
    699 		in6_pcbdisconnect(in6p);
    700 		bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr));
    701 		splx(s);
    702 		so->so_state &= ~SS_ISCONNECTED;		/* XXX */
    703 		break;
    704 
    705 	case PRU_SHUTDOWN:
    706 		socantsendmore(so);
    707 		break;
    708 
    709 	case PRU_SEND:
    710 		return(udp6_output(in6p, m, addr6, control, p));
    711 
    712 	case PRU_ABORT:
    713 		soisdisconnected(so);
    714 		udp6_detach(in6p);
    715 		break;
    716 
    717 	case PRU_SOCKADDR:
    718 		in6_setsockaddr(in6p, addr6);
    719 		break;
    720 
    721 	case PRU_PEERADDR:
    722 		in6_setpeeraddr(in6p, addr6);
    723 		break;
    724 
    725 	case PRU_SENSE:
    726 		/*
    727 		 * stat: don't bother with a blocksize
    728 		 */
    729 		return(0);
    730 
    731 	case PRU_SENDOOB:
    732 	case PRU_FASTTIMO:
    733 	case PRU_SLOWTIMO:
    734 	case PRU_PROTORCV:
    735 	case PRU_PROTOSEND:
    736 		error = EOPNOTSUPP;
    737 		break;
    738 
    739 	case PRU_RCVD:
    740 	case PRU_RCVOOB:
    741 		return(EOPNOTSUPP);	/* do not free mbuf's */
    742 
    743 	default:
    744 		panic("udp6_usrreq");
    745 	}
    746 
    747 release:
    748 	if (control)
    749 		m_freem(control);
    750 	if (m)
    751 		m_freem(m);
    752 	return(error);
    753 }
    754 
    755 static void
    756 udp6_detach(in6p)
    757 	struct in6pcb *in6p;
    758 {
    759 	int s = splsoftnet();
    760 
    761 	if (in6p == udp6_last_in6pcb)
    762 		udp6_last_in6pcb = &udb6;
    763 	in6_pcbdetach(in6p);
    764 	splx(s);
    765 }
    766 
    767 #include <uvm/uvm_extern.h>
    768 #include <sys/sysctl.h>
    769 
    770 int
    771 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
    772 	int *name;
    773 	u_int namelen;
    774 	void *oldp;
    775 	size_t *oldlenp;
    776 	void *newp;
    777 	size_t newlen;
    778 {
    779 	/* All sysctl names at this level are terminal. */
    780 	if (namelen != 1)
    781 		return ENOTDIR;
    782 
    783 	switch (name[0]) {
    784 
    785 	case UDP6CTL_SENDSPACE:
    786 		return sysctl_int(oldp, oldlenp, newp, newlen,
    787 		    &udp6_sendspace);
    788 	case UDP6CTL_RECVSPACE:
    789 		return sysctl_int(oldp, oldlenp, newp, newlen,
    790 		    &udp6_recvspace);
    791 	default:
    792 		return ENOPROTOOPT;
    793 	}
    794 	/* NOTREACHED */
    795 }
    796