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