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