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 <sys/param.h>
67 #include <sys/kernel.h>
68 #include <sys/malloc.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/errno.h>
76 #include <sys/systm.h>
77 #include <sys/syslog.h>
81 #include <net/route.h>
82 #include <net/if_types.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/in_var.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/udp.h>
91 #include <netinet/udp_var.h>
92 #include <netinet6/ip6.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/icmp6.h>
96 #include <netinet6/udp6_var.h>
97 #include <netinet6/ip6protosw.h>
100 #include <netinet6/ipsec.h>
106 * UDP protocol inplementation.
107 * Per RFC 768, August, 1980.
110 extern struct protosw inetsw[];
111 static int in6_mcmatch __P((struct inpcb *, struct in6_addr *, struct ifnet *));
112 static int udp6_detach __P((struct socket *so));
115 in6_mcmatch(in6p, ia6, ifp)
117 register struct in6_addr *ia6;
120 struct ip6_moptions *im6o = in6p->in6p_moptions;
121 struct in6_multi_mship *imm;
126 for (imm = im6o->im6o_memberships.lh_first; imm != NULL;
127 imm = imm->i6mm_chain.le_next) {
129 imm->i6mm_maddr->in6m_ifp == ifp) &&
130 IN6_ARE_ADDR_EQUAL(&imm->i6mm_maddr->in6m_addr,
138 udp6_input(mp, offp, proto)
142 struct mbuf *m = *mp;
143 register struct ip6_hdr *ip6;
144 register struct udphdr *uh;
145 register struct inpcb *in6p;
146 struct mbuf *opts = 0;
149 struct sockaddr_in6 udp_in6;
151 #if defined(NFAITH) && 0 < NFAITH
152 if (m->m_pkthdr.rcvif) {
153 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
154 /* XXX send icmp6 host/port unreach? */
160 udpstat.udps_ipackets++;
162 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
164 ip6 = mtod(m, struct ip6_hdr *);
165 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
166 uh = (struct udphdr *)((caddr_t)ip6 + off);
167 ulen = ntohs((u_short)uh->uh_ulen);
170 udpstat.udps_badlen++;
175 * Checksum extended UDP header and data.
178 udpstat.udps_nosum++;
179 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
180 udpstat.udps_badsum++;
184 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
188 * Deliver a multicast datagram to all sockets
189 * for which the local and remote addresses and ports match
190 * those of the incoming datagram. This allows more than
191 * one process to receive multicasts on the same port.
192 * (This really ought to be done for unicast datagrams as
193 * well, but that would cause problems with existing
194 * applications that open both address-specific sockets and
195 * a wildcard socket listening to the same port -- they would
196 * end up receiving duplicates of every unicast datagram.
197 * Those applications open the multiple sockets to overcome an
198 * inadequacy of the UDP socket interface, but for backwards
199 * compatibility we avoid the problem here rather than
200 * fixing the interface. Maybe 4.5BSD will remedy this?)
204 * In a case that laddr should be set to the link-local
205 * address (this happens in RIPng), the multicast address
206 * specified in the received packet does not match with
207 * laddr. To cure this situation, the matching is relaxed
208 * if the receiving interface is the same as one specified
209 * in the socket and if the destination multicast address
210 * matches one of the multicast groups specified in the socket.
214 * Construct sockaddr format source address.
216 init_sin6(&udp_in6, m); /* general init */
217 udp_in6.sin6_port = uh->uh_sport;
219 * KAME note: usually we drop udphdr from mbuf here.
220 * We need udphdr for IPsec processing so we do that later.
224 * Locate pcb(s) for datagram.
225 * (Algorithm copied from raw_intr().)
228 LIST_FOREACH(in6p, &udb, inp_list) {
229 if ((in6p->inp_vflag & INP_IPV6) == NULL)
231 if (in6p->in6p_lport != uh->uh_dport)
233 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
234 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
236 !in6_mcmatch(in6p, &ip6->ip6_dst,
240 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
241 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
243 in6p->in6p_fport != uh->uh_sport)
252 * Check AH/ESP integrity.
255 ipsec6_in_reject_so(m, last->inp_socket)) {
256 ipsec6stat.in_polvio++;
257 /* do not inject data into pcb */
260 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
263 * m_copy(m, offset, ...) above.
264 * sbappendaddr() expects M_PKTHDR,
265 * and m_copy() will copy M_PKTHDR
266 * only if offset is 0.
268 if (last->in6p_flags & IN6P_CONTROLOPTS
269 || last->in6p_socket->so_options & SO_TIMESTAMP)
270 ip6_savecontrol(last, &opts,
272 m_adj(n, off + sizeof(struct udphdr));
273 if (sbappendaddr(&last->in6p_socket->so_rcv,
274 (struct sockaddr *)&udp_in6,
279 udpstat.udps_fullsock++;
281 sorwakeup(last->in6p_socket);
287 * Don't look for additional matches if this one does
288 * not have either the SO_REUSEPORT or SO_REUSEADDR
289 * socket options set. This heuristic avoids searching
290 * through all pcbs in the common case of a non-shared
291 * port. It assumes that an application will never
292 * clear these options after setting them.
294 if ((last->in6p_socket->so_options &
295 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
301 * No matching pcb found; discard datagram.
302 * (No need to send an ICMP Port Unreachable
303 * for a broadcast or multicast datgram.)
305 udpstat.udps_noport++;
306 udpstat.udps_noportmcast++;
311 * Check AH/ESP integrity.
313 if (last != NULL && ipsec6_in_reject_so(m, last->inp_socket)) {
314 ipsec6stat.in_polvio++;
318 if (last->in6p_flags & IN6P_CONTROLOPTS
319 || last->in6p_socket->so_options & SO_TIMESTAMP)
320 ip6_savecontrol(last, &opts, ip6, m);
322 m_adj(m, off + sizeof(struct udphdr));
323 if (sbappendaddr(&last->in6p_socket->so_rcv,
324 (struct sockaddr *)&udp_in6,
326 udpstat.udps_fullsock++;
329 sorwakeup(last->in6p_socket);
333 * Locate pcb for datagram.
335 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
336 &ip6->ip6_dst, uh->uh_dport, 1,
340 char buf[INET6_ADDRSTRLEN];
342 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
344 "Connection attempt to UDP %s:%d from %s:%d\n",
345 buf, ntohs(uh->uh_dport),
346 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
348 udpstat.udps_noport++;
349 if (m->m_flags & M_MCAST) {
350 printf("UDP6: M_MCAST is set in a unicast packet.\n");
351 udpstat.udps_noportmcast++;
354 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
359 * Check AH/ESP integrity.
361 if (in6p != NULL && ipsec6_in_reject_so(m, in6p->in6p_socket)) {
362 ipsec6stat.in_polvio++;
368 * Construct sockaddr format source address.
369 * Stuff source address and datagram in user buffer.
371 init_sin6(&udp_in6, m); /* general init */
372 udp_in6.sin6_port = uh->uh_sport;
373 if (in6p->in6p_flags & IN6P_CONTROLOPTS
374 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
375 ip6_savecontrol(in6p, &opts, ip6, m);
376 m_adj(m, off + sizeof(struct udphdr));
377 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
378 (struct sockaddr *)&udp_in6,
380 udpstat.udps_fullsock++;
383 sorwakeup(in6p->in6p_socket);
394 udp6_ctlinput(cmd, sa, d)
399 register struct udphdr *uhp;
401 struct sockaddr_in6 sa6;
406 if (sa->sa_family != AF_INET6 ||
407 sa->sa_len != sizeof(struct sockaddr_in6))
410 if (!PRC_IS_REDIRECT(cmd) &&
411 ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
414 /* if the parameter is from icmp6, decode it. */
416 struct ip6ctlparam *ip6cp = (struct ip6ctlparam *)d;
418 ip6 = ip6cp->ip6c_ip6;
419 off = ip6cp->ip6c_off;
425 /* translate addresses into internal form */
426 sa6 = *(struct sockaddr_in6 *)sa;
427 if (IN6_IS_ADDR_LINKLOCAL(&sa6.sin6_addr))
428 sa6.sin6_addr.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
432 * XXX: We assume that when IPV6 is non NULL,
433 * M and OFF are valid.
437 /* translate addresses into internal form */
438 memcpy(&s, &ip6->ip6_src, sizeof(s));
439 if (IN6_IS_ADDR_LINKLOCAL(&s))
440 s.s6_addr16[1] = htons(m->m_pkthdr.rcvif->if_index);
442 if (m->m_len < off + sizeof(uh)) {
444 * this should be rare case,
445 * so we compromise on this copy...
447 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
450 uhp = (struct udphdr *)(mtod(m, caddr_t) + off);
451 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6,
453 uhp->uh_sport, cmd, udp_notify);
455 (void) in6_pcbnotify(&udb, (struct sockaddr *)&sa6, 0,
456 &zeroin6_addr, 0, cmd, udp_notify);
460 udp6_getcred SYSCTL_HANDLER_ARGS
462 struct sockaddr_in6 addrs[2];
466 error = suser(req->p);
469 error = SYSCTL_IN(req, addrs, sizeof(addrs));
473 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
475 &addrs[0].sin6_addr, addrs[0].sin6_port,
477 if (!inp || !inp->inp_socket || !inp->inp_socket->so_cred) {
481 error = SYSCTL_OUT(req, inp->inp_socket->so_cred,
482 sizeof(struct ucred));
489 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
491 udp6_getcred, "S,ucred", "Get the ucred of a UDP6 connection");
494 udp6_output(in6p, m, addr6, control, p)
495 register struct inpcb *in6p;
496 register struct mbuf *m;
497 struct sockaddr *addr6;
498 struct mbuf *control;
501 register int ulen = m->m_pkthdr.len;
502 int plen = sizeof(struct udphdr) + ulen;
505 struct in6_addr laddr6;
506 int s = 0, error = 0;
507 struct ip6_pktopts opt, *stickyopt = in6p->in6p_outputopts;
510 if ((error = ip6_setpktoptions(control, &opt, suser(p))) != 0)
512 in6p->in6p_outputopts = &opt;
516 laddr6 = in6p->in6p_laddr;
517 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
522 * Must block input while temporarily connected.
526 * XXX: the user might want to overwrite the local address
527 * via an ancillary data.
529 bzero(&in6p->in6p_laddr, sizeof(struct in6_addr));
530 error = in6_pcbconnect(in6p, addr6, p);
536 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
542 * Calculate data length and get a mbuf
543 * for UDP and IP6 headers.
545 M_PREPEND(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr),
555 * Stuff checksum and output datagram.
557 ip6 = mtod(m, struct ip6_hdr *);
558 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
559 ip6->ip6_vfc = IPV6_VERSION;
560 /* ip6_plen will be filled in ip6_output. */
561 ip6->ip6_nxt = IPPROTO_UDP;
562 ip6->ip6_hlim = in6_selecthlim(in6p,
563 in6p->in6p_route.ro_rt ?
564 in6p->in6p_route.ro_rt->rt_ifp :
566 ip6->ip6_src = in6p->in6p_laddr;
567 ip6->ip6_dst = in6p->in6p_faddr;
569 udp6 = (struct udphdr *)(ip6 + 1);
570 udp6->uh_sport = in6p->in6p_lport;
571 udp6->uh_dport = in6p->in6p_fport;
572 udp6->uh_ulen = htons((u_short)plen);
575 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
576 sizeof(struct ip6_hdr), plen)) == 0) {
577 udp6->uh_sum = 0xffff;
580 udpstat.udps_opackets++;
583 m->m_pkthdr.rcvif = (struct ifnet *)in6p->in6p_socket;
585 error = ip6_output(m, in6p->in6p_outputopts, &in6p->in6p_route,
586 0, in6p->in6p_moptions, NULL);
589 in6_pcbdisconnect(in6p);
590 in6p->in6p_laddr = laddr6;
600 in6p->in6p_outputopts = stickyopt;
607 udp6_abort(struct socket *so)
614 return EINVAL; /* ??? possible? panic instead? */
615 soisdisconnected(so);
623 udp6_attach(struct socket *so, int proto, struct proc *p)
632 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
633 error = soreserve(so, udp_sendspace, udp_recvspace);
638 error = in_pcballoc(so, &udbinfo, p);
642 inp = (struct inpcb *)so->so_pcb;
643 inp->inp_vflag |= INP_IPV6;
644 inp->in6p_hops = -1; /* use kernel default */
645 inp->in6p_cksum = -1; /* just to be sure */
647 error = ipsec_init_policy(so, &inp->in6p_sp);
657 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
666 inp->inp_vflag &= ~INP_IPV4;
667 inp->inp_vflag |= INP_IPV6;
668 if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_BINDV6ONLY) == NULL) {
669 struct sockaddr_in6 *sin6_p;
671 sin6_p = (struct sockaddr_in6 *)nam;
673 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
674 inp->inp_vflag |= INP_IPV4;
675 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
676 struct sockaddr_in sin;
678 in6_sin6_2_sin(&sin, sin6_p);
679 inp->inp_vflag |= INP_IPV4;
680 inp->inp_vflag &= ~INP_IPV6;
682 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
689 error = in6_pcbbind(inp, nam, p);
695 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
704 if (ip6_mapped_addr_on) {
705 struct sockaddr_in6 *sin6_p;
707 sin6_p = (struct sockaddr_in6 *)nam;
708 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
709 struct sockaddr_in sin;
711 if (inp->inp_faddr.s_addr != INADDR_ANY)
713 in6_sin6_2_sin(&sin, sin6_p);
715 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
718 inp->inp_vflag |= INP_IPV4;
719 inp->inp_vflag &= ~INP_IPV6;
726 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
729 error = in6_pcbconnect(inp, nam, p);
730 if (ip6_auto_flowlabel) {
731 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
732 inp->in6p_flowinfo |=
733 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
737 if (ip6_mapped_addr_on) { /* should be non mapped addr */
738 inp->inp_vflag &= ~INP_IPV4;
739 inp->inp_vflag |= INP_IPV6;
747 udp6_detach(struct socket *so)
762 udp6_disconnect(struct socket *so)
771 if (inp->inp_vflag & INP_IPV4) {
772 struct pr_usrreqs *pru;
774 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
775 return ((*pru->pru_disconnect)(so));
778 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
782 in6_pcbdisconnect(inp);
783 inp->in6p_laddr = in6addr_any;
785 so->so_state &= ~SS_ISCONNECTED; /* XXX */
790 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
791 struct mbuf *control, struct proc *p)
801 if (ip6_mapped_addr_on) {
803 struct sockaddr_in6 *sin6 = 0;
806 hasv4addr = (inp->inp_vflag & INP_IPV4);
808 sin6 = (struct sockaddr_in6 *)addr;
809 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
813 struct pr_usrreqs *pru;
817 in6_sin6_2_sin_in_sock(addr);
818 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
819 error = ((*pru->pru_send)(so, flags, m, addr, control,
821 /* addr will just be freed in sendit(). */
826 return udp6_output(inp, m, addr, control, p);
829 struct pr_usrreqs udp6_usrreqs = {
830 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
831 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
832 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
833 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
834 in6_mapped_sockaddr, sosend, soreceive, sopoll