1 //==========================================================================
3 // src/sys/netinet6/udp6_usrreq.c
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
8 // -------------------------------------------
10 // Portions of this software may have been derived from OpenBSD,
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
17 // -------------------------------------------
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
22 /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $ */
23 /* $KAME: udp6_usrreq.c,v 1.43 2001/11/12 05:04:16 jinmei Exp $ */
26 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * Copyright (c) 1982, 1986, 1989, 1993
56 * The Regents of the University of California. All rights reserved.
58 * Redistribution and use in source and binary forms, with or without
59 * modification, are permitted provided that the following conditions
61 * 1. Redistributions of source code must retain the above copyright
62 * notice, this list of conditions and the following disclaimer.
63 * 2. Redistributions in binary form must reproduce the above copyright
64 * notice, this list of conditions and the following disclaimer in the
65 * documentation and/or other materials provided with the distribution.
66 * 3. All advertising materials mentioning features or use of this software
67 * must display the following acknowledgement:
68 * This product includes software developed by the University of
69 * California, Berkeley and its contributors.
70 * 4. Neither the name of the University nor the names of its contributors
71 * may be used to endorse or promote products derived from this software
72 * without specific prior written permission.
74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
86 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93
89 #include <sys/param.h>
91 #include <sys/protosw.h>
92 #include <sys/socket.h>
93 #include <sys/socketvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/errno.h>
98 #include <net/route.h>
99 #include <net/if_types.h>
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/ip.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
107 #include <netinet/udp.h>
108 #include <netinet/udp_var.h>
109 #include <netinet/ip6.h>
110 #include <netinet6/ip6_var.h>
111 #include <netinet6/in6_pcb.h>
112 #include <netinet/icmp6.h>
113 #include <netinet6/udp6_var.h>
114 #include <netinet6/ip6protosw.h>
117 #include <netinet6/ipsec.h>
121 * UDP protocol inplementation.
122 * Per RFC 768, August, 1980.
125 extern struct protosw inetsw[];
126 static int udp6_detach __P((struct socket *so));
129 udp6_input(mp, offp, proto)
133 struct mbuf *m = *mp;
134 register struct ip6_hdr *ip6;
135 register struct udphdr *uh;
136 register struct inpcb *in6p;
137 struct ip6_recvpktopts opts;
140 struct sockaddr_in6 udp_in6;
142 bzero(&opts, sizeof(opts));
144 ip6 = mtod(m, struct ip6_hdr *);
146 #if defined(NFAITH) && 0 < NFAITH
147 if (faithprefix(&ip6->ip6_dst)) {
148 /* XXX send icmp6 host/port unreach? */
154 #ifndef PULLDOWN_TEST
155 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
156 ip6 = mtod(m, struct ip6_hdr *);
157 uh = (struct udphdr *)((caddr_t)ip6 + off);
159 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
164 udpstat.udps_ipackets++;
166 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
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: traditionally we dropped udpiphdr 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) == 0)
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,
238 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
239 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
241 in6p->in6p_fport != uh->uh_sport)
250 * Check AH/ESP integrity.
252 if (ipsec6_in_reject_so(m, last->inp_socket))
253 ipsec6stat.in_polvio++;
254 /* do not inject data into pcb */
257 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
260 * m_copy(m, offset, ...) above.
261 * sbappendaddr() expects M_PKTHDR,
262 * and m_copy() will copy M_PKTHDR
263 * only if offset is 0.
265 if (last->in6p_flags & IN6P_CONTROLOPTS
266 || last->in6p_socket->so_options & SO_TIMESTAMP)
267 ip6_savecontrol(last, ip6, n,
270 m_adj(n, off + sizeof(struct udphdr));
271 if (sbappendaddr(&last->in6p_socket->so_rcv,
272 (struct sockaddr *)&udp_in6,
273 n, opts.head) == 0) {
277 udpstat.udps_fullsock++;
279 sorwakeup(last->in6p_socket);
280 bzero(&opts, sizeof(opts));
285 * Don't look for additional matches if this one does
286 * not have either the SO_REUSEPORT or SO_REUSEADDR
287 * socket options set. This heuristic avoids searching
288 * through all pcbs in the common case of a non-shared
289 * port. It assumes that an application will never
290 * clear these options after setting them.
292 if ((last->in6p_socket->so_options &
293 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
299 * No matching pcb found; discard datagram.
300 * (No need to send an ICMP Port Unreachable
301 * for a broadcast or multicast datgram.)
303 udpstat.udps_noport++;
304 udpstat.udps_noportmcast++;
309 * Check AH/ESP integrity.
311 if (ipsec6_in_reject_so(m, last->inp_socket)) {
312 ipsec6stat.in_polvio++;
316 if (last->in6p_flags & IN6P_CONTROLOPTS
317 || last->in6p_socket->so_options & SO_TIMESTAMP)
318 ip6_savecontrol(last, ip6, m, &opts, NULL);
320 m_adj(m, off + sizeof(struct udphdr));
321 if (sbappendaddr(&last->in6p_socket->so_rcv,
322 (struct sockaddr *)&udp_in6,
323 m, opts.head) == 0) {
324 udpstat.udps_fullsock++;
327 sorwakeup(last->in6p_socket);
331 * Locate pcb for datagram.
333 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
334 &ip6->ip6_dst, uh->uh_dport, 1,
338 char buf[INET6_ADDRSTRLEN];
340 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
342 "Connection attempt to UDP %s:%d from %s:%d\n",
343 buf, ntohs(uh->uh_dport),
344 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
346 udpstat.udps_noport++;
347 if (m->m_flags & M_MCAST) {
348 printf("UDP6: M_MCAST is set in a unicast packet.\n");
349 udpstat.udps_noportmcast++;
352 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
357 * Check AH/ESP integrity.
359 if (ipsec6_in_reject_so(m, in6p->in6p_socket)) {
360 ipsec6stat.in_polvio++;
366 * Construct sockaddr format source address.
367 * Stuff source address and datagram in user buffer.
369 init_sin6(&udp_in6, m); /* general init */
370 udp_in6.sin6_port = uh->uh_sport;
371 if (in6p->in6p_flags & IN6P_CONTROLOPTS
372 || in6p->in6p_socket->so_options & SO_TIMESTAMP)
373 ip6_savecontrol(in6p, ip6, m, &opts, NULL);
374 m_adj(m, off + sizeof(struct udphdr));
375 if (sbappendaddr(&in6p->in6p_socket->so_rcv,
376 (struct sockaddr *)&udp_in6,
377 m, opts.head) == 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 void (*notify) __P((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(&udb, sa, uh.uh_dport,
452 (struct sockaddr *)ip6cp->ip6c_src,
453 uh.uh_sport, cmd, cmdarg, notify);
455 (void) in6_pcbnotify(&udb, sa, 0, (struct sockaddr *)&sa6_src,
456 0, cmd, cmdarg, notify);
460 udp6_abort(struct socket *so)
467 return EINVAL; /* ??? possible? panic instead? */
468 soisdisconnected(so);
476 udp6_attach(struct socket *so, int proto, struct proc *p)
485 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
486 error = soreserve(so, udp_sendspace, udp_recvspace);
491 error = in_pcballoc(so, &udbinfo, p);
495 inp = (struct inpcb *)so->so_pcb;
496 inp->inp_vflag |= INP_IPV6;
497 inp->in6p_hops = -1; /* use kernel default */
498 inp->in6p_cksum = -1; /* just to be sure */
501 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
502 * because the socket may be bound to an IPv6 wildcard address,
503 * which may match an IPv4-mapped IPv6 address.
505 inp->inp_ip_ttl = ip_defttl;
510 udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
519 inp->inp_vflag &= ~INP_IPV4;
520 inp->inp_vflag |= INP_IPV6;
521 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
522 struct sockaddr_in6 *sin6_p;
524 sin6_p = (struct sockaddr_in6 *)nam;
526 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
527 inp->inp_vflag |= INP_IPV4;
528 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
529 struct sockaddr_in sin;
531 in6_sin6_2_sin(&sin, sin6_p);
532 inp->inp_vflag |= INP_IPV4;
533 inp->inp_vflag &= ~INP_IPV6;
535 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
542 error = in6_pcbbind(inp, nam, p);
548 udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
557 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
558 struct sockaddr_in6 *sin6_p;
560 sin6_p = (struct sockaddr_in6 *)nam;
561 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
562 struct sockaddr_in sin;
564 if (inp->inp_faddr.s_addr != INADDR_ANY)
566 in6_sin6_2_sin(&sin, sin6_p);
568 error = in_pcbconnect(inp, (struct sockaddr *)&sin, p);
571 inp->inp_vflag |= INP_IPV4;
572 inp->inp_vflag &= ~INP_IPV6;
578 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
581 error = in6_pcbconnect(inp, nam, p);
584 if (ip6_mapped_addr_on) { /* should be non mapped addr */
585 inp->inp_vflag &= ~INP_IPV4;
586 inp->inp_vflag |= INP_IPV6;
594 udp6_detach(struct socket *so)
609 udp6_disconnect(struct socket *so)
619 if (inp->inp_vflag & INP_IPV4) {
620 struct pr_usrreqs *pru;
622 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
623 return ((*pru->pru_disconnect)(so));
627 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
631 in6_pcbdisconnect(inp);
632 inp->in6p_laddr = in6addr_any;
634 so->so_state &= ~SS_ISCONNECTED; /* XXX */
639 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
640 struct mbuf *control, struct proc *p)
652 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
656 if (addr->sa_family != AF_INET6) {
657 error = EAFNOSUPPORT;
663 if (ip6_mapped_addr_on) {
665 struct sockaddr_in6 *sin6 = 0;
668 hasv4addr = (inp->inp_vflag & INP_IPV4);
670 sin6 = (struct sockaddr_in6 *)addr;
671 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
675 struct pr_usrreqs *pru;
677 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
679 * since a user of this socket set the
680 * IPV6_V6ONLY flag, we discard this
681 * datagram destined to a v4 addr.
685 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
686 && !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
688 * when remote addr is IPv4-mapped
689 * address, local addr should not be
690 * an IPv6 address; since you cannot
691 * determine how to map IPv6 source
697 in6_sin6_2_sin_in_sock(addr);
698 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
699 error = ((*pru->pru_send)(so, flags, m, addr, control,
701 /* addr will just be freed in sendit(). */
707 return udp6_output(inp, m, addr, control, p);
714 struct pr_usrreqs udp6_usrreqs = {
715 udp6_abort, pru_accept_notsupp, udp6_attach, udp6_bind, udp6_connect,
716 pru_connect2_notsupp, in6_control, udp6_detach, udp6_disconnect,
717 pru_listen_notsupp, in6_mapped_peeraddr, pru_rcvd_notsupp,
718 pru_rcvoob_notsupp, udp6_send, pru_sense_null, udp_shutdown,
719 in6_mapped_sockaddr, sosend, soreceive, sopoll