[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/udp6_usrreq.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

  1 /*-
  2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  3  * All rights reserved.
  4  *
  5  * Redistribution and use in source and binary forms, with or without
  6  * modification, are permitted provided that the following conditions
  7  * are met:
  8  * 1. Redistributions of source code must retain the above copyright
  9  *    notice, this list of conditions and the following disclaimer.
 10  * 2. Redistributions in binary form must reproduce the above copyright
 11  *    notice, this list of conditions and the following disclaimer in the
 12  *    documentation and/or other materials provided with the distribution.
 13  * 3. Neither the name of the project nor the names of its contributors
 14  *    may be used to endorse or promote products derived from this software
 15  *    without specific prior written permission.
 16  *
 17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
 18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
 21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 27  * SUCH DAMAGE.
 28  *
 29  *      $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
 30  *      $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
 31  */
 32 
 33 /*-
 34  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
 35  *      The Regents of the University of California.
 36  * All rights reserved.
 37  *
 38  * Redistribution and use in source and binary forms, with or without
 39  * modification, are permitted provided that the following conditions
 40  * are met:
 41  * 1. Redistributions of source code must retain the above copyright
 42  *    notice, this list of conditions and the following disclaimer.
 43  * 2. Redistributions in binary form must reproduce the above copyright
 44  *    notice, this list of conditions and the following disclaimer in the
 45  *    documentation and/or other materials provided with the distribution.
 46  * 4. Neither the name of the University nor the names of its contributors
 47  *    may be used to endorse or promote products derived from this software
 48  *    without specific prior written permission.
 49  *
 50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 60  * SUCH DAMAGE.
 61  *
 62  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
 63  */
 64 
 65 #include <sys/cdefs.h>
 66 __FBSDID("$FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.101 2008/12/02 21:37:28 bz Exp $");
 67 
 68 #include "opt_inet.h"
 69 #include "opt_inet6.h"
 70 #include "opt_ipsec.h"
 71 #include "opt_mac.h"
 72 
 73 #include <sys/param.h>
 74 #include <sys/jail.h>
 75 #include <sys/kernel.h>
 76 #include <sys/lock.h>
 77 #include <sys/mbuf.h>
 78 #include <sys/priv.h>
 79 #include <sys/proc.h>
 80 #include <sys/protosw.h>
 81 #include <sys/signalvar.h>
 82 #include <sys/socket.h>
 83 #include <sys/socketvar.h>
 84 #include <sys/sx.h>
 85 #include <sys/sysctl.h>
 86 #include <sys/syslog.h>
 87 #include <sys/systm.h>
 88 #include <sys/vimage.h>
 89 
 90 #include <net/if.h>
 91 #include <net/if_types.h>
 92 #include <net/route.h>
 93 
 94 #include <netinet/in.h>
 95 #include <netinet/in_pcb.h>
 96 #include <netinet/in_systm.h>
 97 #include <netinet/in_var.h>
 98 #include <netinet/ip.h>
 99 #include <netinet/ip_icmp.h>
100 #include <netinet/ip6.h>
101 #include <netinet/icmp_var.h>
102 #include <netinet/icmp6.h>
103 #include <netinet/ip_var.h>
104 #include <netinet/udp.h>
105 #include <netinet/udp_var.h>
106 #include <netinet/vinet.h>
107 
108 #include <netinet6/ip6protosw.h>
109 #include <netinet6/ip6_var.h>
110 #include <netinet6/in6_pcb.h>
111 #include <netinet6/udp6_var.h>
112 #include <netinet6/scope6_var.h>
113 #include <netinet6/vinet6.h>
114 
115 #ifdef IPSEC
116 #include <netipsec/ipsec.h>
117 #include <netipsec/ipsec6.h>
118 #endif /* IPSEC */
119 
120 #include <security/mac/mac_framework.h>
121 
122 /*
123  * UDP protocol implementation.
124  * Per RFC 768, August, 1980.
125  */
126 
127 extern struct protosw   inetsw[];
128 static void             udp6_detach(struct socket *so);
129 
130 static void
131 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
132     struct sockaddr_in6 *fromsa)
133 {
134         INIT_VNET_INET(inp->inp_vnet);
135         struct socket *so;
136         struct mbuf *opts;
137 
138         INP_LOCK_ASSERT(inp);
139 
140 #ifdef IPSEC
141         /* Check AH/ESP integrity. */
142         if (ipsec6_in_reject(n, inp)) {
143                 INIT_VNET_IPSEC(inp->inp_vnet);
144                 m_freem(n);
145                 V_ipsec6stat.in_polvio++;
146                 return;
147         }
148 #endif /* IPSEC */
149 #ifdef MAC
150         if (mac_inpcb_check_deliver(inp, n) != 0) {
151                 m_freem(n);
152                 return;
153         }
154 #endif
155         opts = NULL;
156         if (inp->in6p_flags & IN6P_CONTROLOPTS ||
157             inp->inp_socket->so_options & SO_TIMESTAMP)
158                 ip6_savecontrol(inp, n, &opts);
159         m_adj(n, off + sizeof(struct udphdr));
160 
161         so = inp->inp_socket;
162         SOCKBUF_LOCK(&so->so_rcv);
163         if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
164             opts) == 0) {
165                 SOCKBUF_UNLOCK(&so->so_rcv);
166                 m_freem(n);
167                 if (opts)
168                         m_freem(opts);
169                 V_udpstat.udps_fullsock++;
170         } else
171                 sorwakeup_locked(so);
172 }
173 
174 int
175 udp6_input(struct mbuf **mp, int *offp, int proto)
176 {
177         INIT_VNET_INET(curvnet);
178         INIT_VNET_INET6(curvnet);
179         struct mbuf *m = *mp;
180         struct ip6_hdr *ip6;
181         struct udphdr *uh;
182         struct inpcb *inp;
183         int off = *offp;
184         int plen, ulen;
185         struct sockaddr_in6 fromsa;
186 
187         ip6 = mtod(m, struct ip6_hdr *);
188 
189         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
190                 /* XXX send icmp6 host/port unreach? */
191                 m_freem(m);
192                 return (IPPROTO_DONE);
193         }
194 
195 #ifndef PULLDOWN_TEST
196         IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
197         ip6 = mtod(m, struct ip6_hdr *);
198         uh = (struct udphdr *)((caddr_t)ip6 + off);
199 #else
200         IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
201         if (!uh)
202                 return (IPPROTO_DONE);
203 #endif
204 
205         V_udpstat.udps_ipackets++;
206 
207         /*
208          * Destination port of 0 is illegal, based on RFC768.
209          */
210         if (uh->uh_dport == 0)
211                 goto badunlocked;
212 
213         plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
214         ulen = ntohs((u_short)uh->uh_ulen);
215 
216         if (plen != ulen) {
217                 V_udpstat.udps_badlen++;
218                 goto badunlocked;
219         }
220 
221         /*
222          * Checksum extended UDP header and data.
223          */
224         if (uh->uh_sum == 0) {
225                 V_udpstat.udps_nosum++;
226                 goto badunlocked;
227         }
228         if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
229                 V_udpstat.udps_badsum++;
230                 goto badunlocked;
231         }
232 
233         /*
234          * Construct sockaddr format source address.
235          */
236         init_sin6(&fromsa, m);
237         fromsa.sin6_port = uh->uh_sport;
238 
239         INP_INFO_RLOCK(&V_udbinfo);
240         if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
241                 struct inpcb *last;
242 
243                 /*
244                  * In the event that laddr should be set to the link-local
245                  * address (this happens in RIPng), the multicast address
246                  * specified in the received packet will not match laddr.  To
247                  * handle this situation, matching is relaxed if the
248                  * receiving interface is the same as one specified in the
249                  * socket and if the destination multicast address matches
250                  * one of the multicast groups specified in the socket.
251                  */
252 
253                 /*
254                  * KAME note: traditionally we dropped udpiphdr from mbuf
255                  * here.  We need udphdr for IPsec processing so we do that
256                  * later.
257                  */
258                 last = NULL;
259                 LIST_FOREACH(inp, &V_udb, inp_list) {
260                         if ((inp->inp_vflag & INP_IPV6) == 0)
261                                 continue;
262                         if (inp->in6p_lport != uh->uh_dport)
263                                 continue;
264                         /*
265                          * XXX: Do not check source port of incoming datagram
266                          * unless inp_connect() has been called to bind the
267                          * fport part of the 4-tuple; the source could be
268                          * trying to talk to us with an ephemeral port.
269                          */
270                         if (inp->inp_fport != 0 &&
271                             inp->inp_fport != uh->uh_sport)
272                                 continue;
273                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
274                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
275                                                         &ip6->ip6_dst))
276                                         continue;
277                         }
278                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
279                                 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
280                                                         &ip6->ip6_src) ||
281                                     inp->in6p_fport != uh->uh_sport)
282                                         continue;
283                         }
284 
285                         if (last != NULL) {
286                                 struct mbuf *n;
287 
288                                 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
289                                         INP_RLOCK(last);
290                                         udp6_append(last, n, off, &fromsa);
291                                         INP_RUNLOCK(last);
292                                 }
293                         }
294                         last = inp;
295                         /*
296                          * Don't look for additional matches if this one does
297                          * not have either the SO_REUSEPORT or SO_REUSEADDR
298                          * socket options set.  This heuristic avoids
299                          * searching through all pcbs in the common case of a
300                          * non-shared port.  It assumes that an application
301                          * will never clear these options after setting them.
302                          */
303                         if ((last->inp_socket->so_options &
304                              (SO_REUSEPORT|SO_REUSEADDR)) == 0)
305                                 break;
306                 }
307 
308                 if (last == NULL) {
309                         /*
310                          * No matching pcb found; discard datagram.  (No need
311                          * to send an ICMP Port Unreachable for a broadcast
312                          * or multicast datgram.)
313                          */
314                         V_udpstat.udps_noport++;
315                         V_udpstat.udps_noportmcast++;
316                         goto badheadlocked;
317                 }
318                 INP_RLOCK(last);
319                 INP_INFO_RUNLOCK(&V_udbinfo);
320                 udp6_append(last, m, off, &fromsa);
321                 INP_RUNLOCK(last);
322                 return (IPPROTO_DONE);
323         }
324         /*
325          * Locate pcb for datagram.
326          */
327         inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
328             &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
329         if (inp == NULL) {
330                 if (udp_log_in_vain) {
331                         char ip6bufs[INET6_ADDRSTRLEN];
332                         char ip6bufd[INET6_ADDRSTRLEN];
333 
334                         log(LOG_INFO,
335                             "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
336                             ip6_sprintf(ip6bufd, &ip6->ip6_dst),
337                             ntohs(uh->uh_dport),
338                             ip6_sprintf(ip6bufs, &ip6->ip6_src),
339                             ntohs(uh->uh_sport));
340                 }
341                 V_udpstat.udps_noport++;
342                 if (m->m_flags & M_MCAST) {
343                         printf("UDP6: M_MCAST is set in a unicast packet.\n");
344                         V_udpstat.udps_noportmcast++;
345                         goto badheadlocked;
346                 }
347                 INP_INFO_RUNLOCK(&V_udbinfo);
348                 if (V_udp_blackhole)
349                         goto badunlocked;
350                 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
351                         goto badunlocked;
352                 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
353                 return (IPPROTO_DONE);
354         }
355         INP_RLOCK(inp);
356         INP_INFO_RUNLOCK(&V_udbinfo);
357         udp6_append(inp, m, off, &fromsa);
358         INP_RUNLOCK(inp);
359         return (IPPROTO_DONE);
360 
361 badheadlocked:
362         INP_INFO_RUNLOCK(&V_udbinfo);
363 badunlocked:
364         if (m)
365                 m_freem(m);
366         return (IPPROTO_DONE);
367 }
368 
369 void
370 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
371 {
372         INIT_VNET_INET(curvnet);
373         struct udphdr uh;
374         struct ip6_hdr *ip6;
375         struct mbuf *m;
376         int off = 0;
377         struct ip6ctlparam *ip6cp = NULL;
378         const struct sockaddr_in6 *sa6_src = NULL;
379         void *cmdarg;
380         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
381         struct udp_portonly {
382                 u_int16_t uh_sport;
383                 u_int16_t uh_dport;
384         } *uhp;
385 
386         if (sa->sa_family != AF_INET6 ||
387             sa->sa_len != sizeof(struct sockaddr_in6))
388                 return;
389 
390         if ((unsigned)cmd >= PRC_NCMDS)
391                 return;
392         if (PRC_IS_REDIRECT(cmd))
393                 notify = in6_rtchange, d = NULL;
394         else if (cmd == PRC_HOSTDEAD)
395                 d = NULL;
396         else if (inet6ctlerrmap[cmd] == 0)
397                 return;
398 
399         /* if the parameter is from icmp6, decode it. */
400         if (d != NULL) {
401                 ip6cp = (struct ip6ctlparam *)d;
402                 m = ip6cp->ip6c_m;
403                 ip6 = ip6cp->ip6c_ip6;
404                 off = ip6cp->ip6c_off;
405                 cmdarg = ip6cp->ip6c_cmdarg;
406                 sa6_src = ip6cp->ip6c_src;
407         } else {
408                 m = NULL;
409                 ip6 = NULL;
410                 cmdarg = NULL;
411                 sa6_src = &sa6_any;
412         }
413 
414         if (ip6) {
415                 /*
416                  * XXX: We assume that when IPV6 is non NULL,
417                  * M and OFF are valid.
418                  */
419 
420                 /* Check if we can safely examine src and dst ports. */
421                 if (m->m_pkthdr.len < off + sizeof(*uhp))
422                         return;
423 
424                 bzero(&uh, sizeof(uh));
425                 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
426 
427                 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
428                     (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
429                     cmdarg, notify);
430         } else
431                 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
432                     (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
433 }
434 
435 static int
436 udp6_getcred(SYSCTL_HANDLER_ARGS)
437 {
438         INIT_VNET_INET(curvnet);
439         INIT_VNET_INET6(curvnet);
440         struct xucred xuc;
441         struct sockaddr_in6 addrs[2];
442         struct inpcb *inp;
443         int error;
444 
445         error = priv_check(req->td, PRIV_NETINET_GETCRED);
446         if (error)
447                 return (error);
448 
449         if (req->newlen != sizeof(addrs))
450                 return (EINVAL);
451         if (req->oldlen != sizeof(struct xucred))
452                 return (EINVAL);
453         error = SYSCTL_IN(req, addrs, sizeof(addrs));
454         if (error)
455                 return (error);
456         if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
457             (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
458                 return (error);
459         }
460         INP_INFO_RLOCK(&V_udbinfo);
461         inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
462             addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
463             NULL);
464         if (inp != NULL) {
465                 INP_RLOCK(inp);
466                 INP_INFO_RUNLOCK(&V_udbinfo);
467                 if (inp->inp_socket == NULL)
468                         error = ENOENT;
469                 if (error == 0)
470                         error = cr_canseesocket(req->td->td_ucred,
471                             inp->inp_socket);
472                 if (error == 0)
473                         cru2x(inp->inp_cred, &xuc);
474                 INP_RUNLOCK(inp);
475         } else {
476                 INP_INFO_RUNLOCK(&V_udbinfo);
477                 error = ENOENT;
478         }
479         if (error == 0)
480                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
481         return (error);
482 }
483 
484 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
485     0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
486 
487 static int
488 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
489     struct mbuf *control, struct thread *td)
490 {
491         INIT_VNET_INET(curvnet);
492         INIT_VNET_INET6(curvnet);
493         u_int32_t ulen = m->m_pkthdr.len;
494         u_int32_t plen = sizeof(struct udphdr) + ulen;
495         struct ip6_hdr *ip6;
496         struct udphdr *udp6;
497         struct in6_addr *laddr, *faddr;
498         struct sockaddr_in6 *sin6 = NULL;
499         struct ifnet *oifp = NULL;
500         int scope_ambiguous = 0;
501         u_short fport;
502         int error = 0;
503         struct ip6_pktopts *optp, opt;
504         int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
505         int flags;
506         struct sockaddr_in6 tmp;
507 
508         INP_WLOCK_ASSERT(inp);
509 
510         if (addr6) {
511                 /* addr6 has been validated in udp6_send(). */
512                 sin6 = (struct sockaddr_in6 *)addr6;
513 
514                 /* protect *sin6 from overwrites */
515                 tmp = *sin6;
516                 sin6 = &tmp;
517 
518                 /*
519                  * Application should provide a proper zone ID or the use of
520                  * default zone IDs should be enabled.  Unfortunately, some
521                  * applications do not behave as it should, so we need a
522                  * workaround.  Even if an appropriate ID is not determined,
523                  * we'll see if we can determine the outgoing interface.  If we
524                  * can, determine the zone ID based on the interface below.
525                  */
526                 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
527                         scope_ambiguous = 1;
528                 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
529                         return (error);
530         }
531 
532         if (control) {
533                 if ((error = ip6_setpktopts(control, &opt,
534                     inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
535                         goto release;
536                 optp = &opt;
537         } else
538                 optp = inp->in6p_outputopts;
539 
540         if (sin6) {
541                 faddr = &sin6->sin6_addr;
542 
543                 /*
544                  * IPv4 version of udp_output calls in_pcbconnect in this case,
545                  * which needs splnet and affects performance.
546                  * Since we saw no essential reason for calling in_pcbconnect,
547                  * we get rid of such kind of logic, and call in6_selectsrc
548                  * and in6_pcbsetport in order to fill in the local address
549                  * and the local port.
550                  */
551                 if (sin6->sin6_port == 0) {
552                         error = EADDRNOTAVAIL;
553                         goto release;
554                 }
555 
556                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
557                         /* how about ::ffff:0.0.0.0 case? */
558                         error = EISCONN;
559                         goto release;
560                 }
561 
562                 fport = sin6->sin6_port; /* allow 0 port */
563 
564                 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
565                         if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) {
566                                 /*
567                                  * I believe we should explicitly discard the
568                                  * packet when mapped addresses are disabled,
569                                  * rather than send the packet as an IPv6 one.
570                                  * If we chose the latter approach, the packet
571                                  * might be sent out on the wire based on the
572                                  * default route, the situation which we'd
573                                  * probably want to avoid.
574                                  * (20010421 jinmei@kame.net)
575                                  */
576                                 error = EINVAL;
577                                 goto release;
578                         }
579                         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
580                             !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
581                                 /*
582                                  * when remote addr is an IPv4-mapped address,
583                                  * local addr should not be an IPv6 address,
584                                  * since you cannot determine how to map IPv6
585                                  * source address to IPv4.
586                                  */
587                                 error = EINVAL;
588                                 goto release;
589                         }
590 
591                         af = AF_INET;
592                 }
593 
594                 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
595                         laddr = in6_selectsrc(sin6, optp, inp, NULL,
596                             td->td_ucred, &oifp, &error);
597                         if (oifp && scope_ambiguous &&
598                             (error = in6_setscope(&sin6->sin6_addr,
599                             oifp, NULL))) {
600                                 goto release;
601                         }
602                 } else
603                         laddr = &inp->in6p_laddr;       /* XXX */
604                 if (laddr == NULL) {
605                         if (error == 0)
606                                 error = EADDRNOTAVAIL;
607                         goto release;
608                 }
609                 if (inp->in6p_lport == 0 &&
610                     (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
611                         goto release;
612         } else {
613                 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
614                         error = ENOTCONN;
615                         goto release;
616                 }
617                 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
618                         if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) {
619                                 /*
620                                  * XXX: this case would happen when the
621                                  * application sets the V6ONLY flag after
622                                  * connecting the foreign address.
623                                  * Such applications should be fixed,
624                                  * so we bark here.
625                                  */
626                                 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
627                                     "option was set for a connected socket\n");
628                                 error = EINVAL;
629                                 goto release;
630                         } else
631                                 af = AF_INET;
632                 }
633                 laddr = &inp->in6p_laddr;
634                 faddr = &inp->in6p_faddr;
635                 fport = inp->in6p_fport;
636         }
637 
638         if (af == AF_INET)
639                 hlen = sizeof(struct ip);
640 
641         /*
642          * Calculate data length and get a mbuf
643          * for UDP and IP6 headers.
644          */
645         M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
646         if (m == 0) {
647                 error = ENOBUFS;
648                 goto release;
649         }
650 
651         /*
652          * Stuff checksum and output datagram.
653          */
654         udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
655         udp6->uh_sport = inp->in6p_lport; /* lport is always set in the PCB */
656         udp6->uh_dport = fport;
657         if (plen <= 0xffff)
658                 udp6->uh_ulen = htons((u_short)plen);
659         else
660                 udp6->uh_ulen = 0;
661         udp6->uh_sum = 0;
662 
663         switch (af) {
664         case AF_INET6:
665                 ip6 = mtod(m, struct ip6_hdr *);
666                 ip6->ip6_flow   = inp->in6p_flowinfo & IPV6_FLOWINFO_MASK;
667                 ip6->ip6_vfc    &= ~IPV6_VERSION_MASK;
668                 ip6->ip6_vfc    |= IPV6_VERSION;
669 #if 0                           /* ip6_plen will be filled in ip6_output. */
670                 ip6->ip6_plen   = htons((u_short)plen);
671 #endif
672                 ip6->ip6_nxt    = IPPROTO_UDP;
673                 ip6->ip6_hlim   = in6_selecthlim(inp, NULL);
674                 ip6->ip6_src    = *laddr;
675                 ip6->ip6_dst    = *faddr;
676 
677                 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
678                                 sizeof(struct ip6_hdr), plen)) == 0) {
679                         udp6->uh_sum = 0xffff;
680                 }
681 
682                 flags = 0;
683 
684                 V_udpstat.udps_opackets++;
685                 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
686                     NULL, inp);
687                 break;
688         case AF_INET:
689                 error = EAFNOSUPPORT;
690                 goto release;
691         }
692         goto releaseopt;
693 
694 release:
695         m_freem(m);
696 
697 releaseopt:
698         if (control) {
699                 ip6_clearpktopts(&opt, -1);
700                 m_freem(control);
701         }
702         return (error);
703 }
704 
705 static void
706 udp6_abort(struct socket *so)
707 {
708         INIT_VNET_INET(so->so_vnet);
709         struct inpcb *inp;
710 
711         inp = sotoinpcb(so);
712         KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
713 
714 #ifdef INET
715         if (inp->inp_vflag & INP_IPV4) {
716                 struct pr_usrreqs *pru;
717 
718                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
719                 (*pru->pru_abort)(so);
720                 return;
721         }
722 #endif
723 
724         INP_INFO_WLOCK(&V_udbinfo);
725         INP_WLOCK(inp);
726         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
727                 in6_pcbdisconnect(inp);
728                 inp->in6p_laddr = in6addr_any;
729                 soisdisconnected(so);
730         }
731         INP_WUNLOCK(inp);
732         INP_INFO_WUNLOCK(&V_udbinfo);
733 }
734 
735 static int
736 udp6_attach(struct socket *so, int proto, struct thread *td)
737 {
738         INIT_VNET_INET(so->so_vnet);
739         struct inpcb *inp;
740         int error;
741 
742         inp = sotoinpcb(so);
743         KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
744 
745         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
746                 error = soreserve(so, udp_sendspace, udp_recvspace);
747                 if (error)
748                         return (error);
749         }
750         INP_INFO_WLOCK(&V_udbinfo);
751         error = in_pcballoc(so, &V_udbinfo);
752         if (error) {
753                 INP_INFO_WUNLOCK(&V_udbinfo);
754                 return (error);
755         }
756         inp = (struct inpcb *)so->so_pcb;
757         INP_INFO_WUNLOCK(&V_udbinfo);
758         inp->inp_vflag |= INP_IPV6;
759         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
760                 inp->inp_vflag |= INP_IPV4;
761         inp->in6p_hops = -1;    /* use kernel default */
762         inp->in6p_cksum = -1;   /* just to be sure */
763         /*
764          * XXX: ugly!!
765          * IPv4 TTL initialization is necessary for an IPv6 socket as well,
766          * because the socket may be bound to an IPv6 wildcard address,
767          * which may match an IPv4-mapped IPv6 address.
768          */
769         inp->inp_ip_ttl = V_ip_defttl;
770         INP_WUNLOCK(inp);
771         return (0);
772 }
773 
774 static int
775 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
776 {
777         INIT_VNET_INET(so->so_vnet);
778         struct inpcb *inp;
779         int error;
780 
781         inp = sotoinpcb(so);
782         KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
783 
784         INP_INFO_WLOCK(&V_udbinfo);
785         INP_WLOCK(inp);
786         inp->inp_vflag &= ~INP_IPV4;
787         inp->inp_vflag |= INP_IPV6;
788         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
789                 struct sockaddr_in6 *sin6_p;
790 
791                 sin6_p = (struct sockaddr_in6 *)nam;
792 
793                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
794                         inp->inp_vflag |= INP_IPV4;
795                 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
796                         struct sockaddr_in sin;
797 
798                         in6_sin6_2_sin(&sin, sin6_p);
799                         inp->inp_vflag |= INP_IPV4;
800                         inp->inp_vflag &= ~INP_IPV6;
801                         error = in_pcbbind(inp, (struct sockaddr *)&sin,
802                             td->td_ucred);
803                         goto out;
804                 }
805         }
806 
807         error = in6_pcbbind(inp, nam, td->td_ucred);
808 out:
809         INP_WUNLOCK(inp);
810         INP_INFO_WUNLOCK(&V_udbinfo);
811         return (error);
812 }
813 
814 static void
815 udp6_close(struct socket *so)
816 {
817         INIT_VNET_INET(so->so_vnet);
818         struct inpcb *inp;
819 
820         inp = sotoinpcb(so);
821         KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
822 
823 #ifdef INET
824         if (inp->inp_vflag & INP_IPV4) {
825                 struct pr_usrreqs *pru;
826 
827                 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
828                 (*pru->pru_disconnect)(so);
829                 return;
830         }
831 #endif
832         INP_INFO_WLOCK(&V_udbinfo);
833         INP_WLOCK(inp);
834         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
835                 in6_pcbdisconnect(inp);
836                 inp->in6p_laddr = in6addr_any;
837                 soisdisconnected(so);
838         }
839         INP_WUNLOCK(inp);
840         INP_INFO_WUNLOCK(&V_udbinfo);
841 }
842 
843 static int
844 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
845 {
846         INIT_VNET_INET(so->so_vnet);
847         struct inpcb *inp;
848         int error;
849 
850         inp = sotoinpcb(so);
851         KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
852 
853         INP_INFO_WLOCK(&V_udbinfo);
854         INP_WLOCK(inp);
855         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
856                 struct sockaddr_in6 *sin6_p;
857 
858                 sin6_p = (struct sockaddr_in6 *)nam;
859                 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
860                         struct sockaddr_in sin;
861 
862                         if (inp->inp_faddr.s_addr != INADDR_ANY) {
863                                 error = EISCONN;
864                                 goto out;
865                         }
866                         in6_sin6_2_sin(&sin, sin6_p);
867                         if (td && jailed(td->td_ucred))
868                                 if (prison_remote_ip4(td->td_ucred,
869                                     &sin.sin_addr) != 0) {
870                                         error = EAFNOSUPPORT;
871                                         goto out;
872                                 }
873                         error = in_pcbconnect(inp, (struct sockaddr *)&sin,
874                             td->td_ucred);
875                         if (error == 0) {
876                                 inp->inp_vflag |= INP_IPV4;
877                                 inp->inp_vflag &= ~INP_IPV6;
878                                 soisconnected(so);
879                         }
880                         goto out;
881                 }
882         }
883         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
884                 error = EISCONN;
885                 goto out;
886         }
887         if (td && jailed(td->td_ucred)) {
888                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
889                 if (prison_remote_ip6(td->td_ucred, &sin6->sin6_addr) != 0) {
890                         error = EAFNOSUPPORT;
891                         goto out;
892                 }
893         }
894         error = in6_pcbconnect(inp, nam, td->td_ucred);
895         if (error == 0) {
896                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
897                         /* should be non mapped addr */
898                         inp->inp_vflag &= ~INP_IPV4;
899                         inp->inp_vflag |= INP_IPV6;
900                 }
901                 soisconnected(so);
902         }
903 out:
904         INP_WUNLOCK(inp);
905         INP_INFO_WUNLOCK(&V_udbinfo);
906         return (error);
907 }
908 
909 static void
910 udp6_detach(struct socket *so)
911 {
912         INIT_VNET_INET(so->so_vnet);
913         struct inpcb *inp;
914 
915         inp = sotoinpcb(so);
916         KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
917 
918         INP_INFO_WLOCK(&V_udbinfo);
919         INP_WLOCK(inp);
920         in_pcbdetach(inp);
921         in_pcbfree(inp);
922         INP_INFO_WUNLOCK(&V_udbinfo);
923 }
924 
925 static int
926 udp6_disconnect(struct socket *so)
927 {
928         INIT_VNET_INET(so->so_vnet);
929         struct inpcb *inp;
930         int error;
931 
932         inp = sotoinpcb(so);
933         KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
934 
935         INP_INFO_WLOCK(&V_udbinfo);
936         INP_WLOCK(inp);
<