2 /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
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_var.h 8.1 (Berkeley) 6/10/93
65 #include "opt_inet6.h"
66 #include "opt_ipsec.h"
68 #include <sys/param.h>
69 #include <sys/errno.h>
70 #include <sys/kernel.h>
74 #include <sys/protosw.h>
75 #include <sys/signalvar.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/syslog.h>
82 #include <sys/systm.h>
85 #include <net/if_types.h>
86 #include <net/route.h>
88 #include <netinet/in.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/ip6.h>
94 #include <netinet/icmp6.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/udp.h>
97 #include <netinet/udp_var.h>
98 #include <netinet6/ip6protosw.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/in6_pcb.h>
101 #include <netinet6/udp6_var.h>
102 #include <netinet6/scope6_var.h>
105 #include <netinet6/ipsec.h>
106 #include <netinet6/ipsec6.h>
110 #include <netipsec/ipsec.h>
111 #include <netipsec/ipsec6.h>
112 #endif /* FAST_IPSEC */
115 * UDP protocol inplementation.
116 * Per RFC 768, August, 1980.
119 extern struct protosw inetsw[];
120 static void udp6_detach __P((struct socket *so));
123 udp6_append(struct inpcb *in6p, struct mbuf *n, int off,
124 struct sockaddr_in6 *fromsa)
129 /* XXXRW: Not yet: INP_LOCK_ASSERT(in6p); */
131 #if defined(IPSEC) || defined(FAST_IPSEC)
133 * Check AH/ESP integrity.
135 if (ipsec6_in_reject(n, in6p)) {
138 ipsec6stat.in_polvio++;
142 #endif /*IPSEC || FAST_IPSEC*/
145 if (in6p->in6p_flags & IN6P_CONTROLOPTS ||
146 in6p->inp_socket->so_options & SO_TIMESTAMP)
147 ip6_savecontrol(in6p, n, &opts);
148 m_adj(n, off + sizeof(struct udphdr));
150 so = in6p->inp_socket;
151 SOCKBUF_LOCK(&so->so_rcv);
152 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
154 SOCKBUF_UNLOCK(&so->so_rcv);
158 udpstat.udps_fullsock++;
160 sorwakeup_locked(so);
164 udp6_input(mp, offp, proto)
168 struct mbuf *m = *mp;
169 register struct ip6_hdr *ip6;
170 register struct udphdr *uh;
171 register struct inpcb *in6p;
174 struct sockaddr_in6 fromsa;
176 ip6 = mtod(m, struct ip6_hdr *);
178 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
179 /* XXX send icmp6 host/port unreach? */
184 #ifndef PULLDOWN_TEST
185 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
186 ip6 = mtod(m, struct ip6_hdr *);
187 uh = (struct udphdr *)((caddr_t)ip6 + off);
189 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
194 udpstat.udps_ipackets++;
196 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
197 ulen = ntohs((u_short)uh->uh_ulen);
200 udpstat.udps_badlen++;
205 * Checksum extended UDP header and data.
207 if (uh->uh_sum == 0) {
208 udpstat.udps_nosum++;
211 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
212 udpstat.udps_badsum++;
217 * Construct sockaddr format source address.
219 init_sin6(&fromsa, m);
220 fromsa.sin6_port = uh->uh_sport;
222 INP_INFO_RLOCK(&udbinfo);
223 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
227 * Deliver a multicast datagram to all sockets
228 * for which the local and remote addresses and ports match
229 * those of the incoming datagram. This allows more than
230 * one process to receive multicasts on the same port.
231 * (This really ought to be done for unicast datagrams as
232 * well, but that would cause problems with existing
233 * applications that open both address-specific sockets and
234 * a wildcard socket listening to the same port -- they would
235 * end up receiving duplicates of every unicast datagram.
236 * Those applications open the multiple sockets to overcome an
237 * inadequacy of the UDP socket interface, but for backwards
238 * compatibility we avoid the problem here rather than
239 * fixing the interface. Maybe 4.5BSD will remedy this?)
243 * In a case that laddr should be set to the link-local
244 * address (this happens in RIPng), the multicast address
245 * specified in the received packet does not match with
246 * laddr. To cure this situation, the matching is relaxed
247 * if the receiving interface is the same as one specified
248 * in the socket and if the destination multicast address
249 * matches one of the multicast groups specified in the socket.
253 * KAME note: traditionally we dropped udpiphdr from mbuf here.
254 * We need udphdr for IPsec processing so we do that later.
258 * Locate pcb(s) for datagram.
259 * (Algorithm copied from raw_intr().)
261 * XXXRW: The individual inpcbs need to be locked in the
262 * style of udp_input().
265 LIST_FOREACH(in6p, &udb, inp_list) {
266 if ((in6p->inp_vflag & INP_IPV6) == 0)
268 if (in6p->in6p_lport != uh->uh_dport)
270 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
271 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
275 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
276 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
278 in6p->in6p_fport != uh->uh_sport)
285 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
287 udp6_append(in6p, n, off, &fromsa);
293 * Don't look for additional matches if this one does
294 * not have either the SO_REUSEPORT or SO_REUSEADDR
295 * socket options set. This heuristic avoids searching
296 * through all pcbs in the common case of a non-shared
297 * port. It assumes that an application will never
298 * clear these options after setting them.
300 if ((last->in6p_socket->so_options &
301 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
307 * No matching pcb found; discard datagram.
308 * (No need to send an ICMP Port Unreachable
309 * for a broadcast or multicast datgram.)
311 udpstat.udps_noport++;
312 udpstat.udps_noportmcast++;
316 udp6_append(last, m, off, &fromsa);
318 INP_INFO_RUNLOCK(&udbinfo);
322 * Locate pcb for datagram.
324 in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
325 &ip6->ip6_dst, uh->uh_dport, 1,
329 char buf[INET6_ADDRSTRLEN];
331 strcpy(buf, ip6_sprintf(&ip6->ip6_dst));
333 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
334 buf, ntohs(uh->uh_dport),
335 ip6_sprintf(&ip6->ip6_src), ntohs(uh->uh_sport));
337 udpstat.udps_noport++;
338 if (m->m_flags & M_MCAST) {
339 printf("UDP6: M_MCAST is set in a unicast packet.\n");
340 udpstat.udps_noportmcast++;
343 INP_INFO_RUNLOCK(&udbinfo);
344 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
348 udp6_append(in6p, m, off, &fromsa);
350 INP_INFO_RUNLOCK(&udbinfo);
353 INP_INFO_RUNLOCK(&udbinfo);
361 udp6_ctlinput(cmd, sa, d)
370 struct ip6ctlparam *ip6cp = NULL;
371 const struct sockaddr_in6 *sa6_src = NULL;
373 struct inpcb *(*notify) __P((struct inpcb *, int)) = udp_notify;
374 struct udp_portonly {
379 if (sa->sa_family != AF_INET6 ||
380 sa->sa_len != sizeof(struct sockaddr_in6))
383 if ((unsigned)cmd >= PRC_NCMDS)
385 if (PRC_IS_REDIRECT(cmd))
386 notify = in6_rtchange, d = NULL;
387 else if (cmd == PRC_HOSTDEAD)
389 else if (inet6ctlerrmap[cmd] == 0)
392 /* if the parameter is from icmp6, decode it. */
394 ip6cp = (struct ip6ctlparam *)d;
396 ip6 = ip6cp->ip6c_ip6;
397 off = ip6cp->ip6c_off;
398 cmdarg = ip6cp->ip6c_cmdarg;
399 sa6_src = ip6cp->ip6c_src;
409 * XXX: We assume that when IPV6 is non NULL,
410 * M and OFF are valid.
413 /* check if we can safely examine src and dst ports */
414 if (m->m_pkthdr.len < off + sizeof(*uhp))
417 bzero(&uh, sizeof(uh));
418 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
420 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
421 (struct sockaddr *)ip6cp->ip6c_src,
422 uh.uh_sport, cmd, cmdarg, notify);
424 (void) in6_pcbnotify(&udbinfo, sa, 0,
425 (const struct sockaddr *)sa6_src,
426 0, cmd, cmdarg, notify);
430 udp6_getcred(SYSCTL_HANDLER_ARGS)
433 struct sockaddr_in6 addrs[2];
437 error = suser(req->td);
441 if (req->newlen != sizeof(addrs))
443 if (req->oldlen != sizeof(struct xucred))
445 error = SYSCTL_IN(req, addrs, sizeof(addrs));
448 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
449 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
452 INP_INFO_RLOCK(&udbinfo);
453 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
455 &addrs[0].sin6_addr, addrs[0].sin6_port,
458 INP_INFO_RUNLOCK(&udbinfo);
462 KASSERT(inp->inp_socket != NULL,
463 ("udp6_getcred: inp_socket == NULL"));
464 cru2x(inp->inp_socket->so_cred, &xuc);
466 INP_INFO_RUNLOCK(&udbinfo);
467 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
471 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
473 udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
476 udp6_abort(struct socket *so)
481 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
483 INP_INFO_WLOCK(&udbinfo);
485 soisdisconnected(so);
488 INP_INFO_WUNLOCK(&udbinfo);
492 udp6_attach(struct socket *so, int proto, struct thread *td)
498 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
500 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
501 error = soreserve(so, udp_sendspace, udp_recvspace);
505 INP_INFO_WLOCK(&udbinfo);
506 error = in_pcballoc(so, &udbinfo, "udp6inp");
508 INP_INFO_WUNLOCK(&udbinfo);
511 inp = (struct inpcb *)so->so_pcb;
513 INP_INFO_WUNLOCK(&udbinfo);
514 inp->inp_vflag |= INP_IPV6;
515 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
516 inp->inp_vflag |= INP_IPV4;
517 inp->in6p_hops = -1; /* use kernel default */
518 inp->in6p_cksum = -1; /* just to be sure */
521 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
522 * because the socket may be bound to an IPv6 wildcard address,
523 * which may match an IPv4-mapped IPv6 address.
525 inp->inp_ip_ttl = ip_defttl;
531 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
537 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
539 INP_INFO_WLOCK(&udbinfo);
541 inp->inp_vflag &= ~INP_IPV4;
542 inp->inp_vflag |= INP_IPV6;
543 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
544 struct sockaddr_in6 *sin6_p;
546 sin6_p = (struct sockaddr_in6 *)nam;
548 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
549 inp->inp_vflag |= INP_IPV4;
550 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
551 struct sockaddr_in sin;
553 in6_sin6_2_sin(&sin, sin6_p);
554 inp->inp_vflag |= INP_IPV4;
555 inp->inp_vflag &= ~INP_IPV6;
556 error = in_pcbbind(inp, (struct sockaddr *)&sin,
562 error = in6_pcbbind(inp, nam, td->td_ucred);
565 INP_INFO_WUNLOCK(&udbinfo);
570 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
576 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
578 INP_INFO_WLOCK(&udbinfo);
580 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
581 struct sockaddr_in6 *sin6_p;
583 sin6_p = (struct sockaddr_in6 *)nam;
584 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
585 struct sockaddr_in sin;
587 if (inp->inp_faddr.s_addr != INADDR_ANY) {
591 in6_sin6_2_sin(&sin, sin6_p);
592 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
595 inp->inp_vflag |= INP_IPV4;
596 inp->inp_vflag &= ~INP_IPV6;
602 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
606 error = in6_pcbconnect(inp, nam, td->td_ucred);
608 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
609 /* should be non mapped addr */
610 inp->inp_vflag &= ~INP_IPV4;
611 inp->inp_vflag |= INP_IPV6;
617 INP_INFO_WUNLOCK(&udbinfo);
622 udp6_detach(struct socket *so)
627 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
629 INP_INFO_WLOCK(&udbinfo);
633 INP_INFO_WUNLOCK(&udbinfo);
637 udp6_disconnect(struct socket *so)
643 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
645 INP_INFO_WLOCK(&udbinfo);
649 if (inp->inp_vflag & INP_IPV4) {
650 struct pr_usrreqs *pru;
652 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
653 error = (*pru->pru_disconnect)(so);
658 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
663 in6_pcbdisconnect(inp);
664 inp->in6p_laddr = in6addr_any;
665 /* XXXRW: so_state locking? */
666 so->so_state &= ~SS_ISCONNECTED; /* XXX */
669 INP_INFO_WUNLOCK(&udbinfo);
674 udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
675 struct mbuf *control, struct thread *td)
681 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
683 INP_INFO_WLOCK(&udbinfo);
686 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
690 if (addr->sa_family != AF_INET6) {
691 error = EAFNOSUPPORT;
697 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
699 struct sockaddr_in6 *sin6 = 0;
702 hasv4addr = (inp->inp_vflag & INP_IPV4);
704 sin6 = (struct sockaddr_in6 *)addr;
705 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
709 struct pr_usrreqs *pru;
711 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
712 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
714 * when remote addr is IPv4-mapped
715 * address, local addr should not be
716 * an IPv6 address; since you cannot
717 * determine how to map IPv6 source
724 in6_sin6_2_sin_in_sock(addr);
725 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
726 error = ((*pru->pru_send)(so, flags, m, addr, control,
728 /* addr will just be freed in sendit(). */
734 error = udp6_output(inp, m, addr, control, td);
737 INP_INFO_WUNLOCK(&udbinfo);
742 INP_INFO_WUNLOCK(&udbinfo);
747 struct pr_usrreqs udp6_usrreqs = {
748 .pru_abort = udp6_abort,
749 .pru_attach = udp6_attach,
750 .pru_bind = udp6_bind,
751 .pru_connect = udp6_connect,
752 .pru_control = in6_control,
753 .pru_detach = udp6_detach,
754 .pru_disconnect = udp6_disconnect,
755 .pru_peeraddr = in6_mapped_peeraddr,
756 .pru_send = udp6_send,
757 .pru_shutdown = udp_shutdown,
758 .pru_sockaddr = in6_mapped_sockaddr,
759 .pru_sosetlabel = in_pcbsosetlabel