2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
34 * Copyright (c) 1982, 1986, 1989, 1993
35 * The Regents of the University of California. All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
61 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
68 #include "opt_inet6.h"
69 #include "opt_ipsec.h"
71 #include <sys/param.h>
72 #include <sys/errno.h>
73 #include <sys/kernel.h>
77 #include <sys/protosw.h>
78 #include <sys/signalvar.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
83 #include <sys/sysctl.h>
84 #include <sys/syslog.h>
85 #include <sys/systm.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip.h>
96 #include <netinet/ip6.h>
97 #include <netinet/icmp6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/udp.h>
100 #include <netinet/udp_var.h>
101 #include <netinet6/ip6protosw.h>
102 #include <netinet6/ip6_var.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/udp6_var.h>
105 #include <netinet6/scope6_var.h>
108 #include <netinet6/ipsec.h>
109 #include <netinet6/ipsec6.h>
113 #include <netipsec/ipsec.h>
114 #include <netipsec/ipsec6.h>
115 #endif /* FAST_IPSEC */
118 * UDP protocol inplementation.
119 * Per RFC 768, August, 1980.
122 extern struct protosw inetsw[];
123 static int udp6_detach __P((struct socket *so));
126 udp6_input(mp, offp, proto)
130 struct mbuf *m = *mp, *opts;
131 register struct ip6_hdr *ip6;
132 register struct udphdr *uh;
133 register struct inpcb *in6p;
136 struct sockaddr_in6 fromsa;
140 ip6 = mtod(m, struct ip6_hdr *);
142 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
143 /* XXX send icmp6 host/port unreach? */
148 #ifndef PULLDOWN_TEST
149 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
150 ip6 = mtod(m, struct ip6_hdr *);
151 uh = (struct udphdr *)((caddr_t)ip6 + off);
153 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
158 udpstat.udps_ipackets++;
160 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
161 ulen = ntohs((u_short)uh->uh_ulen);
164 udpstat.udps_badlen++;
169 * Checksum extended UDP header and data.
171 if (uh->uh_sum == 0) {
172 udpstat.udps_nosum++;
175 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
176 udpstat.udps_badsum++;
180 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
184 * Deliver a multicast datagram to all sockets
185 * for which the local and remote addresses and ports match
186 * those of the incoming datagram. This allows more than
187 * one process to receive multicasts on the same port.
188 * (This really ought to be done for unicast datagrams as
189 * well, but that would cause problems with existing
190 * applications that open both address-specific sockets and
191 * a wildcard socket listening to the same port -- they would
192 * end up receiving duplicates of every unicast datagram.
193 * Those applications open the multiple sockets to overcome an
194 * inadequacy of the UDP socket interface, but for backwards
195 * compatibility we avoid the problem here rather than
196 * fixing the interface. Maybe 4.5BSD will remedy this?)
200 * In a case that laddr should be set to the link-local
201 * address (this happens in RIPng), the multicast address
202 * specified in the received packet does not match with
203 * laddr. To cure this situation, the matching is relaxed
204 * if the receiving interface is the same as one specified
205 * in the socket and if the destination multicast address
206 * matches one of the multicast groups specified in the socket.
210 * Construct sockaddr format source address.
212 init_sin6(&fromsa, m);
213 fromsa.sin6_port = uh->uh_sport;
215 * KAME note: traditionally we dropped udpiphdr from mbuf here.
216 * We need udphdr for IPsec processing so we do that later.
220 * Locate pcb(s) for datagram.
221 * (Algorithm copied from raw_intr().)
224 LIST_FOREACH(in6p, &udb, inp_list) {
225 if ((in6p->inp_vflag & INP_IPV6) == 0)
227 if (in6p->in6p_lport != uh->uh_dport)
229 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
230 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
234 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
235 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
237 in6p->in6p_fport != uh->uh_sport)
244 #if defined(IPSEC) || defined(FAST_IPSEC)
246 * Check AH/ESP integrity.
248 if (ipsec6_in_reject(m, last)) {
250 ipsec6stat.in_polvio++;
252 /* do not inject data into pcb */
254 #endif /*IPSEC || FAST_IPSEC*/
255 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
258 * m_copy(m, offset, ...) above.
259 * sbappendaddr() expects M_PKTHDR,
260 * and m_copy() will copy M_PKTHDR
261 * only if offset is 0.
263 if (last->in6p_flags & IN6P_CONTROLOPTS
264 || last->in6p_socket->so_options & SO_TIMESTAMP)
265 ip6_savecontrol(last, n, &opts);
267 m_adj(n, off + sizeof(struct udphdr));
268 if (sbappendaddr(&last->in6p_socket->so_rcv,
269 (struct sockaddr *)&fromsa,
274 udpstat.udps_fullsock++;
276 sorwakeup(last->in6p_socket);
282 * Don't look for additional matches if this one does
283 * not have either the SO_REUSEPORT or SO_REUSEADDR
284 * socket options set. This heuristic avoids searching
285 * through all pcbs in the common case of a non-shared
286 * port. It assumes that an application will never
287 * clear these options after setting them.
289 if ((last->in6p_socket->so_options &
290 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
296 * No matching pcb found; discard datagram.
297 * (No need to send an ICMP Port Unreachable
298 * for a broadcast or multicast datgram.)
300 udpstat.udps_noport++;
301 udpstat.udps_noportmcast++;
304 #if defined(IPSEC) || defined(FAST_IPSEC)
306 * Check AH/ESP integrity.
308 if (ipsec6_in_reject(m, last)) {
310 ipsec6stat.in_polvio++;
314 #endif /*IPSEC || FAST_IPSEC*/
315 if (last->in6p_flags & IN6P_CONTROLOPTS
316 || last->in6p_socket->so_options & SO_TIMESTAMP)
317 ip6_savecontrol(last, m, &opts);
319 m_adj(m, off + sizeof(struct udphdr));
320 if (sbappendaddr(&last->in6p_socket->so_rcv,
321 (struct sockaddr *)&fromsa,
323 udpstat.udps_fullsock++;
326 sorwakeup(last->in6p_socket);
330 * Locate pcb for datagram.
332 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
333 &ip6->ip6_dst, uh->uh_dport, 1,
337 char buf[INET6_ADDRSTRLEN];
339 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
341 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
342 buf, ntohs(uh->uh_dport),
343 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
345 udpstat.udps_noport++;
346 if (m->m_flags & M_MCAST) {
347 printf("UDP6: M_MCAST is set in a unicast packet.\n");
348 udpstat.udps_noportmcast++;
351 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
354 #if defined(IPSEC) || defined(FAST_IPSEC)
356 * Check AH/ESP integrity.
358 if (ipsec6_in_reject(m, in6p)) {
360 ipsec6stat.in_polvio++;
364 #endif /*IPSEC || FAST_IPSEC*/
367 * Construct sockaddr format source address.
368 * Stuff source address and datagram in user buffer.
370 init_sin6(&fromsa, m);
371 fromsa.sin6_port = uh->uh_sport;
372 if (in6p->in6p_flags & IN6P_CONTROLOPTS
373 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
374 ip6_savecontrol(in6p, m, &opts);
375 m_adj(m, off + sizeof(struct udphdr));
376 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
377 (struct sockaddr *)&fromsa, m, opts) == 0) {
378 udpstat.udps_fullsock++;
381 sorwakeup(in6p->in6p_socket);
392 udp6_ctlinput(cmd, sa, d)
401 struct ip6ctlparam *ip6cp = NULL;
402 const struct sockaddr_in6 *sa6_src = NULL;
404 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
405 struct udp_portonly {
410 if (sa->sa_family != AF_INET6 ||
411 sa->sa_len != sizeof(struct sockaddr_in6))
414 if ((unsigned)cmd >= PRC_NCMDS)
416 if (PRC_IS_REDIRECT(cmd))
417 notify = in6_rtchange, d = NULL;
418 else if (cmd == PRC_HOSTDEAD)
420 else if (inet6ctlerrmap[cmd] == 0)
423 /* if the parameter is from icmp6, decode it. */
425 ip6cp = (struct ip6ctlparam *)d;
427 ip6 = ip6cp->ip6c_ip6;
428 off = ip6cp->ip6c_off;
429 cmdarg = ip6cp->ip6c_cmdarg;
430 sa6_src = ip6cp->ip6c_src;
440 * XXX: We assume that when IPV6 is non NULL,
441 * M and OFF are valid.
444 /* check if we can safely examine src and dst ports */
445 if (m->m_pkthdr.len < off + sizeof(*uhp))
448 bzero(&uh, sizeof(uh));
449 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
451 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
452 (struct sockaddr *)ip6cp->ip6c_src,
453 uh.uh_sport, cmd, cmdarg, notify);
455 (void) in6_pcbnotify(&udbinfo, sa, 0,
456 (const struct sockaddr *)sa6_src,
457 0, cmd, cmdarg, notify);
461 udp6_getcred(SYSCTL_HANDLER_ARGS)
464 struct sockaddr_in6 addrs[2];
468 error = suser(req->td);
472 if (req->newlen != sizeof(addrs))
474 if (req->oldlen != sizeof(struct xucred))
476 error = SYSCTL_IN(req, addrs, sizeof(addrs));
479 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
480 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
484 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
486 &addrs[0].sin6_addr, addrs[0].sin6_port,
488 if (!inp || !inp->inp_socket) {
492 cru2x(inp->inp_socket->so_cred, &xuc);
493 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
499 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
501 udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
504 udp6_abort(struct socket *so)
509 INP_INFO_WLOCK(&udbinfo);
512 INP_INFO_WUNLOCK(&udbinfo);
513 return EINVAL; /* ??? possible? panic instead? */
515 soisdisconnected(so);
519 INP_INFO_WUNLOCK(&udbinfo);
525 udp6_attach(struct socket *so, int proto, struct thread *td)
530 INP_INFO_WLOCK(&udbinfo);
533 INP_INFO_WUNLOCK(&udbinfo);
537 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
538 error = soreserve(so, udp_sendspace, udp_recvspace);
540 INP_INFO_WUNLOCK(&udbinfo);
545 error = in_pcballoc(so, &udbinfo);
548 INP_INFO_WUNLOCK(&udbinfo);
551 inp = (struct inpcb *)so->so_pcb;
552 INP_INFO_WUNLOCK(&udbinfo);
553 inp->inp_vflag |= INP_IPV6;
554 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
555 inp->inp_vflag |= INP_IPV4;
556 inp->in6p_hops = -1; /* use kernel default */
557 inp->in6p_cksum = -1; /* just to be sure */
560 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
561 * because the socket may be bound to an IPv6 wildcard address,
562 * which may match an IPv4-mapped IPv6 address.
564 inp->inp_ip_ttl = ip_defttl;
570 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
575 INP_INFO_WLOCK(&udbinfo);
578 INP_INFO_WUNLOCK(&udbinfo);
583 inp->inp_vflag &= ~INP_IPV4;
584 inp->inp_vflag |= INP_IPV6;
585 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
586 struct sockaddr_in6 *sin6_p;
588 sin6_p = (struct sockaddr_in6 *)nam;
590 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
591 inp->inp_vflag |= INP_IPV4;
592 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
593 struct sockaddr_in sin;
595 in6_sin6_2_sin(&sin, sin6_p);
596 inp->inp_vflag |= INP_IPV4;
597 inp->inp_vflag &= ~INP_IPV6;
599 error = in_pcbbind(inp, (struct sockaddr *)&sin,
606 error = in6_pcbbind(inp, nam, td->td_ucred);
609 INP_INFO_WUNLOCK(&udbinfo);
615 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
620 INP_INFO_WLOCK(&udbinfo);
623 INP_INFO_WUNLOCK(&udbinfo);
628 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
629 struct sockaddr_in6 *sin6_p;
631 sin6_p = (struct sockaddr_in6 *)nam;
632 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
633 struct sockaddr_in sin;
635 if (inp->inp_faddr.s_addr != INADDR_ANY)
637 in6_sin6_2_sin(&sin, sin6_p);
639 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
643 inp->inp_vflag |= INP_IPV4;
644 inp->inp_vflag &= ~INP_IPV6;
650 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
655 error = in6_pcbconnect(inp, nam, td->td_ucred);
658 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
659 /* should be non mapped addr */
660 inp->inp_vflag &= ~INP_IPV4;
661 inp->inp_vflag |= INP_IPV6;
667 INP_INFO_WUNLOCK(&udbinfo);
672 udp6_detach(struct socket *so)
677 INP_INFO_WLOCK(&udbinfo);
680 INP_INFO_WUNLOCK(&udbinfo);
687 INP_INFO_WUNLOCK(&udbinfo);
692 udp6_disconnect(struct socket *so)
697 INP_INFO_WLOCK(&udbinfo);
700 INP_INFO_WUNLOCK(&udbinfo);
706 if (inp->inp_vflag & INP_IPV4) {
707 struct pr_usrreqs *pru;
709 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
710 error = (*pru->pru_disconnect)(so);
715 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
721 in6_pcbdisconnect(inp);
722 inp->in6p_laddr = in6addr_any;
724 /* XXXRW: so_state locking? */
725 so->so_state &= ~SS_ISCONNECTED; /* XXX */
728 INP_INFO_WUNLOCK(&udbinfo);
733 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
734 struct mbuf *control, struct thread *td)
739 INP_INFO_WLOCK(&udbinfo);
742 INP_INFO_WUNLOCK(&udbinfo);
749 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
753 if (addr->sa_family != AF_INET6) {
754 error = EAFNOSUPPORT;
760 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
762 struct sockaddr_in6 *sin6 = 0;
765 hasv4addr = (inp->inp_vflag & INP_IPV4);
767 sin6 = (struct sockaddr_in6 *)addr;
768 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
772 struct pr_usrreqs *pru;
774 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
775 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
777 * when remote addr is IPv4-mapped
778 * address, local addr should not be
779 * an IPv6 address; since you cannot
780 * determine how to map IPv6 source
787 in6_sin6_2_sin_in_sock(addr);
788 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
789 error = ((*pru->pru_send)(so, flags, m, addr, control,
791 /* addr will just be freed in sendit(). */
797 error = udp6_output(inp, m, addr, control, td);
800 INP_INFO_WUNLOCK(&udbinfo);
805 INP_INFO_WUNLOCK(&udbinfo);
810 struct pr_usrreqs udp6_usrreqs = {
811 .pru_abort = udp6_abort,
812 .pru_attach = udp6_attach,
813 .pru_bind = udp6_bind,
814 .pru_connect = udp6_connect,
815 .pru_control = in6_control,
816 .pru_detach = udp6_detach,
817 .pru_disconnect = udp6_disconnect,
818 .pru_peeraddr = in6_mapped_peeraddr,
819 .pru_send = udp6_send,
820 .pru_shutdown = udp_shutdown,
821 .pru_sockaddr = in6_mapped_sockaddr,
822 .pru_sosetlabel = in_pcbsosetlabel