]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/udp6_usrreq.c
pcb: Move an assignment into in_pcbdisconnect()
[FreeBSD/FreeBSD.git] / sys / netinet6 / udp6_usrreq.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * Copyright (c) 2010-2011 Juniper Networks, Inc.
6  * Copyright (c) 2014 Kevin Lo
7  * All rights reserved.
8  *
9  * Portions of this software were developed by Robert N. M. Watson under
10  * contract to Juniper Networks, Inc.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the project nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *      $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
37  *      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
38  */
39
40 /*-
41  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42  *      The Regents of the University of California.
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
70  */
71
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
74
75 #include "opt_inet.h"
76 #include "opt_inet6.h"
77 #include "opt_ipsec.h"
78 #include "opt_route.h"
79 #include "opt_rss.h"
80
81 #include <sys/param.h>
82 #include <sys/jail.h>
83 #include <sys/kernel.h>
84 #include <sys/lock.h>
85 #include <sys/mbuf.h>
86 #include <sys/priv.h>
87 #include <sys/proc.h>
88 #include <sys/protosw.h>
89 #include <sys/sdt.h>
90 #include <sys/signalvar.h>
91 #include <sys/socket.h>
92 #include <sys/socketvar.h>
93 #include <sys/sx.h>
94 #include <sys/sysctl.h>
95 #include <sys/syslog.h>
96 #include <sys/systm.h>
97
98 #include <net/if.h>
99 #include <net/if_var.h>
100 #include <net/if_types.h>
101 #include <net/route.h>
102 #include <net/rss_config.h>
103
104 #include <netinet/in.h>
105 #include <netinet/in_kdtrace.h>
106 #include <netinet/in_pcb.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
110 #include <netinet/ip6.h>
111 #include <netinet/icmp6.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/udp.h>
114 #include <netinet/udp_var.h>
115 #include <netinet/udplite.h>
116
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet6/in6_fib.h>
120 #include <netinet6/in6_pcb.h>
121 #include <netinet6/in6_rss.h>
122 #include <netinet6/udp6_var.h>
123 #include <netinet6/scope6_var.h>
124
125 #include <netipsec/ipsec_support.h>
126
127 #include <security/mac/mac_framework.h>
128
129 VNET_DEFINE(int, zero_checksum_port) = 0;
130 #define V_zero_checksum_port    VNET(zero_checksum_port)
131 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
132     &VNET_NAME(zero_checksum_port), 0,
133     "Zero UDP checksum allowed for traffic to/from this port.");
134 /*
135  * UDP protocol implementation.
136  * Per RFC 768, August, 1980.
137  */
138
139 extern struct protosw   inetsw[];
140 static void             udp6_detach(struct socket *so);
141
142 static int
143 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
144     struct sockaddr_in6 *fromsa)
145 {
146         struct socket *so;
147         struct mbuf *opts = NULL, *tmp_opts;
148         struct udpcb *up;
149
150         INP_LOCK_ASSERT(inp);
151
152         /*
153          * Engage the tunneling protocol.
154          */
155         up = intoudpcb(inp);
156         if (up->u_tun_func != NULL) {
157                 in_pcbref(inp);
158                 INP_RUNLOCK(inp);
159                 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&fromsa[0],
160                     up->u_tun_ctx);
161                 INP_RLOCK(inp);
162                 return (in_pcbrele_rlocked(inp));
163         }
164 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
165         /* Check AH/ESP integrity. */
166         if (IPSEC_ENABLED(ipv6)) {
167                 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
168                         m_freem(n);
169                         return (0);
170                 }
171         }
172 #endif /* IPSEC */
173 #ifdef MAC
174         if (mac_inpcb_check_deliver(inp, n) != 0) {
175                 m_freem(n);
176                 return (0);
177         }
178 #endif
179         opts = NULL;
180         if (inp->inp_flags & INP_CONTROLOPTS ||
181             inp->inp_socket->so_options & SO_TIMESTAMP)
182                 ip6_savecontrol(inp, n, &opts);
183         if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
184                 tmp_opts = sbcreatecontrol((caddr_t)&fromsa[1],
185                         sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR, IPPROTO_IPV6);
186                 if (tmp_opts) {
187                         if (opts) {
188                                 tmp_opts->m_next = opts;
189                                 opts = tmp_opts;
190                         } else
191                                 opts = tmp_opts;
192                 }
193         }
194         m_adj(n, off + sizeof(struct udphdr));
195
196         so = inp->inp_socket;
197         SOCKBUF_LOCK(&so->so_rcv);
198         if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
199             opts) == 0) {
200                 soroverflow_locked(so);
201                 m_freem(n);
202                 if (opts)
203                         m_freem(opts);
204                 UDPSTAT_INC(udps_fullsock);
205         } else
206                 sorwakeup_locked(so);
207         return (0);
208 }
209
210 int
211 udp6_input(struct mbuf **mp, int *offp, int proto)
212 {
213         struct mbuf *m = *mp;
214         struct ifnet *ifp;
215         struct ip6_hdr *ip6;
216         struct udphdr *uh;
217         struct inpcb *inp;
218         struct inpcbinfo *pcbinfo;
219         struct udpcb *up;
220         int off = *offp;
221         int cscov_partial;
222         int plen, ulen;
223         struct sockaddr_in6 fromsa[2];
224         struct m_tag *fwd_tag;
225         uint16_t uh_sum;
226         uint8_t nxt;
227
228         NET_EPOCH_ASSERT();
229
230         ifp = m->m_pkthdr.rcvif;
231
232         if (m->m_len < off + sizeof(struct udphdr)) {
233                 m = m_pullup(m, off + sizeof(struct udphdr));
234                 if (m == NULL) {
235                         IP6STAT_INC(ip6s_exthdrtoolong);
236                         *mp = NULL;
237                         return (IPPROTO_DONE);
238                 }
239         }
240         ip6 = mtod(m, struct ip6_hdr *);
241         uh = (struct udphdr *)((caddr_t)ip6 + off);
242
243         UDPSTAT_INC(udps_ipackets);
244
245         /*
246          * Destination port of 0 is illegal, based on RFC768.
247          */
248         if (uh->uh_dport == 0)
249                 goto badunlocked;
250
251         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
252         ulen = ntohs((u_short)uh->uh_ulen);
253
254         nxt = proto;
255         cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
256         if (nxt == IPPROTO_UDPLITE) {
257                 /* Zero means checksum over the complete packet. */
258                 if (ulen == 0)
259                         ulen = plen;
260                 if (ulen == plen)
261                         cscov_partial = 0;
262                 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
263                         /* XXX: What is the right UDPLite MIB counter? */
264                         goto badunlocked;
265                 }
266                 if (uh->uh_sum == 0) {
267                         /* XXX: What is the right UDPLite MIB counter? */
268                         goto badunlocked;
269                 }
270         } else {
271                 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
272                         UDPSTAT_INC(udps_badlen);
273                         goto badunlocked;
274                 }
275                 if (uh->uh_sum == 0) {
276                         UDPSTAT_INC(udps_nosum);
277                         /*
278                          * dport 0 was rejected earlier so this is OK even if
279                          * zero_checksum_port is 0 (which is its default value).
280                          */
281                         if (ntohs(uh->uh_dport) == V_zero_checksum_port)
282                                 goto skip_checksum;
283                         else
284                                 goto badunlocked;
285                 }
286         }
287
288         if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
289             !cscov_partial) {
290                 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
291                         uh_sum = m->m_pkthdr.csum_data;
292                 else
293                         uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
294                             m->m_pkthdr.csum_data);
295                 uh_sum ^= 0xffff;
296         } else
297                 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
298
299         if (uh_sum != 0) {
300                 UDPSTAT_INC(udps_badsum);
301                 goto badunlocked;
302         }
303
304 skip_checksum:
305         /*
306          * Construct sockaddr format source address.
307          */
308         init_sin6(&fromsa[0], m, 0);
309         fromsa[0].sin6_port = uh->uh_sport;
310         init_sin6(&fromsa[1], m, 1);
311         fromsa[1].sin6_port = uh->uh_dport;
312
313         pcbinfo = udp_get_inpcbinfo(nxt);
314         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
315                 struct inpcb *last;
316                 struct inpcbhead *pcblist;
317                 struct ip6_moptions *imo;
318
319                 /*
320                  * In the event that laddr should be set to the link-local
321                  * address (this happens in RIPng), the multicast address
322                  * specified in the received packet will not match laddr.  To
323                  * handle this situation, matching is relaxed if the
324                  * receiving interface is the same as one specified in the
325                  * socket and if the destination multicast address matches
326                  * one of the multicast groups specified in the socket.
327                  */
328
329                 /*
330                  * KAME note: traditionally we dropped udpiphdr from mbuf
331                  * here.  We need udphdr for IPsec processing so we do that
332                  * later.
333                  */
334                 pcblist = udp_get_pcblist(nxt);
335                 last = NULL;
336                 CK_LIST_FOREACH(inp, pcblist, inp_list) {
337                         if ((inp->inp_vflag & INP_IPV6) == 0)
338                                 continue;
339                         if (inp->inp_lport != uh->uh_dport)
340                                 continue;
341                         if (inp->inp_fport != 0 &&
342                             inp->inp_fport != uh->uh_sport)
343                                 continue;
344                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
345                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
346                                                         &ip6->ip6_dst))
347                                         continue;
348                         }
349                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
350                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
351                                                         &ip6->ip6_src) ||
352                                     inp->inp_fport != uh->uh_sport)
353                                         continue;
354                         }
355
356                         INP_RLOCK(inp);
357
358                         if (__predict_false(inp->inp_flags2 & INP_FREED)) {
359                                 INP_RUNLOCK(inp);
360                                 continue;
361                         }
362
363                         /*
364                          * XXXRW: Because we weren't holding either the inpcb
365                          * or the hash lock when we checked for a match 
366                          * before, we should probably recheck now that the 
367                          * inpcb lock is (supposed to be) held.
368                          */
369
370                         /*
371                          * Handle socket delivery policy for any-source
372                          * and source-specific multicast. [RFC3678]
373                          */
374                         imo = inp->in6p_moptions;
375                         if (imo != NULL) {
376                                 struct sockaddr_in6      mcaddr;
377                                 int                      blocked;
378
379                                 bzero(&mcaddr, sizeof(struct sockaddr_in6));
380                                 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
381                                 mcaddr.sin6_family = AF_INET6;
382                                 mcaddr.sin6_addr = ip6->ip6_dst;
383
384                                 blocked = im6o_mc_filter(imo, ifp,
385                                         (struct sockaddr *)&mcaddr,
386                                         (struct sockaddr *)&fromsa[0]);
387                                 if (blocked != MCAST_PASS) {
388                                         if (blocked == MCAST_NOTGMEMBER)
389                                                 IP6STAT_INC(ip6s_notmember);
390                                         if (blocked == MCAST_NOTSMEMBER ||
391                                             blocked == MCAST_MUTED)
392                                                 UDPSTAT_INC(udps_filtermcast);
393                                         INP_RUNLOCK(inp);
394                                         continue;
395                                 }
396                         }
397
398                         if (last != NULL) {
399                                 struct mbuf *n;
400
401                                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
402                                     NULL) {
403                                         if (nxt == IPPROTO_UDPLITE)
404                                                 UDPLITE_PROBE(receive, NULL,
405                                                     last, ip6, last, uh);
406                                         else
407                                                 UDP_PROBE(receive, NULL, last,
408                                                     ip6, last, uh);
409                                         if (udp6_append(last, n, off,
410                                             fromsa)) {
411                                                 INP_RUNLOCK(inp);
412                                                 goto badunlocked;
413                                         }
414                                 }
415                                 /* Release PCB lock taken on previous pass. */
416                                 INP_RUNLOCK(last);
417                         }
418                         last = inp;
419                         /*
420                          * Don't look for additional matches if this one does
421                          * not have either the SO_REUSEPORT or SO_REUSEADDR
422                          * socket options set.  This heuristic avoids
423                          * searching through all pcbs in the common case of a
424                          * non-shared port.  It assumes that an application
425                          * will never clear these options after setting them.
426                          */
427                         if ((last->inp_socket->so_options &
428                              (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0)
429                                 break;
430                 }
431
432                 if (last == NULL) {
433                         /*
434                          * No matching pcb found; discard datagram.  (No need
435                          * to send an ICMP Port Unreachable for a broadcast
436                          * or multicast datgram.)
437                          */
438                         UDPSTAT_INC(udps_noport);
439                         UDPSTAT_INC(udps_noportmcast);
440                         goto badunlocked;
441                 }
442
443                 if (nxt == IPPROTO_UDPLITE)
444                         UDPLITE_PROBE(receive, NULL, last, ip6, last, uh);
445                 else
446                         UDP_PROBE(receive, NULL, last, ip6, last, uh);
447                 if (udp6_append(last, m, off, fromsa) == 0)
448                         INP_RUNLOCK(last);
449                 *mp = NULL;
450                 return (IPPROTO_DONE);
451         }
452         /*
453          * Locate pcb for datagram.
454          */
455
456         /*
457          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
458          */
459         if ((m->m_flags & M_IP6_NEXTHOP) &&
460             (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
461                 struct sockaddr_in6 *next_hop6;
462
463                 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
464
465                 /*
466                  * Transparently forwarded. Pretend to be the destination.
467                  * Already got one like this?
468                  */
469                 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
470                     uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
471                     INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
472                 if (!inp) {
473                         /*
474                          * It's new.  Try to find the ambushing socket.
475                          * Because we've rewritten the destination address,
476                          * any hardware-generated hash is ignored.
477                          */
478                         inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
479                             uh->uh_sport, &next_hop6->sin6_addr,
480                             next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
481                             uh->uh_dport, INPLOOKUP_WILDCARD |
482                             INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
483                 }
484                 /* Remove the tag from the packet. We don't need it anymore. */
485                 m_tag_delete(m, fwd_tag);
486                 m->m_flags &= ~M_IP6_NEXTHOP;
487         } else
488                 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
489                     uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
490                     INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
491                     m->m_pkthdr.rcvif, m);
492         if (inp == NULL) {
493                 if (V_udp_log_in_vain) {
494                         char ip6bufs[INET6_ADDRSTRLEN];
495                         char ip6bufd[INET6_ADDRSTRLEN];
496
497                         log(LOG_INFO,
498                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
499                             ip6_sprintf(ip6bufd, &ip6->ip6_dst),
500                             ntohs(uh->uh_dport),
501                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
502                             ntohs(uh->uh_sport));
503                 }
504                 if (nxt == IPPROTO_UDPLITE)
505                         UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
506                 else
507                         UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
508                 UDPSTAT_INC(udps_noport);
509                 if (m->m_flags & M_MCAST) {
510                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
511                         UDPSTAT_INC(udps_noportmcast);
512                         goto badunlocked;
513                 }
514                 if (V_udp_blackhole)
515                         goto badunlocked;
516                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
517                 *mp = NULL;
518                 return (IPPROTO_DONE);
519         }
520         INP_RLOCK_ASSERT(inp);
521         up = intoudpcb(inp);
522         if (cscov_partial) {
523                 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
524                         INP_RUNLOCK(inp);
525                         m_freem(m);
526                         *mp = NULL;
527                         return (IPPROTO_DONE);
528                 }
529         }
530         if (nxt == IPPROTO_UDPLITE)
531                 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
532         else
533                 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
534         if (udp6_append(inp, m, off, fromsa) == 0)
535                 INP_RUNLOCK(inp);
536         *mp = NULL;
537         return (IPPROTO_DONE);
538
539 badunlocked:
540         m_freem(m);
541         *mp = NULL;
542         return (IPPROTO_DONE);
543 }
544
545 static void
546 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
547     struct inpcbinfo *pcbinfo)
548 {
549         struct udphdr uh;
550         struct ip6_hdr *ip6;
551         struct mbuf *m;
552         int off = 0;
553         struct ip6ctlparam *ip6cp = NULL;
554         const struct sockaddr_in6 *sa6_src = NULL;
555         void *cmdarg;
556         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
557         struct udp_portonly {
558                 u_int16_t uh_sport;
559                 u_int16_t uh_dport;
560         } *uhp;
561
562         if (sa->sa_family != AF_INET6 ||
563             sa->sa_len != sizeof(struct sockaddr_in6))
564                 return;
565
566         if ((unsigned)cmd >= PRC_NCMDS)
567                 return;
568         if (PRC_IS_REDIRECT(cmd))
569                 notify = in6_rtchange, d = NULL;
570         else if (cmd == PRC_HOSTDEAD)
571                 d = NULL;
572         else if (inet6ctlerrmap[cmd] == 0)
573                 return;
574
575         /* if the parameter is from icmp6, decode it. */
576         if (d != NULL) {
577                 ip6cp = (struct ip6ctlparam *)d;
578                 m = ip6cp->ip6c_m;
579                 ip6 = ip6cp->ip6c_ip6;
580                 off = ip6cp->ip6c_off;
581                 cmdarg = ip6cp->ip6c_cmdarg;
582                 sa6_src = ip6cp->ip6c_src;
583         } else {
584                 m = NULL;
585                 ip6 = NULL;
586                 cmdarg = NULL;
587                 sa6_src = &sa6_any;
588         }
589
590         if (ip6) {
591                 /*
592                  * XXX: We assume that when IPV6 is non NULL,
593                  * M and OFF are valid.
594                  */
595
596                 /* Check if we can safely examine src and dst ports. */
597                 if (m->m_pkthdr.len < off + sizeof(*uhp))
598                         return;
599
600                 bzero(&uh, sizeof(uh));
601                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
602
603                 if (!PRC_IS_REDIRECT(cmd)) {
604                         /* Check to see if its tunneled */
605                         struct inpcb *inp;
606                         inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
607                             uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
608                             INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
609                             m->m_pkthdr.rcvif, m);
610                         if (inp != NULL) {
611                                 struct udpcb *up;
612                                 
613                                 up = intoudpcb(inp);
614                                 if (up->u_icmp_func) {
615                                         /* Yes it is. */
616                                         INP_RUNLOCK(inp);
617                                         (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
618                                               d, up->u_tun_ctx);
619                                         return;
620                                 } else {
621                                         /* Can't find it. */
622                                         INP_RUNLOCK(inp);
623                                 }
624                         }
625                 }
626                 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
627                     (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
628                     cmdarg, notify);
629         } else
630                 (void)in6_pcbnotify(pcbinfo, sa, 0,
631                     (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
632 }
633
634 void
635 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
636 {
637
638         return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
639 }
640
641 void
642 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
643 {
644
645         return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
646 }
647
648 static int
649 udp6_getcred(SYSCTL_HANDLER_ARGS)
650 {
651         struct xucred xuc;
652         struct sockaddr_in6 addrs[2];
653         struct epoch_tracker et;
654         struct inpcb *inp;
655         int error;
656
657         error = priv_check(req->td, PRIV_NETINET_GETCRED);
658         if (error)
659                 return (error);
660
661         if (req->newlen != sizeof(addrs))
662                 return (EINVAL);
663         if (req->oldlen != sizeof(struct xucred))
664                 return (EINVAL);
665         error = SYSCTL_IN(req, addrs, sizeof(addrs));
666         if (error)
667                 return (error);
668         if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
669             (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
670                 return (error);
671         }
672         NET_EPOCH_ENTER(et);
673         inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
674             addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
675             INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
676         NET_EPOCH_EXIT(et);
677         if (inp != NULL) {
678                 INP_RLOCK_ASSERT(inp);
679                 if (inp->inp_socket == NULL)
680                         error = ENOENT;
681                 if (error == 0)
682                         error = cr_canseesocket(req->td->td_ucred,
683                             inp->inp_socket);
684                 if (error == 0)
685                         cru2x(inp->inp_cred, &xuc);
686                 INP_RUNLOCK(inp);
687         } else
688                 error = ENOENT;
689         if (error == 0)
690                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
691         return (error);
692 }
693
694 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
695     CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
696     0, 0, udp6_getcred, "S,xucred",
697     "Get the xucred of a UDP6 connection");
698
699 static int
700 udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
701     struct sockaddr *addr6, struct mbuf *control, struct thread *td)
702 {
703         struct inpcb *inp;
704         struct ip6_hdr *ip6;
705         struct udphdr *udp6;
706         struct in6_addr *laddr, *faddr, in6a;
707         struct ip6_pktopts *optp, opt;
708         struct sockaddr_in6 *sin6, tmp;
709         struct epoch_tracker et;
710         int cscov_partial, error, flags, hlen, scope_ambiguous;
711         u_int32_t ulen, plen;
712         uint16_t cscov;
713         u_short fport;
714         uint8_t nxt;
715
716         /* addr6 has been validated in udp6_send(). */
717         sin6 = (struct sockaddr_in6 *)addr6;
718
719         /*
720          * In contrast to IPv4 we do not validate the max. packet length
721          * here due to IPv6 Jumbograms (RFC2675).
722          */
723
724         scope_ambiguous = 0;
725         if (sin6) {
726                 /* Protect *addr6 from overwrites. */
727                 tmp = *sin6;
728                 sin6 = &tmp;
729
730                 /*
731                  * Application should provide a proper zone ID or the use of
732                  * default zone IDs should be enabled.  Unfortunately, some
733                  * applications do not behave as it should, so we need a
734                  * workaround.  Even if an appropriate ID is not determined,
735                  * we'll see if we can determine the outgoing interface.  If we
736                  * can, determine the zone ID based on the interface below.
737                  */
738                 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
739                         scope_ambiguous = 1;
740                 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
741                         if (control)
742                                 m_freem(control);
743                         m_freem(m);
744                         return (error);
745                 }
746         }
747
748         inp = sotoinpcb(so);
749         KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
750         /*
751          * In the following cases we want a write lock on the inp for either
752          * local operations or for possible route cache updates in the IPv6
753          * output path:
754          * - on connected sockets (sin6 is NULL) for route cache updates,
755          * - when we are not bound to an address and source port (it is
756          *   in6_pcbsetport() which will require the write lock).
757          *
758          * We check the inp fields before actually locking the inp, so
759          * here exists a race, and we may WLOCK the inp and end with already
760          * bound one by other thread. This is fine.
761          */
762         if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
763             inp->inp_lport == 0))
764                 INP_WLOCK(inp);
765         else
766                 INP_RLOCK(inp);
767
768         nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
769             IPPROTO_UDP : IPPROTO_UDPLITE;
770
771 #ifdef INET
772         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
773                 int hasv4addr;
774
775                 if (sin6 == NULL)
776                         hasv4addr = (inp->inp_vflag & INP_IPV4);
777                 else
778                         hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
779                             ? 1 : 0;
780                 if (hasv4addr) {
781                         struct pr_usrreqs *pru;
782
783                         /*
784                          * XXXRW: We release UDP-layer locks before calling
785                          * udp_send() in order to avoid recursion.  However,
786                          * this does mean there is a short window where inp's
787                          * fields are unstable.  Could this lead to a
788                          * potential race in which the factors causing us to
789                          * select the UDPv4 output routine are invalidated?
790                          */
791                         INP_UNLOCK(inp);
792                         if (sin6)
793                                 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
794                         pru = inetsw[ip_protox[nxt]].pr_usrreqs;
795                         /* addr will just be freed in sendit(). */
796                         return ((*pru->pru_send)(so, flags_arg | PRUS_IPV6, m,
797                             (struct sockaddr *)sin6, control, td));
798                 }
799         } else
800 #endif
801         if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
802                 /*
803                  * Given this is either an IPv6-only socket or no INET is
804                  * supported we will fail the send if the given destination
805                  * address is a v4mapped address.
806                  *
807                  * XXXGL: do we leak m and control?
808                  */
809                 INP_UNLOCK(inp);
810                 return (EINVAL);
811         }
812
813         NET_EPOCH_ENTER(et);
814         if (control) {
815                 if ((error = ip6_setpktopts(control, &opt,
816                     inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
817                         goto release;
818                 }
819                 optp = &opt;
820         } else
821                 optp = inp->in6p_outputopts;
822
823         if (sin6) {
824                 /*
825                  * Since we saw no essential reason for calling in_pcbconnect,
826                  * we get rid of such kind of logic, and call in6_selectsrc
827                  * and in6_pcbsetport in order to fill in the local address
828                  * and the local port.
829                  */
830                 if (sin6->sin6_port == 0) {
831                         error = EADDRNOTAVAIL;
832                         goto release;
833                 }
834
835                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
836                         /* how about ::ffff:0.0.0.0 case? */
837                         error = EISCONN;
838                         goto release;
839                 }
840
841                 /*
842                  * Given we handle the v4mapped case in the INET block above
843                  * assert here that it must not happen anymore.
844                  */
845                 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
846                     ("%s: sin6(%p)->sin6_addr is v4mapped which we "
847                     "should have handled.", __func__, sin6));
848
849                 /* This only requires read-locking. */
850                 error = in6_selectsrc_socket(sin6, optp, inp,
851                     td->td_ucred, scope_ambiguous, &in6a, NULL);
852                 if (error)
853                         goto release;
854                 laddr = &in6a;
855
856                 if (inp->inp_lport == 0) {
857                         struct inpcbinfo *pcbinfo;
858
859                         INP_WLOCK_ASSERT(inp);
860
861                         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
862                         INP_HASH_WLOCK(pcbinfo);
863                         error = in6_pcbsetport(laddr, inp, td->td_ucred);
864                         INP_HASH_WUNLOCK(pcbinfo);
865                         if (error != 0) {
866                                 /* Undo an address bind that may have occurred. */
867                                 inp->in6p_laddr = in6addr_any;
868                                 goto release;
869                         }
870                 }
871                 faddr = &sin6->sin6_addr;
872                 fport = sin6->sin6_port; /* allow 0 port */
873
874         } else {
875                 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
876                         error = ENOTCONN;
877                         goto release;
878                 }
879                 laddr = &inp->in6p_laddr;
880                 faddr = &inp->in6p_faddr;
881                 fport = inp->inp_fport;
882         }
883
884         ulen = m->m_pkthdr.len;
885         plen = sizeof(struct udphdr) + ulen;
886         hlen = sizeof(struct ip6_hdr);
887
888         /*
889          * Calculate data length and get a mbuf
890          * for UDP and IP6 headers.
891          */
892         M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
893         if (m == NULL) {
894                 error = ENOBUFS;
895                 goto release;
896         }
897
898         /*
899          * Stuff checksum and output datagram.
900          */
901         cscov = cscov_partial = 0;
902         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
903         udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
904         udp6->uh_dport = fport;
905         if (nxt == IPPROTO_UDPLITE) {
906                 struct udpcb *up;
907
908                 up = intoudpcb(inp);
909                 cscov = up->u_txcslen;
910                 if (cscov >= plen)
911                         cscov = 0;
912                 udp6->uh_ulen = htons(cscov);
913                 /*
914                  * For UDP-Lite, checksum coverage length of zero means
915                  * the entire UDPLite packet is covered by the checksum.
916                  */
917                 cscov_partial = (cscov == 0) ? 0 : 1;
918         } else if (plen <= 0xffff)
919                 udp6->uh_ulen = htons((u_short)plen);
920         else
921                 udp6->uh_ulen = 0;
922         udp6->uh_sum = 0;
923
924         ip6 = mtod(m, struct ip6_hdr *);
925         ip6->ip6_flow   = inp->inp_flow & IPV6_FLOWINFO_MASK;
926         ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
927         ip6->ip6_vfc    |= IPV6_VERSION;
928         ip6->ip6_plen   = htons((u_short)plen);
929         ip6->ip6_nxt    = nxt;
930         ip6->ip6_hlim   = in6_selecthlim(inp, NULL);
931         ip6->ip6_src    = *laddr;
932         ip6->ip6_dst    = *faddr;
933
934 #ifdef MAC
935         mac_inpcb_create_mbuf(inp, m);
936 #endif
937
938         if (cscov_partial) {
939                 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
940                     sizeof(struct ip6_hdr), plen, cscov)) == 0)
941                         udp6->uh_sum = 0xffff;
942         } else {
943                 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
944                 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
945                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
946         }
947
948         flags = 0;
949 #if defined(ROUTE_MPATH) || defined(RSS)
950         if (CALC_FLOWID_OUTBOUND_SENDTO) {
951                 uint32_t hash_type, hash_val;
952                 uint8_t pr;
953
954                 pr = inp->inp_socket->so_proto->pr_protocol;
955
956                 hash_val = fib6_calc_packet_hash(laddr, faddr,
957                     inp->inp_lport, fport, pr, &hash_type);
958                 m->m_pkthdr.flowid = hash_val;
959                 M_HASHTYPE_SET(m, hash_type);
960         }
961         /* do not use inp flowid */
962         flags |= IP_NODEFAULTFLOWID;
963 #endif
964
965         UDPSTAT_INC(udps_opackets);
966         if (nxt == IPPROTO_UDPLITE)
967                 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
968         else
969                 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
970         error = ip6_output(m, optp,
971             INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
972             inp->in6p_moptions, NULL, inp);
973         INP_UNLOCK(inp);
974         NET_EPOCH_EXIT(et);
975
976         if (control) {
977                 ip6_clearpktopts(&opt, -1);
978                 m_freem(control);
979         }
980         return (error);
981
982 release:
983         INP_UNLOCK(inp);
984         NET_EPOCH_EXIT(et);
985         if (control) {
986                 ip6_clearpktopts(&opt, -1);
987                 m_freem(control);
988         }
989         m_freem(m);
990
991         return (error);
992 }
993
994 static void
995 udp6_abort(struct socket *so)
996 {
997         struct inpcb *inp;
998         struct inpcbinfo *pcbinfo;
999
1000         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1001         inp = sotoinpcb(so);
1002         KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
1003
1004         INP_WLOCK(inp);
1005 #ifdef INET
1006         if (inp->inp_vflag & INP_IPV4) {
1007                 struct pr_usrreqs *pru;
1008                 uint8_t nxt;
1009
1010                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1011                     IPPROTO_UDP : IPPROTO_UDPLITE;
1012                 INP_WUNLOCK(inp);
1013                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1014                 (*pru->pru_abort)(so);
1015                 return;
1016         }
1017 #endif
1018
1019         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1020                 INP_HASH_WLOCK(pcbinfo);
1021                 in6_pcbdisconnect(inp);
1022                 INP_HASH_WUNLOCK(pcbinfo);
1023                 soisdisconnected(so);
1024         }
1025         INP_WUNLOCK(inp);
1026 }
1027
1028 static int
1029 udp6_attach(struct socket *so, int proto, struct thread *td)
1030 {
1031         struct inpcb *inp;
1032         struct inpcbinfo *pcbinfo;
1033         int error;
1034
1035         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1036         inp = sotoinpcb(so);
1037         KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1038
1039         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1040                 error = soreserve(so, udp_sendspace, udp_recvspace);
1041                 if (error)
1042                         return (error);
1043         }
1044         INP_INFO_WLOCK(pcbinfo);
1045         error = in_pcballoc(so, pcbinfo);
1046         if (error) {
1047                 INP_INFO_WUNLOCK(pcbinfo);
1048                 return (error);
1049         }
1050         inp = (struct inpcb *)so->so_pcb;
1051         inp->inp_vflag |= INP_IPV6;
1052         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1053                 inp->inp_vflag |= INP_IPV4;
1054         inp->in6p_hops = -1;    /* use kernel default */
1055         inp->in6p_cksum = -1;   /* just to be sure */
1056         /*
1057          * XXX: ugly!!
1058          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1059          * because the socket may be bound to an IPv6 wildcard address,
1060          * which may match an IPv4-mapped IPv6 address.
1061          */
1062         inp->inp_ip_ttl = V_ip_defttl;
1063
1064         error = udp_newudpcb(inp);
1065         if (error) {
1066                 in_pcbdetach(inp);
1067                 in_pcbfree(inp);
1068                 INP_INFO_WUNLOCK(pcbinfo);
1069                 return (error);
1070         }
1071         INP_WUNLOCK(inp);
1072         INP_INFO_WUNLOCK(pcbinfo);
1073         return (0);
1074 }
1075
1076 static int
1077 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1078 {
1079         struct inpcb *inp;
1080         struct inpcbinfo *pcbinfo;
1081         int error;
1082         u_char vflagsav;
1083
1084         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1085         inp = sotoinpcb(so);
1086         KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1087
1088         if (nam->sa_family != AF_INET6)
1089                 return (EAFNOSUPPORT);
1090         if (nam->sa_len != sizeof(struct sockaddr_in6))
1091                 return (EINVAL);
1092
1093         INP_WLOCK(inp);
1094         INP_HASH_WLOCK(pcbinfo);
1095         vflagsav = inp->inp_vflag;
1096         inp->inp_vflag &= ~INP_IPV4;
1097         inp->inp_vflag |= INP_IPV6;
1098         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1099                 struct sockaddr_in6 *sin6_p;
1100
1101                 sin6_p = (struct sockaddr_in6 *)nam;
1102
1103                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1104                         inp->inp_vflag |= INP_IPV4;
1105 #ifdef INET
1106                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1107                         struct sockaddr_in sin;
1108
1109                         in6_sin6_2_sin(&sin, sin6_p);
1110                         inp->inp_vflag |= INP_IPV4;
1111                         inp->inp_vflag &= ~INP_IPV6;
1112                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
1113                             td->td_ucred);
1114                         goto out;
1115                 }
1116 #endif
1117         }
1118
1119         error = in6_pcbbind(inp, nam, td->td_ucred);
1120 #ifdef INET
1121 out:
1122 #endif
1123         if (error != 0)
1124                 inp->inp_vflag = vflagsav;
1125         INP_HASH_WUNLOCK(pcbinfo);
1126         INP_WUNLOCK(inp);
1127         return (error);
1128 }
1129
1130 static void
1131 udp6_close(struct socket *so)
1132 {
1133         struct inpcb *inp;
1134         struct inpcbinfo *pcbinfo;
1135
1136         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1137         inp = sotoinpcb(so);
1138         KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1139
1140         INP_WLOCK(inp);
1141 #ifdef INET
1142         if (inp->inp_vflag & INP_IPV4) {
1143                 struct pr_usrreqs *pru;
1144                 uint8_t nxt;
1145
1146                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1147                     IPPROTO_UDP : IPPROTO_UDPLITE;
1148                 INP_WUNLOCK(inp);
1149                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1150                 (*pru->pru_disconnect)(so);
1151                 return;
1152         }
1153 #endif
1154         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1155                 INP_HASH_WLOCK(pcbinfo);
1156                 in6_pcbdisconnect(inp);
1157                 INP_HASH_WUNLOCK(pcbinfo);
1158                 soisdisconnected(so);
1159         }
1160         INP_WUNLOCK(inp);
1161 }
1162
1163 static int
1164 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1165 {
1166 #ifdef INET
1167         struct epoch_tracker et;
1168 #endif
1169         struct inpcb *inp;
1170         struct inpcbinfo *pcbinfo;
1171         struct sockaddr_in6 *sin6;
1172         int error;
1173         u_char vflagsav;
1174
1175         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1176         inp = sotoinpcb(so);
1177         KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1178
1179         sin6 = (struct sockaddr_in6 *)nam;
1180         if (sin6->sin6_family != AF_INET6)
1181                 return (EAFNOSUPPORT);
1182         if (sin6->sin6_len != sizeof(*sin6))
1183                 return (EINVAL);
1184
1185         /*
1186          * XXXRW: Need to clarify locking of v4/v6 flags.
1187          */
1188         INP_WLOCK(inp);
1189 #ifdef INET
1190         if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1191                 struct sockaddr_in sin;
1192
1193                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1194                         error = EINVAL;
1195                         goto out;
1196                 }
1197                 if ((inp->inp_vflag & INP_IPV4) == 0) {
1198                         error = EAFNOSUPPORT;
1199                         goto out;
1200                 }
1201                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1202                         error = EISCONN;
1203                         goto out;
1204                 }
1205                 in6_sin6_2_sin(&sin, sin6);
1206                 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1207                 if (error != 0)
1208                         goto out;
1209                 vflagsav = inp->inp_vflag;
1210                 inp->inp_vflag |= INP_IPV4;
1211                 inp->inp_vflag &= ~INP_IPV6;
1212                 NET_EPOCH_ENTER(et);
1213                 INP_HASH_WLOCK(pcbinfo);
1214                 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1215                     td->td_ucred);
1216                 INP_HASH_WUNLOCK(pcbinfo);
1217                 NET_EPOCH_EXIT(et);
1218                 /*
1219                  * If connect succeeds, mark socket as connected. If
1220                  * connect fails and socket is unbound, reset inp_vflag
1221                  * field.
1222                  */
1223                 if (error == 0)
1224                         soisconnected(so);
1225                 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1226                     inp->inp_lport == 0)
1227                         inp->inp_vflag = vflagsav;
1228                 goto out;
1229         } else {
1230                 if ((inp->inp_vflag & INP_IPV6) == 0) {
1231                         error = EAFNOSUPPORT;
1232                         goto out;
1233                 }
1234         }
1235 #endif
1236         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1237                 error = EISCONN;
1238                 goto out;
1239         }
1240         error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1241         if (error != 0)
1242                 goto out;
1243         vflagsav = inp->inp_vflag;
1244         inp->inp_vflag &= ~INP_IPV4;
1245         inp->inp_vflag |= INP_IPV6;
1246         INP_HASH_WLOCK(pcbinfo);
1247         error = in6_pcbconnect(inp, nam, td->td_ucred);
1248         INP_HASH_WUNLOCK(pcbinfo);
1249         /*
1250          * If connect succeeds, mark socket as connected. If
1251          * connect fails and socket is unbound, reset inp_vflag
1252          * field.
1253          */
1254         if (error == 0)
1255                 soisconnected(so);
1256         else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1257             inp->inp_lport == 0)
1258                 inp->inp_vflag = vflagsav;
1259 out:
1260         INP_WUNLOCK(inp);
1261         return (error);
1262 }
1263
1264 static void
1265 udp6_detach(struct socket *so)
1266 {
1267         struct inpcb *inp;
1268         struct inpcbinfo *pcbinfo;
1269         struct udpcb *up;
1270
1271         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1272         inp = sotoinpcb(so);
1273         KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1274
1275         INP_INFO_WLOCK(pcbinfo);
1276         INP_WLOCK(inp);
1277         up = intoudpcb(inp);
1278         KASSERT(up != NULL, ("%s: up == NULL", __func__));
1279         in_pcbdetach(inp);
1280         in_pcbfree(inp);
1281         INP_INFO_WUNLOCK(pcbinfo);
1282         udp_discardcb(up);
1283 }
1284
1285 static int
1286 udp6_disconnect(struct socket *so)
1287 {
1288         struct inpcb *inp;
1289         struct inpcbinfo *pcbinfo;
1290
1291         pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1292         inp = sotoinpcb(so);
1293         KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1294
1295         INP_WLOCK(inp);
1296 #ifdef INET
1297         if (inp->inp_vflag & INP_IPV4) {
1298                 struct pr_usrreqs *pru;
1299                 uint8_t nxt;
1300
1301                 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1302                     IPPROTO_UDP : IPPROTO_UDPLITE;
1303                 INP_WUNLOCK(inp);
1304                 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1305                 (void)(*pru->pru_disconnect)(so);
1306                 return (0);
1307         }
1308 #endif
1309
1310         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1311                 INP_WUNLOCK(inp);
1312                 return (ENOTCONN);
1313         }
1314
1315         INP_HASH_WLOCK(pcbinfo);
1316         in6_pcbdisconnect(inp);
1317         INP_HASH_WUNLOCK(pcbinfo);
1318         SOCK_LOCK(so);
1319         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
1320         SOCK_UNLOCK(so);
1321         INP_WUNLOCK(inp);
1322         return (0);
1323 }
1324
1325 static int
1326 udp6_send(struct socket *so, int flags, struct mbuf *m,
1327     struct sockaddr *addr, struct mbuf *control, struct thread *td)
1328 {
1329         int error;
1330
1331         if (addr) {
1332                 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1333                         error = EINVAL;
1334                         goto bad;
1335                 }
1336                 if (addr->sa_family != AF_INET6) {
1337                         error = EAFNOSUPPORT;
1338                         goto bad;
1339                 }
1340         }
1341
1342         return (udp6_output(so, flags, m, addr, control, td));
1343
1344 bad:
1345         if (control)
1346                 m_freem(control);
1347         m_freem(m);
1348         return (error);
1349 }
1350
1351 struct pr_usrreqs udp6_usrreqs = {
1352         .pru_abort =            udp6_abort,
1353         .pru_attach =           udp6_attach,
1354         .pru_bind =             udp6_bind,
1355         .pru_connect =          udp6_connect,
1356         .pru_control =          in6_control,
1357         .pru_detach =           udp6_detach,
1358         .pru_disconnect =       udp6_disconnect,
1359         .pru_peeraddr =         in6_mapped_peeraddr,
1360         .pru_send =             udp6_send,
1361         .pru_shutdown =         udp_shutdown,
1362         .pru_sockaddr =         in6_mapped_sockaddr,
1363         .pru_soreceive =        soreceive_dgram,
1364         .pru_sosend =           sosend_dgram,
1365         .pru_sosetlabel =       in_pcbsosetlabel,
1366         .pru_close =            udp6_close
1367 };