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