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
31 * Copyright (c) 1982, 1986, 1989, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/kernel.h>
70 #include <sys/malloc.h>
72 #include <sys/protosw.h>
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/sysctl.h>
76 #include <sys/errno.h>
78 #include <sys/systm.h>
79 #include <sys/syslog.h>
83 #include <net/route.h>
84 #include <net/if_types.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_var.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/udp.h>
93 #include <netinet/udp_var.h>
94 #include <netinet6/ip6.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/icmp6.h>
98 #include <netinet6/udp6_var.h>
99 #include <netinet6/ip6protosw.h>
102 #include <netinet6/ipsec.h>
103 #include <netinet6/ipsec6.h>
109 * UDP protocol inplementation.
110 * Per RFC 768, August, 1980.
113 extern struct protosw inetsw[];
114 static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
115 static int udp6_detach __P((struct socket *so));
118 in6_mcmatch(in6p, ia6, ifp)
120 register struct in6_addr *ia6;
123 struct ip6_moptions *im6o = in6p->in6p_moptions;
124 struct in6_multi_mship *imm;
129 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
130 imm = imm->i6mm_chain.le_next) {
132 imm->i6mm_maddr->in6m_ifp == ifp) &&
133 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
141 udp6_input(mp, offp, proto)
145 struct mbuf *m = *mp;
146 register struct ip6_hdr *ip6;
147 register struct udphdr *uh;
148 register struct inpcb *in6p;
149 struct mbuf *opts = 0;
152 struct sockaddr_in6 udp_in6;
154 #if defined(NFAITH) && 0 < NFAITH
155 if (m->m_pkthdr.rcvif) {
156 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
157 /* XXX send icmp6 host/port unreach? */
163 udpstat.udps_ipackets++;
165 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
167 ip6 = mtod(m, struct ip6_hdr *);
168 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
169 uh = (struct udphdr *)((caddr_t)ip6 + off);
170 ulen = ntohs((u_short)uh->uh_ulen);
173 udpstat.udps_badlen++;
178 * Checksum extended UDP header and data.
181 udpstat.udps_nosum++;
182 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
183 udpstat.udps_badsum++;
187 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
191 * Deliver a multicast datagram to all sockets
192 * for which the local and remote addresses and ports match
193 * those of the incoming datagram. This allows more than
194 * one process to receive multicasts on the same port.
195 * (This really ought to be done for unicast datagrams as
196 * well, but that would cause problems with existing
197 * applications that open both address-specific sockets and
198 * a wildcard socket listening to the same port -- they would
199 * end up receiving duplicates of every unicast datagram.
200 * Those applications open the multiple sockets to overcome an
201 * inadequacy of the UDP socket interface, but for backwards
202 * compatibility we avoid the problem here rather than
203 * fixing the interface. Maybe 4.5BSD will remedy this?)
207 * In a case that laddr should be set to the link-local
208 * address (this happens in RIPng), the multicast address
209 * specified in the received packet does not match with
210 * laddr. To cure this situation, the matching is relaxed
211 * if the receiving interface is the same as one specified
212 * in the socket and if the destination multicast address
213 * matches one of the multicast groups specified in the socket.
217 * Construct sockaddr format source address.
219 init_sin6(&udp_in6, m); /* general init */
220 udp_in6.sin6_port = uh->uh_sport;
222 * KAME note: usually we drop udphdr from mbuf here.
223 * We need udphdr for IPsec processing so we do that later.
227 * Locate pcb(s) for datagram.
228 * (Algorithm copied from raw_intr().)
231 LIST_FOREACH(in6p, &udb, inp_list) {
232 if ((in6p->inp_vflag & INP_IPV6) == 0)
234 if (in6p->in6p_lport != uh->uh_dport)
236 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
237 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
239 !in6_mcmatch(in6p, &ip6->ip6_dst,
243 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
244 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
246 in6p->in6p_fport != uh->uh_sport)
255 * Check AH/ESP integrity.
257 if (ipsec6_in_reject_so(m, last->inp_socket))
258 ipsec6stat.in_polvio++;
259 /* do not inject data into pcb */
262 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
265 * m_copy(m, offset, ...) above.
266 * sbappendaddr() expects M_PKTHDR,
267 * and m_copy() will copy M_PKTHDR
268 * only if offset is 0.
270 if (last->in6p_flags & IN6P_CONTROLOPTS
271 || last->in6p_socket->so_options & SO_TIMESTAMP)
272 ip6_savecontrol(last, &opts,
274 m_adj(n, off + sizeof(struct udphdr));
275 if (sbappendaddr(&last->in6p_socket->so_rcv,
276 (struct sockaddr *)&udp_in6,
281 udpstat.udps_fullsock++;
283 sorwakeup(last->in6p_socket);
289 * Don't look for additional matches if this one does
290 * not have either the SO_REUSEPORT or SO_REUSEADDR
291 * socket options set. This heuristic avoids searching
292 * through all pcbs in the common case of a non-shared
293 * port. It assumes that an application will never
294 * clear these options after setting them.
296 if ((last->in6p_socket->so_options &
297 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
303 * No matching pcb found; discard datagram.
304 * (No need to send an ICMP Port Unreachable
305 * for a broadcast or multicast datgram.)
307 udpstat.udps_noport++;
308 udpstat.udps_noportmcast++;
313 * Check AH/ESP integrity.
315 if (ipsec6_in_reject_so(m, last->inp_socket)) {
316 ipsec6stat.in_polvio++;
320 if (last->in6p_flags & IN6P_CONTROLOPTS
321 || last->in6p_socket->so_options & SO_TIMESTAMP)
322 ip6_savecontrol(last, &opts, ip6, m);
324 m_adj(m, off + sizeof(struct udphdr));
325 if (sbappendaddr(&last->in6p_socket->so_rcv,
326 (struct sockaddr *)&udp_in6,
328 udpstat.udps_fullsock++;
331 sorwakeup(last->in6p_socket);
335 * Locate pcb for datagram.
337 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
338 &ip6->ip6_dst, uh->uh_dport, 1,
342 char buf[INET6_ADDRSTRLEN];
344 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
346 "Connection attempt to UDP %s:%d from %s:%d\n",
347 buf, ntohs(uh->uh_dport),
348 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
350 udpstat.udps_noport++;
351 if (m->m_flags & M_MCAST) {
352 printf("UDP6: M_MCAST is set in a unicast packet.\n");
353 udpstat.udps_noportmcast++;
356 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
361 * Check AH/ESP integrity.
363 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
364 ipsec6stat.in_polvio++;
370 * Construct sockaddr format source address.
371 * Stuff source address and datagram in user buffer.
373 init_sin6(&udp_in6, m); /* general init */
374 udp_in6.sin6_port = uh->uh_sport;
375 if (in6p->in6p_flags & IN6P_CONTROLOPTS
376 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
377 ip6_savecontrol(in6p, &opts, ip6, m);
378 m_adj(m, off + sizeof(struct udphdr));
379 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
380 (struct sockaddr *)&udp_in6,
382 udpstat.udps_fullsock++;
385 sorwakeup(in6p->in6p_socket);
396 udp6_ctlinput(cmd, sa, d)
401 register struct udphdr *uhp;
403 struct sockaddr_in6 sa6;
408 if (sa->sa_family != AF_INET6 ||
409 sa->sa_len != sizeof(struct sockaddr_in6))
413 if (!PRC_IS_REDIRECT(cmd) &&
414 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
417 /* if the parameter is from icmp6, decode it. */
419 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
421 ip6 = ip6cp->ip6c_ip6;
422 off = ip6cp->ip6c_off;
428 /* translate addresses into internal form */
429 sa6 = *(struct sockaddr_in6 *)sa;
430 if (m != NULL && IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
431 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
435 * XXX: We assume that when IPV6 is non NULL,
436 * M and OFF are valid.
440 /* translate addresses into internal form */
441 memcpy(&s, &ip6->ip6_src, sizeof(s));
442 if (IN6_IS_ADDR_LINKLOCAL(&s))
443 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
445 if (m->m_len < off + sizeof(uh)) {
447 * this should be rare case,
448 * so we compromise on this copy...
450 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
453 uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
454 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
456 uhp->uh_sport, cmd, udp_notify);
458 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
459 &zeroin6_addr, 0, cmd, udp_notify);
463 udp6_getcred SYSCTL_HANDLER_ARGS
465 struct sockaddr_in6 addrs[2];
469 error = suser(req->p);
472 error = SYSCTL_IN(req, addrs, sizeof(addrs));
476 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
478 &addrs[0].sin6_addr, addrs[0].sin6_port,
480 if (!inp || !inp->inp_socket) {
484 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
485 sizeof(struct ucred));
492 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
494 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
497 udp6_output(in6p, m, addr6, control, p)
498 register struct inpcb *in6p;
500 struct sockaddr *addr6;
501 struct mbuf *control;
504 register int ulen = m->m_pkthdr.len;
505 int plen = sizeof(struct udphdr) + ulen;
508 struct in6_addr laddr6;
509 int s = 0, error = 0;
510 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
513 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
515 in6p->in6p_outputopts = &opt;
519 laddr6 = in6p->in6p_laddr;
520 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
525 * Must block input while temporarily connected.
529 * XXX: the user might want to overwrite the local address
530 * via an ancillary data.
532 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
533 error = in6_pcbconnect(in6p, addr6, p);
539 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
545 * Calculate data length and get a mbuf
546 * for UDP and IP6 headers.
548 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
558 * Stuff checksum and output datagram.
560 ip6 = mtod(m, struct ip6_hdr *);
561 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
562 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
563 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
564 (IPV6_VERSION & IPV6_VERSION_MASK);
565 /* ip6_plen will be filled in ip6_output. */
566 ip6->ip6_nxt = IPPROTO_UDP;
567 ip6->ip6_hlim = in6_selecthlim(in6p,
568 in6p->in6p_route.ro_rt ?
569 in6p->in6p_route.ro_rt->rt_ifp :
571 ip6->ip6_src = in6p->in6p_laddr;
572 ip6->ip6_dst = in6p->in6p_faddr;
574 udp6 = (struct udphdr *)(ip6 + 1);
575 udp6->uh_sport = in6p->in6p_lport;
576 udp6->uh_dport = in6p->in6p_fport;
577 udp6->uh_ulen = htons((u_short)plen);
580 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
581 sizeof(struct ip6_hdr), plen)) == 0) {
582 udp6->uh_sum = 0xffff;
585 udpstat.udps_opackets++;
588 m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
590 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
591 IPV6_SOCKINMRCVIF, in6p->in6p_moptions, NULL);
594 in6_pcbdisconnect(in6p);
595 in6p->in6p_laddr = laddr6;
605 in6p->in6p_outputopts = stickyopt;
612 udp6_abort(struct socket *so)
619 return EINVAL; /* ??? possible? panic instead? */
620 soisdisconnected(so);
628 udp6_attach(struct socket *so, int proto, struct proc *p)
637 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
638 error = soreserve(so, udp_sendspace, udp_recvspace);
643 error = in_pcballoc(so, &udbinfo, p);
647 inp = (struct inpcb *)so->so_pcb;
648 inp->inp_vflag |= INP_IPV6;
649 inp->in6p_hops = -1; /* use kernel default */
650 inp->in6p_cksum = -1; /* just to be sure */
652 error = ipsec_init_policy(so, &inp->in6p_sp);
662 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
671 inp->inp_vflag &= ~INP_IPV4;
672 inp->inp_vflag |= INP_IPV6;
673 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == 0) {
674 struct sockaddr_in6 *sin6_p;
676 sin6_p = (struct sockaddr_in6 *)nam;
678 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
679 inp->inp_vflag |= INP_IPV4;
680 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
681 struct sockaddr_in sin;
683 in6_sin6_2_sin(&sin, sin6_p);
684 inp->inp_vflag |= INP_IPV4;
685 inp->inp_vflag &= ~INP_IPV6;
687 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
694 error = in6_pcbbind(inp, nam, p);
700 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
708 if (ip6_mapped_addr_on) {
709 struct sockaddr_in6 *sin6_p;
711 sin6_p = (struct sockaddr_in6 *)nam;
712 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
713 struct sockaddr_in sin;
715 if (inp->inp_faddr.s_addr != INADDR_ANY)
717 in6_sin6_2_sin(&sin, sin6_p);
719 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
722 inp->inp_vflag |= INP_IPV4;
723 inp->inp_vflag &= ~INP_IPV6;
729 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
732 error = in6_pcbconnect(inp, nam, p);
735 inp->inp_vflag &= ~INP_IPV4;
736 inp->inp_vflag |= INP_IPV6;
743 udp6_detach(struct socket *so)
758 udp6_disconnect(struct socket *so)
767 if (inp->inp_vflag & INP_IPV4) {
768 struct pr_usrreqs *pru;
770 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
771 return ((*pru->pru_disconnect)(so));
774 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
778 in6_pcbdisconnect(inp);
779 inp->in6p_laddr = in6addr_any;
781 so->so_state &= ~SS_ISCONNECTED; /* XXX */
786 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
787 struct mbuf *control, struct proc *p)
797 if (ip6_mapped_addr_on) {
799 struct sockaddr_in6 *sin6 = 0;
802 hasv4addr = (inp->inp_vflag & INP_IPV4);
804 sin6 = (struct sockaddr_in6 *)addr;
805 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
809 struct pr_usrreqs *pru;
813 in6_sin6_2_sin_in_sock(addr);
814 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
815 error = ((*pru->pru_send)(so, flags, m, addr, control,
817 /* addr will just be freed in sendit(). */
822 return udp6_output(inp, m, addr, control, p);
825 struct pr_usrreqs udp6_usrreqs = {
826 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
827 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
828 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
829 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
830 in6_mapped_sockaddr, sosend, soreceive, sopoll